From nobody Sun Feb 8 10:04:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29237EC0 for ; Mon, 20 Jan 2025 00:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737333655; cv=none; b=l9u8spYh9ejaTUhcReCjS7tlIiGsuWqj5mlD/nW83XS/gDXj6yUEpveFlIH9V6R2g5JQJhneUNebkniUcgYhuPOQW6/vOoQmwmb32YXgUideTVOinJIk94RgD/i7G88EA3UzRlAt7Y2rrlZnS4DVw1kQ+IPYV5hIqdPIIr+/+1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737333655; c=relaxed/simple; bh=j9N7stz1Wa6J5HKo7AJPqNmegCkzsPCFOe+fFDUfjxU=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=aW0YvXyIZBWyN2n1SEc1WwbK8gH+FDAgL3O14o1WjwIFtPGRPhUh0J7WOJ011I3L+zChmAOk09gSXRUXIQ++wcQm6CgHQS2TLtjq2RLSejBSLzhuzVS/6m0WpOJXLMIW7axnp3fiSNbgkmXZxle0hqTYA+uax7JJzQcQBw8CJ28= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E115C4CED6; Mon, 20 Jan 2025 00:40:53 +0000 (UTC) Date: Sun, 19 Jan 2025 19:40:51 -0500 From: Steven Rostedt To: LKML Cc: Masami Hiramatsu , Mathieu Desnoyers , Mark Rutland , Andrew Morton , Peter Zijlstra , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Zheng Yejian , Martin Kelly , Christophe Leroy , Josh Poimboeuf , Stephen Rothwell , Linus Torvalds Subject: [for-next][PATCH] scripts/sorttable: Use a structure of function pointers for elf helper Message-ID: <20250119194051.5b2fcbdd@gandalf.local.home> X-Mailer: Claws Mail 3.20.0git84 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" I forgot to push this to for-next even though I tested it over a week ago. git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git sorttable/for-next Head SHA1: 1e5f6771c247b28135307058d2cfe3b0153733dc Steven Rostedt (1): scripts/sorttable: Use a structure of function pointers for elf helpe= rs ---- scripts/sorttable.c | 175 +++++++++++++++++++++++++++++++++++-------------= ---- 1 file changed, 118 insertions(+), 57 deletions(-) --------------------------- commit 1e5f6771c247b28135307058d2cfe3b0153733dc Author: Steven Rostedt Date: Fri Jan 10 07:54:59 2025 -0500 scripts/sorttable: Use a structure of function pointers for elf helpers =20 Instead of having a series of function pointers that gets assigned to t= he Elf64 or Elf32 versions, put them all into a single structure and use that. Add the helper function that chooses the structure into the macros that build the different versions of the elf functions. =20 Link: https://lore.kernel.org/all/CAHk-=3DwiafEyX7UgOeZgvd6fvuByE5WXUPh= 9599kwOc_d-pdeug@mail.gmail.com/ =20 Cc: bpf Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Peter Zijlstra Cc: Masahiro Yamada Cc: Nathan Chancellor Cc: Nicolas Schier Cc: Zheng Yejian Cc: Martin Kelly Cc: Christophe Leroy Cc: Josh Poimboeuf Cc: Stephen Rothwell Link: https://lore.kernel.org/20250110075459.13d4b94c@gandalf.local.home Suggested-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) diff --git a/scripts/sorttable.c b/scripts/sorttable.c index 656c1e9b5ad9..9f41575afd7a 100644 --- a/scripts/sorttable.c +++ b/scripts/sorttable.c @@ -85,6 +85,25 @@ static uint64_t (*r8)(const uint64_t *); static void (*w)(uint32_t, uint32_t *); typedef void (*table_sort_t)(char *, int); =20 +static struct elf_funcs { + int (*compare_extable)(const void *a, const void *b); + uint64_t (*ehdr_shoff)(Elf_Ehdr *ehdr); + uint16_t (*ehdr_shstrndx)(Elf_Ehdr *ehdr); + uint16_t (*ehdr_shentsize)(Elf_Ehdr *ehdr); + uint16_t (*ehdr_shnum)(Elf_Ehdr *ehdr); + uint64_t (*shdr_addr)(Elf_Shdr *shdr); + uint64_t (*shdr_offset)(Elf_Shdr *shdr); + uint64_t (*shdr_size)(Elf_Shdr *shdr); + uint64_t (*shdr_entsize)(Elf_Shdr *shdr); + uint32_t (*shdr_link)(Elf_Shdr *shdr); + uint32_t (*shdr_name)(Elf_Shdr *shdr); + uint32_t (*shdr_type)(Elf_Shdr *shdr); + uint8_t (*sym_type)(Elf_Sym *sym); + uint32_t (*sym_name)(Elf_Sym *sym); + uint64_t (*sym_value)(Elf_Sym *sym); + uint16_t (*sym_shndx)(Elf_Sym *sym); +} e; + static uint64_t ehdr64_shoff(Elf_Ehdr *ehdr) { return r8(&ehdr->e64.e_shoff); @@ -95,6 +114,11 @@ static uint64_t ehdr32_shoff(Elf_Ehdr *ehdr) return r(&ehdr->e32.e_shoff); } =20 +static uint64_t ehdr_shoff(Elf_Ehdr *ehdr) +{ + return e.ehdr_shoff(ehdr); +} + #define EHDR_HALF(fn_name) \ static uint16_t ehdr64_##fn_name(Elf_Ehdr *ehdr) \ { \ @@ -104,6 +128,11 @@ static uint16_t ehdr64_##fn_name(Elf_Ehdr *ehdr) \ static uint16_t ehdr32_##fn_name(Elf_Ehdr *ehdr) \ { \ return r2(&ehdr->e32.e_##fn_name); \ +} \ + \ +static uint16_t ehdr_##fn_name(Elf_Ehdr *ehdr) \ +{ \ + return e.ehdr_##fn_name(ehdr); \ } =20 EHDR_HALF(shentsize) @@ -119,6 +148,11 @@ static uint32_t shdr64_##fn_name(Elf_Shdr *shdr) \ static uint32_t shdr32_##fn_name(Elf_Shdr *shdr) \ { \ return r(&shdr->e32.sh_##fn_name); \ +} \ + \ +static uint32_t shdr_##fn_name(Elf_Shdr *shdr) \ +{ \ + return e.shdr_##fn_name(shdr); \ } =20 #define SHDR_ADDR(fn_name) \ @@ -130,6 +164,11 @@ static uint64_t shdr64_##fn_name(Elf_Shdr *shdr) \ static uint64_t shdr32_##fn_name(Elf_Shdr *shdr) \ { \ return r(&shdr->e32.sh_##fn_name); \ +} \ + \ +static uint64_t shdr_##fn_name(Elf_Shdr *shdr) \ +{ \ + return e.shdr_##fn_name(shdr); \ } =20 #define SHDR_WORD(fn_name) \ @@ -141,6 +180,10 @@ static uint32_t shdr64_##fn_name(Elf_Shdr *shdr) \ static uint32_t shdr32_##fn_name(Elf_Shdr *shdr) \ { \ return r(&shdr->e32.sh_##fn_name); \ +} \ +static uint32_t shdr_##fn_name(Elf_Shdr *shdr) \ +{ \ + return e.shdr_##fn_name(shdr); \ } =20 SHDR_ADDR(addr) @@ -161,6 +204,11 @@ static uint64_t sym64_##fn_name(Elf_Sym *sym) \ static uint64_t sym32_##fn_name(Elf_Sym *sym) \ { \ return r(&sym->e32.st_##fn_name); \ +} \ + \ +static uint64_t sym_##fn_name(Elf_Sym *sym) \ +{ \ + return e.sym_##fn_name(sym); \ } =20 #define SYM_WORD(fn_name) \ @@ -172,6 +220,11 @@ static uint32_t sym64_##fn_name(Elf_Sym *sym) \ static uint32_t sym32_##fn_name(Elf_Sym *sym) \ { \ return r(&sym->e32.st_##fn_name); \ +} \ + \ +static uint32_t sym_##fn_name(Elf_Sym *sym) \ +{ \ + return e.sym_##fn_name(sym); \ } =20 #define SYM_HALF(fn_name) \ @@ -183,6 +236,11 @@ static uint16_t sym64_##fn_name(Elf_Sym *sym) \ static uint16_t sym32_##fn_name(Elf_Sym *sym) \ { \ return r2(&sym->e32.st_##fn_name); \ +} \ + \ +static uint16_t sym_##fn_name(Elf_Sym *sym) \ +{ \ + return e.sym_##fn_name(sym); \ } =20 static uint8_t sym64_type(Elf_Sym *sym) @@ -195,6 +253,11 @@ static uint8_t sym32_type(Elf_Sym *sym) return ELF32_ST_TYPE(sym->e32.st_info); } =20 +static uint8_t sym_type(Elf_Sym *sym) +{ + return e.sym_type(sym); +} + SYM_ADDR(value) SYM_WORD(name) SYM_HALF(shndx) @@ -322,29 +385,16 @@ static int compare_extable_64(const void *a, const vo= id *b) return av > bv; } =20 +static int compare_extable(const void *a, const void *b) +{ + return e.compare_extable(a, b); +} + static inline void *get_index(void *start, int entsize, int index) { return start + (entsize * index); } =20 - -static int (*compare_extable)(const void *a, const void *b); -static uint64_t (*ehdr_shoff)(Elf_Ehdr *ehdr); -static uint16_t (*ehdr_shstrndx)(Elf_Ehdr *ehdr); -static uint16_t (*ehdr_shentsize)(Elf_Ehdr *ehdr); -static uint16_t (*ehdr_shnum)(Elf_Ehdr *ehdr); -static uint64_t (*shdr_addr)(Elf_Shdr *shdr); -static uint64_t (*shdr_offset)(Elf_Shdr *shdr); -static uint64_t (*shdr_size)(Elf_Shdr *shdr); -static uint64_t (*shdr_entsize)(Elf_Shdr *shdr); -static uint32_t (*shdr_link)(Elf_Shdr *shdr); -static uint32_t (*shdr_name)(Elf_Shdr *shdr); -static uint32_t (*shdr_type)(Elf_Shdr *shdr); -static uint8_t (*sym_type)(Elf_Sym *sym); -static uint32_t (*sym_name)(Elf_Sym *sym); -static uint64_t (*sym_value)(Elf_Sym *sym); -static uint16_t (*sym_shndx)(Elf_Sym *sym); - static int extable_ent_size; static int long_size; =20 @@ -864,7 +914,30 @@ static int do_file(char const *const fname, void *addr) } =20 switch (ehdr->e32.e_ident[EI_CLASS]) { - case ELFCLASS32: + case ELFCLASS32: { + struct elf_funcs efuncs =3D { + .compare_extable =3D compare_extable_32, + .ehdr_shoff =3D ehdr32_shoff, + .ehdr_shentsize =3D ehdr32_shentsize, + .ehdr_shstrndx =3D ehdr32_shstrndx, + .ehdr_shnum =3D ehdr32_shnum, + .shdr_addr =3D shdr32_addr, + .shdr_offset =3D shdr32_offset, + .shdr_link =3D shdr32_link, + .shdr_size =3D shdr32_size, + .shdr_name =3D shdr32_name, + .shdr_type =3D shdr32_type, + .shdr_entsize =3D shdr32_entsize, + .sym_type =3D sym32_type, + .sym_name =3D sym32_name, + .sym_value =3D sym32_value, + .sym_shndx =3D sym32_shndx, + }; + + e =3D efuncs; + long_size =3D 4; + extable_ent_size =3D 8; + if (r2(&ehdr->e32.e_ehsize) !=3D sizeof(Elf32_Ehdr) || r2(&ehdr->e32.e_shentsize) !=3D sizeof(Elf32_Shdr)) { fprintf(stderr, @@ -872,26 +945,32 @@ static int do_file(char const *const fname, void *add= r) return -1; } =20 - compare_extable =3D compare_extable_32; - ehdr_shoff =3D ehdr32_shoff; - ehdr_shentsize =3D ehdr32_shentsize; - ehdr_shstrndx =3D ehdr32_shstrndx; - ehdr_shnum =3D ehdr32_shnum; - shdr_addr =3D shdr32_addr; - shdr_offset =3D shdr32_offset; - shdr_link =3D shdr32_link; - shdr_size =3D shdr32_size; - shdr_name =3D shdr32_name; - shdr_type =3D shdr32_type; - shdr_entsize =3D shdr32_entsize; - sym_type =3D sym32_type; - sym_name =3D sym32_name; - sym_value =3D sym32_value; - sym_shndx =3D sym32_shndx; - long_size =3D 4; - extable_ent_size =3D 8; + } break; - case ELFCLASS64: + case ELFCLASS64: { + struct elf_funcs efuncs =3D { + .compare_extable =3D compare_extable_64, + .ehdr_shoff =3D ehdr64_shoff, + .ehdr_shentsize =3D ehdr64_shentsize, + .ehdr_shstrndx =3D ehdr64_shstrndx, + .ehdr_shnum =3D ehdr64_shnum, + .shdr_addr =3D shdr64_addr, + .shdr_offset =3D shdr64_offset, + .shdr_link =3D shdr64_link, + .shdr_size =3D shdr64_size, + .shdr_name =3D shdr64_name, + .shdr_type =3D shdr64_type, + .shdr_entsize =3D shdr64_entsize, + .sym_type =3D sym64_type, + .sym_name =3D sym64_name, + .sym_value =3D sym64_value, + .sym_shndx =3D sym64_shndx, + }; + + e =3D efuncs; + long_size =3D 8; + extable_ent_size =3D 16; + if (r2(&ehdr->e64.e_ehsize) !=3D sizeof(Elf64_Ehdr) || r2(&ehdr->e64.e_shentsize) !=3D sizeof(Elf64_Shdr)) { fprintf(stderr, @@ -900,25 +979,7 @@ static int do_file(char const *const fname, void *addr) return -1; } =20 - compare_extable =3D compare_extable_64; - ehdr_shoff =3D ehdr64_shoff; - ehdr_shentsize =3D ehdr64_shentsize; - ehdr_shstrndx =3D ehdr64_shstrndx; - ehdr_shnum =3D ehdr64_shnum; - shdr_addr =3D shdr64_addr; - shdr_offset =3D shdr64_offset; - shdr_link =3D shdr64_link; - shdr_size =3D shdr64_size; - shdr_name =3D shdr64_name; - shdr_type =3D shdr64_type; - shdr_entsize =3D shdr64_entsize; - sym_type =3D sym64_type; - sym_name =3D sym64_name; - sym_value =3D sym64_value; - sym_shndx =3D sym64_shndx; - long_size =3D 8; - extable_ent_size =3D 16; - + } break; default: fprintf(stderr, "unrecognized ELF class %d %s\n",