arch/x86/kernel/module.c
Source file repositories/reference/linux-study-clean/arch/x86/kernel/module.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kernel/module.c- Extension
.c- Size
- 8217 bytes
- Lines
- 324
- Domain
- Architecture Layer
- Bucket
- arch/x86
- Inferred role
- Architecture Layer: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/moduleloader.hlinux/elf.hlinux/vmalloc.hlinux/fs.hlinux/string.hlinux/kernel.hlinux/kasan.hlinux/bug.hlinux/mm.hlinux/gfp.hlinux/jump_label.hlinux/random.hlinux/memory.hasm/text-patching.hasm/page.hasm/setup.hasm/unwind.h
Detected Declarations
function apply_relocatefunction __write_relocate_addfunction write_relocate_addfunction apply_relocate_addfunction clear_relocate_addfunction module_finalizefunction module_arch_cleanup
Annotated Snippet
switch (ELF32_R_TYPE(rel[i].r_info)) {
case R_386_32:
/* We add the value into the location given */
*location += sym->st_value;
break;
case R_386_PC32:
case R_386_PLT32:
/* Add the value, subtract its position */
*location += sym->st_value - (uint32_t)location;
break;
default:
pr_err("%s: Unknown relocation: %u\n",
me->name, ELF32_R_TYPE(rel[i].r_info));
return -ENOEXEC;
}
}
return 0;
}
#else /*X86_64*/
static int __write_relocate_add(Elf64_Shdr *sechdrs,
const char *strtab,
unsigned int symindex,
unsigned int relsec,
struct module *me,
void *(*write)(void *dest, const void *src, size_t len),
bool apply)
{
unsigned int i;
Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
Elf64_Sym *sym;
void *loc;
u64 val;
u64 zero = 0ULL;
DEBUGP("%s relocate section %u to %u\n",
apply ? "Applying" : "Clearing",
relsec, sechdrs[relsec].sh_info);
for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
size_t size;
/* This is where to make the change */
loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[i].r_offset;
/* This is the symbol it is referring to. Note that all
undefined symbols have been resolved. */
sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
+ ELF64_R_SYM(rel[i].r_info);
DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
(int)ELF64_R_TYPE(rel[i].r_info),
sym->st_value, rel[i].r_addend, (u64)loc);
val = sym->st_value + rel[i].r_addend;
switch (ELF64_R_TYPE(rel[i].r_info)) {
case R_X86_64_NONE:
continue; /* nothing to write */
case R_X86_64_64:
size = 8;
break;
case R_X86_64_32:
if (val != *(u32 *)&val)
goto overflow;
size = 4;
break;
case R_X86_64_32S:
if ((s64)val != *(s32 *)&val)
goto overflow;
size = 4;
break;
case R_X86_64_PC32:
case R_X86_64_PLT32:
val -= (u64)loc;
size = 4;
break;
case R_X86_64_PC64:
val -= (u64)loc;
size = 8;
break;
default:
pr_err("%s: Unknown rela relocation: %llu\n",
me->name, ELF64_R_TYPE(rel[i].r_info));
return -ENOEXEC;
}
if (apply) {
if (memcmp(loc, &zero, size)) {
pr_err("x86/modules: Invalid relocation target, existing value is nonzero for sec %u, idx %u, type %d, loc %lx, val %llx\n",
Annotation
- Immediate include surface: `linux/moduleloader.h`, `linux/elf.h`, `linux/vmalloc.h`, `linux/fs.h`, `linux/string.h`, `linux/kernel.h`, `linux/kasan.h`, `linux/bug.h`.
- Detected declarations: `function apply_relocate`, `function __write_relocate_add`, `function write_relocate_add`, `function apply_relocate_add`, `function clear_relocate_add`, `function module_finalize`, `function module_arch_cleanup`.
- Atlas domain: Architecture Layer / arch/x86.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.