[RFC][PATCH 5/6] objtool: Allow archs to rewrite retpolines

From: Peter Zijlstra
Date: Fri Feb 19 2021 - 16:11:16 EST


When retpolines are employed, compilers typically emit calls to
retpoline thunks. Objtool recognises these calls and marks them as
dynamic calls.

Provide infrastructure for architectures to rewrite/augment what the
compiler wrote for us.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
tools/objtool/check.c | 23 +++++++++++++++++++++--
tools/objtool/include/objtool/arch.h | 3 +++
2 files changed, 24 insertions(+), 2 deletions(-)

--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -775,6 +775,14 @@ static int add_ignore_alternatives(struc
return 0;
}

+
+__weak int arch_rewrite_retpoline(struct objtool_file *file,
+ struct instruction *insn,
+ struct reloc *reloc)
+{
+ return 0;
+}
+
/*
* Find the destination instructions for all jumps.
*/
@@ -808,6 +816,9 @@ static int add_jump_destinations(struct
insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;

insn->retpoline_safe = true;
+
+ arch_rewrite_retpoline(file, insn, reloc);
+
continue;
} else if (insn->func) {
/* internal or external sibling call (with reloc) */
@@ -959,6 +970,8 @@ static int add_call_destinations(struct
insn->type = INSN_CALL_DYNAMIC;
insn->retpoline_safe = true;

+ arch_rewrite_retpoline(file, insn, reloc);
+
remove_insn_ops(insn);
continue;

@@ -1119,6 +1132,8 @@ static int handle_group_alt(struct objto
dest_off = arch_jump_destination(insn);
if (dest_off == special_alt->new_off + special_alt->new_len)
insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
+ else
+ insn->jump_dest = find_insn(file, insn->sec, dest_off);

if (!insn->jump_dest) {
WARN_FUNC("can't find alternative jump destination",
@@ -1704,11 +1719,15 @@ static int decode_sections(struct objtoo
if (ret)
return ret;

- ret = add_jump_destinations(file);
+ /*
+ * Must be before add_{jump,call}_destination; for they can add
+ * magic alternatives we can't actually parse.
+ */
+ ret = add_special_section_alts(file);
if (ret)
return ret;

- ret = add_special_section_alts(file);
+ ret = add_jump_destinations(file);
if (ret)
return ret;

--- a/tools/objtool/include/objtool/arch.h
+++ b/tools/objtool/include/objtool/arch.h
@@ -82,6 +82,9 @@ unsigned long arch_jump_destination(stru
unsigned long arch_dest_reloc_offset(int addend);

const char *arch_nop_insn(int len);
+int arch_rewrite_retpoline(struct objtool_file *file,
+ struct instruction *insn,
+ struct reloc *reloc);

int arch_decode_hint_reg(struct instruction *insn, u8 sp_reg);