Commit ff13f926 authored by Sam Ravnborg's avatar Sam Ravnborg

kbuild: introduce a few helpers in modpost

Introducing helpers to retreive symbol and section
names cleaned up the code a bit.
Signed-off-by: default avatarSam Ravnborg <sam@ravnborg.org>
parent 157c23c8
...@@ -605,6 +605,26 @@ static int strrcmp(const char *s, const char *sub) ...@@ -605,6 +605,26 @@ static int strrcmp(const char *s, const char *sub)
return memcmp(s + slen - sublen, sub, sublen); return memcmp(s + slen - sublen, sub, sublen);
} }
static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
{
return elf->strtab + sym->st_name;
}
static const char *sec_name(struct elf_info *elf, int shndx)
{
Elf_Shdr *sechdrs = elf->sechdrs;
return (void *)elf->hdr +
elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
sechdrs[shndx].sh_name;
}
static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
{
return (void *)elf->hdr +
elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
sechdr->sh_name;
}
/* if sym is empty or point to a string /* if sym is empty or point to a string
* like ".[0-9]+" then return 1. * like ".[0-9]+" then return 1.
* This is the optional prefix added by ld to some sections * This is the optional prefix added by ld to some sections
...@@ -943,17 +963,14 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, ...@@ -943,17 +963,14 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
{ {
Elf_Sym *sym; Elf_Sym *sym;
Elf_Sym *near = NULL; Elf_Sym *near = NULL;
Elf_Ehdr *hdr = elf->hdr;
Elf_Addr distance = ~0; Elf_Addr distance = ~0;
const char *secstrings = (void *)hdr +
elf->sechdrs[hdr->e_shstrndx].sh_offset;
for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
const char *symsec; const char *symsec;
if (sym->st_shndx >= SHN_LORESERVE) if (sym->st_shndx >= SHN_LORESERVE)
continue; continue;
symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name; symsec = sec_name(elf, sym->st_shndx);
if (strcmp(symsec, sec) != 0) if (strcmp(symsec, sec) != 0)
continue; continue;
if (!is_valid_name(elf, sym)) if (!is_valid_name(elf, sym))
...@@ -978,24 +995,21 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, ...@@ -978,24 +995,21 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
static void warn_sec_mismatch(const char *modname, const char *fromsec, static void warn_sec_mismatch(const char *modname, const char *fromsec,
struct elf_info *elf, Elf_Sym *sym, Elf_Rela r) struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
{ {
const char *refsymname = "";
Elf_Sym *where; Elf_Sym *where;
Elf_Sym *refsym; Elf_Sym *refsym;
Elf_Ehdr *hdr = elf->hdr; const char *refsymname = "";
Elf_Shdr *sechdrs = elf->sechdrs; const char *secname;
const char *secstrings = (void *)hdr +
sechdrs[hdr->e_shstrndx].sh_offset;
const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
secname = sec_name(elf, sym->st_shndx);
where = find_elf_symbol2(elf, r.r_offset, fromsec); where = find_elf_symbol2(elf, r.r_offset, fromsec);
refsym = find_elf_symbol(elf, r.r_addend, sym); refsym = find_elf_symbol(elf, r.r_addend, sym);
if (refsym && strlen(elf->strtab + refsym->st_name)) if (refsym && strlen(sym_name(elf, refsym)))
refsymname = elf->strtab + refsym->st_name; refsymname = sym_name(elf, refsym);
/* check whitelist - we may ignore it */ /* check whitelist - we may ignore it */
if (secref_whitelist(modname, secname, fromsec, if (secref_whitelist(modname, secname, fromsec,
where ? elf->strtab + where->st_name : "", where ? sym_name(elf, where) : "",
refsymname)) refsymname))
return; return;
...@@ -1003,7 +1017,7 @@ static void warn_sec_mismatch(const char *modname, const char *fromsec, ...@@ -1003,7 +1017,7 @@ static void warn_sec_mismatch(const char *modname, const char *fromsec,
warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
"in '%s'\n", "in '%s'\n",
modname, fromsec, (unsigned long long)r.r_offset, modname, fromsec, (unsigned long long)r.r_offset,
secname, refsymname, elf->strtab + where->st_name); secname, refsymname, sym_name(elf, where));
} else { } else {
warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n", warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
modname, fromsec, (unsigned long long)r.r_offset, modname, fromsec, (unsigned long long)r.r_offset,
...@@ -1095,14 +1109,10 @@ static void section_rela(const char *modname, struct elf_info *elf, ...@@ -1095,14 +1109,10 @@ static void section_rela(const char *modname, struct elf_info *elf,
const char *fromsec; const char *fromsec;
const char * tosec; const char * tosec;
Elf_Ehdr *hdr = elf->hdr; Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Elf_Rela *start = (void *)hdr + sechdr->sh_offset;
Elf_Rela *stop = (void *)start + sechdr->sh_size; Elf_Rela *stop = (void *)start + sechdr->sh_size;
const char *secstrings = (void *)hdr + fromsec = sech_name(elf, sechdr);
elf->sechdrs[hdr->e_shstrndx].sh_offset;
fromsec = secstrings + sechdr->sh_name;
fromsec += strlen(".rela"); fromsec += strlen(".rela");
/* if from section (name) is know good then skip it */ /* if from section (name) is know good then skip it */
if (match(fromsec, section_white_list)) if (match(fromsec, section_white_list))
...@@ -1111,7 +1121,7 @@ static void section_rela(const char *modname, struct elf_info *elf, ...@@ -1111,7 +1121,7 @@ static void section_rela(const char *modname, struct elf_info *elf,
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);
#if KERNEL_ELFCLASS == ELFCLASS64 #if KERNEL_ELFCLASS == ELFCLASS64
if (hdr->e_machine == EM_MIPS) { if (elf->hdr->e_machine == EM_MIPS) {
unsigned int r_typ; unsigned int r_typ;
r_sym = ELF64_MIPS_R_SYM(rela->r_info); r_sym = ELF64_MIPS_R_SYM(rela->r_info);
r_sym = TO_NATIVE(r_sym); r_sym = TO_NATIVE(r_sym);
...@@ -1131,8 +1141,7 @@ static void section_rela(const char *modname, struct elf_info *elf, ...@@ -1131,8 +1141,7 @@ static void section_rela(const char *modname, struct elf_info *elf,
if (sym->st_shndx >= SHN_LORESERVE) if (sym->st_shndx >= SHN_LORESERVE)
continue; continue;
tosec = secstrings + tosec = sec_name(elf, sym->st_shndx);
elf->sechdrs[sym->st_shndx].sh_name;
if (section_mismatch(fromsec, tosec)) if (section_mismatch(fromsec, tosec))
warn_sec_mismatch(modname, fromsec, elf, sym, r); warn_sec_mismatch(modname, fromsec, elf, sym, r);
} }
...@@ -1148,14 +1157,10 @@ static void section_rel(const char *modname, struct elf_info *elf, ...@@ -1148,14 +1157,10 @@ static void section_rel(const char *modname, struct elf_info *elf,
const char *fromsec; const char *fromsec;
const char * tosec; const char * tosec;
Elf_Ehdr *hdr = elf->hdr; Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Elf_Rel *start = (void *)hdr + sechdr->sh_offset;
Elf_Rel *stop = (void *)start + sechdr->sh_size; Elf_Rel *stop = (void *)start + sechdr->sh_size;
const char *secstrings = (void *)hdr + fromsec = sech_name(elf, sechdr);
elf->sechdrs[hdr->e_shstrndx].sh_offset;
fromsec = secstrings + sechdr->sh_name;
fromsec += strlen(".rel"); fromsec += strlen(".rel");
/* if from section (name) is know good then skip it */ /* if from section (name) is know good then skip it */
if (match(fromsec, section_white_list)) if (match(fromsec, section_white_list))
...@@ -1164,7 +1169,7 @@ static void section_rel(const char *modname, struct elf_info *elf, ...@@ -1164,7 +1169,7 @@ static void section_rel(const char *modname, struct elf_info *elf,
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);
#if KERNEL_ELFCLASS == ELFCLASS64 #if KERNEL_ELFCLASS == ELFCLASS64
if (hdr->e_machine == EM_MIPS) { if (elf->hdr->e_machine == EM_MIPS) {
unsigned int r_typ; unsigned int r_typ;
r_sym = ELF64_MIPS_R_SYM(rel->r_info); r_sym = ELF64_MIPS_R_SYM(rel->r_info);
r_sym = TO_NATIVE(r_sym); r_sym = TO_NATIVE(r_sym);
...@@ -1179,7 +1184,7 @@ static void section_rel(const char *modname, struct elf_info *elf, ...@@ -1179,7 +1184,7 @@ static void section_rel(const char *modname, struct elf_info *elf,
r_sym = ELF_R_SYM(r.r_info); r_sym = ELF_R_SYM(r.r_info);
#endif #endif
r.r_addend = 0; r.r_addend = 0;
switch (hdr->e_machine) { switch (elf->hdr->e_machine) {
case EM_386: case EM_386:
if (addend_386_rel(elf, sechdr, &r)) if (addend_386_rel(elf, sechdr, &r))
continue; continue;
...@@ -1198,8 +1203,7 @@ static void section_rel(const char *modname, struct elf_info *elf, ...@@ -1198,8 +1203,7 @@ static void section_rel(const char *modname, struct elf_info *elf,
if (sym->st_shndx >= SHN_LORESERVE) if (sym->st_shndx >= SHN_LORESERVE)
continue; continue;
tosec = secstrings + tosec = sec_name(elf, sym->st_shndx);
elf->sechdrs[sym->st_shndx].sh_name;
if (section_mismatch(fromsec, tosec)) if (section_mismatch(fromsec, tosec))
warn_sec_mismatch(modname, fromsec, elf, sym, r); warn_sec_mismatch(modname, fromsec, elf, sym, r);
} }
...@@ -1221,11 +1225,10 @@ static void check_sec_ref(struct module *mod, const char *modname, ...@@ -1221,11 +1225,10 @@ static void check_sec_ref(struct module *mod, const char *modname,
struct elf_info *elf) struct elf_info *elf)
{ {
int i; int i;
Elf_Ehdr *hdr = elf->hdr;
Elf_Shdr *sechdrs = elf->sechdrs; Elf_Shdr *sechdrs = elf->sechdrs;
/* Walk through all sections */ /* Walk through all sections */
for (i = 0; i < hdr->e_shnum; i++) { for (i = 0; i < elf->hdr->e_shnum; i++) {
/* We want to process only relocation sections and not .init */ /* We want to process only relocation sections and not .init */
if (sechdrs[i].sh_type == SHT_RELA) if (sechdrs[i].sh_type == SHT_RELA)
section_rela(modname, elf, &elf->sechdrs[i]); section_rela(modname, elf, &elf->sechdrs[i]);
......
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