Skip to content

Commit 1dfb59a

Browse files
committed
scripts/sorttable: Add helper functions for Elf_Ehdr
In order to remove the double #include of sorttable.h for 64 and 32 bit to create duplicate functions, add helper functions for Elf_Ehdr. This will create a function pointer for each helper that will get assigned to the appropriate function to handle either the 64bit or 32bit version. This also moves the _r()/r() wrappers for the Elf_Ehdr references that handle endian and size differences between the different architectures, into the helper function and out of the open code which is more error prone. 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 200d015 commit 1dfb59a

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

scripts/sorttable.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,31 @@ static uint64_t (*r8)(const uint64_t *);
8585
static void (*w)(uint32_t, uint32_t *);
8686
typedef void (*table_sort_t)(char *, int);
8787

88+
static uint64_t ehdr64_shoff(Elf_Ehdr *ehdr)
89+
{
90+
return r8(&ehdr->e64.e_shoff);
91+
}
92+
93+
static uint64_t ehdr32_shoff(Elf_Ehdr *ehdr)
94+
{
95+
return r(&ehdr->e32.e_shoff);
96+
}
97+
98+
#define EHDR_HALF(fn_name) \
99+
static uint16_t ehdr64_##fn_name(Elf_Ehdr *ehdr) \
100+
{ \
101+
return r2(&ehdr->e64.e_##fn_name); \
102+
} \
103+
\
104+
static uint16_t ehdr32_##fn_name(Elf_Ehdr *ehdr) \
105+
{ \
106+
return r2(&ehdr->e32.e_##fn_name); \
107+
}
108+
109+
EHDR_HALF(shentsize)
110+
EHDR_HALF(shstrndx)
111+
EHDR_HALF(shnum)
112+
88113
/*
89114
* Get the whole file as a programming convenience in order to avoid
90115
* malloc+lseek+read+free of many pieces. If successful, then mmap

scripts/sorttable.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#undef uint_t
2828
#undef _r
2929
#undef etype
30+
#undef ehdr_shoff
31+
#undef ehdr_shentsize
32+
#undef ehdr_shstrndx
33+
#undef ehdr_shnum
3034

3135
#ifdef SORTTABLE_64
3236
# define extable_ent_size 16
@@ -39,6 +43,10 @@
3943
# define uint_t uint64_t
4044
# define _r r8
4145
# define etype e64
46+
# define ehdr_shoff ehdr64_shoff
47+
# define ehdr_shentsize ehdr64_shentsize
48+
# define ehdr_shstrndx ehdr64_shstrndx
49+
# define ehdr_shnum ehdr64_shnum
4250
#else
4351
# define extable_ent_size 8
4452
# define compare_extable compare_extable_32
@@ -50,6 +58,10 @@
5058
# define uint_t uint32_t
5159
# define _r r
5260
# define etype e32
61+
# define ehdr_shoff ehdr32_shoff
62+
# define ehdr_shentsize ehdr32_shentsize
63+
# define ehdr_shstrndx ehdr32_shstrndx
64+
# define ehdr_shnum ehdr32_shnum
5365
#endif
5466

5567
#if defined(SORTTABLE_64) && defined(UNWINDER_ORC_ENABLED)
@@ -250,16 +262,16 @@ static int do_sort(Elf_Ehdr *ehdr,
250262
unsigned int orc_num_entries = 0;
251263
#endif
252264

253-
shdr_start = (Elf_Shdr *)((char *)ehdr + _r(&ehdr->etype.e_shoff));
254-
shentsize = r2(&ehdr->etype.e_shentsize);
265+
shdr_start = (Elf_Shdr *)((char *)ehdr + ehdr_shoff(ehdr));
266+
shentsize = ehdr_shentsize(ehdr);
255267

256-
shstrndx = r2(&ehdr->etype.e_shstrndx);
268+
shstrndx = ehdr_shstrndx(ehdr);
257269
if (shstrndx == SHN_XINDEX)
258270
shstrndx = r(&shdr_start->etype.sh_link);
259271
string_sec = get_index(shdr_start, shentsize, shstrndx);
260272
secstrings = (const char *)ehdr + _r(&string_sec->etype.sh_offset);
261273

262-
shnum = r2(&ehdr->etype.e_shnum);
274+
shnum = ehdr_shnum(ehdr);
263275
if (shnum == SHN_UNDEF)
264276
shnum = _r(&shdr_start->etype.sh_size);
265277

0 commit comments

Comments
 (0)