Objtool updates for easier portability
cover Objtool updates for easier portability
Hi,
This patchset includes some of the least controversial changes that were needed as part of the arm64 port [1].
It consist mostly of small fixes or lifting some limitations to make it easier to support a new architecture in objtool. Of course, these will not be the only required changes, but these are the ones I hope make enough sense to be merged separately from the rest of arm64 port series.
The patches should apply cleanly on linux-tip/master tree.
[1] https://lkml.org/lkml/2020/1/9/643
Thanks,
Julien
-->
Julien Thierry (9):
objtool: Move header sync-check ealier in build
objtool: check: Remove redundant checks on operand type
objtool: check: Clean instruction state before each function
validation
objtool: check: Ignore empty alternative groups
objtool: check: Remove check preventing branches within alternative
objtool: check: Use arch specific values in restore_reg()
objtool: check: Allow save/restore hint in non standard function
symbols
objtool: Split generic and arch specific CFI definitions
objtool: Support multiple stack_op per instruction
Raphael Gault (1):
objtool: Add abstraction for computation of symbols offsets
tools/objtool/Makefile | 5 +- tools/objtool/arch.h | 10 +- tools/objtool/arch/x86/decode.c | 24 +++- tools/objtool/arch/x86/include/cfi_regs.h | 25 ++++ tools/objtool/cfi.h | 21 +-- tools/objtool/check.c | 157 +++++++++++++--------- tools/objtool/check.h | 3 +- 7 files changed, 159 insertions(+), 86 deletions(-) create mode 100644 tools/objtool/arch/x86/include/cfi_regs.h
--
2.21.1
PATCH 1/10 objtool: Move header sync-check ealier in build
Currently, the check of tools files against kernel equivalent is only done after every object file has been built. This means one might fix build issues against outdated headers without seeing a warning about this.
Check headers before any object is built. Also, make it part of a FORCE'd recipe so every attempt to build objtool will report the outdated headers (if any).
--- tools/objtool/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/objtool/Makefile +1 -1
@@ -43,10 +43,10 @@ export srctree OUTPUT CFLAGS SRCARCH AWK include $(srctree)/tools/build/Makefile.include $(OBJTOOL_IN): fixdep FORCE + @$(CONFIG_SHELL) ./sync-check.sh @$(MAKE) $(build)=objtool $(OBJTOOL): $(LIBSUBCMD) $(OBJTOOL_IN) - @$(CONFIG_SHELL) ./sync-check.sh $(QUIET_LINK)$(CC) $(OBJTOOL_IN) $(LDFLAGS) -o $@
Signature · 1 line
2.21.1
PATCH 2/10 objtool: check: Remove redundant checks on operand type
In reply to Julien ThierryPOP operations are already in code path where destination operand is OP_DEST_REG. There is no need to check the operand type again.
--- tools/objtool/check.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
tools/objtool/check.c +1 -3
@@ -1730,15 +1730,13 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)case OP_SRC_POP :case OP_SRC_POPF : -if (!state->drap && op->dest.type ==OP_DEST_REG && - op->dest.reg == cfa->base) { +if (!state->drap && op->dest.reg == cfa->base) {/* pop %rbp */ cfa->base =CFI_SP ; }if (state->drap && cfa->base ==CFI_BP_INDIRECT && - op->dest.type ==OP_DEST_REG && op->dest.reg == state->drap_reg && state->drap_offset == -state->stack_size) {
Signature · 1 line
2.21.1
PATCH 3/10 objtool: check: Clean instruction state before each function validation
In reply to Julien ThierryWhen a function fails its validation, it might leave a stale state that will be used for the validation of other functions. That would cause false warnings on potentially valid functions.
Reset the instruction state before the validation of each individual function.
--- tools/objtool/check.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
tools/objtool/check.c +6 -7
@@ -2406,13 +2406,6 @@ static int validate_functions(struct objtool_file *file)struct insn_state state;int ret, warnings =0 ; -clear_insn_state (&state); - - state.cfa = initial_func_cfi.cfa; -memcpy (&state.regs, &initial_func_cfi.regs, -CFI_NUM_REGS *sizeof (struct cfi_reg )); - state.stack_size = initial_func_cfi.cfa.offset; -for_each_sec (file, sec) {list_for_each_entry (func, &sec->symbol_list, list) {if (func->type !=STT_FUNC )
@@ -2431,6 +2424,12 @@ static int validate_functions(struct objtool_file *file)if (!insn || insn->ignore || insn->visited)continue ; +clear_insn_state (&state); + state.cfa = initial_func_cfi.cfa; +memcpy (&state.regs, &initial_func_cfi.regs, +CFI_NUM_REGS *sizeof (struct cfi_reg )); + state.stack_size = initial_func_cfi.cfa.offset; + state.uaccess = func->uaccess_safe; ret =validate_branch (file, func, insn, state);
Signature · 1 line
2.21.1
PATCH 4/10 objtool: check: Ignore empty alternative groups
In reply to Julien ThierryAtlernative section can contain entries for alternatives with no instructions. Objtool will currently crash when handling such an entry.
Just skip that entry, but still give a warning to discourage useless entries.
--- tools/objtool/check.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/objtool/check.c +6 -0
@@ -917,6 +917,12 @@ static int add_special_section_alts(struct objtool_file *file) }if (special_alt->group) { +if (!special_alt->orig_len) { +WARN_FUNC ("empty alternative entry" , + orig_insn->sec, orig_insn->offset); +continue ; + } + ret =handle_group_alt (file, special_alt, orig_insn, &new_insn);if (ret)
Signature · 1 line
2.21.1
PATCH 5/10 objtool: check: Remove check preventing branches within alternative
In reply to Julien ThierryWhile jumping from outside an alternative region to the middle of an alternative region is very likely wrong, jumping from an alternative region into the same region is valid. It is a common pattern on arm64.
The first pattern is unlikely to happen in practice and checking only for this adds a lot of complexity.
Just remove the current check.
--- tools/objtool/check.c | 6 ------ 1 file changed, 6 deletions(-)
tools/objtool/check.c +0 -6
@@ -2015,12 +2015,6 @@ static int validate_branch(struct objtool_file *file, struct symbol *func, insn = first; sec = insn->sec; -if (insn->alt_group &&list_empty (&insn->alts)) { -WARN_FUNC ("don't know how to handle branch to middle of alternative instruction group" , - sec, insn->offset); -return 1 ; - } -while (1 ) { next_insn =next_insn_same_sec (file, insn);
Signature · 1 line
2.21.1
PATCH 6/10 objtool: check: Use arch specific values in restore_reg()
In reply to Julien ThierryInitial register state is set up by arch specific code. Use the value the arch code has set when restoring registers from the stack.
--- tools/objtool/check.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/objtool/check.c +2 -2
@@ -1501,8 +1501,8 @@ static void save_reg(struct insn_state *state, unsigned char reg, int base,static void restore_reg (struct insn_state *state,unsigned char reg) { - state->regs[reg].base =CFI_UNDEFINED ; - state->regs[reg].offset =0 ; + state->regs[reg].base = initial_func_cfi.regs[reg].base; + state->regs[reg].offset = initial_func_cfi.regs[reg].offset; }/*
Signature · 1 line
2.21.1
PATCH 7/10 objtool: check: Allow save/restore hint in non standard function symbols
In reply to Julien ThierryThe kernel code base provides CODE_SYM_START/END to declare assembly code sequences that don't follow function standard calling conventions.
As non-C/non-standard code, these sequences can very much benefit from unwind hints. However, when a restore unwind_hint is used in a non-function code sequence, objtool will crash when looking for the corresponding save hint.
Record the code symbol an instruction belongs to and look for save hints belonging to the same code symbol as the restore hint.
--- tools/objtool/check.c | 27 +++++++++++++++++++-------- tools/objtool/check.h | 1 + 2 files changed, 20 insertions(+), 8 deletions(-)
tools/objtool/check.c +19 -8
@@ -236,7 +236,7 @@ static void clear_insn_state(struct insn_state *state)static int decode_instructions (struct objtool_file *file) {struct section *sec; -struct symbol *func; +struct symbol *sym;unsigned long offset;struct instruction *insn;int ret;
@@ -276,18 +276,22 @@ static int decode_instructions(struct objtool_file *file)list_add_tail (&insn->list, &file->insn_list); } -list_for_each_entry (func, &sec->symbol_list, list) { -if (func->type !=STT_FUNC || func->alias != func) +list_for_each_entry (sym, &sec->symbol_list, list) { +if ((sym->type !=STT_FUNC && sym->type !=STT_NOTYPE ) || + sym->alias != sym)continue ; -if (!find_insn (file, sec, func->offset)) { +if (!find_insn (file, sec, sym->offset)) {WARN ("%s(): can't find starting instruction" , - func->name); + sym->name);return -1 ; } -func_for_each_insn (file, func, insn) - insn->func = func; +func_for_each_insn (file, sym, insn) { + insn->code_sym = sym; +if (sym->type ==STT_FUNC ) + insn->func = sym; + } } }
@@ -771,6 +775,7 @@ static int handle_group_alt(struct objtool_file *file, fake_jump->type =INSN_JUMP_UNCONDITIONAL ; fake_jump->jump_dest =list_next_entry (last_orig_insn, list); fake_jump->func = orig_insn->func; + fake_jump->code_sym = orig_insn->code_sym; }if (!special_alt->new_len) {
@@ -2043,9 +2048,15 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,if (insn->restore) {struct instruction *save_insn, *i; +if (!insn->code_sym) { +WARN_FUNC ("restore instruction doesn't belong to any symbol" , + insn->sec, insn->offset); +return 1 ; + } + i = insn; save_insn =NULL ; -func_for_each_insn_continue_reverse (file, func, i) { +func_for_each_insn_continue_reverse (file, insn->code_sym, i) {if (i->save) { save_insn = i;break ;
tools/objtool/check.h +1 -0
@@ -42,6 +42,7 @@ struct instruction {struct rela *jump_table;struct list_head alts;struct symbol *func; +struct symbol *code_sym;struct stack_op stack_op;struct insn_state state;struct orc_entry orc;
Signature · 1 line
2.21.1
PATCH 8/10 objtool: Add abstraction for computation of symbols offsets
In reply to Julien ThierryFrom: Raphael Gault <[email protected]>
The jump destination and relocation offset used previously are only reliable on x86_64 architecture. We abstract these computations by calling arch-dependent implementations.
[J.T. Remove superfluous comment, replace other addend offsets
with arch_dest_rela_offset]
--- tools/objtool/arch.h | 6 ++++++ tools/objtool/arch/x86/decode.c | 11 +++++++++++ tools/objtool/check.c | 18 ++++++++++-------- 3 files changed, 27 insertions(+), 8 deletions(-)
tools/objtool/arch.h +6 -0
@@ -66,6 +66,8 @@ struct stack_op {struct op_src src; }; +struct instruction ; +void arch_initial_func_cfi_state (struct cfi_state *state); int arch_decode_instruction(struct elf *elf, struct section *sec,
@@ -75,4 +77,8 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,bool arch_callee_saved_reg (unsigned char reg); +unsigned long arch_jump_destination (struct instruction *insn); + +unsigned long arch_dest_rela_offset (int addend); +#endif /* _ARCH_H */
tools/objtool/arch/x86/decode.c +11 -0
@@ -11,6 +11,7 @@#include "../../../arch/x86/lib/inat.c" #include "../../../arch/x86/lib/insn.c" +#include "../../check.h" #include "../../elf.h" #include "../../arch.h" #include "../../warn.h"
@@ -66,6 +67,16 @@ bool arch_callee_saved_reg(unsigned char reg) } } +unsigned long arch_dest_rela_offset (int addend) +{ +return addend +4 ; +} + +unsigned long arch_jump_destination (struct instruction *insn) +{ +return insn->offset + insn->len + insn->immediate; +} + int arch_decode_instruction(struct elf *elf, struct section *sec, unsigned long offset, unsigned int maxlen, unsigned int *len, enum insn_type *type,
tools/objtool/check.c +10 -8
@@ -590,13 +590,14 @@ static int add_jump_destinations(struct objtool_file *file) insn->len);if (!rela) { dest_sec = insn->sec; - dest_off = insn->offset + insn->len + insn->immediate; + dest_off =arch_jump_destination (insn); }else if (rela->sym->type ==STT_SECTION ) { dest_sec = rela->sym->sec; - dest_off = rela->addend +4 ; + dest_off =arch_dest_rela_offset (rela->addend); }else if (rela->sym->sec->idx) { dest_sec = rela->sym->sec; - dest_off = rela->sym->sym.st_value + rela->addend +4 ; + dest_off = rela->sym->sym.st_value + +arch_dest_rela_offset (rela->addend); }else if (strstr (rela->sym->name,"_indirect_thunk_" )) {/* * Retpoline jumps are really dynamic jumps in
@@ -686,7 +687,7 @@ static int add_call_destinations(struct objtool_file *file) rela =find_rela_by_dest_range (insn->sec, insn->offset, insn->len);if (!rela) { - dest_off = insn->offset + insn->len + insn->immediate; + dest_off =arch_jump_destination (insn); insn->call_dest =find_func_by_offset (insn->sec, dest_off);if (!insn->call_dest) insn->call_dest =find_symbol_by_offset (insn->sec, dest_off);
@@ -709,13 +710,14 @@ static int add_call_destinations(struct objtool_file *file) } }else if (rela->sym->type ==STT_SECTION ) { + dest_off =arch_dest_rela_offset (rela->addend); insn->call_dest =find_func_by_offset (rela->sym->sec, - rela->addend+4 ); + dest_off);if (!insn->call_dest) { -WARN_FUNC ("can't find call dest symbol at %s+0x%x" , +WARN_FUNC ("can't find call dest symbol at %s+0x%lx" , insn->sec, insn->offset, rela->sym->sec->name, - rela->addend +4 ); + dest_off);return -1 ; } }else
@@ -827,7 +829,7 @@ static int handle_group_alt(struct objtool_file *file,if (!insn->immediate)continue ; - dest_off = insn->offset + insn->len + insn->immediate; + dest_off =arch_jump_destination (insn);if (dest_off == special_alt->new_off + special_alt->new_len) {if (!fake_jump) { WARN("%s: alternative jump to end of section",
Signature · 1 line
2.21.1
PATCH 9/10 objtool: Split generic and arch specific CFI definitions
In reply to Julien ThierrySome CFI definitions used by generic objtool code have no reason to vary from one architecture to another. Move those definition to generic code and keep a separate per arch header to provide architecture specific CFI definitions.
--- tools/objtool/Makefile | 3 ++- tools/objtool/arch/x86/include/cfi_regs.h | 25 +++++++++++++++++++++++ tools/objtool/cfi.h | 21 ++----------------- 3 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 tools/objtool/arch/x86/include/cfi_regs.h
tools/objtool/Makefile +2 -1
@@ -29,7 +29,8 @@ all: $(OBJTOOL) INCLUDES := -I$(srctree)/tools/include \ -I$(srctree)/tools/arch/$(HOSTARCH)/include/uapi \ - -I$(srctree)/tools/arch/$(SRCARCH)/include + -I$(srctree)/tools/arch/$(SRCARCH)/include \ + -I$(srctree)/tools/objtool/arch/$(SRCARCH)/include WARNINGS := $(EXTRA_WARNINGS) -Wno-switch-default -Wno-switch-enum -Wno-packed CFLAGS := -Werror $(WARNINGS) $(KBUILD_HOSTCFLAGS) -g $(INCLUDES) $(LIBELF_FLAGS) LDFLAGS += $(LIBELF_LIBS) $(LIBSUBCMD) $(KBUILD_HOSTLDFLAGS)
tools/objtool/arch/x86/include/cfi_regs.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef _OBJTOOL_CFI_REGS_H +#define _OBJTOOL_CFI_REGS_H + +#define CFI_AX 0 +#define CFI_DX 1 +#define CFI_CX 2 +#define CFI_BX 3 +#define CFI_SI 4 +#define CFI_DI 5 +#define CFI_BP 6 +#define CFI_SP 7 +#define CFI_R8 8 +#define CFI_R9 9 +#define CFI_R10 10 +#define CFI_R11 11 +#define CFI_R12 12 +#define CFI_R13 13 +#define CFI_R14 14 +#define CFI_R15 15 +#define CFI_RA 16 +#define CFI_NUM_REGS 17 + +#endif /* _OBJTOOL_CFI_REGS_H */
tools/objtool/cfi.h +2 -19
@@ -6,30 +6,13 @@ #ifndef _OBJTOOL_CFI_H #define _OBJTOOL_CFI_H +#include "cfi_regs.h" + #define CFI_UNDEFINED -1 #define CFI_CFA -2 #define CFI_SP_INDIRECT -3 #define CFI_BP_INDIRECT -4 -#define CFI_AX 0 -#define CFI_DX 1 -#define CFI_CX 2 -#define CFI_BX 3 -#define CFI_SI 4 -#define CFI_DI 5 -#define CFI_BP 6 -#define CFI_SP 7 -#define CFI_R8 8 -#define CFI_R9 9 -#define CFI_R10 10 -#define CFI_R11 11 -#define CFI_R12 12 -#define CFI_R13 13 -#define CFI_R14 14 -#define CFI_R15 15 -#define CFI_RA 16 -#define CFI_NUM_REGS 17 - struct cfi_reg { int base; int offset;
Signature · 1 line
2.21.1
PATCH 10/10 objtool: Support multiple stack_op per instruction
In reply to Julien ThierryInstruction sets can include more or less complex operations which might not fit the currently defined set of stack_ops.
Combining more than one stack_op provides more flexibility to describe the behaviour of an instruction. This also reduces the need to define new stack_ops specific to a single instruction set.
Allow instruction decoders to generate multiple stack_op per instruction.
--- tools/objtool/arch.h | 4 +- tools/objtool/arch/x86/decode.c | 13 +++++- tools/objtool/check.c | 79 +++++++++++++++++++++------------ tools/objtool/check.h | 2 +- 4 files changed, 67 insertions(+), 31 deletions(-)
tools/objtool/arch.h +3 -1
@@ -64,6 +64,7 @@ struct op_src {struct stack_op {struct op_dest dest;struct op_src src; +struct list_head list; };struct instruction ;
@@ -73,7 +74,8 @@ void arch_initial_func_cfi_state(struct cfi_state *state);int arch_decode_instruction (struct elf *elf,struct section *sec,unsigned long offset,unsigned int maxlen,unsigned int *len,enum insn_type *type, -unsigned long *immediate,struct stack_op *op); +unsigned long *immediate, +struct list_head *ops_list);bool arch_callee_saved_reg (unsigned char reg);
tools/objtool/arch/x86/decode.c +12 -1
@@ -80,13 +80,15 @@ unsigned long arch_jump_destination(struct instruction *insn)int arch_decode_instruction (struct elf *elf,struct section *sec,unsigned long offset,unsigned int maxlen,unsigned int *len,enum insn_type *type, -unsigned long *immediate,struct stack_op *op) +unsigned long *immediate, +struct list_head *ops_list) {struct insn insn;int x86_64, sign;unsigned char op1, op2, rex =0 , rex_b =0 , rex_r =0 , rex_w =0 , rex_x =0 , modrm =0 , modrm_mod =0 , modrm_rm =0 , modrm_reg =0 , sib =0 ; +struct stack_op *op; x86_64 =is_x86_64 (elf);if (x86_64 ==-1 )
@@ -127,6 +129,10 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,if (insn.sib.nbytes) sib = insn.sib.bytes[0 ]; + op =calloc (1 ,sizeof (*op)); +if (!op) +return -1 ; +switch (op1) {case 0x1 :
@@ -488,6 +494,11 @@ int arch_decode_instruction(struct elf *elf, struct section *sec, *immediate = insn.immediate.nbytes ? insn.immediate.value :0 ; +if (*type ==INSN_STACK ) +list_add_tail (&op->list, ops_list); +else +free (op); +return 0 ; }
tools/objtool/check.c +51 -28
143 lines not on this page · raw message
tools/objtool/check.h +1 -1
@@ -43,7 +43,7 @@ struct instruction {struct list_head alts;struct symbol *func;struct symbol *code_sym; -struct stack_op stack_op; +struct list_head stack_ops;struct insn_state state;struct orc_entry orc; };
Signature · 1 line
2.21.1