[RFC][PATCH 20/36] objtool: mcount: Move find_section_sym_index()

From: Matt Helsley
Date: Fri Apr 10 2020 - 15:38:10 EST


This function is no longer dependent upon the old recordmcount
ELF wrapper code -- it doesn't use the wrapper's Elf_* types nor
does it call wrapped functions. Move it into the C file.

Signed-off-by: Matt Helsley <mhelsley@xxxxxxxxxx>
---
tools/objtool/recordmcount.c | 41 ++++++++++++++++++++++++++++++++
tools/objtool/recordmcount.h | 46 +-----------------------------------
2 files changed, 42 insertions(+), 45 deletions(-)

diff --git a/tools/objtool/recordmcount.c b/tools/objtool/recordmcount.c
index 6c499fe86132..61901b1c7bd3 100644
--- a/tools/objtool/recordmcount.c
+++ b/tools/objtool/recordmcount.c
@@ -482,6 +482,47 @@ static int (*is_fake_mcount)(struct rela const *rela) = fn_is_fake_mcount;

static const unsigned int missing_sym = (unsigned int)-1;

+/*
+ * Find a symbol in the given section, to be used as the base for relocating
+ * the table of offsets of calls to mcount. A local or global symbol suffices,
+ * but avoid a Weak symbol because it may be overridden; the change in value
+ * would invalidate the relocations of the offsets of the calls to mcount.
+ * Often the found symbol will be the unnamed local symbol generated by
+ * GNU 'as' for the start of each section. For example:
+ * Num: Value Size Type Bind Vis Ndx Name
+ * 2: 00000000 0 SECTION LOCAL DEFAULT 1
+ */
+static int find_section_sym_index(unsigned const txtndx,
+ char const *const txtname,
+ unsigned long *const recvalp,
+ unsigned int *sym_index)
+{
+ struct symbol *sym;
+ struct section *txts = find_section_by_index(lf, txtndx);
+
+ if (!txts) {
+ fprintf(stderr, "Cannot find section %u: %s.\n",
+ txtndx, txtname);
+ return missing_sym;
+ }
+
+ list_for_each_entry(sym, &txts->symbol_list, list) {
+ if ((sym->bind == STB_LOCAL) || (sym->bind == STB_GLOBAL)) {
+ /* function symbols on ARM have quirks, avoid them */
+ if (lf->ehdr.e_machine == EM_ARM
+ && sym->type == STT_FUNC)
+ continue;
+
+ *recvalp = sym->sym.st_value;
+ *sym_index = sym->idx;
+ return 0;
+ }
+ }
+ fprintf(stderr, "Cannot find symbol for section %u: %s.\n",
+ txtndx, txtname);
+ return missing_sym;
+}
+
/* 32 bit and 64 bit are very similar */
#include "recordmcount.h"
#define RECORD_MCOUNT_64
diff --git a/tools/objtool/recordmcount.h b/tools/objtool/recordmcount.h
index 066923dd70a9..3c7afb9a2eac 100644
--- a/tools/objtool/recordmcount.h
+++ b/tools/objtool/recordmcount.h
@@ -21,7 +21,6 @@
#undef mcount_adjust
#undef sift_rel_mcount
#undef nop_mcount
-#undef find_section_sym_index
#undef has_rel_mcount
#undef tot_relsize
#undef do_func
@@ -41,7 +40,6 @@
# define append_func append64
# define sift_rel_mcount sift64_rel_mcount
# define nop_mcount nop_mcount_64
-# define find_section_sym_index find64_section_sym_index
# define has_rel_mcount has64_rel_mcount
# define tot_relsize tot64_relsize
# define do_func do64
@@ -61,7 +59,6 @@
# define append_func append32
# define sift_rel_mcount sift32_rel_mcount
# define nop_mcount nop_mcount_32
-# define find_section_sym_index find32_section_sym_index
# define has_rel_mcount has32_rel_mcount
# define tot_relsize tot32_relsize
# define do_func do32
@@ -270,47 +267,6 @@ static int nop_mcount(struct section * const rels,
return 0;
}

-/*
- * Find a symbol in the given section, to be used as the base for relocating
- * the table of offsets of calls to mcount. A local or global symbol suffices,
- * but avoid a Weak symbol because it may be overridden; the change in value
- * would invalidate the relocations of the offsets of the calls to mcount.
- * Often the found symbol will be the unnamed local symbol generated by
- * GNU 'as' for the start of each section. For example:
- * Num: Value Size Type Bind Vis Ndx Name
- * 2: 00000000 0 SECTION LOCAL DEFAULT 1
- */
-static int find_section_sym_index(unsigned const txtndx,
- char const *const txtname,
- unsigned long *const recvalp,
- unsigned int *sym_index)
-{
- struct symbol *sym;
- struct section *txts = find_section_by_index(lf, txtndx);
-
- if (!txts) {
- fprintf(stderr, "Cannot find section %u: %s.\n",
- txtndx, txtname);
- return missing_sym;
- }
-
- list_for_each_entry(sym, &txts->symbol_list, list) {
- if ((sym->bind == STB_LOCAL) || (sym->bind == STB_GLOBAL)) {
- /* function symbols on ARM have quirks, avoid them */
- if (lf->ehdr.e_machine == EM_ARM
- && sym->type == STT_FUNC)
- continue;
-
- *recvalp = sym->sym.st_value;
- *sym_index = sym->idx;
- return 0;
- }
- }
- fprintf(stderr, "Cannot find symbol for section %u: %s.\n",
- txtndx, txtname);
- return missing_sym;
-}
-
static char const *has_rel_mcount(const struct section * const rels)
{
const struct section *txts;
@@ -398,7 +354,7 @@ static int do_func(Elf_Ehdr *const ehdr,
rel_entsize = sec->sh.sh_entsize;
mlocp = sift_rel_mcount(mlocp,
(void *)mlocp - (void *)mloc0, &mrelp,
- sec, recsym, recval, reltype);
+ sec, recsym, (uint_t)recval, reltype);
} else if (txtname && (warn_on_notrace_sect || make_nop)) {
/*
* This section is ignored by ftrace, but still
--
2.20.1