forked from kennytm/Miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_pcrel.idc
196 lines (164 loc) · 5.19 KB
/
fix_pcrel.idc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
fix_pcrel.idc ... IDA Pro 6.1 script to fix PC-relative offsets in LLVM-
generated objects.
Copyright (C) 2012 KennyTM~
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <idc.idc>
static is_arm()
{
return ((GetLongPrm(INF_PROCNAME) & 0xffffff) == 0x4d5241);
}
#define IT_NOP 0
#define IT_SET_PC 1
#define IT_ADD_IMM 2
#define IT_COMMIT 3
#define IT_DISMISS 4
static parse_instruction(ea, pc_store)
{
auto instr, opcode;
auto instr_type, instr_reg, instr_op, tmp;
instr = DecodeInstruction(ea);
if (!instr)
return;
opcode = GetMnem(ea);
instr_type = IT_NOP;
instr_reg = -1;
instr_op = -1;
// Check what to do w.r.t. each instruction.
if (opcode == "ADD")
{
if (instr[1].type == o_reg && instr[1].reg == 15) {
if (instr.n == 2) {
instr_reg = instr[0].reg;
instr_type = IT_SET_PC;
} else if (instr.n == 3 && instr[2].type == o_reg) {
instr_reg = instr[2].reg;
instr_type = IT_SET_PC;
}
}
}
else if (opcode == "LDR" || opcode == "STR")
{
if (instr[1].type == o_phrase && instr[1].reg == 15) {
instr_reg = instr[1].specflag1;
instr_type = IT_SET_PC;
}
}
else if (opcode == "MOVT" || opcode == "MOVT.W")
{
if (instr[0].type == o_reg && instr[1].type == o_imm)
{
instr_reg = instr[0].reg;
instr_type = IT_ADD_IMM;
instr_op = instr[1].value << 16;
}
}
else if (opcode == "MOV" || opcode == "MOVW")
{
if (instr[0].type == o_reg)
{
instr_reg = instr[0].reg;
instr_op = 1;
if (instr[1].type == o_imm)
instr_type = IT_COMMIT;
else
instr_type = IT_DISMISS;
}
}
// Perform the corresponding action.
if (instr_type == IT_SET_PC)
{
pc_store[instr_reg] = ea + (GetReg(ea, "T") ? 4 : 8);
}
else if (instr_type == IT_ADD_IMM)
{
tmp = pc_store[instr_reg];
if (tmp != BADADDR)
pc_store[instr_reg] = tmp + instr_op;
}
else if (instr_type == IT_COMMIT)
{
tmp = pc_store[instr_reg];
if (tmp != BADADDR)
{
pc_store[instr_reg] = BADADDR;
OpOffEx(ea, instr_op, REF_OFF32|REFINFO_NOBASE, -1, tmp, 0);
pc_store.fix = pc_store.fix + 1;
}
}
else if (instr_type == IT_DISMISS)
{
pc_store[instr_reg] = BADADDR;
}
}
static prev_ea(ea, ea_min, pc_store)
{
auto res, i;
for (i = 0; i < 15; ++ i)
if (pc_store[i] != BADADDR) {
return PrevHead(ea, ea_min);
}
res = FindText(ea, SEARCH_UP, 0, -1, "PC");
if (res < ea_min)
res = BADADDR;
return res;
}
static main()
{
auto ea_begin, ea_end, text_seg_id, seg_ea, ea, pc_store, i, report_ea, prev_status;
if (!is_arm())
{
Warning("fix_pcrel.idc is only suitable for ARM processors.");
return;
}
ea_begin = SelStart();
ea = SelEnd();
if (ea == BADADDR)
{
text_seg_id = SegByName("__text");
if (text_seg_id == BADADDR)
{
Warning("__text segment not found --- is this a real Mach-O file?");
return;
}
seg_ea = SegByBase(text_seg_id);
ea_begin = SegStart(seg_ea);
ea = SegEnd(seg_ea);
}
report_ea = ea;
pc_store = object();
for (i = 0; i < 15; ++ i)
pc_store[i] = BADADDR;
pc_store.fix = 0;
prev_status = SetStatus(IDA_STATUS_WORK);
while (ea != BADADDR)
{
ea = prev_ea(ea, ea_begin, pc_store);
parse_instruction(ea, pc_store);
if (ea < report_ea)
{
report_ea = ea - 0x4000;
Message("Fixing PCRel, at %a, fixed %d instructions...\n", report_ea, pc_store.fix);
}
}
SetStatus(prev_status);
Message("Done! (Fixed %d instructions)\n", pc_store.fix);
}