From patchwork Tue May 30 17:20:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101048 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2344497vqr; Tue, 30 May 2023 10:31:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4D5StV8PYyLp7EZQBfEG9ab7ib3Et7wZg/QRHINy6iFW35Kd5XUmiUy68ZmKML2YyP+hY0 X-Received: by 2002:a17:90a:fa82:b0:256:4801:b24 with SMTP id cu2-20020a17090afa8200b0025648010b24mr3055680pjb.7.1685467890482; Tue, 30 May 2023 10:31:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467890; cv=none; d=google.com; s=arc-20160816; b=u613dmI8+Q11MIkDircmINTXYw8Bh836s5rO5VlqHIY1G2xwIGrNa3/LFql4GQUHND Le5/dq2cvVQltnUCFJcyX1AaxjMq1+17yzX8h/w0Nx9SZ7qhogJN2N7tRuIuOF46DvBV i3qTOhpLU7CxWkiVDLWPO8mAMweL4vIt50xsK9ebN4ZlzDXBsVTyNU89Kc2wT2lNkNWA TSK7d4Bps/lXDfZ04C2mtD5hDYoqhAitDLqK8ie81KBvnCR6zwZLZCA516FHZn80Ft// wXJd9k83gEGMI9eqiwlsjUWIh7zQZLQumpvhdQHeanhvZXLkYX8FwJuZ4fqfOtxk085e y3mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tk7bN7n5UdJSBsGXqvrCS95jIAuNM/X0LTBXXC9bdLc=; b=VzctRtuAPgp3W8SaH9xNieN3o1hJjjr7irhHZqW8cWMVtWuEYX68GjkNaT6FkL7Yvo tP0kd1+Nqt0Vc8veK3XK0LXEYuUSbvzp8lbc7CXUeYX4S00fKn7nd2xYX0dvlltYDHEb K0cY8u7wkvdICmxqR8D4Rclj4iX6Fl99lxHIkWzDLwwRWgL+vzXySyNZClULAe512qSB EGfmeJ2DqKPj77YqewjJ/UdM1mqUcMNP3vLPz+1ST6kC+KQIylGF8tUh/oTLtGo5+nWE R64MaUsvtJssCj3kR8wmrzOx04nWmZIZF/A1dVJJy061xdniJugR+RKGRa3JsWAfvfFt OotA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OxnUPmim; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y10-20020a17090a134a00b00256a3130dcasi2273352pjf.160.2023.05.30.10.31.16; Tue, 30 May 2023 10:31:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OxnUPmim; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232408AbjE3RVi (ORCPT + 99 others); Tue, 30 May 2023 13:21:38 -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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341178746080889?= X-GMAIL-MSGID: =?utf-8?q?1767341178746080889?= 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/objtool/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; }; -#define ELF_HASH_BITS 20 - struct elf { Elf *elf; GElf_Ehdr ehdr; @@ -110,53 +108,6 @@ struct elf { struct symbol *symbol_data; }; -#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 = ((_start) & OFFSET_STRIDE_MASK); \ - _offset >= ((_start) & OFFSET_STRIDE_MASK) && \ - _offset <= ((_end) & OFFSET_STRIDE_MASK); \ - _offset += OFFSET_STRIDE) - -static inline u32 sec_offset_hash(struct section *sec, unsigned long offset) -{ - u32 ol, oh, idx = sec->idx; - - offset &= OFFSET_STRIDE_MASK; - - ol = offset; - oh = (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] == 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, unsigned int sh_flags, size_t entsize, int nr); @@ -186,6 +137,24 @@ struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *se unsigned long offset, unsigned int len); struct symbol *find_func_containing(struct section *sec, unsigned long 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] == ELFCLASS32) + return sizeof(u32); + else + return sizeof(u64); +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) @@ -198,4 +167,33 @@ struct symbol *find_func_containing(struct section *sec, unsigned long offset); for_each_sec(file, __sec) \ sec_for_each_sym(__sec, sym) +#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 = ((_start) & OFFSET_STRIDE_MASK); \ + _offset >= ((_start) & OFFSET_STRIDE_MASK) && \ + _offset <= ((_end) & OFFSET_STRIDE_MASK); \ + _offset += OFFSET_STRIDE) + +static inline u32 sec_offset_hash(struct section *sec, unsigned long offset) +{ + u32 ol, oh, idx = sec->idx; + + offset &= OFFSET_STRIDE_MASK; + + ol = offset; + oh = (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 */ From patchwork Tue May 30 17:20:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101030 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340181vqr; Tue, 30 May 2023 10:23:13 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7/T3RZ42C2mmVd97bktM4MpUyZ4k9v8uwuD2qY+8L+eZ/xWVuZroNAq/lULOPYKWaQHe4O X-Received: by 2002:a17:902:eccc:b0:1b0:2986:8a6b with SMTP id a12-20020a170902eccc00b001b029868a6bmr3627508plh.55.1685467393399; Tue, 30 May 2023 10:23:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467393; cv=none; d=google.com; s=arc-20160816; b=tztcUoclOzylh5Jy80e4zGpT3/S05qzv5IwHsCY05npyRPYD6vGzQMOGSu+SYjMC5e MXI967dAnKxqH5ZAjJ9cqEzg0eQwPEoQBihfru5q0H1wPHRT3EsRzFH4Diyb4obsSQTl IC3c4yXqc3Hil8o4aO1bdw9nZR3OXfGs7hsJ1u4F0lIIeaNoqGKG7UC+IqH/CP3zBMdq JkqHz6BSbg1S3ldf3RrxPvvLDDM2yMfU1pbOrEZK/iDtbQaR8B8llWFsS9+GrKThWujr 1JazmzuF4sAl+bImFa708uTFfhht1sFwyEEDs796DUoSQdzMhEt8pVQKu5UUugdGYrir 5TRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dRJIb519Dx7W3hXNjuAJSTybRk2lw8MaVqKlakF7FRY=; b=khohEsJ7cUvSVvS2WyTJiwPQ4CgjmlFYgxBxLMYUeDz/tqSBcC5cpHHkSNQgfvbXsj 1RZjUgheASmqWra/X5BAWrzDbj1lcpaLpTFug9IztYi8WzBKn7VD9roSiXdvj1f5LKCt GgHoiISM7pou0T7IDDAl3idFrwJ9znea6CGzHbA522ls3Z1l6LApTJd12+E0g3Oq7jqt iqKkwH9qGBEUgbniCU8pX8H/jgZP1eZvOYLTjse0Oul68klZsERS287b05gQ64eziDLs KQRMqv4z9350zdVRHb+4J9mcRQckAtUoO9H3VSGkAagWmw51htjmMPck84qcHIqAM18N XuVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="YKeCJ/vR"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t13-20020a1709028c8d00b001aaf91ae3acsi10762589plo.485.2023.05.30.10.22.56; Tue, 30 May 2023 10:23:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="YKeCJ/vR"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232541AbjE3RVm (ORCPT + 99 others); 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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340657238653882?= X-GMAIL-MSGID: =?utf-8?q?1767340657238653882?= 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++; - sec = elf_create_section(file->elf, ".static_call_sites", SHF_WRITE, + sec = elf_create_section(file->elf, ".static_call_sites", sizeof(struct static_call_site), idx); if (!sec) return -1; + /* Allow modules to set the low bits of static_call_site::key */ + sec->sh.sh_flags |= SHF_WRITE; + idx = 0; list_for_each_entry(insn, &file->static_call_list, call_node) { @@ -762,7 +765,7 @@ static int create_retpoline_sites_sections(struct objtool_file *file) if (!idx) return 0; - sec = elf_create_section(file->elf, ".retpoline_sites", 0, + sec = 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; - sec = elf_create_section(file->elf, ".return_sites", 0, + sec = 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 objtool_file *file) if (!idx) return 0; - sec = elf_create_section(file->elf, ".ibt_endbr_seal", 0, + sec = 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 *file) idx++; } - sec = elf_create_section(file->elf, ".cfi_sites", 0, sizeof(unsigned int), idx); + sec = elf_create_section(file->elf, ".cfi_sites", sizeof(unsigned int), idx); if (!sec) return -1; @@ -967,7 +970,7 @@ static int create_mcount_loc_sections(struct objtool_file *file) list_for_each_entry(insn, &file->mcount_loc_list, call_node) idx++; - sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx); + sec = elf_create_section(file->elf, "__mcount_loc", addrsize, idx); if (!sec) return -1; @@ -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++; - sec = elf_create_section(file->elf, ".call_sites", 0, sizeof(unsigned int), idx); + sec = elf_create_section(file->elf, ".call_sites", sizeof(unsigned int), idx); if (!sec) return -1; 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 section *strtab, char *str) } 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 = entsize * nr; @@ -1117,7 +1117,7 @@ struct section *elf_create_section(struct elf *elf, const char *name, sec->sh.sh_entsize = entsize; sec->sh.sh_type = SHT_PROGBITS; sec->sh.sh_addralign = 1; - sec->sh.sh_flags = SHF_ALLOC | sh_flags; + sec->sh.sh_flags = SHF_ALLOC; /* Add section name to .shstrtab (or .strtab for Clang) */ shstrtab = find_section_by_name(elf, ".shstrtab"); @@ -1153,7 +1153,7 @@ static struct section *elf_create_rel_reloc_section(struct elf *elf, struct sect strcpy(relocname, ".rel"); strcat(relocname, base->name); - sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0); + sec = 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); if (addrsize == sizeof(u32)) - sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 0); + sec = elf_create_section(elf, relocname, sizeof(Elf32_Rela), 0); else - sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); + sec = 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/objtool/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 { }; struct elf *elf_open_read(const char *name, int flags); -struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr); +struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr); struct symbol *elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size); 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 = elf_create_section(file->elf, ".orc_unwind", 0, + orc_sec = elf_create_section(file->elf, ".orc_unwind", sizeof(struct orc_entry), nr); if (!orc_sec) return -1; - sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), nr); + sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), nr); if (!sec) return -1; From patchwork Tue May 30 17:20:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101032 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340211vqr; Tue, 30 May 2023 10:23:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4NmtsoWOK+9P55Qa+W0DqFPtqyQGH2vE5uBGuqosXzSuuA7nIbz4hM6EybsYJ9pOjKrkZa X-Received: by 2002:a05:6a20:1602:b0:100:efa1:eab0 with SMTP id l2-20020a056a20160200b00100efa1eab0mr4029164pzj.2.1685467397354; Tue, 30 May 2023 10:23:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467397; cv=none; d=google.com; s=arc-20160816; b=EEEtLx570dnWUp2up9egH+N8UQry3v+wSC8gef78bRTTEqpBraOkyWncGYtFXdWZMY rIBtqnXH44fZh8pv0aDeAPMBD6uo/LtOuNA7oPMCcs2zjdiYifi8lkQ9okjkoplsmIbx UX7srhXmDTV5x4JeGlwtGqwJ3qP/EcVc/lxkY5dZaK6LldAeX1p+0yag+uzUvAwn8ugX pr3c7Kb9TNTSkEDUbBQ/6ROg2OpRfBbNgcy2w+GOsuyCuWx0LsgHRHNB0FPuZ+41sQLJ q2azONyr/aTkq19TaApSw7RJQqrZxb4b/WLwD/s2NtYqsIaVDUq5sc+ylhiKxHk8XG5s GoOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4Eg9j561GeXqTe5iNdEUjA7Gz5I8jEPujn1SfMFhiCo=; b=JMwK9+XdRj6If232k2WKOLrOh2fRPLyuklg0yJ+DxvANlJyQld99ztC0iXC5OMYcpy Z7sFoa7qsFkdD3J7e8UlzJspOaMH2b04+Xx+ZQeImAt+4kEkku/QtmW2lHkcj5DMKWjP 5rOuz2oz19358E1/oxT3kFYhnVnc0ZNX/bJSyWP2ibtG9lvxpBeoWfwvgPoviwJQ+zHt ksZPUiQk+/hxKhol0T93nIJcS6tPL5usGJrd8Rpg+WqjShnex/N7PgOsgxki8fUqPtSY 4wDKA8ppgN8nA+1XcVamr3jQW6Go/ORwP50fTdKF0Ehq6aRBRTuEI4PtxtxnzTnDbPNA nJpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HSPomr8C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bu20-20020a632954000000b00517ab01bb8fsi340026pgb.100.2023.05.30.10.23.02; Tue, 30 May 2023 10:23:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HSPomr8C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233318AbjE3RVt (ORCPT + 99 others); Tue, 30 May 2023 13:21:49 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340661859432018?= X-GMAIL-MSGID: =?utf-8?q?1767340661859432018?= - 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; sym = find_symbol_by_name(file->elf, symname); @@ -503,19 +503,19 @@ static int add_pv_ops(struct objtool_file *file, const char *symname) off = sym->offset; end = off + sym->len; for (;;) { - rel = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); - if (!rel) + reloc = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off); + if (!reloc) break; - func = rel->sym; + func = reloc->sym; if (func->type == STT_SECTION) - func = find_symbol_by_offset(rel->sym->sec, rel->addend); + func = find_symbol_by_offset(reloc->sym->sec, reloc->addend); - idx = (rel->offset - sym->offset) / sizeof(unsigned long); + idx = (reloc->offset - sym->offset) / sizeof(unsigned long); objtool_pv_add(file, idx, func); - off = rel->offset + 1; + off = reloc->offset + 1; if (off > end) break; } @@ -580,20 +580,20 @@ static struct instruction *find_last_insn(struct objtool_file *file, */ static int add_dead_ends(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct reloc *reloc; struct instruction *insn; /* * Check for manually annotated dead ends. */ - sec = find_section_by_name(file->elf, ".rela.discard.unreachable"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.unreachable"); + if (!rsec) goto reachable; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type != STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } insn = 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 = find_section_by_name(file->elf, ".rela.discard.reachable"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.reachable"); + if (!rsec) return 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type != STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } insn = find_insn(file, reloc->sym->sec, reloc->addend); @@ -1043,15 +1043,15 @@ static int create_direct_call_sections(struct objtool_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; - sec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard"); + if (!rsec) return; - 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 = reloc->sym; @@ -1064,7 +1064,8 @@ static void add_ignores(struct objtool_file *file) break; default: - WARN("unexpected relocation symbol type in %s: %d", sec->name, reloc->sym->type); + WARN("unexpected relocation symbol type in %s: %d", + rsec->name, reloc->sym->type); continue; } @@ -1283,17 +1284,17 @@ static void add_uaccess_safe(struct objtool_file *file) */ static int add_ignore_alternatives(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct reloc *reloc; struct instruction *insn; - sec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.ignore_alts"); + if (!rsec) return 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type != STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } @@ -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 = 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 *file) if (!sec) return 0; - relocsec = 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 *file) static int read_noendbr_hints(struct objtool_file *file) { - struct section *sec; struct instruction *insn; + struct section *rsec; struct reloc *reloc; - sec = find_section_by_name(file->elf, ".rela.discard.noendbr"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.noendbr"); + if (!rsec) return 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend); if (!insn) { WARN("bad .discard.noendbr entry"); @@ -2312,17 +2312,17 @@ static int read_noendbr_hints(struct objtool_file *file) static int read_retpoline_hints(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct instruction *insn; struct reloc *reloc; - sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe"); + if (!rsec) return 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type != STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } @@ -2348,17 +2348,17 @@ static int read_retpoline_hints(struct objtool_file *file) static int read_instr_hints(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct instruction *insn; struct reloc *reloc; - sec = find_section_by_name(file->elf, ".rela.discard.instr_end"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.instr_end"); + if (!rsec) return 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type != STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } @@ -2371,13 +2371,13 @@ static int read_instr_hints(struct objtool_file *file) insn->instr--; } - sec = find_section_by_name(file->elf, ".rela.discard.instr_begin"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.instr_begin"); + if (!rsec) return 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type != STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } @@ -2395,17 +2395,17 @@ static int read_instr_hints(struct objtool_file *file) static int read_validate_unret_hints(struct objtool_file *file) { - struct section *sec; + struct section *rsec; struct instruction *insn; struct reloc *reloc; - sec = find_section_by_name(file->elf, ".rela.discard.validate_unret"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.validate_unret"); + if (!rsec) return 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { if (reloc->sym->type != STT_SECTION) { - WARN("unexpected relocation symbol type in %s", sec->name); + WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } @@ -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; - sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); - if (!sec) + rsec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); + if (!rsec) return 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { unsigned long dest_off; if (reloc->sym->type != STT_SECTION) { WARN("unexpected relocation symbol type in %s", - sec->name); + rsec->name); return -1; } @@ -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; if (insn_call_dest(insn)) return insn_call_dest(insn)->name; - rel = insn_reloc(NULL, insn); - if (rel && !strcmp(rel->sym->name, "pv_ops")) { - idx = (rel->addend / sizeof(void *)); + reloc = insn_reloc(NULL, insn); + if (reloc && !strcmp(reloc->sym->name, "pv_ops")) { + idx = (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 instruction *insn) static bool pv_call_dest(struct objtool_file *file, struct instruction *insn) { struct symbol *target; - struct reloc *rel; + struct reloc *reloc; int idx; - rel = insn_reloc(file, insn); - if (!rel || strcmp(rel->sym->name, "pv_ops")) + reloc = insn_reloc(file, insn); + if (!reloc || strcmp(reloc->sym->name, "pv_ops")) return false; - idx = (arch_dest_reloc_offset(rel->addend) / sizeof(void *)); + idx = (arch_dest_reloc_offset(reloc->addend) / sizeof(void *)); 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; - if (!sec->reloc) + if (!sec->rsec) continue; /* @@ -4428,7 +4428,7 @@ static int validate_ibt(struct objtool_file *file) strstr(sec->name, "__patchable_function_entries")) continue; - list_for_each_entry(reloc, &sec->reloc->reloc_list, list) + list_for_each_entry(reloc, &sec->rsec->reloc_list, list) warnings += validate_ibt_data_reloc(file, reloc); } 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 elf *elf, struct section *se unsigned long offset, unsigned int len) { struct reloc *reloc, *r = NULL; + struct section *rsec; unsigned long o; - if (!sec->reloc) + rsec = sec->rsec; + if (!rsec) return NULL; - sec = 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 != sec) + sec_offset_hash(rsec, o)) { + if (reloc->sec != rsec) continue; if (reloc->offset >= offset && reloc->offset < offset + len) { @@ -534,7 +534,7 @@ static int read_symbols(struct elf *elf) } static struct section *elf_create_reloc_section(struct elf *elf, - struct section *base, + struct section *sec, int reltype); int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, @@ -542,7 +542,7 @@ int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, { struct reloc *reloc; - if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA)) + if (!sec->rsec && !elf_create_reloc_section(elf, sec, SHT_RELA)) return -1; reloc = malloc(sizeof(*reloc)); @@ -552,18 +552,18 @@ int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, } memset(reloc, 0, sizeof(*reloc)); - reloc->sec = sec->reloc; + reloc->sec = sec->rsec; reloc->offset = offset; reloc->type = type; reloc->sym = sym; reloc->addend = addend; 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)); - sec->reloc->sh.sh_size += sec->reloc->sh.sh_entsize; - sec->reloc->changed = true; + sec->rsec->sh.sh_size += sec->rsec->sh.sh_entsize; + sec->rsec->changed = true; return 0; } @@ -865,9 +865,9 @@ int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, return elf_add_reloc(elf, sec, offset, type, sym, addend); } -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, struct reloc *reloc, unsig return 0; } -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 *reloc, 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 = 0, tot_reloc = 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; - list_for_each_entry(sec, &elf->sections, list) { - if ((sec->sh.sh_type != SHT_RELA) && - (sec->sh.sh_type != SHT_REL)) + list_for_each_entry(rsec, &elf->sections, list) { + if ((rsec->sh.sh_type != SHT_RELA) && + (rsec->sh.sh_type != SHT_REL)) continue; - sec->base = find_section_by_index(elf, sec->sh.sh_info); - if (!sec->base) { + rsec->base = 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; } - sec->base->reloc = sec; + rsec->base->rsec = rsec; nr_reloc = 0; - sec->reloc_data = calloc(sec->sh.sh_size / sec->sh.sh_entsize, sizeof(*reloc)); - if (!sec->reloc_data) { + rsec->reloc_data = calloc(rsec->sh.sh_size / rsec->sh.sh_entsize, + sizeof(*reloc)); + if (!rsec->reloc_data) { perror("calloc"); return -1; } - for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) { - reloc = &sec->reloc_data[i]; - switch (sec->sh.sh_type) { + for (i = 0; i < rsec->sh.sh_size / rsec->sh.sh_entsize; i++) { + reloc = &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; } - reloc->sec = sec; + reloc->sec = rsec; reloc->idx = i; reloc->sym = sym = 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; } 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)); nr_reloc++; @@ -1140,40 +1141,41 @@ struct section *elf_create_section(struct elf *elf, const char *name, return sec; } -static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base) +static struct section *elf_create_rel_reloc_section(struct elf *elf, + struct section *sec) { char *relocname; - struct section *sec; + struct section *rsec; - relocname = malloc(strlen(base->name) + strlen(".rel") + 1); + relocname = malloc(strlen(sec->name) + strlen(".rel") + 1); if (!relocname) { perror("malloc"); return NULL; } strcpy(relocname, ".rel"); - strcat(relocname, base->name); + strcat(relocname, sec->name); - sec = elf_create_section(elf, relocname, sizeof(GElf_Rel), 0); + rsec = elf_create_section(elf, relocname, sizeof(GElf_Rel), 0); free(relocname); - if (!sec) + if (!rsec) return NULL; - base->reloc = sec; - sec->base = base; + sec->rsec = rsec; + rsec->base = sec; - sec->sh.sh_type = SHT_REL; - sec->sh.sh_addralign = 8; - sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; - sec->sh.sh_info = base->idx; - sec->sh.sh_flags = SHF_INFO_LINK; + rsec->sh.sh_type = SHT_REL; + rsec->sh.sh_addralign = 8; + rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; + rsec->sh.sh_info = sec->idx; + rsec->sh.sh_flags = SHF_INFO_LINK; - return sec; + return rsec; } static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base) { char *relocname; - struct section *sec; + struct section *rsec; int addrsize = elf_class_addrsize(elf); relocname = malloc(strlen(base->name) + strlen(".rela") + 1); @@ -1185,23 +1187,23 @@ static struct section *elf_create_rela_reloc_section(struct elf *elf, struct sec strcat(relocname, base->name); if (addrsize == sizeof(u32)) - sec = elf_create_section(elf, relocname, sizeof(Elf32_Rela), 0); + rsec = elf_create_section(elf, relocname, sizeof(Elf32_Rela), 0); else - sec = elf_create_section(elf, relocname, sizeof(GElf_Rela), 0); + rsec = elf_create_section(elf, relocname, sizeof(GElf_Rela), 0); free(relocname); - if (!sec) + if (!rsec) return NULL; - base->reloc = sec; - sec->base = base; + base->rsec = rsec; + rsec->base = base; - sec->sh.sh_type = SHT_RELA; - sec->sh.sh_addralign = addrsize; - sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; - sec->sh.sh_info = base->idx; - sec->sh.sh_flags = SHF_INFO_LINK; + rsec->sh.sh_type = SHT_RELA; + rsec->sh.sh_addralign = addrsize; + rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; + rsec->sh.sh_info = base->idx; + rsec->sh.sh_flags = SHF_INFO_LINK; - return sec; + return rsec; } static struct section *elf_create_reloc_section(struct elf *elf, @@ -1215,28 +1217,28 @@ static struct section *elf_create_reloc_section(struct elf *elf, } } -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 = 0; void *buf; /* Allocate a buffer for relocations */ - buf = malloc(sec->sh.sh_size); + buf = malloc(rsec->sh.sh_size); if (!buf) { perror("malloc"); return -1; } - sec->data->d_buf = buf; - sec->data->d_size = sec->sh.sh_size; - sec->data->d_type = ELF_T_REL; + rsec->data->d_buf = buf; + rsec->data->d_size = rsec->sh.sh_size; + rsec->data->d_type = ELF_T_REL; idx = 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { reloc->rel.r_offset = reloc->offset; reloc->rel.r_info = 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 section *sec) return 0; } -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 = 0; void *buf; /* Allocate a buffer for relocations with addends */ - buf = malloc(sec->sh.sh_size); + buf = malloc(rsec->sh.sh_size); if (!buf) { perror("malloc"); return -1; } - sec->data->d_buf = buf; - sec->data->d_size = sec->sh.sh_size; - sec->data->d_type = ELF_T_RELA; + rsec->data->d_buf = buf; + rsec->data->d_size = rsec->sh.sh_size; + rsec->data->d_type = ELF_T_RELA; idx = 0; - list_for_each_entry(reloc, &sec->reloc_list, list) { + list_for_each_entry(reloc, &rsec->reloc_list, list) { reloc->rela.r_offset = reloc->offset; reloc->rela.r_addend = reloc->addend; reloc->rela.r_info = 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 section *sec) return 0; } -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, int elf_write_reloc(struct elf *elf, struct reloc *reloc) { - struct section *sec = reloc->sec; + struct section *rsec = reloc->sec; - if (sec->sh.sh_type == SHT_REL) { + if (rsec->sh.sh_type == SHT_REL) { reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); reloc->rel.r_offset = reloc->offset; - 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 *reloc) reloc->rela.r_addend = reloc->addend; reloc->rela.r_offset = reloc->offset; - 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/objtool/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; From patchwork Tue May 30 17:20:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101052 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2345147vqr; Tue, 30 May 2023 10:32:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5p1wtZWGJgbEM9GUjqRBIqq1a7OqCUdls+YNujxaMqu9sAsme3SjhPomjRfQYUAyO4G4oB X-Received: by 2002:a17:90a:fb8a:b0:253:2ddd:4a36 with SMTP id cp10-20020a17090afb8a00b002532ddd4a36mr2855731pjb.6.1685467954069; Tue, 30 May 2023 10:32:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467954; cv=none; d=google.com; s=arc-20160816; b=mshiPXxJZQNnIzu+ZhWr6Psi8k1Ru7x7frj10HurLteF9o3uJBl9KPKk9kRH0fKier pj/q3QmbYH9iBdytcgHgZ0e5DMnTju3aW3LcNJgTXPkSZburdTI4ArwP7PUkb+6zMNH4 CWWoBusZvUbjP0f9Z7ks9PqoUb8p/gJozfEO2q9wsmbzVoasEW353vlTSTd+F8vsoUfp MyFc4shNFKWp1ibgnJ7gJpQibL/TlOZm3oai1gb8m7Hlqxpmx91uqB4FAWigckzi40kl 40AXys4pD4GM3z69w++onQctZKu7T0uj8PYbxz/iBVB0/bgiKMn8mRzRGvDpzJ3FeCZe FI8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mo8ZA22fsLkNze+6XScNiUKhM8pn69FB+vrkvPuboHw=; b=AJhW2iPpvBqwYUgbT1rpRk88QI/I6/wUMhn19Tw2X/028P2MJwHIivLUC0IijkpCbt PDyEvUEzW2mf0nXgdZe0bn0N40rDZgkNnZAoevnk+AXsa3i1xScB7bwz8x9wQzNlHGUF tFQb+wiImpQQKrC1I+KWLIUvGapXWwVXFv3zKGgMi+aBeZTw7PjGbS/4V38gFhL1u3k8 9Y8hVxt3YDhZTEYmXgUqiGHi0IKwgK4vHh1jJGw1kZRJjgy4OtTddMxH5ze7PWSItT+S eaPVq97gK8xu2c1MpxL7EucQtPnq7+glXjf4Tv2TxEbxDYHuQbWvmYri1+tEcy5DfpJV gRZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Ohx3VDW8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nk13-20020a17090b194d00b002567212474bsi4559665pjb.0.2023.05.30.10.32.20; Tue, 30 May 2023 10:32:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Ohx3VDW8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233299AbjE3RVr (ORCPT + 99 others); Tue, 30 May 2023 13:21:47 -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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341245211244643?= X-GMAIL-MSGID: =?utf-8?q?1767341245211244643?= 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 *file) static int create_mcount_loc_sections(struct objtool_file *file) { - int addrsize = elf_class_addrsize(file->elf); + size_t addr_size = 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++; - sec = elf_create_section(file->elf, "__mcount_loc", addrsize, idx); + sec = elf_create_section(file->elf, "__mcount_loc", addr_size, idx); if (!sec) return -1; - sec->sh.sh_addralign = addrsize; + sec->sh.sh_addralign = addr_size; idx = 0; list_for_each_entry(insn, &file->mcount_loc_list, call_node) { void *loc; loc = sec->data->d_buf + idx; - memset(loc, 0, addrsize); + memset(loc, 0, addr_size); if (elf_add_reloc_to_insn(file->elf, sec, idx, - addrsize == sizeof(u64) ? R_ABS64 : R_ABS32, + addr_size == sizeof(u64) ? R_ABS64 : R_ABS32, insn->sec, insn->offset)) return -1; - idx += addrsize; + idx += addr_size; } 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; } -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); int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, unsigned int type, struct symbol *sym, s64 addend) { struct reloc *reloc; - if (!sec->rsec && !elf_create_reloc_section(elf, sec, SHT_RELA)) + if (!sec->rsec && !elf_create_rela_section(elf, sec)) return -1; reloc = malloc(sizeof(*reloc)); @@ -865,29 +864,25 @@ int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, return elf_add_reloc(elf, sec, offset, type, sym, addend); } -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 = GELF_R_TYPE(reloc->rel.r_info); - reloc->addend = 0; - reloc->offset = reloc->rel.r_offset; - *symndx = GELF_R_SYM(reloc->rel.r_info); - return 0; -} + bool rela = rsec->sh.sh_type == SHT_RELA; + void *retp; -static int read_rela_reloc(struct section *rsec, int i, struct reloc *reloc, unsigned int *symndx) -{ - if (!gelf_getrela(rsec->data, i, &reloc->rela)) { + if (rela) + retp = gelf_getrela(rsec->data, i, &reloc->rela); + else + retp = gelf_getrel(rsec->data, i, &reloc->rel); + + if (!retp) { WARN_ELF("gelf_getrela"); return -1; } - reloc->type = GELF_R_TYPE(reloc->rela.r_info); - reloc->addend = reloc->rela.r_addend; - reloc->offset = reloc->rela.r_offset; - *symndx = GELF_R_SYM(reloc->rela.r_info); + + reloc->offset = reloc->rel.r_offset; + reloc->type = GELF_R_TYPE(reloc->rel.r_info); + reloc->addend = rela ? reloc->rela.r_addend : 0; + return 0; } @@ -926,20 +921,13 @@ static int read_relocs(struct elf *elf) } for (i = 0; i < rsec->sh.sh_size / rsec->sh.sh_entsize; i++) { reloc = &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; reloc->sec = rsec; reloc->idx = i; + symndx = GELF_R_SYM(reloc->rel.r_info); reloc->sym = sym = 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; } -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; - relocname = malloc(strlen(sec->name) + strlen(".rel") + 1); - if (!relocname) { + rsec_name = 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); - rsec = elf_create_section(elf, relocname, sizeof(GElf_Rel), 0); - free(relocname); + rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), 0); + free(rsec_name); if (!rsec) return NULL; sec->rsec = rsec; rsec->base = sec; - rsec->sh.sh_type = SHT_REL; - rsec->sh.sh_addralign = 8; + rsec->sh.sh_type = SHT_RELA; + rsec->sh.sh_addralign = elf_addr_size(elf); rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; rsec->sh.sh_info = sec->idx; rsec->sh.sh_flags = SHF_INFO_LINK; @@ -1172,55 +1160,11 @@ static struct section *elf_create_rel_reloc_section(struct elf *elf, return rsec; } -static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base) -{ - char *relocname; - struct section *rsec; - int addrsize = elf_class_addrsize(elf); - - relocname = malloc(strlen(base->name) + strlen(".rela") + 1); - if (!relocname) { - perror("malloc"); - return NULL; - } - strcpy(relocname, ".rela"); - strcat(relocname, base->name); - - if (addrsize == sizeof(u32)) - rsec = elf_create_section(elf, relocname, sizeof(Elf32_Rela), 0); - else - rsec = elf_create_section(elf, relocname, sizeof(GElf_Rela), 0); - free(relocname); - if (!rsec) - return NULL; - - base->rsec = rsec; - rsec->base = base; - - rsec->sh.sh_type = SHT_RELA; - rsec->sh.sh_addralign = addrsize; - rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; - rsec->sh.sh_info = base->idx; - rsec->sh.sh_flags = 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 = rsec->sh.sh_type == SHT_RELA; struct reloc *reloc; - int idx = 0; + int idx = 0, ret; void *buf; /* Allocate a buffer for relocations */ @@ -1232,13 +1176,19 @@ static int elf_rebuild_rel_reloc_section(struct section *rsec) rsec->data->d_buf = buf; rsec->data->d_size = rsec->sh.sh_size; - rsec->data->d_type = ELF_T_REL; + rsec->data->d_type = rela ? ELF_T_RELA : ELF_T_REL; idx = 0; list_for_each_entry(reloc, &rsec->reloc_list, list) { reloc->rel.r_offset = reloc->offset; reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); - if (!gelf_update_rel(rsec->data, idx, &reloc->rel)) { + if (rela) { + reloc->rela.r_addend = reloc->addend; + ret = gelf_update_rela(rsec->data, idx, &reloc->rela); + } else { + ret = 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 section *rsec) return 0; } -static int elf_rebuild_rela_reloc_section(struct section *rsec) -{ - struct reloc *reloc; - int idx = 0; - void *buf; - - /* Allocate a buffer for relocations with addends */ - buf = malloc(rsec->sh.sh_size); - if (!buf) { - perror("malloc"); - return -1; - } - - rsec->data->d_buf = buf; - rsec->data->d_size = rsec->sh.sh_size; - rsec->data->d_type = ELF_T_RELA; - - idx = 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { - reloc->rela.r_offset = reloc->offset; - reloc->rela.r_addend = reloc->addend; - reloc->rela.r_info = 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 = reloc->sec; + int ret; - if (rsec->sh.sh_type == SHT_REL) { - reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); - reloc->rel.r_offset = reloc->offset; + reloc->rel.r_offset = reloc->offset; + reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); - if (!gelf_update_rel(rsec->data, reloc->idx, &reloc->rel)) { - WARN_ELF("gelf_update_rel"); - return -1; - } - } else { - reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); + if (rsec->sh.sh_type == SHT_RELA) { reloc->rela.r_addend = reloc->addend; - reloc->rela.r_offset = reloc->offset; + ret = gelf_update_rela(rsec->data, reloc->idx, &reloc->rela); + } else { + ret = gelf_update_rel(rsec->data, reloc->idx, &reloc->rel); + } - 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; } elf->changed = true; diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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 #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; } -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] == ELFCLASS32) - return sizeof(u32); - else - return sizeof(u64); + return elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8; +} + +static inline size_t elf_rela_size(struct elf *elf) +{ + return elf_addr_size(elf) == 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Rela); } #define for_each_sec(file, sec) \ From patchwork Tue May 30 17:20:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101031 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340195vqr; Tue, 30 May 2023 10:23:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5BZFWgpJh4vfmhXRS5a1M11HAfe9YXwXtokXRxxmGSuRNwxqgraWcrawaWEKIaefdpZTxZ X-Received: by 2002:a05:6a20:3c91:b0:103:8c8b:c689 with SMTP id b17-20020a056a203c9100b001038c8bc689mr3702496pzj.51.1685467395388; Tue, 30 May 2023 10:23:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467395; cv=none; d=google.com; s=arc-20160816; b=DKD4xK50tQUyQtK5k7Ah5ud7SpKoEgYtQYlOexudnJJKDdqHoRhHBTxL22Ri+mmGeB NaMpkMn54R/BFRVUXMyg1B6XqpONvrGObLZSLC6WiAb6AwsFlCbB52BEqTOJ5GwQadzH 32VQahrqs3MfFa2+GdCo4tynC/E6QX1fd48JMDCuyvZA1HGRPUpoCXNeXOby4uOQv7rF AARGWfk1aoZHq6dpnsMq9Ezjz8Nz/Ci2SNpirVetQmpYtGyzyt5ktz4qWizRZxPFQEZZ 6K/twhf1/ZLaFTsFL3g6QWPseuNgqtye6hqZ7wzzC1KwTryUJZSnfaob9oJJy3VIPsGo AEZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3YHfpCUa2gU3LwtJAJ9r9v4P9hVh1hDWJv/GYJnsc+w=; b=Y2OdX9To336t88pkpfYOt2GJtni3EfplKKa0q+pcoWYpzVHBrbnIP6ZLDW3gUlhGcj a6mKPlzx1jwt52kcYE1QRZWcqOwq8IE+DahxlO45CL4j+OFNaY0F5psa8xzHr9tPwH4k A0PxctgqEfXodqG+F/ysY4oijZYTtkjfS8DeEaw3I1q306TxQ3HZ6tXFc5cqvGyuG3OK zh//mUgV53iXbG6l62MoQtia/e7TE7OacR26VORE33gvq2Lg0LIJKGRNInSn1tqAv492 yN2EAtb5WDN9Xg9S7r4R+9pJu3dS5gcWHIoJJwZ8LFVeWkhcbAdRUMnrDbMqczl6YMT6 azjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RWphnRNt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l184-20020a6388c1000000b0053fb39d8e81si1731239pgd.584.2023.05.30.10.22.57; Tue, 30 May 2023 10:23:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RWphnRNt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233262AbjE3RVo (ORCPT + 99 others); Tue, 30 May 2023 13:21:44 -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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340659626737525?= X-GMAIL-MSGID: =?utf-8?q?1767340659626737525?= 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) } } - if (sec->sh.sh_flags & SHF_EXECINSTR) - elf->text_size += 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 += sec->sh.sh_size / sec->sh.sh_entsize; } if (opts.stats) { @@ -888,19 +888,18 @@ static int read_reloc(struct section *rsec, int i, struct reloc *reloc) static int read_relocs(struct elf *elf) { - unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0; + unsigned long nr_reloc, max_reloc = 0; struct section *rsec; struct reloc *reloc; unsigned int symndx; struct symbol *sym; int i; - if (!elf_alloc_hash(reloc, elf->text_size / 16)) + if (!elf_alloc_hash(reloc, elf->num_relocs)) return -1; list_for_each_entry(rsec, &elf->sections, list) { - if ((rsec->sh.sh_type != SHT_RELA) && - (rsec->sh.sh_type != SHT_REL)) + if (!is_reloc_sec(rsec)) continue; rsec->base = find_section_by_index(elf, rsec->sh.sh_info); @@ -942,12 +941,11 @@ static int read_relocs(struct elf *elf) nr_reloc++; } max_reloc = max(max_reloc, nr_reloc); - tot_reloc += nr_reloc; } 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); } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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; 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) == 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Rela); } +static inline bool is_reloc_sec(struct section *sec) +{ + return sec->sh.sh_type == SHT_RELA || sec->sh.sh_type == SHT_REL; +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) From patchwork Tue May 30 17:20:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101047 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2344494vqr; Tue, 30 May 2023 10:31:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7E6uRUL+oiOo2z/yCNzetwtTWOEnOoj4VFqYl7d8gUwoZJJjiJxM8/zL7bs4h0nDzet+xR X-Received: by 2002:a17:90b:e86:b0:256:3191:640f with SMTP id fv6-20020a17090b0e8600b002563191640fmr12193139pjb.4.1685467890070; Tue, 30 May 2023 10:31:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467890; cv=none; d=google.com; s=arc-20160816; b=GEpbosOdBNpSyEMhyuE+InzvUfLJiIlQqyfNzGwJia6ObY7USBBfQhJQcR/6P5rFqo dvZ7QvCHIfsGWiDM4OpFb6Rwd7TNL6oz+KVONUCDpZRXENnXTNft/U/zUhevBuHbYI27 6KT0KJot23H9MOvPRbCwq4lHkGjG+uW23Rr2JSEHgEQ41JYKNOxeZ6fzJ3vj25B11ZdK VqRurwJPAGy76AMRKUXyIqAltBalN69jlhkVxfQCZjuIfLImoJOWGAJl1U7ENSyh0YCH Hb8NSPMbB8QRC3I55xr88fQ8IxRdJiuyqxE5xPujzlC+XUkR14olY+2YuNLKelIohalS dQSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=45HnI+Cc4i+/4k+erjgMAti0FBSEw4sTF+8j6uj4EzY=; b=k5GGQglijUZwfUI3iKWd/F9j4Jqysw8dtYvyGGWqkI5T060JoOtIfuQiB7/a5iOoXB nmlXtQUP4Nv2b2Y1HTmyc3+tFT6/K+LQMz2swCfzx/29Gm67r+2MgKlRadzyg9ueD8XG d3N1Fd27gr29QzO0z/6OWTKW2TaR9ybGEjf2lZzEd1YRZilJI6U9XDQsn/0jRG04sQSC OsffyFuEAottieg6yJhdjo1ba0vE9PG+DJkHtl4mBGr+npTpOElMHRZNhYeoqh8cr0Zq b9fSJhnLRbuwgf8WZi/zEisZ7Q3IDNXostim6oYFlV6/nwFrHYvkTolHZ5aZW8F2yuuk ggHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BX04ZWtW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d187-20020a6336c4000000b00533fce755adsi2572190pga.130.2023.05.30.10.31.15; Tue, 30 May 2023 10:31:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BX04ZWtW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233000AbjE3RVz (ORCPT + 99 others); Tue, 30 May 2023 13:21:55 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341177898379074?= X-GMAIL-MSGID: =?utf-8?q?1767341177898379074?= 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)); sec->rsec->sh.sh_size += sec->rsec->sh.sh_entsize; - sec->rsec->changed = true; + + mark_sec_changed(elf, sec->rsec, true); return 0; } @@ -577,7 +578,7 @@ static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym) struct reloc *reloc; list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) - reloc->sec->changed = true; + mark_sec_changed(elf, reloc->sec, true); } /* @@ -654,7 +655,7 @@ static int elf_update_symbol(struct elf *elf, struct section *symtab, symtab_data->d_align = 1; symtab_data->d_type = ELF_T_SYM; - symtab->changed = true; + mark_sec_changed(elf, symtab, true); symtab->truncate = true; if (t) { @@ -669,7 +670,7 @@ static int elf_update_symbol(struct elf *elf, struct section *symtab, shndx_data->d_align = sizeof(Elf32_Word); shndx_data->d_type = ELF_T_WORD; - symtab_shndx->changed = true; + mark_sec_changed(elf, symtab_shndx, true); symtab_shndx->truncate = true; } @@ -773,11 +774,11 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym) } symtab->sh.sh_size += symtab->sh.sh_entsize; - symtab->changed = true; + mark_sec_changed(elf, symtab, true); if (symtab_shndx) { symtab_shndx->sh.sh_size += sizeof(Elf32_Word); - symtab_shndx->changed = true; + mark_sec_changed(elf, symtab_shndx, true); } return sym; @@ -1040,7 +1041,8 @@ static int elf_add_string(struct elf *elf, struct section *strtab, char *str) len = strtab->sh.sh_size; strtab->sh.sh_size += data->d_size; - strtab->changed = true; + + mark_sec_changed(elf, strtab, true); return len; } @@ -1075,7 +1077,6 @@ struct section *elf_create_section(struct elf *elf, const char *name, } sec->idx = elf_ndxscn(s); - sec->changed = true; sec->data = elf_newdata(s); if (!sec->data) { @@ -1122,7 +1123,7 @@ struct section *elf_create_section(struct elf *elf, const char *name, elf_hash_add(section, &sec->hash, sec->idx); elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); - elf->changed = true; + mark_sec_changed(elf, sec, true); return sec; } @@ -1208,9 +1209,8 @@ int elf_write_insn(struct elf *elf, struct section *sec, } memcpy(data->d_buf + offset, insn, len); - elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY); - elf->changed = true; + mark_sec_changed(elf, sec, true); return 0; } @@ -1235,7 +1235,7 @@ int elf_write_reloc(struct elf *elf, struct reloc *reloc) return -1; } - elf->changed = true; + mark_sec_changed(elf, rsec, true); return 0; } @@ -1307,12 +1307,14 @@ int elf_write(struct elf *elf) if (sec->truncate) elf_truncate_section(elf, sec); - if (sec->changed) { + if (sec_changed(sec)) { s = 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; } - sec->changed = false; - elf->changed = true; + mark_sec_changed(elf, sec, false); } } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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; }; @@ -164,6 +164,18 @@ static inline bool is_reloc_sec(struct section *sec) return sec->sh.sh_type == SHT_RELA || sec->sh.sh_type == SHT_REL; } +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 = changed; + elf->changed |= changed; +} + #define for_each_sec(file, sec) \ list_for_each_entry(sec, &file->elf->sections, list) From patchwork Tue May 30 17:20:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101034 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340305vqr; Tue, 30 May 2023 10:23:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4cKAv/zi43S1euz8cS7DIT0n6DMx2uDQ/ki0q1mIZhL3+oE4JuKkybAH4b24GlaTji6giY X-Received: by 2002:a05:6a20:4290:b0:10c:5802:fce4 with SMTP id o16-20020a056a20429000b0010c5802fce4mr3617585pzj.48.1685467405459; Tue, 30 May 2023 10:23:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467405; cv=none; d=google.com; s=arc-20160816; b=WzWl5oSV9cNYametkt4AUucNsksjcppLobNGxFnS4GiowmHHlNvMja4T/Or4XEqaLd CW81BS0TCX/ia87C1vB5NsD5IPate+xQvqVW39EPMynTyaPL5+qY5RmKiBH5IwpxzvSx VLaN8eKGq0L1dncr7saUIa1Y1bj1BVJMAVuZc50RFE3jQ8sXAXxIwXVJchAoYJMDoh6m wsZyldDo1vLzaVGRWvWugAkVW2STYCnTEqLivrwv/JKLQ1jB9Qs0iQQRGUPX2RSain2k 2zm/Bhg7cTMxoRy/jIhRQbmC1Dd3GJ2sWdcFJHjGIA9xLc3tL4Y6b4tUeeu2YCMbuhah g8yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=KmwCdC7lTczKlaLDRKFCmlGWrCu/5Bhbgf+14uxhVgY=; b=Z8wSS/1ndag3jCcuef0EujHjmUmGBfw9fAJMXgeg5lGdGuyFG7JUyc1OpMxHwiNITO M/9nHdO6B71ZFP2zP5fFHU3+VfhIwyEUIMSWjwryMFb5BpswRoo0FDfGLHNi5jmYB5fP EjaAv7rGKcmiTGEh6nvEL/qVV7TURNfzerYOHkozZO5MZtOkeKXd40IIx8TroELfK0aw HqSNvkBOlZeL5hN2I+mk3xkSdyPOqIGb+Ko8uUKZiFHpmo5RsN9RRMQFq1so2UQ/P3eE XOe0DVLn5MO7VWuaW3dcP2HhxAqBYGQP3qNDNkyTvHN+P0WndOm0D82YQWzj1g42Q6R+ 5tUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YnZlUeQ8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l131-20020a633e89000000b00534897b70f9si10908773pga.29.2023.05.30.10.23.09; Tue, 30 May 2023 10:23:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YnZlUeQ8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231979AbjE3RWJ (ORCPT + 99 others); Tue, 30 May 2023 13:22:09 -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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340669994505736?= X-GMAIL-MSGID: =?utf-8?q?1767340669994505736?= 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 -#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 #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 -#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 #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 -#include #include #include #include @@ -655,8 +654,8 @@ static int add_dead_ends(struct objtool_file *file) 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++; - sec = elf_create_section(file->elf, ".static_call_sites", - sizeof(struct static_call_site), idx); + sec = elf_create_section_pair(file->elf, ".static_call_sites", + sizeof(*site), idx, idx * 2); if (!sec) return -1; - /* 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 |= SHF_WRITE; idx = 0; list_for_each_entry(insn, &file->static_call_list, call_node) { - site = (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; /* find key symbol */ @@ -734,10 +729,10 @@ static int create_static_call_sections(struct objtool_file *file) free(key_name); /* 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; idx++; @@ -765,26 +760,18 @@ static int create_retpoline_sites_sections(struct objtool_file *file) if (!idx) return 0; - sec = elf_create_section(file->elf, ".retpoline_sites", - sizeof(int), idx); - if (!sec) { - WARN("elf_create_section: .retpoline_sites"); + sec = elf_create_section_pair(file->elf, ".retpoline_sites", + sizeof(int), idx, idx); + if (!sec) return -1; - } idx = 0; list_for_each_entry(insn, &file->retpoline_call_list, call_node) { - int *site = (int *)sec->data->d_buf + idx; - *site = 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; - } idx++; } @@ -811,26 +798,18 @@ static int create_return_sites_sections(struct objtool_file *file) if (!idx) return 0; - sec = elf_create_section(file->elf, ".return_sites", - sizeof(int), idx); - if (!sec) { - WARN("elf_create_section: .return_sites"); + sec = elf_create_section_pair(file->elf, ".return_sites", + sizeof(int), idx, idx); + if (!sec) return -1; - } idx = 0; list_for_each_entry(insn, &file->return_thunk_list, call_node) { - int *site = (int *)sec->data->d_buf + idx; - *site = 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; - } idx++; } @@ -863,12 +842,10 @@ static int create_ibt_endbr_seal_sections(struct objtool_file *file) if (!idx) return 0; - sec = elf_create_section(file->elf, ".ibt_endbr_seal", - sizeof(int), idx); - if (!sec) { - WARN("elf_create_section: .ibt_endbr_seal"); + sec = elf_create_section_pair(file->elf, ".ibt_endbr_seal", + sizeof(int), idx, idx); + if (!sec) return -1; - } idx = 0; list_for_each_entry(insn, &file->endbr_list, call_node) { @@ -883,13 +860,10 @@ static int create_ibt_endbr_seal_sections(struct objtool_file *file) !strcmp(sym->name, "cleanup_module"))) WARN("%s(): not an indirect call target", sym->name); - 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; - } idx++; } @@ -901,7 +875,6 @@ static int create_cfi_sections(struct objtool_file *file) { struct section *sec; struct symbol *sym; - unsigned int *loc; int idx; sec = find_section_by_name(file->elf, ".cfi_sites"); @@ -922,7 +895,8 @@ static int create_cfi_sections(struct objtool_file *file) idx++; } - sec = elf_create_section(file->elf, ".cfi_sites", sizeof(unsigned int), idx); + sec = elf_create_section_pair(file->elf, ".cfi_sites", + sizeof(unsigned int), idx, idx); if (!sec) return -1; @@ -934,13 +908,9 @@ static int create_cfi_sections(struct objtool_file *file) if (strncmp(sym->name, "__cfi_", 6)) continue; - loc = (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; idx++; @@ -970,7 +940,8 @@ static int create_mcount_loc_sections(struct objtool_file *file) list_for_each_entry(insn, &file->mcount_loc_list, call_node) idx++; - sec = elf_create_section(file->elf, "__mcount_loc", addr_size, idx); + sec = elf_create_section_pair(file->elf, "__mcount_loc", addr_size, + idx, idx); if (!sec) return -1; @@ -978,17 +949,17 @@ static int create_mcount_loc_sections(struct objtool_file *file) idx = 0; list_for_each_entry(insn, &file->mcount_loc_list, call_node) { - void *loc; - loc = sec->data->d_buf + idx; - memset(loc, 0, addr_size); + struct reloc *reloc; - if (elf_add_reloc_to_insn(file->elf, sec, idx, - addr_size == sizeof(u64) ? R_ABS64 : R_ABS32, - insn->sec, insn->offset)) + reloc = elf_init_reloc_text_sym(file->elf, sec, idx * addr_size, idx, + insn->sec, insn->offset); + if (!reloc) return -1; - idx += addr_size; + reloc->type = addr_size == 8 ? R_ABS64 : R_ABS32; + + idx++; } return 0; @@ -998,7 +969,6 @@ static int create_direct_call_sections(struct objtool_file *file) { struct instruction *insn; struct section *sec; - unsigned int *loc; int idx; sec = find_section_by_name(file->elf, ".call_sites"); @@ -1015,20 +985,17 @@ static int create_direct_call_sections(struct objtool_file *file) list_for_each_entry(insn, &file->call_list, call_node) idx++; - sec = elf_create_section(file->elf, ".call_sites", sizeof(unsigned int), idx); + sec = elf_create_section_pair(file->elf, ".call_sites", + sizeof(unsigned int), idx, idx); if (!sec) return -1; idx = 0; list_for_each_entry(insn, &file->call_list, call_node) { - loc = (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; 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; } -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 offset, - unsigned int type, struct symbol *sym, s64 addend) -{ - struct reloc *reloc; - - if (!sec->rsec && !elf_create_rela_section(elf, sec)) - return -1; - - reloc = malloc(sizeof(*reloc)); - if (!reloc) { - perror("malloc"); - return -1; - } - memset(reloc, 0, sizeof(*reloc)); - - reloc->sec = sec->rsec; - reloc->offset = offset; - reloc->type = type; - reloc->sym = sym; - reloc->addend = 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 += 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_sections() @@ -841,13 +806,57 @@ elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size) return sym; } -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 >= 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 = malloc(sizeof(*reloc)); + if (!reloc) { + perror("malloc"); + return NULL; + } + memset(reloc, 0, sizeof(*reloc)); + + reloc->idx = reloc_idx; + reloc->sec = rsec; + reloc->offset = offset; + reloc->type = type; + reloc->sym = sym; + reloc->addend = 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 = insn_sec->sym; int addend = insn_off; + 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 section *sec, */ sym = elf_create_section_symbol(elf, insn_sec); if (!sym) - return -1; + return NULL; insn_sec->sym = sym; } - 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)); } 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 section *strtab, char *str) } 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 = entsize * nr; @@ -1129,7 +1155,8 @@ struct section *elf_create_section(struct elf *elf, const char *name, } 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(struct elf *elf, strcpy(rsec_name, ".rela"); strcat(rsec_name, sec->name); - rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), 0); + rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr); free(rsec_name); if (!rsec) return NULL; - sec->rsec = rsec; - rsec->base = sec; - + rsec->data->d_type = ELF_T_RELA; rsec->sh.sh_type = SHT_RELA; rsec->sh.sh_addralign = elf_addr_size(elf); rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; rsec->sh.sh_info = sec->idx; rsec->sh.sh_flags = SHF_INFO_LINK; + sec->rsec = rsec; + rsec->base = sec; + return rsec; } +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 = 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 = rsec->sh.sh_type == SHT_RELA; struct reloc *reloc; int idx = 0, ret; - void *buf; - - /* Allocate a buffer for relocations */ - buf = malloc(rsec->sh.sh_size); - if (!buf) { - perror("malloc"); - return -1; - } - - rsec->data->d_buf = buf; - rsec->data->d_size = rsec->sh.sh_size; - rsec->data->d_type = rela ? ELF_T_RELA : ELF_T_REL; idx = 0; list_for_each_entry(reloc, &rsec->reloc_list, list) { reloc->rel.r_offset = reloc->offset; reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); - if (rela) { + if (rsec->sh.sh_type == SHT_RELA) { reloc->rela.r_addend = reloc->addend; ret = gelf_update_rela(rsec->data, idx, &reloc->rela); } else { diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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 { }; 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); struct symbol *elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size); -int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, - 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); 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) == 4 ? sizeof(Elf32_Rela) : sizeof(Elf64_Rela); } +static inline unsigned int elf_data_rela_type(struct elf *elf) +{ + return elf_addr_size(elf) == 4 ? R_DATA32 : R_DATA64; +} + +static inline unsigned int elf_text_rela_type(struct elf *elf) +{ + return elf_addr_size(elf) == 4 ? R_TEXT32 : R_TEXT64; +} + static inline bool is_reloc_sec(struct section *sec) { return sec->sh.sh_type == SHT_RELA || sec->sh.sh_type == 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 section *orc_sec, orc->bp_offset = bswap_if_needed(elf, orc->bp_offset); /* 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; return 0; @@ -242,7 +242,7 @@ int orc_create(struct objtool_file *file) if (!orc_sec) return -1; - sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), nr); + sec = elf_create_section_pair(file->elf, ".orc_unwind_ip", sizeof(int), nr, nr); if (!sec) return -1; From patchwork Tue May 30 17:21:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101033 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340306vqr; Tue, 30 May 2023 10:23:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7JKq/Ty2GKvgqN4RcNH3AqgEj53BwruK3oF+7sNlKNDjGQsmBQKddf8no5CnAcec5rl1uZ X-Received: by 2002:a05:6a20:7f84:b0:103:b436:aef7 with SMTP id d4-20020a056a207f8400b00103b436aef7mr12595372pzj.16.1685467405668; Tue, 30 May 2023 10:23:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467405; cv=none; d=google.com; s=arc-20160816; b=naERyeTqLVUl9yhtk+rcUJ1x6YmtXhNUlMdIDVNdRcBGQBKm8nRnqtmKEWSedqAkSZ IlYZinLi2XM7MDNjC+BKlQEOY5fSgrnafbWqxDpSZ4m1Zx5+vtCOWpzZo7iJFXOSq+uh YbMehBPgn0vEyTcpnU6nr6F/Q9lSpDtLljY8PiWFJItdvqz6nIchFr2cEdymPnhbzsL+ zxPJbUullVgjFHeMkiwo3BuVDUrIdcR+L84DGuNlJjsGiyDbUuW8B1crNvt6ZvOQ8ywK Ibjg4WuFAGxzoF4tXpluotD7DrCLohTR1Aa5siuWCV+z5Ci7/dgf9xwCG6kqmdIkx3+r irEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UcDAqSR1Px9ocY2Z8y0JplTm3/sHup5UI7mt0jAWxxA=; b=LD8i10WTy+CnmrE6nUl+xtzSlXzLhyuQEbIrhDmmLiBuryeRngXj8KrL9DEgyQyaRH WXp67byaT4UfxiaN8XNrA0MRYGslO6WsAPd8/fkrQjQzwrSYpjm2Mc9tNRHtRS/xIZQ3 oLtX/rt8aXJoL6oaWDNCcVX+FuLYoiEnIsi0qgnP8u/zPaQCWk/tuqBcH3yhANplwNW4 6OTdjq1fKJdiErvkajaFiI7cy4Z7nkCGP/vQ/lL3ubmxKTJBwL6s1jAUN+43W2X+lvXD lGLfvFaPPiMAlrKqZBsZjZpxuvDtZTo47ZbB7n+ZQhBNDdO4ohbPtEDqOHJdQuiqbqOw f5SQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="ruh/7afC"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j62-20020a638041000000b0053f6910ccc8si1604471pgd.747.2023.05.30.10.23.10; Tue, 30 May 2023 10:23:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="ruh/7afC"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233382AbjE3RWD (ORCPT + 99 others); Tue, 30 May 2023 13:22:03 -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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340670463249030?= X-GMAIL-MSGID: =?utf-8?q?1767340670463249030?= 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) } /* - * Ensure that any reloc section containing references to @sym is marked - * changed such that it will get re-generated in elf_rebuild_reloc_sections() - * 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; - 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; } /* @@ -716,13 +718,14 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym) hlist_del(&old->hash); elf_hash_add(symbol, &old->hash, old->idx); - elf_dirty_reloc_sym(elf, old); - if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { WARN("elf_update_symbol move"); return NULL; } + if (elf_update_sym_relocs(elf, old)) + return NULL; + new_idx = first_non_local; } @@ -833,12 +836,13 @@ static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, reloc->sym = sym; reloc->addend = addend; + 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)); - mark_sec_changed(elf, rsec, true); - return reloc; } @@ -1203,31 +1207,6 @@ struct section *elf_create_section_pair(struct elf *elf, const char *name, return sec; } -static int elf_rebuild_reloc_section(struct elf *elf, struct section *rsec) -{ - struct reloc *reloc; - int idx = 0, ret; - - idx = 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { - reloc->rel.r_offset = reloc->offset; - reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); - if (rsec->sh.sh_type == SHT_RELA) { - reloc->rela.r_addend = reloc->addend; - ret = gelf_update_rela(rsec->data, idx, &reloc->rela); - } else { - ret = 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; } - if (sec->base && - elf_rebuild_reloc_section(elf, sec)) { - WARN("elf_rebuild_reloc_section"); - return -1; - } - mark_sec_changed(elf, sec, false); } } From patchwork Tue May 30 17:21:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101049 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2344515vqr; Tue, 30 May 2023 10:31:33 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7t6Ig4ly7bslAdktGmJz0CZDogCBNeSk4Jm0UDVBwlOLvtAPmQrdSWy/3uZjCEtkRSK9jd X-Received: by 2002:a17:90b:17ca:b0:253:572f:79b2 with SMTP id me10-20020a17090b17ca00b00253572f79b2mr3221999pjb.36.1685467892888; Tue, 30 May 2023 10:31:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467892; cv=none; d=google.com; s=arc-20160816; b=F0qNyUrQ922SWUfq1aeGKnUo5LFwi4dDlnQE4HveZe9g3JMKETzb3n4KfOcsYX0Lpa fv3QsgRCIFMV3I0zN+NAW64zjh3RF+9Ld7ollA/rFrGvpYxlv0NOVOCnDyyQriz3tKCx eGXVK8rc8g+e1cC03noJvwMHRptNy39G9AIgfkMnz6+Sr8ypXYDAoZgXxA3vm45NGVLU pAPf/0B2sYM2bzC8LHmb9djHLLBC4iZ6qAGsmutcnuoUEDcZs80cFRtmZ1WrE5RKTdwq 4WFswJsRZyx8CLs+0Vtl26LongRiw92K/FOlGUFvVqdD3D9XywAFYetO4p/orxxZslQ+ YuMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VKhdX2Nhlxap9UaTOKSDg7fK54dEeXos3IhKd8EDEQE=; b=uQnTmjaSxeuCHktivUgjZj4AnUoeNohPime8kNd3QlNbf/cJbdbv6FBosUYjsbkieD hEJjcuHrJJq5mw5qk957KJ28+y7WdszTNIzy0mp8657fPTp8Bt2tI1C+RMlAnikvJ3fJ J15Iw/XgxEVSqo5pPw5bBWBBszHj18zTmp7VtzzluhYVG5Byd0ezVLtFm6XjsGg7t4Ks VhN3Fjw3hBWLdLisZSHE05jCyrETiOFo5Y6X3s/R8aOYiBHXcd7fdgX0sns1QtVYoXIv gzGFQsKsdtjzs92YE/5pxp6MIR32NVs27/GCEZxVA5kXxJk4Reo2r8H2em17eCGZjWd4 HvtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=F8yDNQfT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p14-20020a17090a2c4e00b002566f56e9aesi4720536pjm.105.2023.05.30.10.31.18; Tue, 30 May 2023 10:31:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=F8yDNQfT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232222AbjE3RWA (ORCPT + 99 others); Tue, 30 May 2023 13:22:00 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341181493578006?= X-GMAIL-MSGID: =?utf-8?q?1767341181493578006?= 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) 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); if (elf->fd > 0) close(elf->fd); - 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. + */ } From patchwork Tue May 30 17:21:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101044 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2344278vqr; Tue, 30 May 2023 10:31:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4PXKHzNNpU1wOQIslXqI9wq/LYmK4ZcnnKpmlILyQZZufMyBPmZDIxtyGXUBurbEZVYOs8 X-Received: by 2002:a17:902:b194:b0:1ae:62ed:9630 with SMTP id s20-20020a170902b19400b001ae62ed9630mr2879326plr.15.1685467870264; Tue, 30 May 2023 10:31:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467870; cv=none; d=google.com; s=arc-20160816; b=s1/BeHhAo+oHuLlSe3o60JFsBQ2JypZJL84IxPlp157EKFGt0EEJN/8WGO5Ukogj89 +W1mMAg+7HtdbK7E+/OyBN7aJeZuZKl0Nrw4WgUQuFRSjH88QHnEIkyKpkDmOaJ1L3XP Xs4TKIORwKm9nzNuKbqVSczv1qO8H7V+G3ufVJ2b9tIkgfnT3n/B1G1bWPLIXjEwauU1 Ypc5Ay5NC9wMKOm77PD+NNcDnR4tOnztK9NE2VDZncktoa/nSOO4ohnZWH9xcV4K07pa 3BqRcW1MLXV1bJjZPmRT+Y6Tvcj4vN91mRNemXIxqwyH9J34MJ03/0tl4XprJ9y96U+b saOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+anb3718kv6WMM3e/ASp2a9XvJnJmzmQHhbA0o0QOJg=; b=gc3I46O8oWP4oMc9jdtg+VAuKHOsgCkcI5lH0341345eN7/NMZK5ttYnwDY7Snh5ML fQVM1G7wpyuIYG3s27veN6q/7uTycZhoRPoqH0bLioOPXJSk2m/Lp3akvNT385RPu8xl UUYl1mv36LhGjcvbFfVIVT9vw6Ygq63rBC3oQH8g0eO5OSiL8Y4hVxgWuN8YgtjEz63F HikOOxYCbEYM6XsF5sIJD5DGZMl2vRhM9G0ZDlRpeEcY/kV6sJhJP0mndJoTiMH1v8CT Pn6RQO5PSIaz6pfJF5cax6Adger7H0cQFeH2hL4P8AvUUwHP+3b4xYSKQ2ypke4eG4qf Z3Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OWzvEeVS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d16-20020a170902ced000b001ab0e3e0c1asi3491102plg.553.2023.05.30.10.30.55; Tue, 30 May 2023 10:31:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OWzvEeVS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233345AbjE3RWN (ORCPT + 99 others); Tue, 30 May 2023 13:22:13 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341157767475429?= X-GMAIL-MSGID: =?utf-8?q?1767341157767475429?= 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; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type != 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; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type != 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; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { switch (reloc->sym->type) { case STT_FUNC: func = reloc->sym; @@ -1259,7 +1259,7 @@ static int add_ignore_alternatives(struct objtool_file *file) if (!rsec) return 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type != 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) { /* Check for the end of the table: */ if (reloc != table && reloc->jump_table_start) @@ -2264,7 +2264,7 @@ static int read_noendbr_hints(struct objtool_file *file) if (!rsec) return 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend); if (!insn) { WARN("bad .discard.noendbr entry"); @@ -2287,7 +2287,7 @@ static int read_retpoline_hints(struct objtool_file *file) if (!rsec) return 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type != 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; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type != 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; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type != 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_file *file) if (!rsec) return 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { if (reloc->sym->type != 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_file *file) if (!rsec) return 0; - list_for_each_entry(reloc, &rsec->reloc_list, list) { + for_each_reloc(rsec, reloc) { unsigned long dest_off; if (reloc->sym->type != STT_SECTION) { @@ -4395,7 +4395,7 @@ static int validate_ibt(struct objtool_file *file) strstr(sec->name, "__patchable_function_entries")) continue; - list_for_each_entry(reloc, &sec->rsec->reloc_list, list) + for_each_reloc(sec->rsec, reloc) warnings += validate_ibt_data_reloc(file, reloc); } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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, struct section *sec, for_each_sec(file, __sec) \ sec_for_each_sym(__sec, sym) +#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)) From patchwork Tue May 30 17:21:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101036 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340414vqr; Tue, 30 May 2023 10:23:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7wOOo1P93yW0/Iz/HrIERmQOs6UxAHz+uQ1Ve1Y6N+GtwZc8rDSflADK+yQ2SRjmoSX9EG X-Received: by 2002:a17:90a:f3d4:b0:255:2daa:4796 with SMTP id ha20-20020a17090af3d400b002552daa4796mr3048882pjb.44.1685467418514; Tue, 30 May 2023 10:23:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467418; cv=none; d=google.com; s=arc-20160816; b=wNWP7Z+Tp3tClSRkAo5qyH5O9SJYAQBiI1n115BTXf9dIu6PFgoU8zkzCUen0mfVgV YayXjgteARo4o4BzXLlULmgRLHkTzPYQ+g4gbTwodTn6QjbbII7L9b9WuEiF6DRPIC1c xWqzmsN597qTJXjlr3tcatwPbhaxDzFohJEhuLcwLy59qge+3y9LAlHHLpUEKxA7jCcx YT6PvMsEB7bDvCQ2g6ImmbD2DRzjq656pq+9WfvDyWnA9b6I78sKhZp1CV0qhB/Umz03 G+j/8dMP4cYzqs1xP5JQGJC6dbnwm3Ni7aEsGcByN780enJtaRXw9OKyiy/IAQ/k05Nr htwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uHkFdYjXvrXncZHdfm35k0yo2QMa9RY/Ht67wAHgi58=; b=J+4q0SqlGSYE9Ud3NfHQLyt5N8hFgZpvPdDi4WXsxyaeNJQHQb9eCEa0yck98pOTun D67hidcyrvxKrQIwgdFcbjfOLeZSWwLDBcHn6ZUuFt+UnJ84ImPtqdGY2i15hbL+jFdk xnWE4B/eTOy0YrP0oSCInWBgzY2RuVfOsIqt7PPeYM0ElXTpRfLtGw6LvRFJ/8JekmNW zfxtZDYjrC0vBX6OLsL5exryIlwJkFYIxdOjrru/r+OFbVtv31gAeOYOIm/YRmqgeNQ3 mkFUM+ApJ65Wr5TfyzCNgvjXlIGqGZL3aAeE1q5fP9CITMnRVXWQRLD10xv4C57VQR84 jbKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sDI8gjey; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id np12-20020a17090b4c4c00b002532e1993basi2803671pjb.154.2023.05.30.10.23.22; Tue, 30 May 2023 10:23:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sDI8gjey; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231935AbjE3RWU (ORCPT + 99 others); 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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340683841158079?= X-GMAIL-MSGID: =?utf-8?q?1767340683841158079?= 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, struct section *rsec, unsigned long offset, struct symbol *sym, s64 addend, unsigned int type) { - struct reloc *reloc; + struct reloc *reloc, empty = { 0 }; if (reloc_idx >= 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; } - reloc = malloc(sizeof(*reloc)); - if (!reloc) { - perror("malloc"); + reloc = &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)); reloc->idx = reloc_idx; reloc->sec = rsec; @@ -1185,6 +1186,13 @@ static struct section *elf_create_rela_section(struct elf *elf, rsec->sh.sh_info = sec->idx; rsec->sh.sh_flags = SHF_INFO_LINK; + rsec->reloc_data = calloc(rsec->sh.sh_size / rsec->sh.sh_entsize, + sizeof(struct reloc)); + if (!rsec->reloc_data) { + perror("calloc"); + return NULL; + } + sec->rsec = rsec; rsec->base = sec; From patchwork Tue May 30 17:21:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101037 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340552vqr; Tue, 30 May 2023 10:23:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6yJJ/3KB3QhgQBLDGm1dfVDwSlYFa3jNRXhWQLGizVrglNgDMHA1fv/CVs/Iri0xdOFgJs X-Received: by 2002:a05:6a20:1610:b0:111:90f6:a847 with SMTP id l16-20020a056a20161000b0011190f6a847mr3527116pzj.38.1685467434918; Tue, 30 May 2023 10:23:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467434; cv=none; d=google.com; s=arc-20160816; b=Pv+Pqbw10aScMWz6murfnjmLs9sNESPOWKZtVafAqrjZEIXDeC8pu2+4zIqTAGHitm hdtYYQULL+ZVnbW9MCQ2O22oP/nhgaZ06T0YvgH4Y3tYO4baxE58qjVAXgxeMeUBZAt1 hE1QkjdinCzwxTyUuZWgHzU8720ib9xRfxWVG8wH0ypJj1uyKek+umbDDH0BIa9qdbC5 WwJos2O744jvMxcdONy40+TKY0m6a3TuO8ojbjUGdm6g2FVS9iiTt9GsZmRxrnkIH0t1 UT9Uak/xPXju/upCtTZx6mp/S7821+sh88SADvmXL4bK318AX5NXLwHQI92mT89GALNA VfBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kaa7ZYVMwAbol5CFMIqCLXOOMlWhgWC8xRDrSULAq/g=; b=flYm/NuFgtAkP/sDO89lUXub2X6ANiphVGeoMLcd81vE5UMpuoI8xbMeGOOcDEqTQj qpchqrym3IThHEU8z86HoAXzhUTzF+W04Hxu8HOM3qOTWH0PU60dQ32baka9dIYQaaXO zEVjWTROKBh/BNPbZ45uX5XTAIIDP8OvgtAomERq9VTDMqq/1/kodjkGinvGtBgtavGL E5nXXZo96EL/nwXCQAfNWhfKLcxj7U6N40wEdRVrXAI8BUEc6AvXYzDQhxV/ebcNa/X2 kYHHFemTwBYJWTeqnvt5yJ9H/B+6Sd9MjXT1FAyiM+73zmse4uenKq0Rei4k1FHuXZeT 4JrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NUXDqUKI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k5-20020a633d05000000b00502d73d59f4si11727628pga.222.2023.05.30.10.23.40; Tue, 30 May 2023 10:23:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NUXDqUKI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233302AbjE3RWX (ORCPT + 99 others); Tue, 30 May 2023 13:22:23 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340700957540673?= X-GMAIL-MSGID: =?utf-8?q?1767340700957540673?= 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 = &elf->section_data[i]; INIT_LIST_HEAD(&sec->symbol_list); - INIT_LIST_HEAD(&sec->reloc_list); s = 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)); if (is_reloc_sec(sec)) - elf->num_relocs += sec->sh.sh_size / sec->sh.sh_entsize; + elf->num_relocs += sec_num_entries(sec); } if (opts.stats) { @@ -407,7 +406,7 @@ static int read_symbols(struct elf *elf) if (symtab_shndx) shndx_data = symtab_shndx->data; - symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize; + symbols_nr = 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; } - new_idx = symtab->sh.sh_size / symtab->sh.sh_entsize; + new_idx = sec_num_entries(symtab); if (GELF_ST_BIND(sym->sym.st_info) != 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 = { 0 }; - if (reloc_idx >= 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 >= 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; } - reloc = &rsec->reloc_data[reloc_idx]; + reloc = &rsec->relocs[reloc_idx]; 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, struct section *rsec, 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)); return reloc; @@ -944,14 +942,13 @@ static int read_relocs(struct elf *elf) rsec->base->rsec = rsec; nr_reloc = 0; - rsec->reloc_data = calloc(rsec->sh.sh_size / rsec->sh.sh_entsize, - sizeof(*reloc)); - if (!rsec->reloc_data) { + rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc)); + if (!rsec->relocs) { perror("calloc"); return -1; } - for (i = 0; i < rsec->sh.sh_size / rsec->sh.sh_entsize; i++) { - reloc = &rsec->reloc_data[i]; + for (i = 0; i < sec_num_entries(rsec); i++) { + reloc = &rsec->relocs[i]; if (read_reloc(rsec, i, reloc)) return -1; @@ -967,7 +964,6 @@ static int read_relocs(struct elf *elf) } 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)); nr_reloc++; @@ -1093,7 +1089,6 @@ struct section *elf_create_section(struct elf *elf, const char *name, memset(sec, 0, sizeof(*sec)); INIT_LIST_HEAD(&sec->symbol_list); - INIT_LIST_HEAD(&sec->reloc_list); s = elf_newscn(elf->elf); if (!s) { @@ -1186,9 +1181,8 @@ static struct section *elf_create_rela_section(struct elf *elf, rsec->sh.sh_info = sec->idx; rsec->sh.sh_flags = SHF_INFO_LINK; - rsec->reloc_data = calloc(rsec->sh.sh_size / rsec->sh.sh_entsize, - sizeof(struct reloc)); - if (!rsec->reloc_data) { + rsec->relocs = 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/objtool/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; }; struct symbol { @@ -68,7 +67,6 @@ struct symbol { }; 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, struct section *sec, elf->changed |= changed; } +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) @@ -210,10 +213,15 @@ static inline void mark_sec_changed(struct elf *elf, struct section *sec, sec_for_each_sym(__sec, sym) #define for_each_reloc(rsec, reloc) \ - list_for_each_entry(reloc, &rsec->reloc_list, list) + for (int __i = 0, __fake = 1; __fake; __fake = 0) \ + for (reloc = rsec->relocs; \ + __i < sec_num_entries(rsec); \ + __i++, reloc++) #define for_each_reloc_from(rsec, reloc) \ - list_for_each_entry_from(reloc, &rsec->reloc_list, list) + for (int __i = reloc->idx; \ + __i < sec_num_entries(rsec); \ + __i++, reloc++) #define OFFSET_STRIDE_BITS 4 #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) From patchwork Tue May 30 17:21:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101035 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340364vqr; Tue, 30 May 2023 10:23:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5JTvI2PtquVO26fPSNcqXgbcITJSRJEjM69udABLj6xJn6QmSZdxdiOcIm6ZniR87hVWzT X-Received: by 2002:a05:6a00:248b:b0:643:b37d:d350 with SMTP id c11-20020a056a00248b00b00643b37dd350mr4295612pfv.31.1685467412541; Tue, 30 May 2023 10:23:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467412; cv=none; d=google.com; s=arc-20160816; b=WNn/dCb4V8jBQCAK5dZV75eFOV9dQDcHMsdEa6PaEUOMlyscD1pWPyF2ukFDlSmxUG d3U9N6n5PGlxMZSB4SVNgEcysF2cGIRtIKjAGc5nrWIErmR/DPBQrwZ1WIf2KJM9Ww/7 lapG8wQ2oTvfzoXSjwo42BOBfs5chLDX+tI+0A3ggYOZX8Ym3WSbcuCf28sSG4eqcFLe Bu5+PoR7t1y6yqHucrUtJzvyHbaiIxHQST0pDvGaVYAmSeOz34d1DSI0+P1zDqTPzcq2 LWBqcA2i0998u9NhQbeee9ASIXaQxvT6ZETlbm+YD5k5+50sfU9tCKkH6fk7J6G6LK42 xLGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=aUL8VkehfjaEN1SnT3egW4133LmmDsEP03jU7LjsOug=; b=ouMgiPkpE40/HUXOi5iCPN34Kg6xv/89E4jVupW/nwI0N2U7wkF5WFZEzXUrwR3lRV rJMtJzi674jUX3wJfnE/VX2aks1YmED7khw4I7RNzb4DEUJRedzvmJ8dd8aL+rmu6hsA 6Wd4NG1CDp0d4TjbbOzJBGMiHYjRb8QHLKls7GTzp7JO2JGdS7PsQe1hNroZA+jbfQTR 24XgZ22CP4Bqr7PXFnEtTPE87ZJMmCA7oawt5dzNsFVUUJLP6/yHGMdpx05IvZGwQliO ANOk9pzBBF6le24DuhSGcUTi/PA6J88IkSz/HcFvU6MfKzlUTL1DmI/GRqA2G+qHTQ1N ZKNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=elaSImFI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y3-20020aa78f23000000b0063d23b80e00si1975606pfr.392.2023.05.30.10.23.18; Tue, 30 May 2023 10:23:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=elaSImFI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231618AbjE3RWR (ORCPT + 99 others); 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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340677527075024?= X-GMAIL-MSGID: =?utf-8?q?1767340677527075024?= 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, struct section *rsec, return NULL; } - reloc->idx = reloc_idx; reloc->sec = rsec; reloc->offset = offset; reloc->type = type; @@ -954,7 +953,6 @@ static int read_relocs(struct elf *elf) return -1; reloc->sec = rsec; - reloc->idx = i; symndx = GELF_R_SYM(reloc->rel.r_info); reloc->sym = sym = find_symbol_by_index(elf, symndx); if (!reloc->sym) { @@ -1237,9 +1235,9 @@ int elf_write_reloc(struct elf *elf, struct reloc *reloc) if (rsec->sh.sh_type == SHT_RELA) { reloc->rela.r_addend = reloc->addend; - ret = gelf_update_rela(rsec->data, reloc->idx, &reloc->rela); + ret = gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); } else { - ret = gelf_update_rel(rsec->data, reloc->idx, &reloc->rel); + ret = gelf_update_rel(rsec->data, reloc_idx(reloc), &reloc->rel); } if (!ret) { diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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; }; @@ -200,6 +199,11 @@ static inline unsigned int sec_num_entries(struct section *sec) return sec->sh.sh_size / sec->sh.sh_entsize; } +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) @@ -219,7 +223,7 @@ static inline unsigned int sec_num_entries(struct section *sec) __i++, reloc++) #define for_each_reloc_from(rsec, reloc) \ - for (int __i = reloc->idx; \ + for (int __i = reloc_idx(reloc); \ __i < sec_num_entries(rsec); \ __i++, reloc++) From patchwork Tue May 30 17:21:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101039 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340838vqr; Tue, 30 May 2023 10:24:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6c4kM1XN0WtX7mHGvaSoC7r2WGMELZkr8+cG6V/2orJFGpjekeM/c3dVjzNASwJ8M4UAU+ X-Received: by 2002:a17:902:6903:b0:1b0:42ac:74ef with SMTP id j3-20020a170902690300b001b042ac74efmr2653739plk.57.1685467461673; Tue, 30 May 2023 10:24:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467461; cv=none; d=google.com; s=arc-20160816; b=ScClfYklhiIthrcwZegxVfSKzKyc1khkYKxjiaEIe7RQ13jDSoR5zAagSHt/ZaXmZN eh+exQcAvxE9pOnXBVBcmULEtswttSHYkTwc0/EZXtqNtizCvgmjfw8opGjWlW6nTvlN dqS9yGJjz2qqcgmwa0/1mSFI4PPaMNMEmBe+nxT7x7kWyxCRbF77e2SKfd2QW2LmtY+S I2q7HczwGIcVl9Nada+7YTrKR3o7rdKNIsl5pMFSnUKynYFdDvhHPISuKbPX9pvPzAh5 RCfycUWgxGNojc4fHErrNCEIVZj6iroAWEkVMPYOi+oLGq39TL7SCwiBbNDvkj/7MXO3 HR8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MJ4CbtqRr/NpUOkKlDnXdT5E+Lcwubi40t/lmYlSkRg=; b=NpiqzkFJm5c4qGlEPA50qhPDIo9AU76GuEjbLlxkqdi1zd66M3vPxPnZ6BnMN6g23o QinUu5X9LUV50pcZfW4oqTO6eS+KcUmPtcGoE+fiV0tfdMcZE5XWzrvmEX7YeuOl4fUo Q2lhv2YeQagyFaz9G0Wr1DZU5E/3suF/QXqgqeFJG4WH3GAx2LGYsADxj9xXAghWBUF6 DhY6EjFb21bKt/IKtn5JsQp1eAzHOsnCU9UakqXD9MtstqLsdH6oWFBtUQQGYtN5aSPY GURsKArA1wHcUja06yRYCh99EUMaqbzuE9bm+RU62/mm9GTkQKVCYA1eG5z9E9OzvGBD kYtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uOp3p6KQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h17-20020a170902f55100b001ab09a66f8dsi6605185plf.106.2023.05.30.10.24.06; Tue, 30 May 2023 10:24:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uOp3p6KQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233424AbjE3RWd (ORCPT + 99 others); Tue, 30 May 2023 13:22:33 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340729529379625?= X-GMAIL-MSGID: =?utf-8?q?1767340729529379625?= 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, const char *symname) if (func->type == STT_SECTION) func = find_symbol_by_offset(reloc->sym->sec, reloc->addend); - idx = (reloc->offset - sym->offset) / sizeof(unsigned long); + idx = (reloc_offset(reloc) - sym->offset) / sizeof(unsigned long); objtool_pv_add(file, idx, func); - off = reloc->offset + 1; + off = 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; /* Make sure the table entries are consecutive: */ - if (prev_offset && reloc->offset != prev_offset + 8) + if (prev_offset && reloc_offset(reloc) != prev_offset + 8) break; /* Detect function pointers from contiguous objects: */ @@ -2022,7 +2022,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn, alt->insn = dest_insn; alt->next = insn->alts; insn->alts = alt; - prev_offset = reloc->offset; + prev_offset = reloc_offset(reloc); } if (!prev_offset) { @@ -4257,8 +4257,8 @@ static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn for (reloc = insn_reloc(file, insn); reloc; reloc = 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))) { /* * static_call_update() references the trampoline, which @@ -4341,7 +4341,7 @@ static int validate_ibt_data_reloc(struct objtool_file *file, return 0; WARN_FUNC("data relocation to !ENDBR: %s", - reloc->sec->base, reloc->offset, + reloc->sec->base, reloc_offset(reloc), offstr(dest->sec, dest->offset)); 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 != rsec) continue; - if (reloc->offset >= offset && reloc->offset < offset + len) { - if (!r || reloc->offset < r->offset) + if (reloc_offset(reloc) >= offset && + reloc_offset(reloc) < offset + len) { + if (!r || reloc_offset(reloc) < reloc_offset(r)) r = reloc; } } @@ -830,11 +831,12 @@ static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, } reloc->sec = rsec; - reloc->offset = offset; reloc->type = type; reloc->sym = sym; reloc->addend = addend; + reloc->rel.r_offset = offset; + if (elf_write_reloc(elf, reloc)) return NULL; @@ -908,7 +910,6 @@ static int read_reloc(struct section *rsec, int i, struct reloc *reloc) return -1; } - reloc->offset = reloc->rel.r_offset; reloc->type = GELF_R_TYPE(reloc->rel.r_info); reloc->addend = rela ? reloc->rela.r_addend : 0; @@ -1230,7 +1231,6 @@ int elf_write_reloc(struct elf *elf, struct reloc *reloc) struct section *rsec = reloc->sec; int ret; - reloc->rel.r_offset = reloc->offset; reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); if (rsec->sh.sh_type == SHT_RELA) { diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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 *reloc) return reloc - reloc->sec->relocs; } +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) @@ -253,7 +257,7 @@ static inline u32 sec_offset_hash(struct section *sec, unsigned long offset) 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)); } #endif /* _OBJTOOL_ELF_H */ From patchwork Tue May 30 17:21:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101040 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2341033vqr; Tue, 30 May 2023 10:24:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7tng4ruZhg4rTuFKZSIb1J0O2uZJe6dgHYy9B8QDW2ZvEbt4iDWphR2YFlnHecwa/1muEw X-Received: by 2002:a05:6a00:3901:b0:64a:f730:154b with SMTP id fh1-20020a056a00390100b0064af730154bmr3946770pfb.5.1685467487223; Tue, 30 May 2023 10:24:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467487; cv=none; d=google.com; s=arc-20160816; b=sFSX0/hbkYqmXV2OOajNgX9QC9FzvOmOJs9Bb6Ik2wnQ+sUyaEgC57I7KO8X/Kv+0y x4TAX0MgSr8RE0DGbKtspGzS0cIICdmmNOBqYNDWIZSllKIfav7TAEZ0klaxVR/DE5In BvZyMaV6VQDvb3vCKnyPnpV9AfCbT5Ezh+p44URGoRWHikA3YWNivVaYUUBjkGlK9DyU reYODtycnJPkpifslMnn3l+5CoqdNFUflo+dK2Bt+HUVecPQ8XlYf1yBLTegcMsHH3tt I+tkW10gZHwIm443onqCBm198wQoEf0h+F6o8lYrpW829qq8i4kPiPZsPMA0x/Py64vt WlFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QjoF4ua+rNpxICzEJKG84D5uQjWkoIQuYbar0M0+i4w=; b=KYAOIO9yiU+GQU2kDk8f9g1/k1y7RXZi7sTSbKONPSAJrKR9GFigdlGVOo/I21wEY+ NkqObywERUtYDFr57C7MOktmqWBSgjasMQvWGwAEy7nXcUTjgSLT96HIC9Z4ynPT8eQ9 5IVxv7HCpPDx0gMAkPcyA+0hpbf2Rqn4Pkaepzt+pGlEqijklE/bU2xcHCDQ/H4tuUmf HRgnerJSWlIjJsbrWsU32VWHoY0RR+en2MBY2yK4zGdXmI6DJ3B7qvPN5nziJAv7m+AE CYzSkhfy83Em5wBgCwqrT3Lu8QYMzL8afTxajLFJbkLMzZgCTGV33VAxNCC7BBpKXgHy wVJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=std+JsFZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j62-20020a638041000000b0053f6910ccc8si1604471pgd.747.2023.05.30.10.24.33; Tue, 30 May 2023 10:24:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=std+JsFZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233429AbjE3RWg (ORCPT + 99 others); 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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340755364436966?= X-GMAIL-MSGID: =?utf-8?q?1767340755364436966?= 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/decode.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/special.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_file *file, table_offset = text_reloc->addend; table_sec = text_reloc->sym->sec; - if (text_reloc->type == R_X86_64_PC32) + if (reloc_type(text_reloc) == R_X86_64_PC32) table_offset += 4; /* @@ -138,7 +138,7 @@ struct reloc *arch_find_switch_table(struct objtool_file *file, * indicates a rare GCC quirk/bug which can leave dead * code behind. */ - if (text_reloc->type == R_X86_64_PC32) + if (reloc_type(text_reloc) == R_X86_64_PC32) file->ignore_unreachables = true; 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_file *file) if (!reloc) return -1; - reloc->type = addr_size == 8 ? R_ABS64 : R_ABS32; + set_reloc_type(reloc, addr_size == 8 ? R_ABS64 : R_ABS32); idx++; } @@ -1353,7 +1353,7 @@ static void annotate_call_site(struct objtool_file *file, */ if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) { if (reloc) { - reloc->type = R_NONE; + set_reloc_type(reloc, R_NONE); elf_write_reloc(file->elf, reloc); } @@ -1382,7 +1382,7 @@ static void annotate_call_site(struct objtool_file *file, WARN_INSN(insn, "tail call to __fentry__ !?!?"); if (opts.mnop) { if (reloc) { - reloc->type = R_NONE; + set_reloc_type(reloc, R_NONE); elf_write_reloc(file->elf, reloc); } @@ -1864,7 +1864,7 @@ static int handle_jump_alt(struct objtool_file *file, struct reloc *reloc = insn_reloc(file, orig_insn); if (reloc) { - reloc->type = 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 *file, struct instruction *insn continue; off = reloc->sym->offset; - if (reloc->type == R_X86_64_PC32 || reloc->type == R_X86_64_PLT32) + if (reloc_type(reloc) == R_X86_64_PC32 || + reloc_type(reloc) == R_X86_64_PLT32) off += arch_dest_reloc_offset(reloc->addend); else off += 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, struct symbol *sym) struct reloc *reloc; list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) { + reloc->rel.r_info = 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, } reloc->sec = rsec; - reloc->type = type; reloc->sym = sym; reloc->addend = addend; reloc->rel.r_offset = offset; + reloc->rel.r_info = GELF_R_INFO(sym->idx, type); if (elf_write_reloc(elf, reloc)) return NULL; @@ -910,7 +911,6 @@ static int read_reloc(struct section *rsec, int i, struct reloc *reloc) return -1; } - reloc->type = GELF_R_TYPE(reloc->rel.r_info); reloc->addend = rela ? reloc->rela.r_addend : 0; return 0; @@ -1231,8 +1231,6 @@ int elf_write_reloc(struct elf *elf, struct reloc *reloc) struct section *rsec = reloc->sec; int ret; - reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); - if (rsec->sh.sh_type == SHT_RELA) { reloc->rela.r_addend = reloc->addend; ret = gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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; }; @@ -208,6 +207,16 @@ static inline unsigned long reloc_offset(struct reloc *reloc) return reloc->rel.r_offset; } +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 = 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) From patchwork Tue May 30 17:21:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101050 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2344541vqr; Tue, 30 May 2023 10:31:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ76fvubgHxNb+W4TCh980hdfpRVkpqLZBt9w9EjBvg77XsBjjLq6PCpuvLJ9vHug0+B/mV9 X-Received: by 2002:a05:6a20:3d0c:b0:102:18a9:cad7 with SMTP id y12-20020a056a203d0c00b0010218a9cad7mr4004233pzi.58.1685467894556; Tue, 30 May 2023 10:31:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467894; cv=none; d=google.com; s=arc-20160816; b=e19J2+LellNxaghtlr+K2hKMPZWqGL7m8Jx13mNL7u+fMfr3vPSL0lRsuIhGl2OeC5 sZqOOPbbFXPRsz8kwSzjUvDBDvV0lFrO3CsJYXn3boja/UPfZb72LsDSknD6kX5ZnB5k PVeJHL5Gy0U7nmnUU0s6RIC/d0lEHPHIp3ifry8k87BSXsC1HkakVtNTPc4Cm8XypW+K GoF0v6mM2+r85LVuNYTYY5eBPee3reP5DA+EojeEDzyuX5iX9uY92d7HAnBCebMVwiBb /126u+f7D1jBWLlH904yyMHkVl8vV7EL+Ebi89L22wrry8W2gcCghK3/ZoEFZRA1hA0s 8fIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SsWNcEjoC0E1Y85a64UtC+KMn5joxt9h9ElyDDlpJ2I=; b=uMvwBknZL1CC6xjuhwEPoBJ2KS9cOMyuLpZ+yqHD2HldCx4ZS8ezeCqxra7ijPTAq+ PKpjEXdpEXtwJaoq3j5Uuz280iIXMAcGgBpa1Ufi1/3H7M9aaRlucHXOvusYkmNNla5l iIJIB1cUhCCxfP+feoFfsqpTwucjnVJnHiVt0kITZwaAKK/YUsCGn1WcFALQwjQhesPE fAhGbhXRcKFPBJKD9LXL68EjvBMYx7f+/EivNOrriphTkP4mE03KGRA+QkWNBxOGmcBj 7C89ns3gGrQdfZzQi44DJws5X0aoQ5WafvXrrfp8ZY/sUvI9NZF2yW03e4wyRZUfNdSk Z8qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kChLyN3R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e12-20020a17090a630c00b002569147d64asi201960pjj.114.2023.05.30.10.31.20; Tue, 30 May 2023 10:31:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kChLyN3R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233438AbjE3RWl (ORCPT + 99 others); Tue, 30 May 2023 13:22:41 -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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341183147793222?= X-GMAIL-MSGID: =?utf-8?q?1767341183147793222?= 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/decode.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; - idx = (immr->addend + 8) / sizeof(void *); + idx = (reloc_addend(immr) + 8) / sizeof(void *); func = disp->sym; if (disp->sym->type == STT_SECTION) - func = find_symbol_by_offset(disp->sym->sec, disp->addend); + func = 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/special.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_file *file, !text_reloc->sym->sec->rodata) return NULL; - table_offset = text_reloc->addend; + table_offset = reloc_addend(text_reloc); table_sec = text_reloc->sym->sec; if (reloc_type(text_reloc) == 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) func = reloc->sym; if (func->type == STT_SECTION) - func = find_symbol_by_offset(reloc->sym->sec, reloc->addend); + func = find_symbol_by_offset(reloc->sym->sec, + reloc_addend(reloc)); idx = (reloc_offset(reloc) - sym->offset) / sizeof(unsigned long); @@ -582,6 +583,7 @@ static int add_dead_ends(struct objtool_file *file) struct section *rsec; struct reloc *reloc; struct instruction *insn; + s64 addend; /* * Check for manually annotated dead ends. @@ -591,23 +593,27 @@ static int add_dead_ends(struct objtool_file *file) goto reachable; for_each_reloc(rsec, reloc) { + if (reloc->sym->type != STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + + addend = reloc_addend(reloc); + + insn = find_insn(file, reloc->sym->sec, addend); if (insn) insn = prev_insn_same_sec(file, insn); - else if (reloc->addend == reloc->sym->sec->sh.sh_size) { + else if (addend == reloc->sym->sec->sh.sh_size) { insn = 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; } @@ -626,23 +632,27 @@ static int add_dead_ends(struct objtool_file *file) return 0; for_each_reloc(rsec, reloc) { + if (reloc->sym->type != STT_SECTION) { WARN("unexpected relocation symbol type in %s", rsec->name); return -1; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + + addend = reloc_addend(reloc); + + insn = find_insn(file, reloc->sym->sec, addend); if (insn) insn = prev_insn_same_sec(file, insn); - else if (reloc->addend == reloc->sym->sec->sh.sh_size) { + else if (addend == reloc->sym->sec->sh.sh_size) { insn = 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; } @@ -1025,7 +1035,7 @@ static void add_ignores(struct objtool_file *file) break; case STT_SECTION: - func = find_func_by_offset(reloc->sym->sec, reloc->addend); + func = 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_file *file) return -1; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + insn = 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 = arch_jump_destination(insn); } else if (reloc->sym->type == STT_SECTION) { dest_sec = reloc->sym->sec; - dest_off = arch_dest_reloc_offset(reloc->addend); + dest_off = 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 = reloc->sym->sec; dest_off = 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) } } else if (reloc->sym->type == STT_SECTION) { - dest_off = arch_dest_reloc_offset(reloc->addend); + dest_off = arch_dest_reloc_offset(reloc_addend(reloc)); dest = 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, /* Detect function pointers from contiguous objects: */ if (reloc->sym->sec == pfunc->sec && - reloc->addend == pfunc->offset) + reloc_addend(reloc) == pfunc->offset) break; - dest_insn = find_insn(file, reloc->sym->sec, reloc->addend); + dest_insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc)); if (!dest_insn) break; @@ -2066,7 +2076,7 @@ static struct reloc *find_jump_table(struct objtool_file *file, table_reloc = arch_find_switch_table(file, insn); if (!table_reloc) continue; - dest_insn = find_insn(file, table_reloc->sym->sec, table_reloc->addend); + dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc)); if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func) continue; @@ -2202,7 +2212,7 @@ static int read_unwind_hints(struct objtool_file *file) return -1; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + insn = 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 *file) return 0; for_each_reloc(rsec, reloc) { - insn = find_insn(file, reloc->sym->sec, reloc->sym->offset + reloc->addend); + insn = 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; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + insn = 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; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + insn = 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; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + insn = 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_file *file) return -1; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + insn = 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_file *file) return -1; } - insn = find_insn(file, reloc->sym->sec, reloc->addend); + insn = 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 instruction *insn) reloc = insn_reloc(NULL, insn); if (reloc && !strcmp(reloc->sym->name, "pv_ops")) { - idx = (reloc->addend / sizeof(void *)); + idx = (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, struct instruction *insn) if (!reloc || strcmp(reloc->sym->name, "pv_ops")) return false; - idx = (arch_dest_reloc_offset(reloc->addend) / sizeof(void *)); + idx = (arch_dest_reloc_offset(reloc_addend(reloc)) / sizeof(void *)); if (file->pv_ops[idx].clean) return true; @@ -4270,9 +4281,9 @@ static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn off = reloc->sym->offset; if (reloc_type(reloc) == R_X86_64_PC32 || reloc_type(reloc) == R_X86_64_PLT32) - off += arch_dest_reloc_offset(reloc->addend); + off += arch_dest_reloc_offset(reloc_addend(reloc)); else - off += reloc->addend; + off += reloc_addend(reloc); dest = find_insn(file, reloc->sym->sec, off); if (!dest) @@ -4329,7 +4340,7 @@ static int validate_ibt_data_reloc(struct objtool_file *file, struct instruction *dest; dest = find_insn(file, reloc->sym->sec, - reloc->sym->offset + reloc->addend); + reloc->sym->offset + reloc_addend(reloc)); if (!dest) return 0; 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, reloc->sec = rsec; reloc->sym = sym; - reloc->addend = addend; reloc->rel.r_offset = offset; reloc->rel.r_info = GELF_R_INFO(sym->idx, type); + reloc->rela.r_addend = addend; if (elf_write_reloc(elf, reloc)) return NULL; @@ -911,8 +911,6 @@ static int read_reloc(struct section *rsec, int i, struct reloc *reloc) return -1; } - reloc->addend = rela ? reloc->rela.r_addend : 0; - return 0; } @@ -1231,12 +1229,10 @@ int elf_write_reloc(struct elf *elf, struct reloc *reloc) struct section *rsec = reloc->sec; int ret; - if (rsec->sh.sh_type == SHT_RELA) { - reloc->rela.r_addend = reloc->addend; + if (rsec->sh.sh_type == SHT_RELA) ret = gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); - } else { + else ret = gelf_update_rel(rsec->data, reloc_idx(reloc), &reloc->rel); - } if (!ret) { WARN_ELF("gelf_update_rela"); diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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; }; @@ -217,6 +216,11 @@ static inline void set_reloc_type(struct reloc *reloc, int type) reloc->rel.r_info = GELF_R_INFO(GELF_R_SYM(reloc->rel.r_info), type); } +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) 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 = reloc->sym->sec; - *off = reloc->sym->offset + reloc->addend; + *off = reloc->sym->offset + reloc_addend(reloc); } static int get_alt_entry(struct elf *elf, const struct special_entry *entry, @@ -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 = key_reloc->addend; + alt->key_addend = reloc_addend(key_reloc); } return 0; From patchwork Tue May 30 17:21:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101041 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2341818vqr; Tue, 30 May 2023 10:26:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5hUF/QDzK0sscKsC6mBvXXyw13x02p8f2oQGiQ1cdTdYPIlgpUot3JiJBA6rqtE37/hA7P X-Received: by 2002:a17:90a:e602:b0:23f:81c0:eadd with SMTP id j2-20020a17090ae60200b0023f81c0eaddmr3001199pjy.47.1685467577318; Tue, 30 May 2023 10:26:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467577; cv=none; d=google.com; s=arc-20160816; b=SM7Lz/npKmL5cQt9HgdiCWdEVdL4OBQFb96V/uoIGDLEfbFCI4OhM55T0Ta39DT0u7 3Noq0UIyBCRrMcREycY7FJ/aEpgSr2jbAH4qLlTWFu8kGTAPbrxEhWYI8YoU1Z35Q4La dD3rU3/PJNdWyjugec7aGz6S+VfF0BYIemPklDeE+VSVChkxm4HwVU73ChJpe90OlZbI 1XHSnfhCYJqvyCHmiaU8hZVb6Uy2KcmyojYldURAnsRCTvWWm7KzVvWtwDhVAwn5m1MU +mdreXCgJg2L1I96M8euctQqv3Mwfzo0PvnX99/PFe6RxZK6yXuZPqqeuYfDSnWlNjtb 8z6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7g55zyK5B7gX+9R4n0TzKnmmAWJQa5O83b4lRfwVVSY=; b=u7oLRJTIWk+GETK8rZScENHazhYQLPWVrvIaySpC+TaAvLmVaWuW1qDq6TxYqsVIxm XmkJJ4ahXOcerVuy82YKbTO8Ek9WOD0RoY5U67qJz6Wo59EZfwz6GWUVy5v8eM/cEi9f wGqyYNHFdMQ+ZOiYWc/3imurtW096Jg0NQNaGfN3xIijwd2ac+Hiygs80pd2O0W+w/OX bftBliwh/MCB5P/HshYMfizzmutXznIblJ6Uti5lnUUEtldiACQA/2XzCrGZj5DCTVl5 To4IZeDG95sgOWkNjwXEzWU6ACGQfJIJETpeJ2Yvx5qOc590nBHhlEfXzWNpW88j+uUn xZkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=k+M69oU7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id np12-20020a17090b4c4c00b002532e1993basi2803671pjb.154.2023.05.30.10.26.00; Tue, 30 May 2023 10:26:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=k+M69oU7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233450AbjE3RWq (ORCPT + 99 others); Tue, 30 May 2023 13:22:46 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340849909618738?= X-GMAIL-MSGID: =?utf-8?q?1767340849909618738?= 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) } static int add_jump_table(struct objtool_file *file, struct instruction *insn, - struct reloc *table) + struct reloc *next_table) { - struct reloc *reloc = table; - struct instruction *dest_insn; - struct alternative *alt; struct symbol *pfunc = insn_func(insn)->pfunc; + struct reloc *table = insn_jump_table(insn); + struct instruction *dest_insn; unsigned int prev_offset = 0; + struct reloc *reloc = table; + struct alternative *alt; /* * 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) { /* Check for the end of the table: */ - if (reloc != table && reloc->jump_table_start) + if (reloc != table && reloc == next_table) break; /* Make sure the table entries are consecutive: */ @@ -2118,29 +2119,39 @@ static void mark_func_jump_tables(struct objtool_file *file, continue; reloc = find_jump_table(file, func, insn); - if (reloc) { - reloc->jump_table_start = true; + if (reloc) insn->_jump_table = reloc; - } } } static int add_func_jump_tables(struct objtool_file *file, struct symbol *func) { - struct instruction *insn; - int ret; + struct instruction *insn, *insn_t1 = NULL, *insn_t2; + int ret = 0; func_for_each_insn(file, func, insn) { if (!insn_jump_table(insn)) continue; - ret = add_jump_table(file, insn, insn_jump_table(insn)); + if (!insn_t1) { + insn_t1 = insn; + continue; + } + + insn_t2 = insn; + + ret = add_jump_table(file, insn_t1, insn_jump_table(insn_t2)); if (ret) return ret; + + insn_t1 = insn_t2; } - return 0; + if (insn_t1) + ret = add_jump_table(file, insn_t1, NULL); + + return ret; } /* diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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; }; struct elf { From patchwork Tue May 30 17:21:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101051 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2344775vqr; Tue, 30 May 2023 10:31:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ59U0iWLzR3bo8USgm5QONn1yLb1JFKhG1fZVvoSix3my/GKk5rdgaGvE/fbmwkykxEf71X X-Received: by 2002:a05:6a20:3c90:b0:10d:3ff2:452b with SMTP id b16-20020a056a203c9000b0010d3ff2452bmr3759667pzj.18.1685467917399; Tue, 30 May 2023 10:31:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467917; cv=none; d=google.com; s=arc-20160816; b=ULbRrKHDEx3JF4PMCTMJS8FUC5RkwUdQJB0yQZ5DZXChzPAME7StqOmGdrnouKdskr a/O9zcruwu2PfrM3IEWtYLr6+o4puFNTLO/EmoZY9YQQ21lYaZncNN7YI2ojovXuhCfj 0uziUzbMO9n547HmpNCwwOJY/7j9RdYEkvO4op+GAjIqDgXFYi47hsw3JFWCqdG6cqJ+ 9MZWPvVx8bqZJ+XE/ncJND/eYCF3zVOeV7rAJlvk+65+gunuf0qpWIu2HDdtOQL0khBh d1nXw75d4xOHCljJSNOqA54kYnmWS8c0okEFAdLcOmVkUYAwLVZ6LGgwbJEgfvWL0dR/ uQeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tYh8HJKBbj/U8nyT+X7ZZk+rRUQ3VVVzX9ePAEyGzA4=; b=oLv9nXa3BlzsbJpsPaiZ8B3ImNGBDKvrROBxK3Fxtx764NOTP1y8CsvqoaihkKK0i0 29Ofnr5TrfMg6Q8o8GwMqrRa06ekkoOzkqHq2dGRiH24J9+VSwuP2SBZTYVeb3pPL+Y5 l9Tp9KjxKNGUZOGh3gTTaV9rBMdUNP4C+2Y0NZWnlP4TEifBYjnjHyWW+PKyKBQgg0rm o+RBty9TZMPT37aSbxwi+N/vU6Yp1Kc1GzkTxZf6o01fNaiXZUAHML2IAAEitwlxTAHk +RlaThsoA+2VTfWgOvoLYiqvQe9BORXK4MPrIj3MIFcxtdPRnHIWUieadERoDg5k3PFF vbmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EtNkEs+D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z22-20020a63b916000000b0051b37c000dcsi5263572pge.81.2023.05.30.10.31.42; Tue, 30 May 2023 10:31:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EtNkEs+D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233434AbjE3RWi (ORCPT + 99 others); Tue, 30 May 2023 13:22:38 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341207189036176?= X-GMAIL-MSGID: =?utf-8?q?1767341207189036176?= 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 symbol *sym) struct rb_node *pnode; struct symbol *iter; - INIT_LIST_HEAD(&sym->reloc_list); INIT_LIST_HEAD(&sym->pv_target); sym->alias = sym; @@ -540,7 +539,7 @@ static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym) { struct reloc *reloc; - list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) { + for (reloc = sym->relocs; reloc; reloc = reloc->sym_next_reloc) { reloc->rel.r_info = 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, struct section *rsec, if (elf_write_reloc(elf, reloc)) return NULL; - list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); + reloc->sym_next_reloc = sym->relocs; + sym->relocs = reloc; return reloc; } @@ -960,8 +960,9 @@ static int read_relocs(struct elf *elf) return -1; } - list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); + reloc->sym_next_reloc = sym->relocs; + sym->relocs = reloc; nr_reloc++; } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/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; }; 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; }; struct elf { From patchwork Tue May 30 17:21:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101045 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2344349vqr; Tue, 30 May 2023 10:31:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5JjSWsU8nVo4noLaHpZL0Ch1B+sj5S/Nwmuh2KPwCJMXs9av0KwJwe4+Lrz95amkH7iwhV X-Received: by 2002:a17:902:bf4c:b0:1af:e8cf:7004 with SMTP id u12-20020a170902bf4c00b001afe8cf7004mr2687200pls.15.1685467876141; Tue, 30 May 2023 10:31:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467876; cv=none; d=google.com; s=arc-20160816; b=mJn82C4sWFTH6HVMgkaxGZdsvHIcrpGXiLQO52SvNwGe7tpb3xHj0q8I0VdffLGdHL WM5UJdDD6snzxffevTWm7tilRYhkMoWjeU6n1c/2YPwz8NNOXMlniWX/BUwYgKQGnbt/ oKt1Qu6LxY/YNgAnGLO50BtO2yJqyIXRMjVMDkwj1Zlhpimyjy7bXVznoaj1zeLITquC /kAn4WpnNDKzf/lgZdtYWBNPc5p7TUHMmpBEGjFQ1CjsO96n2mAV7JvpLLrzpAr0vsW9 11XjIeQLaWI/wIKHRLBVawN+oL+Bh5MU8c/XREAM29BB69ONUzK+Y7Cwl9OUjOQtStxM bP5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=yYi5sYZSzdLscBH2ECNAvYFTmtSOMXKu0pWpCznxKd8=; b=LOtv4ajT7A6cBJtewn2fdiuELWcYBTq1QXGGr2vRGuESlyFfU3nrDvjUp/rPQ4R+3V tJN8Ug6j4QtdPDT+37S2OPz5kTgWpkIOM+EU9vzE5vUij5wZciJftSQynKS75/svxxST J+41eWnHeaD8DqXgBqZKwk+fvjXvDd5Bdl9pFisxG5lf+XwtQjiWE7muVzLkfe1+S+cH nALDyFd8cicFdRZeIAH7Q+ic5Dfvo39pp14WOLMYDi2LVkZ8hCPzHDGsoAzwCurL1yeb kTeaCsNeIv8Zprcs78Q14VKcZUJ4ClNP5TIpFFp6isbuDVi71z2pIrWh30xI+rrWxXwx s29w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q96xRZ8G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n10-20020a170902d2ca00b001b06652531fsi1222031plc.486.2023.05.30.10.31.01; Tue, 30 May 2023 10:31:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q96xRZ8G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233453AbjE3RWu (ORCPT + 99 others); Tue, 30 May 2023 13:22:50 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341163665917242?= X-GMAIL-MSGID: =?utf-8?q?1767341163665917242?= 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) -#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))] -#define elf_hash_for_each_possible(name, obj, member, key) \ - hlist_for_each_entry(obj, &__elf_table(name)[hash_min(key, __elf_bits(name))], member) +#define elf_hash_add(name, node, key) \ +({ \ + struct elf_hash_node *__node = node; \ + __node->next = __elf_table_entry(name, key); \ + __elf_table_entry(name, key) = __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 == *head) { + *head = node->next; + return; + } + + for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) { + if (cur == node) { + prev->next = 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 = (ptr); \ + __ptr ? container_of(__ptr, type, member) : NULL; \ +}) + +#define elf_hash_for_each_possible(name, obj, member, key) \ + for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \ + obj; \ + obj = elf_list_entry(obj->member.next, typeof(*(obj)), member)) #define elf_alloc_hash(name, size) \ ({ \ __elf_bits(name) = max(10, ilog2(size)); \ - __elf_table(name) = mmap(NULL, sizeof(struct hlist_head) << __elf_bits(name), \ + __elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \ PROT_READ|PROT_WRITE, \ MAP_PRIVATE|MAP_ANON, -1, 0); \ if (__elf_table(name) == (void *)-1L) { \ @@ -713,10 +749,10 @@ __elf_create_symbol(struct elf *elf, struct symbol *sym) first_non_local = symtab->sh.sh_info; old = find_symbol_by_index(elf, first_non_local); if (old) { - old->idx = new_idx; - 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 = new_idx; 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/objtool/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 +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 { }; 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; - 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; struct section *section_data; struct symbol *symbol_data; From patchwork Tue May 30 17:21:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101043 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2343705vqr; Tue, 30 May 2023 10:30:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4QPs9eAh3mptrVxwwDnewHxb8TALWt908hqFMOGM81SZNwZ9pdgm+GSMUbFhqpOVfCLPUx X-Received: by 2002:a05:6a00:c8e:b0:64d:1185:2411 with SMTP id a14-20020a056a000c8e00b0064d11852411mr3516179pfv.18.1685467815076; Tue, 30 May 2023 10:30:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467815; cv=none; d=google.com; s=arc-20160816; b=keXigCOIIuf7B4UfbXR0IDtOfyyFham/W1Y06ugylWFZCJMOCxB5cmK9Ew2L+c5+Fa kdibzxvGMHmAEa2MaFWxMnAzJ5FcRjM+fXYZUyOMnPG5tNlQpeUptWmnmAfCjKJnwvvP 4f98W/KfjRl1lWbHomO3mz8t+pQERukeSmQYm6JRxUXVkj5nXUMZq6jz/gOzeQj2ZdF3 EDsnQ72nJHnKYz4+yWY8rzBOaVIO1jFYnN690zE1wNkst8bMawfZo75Uw5f2G5SAAJ1h DOh6jUZ1UmyuEXSTLfFVOYfIjD3e0Uq1yX5Nms8zoV7Fi+q1UKtq1r1+rF9MLdTbLKEP Y+hQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H3U9hvSuVatyh/EXfUdC/eG+JDlmd6WTyTDg1zZXN94=; b=E9sEzK8P/YPUlHWyD+sZOcpDccBKrPaZ7IU7Q4k0UDzFT+BUCZVYTLU7McVVlA+XxP Fy6qQ+HQP6OaxnEKxEbfaPnIbzbEgE4uppHazLZr7nATd5niwiWahDHaxeZZ7JK5wW0w ilX6Dsyc8hyx3y3JhRVCCbkw2tq14DB6a+IgAuQdkltZJSIC8OA9SbMWK+VY7L1zSUaQ RclrYgeGEXofEPyf4zEiYZtqFSXbH4roY2oq+1c6w4DJ0rH4tdfrqu8b0d9yTu5749lc kVHVtKQum+HLOJ8BX3+HVciDrFNWiQI2xZ0tQZCYocP2tD+SnEP5bT4MYkZMe8uq3uu6 4rmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jzxmsBR5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 3-20020a620503000000b006367f3bb8a9si1982018pff.249.2023.05.30.10.29.36; Tue, 30 May 2023 10:30:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jzxmsBR5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233467AbjE3RW5 (ORCPT + 99 others); Tue, 30 May 2023 13:22:57 -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 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341099961865235?= X-GMAIL-MSGID: =?utf-8?q?1767341099961865235?= 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_file *file) if (!reloc) return -1; - set_reloc_type(reloc, addr_size == 8 ? R_ABS64 : R_ABS32); + set_reloc_type(file->elf, reloc, addr_size == 8 ? R_ABS64 : R_ABS32); 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); 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); 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 = insn_reloc(file, orig_insn); - 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, struct symbol *sym) { struct reloc *reloc; - for (reloc = sym->relocs; reloc; reloc = reloc->sym_next_reloc) { - reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc_type(reloc)); - if (elf_write_reloc(elf, reloc)) - return -1; - } + for (reloc = sym->relocs; reloc; reloc = reloc->sym_next_reloc) + set_reloc_sym(elf, reloc, reloc->sym->idx); return 0; } @@ -869,12 +866,10 @@ static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, reloc->sec = rsec; reloc->sym = sym; - reloc->rel.r_offset = offset; - reloc->rel.r_info = GELF_R_INFO(sym->idx, type); - reloc->rela.r_addend = 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); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); reloc->sym_next_reloc = sym->relocs; @@ -932,24 +927,6 @@ struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec, elf_data_rela_type(elf)); } -static int read_reloc(struct section *rsec, int i, struct reloc *reloc) -{ - bool rela = rsec->sh.sh_type == SHT_RELA; - void *retp; - - if (rela) - retp = gelf_getrela(rsec->data, i, &reloc->rela); - else - retp = 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 = 0; @@ -984,11 +961,8 @@ static int read_relocs(struct elf *elf) for (i = 0; i < sec_num_entries(rsec); i++) { reloc = &rsec->relocs[i]; - if (read_reloc(rsec, i, reloc)) - return -1; - reloc->sec = rsec; - symndx = GELF_R_SYM(reloc->rel.r_info); + symndx = reloc_sym(reloc); reloc->sym = sym = 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; } -int elf_write_reloc(struct elf *elf, struct reloc *reloc) -{ - struct section *rsec = reloc->sec; - int ret; - - if (rsec->sh.sh_type == SHT_RELA) - ret = gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); - else - ret = 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/objtool/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 { 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); @@ -204,24 +199,99 @@ static inline unsigned int reloc_idx(struct reloc *reloc) return reloc - reloc->sec->relocs; } -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 = reloc->sec; + + return rsec->data->d_buf + (reloc_idx(reloc) * rsec->sh.sh_entsize); } -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; } -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 = val; \ + else \ + ((Elf64_Rela *)reloc_rel(reloc))->field = val; \ +}) + +static inline u64 reloc_offset(struct reloc *reloc) { - reloc->rel.r_info = 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); } 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 = __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 = __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, unsigned int sym) +{ + u64 info = 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, unsigned int type) +{ + u64 info = 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); } #define for_each_sec(file, sec) \ From patchwork Tue May 30 17:21:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101038 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2340580vqr; Tue, 30 May 2023 10:23:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5hjVRGLc4PFfg86lesPNMR6kjEslNyijh/wIK7DNGqcOmbH4DToOQ+anuKJr1dRJ2ZjRDc X-Received: by 2002:a05:6a20:12d3:b0:10b:7400:ceea with SMTP id v19-20020a056a2012d300b0010b7400ceeamr3682736pzg.61.1685467437364; Tue, 30 May 2023 10:23:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467437; cv=none; d=google.com; s=arc-20160816; b=QqZaflm1Ox0QUww9itbv3wMDM6/UXdFKEwD9ZEdaVFN8og9EB4HKVk3Lq1emDQswH5 2EK7FpDgjufRN5MYDn/Czl4ZP4vqQ4DFo3wpg22w1nJSLpImiOjmwuzRc+LfMSknHdTv uBh0tiaiaSIRDGq6sHPzmh7+x/E7i6w4+z8KCUBHjBjdOT6qzrpmaec4vwi29PxZ794Q PVcXn6i4OMZ1Lnpf6w/LIRrt3xpFbGPYVFTeITWlnOc3ncbxucf9rfFYgE+qUZvz/xZQ 8n+AogfT107e9Orip6tD1PV3ZWXGjfQfNR5WUCq3ogjxzDN79Nozgm1QhNXU1Sr/oWRI cj8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=y2tnD4yzECIEkZuYzR398SuaiitWFfKbB8rhpof02jI=; b=Qcgv3iQYKWalqUd11mo/TDc0oXH9J5TtaNxScpZyPzFm4Yqg9h0svRANoOiG5rQeDL pns62QNwoYlOIwHYmBgBMd8kPhyXbEbr/rJsPUCdC/gy7qOc5DhllS95+m5x2RnpC9gp utP/E/InbfNFSbFD7KJRhWreQ0mjAzrYLSXh2FZoqzOEYjqxy3bQEo/NvcRddK+MaB7O Ki0v1590tv5UlbmpglDInbpxkmeuDCbnXvNYICNXqZDdzseFvEA9vBObxo1TLmm9z63X p/HVBQudGOrHhTeVwZ42/tlGLGEjq2f8XdDI5hhrpPhRNIAVrBEiHhf7qciaMdkucTES 8jWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rbpRFfjJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l184-20020a6388c1000000b0053fb39d8e81si1731239pgd.584.2023.05.30.10.23.43; Tue, 30 May 2023 10:23:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rbpRFfjJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233405AbjE3RW2 (ORCPT + 99 others); Tue, 30 May 2023 13:22:28 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767340703563670257?= X-GMAIL-MSGID: =?utf-8?q?1767340703563670257?= 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 *file) return 0; } +struct insn_chunk { + void *addr; + struct insn_chunk *next; +}; + +/* + * Reduce peak RSS usage by freeing insns memory before writing the ELF file, + * which can trigger more allocations for .debug_* sections whose data hasn't + * been read yet. + */ +static void free_insns(struct objtool_file *file) +{ + struct instruction *insn; + struct insn_chunk *chunks = NULL, *chunk; + + for_each_insn(file, insn) { + if (!insn->idx) { + chunk = malloc(sizeof(*chunk)); + chunk->addr = insn; + chunk->next = chunks; + chunks = chunk; + } + } + + for (chunk = chunks; chunk; chunk = chunk->next) + free(chunk->addr); +} + int check(struct objtool_file *file) { int ret, warnings = 0; @@ -4731,6 +4759,8 @@ int check(struct objtool_file *file) warnings += ret; } + free_insns(file); + if (opts.verbose) disas_warned_funcs(file); From patchwork Tue May 30 17:21:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 101042 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2343704vqr; Tue, 30 May 2023 10:30:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5eEif9n5AxJseoThKCQ29Pvw7c9eyyRw1KYunOLmOpkP9t2l4E0oRUU4IUF2xSSMqzyOq+ X-Received: by 2002:a17:902:8684:b0:1af:c6d2:287 with SMTP id g4-20020a170902868400b001afc6d20287mr2958148plo.53.1685467815097; Tue, 30 May 2023 10:30:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685467815; cv=none; d=google.com; s=arc-20160816; b=r2ZM+n0Yu/qACav4jhXuiVj4hV12KPqJKc5KcKU6xMurKRffeUxoKQsJnAyHGiNre6 d5eVXy6acVVdLHTwZedFmMaZmdLm6Duv89q0qZxgMoTs2ogmRZC0a6uVDV5SsPu66LQv 4mb5BUShPNW7NcPXk+u6lz4LUF+ZZ0AcSimvpFF3Q0ALDfigY8OzvNvd4LlrNbSQl4qj nGe2WCxbvgE3fPLZeePWdUKJIOewj2CzrMLcX5w0MCO4BDGhTcFBDcxYjtUkLC7PQoP/ VUCj7ZX+GIb8geQtfa6oIcPDbslFsHYYLg+PE+BV1pyLJCBNiwc6hsLcmCtZU2th49sv hqWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IUgouatdz6Tjrk+/WJxNUTUqMB7kQz9T8hqV6LHVyoA=; b=B+0RgbKn5JjSqVqlYb+xiltMHkoBPcvm260saFNMyqEJG1fbzUWLUYBtjUJLVbrD/X QmAnsfx4B9qWLLbT0FhSMGniw8ZDbptPklwfn8kyjp2hKsu+40rDC8Ku2OEllQ1v1Wpm M7ANSwaxXnee23iwDcL1sMLZb5oBljDaioUTec2Clcp7nGGT/7unLstsVtUkHQJ6g4Xa wKQuc+0cRGCeUDmWkS4BuD6LJ9k7mKOkqShU9HxvnfaEUe/eNHttiYRUl7mpSsZfh7gt Rjyl91CylMp4UO0h7zWo3L8LxOAtAxGq23RInOOUZbDF7+DtBdx4ghwH5U2ZOYk5NOBQ VR2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tmRnWW51; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l17-20020a170902f69100b001ab286e740bsi735834plg.328.2023.05.30.10.29.38; Tue, 30 May 2023 10:30:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tmRnWW51; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233463AbjE3RWx (ORCPT + 99 others); Tue, 30 May 2023 13:22:53 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767341099521327022?= X-GMAIL-MSGID: =?utf-8?q?1767341099521327022?= 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); } +static bool is_dwarf_section(struct section *sec) +{ + return !strncmp(sec->name, ".debug_", 7); +} + static int read_sections(struct elf *elf) { Elf_Scn *s = NULL; @@ -350,7 +355,7 @@ static int read_sections(struct elf *elf) return -1; } - if (sec->sh.sh_size != 0) { + if (sec->sh.sh_size != 0 && !is_dwarf_section(sec)) { sec->data = elf_getdata(s, NULL); if (!sec->data) { WARN_ELF("elf_getdata");