linux-kbuild.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Masahiro Yamada <masahiroy@kernel.org>
To: linux-kbuild@vger.kernel.org
Cc: linux-kernel@vger.kernel.org,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Nicolas Pitre <npitre@baylibre.com>,
	Nicolas Schier <nicolas@fjasle.eu>,
	Masahiro Yamada <masahiroy@kernel.org>
Subject: [PATCH v4 10/21] modpost: simplify find_elf_symbol()
Date: Sun, 14 May 2023 05:44:51 +0900	[thread overview]
Message-ID: <20230513204502.1593923-11-masahiroy@kernel.org> (raw)
In-Reply-To: <20230513204502.1593923-1-masahiroy@kernel.org>

I do not know why commit 157c23c80eed ("kbuild: use simpler section
mismatch warnings in modpost") is only applicable to find_elf_symbol2().

Simplify find_elf_symbol() based on find_elf_symbol2().

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/mod/modpost.c | 64 +++++++++----------------------------------
 1 file changed, 13 insertions(+), 51 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 3b7b78e69137..b4fa9e0be4d1 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1117,57 +1117,7 @@ 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 st_name != 0 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_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
-				Elf_Sym *relsym)
-{
-	Elf_Sym *sym;
-	Elf_Sym *near = NULL;
-	Elf64_Sword distance = 20;
-	Elf64_Sword d;
-	unsigned int relsym_secindex;
-
-	if (relsym->st_name != 0)
-		return relsym;
-
-	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.
- **/
+/* Look up the nearest symbol based on the section and the address */
 static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
 				 unsigned int secndx)
 {
@@ -1178,6 +1128,8 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
 	for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
 		if (get_secindex(elf, sym) != secndx)
 			continue;
+		if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
+			continue;
 		if (!is_valid_name(elf, sym))
 			continue;
 		if (sym->st_value <= addr && addr - sym->st_value <= distance) {
@@ -1188,6 +1140,16 @@ static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
 	return near;
 }
 
+static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr, Elf_Sym *sym)
+{
+	/* If the supplied symbol is valid, return it */
+	if (sym->st_name != 0)
+		return sym;
+
+	/* Otherwise, look up a better symbol */
+	return find_elf_symbol2(elf, addr, get_secindex(elf, sym));
+}
+
 static bool is_executable_section(struct elf_info *elf, unsigned int secndx)
 {
 	if (secndx > elf->num_sections)
-- 
2.39.2


  parent reply	other threads:[~2023-05-13 20:46 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-13 20:44 [PATCH v4 00/21] Unify <linux/export.h> and <asm/export.h>, remove EXPORT_DATA_SYMBOL(), faster TRIM_UNUSED_KSYMS Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 01/21] modpost: remove broken calculation of exception_table_entry size Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 02/21] modpost: remove fromsym info in __ex_table section mismatch warning Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 03/21] modpost: remove get_prettyname() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 04/21] modpost: squash report_extable_warnings() into extable_mismatch_handler() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 05/21] modpost: squash report_sec_mismatch() into default_mismatch_handler() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 06/21] modpost: clean up is_executable_section() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 07/21] modpost: squash extable_mismatch_handler() into default_mismatch_handler() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 08/21] modpost: pass 'tosec' down to default_mismatch_handler() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 09/21] modpost: pass section index to find_elf_symbol2() Masahiro Yamada
2023-05-13 20:44 ` Masahiro Yamada [this message]
2023-05-13 20:44 ` [PATCH v4 11/21] modpost: rename find_elf_symbol() and find_elf_symbol2() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 12/21] modpost: unify 'sym' and 'to' in default_mismatch_handler() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 13/21] modpost: replace r->r_offset, r->r_addend with faddr, taddr Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 14/21] modpost: remove is_shndx_special() check from section_rel(a) Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 15/21] modpost: pass struct module pointer to check_section_mismatch() Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 16/21] kbuild: generate KSYMTAB entries by modpost Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 17/21] ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL* Masahiro Yamada
2023-05-13 20:44 ` [PATCH v4 18/21] modpost: check static EXPORT_SYMBOL* by modpost again Masahiro Yamada
2023-05-13 20:45 ` [PATCH v4 19/21] modpost: squash sym_update_namespace() into sym_add_exported() Masahiro Yamada
2023-05-13 20:45 ` [PATCH v4 20/21] modpost: use null string instead of NULL pointer for default namespace Masahiro Yamada
2023-05-13 20:45 ` [PATCH v4 21/21] kbuild: implement CONFIG_TRIM_UNUSED_KSYMS without recursion Masahiro Yamada
2023-05-14 12:43 ` [PATCH v4 00/21] Unify <linux/export.h> and <asm/export.h>, remove EXPORT_DATA_SYMBOL(), faster TRIM_UNUSED_KSYMS Masahiro Yamada

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230513204502.1593923-11-masahiroy@kernel.org \
    --to=masahiroy@kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nicolas@fjasle.eu \
    --cc=npitre@baylibre.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).