[PATCH v3 1/6] Fixup for "objtool/powerpc: Add --mcount specific implementation"

From: Christophe Leroy
Date: Thu Sep 01 2022 - 07:49:48 EST


Make arch_decode_instruction() more future proof and less error prone:
- Use local vars for type and imm so that GCC will detect when we
forget to update them
- Handle imm outside the branch type check
- Adapt len for prefixed instructions

Handle the four branch types b, bl, ba, bla

Part of it should probably go into "objtool/powerpc: Enable objtool
to be built on ppc" for instance the setup of the len. Maybe all the
skeleton with the local vars, the switch with its default, etc ...
Then following patches only have to add stuff inside the switch.

Signed-off-by: Christophe Leroy <christophe.leroy@xxxxxxxxxx>
---
tools/objtool/arch/powerpc/decode.c | 39 +++++++++++++++++++++--------
1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/tools/objtool/arch/powerpc/decode.c b/tools/objtool/arch/powerpc/decode.c
index b71c265ed503..f9932351908c 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -50,31 +50,48 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
{
u32 insn;
unsigned int opcode;
+ unsigned long imm;
+ enum insn_type typ;

- *immediate = 0;
insn = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
- *len = 4;
- *type = INSN_OTHER;

opcode = insn >> 26;

switch (opcode) {
- case 18: /* bl */
- if ((insn & 3) == 1) {
- *type = INSN_CALL;
- *immediate = insn & 0x3fffffc;
- if (*immediate & 0x2000000)
- *immediate -= 0x4000000;
- }
+ case 18: /* bl/b/bla/ba */
+ if (insn & 1)
+ typ = INSN_CALL;
+ else
+ typ = INSN_JUMP_UNCONDITIONAL;
+
+ imm = insn & 0x3fffffc;
+ if (imm & 0x2000000)
+ imm -= 0x4000000;
+ imm |= insn & 2; /* AA flag */
+ break;
+ default:
+ typ = INSN_OTHER;
+ imm = 0;
break;
}

+ if (opcode == 1)
+ *len = 8;
+ else
+ *len = 4;
+
+ *type = typ;
+ *immediate = imm;
+
return 0;
}

unsigned long arch_jump_destination(struct instruction *insn)
{
- return insn->offset + insn->immediate;
+ if (insn->immediate & 2)
+ return insn->immediate & ~2;
+
+ return insn->offset + insn->immediate;
}

void arch_initial_func_cfi_state(struct cfi_init_state *state)
--
2.37.1