Skip to content

Commit 20a5546

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
objtool: Change arch_decode_instruction() signature
In preparation to changing struct instruction around a bit, avoid passing it's members by pointer and instead pass the whole thing. A cleanup in it's own right too. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Tested-by: Nathan Chancellor <[email protected]> # build only Tested-by: Thomas Weißschuh <[email protected]> # compile and run Link: https://lore.kernel.org/r/[email protected]
1 parent 585a78c commit 20a5546

File tree

4 files changed

+64
-71
lines changed

4 files changed

+64
-71
lines changed

tools/objtool/arch/powerpc/decode.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,36 @@ const char *arch_ret_insn(int len)
4141

4242
int arch_decode_instruction(struct objtool_file *file, const struct section *sec,
4343
unsigned long offset, unsigned int maxlen,
44-
unsigned int *len, enum insn_type *type,
45-
unsigned long *immediate,
46-
struct list_head *ops_list)
44+
struct instruction *insn)
4745
{
4846
unsigned int opcode;
4947
enum insn_type typ;
5048
unsigned long imm;
51-
u32 insn;
49+
u32 ins;
5250

53-
insn = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
54-
opcode = insn >> 26;
51+
ins = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
52+
opcode = ins >> 26;
5553
typ = INSN_OTHER;
5654
imm = 0;
5755

5856
switch (opcode) {
5957
case 18: /* b[l][a] */
60-
if ((insn & 3) == 1) /* bl */
58+
if ((ins & 3) == 1) /* bl */
6159
typ = INSN_CALL;
6260

63-
imm = insn & 0x3fffffc;
61+
imm = ins & 0x3fffffc;
6462
if (imm & 0x2000000)
6563
imm -= 0x4000000;
6664
break;
6765
}
6866

6967
if (opcode == 1)
70-
*len = 8;
68+
insn->len = 8;
7169
else
72-
*len = 4;
70+
insn->len = 4;
7371

74-
*type = typ;
75-
*immediate = imm;
72+
insn->type = typ;
73+
insn->immediate = imm;
7674

7775
return 0;
7876
}

tools/objtool/arch/x86/decode.c

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,11 @@ static bool has_notrack_prefix(struct insn *insn)
146146

