objtool: Add support for Arm64
cover objtool: Add support for Arm64
Hi,
As of now, objtool only supports the x86_64 architecture but the groundwork has already been done in order to add support for other architecture without too much effort.
This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.
* Patch 1 adapts the existing code to be able to add support for other architecture.
* Patch 2 provide implementation of the required function for the arm64 architecture.
* Patch 3 adapts the checking of the stack state for the arm64 architecture.
* Patch 4 & 5 fix some warning objtool raised in some particular functions of ~/arch/arm64/kernel/sleep.S. Patch 4 add a macro to signal that some function should be ignored by objtool.
* Patch 6 enables stack validation for arm64.
Theses patches should provide support for the main cases and behaviour. However a few corner cases are not yet handled by objtool:
* In the ~/arch/arm64/crypto/ directory, I noticed that some plain data are sometimes stored in the .text section causing objtool to mistake this for instructions and trying (and failing) to interprete them. If someone could explain to me why we store data directly in the .text section I would appreciate it.
* In the support for arm32 architecture such as in ~/arch/arm64/kernel/kuser32.S some A32 instructions are used but such instructions are not understood by objtool causing a warning.
I also have a few unclear points I would like to bring to your attention:
* For x86_64, when looking for a symbol relocation with explicit addend, objtool systematically adds a +4 offset to the addend. I don't understand why even if I have a feeling it is related to the type of relacation.
* I currently don't have a clear understanding about how switch-tables are generated on arm64 and how to retrieve them (based on relocations).
Please provide me with any feedback and comments as well on the content than the style of these patches.
Thanks,
Raphael
->
Raphael Gault (6):
objtool: Refactor code to make it more suitable for multiple
architecture support
objtool: arm64: Add required implementation for supporting the aarch64
architecture in objtool.
objtool: arm64: Adapt the stack frame checks and the section analysis
for the arm architecture
arm64: assembler: Add macro to annotate asm function having non
standard stack-frame.
arm64: sleep: Add stack frame setup for __cpu_supsend_enter
objtool: arm64: Enable stack validation for arm64
arch/arm64/Kconfig | 1 +
arch/arm64/include/asm/assembler.h | 18 +
arch/arm64/kernel/sleep.S | 4 +
tools/objtool/Build | 1 -
tools/objtool/arch.h | 11 +
tools/objtool/arch/arm64/Build | 6 +
tools/objtool/arch/arm64/bit_operations.c | 65 +
tools/objtool/arch/arm64/decode.c | 2870 +++++++++++++++++
.../objtool/arch/arm64/include/arch_special.h | 44 +
.../arch/arm64/include/asm/orc_types.h | 109 +
.../arch/arm64/include/bit_operations.h | 22 +
tools/objtool/arch/arm64/include/cfi.h | 76 +
.../objtool/arch/arm64/include/insn_decode.h | 219 ++
tools/objtool/arch/arm64/orc_gen.c | 40 +
tools/objtool/arch/x86/Build | 1 +
tools/objtool/arch/x86/decode.c | 111 +
tools/objtool/arch/x86/include/arch_special.h | 35 +
tools/objtool/{ => arch/x86/include}/cfi.h | 0
tools/objtool/{ => arch/x86}/orc_gen.c | 10 +-
tools/objtool/check.c | 209 +-
tools/objtool/check.h | 1 +
tools/objtool/elf.c | 3 +-
tools/objtool/orc.h | 4 +-
tools/objtool/special.c | 18 +-
24 files changed, 3740 insertions(+), 138 deletions(-)
create mode 100644 tools/objtool/arch/arm64/Build
create mode 100644 tools/objtool/arch/arm64/bit_operations.c
create mode 100644 tools/objtool/arch/arm64/decode.c
create mode 100644 tools/objtool/arch/arm64/include/arch_special.h
create mode 100644 tools/objtool/arch/arm64/include/asm/orc_types.h
create mode 100644 tools/objtool/arch/arm64/include/bit_operations.h
create mode 100644 tools/objtool/arch/arm64/include/cfi.h
create mode 100644 tools/objtool/arch/arm64/include/insn_decode.h
create mode 100644 tools/objtool/arch/arm64/orc_gen.c
create mode 100644 tools/objtool/arch/x86/include/arch_special.h
rename tools/objtool/{ => arch/x86/include}/cfi.h (100%)
rename tools/objtool/{ => arch/x86}/orc_gen.c (96%)Signature · 1 line
2.17.1
RFC 1/6 objtool: Refactor code to make it more suitable for multiple architecture support
The jump destination and relocation offset used previously are only reliable on x86_64 architecture. We abstract these computations by calling arch-dependant implementation.
The control flow information and register macro definitions were based on the x86_64 architecture but should be abstract so that each architecture can define the correct values for the registers, especially the registers related to the stack frame (Frame Pointer, Stack Pointer and possibly Return Address).
The ORC unwinder is only supported on x86 at the moment and should thus be in the x86 architecture code. In order not to break the whole structure in case another architecture decides to support the ORC unwinder via objtool we choose to let the implementation be done in the architecture dependant code.
---
tools/objtool/Build | 1 -
tools/objtool/arch.h | 9 ++
tools/objtool/arch/x86/Build | 1 +
tools/objtool/arch/x86/decode.c | 106 ++++++++++++++++
tools/objtool/arch/x86/include/arch_special.h | 35 ++++++
tools/objtool/{ => arch/x86/include}/cfi.h | 0
tools/objtool/{ => arch/x86}/orc_gen.c | 10 +-
tools/objtool/check.c | 114 ++----------------
tools/objtool/check.h | 1 +
tools/objtool/orc.h | 4 +-
tools/objtool/special.c | 18 +--
11 files changed, 173 insertions(+), 126 deletions(-)
create mode 100644 tools/objtool/arch/x86/include/arch_special.h
rename tools/objtool/{ => arch/x86/include}/cfi.h (100%)
rename tools/objtool/{ => arch/x86}/orc_gen.c (96%)tools/objtool/Build +0 -1
@@ -2,7 +2,6 @@ objtool-y += arch/$(SRCARCH)/ objtool-y += builtin-check.o objtool-y += builtin-orc.o objtool-y += check.o -objtool-y += orc_gen.o objtool-y += orc_dump.o objtool-y += elf.o objtool-y += special.o
tools/objtool/arch.h +9 -0
@@ -22,6 +22,7 @@#include <linux/list.h> #include "elf.h" #include "cfi.h" +#include "orc.h" #define INSN_JUMP_CONDITIONAL 1#define INSN_JUMP_UNCONDITIONAL 2
@@ -70,6 +71,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,
@@ -79,4 +82,10 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,bool arch_callee_saved_reg (unsigned char reg); +int arch_orc_read_unwind_hints (struct objtool_file *file); + +unsigned long arch_compute_jump_destination (struct instruction *insn); + +unsigned long arch_compute_rela_sym_offset (int addend); +#endif /* _ARCH_H */
tools/objtool/arch/x86/Build +1 -0
@@ -1,4 +1,5 @@ objtool-y += decode.o +objtool-y += orc_gen.o inat_tables_script = arch/x86/tools/gen-insn-attr-x86.awk inat_tables_maps = arch/x86/lib/x86-opcode-map.txt
tools/objtool/arch/x86/decode.c +106 -0
121 lines not on this page · raw message
tools/objtool/arch/x86/include/arch_special.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#define EX_ENTRY_SIZE 12 +#define EX_ORIG_OFFSET 0 +#define EX_NEW_OFFSET 4 + +#define JUMP_ENTRY_SIZE 16 +#define JUMP_ORIG_OFFSET 0 +#define JUMP_NEW_OFFSET 4 + +#define ALT_ENTRY_SIZE 13 +#define ALT_ORIG_OFFSET 0 +#define ALT_NEW_OFFSET 4 +#define ALT_FEATURE_OFFSET 8 +#define ALT_ORIG_LEN_OFFSET 10 +#define ALT_NEW_LEN_OFFSET 11 + +#define IGNORE_SHF_EXEC_FLAG 0 + +#define JUMP_DYNAMIC_IS_SWITCH_TABLE 0 + +#define X86_FEATURE_POPCNT (4*32+23)
tools/objtool/arch/x86/include/cfi.h renamed from tools/objtool/cfi.h +0 -0
tools/objtool/arch/x86/orc_gen.c renamed from tools/objtool/orc_gen.c +5 -5
@@ -18,11 +18,11 @@#include <stdlib.h> #include <string.h> -#include "orc.h" -#include "check.h" -#include "warn.h" +#include "../../orc.h" +#include "../../check.h" +#include "../../warn.h" -int create_orc (struct objtool_file *file) +int arch_create_orc (struct objtool_file *file) {struct instruction *insn;
@@ -128,7 +128,7 @@ static int create_orc_entry(struct section *u_sec, struct section *ip_relasec,return 0 ; } -int create_orc_sections (struct objtool_file *file) +int arch_create_orc_sections (struct objtool_file *file) {struct instruction *insn, *prev_insn;struct section *sec, *u_sec, *ip_relasec;
tools/objtool/check.c +13 -101
158 lines not on this page · raw message
tools/objtool/check.h +1 -0
@@ -23,6 +23,7 @@#include "cfi.h" #include "arch.h" #include "orc.h" +#include "arch_special.h" #include <linux/hashtable.h> struct insn_state {
tools/objtool/orc.h +2 -2
@@ -22,8 +22,8 @@struct objtool_file ; -int create_orc (struct objtool_file *file); -int create_orc_sections (struct objtool_file *file); +int arch_create_orc (struct objtool_file *file); +int arch_create_orc_sections (struct objtool_file *file);int orc_dump (const char *objname);
tools/objtool/special.c +1 -17
@@ -25,23 +25,7 @@#include "special.h" #include "warn.h" - -#define EX_ENTRY_SIZE 12 -#define EX_ORIG_OFFSET 0 -#define EX_NEW_OFFSET 4 - -#define JUMP_ENTRY_SIZE 16 -#define JUMP_ORIG_OFFSET 0 -#define JUMP_NEW_OFFSET 4 - -#define ALT_ENTRY_SIZE 13 -#define ALT_ORIG_OFFSET 0 -#define ALT_NEW_OFFSET 4 -#define ALT_FEATURE_OFFSET 8 -#define ALT_ORIG_LEN_OFFSET 10 -#define ALT_NEW_LEN_OFFSET 11 - -#define X86_FEATURE_POPCNT (4*32+23) +#include "arch_special.h" struct special_entry {const char *sec;
Signature · 1 line
2.17.1
Re: [RFC 1/6] objtool: Refactor code to make it more suitable for multiple architecture support
Quoted 2 lines · Raphael Gault
The jump destination and relocation offset used previously are only reliable on
x86_64architecture. We abstract these computations by calling arch-dependant
"dependent"
Quoted 8 lines
implementation.
The control flow information and register macro definitions were based on the
x86_64architecture but should be abstract so that each architecture can define the correct values for the registers, especially the registers related to the stack frame (Frame Pointer, Stack Pointer and possibly Return Address).
Quoted 5 lines
The ORC unwinder is only supported on x86 at the moment and should thus be in the x86 architecture code. In order not to break the whole structure in case another architecture decides to support the ORC unwinder via objtool we choose to let the implementation be done in the architecture dependant code.
It's good practice to put each logical change into a separate patch. That will also make the patches easier to review.
For example, each of the above three paragraphs should be a separate commit.
Also it's a good idea to run the patches through checkpatch.pl (though its warnings should be taken with a grain of salt).
Quoted 23 lines
Signed-off-by Raphael Gault <[email protected]>--- tools/objtool/Build | 1 - tools/objtool/arch.h | 9 ++ tools/objtool/arch/x86/Build | 1 + tools/objtool/arch/x86/decode.c | 106 ++++++++++++++++ tools/objtool/arch/x86/include/arch_special.h | 35 ++++++ tools/objtool/{ => arch/x86/include}/cfi.h | 0 tools/objtool/{ => arch/x86}/orc_gen.c | 10 +- tools/objtool/check.c | 114 ++---------------- tools/objtool/check.h | 1 + tools/objtool/orc.h | 4 +- tools/objtool/special.c | 18 +-- 11 files changed, 173 insertions(+), 126 deletions(-) create mode 100644 tools/objtool/arch/x86/include/arch_special.h rename tools/objtool/{ => arch/x86/include}/cfi.h (100%) rename tools/objtool/{ => arch/x86}/orc_gen.c (96%)tools/objtool/Build +0
-0
tools/objtool/Build objtool-y += arch/$(SRCARCH)/ · line 5
Quoted 4 lines
tools/objtool/Build +0
-1@@ -2,7 +2,6 @@ objtool-y += arch/$(SRCARCH)/ objtool-y += builtin-check.o objtool-y += builtin-orc.o objtool-y += check.o -objtool-y += orc_gen.o
I'm not sure whether moving ORC out to the arch-specific code is the best option. I expect a lot of the ORC code to be generic. But this might be ok for now, until we get another ORC implementation.
Another possibility would be to make weak versions of the orc functions somewhere (check.c?) and only compile the generic orc_gen.c on arches which support it. Then we could abstract out the arch-specific ORC bits later.
tools/objtool/Build objtool-y += arch/$(SRCARCH)/ · line 5
Quoted 1 line
tools/objtool/Build +0
-0objtool-y += orc_dump.o
orc_dump.o doesn't need to be built on arm64. The "orc dump" option should fail accordingly.
Quoted 24 lines
tools/objtool/Build +0
-0objtool-y += elf.o objtool-y += special.otools/objtool/arch.h +3
-0@@ -22,6 +22,7 @@#include <linux/list.h> #include "elf.h" #include "cfi.h" +#include "orc.h" #define INSN_JUMP_CONDITIONAL 1#define INSN_JUMP_UNCONDITIONAL 2@@ -70,6 +71,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,
tools/objtool/arch.h int arch_decode_instruction(struct elf *elf, struct section *sec, · line 89
Quoted 8 lines
tools/objtool/arch.h +5
-0@@ -79,4 +82,10 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,bool arch_callee_saved_reg (unsigned char reg); +int arch_orc_read_unwind_hints (struct objtool_file *file); + +unsigned long arch_compute_jump_destination (struct instruction *insn); + +unsigned long arch_compute_rela_sym_offset (int addend);
arch_dest_rela_addend_offset() might be a more descriptive name. Also it might be simpler to just make it an arch-specific macro which is 0 on arm64 and 4 on x86.
"compute" is implied, it can probably be removed from the names to make them a little more concise.
Quoted 26 lines
tools/objtool/arch.h +1
-0+#endif /* _ARCH_H */ tools/objtool/arch/x86/Build +1
-0@@ -1,4 +1,5 @@ objtool-y += decode.o +objtool-y += orc_gen.o inat_tables_script = arch/x86/tools/gen-insn-attr-x86.awk inat_tables_maps = arch/x86/lib/x86-opcode-map.txttools/objtool/arch/x86/decode.c +2
-0@@ -23,6 +23,8 @@#include "lib/inat.c" #include "lib/insn.c" + +#include "../../check.h" #include "../../elf.h" #include "../../arch.h" #include "../../warn.h"
tools/objtool/arch/x86/decode.c bool arch_callee_saved_reg(unsigned char reg) · line 88
Quoted 9 lines
tools/objtool/arch/x86/decode.c +6
-0@@ -78,6 +80,105 @@ bool arch_callee_saved_reg(unsigned char reg) } } +unsigned long arch_compute_rela_sym_offset (int addend) +{ +return addend +4 ; +} + +int arch_orc_read_unwind_hints (struct objtool_file *file)
I think this function would be better suited for orc_gen.c (which could be renamed to just orc.c).
Quoted 10 lines
tools/objtool/arch/x86/include/arch_special.h new file +4
-0@@ -0,0 +1,35 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2
tools/objtool/arch/x86/include/arch_special.h line 14
Quoted 10 lines
tools/objtool/arch/x86/include/arch_special.h new file +10
-0+* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/
This needs the standard header macro guards, e.g.
#ifndef _X86_ARCH_SPECIAL_H
#define _X86_ARCH_SPECIAL_H
Quoted 9 lines
tools/objtool/arch/x86/include/arch_special.h new file +9
-0+ +#define EX_ENTRY_SIZE 12 +#define EX_ORIG_OFFSET 0 +#define EX_NEW_OFFSET 4 + +#define JUMP_ENTRY_SIZE 16 +#define JUMP_ORIG_OFFSET 0 +#define JUMP_NEW_OFFSET 4 +
tools/objtool/arch/x86/include/arch_special.h line 33
Quoted 10 lines
tools/objtool/arch/x86/include/arch_special.h new file +10
-0+#define ALT_ENTRY_SIZE 13 +#define ALT_ORIG_OFFSET 0 +#define ALT_NEW_OFFSET 4 +#define ALT_FEATURE_OFFSET 8 +#define ALT_ORIG_LEN_OFFSET 10 +#define ALT_NEW_LEN_OFFSET 11 + +#define IGNORE_SHF_EXEC_FLAG 0 + +#define JUMP_DYNAMIC_IS_SWITCH_TABLE 0
These flags should be added with the commit which actually uses them.
Also "arch_special.h" is specific to special section parsing, so I'm thinking these two macros don't really belong here. Or maybe the header file could be renamed to something more generic.
Signature · 1 line
Josh
Re: [RFC 1/6] objtool: Refactor code to make it more suitable for multiple architecture support
Quoted 14 lines · Josh Poimboeuf
Quoted 2 lines · Raphael Gault
The jump destination and relocation offset used previously are only reliable on
x86_64architecture. We abstract these computations by calling arch-dependant"dependent"
Quoted 8 lines
implementation.
The control flow information and register macro definitions were based on the
x86_64architecture but should be abstract so that each architecture can define the correct values for the registers, especially the registers related to the stack frame (Frame Pointer, Stack Pointer and possibly Return Address).
Quoted 9 lines
Quoted 5 lines
The ORC unwinder is only supported on x86 at the moment and should thus be in the x86 architecture code. In order not to break the whole structure in case another architecture decides to support the ORC unwinder via objtool we choose to let the implementation be done in the architecture dependant code.
It's good practice to put each logical change into a separate patch. That will also make the patches easier to review.
Indeed this will be better.
Quoted 6 lines
For example, each of the above three paragraphs should be a separate commit.
Also it's a good idea to run the patches through checkpatch.pl (though its warnings should be taken with a grain of salt).
Yes, I realised after the comment from Peter that I needed to correct quite a few things, thank you.
Quoted 79 lines
Quoted 23 lines
Signed-off-by Raphael Gault <[email protected]>--- tools/objtool/Build | 1 - tools/objtool/arch.h | 9 ++ tools/objtool/arch/x86/Build | 1 + tools/objtool/arch/x86/decode.c | 106 ++++++++++++++++ tools/objtool/arch/x86/include/arch_special.h | 35 ++++++ tools/objtool/{ => arch/x86/include}/cfi.h | 0 tools/objtool/{ => arch/x86}/orc_gen.c | 10 +- tools/objtool/check.c | 114 ++---------------- tools/objtool/check.h | 1 + tools/objtool/orc.h | 4 +- tools/objtool/special.c | 18 +-- 11 files changed, 173 insertions(+), 126 deletions(-)create mode 100644
tools/objtool/arch/x86/include/arch_special.hrenametools/objtool/{=>arch/x86/include}/cfi.h(100%) renametools/objtool/{=>arch/x86}/orc_gen.c(96%)tools/objtool/Build +0
-0tools/objtool/Build objtool-y += arch/$(SRCARCH)/ · line 5
Quoted 4 lines
tools/objtool/Build +0
-1@@ -2,7 +2,6 @@ objtool-y += arch/$(SRCARCH)/ objtool-y += builtin-check.o objtool-y += builtin-orc.o objtool-y += check.o -objtool-y += orc_gen.oI'm not sure whether moving ORC out to the arch-specific code is the best option. I expect a lot of the ORC code to be generic. But this might be ok for now, until we get another ORC implementation.
Another possibility would be to make weak versions of the orc functions somewhere (
check.c?) and only compile the genericorc_gen.con arches which support it. Then we could abstract out the arch-specific ORC bits later.tools/objtool/Build objtool-y += arch/$(SRCARCH)/ · line 5
Quoted 1 line
tools/objtool/Build +0
-0objtool-y += orc_dump.oorc_dump.o doesn't need to be built on arm64. The "orc dump" option should fail accordingly.
Quoted 24 lines
tools/objtool/Build +0
-0objtool-y += elf.o objtool-y += special.otools/objtool/arch.h +3
-0@@ -22,6 +22,7 @@#include <linux/list.h> #include "elf.h" #include "cfi.h" +#include "orc.h" #define INSN_JUMP_CONDITIONAL1 #define INSN_JUMP_UNCONDITIONAL2 @@ -70,6 +71,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,tools/objtool/arch.h int arch_decode_instruction(struct elf *elf, struct section *sec, · line 89
Quoted 8 lines
tools/objtool/arch.h +5
-0@@ -79,4 +82,10 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,bool arch_callee_saved_reg (unsigned char reg); +int arch_orc_read_unwind_hints (struct objtool_file *file); + +unsigned long arch_compute_jump_destination (struct instruction *insn); + +unsigned long arch_compute_rela_sym_offset (int addend);
arch_dest_rela_addend_offset()might be a more descriptive name. Also it might be simpler to just make it an arch-specific macro which is 0 on arm64 and 4 on x86.
Quoted 3 lines
"compute" is implied, it can probably be removed from the names to make them a little more concise.
I am more in favor of the functions, I have to admit I don't know which is really better but in any event I will rename them.
Quoted 29 lines
Quoted 29 lines
tools/objtool/arch.h +1
-0+#endif /* _ARCH_H */ tools/objtool/arch/x86/Build +1
-0@@ -1,4 +1,5 @@ objtool-y += decode.o +objtool-y += orc_gen.o inat_tables_script = arch/x86/tools/gen-insn-attr-x86.awk inat_tables_maps = arch/x86/lib/x86-opcode-map.txttools/objtool/arch/x86/decode.c +2
-0@@ -23,6 +23,8 @@#include "lib/inat.c" #include "lib/insn.c" + +#include "../../check.h" #include "../../elf.h" #include "../../arch.h" #include "../../warn.h" @@ -78,6 +80,105 @@ bool arch_callee_saved_reg(unsigned char reg) } }
Quoted 10 lines
tools/objtool/arch/x86/decode.c bool arch_callee_saved_reg(unsigned char reg) · line 88
Quoted 6 lines
tools/objtool/arch/x86/decode.c +6
-0+unsigned long arch_compute_rela_sym_offset (int addend) +{ +return addend +4 ; +} + +int arch_orc_read_unwind_hints (struct objtool_file *file)I think this function would be better suited for
orc_gen.c(which could be renamed to justorc.c).
That is indeed true. I did that for the arm64 side so for consistency it should be the same here.
Quoted 23 lines
Quoted 10 lines
tools/objtool/arch/x86/include/arch_special.h new file +4
-0@@ -0,0 +1,35 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 tools/objtool/arch/x86/include/arch_special.h line 14
Quoted 10 lines
tools/objtool/arch/x86/include/arch_special.h new file +10
-0+* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ This needs the standard header macro guards, e.g.
Quoted 3 lines
#ifndef
_X86_ARCH_SPECIAL_H
#define_X86_ARCH_SPECIAL_H
I will correct this! Thank you.
Quoted 22 lines
Quoted 9 lines
tools/objtool/arch/x86/include/arch_special.h new file +9
-0+ +#define EX_ENTRY_SIZE12 +#define EX_ORIG_OFFSET0 +#define EX_NEW_OFFSET4 + +#define JUMP_ENTRY_SIZE16 +#define JUMP_ORIG_OFFSET0 +#define JUMP_NEW_OFFSET4 +tools/objtool/arch/x86/include/arch_special.h line 33
Quoted 10 lines
tools/objtool/arch/x86/include/arch_special.h new file +10
-0+#define ALT_ENTRY_SIZE13 +#define ALT_ORIG_OFFSET0 +#define ALT_NEW_OFFSET4 +#define ALT_FEATURE_OFFSET8 +#define ALT_ORIG_LEN_OFFSET10 +#define ALT_NEW_LEN_OFFSET11 + +#define IGNORE_SHF_EXEC_FLAG0 + +#define JUMP_DYNAMIC_IS_SWITCH_TABLE0 These flags should be added with the commit which actually uses them.
Quoted 4 lines
Also "
arch_special.h" is specific to special section parsing, so I'm thinking these two macros don't really belong here. Or maybe the header file could be renamed to something more generic.
My approach was indeed wrong. In the next version I'll get rid of those macros which are inelegant and unrelated to x86.
Thanks,
--
Raphael Gault
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [RFC 1/6] objtool: Refactor code to make it more suitable for multiple architecture support
Quoted 11 lines · Raphael Gault
Quoted 7 lines · Josh Poimboeuf
arch_dest_rela_addend_offset()might be a more descriptive name. Also it might be simpler to just make it an arch-specific macro which is 0 on arm64 and 4 on x86."compute" is implied, it can probably be removed from the names to make them a little more concise.
I am more in favor of the functions, I have to admit I don't know which is really better but in any event I will rename them.
If you prefer functions, that's fine with me. I don't have a strong preference.
Signature · 1 line
Josh
RFC 2/6 objtool: arm64: Add required implementation for supporting the aarch64 architecture in objtool.
In reply to Raphael GaultProvide implementation for the arch-dependent functions that are called by the main check function of objtool.
The ORC unwinder is not yet supported by the arm64 architecture so we only provide a dummy interface for now.
The decoding of the instruction is split into classes and subclasses as described into the Instruction Encoding in the ArmV8.5 Architecture Reference Manual.
--- tools/objtool/arch/arm64/Build | 6 + tools/objtool/arch/arm64/bit_operations.c | 65 + tools/objtool/arch/arm64/decode.c | 2843 +++++++++++++++++ .../objtool/arch/arm64/include/arch_special.h | 44 + .../arch/arm64/include/asm/orc_types.h | 109 + .../arch/arm64/include/bit_operations.h | 22 + tools/objtool/arch/arm64/include/cfi.h | 76 + .../objtool/arch/arm64/include/insn_decode.h | 219 ++ tools/objtool/arch/arm64/orc_gen.c | 40 + 9 files changed, 3424 insertions(+) create mode 100644 tools/objtool/arch/arm64/Build create mode 100644 tools/objtool/arch/arm64/bit_operations.c create mode 100644 tools/objtool/arch/arm64/decode.c create mode 100644 tools/objtool/arch/arm64/include/arch_special.h create mode 100644 tools/objtool/arch/arm64/include/asm/orc_types.h create mode 100644 tools/objtool/arch/arm64/include/bit_operations.h create mode 100644 tools/objtool/arch/arm64/include/cfi.h create mode 100644 tools/objtool/arch/arm64/include/insn_decode.h create mode 100644 tools/objtool/arch/arm64/orc_gen.c
tools/objtool/arch/arm64/Build new file +6 -0
@@ -0,0 +1,6 @@ +objtool-y += decode.o +objtool-y += orc_gen.o +objtool-y += bit_operations.o + + +CFLAGS_decode.o += -I$(OUTPUT)arch/arm64/lib
tools/objtool/arch/arm64/bit_operations.c new file +65 -0
65 lines not on this page · raw message
tools/objtool/arch/arm64/decode.c new file +2843 -0
2,843 lines not on this page · raw message
tools/objtool/arch/arm64/include/arch_special.h new file +44 -0
44 lines not on this page · raw message
tools/objtool/arch/arm64/include/asm/orc_types.h new file +109 -0
42 lines not on this page · raw message
tools/objtool/arch/arm64/include/bit_operations.h new file +22 -0
Lines of this file are not shown. View the raw message.
tools/objtool/arch/arm64/include/cfi.h new file +76 -0
76 lines not on this page · raw message
tools/objtool/arch/arm64/include/insn_decode.h new file +219 -0
219 lines not on this page · raw message
tools/objtool/arch/arm64/orc_gen.c new file +40 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.17.1
Part of the diff is not shown here. View the raw message.
Peter Zijlstra
maintainerRe: [RFC 2/6] objtool: arm64: Add required implementation for supporting the aarch64 architecture in objtool.
Quoted 4 lines · Raphael Gault
The decoding of the instruction is split into classes and subclasses as described into the Instruction Encoding in the ArmV8.5 Architecture Reference Manual.
tools/objtool/arch/arm64/decode.c | 2843 +++++++++++++++++
Oh man, I hope you generated much of that from the ARM64 Instruction Set Architecture XML files [*]. Otherwise that's been a lot of typing.
Anyway, since you now have that, would it make sense to use this same decoder in your kernel tree? I found at least one partial decoder in arch/arm64/kernel/probes/decode-insn.c, but I suspect you have more.
(Note kprobes is how x86 initially grew its instruction decoder IIRC)
* https://developer.arm.com/architectures/cpu-architecture/a-profile/exploration-tools
Re: [RFC 2/6] objtool: arm64: Add required implementation for supporting the aarch64 architecture in objtool.
In reply to Raphael GaultQuoted 6 lines · Raphael Gault
Provide implementation for the arch-dependent functions that are called by the main check function of objtool.
The ORC unwinder is not yet supported by the arm64 architecture so we only provide a dummy interface for now.
The decoding of the instruction is split into classes and subclasses as described into the Instruction Encoding in the ArmV8.5 Architecture Reference Manual.
Where did the code for the decoder come from? Was it written from scratch?
Quoted 11 lines
tools/objtool/arch/arm64/include/arch_special.h new file +5
-0@@ -0,0 +1,44 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version.
tools/objtool/arch/arm64/include/arch_special.h line 15
Quoted 10 lines
tools/objtool/arch/arm64/include/arch_special.h new file +10
-0+* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ +
Needs a header guard.
Quoted 17 lines
tools/objtool/arch/arm64/include/arch_special.h new file +17
-0+#define EX_ENTRY_SIZE 8 +#define EX_ORIG_OFFSET 0 +#define EX_NEW_OFFSET 4 + +#define JUMP_ENTRY_SIZE 16 +#define JUMP_ORIG_OFFSET 0 +#define JUMP_NEW_OFFSET 4 + +#define ALT_ENTRY_SIZE 12 +#define ALT_ORIG_OFFSET 0 +#define ALT_NEW_OFFSET 4 +#define ALT_FEATURE_OFFSET 8 +#define ALT_ORIG_LEN_OFFSET 10 +#define ALT_NEW_LEN_OFFSET 11 + +/* +* On arm64 the .altinstr_replacement is not always marked
tools/objtool/arch/arm64/include/arch_special.h line 42
Quoted 10 lines
tools/objtool/arch/arm64/include/arch_special.h new file +10
-0+* as containing executable instruction. But we still want +* to process it so we ignore the SHF_EXEC flag +*/ +#define IGNORE_SHF_EXEC_FLAG 1 + +/* +* The jump table detection is not the same on arm64 so for +* now we just detect if it is a dynamic jump (br <Xn> insn) +*/ +#define JUMP_DYNAMIC_IS_SWITCH_TABLE 1
Same as for x86, these flags should be added in the same patch which uses them.
Quoted 107 lines
tools/objtool/arch/arm64/include/arch_special.h new file +2
-0+ +#define X86_FEATURE_POPCNT (4*32+23)tools/objtool/arch/arm64/include/asm/orc_types.h new file +99
-0@@ -0,0 +1,109 @@ +/* +* Copyright (C) 2017 Josh Poimboeuf <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _ORC_TYPES_H +#define _ORC_TYPES_H + +#include <linux/types.h> +#include <linux/compiler.h> + +/* +* The ORC_REG_* registers are base registers which are used to find other +* registers on the stack. +* +* ORC_REG_PREV_SP, also known as DWARF Call Frame Address (CFA), is the +* address of the previous frame: the caller's SP before it called the current +* function. +* +* ORC_REG_UNDEFINED means the corresponding register's value didn't change in +* the current frame. +* +* The most commonly used base registers are SP and BP -- which the previous SP +* is usually based on -- and PREV_SP and UNDEFINED -- which the previous BP is +* usually based on. +* +* The rest of the base registers are needed for special cases like entry code +* and GCC realigned stacks. +*/ +#define ORC_REG_UNDEFINED 0 +#define ORC_REG_PREV_SP 1 +#define ORC_REG_DX 2 +#define ORC_REG_DI 3 +#define ORC_REG_BP 4 +#define ORC_REG_SP 5 +#define ORC_REG_R10 6 +#define ORC_REG_R13 7 +#define ORC_REG_BP_INDIRECT 8 +#define ORC_REG_SP_INDIRECT 9 +#define ORC_REG_MAX 15 + +/* +* ORC_TYPE_CALL: Indicates that sp_reg+sp_offset resolves to PREV_SP (the +* caller's SP right before it made the call). Used for all callable +* functions, i.e. all C code and all callable asm functions. +* +* ORC_TYPE_REGS: Used in entry code to indicate that sp_reg+sp_offset points +* to a fully populated pt_regs from a syscall, interrupt, or exception. +* +* ORC_TYPE_REGS_IRET: Used in entry code to indicate that sp_reg+sp_offset +* points to the iret return frame. +* +* The UNWIND_HINT macros are used only for the unwind_hint struct. They +* aren't used in struct orc_entry due to size and complexity constraints. +* Objtool converts them to real types when it converts the hints to orc +* entries. +*/ +#define ORC_TYPE_CALL 0 +#define ORC_TYPE_REGS 1 +#define ORC_TYPE_REGS_IRET 2 +#define UNWIND_HINT_TYPE_SAVE 3 +#define UNWIND_HINT_TYPE_RESTORE 4 + +#ifndef __ASSEMBLY__ +/* +* This struct is more or less a vastly simplified version of the DWARF Call +* Frame Information standard. It contains only the necessary parts of DWARF +* CFI, simplified for ease of access by the in-kernel unwinder. It tells the +* unwinder how to find the previous SP and BP (and sometimes entry regs) on +* the stack for a given code address. Each instance of the struct corresponds +* to one or more code locations. +*/ +struct orc_entry { +s16 sp_offset; +s16 bp_offset; +unsigned sp_reg:4 ; +unsigned bp_reg:4 ; +unsigned type:2 ; +unsigned end:1 ; +} __packed; + +/* +* This struct is used by asm and inline asm code to manually annotate the +* location of registers on the stack for the ORC unwinder. +* +* Type can be either ORC_TYPE_* or UNWIND_HINT_TYPE_*. +*/
tools/objtool/arch/arm64/include/asm/orc_types.h line 109
Quoted 10 lines
tools/objtool/arch/arm64/include/asm/orc_types.h new file +10
-0+struct unwind_hint { +u32 ip; +s16 sp_offset; +u8 sp_reg; +u8 type; +u8 end; +}; +#endif /* __ASSEMBLY__ */ + +#endif /* _ORC_TYPES_H */
It seems odd to have the above header file in arm64 code, since it doesn't implement ORC. Is it really needed?
Signature · 1 line
Josh
Re: [RFC 2/6] objtool: arm64: Add required implementation for supporting the aarch64 architecture in objtool.
Quoted 11 lines · Josh Poimboeuf
Quoted 6 lines · Raphael Gault
Provide implementation for the arch-dependent functions that are called by the main check function of objtool.
The ORC unwinder is not yet supported by the arm64 architecture so we only provide a dummy interface for now.
The decoding of the instruction is split into classes and subclasses as described into the Instruction Encoding in the ArmV8.5 Architecture Reference Manual.Where did the code for the decoder come from? Was it written from scratch?
This decoder was indeed written from scratch based on the armv8 ARM. The automatic generation idea hasn't really been discussed yet.
Quoted 46 lines
Quoted 11 lines
tools/objtool/arch/arm64/include/arch_special.h new file +5
-0@@ -0,0 +1,44 @@ +/* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. tools/objtool/arch/arm64/include/arch_special.h line 15
Quoted 10 lines
tools/objtool/arch/arm64/include/arch_special.h new file +10
-0+* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ +Needs a header guard.
Quoted 22 lines
tools/objtool/arch/arm64/include/arch_special.h new file +22
-0+#define EX_ENTRY_SIZE8 +#define EX_ORIG_OFFSET0 +#define EX_NEW_OFFSET4 + +#define JUMP_ENTRY_SIZE16 +#define JUMP_ORIG_OFFSET0 +#define JUMP_NEW_OFFSET4 + +#define ALT_ENTRY_SIZE12 +#define ALT_ORIG_OFFSET0 +#define ALT_NEW_OFFSET4 +#define ALT_FEATURE_OFFSET8 +#define ALT_ORIG_LEN_OFFSET10 +#define ALT_NEW_LEN_OFFSET11 + +/* +* On arm64 the .altinstr_replacement is not always marked +* as containing executable instruction. But we still want +* to process it so we ignore the SHF_EXEC flag +*/ +#define IGNORE_SHF_EXEC_FLAG1 +
Quoted 9 lines
tools/objtool/arch/arm64/include/arch_special.h
Quoted 5 lines
tools/objtool/arch/arm64/include/arch_special.h new file +5
-0+/* +* The jump table detection is not the same on arm64 so for +* now we just detect if it is a dynamic jump (br <Xn> insn) +*/ +#define JUMP_DYNAMIC_IS_SWITCH_TABLE1 Same as for x86, these flags should be added in the same patch which uses them.
This will disappear in the next version, thanks.
Quoted 112 lines
Quoted 112 lines
tools/objtool/arch/arm64/include/arch_special.h new file +2
-0+ +#define X86_FEATURE_POPCNT (4*32+23)tools/objtool/arch/arm64/include/asm/orc_types.h new file +104
-0@@ -0,0 +1,109 @@ +/* +* Copyright (C) 2017 Josh Poimboeuf <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _ORC_TYPES_H +#define _ORC_TYPES_H + +#include <linux/types.h> +#include <linux/compiler.h> + +/* + * The ORC_REG_* registers are base registers which are used to find other + * registers on the stack. + * + * ORC_REG_PREV_SP, also known as DWARF Call Frame Address (CFA), is the + * address of the previous frame: the caller's SP before it called the current + * function. + * + * ORC_REG_UNDEFINED means the corresponding register's value didn't change in + * the current frame. + * + * The most commonly used base registers are SP and BP -- which the previous SP + * is usually based on -- and PREV_SP and UNDEFINED -- which the previous BP is + * usually based on. + * + * The rest of the base registers are needed for special cases like entry code + * and GCC realigned stacks. + */ +#define ORC_REG_UNDEFINED0 +#define ORC_REG_PREV_SP1 +#define ORC_REG_DX2 +#define ORC_REG_DI3 +#define ORC_REG_BP4 +#define ORC_REG_SP5 +#define ORC_REG_R106 +#define ORC_REG_R137 +#define ORC_REG_BP_INDIRECT8 +#define ORC_REG_SP_INDIRECT9 +#define ORC_REG_MAX15 + +/* + * ORC_TYPE_CALL: Indicates that sp_reg+sp_offset resolves to PREV_SP (the + * caller's SP right before it made the call). Used for all callable + * functions, i.e. all C code and all callable asm functions. + * + * ORC_TYPE_REGS: Used in entry code to indicate that sp_reg+sp_offset points + * to a fully populated pt_regs from a syscall, interrupt, or exception. + * + * ORC_TYPE_REGS_IRET: Used in entry code to indicate that sp_reg+sp_offset + * points to the iret return frame. + * + * The UNWIND_HINT macros are used only for the unwind_hint struct. They + * aren't used in struct orc_entry due to size and complexity constraints. + * Objtool converts them to real types when it converts the hints to orc + * entries. + */ +#define ORC_TYPE_CALL0 +#define ORC_TYPE_REGS1 +#define ORC_TYPE_REGS_IRET2 +#define UNWIND_HINT_TYPE_SAVE3 +#define UNWIND_HINT_TYPE_RESTORE4 + +#ifndef __ASSEMBLY__ +/* + * This struct is more or less a vastly simplified version of the DWARF Call + * Frame Information standard. It contains only the necessary parts of DWARF + * CFI, simplified for ease of access by the in-kernel unwinder. It tells the + * unwinder how to find the previous SP and BP (and sometimes entry regs) on + * the stack for a given code address. Each instance of the struct corresponds + * to one or more code locations. + */ +struct orc_entry { +s16sp_offset; +s16bp_offset; +unsignedsp_reg:4; +unsignedbp_reg:4; +unsignedtype:2; +unsignedend:1; +} __packed; + +/* + * This struct is used by asm and inline asm code to manually annotate the + * location of registers on the stack for the ORC unwinder. + * + * Type can be either ORC_TYPE_* or UNWIND_HINT_TYPE_*. + */ +struct unwind_hint { +u32ip; +s16sp_offset; +u8sp_reg; +u8type;
Quoted 9 lines
tools/objtool/arch/arm64/include/asm/orc_types.h line 109
Quoted 5 lines
tools/objtool/arch/arm64/include/asm/orc_types.h new file +5
-0+u8end; +}; +#endif /* __ASSEMBLY__ */ + +#endif /* _ORC_TYPES_H */ It seems odd to have the above header file in arm64 code, since it doesn't implement ORC. Is it really needed?
The unwind_hint part can safely be removed. However the orc_entry seems to be needed since the struct instruction comports a struct orc_entry field. I have chosen to let it here as well but maybe a better approach is possible.
Cheers,
--
Raphael Gault
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [RFC 2/6] objtool: arm64: Add required implementation for supporting the aarch64 architecture in objtool.
Quoted 9 lines · Raphael Gault
Quoted 8 lines · Josh Poimboeuf
Quoted 6 lines · Raphael Gault
Provide implementation for the arch-dependent functions that are called by the main check function of objtool.
The ORC unwinder is not yet supported by the arm64 architecture so we only provide a dummy interface for now.
The decoding of the instruction is split into classes and subclasses as described into the Instruction Encoding in the ArmV8.5 Architecture Reference Manual.
Quoted 6 lines
Quoted 3 lines
Where did the code for the decoder come from? Was it written from scratch?
This decoder was indeed written from scratch based on the armv8 ARM. The automatic generation idea hasn't really been discussed yet.
Ok. That's a lot of code. Hopefully ARM folks can review it closely.
Quoted 39 lines
Quoted 29 lines
Quoted 29 lines
tools/objtool/arch/arm64/include/asm/orc_types.h new file +29
-0+#ifndef __ASSEMBLY__ +/* + * This struct is more or less a vastly simplified version of the DWARF Call + * Frame Information standard. It contains only the necessary parts of DWARF + * CFI, simplified for ease of access by the in-kernel unwinder. It tells the + * unwinder how to find the previous SP and BP (and sometimes entry regs) on + * the stack for a given code address. Each instance of the struct corresponds + * to one or more code locations. + */ +struct orc_entry { +s16sp_offset; +s16bp_offset; +unsignedsp_reg:4; +unsignedbp_reg:4; +unsignedtype:2; +unsignedend:1; +} __packed; + +/* + * This struct is used by asm and inline asm code to manually annotate the + * location of registers on the stack for the ORC unwinder. + * + * Type can be either ORC_TYPE_* or UNWIND_HINT_TYPE_*. + */ +struct unwind_hint { +u32ip; +s16sp_offset; +u8sp_reg; +u8type;Quoted 9 lines
tools/objtool/arch/arm64/include/asm/orc_types.h line 109
Quoted 5 lines
tools/objtool/arch/arm64/include/asm/orc_types.h new file +5
-0+u8end; +}; +#endif /* __ASSEMBLY__ */ + +#endif /* _ORC_TYPES_H */ It seems odd to have the above header file in arm64 code, since it doesn't implement ORC. Is it really needed?
Quoted 4 lines
The
unwind_hintpart can safely be removed. However theorc_entryseems to be needed since the struct instruction comports a structorc_entryfield. I have chosen to let it here as well but maybe a better approach is possible.
Ideally we can figure out a way to decouple 'struct instruction' from 'struct orc_entry'. But yes, I think your approach is fine for now.
Though I think using an arch-independent header file would be better, to avoid creating duplicated (dead) code.
Signature · 1 line
Josh
RFC 3/6 objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
In reply to Raphael GaultSince the way the initial stack frame when entering a function is different that what is done in the x86_64 architecture, we need to add some more check to support the different cases. As opposed as for x86_64, the return address is not stored by the call instruction but is instead loaded in a register. The initial stack frame is thus empty when entering a function and 2 push operations are needed to set it up correctly. All the different combinations need to be taken into account.
On arm64, the .altinstr_replacement section is not flagged as containing executable instructions but we still need to process it.
Switch tables are alse stored in a different way on arm64 than on x86_64 so we need to be able to identify in which case we are when looking for it.
--- tools/objtool/arch.h | 2 + tools/objtool/arch/arm64/decode.c | 27 +++++++++ tools/objtool/arch/x86/decode.c | 5 ++ tools/objtool/check.c | 95 +++++++++++++++++++++++++++---- tools/objtool/elf.c | 3 +- 5 files changed, 120 insertions(+), 12 deletions(-)
tools/objtool/arch.h +2 -0
@@ -88,4 +88,6 @@ unsigned long arch_compute_jump_destination(struct instruction *insn);unsigned long arch_compute_rela_sym_offset (int addend); +bool arch_is_insn_sibling_call (struct instruction *insn); +#endif /* _ARCH_H */
tools/objtool/arch/arm64/decode.c +27 -0
@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev (l, &insn->list) { + prev = (void *)l; +if (!prev->func + || prev->func->pfunc != insn->func->pfunc) +return false; +if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; + sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC + || sym->pfunc != insn->func->pfunc) +return true; +break ; + } +return true; +}static int is_arm64 (struct elf *elf) {switch (elf->ehdr.e_machine){
tools/objtool/arch/x86/decode.c +5 -0
@@ -85,6 +85,11 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend +4 ; } +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +return true; +} +int arch_orc_read_unwind_hints (struct objtool_file *file) {struct section *sec, *relasec;
tools/objtool/check.c +84 -11
174 lines not on this page · raw message
tools/objtool/elf.c +2 -1
@@ -74,7 +74,8 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)struct symbol *sym;list_for_each_entry (sym, &sec->symbol_list, list) -if (sym->type !=STT_SECTION && +if (sym->type !=STT_NOTYPE && + sym->type !=STT_SECTION && sym->offset == offset)return sym;
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
I'm just doing my initial read-through,.. however
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 270
Quoted 3 lines · Raphael Gault
tools/objtool/check.c +2
-0+if (!(sec->sh.sh_flags &SHF_EXECINSTR ) + && (strcmp (sec->name,".altinstr_replacement" ) || !IGNORE_SHF_EXEC_FLAG ))continue ;
could you please not format code like that. Operators go at the end of the line, and continuation should match the indentation of the opening paren. So the above would look like:
Review
Quoted 3 lines
(quoted diff) +2
-0+ if (!(sec->sh.sh_flags & SHF_EXECINSTR) && + (strcmp(sec->name, ".altinstr_replacement") || !IGNORE_SHF_EXEC_FLAG)) continue;
You appear to be doing that quit consistently, and it is against style.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Mark Rutland
reviewerRe: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Quoted 12 lines · Peter Zijlstra
I'm just doing my initial read-through,.. however
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 270
Quoted 3 lines · Raphael Gault
tools/objtool/check.c +2
-0+if (!(sec->sh.sh_flags &SHF_EXECINSTR ) + && (strcmp (sec->name,".altinstr_replacement" ) || !IGNORE_SHF_EXEC_FLAG ))continue ;could you please not format code like that. Operators go at the end of the line, and continuation should match the indentation of the opening paren. So the above would look like:
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ if (!(sec->sh.sh_flags & SHF_EXECINSTR) && + (strcmp(sec->name, ".altinstr_replacement") || !IGNORE_SHF_EXEC_FLAG)) continue;You appear to be doing that quit consistently, and it is against style.
Raphael, as a heads-up, ./scripts/checkpatch.pl can catch issues like this. You can run it over a list of patches, so for a patch series you can run:
$ ./scripts/checkpatch.pl *.patch
... and hopefully most of the output will be reasonable.
Thanks,
Mark.
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Quoted 19 lines · Mark Rutland
Quoted 12 lines · Peter Zijlstra
I'm just doing my initial read-through,.. however
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 270
Quoted 3 lines · Raphael Gault
tools/objtool/check.c +2
-0+if (!(sec->sh.sh_flags &SHF_EXECINSTR ) + && (strcmp (sec->name,".altinstr_replacement" ) || !IGNORE_SHF_EXEC_FLAG ))continue ;could you please not format code like that. Operators go at the end of the line, and continuation should match the indentation of the opening paren. So the above would look like:
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ if (!(sec->sh.sh_flags & SHF_EXECINSTR) && + (strcmp(sec->name, ".altinstr_replacement") || !IGNORE_SHF_EXEC_FLAG)) continue;You appear to be doing that quit consistently, and it is against style.
Quoted 8 lines
Raphael, as a heads-up,
./scripts/checkpatch.plcan catch issues like this. You can run it over a list of patches, so for a patch series you can run:$ ./scripts/checkpatch.pl *.patch... and hopefully most of the output will be reasonable.
For this particular case, checkpatch only warns about it if you pass it "--strict" option. So in general it might be useful to include this option at least for the first pass at including large pieces of code.
Cheers,
Signature · 1 line
Julien Thierry
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Hi,
Quoted 16 lines · Julien Thierry
Quoted 13 lines · Mark Rutland
Quoted 12 lines · Peter Zijlstra
I'm just doing my initial read-through,.. however
Quoted 3 lines · Raphael Gault
+if (!(sec->sh.sh_flags &
SHF_EXECINSTR)
+&& (strcmp(sec->name, ".altinstr_replacement")|| !IGNORE_SHF_EXEC_FLAG))
continue;could you please not format code like that. Operators go at the end of the line, and continuation should match the indentation of the opening paren. So the above would look like:
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+if (!(sec->sh.sh_flags & SHF_EXECINSTR) && + (strcmp(sec->name, ".altinstr_replacement") || !IGNORE_SHF_EXEC_FLAG)) continue;You appear to be doing that quit consistently, and it is against style.
Thank you for these remarks, I will correct this!
Quoted 5 lines
Quoted 5 lines
Raphael, as a heads-up,
./scripts/checkpatch.plcan catch issues like this. You can run it over a list of patches, so for a patch series you can run:
Quoted 9 lines
Quoted 4 lines
$ ./scripts/checkpatch.pl *.patch... and hopefully most of the output will be reasonable.
For this particular case, checkpatch only warns about it if you pass it "--strict" option. So in general it might be useful to include this option at least for the first pass at including large pieces of code.
Indeed that sounds usefull, thanks,
Quoted 2 lines
Cheers,
Cheers,
--
Raphael Gault
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
In reply to Raphael GaultQuoted 58 lines · Raphael Gault
Since the way the initial stack frame when entering a function is different that what is done in the
x86_64architecture, we need to add some more check to support the different cases. As opposed as forx86_64, the return address is not stored by the call instruction but is instead loaded in a register. The initial stack frame is thus empty when entering a function and 2 push operations are needed to set it up correctly. All the different combinations need to be taken into account.On arm64, the .altinstr_replacement section is not flagged as containing executable instructions but we still need to process it.
Switch tables are alse stored in a different way on arm64 than on
x86_64so we need to be able to identify in which case we are when looking for it.Signed-off-by Raphael Gault <[email protected]>--- tools/objtool/arch.h | 2 + tools/objtool/arch/arm64/decode.c | 27 +++++++++ tools/objtool/arch/x86/decode.c | 5 ++ tools/objtool/check.c | 95 +++++++++++++++++++++++++++---- tools/objtool/elf.c | 3 +- 5 files changed, 120 insertions(+), 12 deletions(-)tools/objtool/arch.h +2
-0@@ -88,4 +88,6 @@ unsigned long arch_compute_jump_destination(struct instruction *insn);unsigned long arch_compute_rela_sym_offset (int addend); +bool arch_is_insn_sibling_call (struct instruction *insn); +#endif /* _ARCH_H */ tools/objtool/arch/arm64/decode.c +17
-0@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev (l, &insn->list) { + prev = (void *)l; +if (!prev->func + || prev->func->pfunc != insn->func->pfunc) +return false;
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 134
Quoted 10 lines
tools/objtool/arch/arm64/decode.c +10
-0+if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; + sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC + || sym->pfunc != insn->func->pfunc) +return true; +break ; + } +return true; +}
I get the feeling there might be a better way to do this, but I can't figure out what this function is actually doing. It looks like it searches backwards in the function for an instruction which has stack_op.src.reg != ADR_SOURCE -- what does that mean? And why doesn't it do anything with the instruction after it finds it?
Quoted 8 lines
tools/objtool/arch/arm64/decode.c +0
-0static int is_arm64 (struct elf *elf) {switch (elf->ehdr.e_machine){tools/objtool/arch/x86/decode.c +0
-0
tools/objtool/arch/x86/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 91
Quoted 7 lines
tools/objtool/arch/x86/decode.c +4
-0@@ -85,6 +85,11 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend +4 ; } +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +return true; +}
All x86 instructions are sibling calls?
Quoted 10 lines
tools/objtool/arch/x86/decode.c +1
-0+int arch_orc_read_unwind_hints (struct objtool_file *file) {struct section *sec, *relasec;tools/objtool/check.c +0
-0@@ -261,10 +261,12 @@ static int decode_instructions(struct objtool_file *file)unsigned long offset;
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 270
Quoted 10 lines
tools/objtool/check.c +3
-1struct instruction *insn;int ret; +static int composed_insn =0 ;for_each_sec (file, sec) { -if (!(sec->sh.sh_flags &SHF_EXECINSTR )) +if (!(sec->sh.sh_flags &SHF_EXECINSTR ) + && (strcmp (sec->name,".altinstr_replacement" ) || !IGNORE_SHF_EXEC_FLAG ))continue ;
We should just fix the root cause instead, presumably:
arch/arm64/include/asm/alternative.h +1 -1
@@ -71,7 +71,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ALTINSTR_ENTRY(feature,cb) \".popsection\n" \ " .if " __stringify(cb) " == 0\n" \ -".pushsection .altinstr_replacement, \"a\"\n" \ +".pushsection .altinstr_replacement, \"ax\"\n" \"663:\n\t" \ newinstr"\n" \"664:\n\t" \
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 303
Quoted 9 lines
tools/objtool/check.c +2
-1if (strcmp(sec->name, ".altinstr_replacement") &&@@ -297,10 +299,22 @@ static int decode_instructions(struct objtool_file *file)WARN_FUNC ("invalid instruction type %d" , insn->sec, insn->offset, insn->type); ret =-1 ; - goto err; +free (insn); +continue ;
What's the purpose of this change? If it's really needed then it looks like it should be a separate patch.
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 309
Quoted 8 lines
tools/objtool/check.c +5
-2} - -hash_add (file->insn_hash, &insn->hash, insn->offset); +/* +* For arm64 architecture, we sometime split instructions so that +* we can track the state evolution (i.e. load/store of pairs of registers). +* We thus need to take both into account and not erase the previous ones. +*/
Ew... Is this an architectural thing, or just a quirk of the arm64 decoder?
Quoted 25 lines
tools/objtool/check.c +10
-2+if (composed_insn >0 ) +hash_add (file->insn_hash, &insn->hash, insn->offset + composed_insn); +else +hash_add (file->insn_hash, &insn->hash, insn->offset); +if (insn->len ==0 ) + composed_insn++; +else + composed_insn =0 ;list_add_tail (&insn->list, &file->insn_list); }@@ -510,10 +524,10 @@ static int add_jump_destinations(struct objtool_file *file) dest_off =arch_compute_jump_destination (insn); }else if (rela->sym->type ==STT_SECTION ) { dest_sec = rela->sym->sec; - dest_off = rela->addend +4 ; + dest_off =arch_compute_rela_sym_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_compute_rela_sym_offset (rela->addend); }else if (strstr (rela->sym->name,"_indirect_thunk_" )) {/* * Retpoline jumps are really dynamic jumps in
tools/objtool/check.c static int handle_group_alt(struct objtool_file *file, · line 680
Quoted 5 lines
tools/objtool/check.c +1
-1@@ -663,7 +677,7 @@ static int handle_group_alt(struct objtool_file *file, last_orig_insn = insn; } -if (next_insn_same_sec (file, last_orig_insn)) { +if (last_orig_insn &&next_insn_same_sec (file, last_orig_insn)) {
This might belong in a separate patch which explains the reason for the change.
tools/objtool/check.c static struct rela *find_switch_table(struct objtool_file *file, · line 994
Quoted 9 lines
tools/objtool/check.c +2
-0fake_jump =malloc (sizeof (*fake_jump));if (!fake_jump) {WARN ("malloc failed" );@@ -976,6 +990,17 @@ static struct rela *find_switch_table(struct objtool_file *file,if (find_symbol_containing (rodata_sec, table_offset))continue ; +/* +* If we are on arm64 architecture, we now that we
"know"
tools/objtool/check.c static struct rela *find_switch_table(struct objtool_file *file, · line 1003
Quoted 9 lines
tools/objtool/check.c +9
-0+* are in presence of a switch table thanks to +* the `br <Xn>` insn. but we can't retrieve it yet. +* So we just ignore unreachable for this file. +*/ +if (JUMP_DYNAMIC_IS_SWITCH_TABLE ) { + file->ignore_unreachables = true; +return NULL ; + } +
I think this means switch table reading is not yet supported? If so then maybe the flag should be called SWITCH_TABLE_NOT_SUPPORTED.
But really this needs to be fixed anyway before merging the code.
Quoted 86 lines
tools/objtool/check.c +43
-3rodata_rela =find_rela_by_dest (rodata_sec, table_offset);if (rodata_rela) {/* @@ -1258,8 +1283,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; }/* @@ -1415,8 +1440,33 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)/* add imm, %rsp */ state->stack_size -= op->src.offset; -if (cfa->base ==CFI_SP ) +if (cfa->base ==CFI_SP ) { cfa->offset -= op->src.offset; +if (state->stack_size ==0 + && initial_func_cfi.cfa.base ==CFI_CFA ) { + cfa->base =CFI_CFA ; + cfa->offset =0 ; + } + } +/* +* on arm64 the save/restore of sp into fp is not automatic +* and the first one can be done without the other so we +* need to be careful not to invalidate the stack frame in such +* cases. +*/ +else if (cfa->base ==CFI_BP ) { +if (state->stack_size ==0 + && initial_func_cfi.cfa.base ==CFI_CFA ) { + cfa->base =CFI_CFA ; + cfa->offset =0 ; +restore_reg (state,CFI_BP ); + } + } +else if (cfa->base ==CFI_CFA ) { + cfa->base =CFI_SP ; +if (state->stack_size >=16 ) + cfa->offset =16 ; + }break ; }@@ -1427,6 +1477,16 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)break ; } +if (op->src.reg ==CFI_SP && op->dest.reg ==CFI_BP && + cfa->base ==CFI_SP && + regs[CFI_BP ].base ==CFI_CFA && + regs[CFI_BP ].offset == -cfa->offset) { + +/* mov %rsp, %rbp */ + cfa->base = op->dest.reg; + state->bp_scratch = false; +break ; + }if (op->src.reg ==CFI_SP && cfa->base ==CFI_SP ) {/* drap: lea disp(%rsp), %drap */ @@ -1518,6 +1578,9 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state) state->stack_size -=8 ;if (cfa->base ==CFI_SP ) cfa->offset -=8 ; +if (cfa->base ==CFI_SP && cfa->offset ==0 + && initial_func_cfi.cfa.base ==CFI_CFA ) + cfa->base =CFI_CFA ;break ;@@ -1557,6 +1620,8 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)case OP_DEST_PUSH : state->stack_size +=8 ; +if (cfa->base ==CFI_CFA ) + cfa->base =CFI_SP ;if (cfa->base ==CFI_SP ) cfa->offset +=8 ;
tools/objtool/check.c static int validate_branch(struct objtool_file *file, struct instruction *first, · line 1796
Quoted 5 lines
tools/objtool/check.c +1
-1@@ -1728,7 +1793,7 @@ static int validate_branch(struct objtool_file *file, struct instruction *first, insn = first; sec = insn->sec; -if (insn->alt_group &&list_empty (&insn->alts)) { +if (!insn->visited && insn->alt_group &&list_empty (&insn->alts)) {
Why? This looks like another one that might belong in a separate patch.
Quoted 23 lines
tools/objtool/check.c +8
-0WARN_FUNC ("don't know how to handle branch to middle of alternative instruction group" , sec, insn->offset);return 1 ;@@ -1871,6 +1936,14 @@ static int validate_branch(struct objtool_file *file, struct instruction *first,case INSN_JUMP_DYNAMIC :if (func &&list_empty (&insn->alts) &&has_modified_stack_frame (&state)) { +/* +* On arm64 `br <Xn>` insn can be used for switch-tables +* but it cannot be distinguished in itself from a sibling +* call thus we need to have a look at the previous instructions +* to determine which it is +*/ +if (!arch_is_insn_sibling_call (insn)) +break ;WARN_FUNC ("sibling call from callable instruction with modified stack frame" , sec, insn->offset);return 1 ;tools/objtool/elf.c +0
-0
tools/objtool/elf.c struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset) · line 78
Quoted 6 lines
tools/objtool/elf.c +2
-1@@ -74,7 +74,8 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)struct symbol *sym;list_for_each_entry (sym, &sec->symbol_list, list) - if (sym->type != STT_SECTION && + if (sym->type != STT_NOTYPE && + sym->type != STT_SECTION &&
Why? Another potential separate patch.
Quoted 6 lines
tools/objtool/elf.c +0
-0sym->offset == offset)return sym;Signature · 1 line
2.17.1
Signature · 1 line
Josh
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Hi,
As Julien also pointed out I realise that similarly to the first patch of this series, this patch should be split into smaller ones. I will do this for the next version.
Quoted 67 lines · Josh Poimboeuf
Quoted 66 lines · Raphael Gault
Since the way the initial stack frame when entering a function is different that what is done in the
x86_64architecture, we need to add some more check to support the different cases. As opposed as forx86_64, the return address is not stored by the call instruction but is instead loaded in a register. The initial stack frame is thus empty when entering a function and 2 push operations are needed to set it up correctly. All the different combinations need to be taken into account.On arm64, the .altinstr_replacement section is not flagged as containing executable instructions but we still need to process it.
Switch tables are alse stored in a different way on arm64 than on
x86_64so we need to be able to identify in which case we are when looking for it.Signed-off-by Raphael Gault <[email protected]>--- tools/objtool/arch.h | 2 + tools/objtool/arch/arm64/decode.c | 27 +++++++++ tools/objtool/arch/x86/decode.c | 5 ++ tools/objtool/check.c | 95 +++++++++++++++++++++++++++---- tools/objtool/elf.c | 3 +- 5 files changed, 120 insertions(+), 12 deletions(-)tools/objtool/arch.h +2
-0@@ -88,4 +88,6 @@ unsigned long arch_compute_jump_destination(struct instruction *insn);unsigned long arch_compute_rela_sym_offset (int addend); +bool arch_is_insn_sibling_call (struct instruction *insn); +#endif /* _ARCH_H */ tools/objtool/arch/arm64/decode.c +25
-0@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev(l, &insn->list) { +prev = (void *)l; +if (!prev->func +|| prev->func->pfunc != insn->func->pfunc) +return false; +if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; +sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC +|| sym->pfunc != insn->func->pfunc) +return true; +break ; +}
Quoted 9 lines
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 134
Quoted 2 lines
tools/objtool/arch/arm64/decode.c +2
-0+return true; +}I get the feeling there might be a better way to do this, but I can't figure out what this function is actually doing. It looks like it searches backwards in the function for an instruction which has stack_op.src.reg !=
ADR_SOURCE-- what does that mean? And why doesn't it do anything with the instruction after it finds it?
I will indeed try to make it better.
Quoted 11 lines
Quoted 11 lines
tools/objtool/arch/arm64/decode.c +0
-0static int is_arm64 (struct elf *elf) {switch (elf->ehdr.e_machine){tools/objtool/arch/x86/decode.c +0
-0@@ -85,6 +85,11 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend +4 ; }
Quoted 7 lines
tools/objtool/arch/x86/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 91
Quoted 4 lines
tools/objtool/arch/x86/decode.c +4
-0+bool arch_is_insn_sibling_call (struct instruction *insn) +{ +return true; +}All x86 instructions are sibling calls?
The sementic here is indeed wrong. I implemented it like that on the x86 side because at the place it is called from in check.c we already know that we are in presence of a sibling calls when on x86. I will improve these bits on the next iteration.
Quoted 30 lines
Quoted 10 lines
tools/objtool/arch/x86/decode.c +1
-0+int arch_orc_read_unwind_hints (struct objtool_file *file) {struct section *sec, *relasec;tools/objtool/check.c +0
-0@@ -261,10 +261,12 @@ static int decode_instructions(struct objtool_file *file)unsigned long offset;tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 270
Quoted 10 lines
tools/objtool/check.c +3
-1struct instruction *insn;int ret; +static int composed_insn =0 ;for_each_sec (file, sec) { -if (!(sec->sh.sh_flags &SHF_EXECINSTR )) +if (!(sec->sh.sh_flags &SHF_EXECINSTR ) +&& (strcmp (sec->name,".altinstr_replacement" ) || !IGNORE_SHF_EXEC_FLAG ))continue ;We should just fix the root cause instead, presumably:
arch/arm64/include/asm/alternative.h +0
-0
arch/arm64/include/asm/alternative.h static inline void apply_alternatives_module(void *start, size_t length) { } · line 77
Quoted 8 lines
arch/arm64/include/asm/alternative.h +1
-1@@ -71,7 +71,7 @@ static inline void apply_alternatives_module(void *start, size_t length) { } ALTINSTR_ENTRY(feature,cb)\".popsection\n" \ " .if " __stringify(cb) " == 0\n"\ -".pushsection .altinstr_replacement, \"a\"\n" \ +".pushsection .altinstr_replacement, \"ax\"\n" \"663:\n\t" \ newinstr"\n" \"664:\n\t" \
I will take your advice and suggest this in the next version as well.
Quoted 13 lines
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 303
Quoted 9 lines
tools/objtool/check.c +2
-1if (strcmp(sec->name, ".altinstr_replacement") &&@@ -297,10 +299,22 @@ static int decode_instructions(struct objtool_file *file)WARN_FUNC ("invalid instruction type %d" , insn->sec, insn->offset, insn->type); ret =-1 ; -goto err; +free (insn); +continue ;What's the purpose of this change? If it's really needed then it looks like it should be a separate patch.
Quoted 12 lines
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 309
Quoted 8 lines
tools/objtool/check.c +5
-2} - -hash_add (file->insn_hash, &insn->hash, insn->offset); +/* +* For arm64 architecture, we sometime split instructions so that +* we can track the state evolution (i.e. load/store of pairs of registers). +* We thus need to take both into account and not erase the previous ones. +*/ Ew... Is this an architectural thing, or just a quirk of the arm64 decoder?
The motivation for this is to simulate the two consecutive operations that would be executed on x86 but are done in one on arm64. This is strictly a decoder related quirk. I don't know if there is a better way to do it without modifying the struct op_src and struct instruction.
Quoted 28 lines
Quoted 28 lines
tools/objtool/check.c +10
-2+if (composed_insn >0 ) +hash_add (file->insn_hash, &insn->hash, insn->offset + composed_insn); +else +hash_add (file->insn_hash, &insn->hash, insn->offset); +if (insn->len ==0 ) +composed_insn++; +else +composed_insn =0 ;list_add_tail (&insn->list, &file->insn_list); }@@ -510,10 +524,10 @@ static int add_jump_destinations(struct objtool_file *file) dest_off =arch_compute_jump_destination (insn); }else if (rela->sym->type ==STT_SECTION ) { dest_sec = rela->sym->sec; -dest_off = rela->addend +4 ; +dest_off =arch_compute_rela_sym_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_compute_rela_sym_offset (rela->addend); }else if (strstr (rela->sym->name,"_indirect_thunk_" )) {/* * Retpoline jumps are really dynamic jumps in @@ -663,7 +677,7 @@ static int handle_group_alt(struct objtool_file *file, last_orig_insn = insn; }
Quoted 6 lines
tools/objtool/check.c static int handle_group_alt(struct objtool_file *file, · line 680
Quoted 2 lines
tools/objtool/check.c +1
-1-if (next_insn_same_sec (file, last_orig_insn)) { +if (last_orig_insn &&next_insn_same_sec (file, last_orig_insn)) {This might belong in a separate patch which explains the reason for the change.
I did this because I encountered situations where the last_orig_insn was NULL because of the offset check perform in the preceding loop. This caused a null dereference to occur. But it definitely should be split up.
Quoted 25 lines
tools/objtool/check.c static struct rela *find_switch_table(struct objtool_file *file, · line 994
Quoted 9 lines
tools/objtool/check.c +2
-0fake_jump =malloc (sizeof (*fake_jump));if (!fake_jump) {WARN ("malloc failed" );@@ -976,6 +990,17 @@ static struct rela *find_switch_table(struct objtool_file *file,if (find_symbol_containing (rodata_sec, table_offset))continue ; +/* +* If we are on arm64 architecture, we now that we "know"
tools/objtool/check.c static struct rela *find_switch_table(struct objtool_file *file, · line 1003
Quoted 9 lines
tools/objtool/check.c +9
-0+* are in presence of a switch table thanks to +* the `br <Xn>` insn. but we can't retrieve it yet. +* So we just ignore unreachable for this file. +*/ +if (JUMP_DYNAMIC_IS_SWITCH_TABLE ) { +file->ignore_unreachables = true; +return NULL ; +} +I think this means switch table reading is not yet supported? If so then maybe the flag should be called
SWITCH_TABLE_NOT_SUPPORTED.
Quoted 2 lines
But really this needs to be fixed anyway before merging the code.
This was indeed an ugly way to do this. I will propose a better solution for this part in the next iteration.
Quoted 85 lines
Quoted 85 lines
tools/objtool/check.c +43
-3rodata_rela =find_rela_by_dest (rodata_sec, table_offset);if (rodata_rela) {/* @@ -1258,8 +1283,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; }/* @@ -1415,8 +1440,33 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)/* add imm, %rsp */ state->stack_size -= op->src.offset; -if (cfa->base ==CFI_SP ) +if (cfa->base ==CFI_SP ) { cfa->offset -= op->src.offset; +if (state->stack_size ==0 +&& initial_func_cfi.cfa.base ==CFI_CFA ) { +cfa->base =CFI_CFA ; +cfa->offset =0 ; +} +} +/* +* on arm64 the save/restore of sp into fp is not automatic +* and the first one can be done without the other so we +* need to be careful not to invalidate the stack frame in such +* cases. +*/ +else if (cfa->base ==CFI_BP ) { +if (state->stack_size ==0 +&& initial_func_cfi.cfa.base ==CFI_CFA ) { +cfa->base =CFI_CFA ; +cfa->offset =0 ; +restore_reg (state,CFI_BP ); +} +} +else if (cfa->base ==CFI_CFA ) { +cfa->base =CFI_SP ; +if (state->stack_size >=16 ) +cfa->offset =16 ; +}break ; }@@ -1427,6 +1477,16 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)break ; } +if (op->src.reg ==CFI_SP && op->dest.reg ==CFI_BP && + cfa->base ==CFI_SP && + regs[CFI_BP ].base ==CFI_CFA && + regs[CFI_BP ].offset == -cfa->offset) { + +/* mov %rsp, %rbp */ +cfa->base = op->dest.reg; +state->bp_scratch = false; +break ; +}if (op->src.reg ==CFI_SP && cfa->base ==CFI_SP ) {/* drap: lea disp(%rsp), %drap */ @@ -1518,6 +1578,9 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state) state->stack_size -=8 ;if (cfa->base ==CFI_SP ) cfa->offset -=8 ; +if (cfa->base ==CFI_SP && cfa->offset ==0 +&& initial_func_cfi.cfa.base ==CFI_CFA ) +cfa->base =CFI_CFA ;break ;@@ -1557,6 +1620,8 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)case OP_DEST_PUSH : state->stack_size +=8 ; +if (cfa->base ==CFI_CFA ) +cfa->base =CFI_SP ;if (cfa->base ==CFI_SP ) cfa->offset +=8 ;
Quoted 9 lines
tools/objtool/check.c static int validate_branch(struct objtool_file *file, struct instruction *first, · line 1796
Quoted 6 lines
tools/objtool/check.c +1
-1@@ -1728,7 +1793,7 @@ static int validate_branch(struct objtool_file *file, struct instruction *first, insn = first; sec = insn->sec; -if (insn->alt_group &&list_empty (&insn->alts)) { +if (!insn->visited && insn->alt_group &&list_empty (&insn->alts)) {Why? This looks like another one that might belong in a separate patch.
Indeed it belongs to a separate patch. I did this because I encountered a situation where one of the alternative instructions (in .altinstr_replacement) jumps at the offset of an instruction which is replaced as well. I thus considered that if one of the instruction of the group is replaced then all instructions of the group should be replaced and thus this would never happen at execution.
Quoted 25 lines
Quoted 25 lines
tools/objtool/check.c +8
-0WARN_FUNC ("don't know how to handle branch to middle of alternative instruction group" , sec, insn->offset);return 1 ;@@ -1871,6 +1936,14 @@ static int validate_branch(struct objtool_file *file, struct instruction *first,case INSN_JUMP_DYNAMIC :if (func &&list_empty (&insn->alts) &&has_modified_stack_frame (&state)) { +/* +* On arm64 `br <Xn>` insn can be used for switch-tables +* but it cannot be distinguished in itself from a sibling +* call thus we need to have a look at the previous instructions +* to determine which it is +*/ +if (!arch_is_insn_sibling_call (insn)) +break ;WARN_FUNC ("sibling call from callable instruction with modified stack frame" , sec, insn->offset);return 1 ;tools/objtool/elf.c +0
-0@@ -74,7 +74,8 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)struct symbol *sym;
Quoted 7 lines
tools/objtool/elf.c struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset) · line 78
Quoted 4 lines
tools/objtool/elf.c +2
-1list_for_each_entry (sym, &sec->symbol_list, list) -if (sym->type != STT_SECTION && +if (sym->type != STT_NOTYPE && + sym->type != STT_SECTION &&Why? Another potential separate patch.
This is again a special case I encountered where several symbols can be found with the same offset so the sole condition of the offset wasn't enough to distinguish the correct destination. I thus added some extra check on the type of the symbol.
Quoted 7 lines
Quoted 6 lines
tools/objtool/elf.c +0
-1sym->offset == offset)return sym; --2.17.1
Please fill free to comment back on my answers. I would really like to get you opinion on some of the special cases I mentioned.
Thanks,
--
Raphael Gault
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Quoted 43 lines · Raphael Gault
Quoted 33 lines
Quoted 33 lines
tools/objtool/arch/arm64/decode.c +25
-0@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev(l, &insn->list) { +prev = (void *)l; +if (!prev->func +|| prev->func->pfunc != insn->func->pfunc) +return false; +if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; +sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC +|| sym->pfunc != insn->func->pfunc) +return true; +break ; +}Quoted 9 lines
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 134
Quoted 2 lines
tools/objtool/arch/arm64/decode.c +2
-0+return true; +}I get the feeling there might be a better way to do this, but I can't figure out what this function is actually doing. It looks like it searches backwards in the function for an instruction which has stack_op.src.reg !=
ADR_SOURCE-- what does that mean? And why doesn't it do anything with the instruction after it finds it?
Quoted 1 line
I will indeed try to make it better.
I still don't quite get what it's trying to accomplish, but I wonder if there's some kind of tracking you can add in validate_branch() to keep track of whatever you're looking for, leading up to the indirect jump.
Quoted 11 lines
Quoted 10 lines
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend)
Quoted 6 lines
tools/objtool/arch/arm64/decode.c +5
-1-hash_add (file->insn_hash, &insn->hash, insn->offset); +/* +* For arm64 architecture, we sometime split instructions so that +* we can track the state evolution (i.e. load/store of pairs of registers). +* We thus need to take both into account and not erase the previous ones. +*/ Ew... Is this an architectural thing, or just a quirk of the arm64 decoder?
Quoted 4 lines
The motivation for this is to simulate the two consecutive operations that would be executed on x86 but are done in one on arm64. This is strictly a decoder related quirk. I don't know if there is a better way to do it without modifying the struct
op_srcand struct instruction.
Ah. Which ops are those? Hopefully we can find a better way to represent that with a single instruction. Adding fake instructions is fragile.
Signature · 1 line
Josh
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Hi Josh,
Quoted 44 lines · Josh Poimboeuf
Quoted 43 lines · Raphael Gault
Quoted 42 lines
Quoted 25 lines
tools/objtool/arch/arm64/decode.c +17
-0@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev(l, &insn->list) { +prev = (void *)l; +if (!prev->func +|| prev->func->pfunc != insn->func->pfunc) +return false;tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 134
Quoted 10 lines
tools/objtool/arch/arm64/decode.c +10
-0+if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; +sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC +|| sym->pfunc != insn->func->pfunc) +return true; +break ; +} +return true; +}I get the feeling there might be a better way to do this, but I can't figure out what this function is actually doing. It looks like it searches backwards in the function for an instruction which has stack_op.src.reg !=
ADR_SOURCE-- what does that mean? And why doesn't it do anything with the instruction after it finds it?
Quoted 6 lines
Quoted 1 line
I will indeed try to make it better.
I still don't quite get what it's trying to accomplish, but I wonder if there's some kind of tracking you can add in
validate_branch()to keep track of whatever you're looking for, leading up to the indirect jump.
The motivation behind this is that the br <Xn> instruction is a dynamic jump (jump to the address contained in the provided register). This instruction is used for sibling calls but can also be used for switch table. I use this to differentiate these two cases from one another:
Generally the adr/adrp instruction is used prior to br in order to load the address into the register. What I do here is go back throught the instructions and try to identify if the address loaded.
I also thought of implementing some sort of tracking in validate branch because it could be useful for identifying the switch tables as well. But it seemed to me like a major change in the sementic of this tool:
indeed, from my perspective I would have to track the state of the registers and I don't know if we want to do that.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend)
Quoted 6 lines
tools/objtool/arch/arm64/decode.c +5
-1-hash_add (file->insn_hash, &insn->hash, insn->offset); +/* +* For arm64 architecture, we sometime split instructions so that +* we can track the state evolution (i.e. load/store of pairs of registers). +* We thus need to take both into account and not erase the previous ones. +*/ Ew... Is this an architectural thing, or just a quirk of the arm64 decoder?
Quoted 9 lines
Quoted 4 lines
The motivation for this is to simulate the two consecutive operations that would be executed on x86 but are done in one on arm64. This is strictly a decoder related quirk. I don't know if there is a better way to do it without modifying the struct
op_srcand struct instruction.Ah. Which ops are those? Hopefully we can find a better way to represent that with a single instruction. Adding fake instructions is fragile.
Those are the load/store of pairs of registers, mainly stp/ldp. Those are often use in the function prologues/epilogues to save/restore the stack pointers and frame pointers however it can be used with any register pair.
The idea to add a new instruction could work but I would need to extend the struct op_src as well I think.
Thanks,
--
Raphael Gault
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Peter Zijlstra
maintainerRe: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Quoted 8 lines · Raphael Gault
The motivation behind this is that the
br <Xn>instruction is a dynamic jump (jump to the address contained in the provided register). This instruction is used for sibling calls but can also be used for switch table. I use this to differentiate these two cases from one another:Generally the
adr/adrpinstruction is used prior tobrin order to load the address into the register. What I do here is go back throught the instructions and try to identify if the address loaded.
Yikes, be very careful with simply going back on the instruction stream.
The problem case would be something like:
adr ... b 1f ... b ...
1: br ...
In that case, simply going backwards from 1 will not yield the desired result.
At some point I did a pass storing all the fwd jumps in their destination and used that to 'rewind' the instruction stream, but that wasn't very pretty either.
The best way might be to, while doing validate_branch() keep a state table of all most recent ADR(P) instructions and when encountering BR check that state to see what it really is.
We currently don't do dynamic insn->type, but it shouldn't be too hard (famous last words of course).
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
In reply to Raphael GaultQuoted 63 lines · Raphael Gault
Hi Josh,
Quoted 44 lines · Josh Poimboeuf
Quoted 43 lines · Raphael Gault
Quoted 42 lines
Quoted 25 lines
tools/objtool/arch/arm64/decode.c +17
-0@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev(l, &insn->list) { +prev = (void *)l; +if (!prev->func +|| prev->func->pfunc != insn->func->pfunc) +return false;tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 134
Quoted 10 lines
tools/objtool/arch/arm64/decode.c +10
-0+if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; +sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC +|| sym->pfunc != insn->func->pfunc) +return true; +break ; +} +return true; +}I get the feeling there might be a better way to do this, but I can't figure out what this function is actually doing. It looks like it searches backwards in the function for an instruction which has stack_op.src.reg !=
ADR_SOURCE-- what does that mean? And why doesn't it do anything with the instruction after it finds it?Quoted 6 lines
Quoted 1 line
I will indeed try to make it better.
I still don't quite get what it's trying to accomplish, but I wonder if there's some kind of tracking you can add in
validate_branch()to keep track of whatever you're looking for, leading up to the indirect jump.The motivation behind this is that the
br <Xn>instruction is a dynamic jump (jump to the address contained in the provided register). This instruction is used for sibling calls but can also be used for switch table. I use this to differentiate these two cases from one another:Generally the
adr/adrpinstruction is used prior tobrin order to load the address into the register. What I do here is go back throught the instructions and try to identify if the address loaded.
Quoted 5 lines
I also thought of implementing some sort of tracking in validate branch because it could be useful for identifying the switch tables as well. But it seemed to me like a major change in the sementic of this tool:
indeed, from my perspective I would have to track the state of the registers and I don't know if we want to do that.
I don't have much time to look at this today (and I'll be out next week), but we had a similar problem in x86. See the comments above find_switch_table(), particularly #3. Does that function not work for the arm64 case?
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend)
Quoted 6 lines
tools/objtool/arch/arm64/decode.c +5
-1-hash_add (file->insn_hash, &insn->hash, insn->offset); +/* +* For arm64 architecture, we sometime split instructions so that +* we can track the state evolution (i.e. load/store of pairs of registers). +* We thus need to take both into account and not erase the previous ones. +*/ Ew... Is this an architectural thing, or just a quirk of the arm64 decoder?
Quoted 9 lines
Quoted 4 lines
The motivation for this is to simulate the two consecutive operations that would be executed on x86 but are done in one on arm64. This is strictly a decoder related quirk. I don't know if there is a better way to do it without modifying the struct
op_srcand struct instruction.Ah. Which ops are those? Hopefully we can find a better way to represent that with a single instruction. Adding fake instructions is fragile.
Quoted 7 lines
Those are the load/store of pairs of registers, mainly stp/ldp. Those are often use in the function prologues/epilogues to save/restore the stack pointers and frame pointers however it can be used with any register pair.
The idea to add a new instruction could work but I would need to extend the
struct op_srcas well I think.
Again I don't have much time to look at it, but I do think that changing op_src/dest to allow for the stp/ldp instructions would work better than inserting a fake instruction to emulate x86.
Or another idea would be to associate multiple stack_ops with a single instruction.
Signature · 1 line
Josh
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Hi Josh,
Quoted 70 lines · Josh Poimboeuf
Quoted 63 lines · Raphael Gault
Hi Josh,
Quoted 44 lines · Josh Poimboeuf
Quoted 43 lines · Raphael Gault
Quoted 42 lines
Quoted 25 lines
tools/objtool/arch/arm64/decode.c +17
-0@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev(l, &insn->list) { +prev = (void *)l; +if (!prev->func +|| prev->func->pfunc != insn->func->pfunc) +return false;tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 134
Quoted 10 lines
tools/objtool/arch/arm64/decode.c +10
-0+if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; +sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC +|| sym->pfunc != insn->func->pfunc) +return true; +break ; +} +return true; +}I get the feeling there might be a better way to do this, but I can't figure out what this function is actually doing. It looks like it searches backwards in the function for an instruction which has stack_op.src.reg !=
ADR_SOURCE-- what does that mean? And why doesn't it do anything with the instruction after it finds it?Quoted 6 lines
Quoted 1 line
I will indeed try to make it better.
I still don't quite get what it's trying to accomplish, but I wonder if there's some kind of tracking you can add in
validate_branch()to keep track of whatever you're looking for, leading up to the indirect jump.The motivation behind this is that the
br <Xn>instruction is a dynamic jump (jump to the address contained in the provided register). This instruction is used for sibling calls but can also be used for switch table. I use this to differentiate these two cases from one another:Generally the
adr/adrpinstruction is used prior tobrin order to load the address into the register. What I do here is go back throught the instructions and try to identify if the address loaded.Quoted 5 lines
I also thought of implementing some sort of tracking in validate branch because it could be useful for identifying the switch tables as well. But it seemed to me like a major change in the sementic of this tool:
indeed, from my perspective I would have to track the state of the registers and I don't know if we want to do that.
Quoted 5 lines
I don't have much time to look at this today (and I'll be out next week), but we had a similar problem in x86. See the comments above
find_switch_table(), particularly #3. Does that function not work for the arm64 case?
Honestly, I don't have a full understanding of how the switch tables are handled on arm64. All I know is that I've identified a case in which it doesn't work (and I get an unreachable instruction warning). When trying to figure out how the switch tables work on arm64 and how objtool is retrieving them (on x86 at least) I realised that you look for 2 relocations :
- One from (.rela).text which refers to the .rodata section
- One from (.rela).rodata which refers somewhere else.
On the case I identified the second relocation doesn't exist thus the function doesn't find the switch table.
Again since I do not have a good understanding about this I am not able to say if it is a corner case or not.
Quoted 29 lines
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend)
Quoted 6 lines
tools/objtool/arch/arm64/decode.c +5
-1-hash_add (file->insn_hash, &insn->hash, insn->offset); +/* +* For arm64 architecture, we sometime split instructions so that +* we can track the state evolution (i.e. load/store of pairs of registers). +* We thus need to take both into account and not erase the previous ones. +*/ Ew... Is this an architectural thing, or just a quirk of the arm64 decoder?
Quoted 9 lines
Quoted 4 lines
The motivation for this is to simulate the two consecutive operations that would be executed on x86 but are done in one on arm64. This is strictly a decoder related quirk. I don't know if there is a better way to do it without modifying the struct
op_srcand struct instruction.Ah. Which ops are those? Hopefully we can find a better way to represent that with a single instruction. Adding fake instructions is fragile.
Quoted 7 lines
Those are the load/store of pairs of registers, mainly stp/ldp. Those are often use in the function prologues/epilogues to save/restore the stack pointers and frame pointers however it can be used with any register pair.
The idea to add a new instruction could work but I would need to extend the
struct op_srcas well I think.
Quoted 7 lines
Again I don't have much time to look at it, but I do think that changing op_src/dest to allow for the stp/ldp instructions would work better than inserting a fake instruction to emulate x86.
Or another idea would be to associate multiple
stack_opswith a single instruction.
I haven't looked at it in depth yet but I will try to figure out a good way to represent those instructions on a more proper manner.
Thanks,
--
Raphael Gault
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
Hi Josh,
Quoted 126 lines · Raphael Gault
Hi Josh,
Quoted 70 lines · Josh Poimboeuf
Quoted 63 lines · Raphael Gault
Hi Josh,
Quoted 44 lines · Josh Poimboeuf
Quoted 43 lines · Raphael Gault
Quoted 42 lines
Quoted 25 lines
tools/objtool/arch/arm64/decode.c +17
-0@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are +* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev(l, &insn->list) { +prev = (void *)l; +if (!prev->func +|| prev->func->pfunc != insn->func->pfunc) +return false;tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 134
Quoted 10 lines
tools/objtool/arch/arm64/decode.c +10
-0+if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; +sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC +|| sym->pfunc != insn->func->pfunc) +return true; +break ; +} +return true; +}I get the feeling there might be a better way to do this, but I can't figure out what this function is actually doing. It looks like it searches backwards in the function for an instruction which has stack_op.src.reg !=
ADR_SOURCE-- what does that mean? And why doesn't it do anything with the instruction after it finds it?Quoted 6 lines
Quoted 1 line
I will indeed try to make it better.
I still don't quite get what it's trying to accomplish, but I wonder if there's some kind of tracking you can add in
validate_branch()to keep track of whatever you're looking for, leading up to the indirect jump.The motivation behind this is that the
br <Xn>instruction is a dynamic jump (jump to the address contained in the provided register). This instruction is used for sibling calls but can also be used for switch table. I use this to differentiate these two cases from one another:Generally the
adr/adrpinstruction is used prior tobrin order to load the address into the register. What I do here is go back throught the instructions and try to identify if the address loaded.Quoted 5 lines
I also thought of implementing some sort of tracking in validate branch because it could be useful for identifying the switch tables as well. But it seemed to me like a major change in the sementic of this tool:
indeed, from my perspective I would have to track the state of the registers and I don't know if we want to do that.Quoted 5 lines
I don't have much time to look at this today (and I'll be out next week), but we had a similar problem in x86. See the comments above
find_switch_table(), particularly #3. Does that function not work for the arm64 case?Honestly, I don't have a full understanding of how the switch tables are handled on arm64. All I know is that I've identified a case in which it doesn't work (and I get an unreachable instruction warning). When trying to figure out how the switch tables work on arm64 and how objtool is retrieving them (on x86 at least) I realised that you look for 2 relocations :
- One from (.rela).text which refers to the .rodata section
- One from (.rela).rodata which refers somewhere else.
On the case I identified the second relocation doesn't exist thus the function doesn't find the switch table.Again since I do not have a good understanding about this I am not able to say if it is a corner case or not.
Quoted 33 lines
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend)
Quoted 6 lines
tools/objtool/arch/arm64/decode.c +5
-1-hash_add (file->insn_hash, &insn->hash, insn->offset); +/* +* For arm64 architecture, we sometime split instructions so that +* we can track the state evolution (i.e. load/store of pairs of registers). +* We thus need to take both into account and not erase the previous ones. +*/ Ew... Is this an architectural thing, or just a quirk of the arm64 decoder?
Quoted 9 lines
Quoted 4 lines
The motivation for this is to simulate the two consecutive operations that would be executed on x86 but are done in one on arm64. This is strictly a decoder related quirk. I don't know if there is a better way to do it without modifying the struct
op_srcand struct instruction.Ah. Which ops are those? Hopefully we can find a better way to represent that with a single instruction. Adding fake instructions is fragile.
Quoted 7 lines
Those are the load/store of pairs of registers, mainly stp/ldp. Those are often use in the function prologues/epilogues to save/restore the stack pointers and frame pointers however it can be used with any register pair.
The idea to add a new instruction could work but I would need to extend the
struct op_srcas well I think.Again I don't have much time to look at it, but I do think that changing op_src/dest to allow for the stp/ldp instructions would work better than inserting a fake instruction to emulate x86.
Quoted 6 lines
Quoted 3 lines
Or another idea would be to associate multiple
stack_opswith a single instruction.I haven't looked at it in depth yet but I will try to figure out a good way to represent those instructions on a more proper manner.
I wanted to get your thoughts on the solution I found without waiting for v2. If it's too much trouble reviewing it now I'll wait for the v2.
I added a field to the struct stack_op in order to have access to an extra register. This way I can provide the extra register to the instruction in order to use it later.
tools/objtool/arch.h +8 -0
@@ -51,6 +51,7 @@ struct op_dest {int offset; }; +enum op_src_type {OP_SRC_REG ,OP_SRC_REG_INDIRECT ,
@@ -66,9 +67,16 @@ struct op_src {int offset; }; +struct op_extra { +unsigned char used; +unsigned char reg; +int offset; +}; +struct stack_op {struct op_dest dest;struct op_src src; +struct op_extra extra; };struct instruction ;
tools/objtool/arch/arm64/decode.c +9 -6
@@ -1743,23 +1673,26 @@ int arm_decode_ld_st_regs_pair_off(u32 instr, unsigned char *type, op->src.type =OP_SRC_REG_INDIRECT ; op->src.reg =CFI_SP ; op->src.offset =0 ; -state.curr_offset =8 ; op->dest.type =OP_DEST_REG ; -op->dest.reg = state.regs[0 ]; +op->dest.reg = rt; op->dest.offset =0 ; +op->extra.used =1 ; +op->extra.reg = rt2; +op->extra.offset =8 ;break ;default : op->dest.type =OP_DEST_REG_INDIRECT ; op->dest.reg =CFI_SP ; op->dest.offset =8 ; -state.curr_offset =0 ; op->src.type =OP_SRC_REG ; -op->src.reg = state.regs[1 ]; +op->src.reg = rt2; op->src.offset =0 ; +op->extra.used =1 ; +op->extra.reg = rt; +op->extra.offset =0 ;/* store */ } -state.op = *op; -return INSN_COMPOSED ; +return 0 ; } int arm_decode_ld_st_regs_pair_post(u32 instr, unsigned char *type,
tools/objtool/check.c +49 -0
92 lines not on this page · raw message
Thanks,
--
Raphael Gault
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [RFC 3/6] objtool: arm64: Adapt the stack frame checks and the section analysis for the arm architecture
In reply to Raphael GaultHi Raphaël,
I think you could split the patch in at least 3:
- Handling the split instructions
- Handling the jump offset
- Dynamic jumps/switch table
Quoted 1 line · Raphael Gault
Since the way the initial stack frame when entering a function is different that what is done
Nit: "different from what is done"
Quoted 10 lines
in the
x86_64architecture, we need to add some more check to support the different cases. As opposed as forx86_64, the return address is not stored by the call instruction but is instead loaded in a register. The initial stack frame is thus empty when entering a function and 2 push operations are needed to set it up correctly. All the different combinations need to be taken into account.On arm64, the .altinstr_replacement section is not flagged as containing executable instructions but we still need to process it.
Switch tables are alse stored in a different way on arm64 than on
x86_64so we need to be able
Nit: also
Quoted 34 lines
to identify in which case we are when looking for it.
Signed-off-by Raphael Gault <[email protected]>--- tools/objtool/arch.h | 2 + tools/objtool/arch/arm64/decode.c | 27 +++++++++ tools/objtool/arch/x86/decode.c | 5 ++ tools/objtool/check.c | 95 +++++++++++++++++++++++++++---- tools/objtool/elf.c | 3 +- 5 files changed, 120 insertions(+), 12 deletions(-)tools/objtool/arch.h +2
-0@@ -88,4 +88,6 @@ unsigned long arch_compute_jump_destination(struct instruction *insn);unsigned long arch_compute_rela_sym_offset (int addend); +bool arch_is_insn_sibling_call (struct instruction *insn); +#endif /* _ARCH_H */ tools/objtool/arch/arm64/decode.c +4
-0@@ -105,6 +105,33 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend; } +/* +* In order to know if we are in presence of a sibling +* call and not in presence of a switch table we look +* back at the previous instructions and see if we are
tools/objtool/arch/arm64/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 121
Quoted 10 lines
tools/objtool/arch/arm64/decode.c +10
-0+* jumping inside the same function that we are already +* in. +*/ +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +struct instruction *prev; +struct list_head *l; +struct symbol *sym; +list_for_each_prev (l, &insn->list) { + prev = (void *)l;
Please use list_entry() instead of casting, this only happens to work because list is the first member of the struct instruction.
Quoted 21 lines
tools/objtool/arch/arm64/decode.c +13
-0+if (!prev->func + || prev->func->pfunc != insn->func->pfunc) +return false; +if (prev->stack_op.src.reg !=ADR_SOURCE ) +continue ; + sym =find_symbol_containing (insn->sec, insn->immediate); +if (!sym || sym->type !=STT_FUNC + || sym->pfunc != insn->func->pfunc) +return true; +break ; + } +return true; +}static int is_arm64 (struct elf *elf) {switch (elf->ehdr.e_machine){tools/objtool/arch/x86/decode.c +0
-0
tools/objtool/arch/x86/decode.c unsigned long arch_compute_rela_sym_offset(int addend) · line 90
Quoted 6 lines
tools/objtool/arch/x86/decode.c +3
-0@@ -85,6 +85,11 @@ unsigned long arch_compute_rela_sym_offset(int addend)return addend +4 ; } +bool arch_is_insn_sibling_call (struct instruction *insn) +{ +return true;
The naming and what the function returns does seem to be really fitting. Makes it seem like every x86 instruction is a sibling call, which is unlikely to be the case.
Quoted 32 lines
tools/objtool/arch/x86/decode.c +2
-0+} +int arch_orc_read_unwind_hints (struct objtool_file *file) {struct section *sec, *relasec;tools/objtool/check.c +5
-3@@ -261,10 +261,12 @@ static int decode_instructions(struct objtool_file *file)unsigned long offset;struct instruction *insn;int ret; +static int composed_insn =0 ;for_each_sec (file, sec) { -if (!(sec->sh.sh_flags &SHF_EXECINSTR )) +if (!(sec->sh.sh_flags &SHF_EXECINSTR ) + && (strcmp (sec->name,".altinstr_replacement" ) || !IGNORE_SHF_EXEC_FLAG ))continue ; if (strcmp(sec->name, ".altinstr_replacement") &&@@ -297,10 +299,22 @@ static int decode_instructions(struct objtool_file *file)WARN_FUNC ("invalid instruction type %d" , insn->sec, insn->offset, insn->type); ret =-1 ; - goto err; +free (insn); +continue ; } -
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 313
Quoted 10 lines
tools/objtool/check.c +9
-1-hash_add (file->insn_hash, &insn->hash, insn->offset); +/* +* For arm64 architecture, we sometime split instructions so that +* we can track the state evolution (i.e. load/store of pairs of registers). +* We thus need to take both into account and not erase the previous ones. +*/ +if (composed_insn >0 ) +hash_add (file->insn_hash, &insn->hash, insn->offset + composed_insn); +else +hash_add (file->insn_hash, &insn->hash, insn->offset);
composed_insn has no reason to be negative, right? So this if is equivalent to:
hash_add(file->insn_hash, &insn->hash, insn->offset + composed_insn);
Also, that means that this only works because all arm64 instructions are 4 bytes long. Otherwise you could have an overlap between the "second" part of the composed instruction and the instruction that follows it.
It feels a bit too hackish for the arch independent code.
If we want to use that trick, maybe it should be the arm64 decode that should return a length of 1 or 2 for composed insn, and when decoding an instruction that isn't 4-bytes aligned, we would know to look 2 bytes before to find what we were decoding, then return (4 - len) so that we end up on the next instruction on the next itertation.
This way we don't have to change anything to the decode loop.
Also, I've got the impression that this hash table is most often use to find the instruction at the starting offset of a function. Meaning it is unlikely we'll end up looking up that composed instruction. Might be worth checking whether this is the case and if so, maybe we can just add the one real instruction to the hash table and focus on the instruction list for our instruction splitting.
tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 320
Quoted 7 lines
tools/objtool/check.c +4
-0+if (insn->len ==0 ) + composed_insn++; +else + composed_insn =0 ;list_add_tail (&insn->list, &file->insn_list); }
Thanks,
Signature · 1 line
Julien Thierry
RFC 4/6 arm64: assembler: Add macro to annotate asm function having non standard stack-frame.
In reply to Raphael Gault--- arch/arm64/include/asm/assembler.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/arm64/include/asm/assembler.h +18 -0
@@ -748,4 +748,22 @@ USER(\label, ic ivau, \tmp2) // invalidate I line PoU .Lyield_out_\@ : .endm + +#ifdef CONFIG_STACK_VALIDATION + /* + * This macro is the arm64 assembler equivalent of the + * macro STACK_FRAME_NON_STANDARD define at + * ~/include/linux/frame.h + */ + .macro asm_stack_frame_non_standard func + .pushsection ".discard.func_stack_frame_non_standard" + .8byte \func + .popsection + .endm +#else + .macro asm_stack_frame_non_standard func + .endm +#endif + +#endif /* __ASM_ASSEMBLER_H */
Signature · 1 line
2.17.1
Re: [RFC 4/6] arm64: assembler: Add macro to annotate asm function having non standard stack-frame.
Hi Raphaël,
Quoted 1 line · Raphael Gault
Signed-off-by Raphael Gault <[email protected]>
Even there is not much to say, we include a commit message to explain what the patch does and/or why we want it.
Quoted 20 lines
--- arch/arm64/include/asm/assembler.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)arch/arm64/include/asm/assembler.h +8
-0@@ -748,4 +748,22 @@ USER(\label, ic ivau, \tmp2) // invalidate I line PoU .Lyield_out_\@ : .endm + +#ifdef CONFIG_STACK_VALIDATION + /* + * This macro is the arm64 assembler equivalent of the + * macro STACK_FRAME_NON_STANDARD define at + * ~/include/linux/frame.h + */ + .macro asm_stack_frame_non_standard func
arch/arm64/include/asm/assembler.h USER(\label, ic ivau, \tmp2) // invalidate I line PoU · line 768
Quoted 10 lines
arch/arm64/include/asm/assembler.h +10
-0+ .pushsection ".discard.func_stack_frame_non_standard" + .8byte \func + .popsection + .endm +#else + .macro asm_stack_frame_non_standard func + .endm +#endif + +
This can be simplified as:
.macro asm_stack_frame_non_standard func
#ifdef CONFIG_STACK_VALIDATION [...]
#endif
.endm
arch/arm64/include/asm/assembler.h USER(\label, ic ivau, \tmp2) // invalidate I line PoU · line 769
Quoted 2 lines
arch/arm64/include/asm/assembler.h +0
-0#endif /* __ASM_ASSEMBLER_H */
Cheers,
Signature · 1 line
Julien Thierry
RFC 5/6 arm64: sleep: Add stack frame setup for __cpu_supsend_enter
In reply to Raphael GaultAnnotate cpu_resume and _cpu_resume to silence objtool warning about non-standard stack frame.
--- arch/arm64/kernel/sleep.S | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/kernel/sleep.S +4 -0
@@ -90,6 +90,7 @@ ENTRY(__cpu_suspend_enter) str x0, [x1] add x0, x0, #SLEEP_STACK_DATA_SYSTEM_REGS stp x29, lr, [sp, #-16]! + mov x29, sp bl cpu_do_suspend ldp x29, lr, [sp], #16 mov x0, #1
@@ -146,3 +147,6 @@ ENTRY(_cpu_resume) mov x0, #0 ret ENDPROC(_cpu_resume) + + asm_stack_frame_non_standard cpu_resume + asm_stack_frame_non_standard _cpu_resume
Signature · 1 line
2.17.1
Re: [RFC 5/6] arm64: sleep: Add stack frame setup for __cpu_supsend_enter
In $SUBJECT, s/supsend/suspend/.
Quoted 13 lines · Raphael Gault
Annotate
cpu_resumeand_cpu_resumeto silence objtool warning about non-standard stack frame.Signed-off-by Raphael Gault <[email protected]>--- arch/arm64/kernel/sleep.S | 4 ++++ 1 file changed, 4 insertions(+)arch/arm64/kernel/sleep.S +0
-0
arch/arm64/kernel/sleep.S ENTRY(__cpu_suspend_enter) · line 93
Quoted 4 lines
arch/arm64/kernel/sleep.S +1
-0@@ -90,6 +90,7 @@ ENTRY(__cpu_suspend_enter) str x0, [x1] add x0, x0, #SLEEP_STACK_DATA_SYSTEM_REGS stp x29, lr, [sp, #-16]! + mov x29, sp
Why is it changing the actual code? The patch description indicates that it's only adding annotations.
Quoted 13 lines
arch/arm64/kernel/sleep.S +3
-0bl cpu_do_suspend ldp x29, lr, [sp], #16 mov x0, #1@@ -146,3 +147,6 @@ ENTRY(_cpu_resume) mov x0, #0 ret ENDPROC(_cpu_resume) + + asm_stack_frame_non_standard cpu_resume + asm_stack_frame_non_standard _cpu_resumeSignature · 1 line
2.17.1
Signature · 1 line
Josh
RFC 6/6 objtool: arm64: Enable stack validation for arm64
In reply to Raphael Gault--- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
@@ -151,6 +151,7 @@ config ARM64 select HAVE_RCU_TABLE_INVALIDATE select HAVE_RSEQ select HAVE_STACKPROTECTOR + select HAVE_STACK_VALIDATION select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES
Signature · 1 line
2.17.1
Quoted 5 lines · Raphael Gault
Hi,
As of now, objtool only supports the
x86_64architecture but the groundwork has already been done in order to add support for other architecture without too much effort.
Hi Raphael,
This was a pleasant surprise in my inbox. From a quick glance I'm actually surprised at how "easy" it looks, though I can tell it was quite a big effort. Doing the first arch port is always the hardest. Kudos! I will give it a proper review soon.
Signature · 1 line
Josh
Quoted 10 lines · Raphael Gault
Hi,
As of now, objtool only supports the
x86_64architecture but the groundwork has already been done in order to add support for other architecture without too much effort.This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.
I think it makes sense to clarify *why* we want this on arm64. Also, we should identify things that objtool does today that maybe we don't want on arm64, rather than buy into all of it by default.
Quoted 11 lines
* Patch 1 adapts the existing code to be able to add support for other architecture.
* Patch 2 provide implementation of the required function for the arm64 architecture.
* Patch 3 adapts the checking of the stack state for the arm64 architecture.
* Patch 4 & 5 fix some warning objtool raised in some particular functions of~/arch/arm64/kernel/sleep.S. Patch 4 add a macro to signal that some function should be ignored by objtool.
* Patch 6 enables stack validation for arm64.
Quoted 9 lines
Theses patches should provide support for the main cases and behaviour. However a few corner cases are not yet handled by objtool:
* In the
~/arch/arm64/crypto/directory, I noticed that some plain data are sometimes stored in the.textsection causing objtool to mistake this for instructions and trying (and failing) to interprete them. If someone could explain to me why we store data directly in the .text section I would appreciate it.
Just for convenience, basically. Not specifying a section at all when emitting a literal amounts to putting it into .text, and a a bonus, it is guaranteed to be in range for a ADR instructions (which is not the case when the code is built into the kernel rather than enabled as a module)
Due to Spectre I have already updated some of the code so larger tables are moved into .rodata (since literal data might contain binary sequences that are usable as spectre gadgets), but some work remains to be done.
Quoted 82 lines
* In the support for arm32 architecture such as in
~/arch/arm64/kernel/kuser32.Ssome A32 instructions are used but such instructions are not understood by objtool causing a warning.I also have a few unclear points I would like to bring to your attention:
* For
x86_64, when looking for a symbol relocation with explicit addend, objtool systematically adds a +4 offset to the addend. I don't understand why even if I have a feeling it is related to the type of relacation.* I currently don't have a clear understanding about how switch-tables are generated on arm64 and how to retrieve them (based on relocations).
Please provide me with any feedback and comments as well on the content than the style of these patches.
Thanks,
Raphael
->
Raphael Gault (6):
objtool: Refactor code to make it more suitable for multiple
architecture support
objtool: arm64: Add required implementation for supporting the aarch64
architecture in objtool.
objtool: arm64: Adapt the stack frame checks and the section analysis
for the arm architecture
arm64: assembler: Add macro to annotate asm function having non
standard stack-frame.
arm64: sleep: Add stack frame setup for__cpu_supsend_enter
objtool: arm64: Enable stack validation for arm64arch/arm64/Kconfig | 1 + arch/arm64/include/asm/assembler.h | 18 + arch/arm64/kernel/sleep.S | 4 + tools/objtool/Build | 1 - tools/objtool/arch.h | 11 + tools/objtool/arch/arm64/Build | 6 + tools/objtool/arch/arm64/bit_operations.c | 65 + tools/objtool/arch/arm64/decode.c | 2870 +++++++++++++++++ .../objtool/arch/arm64/include/arch_special.h | 44 + .../arch/arm64/include/asm/orc_types.h | 109 + .../arch/arm64/include/bit_operations.h | 22 + tools/objtool/arch/arm64/include/cfi.h | 76 + .../objtool/arch/arm64/include/insn_decode.h | 219 ++ tools/objtool/arch/arm64/orc_gen.c | 40 + tools/objtool/arch/x86/Build | 1 + tools/objtool/arch/x86/decode.c | 111 + tools/objtool/arch/x86/include/arch_special.h | 35 + tools/objtool/{ => arch/x86/include}/cfi.h | 0 tools/objtool/{ => arch/x86}/orc_gen.c | 10 +- tools/objtool/check.c | 209 +- tools/objtool/check.h | 1 + tools/objtool/elf.c | 3 +- tools/objtool/orc.h | 4 +- tools/objtool/special.c | 18 +- 24 files changed, 3740 insertions(+), 138 deletions(-) create mode 100644 tools/objtool/arch/arm64/Build create mode 100644 tools/objtool/arch/arm64/bit_operations.c create mode 100644 tools/objtool/arch/arm64/decode.c create mode 100644 tools/objtool/arch/arm64/include/arch_special.h create mode 100644 tools/objtool/arch/arm64/include/asm/orc_types.h create mode 100644 tools/objtool/arch/arm64/include/bit_operations.h create mode 100644 tools/objtool/arch/arm64/include/cfi.h create mode 100644 tools/objtool/arch/arm64/include/insn_decode.h create mode 100644 tools/objtool/arch/arm64/orc_gen.c create mode 100644 tools/objtool/arch/x86/include/arch_special.h rename tools/objtool/{ => arch/x86/include}/cfi.h (100%) rename tools/objtool/{ => arch/x86}/orc_gen.c (96%)--
2.17.1_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 8 lines · Ard Biesheuvel
Quoted 7 lines · Raphael Gault
Hi,
As of now, objtool only supports the
x86_64architecture but the groundwork has already been done in order to add support for other architecture without too much effort.
Quoted 7 lines
Quoted 3 lines
This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.
I think it makes sense to clarify *why* we want this on arm64. Also, we should identify things that objtool does today that maybe we don't want on arm64, rather than buy into all of it by default.
Agreed, the "why" should at least be in the cover letter. From my perspective, the "why" includes:
- Live patching - objtool stack validation is the foundation for a reliable unwinder
- ORC unwinder - benefits include presumed improved overall performance from disabling frame pointers, and the ability to unwind across interrupts and exceptions
- PeterZ's new uaccess validation?
Signature · 1 line
Josh
Quoted 17 lines · Josh Poimboeuf
Quoted 8 lines · Ard Biesheuvel
Quoted 7 lines · Raphael Gault
Hi,
As of now, objtool only supports the
x86_64architecture but the groundwork has already been done in order to add support for other architecture without too much effort.Quoted 7 lines
Quoted 3 lines
This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.
I think it makes sense to clarify *why* we want this on arm64. Also, we should identify things that objtool does today that maybe we don't want on arm64, rather than buy into all of it by default.
Quoted 6 lines
Agreed, the "why" should at least be in the cover letter. From my perspective, the "why" includes:
- Live patching - objtool stack validation is the foundation for a reliable unwinder
Yes, as I understand Live patching is a work in progress for arm64. Having objtool to provide more guarantees would be nice.
Quoted 4 lines
- ORC unwinder - benefits include presumed improved overall performance from disabling frame pointers, and the ability to unwind across interrupts and exceptions
I'm unsure this will be part of the plan. I believe so far arm64 code heavily relies on the presence of frame pointers. It's also part of the Aarch64 Procedure Call Standard.
But who knows.
Quoted 2 lines
- PeterZ's new uaccess validation?
Yes, we've reverted twice our implementation of user_access_begin/end() on arm64 because of the issue of potentially calling sleeping functions.
Once we have the base of objtool work, this would be one of the next work items.
Thanks,
Signature · 1 line
Julien Thierry
Quoted 20 lines · Raphael Gault
Hi,
As of now, objtool only supports the
x86_64architecture but the groundwork has already been done in order to add support for other architecture without too much effort.This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.
* Patch 1 adapts the existing code to be able to add support for other architecture.
* Patch 2 provide implementation of the required function for the arm64 architecture.
* Patch 3 adapts the checking of the stack state for the arm64 architecture.
* Patch 4 & 5 fix some warning objtool raised in some particular functions of~/arch/arm64/kernel/sleep.S. Patch 4 add a macro to signal that some function should be ignored by objtool.
* Patch 6 enables stack validation for arm64.
Quoted 8 lines
Theses patches should provide support for the main cases and behaviour. However a few corner cases are not yet handled by objtool:
* In the
~/arch/arm64/crypto/directory, I noticed that some plain data are sometimes stored in the.textsection causing objtool to mistake this for instructions and trying (and failing) to interprete them. If someone could explain to me why we store data directly in the .text section I would appreciate it.
I haven't looked, but it should probably be moved to .rodata. We had cases like that for x86.
Quoted 11 lines
* In the support for arm32 architecture such as in
~/arch/arm64/kernel/kuser32.Ssome A32 instructions are used but such instructions are not understood by objtool causing a warning.I also have a few unclear points I would like to bring to your attention:
* For
x86_64, when looking for a symbol relocation with explicit addend, objtool systematically adds a +4 offset to the addend. I don't understand why even if I have a feeling it is related to the type of relacation.
This is because of how relative call/jump addresses are implemented on x86. It calculates the call/jump destination by adding the encoded offset to the *ending* address of the instruction, rather than to the address of the encoded offset itself.
For example:
119ca0: e8 00 00 00 00 callq 119ca5 <__ia32_sys_sched_rr_get_interval+0x5>
119ca1: R_X86_64_PC32 __fentry__-0x4
This instruction is a call to the __fentry__ function. The rela addend is the address of the destination function (__fentry__) minus 4. After applying the relocation, it resolves to:
ffffffff81002010: e8 eb f7 9f 00 callq ffffffff81a01800 <__fentry__>
The destination address is "0x9ff7eb", which is indeed __fentry__ - 4.
x86 expects it to be that way, because the x86 CPU adds the offset to the *end* of the instruction: ffffffff81002015 (last digit is 5, not 0).
0xffffffff81002015 + 0x9ff7eb = 0xfffffff81a01800, which is indeed the address of __fentry__.
And there's always a 4-byte gap between the relocation target and the end of the instruction, so the rela addend always has the -4. So when reading the relocation we have to add the 4 bytes back to get the actual destination address.
Quoted 2 lines
* I currently don't have a clear understanding about how switch-tables are generated on arm64 and how to retrieve them (based on relocations).
This is indeed a bit tricky on x86.
Quoted 2 lines
Please provide me with any feedback and comments as well on the content than the style of these patches.
Overall it looks like a great start. I added some per-patch comments.
Has the cross-compile path been tested? Specifically, compiling for a arm64 target on an x86 host? In other words, objtool would be an x86 binary which reads arm64 objects. I imagine that will be a semi-common use case. Objtool already supports cross-compiling for an x86-64 target on an x86-32 host (and also a powerpc host IIRC), so it should be do-able in theory, and it might "just work".
For the next version, please base it on the -tip tree, as that's where all the latest objtool changes are. Peter refactored some code which has some minor conflicts with yours.
Signature · 1 line
Josh
Hi Josh,
Quoted 66 lines · Josh Poimboeuf
Quoted 20 lines · Raphael Gault
Hi,
As of now, objtool only supports the
x86_64architecture but the groundwork has already been done in order to add support for other architecture without too much effort.This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.
* Patch 1 adapts the existing code to be able to add support for other architecture.
* Patch 2 provide implementation of the required function for the arm64 architecture.
* Patch 3 adapts the checking of the stack state for the arm64 architecture.
* Patch 4 & 5 fix some warning objtool raised in some particular functions of~/arch/arm64/kernel/sleep.S. Patch 4 add a macro to signal that some function should be ignored by objtool.
* Patch 6 enables stack validation for arm64.Quoted 8 lines
Theses patches should provide support for the main cases and behaviour. However a few corner cases are not yet handled by objtool:
* In the
~/arch/arm64/crypto/directory, I noticed that some plain data are sometimes stored in the.textsection causing objtool to mistake this for instructions and trying (and failing) to interprete them. If someone could explain to me why we store data directly in the .text section I would appreciate it.I haven't looked, but it should probably be moved to .rodata. We had cases like that for x86.
Quoted 11 lines
* In the support for arm32 architecture such as in
~/arch/arm64/kernel/kuser32.Ssome A32 instructions are used but such instructions are not understood by objtool causing a warning.I also have a few unclear points I would like to bring to your attention:
* For
x86_64, when looking for a symbol relocation with explicit addend, objtool systematically adds a +4 offset to the addend. I don't understand why even if I have a feeling it is related to the type of relacation.This is because of how relative call/jump addresses are implemented on x86. It calculates the call/jump destination by adding the encoded offset to the *ending* address of the instruction, rather than to the address of the encoded offset itself.
For example:
119ca0: e8 00 00 00 00 callq 119ca5 <__ia32_sys_sched_rr_get_interval+0x5> 119ca1: R_X86_64_PC32 __fentry__-0x4This instruction is a call to the
__fentry__function. The rela addend is the address of the destination function (__fentry__) minus 4. After applying the relocation, it resolves to:
ffffffff81002010: e8 eb f7 9f 00 callqffffffff81a01800<__fentry__>The destination address is "
0x9ff7eb", which is indeed__fentry__- 4.x86 expects it to be that way, because the x86 CPU adds the offset to the *end* of the instruction:
ffffffff81002015(last digit is 5, not 0).
Quoted 8 lines
0xffffffff81002015+0x9ff7eb=0xfffffff81a01800, which is indeed the address of__fentry__.And there's always a 4-byte gap between the relocation target and the end of the instruction, so the rela addend always has the -4. So when reading the relocation we have to add the 4 bytes back to get the actual destination address.
Thank you for the explanation!
Quoted 10 lines
Quoted 2 lines
* I currently don't have a clear understanding about how switch-tables are generated on arm64 and how to retrieve them (based on relocations).
This is indeed a bit tricky on x86.
Quoted 2 lines
Please provide me with any feedback and comments as well on the content than the style of these patches.
Overall it looks like a great start. I added some per-patch comments.
Thank you, I will address them.
Quoted 7 lines
Has the cross-compile path been tested? Specifically, compiling for a arm64 target on an x86 host? In other words, objtool would be an x86 binary which reads arm64 objects. I imagine that will be a semi-common use case. Objtool already supports cross-compiling for an x86-64 target on an x86-32 host (and also a powerpc host IIRC), so it should be do-able in theory, and it might "just work".
It has indeed been tested to build for arm64 target on a x86 host and it works fine.
Quoted 4 lines
For the next version, please base it on the -tip tree, as that's where all the latest objtool changes are. Peter refactored some code which has some minor conflicts with yours.
Thanks for letting me know about this. Just a quick precision, when you talk about the -tip tree, are you talking about this tree ?
https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/
Thanks,
--
Raphael Gault
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Quoted 8 lines · Raphael Gault
Quoted 4 lines
For the next version, please base it on the -tip tree, as that's where all the latest objtool changes are. Peter refactored some code which has some minor conflicts with yours.
Thanks for letting me know about this. Just a quick precision, when you talk about the -tip tree, are you talking about this tree ?
https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/
Indeed. The git link I use is:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git
And generally the master branch of that tree seems to be a good one to use.
Signature · 1 line
Josh