From patchwork Sun May 21 16:04:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 96892 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp977292vqo; Sun, 21 May 2023 10:14:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7wp8o0s1WIdO1ziDO+XJaKG3jlD9Jt0iQVchzaenDYj+NNB+0YgXmu1yfvloFnrcLRnAa2 X-Received: by 2002:a17:903:246:b0:1a9:8d57:6d6c with SMTP id j6-20020a170903024600b001a98d576d6cmr8744303plh.24.1684689259535; Sun, 21 May 2023 10:14:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684689259; cv=none; d=google.com; s=arc-20160816; b=ae/SO9hn8n7/8gtr6hr2bDG9WqvkpMaM18g52t9m6mZk/ek//50D8/3DjDK4/ZGW5U 35LKYaBI2ocBLO2tymYZ0TiPyS+l9ycI6m6dWW9U9rg4r4qQ99epiHB8ujtliRW89Hj5 zwGcIEYoJOcf2F0EY4xIis+C5ngFaJZv3owdDpBgIUOaSUw4qsOJ0osIrbWzsWRXQSKb vSPdJYLcb3FMC7Jiyp3/dmfWcTx4ywynIN9KdK5s6kuHkaYfqDjcLazWSaujB2nuD/ID neTOurtB3nRNf/tRWhnt++tdbUMGgfhiL+w0lPV1tLKNIKOUyj86dj18vdsQeo1wgxQk YEag== 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=ALBNrlMG75y99LtO+JU61VVQ0PojHNEqHQ1ekOcmxZQ=; b=Ue0ZAuyi/M5jnDJILvk4s3uzwZLe1IYbClJZIQAF0YyMsziN7ZrgL0ewD6g6y3R3t0 nv8Pm4UuxzZuk2FqNkByTLYTobbv9qyrGWs1cWXpmVTwKI1bw9Kk5YnSt5A8CXZtLgs5 qVXG8UIVBgBlt+Cx4SLCNX7i73GamPDPPS0LlXP+YF+HdWSriPPYUZxKwFwFipdCgeXU JU8WlyK1nMu9Fo8s72DdfAepdE0SKRzlmzFb+E4EPij8WMuLqLL1JkMfernwAnFBUOuW CI444QmG4iT2MW5NN5nDlV/kM7RI2cbGetYyhUabxmjjPN9M6f+06FNxuKkBywZWaJsD wqNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VtSxQ55x; 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 l20-20020a17090ac59400b002508889985fsi5387702pjt.95.2023.05.21.10.13.56; Sun, 21 May 2023 10:14:19 -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=VtSxQ55x; 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 S230186AbjEUQFe (ORCPT + 99 others); Sun, 21 May 2023 12:05:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230179AbjEUQFZ (ORCPT ); Sun, 21 May 2023 12:05:25 -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 31E12DB; Sun, 21 May 2023 09:05:24 -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 9E53860ECB; Sun, 21 May 2023 16:05:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4B1FC433EF; Sun, 21 May 2023 16:05:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684685123; bh=8ebBj1H5+fQEZ8XTbWPo2Yu/XZ2oEgvvBLIxSqvFjiI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VtSxQ55xiiPeLMuns3WBbXD0ipzJ4c8DgUzXuGqJ5e2EZLMif+X/aydsuyrpKr09c Yvdl0Os3Sl9meD3RS5OCV9xIAf80M8/z2c5fiq3Xz7oeZHpyNIYiO+H05DOIojwnWQ C7o8AjrUlqfw8xzFl9P9ZtXC3rJa53r8j8a0i3rXFulX9wCDZ1ylgZTdLmQrCnTDkN dueDM0qd5oKmz8N/XqqsChCQ3yNWGjUHuThWE0e+YOZZFpj9gCZCj/YGlr6J1j+5fz lwug6aJ9QZ7RZp23X1LyegOqL/GNh2JWd3hhGbp/Ou3VoVbN1hW1uCtI74tqZSlZcE JCJEMQOqCaGrQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Nathan Chancellor , Nick Desaulniers , Nicolas Schier , Masahiro Yamada Subject: [PATCH v6 05/20] modpost: refactor find_fromsym() and find_tosym() Date: Mon, 22 May 2023 01:04:10 +0900 Message-Id: <20230521160426.1881124-6-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230521160426.1881124-1-masahiroy@kernel.org> References: <20230521160426.1881124-1-masahiroy@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 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?1766524724934722801?= X-GMAIL-MSGID: =?utf-8?q?1766524724934722801?= find_fromsym() and find_tosym() are similar - both of them iterate in the .symtab section and return the nearest symbol. The difference between them is that find_tosym() allows a negative distance, but the distance must be less than 20. Factor out the common part into find_nearest_sym(). Signed-off-by: Masahiro Yamada Reviewed-by: Nick Desaulniers --- Changes in v6: - Revive the check for distance less than 20 scripts/mod/modpost.c | 95 ++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 59 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index d2329ac32177..6ac0d571542c 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1104,81 +1104,58 @@ static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) return !is_mapping_symbol(name); } -/** - * Find symbol based on relocation record info. - * In some cases the symbol supplied is a valid symbol so - * return refsym. If is_valid_name() == true, we assume this is a valid symbol. - * In other cases the symbol needs to be looked up in the symbol table - * based on section and address. - * **/ -static Elf_Sym *find_tosym(struct elf_info *elf, Elf64_Sword addr, - Elf_Sym *relsym) +/* Look up the nearest symbol based on the section and the address */ +static Elf_Sym *find_nearest_sym(struct elf_info *elf, Elf_Addr addr, + unsigned int secndx, bool allow_negative, + Elf_Addr min_distance) { Elf_Sym *sym; Elf_Sym *near = NULL; - Elf64_Sword distance = 20; - Elf64_Sword d; - unsigned int relsym_secindex; - - if (is_valid_name(elf, relsym)) - return relsym; - - /* - * Strive to find a better symbol name, but the resulting name does not - * always match the symbol referenced in the original code. - */ - relsym_secindex = get_secindex(elf, relsym); - for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { - if (get_secindex(elf, sym) != relsym_secindex) - continue; - if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) - continue; - if (!is_valid_name(elf, sym)) - continue; - if (sym->st_value == addr) - return sym; - /* Find a symbol nearby - addr are maybe negative */ - d = sym->st_value - addr; - if (d < 0) - d = addr - sym->st_value; - if (d < distance) { - distance = d; - near = sym; - } - } - /* We need a close match */ - if (distance < 20) - return near; - else - return NULL; -} - -/* - * Find symbols before or equal addr and after addr - in the section sec. - * If we find two symbols with equal offset prefer one with a valid name. - * The ELF format may have a better way to detect what type of symbol - * it is, but this works for now. - **/ -static Elf_Sym *find_fromsym(struct elf_info *elf, Elf_Addr addr, - unsigned int secndx) -{ - Elf_Sym *sym; - Elf_Sym *near = NULL; - Elf_Addr distance = ~0; + Elf_Addr distance; for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { if (get_secindex(elf, sym) != secndx) continue; if (!is_valid_name(elf, sym)) continue; - if (sym->st_value <= addr && addr - sym->st_value <= distance) { + + if (addr >= sym->st_value) distance = addr - sym->st_value; + else if (allow_negative) + distance = sym->st_value - addr; + else + continue; + + if (distance <= min_distance) { + min_distance = distance; near = sym; } + + if (min_distance == 0) + break; } return near; } +static Elf_Sym *find_fromsym(struct elf_info *elf, Elf_Addr addr, + unsigned int secndx) +{ + return find_nearest_sym(elf, addr, secndx, false, ~0); +} + +static Elf_Sym *find_tosym(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym) +{ + /* If the supplied symbol has a valid name, return it */ + if (is_valid_name(elf, sym)) + return sym; + + /* + * Strive to find a better symbol name, but the resulting name does not + * always match the symbol referenced in the original code. + */ + return find_nearest_sym(elf, addr, get_secindex(elf, sym), true, 20); +} + static bool is_executable_section(struct elf_info *elf, unsigned int secndx) { if (secndx > elf->num_sections)