scripts/sorttable: Convert Elf_Sym MACRO over to a union
authorSteven Rostedt <rostedt@goodmis.org>
Sun, 5 Jan 2025 16:22:19 +0000 (11:22 -0500)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Wed, 8 Jan 2025 03:23:05 +0000 (22:23 -0500)
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 <bpf@vger.kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nicolas Schier <nicolas@fjasle.eu>
Cc: Zheng Yejian <zhengyejian1@huawei.com>
Cc: Martin Kelly <martin.kelly@crowdstrike.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Link: https://lore.kernel.org/20250105162345.528626969@goodmis.org
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
scripts/sorttable.c
scripts/sorttable.h

index 94497b8ab04c3cfcc88b66feca6e3c97b78c2bf3..57792cf2aa89b76a608d6c182ca102a1cc2e48e6 100644 (file)
@@ -74,6 +74,11 @@ typedef union {
        Elf64_Shdr      e64;
 } Elf_Shdr;
 
+typedef union {
+       Elf32_Sym       e32;
+       Elf64_Sym       e64;
+} Elf_Sym;
+
 static uint32_t (*r)(const uint32_t *);
 static uint16_t (*r2)(const uint16_t *);
 static uint64_t (*r8)(const uint64_t *);
index 3daf37bb6b9ab75f074c2d497341828f6e62a0f5..cd4429c8a9f42b9eb96f4f79f6b77596759bf8e0 100644 (file)
@@ -23,7 +23,6 @@
 #undef sort_mcount_loc
 #undef elf_mcount_loc
 #undef do_sort
-#undef Elf_Sym
 #undef ELF_ST_TYPE
 #undef uint_t
 #undef _r
@@ -36,7 +35,6 @@
 # define sort_mcount_loc       sort_mcount_loc_64
 # define elf_mcount_loc                elf_mcount_loc_64
 # define do_sort               do_sort_64
-# define Elf_Sym               Elf64_Sym
 # define ELF_ST_TYPE           ELF64_ST_TYPE
 # define uint_t                        uint64_t
 # define _r                    r8
@@ -48,7 +46,6 @@
 # define sort_mcount_loc       sort_mcount_loc_32
 # define elf_mcount_loc                elf_mcount_loc_32
 # define do_sort               do_sort_32
-# define Elf_Sym               Elf32_Sym
 # define ELF_ST_TYPE           ELF32_ST_TYPE
 # define uint_t                        uint32_t
 # define _r                    r
@@ -230,10 +227,13 @@ static int do_sort(Elf_Ehdr *ehdr,
        Elf_Sym *sort_needed_sym = NULL;
        Elf_Shdr *sort_needed_sec;
        uint32_t *sort_needed_loc;
+       void *sym_start;
+       void *sym_end;
        const char *secstrings;
        const char *strtab;
        char *extab_image;
        int sort_need_index;
+       int symentsize;
        int shentsize;
        int idx;
        int i;
@@ -376,12 +376,15 @@ static int do_sort(Elf_Ehdr *ehdr,
        }
 
        /* find the flag main_extable_sort_needed */
-       for (sym = (void *)ehdr + _r(&symtab_sec->etype.sh_offset);
-            sym < sym + _r(&symtab_sec->etype.sh_size) / sizeof(Elf_Sym);
-            sym++) {
-               if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
+       sym_start = (void *)ehdr + _r(&symtab_sec->etype.sh_offset);
+       sym_end = sym_start + _r(&symtab_sec->etype.sh_size);
+       symentsize = _r(&symtab_sec->etype.sh_entsize);
+
+       for (sym = sym_start; (void *)sym + symentsize < sym_end;
+            sym = (void *)sym + symentsize) {
+               if (ELF_ST_TYPE(sym->etype.st_info) != STT_OBJECT)
                        continue;
-               if (!strcmp(strtab + r(&sym->st_name),
+               if (!strcmp(strtab + r(&sym->etype.st_name),
                            "main_extable_sort_needed")) {
                        sort_needed_sym = sym;
                        break;
@@ -395,13 +398,13 @@ static int do_sort(Elf_Ehdr *ehdr,
                goto out;
        }
 
-       sort_need_index = get_secindex(r2(&sym->st_shndx),
-                                      sort_needed_sym - symtab,
+       sort_need_index = get_secindex(r2(&sym->etype.st_shndx),
+                                      ((void *)sort_needed_sym - (void *)symtab) / symentsize,
                                       symtab_shndx);
        sort_needed_sec = get_index(shdr_start, shentsize, sort_need_index);
        sort_needed_loc = (void *)ehdr +
                _r(&sort_needed_sec->etype.sh_offset) +
-               _r(&sort_needed_sym->st_value) -
+               _r(&sort_needed_sym->etype.st_value) -
                _r(&sort_needed_sec->etype.sh_addr);
 
        /* extable has been sorted, clear the flag */