Skip to contentSkip to conversation

objtool: Add support for arm64

RFC In review Raphael Gault 4 participants 40 messages Aug 16, 2019 – Oct 14, 2019

Subsystems (6)
Kbuild Crypto API arm64 KVM arm64 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v4 cover objtool: Add support for arm64

Hi,

Changes since RFC V3:
* Rebased on tip/master: Switch/jump table had been refactored
* Take Catalin Marinas comments into account regarding the asm macro for marking exceptions.

As of now, objtool only supports the x86_64 architecture but the groundwork has already been done in order to add support for other architectures without too much effort.

This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.

Objtool will be a valuable tool to progress and provide more guarentees on live patching which is a work in progress for arm64.

Once we have the base of objtool working the next steps will be to port Peter Z's uaccess validation for arm64.

Raphael Gault (18):
objtool: Add abstraction for computation of symbols offsets
objtool: orc: Refactor ORC API for other architectures to implement.
objtool: Move registers and control flow to arch-dependent code
objtool: arm64: Add required implementation for supporting the aarch64
architecture in objtool.
objtool: special: Adapt special section handling
objtool: arm64: Adapt the stack frame checks for arm architecture
objtool: Introduce INSN_UNKNOWN type
objtool: Refactor switch-tables code to support other architectures
gcc-plugins: objtool: Add plugin to detect switch table on arm64
objtool: arm64: Implement functions to add switch tables alternatives
arm64: alternative: Mark .altinstr_replacement as containing
executable instructions
arm64: assembler: Add macro to annotate asm function having non
standard stack-frame.
arm64: sleep: Prevent stack frame warnings from objtool
arm64: kvm: Annotate non-standard stack frame functions
arm64: kernel: Add exception on kuser32 to prevent stack analysis
arm64: crypto: Add exceptions for crypto object to prevent stack
analysis
arm64: kernel: Annotate non-standard stack frame functions
objtool: arm64: Enable stack validation for arm64

 arch/arm64/Kconfig                            |    1 +
 arch/arm64/crypto/Makefile                    |    3 +
 arch/arm64/include/asm/alternative.h          |    2 +-
 arch/arm64/kernel/Makefile                    |    3 +
 arch/arm64/kernel/hyp-stub.S                  |    3 +
 arch/arm64/kernel/sleep.S                     |    5 +
 arch/arm64/kvm/hyp-init.S                     |    3 +
 arch/arm64/kvm/hyp/entry.S                    |    3 +
 include/linux/frame.h                         |   19 +-
 scripts/Makefile.gcc-plugins                  |    2 +
 scripts/gcc-plugins/Kconfig                   |    9 +
 .../arm64_switch_table_detection_plugin.c     |   58 +
 tools/objtool/Build                           |    2 -
 tools/objtool/arch.h                          |   19 +
 tools/objtool/arch/arm64/Build                |    8 +
 tools/objtool/arch/arm64/arch_special.c       |  165 +
 tools/objtool/arch/arm64/bit_operations.c     |   67 +
 tools/objtool/arch/arm64/decode.c             | 2815 +++++++++++++++++
 .../objtool/arch/arm64/include/arch_special.h |   52 +
 .../arch/arm64/include/asm/orc_types.h        |   96 +
 .../arch/arm64/include/bit_operations.h       |   24 +
 tools/objtool/arch/arm64/include/cfi.h        |   74 +
 .../objtool/arch/arm64/include/insn_decode.h  |  210 ++
 tools/objtool/arch/arm64/orc_dump.c           |   26 +
 tools/objtool/arch/arm64/orc_gen.c            |   40 +
 tools/objtool/arch/x86/Build                  |    3 +
 tools/objtool/arch/x86/arch_special.c         |  107 +
 tools/objtool/arch/x86/decode.c               |   16 +
 tools/objtool/arch/x86/include/arch_special.h |   45 +
 tools/objtool/{ => arch/x86/include}/cfi.h    |    0
 tools/objtool/{ => arch/x86}/orc_dump.c       |    4 +-
 tools/objtool/{ => arch/x86}/orc_gen.c        |  104 +-
 tools/objtool/check.c                         |  309 +-
 tools/objtool/check.h                         |   10 +
 tools/objtool/elf.c                           |    3 +-
 tools/objtool/orc.h                           |    4 +-
 tools/objtool/special.c                       |   28 +-
 tools/objtool/special.h                       |   13 +-
 38 files changed, 4129 insertions(+), 226 deletions(-)
 create mode 100644 scripts/gcc-plugins/arm64_switch_table_detection_plugin.c
 create mode 100644 tools/objtool/arch/arm64/Build
 create mode 100644 tools/objtool/arch/arm64/arch_special.c
 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_dump.c
 create mode 100644 tools/objtool/arch/arm64/orc_gen.c
 create mode 100644 tools/objtool/arch/x86/arch_special.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_dump.c (98%)
 rename tools/objtool/{ => arch/x86}/orc_gen.c (66%)
Signature · 1 line
2.17.1
#1 lore raw

RFC v4 1/18 objtool: Add abstraction for computation of symbols offsets

The jump destination and relocation offset used previously are only reliable on x86_64 architecture. We abstract these computations by calling arch-dependent implementations.

---
 tools/objtool/arch.h            |  6 ++++++
 tools/objtool/arch/x86/decode.c | 11 +++++++++++
 tools/objtool/check.c           | 15 ++++++++++-----
 3 files changed, 27 insertions(+), 5 deletions(-)
tools/objtool/arch.h +6 -0
index ced3765c4f44..a9a50a25ca66 100644
@@ -66,6 +66,8 @@ struct stack_op {
 	struct op_src src;
 };
 
+struct instruction;
+
 void arch_initial_func_cfi_state(struct cfi_state *state);
 
 int arch_decode_instruction(struct elf *elf, struct section *sec,
@@ -75,4 +77,8 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,
 
 bool arch_callee_saved_reg(unsigned char reg);
 
+unsigned long arch_jump_destination(struct instruction *insn);
+
+unsigned long arch_dest_rela_offset(int addend);
+
 #endif /* _ARCH_H */
tools/objtool/arch/x86/decode.c +11 -0
index 0567c47a91b1..fa33b3465722 100644
@@ -11,6 +11,7 @@
 #include "lib/inat.c"
 #include "lib/insn.c"
 
+#include "../../check.h"
 #include "../../elf.h"
 #include "../../arch.h"
 #include "../../warn.h"
@@ -66,6 +67,11 @@ bool arch_callee_saved_reg(unsigned char reg)
 	}
 }
 
+unsigned long arch_dest_rela_offset(int addend)
+{
+	return addend + 4;
+}
+
 int arch_decode_instruction(struct elf *elf, struct section *sec,
 			    unsigned long offset, unsigned int maxlen,
 			    unsigned int *len, enum insn_type *type,
@@ -497,3 +503,8 @@ void arch_initial_func_cfi_state(struct cfi_state *state)
 	state->regs[16].base = CFI_CFA;
 	state->regs[16].offset = -8;
 }
+
+unsigned long arch_jump_destination(struct instruction *insn)
+{
+	return insn->offset + insn->len + insn->immediate;
+}
tools/objtool/check.c +10 -5
index 176f2f084060..479fab46b656 100644
@@ -563,7 +563,7 @@ static int add_jump_destinations(struct objtool_file *file)
 					       insn->len);
 		if (!rela) {
 			dest_sec = insn->sec;
-			dest_off = insn->offset + insn->len + insn->immediate;
+			dest_off = arch_jump_destination(insn);
 		} else if (rela->sym->type == STT_SECTION) {
 			dest_sec = rela->sym->sec;
 			dest_off = rela->addend + 4;
@@ -659,7 +659,7 @@ static int add_call_destinations(struct objtool_file *file)
 		rela = find_rela_by_dest_range(insn->sec, insn->offset,
 					       insn->len);
 		if (!rela) {
-			dest_off = insn->offset + insn->len + insn->immediate;
+			dest_off = arch_jump_destination(insn);
 			insn->call_dest = find_symbol_by_offset(insn->sec,
 								dest_off);
 
@@ -672,14 +672,19 @@ static int add_call_destinations(struct objtool_file *file)
 			}
 
 		} else if (rela->sym->type == STT_SECTION) {
+			/*
+			 * the original x86_64 code adds 4 to the rela->addend
+			 * which is not needed on arm64 architecture.
+			 */
+			dest_off = arch_dest_rela_offset(rela->addend);
 			insn->call_dest = find_symbol_by_offset(rela->sym->sec,
-								rela->addend+4);
+								dest_off);
 			if (!insn->call_dest ||
 			    insn->call_dest->type != STT_FUNC) {
-				WARN_FUNC("can't find call dest symbol at %s+0x%x",
+				WARN_FUNC("can't find call dest symbol at %s+0x%lx",
 					  insn->sec, insn->offset,
 					  rela->sym->sec->name,
-					  rela->addend + 4);
+					  dest_off);
 				return -1;
 			}
 		} else
Signature · 1 line
2.17.1
#2 lore raw

Re: [RFC v4 01/18] objtool: Add abstraction for computation of symbols offsets

Hi Raphaël,

Quoted 89 lines · Raphael Gault

The jump destination and relocation offset used previously are only reliable on x86_64 architecture. We abstract these computations by calling arch-dependent implementations.

---
  tools/objtool/arch.h            |  6 ++++++
  tools/objtool/arch/x86/decode.c | 11 +++++++++++
  tools/objtool/check.c           | 15 ++++++++++-----
  3 files changed, 27 insertions(+), 5 deletions(-)
tools/objtool/arch.h +6 -0
index ced3765c4f44..a9a50a25ca66 100644
@@ -66,6 +66,8 @@ struct stack_op {
  	struct op_src src;
  };
 
+struct instruction;
+
  void arch_initial_func_cfi_state(struct cfi_state *state);
 
  int arch_decode_instruction(struct elf *elf, struct section *sec,
@@ -75,4 +77,8 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,
 
  bool arch_callee_saved_reg(unsigned char reg);
 
+unsigned long arch_jump_destination(struct instruction *insn);
+
+unsigned long arch_dest_rela_offset(int addend);
+
  #endif /* _ARCH_H */
tools/objtool/arch/x86/decode.c +11 -0
index 0567c47a91b1..fa33b3465722 100644
@@ -11,6 +11,7 @@
  #include "lib/inat.c"
  #include "lib/insn.c"
 
+#include "../../check.h"
  #include "../../elf.h"
  #include "../../arch.h"
  #include "../../warn.h"
@@ -66,6 +67,11 @@ bool arch_callee_saved_reg(unsigned char reg)
  	}
  }
 
+unsigned long arch_dest_rela_offset(int addend)
+{
+	return addend + 4;
+}
+
  int arch_decode_instruction(struct elf *elf, struct section *sec,
  			    unsigned long offset, unsigned int maxlen,
  			    unsigned int *len, enum insn_type *type,
@@ -497,3 +503,8 @@ void arch_initial_func_cfi_state(struct cfi_state *state)
  	state->regs[16].base = CFI_CFA;
  	state->regs[16].offset = -8;
  }
+
+unsigned long arch_jump_destination(struct instruction *insn)
+{
+	return insn->offset + insn->len + insn->immediate;
+}
tools/objtool/check.c +2 -2
index 176f2f084060..479fab46b656 100644
@@ -563,7 +563,7 @@ static int add_jump_destinations(struct objtool_file *file)
  					       insn->len);
  		if (!rela) {
  			dest_sec = insn->sec;
-			dest_off = insn->offset + insn->len + insn->immediate;
+			dest_off = arch_jump_destination(insn);
  		} else if (rela->sym->type == STT_SECTION) {
  			dest_sec = rela->sym->sec;
  			dest_off = rela->addend + 4;
@@ -659,7 +659,7 @@ static int add_call_destinations(struct objtool_file *file)
  		rela = find_rela_by_dest_range(insn->sec, insn->offset,
  					       insn->len);
  		if (!rela) {
-			dest_off = insn->offset + insn->len + insn->immediate;
+			dest_off = arch_jump_destination(insn);
  			insn->call_dest = find_symbol_by_offset(insn->sec,
  								dest_off);
 

tools/objtool/check.c static int add_call_destinations(struct objtool_file *file) · line 678

Quoted 7 lines
tools/objtool/check.c +4 -0
@@ -672,14 +672,19 @@ static int add_call_destinations(struct objtool_file *file)
  			}
 
  		} else if (rela->sym->type == STT_SECTION) {
+			/*
+			 * the original x86_64 code adds 4 to the rela->addend
+			 * which is not needed on arm64 architecture.
+			 */

I'm not sure this is worth mentioning in generic code. You might include it in the commit message to justify the change.

Quoted 6 lines
tools/objtool/check.c +2 -1
+			dest_off = arch_dest_rela_offset(rela->addend);
  			insn->call_dest = find_symbol_by_offset(rela->sym->sec,
-								rela->addend+4);
+								dest_off);
  			if (!insn->call_dest ||

tools/objtool/check.c static int add_call_destinations(struct objtool_file *file) · line 690

Quoted 10 lines
tools/objtool/check.c +2 -2
  			    insn->call_dest->type != STT_FUNC) {
-				WARN_FUNC("can't find call dest symbol at %s+0x%x",
+				WARN_FUNC("can't find call dest symbol at %s+0x%lx",
  					  insn->sec, insn->offset,
  					  rela->sym->sec->name,
-					  rela->addend + 4);
+					  dest_off);
  				return -1;
  			}
  		} else

Otherwise, the change looks good to me.

Thanks,

Signature · 1 line
Julien Thierry
#3 lore raw

Re: [RFC v4 01/18] objtool: Add abstraction for computation of symbols offsets

In reply to Raphael Gault

tools/objtool/check.c static int add_call_destinations(struct objtool_file *file) · line 679

