Commit 18f7a1ae authored by Rusty Russell's avatar Rusty Russell Committed by Linus Torvalds

[PATCH] Modules 2/3: Use sh_addr instead of sh_offset

From: Richard Henderson <rth@twiddle.net>

Richard points out that we should be using sh_addr to hold the address,

The original patch used to overload sh_offset to a pointer to the
location of the section.  This uses sh_addr, which is more correct
and less surprising.
parent dfe2cbd1
......@@ -88,7 +88,7 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
Elf32_Shdr *symsec = sechdrs + symindex;
Elf32_Shdr *relsec = sechdrs + relindex;
Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
Elf32_Rel *rel = (void *)relsec->sh_offset;
Elf32_Rel *rel = (void *)relsec->sh_addr;
unsigned int i;
for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
......@@ -103,7 +103,7 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
return -ENOEXEC;
}
sym = ((Elf32_Sym *)symsec->sh_offset) + offset;
sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
if (!sym->st_value) {
printk(KERN_WARNING "%s: unknown symbol %s\n",
module->name, strtab + sym->st_name);
......@@ -118,7 +118,7 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
return -ENOEXEC;
}
loc = dstsec->sh_offset + rel->r_offset;
loc = dstsec->sh_addr + rel->r_offset;
switch (ELF32_R_TYPE(rel->r_info)) {
case R_ARM_ABS32:
......
......@@ -68,7 +68,7 @@ int apply_relocate(Elf32_Shdr *sechdrs,
struct module *me)
{
unsigned int i;
Elf32_Rel *rel = (void *)sechdrs[relsec].sh_offset;
Elf32_Rel *rel = (void *)sechdrs[relsec].sh_addr;
Elf32_Sym *sym;
uint32_t *location;
......@@ -76,10 +76,10 @@ int apply_relocate(Elf32_Shdr *sechdrs,
sechdrs[relsec].sh_info);
for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
/* This is where to make the change */
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_offset
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[i].r_offset;
/* This is the symbol it is referring to */
sym = (Elf32_Sym *)sechdrs[symindex].sh_offset
sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
+ ELF32_R_SYM(rel[i].r_info);
if (!sym->st_value) {
printk(KERN_WARNING "%s: Unknown symbol %s\n",
......
......@@ -176,7 +176,7 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
struct module *module)
{
unsigned int i;
Elf32_Rela *rela = (void *)sechdrs[relsec].sh_offset;
Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
Elf32_Sym *sym;
uint32_t *location;
uint32_t value;
......@@ -185,10 +185,10 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
sechdrs[relsec].sh_info);
for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
/* This is where to make the change */
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_offset
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rela[i].r_offset;
/* This is the symbol it is referring to */
sym = (Elf32_Sym *)sechdrs[symindex].sh_offset
sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
+ ELF32_R_SYM(rela[i].r_info);
if (!sym->st_value) {
printk(KERN_WARNING "%s: Unknown symbol %s\n",
......
......@@ -78,7 +78,7 @@ int apply_relocate(Elf_Shdr *sechdrs,
struct module *me)
{
unsigned int i;
ElfW(Rel) *rel = (void *)sechdrs[relsec].sh_offset;
ElfW(Rel) *rel = (void *)sechdrs[relsec].sh_addr;
ElfW(Sym) *sym;
ElfW(Addr) *location;
......@@ -86,10 +86,10 @@ int apply_relocate(Elf_Shdr *sechdrs,
sechdrs[relsec].sh_info);
for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
/* This is where to make the change */
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_offset
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[i].r_offset;
/* This is the symbol it is referring to */
sym = (ElfW(Sym) *)sechdrs[symindex].sh_offset
sym = (ElfW(Sym) *)sechdrs[symindex].sh_addr
+ ELFW(R_SYM)(rel[i].r_info);
if (!sym->st_value) {
printk(KERN_WARNING "%s: Unknown symbol %s\n",
......
......@@ -78,7 +78,7 @@ int apply_relocate(Elf_Shdr *sechdrs,
struct module *me)
{
unsigned int i;
ElfW(Rel) *rel = (void *)sechdrs[relsec].sh_offset;
ElfW(Rel) *rel = (void *)sechdrs[relsec].sh_addr;
ElfW(Sym) *sym;
ElfW(Addr) *location;
......@@ -86,10 +86,10 @@ int apply_relocate(Elf_Shdr *sechdrs,
sechdrs[relsec].sh_info);
for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
/* This is where to make the change */
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_offset
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[i].r_offset;
/* This is the symbol it is referring to */
sym = (ElfW(Sym) *)sechdrs[symindex].sh_offset
sym = (ElfW(Sym) *)sechdrs[symindex].sh_addr
+ ELFW(R_SYM)(rel[i].r_info);
if (!sym->st_value) {
printk(KERN_WARNING "%s: Unknown symbol %s\n",
......
......@@ -71,7 +71,7 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
struct module *me)
{
unsigned int i;
Elf32_Rela *rel = (void *)sechdrs[relsec].sh_offset;
Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
Elf32_Sym *sym;
u8 *location;
u32 *loc32;
......@@ -80,11 +80,11 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
Elf32_Addr v;
/* This is where to make the change */
location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_offset
location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[i].r_offset;
loc32 = (u32 *) location;
/* This is the symbol it is referring to */
sym = (Elf32_Sym *)sechdrs[symindex].sh_offset
sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
+ ELF32_R_SYM(rel[i].r_info);
if (!(v = sym->st_value)) {
printk(KERN_WARNING "%s: Unknown symbol %s\n",
......
......@@ -178,7 +178,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
struct module *me)
{
unsigned int i;
Elf64_Rela *rel = (void *)sechdrs[relsec].sh_offset;
Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
Elf64_Sym *sym;
u8 *location;
u32 *loc32;
......@@ -187,14 +187,14 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
Elf64_Addr v;
/* This is where to make the change */
location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_offset
location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[i].r_offset;
loc32 = (u32 *) location;
BUG_ON(((u64)location >> (u64)32) != (u64)0);
/* This is the symbol it is referring to */
sym = (Elf64_Sym *)sechdrs[symindex].sh_offset
sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
+ ELF64_R_SYM(rel[i].r_info);
if (!(v = sym->st_value)) {
printk(KERN_WARNING "%s: Unknown symbol %s\n",
......
......@@ -160,7 +160,7 @@ int apply_relocate_add (Elf32_Shdr *sechdrs, const char *strtab,
struct module *mod)
{
unsigned int i;
Elf32_Rela *rela = (void *)sechdrs[relsec].sh_offset;
Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
DEBUGP ("Applying relocate section %u to %u\n", relsec,
sechdrs[relsec].sh_info);
......@@ -168,11 +168,11 @@ int apply_relocate_add (Elf32_Shdr *sechdrs, const char *strtab,
for (i = 0; i < sechdrs[relsec].sh_size / sizeof (*rela); i++) {
/* This is where to make the change */
uint32_t *loc
= ((void *)sechdrs[sechdrs[relsec].sh_info].sh_offset
= ((void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rela[i].r_offset);
/* This is the symbol it is referring to */
Elf32_Sym *sym
= ((Elf32_Sym *)sechdrs[symindex].sh_offset
= ((Elf32_Sym *)sechdrs[symindex].sh_addr
+ ELF32_R_SYM (rela[i].r_info));
uint32_t val = sym->st_value;
......
......@@ -49,7 +49,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
struct module *me)
{
unsigned int i;
Elf64_Rela *rel = (void *)sechdrs[relsec].sh_offset;
Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
Elf64_Sym *sym;
void *loc;
u64 val;
......@@ -58,11 +58,11 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
sechdrs[relsec].sh_info);
for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
/* This is where to make the change */
loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_offset
loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
+ rel[i].r_offset;
/* This is the symbol it is referring to */
sym = (Elf64_Sym *)sechdrs[symindex].sh_offset
sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
+ ELF64_R_SYM(rel[i].r_info);
if (!sym->st_value) {
printk(KERN_WARNING "%s: Unknown symbol %s\n",
......
......@@ -94,7 +94,7 @@ static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
const char *name)
{
unsigned int i;
Elf_Sym *sym = (void *)sechdrs[symindex].sh_offset;
Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
/* Search (defined) internal symbols first. */
for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
......@@ -810,7 +810,7 @@ static int handle_section(const char *name,
struct module *mod)
{
int ret;
const char *strtab = (char *)sechdrs[strindex].sh_offset;
const char *strtab = (char *)sechdrs[strindex].sh_addr;
switch (sechdrs[i].sh_type) {
case SHT_REL:
......@@ -852,7 +852,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
/* First simplify defined symbols, so if they become the
"answer" to undefined symbols, copying their st_value us
correct. */
for (sym = (void *)sechdrs[symindex].sh_offset, i = 0;
for (sym = (void *)sechdrs[symindex].sh_addr, i = 0;
i < sechdrs[symindex].sh_size / sizeof(Elf_Sym);
i++) {
switch (sym[i].st_shndx) {
......@@ -874,20 +874,20 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
default:
sym[i].st_value
= (unsigned long)
(sechdrs[sym[i].st_shndx].sh_offset
(sechdrs[sym[i].st_shndx].sh_addr
+ sym[i].st_value);
}
}
/* Now try to resolve undefined symbols */
for (sym = (void *)sechdrs[symindex].sh_offset, i = 0;
for (sym = (void *)sechdrs[symindex].sh_addr, i = 0;
i < sechdrs[symindex].sh_size / sizeof(Elf_Sym);
i++) {
if (sym[i].st_shndx == SHN_UNDEF) {
/* Look for symbol */
struct kernel_symbol_group *ksg = NULL;
const char *strtab
= (char *)sechdrs[strindex].sh_offset;
= (char *)sechdrs[strindex].sh_addr;
sym[i].st_value
= find_symbol_internal(sechdrs,
......@@ -984,6 +984,10 @@ static struct module *load_module(void *umod,
/* Find where important sections are */
for (i = 1; i < hdr->e_shnum; i++) {
/* Mark all sections sh_addr with their address in the
temporary image. */
sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
if (sechdrs[i].sh_type == SHT_SYMTAB) {
/* Internal symbols */
DEBUGP("Symbol table in section %u\n", i);
......@@ -1034,7 +1038,7 @@ static struct module *load_module(void *umod,
err = -ENOEXEC;
goto free_hdr;
}
mod = (void *)hdr + sechdrs[modindex].sh_offset;
mod = (void *)sechdrs[modindex].sh_addr;
/* Now copy in args */
err = strlen_user(uargs);
......@@ -1094,7 +1098,7 @@ static struct module *load_module(void *umod,
memset(ptr, 0, mod->init_size);
mod->module_init = ptr;
/* Transfer each section which requires ALLOC, and set sh_offset
/* Transfer each section which requires ALLOC, and set sh_addr
fields to absolute addresses. */
used.core_size = 0;
used.init_size = 0;
......@@ -1104,12 +1108,10 @@ static struct module *load_module(void *umod,
hdr, &sechdrs[i], mod, &used);
if (IS_ERR(ptr))
goto cleanup;
sechdrs[i].sh_offset = (unsigned long)ptr;
sechdrs[i].sh_addr = (unsigned long)ptr;
/* Have we just copied __this_module across? */
if (i == modindex)
mod = ptr;
} else {
sechdrs[i].sh_offset += (unsigned long)hdr;
}
}
/* Don't use more than we allocated! */
......@@ -1128,7 +1130,7 @@ static struct module *load_module(void *umod,
if (exportindex) {
mod->symbols.num_syms = (sechdrs[exportindex].sh_size
/ sizeof(*mod->symbols.syms));
mod->symbols.syms = (void *)sechdrs[exportindex].sh_offset;
mod->symbols.syms = (void *)sechdrs[exportindex].sh_addr;
}
/* Set up exception table */
......@@ -1137,7 +1139,7 @@ static struct module *load_module(void *umod,
mod->extable.num_entries = (sechdrs[exindex].sh_size
/ sizeof(struct
exception_table_entry));
mod->extable.entry = (void *)sechdrs[exindex].sh_offset;
mod->extable.entry = (void *)sechdrs[exindex].sh_addr;
}
/* Now handle each section. */
......@@ -1149,9 +1151,9 @@ static struct module *load_module(void *umod,
}
#ifdef CONFIG_KALLSYMS
mod->symtab = (void *)sechdrs[symindex].sh_offset;
mod->symtab = (void *)sechdrs[symindex].sh_addr;
mod->num_syms = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
mod->strtab = (void *)sechdrs[strindex].sh_offset;
mod->strtab = (void *)sechdrs[strindex].sh_addr;
#endif
err = module_finalize(hdr, sechdrs, mod);
if (err < 0)
......@@ -1161,16 +1163,16 @@ static struct module *load_module(void *umod,
if (obsparmindex) {
err = obsolete_params(mod->name, mod->args,
(struct obsolete_modparm *)
sechdrs[obsparmindex].sh_offset,
sechdrs[obsparmindex].sh_addr,
sechdrs[obsparmindex].sh_size
/ sizeof(struct obsolete_modparm),
sechdrs, symindex,
(char *)sechdrs[strindex].sh_offset);
(char *)sechdrs[strindex].sh_addr);
} else {
/* Size of section 0 is 0, so this works well if no params */
err = parse_args(mod->name, mod->args,
(struct kernel_param *)
sechdrs[setupindex].sh_offset,
sechdrs[setupindex].sh_addr,
sechdrs[setupindex].sh_size
/ sizeof(struct kernel_param),
NULL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment