All of lore.kernel.org
 help / color / mirror / Atom feed
From: Masahiro Yamada <masahiroy@kernel.org>
To: linux-kbuild@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Greg Ungerer <gerg@kernel.org>,
	Jack Brennen <jbrennen@google.com>,
	Masahiro Yamada <masahiroy@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Nicolas Schier <nicolas@fjasle.eu>
Subject: [PATCH 6/7] modpost: add symsearch_find_with_name() helper function
Date: Thu,  2 Nov 2023 00:04:03 +0900	[thread overview]
Message-ID: <20231101150404.754108-7-masahiroy@kernel.org> (raw)
In-Reply-To: <20231101150404.754108-1-masahiroy@kernel.org>

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 <masahiroy@kernel.org>
---

 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);
+}
-- 
2.40.1


  parent reply	other threads:[~2023-11-01 15:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-01 15:03 [PATCH 0/7] modpost: fix modpost errors for m68k-uclinux-gcc Masahiro Yamada
2023-11-01 15:03 ` [PATCH 1/7] modpost: move sym_name() to modpost.h Masahiro Yamada
2023-11-01 15:03 ` [PATCH 2/7] modpost: add const qualifier to syminfo table Masahiro Yamada
2023-11-01 15:04 ` [PATCH 3/7] modpost: add table_size local variable to symsearch_find_nearest() Masahiro Yamada
2023-11-01 15:04 ` [PATCH 4/7] modpost: introduce a filtering feature to symsearch Masahiro Yamada
2023-11-01 15:04 ` [PATCH 5/7] modpost: prefer global symbols in symsearch_find_nearest() Masahiro Yamada
2023-11-01 15:04 ` Masahiro Yamada [this message]
2023-11-01 15:04 ` [PATCH 7/7] modpost: look up the correct symbol in check_export_symbol() Masahiro Yamada
2023-11-02 15:00 ` [PATCH 0/7] modpost: fix modpost errors for m68k-uclinux-gcc Greg Ungerer

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=20231101150404.754108-7-masahiroy@kernel.org \
    --to=masahiroy@kernel.org \
    --cc=gerg@kernel.org \
    --cc=jbrennen@google.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nicolas@fjasle.eu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.