Quoted 9 lines · Raphael Gault
tools/objtool/check.c +5 -0
@@ -672,14 +672,19 @@ static int add_call_destinations(struct objtool_file *file)
 			}
 
 		} else if (rela->sym->type == STT_SECTION) {
+			/*
+			 * the original x86_64 code adds 4 to the rela->addend
+			 * which is not needed on arm64 architecture.
+			 */
+			dest_off = arch_dest_rela_offset(rela->addend);

I agree with Julien that this comment isn't needed. The "arch_" prefix already implies there are arch-specific differences.

Also, this patch should replace all the other "addend + 4" usages:

$ git grep "addend + 4" tools/objtool
tools/objtool/arch/x86/decode.c:        return addend + 4;
tools/objtool/check.c:                  dest_off = rela->addend + 4;
tools/objtool/check.c:                  dest_off = rela->sym->sym.st_value + rela->addend + 4;
Signature · 1 line
Josh
#4 lore raw

RFC v4 2/18 objtool: orc: Refactor ORC API for other architectures to implement.

In reply to Raphael Gault

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 dependent code.

---
 tools/objtool/Build                     |   2 -
 tools/objtool/arch.h                    |   3 +
 tools/objtool/arch/x86/Build            |   2 +
 tools/objtool/{ => arch/x86}/orc_dump.c |   4 +-
 tools/objtool/{ => arch/x86}/orc_gen.c  | 104 ++++++++++++++++++++++--
 tools/objtool/check.c                   |  99 +---------------------
 tools/objtool/orc.h                     |   4 +-
 7 files changed, 111 insertions(+), 107 deletions(-)
 rename tools/objtool/{ => arch/x86}/orc_dump.c (98%)
 rename tools/objtool/{ => arch/x86}/orc_gen.c (66%)
tools/objtool/Build +0 -2
index 8dc4f0848362..d069d26d97fa 100644
@@ -2,8 +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
 objtool-y += objtool.o
tools/objtool/arch.h +3 -0
index a9a50a25ca66..e91e12807678 100644
@@ -10,6 +10,7 @@
 #include <linux/list.h>
 #include "elf.h"
 #include "cfi.h"
+#include "orc.h"
 
 enum insn_type {
 	INSN_JUMP_CONDITIONAL,
@@ -77,6 +78,8 @@ 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_jump_destination(struct instruction *insn);
 
 unsigned long arch_dest_rela_offset(int addend);
tools/objtool/arch/x86/Build +2 -0
index b998412c017d..1f11b45999d0 100644
@@ -1,4 +1,6 @@
 objtool-y += decode.o
+objtool-y += orc_dump.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/orc_dump.c renamed from tools/objtool/orc_dump.c +2 -2
similarity index 98%
rename from tools/objtool/orc_dump.c
rename to tools/objtool/arch/x86/orc_dump.c
index 13ccf775a83a..cfe8f96bdd68 100644
@@ -4,8 +4,8 @@
  */
 
 #include <unistd.h>
-#include "orc.h"
-#include "warn.h"
+#include "../../orc.h"
+#include "../../warn.h"
 
 static const char *reg_name(unsigned int reg)
 {
tools/objtool/arch/x86/orc_gen.c renamed from tools/objtool/orc_gen.c +99 -5

120 lines not on this page · raw message

tools/objtool/check.c +3 -96

120 lines not on this page · raw message

tools/objtool/orc.h +2 -2
index ee2832221e62..fd7fa1d34a81 100644
@@ -10,8 +10,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);
 
Signature · 1 line
2.17.1
#5 lore raw

Re: [RFC v4 02/18] objtool: orc: Refactor ORC API for other architectures to implement.

Quoted 6 lines · Raphael Gault

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 dependent code.

A general comment for all the patch descriptions: they should use imperative language, like:

"move the implementation to the architecture-specific code."

Also the subjects shouldn't have periods.

It would be a good idea to review
Documentation/process/submitting-patches.rst.

Quoted 28 lines
tools/objtool/arch/x86/orc_gen.c renamed from tools/objtool/orc_gen.c +5 -5
@@ -6,11 +6,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;
 
@@ -116,7 +116,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/arch/x86/orc_gen.c int create_orc_sections(struct objtool_file *file) · line 214

Quoted 6 lines
tools/objtool/arch/x86/orc_gen.c renamed from tools/objtool/orc_gen.c +3 -0
@@ -209,3 +209,97 @@ int create_orc_sections(struct objtool_file *file)
 
 	return 0;
 }
+
+int arch_orc_read_unwind_hints(struct objtool_file *file)
+{

For naming consistency, please give them all an arch_orc prefix. Also I think arch_create_orc() should have a more specific name anyway, maybe arch_orc_init(). So:

arch_orc_init()
arch_orc_create_sections()
arch_orc_read_unwind_hints()

Signature · 1 line
Josh
#6 lore raw

RFC v4 3/18 objtool: Move registers and control flow to arch-dependent code

In reply to Raphael Gault

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).

---
 tools/objtool/arch/x86/include/arch_special.h | 36 +++++++++++++++++++
 tools/objtool/{ => arch/x86/include}/cfi.h    |  0
 tools/objtool/check.h                         |  1 +
 tools/objtool/special.c                       | 19 +---------
 4 files changed, 38 insertions(+), 18 deletions(-)
 create mode 100644 tools/objtool/arch/x86/include/arch_special.h
 rename tools/objtool/{ => arch/x86/include}/cfi.h (100%)
tools/objtool/arch/x86/include/arch_special.h new file +36 -0
new file mode 100644
index 000000000000..424ce47013e3
@@ -0,0 +1,36 @@
+/*
+ * 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 _X86_ARCH_SPECIAL_H
+#define _X86_ARCH_SPECIAL_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)
+#define X86_FEATURE_SMAP   (9 * 32 + 20)
+
+#endif /* _X86_ARCH_SPECIAL_H */
tools/objtool/arch/x86/include/cfi.h renamed from tools/objtool/cfi.h +0 -0
similarity index 100%
rename from tools/objtool/cfi.h
rename to tools/objtool/arch/x86/include/cfi.h
tools/objtool/check.h +1 -0
index 6d875ca6fce0..af87b55db454 100644
@@ -11,6 +11,7 @@
 #include "cfi.h"
 #include "arch.h"
 #include "orc.h"
+#include "arch_special.h"
 #include <linux/hashtable.h>
 
 struct insn_state {
tools/objtool/special.c +1 -18
index fdbaa611146d..b8ccee1b5382 100644
@@ -14,24 +14,7 @@
 #include "builtin.h"
 #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)
-#define X86_FEATURE_SMAP   (9*32+20)
+#include "arch_special.h"
 
 struct special_entry {
 	const char *sec;
Signature · 1 line
2.17.1
#7 lore raw

Re: [RFC v4 03/18] objtool: Move registers and control flow to arch-dependent code

tools/objtool/arch/x86/include/arch_special.h

Quoted 21 lines · Raphael Gault

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).

---
 tools/objtool/arch/x86/include/arch_special.h | 36 +++++++++++++++++++
 tools/objtool/{ => arch/x86/include}/cfi.h    |  0
 tools/objtool/check.h                         |  1 +
 tools/objtool/special.c                       | 19 +---------
 4 files changed, 38 insertions(+), 18 deletions(-)
 create mode 100644 tools/objtool/arch/x86/include/arch_special.h
 rename tools/objtool/{ => arch/x86/include}/cfi.h (100%)
tools/objtool/arch/x86/include/arch_special.h new file +0 -0
new file mode 100644
index 000000000000..424ce47013e3

The arch in the filename is redundant. It would be a bit nicer if it were named "special.h" but I guess that would conflict with the non-arch special.h, unless we put non-arch headers and arch headers in separate namespaces somehow. Maybe it's ok for now unless anybody has a better idea.

Quoted 5 lines
tools/objtool/arch/x86/include/arch_special.h new file +4 -0
@@ -0,0 +1,36 @@
+/*
+ * 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/>.
+ */

Instead of the above can you use the SPDX identifier?

/* SPDX-License-Identifier: GPL-2.0-or-later */

And the same comment for the other headers in the patch set.

Signature · 1 line
Josh
#8 lore raw

RFC v4 4/18 objtool: arm64: Add required implementation for supporting the aarch64 architecture in objtool.

In reply to 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.

In order to handle the load/store instructions for a pair of registers we add an extra field to the stack_op structure.

We consider that the hypervisor/secure-monitor is behaving correctly. This enables us to handle hvc/smc/svc context switching instructions as nop since we consider that the context is restored correctly.

---
 tools/objtool/arch.h                          |    7 +
 tools/objtool/arch/arm64/Build                |    7 +
 tools/objtool/arch/arm64/bit_operations.c     |   67 +
 tools/objtool/arch/arm64/decode.c             | 2787 +++++++++++++++++
 .../objtool/arch/arm64/include/arch_special.h |   36 +
 .../arch/arm64/include/asm/orc_types.h        |   96 +
 .../arch/arm64/include/bit_operations.h       |   24 +
 tools/objtool/arch/arm64/include/cfi.h        |   74 +
 .../objtool/arch/arm64/include/insn_decode.h  |  211 ++
 tools/objtool/arch/arm64/orc_dump.c           |   26 +
 tools/objtool/arch/arm64/orc_gen.c            |   40 +
 11 files changed, 3375 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_dump.c
 create mode 100644 tools/objtool/arch/arm64/orc_gen.c
tools/objtool/arch.h +7 -0
index e91e12807678..bb5ce810fb6e 100644
@@ -62,9 +62,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/Build new file +7 -0
new file mode 100644
index 000000000000..bf7a32c2b9e9
@@ -0,0 +1,7 @@
+objtool-y += decode.o
+objtool-y += orc_dump.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 +67 -0

67 lines not on this page · raw message

tools/objtool/arch/arm64/decode.c new file +2787 -0

2,787 lines not on this page · raw message

tools/objtool/arch/arm64/include/arch_special.h new file +36 -0
new file mode 100644
index 000000000000..63da775d0581
@@ -0,0 +1,36 @@
+/*
+ * 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 _ARM64_ARCH_SPECIAL_H
+#define _ARM64_ARCH_SPECIAL_H
+
+#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
+
+#define X86_FEATURE_POPCNT (4 * 32 + 23)
+#define X86_FEATURE_SMAP   (9 * 32 + 20)
+
+#endif /* _ARM64_ARCH_SPECIAL_H */
tools/objtool/arch/arm64/include/asm/orc_types.h new file +96 -0

87 lines not on this page · raw message

tools/objtool/arch/arm64/include/bit_operations.h new file +24 -0
new file mode 100644
index 000000000000..c198c0724b7c

Lines of this file are not shown. View the raw message.

tools/objtool/arch/arm64/include/cfi.h new file +74 -0

74 lines not on this page · raw message

tools/objtool/arch/arm64/include/insn_decode.h new file +211 -0

211 lines not on this page · raw message

tools/objtool/arch/arm64/orc_dump.c new file +26 -0
new file mode 100644
index 000000000000..d1dc39829c9e

Lines of this file are not shown. View the raw message.

tools/objtool/arch/arm64/orc_gen.c new file +40 -0
new file mode 100644
index 000000000000..81383d34a743

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.

#9 lore raw

Re: [RFC v4 04/18] objtool: arm64: Add required implementation for supporting the aarch64 architecture in objtool.

Quoted 8 lines · Raphael Gault
tools/objtool/arch.h +0 -0
index e91e12807678..bb5ce810fb6e 100644
@@ -62,9 +62,16 @@ struct op_src {
 	int offset;
 };
 

tools/objtool/arch.h struct op_src { · line 74

Quoted 10 lines
tools/objtool/arch.h +7 -0
+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;

Maybe the arch-specific data structure should just be named 'arch' instead of 'extra'.

Quoted 27 lines
tools/objtool/arch/arm64/include/arch_special.h new file +24 -0
@@ -0,0 +1,36 @@
+/*
+ * 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 _ARM64_ARCH_SPECIAL_H
+#define _ARM64_ARCH_SPECIAL_H
+
+#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

tools/objtool/arch/arm64/include/arch_special.h line 34

Quoted 10 lines
tools/objtool/arch/arm64/include/arch_special.h new file +10 -0
+
+#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
+
+#define X86_FEATURE_POPCNT (4 * 32 + 23)
+#define X86_FEATURE_SMAP   (9 * 32 + 20)

It's weird to have these x86-specific macros here. I guess they're needed to compile because the later patch hasn't abstracted it out yet.

This patch should really come later in the series, *after* all the arch-specific bits have been abstracted out of the generic code.

Quoted 94 lines
tools/objtool/arch/arm64/include/arch_special.h new file +2 -0
+
+#endif /* _ARM64_ARCH_SPECIAL_H */
tools/objtool/arch/arm64/include/asm/orc_types.h new file +86 -0
new file mode 100644
index 000000000000..9b04885eb785
@@ -0,0 +1,96 @@
+/*
+ * 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;

tools/objtool/arch/arm64/include/asm/orc_types.h line 96

Quoted 10 lines
tools/objtool/arch/arm64/include/asm/orc_types.h new file +10 -0
+	s16		bp_offset;
+	unsigned	sp_reg:4;
+	unsigned	bp_reg:4;
+	unsigned	type:2;
+	unsigned	end:1;
+} __packed;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* _ORC_TYPES_H */

Since ORC is now treated as arch-specific, and not yet implemented for arm64, can this header file just be (basically) empty for now?

Signature · 1 line
Josh
#10 lore raw

RFC v4 5/18 objtool: special: Adapt special section handling

In reply to Raphael Gault

This patch abstracts the few architecture dependent tests that are perform when handling special section and switch tables. It enables any architecture to ignore a particular CPU feature or not to handle switch tables.

---
 tools/objtool/arch/arm64/Build                |  1 +
 tools/objtool/arch/arm64/arch_special.c       | 22 +++++++++++++++
 .../objtool/arch/arm64/include/arch_special.h | 10 +++++--
 tools/objtool/arch/x86/Build                  |  1 +
 tools/objtool/arch/x86/arch_special.c         | 28 +++++++++++++++++++
 tools/objtool/arch/x86/include/arch_special.h |  9 ++++++
 tools/objtool/check.c                         | 24 ++++++++++++++--
 tools/objtool/special.c                       |  9 ++----
 tools/objtool/special.h                       |  3 ++
 9 files changed, 96 insertions(+), 11 deletions(-)
 create mode 100644 tools/objtool/arch/arm64/arch_special.c
 create mode 100644 tools/objtool/arch/x86/arch_special.c
tools/objtool/arch/arm64/Build +1 -0
index bf7a32c2b9e9..3d09be745a84 100644
@@ -1,3 +1,4 @@
+objtool-y += arch_special.o
 objtool-y += decode.o
 objtool-y += orc_dump.o
 objtool-y += orc_gen.o
tools/objtool/arch/arm64/arch_special.c new file +22 -0
new file mode 100644
index 000000000000..a21d28876317
@@ -0,0 +1,22 @@
+/*
+ * 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/>.
+ */
+#include "../../special.h"
+#include "arch_special.h"
+
+void arch_force_alt_path(unsigned short feature,
+			 bool uaccess,
+			 struct special_alt *alt)
+{
+}
tools/objtool/arch/arm64/include/arch_special.h +8 -2
index 63da775d0581..185103be8a51 100644
@@ -30,7 +30,13 @@
 #define ALT_ORIG_LEN_OFFSET	10
 #define ALT_NEW_LEN_OFFSET	11
 
-#define X86_FEATURE_POPCNT (4 * 32 + 23)
-#define X86_FEATURE_SMAP   (9 * 32 + 20)
+static inline bool arch_should_ignore_feature(unsigned short feature)
+{
+	return false;
+}
 
+static inline bool arch_support_switch_table(void)
+{
+	return false;
+}
 #endif /* _ARM64_ARCH_SPECIAL_H */
tools/objtool/arch/x86/Build +1 -0
index 1f11b45999d0..63e167775bc8 100644
@@ -1,3 +1,4 @@
+objtool-y += arch_special.o
 objtool-y += decode.o
 objtool-y += orc_dump.o
 objtool-y += orc_gen.o
tools/objtool/arch/x86/arch_special.c new file +28 -0
new file mode 100644
index 000000000000..6583a1770bb2
@@ -0,0 +1,28 @@
+/*
+ * 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/>.
+ */
+#include "../../special.h"
+#include "arch_special.h"
+
+void arch_force_alt_path(unsigned short feature,
+			 bool uaccess,
+			 struct special_alt *alt)
+{
+		if (feature == X86_FEATURE_SMAP) {
+			if (uaccess)
+				alt->skip_orig = true;
+			else
+				alt->skip_alt = true;
+		}
+}
tools/objtool/arch/x86/include/arch_special.h +9 -0
index 424ce47013e3..fce2b1193194 100644
@@ -33,4 +33,13 @@
 #define X86_FEATURE_POPCNT (4 * 32 + 23)
 #define X86_FEATURE_SMAP   (9 * 32 + 20)
 
+static inline bool arch_should_ignore_feature(unsigned short feature)
+{
+	return feature == X86_FEATURE_POPCNT;
+}
+
+static inline bool arch_support_switch_table(void)
+{
+	return true;
+}
 #endif /* _X86_ARCH_SPECIAL_H */
tools/objtool/check.c +22 -2
index 30e147391dcb..4af6422d3428 100644
@@ -729,7 +729,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)) {
 		fake_jump = malloc(sizeof(*fake_jump));
 		if (!fake_jump) {
 			WARN("malloc failed");
@@ -1061,6 +1061,26 @@ static struct rela *find_jump_table(struct objtool_file *file,
 		table_rela = find_rela_by_dest(table_sec, table_offset);
 		if (!table_rela)
 			continue;
+		/*
+		 * If we are on arm64 architecture, we now that we
+		 * 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 (!arch_support_switch_table()) {
+			file->ignore_unreachables = true;
+			return NULL;
+		}
+
+		rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
+		if (rodata_rela) {
+			/*
+			 * Use of RIP-relative switch jumps is quite rare, and
+			 * indicates a rare GCC quirk/bug which can leave dead
+			 * code behind.
+			 */
+			if (text_rela->type == R_X86_64_PC32)
+				file->ignore_unreachables = true;
 
 		/*
 		 * Use of RIP-relative switch jumps is quite rare, and
@@ -1864,7 +1884,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
 	insn = first;
 	sec = insn->sec;
 
-	if (insn->alt_group && list_empty(&insn->alts)) {
+	if (!insn->visited && insn->alt_group && list_empty(&insn->alts)) {
 		WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
 			  sec, insn->offset);
 		return 1;
tools/objtool/special.c +2 -7
index b8ccee1b5382..7a0092d6e5b3 100644
@@ -81,7 +81,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
 		 * feature path which is a "very very small percentage of
 		 * machines".
 		 */
-		if (feature == X86_FEATURE_POPCNT)
+		if (arch_should_ignore_feature(feature))
 			alt->skip_orig = true;
 
 		/*
@@ -93,12 +93,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
 		 * find paths that see the STAC but take the NOP instead of
 		 * CLAC and the other way around.
 		 */
-		if (feature == X86_FEATURE_SMAP) {
-			if (uaccess)
-				alt->skip_orig = true;
-			else
-				alt->skip_alt = true;
-		}
+		arch_force_alt_path(feature, uaccess, alt);
 	}
 
 	orig_rela = find_rela_by_dest(sec, offset + entry->orig);
tools/objtool/special.h +3 -0
index 35061530e46e..90626a7e41cf 100644
@@ -27,5 +27,8 @@ struct special_alt {
 };
 
 int special_get_alts(struct elf *elf, struct list_head *alts);
+void arch_force_alt_path(unsigned short feature,
+			 bool uaccess,
+			 struct special_alt *alt);
 
 #endif /* _SPECIAL_H */
Signature · 1 line
2.17.1
#11 lore raw

Re: [RFC v4 05/18] objtool: special: Adapt special section handling

Quoted 1 line · Raphael Gault

This patch abstracts the few architecture dependent tests that are

The patch description shouldn't talk about the patch specifically, but should rather describe what it does, in imperative language.

Quoted 1 line

perform when handling special section and switch tables. It enables any

"performed"

Quoted 45 lines

architecture to ignore a particular CPU feature or not to handle switch tables.

---
 tools/objtool/arch/arm64/Build                |  1 +
 tools/objtool/arch/arm64/arch_special.c       | 22 +++++++++++++++
 .../objtool/arch/arm64/include/arch_special.h | 10 +++++--
 tools/objtool/arch/x86/Build                  |  1 +
 tools/objtool/arch/x86/arch_special.c         | 28 +++++++++++++++++++
 tools/objtool/arch/x86/include/arch_special.h |  9 ++++++
 tools/objtool/check.c                         | 24 ++++++++++++++--
 tools/objtool/special.c                       |  9 ++----
 tools/objtool/special.h                       |  3 ++
 9 files changed, 96 insertions(+), 11 deletions(-)
 create mode 100644 tools/objtool/arch/arm64/arch_special.c
 create mode 100644 tools/objtool/arch/x86/arch_special.c
tools/objtool/arch/arm64/Build +1 -0
index bf7a32c2b9e9..3d09be745a84 100644
@@ -1,3 +1,4 @@
+objtool-y += arch_special.o
 objtool-y += decode.o
 objtool-y += orc_dump.o
 objtool-y += orc_gen.o
tools/objtool/arch/arm64/arch_special.c new file +12 -0
new file mode 100644
index 000000000000..a21d28876317
@@ -0,0 +1,22 @@
+/*
+ * 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

tools/objtool/arch/arm64/arch_special.c line 22

Quoted 10 lines
tools/objtool/arch/arm64/arch_special.c new file +10 -0
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "../../special.h"
+#include "arch_special.h"
+
+void arch_force_alt_path(unsigned short feature,
+			 bool uaccess,
+			 struct special_alt *alt)
+{
+}

Instead of these dedicated files with empty .c functions -- including the arm64 orc .c files -- I'd rather just have them be empty static inline functions in header files, like the kernel does.

tools/objtool/arch/x86/arch_special.c

Quoted 34 lines
tools/objtool/arch/arm64/include/arch_special.h +8 -2
index 63da775d0581..185103be8a51 100644
@@ -30,7 +30,13 @@
 #define ALT_ORIG_LEN_OFFSET	10
 #define ALT_NEW_LEN_OFFSET	11
 
-#define X86_FEATURE_POPCNT (4 * 32 + 23)
-#define X86_FEATURE_SMAP   (9 * 32 + 20)
+static inline bool arch_should_ignore_feature(unsigned short feature)
+{
+	return false;
+}
 
+static inline bool arch_support_switch_table(void)
+{
+	return false;
+}
 #endif /* _ARM64_ARCH_SPECIAL_H */
tools/objtool/arch/x86/Build +1 -0
index 1f11b45999d0..63e167775bc8 100644
@@ -1,3 +1,4 @@
+objtool-y += arch_special.o
 objtool-y += decode.o
 objtool-y += orc_dump.o
 objtool-y += orc_gen.o
tools/objtool/arch/x86/arch_special.c new file +0 -0
new file mode 100644
index 000000000000..6583a1770bb2

The "arch_" is redundant, these files can just be named "special.c".

Quoted 19 lines
tools/objtool/arch/x86/arch_special.c new file +18 -0
@@ -0,0 +1,28 @@
+/*
+ * 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/>.
+ */
+#include "../../special.h"
+#include "arch_special.h"
+
+void arch_force_alt_path(unsigned short feature,

tools/objtool/arch/x86/arch_special.c line 28

Quoted 10 lines
tools/objtool/arch/x86/arch_special.c new file +10 -0
+			 bool uaccess,
+			 struct special_alt *alt)
+{
+		if (feature == X86_FEATURE_SMAP) {
+			if (uaccess)
+				alt->skip_orig = true;
+			else
+				alt->skip_alt = true;
+		}
+}

Bad indention.

Quoted 23 lines
tools/objtool/arch/x86/include/arch_special.h +9 -0
index 424ce47013e3..fce2b1193194 100644
@@ -33,4 +33,13 @@
 #define X86_FEATURE_POPCNT (4 * 32 + 23)
 #define X86_FEATURE_SMAP   (9 * 32 + 20)
 
+static inline bool arch_should_ignore_feature(unsigned short feature)
+{
+	return feature == X86_FEATURE_POPCNT;
+}
+
+static inline bool arch_support_switch_table(void)
+{
+	return true;
+}
 #endif /* _X86_ARCH_SPECIAL_H */
tools/objtool/check.c +0 -0
index 30e147391dcb..4af6422d3428 100644

tools/objtool/check.c static int handle_group_alt(struct objtool_file *file, · line 732

Quoted 5 lines
tools/objtool/check.c +1 -1
@@ -729,7 +729,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)) {

Even if the reason for the change is trivial, these types of changes (and the insn->visited change below) should each be in an earlier separate patch which explains the reasoning. Putting each logical change in its own patch helps with review, and also future bisections, debugging and archaeology.

tools/objtool/check.c static struct rela *find_jump_table(struct objtool_file *file, · line 1065

Quoted 9 lines
tools/objtool/check.c +2 -0
 		fake_jump = malloc(sizeof(*fake_jump));
 		if (!fake_jump) {
 			WARN("malloc failed");
@@ -1061,6 +1061,26 @@ static struct rela *find_jump_table(struct objtool_file *file,
 		table_rela = find_rela_by_dest(table_sec, table_offset);
 		if (!table_rela)
 			continue;
+		/*
+		 * If we are on arm64 architecture, we now that we

"know"

tools/objtool/check.c static struct rela *find_jump_table(struct objtool_file *file, · line 1073

Quoted 8 lines
tools/objtool/check.c +8 -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 (!arch_support_switch_table()) {
+			file->ignore_unreachables = true;
+			return NULL;
+		}

All arches need to support switch tables, otherwise it's a major gap in functionality.

I think you did it this way because the switch table support comes in a later patch, right? If so, the patches should be reordered so that you don't need to add arch_support_switch_table() in the middle.

tools/objtool/check.c static struct rela *find_jump_table(struct objtool_file *file, · line 1086

Quoted 13 lines
tools/objtool/check.c +10 -0
+
+		rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
+		if (rodata_rela) {
+			/*
+			 * Use of RIP-relative switch jumps is quite rare, and
+			 * indicates a rare GCC quirk/bug which can leave dead
+			 * code behind.
+			 */
+			if (text_rela->type == R_X86_64_PC32)
+				file->ignore_unreachables = true;
 
 		/*
 		 * Use of RIP-relative switch jumps is quite rare, and

This repeats code which already exists right below it?

Quoted 23 lines
tools/objtool/check.c +1 -1
@@ -1864,7 +1884,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
 	insn = first;
 	sec = insn->sec;
 
-	if (insn->alt_group && list_empty(&insn->alts)) {
+	if (!insn->visited && insn->alt_group && list_empty(&insn->alts)) {
 		WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
 			  sec, insn->offset);
 		return 1;
tools/objtool/special.c +1 -1
index b8ccee1b5382..7a0092d6e5b3 100644
@@ -81,7 +81,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
 		 * feature path which is a "very very small percentage of
 		 * machines".
 		 */
-		if (feature == X86_FEATURE_POPCNT)
+		if (arch_should_ignore_feature(feature))
 			alt->skip_orig = true;
 
 		/*

tools/objtool/special.c static int get_alt_entry(struct elf *elf, struct special_entry *entry, · line 96

Quoted 10 lines
tools/objtool/special.c +1 -6
@@ -93,12 +93,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
 		 * find paths that see the STAC but take the NOP instead of
 		 * CLAC and the other way around.
 		 */
-		if (feature == X86_FEATURE_SMAP) {
-			if (uaccess)
-				alt->skip_orig = true;
-			else
-				alt->skip_alt = true;
-		}
+		arch_force_alt_path(feature, uaccess, alt);

Instead of arch_force_alt_path(), maybe it could be something like:

		if (arch_is_uaccess_feature(alt))
			if (uaccess)
				alt->skip_orig = true;
			else
				alt->skip_alt = true;

That helps keep the common bits common, and even better it makes the code clearer.

Quoted 19 lines
tools/objtool/special.c +0 -0
 	}
 
 	orig_rela = find_rela_by_dest(sec, offset + entry->orig);
tools/objtool/special.h +3 -0
index 35061530e46e..90626a7e41cf 100644
@@ -27,5 +27,8 @@ struct special_alt {
 };
 
 int special_get_alts(struct elf *elf, struct list_head *alts);
+void arch_force_alt_path(unsigned short feature,
+			 bool uaccess,
+			 struct special_alt *alt);
 
 #endif /* _SPECIAL_H */
Signature · 1 line
2.17.1
Signature · 1 line
Josh
#12 lore raw

Re: [RFC v4 05/18] objtool: special: Adapt special section handling

In reply to Raphael Gault

Hi Raphaël,

Quoted 4 lines · Raphael Gault

This patch abstracts the few architecture dependent tests that are perform when handling special section and switch tables. It enables any architecture to ignore a particular CPU feature or not to handle switch tables.

I think it would be better if this patch only focused on CPU features and leave dealing with switch table to subsequent patches.

Quoted 61 lines
---
  tools/objtool/arch/arm64/Build                |  1 +
  tools/objtool/arch/arm64/arch_special.c       | 22 +++++++++++++++
  .../objtool/arch/arm64/include/arch_special.h | 10 +++++--
  tools/objtool/arch/x86/Build                  |  1 +
  tools/objtool/arch/x86/arch_special.c         | 28 +++++++++++++++++++
  tools/objtool/arch/x86/include/arch_special.h |  9 ++++++
  tools/objtool/check.c                         | 24 ++++++++++++++--
  tools/objtool/special.c                       |  9 ++----
  tools/objtool/special.h                       |  3 ++
  9 files changed, 96 insertions(+), 11 deletions(-)

create mode 100644 tools/objtool/arch/arm64/arch_special.c create mode 100644 tools/objtool/arch/x86/arch_special.c

tools/objtool/arch/arm64/Build +1 -0
index bf7a32c2b9e9..3d09be745a84 100644
@@ -1,3 +1,4 @@
+objtool-y += arch_special.o
  objtool-y += decode.o
  objtool-y += orc_dump.o
  objtool-y += orc_gen.o
tools/objtool/arch/arm64/arch_special.c new file +22 -0
new file mode 100644
index 000000000000..a21d28876317
@@ -0,0 +1,22 @@
+/*
+ * 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/>.
+ */
+#include "../../special.h"
+#include "arch_special.h"
+
+void arch_force_alt_path(unsigned short feature,
+			 bool uaccess,
+			 struct special_alt *alt)
+{
+}
tools/objtool/arch/arm64/include/arch_special.h +0 -0
index 63da775d0581..185103be8a51 100644
@@ -30,7 +30,13 @@
  #define ALT_ORIG_LEN_OFFSET	10
  #define ALT_NEW_LEN_OFFSET	11
 

tools/objtool/arch/arm64/include/arch_special.h line 40

Quoted 10 lines
tools/objtool/arch/arm64/include/arch_special.h +7 -2
-#define X86_FEATURE_POPCNT (4 * 32 + 23)
-#define X86_FEATURE_SMAP   (9 * 32 + 20)
+static inline bool arch_should_ignore_feature(unsigned short feature)
+{
+	return false;
+}
 
+static inline bool arch_support_switch_table(void)
+{
+	return false;

If I understand correctly , this gets later (patch 8) replaced by arch_find_switch_table() which can return NULL if unable to find a switch table.

So, is it necessary to introduce this function at this stage of the patchset? Can't we just have directly arch_find_switch_table() ?

Also, I believe that in the end you keep the two arch_support_switch_table() implementations (x86 and arm64) despite getting rid of the only caller (in patch 8).

Quoted 26 lines
tools/objtool/arch/arm64/include/arch_special.h +1 -0
+}
  #endif /* _ARM64_ARCH_SPECIAL_H */
tools/objtool/arch/x86/Build +1 -0
index 1f11b45999d0..63e167775bc8 100644
@@ -1,3 +1,4 @@
+objtool-y += arch_special.o
  objtool-y += decode.o
  objtool-y += orc_dump.o
  objtool-y += orc_gen.o
tools/objtool/arch/x86/arch_special.c new file +9 -0
new file mode 100644
index 000000000000..6583a1770bb2
@@ -0,0 +1,28 @@
+/*
+ * 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

tools/objtool/arch/x86/arch_special.c line 19

Quoted 10 lines
tools/objtool/arch/x86/arch_special.c new file +10 -0
+ * 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/>.
+ */
+#include "../../special.h"
+#include "arch_special.h"
+
+void arch_force_alt_path(unsigned short feature,
+			 bool uaccess,

Maybe you could have something like:

void arch_select_alt_path(unsigned short feature, struct special_alt *alt);

or arch_process_alt_inst(...);

This way you just let the arch decide what to do for alternative instructions associated with their features, and call it in the "if (entry->feature)" branch of get_alt_entry().

You don't need to pass the "uaccess" as that info is globally accessible from builtin.h.

Quoted 14 lines
tools/objtool/arch/x86/arch_special.c new file +9 -0
+			 struct special_alt *alt)
+{
+		if (feature == X86_FEATURE_SMAP) {
+			if (uaccess)
+				alt->skip_orig = true;
+			else
+				alt->skip_alt = true;
+		}
+}
tools/objtool/arch/x86/include/arch_special.h +0 -0
index 424ce47013e3..fce2b1193194 100644

tools/objtool/arch/x86/include/arch_special.h line 39

Quoted 7 lines
tools/objtool/arch/x86/include/arch_special.h +4 -0
@@ -33,4 +33,13 @@
  #define X86_FEATURE_POPCNT (4 * 32 + 23)
  #define X86_FEATURE_SMAP   (9 * 32 + 20)
 
+static inline bool arch_should_ignore_feature(unsigned short feature)
+{
+	return feature == X86_FEATURE_POPCNT;
+}

With my above suggestion you shouldn't need that and just use the arch specific alternative handling set the alt->skip_orig.

Quoted 88 lines
tools/objtool/arch/x86/include/arch_special.h +5 -0
+
+static inline bool arch_support_switch_table(void)
+{
+	return true;
+}
  #endif /* _X86_ARCH_SPECIAL_H */
tools/objtool/check.c +22 -2
index 30e147391dcb..4af6422d3428 100644
@@ -729,7 +729,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)) {
  		fake_jump = malloc(sizeof(*fake_jump));
  		if (!fake_jump) {
  			WARN("malloc failed");
@@ -1061,6 +1061,26 @@ static struct rela *find_jump_table(struct objtool_file *file,
  		table_rela = find_rela_by_dest(table_sec, table_offset);
  		if (!table_rela)
  			continue;
+		/*
+		 * If we are on arm64 architecture, we now that we
+		 * 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 (!arch_support_switch_table()) {
+			file->ignore_unreachables = true;
+			return NULL;
+		}
+
+		rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
+		if (rodata_rela) {
+			/*
+			 * Use of RIP-relative switch jumps is quite rare, and
+			 * indicates a rare GCC quirk/bug which can leave dead
+			 * code behind.
+			 */
+			if (text_rela->type == R_X86_64_PC32)
+				file->ignore_unreachables = true;
 
  		/*
  		 * Use of RIP-relative switch jumps is quite rare, and
@@ -1864,7 +1884,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
  	insn = first;
  	sec = insn->sec;
 
-	if (insn->alt_group && list_empty(&insn->alts)) {
+	if (!insn->visited && insn->alt_group && list_empty(&insn->alts)) {
  		WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
  			  sec, insn->offset);
  		return 1;
tools/objtool/special.c +2 -7
index b8ccee1b5382..7a0092d6e5b3 100644
@@ -81,7 +81,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
  		 * feature path which is a "very very small percentage of
  		 * machines".
  		 */
-		if (feature == X86_FEATURE_POPCNT)
+		if (arch_should_ignore_feature(feature))
  			alt->skip_orig = true;
 
  		/*
@@ -93,12 +93,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
  		 * find paths that see the STAC but take the NOP instead of
  		 * CLAC and the other way around.
  		 */
-		if (feature == X86_FEATURE_SMAP) {
-			if (uaccess)
-				alt->skip_orig = true;
-			else
-				alt->skip_alt = true;
-		}
+		arch_force_alt_path(feature, uaccess, alt);
  	}
 
  	orig_rela = find_rela_by_dest(sec, offset + entry->orig);
tools/objtool/special.h +0 -0
index 35061530e46e..90626a7e41cf 100644

tools/objtool/special.h struct special_alt { · line 34

Quoted 8 lines
tools/objtool/special.h +3 -0
@@ -27,5 +27,8 @@ struct special_alt {
  };
 
  int special_get_alts(struct elf *elf, struct list_head *alts);
+void arch_force_alt_path(unsigned short feature,
+			 bool uaccess,
+			 struct special_alt *alt);
 
  #endif /* _SPECIAL_H */

CHeers,

Signature · 1 line
Julien Thierry
#13 lore raw

RFC v4 6/18 objtool: arm64: Adapt the stack frame checks for arm architecture

In reply to Raphael Gault

Since 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.

---
 tools/objtool/arch.h              |   2 +
 tools/objtool/arch/arm64/decode.c |  28 +++++++++
 tools/objtool/arch/x86/decode.c   |   5 ++
 tools/objtool/check.c             | 100 ++++++++++++++++++++++++++++--
 tools/objtool/elf.c               |   3 +-
 5 files changed, 131 insertions(+), 7 deletions(-)
tools/objtool/arch.h +2 -0
index bb5ce810fb6e..68d6371a24a2 100644
@@ -91,4 +91,6 @@ unsigned long arch_jump_destination(struct instruction *insn);
 
 unsigned long arch_dest_rela_offset(int addend);
 
+bool arch_is_insn_sibling_call(struct instruction *insn);
+
 #endif /* _ARCH_H */
tools/objtool/arch/arm64/decode.c +28 -0
index 395c5777afab..be3d2eb10227 100644
@@ -106,6 +106,34 @@ unsigned long arch_dest_rela_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 = list_entry(l, struct instruction, list);
+		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)
+			return false;
+		else if (sym->type == STT_FUNC)
+			return true;
+		break;
+	}
+	return false;
+}
 static int is_arm64(struct elf *elf)
 {
 	switch (elf->ehdr.e_machine) {
tools/objtool/arch/x86/decode.c +5 -0
index fa33b3465722..98726990714d 100644
@@ -72,6 +72,11 @@ unsigned long arch_dest_rela_offset(int addend)
 	return addend + 4;
 }
 
+bool arch_is_insn_sibling_call(struct instruction *insn)
+{
+	return true;
+}
+
 int arch_decode_instruction(struct elf *elf, struct section *sec,
 			    unsigned long offset, unsigned int maxlen,
 			    unsigned int *len, enum insn_type *type,
tools/objtool/check.c +94 -6

182 lines not on this page · raw message

tools/objtool/elf.c +2 -1
index edba4745f25a..c6ac0b771b73 100644
@@ -62,7 +62,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
#14 lore raw

Re: [RFC v4 06/18] objtool: arm64: Adapt the stack frame checks for arm architecture

Quoted 26 lines · Raphael Gault
tools/objtool/arch/arm64/decode.c +18 -0
index 395c5777afab..be3d2eb10227 100644
@@ -106,6 +106,34 @@ unsigned long arch_dest_rela_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 = list_entry(l, struct instruction, list);
+		if (!prev->func ||
+		    prev->func->pfunc != insn->func->pfunc)
+			return false;
+		if (prev->stack_op.src.reg != ADR_SOURCE)

tools/objtool/arch/arm64/decode.c unsigned long arch_dest_rela_offset(int addend) · line 136

Quoted 10 lines
tools/objtool/arch/arm64/decode.c +10 -0
+			continue;
+		sym = find_symbol_containing(insn->sec, insn->immediate);
+		if (!sym || sym->type != STT_FUNC)
+			return false;
+		else if (sym->type == STT_FUNC)
+			return true;
+		break;
+	}
+	return false;
+}

As Peter said, going backwards is going to be fragile:

https://lkml.kernel.org/r/[email protected]

Now that there's the GCC plugin for annotating switch tables, can we use information from the plugin to distinguish sibling calls from switch tables?

Or if that doesn't work for some reason, you may need some logic in validate_branch() to do the above properly.

tools/objtool/arch/arm64/decode.c unsigned long arch_dest_rela_offset(int addend) · line 79

Quoted 10 lines
tools/objtool/arch/arm64/decode.c +6 -0
+++ b/tools/objtool/arch/x86/decode.c
@@ -72,6 +72,11 @@ unsigned long arch_dest_rela_offset(int addend)
 	return addend + 4;
 }
 
+bool arch_is_insn_sibling_call(struct instruction *insn)
+{
+	return true;
+}
+

The semantics still aren't right -- not all instructions are sibling calls on x86.

Quoted 8 lines
tools/objtool/arch/arm64/decode.c +0 -0
 int arch_decode_instruction(struct elf *elf, struct section *sec,
 			    unsigned long offset, unsigned int maxlen,
 			    unsigned int *len, enum insn_type *type,
tools/objtool/check.c +0 -0
index 4af6422d3428..519569b0329f 100644

tools/objtool/check.c static int add_jump_destinations(struct objtool_file *file) · line 572

Quoted 9 lines
tools/objtool/check.c +2 -2
@@ -566,10 +566,10 @@ static int add_jump_destinations(struct objtool_file *file)
 			dest_off = arch_jump_destination(insn);
 		} else if (rela->sym->type == STT_SECTION) {
 			dest_sec = rela->sym->sec;
-			dest_off = rela->addend + 4;
+			dest_off = arch_dest_rela_offset(rela->addend);
 		} else if (rela->sym->sec->idx) {
 			dest_sec = rela->sym->sec;
-			dest_off = rela->sym->sym.st_value + rela->addend + 4;
+			dest_off = rela->sym->sym.st_value + arch_dest_rela_offset(rela->addend);

These changes should be in patch 1.

Quoted 164 lines
tools/objtool/check.c +87 -3
 		} else if (strstr(rela->sym->name, "_indirect_thunk_")) {
 			/*
 			 * Retpoline jumps are really dynamic jumps in
@@ -1368,8 +1368,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;
 }
 
 /*
@@ -1525,8 +1525,32 @@ 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;
 			}
 
@@ -1537,6 +1561,15 @@ 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 */
@@ -1629,6 +1662,22 @@ 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;
+
+			if (op->extra.used) {
+				if (regs[op->extra.reg].offset == -state->stack_size)
+					restore_reg(state, op->extra.reg);
+				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;
 
@@ -1648,12 +1697,22 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
 				/* drap: mov disp(%rbp), %reg */
 				restore_reg(state, op->dest.reg);
 
+				if (op->extra.used &&
+				    op->src.reg == CFI_BP &&
+				    op->extra.offset == regs[op->extra.reg].offset)
+					restore_reg(state, op->extra.reg);
+
 			} else if (op->src.reg == cfa->base &&
 			    op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
 
 				/* mov disp(%rbp), %reg */
 				/* mov disp(%rsp), %reg */
 				restore_reg(state, op->dest.reg);
+
+				if (op->extra.used &&
+				    op->src.reg == cfa->base &&
+				    op->extra.offset == regs[op->extra.reg].offset + cfa->offset)
+					restore_reg(state, op->extra.reg);
 			}
 
 			break;
@@ -1669,6 +1728,8 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
 	case OP_DEST_PUSH:
 	case OP_DEST_PUSHF:
 		state->stack_size += 8;
+		if (cfa->base == CFI_CFA)
+			cfa->base = CFI_SP;
 		if (cfa->base == CFI_SP)
 			cfa->offset += 8;
 
@@ -1702,6 +1763,21 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
 			save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
 		}
 
+		if (op->extra.used) {
+			state->stack_size += 8;
+			if (cfa->base == CFI_CFA)
+				cfa->base = CFI_SP;
+			if (cfa->base == CFI_SP)
+				cfa->offset += 8;
+			if (!state->drap ||
+			    (!(op->extra.reg == cfa->base &&
+			       op->extra.reg == state->drap_reg) &&
+			     !(op->extra.reg == CFI_BP &&
+			       cfa->base == state->drap_reg) &&
+			     regs[op->extra.reg].base == CFI_UNDEFINED))
+			save_reg(state, op->extra.reg, CFI_CFA,
+				 -state->stack_size);
+		}
 		/* detect when asm code uses rbp as a scratch register */
 		if (!no_fp && insn->func && op->src.reg == CFI_BP &&
 		    cfa->base != CFI_BP)
@@ -1720,11 +1796,19 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
 				/* save drap offset so we know when to restore it */
 				state->drap_offset = op->dest.offset;
 			}
+			if (op->extra.used && op->extra.reg == cfa->base &&
+			    op->extra.reg == state->drap_reg) {
+				cfa->base = CFI_BP_INDIRECT;
+				cfa->offset = op->extra.offset;
+			}
 
 			else if (regs[op->src.reg].base == CFI_UNDEFINED) {
 
 				/* drap: mov reg, disp(%rbp) */
 				save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
+				if (op->extra.used)
+					save_reg(state, op->extra.reg, CFI_BP,
+						 op->extra.offset);
 			}
 
 		} else if (op->dest.reg == cfa->base) {
@@ -1733,8 +1817,12 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
 			/* mov reg, disp(%rsp) */
 			save_reg(state, op->src.reg, CFI_CFA,

tools/objtool/check.c static int update_insn_state(struct instruction *insn, struct insn_state *state) · line 1828

Quoted 10 lines
tools/objtool/check.c +4 -0
 				 op->dest.offset - state->cfa.offset);
+			if (op->extra.used)
+				save_reg(state, op->extra.reg, CFI_CFA,
+					 op->extra.offset - state->cfa.offset);
 		}
 
