blob: bbd52f04544f7418350a6ff53286875a700ba602 [file] [log] [blame]
[email protected]39ed9732013-06-20 10:17:531// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "courgette/disassembler_elf_32_arm.h"
6
mostynb1007a4a2016-04-11 23:18:067#include <memory>
etiennep7d4e8ee2016-05-11 20:13:368#include <utility>
[email protected]39ed9732013-06-20 10:17:539#include <vector>
10
[email protected]39ed9732013-06-20 10:17:5311#include "base/logging.h"
[email protected]39ed9732013-06-20 10:17:5312#include "courgette/assembly_program.h"
13#include "courgette/courgette.h"
[email protected]39ed9732013-06-20 10:17:5314
15namespace courgette {
16
aviab98dcc92015-12-21 19:35:3317CheckBool DisassemblerElf32ARM::Compress(ARM_RVA type,
18 uint32_t arm_op,
19 RVA rva,
20 uint16_t* c_op,
21 uint32_t* addr) {
huangsdda11d062016-03-14 16:35:3922 // Notation for bit ranges in comments:
23 // - Listing bits from highest to lowest.
24 // - A-Z or (j1), (j2), etc.: single bit in source.
25 // - a-z: multiple, consecutive bits in source.
[email protected]2b637b62013-08-01 00:11:2426 switch (type) {
27 case ARM_OFF8: {
huangsdda11d062016-03-14 16:35:3928 // Encoding T1.
29 // The offset is given by lower 8 bits of the op. It is a 9-bit offset,
30 // shifted right 1 bit, and signed extended.
31 // arm_op = aaaaaaaa Snnnnnnn
32 // *addr := SSSSSSSS SSSSSSSS SSSSSSSS nnnnnnn0 + 100
33 // *c_op := 00010000 aaaaaaaa
aviab98dcc92015-12-21 19:35:3334 uint32_t temp = (arm_op & 0x00FF) << 1;
[email protected]144c8e92013-07-23 21:18:1935 if (temp & 0x0100)
36 temp |= 0xFFFFFE00;
37 temp += 4; // Offset from _next_ PC.
[email protected]2b637b62013-08-01 00:11:2438
39 (*addr) = temp;
aviab98dcc92015-12-21 19:35:3340 (*c_op) = static_cast<uint16_t>(arm_op >> 8) | 0x1000;
[email protected]144c8e92013-07-23 21:18:1941 break;
[email protected]2b637b62013-08-01 00:11:2442 }
43 case ARM_OFF11: {
huangsdda11d062016-03-14 16:35:3944 // Encoding T2.
45 // The offset is given by lower 11 bits of the op, and is a 12-bit offset,
46 // shifted right 1 bit, and sign extended.
47 // arm_op = aaaaaSnn nnnnnnnn
48 // *addr := SSSSSSSS SSSSSSSS SSSSSnnn nnnnnnn0 + 100
49 // *c_op := 00100000 000aaaaa
aviab98dcc92015-12-21 19:35:3350 uint32_t temp = (arm_op & 0x07FF) << 1;
[email protected]144c8e92013-07-23 21:18:1951 if (temp & 0x00000800)
52 temp |= 0xFFFFF000;
53 temp += 4; // Offset from _next_ PC.
[email protected]2b637b62013-08-01 00:11:2454
55 (*addr) = temp;
aviab98dcc92015-12-21 19:35:3356 (*c_op) = static_cast<uint16_t>(arm_op >> 11) | 0x2000;
[email protected]144c8e92013-07-23 21:18:1957 break;
[email protected]2b637b62013-08-01 00:11:2458 }
59 case ARM_OFF24: {
60 // The offset is given by the lower 24-bits of the op, shifted
61 // left 2 bits, and sign extended.
huangsdda11d062016-03-14 16:35:3962 // arm_op = aaaaaaaa Snnnnnnn nnnnnnnn nnnnnnnn
63 // *addr := SSSSSSSn nnnnnnnn nnnnnnnn nnnnnn00 + 1000
64 // *c_op := 00110000 aaaaaaaa
aviab98dcc92015-12-21 19:35:3365 uint32_t temp = (arm_op & 0x00FFFFFF) << 2;
[email protected]2b637b62013-08-01 00:11:2466 if (temp & 0x02000000)
67 temp |= 0xFC000000;
68 temp += 8;
69
70 (*addr) = temp;
71 (*c_op) = (arm_op >> 24) | 0x3000;
72 break;
73 }
74 case ARM_OFF25: {
huangsdda11d062016-03-14 16:35:3975 // Encoding T4.
76 // arm_op = aaaaaSmm mmmmmmmm BC(j1)D(j2)nnn nnnnnnnn
77 // where CD is in {01, 10, 11}
78 // i1 := ~(j1 ^ S)
79 // i2 := ~(j2 ^ S)
80 // If CD == 10:
81 // pppp := (rva % 4 == 0) ? 0100 : 0010
82 // Else:
83 // pppp := 0100
84 // *addr := SSSSSSSS (i1)(i2)mmmmmm mmmmnnnn nnnnnnn0 + pppp
85 // *c_op := 0100pppp aaaaaBCD
86 // TODO(huangs): aaaaa = 11110 and B = 1 always? Investigate and fix.
aviab98dcc92015-12-21 19:35:3387 uint32_t temp = 0;
[email protected]2b637b62013-08-01 00:11:2488 temp |= (arm_op & 0x000007FF) << 1; // imm11
89 temp |= (arm_op & 0x03FF0000) >> 4; // imm10
90
aviab98dcc92015-12-21 19:35:3391 uint32_t S = (arm_op & (1 << 26)) >> 26;
92 uint32_t j2 = (arm_op & (1 << 11)) >> 11;
93 uint32_t j1 = (arm_op & (1 << 13)) >> 13;
huangsdda11d062016-03-14 16:35:3994 bool bit12 = ((arm_op & (1 << 12)) >> 12) != 0; // D
95 bool bit14 = ((arm_op & (1 << 14)) >> 14) != 0; // C
[email protected]2b637b62013-08-01 00:11:2496
aviab98dcc92015-12-21 19:35:3397 uint32_t i2 = ~(j2 ^ S) & 1;
98 uint32_t i1 = ~(j1 ^ S) & 1;
[email protected]2b637b62013-08-01 00:11:2499 bool toARM = bit14 && !bit12;
100
101 temp |= (S << 24) | (i1 << 23) | (i2 << 22);
102
huangs7b221a52016-11-09 22:28:23103 if (temp & 0x01000000) // sign extension
[email protected]2b637b62013-08-01 00:11:24104 temp |= 0xFE000000;
aviab98dcc92015-12-21 19:35:33105 uint32_t prefetch;
[email protected]2b637b62013-08-01 00:11:24106 if (toARM) {
huangsdda11d062016-03-14 16:35:39107 // Align PC on 4-byte boundary.
aviab98dcc92015-12-21 19:35:33108 uint32_t align4byte = (rva % 4) ? 2 : 4;
[email protected]2b637b62013-08-01 00:11:24109 prefetch = align4byte;
110 } else {
111 prefetch = 4;
112 }
113 temp += prefetch;
114 (*addr) = temp;
115
aviab98dcc92015-12-21 19:35:33116 uint32_t temp2 = 0x4000;
huangsdda11d062016-03-14 16:35:39117 temp2 |= (arm_op & (1 << 12)) >> 12; // .......D
118 temp2 |= (arm_op & (1 << 14)) >> 13; // ......C.
119 temp2 |= (arm_op & (1 << 15)) >> 13; // .....B..
120 temp2 |= (arm_op & 0xF8000000) >> 24; // aaaaa...
[email protected]2b637b62013-08-01 00:11:24121 temp2 |= (prefetch & 0x0000000F) << 8;
aviab98dcc92015-12-21 19:35:33122 (*c_op) = static_cast<uint16_t>(temp2);
[email protected]2b637b62013-08-01 00:11:24123 break;
124 }
125 case ARM_OFF21: {
huangsdda11d062016-03-14 16:35:39126 // Encoding T3.
127 // arm_op = 11110Scc ccmmmmmm 10(j1)0(j2)nnn nnnnnnnn
128 // *addr := SSSSSSSS SSSS(j1)(j2)mm mmmmnnnn nnnnnnn0 + 100
129 // *c_op := 01010000 0000cccc
aviab98dcc92015-12-21 19:35:33130 uint32_t temp = 0;
[email protected]11336c02013-09-25 19:05:51131 temp |= (arm_op & 0x000007FF) << 1; // imm11
132 temp |= (arm_op & 0x003F0000) >> 4; // imm6
[email protected]2b637b62013-08-01 00:11:24133
aviab98dcc92015-12-21 19:35:33134 uint32_t S = (arm_op & (1 << 26)) >> 26;
huangsdda11d062016-03-14 16:35:39135 // TODO(huangs): Check with docs: Perhaps j1, j2 should swap?
aviab98dcc92015-12-21 19:35:33136 uint32_t j2 = (arm_op & (1 << 11)) >> 11;
137 uint32_t j1 = (arm_op & (1 << 13)) >> 13;
[email protected]2b637b62013-08-01 00:11:24138
139 temp |= (S << 20) | (j1 << 19) | (j2 << 18);
140
[email protected]11336c02013-09-25 19:05:51141 if (temp & 0x00100000) // sign extension
[email protected]2b637b62013-08-01 00:11:24142 temp |= 0xFFE00000;
143 temp += 4;
144 (*addr) = temp;
145
aviab98dcc92015-12-21 19:35:33146 uint32_t temp2 = 0x5000;
[email protected]2b637b62013-08-01 00:11:24147 temp2 |= (arm_op & 0x03C00000) >> 22; // just save the cond
aviab98dcc92015-12-21 19:35:33148 (*c_op) = static_cast<uint16_t>(temp2);
[email protected]2b637b62013-08-01 00:11:24149 break;
150 }
[email protected]144c8e92013-07-23 21:18:19151 default:
152 return false;
153 }
[email protected]144c8e92013-07-23 21:18:19154 return true;
155}
156
aviab98dcc92015-12-21 19:35:33157CheckBool DisassemblerElf32ARM::Decompress(ARM_RVA type,
158 uint16_t c_op,
159 uint32_t addr,
160 uint32_t* arm_op) {
[email protected]2b637b62013-08-01 00:11:24161 switch (type) {
162 case ARM_OFF8:
huangsdda11d062016-03-14 16:35:39163 // addr = SSSSSSSS SSSSSSSS SSSSSSSS nnnnnnn0 + 100
164 // c_op = 00010000 aaaaaaaa
165 // *arm_op := aaaaaaaa Snnnnnnn
[email protected]2b637b62013-08-01 00:11:24166 (*arm_op) = ((c_op & 0x0FFF) << 8) | (((addr - 4) >> 1) & 0x000000FF);
167 break;
168 case ARM_OFF11:
huangsdda11d062016-03-14 16:35:39169 // addr = SSSSSSSS SSSSSSSS SSSSSnnn nnnnnnn0 + 100
170 // c_op = 00100000 000aaaaa
171 // *arm_op := aaaaaSnn nnnnnnnn
[email protected]2b637b62013-08-01 00:11:24172 (*arm_op) = ((c_op & 0x0FFF) << 11) | (((addr - 4) >> 1) & 0x000007FF);
173 break;
174 case ARM_OFF24:
huangsdda11d062016-03-14 16:35:39175 // addr = SSSSSSSn nnnnnnnn nnnnnnnn nnnnnn00 + 1000
176 // c_op = 00110000 aaaaaaaa
177 // *arm_op := aaaaaaaa Snnnnnnn nnnnnnnn nnnnnnnn
[email protected]2b637b62013-08-01 00:11:24178 (*arm_op) = ((c_op & 0x0FFF) << 24) | (((addr - 8) >> 2) & 0x00FFFFFF);
179 break;
180 case ARM_OFF25: {
huangsdda11d062016-03-14 16:35:39181 // addr = SSSSSSSS (i1)(i2)mmmmmm mmmmnnnn nnnnnnn0 + pppp
182 // c_op = 0100pppp aaaaaBCD
183 // j1 := ~i1 ^ S
184 // j2 := ~i2 ^ S
185 // *arm_op := aaaaaSmm mmmmmmmm BC(j1)D(j2)nnn nnnnnnnn
aviab98dcc92015-12-21 19:35:33186 uint32_t temp = 0;
[email protected]2b637b62013-08-01 00:11:24187 temp |= (c_op & (1 << 0)) << 12;
188 temp |= (c_op & (1 << 1)) << 13;
189 temp |= (c_op & (1 << 2)) << 13;
190 temp |= (c_op & (0xF8000000 >> 24)) << 24;
191
aviab98dcc92015-12-21 19:35:33192 uint32_t prefetch = (c_op & 0x0F00) >> 8;
[email protected]2b637b62013-08-01 00:11:24193 addr -= prefetch;
194
195 addr &= 0x01FFFFFF;
196
aviab98dcc92015-12-21 19:35:33197 uint32_t S = (addr & (1 << 24)) >> 24;
198 uint32_t i1 = (addr & (1 << 23)) >> 23;
199 uint32_t i2 = (addr & (1 << 22)) >> 22;
[email protected]2b637b62013-08-01 00:11:24200
aviab98dcc92015-12-21 19:35:33201 uint32_t j1 = ((~i1) ^ S) & 1;
202 uint32_t j2 = ((~i2) ^ S) & 1;
[email protected]2b637b62013-08-01 00:11:24203
204 temp |= S << 26;
205 temp |= j2 << 11;
206 temp |= j1 << 13;
207
208 temp |= (addr & (0x000007FF << 1)) >> 1;
209 temp |= (addr & (0x03FF0000 >> 4)) << 4;
210
211 (*arm_op) = temp;
212 break;
213 }
214 case ARM_OFF21: {
huangsdda11d062016-03-14 16:35:39215 // addr = SSSSSSSS SSSS(j1)(j2)mm mmmmnnnn nnnnnnn0 + 100
216 // c_op = 01010000 0000cccc
217 // *arm_op := 11110Scc ccmmmmmm 10(j1)0(j2)nnn nnnnnnnn
aviab98dcc92015-12-21 19:35:33218 uint32_t temp = 0xF0008000;
[email protected]2b637b62013-08-01 00:11:24219 temp |= (c_op & (0x03C00000 >> 22)) << 22;
220
221 addr -= 4;
222 addr &= 0x001FFFFF;
223
aviab98dcc92015-12-21 19:35:33224 uint32_t S = (addr & (1 << 20)) >> 20;
225 uint32_t j1 = (addr & (1 << 19)) >> 19;
226 uint32_t j2 = (addr & (1 << 18)) >> 18;
[email protected]2b637b62013-08-01 00:11:24227
228 temp |= S << 26;
229 temp |= j2 << 11;
230 temp |= j1 << 13;
231
232 temp |= (addr & (0x000007FF << 1)) >> 1;
233 temp |= (addr & (0x003F0000 >> 4)) << 4;
234
235 (*arm_op) = temp;
236 break;
237 }
238 default:
239 return false;
240 }
241 return true;
242}
243
aviab98dcc92015-12-21 19:35:33244uint16_t DisassemblerElf32ARM::TypedRVAARM::op_size() const {
[email protected]2b637b62013-08-01 00:11:24245 switch (type_) {
246 case ARM_OFF8:
247 return 2;
248 case ARM_OFF11:
249 return 2;
250 case ARM_OFF24:
251 return 4;
252 case ARM_OFF25:
253 return 4;
254 case ARM_OFF21:
255 return 4;
256 default:
[email protected]0ef486b2014-07-08 08:40:56257 return 0xFFFF;
[email protected]2b637b62013-08-01 00:11:24258 }
259}
260
261CheckBool DisassemblerElf32ARM::TypedRVAARM::ComputeRelativeTarget(
aviab98dcc92015-12-21 19:35:33262 const uint8_t* op_pointer) {
[email protected]2b637b62013-08-01 00:11:24263 arm_op_ = op_pointer;
264 switch (type_) {
huangsdda11d062016-03-14 16:35:39265 case ARM_OFF8: // Falls through.
[email protected]2b637b62013-08-01 00:11:24266 case ARM_OFF11: {
267 RVA relative_target;
huangsdda11d062016-03-14 16:35:39268 CheckBool ret = Compress(type_,
269 Read16LittleEndian(op_pointer),
270 rva(),
271 &c_op_,
272 &relative_target);
[email protected]2b637b62013-08-01 00:11:24273 set_relative_target(relative_target);
274 return ret;
275 }
276 case ARM_OFF24: {
277 RVA relative_target;
huangsdda11d062016-03-14 16:35:39278 CheckBool ret = Compress(type_,
279 Read32LittleEndian(op_pointer),
280 rva(),
281 &c_op_,
282 &relative_target);
[email protected]2b637b62013-08-01 00:11:24283 set_relative_target(relative_target);
284 return ret;
285 }
huangsdda11d062016-03-14 16:35:39286 case ARM_OFF25: // Falls through.
[email protected]2b637b62013-08-01 00:11:24287 case ARM_OFF21: {
288 // A thumb-2 op is 32 bits stored as two 16-bit words
aviab98dcc92015-12-21 19:35:33289 uint32_t pval = (Read16LittleEndian(op_pointer) << 16) |
290 Read16LittleEndian(op_pointer + 2);
[email protected]2b637b62013-08-01 00:11:24291 RVA relative_target;
292 CheckBool ret = Compress(type_, pval, rva(), &c_op_, &relative_target);
293 set_relative_target(relative_target);
294 return ret;
295 }
296 default:
297 return false;
298 }
299}
300
301CheckBool DisassemblerElf32ARM::TypedRVAARM::EmitInstruction(
huangs7b221a52016-11-09 22:28:23302 Label* label,
303 InstructionReceptor* receptor) {
304 return receptor->EmitRel32ARM(c_op(), label, arm_op_, op_size());
[email protected]2b637b62013-08-01 00:11:24305}
306
etiennep5059bca2016-07-08 17:55:20307DisassemblerElf32ARM::DisassemblerElf32ARM(const uint8_t* start, size_t length)
308 : DisassemblerElf32(start, length) {}
[email protected]39ed9732013-06-20 10:17:53309
huangsdda11d062016-03-14 16:35:39310// Convert an ELF relocation struction into an RVA.
[email protected]39ed9732013-06-20 10:17:53311CheckBool DisassemblerElf32ARM::RelToRVA(Elf32_Rel rel, RVA* result) const {
huangsdda11d062016-03-14 16:35:39312 // The rightmost byte of r_info is the type.
scottmg4a95ca52016-03-12 23:54:56313 elf32_rel_arm_type_values type =
huangsdda11d062016-03-14 16:35:39314 static_cast<elf32_rel_arm_type_values>(rel.r_info & 0xFF);
scottmg4a95ca52016-03-12 23:54:56315
huangsdda11d062016-03-14 16:35:39316 // The other 3 bytes of r_info are the symbol.
aviab98dcc92015-12-21 19:35:33317 uint32_t symbol = rel.r_info >> 8;
[email protected]39ed9732013-06-20 10:17:53318
huangsdda11d062016-03-14 16:35:39319 switch (type) {
[email protected]39ed9732013-06-20 10:17:53320 case R_ARM_RELATIVE:
321 if (symbol != 0)
322 return false;
323
huangsdda11d062016-03-14 16:35:39324 // This is a basic ABS32 relocation address.
[email protected]39ed9732013-06-20 10:17:53325 *result = rel.r_offset;
326 return true;
327
328 default:
329 return false;
330 }
[email protected]39ed9732013-06-20 10:17:53331}
332
333CheckBool DisassemblerElf32ARM::ParseRelocationSection(
huangsdda11d062016-03-14 16:35:39334 const Elf32_Shdr* section_header,
huangs7b221a52016-11-09 22:28:23335 InstructionReceptor* receptor) const {
huangsdda11d062016-03-14 16:35:39336 // This method compresses a contiguous stretch of R_ARM_RELATIVE entries in
337 // the relocation table with a Courgette relocation table instruction.
338 // It skips any entries at the beginning that appear in a section that
339 // Courgette doesn't support, e.g. INIT.
340 //
[email protected]a8e80412013-07-18 22:07:53341 // Specifically, the entries should be
342 // (1) In the same relocation table
343 // (2) Are consecutive
344 // (3) Are sorted in memory address order
[email protected]39ed9732013-06-20 10:17:53345 //
huangsdda11d062016-03-14 16:35:39346 // Happily, this is normally the case, but it's not required by spec so we
347 // check, and just don't do it if we don't match up.
[email protected]a8e80412013-07-18 22:07:53348 //
huangsdda11d062016-03-14 16:35:39349 // The expectation is that one relocation section will contain all of our
350 // R_ARM_RELATIVE entries in the expected order followed by assorted other
351 // entries we can't use special handling for.
[email protected]39ed9732013-06-20 10:17:53352
353 bool match = true;
354
huangsdda11d062016-03-14 16:35:39355 // Walk all the bytes in the section, matching relocation table or not.
356 FileOffset file_offset = section_header->sh_offset;
357 FileOffset section_end = section_header->sh_offset + section_header->sh_size;
[email protected]39ed9732013-06-20 10:17:53358
huangsdda11d062016-03-14 16:35:39359 const Elf32_Rel* section_relocs_iter = reinterpret_cast<const Elf32_Rel*>(
360 FileOffsetToPointer(section_header->sh_offset));
[email protected]39ed9732013-06-20 10:17:53361
aviab98dcc92015-12-21 19:35:33362 uint32_t section_relocs_count =
363 section_header->sh_size / section_header->sh_entsize;
[email protected]39ed9732013-06-20 10:17:53364
365 if (abs32_locations_.size() > section_relocs_count)
366 match = false;
367
[email protected]a8e80412013-07-18 22:07:53368 if (!abs32_locations_.empty()) {
huangs257f9fb02017-03-23 23:17:50369 std::vector<RVA>::const_iterator reloc_iter = abs32_locations_.begin();
[email protected]39ed9732013-06-20 10:17:53370
huangsdda11d062016-03-14 16:35:39371 for (uint32_t i = 0; i < section_relocs_count; ++i) {
[email protected]a8e80412013-07-18 22:07:53372 if (section_relocs_iter->r_offset == *reloc_iter)
373 break;
[email protected]39ed9732013-06-20 10:17:53374
huangs7b221a52016-11-09 22:28:23375 if (!ParseSimpleRegion(file_offset, file_offset + sizeof(Elf32_Rel),
376 receptor)) {
[email protected]a8e80412013-07-18 22:07:53377 return false;
huangsdda11d062016-03-14 16:35:39378 }
[email protected]a8e80412013-07-18 22:07:53379
380 file_offset += sizeof(Elf32_Rel);
381 ++section_relocs_iter;
382 }
383
384 while (match && (reloc_iter != abs32_locations_.end())) {
385 if (section_relocs_iter->r_info != R_ARM_RELATIVE ||
huangsdda11d062016-03-14 16:35:39386 section_relocs_iter->r_offset != *reloc_iter) {
[email protected]a8e80412013-07-18 22:07:53387 match = false;
huangsdda11d062016-03-14 16:35:39388 }
[email protected]a8e80412013-07-18 22:07:53389
huangsdda11d062016-03-14 16:35:39390 ++section_relocs_iter;
391 ++reloc_iter;
[email protected]a8e80412013-07-18 22:07:53392 file_offset += sizeof(Elf32_Rel);
393 }
394
395 if (match) {
396 // Skip over relocation tables
huangs7b221a52016-11-09 22:28:23397 if (!receptor->EmitElfARMRelocation())
[email protected]a8e80412013-07-18 22:07:53398 return false;
399 }
[email protected]39ed9732013-06-20 10:17:53400 }
401
huangs7b221a52016-11-09 22:28:23402 return ParseSimpleRegion(file_offset, section_end, receptor);
[email protected]39ed9732013-06-20 10:17:53403}
404
huangsdda11d062016-03-14 16:35:39405// TODO(huangs): Detect and avoid overlap with abs32 addresses.
[email protected]39ed9732013-06-20 10:17:53406CheckBool DisassemblerElf32ARM::ParseRel32RelocsFromSection(
407 const Elf32_Shdr* section_header) {
huangsdda11d062016-03-14 16:35:39408 FileOffset start_file_offset = section_header->sh_offset;
409 FileOffset end_file_offset = start_file_offset + section_header->sh_size;
[email protected]2b637b62013-08-01 00:11:24410
huangsdda11d062016-03-14 16:35:39411 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
412 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
[email protected]2b637b62013-08-01 00:11:24413
414 // Quick way to convert from Pointer to RVA within a single Section is to
huangsdda11d062016-03-14 16:35:39415 // subtract |pointer_to_rva|.
aviab98dcc92015-12-21 19:35:33416 const uint8_t* const adjust_pointer_to_rva =
417 start_pointer - section_header->sh_addr;
[email protected]2b637b62013-08-01 00:11:24418
419 // Find the rel32 relocations.
aviab98dcc92015-12-21 19:35:33420 const uint8_t* p = start_pointer;
huangsdda11d062016-03-14 16:35:39421 bool on_32bit = 1; // 32-bit ARM ops appear on 32-bit boundaries, so track it
[email protected]2b637b62013-08-01 00:11:24422 while (p < end_pointer) {
423 // Heuristic discovery of rel32 locations in instruction stream: are the
424 // next few bytes the start of an instruction containing a rel32
425 // addressing mode?
mostynb1007a4a2016-04-11 23:18:06426 std::unique_ptr<TypedRVAARM> rel32_rva;
[email protected]093688992014-04-03 11:35:46427 RVA target_rva = 0;
[email protected]2b637b62013-08-01 00:11:24428 bool found = false;
429
430 // 16-bit thumb ops
huangsdda11d062016-03-14 16:35:39431 if (!found && p + 3 <= end_pointer) {
aviab98dcc92015-12-21 19:35:33432 uint16_t pval = Read16LittleEndian(p);
[email protected]2b637b62013-08-01 00:11:24433 if ((pval & 0xF000) == 0xD000) {
434 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
435
huangsdda11d062016-03-14 16:35:39436 rel32_rva.reset(new TypedRVAARM(ARM_OFF8, rva));
437 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24438 return false;
huangsdda11d062016-03-14 16:35:39439
[email protected]2b637b62013-08-01 00:11:24440 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
441 found = true;
442 } else if ((pval & 0xF800) == 0xE000) {
443 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
444
huangsdda11d062016-03-14 16:35:39445 rel32_rva.reset(new TypedRVAARM(ARM_OFF11, rva));
446 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24447 return false;
huangsdda11d062016-03-14 16:35:39448
[email protected]2b637b62013-08-01 00:11:24449 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
450 found = true;
451 }
452 }
453
huangsdda11d062016-03-14 16:35:39454 // thumb-2 ops comprised of two 16-bit words.
455 if (!found && p + 5 <= end_pointer) {
[email protected]2b637b62013-08-01 00:11:24456 // This is really two 16-bit words, not one 32-bit word.
aviab98dcc92015-12-21 19:35:33457 uint32_t pval = (Read16LittleEndian(p) << 16) | Read16LittleEndian(p + 2);
[email protected]2b637b62013-08-01 00:11:24458 if ((pval & 0xF8008000) == 0xF0008000) {
459 // Covers thumb-2's 32-bit conditional/unconditional branches
huangsdda11d062016-03-14 16:35:39460 if ((pval & (1 << 14)) || (pval & (1 << 12))) {
[email protected]2b637b62013-08-01 00:11:24461 // A branch, with link, or with link and exchange.
462 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
463
huangsdda11d062016-03-14 16:35:39464 rel32_rva.reset(new TypedRVAARM(ARM_OFF25, rva));
465 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24466 return false;
huangsdda11d062016-03-14 16:35:39467
[email protected]2b637b62013-08-01 00:11:24468 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
469 found = true;
huangsdda11d062016-03-14 16:35:39470
[email protected]2b637b62013-08-01 00:11:24471 } else {
472 // TODO(paulgazz) make sure cond is not 111
473 // A conditional branch instruction
474 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
475
huangsdda11d062016-03-14 16:35:39476 rel32_rva.reset(new TypedRVAARM(ARM_OFF21, rva));
477 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24478 return false;
huangsdda11d062016-03-14 16:35:39479
[email protected]2b637b62013-08-01 00:11:24480 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
481 found = true;
482 }
483 }
484 }
485
huangsdda11d062016-03-14 16:35:39486 // 32-bit ARM ops.
[email protected]2b637b62013-08-01 00:11:24487 if (!found && on_32bit && (p + 5) <= end_pointer) {
aviab98dcc92015-12-21 19:35:33488 uint32_t pval = Read32LittleEndian(p);
[email protected]2b637b62013-08-01 00:11:24489 if ((pval & 0x0E000000) == 0x0A000000) {
490 // Covers both 0x0A 0x0B ARM relative branches
491 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
492
huangsdda11d062016-03-14 16:35:39493 rel32_rva.reset(new TypedRVAARM(ARM_OFF24, rva));
494 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24495 return false;
huangsdda11d062016-03-14 16:35:39496
[email protected]2b637b62013-08-01 00:11:24497 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
498 found = true;
499 }
500 }
501
huangsdda11d062016-03-14 16:35:39502 if (found && IsValidTargetRVA(target_rva)) {
503 uint16_t op_size = rel32_rva->op_size();
etiennep7d4e8ee2016-05-11 20:13:36504 rel32_locations_.push_back(std::move(rel32_rva));
[email protected]2b637b62013-08-01 00:11:24505#if COURGETTE_HISTOGRAM_TARGETS
506 ++rel32_target_rvas_[target_rva];
507#endif
huangsdda11d062016-03-14 16:35:39508 p += op_size;
[email protected]2b637b62013-08-01 00:11:24509
huangsdda11d062016-03-14 16:35:39510 // A tricky way to update the on_32bit flag. Here is the truth table:
[email protected]2b637b62013-08-01 00:11:24511 // on_32bit | on_32bit size is 4
512 // ---------+---------------------
513 // 1 | 0 0
514 // 0 | 0 1
515 // 0 | 1 0
516 // 1 | 1 1
huangsdda11d062016-03-14 16:35:39517 on_32bit = (~(on_32bit ^ (op_size == 4))) != 0;
[email protected]2b637b62013-08-01 00:11:24518 } else {
519 // Move 2 bytes at a time, but track 32-bit boundaries
520 p += 2;
521 on_32bit = ((on_32bit + 1) % 2) != 0;
522 }
523 }
524
[email protected]39ed9732013-06-20 10:17:53525 return true;
526}
527
528} // namespace courgette