[PATCH v3 10/15] objtool: Add support for relative switch tables

From: Christophe Leroy
Date: Fri Jun 23 2023 - 13:07:11 EST


On powerpc, switch tables are relative, than means the address of the
table is added to the value of the entry in order to get the pointed
address: (r10 is the table address, r4 the index in the table)

lis r10,0 <== Load r10 with upper part of .rodata address
R_PPC_ADDR16_HA .rodata
addi r10,r10,0 <== Add lower part of .rodata address
R_PPC_ADDR16_LO .rodata
lwzx r8,r10,r4 <== Read table entry at r10 + r4 into r8
add r10,r8,r10 <== Add table address to read value
mtctr r10 <== Save calculated address in CTR
bctr <== Branch to address in CTR

RELOCATION RECORDS FOR [.rodata]:
OFFSET TYPE VALUE
00000000 R_PPC_REL32 .text+0x0000054c
00000004 R_PPC_REL32 .text+0x000003d0
...

But for c_jump_tables it is not the case, they contain the
pointed address directly:

lis r28,0 <== Load r28 with upper .rodata..c_jump_table
R_PPC_ADDR16_HA .rodata..c_jump_table
addi r28,r28,0 <== Add lower part of .rodata..c_jump_table
R_PPC_ADDR16_LO .rodata..c_jump_table
lwzx r10,r28,r10 <== Read table entry at r10 + r28 into r10
mtctr r10 <== Save read value in CTR
bctr <== Branch to address in CTR

RELOCATION RECORDS FOR [.rodata..c_jump_table]:
OFFSET TYPE VALUE
00000000 R_PPC_ADDR32 .text+0x00000dc8
00000004 R_PPC_ADDR32 .text+0x00000dc8
...

Add support to objtool for relative tables, based on the relocation
type which is R_PPC_REL32 for switch tables and R_PPC_ADDR32 for
C jump tables. Do the comparison using R_ABS32 and R_ABS64 which are
architecture agnostic.

And use correct size for 'long' instead of hard coding a size of '8'.

Signed-off-by: Christophe Leroy <christophe.leroy@xxxxxxxxxx>
---
tools/objtool/check.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index ae4a1608d97e..05d789e6d3b6 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1988,7 +1988,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
struct symbol *pfunc = insn_func(insn)->pfunc;
struct reloc *table = insn_jump_table(insn);
struct instruction *dest_insn;
- unsigned int prev_offset = 0;
+ unsigned int offset, prev_offset = 0;
struct reloc *reloc = table;
struct alternative *alt;

@@ -2003,7 +2003,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
break;

/* Make sure the table entries are consecutive: */
- if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
+ if (prev_offset && reloc_offset(reloc) != prev_offset + elf_addr_size(file->elf))
break;

/* Detect function pointers from contiguous objects: */
@@ -2011,7 +2011,12 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
reloc_addend(reloc) == pfunc->offset)
break;

- dest_insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
+ if (reloc_type(reloc) == R_ABS32 || reloc_type(reloc) == R_ABS64)
+ offset = reloc_addend(reloc);
+ else
+ offset = reloc_addend(reloc) + reloc_offset(table) - reloc_offset(reloc);
+
+ dest_insn = find_insn(file, reloc->sym->sec, offset);
if (!dest_insn)
break;

--
2.40.1