+
 		break;
 
 	case OP_DEST_LEAVE:

TBH, all these update_insn_state() changes make me nervous, as this code was already a bit rickety and magical. I'll need to review this much more carefully at some point.

If it would be feasible to split the changes up somehow in separate patches, with a description behind the reasoning for each change, that may help a lot.

Quoted 14 lines
tools/objtool/check.c +1 -1
@@ -1857,7 +1945,7 @@ static int validate_call(struct instruction *insn, struct insn_state *state)
 
 static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
 {
-	if (has_modified_stack_frame(state)) {
+	if (arch_is_insn_sibling_call(insn) && has_modified_stack_frame(state)) {
 		WARN_FUNC("sibling call from callable instruction with modified stack frame",
 				insn->sec, insn->offset);
 		return 1;
tools/objtool/elf.c +0 -0
index edba4745f25a..c6ac0b771b73 100644

tools/objtool/elf.c struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset) · line 68

Quoted 8 lines
tools/objtool/elf.c +2 -1
@@ -62,7 +62,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;

Here's another one that I think belongs in a separate patch.

Signature · 1 line
Josh
#15 lore raw

RFC v4 7/18 objtool: Introduce INSN_UNKNOWN type

In reply to Raphael Gault

On arm64 some object files contain data stored in the .text section. This data is interpreted by objtool as instruction but can't be identified as a valid one. In order to keep analysing those files we introduce INSN_UNKNOWN type. The "unknown instruction" warning will thus only be raised if such instructions are uncountered while validating an execution branch.

This change doesn't impact the x86 decoding logic since 0 is still used as a way to specify an unknown type, raising the "unknown instruction" warning during the decoding phase still.

---
 tools/objtool/arch.h                           |  1 +
 tools/objtool/arch/arm64/decode.c              |  8 ++++----
 tools/objtool/arch/arm64/include/insn_decode.h |  4 ++--
 tools/objtool/check.c                          | 10 +++++++++-
 4 files changed, 16 insertions(+), 7 deletions(-)
tools/objtool/arch.h +1 -0
index 68d6371a24a2..9f2590e1df79 100644
@@ -28,6 +28,7 @@ enum insn_type {
 	INSN_CLAC,
 	INSN_STD,
 	INSN_CLD,
+	INSN_UNKNOWN,
 	INSN_OTHER,
 };
 
tools/objtool/arch/arm64/decode.c +4 -4
index be3d2eb10227..4cb9402d6fe1 100644
@@ -37,9 +37,9 @@
  */
 static arm_decode_class aarch64_insn_class_decode_table[] = {
 	[INSN_RESERVED]			= arm_decode_reserved,
-	[INSN_UNKNOWN]			= arm_decode_unknown,
+	[INSN_UNALLOC_1]		= arm_decode_unknown,
 	[INSN_SVE_ENC]			= arm_decode_sve_encoding,
-	[INSN_UNALLOC]			= arm_decode_unknown,
+	[INSN_UNALLOC_2]		= arm_decode_unknown,
 	[INSN_LD_ST_4]			= arm_decode_ld_st,
 	[INSN_DP_REG_5]			= arm_decode_dp_reg,
 	[INSN_LD_ST_6]			= arm_decode_ld_st,
@@ -191,7 +191,7 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,
 int arm_decode_unknown(u32 instr, unsigned char *type,
 		       unsigned long *immediate, struct stack_op *op)
 {
-	*type = 0;
+	*type = INSN_UNKNOWN;
 	return 0;
 }
 
@@ -206,7 +206,7 @@ int arm_decode_reserved(u32 instr, unsigned char *type,
 			unsigned long *immediate, struct stack_op *op)
 {
 	*immediate = instr & ONES(16);
-	*type = INSN_BUG;
+	*type = INSN_UNKNOWN;
 	return 0;
 }
 
tools/objtool/arch/arm64/include/insn_decode.h +2 -2
index eb54fc39dca5..a01d76306749 100644
@@ -20,9 +20,9 @@
 #include "../../../arch.h"
 
 #define INSN_RESERVED	0b0000
-#define INSN_UNKNOWN	0b0001
+#define INSN_UNALLOC_1	0b0001
 #define INSN_SVE_ENC	0b0010
-#define INSN_UNALLOC	0b0011
+#define INSN_UNALLOC_2	0b0011
 #define INSN_DP_IMM	0b1001	//0x100x
 #define INSN_BRANCH	0b1011	//0x101x
 #define INSN_LD_ST_4	0b0100	//0bx1x0
tools/objtool/check.c +9 -1
index 519569b0329f..baa6a93f37cd 100644
@@ -1981,6 +1981,13 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
 	while (1) {
 		next_insn = next_insn_same_sec(file, insn);
 
+		if (insn->type == INSN_UNKNOWN) {
+			WARN("%s+0x%lx unknown instruction type, should never be reached",
+			     insn->sec->name,
+			     insn->offset);
+			return 1;
+		}
+
 		if (file->c_file && func && insn->func && func != insn->func->pfunc) {
 			WARN("%s() falls through to next function %s()",
 			     func->name, insn->func->name);
@@ -2414,7 +2421,8 @@ static int validate_reachable_instructions(struct objtool_file *file)
 		return 0;
 
 	for_each_insn(file, insn) {
-		if (insn->visited || ignore_unreachable_insn(insn))
+		if (insn->visited || ignore_unreachable_insn(insn) ||
+		    insn->type == INSN_UNKNOWN)
 			continue;
 
 		WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
Signature · 1 line
2.17.1
#16 lore raw

Re: [RFC v4 07/18] objtool: Introduce INSN_UNKNOWN type

Quoted 7 lines · Raphael Gault

On arm64 some object files contain data stored in the .text section. This data is interpreted by objtool as instruction but can't be identified as a valid one. In order to keep analysing those files we introduce INSN_UNKNOWN type. The "unknown instruction" warning will thus only be raised if such instructions are uncountered while validating an execution branch.

Quoted 5 lines

This change doesn't impact the x86 decoding logic since 0 is still used as a way to specify an unknown type, raising the "unknown instruction" warning during the decoding phase still.

Is there a reason such data can't be moved to .rodata? That would seem like the proper fix.

Signature · 1 line
Josh
#17 lore raw

Re: [RFC v4 07/18] objtool: Introduce INSN_UNKNOWN type

Hi Josh,

Quoted 8 lines · Josh Poimboeuf
Quoted 7 lines · Raphael Gault

On arm64 some object files contain data stored in the .text section. This data is interpreted by objtool as instruction but can't be identified as a valid one. In order to keep analysing those files we introduce INSN_UNKNOWN type. The "unknown instruction" warning will thus only be raised if such instructions are uncountered while validating an execution branch.

Quoted 9 lines
Quoted 5 lines

This change doesn't impact the x86 decoding logic since 0 is still used as a way to specify an unknown type, raising the "unknown instruction" warning during the decoding phase still.

Is there a reason such data can't be moved to .rodata? That would seem like the proper fix.

Raphaël can confirm, if I remember correctly, that issue was encountered on assembly files implementing crypto algorithms were some words/double-words of data were in the middle of the .text. I think it is done this way to make sure the data can be loaded in a single instruction. So moving it to another section could impact the crypto performance depending on the relocations.

That was my understanding at least.

Cheers,

Signature · 1 line
Julien Thierry
#18 lore raw

Re: [RFC v4 07/18] objtool: Introduce INSN_UNKNOWN type

Quoted 21 lines · Julien

Hi Josh,

Quoted 8 lines · Josh Poimboeuf
Quoted 7 lines · Raphael Gault

On arm64 some object files contain data stored in the .text section. This data is interpreted by objtool as instruction but can't be identified as a valid one. In order to keep analysing those files we introduce INSN_UNKNOWN type. The "unknown instruction" warning will thus only be raised if such instructions are uncountered while validating an execution branch.

Quoted 9 lines
Quoted 5 lines

This change doesn't impact the x86 decoding logic since 0 is still used as a way to specify an unknown type, raising the "unknown instruction" warning during the decoding phase still.

Is there a reason such data can't be moved to .rodata? That would seem like the proper fix.

Quoted 7 lines

Raphaël can confirm, if I remember correctly, that issue was encountered on assembly files implementing crypto algorithms were some words/double-words of data were in the middle of the .text. I think it is done this way to make sure the data can be loaded in a single instruction. So moving it to another section could impact the crypto performance depending on the relocations.

That was my understanding at least.

Thanks. If that's the case then that would be useful information to put in the patch description. A code excerpt of an example code site would be useful too.

I'm not sure INSN_UNKNOWN is the right name though, since the decoder does actually know about it. Maybe INSN_DATA or something?

Signature · 1 line
Josh
#19 lore raw

RFC v4 8/18 objtool: Refactor switch-tables code to support other architectures

In reply to Raphael Gault

The way to identify switch-tables and retrieves all the data necessary to handle the different execution branches is not the same on all architecture. In order to be able to add other architecture support, this patch defines arch-dependent functions to process jump-tables.

---
 tools/objtool/arch/arm64/arch_special.c | 15 ++++
 tools/objtool/arch/arm64/decode.c       |  4 +-
 tools/objtool/arch/x86/arch_special.c   | 79 ++++++++++++++++++++
 tools/objtool/check.c                   | 95 +------------------------
 tools/objtool/check.h                   |  7 ++
 tools/objtool/special.h                 | 10 ++-
 6 files changed, 114 insertions(+), 96 deletions(-)
tools/objtool/arch/arm64/arch_special.c +15 -0
index a21d28876317..17a8a06aac2a 100644
@@ -20,3 +20,18 @@ void arch_force_alt_path(unsigned short feature,
 			 struct special_alt *alt)
 {
 }
+
+int arch_add_jump_table(struct objtool_file *file, struct instruction *insn,
+			struct rela *table, struct rela *next_table)
+{
+	return 0;
+}
+
+struct rela *arch_find_switch_table(struct objtool_file *file,
+				  struct rela *text_rela,
+				  struct section *rodata_sec,
+				  unsigned long table_offset)
+{
+	file->ignore_unreachables = true;
+	return NULL;
+}
tools/objtool/arch/arm64/decode.c +2 -2
index 4cb9402d6fe1..a20725c1bfd7 100644
@@ -159,7 +159,7 @@ static int is_arm64(struct elf *elf)
 
 int arch_decode_instruction(struct elf *elf, struct section *sec,
 			    unsigned long offset, unsigned int maxlen,
-			    unsigned int *len, unsigned char *type,
+			    unsigned int *len, enum insn_type *type,
 			    unsigned long *immediate, struct stack_op *op)
 {
 	int arm64 = 0;
@@ -184,7 +184,7 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,
 	insn = *(u32 *)(sec->data->d_buf + offset);
 
 	//dispatch according to encoding classes
-	return aarch64_insn_class_decode_table[(insn >> 25) & 0xf](insn, type,
+	return aarch64_insn_class_decode_table[(insn >> 25) & 0xf](insn, (unsigned char *)type,
 							immediate, op);
 }
 
tools/objtool/arch/x86/arch_special.c +79 -0

82 lines not on this page · raw message

tools/objtool/check.c +2 -93

121 lines not on this page · raw message

tools/objtool/check.h +7 -0
index af87b55db454..267759760a3d 100644
@@ -13,6 +13,7 @@
 #include "orc.h"
 #include "arch_special.h"
 #include <linux/hashtable.h>
+;
 
 struct insn_state {
 	struct cfi_reg cfa;
@@ -48,6 +49,12 @@ struct instruction {
 	struct orc_entry orc;
 };
 
+struct alternative {
+	struct list_head list;
+	struct instruction *insn;
+	bool skip_orig;
+};
+
 struct objtool_file {
 	struct elf *elf;
 	struct list_head insn_list;
tools/objtool/special.h +9 -1
index 90626a7e41cf..9b1f968a4325 100644
@@ -7,7 +7,10 @@
 #define _SPECIAL_H
 
 #include <stdbool.h>
+#include <stdlib.h>
+#include "check.h"
 #include "elf.h"
+#include "warn.h"
 
 struct special_alt {
 	struct list_head list;
@@ -30,5 +33,10 @@ int special_get_alts(struct elf *elf, struct list_head *alts);
 void arch_force_alt_path(unsigned short feature,
 			 bool uaccess,
 			 struct special_alt *alt);
-
+int arch_add_jump_table(struct objtool_file *file, struct instruction *insn,
+			struct rela *table, struct rela *next_table);
+struct rela *arch_find_switch_table(struct objtool_file *file,
+				  struct rela *text_rela,
+				  struct section *rodata_sec,
+				  unsigned long table_offset);
 #endif /* _SPECIAL_H */
Signature · 1 line
2.17.1
#20 lore raw

Re: [RFC v4 08/18] objtool: Refactor switch-tables code to support other architectures

Quoted 28 lines · Raphael Gault

The way to identify switch-tables and retrieves all the data necessary to handle the different execution branches is not the same on all architecture. In order to be able to add other architecture support, this patch defines arch-dependent functions to process jump-tables.

---
 tools/objtool/arch/arm64/arch_special.c | 15 ++++
 tools/objtool/arch/arm64/decode.c       |  4 +-
 tools/objtool/arch/x86/arch_special.c   | 79 ++++++++++++++++++++
 tools/objtool/check.c                   | 95 +------------------------
 tools/objtool/check.h                   |  7 ++
 tools/objtool/special.h                 | 10 ++-
 6 files changed, 114 insertions(+), 96 deletions(-)
tools/objtool/arch/arm64/arch_special.c +5 -0
index a21d28876317..17a8a06aac2a 100644
@@ -20,3 +20,18 @@ void arch_force_alt_path(unsigned short feature,
 			 struct special_alt *alt)
 {
 }
+
+int arch_add_jump_table(struct objtool_file *file, struct instruction *insn,
+			struct rela *table, struct rela *next_table)
+{
+	return 0;

tools/objtool/arch/arm64/arch_special.c void arch_force_alt_path(unsigned short feature, · line 37

Quoted 10 lines
tools/objtool/arch/arm64/arch_special.c +10 -0
+}
+
+struct rela *arch_find_switch_table(struct objtool_file *file,
+				  struct rela *text_rela,
+				  struct section *rodata_sec,
+				  unsigned long table_offset)
+{
+	file->ignore_unreachables = true;
+	return NULL;
+}

If this refactoring is done before adding arm64 support then you won't need intermediate hacks like this.

Signature · 1 line
Josh
#21 lore raw

RFC v4 9/18 gcc-plugins: objtool: Add plugin to detect switch table on arm64

In reply to Raphael Gault

This plugins comes into play before the final 2 RTL passes of GCC and detects switch-tables that are to be outputed in the ELF and writes information in an "objtool_data" section which will be used by objtool.

---
 scripts/Makefile.gcc-plugins                  |  2 +
 scripts/gcc-plugins/Kconfig                   |  9 +++
 .../arm64_switch_table_detection_plugin.c     | 58 +++++++++++++++++++
 3 files changed, 69 insertions(+)
 create mode 100644 scripts/gcc-plugins/arm64_switch_table_detection_plugin.c
scripts/Makefile.gcc-plugins +2 -0
index 5f7df50cfe7a..a56736df9dc2 100644
@@ -44,6 +44,8 @@ ifdef CONFIG_GCC_PLUGIN_ARM_SSP_PER_TASK
 endif
 export DISABLE_ARM_SSP_PER_TASK_PLUGIN
 
+gcc-plugin-$(CONFIG_GCC_PLUGIN_SWITCH_TABLES)	+= arm64_switch_table_detection_plugin.so
+
 # All the plugin CFLAGS are collected here in case a build target needs to
 # filter them out of the KBUILD_CFLAGS.
 GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
scripts/gcc-plugins/Kconfig +9 -0
index d33de0b9f4f5..1daeffb55dce 100644
@@ -113,4 +113,13 @@ config GCC_PLUGIN_ARM_SSP_PER_TASK
 	bool
 	depends on GCC_PLUGINS && ARM
 
+config GCC_PLUGIN_SWITCH_TABLES
+	bool "GCC Plugin: Identify switch tables at compile time"
+	default y
+	depends on STACK_VALIDATION && ARM64
+	help
+	  Plugin to identify switch tables generated at compile time and store
+	  them in a .objtool_data section. Objtool will then use that section
+	  to analyse the different execution path of the switch table.
+
 endmenu
scripts/gcc-plugins/arm64_switch_table_detection_plugin.c new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.17.1
#22 lore raw

Re: [RFC v4 09/18] gcc-plugins: objtool: Add plugin to detect switch table on arm64

Quoted 1 line · Raphael Gault

This plugins comes into play before the final 2 RTL passes of GCC and

"plugin"

Quoted 2 lines

detects switch-tables that are to be outputed in the ELF and writes information in an "objtool_data" section which will be used by objtool.

The section should probably have a ".discard" prefix (.discard.objtool_data) so it gets discarded at link time.

Also, "objtool_data" is a bit generic. How about
".discard.switch_tables" or something.

Quoted 28 lines
---
 scripts/Makefile.gcc-plugins                  |  2 +
 scripts/gcc-plugins/Kconfig                   |  9 +++
 .../arm64_switch_table_detection_plugin.c     | 58 +++++++++++++++++++
 3 files changed, 69 insertions(+)
 create mode 100644 scripts/gcc-plugins/arm64_switch_table_detection_plugin.c
scripts/Makefile.gcc-plugins +2 -0
index 5f7df50cfe7a..a56736df9dc2 100644
@@ -44,6 +44,8 @@ ifdef CONFIG_GCC_PLUGIN_ARM_SSP_PER_TASK
 endif
 export DISABLE_ARM_SSP_PER_TASK_PLUGIN
 
+gcc-plugin-$(CONFIG_GCC_PLUGIN_SWITCH_TABLES)	+= arm64_switch_table_detection_plugin.so
+
 # All the plugin CFLAGS are collected here in case a build target needs to
 # filter them out of the KBUILD_CFLAGS.
 GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
scripts/gcc-plugins/Kconfig +0 -0
index d33de0b9f4f5..1daeffb55dce 100644
@@ -113,4 +113,13 @@ config GCC_PLUGIN_ARM_SSP_PER_TASK
 	bool

scripts/gcc-plugins/Kconfig config GCC_PLUGIN_ARM_SSP_PER_TASK · line 123

Quoted 10 lines
scripts/gcc-plugins/Kconfig +8 -0
 	depends on GCC_PLUGINS && ARM
 
+config GCC_PLUGIN_SWITCH_TABLES
+	bool "GCC Plugin: Identify switch tables at compile time"
+	default y
+	depends on STACK_VALIDATION && ARM64
+	help
+	  Plugin to identify switch tables generated at compile time and store
+	  them in a .objtool_data section. Objtool will then use that section
+	  to analyse the different execution path of the switch table.

This isn't something you want to ask the user about, as objtool for arm64 requires it. For the same reason, instead of GCC_PLUGIN_SWITCH_TABLES depending on STACK_VALIDATION, arm64 HAVE_STACK_VALIDATION should depend on GCC_PLUGIN_SWITCH_TABLES.

Signature · 1 line
Josh
#23 lore raw

RFC v4 10/18 objtool: arm64: Implement functions to add switch tables alternatives

In reply to Raphael Gault

This patch implements the functions required to identify and add as alternatives all the possible destinations of the switch table. This implementation relies on the new plugin introduced previously which records information about the switch-table in a .objtool_data section.

---
 tools/objtool/arch/arm64/arch_special.c       | 132 +++++++++++++++++-
 .../objtool/arch/arm64/include/arch_special.h |  10 ++
 .../objtool/arch/arm64/include/insn_decode.h  |   3 +-
 tools/objtool/check.c                         |   6 +-
 tools/objtool/check.h                         |   2 +
 5 files changed, 146 insertions(+), 7 deletions(-)
tools/objtool/arch/arm64/arch_special.c +130 -2
index 17a8a06aac2a..11284066157c 100644
@@ -12,8 +12,13 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
+
+#include <stdlib.h>
+#include <string.h>
+
 #include "../../special.h"
 #include "arch_special.h"
+#include "bit_operations.h"
 
 void arch_force_alt_path(unsigned short feature,
 			 bool uaccess,
@@ -21,9 +26,133 @@ void arch_force_alt_path(unsigned short feature,
 {
 }
 
+static u32 next_offset(u8 *table, u8 entry_size)
+{
+	switch (entry_size) {
+	case 1:
+		return table[0];
+	case 2:
+		return *(u16 *)(table);
+	default:
+		return *(u32 *)(table);
+	}
+}
+
+static u32 get_table_entry_size(u32 insn)
+{
+	unsigned char size = (insn >> 30) & ONES(2);
+	switch (size) {
+	case 0:
+		return 1;
+	case 1:
+		return 2;
+	default:
+		return 4;
+	}
+}
+
+static int add_possible_branch(struct objtool_file *file,
+			       struct instruction *insn,
+			       u32 base, u32 offset)
+{
+	struct instruction *dest_insn;
+	struct alternative *alt;
+	offset = base + 4 * offset;
+
+	alt = calloc(1, sizeof(*alt));
+	if (alt == NULL) {
+		WARN("allocation failure, can't add jump alternative");
+		return -1;
+	}
+
+	dest_insn = find_insn(file, insn->sec, offset);
+	if (dest_insn == NULL) {
+		free(alt);
+		return 0;
+	}
+	alt->insn = dest_insn;
+	alt->skip_orig = true;
+	list_add_tail(&alt->list, &insn->alts);
+	return 0;
+}
+
 int arch_add_jump_table(struct objtool_file *file, struct instruction *insn,
 			struct rela *table, struct rela *next_table)
 {
+	struct rela *objtool_data_rela = NULL;
+	struct switch_table_info *swt_info = NULL;
+	struct section *objtool_data = find_section_by_name(file->elf, ".objtool_data");
+	struct section *rodata_sec = find_section_by_name(file->elf, ".rodata");
+	struct section *branch_sec = NULL;
+	u8 *switch_table = NULL;
+	u64 base_offset = 0;
+	struct instruction *pre_jump_insn;
+	u32 sec_size = 0;
+	u32 entry_size = 0;
+	u32 offset = 0;
+	u32 i, j;
+
+	if (objtool_data == NULL)
+		return 0;
+
+	/*
+	 * 1. Using rela, Identify entry for the switch table
+	 * 2. Retrieve base offset
+	 * 3. Retrieve branch instruction
+	 * 3. For all entries in switch table:
+	 * 	3.1. Compute new offset
+	 * 	3.2. Create alternative instruction
+	 * 	3.3. Add alt_instr to insn->alts list
+	 */
+	sec_size = objtool_data->sh.sh_size;
+	for (i = 0, swt_info = (void *)objtool_data->data->d_buf;
+	     i < sec_size / sizeof(struct switch_table_info);
+	     i++, swt_info++) {
+		offset = i * sizeof(struct switch_table_info);
+		objtool_data_rela = find_rela_by_dest_range(objtool_data, offset,
+							    sizeof(u64));
+		/* retrieving the objtool data of the switch table we need */
+		if (objtool_data_rela == NULL ||
+		    table->sym->sec != objtool_data_rela->sym->sec ||
+		    table->addend != objtool_data_rela->addend)
+			continue;
+
+		/* retrieving switch table content */
+		switch_table = (u8 *)(rodata_sec->data->d_buf + table->addend);
+
+		/* retrieving pre jump instruction (ldr) */
+		branch_sec = insn->sec;
+		pre_jump_insn = find_insn(file, branch_sec,
+					  insn->offset - 3 * sizeof(u32));
+		entry_size = get_table_entry_size(*(u32 *)(branch_sec->data->d_buf + pre_jump_insn->offset));
+
+		/*
+		 * iterating over the pre-jumps instruction in order to
+		 * retrieve switch base offset.
+		 */
+		while (pre_jump_insn != NULL &&
+		       pre_jump_insn->offset <= insn->offset) {
+			if (pre_jump_insn->stack_op.src.reg == ADR_SOURCE) {
+				base_offset = pre_jump_insn->offset +
+					      pre_jump_insn->immediate;
+				/*
+				 * Once we have the switch table entry size
+				 * we add every possible destination using
+				 * alternatives of the original branch
+				 * instruction
+				 */
+				for (j = 0; j < swt_info->nb_entries; j++) {
+					if (add_possible_branch(file, insn,
+								base_offset,
+								next_offset(switch_table, entry_size))) {
+						return -1;
+					}
+					switch_table += entry_size;
+				}
+			}
+			pre_jump_insn = next_insn_same_sec(file, pre_jump_insn);
+		}
+	}
 	return 0;
 }
 
@@ -32,6 +161,5 @@ struct rela *arch_find_switch_table(struct objtool_file *file,
 				  struct section *rodata_sec,
 				  unsigned long table_offset)
 {
-	file->ignore_unreachables = true;
-	return NULL;
+	return text_rela;
 }
tools/objtool/arch/arm64/include/arch_special.h +10 -0
index 185103be8a51..cba432fed80f 100644
@@ -15,6 +15,8 @@
 #ifndef _ARM64_ARCH_SPECIAL_H
 #define _ARM64_ARCH_SPECIAL_H
 
+#include <linux/types.h>
+
 #define EX_ENTRY_SIZE		8
 #define EX_ORIG_OFFSET		0
 #define EX_NEW_OFFSET		4
@@ -30,6 +32,14 @@
 #define ALT_ORIG_LEN_OFFSET	10
 #define ALT_NEW_LEN_OFFSET	11
 
+#define ADR_SOURCE	255
+
+struct switch_table_info {
+	u64 switch_table_label;
+	u64 nb_entries;
+	u64 offset_unsigned;
+} __attribute__((__packed__));
+
 static inline bool arch_should_ignore_feature(unsigned short feature)
 {
 	return false;
tools/objtool/arch/arm64/include/insn_decode.h +1 -2
index a01d76306749..65b6efecd68f 100644
@@ -18,6 +18,7 @@
 #define _ARM_INSN_DECODE_H
 
 #include "../../../arch.h"
+#include "arch_special.h"
 
 #define INSN_RESERVED	0b0000
 #define INSN_UNALLOC_1	0b0001
@@ -58,8 +59,6 @@
 #define COMPOSED_INSN_REGS_NUM	2
 #define INSN_COMPOSED	1
 
-#define ADR_SOURCE	-1
-
 typedef int (*arm_decode_class)(u32 instr, unsigned char *type,
 				unsigned long *immediate, struct stack_op *op);
 
tools/objtool/check.c +3 -3
index 18f7fb47392a..2c4ea51041e1 100644
@@ -35,8 +35,8 @@ struct instruction *find_insn(struct objtool_file *file,
 	return NULL;
 }
 
-static struct instruction *next_insn_same_sec(struct objtool_file *file,
-					      struct instruction *insn)
+struct instruction *next_insn_same_sec(struct objtool_file *file,
+				       struct instruction *insn)
 {
 	struct instruction *next = list_next_entry(insn, list);
 
@@ -1856,7 +1856,7 @@ static int validate_sibling_call(struct instruction *insn, struct insn_state *st
 {
 	if (arch_is_insn_sibling_call(insn) && has_modified_stack_frame(state)) {
 		WARN_FUNC("sibling call from callable instruction with modified stack frame",
-				insn->sec, insn->offset);
+			  insn->sec, insn->offset);
 		return 1;
 	}
 
tools/objtool/check.h +2 -0
index 267759760a3d..5833f85f71c3 100644
@@ -66,6 +66,8 @@ int check(const char *objname, bool orc);
 
 struct instruction *find_insn(struct objtool_file *file,
 			      struct section *sec, unsigned long offset);
+struct instruction *next_insn_same_sec(struct objtool_file *file,
+				       struct instruction *insn);
 
 #define for_each_insn(file, insn)					\
 	list_for_each_entry(insn, &file->insn_list, list)
Signature · 1 line
2.17.1
#24 lore raw

Re: [RFC v4 10/18] objtool: arm64: Implement functions to add switch tables alternatives

Hi Raphaël,

The title should probably go straight to the point, this provides support for arm64 switch tables. The use of alternatives is just an implementation detail (it is good to have it explained in the commit message, but in the title it would be more helpful to see that this actually adds support for something that was not working).

Quoted 5 lines · Raphael Gault

This patch implements the functions required to identify and add as alternatives all the possible destinations of the switch table. This implementation relies on the new plugin introduced previously which records information about the switch-table in a .objtool_data section.

I think it would be worth giving a bit more context. Why are you adding all possible destinations of a switch table as alternatives? What does this solve?

Quoted 36 lines
---
  tools/objtool/arch/arm64/arch_special.c       | 132 +++++++++++++++++-
  .../objtool/arch/arm64/include/arch_special.h |  10 ++
  .../objtool/arch/arm64/include/insn_decode.h  |   3 +-
  tools/objtool/check.c                         |   6 +-
  tools/objtool/check.h                         |   2 +
  5 files changed, 146 insertions(+), 7 deletions(-)
tools/objtool/arch/arm64/arch_special.c +10 -0
index 17a8a06aac2a..11284066157c 100644
@@ -12,8 +12,13 @@
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, see <http://www.gnu.org/licenses/>.
   */
+
+#include <stdlib.h>
+#include <string.h>
+
  #include "../../special.h"
  #include "arch_special.h"
+#include "bit_operations.h"
 
  void arch_force_alt_path(unsigned short feature,
  			 bool uaccess,
@@ -21,9 +26,133 @@ void arch_force_alt_path(unsigned short feature,
  {
  }
 
+static u32 next_offset(u8 *table, u8 entry_size)
+{
+	switch (entry_size) {
+	case 1:
+		return table[0];

tools/objtool/arch/arm64/arch_special.c void arch_force_alt_path(unsigned short feature, · line 43

Quoted 10 lines
tools/objtool/arch/arm64/arch_special.c +10 -0
+	case 2:
+		return *(u16 *)(table);
+	default:
+		return *(u32 *)(table);
+	}
+}
+
+static u32 get_table_entry_size(u32 insn)
+{
+	unsigned char size = (insn >> 30) & ONES(2);

I guess you're assuming insn is a ld* instruction. It would be good to clarify what this mask is doing and on which type of instructions that can work.

Maybe you could re-use the arm64 decoder to retrieve the size. You could directly call arm_decode_ld_st() if you know this is a load instruction if you are very confident about it or call the whole decoding process and check that you actually have the type of instruction you were expecting.

Quoted 17 lines
tools/objtool/arch/arm64/arch_special.c +17 -0
+	switch (size) {
+	case 0:
+		return 1;
+	case 1:
+		return 2;
+	default:
+		return 4;
+	}
+}
+
+static int add_possible_branch(struct objtool_file *file,
+			       struct instruction *insn,
+			       u32 base, u32 offset)
+{
+	struct instruction *dest_insn;
+	struct alternative *alt;
+	offset = base + 4 * offset;

tools/objtool/arch/arm64/arch_special.c void arch_force_alt_path(unsigned short feature, · line 70

Quoted 10 lines
tools/objtool/arch/arm64/arch_special.c +10 -0
+
+	alt = calloc(1, sizeof(*alt));
+	if (alt == NULL) {
+		WARN("allocation failure, can't add jump alternative");
+		return -1;
+	}
+
+	dest_insn = find_insn(file, insn->sec, offset);
+	if (dest_insn == NULL) {
+		free(alt);

You probably should allocate the alternative only once you've found the target instruction.

tools/objtool/arch/arm64/arch_special.c void arch_force_alt_path(unsigned short feature, · line 71

Quoted 1 line
tools/objtool/arch/arm64/arch_special.c +1 -0
+		return 0;

Is is really fine not to find an instruction when you have an entry in your obtool_data table?

tools/objtool/arch/arm64/arch_special.c void arch_force_alt_path(unsigned short feature, · line 74

Quoted 3 lines
tools/objtool/arch/arm64/arch_special.c +3 -0
+	}
+	alt->insn = dest_insn;
+	alt->skip_orig = true;

Is skipping the original instruction needed? It still gets executed before we jump to the target instruction.

Quoted 46 lines
tools/objtool/arch/arm64/arch_special.c +43 -0
+	list_add_tail(&alt->list, &insn->alts);
+	return 0;
+}
+
  int arch_add_jump_table(struct objtool_file *file, struct instruction *insn,
  			struct rela *table, struct rela *next_table)
  {
+	struct rela *objtool_data_rela = NULL;
+	struct switch_table_info *swt_info = NULL;
+	struct section *objtool_data = find_section_by_name(file->elf, ".objtool_data");
+	struct section *rodata_sec = find_section_by_name(file->elf, ".rodata");
+	struct section *branch_sec = NULL;
+	u8 *switch_table = NULL;
+	u64 base_offset = 0;
+	struct instruction *pre_jump_insn;
+	u32 sec_size = 0;
+	u32 entry_size = 0;
+	u32 offset = 0;
+	u32 i, j;
+
+	if (objtool_data == NULL)
+		return 0;
+
+	/*
+	 * 1. Using rela, Identify entry for the switch table
+	 * 2. Retrieve base offset
+	 * 3. Retrieve branch instruction
+	 * 3. For all entries in switch table:
+	 * 	3.1. Compute new offset
+	 * 	3.2. Create alternative instruction
+	 * 	3.3. Add alt_instr to insn->alts list
+	 */
+	sec_size = objtool_data->sh.sh_size;
+	for (i = 0, swt_info = (void *)objtool_data->data->d_buf;
+	     i < sec_size / sizeof(struct switch_table_info);
+	     i++, swt_info++) {
+		offset = i * sizeof(struct switch_table_info);
+		objtool_data_rela = find_rela_by_dest_range(objtool_data, offset,
+							    sizeof(u64));
+		/* retrieving the objtool data of the switch table we need */
+		if (objtool_data_rela == NULL ||
+		    table->sym->sec != objtool_data_rela->sym->sec ||
+		    table->addend != objtool_data_rela->addend)
+			continue;
+
+		/* retrieving switch table content */

tools/objtool/arch/arm64/arch_special.c void arch_force_alt_path(unsigned short feature, · line 130

Quoted 10 lines
tools/objtool/arch/arm64/arch_special.c +10 -0
+		switch_table = (u8 *)(rodata_sec->data->d_buf + table->addend);
+
+		/* retrieving pre jump instruction (ldr) */
+		branch_sec = insn->sec;
+		pre_jump_insn = find_insn(file, branch_sec,
+					  insn->offset - 3 * sizeof(u32));
+		entry_size = get_table_entry_size(*(u32 *)(branch_sec->data->d_buf + pre_jump_insn->offset));
+
+		/*
+		 * iterating over the pre-jumps instruction in order to

What's a pre-jump instruction? Can you include as comment an example of what the arm64 jump table looks like? That would help understand what is actually in the objtool_data section and how you make sense of it.

tools/objtool/arch/arm64/arch_special.c void arch_force_alt_path(unsigned short feature, · line 135

Quoted 5 lines
tools/objtool/arch/arm64/arch_special.c +5 -0
+		 * retrieve switch base offset.
+		 */
+		while (pre_jump_insn != NULL &&
+		       pre_jump_insn->offset <= insn->offset) {
+			if (pre_jump_insn->stack_op.src.reg == ADR_SOURCE) {

Maybe you can have a separate function to do the processing of a single switch table/switch_table_info.

Quoted 110 lines
tools/objtool/arch/arm64/arch_special.c +21 -2
+				base_offset = pre_jump_insn->offset +
+					      pre_jump_insn->immediate;
+				/*
+				 * Once we have the switch table entry size
+				 * we add every possible destination using
+				 * alternatives of the original branch
+				 * instruction
+				 */
+				for (j = 0; j < swt_info->nb_entries; j++) {
+					if (add_possible_branch(file, insn,
+								base_offset,
+								next_offset(switch_table, entry_size))) {
+						return -1;
+					}
+					switch_table += entry_size;
+				}
+			}
+			pre_jump_insn = next_insn_same_sec(file, pre_jump_insn);
+		}
+	}
  	return 0;
  }
 
@@ -32,6 +161,5 @@ struct rela *arch_find_switch_table(struct objtool_file *file,
  				  struct section *rodata_sec,
  				  unsigned long table_offset)
  {
-	file->ignore_unreachables = true;
-	return NULL;
+	return text_rela;
  }
tools/objtool/arch/arm64/include/arch_special.h +10 -0
index 185103be8a51..cba432fed80f 100644
@@ -15,6 +15,8 @@
  #ifndef _ARM64_ARCH_SPECIAL_H
  #define _ARM64_ARCH_SPECIAL_H
 
+#include <linux/types.h>
+
  #define EX_ENTRY_SIZE		8
  #define EX_ORIG_OFFSET		0
  #define EX_NEW_OFFSET		4
@@ -30,6 +32,14 @@
  #define ALT_ORIG_LEN_OFFSET	10
  #define ALT_NEW_LEN_OFFSET	11
 
+#define ADR_SOURCE	255
+
+struct switch_table_info {
+	u64 switch_table_label;
+	u64 nb_entries;
+	u64 offset_unsigned;
+} __attribute__((__packed__));
+
  static inline bool arch_should_ignore_feature(unsigned short feature)
  {
  	return false;
tools/objtool/arch/arm64/include/insn_decode.h +1 -2
index a01d76306749..65b6efecd68f 100644
@@ -18,6 +18,7 @@
  #define _ARM_INSN_DECODE_H
 
  #include "../../../arch.h"
+#include "arch_special.h"
 
  #define INSN_RESERVED	0b0000
  #define INSN_UNALLOC_1	0b0001
@@ -58,8 +59,6 @@
  #define COMPOSED_INSN_REGS_NUM	2
  #define INSN_COMPOSED	1
 
-#define ADR_SOURCE	-1
-
  typedef int (*arm_decode_class)(u32 instr, unsigned char *type,
  				unsigned long *immediate, struct stack_op *op);
 
tools/objtool/check.c +3 -3
index 18f7fb47392a..2c4ea51041e1 100644
@@ -35,8 +35,8 @@ struct instruction *find_insn(struct objtool_file *file,
  	return NULL;
  }
 
-static struct instruction *next_insn_same_sec(struct objtool_file *file,
-					      struct instruction *insn)
+struct instruction *next_insn_same_sec(struct objtool_file *file,
+				       struct instruction *insn)
  {
  	struct instruction *next = list_next_entry(insn, list);
 
@@ -1856,7 +1856,7 @@ static int validate_sibling_call(struct instruction *insn, struct insn_state *st
  {
  	if (arch_is_insn_sibling_call(insn) && has_modified_stack_frame(state)) {
  		WARN_FUNC("sibling call from callable instruction with modified stack frame",
-				insn->sec, insn->offset);
+			  insn->sec, insn->offset);
  		return 1;
  	}
 
tools/objtool/check.h +0 -0
index 267759760a3d..5833f85f71c3 100644

tools/objtool/check.h int check(const char *objname, bool orc); · line 73

Quoted 8 lines
tools/objtool/check.h +2 -0
@@ -66,6 +66,8 @@ int check(const char *objname, bool orc);
 
  struct instruction *find_insn(struct objtool_file *file,
  			      struct section *sec, unsigned long offset);
+struct instruction *next_insn_same_sec(struct objtool_file *file,
+				       struct instruction *insn);
 
  #define for_each_insn(file, insn)					\
  	list_for_each_entry(insn, &file->insn_list, list)

Thanks,

Signature · 1 line
Julien Thierry
#25 lore raw

RFC v4 11/18 arm64: alternative: Mark .altinstr_replacement as containing executable instructions

In reply to Raphael Gault

Until now, the section .altinstr_replacement wasn't marked as containing executable instructions on arm64. This patch changes that so that it is coherent with what is done on x86.

---
 arch/arm64/include/asm/alternative.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/alternative.h +1 -1
index b9f8d787eea9..e9e6b81e3eb4 100644
@@ -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"							\
Signature · 1 line
2.17.1
#26 lore raw

RFC v4 12/18 arm64: assembler: Add macro to annotate asm function having non standard stack-frame.

In reply to Raphael Gault

Some functions don't have standard stack-frames but are intended this way. In order for objtool to ignore those particular cases we add a macro that enables us to annotate the cases we chose to mark as particular.

---
 include/linux/frame.h | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
include/linux/frame.h +18 -1
index 02d3ca2d9598..1e35e58ab259 100644
@@ -11,14 +11,31 @@
  *
  * For more information, see tools/objtool/Documentation/stack-validation.txt.
  */
+#ifndef __ASSEMBLY__
 #define STACK_FRAME_NON_STANDARD(func) \
 	static void __used __section(.discard.func_stack_frame_non_standard) \
 		*__func_stack_frame_non_standard_##func = func
+#else
+	/*
+	 * 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"
+	.quad	\func
+	.popsection
+	.endm
 
+#endif /* __ASSEMBLY__ */
 #else /* !CONFIG_STACK_VALIDATION */
 
+#ifndef __ASSEMBLY__
 #define STACK_FRAME_NON_STANDARD(func)
-
+#else
+	.macro	asm_stack_frame_non_standard	func
+	.endm
+#endif /* __ASSEMBLY__ */
 #endif /* CONFIG_STACK_VALIDATION */
 
 #endif /* _LINUX_FRAME_H */
Signature · 1 line
2.17.1
#27 lore raw

Re: [RFC v4 12/18] arm64: assembler: Add macro to annotate asm function having non standard stack-frame.

Quoted 18 lines · Raphael Gault

Some functions don't have standard stack-frames but are intended this way. In order for objtool to ignore those particular cases we add a macro that enables us to annotate the cases we chose to mark as particular.

---
 include/linux/frame.h | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
include/linux/frame.h +0 -0
index 02d3ca2d9598..1e35e58ab259 100644
@@ -11,14 +11,31 @@
  *
  * For more information, see tools/objtool/Documentation/stack-validation.txt.
  */

include/linux/frame.h line 23

Quoted 10 lines
include/linux/frame.h +7 -0
+#ifndef __ASSEMBLY__
 #define STACK_FRAME_NON_STANDARD(func) \
 	static void __used __section(.discard.func_stack_frame_non_standard) \
 		*__func_stack_frame_non_standard_##func = func
+#else
+	/*
+	 * This macro is the arm64 assembler equivalent of the
+	 * macro STACK_FRAME_NON_STANDARD define at
+	 * ~/include/linux/frame.h
+	 */

This comment is a bit confusing as it's referring to its own header file. And it's not arm64-specific. I don't think we really need a comment here anyway.

include/linux/frame.h line 28

Quoted 5 lines
include/linux/frame.h +5 -0
+	.macro	asm_stack_frame_non_standard	func
+	.pushsection ".discard.func_stack_frame_non_standard"
+	.quad	\func
+	.popsection
+	.endm

Can you call it STACK_FRAME_NON_STANDARD for consistency with the non-asm version?

Signature · 1 line
Josh
#28 lore raw

RFC v4 13/18 arm64: sleep: Prevent stack frame warnings from objtool

In reply to Raphael Gault

This code doesn't respect the Arm PCS but it is intended this way. Adapting it to respect the PCS would result in altering the behaviour.

In order to suppress objtool's warnings, we setup a stack frame for __cpu_suspend_enter and annotate cpu_resume and _cpu_resume as having non-standard stack frames.

---
 arch/arm64/kernel/sleep.S | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/kernel/sleep.S +5 -0
index f5b04dd8a710..55c7c099d32c 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #include <linux/errno.h>
+#include <linux/frame.h>
 #include <linux/linkage.h>
 #include <asm/asm-offsets.h>
 #include <asm/assembler.h>
@@ -90,6 +91,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 +148,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
#29 lore raw

Re: [RFC v4 13/18] arm64: sleep: Prevent stack frame warnings from objtool

Quoted 33 lines · Raphael Gault

This code doesn't respect the Arm PCS but it is intended this way. Adapting it to respect the PCS would result in altering the behaviour.

In order to suppress objtool's warnings, we setup a stack frame for __cpu_suspend_enter and annotate cpu_resume and _cpu_resume as having non-standard stack frames.

---
 arch/arm64/kernel/sleep.S | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/kernel/sleep.S +2 -0
index f5b04dd8a710..55c7c099d32c 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #include <linux/errno.h>
+#include <linux/frame.h>
 #include <linux/linkage.h>
 #include <asm/asm-offsets.h>
 #include <asm/assembler.h>
@@ -90,6 +91,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

arch/arm64/kernel/sleep.S ENTRY(_cpu_resume) · line 153

Quoted 6 lines
arch/arm64/kernel/sleep.S +3 -0
@@ -146,3 +148,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

We usually put these annotations immediately after the functions they're annotating. And they should resemble the C macros like

STACK_FRAME_NON_STANDARD(cpu_resume)

Signature · 1 line
Josh
#30 lore raw

RFC v4 14/18 arm64: kvm: Annotate non-standard stack frame functions

In reply to Raphael Gault

Both __guest_entry and __guest_exit functions do not setup a correct stack frame. Because they can be considered as callable functions, even if they are particular cases, we chose to silence the warnings given by objtool by annotating them as non-standard.

---
 arch/arm64/kvm/hyp/entry.S | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/entry.S +3 -0
index e5cc8d66bf53..c3443bfd0944 100644
@@ -15,6 +15,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/kvm_mmu.h>
 #include <asm/kvm_ptrauth.h>
+#include <linux/frame.h>
 
 #define CPU_GP_REG_OFFSET(x)	(CPU_GP_REGS + x)
 #define CPU_XREG_OFFSET(x)	CPU_GP_REG_OFFSET(CPU_USER_PT_REGS + 8*x)
@@ -97,6 +98,7 @@ alternative_else_nop_endif
 	eret
 	sb
 ENDPROC(__guest_enter)
+asm_stack_frame_non_standard __guest_enter
 
 ENTRY(__guest_exit)
 	// x0: return code
@@ -193,3 +195,4 @@ abort_guest_exit_end:
 	orr	x0, x0, x5
 1:	ret
 ENDPROC(__guest_exit)
+asm_stack_frame_non_standard __guest_exit
Signature · 1 line
2.17.1
#31 lore raw

RFC v4 15/18 arm64: kernel: Add exception on kuser32 to prevent stack analysis

In reply to Raphael Gault

kuser32 being used for compatibility, it contains a32 instructions which are not recognised by objtool when trying to analyse arm64 object files. Thus, we add an exception to skip validation on this particular file.

---
 arch/arm64/kernel/Makefile | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kernel/Makefile +3 -0
index 478491f07b4f..1239c7da4c02 100644
@@ -33,6 +33,9 @@ ifneq ($(CONFIG_COMPAT_VDSO), y)
 obj-$(CONFIG_COMPAT)			+= sigreturn32.o
 endif
 obj-$(CONFIG_KUSER_HELPERS)		+= kuser32.o
+
+OBJECT_FILES_NON_STANDARD_kuser32.o := y
+
 obj-$(CONFIG_FUNCTION_TRACER)		+= ftrace.o entry-ftrace.o
 obj-$(CONFIG_MODULES)			+= module.o
 obj-$(CONFIG_ARM64_MODULE_PLTS)		+= module-plts.o
Signature · 1 line
2.17.1
#32 lore raw

RFC v4 16/18 arm64: crypto: Add exceptions for crypto object to prevent stack analysis

In reply to Raphael Gault

Some crypto modules contain .word of data in the .text section. Since objtool can't make the distinction between data and incorrect instruction, it gives a warning about the instruction beeing unknown and stops the analysis of the object file.

The exception can be removed if the data are moved to another section or if objtool is tweaked to handle this particular case.

---
 arch/arm64/crypto/Makefile | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/crypto/Makefile +3 -0
index 0435f2a0610e..e2a25919ebaa 100644
@@ -43,9 +43,11 @@ aes-neon-blk-y := aes-glue-neon.o aes-neon.o
 
 obj-$(CONFIG_CRYPTO_SHA256_ARM64) += sha256-arm64.o
 sha256-arm64-y := sha256-glue.o sha256-core.o
+OBJECT_FILES_NON_STANDARD_sha256-core.o := y
 
 obj-$(CONFIG_CRYPTO_SHA512_ARM64) += sha512-arm64.o
 sha512-arm64-y := sha512-glue.o sha512-core.o
+OBJECT_FILES_NON_STANDARD_sha512-core.o := y
 
 obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha-neon.o
 chacha-neon-y := chacha-neon-core.o chacha-neon-glue.o
@@ -58,6 +60,7 @@ aes-arm64-y := aes-cipher-core.o aes-cipher-glue.o
 
 obj-$(CONFIG_CRYPTO_AES_ARM64_BS) += aes-neon-bs.o
 aes-neon-bs-y := aes-neonbs-core.o aes-neonbs-glue.o
+OBJECT_FILES_NON_STANDARD_aes-neonbs-core.o := y
 
 CFLAGS_aes-glue-ce.o	:= -DUSE_V8_CRYPTO_EXTENSIONS
 
Signature · 1 line
2.17.1
#33 lore raw

Re: [RFC v4 16/18] arm64: crypto: Add exceptions for crypto object to prevent stack analysis

Quoted 9 lines · Raphael Gault

Some crypto modules contain .word of data in the .text section. Since objtool can't make the distinction between data and incorrect instruction, it gives a warning about the instruction beeing unknown and stops the analysis of the object file.

The exception can be removed if the data are moved to another section or if objtool is tweaked to handle this particular case.

If the data can be moved to .rodata then I think that would be a much better solution.

Quoted 31 lines
---
 arch/arm64/crypto/Makefile | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/crypto/Makefile +3 -0
index 0435f2a0610e..e2a25919ebaa 100644
@@ -43,9 +43,11 @@ aes-neon-blk-y := aes-glue-neon.o aes-neon.o
 
 obj-$(CONFIG_CRYPTO_SHA256_ARM64) += sha256-arm64.o
 sha256-arm64-y := sha256-glue.o sha256-core.o
+OBJECT_FILES_NON_STANDARD_sha256-core.o := y
 
 obj-$(CONFIG_CRYPTO_SHA512_ARM64) += sha512-arm64.o
 sha512-arm64-y := sha512-glue.o sha512-core.o
+OBJECT_FILES_NON_STANDARD_sha512-core.o := y
 
 obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha-neon.o
 chacha-neon-y := chacha-neon-core.o chacha-neon-glue.o
@@ -58,6 +60,7 @@ aes-arm64-y := aes-cipher-core.o aes-cipher-glue.o
 
 obj-$(CONFIG_CRYPTO_AES_ARM64_BS) += aes-neon-bs.o
 aes-neon-bs-y := aes-neonbs-core.o aes-neonbs-glue.o
+OBJECT_FILES_NON_STANDARD_aes-neonbs-core.o := y
 
 CFLAGS_aes-glue-ce.o	:= -DUSE_V8_CRYPTO_EXTENSIONS
 
Signature · 1 line
2.17.1
Signature · 6 lines
Josh

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#34 lore raw

RFC v4 17/18 arm64: kernel: Annotate non-standard stack frame functions

In reply to Raphael Gault

Annotate assembler functions which are callable but do not setup a correct stack frame.

---
 arch/arm64/kernel/hyp-stub.S | 3 +++
 arch/arm64/kvm/hyp-init.S    | 3 +++
 2 files changed, 6 insertions(+)
arch/arm64/kernel/hyp-stub.S +3 -0
index 73d46070b315..8917d42f38c7 100644
@@ -6,6 +6,7 @@
  * Author:	Marc Zyngier <[email protected]>
  */
 
+#include <linux/frame.h>
 #include <linux/init.h>
 #include <linux/linkage.h>
 #include <linux/irqchip/arm-gic-v3.h>
@@ -42,6 +43,7 @@ ENTRY(__hyp_stub_vectors)
 	ventry	el1_fiq_invalid			// FIQ 32-bit EL1
 	ventry	el1_error_invalid		// Error 32-bit EL1
 ENDPROC(__hyp_stub_vectors)
+asm_stack_frame_non_standard __hyp_stub_vectors
 
 	.align 11
 
@@ -69,6 +71,7 @@ el1_sync:
 9:	mov	x0, xzr
 	eret
 ENDPROC(el1_sync)
+asm_stack_frame_non_standard el1_sync
 
 .macro invalid_vector	label
 \label:
arch/arm64/kvm/hyp-init.S +3 -0
index 160be2b4696d..63deea39313d 100644
@@ -12,6 +12,7 @@
 #include <asm/pgtable-hwdef.h>
 #include <asm/sysreg.h>
 #include <asm/virt.h>
+#include <linux/frame.h>
 
 	.text
 	.pushsection	.hyp.idmap.text, "ax"
@@ -118,6 +119,7 @@ CPU_BE(	orr	x4, x4, #SCTLR_ELx_EE)
 	/* Hello, World! */
 	eret
 ENDPROC(__kvm_hyp_init)
+asm_stack_frame_non_standard __kvm_hyp_init
 
 ENTRY(__kvm_handle_stub_hvc)
 	cmp	x0, #HVC_SOFT_RESTART
@@ -159,6 +161,7 @@ reset:
 	eret
 
 ENDPROC(__kvm_handle_stub_hvc)
+asm_stack_frame_non_standard __kvm_handle_stub_hvc
 
 	.ltorg
 
Signature · 1 line
2.17.1
#35 lore raw

RFC v4 18/18 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
index 3adcec05b1f6..dc3de85b2502 100644
@@ -163,6 +163,7 @@ config ARM64
 	select HAVE_RCU_TABLE_FREE
 	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
#36 lore raw
In reply to Raphael Gault
Quoted 11 lines · Raphael Gault

Hi,

Changes since RFC V3:
* Rebased on tip/master: Switch/jump table had been refactored
* Take Catalin Marinas comments into account regarding the asm macro for marking exceptions.

As of now, objtool only supports the x86_64 architecture but the groundwork has already been done in order to add support for other architectures without too much effort.

Quoted 8 lines

This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.

Objtool will be a valuable tool to progress and provide more guarentees on live patching which is a work in progress for arm64.

Once we have the base of objtool working the next steps will be to port Peter Z's uaccess validation for arm64.

Hi Raphael,

Sorry about the long delay. I have some comments coming shortly.

One general comment: I noticed that several of the (mostly minor) suggested changes I made for v1 haven't been fixed.

I'll try to suggest them again here for v4, so you don't need to go back and find them. But in the future please try to incorporate all the comments from previous patch sets before posting new versions. I'm sure it wasn't intentional, as you did acknowledge and agree to most of the changes. But it does waste people's time and goodwill if you neglect to incorporate their suggestions. Thanks.

Signature · 1 line
Josh
#37 lore raw

Hi Josh,

Quoted 28 lines · Josh Poimboeuf
Quoted 11 lines · Raphael Gault

Hi,

Changes since RFC V3:
* Rebased on tip/master: Switch/jump table had been refactored
* Take Catalin Marinas comments into account regarding the asm macro for marking exceptions.

As of now, objtool only supports the x86_64 architecture but the groundwork has already been done in order to add support for other architectures without too much effort.

Quoted 8 lines

This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.

Objtool will be a valuable tool to progress and provide more guarentees on live patching which is a work in progress for arm64.

Once we have the base of objtool working the next steps will be to port Peter Z's uaccess validation for arm64.

Hi Raphael,

Sorry about the long delay. I have some comments coming shortly.

One general comment: I noticed that several of the (mostly minor) suggested changes I made for v1 haven't been fixed.

Quoted 7 lines

I'll try to suggest them again here for v4, so you don't need to go back and find them. But in the future please try to incorporate all the comments from previous patch sets before posting new versions. I'm sure it wasn't intentional, as you did acknowledge and agree to most of the changes. But it does waste people's time and goodwill if you neglect to incorporate their suggestions. Thanks.

Indeed, sorry about that.

Thanks for you comments, I will do my best to address them shortly. However, I won't have access to my professional emails for a little while and probably won't be able to work on this before at least a week. I'll try to have a new soon though and use my personal email.

Thanks,

Signature · 1 line
Raphael Gault
#38 lore raw
In reply to Raphael Gault

Hi,

Quoted 2 lines · Raphael Gault

Hi,

[...]

Quoted 7 lines

As of now, objtool only supports the x86_64 architecture but the groundwork has already been done in order to add support for other architectures without too much effort.

This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.

I was wondering about the current status of these patches. Is anyone actively working on this?

If not, I can pick that up and try to make this go forward.

Cheers,

Signature · 1 line
Julien Thierry
#39 lore raw

Hi Julien,

Quoted 10 lines · Julien Thierry
Quoted 2 lines · Raphael Gault

Hi,

[...]

Quoted 4 lines

As of now, objtool only supports the x86_64 architecture but the groundwork has already been done in order to add support for other architectures without too much effort.

Quoted 9 lines
Quoted 3 lines

This series of patches adds support for the arm64 architecture based on the Armv8.5 Architecture Reference Manual.

I was wondering about the current status of these patches. Is anyone actively working on this?

If not, I can pick that up and try to make this go forward.

I am not working on these patches at the moment and I don't know when I can get back at it so as far as I am concerned you can pick it up but maybe wait a bit to see if someone else has an interest in this.

Thanks,
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.