Commit 5b24c071 authored by Sam Ravnborg's avatar Sam Ravnborg

kbuild: code refactoring in modpost

Split a too long function up in smaller bits to make
prgram logic easier to follow.
A few related changes done due to parameter
changes.

No functional changes.
Signed-off-by: default avatarSam Ravnborg <sam@ravnborg.org>
parent 9ad21c3f
...@@ -937,19 +937,19 @@ static void warn_sec_mismatch(const char *modname, const char *fromsec, ...@@ -937,19 +937,19 @@ static void warn_sec_mismatch(const char *modname, const char *fromsec,
} }
static unsigned int *reloc_location(struct elf_info *elf, static unsigned int *reloc_location(struct elf_info *elf,
int rsection, Elf_Rela *r) Elf_Shdr *sechdr, Elf_Rela *r)
{ {
Elf_Shdr *sechdrs = elf->sechdrs; Elf_Shdr *sechdrs = elf->sechdrs;
int section = sechdrs[rsection].sh_info; int section = sechdr->sh_info;
return (void *)elf->hdr + sechdrs[section].sh_offset + return (void *)elf->hdr + sechdrs[section].sh_offset +
(r->r_offset - sechdrs[section].sh_addr); (r->r_offset - sechdrs[section].sh_addr);
} }
static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r) static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
{ {
unsigned int r_typ = ELF_R_TYPE(r->r_info); unsigned int r_typ = ELF_R_TYPE(r->r_info);
unsigned int *location = reloc_location(elf, rsection, r); unsigned int *location = reloc_location(elf, sechdr, r);
switch (r_typ) { switch (r_typ) {
case R_386_32: case R_386_32:
...@@ -965,7 +965,7 @@ static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r) ...@@ -965,7 +965,7 @@ static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
return 0; return 0;
} }
static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r) static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
{ {
unsigned int r_typ = ELF_R_TYPE(r->r_info); unsigned int r_typ = ELF_R_TYPE(r->r_info);
...@@ -978,8 +978,8 @@ static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r) ...@@ -978,8 +978,8 @@ static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
case R_ARM_PC24: case R_ARM_PC24:
/* From ARM ABI: ((S + A) | T) - P */ /* From ARM ABI: ((S + A) | T) - P */
r->r_addend = (int)(long)(elf->hdr + r->r_addend = (int)(long)(elf->hdr +
elf->sechdrs[rsection].sh_offset + sechdr->sh_offset +
(r->r_offset - elf->sechdrs[rsection].sh_addr)); (r->r_offset - sechdr->sh_addr));
break; break;
default: default:
return 1; return 1;
...@@ -987,10 +987,10 @@ static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r) ...@@ -987,10 +987,10 @@ static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
return 0; return 0;
} }
static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r) static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
{ {
unsigned int r_typ = ELF_R_TYPE(r->r_info); unsigned int r_typ = ELF_R_TYPE(r->r_info);
unsigned int *location = reloc_location(elf, rsection, r); unsigned int *location = reloc_location(elf, sechdr, r);
unsigned int inst; unsigned int inst;
if (r_typ == R_MIPS_HI16) if (r_typ == R_MIPS_HI16)
...@@ -1010,44 +1010,29 @@ static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r) ...@@ -1010,44 +1010,29 @@ static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
return 0; return 0;
} }
/** static void section_rela(const char *modname, struct elf_info *elf,
* A module includes a number of sections that are discarded Elf_Shdr *sechdr, int section(const char *),
* either when loaded or when used as built-in.
* For loaded modules all functions marked __init and all data
* marked __initdata will be discarded when the module has been intialized.
* Likewise for modules used built-in the sections marked __exit
* are discarded because __exit marked function are supposed to be called
* only when a moduel is unloaded which never happes for built-in modules.
* The check_sec_ref() function traverses all relocation records
* to find all references to a section that reference a section that will
* be discarded and warns about it.
**/
static void check_sec_ref(struct module *mod, const char *modname,
struct elf_info *elf,
int section(const char *),
int section_ref_ok(const char *)) int section_ref_ok(const char *))
{ {
int i;
Elf_Sym *sym; Elf_Sym *sym;
Elf_Rela *rela;
Elf_Rela r;
unsigned int r_sym;
const char *fromsec;
const char * tosec;
Elf_Ehdr *hdr = elf->hdr; Elf_Ehdr *hdr = elf->hdr;
Elf_Shdr *sechdrs = elf->sechdrs; Elf_Rela *start = (void *)hdr + sechdr->sh_offset;
Elf_Rela *stop = (void *)start + sechdr->sh_size;
const char *secstrings = (void *)hdr + const char *secstrings = (void *)hdr +
sechdrs[hdr->e_shstrndx].sh_offset; elf->sechdrs[hdr->e_shstrndx].sh_offset;
/* Walk through all sections */ fromsec = secstrings + sechdr->sh_name;
for (i = 0; i < hdr->e_shnum; i++) { fromsec += strlen(".rela");
const char *name = secstrings + sechdrs[i].sh_name; /* if from section (name) is know good then skip it */
const char *secname; if (section_ref_ok(fromsec))
Elf_Rela r; return;
unsigned int r_sym;
/* We want to process only relocation sections and not .init */
if (sechdrs[i].sh_type == SHT_RELA) {
Elf_Rela *rela;
Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
Elf_Rela *stop = (void *)start + sechdrs[i].sh_size;
name += strlen(".rela");
if (section_ref_ok(name))
continue;
for (rela = start; rela < stop; rela++) { for (rela = start; rela < stop; rela++) {
r.r_offset = TO_NATIVE(rela->r_offset); r.r_offset = TO_NATIVE(rela->r_offset);
...@@ -1072,19 +1057,36 @@ static void check_sec_ref(struct module *mod, const char *modname, ...@@ -1072,19 +1057,36 @@ static void check_sec_ref(struct module *mod, const char *modname,
if (sym->st_shndx >= SHN_LORESERVE) if (sym->st_shndx >= SHN_LORESERVE)
continue; continue;
secname = secstrings + tosec = secstrings +
sechdrs[sym->st_shndx].sh_name; elf->sechdrs[sym->st_shndx].sh_name;
if (section(secname)) if (section(tosec))
warn_sec_mismatch(modname, name, warn_sec_mismatch(modname, fromsec, elf, sym, r);
elf, sym, r);
} }
} else if (sechdrs[i].sh_type == SHT_REL) { }
static void section_rel(const char *modname, struct elf_info *elf,
Elf_Shdr *sechdr, int section(const char *),
int section_ref_ok(const char *))
{
Elf_Sym *sym;
Elf_Rel *rel; Elf_Rel *rel;
Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset; Elf_Rela r;
Elf_Rel *stop = (void *)start + sechdrs[i].sh_size; unsigned int r_sym;
name += strlen(".rel"); const char *fromsec;
if (section_ref_ok(name)) const char * tosec;
continue;
Elf_Ehdr *hdr = elf->hdr;
Elf_Rel *start = (void *)hdr + sechdr->sh_offset;
Elf_Rel *stop = (void *)start + sechdr->sh_size;
const char *secstrings = (void *)hdr +
elf->sechdrs[hdr->e_shstrndx].sh_offset;
fromsec = secstrings + sechdr->sh_name;
fromsec += strlen(".rel");
/* if from section (name) is know good then skip it */
if (section_ref_ok(fromsec))
return;
for (rel = start; rel < stop; rel++) { for (rel = start; rel < stop; rel++) {
r.r_offset = TO_NATIVE(rel->r_offset); r.r_offset = TO_NATIVE(rel->r_offset);
...@@ -1106,15 +1108,15 @@ static void check_sec_ref(struct module *mod, const char *modname, ...@@ -1106,15 +1108,15 @@ static void check_sec_ref(struct module *mod, const char *modname,
r.r_addend = 0; r.r_addend = 0;
switch (hdr->e_machine) { switch (hdr->e_machine) {
case EM_386: case EM_386:
if (addend_386_rel(elf, i, &r)) if (addend_386_rel(elf, sechdr, &r))
continue; continue;
break; break;
case EM_ARM: case EM_ARM:
if (addend_arm_rel(elf, i, &r)) if (addend_arm_rel(elf, sechdr, &r))
continue; continue;
break; break;
case EM_MIPS: case EM_MIPS:
if (addend_mips_rel(elf, i, &r)) if (addend_mips_rel(elf, sechdr, &r))
continue; continue;
break; break;
} }
...@@ -1123,13 +1125,43 @@ static void check_sec_ref(struct module *mod, const char *modname, ...@@ -1123,13 +1125,43 @@ static void check_sec_ref(struct module *mod, const char *modname,
if (sym->st_shndx >= SHN_LORESERVE) if (sym->st_shndx >= SHN_LORESERVE)
continue; continue;
secname = secstrings + tosec = secstrings +
sechdrs[sym->st_shndx].sh_name; elf->sechdrs[sym->st_shndx].sh_name;
if (section(secname)) if (section(tosec))
warn_sec_mismatch(modname, name, warn_sec_mismatch(modname, fromsec, elf, sym, r);
elf, sym, r);
}
} }
}
/**
* A module includes a number of sections that are discarded
* either when loaded or when used as built-in.
* For loaded modules all functions marked __init and all data
* marked __initdata will be discarded when the module has been intialized.
* Likewise for modules used built-in the sections marked __exit
* are discarded because __exit marked function are supposed to be called
* only when a moduel is unloaded which never happes for built-in modules.
* The check_sec_ref() function traverses all relocation records
* to find all references to a section that reference a section that will
* be discarded and warns about it.
**/
static void check_sec_ref(struct module *mod, const char *modname,
struct elf_info *elf,
int section(const char *),
int section_ref_ok(const char *))
{
int i;
Elf_Ehdr *hdr = elf->hdr;
Elf_Shdr *sechdrs = elf->sechdrs;
/* Walk through all sections */
for (i = 0; i < hdr->e_shnum; i++) {
/* We want to process only relocation sections and not .init */
if (sechdrs[i].sh_type == SHT_RELA)
section_rela(modname, elf, &elf->sechdrs[i],
section, section_ref_ok);
else if (sechdrs[i].sh_type == SHT_REL)
section_rel(modname, elf, &elf->sechdrs[i],
section, section_ref_ok);
} }
} }
......
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