Skip to content

Commit 157fb5b

Browse files
committed
scripts/sorttable: Convert Elf_Ehdr to 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_Ehdr macro with a union that defines both Elf64_Ehdr and Elf32_Ehdr, with field e64 for the 64bit version, and e32 for the 32bit version. Then a macro etype can be used instead to get to 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 7ffc0d0 commit 157fb5b

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

scripts/sorttable.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
#define EM_LOONGARCH 258
6565
#endif
6666

67+
typedef union {
68+
Elf32_Ehdr e32;
69+
Elf64_Ehdr e64;
70+
} Elf_Ehdr;
71+
6772
static uint32_t (*r)(const uint32_t *);
6873
static uint16_t (*r2)(const uint16_t *);
6974
static uint64_t (*r8)(const uint64_t *);
@@ -266,10 +271,10 @@ static void sort_relative_table_with_data(char *extab_image, int image_size)
266271
static int do_file(char const *const fname, void *addr)
267272
{
268273
int rc = -1;
269-
Elf32_Ehdr *ehdr = addr;
274+
Elf_Ehdr *ehdr = addr;
270275
table_sort_t custom_sort = NULL;
271276

272-
switch (ehdr->e_ident[EI_DATA]) {
277+
switch (ehdr->e32.e_ident[EI_DATA]) {
273278
case ELFDATA2LSB:
274279
r = rle;
275280
r2 = r2le;
@@ -284,18 +289,18 @@ static int do_file(char const *const fname, void *addr)
284289
break;
285290
default:
286291
fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
287-
ehdr->e_ident[EI_DATA], fname);
292+
ehdr->e32.e_ident[EI_DATA], fname);
288293
return -1;
289294
}
290295

291-
if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
292-
(r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
293-
ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
296+
if (memcmp(ELFMAG, ehdr->e32.e_ident, SELFMAG) != 0 ||
297+
(r2(&ehdr->e32.e_type) != ET_EXEC && r2(&ehdr->e32.e_type) != ET_DYN) ||
298+
ehdr->e32.e_ident[EI_VERSION] != EV_CURRENT) {
294299
fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
295300
return -1;
296301
}
297302

298-
switch (r2(&ehdr->e_machine)) {
303+
switch (r2(&ehdr->e32.e_machine)) {
299304
case EM_386:
300305
case EM_AARCH64:
301306
case EM_LOONGARCH:
@@ -318,14 +323,14 @@ static int do_file(char const *const fname, void *addr)
318323
break;
319324
default:
320325
fprintf(stderr, "unrecognized e_machine %d %s\n",
321-
r2(&ehdr->e_machine), fname);
326+
r2(&ehdr->e32.e_machine), fname);
322327
return -1;
323328
}
324329

325-
switch (ehdr->e_ident[EI_CLASS]) {
330+
switch (ehdr->e32.e_ident[EI_CLASS]) {
326331
case ELFCLASS32:
327-
if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
328-
r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
332+
if (r2(&ehdr->e32.e_ehsize) != sizeof(Elf32_Ehdr) ||
333+
r2(&ehdr->e32.e_shentsize) != sizeof(Elf32_Shdr)) {
329334
fprintf(stderr,
330335
"unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
331336
break;
@@ -334,20 +339,19 @@ static int do_file(char const *const fname, void *addr)
334339
break;
335340
case ELFCLASS64:
336341
{
337-
Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
338-
if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
339-
r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
342+
if (r2(&ehdr->e64.e_ehsize) != sizeof(Elf64_Ehdr) ||
343+
r2(&ehdr->e64.e_shentsize) != sizeof(Elf64_Shdr)) {
340344
fprintf(stderr,
341345
"unrecognized ET_EXEC/ET_DYN file: %s\n",
342346
fname);
343347
break;
344348
}
345-
rc = do_sort_64(ghdr, fname, custom_sort);
349+
rc = do_sort_64(ehdr, fname, custom_sort);
346350
}
347351
break;
348352
default:
349353
fprintf(stderr, "unrecognized ELF class %d %s\n",
350-
ehdr->e_ident[EI_CLASS], fname);
354+
ehdr->e32.e_ident[EI_CLASS], fname);
351355
break;
352356
}
353357

scripts/sorttable.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
#undef sort_mcount_loc
2424
#undef elf_mcount_loc
2525
#undef do_sort
26-
#undef Elf_Ehdr
2726
#undef Elf_Shdr
2827
#undef Elf_Sym
2928
#undef ELF_ST_TYPE
3029
#undef uint_t
3130
#undef _r
31+
#undef etype
3232

3333
#ifdef SORTTABLE_64
3434
# define extable_ent_size 16
@@ -37,25 +37,25 @@
3737
# define sort_mcount_loc sort_mcount_loc_64
3838
# define elf_mcount_loc elf_mcount_loc_64
3939
# define do_sort do_sort_64
40-
# define Elf_Ehdr Elf64_Ehdr
4140
# define Elf_Shdr Elf64_Shdr
4241
# define Elf_Sym Elf64_Sym
4342
# define ELF_ST_TYPE ELF64_ST_TYPE
4443
# define uint_t uint64_t
4544
# define _r r8
45+
# define etype e64
4646
#else
4747
# define extable_ent_size 8
4848
# define compare_extable compare_extable_32
4949
# define get_mcount_loc get_mcount_loc_32
5050
# define sort_mcount_loc sort_mcount_loc_32
5151
# define elf_mcount_loc elf_mcount_loc_32
5252
# define do_sort do_sort_32
53-
# define Elf_Ehdr Elf32_Ehdr
5453
# define Elf_Shdr Elf32_Shdr
5554
# define Elf_Sym Elf32_Sym
5655
# define ELF_ST_TYPE ELF32_ST_TYPE
5756
# define uint_t uint32_t
5857
# define _r r
58+
# define etype e32
5959
#endif
6060

6161
#if defined(SORTTABLE_64) && defined(UNWINDER_ORC_ENABLED)
@@ -222,7 +222,7 @@ static int do_sort(Elf_Ehdr *ehdr,
222222
table_sort_t custom_sort)
223223
{
224224
int rc = -1;
225-
Elf_Shdr *s, *shdr = (Elf_Shdr *)((char *)ehdr + _r(&ehdr->e_shoff));
225+
Elf_Shdr *s, *shdr = (Elf_Shdr *)((char *)ehdr + _r(&ehdr->etype.e_shoff));
226226
Elf_Shdr *strtab_sec = NULL;
227227
Elf_Shdr *symtab_sec = NULL;
228228
Elf_Shdr *extab_sec = NULL;
@@ -249,12 +249,12 @@ static int do_sort(Elf_Ehdr *ehdr,
249249
unsigned int orc_num_entries = 0;
250250
#endif
251251

252-
shstrndx = r2(&ehdr->e_shstrndx);
252+
shstrndx = r2(&ehdr->etype.e_shstrndx);
253253
if (shstrndx == SHN_XINDEX)
254254
shstrndx = r(&shdr[0].sh_link);
255255
secstrings = (const char *)ehdr + _r(&shdr[shstrndx].sh_offset);
256256

257-
shnum = r2(&ehdr->e_shnum);
257+
shnum = r2(&ehdr->etype.e_shnum);
258258
if (shnum == SHN_UNDEF)
259259
shnum = _r(&shdr[0].sh_size);
260260

0 commit comments

Comments
 (0)