[RFC][PATCH v5 18/51] objtool: mcount: Move find_section_sym_index()

From: Matt Helsley
Date: Thu Jun 18 2020 - 16:39:58 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 | 42 ++++++++++++++++++++++++++++++++
tools/objtool/recordmcount.h | 47 +-----------------------------------
2 files changed, 43 insertions(+), 46 deletions(-)

diff --git a/tools/objtool/recordmcount.c b/tools/objtool/recordmcount.c
index 24b019b82795..2225479157e5 100644
--- a/tools/objtool/recordmcount.c
+++ b/tools/objtool/recordmcount.c
@@ -482,6 +482,48 @@ static int (*is_fake_mcount)(struct reloc const *reloc) = 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) {
+ /* avoid symbols with weak binding */
+ 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 cf420f9f64b0..eed592954f37 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 get_shnum
@@ -44,7 +43,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 get_shnum get_shnum64
@@ -67,7 +65,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 get_shnum get_shnum32
@@ -307,48 +304,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) {
- /* avoid symbols with weak binding */
- 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;
@@ -435,7 +390,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