147147
int arch_decode_instruction(struct objtool_file *file, const struct section *sec,
148148
unsigned long offset, unsigned int maxlen,
149-
unsigned int *len, enum insn_type *type,
150-
unsigned long *immediate,
151-
struct list_head *ops_list)
149+
struct instruction *insn)
152150
{
151+
struct list_head *ops_list = &insn->stack_ops;
153152
const struct elf *elf = file->elf;
154-
struct insn insn;
153+
struct insn ins;
155154
int x86_64, ret;
156155
unsigned char op1, op2, op3, prefix,
157156
rex = 0, rex_b = 0, rex_r = 0, rex_w = 0, rex_x = 0,
@@ -165,42 +164,42 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
165164
if (x86_64 == -1)
166165
return -1;
167166

168-
ret = insn_decode(&insn, sec->data->d_buf + offset, maxlen,
167+
ret = insn_decode(&ins, sec->data->d_buf + offset, maxlen,
169168
x86_64 ? INSN_MODE_64 : INSN_MODE_32);
170169
if (ret < 0) {
171170
WARN("can't decode instruction at %s:0x%lx", sec->name, offset);
172171
return -1;
173172
}
174173

175-
*len = insn.length;
176-
*type = INSN_OTHER;
174+
insn->len = ins.length;
175+
insn->type = INSN_OTHER;
177176

178-
if (insn.vex_prefix.nbytes)
177+
if (ins.vex_prefix.nbytes)
179178
return 0;
180179

181-
prefix = insn.prefixes.bytes[0];
180+
prefix = ins.prefixes.bytes[0];
182181

183-
op1 = insn.opcode.bytes[0];
184-
op2 = insn.opcode.bytes[1];
185-
op3 = insn.opcode.bytes[2];
182+
op1 = ins.opcode.bytes[0];
183+
op2 = ins.opcode.bytes[1];
184+
op3 = ins.opcode.bytes[2];
186185

187-
if (insn.rex_prefix.nbytes) {
188-
rex = insn.rex_prefix.bytes[0];
186+
if (ins.rex_prefix.nbytes) {
187+
rex = ins.rex_prefix.bytes[0];
189188
rex_w = X86_REX_W(rex) >> 3;
190189
rex_r = X86_REX_R(rex) >> 2;
191190
rex_x = X86_REX_X(rex) >> 1;
192191
rex_b = X86_REX_B(rex);
193192
}
194193

195-
if (insn.modrm.nbytes) {
196-
modrm = insn.modrm.bytes[0];
194+
if (ins.modrm.nbytes) {
195+
modrm = ins.modrm.bytes[0];
197196
modrm_mod = X86_MODRM_MOD(modrm);
198197
modrm_reg = X86_MODRM_REG(modrm) + 8*rex_r;
199198
modrm_rm = X86_MODRM_RM(modrm) + 8*rex_b;
200199
}
201200

202-
if (insn.sib.nbytes) {
203-
sib = insn.sib.bytes[0];
201+
if (ins.sib.nbytes) {
202+
sib = ins.sib.bytes[0];
204203
/* sib_scale = X86_SIB_SCALE(sib); */
205204
sib_index = X86_SIB_INDEX(sib) + 8*rex_x;
206205
sib_base = X86_SIB_BASE(sib) + 8*rex_b;
@@ -254,7 +253,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
254253
break;
255254

256255
case 0x70 ... 0x7f:
257-
*type = INSN_JUMP_CONDITIONAL;
256+
insn->type = INSN_JUMP_CONDITIONAL;
258257
break;
259258

260259
case 0x80 ... 0x83:
@@ -278,7 +277,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
278277
if (!rm_is_reg(CFI_SP))
279278
break;
280279

281-
imm = insn.immediate.value;
280+
imm = ins.immediate.value;
282281
if (op1 & 2) { /* sign extend */
283282
if (op1 & 1) { /* imm32 */
284283
imm <<= 32;
@@ -309,7 +308,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
309308
ADD_OP(op) {
310309
op->src.type = OP_SRC_AND;
311310
op->src.reg = CFI_SP;
312-
op->src.offset = insn.immediate.value;
311+
op->src.offset = ins.immediate.value;
313312
op->dest.type = OP_DEST_REG;
314313
op->dest.reg = CFI_SP;
315314
}
@@ -356,7 +355,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
356355
op->src.reg = CFI_SP;
357356
op->dest.type = OP_DEST_REG_INDIRECT;
358357
op->dest.reg = modrm_rm;
359-
op->dest.offset = insn.displacement.value;
358+
op->dest.offset = ins.displacement.value;
360359
}
361360
break;
362361
}
@@ -389,7 +388,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
389388
op->src.reg = modrm_reg;
390389
op->dest.type = OP_DEST_REG_INDIRECT;
391390
op->dest.reg = CFI_BP;
392-
op->dest.offset = insn.displacement.value;
391+
op->dest.offset = ins.displacement.value;
393392
}
394393
break;
395394
}
@@ -402,7 +401,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
402401
op->src.reg = modrm_reg;
403402
op->dest.type = OP_DEST_REG_INDIRECT;
404403
op->dest.reg = CFI_SP;
405-
op->dest.offset = insn.displacement.value;
404+
op->dest.offset = ins.displacement.value;
406405
}
407406
break;
408407
}
@@ -419,7 +418,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
419418
ADD_OP(op) {
420419
op->src.type = OP_SRC_REG_INDIRECT;
421420
op->src.reg = CFI_BP;
422-
op->src.offset = insn.displacement.value;
421+
op->src.offset = ins.displacement.value;
423422
op->dest.type = OP_DEST_REG;
424423
op->dest.reg = modrm_reg;
425424
}
@@ -432,7 +431,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
432431
ADD_OP(op) {
433432
op->src.type = OP_SRC_REG_INDIRECT;
434433
op->src.reg = CFI_SP;
435-
op->src.offset = insn.displacement.value;
434+
op->src.offset = ins.displacement.value;
436435
op->dest.type = OP_DEST_REG;
437436
op->dest.reg = modrm_reg;
438437
}
@@ -464,7 +463,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
464463

465464
/* lea disp(%src), %dst */
466465
ADD_OP(op) {
467-
op->src.offset = insn.displacement.value;
466+
op->src.offset = ins.displacement.value;
468467
if (!op->src.offset) {
469468
/* lea (%src), %dst */
470469
op->src.type = OP_SRC_REG;
@@ -487,7 +486,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
487486
break;
488487

489488
case 0x90:
490-
*type = INSN_NOP;
489+
insn->type = INSN_NOP;
491490
break;
492491

493492
case 0x9c:
@@ -511,39 +510,39 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
511510
if (op2 == 0x01) {
512511

513512
if (modrm == 0xca)
514-
*type = INSN_CLAC;
513+
insn->type = INSN_CLAC;
515514
else if (modrm == 0xcb)
516-
*type = INSN_STAC;
515+
insn->type = INSN_STAC;
517516

518517
} else if (op2 >= 0x80 && op2 <= 0x8f) {
519518

520-
*type = INSN_JUMP_CONDITIONAL;
519+
insn->type = INSN_JUMP_CONDITIONAL;
521520

522521
} else if (op2 == 0x05 || op2 == 0x07 || op2 == 0x34 ||
523522
op2 == 0x35) {
524523

525524
/* sysenter, sysret */
526-
*type = INSN_CONTEXT_SWITCH;
525+
insn->type = INSN_CONTEXT_SWITCH;
527526

528527
} else if (op2 == 0x0b || op2 == 0xb9) {
529528

530529
/* ud2 */
531-
*type = INSN_BUG;
530+
insn->type = INSN_BUG;
532531

533532
} else if (op2 == 0x0d || op2 == 0x1f) {
534533

535534
/* nopl/nopw */
536-
*type = INSN_NOP;
535+
insn->type = INSN_NOP;
537536

538537
} else if (op2 == 0x1e) {
539538

540539
if (prefix == 0xf3 && (modrm == 0xfa || modrm == 0xfb))
541-
*type = INSN_ENDBR;
540+
insn->type = INSN_ENDBR;
542541

543542

544543
} else if (op2 == 0x38 && op3 == 0xf8) {
545-
if (insn.prefixes.nbytes == 1 &&
546-
insn.prefixes.bytes[0] == 0xf2) {
544+
if (ins.prefixes.nbytes == 1 &&
545+
ins.prefixes.bytes[0] == 0xf2) {
547546
/* ENQCMD cannot be used in the kernel. */
548547
WARN("ENQCMD instruction at %s:%lx", sec->name,
549548
offset);
@@ -591,29 +590,29 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
591590

592591
case 0xcc:
593592
/* int3 */
594-
*type = INSN_TRAP;
593+
insn->type = INSN_TRAP;
595594
break;
596595

597596
case 0xe3:
598597
/* jecxz/jrcxz */
599-
*type = INSN_JUMP_CONDITIONAL;
598+
insn->type = INSN_JUMP_CONDITIONAL;
600599
break;
601600

602601
case 0xe9:
603602
case 0xeb:
604-
*type = INSN_JUMP_UNCONDITIONAL;
603+
insn->type = INSN_JUMP_UNCONDITIONAL;
605604
break;
606605

607606
case 0xc2:
608607
case 0xc3:
609-
*type = INSN_RETURN;
608+
insn->type = INSN_RETURN;
610609
break;
611610

612611
case 0xc7: /* mov imm, r/m */
613612
if (!opts.noinstr)
614613
break;
615614

616-
if (insn.length == 3+4+4 && !strncmp(sec->name, ".init.text", 10)) {
615+
if (ins.length == 3+4+4 && !strncmp(sec->name, ".init.text", 10)) {
617616
struct reloc *immr, *disp;
618617
struct symbol *func;
619618
int idx;
@@ -661,17 +660,17 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
661660

662661
case 0xca: /* retf */
663662
case 0xcb: /* retf */
664-
*type = INSN_CONTEXT_SWITCH;
663+
insn->type = INSN_CONTEXT_SWITCH;
665664
break;
666665

667666
case 0xe0: /* loopne */
668667
case 0xe1: /* loope */
669668
case 0xe2: /* loop */
670-
*type = INSN_JUMP_CONDITIONAL;
669+
insn->type = INSN_JUMP_CONDITIONAL;
671670
break;
672671

673672
case 0xe8:
674-
*type = INSN_CALL;
673+
insn->type = INSN_CALL;
675674
/*
676675
* For the impact on the stack, a CALL behaves like
677676
* a PUSH of an immediate value (the return address).
@@ -683,30 +682,30 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
683682
break;
684683

685684
case 0xfc:
686-
*type = INSN_CLD;
685+
insn->type = INSN_CLD;
687686
break;
688687

689688
case 0xfd:
690-
*type = INSN_STD;
689+
insn->type = INSN_STD;
691690
break;
692691

693692
case 0xff:
694693
if (modrm_reg == 2 || modrm_reg == 3) {
695694

696-
*type = INSN_CALL_DYNAMIC;
697-
if (has_notrack_prefix(&insn))
695+
insn->type = INSN_CALL_DYNAMIC;
696+
if (has_notrack_prefix(&ins))
698697
WARN("notrack prefix found at %s:0x%lx", sec->name, offset);
699698

700699
} else if (modrm_reg == 4) {
701700

702-
*type = INSN_JUMP_DYNAMIC;
703-
if (has_notrack_prefix(&insn))
701+
insn->type = INSN_JUMP_DYNAMIC;
702+
if (has_notrack_prefix(&ins))
704703
WARN("notrack prefix found at %s:0x%lx", sec->name, offset);
705704

706705
} else if (modrm_reg == 5) {
707706

708707
/* jmpf */
709-
*type = INSN_CONTEXT_SWITCH;
708+
insn->type = INSN_CONTEXT_SWITCH;
710709

711710
} else if (modrm_reg == 6) {
712711

@@ -723,7 +722,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
723722
break;
724723
}
725724

726-
*immediate = insn.immediate.nbytes ? insn.immediate.value : 0;
725+
insn->immediate = ins.immediate.nbytes ? ins.immediate.value : 0;
727726

728727
return 0;
729728
}

tools/objtool/check.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ static int decode_instructions(struct objtool_file *file)
406406

407407
ret = arch_decode_instruction(file, sec, offset,
408408
sec->sh.sh_size - offset,
409-
&insn->len, &insn->type,
410-
&insn->immediate,
411-
&insn->stack_ops);
409+
insn);
412410
if (ret)
413411
goto err;
414412

tools/objtool/include/objtool/arch.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ void arch_initial_func_cfi_state(struct cfi_init_state *state);
7575

7676
int arch_decode_instruction(struct objtool_file *file, const struct section *sec,
7777
unsigned long offset, unsigned int maxlen,
78-
unsigned int *len, enum insn_type *type,
79-
unsigned long *immediate,
80-
struct list_head *ops_list);
78+
struct instruction *insn);
8179

8280
bool arch_callee_saved_reg(unsigned char reg);
8381

0 commit comments

Comments
 (0)