Commit 6845756b authored by Anders Kaseorg's avatar Anders Kaseorg Committed by Rusty Russell

modpost: Update 64k section support for binutils 2.18.50

Binutils 2.18.50 made a backwards-incompatible change in the way it
writes ELF objects with over 65280 sections, to improve conformance
with the ELF specification and interoperability with other ELF tools.
Specifically, it no longer adds 256 to section indices SHN_LORESERVE
and higher to skip over the reserved range SHN_LORESERVE through
SHN_HIRESERVE; those values are only considered special in the
st_shndx field, and not in other places where section indices are
stored.  See:

http://sourceware.org/bugzilla/show_bug.cgi?id=5900
http://groups.google.com/group/generic-abi/browse_thread/thread/e8bb63714b072e67/6c63738f12cc8a17Signed-off-by: default avatarAnders Kaseorg <andersk@ksplice.com>
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 9d63487f
...@@ -420,11 +420,10 @@ static int parse_elf(struct elf_info *info, const char *filename) ...@@ -420,11 +420,10 @@ static int parse_elf(struct elf_info *info, const char *filename)
return 0; return 0;
} }
if (hdr->e_shnum == 0) { if (hdr->e_shnum == SHN_UNDEF) {
/* /*
* There are more than 64k sections, * There are more than 64k sections,
* read count from .sh_size. * read count from .sh_size.
* note: it doesn't need shndx2secindex()
*/ */
info->num_sections = TO_NATIVE(sechdrs[0].sh_size); info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
} }
...@@ -432,8 +431,7 @@ static int parse_elf(struct elf_info *info, const char *filename) ...@@ -432,8 +431,7 @@ static int parse_elf(struct elf_info *info, const char *filename)
info->num_sections = hdr->e_shnum; info->num_sections = hdr->e_shnum;
} }
if (hdr->e_shstrndx == SHN_XINDEX) { if (hdr->e_shstrndx == SHN_XINDEX) {
info->secindex_strings = info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
shndx2secindex(TO_NATIVE(sechdrs[0].sh_link));
} }
else { else {
info->secindex_strings = hdr->e_shstrndx; info->secindex_strings = hdr->e_shstrndx;
...@@ -489,7 +487,7 @@ static int parse_elf(struct elf_info *info, const char *filename) ...@@ -489,7 +487,7 @@ static int parse_elf(struct elf_info *info, const char *filename)
sechdrs[i].sh_offset; sechdrs[i].sh_offset;
info->symtab_stop = (void *)hdr + info->symtab_stop = (void *)hdr +
sechdrs[i].sh_offset + sechdrs[i].sh_size; sechdrs[i].sh_offset + sechdrs[i].sh_size;
sh_link_idx = shndx2secindex(sechdrs[i].sh_link); sh_link_idx = sechdrs[i].sh_link;
info->strtab = (void *)hdr + info->strtab = (void *)hdr +
sechdrs[sh_link_idx].sh_offset; sechdrs[sh_link_idx].sh_offset;
} }
...@@ -516,11 +514,9 @@ static int parse_elf(struct elf_info *info, const char *filename) ...@@ -516,11 +514,9 @@ static int parse_elf(struct elf_info *info, const char *filename)
if (symtab_shndx_idx != ~0U) { if (symtab_shndx_idx != ~0U) {
Elf32_Word *p; Elf32_Word *p;
if (symtab_idx != if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
shndx2secindex(sechdrs[symtab_shndx_idx].sh_link))
fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n", fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
filename, filename, sechdrs[symtab_shndx_idx].sh_link,
shndx2secindex(sechdrs[symtab_shndx_idx].sh_link),
symtab_idx); symtab_idx);
/* Fix endianness */ /* Fix endianness */
for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop; for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
...@@ -1446,7 +1442,7 @@ static unsigned int *reloc_location(struct elf_info *elf, ...@@ -1446,7 +1442,7 @@ static unsigned int *reloc_location(struct elf_info *elf,
Elf_Shdr *sechdr, Elf_Rela *r) Elf_Shdr *sechdr, Elf_Rela *r)
{ {
Elf_Shdr *sechdrs = elf->sechdrs; Elf_Shdr *sechdrs = elf->sechdrs;
int section = shndx2secindex(sechdr->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; r->r_offset;
......
...@@ -145,33 +145,22 @@ static inline int is_shndx_special(unsigned int i) ...@@ -145,33 +145,22 @@ static inline int is_shndx_special(unsigned int i)
return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE; return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
} }
/* shndx is in [0..SHN_LORESERVE) U (SHN_HIRESERVE, 0xfffffff], thus: /*
* shndx == 0 <=> sechdrs[0] * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
* ...... * the way to -256..-1, to avoid conflicting with real section
* shndx == SHN_LORESERVE-1 <=> sechdrs[SHN_LORESERVE-1] * indices.
* shndx == SHN_HIRESERVE+1 <=> sechdrs[SHN_LORESERVE]
* shndx == SHN_HIRESERVE+2 <=> sechdrs[SHN_LORESERVE+1]
* ......
* fyi: sym->st_shndx is uint16, SHN_LORESERVE = ff00, SHN_HIRESERVE = ffff,
* so basically we map 0000..feff -> 0000..feff
* ff00..ffff -> (you are a bad boy, dont do it)
* 10000..xxxx -> ff00..(xxxx-0x100)
*/ */
static inline unsigned int shndx2secindex(unsigned int i) #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
{
if (i <= SHN_HIRESERVE)
return i;
return i - (SHN_HIRESERVE + 1 - SHN_LORESERVE);
}
/* Accessor for sym->st_shndx, hides ugliness of "64k sections" */ /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
static inline unsigned int get_secindex(const struct elf_info *info, static inline unsigned int get_secindex(const struct elf_info *info,
const Elf_Sym *sym) const Elf_Sym *sym)
{ {
if (is_shndx_special(sym->st_shndx))
return SPECIAL(sym->st_shndx);
if (sym->st_shndx != SHN_XINDEX) if (sym->st_shndx != SHN_XINDEX)
return sym->st_shndx; return sym->st_shndx;
return shndx2secindex(info->symtab_shndx_start[sym - return info->symtab_shndx_start[sym - info->symtab_start];
info->symtab_start]);
} }
/* file2alias.c */ /* file2alias.c */
......
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