[patch 09/38] objtool: Add .sym_sites section

Thomas Gleixner
2022-07-16T23:17:54Z

From: Peter Zijlstra <peterz@infradead.org>

In preparation for call depth tracking provide a section which collects all
all !init symbols to generate thunks for.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
arch/x86/kernel/vmlinux.lds.S | 7 +++++
tools/objtool/check.c | 55 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 62 insertions(+)

--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -297,6 +297,13 @@ SECTIONS
*(.call_sites)
__call_sites_end = .;
}
+
+ . = ALIGN(8);
+ .sym_sites : AT(ADDR(.sym_sites) - LOAD_OFFSET) {
+ __sym_sites = .;
+ *(.sym_sites)
+ __sym_sites_end = .;
+ }
#endif

#ifdef CONFIG_X86_KERNEL_IBT
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -941,6 +941,56 @@ static int create_direct_call_sections(s
return 0;
}

+static int create_sym_thunk_sections(struct objtool_file *file)
+{
+ struct section *sec, *s;
+ struct symbol *sym;
+ unsigned int *loc;
+ int idx;
+
+ sec = find_section_by_name(file->elf, ".sym_sites");
+ if (sec) {
+ INIT_LIST_HEAD(&file->call_list);
+ WARN("file already has .sym_sites section, skipping");
+ return 0;
+ }
+
+ idx = 0;
+ for_each_sec(file, s) {
+ if (!s->text || s->init)
+ continue;
+
+ list_for_each_entry(sym, &s->symbol_list, list)
+ idx++;
+ }
+
+ sec = elf_create_section(file->elf, ".sym_sites", 0, sizeof(unsigned int), idx);
+ if (!sec)
+ return -1;
+
+ idx = 0;
+ for_each_sec(file, s) {
+ if (!s->text || s->init)
+ continue;
+
+ list_for_each_entry(sym, &s->symbol_list, list) {
+
+ loc = (unsigned int *)sec->data->d_buf + idx;
+ memset(loc, 0, sizeof(unsigned int));
+
+ if (elf_add_reloc_to_insn(file->elf, sec,
+ idx * sizeof(unsigned int),
+ R_X86_64_PC32,
+ s, sym->offset))
+ return -1;
+
+ idx++;
+ }
+ }
+
+ return 0;
+}
+
/*
* Warnings shouldn't be reported for ignored functions.
*/
@@ -4326,6 +4376,11 @@ int check(struct objtool_file *file)
if (ret < 0)
goto out;
warnings += ret;
+
+ ret = create_sym_thunk_sections(file);
+ if (ret < 0)
+ goto out;
+ warnings += ret;
}

if (opts.mcount) {