From patchwork Wed Nov 1 15:04:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 160571 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:abcd:0:b0:403:3b70:6f57 with SMTP id f13csp486985vqx; Wed, 1 Nov 2023 08:06:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH2nsF5gE6pyPjpaxeIy7l9gEsj/CQvt9lamJYgGm+yQQT1k6kArffMV+pWpE7ZJL6jHHl+ X-Received: by 2002:a9d:7499:0:b0:6b9:1917:b2f3 with SMTP id t25-20020a9d7499000000b006b91917b2f3mr16071021otk.33.1698851196554; Wed, 01 Nov 2023 08:06:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698851196; cv=none; d=google.com; s=arc-20160816; b=OY9T0iX3wM6jtSWrfH7/eQp3OhKlVT1I9CKqBchuwgtjTcnw/uTSwZZWwVpkJLP6sV 05mhCvFNaNnl88pj8Etnj/CAySQUBlb9OwDP3PM01jXz2n51y1OjHdIpAIBz/cLzE8GF fMcWkmLvFS5Ef77Bhba+HFDVQ8EMvE0z/PBRDeWQoT7KhZqjsQ79xD2zMaXj2JYG0qM8 qMDPrw2KV7R+XdWEmD2G2Zep8GXPqgJ36M+l0PFnWDb/9PJdwn2SRBfSAn5KheXHd5+J D0M0txgoZio+RqV82V2hLSYjegTHzJe6lMtF/4LlkmPyq64Sk/smgnS2iCYPEqxGiotV Ad+w== 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=PoUIQR7NcgEDdx3RkciaRdO2BQ/sSWxH+8T8qqg3VuI=; fh=dBMhXJFA9J4pDfXGrZbkhy+Gp1pVbk3H3l2Zx64V9wM=; b=LwGFsBU54nZcG0+Ora408D+hblmcZn7OOihGMHVpmRyLcssSMBggkqiz8xZMlgsl2/ xB9YKwJBgW5XEFAhccXLpt8dBtsa+amv4oCjqj5cf2YhSigGhH3RlbZOcJjyaqMcLgnX 3V6v+RGeam4l3Kn56l8CZ+Ou1R/L7pVs+0zU5THD718Fo5O4ZPzt8LHc92vMLAO1Jp1c mIxKMACi9vYNu8yIN/b6f+PeSMtRXzu+Uo3cintJNysrcUFkQhMucP6p6hvnCy0mw/z2 cIIPjlo3Z5unz3c4geKahNIwsc19oGDdNtbSnLot0qfgb8AUDER3Y7hKvLc1uLcxhwnF LXow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="mForM/El"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id g1-20020a056830308100b006d3162b9a41si546146ots.32.2023.11.01.08.06.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 08:06:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="mForM/El"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id A3B1C80327DA; Wed, 1 Nov 2023 08:06:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233074AbjKAPFQ (ORCPT + 35 others); Wed, 1 Nov 2023 11:05:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233002AbjKAPFM (ORCPT ); Wed, 1 Nov 2023 11:05:12 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2885E12B; Wed, 1 Nov 2023 08:05:03 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5B8EEC433C7; Wed, 1 Nov 2023 15:05:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698851102; bh=J9TDTTp5Th8l4Sk4Cvv9hQrUXmMuJKZ9hBfQCH+y39o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mForM/ElSg9nfUfHoSsS9u0waPf4ExfykoG7pn8Q3XaEFgsuytu/ouZBmYcqQY8rw mBCLgYXYsf/9nsFDRochpXiVbM1DwA2OtewmFI4or+JDh0c8/cnOXG1n9KvZPrO34R XR3XxeLrPAqnnn3W3aaXyYsWXUdRH2RsWT8+z5XWQAtZ2dFQdViSHXC//r9Aa7d9n/ r/L053pdsVpMNa65QHfpWWceGMtD27/WRDnLzz9u1jsm+cnaMmvTfc9ENNegOoaUfY e2eSn8UCofIHVcgoEC8GrhEsPMr5SHW7ge7vA1krBOKkEgAaV+Xi+9vmk0tvA4Kv5C pHhg1GVhlpXWw== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Greg Ungerer , Jack Brennen , Masahiro Yamada , Nathan Chancellor , Nick Desaulniers , Nicolas Schier Subject: [PATCH 6/7] modpost: add symsearch_find_with_name() helper function Date: Thu, 2 Nov 2023 00:04:03 +0900 Message-Id: <20231101150404.754108-7-masahiroy@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231101150404.754108-1-masahiroy@kernel.org> References: <20231101150404.754108-1-masahiroy@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.6 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 01 Nov 2023 08:06:27 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781374592526670129 X-GMAIL-MSGID: 1781374592526670129 This helper function searches for a symbol with the provided name. The symbol must be located in the specified section and within the given distance from the target address. In the expected use case, the min_distance is very small, so the linear search will finish within a few iterations. Signed-off-by: Masahiro Yamada --- scripts/mod/modpost.h | 3 +++ scripts/mod/symsearch.c | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h index 9834ac44846d..43148b1a762b 100644 --- a/scripts/mod/modpost.h +++ b/scripts/mod/modpost.h @@ -186,6 +186,9 @@ void symsearch_finish(struct elf_info *elf); Elf_Sym *symsearch_find_nearest(struct elf_info *elf, Elf_Addr addr, unsigned int secndx, bool allow_negative, Elf_Addr min_distance); +Elf_Sym *symsearch_find_with_name(struct elf_info *elf, Elf_Addr addr, + unsigned int secndx, bool allow_negative, + Elf_Addr min_distance, const char *name); /* file2alias.c */ void handle_moddevtable(struct module *mod, struct elf_info *info, diff --git a/scripts/mod/symsearch.c b/scripts/mod/symsearch.c index 13464e4f4d72..9101bb9584a4 100644 --- a/scripts/mod/symsearch.c +++ b/scripts/mod/symsearch.c @@ -240,3 +240,47 @@ Elf_Sym *symsearch_find_nearest(struct elf_info *elf, Elf_Addr addr, return symsearch_find(elf, addr, secndx, allow_negative, min_distance, symsearch_nearest_filter, elf); } + +struct name_filter_data { + struct elf_info *elf; + const char *name; +}; + +static bool symsearch_name_filter(const Elf_Sym *sym1, const Elf_Sym *sym2, + void *_data) +{ + struct name_filter_data *data = _data; + const char *name; + + /* Check the symbol name. */ + name = sym_name(data->elf, sym1); + if (strcmp(name, data->name)) + return false; + + /* If sym2 is NULL, this is the first occurrence, always take it. */ + if (!sym2) + return true; + + /* Prefer lower address. */ + return sym1->st_value < sym2->st_value; +} + +/* + * Find the symbol which is in secndx and has the given name, and is located + * close enough to the given address. + * allow_negative: allow returning a symbol whose address is > addr. + * min_distance: ignore symbols which are further away than this. + * name: the name of the symbol to search for. + * + * Returns a pointer into the symbol table for success. + * Returns NULL if no legal symbol is found within the requested range. + */ +Elf_Sym *symsearch_find_with_name(struct elf_info *elf, Elf_Addr addr, + unsigned int secndx, bool allow_negative, + Elf_Addr min_distance, const char *name) +{ + struct name_filter_data data = { .elf = elf, .name = name }; + + return symsearch_find(elf, addr, secndx, allow_negative, min_distance, + symsearch_name_filter, &data); +}