From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8CBDC77B7A for ; Tue, 30 May 2023 17:21:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232870AbjE3RVj (ORCPT ); Tue, 30 May 2023 13:21:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231264AbjE3RVe (ORCPT ); Tue, 30 May 2023 13:21:34 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52A0BB2 for ; Tue, 30 May 2023 10:21:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DDB18630F9 for ; Tue, 30 May 2023 17:21:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C719C4339C; Tue, 30 May 2023 17:21:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467292; bh=f0Zkn5jXIKjGAYlwWWUPP5ioQ2KYARa2dNntzf0AIO0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OxnUPmimkmalR7CDQ193rpnYzskhU71pNu8Ny++TNm7eT6EqNSDLRfiYNd9Mj0ylC I6sBD8RFM5j3uqSR4FV+i/usidJaTzssrGZIeEvu37HTTyEGWTLmv0NxtgVHuH7xw4 HRCPIm1fpqe9okZaLaSNpRhI9t5K8+LZ7vzttJdBU2swXxPUM3gS+9qSoy0ssUsZnf 5xPA19iWudGVHk0kWFyt3dU/W4RZprl8YqoN/SkPdL66wOab2dzTieX/6hkJQgiEh7 NOx0iz0j75lIo63SgJTdjfyBCnJSAGkNaY3MDsWATHcpsqdgnolYZgqgWjG4Wd3taA 16F0RUzzrQ3PQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 01/22] objtool: Tidy elf.h Date: Tue, 30 May 2023 10:20:53 -0700 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Reorganize elf.h a bit: - Move the prototypes higher up so they can be used by the inline functions. - Move hash-related code to the bottom. - Remove the unused ELF_HASH_BITS macro. No functional changes. Signed-off-by: Josh Poimboeuf --- tools/objtool/include/objtool/elf.h | 96 ++++++++++++++--------------- 1 file changed, 47 insertions(+), 49 deletions(-) diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 78e2d0fc21ca..b24f83e7ca34 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -83,8 +83,6 @@ struct reloc { bool jump_table_start; }; =20 -#define ELF_HASH_BITS 20 - struct elf { Elf *elf; GElf_Ehdr ehdr; @@ -110,53 +108,6 @@ struct elf { struct symbol *symbol_data; }; =20 -#define OFFSET_STRIDE_BITS 4 -#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) -#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1)) - -#define for_offset_range(_offset, _start, _end) \ - for (_offset =3D ((_start) & OFFSET_STRIDE_MASK); \ - _offset >=3D ((_start) & OFFSET_STRIDE_MASK) && \ - _offset <=3D ((_end) & OFFSET_STRIDE_MASK); \ - _offset +=3D OFFSET_STRIDE) - -static inline u32 sec_offset_hash(struct section *sec, unsigned long offse= t) -{ - u32 ol, oh, idx =3D sec->idx; - - offset &=3D OFFSET_STRIDE_MASK; - - ol =3D offset; - oh =3D (offset >> 16) >> 16; - - __jhash_mix(ol, oh, idx); - - return ol; -} - -static inline u32 reloc_hash(struct reloc *reloc) -{ - return sec_offset_hash(reloc->sec, reloc->offset); -} - -/* - * Try to see if it's a whole archive (vmlinux.o or module). - * - * Note this will miss the case where a module only has one source file. - */ -static inline bool has_multiple_files(struct elf *elf) -{ - return elf->num_files > 1; -} - -static inline int elf_class_addrsize(struct elf *elf) -{ - if (elf->ehdr.e_ident[EI_CLASS] =3D=3D ELFCLASS32) - return sizeof(u32); - else - return sizeof(u64); -} - struct elf *elf_open_read(const char *name, int flags); struct section *elf_create_section(struct elf *elf, const char *name, unsi= gned int sh_flags, size_t entsize, int nr); =20 @@ -186,6 +137,24 @@ struct reloc *find_reloc_by_dest_range(const struct el= f *elf, struct section *se unsigned long offset, unsigned int len); struct symbol *find_func_containing(struct section *sec, unsigned long off= set); =20 +/* + * Try to see if it's a whole archive (vmlinux.o or module). + * + * Note this will miss the case where a module only has one source file. + */ +static inline bool has_multiple_files(struct elf *elf) +{ + return elf->num_files > 1; +} + +static inline int elf_class_addrsize(struct elf *elf) +{ + if (elf->ehdr.e_ident[EI_CLASS] =3D=3D ELFCLASS32) + return sizeof(u32); + else + return sizeof(u64); +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) =20 @@ -198,4 +167,33 @@ struct symbol *find_func_containing(struct section *se= c, unsigned long offset); for_each_sec(file, __sec) \ sec_for_each_sym(__sec, sym) =20 +#define OFFSET_STRIDE_BITS 4 +#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) +#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1)) + +#define for_offset_range(_offset, _start, _end) \ + for (_offset =3D ((_start) & OFFSET_STRIDE_MASK); \ + _offset >=3D ((_start) & OFFSET_STRIDE_MASK) && \ + _offset <=3D ((_end) & OFFSET_STRIDE_MASK); \ + _offset +=3D OFFSET_STRIDE) + +static inline u32 sec_offset_hash(struct section *sec, unsigned long offse= t) +{ + u32 ol, oh, idx =3D sec->idx; + + offset &=3D OFFSET_STRIDE_MASK; + + ol =3D offset; + oh =3D (offset >> 16) >> 16; + + __jhash_mix(ol, oh, idx); + + return ol; +} + +static inline u32 reloc_hash(struct reloc *reloc) +{ + return sec_offset_hash(reloc->sec, reloc->offset); +} + #endif /* _OBJTOOL_ELF_H */ --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9072C7EE23 for ; Tue, 30 May 2023 17:21:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233182AbjE3RVm (ORCPT ); Tue, 30 May 2023 13:21:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231548AbjE3RVf (ORCPT ); Tue, 30 May 2023 13:21:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF45DC5 for ; Tue, 30 May 2023 10:21:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 371FC63119 for ; Tue, 30 May 2023 17:21:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E048C433A1; Tue, 30 May 2023 17:21:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467292; bh=ezwwKI2GDxpeRWO6jitsfuA2ZdZYwPWarLEJ85t+eoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YKeCJ/vRmwMEfsTROMVCnxQEDwItNceboZrlvf8/twBja4RR58SkJ69m8DZDl7l18 Gxi9RGsi8QVkc/1vkcP81dnFL2DU5KAh17YLkpe+Z0FLgBDpHxUfZgsIPoqwXUZkeL ewhvS26MUcpCs/o3uM3nF/3aASMKl3qsbZTBgncSOdIBmkgKpWaS5HWRlsS9yRQc6j fxLan87+aEv4AkIrzPPGHfR6D6x+2hgIx9ua6Z5KwXgWSUF4sVkH4jlMNBpeT1CpQH H8NlL5PXhThgyvd0yJgdtzFzz6koPEtYMcvKN0oZiaVrobZO6I7AdTYDhObmrqhAvE aG+5gcH4Y5toA== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 02/22] objtool: Remove flags argument from elf_create_section() Date: Tue, 30 May 2023 10:20:54 -0700 Message-Id: <515235d9cf62637a14bee37bfa9169ef20065471.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Simplify the elf_create_section() interface a bit by removing the flags argument. Most callers don't care about changing the section header flags. If needed, they can be modified afterwards, just like any other section header field. Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 17 ++++++++++------- tools/objtool/elf.c | 10 +++++----- tools/objtool/include/objtool/elf.h | 2 +- tools/objtool/orc_gen.c | 4 ++-- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index a13c257f80dd..bc6771550423 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -676,11 +676,14 @@ static int create_static_call_sections(struct objtool= _file *file) list_for_each_entry(insn, &file->static_call_list, call_node) idx++; =20 - sec =3D elf_create_section(file->elf, ".static_call_sites", SHF_WRITE, + sec =3D elf_create_section(file->elf, ".static_call_sites", sizeof(struct static_call_site), idx); if (!sec) return -1; =20 + /* Allow modules to set the low bits of static_call_site::key */ + sec->sh.sh_flags |=3D SHF_WRITE; + idx =3D 0; list_for_each_entry(insn, &file->static_call_list, call_node) { =20 @@ -762,7 +765,7 @@ static int create_retpoline_sites_sections(struct objto= ol_file *file) if (!idx) return 0; =20 - sec =3D elf_create_section(file->elf, ".retpoline_sites", 0, + sec =3D elf_create_section(file->elf, ".retpoline_sites", sizeof(int), idx); if (!sec) { WARN("elf_create_section: .retpoline_sites"); @@ -808,7 +811,7 @@ static int create_return_sites_sections(struct objtool_= file *file) if (!idx) return 0; =20 - sec =3D elf_create_section(file->elf, ".return_sites", 0, + sec =3D elf_create_section(file->elf, ".return_sites", sizeof(int), idx); if (!sec) { WARN("elf_create_section: .return_sites"); @@ -860,7 +863,7 @@ static int create_ibt_endbr_seal_sections(struct objtoo= l_file *file) if (!idx) return 0; =20 - sec =3D elf_create_section(file->elf, ".ibt_endbr_seal", 0, + sec =3D elf_create_section(file->elf, ".ibt_endbr_seal", sizeof(int), idx); if (!sec) { WARN("elf_create_section: .ibt_endbr_seal"); @@ -919,7 +922,7 @@ static int create_cfi_sections(struct objtool_file *fil= e) idx++; } =20 - sec =3D elf_create_section(file->elf, ".cfi_sites", 0, sizeof(unsigned in= t), idx); + sec =3D elf_create_section(file->elf, ".cfi_sites", sizeof(unsigned int),= idx); if (!sec) return -1; =20 @@ -967,7 +970,7 @@ static int create_mcount_loc_sections(struct objtool_fi= le *file) list_for_each_entry(insn, &file->mcount_loc_list, call_node) idx++; =20 - sec =3D elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx); + sec =3D elf_create_section(file->elf, "__mcount_loc", addrsize, idx); if (!sec) return -1; =20 @@ -1012,7 +1015,7 @@ static int create_direct_call_sections(struct objtool= _file *file) list_for_each_entry(insn, &file->call_list, call_node) idx++; =20 - sec =3D elf_create_section(file->elf, ".call_sites", 0, sizeof(unsigned i= nt), idx); + sec =3D elf_create_section(file->elf, ".call_sites", sizeof(unsigned int)= , idx); if (!sec) return -1; =20 diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 500e92979a31..7598c0a2633d 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -1059,7 +1059,7 @@ static int elf_add_string(struct elf *elf, struct sec= tion *strtab, char *str) } =20 struct section *elf_create_section(struct elf *elf, const char *name, - unsigned int sh_flags, size_t entsize, int nr) + size_t entsize, int nr) { struct section *sec, *shstrtab; size_t size =3D entsize * nr; @@ -1117,7 +1117,7 @@ struct section *elf_create_section(struct elf *elf, c= onst char *name, sec->sh.sh_entsize =3D entsize; sec->sh.sh_type =3D SHT_PROGBITS; sec->sh.sh_addralign =3D 1; - sec->sh.sh_flags =3D SHF_ALLOC | sh_flags; + sec->sh.sh_flags =3D SHF_ALLOC; =20 /* Add section name to .shstrtab (or .strtab for Clang) */ shstrtab =3D find_section_by_name(elf, ".shstrtab"); @@ -1153,7 +1153,7 @@ static struct section *elf_create_rel_reloc_section(s= truct elf *elf, struct sect strcpy(relocname, ".rel"); strcat(relocname, base->name); =20 - sec =3D elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0); + sec =3D elf_create_section(elf, relocname, sizeof(GElf_Rel), 0); free(relocname); if (!sec) return NULL; @@ -1185,9 +1185,9 @@ static struct section *elf_create_rela_reloc_section(= struct elf *elf, struct sec strcat(relocname, base->name); =20 if (addrsize =3D=3D sizeof(u32)) - sec =3D elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 0); + sec =3D elf_create_section(elf, relocname, sizeof(Elf32_Rela), 0); else - sec =3D elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); + sec =3D elf_create_section(elf, relocname, sizeof(GElf_Rela), 0); free(relocname); if (!sec) return NULL; diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index b24f83e7ca34..2c28aeeb3cb2 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -109,7 +109,7 @@ struct elf { }; =20 struct elf *elf_open_read(const char *name, int flags); -struct section *elf_create_section(struct elf *elf, const char *name, unsi= gned int sh_flags, size_t entsize, int nr); +struct section *elf_create_section(struct elf *elf, const char *name, size= _t entsize, int nr); =20 struct symbol *elf_create_prefix_symbol(struct elf *elf, struct symbol *or= ig, long size); =20 diff --git a/tools/objtool/orc_gen.c b/tools/objtool/orc_gen.c index 48efd1e2f00d..d5f750be7d7d 100644 --- a/tools/objtool/orc_gen.c +++ b/tools/objtool/orc_gen.c @@ -237,12 +237,12 @@ int orc_create(struct objtool_file *file) WARN("file already has .orc_unwind section, skipping"); return -1; } - orc_sec =3D elf_create_section(file->elf, ".orc_unwind", 0, + orc_sec =3D elf_create_section(file->elf, ".orc_unwind", sizeof(struct orc_entry), nr); if (!orc_sec) return -1; =20 - sec =3D elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), n= r); + sec =3D elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), nr); if (!sec) return -1; =20 --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B21BC77B7A for ; Tue, 30 May 2023 17:21:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233322AbjE3RVu (ORCPT ); Tue, 30 May 2023 13:21:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232411AbjE3RVh (ORCPT ); Tue, 30 May 2023 13:21:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DE49E8 for ; Tue, 30 May 2023 10:21:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 940A263120 for ; Tue, 30 May 2023 17:21:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C249EC4339B; Tue, 30 May 2023 17:21:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467293; bh=ObRta/cmQf+Y5yZ8nzlOMvBFJEPR6qPtpNwKwUR6Mh8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HSPomr8CpVJD+AazpC7kUdFbyw14WTCcI1+eg8EUGQE6UUKPaNDK+rgIN0SpFe5H/ VWkO7QXG6a7+2pETEQmgmFP0QQP7pjegOjvFPYZHwgPW25ovyVxQKijyqF+PrafkKv rVhNWkuFOhq4V264zXO8d4WSG/WLkbGrP43kkez86QAWogNWTvT4UqdzxbvnddzyNy bp8wzdxDMdk/Kj6L4/kkht9Cby0Md9UjZEshadpiAx20mrwpc1zjxpSeubIMOASSZ4 UGKNYVcm5CnafrOJAJKhZm/vRwsxOh/lptSr4wi9VcmkDk0p3dVw3V7lftQ9hyfY0S nEnFCURNjOpwg== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 03/22] objtool: Improve reloc naming Date: Tue, 30 May 2023 10:20:55 -0700 Message-Id: <8b790e403df46f445c21003e7893b8f53b99a6f3.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" - The term "reloc" is overloaded to mean both "an instance of struct reloc" and "a reloc section". Change the latter to "rsec". - For variable names, use "sec" for regular sections and "rsec" for rela sections to prevent them getting mixed up. - For struct reloc variables, use "reloc" instead of "rel" everywhere for consistency. Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 134 +++++++++++------------ tools/objtool/elf.c | 164 ++++++++++++++-------------- tools/objtool/include/objtool/elf.h | 2 +- 3 files changed, 151 insertions(+), 149 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index bc6771550423..189b4161e713 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -493,7 +493,7 @@ static int add_pv_ops(struct objtool_file *file, const = char *symname) { struct symbol *sym, *func; unsigned long off, end; - struct reloc *rel; + struct reloc *reloc; int idx; =20 sym =3D find_symbol_by_name(file->elf, symname); @@ -503,19 +503,19 @@ static int add_pv_ops(struct objtool_file *file, cons= t char *symname) off =3D sym->offset; end =3D off + sym->len; for (;;) { - rel =3D find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); - if (!rel) + reloc =3D find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); + if (!reloc) break; =20 - func =3D rel->sym; + func =3D reloc->sym; if (func->type =3D=3D STT_SECTION) - func =3D find_symbol_by_offset(rel->sym->sec, rel->addend); + func =3D find_symbol_by_offset(reloc->sym->sec, reloc->addend); =20 - idx =3D (rel->offset - sym->offset) / sizeof(unsigned long); + idx =3D (reloc->offset - sym->offset) / sizeof(unsigned long); =20 objtool_pv_add(file, idx, func); =20 - off =3D rel->offset + 1; + off =3D reloc->offset + 1; if (off > end) break; } @@ -580,20 +580,20 @@ static struct instruction *find_last_insn(struct objt= ool_file *file, */ static int add_dead_ends(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct reloc *reloc; struct instruction *insn; =20 /* * Check for manually annotated dead ends. */ - sec =3D find_section_by_name(file->elf, ".rela.discard.unreachable"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.unreachable"); + if (!rsec) goto reachable; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type !=3D STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } insn =3D find_insn(file, reloc->sym->sec, reloc->addend); @@ -622,13 +622,13 @@ static int add_dead_ends(struct objtool_file *file) * GCC doesn't know the "ud2" is fatal, so it generates code as if it's * not a dead end. */ - sec =3D find_section_by_name(file->elf, ".rela.discard.reachable"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.reachable"); + if (!rsec) return 0; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type !=3D STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } insn =3D find_insn(file, reloc->sym->sec, reloc->addend); @@ -1043,15 +1043,15 @@ static int create_direct_call_sections(struct objto= ol_file *file) static void add_ignores(struct objtool_file *file) { struct instruction *insn; - struct section *sec; + struct section *rsec; struct symbol *func; struct reloc *reloc; =20 - sec =3D find_section_by_name(file->elf, ".rela.discard.func_stack_frame_n= on_standard"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.func_stack_frame_= non_standard"); + if (!rsec) return; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { switch (reloc->sym->type) { case STT_FUNC: func =3D reloc->sym; @@ -1064,7 +1064,8 @@ static void add_ignores(struct objtool_file *file) break; =20 default: - WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->s= ym->type); + WARN("unexpected relocation symbol type in %s: %d", + rsec->name, reloc->sym->type); continue; } =20 @@ -1283,17 +1284,17 @@ static void add_uaccess_safe(struct objtool_file *f= ile) */ static int add_ignore_alternatives(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct reloc *reloc; struct instruction *insn; =20 - sec =3D find_section_by_name(file->elf, ".rela.discard.ignore_alts"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.ignore_alts"); + if (!rsec) return 0; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type !=3D STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } =20 @@ -2203,7 +2204,7 @@ static void set_func_state(struct cfi_state *state) static int read_unwind_hints(struct objtool_file *file) { struct cfi_state cfi =3D init_cfi; - struct section *sec, *relocsec; + struct section *sec; struct unwind_hint *hint; struct instruction *insn; struct reloc *reloc; @@ -2213,8 +2214,7 @@ static int read_unwind_hints(struct objtool_file *fil= e) if (!sec) return 0; =20 - relocsec =3D sec->reloc; - if (!relocsec) { + if (!sec->rsec) { WARN("missing .rela.discard.unwind_hints section"); return -1; } @@ -2289,15 +2289,15 @@ static int read_unwind_hints(struct objtool_file *f= ile) =20 static int read_noendbr_hints(struct objtool_file *file) { - struct section *sec; struct instruction *insn; + struct section *rsec; struct reloc *reloc; =20 - sec =3D find_section_by_name(file->elf, ".rela.discard.noendbr"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.noendbr"); + if (!rsec) return 0; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { insn =3D find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->ad= dend); if (!insn) { WARN("bad .discard.noendbr entry"); @@ -2312,17 +2312,17 @@ static int read_noendbr_hints(struct objtool_file *= file) =20 static int read_retpoline_hints(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct instruction *insn; struct reloc *reloc; =20 - sec =3D find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); + if (!rsec) return 0; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type !=3D STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } =20 @@ -2348,17 +2348,17 @@ static int read_retpoline_hints(struct objtool_file= *file) =20 static int read_instr_hints(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct instruction *insn; struct reloc *reloc; =20 - sec =3D find_section_by_name(file->elf, ".rela.discard.instr_end"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.instr_end"); + if (!rsec) return 0; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type !=3D STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } =20 @@ -2371,13 +2371,13 @@ static int read_instr_hints(struct objtool_file *fi= le) insn->instr--; } =20 - sec =3D find_section_by_name(file->elf, ".rela.discard.instr_begin"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.instr_begin"); + if (!rsec) return 0; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type !=3D STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } =20 @@ -2395,17 +2395,17 @@ static int read_instr_hints(struct objtool_file *fi= le) =20 static int read_validate_unret_hints(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct instruction *insn; struct reloc *reloc; =20 - sec =3D find_section_by_name(file->elf, ".rela.discard.validate_unret"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.validate_unret"); + if (!rsec) return 0; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type !=3D STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } =20 @@ -2424,19 +2424,19 @@ static int read_validate_unret_hints(struct objtool= _file *file) static int read_intra_function_calls(struct objtool_file *file) { struct instruction *insn; - struct section *sec; + struct section *rsec; struct reloc *reloc; =20 - sec =3D find_section_by_name(file->elf, ".rela.discard.intra_function_cal= ls"); - if (!sec) + rsec =3D find_section_by_name(file->elf, ".rela.discard.intra_function_ca= lls"); + if (!rsec) return 0; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { unsigned long dest_off; =20 if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", - sec->name); + rsec->name); return -1; } =20 @@ -3332,15 +3332,15 @@ static inline bool func_uaccess_safe(struct symbol = *func) static inline const char *call_dest_name(struct instruction *insn) { static char pvname[19]; - struct reloc *rel; + struct reloc *reloc; int idx; =20 if (insn_call_dest(insn)) return insn_call_dest(insn)->name; =20 - rel =3D insn_reloc(NULL, insn); - if (rel && !strcmp(rel->sym->name, "pv_ops")) { - idx =3D (rel->addend / sizeof(void *)); + reloc =3D insn_reloc(NULL, insn); + if (reloc && !strcmp(reloc->sym->name, "pv_ops")) { + idx =3D (reloc->addend / sizeof(void *)); snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx); return pvname; } @@ -3351,14 +3351,14 @@ static inline const char *call_dest_name(struct ins= truction *insn) static bool pv_call_dest(struct objtool_file *file, struct instruction *in= sn) { struct symbol *target; - struct reloc *rel; + struct reloc *reloc; int idx; =20 - rel =3D insn_reloc(file, insn); - if (!rel || strcmp(rel->sym->name, "pv_ops")) + reloc =3D insn_reloc(file, insn); + if (!reloc || strcmp(reloc->sym->name, "pv_ops")) return false; =20 - idx =3D (arch_dest_reloc_offset(rel->addend) / sizeof(void *)); + idx =3D (arch_dest_reloc_offset(reloc->addend) / sizeof(void *)); =20 if (file->pv_ops[idx].clean) return true; @@ -4401,7 +4401,7 @@ static int validate_ibt(struct objtool_file *file) if (sec->sh.sh_flags & SHF_EXECINSTR) continue; =20 - if (!sec->reloc) + if (!sec->rsec) continue; =20 /* @@ -4428,7 +4428,7 @@ static int validate_ibt(struct objtool_file *file) strstr(sec->name, "__patchable_function_entries")) continue; =20 - list_for_each_entry(reloc, &sec->reloc->reloc_list, list) + list_for_each_entry(reloc, &sec->rsec->reloc_list, list) warnings +=3D validate_ibt_data_reloc(file, reloc); } =20 diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 7598c0a2633d..86ae62dfdba2 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -233,17 +233,17 @@ struct reloc *find_reloc_by_dest_range(const struct e= lf *elf, struct section *se unsigned long offset, unsigned int len) { struct reloc *reloc, *r =3D NULL; + struct section *rsec; unsigned long o; =20 - if (!sec->reloc) + rsec =3D sec->rsec; + if (!rsec) return NULL; =20 - sec =3D sec->reloc; - for_offset_range(o, offset, offset + len) { elf_hash_for_each_possible(reloc, reloc, hash, - sec_offset_hash(sec, o)) { - if (reloc->sec !=3D sec) + sec_offset_hash(rsec, o)) { + if (reloc->sec !=3D rsec) continue; =20 if (reloc->offset >=3D offset && reloc->offset < offset + len) { @@ -534,7 +534,7 @@ static int read_symbols(struct elf *elf) } =20 static struct section *elf_create_reloc_section(struct elf *elf, - struct section *base, + struct section *sec, int reltype); =20 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offs= et, @@ -542,7 +542,7 @@ int elf_add_reloc(struct elf *elf, struct section *sec,= unsigned long offset, { struct reloc *reloc; =20 - if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA)) + if (!sec->rsec && !elf_create_reloc_section(elf, sec, SHT_RELA)) return -1; =20 reloc =3D malloc(sizeof(*reloc)); @@ -552,18 +552,18 @@ int elf_add_reloc(struct elf *elf, struct section *se= c, unsigned long offset, } memset(reloc, 0, sizeof(*reloc)); =20 - reloc->sec =3D sec->reloc; + reloc->sec =3D sec->rsec; reloc->offset =3D offset; reloc->type =3D type; reloc->sym =3D sym; reloc->addend =3D addend; =20 list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); - list_add_tail(&reloc->list, &sec->reloc->reloc_list); + list_add_tail(&reloc->list, &sec->rsec->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); =20 - sec->reloc->sh.sh_size +=3D sec->reloc->sh.sh_entsize; - sec->reloc->changed =3D true; + sec->rsec->sh.sh_size +=3D sec->rsec->sh.sh_entsize; + sec->rsec->changed =3D true; =20 return 0; } @@ -865,9 +865,9 @@ int elf_add_reloc_to_insn(struct elf *elf, struct secti= on *sec, return elf_add_reloc(elf, sec, offset, type, sym, addend); } =20 -static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc,= unsigned int *symndx) +static int read_rel_reloc(struct section *rsec, int i, struct reloc *reloc= , unsigned int *symndx) { - if (!gelf_getrel(sec->data, i, &reloc->rel)) { + if (!gelf_getrel(rsec->data, i, &reloc->rel)) { WARN_ELF("gelf_getrel"); return -1; } @@ -878,9 +878,9 @@ static int read_rel_reloc(struct section *sec, int i, s= truct reloc *reloc, unsig return 0; } =20 -static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc= , unsigned int *symndx) +static int read_rela_reloc(struct section *rsec, int i, struct reloc *relo= c, unsigned int *symndx) { - if (!gelf_getrela(sec->data, i, &reloc->rela)) { + if (!gelf_getrela(rsec->data, i, &reloc->rela)) { WARN_ELF("gelf_getrela"); return -1; } @@ -894,7 +894,7 @@ static int read_rela_reloc(struct section *sec, int i, = struct reloc *reloc, unsi static int read_relocs(struct elf *elf) { unsigned long nr_reloc, max_reloc =3D 0, tot_reloc =3D 0; - struct section *sec; + struct section *rsec; struct reloc *reloc; unsigned int symndx; struct symbol *sym; @@ -903,51 +903,52 @@ static int read_relocs(struct elf *elf) if (!elf_alloc_hash(reloc, elf->text_size / 16)) return -1; =20 - list_for_each_entry(sec, &elf->sections, list) { - if ((sec->sh.sh_type !=3D SHT_RELA) && - (sec->sh.sh_type !=3D SHT_REL)) + list_for_each_entry(rsec, &elf->sections, list) { + if ((rsec->sh.sh_type !=3D SHT_RELA) && + (rsec->sh.sh_type !=3D SHT_REL)) continue; =20 - sec->base =3D find_section_by_index(elf, sec->sh.sh_info); - if (!sec->base) { + rsec->base =3D find_section_by_index(elf, rsec->sh.sh_info); + if (!rsec->base) { WARN("can't find base section for reloc section %s", - sec->name); + rsec->name); return -1; } =20 - sec->base->reloc =3D sec; + rsec->base->rsec =3D rsec; =20 nr_reloc =3D 0; - sec->reloc_data =3D calloc(sec->sh.sh_size / sec->sh.sh_entsize, sizeof(= *reloc)); - if (!sec->reloc_data) { + rsec->reloc_data =3D calloc(rsec->sh.sh_size / rsec->sh.sh_entsize, + sizeof(*reloc)); + if (!rsec->reloc_data) { perror("calloc"); return -1; } - for (i =3D 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) { - reloc =3D &sec->reloc_data[i]; - switch (sec->sh.sh_type) { + for (i =3D 0; i < rsec->sh.sh_size / rsec->sh.sh_entsize; i++) { + reloc =3D &rsec->reloc_data[i]; + switch (rsec->sh.sh_type) { case SHT_REL: - if (read_rel_reloc(sec, i, reloc, &symndx)) + if (read_rel_reloc(rsec, i, reloc, &symndx)) return -1; break; case SHT_RELA: - if (read_rela_reloc(sec, i, reloc, &symndx)) + if (read_rela_reloc(rsec, i, reloc, &symndx)) return -1; break; default: return -1; } =20 - reloc->sec =3D sec; + reloc->sec =3D rsec; reloc->idx =3D i; reloc->sym =3D sym =3D find_symbol_by_index(elf, symndx); if (!reloc->sym) { WARN("can't find reloc entry symbol %d for %s", - symndx, sec->name); + symndx, rsec->name); return -1; } =20 list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); - list_add_tail(&reloc->list, &sec->reloc_list); + list_add_tail(&reloc->list, &rsec->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); =20 nr_reloc++; @@ -1140,40 +1141,41 @@ struct section *elf_create_section(struct elf *elf,= const char *name, return sec; } =20 -static struct section *elf_create_rel_reloc_section(struct elf *elf, struc= t section *base) +static struct section *elf_create_rel_reloc_section(struct elf *elf, + struct section *sec) { char *relocname; - struct section *sec; + struct section *rsec; =20 - relocname =3D malloc(strlen(base->name) + strlen(".rel") + 1); + relocname =3D malloc(strlen(sec->name) + strlen(".rel") + 1); if (!relocname) { perror("malloc"); return NULL; } strcpy(relocname, ".rel"); - strcat(relocname, base->name); + strcat(relocname, sec->name); =20 - sec =3D elf_create_section(elf, relocname, sizeof(GElf_Rel), 0); + rsec =3D elf_create_section(elf, relocname, sizeof(GElf_Rel), 0); free(relocname); - if (!sec) + if (!rsec) return NULL; =20 - base->reloc =3D sec; - sec->base =3D base; + sec->rsec =3D rsec; + rsec->base =3D sec; =20 - sec->sh.sh_type =3D SHT_REL; - sec->sh.sh_addralign =3D 8; - sec->sh.sh_link =3D find_section_by_name(elf, ".symtab")->idx; - sec->sh.sh_info =3D base->idx; - sec->sh.sh_flags =3D SHF_INFO_LINK; + rsec->sh.sh_type =3D SHT_REL; + rsec->sh.sh_addralign =3D 8; + rsec->sh.sh_link =3D find_section_by_name(elf, ".symtab")->idx; + rsec->sh.sh_info =3D sec->idx; + rsec->sh.sh_flags =3D SHF_INFO_LINK; =20 - return sec; + return rsec; } =20 static struct section *elf_create_rela_reloc_section(struct elf *elf, stru= ct section *base) { char *relocname; - struct section *sec; + struct section *rsec; int addrsize =3D elf_class_addrsize(elf); =20 relocname =3D malloc(strlen(base->name) + strlen(".rela") + 1); @@ -1185,23 +1187,23 @@ static struct section *elf_create_rela_reloc_sectio= n(struct elf *elf, struct sec strcat(relocname, base->name); =20 if (addrsize =3D=3D sizeof(u32)) - sec =3D elf_create_section(elf, relocname, sizeof(Elf32_Rela), 0); + rsec =3D elf_create_section(elf, relocname, sizeof(Elf32_Rela), 0); else - sec =3D elf_create_section(elf, relocname, sizeof(GElf_Rela), 0); + rsec =3D elf_create_section(elf, relocname, sizeof(GElf_Rela), 0); free(relocname); - if (!sec) + if (!rsec) return NULL; =20 - base->reloc =3D sec; - sec->base =3D base; + base->rsec =3D rsec; + rsec->base =3D base; =20 - sec->sh.sh_type =3D SHT_RELA; - sec->sh.sh_addralign =3D addrsize; - sec->sh.sh_link =3D find_section_by_name(elf, ".symtab")->idx; - sec->sh.sh_info =3D base->idx; - sec->sh.sh_flags =3D SHF_INFO_LINK; + rsec->sh.sh_type =3D SHT_RELA; + rsec->sh.sh_addralign =3D addrsize; + rsec->sh.sh_link =3D find_section_by_name(elf, ".symtab")->idx; + rsec->sh.sh_info =3D base->idx; + rsec->sh.sh_flags =3D SHF_INFO_LINK; =20 - return sec; + return rsec; } =20 static struct section *elf_create_reloc_section(struct elf *elf, @@ -1215,28 +1217,28 @@ static struct section *elf_create_reloc_section(str= uct elf *elf, } } =20 -static int elf_rebuild_rel_reloc_section(struct section *sec) +static int elf_rebuild_rel_reloc_section(struct section *rsec) { struct reloc *reloc; int idx =3D 0; void *buf; =20 /* Allocate a buffer for relocations */ - buf =3D malloc(sec->sh.sh_size); + buf =3D malloc(rsec->sh.sh_size); if (!buf) { perror("malloc"); return -1; } =20 - sec->data->d_buf =3D buf; - sec->data->d_size =3D sec->sh.sh_size; - sec->data->d_type =3D ELF_T_REL; + rsec->data->d_buf =3D buf; + rsec->data->d_size =3D rsec->sh.sh_size; + rsec->data->d_type =3D ELF_T_REL; =20 idx =3D 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { reloc->rel.r_offset =3D reloc->offset; reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); - if (!gelf_update_rel(sec->data, idx, &reloc->rel)) { + if (!gelf_update_rel(rsec->data, idx, &reloc->rel)) { WARN_ELF("gelf_update_rel"); return -1; } @@ -1246,29 +1248,29 @@ static int elf_rebuild_rel_reloc_section(struct sec= tion *sec) return 0; } =20 -static int elf_rebuild_rela_reloc_section(struct section *sec) +static int elf_rebuild_rela_reloc_section(struct section *rsec) { struct reloc *reloc; int idx =3D 0; void *buf; =20 /* Allocate a buffer for relocations with addends */ - buf =3D malloc(sec->sh.sh_size); + buf =3D malloc(rsec->sh.sh_size); if (!buf) { perror("malloc"); return -1; } =20 - sec->data->d_buf =3D buf; - sec->data->d_size =3D sec->sh.sh_size; - sec->data->d_type =3D ELF_T_RELA; + rsec->data->d_buf =3D buf; + rsec->data->d_size =3D rsec->sh.sh_size; + rsec->data->d_type =3D ELF_T_RELA; =20 idx =3D 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { reloc->rela.r_offset =3D reloc->offset; reloc->rela.r_addend =3D reloc->addend; reloc->rela.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); - if (!gelf_update_rela(sec->data, idx, &reloc->rela)) { + if (!gelf_update_rela(rsec->data, idx, &reloc->rela)) { WARN_ELF("gelf_update_rela"); return -1; } @@ -1278,11 +1280,11 @@ static int elf_rebuild_rela_reloc_section(struct se= ction *sec) return 0; } =20 -static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec) +static int elf_rebuild_reloc_section(struct elf *elf, struct section *rsec) { - switch (sec->sh.sh_type) { - case SHT_REL: return elf_rebuild_rel_reloc_section(sec); - case SHT_RELA: return elf_rebuild_rela_reloc_section(sec); + switch (rsec->sh.sh_type) { + case SHT_REL: return elf_rebuild_rel_reloc_section(rsec); + case SHT_RELA: return elf_rebuild_rela_reloc_section(rsec); default: return -1; } } @@ -1308,13 +1310,13 @@ int elf_write_insn(struct elf *elf, struct section = *sec, =20 int elf_write_reloc(struct elf *elf, struct reloc *reloc) { - struct section *sec =3D reloc->sec; + struct section *rsec =3D reloc->sec; =20 - if (sec->sh.sh_type =3D=3D SHT_REL) { + if (rsec->sh.sh_type =3D=3D SHT_REL) { reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); reloc->rel.r_offset =3D reloc->offset; =20 - if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) { + if (!gelf_update_rel(rsec->data, reloc->idx, &reloc->rel)) { WARN_ELF("gelf_update_rel"); return -1; } @@ -1323,7 +1325,7 @@ int elf_write_reloc(struct elf *elf, struct reloc *re= loc) reloc->rela.r_addend =3D reloc->addend; reloc->rela.r_offset =3D reloc->offset; =20 - if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) { + if (!gelf_update_rela(rsec->data, reloc->idx, &reloc->rela)) { WARN_ELF("gelf_update_rela"); return -1; } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 2c28aeeb3cb2..a4e43a69f922 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -33,7 +33,7 @@ struct section { struct rb_root_cached symbol_tree; struct list_head symbol_list; struct list_head reloc_list; - struct section *base, *reloc; + struct section *base, *rsec; struct symbol *sym; Elf_Data *data; char *name; --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6AEA3C7EE23 for ; Tue, 30 May 2023 17:21:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233309AbjE3RVs (ORCPT ); Tue, 30 May 2023 13:21:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232125AbjE3RVg (ORCPT ); Tue, 30 May 2023 13:21:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 067B3E5 for ; Tue, 30 May 2023 10:21:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 849056311A for ; Tue, 30 May 2023 17:21:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2ABBDC433A0; Tue, 30 May 2023 17:21:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467293; bh=4EtaJer+wkp3B2cXK/6nqiqx6D7o7NqYDACyTftapxU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ohx3VDW85auwWbl9PBVvR1ldBZaPahy/YA5rXd55Vg9UeN3oUAdm08MNzpR39xahI IcHR+oC9wdZp7/2geEr6kOY385tMyj5Q8qZT5DVA8vqyuss4fsGBUFZ8Z7Q8LWdcob a+aTO/9JHUbmAf/jafvtgBG+gMbmYHC3buwjKeU9vHqC5djOadYE8F28miqG78kvj/ NQSC2R22nZDBE0T906t0WfkZpw7xJ6pu177f3ruxkK1VGrcvhUCw5C1dloEc1UipHQ djE3qeIBNTV00XoCI1iiK+3Tw4R4GQP9b5dD5f4paBgUM29grUSdfxWVXI6hp1ZRCN 24UTqOgNbj/BA== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 04/22] objtool: Consolidate rel/rela handling Date: Tue, 30 May 2023 10:20:56 -0700 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The GElf_Rel[a] structs have more similarities than differences. It's safe to hard-code the assumptions about their shared fields as they will never change. Consolidate their handling where possible, getting rid of duplicated code. Also, at least for now we only ever create rela sections, so simplify the relocation creation code to be rela-only. Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 12 +- tools/objtool/elf.c | 202 ++++++++-------------------- tools/objtool/include/objtool/elf.h | 13 +- 3 files changed, 68 insertions(+), 159 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 189b4161e713..b6d0cb24085b 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -951,7 +951,7 @@ static int create_cfi_sections(struct objtool_file *fil= e) =20 static int create_mcount_loc_sections(struct objtool_file *file) { - int addrsize =3D elf_class_addrsize(file->elf); + size_t addr_size =3D elf_addr_size(file->elf); struct instruction *insn; struct section *sec; int idx; @@ -970,25 +970,25 @@ static int create_mcount_loc_sections(struct objtool_= file *file) list_for_each_entry(insn, &file->mcount_loc_list, call_node) idx++; =20 - sec =3D elf_create_section(file->elf, "__mcount_loc", addrsize, idx); + sec =3D elf_create_section(file->elf, "__mcount_loc", addr_size, idx); if (!sec) return -1; =20 - sec->sh.sh_addralign =3D addrsize; + sec->sh.sh_addralign =3D addr_size; =20 idx =3D 0; list_for_each_entry(insn, &file->mcount_loc_list, call_node) { void *loc; =20 loc =3D sec->data->d_buf + idx; - memset(loc, 0, addrsize); + memset(loc, 0, addr_size); =20 if (elf_add_reloc_to_insn(file->elf, sec, idx, - addrsize =3D=3D sizeof(u64) ? R_ABS64 : R_ABS32, + addr_size =3D=3D sizeof(u64) ? R_ABS64 : R_ABS32, insn->sec, insn->offset)) return -1; =20 - idx +=3D addrsize; + idx +=3D addr_size; } =20 return 0; diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 86ae62dfdba2..4bbdd8e6df2c 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -533,16 +533,15 @@ static int read_symbols(struct elf *elf) return -1; } =20 -static struct section *elf_create_reloc_section(struct elf *elf, - struct section *sec, - int reltype); +static struct section *elf_create_rela_section(struct elf *elf, + struct section *sec); =20 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offs= et, unsigned int type, struct symbol *sym, s64 addend) { struct reloc *reloc; =20 - if (!sec->rsec && !elf_create_reloc_section(elf, sec, SHT_RELA)) + if (!sec->rsec && !elf_create_rela_section(elf, sec)) return -1; =20 reloc =3D malloc(sizeof(*reloc)); @@ -865,29 +864,25 @@ int elf_add_reloc_to_insn(struct elf *elf, struct sec= tion *sec, return elf_add_reloc(elf, sec, offset, type, sym, addend); } =20 -static int read_rel_reloc(struct section *rsec, int i, struct reloc *reloc= , unsigned int *symndx) +static int read_reloc(struct section *rsec, int i, struct reloc *reloc) { - if (!gelf_getrel(rsec->data, i, &reloc->rel)) { - WARN_ELF("gelf_getrel"); - return -1; - } - reloc->type =3D GELF_R_TYPE(reloc->rel.r_info); - reloc->addend =3D 0; - reloc->offset =3D reloc->rel.r_offset; - *symndx =3D GELF_R_SYM(reloc->rel.r_info); - return 0; -} + bool rela =3D rsec->sh.sh_type =3D=3D SHT_RELA; + void *retp; =20 -static int read_rela_reloc(struct section *rsec, int i, struct reloc *relo= c, unsigned int *symndx) -{ - if (!gelf_getrela(rsec->data, i, &reloc->rela)) { + if (rela) + retp =3D gelf_getrela(rsec->data, i, &reloc->rela); + else + retp =3D gelf_getrel(rsec->data, i, &reloc->rel); + + if (!retp) { WARN_ELF("gelf_getrela"); return -1; } - reloc->type =3D GELF_R_TYPE(reloc->rela.r_info); - reloc->addend =3D reloc->rela.r_addend; - reloc->offset =3D reloc->rela.r_offset; - *symndx =3D GELF_R_SYM(reloc->rela.r_info); + + reloc->offset =3D reloc->rel.r_offset; + reloc->type =3D GELF_R_TYPE(reloc->rel.r_info); + reloc->addend =3D rela ? reloc->rela.r_addend : 0; + return 0; } =20 @@ -926,20 +921,13 @@ static int read_relocs(struct elf *elf) } for (i =3D 0; i < rsec->sh.sh_size / rsec->sh.sh_entsize; i++) { reloc =3D &rsec->reloc_data[i]; - switch (rsec->sh.sh_type) { - case SHT_REL: - if (read_rel_reloc(rsec, i, reloc, &symndx)) - return -1; - break; - case SHT_RELA: - if (read_rela_reloc(rsec, i, reloc, &symndx)) - return -1; - break; - default: return -1; - } + + if (read_reloc(rsec, i, reloc)) + return -1; =20 reloc->sec =3D rsec; reloc->idx =3D i; + symndx =3D GELF_R_SYM(reloc->rel.r_info); reloc->sym =3D sym =3D find_symbol_by_index(elf, symndx); if (!reloc->sym) { WARN("can't find reloc entry symbol %d for %s", @@ -1141,30 +1129,30 @@ struct section *elf_create_section(struct elf *elf,= const char *name, return sec; } =20 -static struct section *elf_create_rel_reloc_section(struct elf *elf, - struct section *sec) +static struct section *elf_create_rela_section(struct elf *elf, + struct section *sec) { - char *relocname; struct section *rsec; + char *rsec_name; =20 - relocname =3D malloc(strlen(sec->name) + strlen(".rel") + 1); - if (!relocname) { + rsec_name =3D malloc(strlen(sec->name) + strlen(".rela") + 1); + if (!rsec_name) { perror("malloc"); return NULL; } - strcpy(relocname, ".rel"); - strcat(relocname, sec->name); + strcpy(rsec_name, ".rela"); + strcat(rsec_name, sec->name); =20 - rsec =3D elf_create_section(elf, relocname, sizeof(GElf_Rel), 0); - free(relocname); + rsec =3D elf_create_section(elf, rsec_name, elf_rela_size(elf), 0); + free(rsec_name); if (!rsec) return NULL; =20 sec->rsec =3D rsec; rsec->base =3D sec; =20 - rsec->sh.sh_type =3D SHT_REL; - rsec->sh.sh_addralign =3D 8; + rsec->sh.sh_type =3D SHT_RELA; + rsec->sh.sh_addralign =3D elf_addr_size(elf); rsec->sh.sh_link =3D find_section_by_name(elf, ".symtab")->idx; rsec->sh.sh_info =3D sec->idx; rsec->sh.sh_flags =3D SHF_INFO_LINK; @@ -1172,55 +1160,11 @@ static struct section *elf_create_rel_reloc_section= (struct elf *elf, return rsec; } =20 -static struct section *elf_create_rela_reloc_section(struct elf *elf, stru= ct section *base) -{ - char *relocname; - struct section *rsec; - int addrsize =3D elf_class_addrsize(elf); - - relocname =3D malloc(strlen(base->name) + strlen(".rela") + 1); - if (!relocname) { - perror("malloc"); - return NULL; - } - strcpy(relocname, ".rela"); - strcat(relocname, base->name); - - if (addrsize =3D=3D sizeof(u32)) - rsec =3D elf_create_section(elf, relocname, sizeof(Elf32_Rela), 0); - else - rsec =3D elf_create_section(elf, relocname, sizeof(GElf_Rela), 0); - free(relocname); - if (!rsec) - return NULL; - - base->rsec =3D rsec; - rsec->base =3D base; - - rsec->sh.sh_type =3D SHT_RELA; - rsec->sh.sh_addralign =3D addrsize; - rsec->sh.sh_link =3D find_section_by_name(elf, ".symtab")->idx; - rsec->sh.sh_info =3D base->idx; - rsec->sh.sh_flags =3D SHF_INFO_LINK; - - return rsec; -} - -static struct section *elf_create_reloc_section(struct elf *elf, - struct section *base, - int reltype) -{ - switch (reltype) { - case SHT_REL: return elf_create_rel_reloc_section(elf, base); - case SHT_RELA: return elf_create_rela_reloc_section(elf, base); - default: return NULL; - } -} - -static int elf_rebuild_rel_reloc_section(struct section *rsec) +static int elf_rebuild_reloc_section(struct elf *elf, struct section *rsec) { + bool rela =3D rsec->sh.sh_type =3D=3D SHT_RELA; struct reloc *reloc; - int idx =3D 0; + int idx =3D 0, ret; void *buf; =20 /* Allocate a buffer for relocations */ @@ -1232,13 +1176,19 @@ static int elf_rebuild_rel_reloc_section(struct sec= tion *rsec) =20 rsec->data->d_buf =3D buf; rsec->data->d_size =3D rsec->sh.sh_size; - rsec->data->d_type =3D ELF_T_REL; + rsec->data->d_type =3D rela ? ELF_T_RELA : ELF_T_REL; =20 idx =3D 0; list_for_each_entry(reloc, &rsec->reloc_list, list) { reloc->rel.r_offset =3D reloc->offset; reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); - if (!gelf_update_rel(rsec->data, idx, &reloc->rel)) { + if (rela) { + reloc->rela.r_addend =3D reloc->addend; + ret =3D gelf_update_rela(rsec->data, idx, &reloc->rela); + } else { + ret =3D gelf_update_rel(rsec->data, idx, &reloc->rel); + } + if (!ret) { WARN_ELF("gelf_update_rel"); return -1; } @@ -1248,47 +1198,6 @@ static int elf_rebuild_rel_reloc_section(struct sect= ion *rsec) return 0; } =20 -static int elf_rebuild_rela_reloc_section(struct section *rsec) -{ - struct reloc *reloc; - int idx =3D 0; - void *buf; - - /* Allocate a buffer for relocations with addends */ - buf =3D malloc(rsec->sh.sh_size); - if (!buf) { - perror("malloc"); - return -1; - } - - rsec->data->d_buf =3D buf; - rsec->data->d_size =3D rsec->sh.sh_size; - rsec->data->d_type =3D ELF_T_RELA; - - idx =3D 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { - reloc->rela.r_offset =3D reloc->offset; - reloc->rela.r_addend =3D reloc->addend; - reloc->rela.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); - if (!gelf_update_rela(rsec->data, idx, &reloc->rela)) { - WARN_ELF("gelf_update_rela"); - return -1; - } - idx++; - } - - return 0; -} - -static int elf_rebuild_reloc_section(struct elf *elf, struct section *rsec) -{ - switch (rsec->sh.sh_type) { - case SHT_REL: return elf_rebuild_rel_reloc_section(rsec); - case SHT_RELA: return elf_rebuild_rela_reloc_section(rsec); - default: return -1; - } -} - int elf_write_insn(struct elf *elf, struct section *sec, unsigned long offset, unsigned int len, const char *insn) @@ -1311,24 +1220,21 @@ int elf_write_insn(struct elf *elf, struct section = *sec, int elf_write_reloc(struct elf *elf, struct reloc *reloc) { struct section *rsec =3D reloc->sec; + int ret; =20 - if (rsec->sh.sh_type =3D=3D SHT_REL) { - reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); - reloc->rel.r_offset =3D reloc->offset; + reloc->rel.r_offset =3D reloc->offset; + reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); =20 - if (!gelf_update_rel(rsec->data, reloc->idx, &reloc->rel)) { - WARN_ELF("gelf_update_rel"); - return -1; - } - } else { - reloc->rela.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); + if (rsec->sh.sh_type =3D=3D SHT_RELA) { reloc->rela.r_addend =3D reloc->addend; - reloc->rela.r_offset =3D reloc->offset; + ret =3D gelf_update_rela(rsec->data, reloc->idx, &reloc->rela); + } else { + ret =3D gelf_update_rel(rsec->data, reloc->idx, &reloc->rel); + } =20 - if (!gelf_update_rela(rsec->data, reloc->idx, &reloc->rela)) { - WARN_ELF("gelf_update_rela"); - return -1; - } + if (!ret) { + WARN_ELF("gelf_update_rela"); + return -1; } =20 elf->changed =3D true; diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index a4e43a69f922..6f82f2515d6b 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -12,6 +12,7 @@ #include #include #include +#include =20 #ifdef LIBELF_USE_DEPRECATED # define elf_getshdrnum elf_getshnum @@ -147,12 +148,14 @@ static inline bool has_multiple_files(struct elf *elf) return elf->num_files > 1; } =20 -static inline int elf_class_addrsize(struct elf *elf) +static inline size_t elf_addr_size(struct elf *elf) { - if (elf->ehdr.e_ident[EI_CLASS] =3D=3D ELFCLASS32) - return sizeof(u32); - else - return sizeof(u64); + return elf->ehdr.e_ident[EI_CLASS] =3D=3D ELFCLASS32 ? 4 : 8; +} + +static inline size_t elf_rela_size(struct elf *elf) +{ + return elf_addr_size(elf) =3D=3D 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Re= la); } =20 #define for_each_sec(file, sec) \ --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 122B0C77B7A for ; Tue, 30 May 2023 17:21:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233269AbjE3RVp (ORCPT ); Tue, 30 May 2023 13:21:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231956AbjE3RVg (ORCPT ); Tue, 30 May 2023 13:21:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1997A3 for ; Tue, 30 May 2023 10:21:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 410BB6311B for ; Tue, 30 May 2023 17:21:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B0BAC433D2; Tue, 30 May 2023 17:21:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467293; bh=hMoGya2geNmJobaVb9+9In1Lii1AlJYW6XIs+9NEqCg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RWphnRNt3nJjnSbOZRPLqIQ5DMRcjM9EADpoiahmDgdMD79fu5kXF6efQ9JmQ5Fyk bq8GfakE0FRygnybJ9tVrtBUXGMKkmEANROD7QeQRWaTYKKf0E6y8EuS80IFtzmcvp oxr0JyJK4OcbNSKBtuXkMVn7UslSJSiFU6LzHdGmjDL7lBaj5BbC3IyKCmCEy/ddzz XYHpSsud1rwi9cTKeV4r826dR4v3PDuPyKWsUI72qEVSwvHOItFdJAX9gWUK1VDOAP Y+skuOmN5h7gU9He8FY8gGgSA2TgfZ4GRqUlktQhOiURbK9dAxq6UtxdDlyW9Vhk8D xJX39TcyvhwmQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 05/22] objtool: Fix reloc_hash size Date: Tue, 30 May 2023 10:20:57 -0700 Message-Id: <38ef60dc8043270bf3b9dfd139ae2a30ca3f75cc.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" With CONFIG_DEBUG_INFO, DWARF creates a lot of relocations and reloc_hash is woefully undersized, which can affect performance significantly. Fix that. Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 16 +++++++--------- tools/objtool/include/objtool/elf.h | 8 +++++++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 4bbdd8e6df2c..f72ec6d8fb14 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -328,12 +328,12 @@ static int read_sections(struct elf *elf) } } =20 - if (sec->sh.sh_flags & SHF_EXECINSTR) - elf->text_size +=3D sec->sh.sh_size; - list_add_tail(&sec->list, &elf->sections); elf_hash_add(section, &sec->hash, sec->idx); elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); + + if (is_reloc_sec(sec)) + elf->num_relocs +=3D sec->sh.sh_size / sec->sh.sh_entsize; } =20 if (opts.stats) { @@ -888,19 +888,18 @@ static int read_reloc(struct section *rsec, int i, st= ruct reloc *reloc) =20 static int read_relocs(struct elf *elf) { - unsigned long nr_reloc, max_reloc =3D 0, tot_reloc =3D 0; + unsigned long nr_reloc, max_reloc =3D 0; struct section *rsec; struct reloc *reloc; unsigned int symndx; struct symbol *sym; int i; =20 - if (!elf_alloc_hash(reloc, elf->text_size / 16)) + if (!elf_alloc_hash(reloc, elf->num_relocs)) return -1; =20 list_for_each_entry(rsec, &elf->sections, list) { - if ((rsec->sh.sh_type !=3D SHT_RELA) && - (rsec->sh.sh_type !=3D SHT_REL)) + if (!is_reloc_sec(rsec)) continue; =20 rsec->base =3D find_section_by_index(elf, rsec->sh.sh_info); @@ -942,12 +941,11 @@ static int read_relocs(struct elf *elf) nr_reloc++; } max_reloc =3D max(max_reloc, nr_reloc); - tot_reloc +=3D nr_reloc; } =20 if (opts.stats) { printf("max_reloc: %lu\n", max_reloc); - printf("tot_reloc: %lu\n", tot_reloc); + printf("num_relocs: %lu\n", elf->num_relocs); printf("reloc_bits: %d\n", elf->reloc_bits); } =20 diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 6f82f2515d6b..b81d78b35126 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -90,8 +90,9 @@ struct elf { int fd; bool changed; char *name; - unsigned int text_size, num_files; + unsigned int num_files; struct list_head sections; + unsigned long num_relocs; =20 int symbol_bits; int symbol_name_bits; @@ -158,6 +159,11 @@ static inline size_t elf_rela_size(struct elf *elf) return elf_addr_size(elf) =3D=3D 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Re= la); } =20 +static inline bool is_reloc_sec(struct section *sec) +{ + return sec->sh.sh_type =3D=3D SHT_RELA || sec->sh.sh_type =3D=3D SHT_REL; +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) =20 --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02E56C7EE24 for ; Tue, 30 May 2023 17:22:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233244AbjE3RV5 (ORCPT ); Tue, 30 May 2023 13:21:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232580AbjE3RVi (ORCPT ); Tue, 30 May 2023 13:21:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0ADBDBE for ; Tue, 30 May 2023 10:21:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 93CD563124 for ; Tue, 30 May 2023 17:21:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBFBBC4339C; Tue, 30 May 2023 17:21:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467294; bh=/gny+U0KyTIKAob4MYatyz6EsssqX1Se8bu0EbKgaIo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BX04ZWtWbxtq70YVJJRjpAvsIMqgzMSC+2JSsrIA+ZoDMe0grMLRyFbvopLaazmFy 41vh6bNUpoHiKpM6yFey+X6JBe/4gEQPZ1CfG3+4XP4AWUMiqcbCm7B6IKpk1e7laT 9scNyb9avMScJuTeTRazsjOWJxAfzU6CaX6/jyQjC99ydfS9qFT4U20Xj9aaANCHtc BCRYMor+ZFOM3+NZ33CxJqZnKDZbZu0vS/MVhTmjd9BALTzYL4LGVL6QK7PsAnya3C aa+qpAN3R2KAib4lnFYOZ8+QGq9nb0VDyyQpp5aSC9mHk5mYLPpYotuLNeuRuh9h7o DVX88xKso6bVg== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 06/22] objtool: Add mark_sec_changed() Date: Tue, 30 May 2023 10:20:58 -0700 Message-Id: <9a810a8d2e28af6ba07325362d0eb4703bb09d3a.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Ensure elf->changed always gets set when sec->changed gets set. Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 31 +++++++++++++++-------------- tools/objtool/include/objtool/elf.h | 14 ++++++++++++- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index f72ec6d8fb14..67967c231972 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -562,7 +562,8 @@ int elf_add_reloc(struct elf *elf, struct section *sec,= unsigned long offset, elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); =20 sec->rsec->sh.sh_size +=3D sec->rsec->sh.sh_entsize; - sec->rsec->changed =3D true; + + mark_sec_changed(elf, sec->rsec, true); =20 return 0; } @@ -577,7 +578,7 @@ static void elf_dirty_reloc_sym(struct elf *elf, struct= symbol *sym) struct reloc *reloc; =20 list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) - reloc->sec->changed =3D true; + mark_sec_changed(elf, reloc->sec, true); } =20 /* @@ -654,7 +655,7 @@ static int elf_update_symbol(struct elf *elf, struct se= ction *symtab, symtab_data->d_align =3D 1; symtab_data->d_type =3D ELF_T_SYM; =20 - symtab->changed =3D true; + mark_sec_changed(elf, symtab, true); symtab->truncate =3D true; =20 if (t) { @@ -669,7 +670,7 @@ static int elf_update_symbol(struct elf *elf, struct se= ction *symtab, shndx_data->d_align =3D sizeof(Elf32_Word); shndx_data->d_type =3D ELF_T_WORD; =20 - symtab_shndx->changed =3D true; + mark_sec_changed(elf, symtab_shndx, true); symtab_shndx->truncate =3D true; } =20 @@ -773,11 +774,11 @@ __elf_create_symbol(struct elf *elf, struct symbol *s= ym) } =20 symtab->sh.sh_size +=3D symtab->sh.sh_entsize; - symtab->changed =3D true; + mark_sec_changed(elf, symtab, true); =20 if (symtab_shndx) { symtab_shndx->sh.sh_size +=3D sizeof(Elf32_Word); - symtab_shndx->changed =3D true; + mark_sec_changed(elf, symtab_shndx, true); } =20 return sym; @@ -1040,7 +1041,8 @@ static int elf_add_string(struct elf *elf, struct sec= tion *strtab, char *str) =20 len =3D strtab->sh.sh_size; strtab->sh.sh_size +=3D data->d_size; - strtab->changed =3D true; + + mark_sec_changed(elf, strtab, true); =20 return len; } @@ -1075,7 +1077,6 @@ struct section *elf_create_section(struct elf *elf, c= onst char *name, } =20 sec->idx =3D elf_ndxscn(s); - sec->changed =3D true; =20 sec->data =3D elf_newdata(s); if (!sec->data) { @@ -1122,7 +1123,7 @@ struct section *elf_create_section(struct elf *elf, c= onst char *name, elf_hash_add(section, &sec->hash, sec->idx); elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); =20 - elf->changed =3D true; + mark_sec_changed(elf, sec, true); =20 return sec; } @@ -1208,9 +1209,8 @@ int elf_write_insn(struct elf *elf, struct section *s= ec, } =20 memcpy(data->d_buf + offset, insn, len); - elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY); =20 - elf->changed =3D true; + mark_sec_changed(elf, sec, true); =20 return 0; } @@ -1235,7 +1235,7 @@ int elf_write_reloc(struct elf *elf, struct reloc *re= loc) return -1; } =20 - elf->changed =3D true; + mark_sec_changed(elf, rsec, true); =20 return 0; } @@ -1307,12 +1307,14 @@ int elf_write(struct elf *elf) if (sec->truncate) elf_truncate_section(elf, sec); =20 - if (sec->changed) { + if (sec_changed(sec)) { s =3D elf_getscn(elf->elf, sec->idx); if (!s) { WARN_ELF("elf_getscn"); return -1; } + + /* Note this also flags the section dirty */ if (!gelf_update_shdr(s, &sec->sh)) { WARN_ELF("gelf_update_shdr"); return -1; @@ -1324,8 +1326,7 @@ int elf_write(struct elf *elf) return -1; } =20 - sec->changed =3D false; - elf->changed =3D true; + mark_sec_changed(elf, sec, false); } } =20 diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index b81d78b35126..56b66ff91943 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -39,7 +39,7 @@ struct section { Elf_Data *data; char *name; int idx; - bool changed, text, rodata, noinstr, init, truncate; + bool _changed, text, rodata, noinstr, init, truncate; struct reloc *reloc_data; }; =20 @@ -164,6 +164,18 @@ static inline bool is_reloc_sec(struct section *sec) return sec->sh.sh_type =3D=3D SHT_RELA || sec->sh.sh_type =3D=3D SHT_REL; } =20 +static inline bool sec_changed(struct section *sec) +{ + return sec->_changed; +} + +static inline void mark_sec_changed(struct elf *elf, struct section *sec, + bool changed) +{ + sec->_changed =3D changed; + elf->changed |=3D changed; +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) =20 --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 599CFC7EE23 for ; Tue, 30 May 2023 17:22:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232346AbjE3RWK (ORCPT ); Tue, 30 May 2023 13:22:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232883AbjE3RVj (ORCPT ); Tue, 30 May 2023 13:21:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06496B2 for ; Tue, 30 May 2023 10:21:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 854D961179 for ; Tue, 30 May 2023 17:21:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2AC06C433A1; Tue, 30 May 2023 17:21:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467294; bh=1E8+NZr2wa3zbKFfb87sPwKr84rMmdbhoxlyBO6Tvz4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YnZlUeQ8JOPhSdMlG0CXnAPGJoc+NCTAYl7zr+CGG3xDqWwPlr9INZ4g0jS2pMUZ9 EAU83JUWnZb9rq2RJqLz4HpjgHNp02vA6AL3ScMJpQRoasqP/tLLTtC5/A4XCbIUiR 3/5W1NBqDojAEfF/BLS4CgWjVyjdth4pfz244tWZ9o41twxPYS77jrvD47xvY4P8ST C52oiJ/k+Vnp431oCNOFzrJLcoR2E6hiKpCM/uN4HhHT/6BK1h4JnMAiVCEYdQxe18 EGhMHNEnIP74EkdUh3fkajbdQ0oY7bG8fQ7as0EVp5hp9zXJsuQVo+7NO/mMRLwayR Os5EvOTgB7Qaw== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 07/22] objtool: Add elf_create_section_pair() Date: Tue, 30 May 2023 10:20:59 -0700 Message-Id: <048e908f3ede9b66c15e44672b6dda992b1dae3e.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When creating an annotation section, allocate the reloc section data at the beginning. This simplifies the data model a bit and also saves memory due to the removal of malloc() in elf_rebuild_reloc_section(). With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 53.49G - After: peak heap memory consumption: 49.02G Signed-off-by: Josh Poimboeuf --- tools/objtool/arch/powerpc/include/arch/elf.h | 11 +- tools/objtool/arch/x86/include/arch/elf.h | 11 +- tools/objtool/check.c | 129 ++++++--------- tools/objtool/elf.c | 151 +++++++++++------- tools/objtool/include/objtool/elf.h | 33 +++- tools/objtool/orc_gen.c | 6 +- 6 files changed, 184 insertions(+), 157 deletions(-) diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h b/tools/objtool/= arch/powerpc/include/arch/elf.h index 73f9ae172fe5..66814fa28024 100644 --- a/tools/objtool/arch/powerpc/include/arch/elf.h +++ b/tools/objtool/arch/powerpc/include/arch/elf.h @@ -1,10 +1,13 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ - #ifndef _OBJTOOL_ARCH_ELF #define _OBJTOOL_ARCH_ELF =20 -#define R_NONE R_PPC_NONE -#define R_ABS64 R_PPC64_ADDR64 -#define R_ABS32 R_PPC_ADDR32 +#define R_NONE R_PPC_NONE +#define R_ABS64 R_PPC64_ADDR64 +#define R_ABS32 R_PPC_ADDR32 +#define R_DATA32 R_PPC_REL32 +#define R_DATA64 R_PPC64_REL64 +#define R_TEXT32 R_PPC_REL32 +#define R_TEXT64 R_PPC64_REL32 =20 #endif /* _OBJTOOL_ARCH_ELF */ diff --git a/tools/objtool/arch/x86/include/arch/elf.h b/tools/objtool/arch= /x86/include/arch/elf.h index ac14987cf687..7131f7f51a4e 100644 --- a/tools/objtool/arch/x86/include/arch/elf.h +++ b/tools/objtool/arch/x86/include/arch/elf.h @@ -1,8 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef _OBJTOOL_ARCH_ELF #define _OBJTOOL_ARCH_ELF =20 -#define R_NONE R_X86_64_NONE -#define R_ABS64 R_X86_64_64 -#define R_ABS32 R_X86_64_32 +#define R_NONE R_X86_64_NONE +#define R_ABS32 R_X86_64_32 +#define R_ABS64 R_X86_64_64 +#define R_DATA32 R_X86_64_PC32 +#define R_DATA64 R_X86_64_PC32 +#define R_TEXT32 R_X86_64_PC32 +#define R_TEXT64 R_X86_64_PC32 =20 #endif /* _OBJTOOL_ARCH_ELF */ diff --git a/tools/objtool/check.c b/tools/objtool/check.c index b6d0cb24085b..88f54bb1b01c 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -8,7 +8,6 @@ #include #include =20 -#include #include #include #include @@ -655,8 +654,8 @@ static int add_dead_ends(struct objtool_file *file) =20 static int create_static_call_sections(struct objtool_file *file) { - struct section *sec; struct static_call_site *site; + struct section *sec; struct instruction *insn; struct symbol *key_sym; char *key_name, *tmp; @@ -676,25 +675,21 @@ static int create_static_call_sections(struct objtool= _file *file) list_for_each_entry(insn, &file->static_call_list, call_node) idx++; =20 - sec =3D elf_create_section(file->elf, ".static_call_sites", - sizeof(struct static_call_site), idx); + sec =3D elf_create_section_pair(file->elf, ".static_call_sites", + sizeof(*site), idx, idx * 2); if (!sec) return -1; =20 - /* Allow modules to set the low bits of static_call_site::key */ + /* Allow modules to modify the low bits of static_call_site::key */ sec->sh.sh_flags |=3D SHF_WRITE; =20 idx =3D 0; list_for_each_entry(insn, &file->static_call_list, call_node) { =20 - site =3D (struct static_call_site *)sec->data->d_buf + idx; - memset(site, 0, sizeof(struct static_call_site)); - /* populate reloc for 'addr' */ - if (elf_add_reloc_to_insn(file->elf, sec, - idx * sizeof(struct static_call_site), - R_X86_64_PC32, - insn->sec, insn->offset)) + if (!elf_init_reloc_text_sym(file->elf, sec, + idx * sizeof(*site), idx * 2, + insn->sec, insn->offset)) return -1; =20 /* find key symbol */ @@ -734,10 +729,10 @@ static int create_static_call_sections(struct objtool= _file *file) free(key_name); =20 /* populate reloc for 'key' */ - if (elf_add_reloc(file->elf, sec, - idx * sizeof(struct static_call_site) + 4, - R_X86_64_PC32, key_sym, - is_sibling_call(insn) * STATIC_CALL_SITE_TAIL)) + if (!elf_init_reloc_data_sym(file->elf, sec, + idx * sizeof(*site) + 4, + (idx * 2) + 1, key_sym, + is_sibling_call(insn) * STATIC_CALL_SITE_TAIL)) return -1; =20 idx++; @@ -765,26 +760,18 @@ static int create_retpoline_sites_sections(struct obj= tool_file *file) if (!idx) return 0; =20 - sec =3D elf_create_section(file->elf, ".retpoline_sites", - sizeof(int), idx); - if (!sec) { - WARN("elf_create_section: .retpoline_sites"); + sec =3D elf_create_section_pair(file->elf, ".retpoline_sites", + sizeof(int), idx, idx); + if (!sec) return -1; - } =20 idx =3D 0; list_for_each_entry(insn, &file->retpoline_call_list, call_node) { =20 - int *site =3D (int *)sec->data->d_buf + idx; - *site =3D 0; - - if (elf_add_reloc_to_insn(file->elf, sec, - idx * sizeof(int), - R_X86_64_PC32, - insn->sec, insn->offset)) { - WARN("elf_add_reloc_to_insn: .retpoline_sites"); + if (!elf_init_reloc_text_sym(file->elf, sec, + idx * sizeof(int), idx, + insn->sec, insn->offset)) return -1; - } =20 idx++; } @@ -811,26 +798,18 @@ static int create_return_sites_sections(struct objtoo= l_file *file) if (!idx) return 0; =20 - sec =3D elf_create_section(file->elf, ".return_sites", - sizeof(int), idx); - if (!sec) { - WARN("elf_create_section: .return_sites"); + sec =3D elf_create_section_pair(file->elf, ".return_sites", + sizeof(int), idx, idx); + if (!sec) return -1; - } =20 idx =3D 0; list_for_each_entry(insn, &file->return_thunk_list, call_node) { =20 - int *site =3D (int *)sec->data->d_buf + idx; - *site =3D 0; - - if (elf_add_reloc_to_insn(file->elf, sec, - idx * sizeof(int), - R_X86_64_PC32, - insn->sec, insn->offset)) { - WARN("elf_add_reloc_to_insn: .return_sites"); + if (!elf_init_reloc_text_sym(file->elf, sec, + idx * sizeof(int), idx, + insn->sec, insn->offset)) return -1; - } =20 idx++; } @@ -863,12 +842,10 @@ static int create_ibt_endbr_seal_sections(struct objt= ool_file *file) if (!idx) return 0; =20 - sec =3D elf_create_section(file->elf, ".ibt_endbr_seal", - sizeof(int), idx); - if (!sec) { - WARN("elf_create_section: .ibt_endbr_seal"); + sec =3D elf_create_section_pair(file->elf, ".ibt_endbr_seal", + sizeof(int), idx, idx); + if (!sec) return -1; - } =20 idx =3D 0; list_for_each_entry(insn, &file->endbr_list, call_node) { @@ -883,13 +860,10 @@ static int create_ibt_endbr_seal_sections(struct objt= ool_file *file) !strcmp(sym->name, "cleanup_module"))) WARN("%s(): not an indirect call target", sym->name); =20 - if (elf_add_reloc_to_insn(file->elf, sec, - idx * sizeof(int), - R_X86_64_PC32, - insn->sec, insn->offset)) { - WARN("elf_add_reloc_to_insn: .ibt_endbr_seal"); + if (!elf_init_reloc_text_sym(file->elf, sec, + idx * sizeof(int), idx, + insn->sec, insn->offset)) return -1; - } =20 idx++; } @@ -901,7 +875,6 @@ static int create_cfi_sections(struct objtool_file *fil= e) { struct section *sec; struct symbol *sym; - unsigned int *loc; int idx; =20 sec =3D find_section_by_name(file->elf, ".cfi_sites"); @@ -922,7 +895,8 @@ static int create_cfi_sections(struct objtool_file *fil= e) idx++; } =20 - sec =3D elf_create_section(file->elf, ".cfi_sites", sizeof(unsigned int),= idx); + sec =3D elf_create_section_pair(file->elf, ".cfi_sites", + sizeof(unsigned int), idx, idx); if (!sec) return -1; =20 @@ -934,13 +908,9 @@ static int create_cfi_sections(struct objtool_file *fi= le) if (strncmp(sym->name, "__cfi_", 6)) continue; =20 - loc =3D (unsigned int *)sec->data->d_buf + idx; - memset(loc, 0, sizeof(unsigned int)); - - if (elf_add_reloc_to_insn(file->elf, sec, - idx * sizeof(unsigned int), - R_X86_64_PC32, - sym->sec, sym->offset)) + if (!elf_init_reloc_text_sym(file->elf, sec, + idx * sizeof(unsigned int), idx, + sym->sec, sym->offset)) return -1; =20 idx++; @@ -970,7 +940,8 @@ static int create_mcount_loc_sections(struct objtool_fi= le *file) list_for_each_entry(insn, &file->mcount_loc_list, call_node) idx++; =20 - sec =3D elf_create_section(file->elf, "__mcount_loc", addr_size, idx); + sec =3D elf_create_section_pair(file->elf, "__mcount_loc", addr_size, + idx, idx); if (!sec) return -1; =20 @@ -978,17 +949,17 @@ static int create_mcount_loc_sections(struct objtool_= file *file) =20 idx =3D 0; list_for_each_entry(insn, &file->mcount_loc_list, call_node) { - void *loc; =20 - loc =3D sec->data->d_buf + idx; - memset(loc, 0, addr_size); + struct reloc *reloc; =20 - if (elf_add_reloc_to_insn(file->elf, sec, idx, - addr_size =3D=3D sizeof(u64) ? R_ABS64 : R_ABS32, - insn->sec, insn->offset)) + reloc =3D elf_init_reloc_text_sym(file->elf, sec, idx * addr_size, idx, + insn->sec, insn->offset); + if (!reloc) return -1; =20 - idx +=3D addr_size; + reloc->type =3D addr_size =3D=3D 8 ? R_ABS64 : R_ABS32; + + idx++; } =20 return 0; @@ -998,7 +969,6 @@ static int create_direct_call_sections(struct objtool_f= ile *file) { struct instruction *insn; struct section *sec; - unsigned int *loc; int idx; =20 sec =3D find_section_by_name(file->elf, ".call_sites"); @@ -1015,20 +985,17 @@ static int create_direct_call_sections(struct objtoo= l_file *file) list_for_each_entry(insn, &file->call_list, call_node) idx++; =20 - sec =3D elf_create_section(file->elf, ".call_sites", sizeof(unsigned int)= , idx); + sec =3D elf_create_section_pair(file->elf, ".call_sites", + sizeof(unsigned int), idx, idx); if (!sec) return -1; =20 idx =3D 0; list_for_each_entry(insn, &file->call_list, call_node) { =20 - loc =3D (unsigned int *)sec->data->d_buf + idx; - memset(loc, 0, sizeof(unsigned int)); - - if (elf_add_reloc_to_insn(file->elf, sec, - idx * sizeof(unsigned int), - R_X86_64_PC32, - insn->sec, insn->offset)) + if (!elf_init_reloc_text_sym(file->elf, sec, + idx * sizeof(unsigned int), idx, + insn->sec, insn->offset)) return -1; =20 idx++; diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 67967c231972..5cbc9d578a45 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -533,41 +533,6 @@ static int read_symbols(struct elf *elf) return -1; } =20 -static struct section *elf_create_rela_section(struct elf *elf, - struct section *sec); - -int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offs= et, - unsigned int type, struct symbol *sym, s64 addend) -{ - struct reloc *reloc; - - if (!sec->rsec && !elf_create_rela_section(elf, sec)) - return -1; - - reloc =3D malloc(sizeof(*reloc)); - if (!reloc) { - perror("malloc"); - return -1; - } - memset(reloc, 0, sizeof(*reloc)); - - reloc->sec =3D sec->rsec; - reloc->offset =3D offset; - reloc->type =3D type; - reloc->sym =3D sym; - reloc->addend =3D addend; - - list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); - list_add_tail(&reloc->list, &sec->rsec->reloc_list); - elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); - - sec->rsec->sh.sh_size +=3D sec->rsec->sh.sh_entsize; - - mark_sec_changed(elf, sec->rsec, true); - - return 0; -} - /* * Ensure that any reloc section containing references to @sym is marked * changed such that it will get re-generated in elf_rebuild_reloc_section= s() @@ -841,13 +806,57 @@ elf_create_prefix_symbol(struct elf *elf, struct symb= ol *orig, long size) return sym; } =20 -int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, - unsigned long offset, unsigned int type, - struct section *insn_sec, unsigned long insn_off) +static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, + unsigned int reloc_idx, + unsigned long offset, struct symbol *sym, + s64 addend, unsigned int type) +{ + struct reloc *reloc; + + if (reloc_idx >=3D rsec->sh.sh_size / elf_rela_size(elf)) { + WARN("%s: bad reloc_idx %u for %s with size 0x%lx", + __func__, reloc_idx, rsec->name, rsec->sh.sh_size); + return NULL; + } + + reloc =3D malloc(sizeof(*reloc)); + if (!reloc) { + perror("malloc"); + return NULL; + } + memset(reloc, 0, sizeof(*reloc)); + + reloc->idx =3D reloc_idx; + reloc->sec =3D rsec; + reloc->offset =3D offset; + reloc->type =3D type; + reloc->sym =3D sym; + reloc->addend =3D addend; + + list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); + list_add_tail(&reloc->list, &rsec->reloc_list); + elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); + + mark_sec_changed(elf, rsec, true); + + return reloc; +} + +struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec, + unsigned long offset, + unsigned int reloc_idx, + struct section *insn_sec, + unsigned long insn_off) { struct symbol *sym =3D insn_sec->sym; int addend =3D insn_off; =20 + if (!(insn_sec->sh.sh_flags & SHF_EXECINSTR)) { + WARN("bad call to %s() for data symbol %s", + __func__, sym->name); + return NULL; + } + if (!sym) { /* * Due to how weak functions work, we must use section based @@ -857,12 +866,29 @@ int elf_add_reloc_to_insn(struct elf *elf, struct sec= tion *sec, */ sym =3D elf_create_section_symbol(elf, insn_sec); if (!sym) - return -1; + return NULL; =20 insn_sec->sym =3D sym; } =20 - return elf_add_reloc(elf, sec, offset, type, sym, addend); + return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, + elf_text_rela_type(elf)); +} + +struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec, + unsigned long offset, + unsigned int reloc_idx, + struct symbol *sym, + s64 addend) +{ + if (sym->sec && (sec->sh.sh_flags & SHF_EXECINSTR)) { + WARN("bad call to %s() for text symbol %s", + __func__, sym->name); + return NULL; + } + + return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, + elf_data_rela_type(elf)); } =20 static int read_reloc(struct section *rsec, int i, struct reloc *reloc) @@ -1048,7 +1074,7 @@ static int elf_add_string(struct elf *elf, struct sec= tion *strtab, char *str) } =20 struct section *elf_create_section(struct elf *elf, const char *name, - size_t entsize, int nr) + size_t entsize, unsigned int nr) { struct section *sec, *shstrtab; size_t size =3D entsize * nr; @@ -1129,7 +1155,8 @@ struct section *elf_create_section(struct elf *elf, c= onst char *name, } =20 static struct section *elf_create_rela_section(struct elf *elf, - struct section *sec) + struct section *sec, + unsigned int reloc_nr) { struct section *rsec; char *rsec_name; @@ -1142,46 +1169,50 @@ static struct section *elf_create_rela_section(stru= ct elf *elf, strcpy(rsec_name, ".rela"); strcat(rsec_name, sec->name); =20 - rsec =3D elf_create_section(elf, rsec_name, elf_rela_size(elf), 0); + rsec =3D elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr); free(rsec_name); if (!rsec) return NULL; =20 - sec->rsec =3D rsec; - rsec->base =3D sec; - + rsec->data->d_type =3D ELF_T_RELA; rsec->sh.sh_type =3D SHT_RELA; rsec->sh.sh_addralign =3D elf_addr_size(elf); rsec->sh.sh_link =3D find_section_by_name(elf, ".symtab")->idx; rsec->sh.sh_info =3D sec->idx; rsec->sh.sh_flags =3D SHF_INFO_LINK; =20 + sec->rsec =3D rsec; + rsec->base =3D sec; + return rsec; } =20 +struct section *elf_create_section_pair(struct elf *elf, const char *name, + size_t entsize, unsigned int nr, + unsigned int reloc_nr) +{ + struct section *sec; + + sec =3D elf_create_section(elf, name, entsize, nr); + if (!sec) + return NULL; + + if (!elf_create_rela_section(elf, sec, reloc_nr)) + return NULL; + + return sec; +} + static int elf_rebuild_reloc_section(struct elf *elf, struct section *rsec) { - bool rela =3D rsec->sh.sh_type =3D=3D SHT_RELA; struct reloc *reloc; int idx =3D 0, ret; - void *buf; - - /* Allocate a buffer for relocations */ - buf =3D malloc(rsec->sh.sh_size); - if (!buf) { - perror("malloc"); - return -1; - } - - rsec->data->d_buf =3D buf; - rsec->data->d_size =3D rsec->sh.sh_size; - rsec->data->d_type =3D rela ? ELF_T_RELA : ELF_T_REL; =20 idx =3D 0; list_for_each_entry(reloc, &rsec->reloc_list, list) { reloc->rel.r_offset =3D reloc->offset; reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); - if (rela) { + if (rsec->sh.sh_type =3D=3D SHT_RELA) { reloc->rela.r_addend =3D reloc->addend; ret =3D gelf_update_rela(rsec->data, idx, &reloc->rela); } else { diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 56b66ff91943..74f63934afd3 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -111,15 +111,26 @@ struct elf { }; =20 struct elf *elf_open_read(const char *name, int flags); -struct section *elf_create_section(struct elf *elf, const char *name, size= _t entsize, int nr); + +struct section *elf_create_section(struct elf *elf, const char *name, + size_t entsize, unsigned int nr); +struct section *elf_create_section_pair(struct elf *elf, const char *name, + size_t entsize, unsigned int nr, + unsigned int reloc_nr); =20 struct symbol *elf_create_prefix_symbol(struct elf *elf, struct symbol *or= ig, long size); =20 -int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offs= et, - unsigned int type, struct symbol *sym, s64 addend); -int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, - unsigned long offset, unsigned int type, - struct section *insn_sec, unsigned long insn_off); +struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec, + unsigned long offset, + unsigned int reloc_idx, + struct section *insn_sec, + unsigned long insn_off); + +struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec, + unsigned long offset, + unsigned int reloc_idx, + struct symbol *sym, + s64 addend); =20 int elf_write_insn(struct elf *elf, struct section *sec, unsigned long offset, unsigned int len, @@ -159,6 +170,16 @@ static inline size_t elf_rela_size(struct elf *elf) return elf_addr_size(elf) =3D=3D 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Re= la); } =20 +static inline unsigned int elf_data_rela_type(struct elf *elf) +{ + return elf_addr_size(elf) =3D=3D 4 ? R_DATA32 : R_DATA64; +} + +static inline unsigned int elf_text_rela_type(struct elf *elf) +{ + return elf_addr_size(elf) =3D=3D 4 ? R_TEXT32 : R_TEXT64; +} + static inline bool is_reloc_sec(struct section *sec) { return sec->sh.sh_type =3D=3D SHT_RELA || sec->sh.sh_type =3D=3D SHT_REL; diff --git a/tools/objtool/orc_gen.c b/tools/objtool/orc_gen.c index d5f750be7d7d..bae343908867 100644 --- a/tools/objtool/orc_gen.c +++ b/tools/objtool/orc_gen.c @@ -118,8 +118,8 @@ static int write_orc_entry(struct elf *elf, struct sect= ion *orc_sec, orc->bp_offset =3D bswap_if_needed(elf, orc->bp_offset); =20 /* populate reloc for ip */ - if (elf_add_reloc_to_insn(elf, ip_sec, idx * sizeof(int), R_X86_64_PC32, - insn_sec, insn_off)) + if (!elf_init_reloc_text_sym(elf, ip_sec, idx * sizeof(int), idx, + insn_sec, insn_off)) return -1; =20 return 0; @@ -242,7 +242,7 @@ int orc_create(struct objtool_file *file) if (!orc_sec) return -1; =20 - sec =3D elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), nr); + sec =3D elf_create_section_pair(file->elf, ".orc_unwind_ip", sizeof(int),= nr, nr); if (!sec) return -1; =20 --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0D19C7EE23 for ; Tue, 30 May 2023 17:22:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233300AbjE3RWF (ORCPT ); Tue, 30 May 2023 13:22:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232862AbjE3RVj (ORCPT ); Tue, 30 May 2023 13:21:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB2BDEC for ; Tue, 30 May 2023 10:21:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 432C763123 for ; Tue, 30 May 2023 17:21:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7C5AEC433A0; Tue, 30 May 2023 17:21:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467294; bh=Ysxh7lAkSp3EE9042bKmW8CQmu0Vy2TT9tV2rI9hxSs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ruh/7afCQoGJd7hDuV73+GW+lr51JA200teuHzksxnUpgZgRQWR6ZKQf2SKyBOiw5 Og4+2H07UOJuxuCwPKZAF9s/9hnWoWsg6PS0D5D3rMqnFpXcONgHfKJk13K++5naWq 2f24BRs6Cz7y0HMj0+xOEGiCzX7XFiUqTDdsuOQoTb/g8vHmXT0Eg28jMq0Vg+Elds ClHLv3phlxlXv7Rj9NCFjQYmFBCER3BgOYjf/CVH9hLBbyyrfOve0GPq5UWBLaXjVf tCuMJ4X3BcsW05PqTiSxAc2ry1bNsXO2QiDz6uvS/zBmDb+4wuzekpeeH3N9/e1BJd 0DBc51CYo3+SQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 08/22] objtool: Keep GElf_Rel[a] structs synced Date: Tue, 30 May 2023 10:21:00 -0700 Message-Id: <156d8a3e528a11e5c8577cf552890ed1f2b9567b.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Keep the GElf_Rela structs synced with their 'struct reloc' counterparts instead of having to go back and "rebuild" them later. Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 55 ++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 41 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 5cbc9d578a45..8d491b2d123e 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -534,16 +534,18 @@ static int read_symbols(struct elf *elf) } =20 /* - * Ensure that any reloc section containing references to @sym is marked - * changed such that it will get re-generated in elf_rebuild_reloc_section= s() - * with the new symbol index. + * @sym's idx has changed. Update the relocs which reference it. */ -static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym) +static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym) { struct reloc *reloc; =20 - list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) - mark_sec_changed(elf, reloc->sec, true); + list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) { + if (elf_write_reloc(elf, reloc)) + return -1; + } + + return 0; } =20 /* @@ -716,13 +718,14 @@ __elf_create_symbol(struct elf *elf, struct symbol *s= ym) hlist_del(&old->hash); elf_hash_add(symbol, &old->hash, old->idx); =20 - elf_dirty_reloc_sym(elf, old); - if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { WARN("elf_update_symbol move"); return NULL; } =20 + if (elf_update_sym_relocs(elf, old)) + return NULL; + new_idx =3D first_non_local; } =20 @@ -833,12 +836,13 @@ static struct reloc *elf_init_reloc(struct elf *elf, = struct section *rsec, reloc->sym =3D sym; reloc->addend =3D addend; =20 + if (elf_write_reloc(elf, reloc)) + return NULL; + list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); list_add_tail(&reloc->list, &rsec->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); =20 - mark_sec_changed(elf, rsec, true); - return reloc; } =20 @@ -1203,31 +1207,6 @@ struct section *elf_create_section_pair(struct elf *= elf, const char *name, return sec; } =20 -static int elf_rebuild_reloc_section(struct elf *elf, struct section *rsec) -{ - struct reloc *reloc; - int idx =3D 0, ret; - - idx =3D 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { - reloc->rel.r_offset =3D reloc->offset; - reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); - if (rsec->sh.sh_type =3D=3D SHT_RELA) { - reloc->rela.r_addend =3D reloc->addend; - ret =3D gelf_update_rela(rsec->data, idx, &reloc->rela); - } else { - ret =3D gelf_update_rel(rsec->data, idx, &reloc->rel); - } - if (!ret) { - WARN_ELF("gelf_update_rel"); - return -1; - } - idx++; - } - - return 0; -} - int elf_write_insn(struct elf *elf, struct section *sec, unsigned long offset, unsigned int len, const char *insn) @@ -1351,12 +1330,6 @@ int elf_write(struct elf *elf) return -1; } =20 - if (sec->base && - elf_rebuild_reloc_section(elf, sec)) { - WARN("elf_rebuild_reloc_section"); - return -1; - } - mark_sec_changed(elf, sec, false); } } --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34D48C7EE23 for ; Tue, 30 May 2023 17:22:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233001AbjE3RWB (ORCPT ); Tue, 30 May 2023 13:22:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232657AbjE3RVi (ORCPT ); Tue, 30 May 2023 13:21:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E007F1 for ; Tue, 30 May 2023 10:21:36 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CEF006312A for ; Tue, 30 May 2023 17:21:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD3D7C433A7; Tue, 30 May 2023 17:21:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467295; bh=d0V+rEHElAyacw8AbdTR1mJCInv/LtANWszfGeTQmgw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F8yDNQfTKA9nvMlc46CPI5hnKobBApcx8UpbyQ1/1Jd06THr0c1Yrc29KuHgNJONw L92oBaxPY9PsXAmIEgotQuKIHrBkiBC5GVST6kWNDK0uY+8YBWNgPibY1j4B43ldNH 562+wEDDcglgp32ZVVEowehtdTQbu8a8m+AzjLqnok5n0PNYOhfgNJfJFTbsJVEYk4 QiWITQu8W4BiqW6AzlDvZkF4NvH7PLwWEkcDs4w0GFg2PXNyB24+4KOb8AAWss5c+W 8FlrJpL2uKKklAZ58mASkOL3Kecc7GquZ+e9++L4rvpc0NfyWRlNGehVlJUYSokNeI QzUtIam/mbfug== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 09/22] objtool: Don't free memory in elf_close() Date: Tue, 30 May 2023 10:21:01 -0700 Message-Id: <74bdb3058b8f029db8d5b3b5175f2a200804196d.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" It's not necessary, objtool's about to exit anyway. Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 8d491b2d123e..8d53f18a9502 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -1350,30 +1350,14 @@ int elf_write(struct elf *elf) =20 void elf_close(struct elf *elf) { - struct section *sec, *tmpsec; - struct symbol *sym, *tmpsym; - struct reloc *reloc, *tmpreloc; - if (elf->elf) elf_end(elf->elf); =20 if (elf->fd > 0) close(elf->fd); =20 - list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { - list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) { - list_del(&sym->list); - hash_del(&sym->hash); - } - list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) { - list_del(&reloc->list); - hash_del(&reloc->hash); - } - list_del(&sec->list); - free(sec->reloc_data); - } - - free(elf->symbol_data); - free(elf->section_data); - free(elf); + /* + * NOTE: All remaining allocations are leaked on purpose. Objtool is + * about to exit anyway. + */ } --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A8BCC77B7A for ; Tue, 30 May 2023 17:22:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233276AbjE3RWO (ORCPT ); Tue, 30 May 2023 13:22:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232942AbjE3RVj (ORCPT ); Tue, 30 May 2023 13:21:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45AF7C5 for ; Tue, 30 May 2023 10:21:36 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BDDC963119 for ; Tue, 30 May 2023 17:21:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6362CC433D2; Tue, 30 May 2023 17:21:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467295; bh=ahsLwQFtwGTVxZ23xRH22wo/YjNgv9FRU2AJq+x5RMQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OWzvEeVSYvQlaQGPfxNwl5nzNeioFO0LUl3VrwYzvS7s2yCRICZBPsaPuU62KBtqY 3UAScF7gOdWgQC7XVtY6fL8dnFFZ4XQuqh1nBd0n7V9im5h+XJBtMwnhPzcT9g98lR vj8TSZkjh+eKiG2zcccrGYq2Bl/qm519Uj850VMYcVOI64DenethuXFmM7tPvAuvgd suY1uIsaEFkfuWvSLDSnRlVnrY6WtMM7k6JVEdsZUY/HzgxMukDpHz61kO3BUFW02F 8d4jBAAZYfEGLoAh6NRGP9xtXMzjv16+xYWt5uYXcr+oICFSuIz7vdo/MlpARIyXG4 dOG+ARt/I23QQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 10/22] objtool: Add for_each_reloc() Date: Tue, 30 May 2023 10:21:02 -0700 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 24 ++++++++++++------------ tools/objtool/include/objtool/elf.h | 6 ++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 88f54bb1b01c..1355e6fec7d6 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -590,7 +590,7 @@ static int add_dead_ends(struct objtool_file *file) if (!rsec) goto reachable; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; @@ -625,7 +625,7 @@ static int add_dead_ends(struct objtool_file *file) if (!rsec) return 0; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; @@ -1018,7 +1018,7 @@ static void add_ignores(struct objtool_file *file) if (!rsec) return; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { switch (reloc->sym->type) { case STT_FUNC: func =3D reloc->sym; @@ -1259,7 +1259,7 @@ static int add_ignore_alternatives(struct objtool_fil= e *file) if (!rsec) return 0; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; @@ -1990,7 +1990,7 @@ static int add_jump_table(struct objtool_file *file, = struct instruction *insn, * Each @reloc is a switch table relocation which points to the target * instruction. */ - list_for_each_entry_from(reloc, &table->sec->reloc_list, list) { + for_each_reloc_from(table->sec, reloc) { =20 /* Check for the end of the table: */ if (reloc !=3D table && reloc->jump_table_start) @@ -2264,7 +2264,7 @@ static int read_noendbr_hints(struct objtool_file *fi= le) if (!rsec) return 0; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { insn =3D find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->ad= dend); if (!insn) { WARN("bad .discard.noendbr entry"); @@ -2287,7 +2287,7 @@ static int read_retpoline_hints(struct objtool_file *= file) if (!rsec) return 0; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; @@ -2323,7 +2323,7 @@ static int read_instr_hints(struct objtool_file *file) if (!rsec) return 0; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; @@ -2342,7 +2342,7 @@ static int read_instr_hints(struct objtool_file *file) if (!rsec) return 0; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; @@ -2370,7 +2370,7 @@ static int read_validate_unret_hints(struct objtool_f= ile *file) if (!rsec) return 0; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; @@ -2398,7 +2398,7 @@ static int read_intra_function_calls(struct objtool_f= ile *file) if (!rsec) return 0; =20 - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { unsigned long dest_off; =20 if (reloc->sym->type !=3D STT_SECTION) { @@ -4395,7 +4395,7 @@ static int validate_ibt(struct objtool_file *file) strstr(sec->name, "__patchable_function_entries")) continue; =20 - list_for_each_entry(reloc, &sec->rsec->reloc_list, list) + for_each_reloc(sec->rsec, reloc) warnings +=3D validate_ibt_data_reloc(file, reloc); } =20 diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 74f63934afd3..a938cb1d4172 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -209,6 +209,12 @@ static inline void mark_sec_changed(struct elf *elf, s= truct section *sec, for_each_sec(file, __sec) \ sec_for_each_sym(__sec, sym) =20 +#define for_each_reloc(rsec, reloc) \ + list_for_each_entry(reloc, &rsec->reloc_list, list) + +#define for_each_reloc_from(rsec, reloc) \ + list_for_each_entry_from(reloc, &rsec->reloc_list, list) + #define OFFSET_STRIDE_BITS 4 #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) #define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1)) --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE739C7EE24 for ; Tue, 30 May 2023 17:22:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233291AbjE3RWU (ORCPT ); Tue, 30 May 2023 13:22:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232964AbjE3RVk (ORCPT ); Tue, 30 May 2023 13:21:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D2D1F3 for ; Tue, 30 May 2023 10:21:36 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 77EEF63104 for ; Tue, 30 May 2023 17:21:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B477DC4339E; Tue, 30 May 2023 17:21:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467295; bh=5bKLUxUNwXL7zbFSdm1bk4h9wh+okZrHItfELV5Rer8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sDI8gjeyCx8VJZPs5/Q+PYBmp/xKv0tWOcR7wXGh52Nysw5W4vbK87wbGXynvcBV0 L+xaZ2Jz/MogFnOZ/fkKdm/BnHPFkYTiz4OarWEswn/W9C4RGOlf5AVpDv/Sgw8hRn J67seHDmXbTJPpjmx1g5nFzVytRSrgB5iQTNsz7Trtcz7s4OKHA24AeaMW1kU665+J ltZSMJMbsEGwceccQ7donK1Yir8UNleyR1Jz28hjmr8hto0W2JqzrVxQxcLAhByqXK f01AOv8QIi5t03mrjbNodPmY1XQ785vNu+SEQtpq+kAJ03KUZ7cwK4KMbJ7RXUah63 y+m0jEUmFzTgw== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 11/22] objtool: Allocate relocs in advance for new rela sections Date: Tue, 30 May 2023 10:21:03 -0700 Message-Id: <5332d845c5a2d6c2d052075b381bfba8bcb67ed5.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Similar to read_relocs(), allocate the reloc structs all together in an array rather than allocating them one at a time. Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 8d53f18a9502..5f69d4590117 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -814,7 +814,7 @@ static struct reloc *elf_init_reloc(struct elf *elf, st= ruct section *rsec, unsigned long offset, struct symbol *sym, s64 addend, unsigned int type) { - struct reloc *reloc; + struct reloc *reloc, empty =3D { 0 }; =20 if (reloc_idx >=3D rsec->sh.sh_size / elf_rela_size(elf)) { WARN("%s: bad reloc_idx %u for %s with size 0x%lx", @@ -822,12 +822,13 @@ static struct reloc *elf_init_reloc(struct elf *elf, = struct section *rsec, return NULL; } =20 - reloc =3D malloc(sizeof(*reloc)); - if (!reloc) { - perror("malloc"); + reloc =3D &rsec->reloc_data[reloc_idx]; + + if (memcmp(reloc, &empty, sizeof(empty))) { + WARN("%s: %s: reloc %d already initialized!", + __func__, rsec->name, reloc_idx); return NULL; } - memset(reloc, 0, sizeof(*reloc)); =20 reloc->idx =3D reloc_idx; reloc->sec =3D rsec; @@ -1185,6 +1186,13 @@ static struct section *elf_create_rela_section(struc= t elf *elf, rsec->sh.sh_info =3D sec->idx; rsec->sh.sh_flags =3D SHF_INFO_LINK; =20 + rsec->reloc_data =3D calloc(rsec->sh.sh_size / rsec->sh.sh_entsize, + sizeof(struct reloc)); + if (!rsec->reloc_data) { + perror("calloc"); + return NULL; + } + sec->rsec =3D rsec; rsec->base =3D sec; =20 --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07FA1C7EE23 for ; Tue, 30 May 2023 17:22:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233389AbjE3RWZ (ORCPT ); Tue, 30 May 2023 13:22:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233048AbjE3RVl (ORCPT ); Tue, 30 May 2023 13:21:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5564EE5 for ; Tue, 30 May 2023 10:21:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C85E963134 for ; Tue, 30 May 2023 17:21:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11BFBC4339C; Tue, 30 May 2023 17:21:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467296; bh=SzgZPlsWLNkI7F0pgFK7KJ4T9CY98WR4ayNrJxiorE0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NUXDqUKIDIImmiZODxKha9iN5Q5GMhFqDe7ZB+R7+5YeCCtP/RR4xoeHSA3c09NsQ D1ngrfqvcapjbqIb9sJw4TRF/XkjipantOIA7GB4gQmIHJT/pGW6qaY0x7KPo8SGRC ONZuegUvukCaa/hYqjlwskUpFs8DG3MkenJ1lR4wYmsliyg+5A9c3PKEtC6tOCsaaJ ieP7YhZQiJGRiM0B/V7d4ElgNnwpqzrMXCftm9j+0j8ePGQU/NCNMf/YPWb8SPE0/z lnLC04VApjTYbsEssluA6tWxXh32i1Vy7GI0BzCnouGXhM/2XL9uqMwzSNgdzp33df PiYGOK4WkSkCg== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 12/22] objtool: Get rid of reloc->list Date: Tue, 30 May 2023 10:21:04 -0700 Message-Id: <71e7a2c017dbc46bb497857ec97d67214f832d10.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now that all relocs are allocated in an array, the linked list is no longer needed. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 49.02G - After: peak heap memory consumption: 45.56G Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 32 ++++++++++++----------------- tools/objtool/include/objtool/elf.h | 18 +++++++++++----- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 5f69d4590117..19ac53ad76ee 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -293,7 +293,6 @@ static int read_sections(struct elf *elf) sec =3D &elf->section_data[i]; =20 INIT_LIST_HEAD(&sec->symbol_list); - INIT_LIST_HEAD(&sec->reloc_list); =20 s =3D elf_getscn(elf->elf, i); if (!s) { @@ -333,7 +332,7 @@ static int read_sections(struct elf *elf) elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); =20 if (is_reloc_sec(sec)) - elf->num_relocs +=3D sec->sh.sh_size / sec->sh.sh_entsize; + elf->num_relocs +=3D sec_num_entries(sec); } =20 if (opts.stats) { @@ -407,7 +406,7 @@ static int read_symbols(struct elf *elf) if (symtab_shndx) shndx_data =3D symtab_shndx->data; =20 - symbols_nr =3D symtab->sh.sh_size / symtab->sh.sh_entsize; + symbols_nr =3D sec_num_entries(symtab); } else { /* * A missing symbol table is actually possible if it's an empty @@ -701,7 +700,7 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym) return NULL; } =20 - new_idx =3D symtab->sh.sh_size / symtab->sh.sh_entsize; + new_idx =3D sec_num_entries(symtab); =20 if (GELF_ST_BIND(sym->sym.st_info) !=3D STB_LOCAL) goto non_local; @@ -816,13 +815,13 @@ static struct reloc *elf_init_reloc(struct elf *elf, = struct section *rsec, { struct reloc *reloc, empty =3D { 0 }; =20 - if (reloc_idx >=3D rsec->sh.sh_size / elf_rela_size(elf)) { - WARN("%s: bad reloc_idx %u for %s with size 0x%lx", - __func__, reloc_idx, rsec->name, rsec->sh.sh_size); + if (reloc_idx >=3D sec_num_entries(rsec)) { + WARN("%s: bad reloc_idx %u for %s with %d relocs", + __func__, reloc_idx, rsec->name, sec_num_entries(rsec)); return NULL; } =20 - reloc =3D &rsec->reloc_data[reloc_idx]; + reloc =3D &rsec->relocs[reloc_idx]; =20 if (memcmp(reloc, &empty, sizeof(empty))) { WARN("%s: %s: reloc %d already initialized!", @@ -841,7 +840,6 @@ static struct reloc *elf_init_reloc(struct elf *elf, st= ruct section *rsec, return NULL; =20 list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); - list_add_tail(&reloc->list, &rsec->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); =20 return reloc; @@ -944,14 +942,13 @@ static int read_relocs(struct elf *elf) rsec->base->rsec =3D rsec; =20 nr_reloc =3D 0; - rsec->reloc_data =3D calloc(rsec->sh.sh_size / rsec->sh.sh_entsize, - sizeof(*reloc)); - if (!rsec->reloc_data) { + rsec->relocs =3D calloc(sec_num_entries(rsec), sizeof(*reloc)); + if (!rsec->relocs) { perror("calloc"); return -1; } - for (i =3D 0; i < rsec->sh.sh_size / rsec->sh.sh_entsize; i++) { - reloc =3D &rsec->reloc_data[i]; + for (i =3D 0; i < sec_num_entries(rsec); i++) { + reloc =3D &rsec->relocs[i]; =20 if (read_reloc(rsec, i, reloc)) return -1; @@ -967,7 +964,6 @@ static int read_relocs(struct elf *elf) } =20 list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); - list_add_tail(&reloc->list, &rsec->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); =20 nr_reloc++; @@ -1093,7 +1089,6 @@ struct section *elf_create_section(struct elf *elf, c= onst char *name, memset(sec, 0, sizeof(*sec)); =20 INIT_LIST_HEAD(&sec->symbol_list); - INIT_LIST_HEAD(&sec->reloc_list); =20 s =3D elf_newscn(elf->elf); if (!s) { @@ -1186,9 +1181,8 @@ static struct section *elf_create_rela_section(struct= elf *elf, rsec->sh.sh_info =3D sec->idx; rsec->sh.sh_flags =3D SHF_INFO_LINK; =20 - rsec->reloc_data =3D calloc(rsec->sh.sh_size / rsec->sh.sh_entsize, - sizeof(struct reloc)); - if (!rsec->reloc_data) { + rsec->relocs =3D calloc(sec_num_entries(rsec), sizeof(struct reloc)); + if (!rsec->relocs) { perror("calloc"); return NULL; } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index a938cb1d4172..a09da208ddb8 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -33,14 +33,13 @@ struct section { GElf_Shdr sh; struct rb_root_cached symbol_tree; struct list_head symbol_list; - struct list_head reloc_list; struct section *base, *rsec; struct symbol *sym; Elf_Data *data; char *name; int idx; bool _changed, text, rodata, noinstr, init, truncate; - struct reloc *reloc_data; + struct reloc *relocs; }; =20 struct symbol { @@ -68,7 +67,6 @@ struct symbol { }; =20 struct reloc { - struct list_head list; struct hlist_node hash; union { GElf_Rela rela; @@ -197,6 +195,11 @@ static inline void mark_sec_changed(struct elf *elf, s= truct section *sec, elf->changed |=3D changed; } =20 +static inline unsigned int sec_num_entries(struct section *sec) +{ + return sec->sh.sh_size / sec->sh.sh_entsize; +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) =20 @@ -210,10 +213,15 @@ static inline void mark_sec_changed(struct elf *elf, = struct section *sec, sec_for_each_sym(__sec, sym) =20 #define for_each_reloc(rsec, reloc) \ - list_for_each_entry(reloc, &rsec->reloc_list, list) + for (int __i =3D 0, __fake =3D 1; __fake; __fake =3D 0) \ + for (reloc =3D rsec->relocs; \ + __i < sec_num_entries(rsec); \ + __i++, reloc++) =20 #define for_each_reloc_from(rsec, reloc) \ - list_for_each_entry_from(reloc, &rsec->reloc_list, list) + for (int __i =3D reloc->idx; \ + __i < sec_num_entries(rsec); \ + __i++, reloc++) =20 #define OFFSET_STRIDE_BITS 4 #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D6A3C77B7A for ; Tue, 30 May 2023 17:22:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233207AbjE3RWR (ORCPT ); Tue, 30 May 2023 13:22:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232951AbjE3RVk (ORCPT ); Tue, 30 May 2023 13:21:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48E84A3 for ; Tue, 30 May 2023 10:21:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BDE4063132 for ; Tue, 30 May 2023 17:21:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64178C433A7; Tue, 30 May 2023 17:21:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467296; bh=HlkIG7/lxu8Z5s71NR4vy/FPtwUZQJbUdoT7MnQ0tW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=elaSImFIdZdrh5zUJAZbamkTBBWmB/G5gb4R5nDCe89iOK7EhU0qPE1Ww/bt0hAbW BOLYSm2+v4LE4r28TUhHad6gYSbWpxTS+xjTCJcsCd9bqOtdyh8Quo6O2UJQRi0zU7 w2dkDzzkxPW4UJbUjml3HEYKs4lHsoQ3h0MK42pyDGzONhi4wjf+nixZ01lgKDWSRl eyOCI83b20wPauHlfpDlFSE2DsZyYISFT2GwTNwQO7OgvkuRco9Ult+WCMCukZnOYl kphyLS2RwJeRwO7GRd4DHcWWTO1mfdQJfyCbbo1y1JCAlcDXutbPnQ7ljPkUjyBWh1 +HAthGrhF5lkg== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 13/22] objtool: Get rid of reloc->idx Date: Tue, 30 May 2023 10:21:05 -0700 Message-Id: <7351d2ebad0519027db14a32f6204af84952574a.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Use the array offset to calculate the reloc index. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 45.56G - After: peak heap memory consumption: 43.83G Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 6 ++---- tools/objtool/include/objtool/elf.h | 10 +++++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 19ac53ad76ee..70c801254566 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -829,7 +829,6 @@ static struct reloc *elf_init_reloc(struct elf *elf, st= ruct section *rsec, return NULL; } =20 - reloc->idx =3D reloc_idx; reloc->sec =3D rsec; reloc->offset =3D offset; reloc->type =3D type; @@ -954,7 +953,6 @@ static int read_relocs(struct elf *elf) return -1; =20 reloc->sec =3D rsec; - reloc->idx =3D i; symndx =3D GELF_R_SYM(reloc->rel.r_info); reloc->sym =3D sym =3D find_symbol_by_index(elf, symndx); if (!reloc->sym) { @@ -1237,9 +1235,9 @@ int elf_write_reloc(struct elf *elf, struct reloc *re= loc) =20 if (rsec->sh.sh_type =3D=3D SHT_RELA) { reloc->rela.r_addend =3D reloc->addend; - ret =3D gelf_update_rela(rsec->data, reloc->idx, &reloc->rela); + ret =3D gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); } else { - ret =3D gelf_update_rel(rsec->data, reloc->idx, &reloc->rel); + ret =3D gelf_update_rel(rsec->data, reloc_idx(reloc), &reloc->rel); } =20 if (!ret) { diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index a09da208ddb8..2a14da633d56 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -76,9 +76,8 @@ struct reloc { struct symbol *sym; struct list_head sym_reloc_entry; unsigned long offset; - unsigned int type; s64 addend; - int idx; + unsigned int type; bool jump_table_start; }; =20 @@ -200,6 +199,11 @@ static inline unsigned int sec_num_entries(struct sect= ion *sec) return sec->sh.sh_size / sec->sh.sh_entsize; } =20 +static inline unsigned int reloc_idx(struct reloc *reloc) +{ + return reloc - reloc->sec->relocs; +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) =20 @@ -219,7 +223,7 @@ static inline unsigned int sec_num_entries(struct secti= on *sec) __i++, reloc++) =20 #define for_each_reloc_from(rsec, reloc) \ - for (int __i =3D reloc->idx; \ + for (int __i =3D reloc_idx(reloc); \ __i < sec_num_entries(rsec); \ __i++, reloc++) =20 --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E32DCC77B7A for ; Tue, 30 May 2023 17:22:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233426AbjE3RWe (ORCPT ); Tue, 30 May 2023 13:22:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233010AbjE3RVl (ORCPT ); Tue, 30 May 2023 13:21:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE2F398 for ; Tue, 30 May 2023 10:21:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7A83E63135 for ; Tue, 30 May 2023 17:21:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4455C4339B; Tue, 30 May 2023 17:21:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467296; bh=9ZwKWAhUoEay6Gr+X0+W5r9EsaeSEGXdmDt4xMDFxig=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uOp3p6KQSqRlUXTu0MO4j7jwG+tr59MrcAb6UNI7EASTcyL1slOqos+h3GT+i8rsA z98k0FK2n7YqPSjWTaME0/X1wIFB0MzPMqcrmxzehqovSUdJ5juEsupsfwDf41wWo0 n5jH8AGZ6F06BhZ5ZfxSiA1AKs8PKvT+NwkzMXdXGooU26T4AOdrL5Wf3MaIk2vw/X SdMEJibAB6X2Zj8Ld/0+yMyLdIjFupW91skSdRfAOBktfJz1yZTNU6ALcqI8wAgGpM pQCSqQeqpNADUGiZN4/S25W9bGQth9ff6pXUMzPfUGVrN9QDyEKNd7SknO+dfxCS/i GnWLaBABh6dXw== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 14/22] objtool: Get rid of reloc->offset Date: Tue, 30 May 2023 10:21:06 -0700 Message-Id: <2b9ec01178baa346a99522710bf2e82159412e3a.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Get the offset from the embedded GElf_Rel[a] struct. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 43.83G - After: peak heap memory consumption: 42.10G Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 14 +++++++------- tools/objtool/elf.c | 10 +++++----- tools/objtool/include/objtool/elf.h | 8 ++++++-- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 1355e6fec7d6..440c326121d4 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -510,11 +510,11 @@ static int add_pv_ops(struct objtool_file *file, cons= t char *symname) if (func->type =3D=3D STT_SECTION) func =3D find_symbol_by_offset(reloc->sym->sec, reloc->addend); =20 - idx =3D (reloc->offset - sym->offset) / sizeof(unsigned long); + idx =3D (reloc_offset(reloc) - sym->offset) / sizeof(unsigned long); =20 objtool_pv_add(file, idx, func); =20 - off =3D reloc->offset + 1; + off =3D reloc_offset(reloc) + 1; if (off > end) break; } @@ -1997,7 +1997,7 @@ static int add_jump_table(struct objtool_file *file, = struct instruction *insn, break; =20 /* Make sure the table entries are consecutive: */ - if (prev_offset && reloc->offset !=3D prev_offset + 8) + if (prev_offset && reloc_offset(reloc) !=3D prev_offset + 8) break; =20 /* Detect function pointers from contiguous objects: */ @@ -2022,7 +2022,7 @@ static int add_jump_table(struct objtool_file *file, = struct instruction *insn, alt->insn =3D dest_insn; alt->next =3D insn->alts; insn->alts =3D alt; - prev_offset =3D reloc->offset; + prev_offset =3D reloc_offset(reloc); } =20 if (!prev_offset) { @@ -4257,8 +4257,8 @@ static int validate_ibt_insn(struct objtool_file *fil= e, struct instruction *insn for (reloc =3D insn_reloc(file, insn); reloc; reloc =3D find_reloc_by_dest_range(file->elf, insn->sec, - reloc->offset + 1, - (insn->offset + insn->len) - (reloc->offset + 1))) { + reloc_offset(reloc) + 1, + (insn->offset + insn->len) - (reloc_offset(reloc) + 1))) { =20 /* * static_call_update() references the trampoline, which @@ -4341,7 +4341,7 @@ static int validate_ibt_data_reloc(struct objtool_fil= e *file, return 0; =20 WARN_FUNC("data relocation to !ENDBR: %s", - reloc->sec->base, reloc->offset, + reloc->sec->base, reloc_offset(reloc), offstr(dest->sec, dest->offset)); =20 return 1; diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 70c801254566..2b45460225d1 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -246,8 +246,9 @@ struct reloc *find_reloc_by_dest_range(const struct elf= *elf, struct section *se if (reloc->sec !=3D rsec) continue; =20 - if (reloc->offset >=3D offset && reloc->offset < offset + len) { - if (!r || reloc->offset < r->offset) + if (reloc_offset(reloc) >=3D offset && + reloc_offset(reloc) < offset + len) { + if (!r || reloc_offset(reloc) < reloc_offset(r)) r =3D reloc; } } @@ -830,11 +831,12 @@ static struct reloc *elf_init_reloc(struct elf *elf, = struct section *rsec, } =20 reloc->sec =3D rsec; - reloc->offset =3D offset; reloc->type =3D type; reloc->sym =3D sym; reloc->addend =3D addend; =20 + reloc->rel.r_offset =3D offset; + if (elf_write_reloc(elf, reloc)) return NULL; =20 @@ -908,7 +910,6 @@ static int read_reloc(struct section *rsec, int i, stru= ct reloc *reloc) return -1; } =20 - reloc->offset =3D reloc->rel.r_offset; reloc->type =3D GELF_R_TYPE(reloc->rel.r_info); reloc->addend =3D rela ? reloc->rela.r_addend : 0; =20 @@ -1230,7 +1231,6 @@ int elf_write_reloc(struct elf *elf, struct reloc *re= loc) struct section *rsec =3D reloc->sec; int ret; =20 - reloc->rel.r_offset =3D reloc->offset; reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); =20 if (rsec->sh.sh_type =3D=3D SHT_RELA) { diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 2a14da633d56..2070860a099e 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -75,7 +75,6 @@ struct reloc { struct section *sec; struct symbol *sym; struct list_head sym_reloc_entry; - unsigned long offset; s64 addend; unsigned int type; bool jump_table_start; @@ -204,6 +203,11 @@ static inline unsigned int reloc_idx(struct reloc *rel= oc) return reloc - reloc->sec->relocs; } =20 +static inline unsigned long reloc_offset(struct reloc *reloc) +{ + return reloc->rel.r_offset; +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) =20 @@ -253,7 +257,7 @@ static inline u32 sec_offset_hash(struct section *sec, = unsigned long offset) =20 static inline u32 reloc_hash(struct reloc *reloc) { - return sec_offset_hash(reloc->sec, reloc->offset); + return sec_offset_hash(reloc->sec, reloc_offset(reloc)); } =20 #endif /* _OBJTOOL_ELF_H */ --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CD1CC7EE24 for ; Tue, 30 May 2023 17:22:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233431AbjE3RWg (ORCPT ); Tue, 30 May 2023 13:22:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233018AbjE3RVl (ORCPT ); Tue, 30 May 2023 13:21:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4899AF9 for ; Tue, 30 May 2023 10:21:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CC3186313A for ; Tue, 30 May 2023 17:21:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11C8EC433D2; Tue, 30 May 2023 17:21:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467297; bh=X33W17ZC/Rt3EvD2X+9vvXrqdrMNzh7cjLu7YmfFviY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=std+JsFZ2uczdRX0lNXPswXLmLYXVh447/2/kJ68S1HlEdoEvI2MbV3tFN+/Sx9PS v75+bPFHQY35tESdH+JBSkZuvNTzW0vik54/xrAOQLoehSNfWznRWQ6OKQDaZuGGeQ 11UAX7zpio69+XGVJVqH4Oqhpwi1f2kfSYiOODITR+ylPVx0xdfkaNx8EuF99R6MKl LbqeLqd2cxiZTbv7+ttB1nrt1c/KtT84H4xmcImfg+P7i1OOScjbPKH6F3JLhTXeXV v4eaX+vxaV/l6PrrdFLwyafc2mNTjNrC/2v7CizlhdYPZMA48pfFmyZDGQ5mym9krb SXcSNfALHMmYA== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 15/22] objtool: Get rid of reloc->type Date: Tue, 30 May 2023 10:21:07 -0700 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Get the type from the embedded GElf_Rel[a] struct. Signed-off-by: Josh Poimboeuf --- tools/objtool/arch/x86/decode.c | 2 +- tools/objtool/arch/x86/special.c | 4 ++-- tools/objtool/check.c | 11 ++++++----- tools/objtool/elf.c | 6 ++---- tools/objtool/include/objtool/elf.h | 11 ++++++++++- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decod= e.c index 9ef024fd648c..ffb12e83b238 100644 --- a/tools/objtool/arch/x86/decode.c +++ b/tools/objtool/arch/x86/decode.c @@ -84,7 +84,7 @@ bool arch_pc_relative_reloc(struct reloc *reloc) * All relocation types where P (the address of the target) * is included in the computation. */ - switch (reloc->type) { + switch (reloc_type(reloc)) { case R_X86_64_PC8: case R_X86_64_PC16: case R_X86_64_PC32: diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/spec= ial.c index 7c97b7391279..1a54a249cb50 100644 --- a/tools/objtool/arch/x86/special.c +++ b/tools/objtool/arch/x86/special.c @@ -108,7 +108,7 @@ struct reloc *arch_find_switch_table(struct objtool_fil= e *file, table_offset =3D text_reloc->addend; table_sec =3D text_reloc->sym->sec; =20 - if (text_reloc->type =3D=3D R_X86_64_PC32) + if (reloc_type(text_reloc) =3D=3D R_X86_64_PC32) table_offset +=3D 4; =20 /* @@ -138,7 +138,7 @@ struct reloc *arch_find_switch_table(struct objtool_fil= e *file, * indicates a rare GCC quirk/bug which can leave dead * code behind. */ - if (text_reloc->type =3D=3D R_X86_64_PC32) + if (reloc_type(text_reloc) =3D=3D R_X86_64_PC32) file->ignore_unreachables =3D true; =20 return rodata_reloc; diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 440c326121d4..1d794d15bc53 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -957,7 +957,7 @@ static int create_mcount_loc_sections(struct objtool_fi= le *file) if (!reloc) return -1; =20 - reloc->type =3D addr_size =3D=3D 8 ? R_ABS64 : R_ABS32; + set_reloc_type(reloc, addr_size =3D=3D 8 ? R_ABS64 : R_ABS32); =20 idx++; } @@ -1353,7 +1353,7 @@ static void annotate_call_site(struct objtool_file *f= ile, */ if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) { if (reloc) { - reloc->type =3D R_NONE; + set_reloc_type(reloc, R_NONE); elf_write_reloc(file->elf, reloc); } =20 @@ -1382,7 +1382,7 @@ static void annotate_call_site(struct objtool_file *f= ile, WARN_INSN(insn, "tail call to __fentry__ !?!?"); if (opts.mnop) { if (reloc) { - reloc->type =3D R_NONE; + set_reloc_type(reloc, R_NONE); elf_write_reloc(file->elf, reloc); } =20 @@ -1864,7 +1864,7 @@ static int handle_jump_alt(struct objtool_file *file, struct reloc *reloc =3D insn_reloc(file, orig_insn); =20 if (reloc) { - reloc->type =3D R_NONE; + set_reloc_type(reloc, R_NONE); elf_write_reloc(file->elf, reloc); } elf_write_insn(file->elf, orig_insn->sec, @@ -4268,7 +4268,8 @@ static int validate_ibt_insn(struct objtool_file *fil= e, struct instruction *insn continue; =20 off =3D reloc->sym->offset; - if (reloc->type =3D=3D R_X86_64_PC32 || reloc->type =3D=3D R_X86_64_PLT3= 2) + if (reloc_type(reloc) =3D=3D R_X86_64_PC32 || + reloc_type(reloc) =3D=3D R_X86_64_PLT32) off +=3D arch_dest_reloc_offset(reloc->addend); else off +=3D reloc->addend; diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 2b45460225d1..be9d24dcdf56 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -541,6 +541,7 @@ static int elf_update_sym_relocs(struct elf *elf, struc= t symbol *sym) struct reloc *reloc; =20 list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) { + reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc_type(reloc)); if (elf_write_reloc(elf, reloc)) return -1; } @@ -831,11 +832,11 @@ static struct reloc *elf_init_reloc(struct elf *elf, = struct section *rsec, } =20 reloc->sec =3D rsec; - reloc->type =3D type; reloc->sym =3D sym; reloc->addend =3D addend; =20 reloc->rel.r_offset =3D offset; + reloc->rel.r_info =3D GELF_R_INFO(sym->idx, type); =20 if (elf_write_reloc(elf, reloc)) return NULL; @@ -910,7 +911,6 @@ static int read_reloc(struct section *rsec, int i, stru= ct reloc *reloc) return -1; } =20 - reloc->type =3D GELF_R_TYPE(reloc->rel.r_info); reloc->addend =3D rela ? reloc->rela.r_addend : 0; =20 return 0; @@ -1231,8 +1231,6 @@ int elf_write_reloc(struct elf *elf, struct reloc *re= loc) struct section *rsec =3D reloc->sec; int ret; =20 - reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc->type); - if (rsec->sh.sh_type =3D=3D SHT_RELA) { reloc->rela.r_addend =3D reloc->addend; ret =3D gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 2070860a099e..41d2149f8bb8 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -76,7 +76,6 @@ struct reloc { struct symbol *sym; struct list_head sym_reloc_entry; s64 addend; - unsigned int type; bool jump_table_start; }; =20 @@ -208,6 +207,16 @@ static inline unsigned long reloc_offset(struct reloc = *reloc) return reloc->rel.r_offset; } =20 +static inline unsigned int reloc_type(struct reloc *reloc) +{ + return GELF_R_TYPE(reloc->rel.r_info); +} + +static inline void set_reloc_type(struct reloc *reloc, int type) +{ + reloc->rel.r_info =3D GELF_R_INFO(GELF_R_SYM(reloc->rel.r_info), type); +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) =20 --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2480BC7EE23 for ; Tue, 30 May 2023 17:22:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233440AbjE3RWo (ORCPT ); Tue, 30 May 2023 13:22:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233230AbjE3RVn (ORCPT ); Tue, 30 May 2023 13:21:43 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F9DEF7 for ; Tue, 30 May 2023 10:21:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BCB3063138 for ; Tue, 30 May 2023 17:21:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6289DC433A0; Tue, 30 May 2023 17:21:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467297; bh=pNfbAmu0/tic6xcfteWUfNk4M0kZWxM3aNAl/VUcN9U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kChLyN3RMKEm349Q59K+r3Bwcshk/vF0duCdtnLKqYmXwXbWFURcScsNVjas8VJiu Z/PE6hzQg1eSMRDBWsLwduTV3tGdymqtG58qVdMnPaTtA0r0DLwqGI6Ap/pqnypTag UwR5HqjWp5nJYekzctbf3yg4wnSfLYxCT0d9U270qWFT2f81h6O3x1mebY7ksdy3Sx EoIqSFRe69p/Z0SkC9x0lE0D+NaDyOXT13Eog4mUqNzb6bexZpQ6tVF5Rt0P/4mD+/ Ipgv9gkzgDRKyN7QP3oSZetMSqBYSeq4dUV1Iwbp8OdMUxfHaV0ZjggypAIwiWiH17 0Vkporxo7fbRw== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 16/22] objtool: Get rid of reloc->addend Date: Tue, 30 May 2023 10:21:08 -0700 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Get the addend from the embedded GElf_Rel[a] struct. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 42.10G - After: peak heap memory consumption: 40.37G Signed-off-by: Josh Poimboeuf --- tools/objtool/arch/x86/decode.c | 4 +- tools/objtool/arch/x86/special.c | 2 +- tools/objtool/check.c | 69 +++++++++++++++++------------ tools/objtool/elf.c | 10 ++--- tools/objtool/include/objtool/elf.h | 6 ++- tools/objtool/special.c | 4 +- 6 files changed, 53 insertions(+), 42 deletions(-) diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decod= e.c index ffb12e83b238..2e1caabecb18 100644 --- a/tools/objtool/arch/x86/decode.c +++ b/tools/objtool/arch/x86/decode.c @@ -623,11 +623,11 @@ int arch_decode_instruction(struct objtool_file *file= , const struct section *sec if (!immr || strcmp(immr->sym->name, "pv_ops")) break; =20 - idx =3D (immr->addend + 8) / sizeof(void *); + idx =3D (reloc_addend(immr) + 8) / sizeof(void *); =20 func =3D disp->sym; if (disp->sym->type =3D=3D STT_SECTION) - func =3D find_symbol_by_offset(disp->sym->sec, disp->addend); + func =3D find_symbol_by_offset(disp->sym->sec, reloc_addend(disp)); if (!func) { WARN("no func for pv_ops[]"); return -1; diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/spec= ial.c index 1a54a249cb50..65f48f35b97e 100644 --- a/tools/objtool/arch/x86/special.c +++ b/tools/objtool/arch/x86/special.c @@ -105,7 +105,7 @@ struct reloc *arch_find_switch_table(struct objtool_fil= e *file, !text_reloc->sym->sec->rodata) return NULL; =20 - table_offset =3D text_reloc->addend; + table_offset =3D reloc_addend(text_reloc); table_sec =3D text_reloc->sym->sec; =20 if (reloc_type(text_reloc) =3D=3D R_X86_64_PC32) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 1d794d15bc53..12a9e6fb3fe7 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -508,7 +508,8 @@ static int add_pv_ops(struct objtool_file *file, const = char *symname) =20 func =3D reloc->sym; if (func->type =3D=3D STT_SECTION) - func =3D find_symbol_by_offset(reloc->sym->sec, reloc->addend); + func =3D find_symbol_by_offset(reloc->sym->sec, + reloc_addend(reloc)); =20 idx =3D (reloc_offset(reloc) - sym->offset) / sizeof(unsigned long); =20 @@ -582,6 +583,7 @@ static int add_dead_ends(struct objtool_file *file) struct section *rsec; struct reloc *reloc; struct instruction *insn; + s64 addend; =20 /* * Check for manually annotated dead ends. @@ -591,23 +593,27 @@ static int add_dead_ends(struct objtool_file *file) goto reachable; =20 for_each_reloc(rsec, reloc) { + if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + + addend =3D reloc_addend(reloc); + + insn =3D find_insn(file, reloc->sym->sec, addend); if (insn) insn =3D prev_insn_same_sec(file, insn); - else if (reloc->addend =3D=3D reloc->sym->sec->sh.sh_size) { + else if (addend =3D=3D reloc->sym->sec->sh.sh_size) { insn =3D find_last_insn(file, reloc->sym->sec); if (!insn) { WARN("can't find unreachable insn at %s+0x%" PRIx64, - reloc->sym->sec->name, reloc->addend); + reloc->sym->sec->name, addend); return -1; } } else { WARN("can't find unreachable insn at %s+0x%" PRIx64, - reloc->sym->sec->name, reloc->addend); + reloc->sym->sec->name, addend); return -1; } =20 @@ -626,23 +632,27 @@ static int add_dead_ends(struct objtool_file *file) return 0; =20 for_each_reloc(rsec, reloc) { + if (reloc->sym->type !=3D STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + + addend =3D reloc_addend(reloc); + + insn =3D find_insn(file, reloc->sym->sec, addend); if (insn) insn =3D prev_insn_same_sec(file, insn); - else if (reloc->addend =3D=3D reloc->sym->sec->sh.sh_size) { + else if (addend =3D=3D reloc->sym->sec->sh.sh_size) { insn =3D find_last_insn(file, reloc->sym->sec); if (!insn) { WARN("can't find reachable insn at %s+0x%" PRIx64, - reloc->sym->sec->name, reloc->addend); + reloc->sym->sec->name, addend); return -1; } } else { WARN("can't find reachable insn at %s+0x%" PRIx64, - reloc->sym->sec->name, reloc->addend); + reloc->sym->sec->name, addend); return -1; } =20 @@ -1025,7 +1035,7 @@ static void add_ignores(struct objtool_file *file) break; =20 case STT_SECTION: - func =3D find_func_by_offset(reloc->sym->sec, reloc->addend); + func =3D find_func_by_offset(reloc->sym->sec, reloc_addend(reloc)); if (!func) continue; break; @@ -1265,7 +1275,7 @@ static int add_ignore_alternatives(struct objtool_fil= e *file) return -1; } =20 - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!insn) { WARN("bad .discard.ignore_alts entry"); return -1; @@ -1541,7 +1551,7 @@ static int add_jump_destinations(struct objtool_file = *file) dest_off =3D arch_jump_destination(insn); } else if (reloc->sym->type =3D=3D STT_SECTION) { dest_sec =3D reloc->sym->sec; - dest_off =3D arch_dest_reloc_offset(reloc->addend); + dest_off =3D arch_dest_reloc_offset(reloc_addend(reloc)); } else if (reloc->sym->retpoline_thunk) { add_retpoline_call(file, insn); continue; @@ -1558,7 +1568,7 @@ static int add_jump_destinations(struct objtool_file = *file) } else if (reloc->sym->sec->idx) { dest_sec =3D reloc->sym->sec; dest_off =3D reloc->sym->sym.st_value + - arch_dest_reloc_offset(reloc->addend); + arch_dest_reloc_offset(reloc_addend(reloc)); } else { /* non-func asm code jumping to another file */ continue; @@ -1675,7 +1685,7 @@ static int add_call_destinations(struct objtool_file = *file) } =20 } else if (reloc->sym->type =3D=3D STT_SECTION) { - dest_off =3D arch_dest_reloc_offset(reloc->addend); + dest_off =3D arch_dest_reloc_offset(reloc_addend(reloc)); dest =3D find_call_destination(reloc->sym->sec, dest_off); if (!dest) { WARN_INSN(insn, "can't find call dest symbol at %s+0x%lx", @@ -2002,10 +2012,10 @@ static int add_jump_table(struct objtool_file *file= , struct instruction *insn, =20 /* Detect function pointers from contiguous objects: */ if (reloc->sym->sec =3D=3D pfunc->sec && - reloc->addend =3D=3D pfunc->offset) + reloc_addend(reloc) =3D=3D pfunc->offset) break; =20 - dest_insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + dest_insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!dest_insn) break; =20 @@ -2066,7 +2076,7 @@ static struct reloc *find_jump_table(struct objtool_f= ile *file, table_reloc =3D arch_find_switch_table(file, insn); if (!table_reloc) continue; - dest_insn =3D find_insn(file, table_reloc->sym->sec, table_reloc->addend= ); + dest_insn =3D find_insn(file, table_reloc->sym->sec, reloc_addend(table_= reloc)); if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != =3D func) continue; =20 @@ -2202,7 +2212,7 @@ static int read_unwind_hints(struct objtool_file *fil= e) return -1; } =20 - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!insn) { WARN("can't find insn for unwind_hints[%d]", i); return -1; @@ -2265,7 +2275,8 @@ static int read_noendbr_hints(struct objtool_file *fi= le) return 0; =20 for_each_reloc(rsec, reloc) { - insn =3D find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->ad= dend); + insn =3D find_insn(file, reloc->sym->sec, + reloc->sym->offset + reloc_addend(reloc)); if (!insn) { WARN("bad .discard.noendbr entry"); return -1; @@ -2293,7 +2304,7 @@ static int read_retpoline_hints(struct objtool_file *= file) return -1; } =20 - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!insn) { WARN("bad .discard.retpoline_safe entry"); return -1; @@ -2329,7 +2340,7 @@ static int read_instr_hints(struct objtool_file *file) return -1; } =20 - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!insn) { WARN("bad .discard.instr_end entry"); return -1; @@ -2348,7 +2359,7 @@ static int read_instr_hints(struct objtool_file *file) return -1; } =20 - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!insn) { WARN("bad .discard.instr_begin entry"); return -1; @@ -2376,7 +2387,7 @@ static int read_validate_unret_hints(struct objtool_f= ile *file) return -1; } =20 - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!insn) { WARN("bad .discard.instr_end entry"); return -1; @@ -2407,7 +2418,7 @@ static int read_intra_function_calls(struct objtool_f= ile *file) return -1; } =20 - insn =3D find_insn(file, reloc->sym->sec, reloc->addend); + insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!insn) { WARN("bad .discard.intra_function_call entry"); return -1; @@ -3307,7 +3318,7 @@ static inline const char *call_dest_name(struct instr= uction *insn) =20 reloc =3D insn_reloc(NULL, insn); if (reloc && !strcmp(reloc->sym->name, "pv_ops")) { - idx =3D (reloc->addend / sizeof(void *)); + idx =3D (reloc_addend(reloc) / sizeof(void *)); snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx); return pvname; } @@ -3325,7 +3336,7 @@ static bool pv_call_dest(struct objtool_file *file, s= truct instruction *insn) if (!reloc || strcmp(reloc->sym->name, "pv_ops")) return false; =20 - idx =3D (arch_dest_reloc_offset(reloc->addend) / sizeof(void *)); + idx =3D (arch_dest_reloc_offset(reloc_addend(reloc)) / sizeof(void *)); =20 if (file->pv_ops[idx].clean) return true; @@ -4270,9 +4281,9 @@ static int validate_ibt_insn(struct objtool_file *fil= e, struct instruction *insn off =3D reloc->sym->offset; if (reloc_type(reloc) =3D=3D R_X86_64_PC32 || reloc_type(reloc) =3D=3D R_X86_64_PLT32) - off +=3D arch_dest_reloc_offset(reloc->addend); + off +=3D arch_dest_reloc_offset(reloc_addend(reloc)); else - off +=3D reloc->addend; + off +=3D reloc_addend(reloc); =20 dest =3D find_insn(file, reloc->sym->sec, off); if (!dest) @@ -4329,7 +4340,7 @@ static int validate_ibt_data_reloc(struct objtool_fil= e *file, struct instruction *dest; =20 dest =3D find_insn(file, reloc->sym->sec, - reloc->sym->offset + reloc->addend); + reloc->sym->offset + reloc_addend(reloc)); if (!dest) return 0; =20 diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index be9d24dcdf56..16e019a1762c 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -833,10 +833,10 @@ static struct reloc *elf_init_reloc(struct elf *elf, = struct section *rsec, =20 reloc->sec =3D rsec; reloc->sym =3D sym; - reloc->addend =3D addend; =20 reloc->rel.r_offset =3D offset; reloc->rel.r_info =3D GELF_R_INFO(sym->idx, type); + reloc->rela.r_addend =3D addend; =20 if (elf_write_reloc(elf, reloc)) return NULL; @@ -911,8 +911,6 @@ static int read_reloc(struct section *rsec, int i, stru= ct reloc *reloc) return -1; } =20 - reloc->addend =3D rela ? reloc->rela.r_addend : 0; - return 0; } =20 @@ -1231,12 +1229,10 @@ int elf_write_reloc(struct elf *elf, struct reloc *= reloc) struct section *rsec =3D reloc->sec; int ret; =20 - if (rsec->sh.sh_type =3D=3D SHT_RELA) { - reloc->rela.r_addend =3D reloc->addend; + if (rsec->sh.sh_type =3D=3D SHT_RELA) ret =3D gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); - } else { + else ret =3D gelf_update_rel(rsec->data, reloc_idx(reloc), &reloc->rel); - } =20 if (!ret) { WARN_ELF("gelf_update_rela"); diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 41d2149f8bb8..be08b32a93ee 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -75,7 +75,6 @@ struct reloc { struct section *sec; struct symbol *sym; struct list_head sym_reloc_entry; - s64 addend; bool jump_table_start; }; =20 @@ -217,6 +216,11 @@ static inline void set_reloc_type(struct reloc *reloc,= int type) reloc->rel.r_info =3D GELF_R_INFO(GELF_R_SYM(reloc->rel.r_info), type); } =20 +static inline s64 reloc_addend(struct reloc *reloc) +{ + return reloc->rela.r_addend; +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) =20 diff --git a/tools/objtool/special.c b/tools/objtool/special.c index baa85c31526b..91b1950f5bd8 100644 --- a/tools/objtool/special.c +++ b/tools/objtool/special.c @@ -62,7 +62,7 @@ static void reloc_to_sec_off(struct reloc *reloc, struct = section **sec, unsigned long *off) { *sec =3D reloc->sym->sec; - *off =3D reloc->sym->offset + reloc->addend; + *off =3D reloc->sym->offset + reloc_addend(reloc); } =20 static int get_alt_entry(struct elf *elf, const struct special_entry *entr= y, @@ -126,7 +126,7 @@ static int get_alt_entry(struct elf *elf, const struct = special_entry *entry, sec, offset + entry->key); return -1; } - alt->key_addend =3D key_reloc->addend; + alt->key_addend =3D reloc_addend(key_reloc); } =20 return 0; --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C6DEC77B7A for ; Tue, 30 May 2023 17:22:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233252AbjE3RWs (ORCPT ); Tue, 30 May 2023 13:22:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233239AbjE3RVo (ORCPT ); Tue, 30 May 2023 13:21:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BFB8EA for ; Tue, 30 May 2023 10:21:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 79AD46312F for ; Tue, 30 May 2023 17:21:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B34F1C4339C; Tue, 30 May 2023 17:21:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467297; bh=nigN3+rTGBIIkX5YK6qdZ6MMc2ZnMKe0BoHwqwd6Fek=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k+M69oU78MdEJXI9LIn8c8i+fZkQYa9WPHxaS7CmX1iegENkA4232pAZH69B9HoqJ gmi3V7rp7vybp+7CWaT/0eEdY8DlRPQ6yKIMUwuUlpjj8hukrXmFPcECoyZfetQoM7 JiM/R6ngcsJ2ZM3NSb7N41q5/82mOAjwA4pmu+KgNzeOUNNHTLuMrvuOy61VOWSUvs bz8voy/GPaqTFwsQaJJxqSvYvj7jDe2krDOXRDXtrYT8mzuV/D2kPRr5EKKjcWKeBE ZH7w2jSv80Jo302rCgG7EWji026vEv+VJG6S1SG8vFmbSYNXSxd4ZmhIcCyAwLTMbP /6R5MLlk8dn7A== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 17/22] objtool: Get rid of reloc->jump_table_start Date: Tue, 30 May 2023 10:21:09 -0700 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rework the jump table logic slightly so 'jump_table_start' is no longer needed. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 40.37G - After: peak heap memory consumption: 38.64G Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 35 +++++++++++++++++++---------- tools/objtool/include/objtool/elf.h | 1 - 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 12a9e6fb3fe7..3fe6b3657e22 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1988,13 +1988,14 @@ static int add_special_section_alts(struct objtool_= file *file) } =20 static int add_jump_table(struct objtool_file *file, struct instruction *i= nsn, - struct reloc *table) + struct reloc *next_table) { - struct reloc *reloc =3D table; - struct instruction *dest_insn; - struct alternative *alt; struct symbol *pfunc =3D insn_func(insn)->pfunc; + struct reloc *table =3D insn_jump_table(insn); + struct instruction *dest_insn; unsigned int prev_offset =3D 0; + struct reloc *reloc =3D table; + struct alternative *alt; =20 /* * Each @reloc is a switch table relocation which points to the target @@ -2003,7 +2004,7 @@ static int add_jump_table(struct objtool_file *file, = struct instruction *insn, for_each_reloc_from(table->sec, reloc) { =20 /* Check for the end of the table: */ - if (reloc !=3D table && reloc->jump_table_start) + if (reloc !=3D table && reloc =3D=3D next_table) break; =20 /* Make sure the table entries are consecutive: */ @@ -2118,29 +2119,39 @@ static void mark_func_jump_tables(struct objtool_fi= le *file, continue; =20 reloc =3D find_jump_table(file, func, insn); - if (reloc) { - reloc->jump_table_start =3D true; + if (reloc) insn->_jump_table =3D reloc; - } } } =20 static int add_func_jump_tables(struct objtool_file *file, struct symbol *func) { - struct instruction *insn; - int ret; + struct instruction *insn, *insn_t1 =3D NULL, *insn_t2; + int ret =3D 0; =20 func_for_each_insn(file, func, insn) { if (!insn_jump_table(insn)) continue; =20 - ret =3D add_jump_table(file, insn, insn_jump_table(insn)); + if (!insn_t1) { + insn_t1 =3D insn; + continue; + } + + insn_t2 =3D insn; + + ret =3D add_jump_table(file, insn_t1, insn_jump_table(insn_t2)); if (ret) return ret; + + insn_t1 =3D insn_t2; } =20 - return 0; + if (insn_t1) + ret =3D add_jump_table(file, insn_t1, NULL); + + return ret; } =20 /* diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index be08b32a93ee..60686f746c9e 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -75,7 +75,6 @@ struct reloc { struct section *sec; struct symbol *sym; struct list_head sym_reloc_entry; - bool jump_table_start; }; =20 struct elf { --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25DC0C7EE23 for ; Tue, 30 May 2023 17:22:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233436AbjE3RWk (ORCPT ); Tue, 30 May 2023 13:22:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233237AbjE3RVo (ORCPT ); Tue, 30 May 2023 13:21:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB62BE for ; Tue, 30 May 2023 10:21:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C6C4B6314D for ; Tue, 30 May 2023 17:21:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 103B1C433D2; Tue, 30 May 2023 17:21:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467298; bh=6IoUil4pi2vqJ2egXzU2ifWGGynkECsbxuV8rRcIrsA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EtNkEs+DQNw3m2EDY+JnKtgWFehlb+1Id6wTd8gOBuASpMrK0RzedKPnjueqXPBvp ukItez3LW3k3NNKgnNM3RREcWDPZx3qet8/hsdNwn+Soy7CpKwR/v17fbFl5LfaHBa 2GDADgxQuIYci1wk6rtQdExheBglKeBCkkJ7m79jP97+gcWI9o8zkyeNfY5KVTKRkj XeQHBzwVB+Bg+YjKh7RrvXYwE0PQpXg7cVtfHH4tr6dH+L0yc/msShU/e/csmPHehS H3fxPvRkTxxhW8v8zcKBJNX7MbFaQ/RSzST6/iU0LYAJ6WZw/LmeCQkWPKshyrUEqO T4lCDB3e+Q4AQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 18/22] objtool: Shrink reloc->sym_reloc_entry Date: Tue, 30 May 2023 10:21:10 -0700 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Convert it to a singly-linked list. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 38.64G - After: peak heap memory consumption: 36.89G Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 9 +++++---- tools/objtool/include/objtool/elf.h | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 16e019a1762c..4b0de0e56068 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -356,7 +356,6 @@ static void elf_add_symbol(struct elf *elf, struct symb= ol *sym) struct rb_node *pnode; struct symbol *iter; =20 - INIT_LIST_HEAD(&sym->reloc_list); INIT_LIST_HEAD(&sym->pv_target); sym->alias =3D sym; =20 @@ -540,7 +539,7 @@ static int elf_update_sym_relocs(struct elf *elf, struc= t symbol *sym) { struct reloc *reloc; =20 - list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) { + for (reloc =3D sym->relocs; reloc; reloc =3D reloc->sym_next_reloc) { reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc_type(reloc)); if (elf_write_reloc(elf, reloc)) return -1; @@ -841,8 +840,9 @@ static struct reloc *elf_init_reloc(struct elf *elf, st= ruct section *rsec, if (elf_write_reloc(elf, reloc)) return NULL; =20 - list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); + reloc->sym_next_reloc =3D sym->relocs; + sym->relocs =3D reloc; =20 return reloc; } @@ -960,8 +960,9 @@ static int read_relocs(struct elf *elf) return -1; } =20 - list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); + reloc->sym_next_reloc =3D sym->relocs; + sym->relocs =3D reloc; =20 nr_reloc++; } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 60686f746c9e..7b808ac3156c 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -63,7 +63,7 @@ struct symbol { u8 profiling_func : 1; u8 warned : 1; struct list_head pv_target; - struct list_head reloc_list; + struct reloc *relocs; }; =20 struct reloc { @@ -74,7 +74,7 @@ struct reloc { }; struct section *sec; struct symbol *sym; - struct list_head sym_reloc_entry; + struct reloc *sym_next_reloc; }; =20 struct elf { --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6DE1C7EE23 for ; Tue, 30 May 2023 17:22:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233455AbjE3RWv (ORCPT ); Tue, 30 May 2023 13:22:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233250AbjE3RVo (ORCPT ); Tue, 30 May 2023 13:21:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EC43FE for ; Tue, 30 May 2023 10:21:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BB121630DB for ; Tue, 30 May 2023 17:21:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 61182C433A7; Tue, 30 May 2023 17:21:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467298; bh=FMKirBi6c8n5y7aSisMhuMOzaOwJD8A67Hi2Xih1kgQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q96xRZ8Gp3x4c8ouOPI335jvybyekk1BwDQAeA4eP0jRMs3a7efTb3cfbRmzkXEEl cllEIbQjA7bqxKAzUM1O+6iB3NqPJcSdoHbmvokyraiGcx4j754gzZrt4DyoqCuqMY UttBIfS/aj4ZwmpsOszBVx1Jj66a+daRDPbftrsyPFrnpgV+zcTCEoI7K4VfsCCPs9 Sgv00CbKcztQsuQPnM4xonVnO0iYtuUF7BTrQlnR8UoDTcp+S7XF5vHWI29SkX1V6U pk/+qJEolqLZIfBKFh9+XaASjDhO5FHIi/W3vAsCjM9LD6kNjqERjMRh0RQsxFnTf6 tj/kky7o29eqg== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 19/22] objtool: Shrink elf hash nodes Date: Tue, 30 May 2023 10:21:11 -0700 Message-Id: <6e8cd305ed22e743c30d6e72cfdc1be20fb94cd4.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Instead of using hlist for the 'struct elf' hashes, use a custom single-linked list scheme. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 36.89G - After: peak heap memory consumption: 35.12G Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 52 ++++++++++++++++++++++++----- tools/objtool/include/objtool/elf.h | 24 +++++++------ 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 4b0de0e56068..04038b1324cf 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -32,16 +32,52 @@ static inline u32 str_hash(const char *str) #define __elf_table(name) (elf->name##_hash) #define __elf_bits(name) (elf->name##_bits) =20 -#define elf_hash_add(name, node, key) \ - hlist_add_head(node, &__elf_table(name)[hash_min(key, __elf_bits(name))]) +#define __elf_table_entry(name, key) \ + __elf_table(name)[hash_min(key, __elf_bits(name))] =20 -#define elf_hash_for_each_possible(name, obj, member, key) \ - hlist_for_each_entry(obj, &__elf_table(name)[hash_min(key, __elf_bits(nam= e))], member) +#define elf_hash_add(name, node, key) \ +({ \ + struct elf_hash_node *__node =3D node; \ + __node->next =3D __elf_table_entry(name, key); \ + __elf_table_entry(name, key) =3D __node; \ +}) + +static inline void __elf_hash_del(struct elf_hash_node *node, + struct elf_hash_node **head) +{ + struct elf_hash_node *cur, *prev; + + if (node =3D=3D *head) { + *head =3D node->next; + return; + } + + for (prev =3D NULL, cur =3D *head; cur; prev =3D cur, cur =3D cur->next) { + if (cur =3D=3D node) { + prev->next =3D cur->next; + break; + } + } +} + +#define elf_hash_del(name, node, key) \ + __elf_hash_del(node, &__elf_table_entry(name, key)) + +#define elf_list_entry(ptr, type, member) \ +({ \ + typeof(ptr) __ptr =3D (ptr); \ + __ptr ? container_of(__ptr, type, member) : NULL; \ +}) + +#define elf_hash_for_each_possible(name, obj, member, key) \ + for (obj =3D elf_list_entry(__elf_table_entry(name, key), typeof(*obj), m= ember); \ + obj; \ + obj =3D elf_list_entry(obj->member.next, typeof(*(obj)), member)) =20 #define elf_alloc_hash(name, size) \ ({ \ __elf_bits(name) =3D max(10, ilog2(size)); \ - __elf_table(name) =3D mmap(NULL, sizeof(struct hlist_head) << __elf_bits(= name), \ + __elf_table(name) =3D mmap(NULL, sizeof(struct elf_hash_node *) << __elf_= bits(name), \ PROT_READ|PROT_WRITE, \ MAP_PRIVATE|MAP_ANON, -1, 0); \ if (__elf_table(name) =3D=3D (void *)-1L) { \ @@ -713,10 +749,10 @@ __elf_create_symbol(struct elf *elf, struct symbol *s= ym) first_non_local =3D symtab->sh.sh_info; old =3D find_symbol_by_index(elf, first_non_local); if (old) { - old->idx =3D new_idx; =20 - hlist_del(&old->hash); - elf_hash_add(symbol, &old->hash, old->idx); + elf_hash_del(symbol, &old->hash, old->idx); + elf_hash_add(symbol, &old->hash, new_idx); + old->idx =3D new_idx; =20 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { WARN("elf_update_symbol move"); diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 7b808ac3156c..03a9040f696c 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -26,10 +26,14 @@ #define ELF_C_READ_MMAP ELF_C_READ #endif =20 +struct elf_hash_node { + struct elf_hash_node *next; +}; + struct section { struct list_head list; - struct hlist_node hash; - struct hlist_node name_hash; + struct elf_hash_node hash; + struct elf_hash_node name_hash; GElf_Shdr sh; struct rb_root_cached symbol_tree; struct list_head symbol_list; @@ -45,8 +49,8 @@ struct section { struct symbol { struct list_head list; struct rb_node node; - struct hlist_node hash; - struct hlist_node name_hash; + struct elf_hash_node hash; + struct elf_hash_node name_hash; GElf_Sym sym; struct section *sec; char *name; @@ -67,7 +71,7 @@ struct symbol { }; =20 struct reloc { - struct hlist_node hash; + struct elf_hash_node hash; union { GElf_Rela rela; GElf_Rel rel; @@ -93,11 +97,11 @@ struct elf { int section_name_bits; int reloc_bits; =20 - struct hlist_head *symbol_hash; - struct hlist_head *symbol_name_hash; - struct hlist_head *section_hash; - struct hlist_head *section_name_hash; - struct hlist_head *reloc_hash; + struct elf_hash_node **symbol_hash; + struct elf_hash_node **symbol_name_hash; + struct elf_hash_node **section_hash; + struct elf_hash_node **section_name_hash; + struct elf_hash_node **reloc_hash; =20 struct section *section_data; struct symbol *symbol_data; --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBE86C77B7A for ; Tue, 30 May 2023 17:22:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233470AbjE3RW6 (ORCPT ); Tue, 30 May 2023 13:22:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233260AbjE3RVo (ORCPT ); Tue, 30 May 2023 13:21:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B1EA102 for ; Tue, 30 May 2023 10:21:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 78E7563142 for ; Tue, 30 May 2023 17:21:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B1D4FC433A8; Tue, 30 May 2023 17:21:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467298; bh=0IXsiwvSgd8r+C6PdGxTPB0uO8ObUewc0Gid2X5/8PQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jzxmsBR5UpBN254QFLmUM1wJRNe/wFbBK4T9x0dQZ5+/HmagoaIFdkgS7usjxbPsw N3gdU3SphMkqKfYhXuDOJMClwzAv2HoQAZPwX6jWkptEaPzJ+a7VjV5uw7mvSii6nT 53fqxHMPakbpDo/Jm4L67CmSsxA7x+vH19I7FnwUqtOjvX6p45VYaY7lUEmH2CimdO t1LrJGJlELUeKE6IwyO81+Opl60z9iUFWo6Yob00zGwlamWQoOruxwHSdLc0Gg21FU FvxeiHJKPGU3KMRDriUX4JF9WHWSXmjyyXm8Dj4g+VcYEWKXp6p8uOvR1Bx9FjoekP h2znXtIfEnB0Q== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 20/22] objtool: Get rid of reloc->rel[a] Date: Tue, 30 May 2023 10:21:12 -0700 Message-Id: <2be32323de6d8cc73179ee0ff14b71f4e7cefaa0.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Get the relocation entry info from the underlying rsec->data. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 35.12G - After: peak heap memory consumption: 29.93G Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 20 +++--- tools/objtool/elf.c | 60 +++--------------- tools/objtool/include/objtool/elf.h | 94 +++++++++++++++++++++++++---- 3 files changed, 96 insertions(+), 78 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 3fe6b3657e22..65c59b0b1e96 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -967,7 +967,7 @@ static int create_mcount_loc_sections(struct objtool_fi= le *file) if (!reloc) return -1; =20 - set_reloc_type(reloc, addr_size =3D=3D 8 ? R_ABS64 : R_ABS32); + set_reloc_type(file->elf, reloc, addr_size =3D=3D 8 ? R_ABS64 : R_ABS32); =20 idx++; } @@ -1362,10 +1362,8 @@ static void annotate_call_site(struct objtool_file *= file, * noinstr text. */ if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) { - if (reloc) { - set_reloc_type(reloc, R_NONE); - elf_write_reloc(file->elf, reloc); - } + if (reloc) + set_reloc_type(file->elf, reloc, R_NONE); =20 elf_write_insn(file->elf, insn->sec, insn->offset, insn->len, @@ -1391,10 +1389,8 @@ static void annotate_call_site(struct objtool_file *= file, if (sibling) WARN_INSN(insn, "tail call to __fentry__ !?!?"); if (opts.mnop) { - if (reloc) { - set_reloc_type(reloc, R_NONE); - elf_write_reloc(file->elf, reloc); - } + if (reloc) + set_reloc_type(file->elf, reloc, R_NONE); =20 elf_write_insn(file->elf, insn->sec, insn->offset, insn->len, @@ -1873,10 +1869,8 @@ static int handle_jump_alt(struct objtool_file *file, if (opts.hack_jump_label && special_alt->key_addend & 2) { struct reloc *reloc =3D insn_reloc(file, orig_insn); =20 - if (reloc) { - set_reloc_type(reloc, R_NONE); - elf_write_reloc(file->elf, reloc); - } + if (reloc) + set_reloc_type(file->elf, reloc, R_NONE); elf_write_insn(file->elf, orig_insn->sec, orig_insn->offset, orig_insn->len, arch_nop_insn(orig_insn->len)); diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 04038b1324cf..54d182ddc4bb 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -575,11 +575,8 @@ static int elf_update_sym_relocs(struct elf *elf, stru= ct symbol *sym) { struct reloc *reloc; =20 - for (reloc =3D sym->relocs; reloc; reloc =3D reloc->sym_next_reloc) { - reloc->rel.r_info =3D GELF_R_INFO(reloc->sym->idx, reloc_type(reloc)); - if (elf_write_reloc(elf, reloc)) - return -1; - } + for (reloc =3D sym->relocs; reloc; reloc =3D reloc->sym_next_reloc) + set_reloc_sym(elf, reloc, reloc->sym->idx); =20 return 0; } @@ -869,12 +866,10 @@ static struct reloc *elf_init_reloc(struct elf *elf, = struct section *rsec, reloc->sec =3D rsec; reloc->sym =3D sym; =20 - reloc->rel.r_offset =3D offset; - reloc->rel.r_info =3D GELF_R_INFO(sym->idx, type); - reloc->rela.r_addend =3D addend; - - if (elf_write_reloc(elf, reloc)) - return NULL; + set_reloc_offset(elf, reloc, offset); + set_reloc_sym(elf, reloc, sym->idx); + set_reloc_type(elf, reloc, type); + set_reloc_addend(elf, reloc, addend); =20 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); reloc->sym_next_reloc =3D sym->relocs; @@ -932,24 +927,6 @@ struct reloc *elf_init_reloc_data_sym(struct elf *elf,= struct section *sec, elf_data_rela_type(elf)); } =20 -static int read_reloc(struct section *rsec, int i, struct reloc *reloc) -{ - bool rela =3D rsec->sh.sh_type =3D=3D SHT_RELA; - void *retp; - - if (rela) - retp =3D gelf_getrela(rsec->data, i, &reloc->rela); - else - retp =3D gelf_getrel(rsec->data, i, &reloc->rel); - - if (!retp) { - WARN_ELF("gelf_getrela"); - return -1; - } - - return 0; -} - static int read_relocs(struct elf *elf) { unsigned long nr_reloc, max_reloc =3D 0; @@ -984,11 +961,8 @@ static int read_relocs(struct elf *elf) for (i =3D 0; i < sec_num_entries(rsec); i++) { reloc =3D &rsec->relocs[i]; =20 - if (read_reloc(rsec, i, reloc)) - return -1; - reloc->sec =3D rsec; - symndx =3D GELF_R_SYM(reloc->rel.r_info); + symndx =3D reloc_sym(reloc); reloc->sym =3D sym =3D find_symbol_by_index(elf, symndx); if (!reloc->sym) { WARN("can't find reloc entry symbol %d for %s", @@ -1261,26 +1235,6 @@ int elf_write_insn(struct elf *elf, struct section *= sec, return 0; } =20 -int elf_write_reloc(struct elf *elf, struct reloc *reloc) -{ - struct section *rsec =3D reloc->sec; - int ret; - - if (rsec->sh.sh_type =3D=3D SHT_RELA) - ret =3D gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); - else - ret =3D gelf_update_rel(rsec->data, reloc_idx(reloc), &reloc->rel); - - if (!ret) { - WARN_ELF("gelf_update_rela"); - return -1; - } - - mark_sec_changed(elf, rsec, true); - - return 0; -} - /* * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size * do you: diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/ob= jtool/elf.h index 03a9040f696c..c532d70864dc 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -72,10 +72,6 @@ struct symbol { =20 struct reloc { struct elf_hash_node hash; - union { - GElf_Rela rela; - GElf_Rel rel; - }; struct section *sec; struct symbol *sym; struct reloc *sym_next_reloc; @@ -132,7 +128,6 @@ struct reloc *elf_init_reloc_data_sym(struct elf *elf, = struct section *sec, int elf_write_insn(struct elf *elf, struct section *sec, unsigned long offset, unsigned int len, const char *insn); -int elf_write_reloc(struct elf *elf, struct reloc *reloc); int elf_write(struct elf *elf); void elf_close(struct elf *elf); =20 @@ -204,24 +199,99 @@ static inline unsigned int reloc_idx(struct reloc *re= loc) return reloc - reloc->sec->relocs; } =20 -static inline unsigned long reloc_offset(struct reloc *reloc) +static inline void *reloc_rel(struct reloc *reloc) { - return reloc->rel.r_offset; + struct section *rsec =3D reloc->sec; + + return rsec->data->d_buf + (reloc_idx(reloc) * rsec->sh.sh_entsize); } =20 -static inline unsigned int reloc_type(struct reloc *reloc) +static inline bool is_32bit_reloc(struct reloc *reloc) { - return GELF_R_TYPE(reloc->rel.r_info); + /* + * Elf32_Rel: 8 bytes + * Elf32_Rela: 12 bytes + * Elf64_Rel: 16 bytes + * Elf64_Rela: 24 bytes + */ + return reloc->sec->sh.sh_entsize < 16; } =20 -static inline void set_reloc_type(struct reloc *reloc, int type) +#define __get_reloc_field(reloc, field) \ +({ \ + is_32bit_reloc(reloc) ? \ + ((Elf32_Rela *)reloc_rel(reloc))->field : \ + ((Elf64_Rela *)reloc_rel(reloc))->field; \ +}) + +#define __set_reloc_field(reloc, field, val) \ +({ \ + if (is_32bit_reloc(reloc)) \ + ((Elf32_Rela *)reloc_rel(reloc))->field =3D val; \ + else \ + ((Elf64_Rela *)reloc_rel(reloc))->field =3D val; \ +}) + +static inline u64 reloc_offset(struct reloc *reloc) { - reloc->rel.r_info =3D GELF_R_INFO(GELF_R_SYM(reloc->rel.r_info), type); + return __get_reloc_field(reloc, r_offset); +} + +static inline void set_reloc_offset(struct elf *elf, struct reloc *reloc, = u64 offset) +{ + __set_reloc_field(reloc, r_offset, offset); + mark_sec_changed(elf, reloc->sec, true); } =20 static inline s64 reloc_addend(struct reloc *reloc) { - return reloc->rela.r_addend; + return __get_reloc_field(reloc, r_addend); +} + +static inline void set_reloc_addend(struct elf *elf, struct reloc *reloc, = s64 addend) +{ + __set_reloc_field(reloc, r_addend, addend); + mark_sec_changed(elf, reloc->sec, true); +} + + +static inline unsigned int reloc_sym(struct reloc *reloc) +{ + u64 info =3D __get_reloc_field(reloc, r_info); + + return is_32bit_reloc(reloc) ? + ELF32_R_SYM(info) : + ELF64_R_SYM(info); +} + +static inline unsigned int reloc_type(struct reloc *reloc) +{ + u64 info =3D __get_reloc_field(reloc, r_info); + + return is_32bit_reloc(reloc) ? + ELF32_R_TYPE(info) : + ELF64_R_TYPE(info); +} + +static inline void set_reloc_sym(struct elf *elf, struct reloc *reloc, uns= igned int sym) +{ + u64 info =3D is_32bit_reloc(reloc) ? + ELF32_R_INFO(sym, reloc_type(reloc)) : + ELF64_R_INFO(sym, reloc_type(reloc)); + + __set_reloc_field(reloc, r_info, info); + + mark_sec_changed(elf, reloc->sec, true); +} +static inline void set_reloc_type(struct elf *elf, struct reloc *reloc, un= signed int type) +{ + u64 info =3D is_32bit_reloc(reloc) ? + ELF32_R_INFO(reloc_sym(reloc), type) : + ELF64_R_INFO(reloc_sym(reloc), type); + + __set_reloc_field(reloc, r_info, info); + + mark_sec_changed(elf, reloc->sec, true); } =20 #define for_each_sec(file, sec) \ --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61B2AC7EE23 for ; Tue, 30 May 2023 17:22:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233422AbjE3RW3 (ORCPT ); Tue, 30 May 2023 13:22:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233217AbjE3RVn (ORCPT ); Tue, 30 May 2023 13:21:43 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E690F10A for ; Tue, 30 May 2023 10:21:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C66D063159 for ; Tue, 30 May 2023 17:21:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F096C4331E; Tue, 30 May 2023 17:21:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467299; bh=ecG2X1Qumz/2oCyr7RUA0LcooVSAeFFdy7NDRLMkRxc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rbpRFfjJGiYuUw11vsmHWm4yiGB4iJ1K4ATRPYtqMG6AdOc5+lo499YNfPtSyPnoZ eCIYc8roJZVEYSbuJ95Ogtej0Izo2Cz6fQr+2d7kq0A5+4zIXVOnSbbtc3zwrPbtls M/kSq8iAh5w53c7PnZjsVn6M4qt1hQAYSrB9Hb5NNqQjc5ktdGkL5bsnSvNvLsZoaV CopHxL/NpxmhSgLMcW0Lgyklnpbwp4pTNPbV7NjwHoS6O6ozckoge8Gnmv06glX6TN falLUWjvXcR+bE6VER0yfeosQWUEX64XE4SppA11c+gzzg05yyqJPUr73iR/RzCH9i zIxlR8MKOaYeQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 21/22] objtool: Free insns when done Date: Tue, 30 May 2023 10:21:13 -0700 Message-Id: <4d4bca1a0f869de020dac80d91f9acbf6df77eab.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Free the decoded instructions as they're no longer needed after this point. This frees up a big chunk of heap, which will come handy when skipping the reading of DWARF section data. Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 65c59b0b1e96..80d3a97e442b 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -4587,6 +4587,34 @@ static int disas_warned_funcs(struct objtool_file *f= ile) return 0; } =20 +struct insn_chunk { + void *addr; + struct insn_chunk *next; +}; + +/* + * Reduce peak RSS usage by freeing insns memory before writing the ELF fi= le, + * which can trigger more allocations for .debug_* sections whose data has= n't + * been read yet. + */ +static void free_insns(struct objtool_file *file) +{ + struct instruction *insn; + struct insn_chunk *chunks =3D NULL, *chunk; + + for_each_insn(file, insn) { + if (!insn->idx) { + chunk =3D malloc(sizeof(*chunk)); + chunk->addr =3D insn; + chunk->next =3D chunks; + chunks =3D chunk; + } + } + + for (chunk =3D chunks; chunk; chunk =3D chunk->next) + free(chunk->addr); +} + int check(struct objtool_file *file) { int ret, warnings =3D 0; @@ -4731,6 +4759,8 @@ int check(struct objtool_file *file) warnings +=3D ret; } =20 + free_insns(file); + if (opts.verbose) disas_warned_funcs(file); =20 --=20 2.40.1 From nobody Sat Feb 7 11:31:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1957BC7EE23 for ; Tue, 30 May 2023 17:22:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233465AbjE3RWy (ORCPT ); Tue, 30 May 2023 13:22:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233258AbjE3RVo (ORCPT ); Tue, 30 May 2023 13:21:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3404106 for ; Tue, 30 May 2023 10:21:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BABE8616AF for ; Tue, 30 May 2023 17:21:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60BA8C43321; Tue, 30 May 2023 17:21:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685467299; bh=KD8Lf36i8S8fkl9e26OJy++ossCBPSeuvsfaXWI90DE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tmRnWW51kF+qVvCNHgNioj2opxfXrWksnpanZ9AMj5ePvZzpzLlj6ClrgoIwJTh21 T1cong5ST2Vg2+GZOlz51ko9EoC3dcuhJTdH9w9ZDdFgEqRUA0A/vPt69/8HtrmWqc uihOUcYaaHj6pgnhWPpHUn3yyYVt/rzddmfsdy9o0/QATGQ/2sh+qpE/RYszwCNWg0 TmTYiWG82Ew/7mw63L+7sLqjuDN+TMMQDK/20/GvyFImWRb3fYgBFws8cJNIZtcidg DU3k13QPvoxpCdMwH0cXLaBnwxijXLb9PEnZt53gSLFPeGQ+wkbdQaEQbYIKoxr92+ ve+u+jFFg65TA== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Miroslav Benes Subject: [PATCH 22/22] objtool: Skip reading DWARF section data Date: Tue, 30 May 2023 10:21:14 -0700 Message-Id: <52a9698835861dd35f2ec35c49f96d0bb39fb177.1685464332.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Objtool doesn't use DWARF at all, and the DWARF sections' data take up a lot of memory. Skip reading them. Note this only skips the DWARF base sections, not the rela sections. The relas are needed because their symbol references may need to be reindexed if any local symbols get added by elf_create_symbol(). Also note the DWARF data will eventually be read by libelf anyway, when writing the object file. But that's fine, the goal here is to reduce *peak* memory usage, and the previous patch (which freed insn memory) gave some breathing room. So the allocation gets shifted to a later time, resulting in lower peak memory usage. With allyesconfig + CONFIG_DEBUG_INFO: - Before: peak heap memory consumption: 29.93G - After: peak heap memory consumption: 25.47G Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 54d182ddc4bb..d420b5d2e2b6 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -300,6 +300,11 @@ struct reloc *find_reloc_by_dest(const struct elf *elf= , struct section *sec, uns return find_reloc_by_dest_range(elf, sec, offset, 1); } =20 +static bool is_dwarf_section(struct section *sec) +{ + return !strncmp(sec->name, ".debug_", 7); +} + static int read_sections(struct elf *elf) { Elf_Scn *s =3D NULL; @@ -350,7 +355,7 @@ static int read_sections(struct elf *elf) return -1; } =20 - if (sec->sh.sh_size !=3D 0) { + if (sec->sh.sh_size !=3D 0 && !is_dwarf_section(sec)) { sec->data =3D elf_getdata(s, NULL); if (!sec->data) { WARN_ELF("elf_getdata"); --=20 2.40.1