Skip to content

Commit 59c3645

Browse files
jamieilestorvalds
authored andcommitted
scripts/sortextable: support objects with more than 64K sections.
Building with a large config and -ffunction-sections results in a large number of sections and sortextable needs to be able to handle that. Implement support for > 64K sections as modpost does. Signed-off-by: Jamie Iles <jamie.iles@oracle.com> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Michal Marek <mmarek@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a2529ad commit 59c3645

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

scripts/sortextable.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ static void (*w2)(uint16_t, uint16_t *);
152152

153153
typedef void (*table_sort_t)(char *, int);
154154

155+
/*
156+
* Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
157+
* the way to -256..-1, to avoid conflicting with real section
158+
* indices.
159+
*/
160+
#define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
161+
162+
static inline int is_shndx_special(unsigned int i)
163+
{
164+
return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
165+
}
166+
167+
/* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
168+
static inline unsigned int get_secindex(unsigned int shndx,
169+
unsigned int sym_offs,
170+
const Elf32_Word *symtab_shndx_start)
171+
{
172+
if (is_shndx_special(shndx))
173+
return SPECIAL(shndx);
174+
if (shndx != SHN_XINDEX)
175+
return shndx;
176+
return r(&symtab_shndx_start[sym_offs]);
177+
}
178+
155179
/* 32 bit and 64 bit are very similar */
156180
#include "sortextable.h"
157181
#define SORTEXTABLE_64

scripts/sortextable.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
9898
Elf_Shdr *symtab_sec = NULL;
9999
Elf_Shdr *extab_sec = NULL;
100100
Elf_Sym *sym;
101+
const Elf_Sym *symtab;
102+
Elf32_Word *symtab_shndx_start = NULL;
101103
Elf_Sym *sort_needed_sym;
102104
Elf_Shdr *sort_needed_sec;
103105
Elf_Rel *relocs = NULL;
@@ -109,11 +111,22 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
109111
int extab_index = 0;
110112
int i;
111113
int idx;
114+
unsigned int num_sections;
115+
unsigned int secindex_strings;
112116

113117
shdr = (Elf_Shdr *)((char *)ehdr + _r(&ehdr->e_shoff));
114-
shstrtab_sec = shdr + r2(&ehdr->e_shstrndx);
118+
119+
num_sections = r2(&ehdr->e_shnum);
120+
if (num_sections == SHN_UNDEF)
121+
num_sections = _r(&shdr[0].sh_size);
122+
123+
secindex_strings = r2(&ehdr->e_shstrndx);
124+
if (secindex_strings == SHN_XINDEX)
125+
secindex_strings = r(&shdr[0].sh_link);
126+
127+
shstrtab_sec = shdr + secindex_strings;
115128
secstrtab = (const char *)ehdr + _r(&shstrtab_sec->sh_offset);
116-
for (i = 0; i < r2(&ehdr->e_shnum); i++) {
129+
for (i = 0; i < num_sections; i++) {
117130
idx = r(&shdr[i].sh_name);
118131
if (strcmp(secstrtab + idx, "__ex_table") == 0) {
119132
extab_sec = shdr + i;
@@ -129,6 +142,9 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
129142
symtab_sec = shdr + i;
130143
if (strcmp(secstrtab + idx, ".strtab") == 0)
131144
strtab_sec = shdr + i;
145+
if (r(&shdr[i].sh_type) == SHT_SYMTAB_SHNDX)
146+
symtab_shndx_start = (Elf32_Word *)(
147+
(const char *)ehdr + _r(&shdr[i].sh_offset));
132148
}
133149
if (strtab_sec == NULL) {
134150
fprintf(stderr, "no .strtab in file: %s\n", fname);
@@ -138,6 +154,8 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
138154
fprintf(stderr, "no .symtab in file: %s\n", fname);
139155
fail_file();
140156
}
157+
symtab = (const Elf_Sym *)((const char *)ehdr +
158+
_r(&symtab_sec->sh_offset));
141159
if (extab_sec == NULL) {
142160
fprintf(stderr, "no __ex_table in file: %s\n", fname);
143161
fail_file();
@@ -176,7 +194,9 @@ do_func(Elf_Ehdr *ehdr, char const *const fname, table_sort_t custom_sort)
176194
fname);
177195
fail_file();
178196
}
179-
sort_needed_sec = &shdr[r2(&sort_needed_sym->st_shndx)];
197+
sort_needed_sec = &shdr[get_secindex(r2(&sym->st_shndx),
198+
sort_needed_sym - symtab,
199+
symtab_shndx_start)];
180200
sort_done_location = (void *)ehdr +
181201
_r(&sort_needed_sec->sh_offset) +
182202
_r(&sort_needed_sym->st_value) -

0 commit comments

Comments
 (0)