Skip to content

Commit 200d015

Browse files
committed
scripts/sorttable: Convert Elf_Sym MACRO over to a union
In order to remove the double #include of sorttable.h for 64 and 32 bit to create duplicate functions for both, replace the Elf_Sym macro with a union that defines both Elf64_Sym and Elf32_Sym, with field e64 for the 64bit version, and e32 for the 32bit version. It can then use the macro etype to get the proper value. This will eventually be replaced with just single functions that can handle both 32bit and 64bit ELF parsing. Cc: bpf <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Masahiro Yamada <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Nicolas Schier <[email protected]> Cc: Zheng Yejian <[email protected]> Cc: Martin Kelly <[email protected]> Cc: Christophe Leroy <[email protected]> Cc: Josh Poimboeuf <[email protected]> Link: https://lore.kernel.org/[email protected] Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 545f6cf commit 200d015

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

scripts/sorttable.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ typedef union {
7474
Elf64_Shdr e64;
7575
} Elf_Shdr;
7676

77+
typedef union {
78+
Elf32_Sym e32;
79+
Elf64_Sym e64;
80+
} Elf_Sym;
81+
7782
static uint32_t (*r)(const uint32_t *);
7883
static uint16_t (*r2)(const uint16_t *);
7984
static uint64_t (*r8)(const uint64_t *);

scripts/sorttable.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#undef sort_mcount_loc
2424
#undef elf_mcount_loc
2525
#undef do_sort
26-
#undef Elf_Sym
2726
#undef ELF_ST_TYPE
2827
#undef uint_t
2928
#undef _r
@@ -36,7 +35,6 @@
3635
# define sort_mcount_loc sort_mcount_loc_64
3736
# define elf_mcount_loc elf_mcount_loc_64
3837
# define do_sort do_sort_64
39-
# define Elf_Sym Elf64_Sym
4038
# define ELF_ST_TYPE ELF64_ST_TYPE
4139
# define uint_t uint64_t
4240
# define _r r8
@@ -48,7 +46,6 @@
4846
# define sort_mcount_loc sort_mcount_loc_32
4947
# define elf_mcount_loc elf_mcount_loc_32
5048
# define do_sort do_sort_32
51-
# define Elf_Sym Elf32_Sym
5249
# define ELF_ST_TYPE ELF32_ST_TYPE
5350
# define uint_t uint32_t
5451
# define _r r
@@ -230,10 +227,13 @@ static int do_sort(Elf_Ehdr *ehdr,
230227
Elf_Sym *sort_needed_sym = NULL;
231228
Elf_Shdr *sort_needed_sec;
232229
uint32_t *sort_needed_loc;
230+
void *sym_start;
231+
void *sym_end;
233232
const char *secstrings;
234233
const char *strtab;
235234
char *extab_image;
236235
int sort_need_index;
236+
int symentsize;
237237
int shentsize;
238238
int idx;
239239
int i;
@@ -376,12 +376,15 @@ static int do_sort(Elf_Ehdr *ehdr,
376376
}
377377

378378
/* find the flag main_extable_sort_needed */
379-
for (sym = (void *)ehdr + _r(&symtab_sec->etype.sh_offset);
380-
sym < sym + _r(&symtab_sec->etype.sh_size) / sizeof(Elf_Sym);
381-
sym++) {
382-
if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
379+
sym_start = (void *)ehdr + _r(&symtab_sec->etype.sh_offset);
380+
sym_end = sym_start + _r(&symtab_sec->etype.sh_size);
381+
symentsize = _r(&symtab_sec->etype.sh_entsize);
382+
383+
for (sym = sym_start; (void *)sym + symentsize < sym_end;
384+
sym = (void *)sym + symentsize) {
385+
if (ELF_ST_TYPE(sym->etype.st_info) != STT_OBJECT)
383386
continue;
384-
if (!strcmp(strtab + r(&sym->st_name),
387+
if (!strcmp(strtab + r(&sym->etype.st_name),
385388
"main_extable_sort_needed")) {
386389
sort_needed_sym = sym;
387390
break;
@@ -395,13 +398,13 @@ static int do_sort(Elf_Ehdr *ehdr,
395398
goto out;
396399
}
397400

398-
sort_need_index = get_secindex(r2(&sym->st_shndx),
399-
sort_needed_sym - symtab,
401+
sort_need_index = get_secindex(r2(&sym->etype.st_shndx),
402+
((void *)sort_needed_sym - (void *)symtab) / symentsize,
400403
symtab_shndx);
401404
sort_needed_sec = get_index(shdr_start, shentsize, sort_need_index);
402405
sort_needed_loc = (void *)ehdr +
403406
_r(&sort_needed_sec->etype.sh_offset) +
404-
_r(&sort_needed_sym->st_value) -
407+
_r(&sort_needed_sym->etype.st_value) -
405408
_r(&sort_needed_sec->etype.sh_addr);
406409

407410
/* extable has been sorted, clear the flag */

0 commit comments

Comments
 (0)