1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <objtool/check.h>
6 #include <objtool/elf.h>
7 #include <objtool/arch.h>
8 #include <objtool/warn.h>
9 #include <objtool/builtin.h>
10 #include <objtool/endianness.h>
11 
arch_ftrace_match(char * name)12 int arch_ftrace_match(char *name)
13 {
14 	return !strcmp(name, "_mcount");
15 }
16 
arch_dest_reloc_offset(int addend)17 unsigned long arch_dest_reloc_offset(int addend)
18 {
19 	return addend;
20 }
21 
arch_callee_saved_reg(unsigned char reg)22 bool arch_callee_saved_reg(unsigned char reg)
23 {
24 	return false;
25 }
26 
arch_decode_hint_reg(u8 sp_reg,int * base)27 int arch_decode_hint_reg(u8 sp_reg, int *base)
28 {
29 	exit(-1);
30 }
31 
arch_nop_insn(int len)32 const char *arch_nop_insn(int len)
33 {
34 	exit(-1);
35 }
36 
arch_ret_insn(int len)37 const char *arch_ret_insn(int len)
38 {
39 	exit(-1);
40 }
41 
arch_decode_instruction(struct objtool_file * file,const struct section * sec,unsigned long offset,unsigned int maxlen,struct instruction * insn)42 int arch_decode_instruction(struct objtool_file *file, const struct section *sec,
43 			    unsigned long offset, unsigned int maxlen,
44 			    struct instruction *insn)
45 {
46 	unsigned int opcode;
47 	enum insn_type typ;
48 	unsigned long imm;
49 	u32 ins;
50 
51 	ins = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
52 	opcode = ins >> 26;
53 	typ = INSN_OTHER;
54 	imm = 0;
55 
56 	switch (opcode) {
57 	case 18: /* b[l][a] */
58 		if (ins == 0x48000005)	/* bl .+4 */
59 			typ = INSN_OTHER;
60 		else if (ins & 1)	/* bl[a] */
61 			typ = INSN_CALL;
62 		else		/* b[a] */
63 			typ = INSN_JUMP_UNCONDITIONAL;
64 
65 		imm = ins & 0x3fffffc;
66 		if (imm & 0x2000000)
67 			imm -= 0x4000000;
68 		imm |= ins & 2;	/* AA flag */
69 		break;
70 	}
71 
72 	if (opcode == 1)
73 		insn->len = 8;
74 	else
75 		insn->len = 4;
76 
77 	insn->type = typ;
78 	insn->immediate = imm;
79 
80 	return 0;
81 }
82 
arch_jump_destination(struct instruction * insn)83 unsigned long arch_jump_destination(struct instruction *insn)
84 {
85 	if (insn->immediate & 2)
86 		return insn->immediate & ~2;
87 
88 	return insn->offset + insn->immediate;
89 }
90 
arch_pc_relative_reloc(struct reloc * reloc)91 bool arch_pc_relative_reloc(struct reloc *reloc)
92 {
93 	/*
94 	 * The powerpc build only allows certain relocation types, see
95 	 * relocs_check.sh, and none of those accepted are PC relative.
96 	 */
97 	return false;
98 }
99 
arch_initial_func_cfi_state(struct cfi_init_state * state)100 void arch_initial_func_cfi_state(struct cfi_init_state *state)
101 {
102 	int i;
103 
104 	for (i = 0; i < CFI_NUM_REGS; i++) {
105 		state->regs[i].base = CFI_UNDEFINED;
106 		state->regs[i].offset = 0;
107 	}
108 
109 	/* initial CFA (call frame address) */
110 	state->cfa.base = CFI_SP;
111 	state->cfa.offset = 0;
112 
113 	/* initial LR (return address) */
114 	state->regs[CFI_RA].base = CFI_CFA;
115 	state->regs[CFI_RA].offset = 0;
116 }
117 
arch_reloc_size(struct reloc * reloc)118 unsigned int arch_reloc_size(struct reloc *reloc)
119 {
120 	switch (reloc_type(reloc)) {
121 	case R_PPC_REL32:
122 	case R_PPC_ADDR32:
123 	case R_PPC_UADDR32:
124 	case R_PPC_PLT32:
125 	case R_PPC_PLTREL32:
126 		return 4;
127 	default:
128 		return 8;
129 	}
130 }
131