blob: 39172f462d171cde94ee6b037db44fef6aa71c7e [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
[email protected]39ed9732013-06-20 10:17:537#include <vector>
8
[email protected]39ed9732013-06-20 10:17:539#include "base/logging.h"
huangsdda11d062016-03-14 16:35:3910#include "base/memory/scoped_ptr.h"
[email protected]39ed9732013-06-20 10:17:5311#include "courgette/assembly_program.h"
12#include "courgette/courgette.h"
[email protected]39ed9732013-06-20 10:17:5313
14namespace courgette {
15
aviab98dcc92015-12-21 19:35:3316CheckBool DisassemblerElf32ARM::Compress(ARM_RVA type,
17 uint32_t arm_op,
18 RVA rva,
19 uint16_t* c_op,
20 uint32_t* addr) {
huangsdda11d062016-03-14 16:35:3921 // Notation for bit ranges in comments:
22 // - Listing bits from highest to lowest.
23 // - A-Z or (j1), (j2), etc.: single bit in source.
24 // - a-z: multiple, consecutive bits in source.
[email protected]2b637b62013-08-01 00:11:2425 switch (type) {
26 case ARM_OFF8: {
huangsdda11d062016-03-14 16:35:3927 // Encoding T1.
28 // The offset is given by lower 8 bits of the op. It is a 9-bit offset,
29 // shifted right 1 bit, and signed extended.
30 // arm_op = aaaaaaaa Snnnnnnn
31 // *addr := SSSSSSSS SSSSSSSS SSSSSSSS nnnnnnn0 + 100
32 // *c_op := 00010000 aaaaaaaa
aviab98dcc92015-12-21 19:35:3333 uint32_t temp = (arm_op & 0x00FF) << 1;
[email protected]144c8e92013-07-23 21:18:1934 if (temp & 0x0100)
35 temp |= 0xFFFFFE00;
36 temp += 4; // Offset from _next_ PC.
[email protected]2b637b62013-08-01 00:11:2437
38 (*addr) = temp;
aviab98dcc92015-12-21 19:35:3339 (*c_op) = static_cast<uint16_t>(arm_op >> 8) | 0x1000;
[email protected]144c8e92013-07-23 21:18:1940 break;
[email protected]2b637b62013-08-01 00:11:2441 }
42 case ARM_OFF11: {
huangsdda11d062016-03-14 16:35:3943 // Encoding T2.
44 // The offset is given by lower 11 bits of the op, and is a 12-bit offset,
45 // shifted right 1 bit, and sign extended.
46 // arm_op = aaaaaSnn nnnnnnnn
47 // *addr := SSSSSSSS SSSSSSSS SSSSSnnn nnnnnnn0 + 100
48 // *c_op := 00100000 000aaaaa
aviab98dcc92015-12-21 19:35:3349 uint32_t temp = (arm_op & 0x07FF) << 1;
[email protected]144c8e92013-07-23 21:18:1950 if (temp & 0x00000800)
51 temp |= 0xFFFFF000;
52 temp += 4; // Offset from _next_ PC.
[email protected]2b637b62013-08-01 00:11:2453
54 (*addr) = temp;
aviab98dcc92015-12-21 19:35:3355 (*c_op) = static_cast<uint16_t>(arm_op >> 11) | 0x2000;
[email protected]144c8e92013-07-23 21:18:1956 break;
[email protected]2b637b62013-08-01 00:11:2457 }
58 case ARM_OFF24: {
59 // The offset is given by the lower 24-bits of the op, shifted
60 // left 2 bits, and sign extended.
huangsdda11d062016-03-14 16:35:3961 // arm_op = aaaaaaaa Snnnnnnn nnnnnnnn nnnnnnnn
62 // *addr := SSSSSSSn nnnnnnnn nnnnnnnn nnnnnn00 + 1000
63 // *c_op := 00110000 aaaaaaaa
aviab98dcc92015-12-21 19:35:3364 uint32_t temp = (arm_op & 0x00FFFFFF) << 2;
[email protected]2b637b62013-08-01 00:11:2465 if (temp & 0x02000000)
66 temp |= 0xFC000000;
67 temp += 8;
68
69 (*addr) = temp;
70 (*c_op) = (arm_op >> 24) | 0x3000;
71 break;
72 }
73 case ARM_OFF25: {
huangsdda11d062016-03-14 16:35:3974 // Encoding T4.
75 // arm_op = aaaaaSmm mmmmmmmm BC(j1)D(j2)nnn nnnnnnnn
76 // where CD is in {01, 10, 11}
77 // i1 := ~(j1 ^ S)
78 // i2 := ~(j2 ^ S)
79 // If CD == 10:
80 // pppp := (rva % 4 == 0) ? 0100 : 0010
81 // Else:
82 // pppp := 0100
83 // *addr := SSSSSSSS (i1)(i2)mmmmmm mmmmnnnn nnnnnnn0 + pppp
84 // *c_op := 0100pppp aaaaaBCD
85 // TODO(huangs): aaaaa = 11110 and B = 1 always? Investigate and fix.
aviab98dcc92015-12-21 19:35:3386 uint32_t temp = 0;
[email protected]2b637b62013-08-01 00:11:2487 temp |= (arm_op & 0x000007FF) << 1; // imm11
88 temp |= (arm_op & 0x03FF0000) >> 4; // imm10
89
aviab98dcc92015-12-21 19:35:3390 uint32_t S = (arm_op & (1 << 26)) >> 26;
91 uint32_t j2 = (arm_op & (1 << 11)) >> 11;
92 uint32_t j1 = (arm_op & (1 << 13)) >> 13;
huangsdda11d062016-03-14 16:35:3993 bool bit12 = ((arm_op & (1 << 12)) >> 12) != 0; // D
94 bool bit14 = ((arm_op & (1 << 14)) >> 14) != 0; // C
[email protected]2b637b62013-08-01 00:11:2495
aviab98dcc92015-12-21 19:35:3396 uint32_t i2 = ~(j2 ^ S) & 1;
97 uint32_t i1 = ~(j1 ^ S) & 1;
[email protected]2b637b62013-08-01 00:11:2498 bool toARM = bit14 && !bit12;
99
100 temp |= (S << 24) | (i1 << 23) | (i2 << 22);
101
102 if (temp & 0x01000000) // sign extension
103 temp |= 0xFE000000;
aviab98dcc92015-12-21 19:35:33104 uint32_t prefetch;
[email protected]2b637b62013-08-01 00:11:24105 if (toARM) {
huangsdda11d062016-03-14 16:35:39106 // Align PC on 4-byte boundary.
aviab98dcc92015-12-21 19:35:33107 uint32_t align4byte = (rva % 4) ? 2 : 4;
[email protected]2b637b62013-08-01 00:11:24108 prefetch = align4byte;
109 } else {
110 prefetch = 4;
111 }
112 temp += prefetch;
113 (*addr) = temp;
114
aviab98dcc92015-12-21 19:35:33115 uint32_t temp2 = 0x4000;
huangsdda11d062016-03-14 16:35:39116 temp2 |= (arm_op & (1 << 12)) >> 12; // .......D
117 temp2 |= (arm_op & (1 << 14)) >> 13; // ......C.
118 temp2 |= (arm_op & (1 << 15)) >> 13; // .....B..
119 temp2 |= (arm_op & 0xF8000000) >> 24; // aaaaa...
[email protected]2b637b62013-08-01 00:11:24120 temp2 |= (prefetch & 0x0000000F) << 8;
aviab98dcc92015-12-21 19:35:33121 (*c_op) = static_cast<uint16_t>(temp2);
[email protected]2b637b62013-08-01 00:11:24122 break;
123 }
124 case ARM_OFF21: {
huangsdda11d062016-03-14 16:35:39125 // Encoding T3.
126 // arm_op = 11110Scc ccmmmmmm 10(j1)0(j2)nnn nnnnnnnn
127 // *addr := SSSSSSSS SSSS(j1)(j2)mm mmmmnnnn nnnnnnn0 + 100
128 // *c_op := 01010000 0000cccc
aviab98dcc92015-12-21 19:35:33129 uint32_t temp = 0;
[email protected]11336c02013-09-25 19:05:51130 temp |= (arm_op & 0x000007FF) << 1; // imm11
131 temp |= (arm_op & 0x003F0000) >> 4; // imm6
[email protected]2b637b62013-08-01 00:11:24132
aviab98dcc92015-12-21 19:35:33133 uint32_t S = (arm_op & (1 << 26)) >> 26;
huangsdda11d062016-03-14 16:35:39134 // TODO(huangs): Check with docs: Perhaps j1, j2 should swap?
aviab98dcc92015-12-21 19:35:33135 uint32_t j2 = (arm_op & (1 << 11)) >> 11;
136 uint32_t j1 = (arm_op & (1 << 13)) >> 13;
[email protected]2b637b62013-08-01 00:11:24137
138 temp |= (S << 20) | (j1 << 19) | (j2 << 18);
139
[email protected]11336c02013-09-25 19:05:51140 if (temp & 0x00100000) // sign extension
[email protected]2b637b62013-08-01 00:11:24141 temp |= 0xFFE00000;
142 temp += 4;
143 (*addr) = temp;
144
aviab98dcc92015-12-21 19:35:33145 uint32_t temp2 = 0x5000;
[email protected]2b637b62013-08-01 00:11:24146 temp2 |= (arm_op & 0x03C00000) >> 22; // just save the cond
aviab98dcc92015-12-21 19:35:33147 (*c_op) = static_cast<uint16_t>(temp2);
[email protected]2b637b62013-08-01 00:11:24148 break;
149 }
[email protected]144c8e92013-07-23 21:18:19150 default:
151 return false;
152 }
[email protected]144c8e92013-07-23 21:18:19153 return true;
154}
155
aviab98dcc92015-12-21 19:35:33156CheckBool DisassemblerElf32ARM::Decompress(ARM_RVA type,
157 uint16_t c_op,
158 uint32_t addr,
159 uint32_t* arm_op) {
[email protected]2b637b62013-08-01 00:11:24160 switch (type) {
161 case ARM_OFF8:
huangsdda11d062016-03-14 16:35:39162 // addr = SSSSSSSS SSSSSSSS SSSSSSSS nnnnnnn0 + 100
163 // c_op = 00010000 aaaaaaaa
164 // *arm_op := aaaaaaaa Snnnnnnn
[email protected]2b637b62013-08-01 00:11:24165 (*arm_op) = ((c_op & 0x0FFF) << 8) | (((addr - 4) >> 1) & 0x000000FF);
166 break;
167 case ARM_OFF11:
huangsdda11d062016-03-14 16:35:39168 // addr = SSSSSSSS SSSSSSSS SSSSSnnn nnnnnnn0 + 100
169 // c_op = 00100000 000aaaaa
170 // *arm_op := aaaaaSnn nnnnnnnn
[email protected]2b637b62013-08-01 00:11:24171 (*arm_op) = ((c_op & 0x0FFF) << 11) | (((addr - 4) >> 1) & 0x000007FF);
172 break;
173 case ARM_OFF24:
huangsdda11d062016-03-14 16:35:39174 // addr = SSSSSSSn nnnnnnnn nnnnnnnn nnnnnn00 + 1000
175 // c_op = 00110000 aaaaaaaa
176 // *arm_op := aaaaaaaa Snnnnnnn nnnnnnnn nnnnnnnn
[email protected]2b637b62013-08-01 00:11:24177 (*arm_op) = ((c_op & 0x0FFF) << 24) | (((addr - 8) >> 2) & 0x00FFFFFF);
178 break;
179 case ARM_OFF25: {
huangsdda11d062016-03-14 16:35:39180 // addr = SSSSSSSS (i1)(i2)mmmmmm mmmmnnnn nnnnnnn0 + pppp
181 // c_op = 0100pppp aaaaaBCD
182 // j1 := ~i1 ^ S
183 // j2 := ~i2 ^ S
184 // *arm_op := aaaaaSmm mmmmmmmm BC(j1)D(j2)nnn nnnnnnnn
aviab98dcc92015-12-21 19:35:33185 uint32_t temp = 0;
[email protected]2b637b62013-08-01 00:11:24186 temp |= (c_op & (1 << 0)) << 12;
187 temp |= (c_op & (1 << 1)) << 13;
188 temp |= (c_op & (1 << 2)) << 13;
189 temp |= (c_op & (0xF8000000 >> 24)) << 24;
190
aviab98dcc92015-12-21 19:35:33191 uint32_t prefetch = (c_op & 0x0F00) >> 8;
[email protected]2b637b62013-08-01 00:11:24192 addr -= prefetch;
193
194 addr &= 0x01FFFFFF;
195
aviab98dcc92015-12-21 19:35:33196 uint32_t S = (addr & (1 << 24)) >> 24;
197 uint32_t i1 = (addr & (1 << 23)) >> 23;
198 uint32_t i2 = (addr & (1 << 22)) >> 22;
[email protected]2b637b62013-08-01 00:11:24199
aviab98dcc92015-12-21 19:35:33200 uint32_t j1 = ((~i1) ^ S) & 1;
201 uint32_t j2 = ((~i2) ^ S) & 1;
[email protected]2b637b62013-08-01 00:11:24202
203 temp |= S << 26;
204 temp |= j2 << 11;
205 temp |= j1 << 13;
206
207 temp |= (addr & (0x000007FF << 1)) >> 1;
208 temp |= (addr & (0x03FF0000 >> 4)) << 4;
209
210 (*arm_op) = temp;
211 break;
212 }
213 case ARM_OFF21: {
huangsdda11d062016-03-14 16:35:39214 // addr = SSSSSSSS SSSS(j1)(j2)mm mmmmnnnn nnnnnnn0 + 100
215 // c_op = 01010000 0000cccc
216 // *arm_op := 11110Scc ccmmmmmm 10(j1)0(j2)nnn nnnnnnnn
aviab98dcc92015-12-21 19:35:33217 uint32_t temp = 0xF0008000;
[email protected]2b637b62013-08-01 00:11:24218 temp |= (c_op & (0x03C00000 >> 22)) << 22;
219
220 addr -= 4;
221 addr &= 0x001FFFFF;
222
aviab98dcc92015-12-21 19:35:33223 uint32_t S = (addr & (1 << 20)) >> 20;
224 uint32_t j1 = (addr & (1 << 19)) >> 19;
225 uint32_t j2 = (addr & (1 << 18)) >> 18;
[email protected]2b637b62013-08-01 00:11:24226
227 temp |= S << 26;
228 temp |= j2 << 11;
229 temp |= j1 << 13;
230
231 temp |= (addr & (0x000007FF << 1)) >> 1;
232 temp |= (addr & (0x003F0000 >> 4)) << 4;
233
234 (*arm_op) = temp;
235 break;
236 }
237 default:
238 return false;
239 }
240 return true;
241}
242
aviab98dcc92015-12-21 19:35:33243uint16_t DisassemblerElf32ARM::TypedRVAARM::op_size() const {
[email protected]2b637b62013-08-01 00:11:24244 switch (type_) {
245 case ARM_OFF8:
246 return 2;
247 case ARM_OFF11:
248 return 2;
249 case ARM_OFF24:
250 return 4;
251 case ARM_OFF25:
252 return 4;
253 case ARM_OFF21:
254 return 4;
255 default:
[email protected]0ef486b2014-07-08 08:40:56256 return 0xFFFF;
[email protected]2b637b62013-08-01 00:11:24257 }
258}
259
260CheckBool DisassemblerElf32ARM::TypedRVAARM::ComputeRelativeTarget(
aviab98dcc92015-12-21 19:35:33261 const uint8_t* op_pointer) {
[email protected]2b637b62013-08-01 00:11:24262 arm_op_ = op_pointer;
263 switch (type_) {
huangsdda11d062016-03-14 16:35:39264 case ARM_OFF8: // Falls through.
[email protected]2b637b62013-08-01 00:11:24265 case ARM_OFF11: {
266 RVA relative_target;
huangsdda11d062016-03-14 16:35:39267 CheckBool ret = Compress(type_,
268 Read16LittleEndian(op_pointer),
269 rva(),
270 &c_op_,
271 &relative_target);
[email protected]2b637b62013-08-01 00:11:24272 set_relative_target(relative_target);
273 return ret;
274 }
275 case ARM_OFF24: {
276 RVA relative_target;
huangsdda11d062016-03-14 16:35:39277 CheckBool ret = Compress(type_,
278 Read32LittleEndian(op_pointer),
279 rva(),
280 &c_op_,
281 &relative_target);
[email protected]2b637b62013-08-01 00:11:24282 set_relative_target(relative_target);
283 return ret;
284 }
huangsdda11d062016-03-14 16:35:39285 case ARM_OFF25: // Falls through.
[email protected]2b637b62013-08-01 00:11:24286 case ARM_OFF21: {
287 // A thumb-2 op is 32 bits stored as two 16-bit words
aviab98dcc92015-12-21 19:35:33288 uint32_t pval = (Read16LittleEndian(op_pointer) << 16) |
289 Read16LittleEndian(op_pointer + 2);
[email protected]2b637b62013-08-01 00:11:24290 RVA relative_target;
291 CheckBool ret = Compress(type_, pval, rva(), &c_op_, &relative_target);
292 set_relative_target(relative_target);
293 return ret;
294 }
295 default:
296 return false;
297 }
298}
299
300CheckBool DisassemblerElf32ARM::TypedRVAARM::EmitInstruction(
301 AssemblyProgram* program,
302 RVA target_rva) {
303 return program->EmitRel32ARM(c_op(),
304 program->FindOrMakeRel32Label(target_rva),
305 arm_op_,
306 op_size());
307}
308
[email protected]39ed9732013-06-20 10:17:53309DisassemblerElf32ARM::DisassemblerElf32ARM(const void* start, size_t length)
huangsdda11d062016-03-14 16:35:39310 : DisassemblerElf32(start, length) {
[email protected]39ed9732013-06-20 10:17:53311}
312
huangsdda11d062016-03-14 16:35:39313// Convert an ELF relocation struction into an RVA.
[email protected]39ed9732013-06-20 10:17:53314CheckBool DisassemblerElf32ARM::RelToRVA(Elf32_Rel rel, RVA* result) const {
huangsdda11d062016-03-14 16:35:39315 // The rightmost byte of r_info is the type.
scottmg4a95ca52016-03-12 23:54:56316 elf32_rel_arm_type_values type =
huangsdda11d062016-03-14 16:35:39317 static_cast<elf32_rel_arm_type_values>(rel.r_info & 0xFF);
scottmg4a95ca52016-03-12 23:54:56318
huangsdda11d062016-03-14 16:35:39319 // The other 3 bytes of r_info are the symbol.
aviab98dcc92015-12-21 19:35:33320 uint32_t symbol = rel.r_info >> 8;
[email protected]39ed9732013-06-20 10:17:53321
huangsdda11d062016-03-14 16:35:39322 switch (type) {
[email protected]39ed9732013-06-20 10:17:53323 case R_ARM_RELATIVE:
324 if (symbol != 0)
325 return false;
326
huangsdda11d062016-03-14 16:35:39327 // This is a basic ABS32 relocation address.
[email protected]39ed9732013-06-20 10:17:53328 *result = rel.r_offset;
329 return true;
330
331 default:
332 return false;
333 }
[email protected]39ed9732013-06-20 10:17:53334}
335
336CheckBool DisassemblerElf32ARM::ParseRelocationSection(
huangsdda11d062016-03-14 16:35:39337 const Elf32_Shdr* section_header,
338 AssemblyProgram* program) {
339 // This method compresses a contiguous stretch of R_ARM_RELATIVE entries in
340 // the relocation table with a Courgette relocation table instruction.
341 // It skips any entries at the beginning that appear in a section that
342 // Courgette doesn't support, e.g. INIT.
343 //
[email protected]a8e80412013-07-18 22:07:53344 // Specifically, the entries should be
345 // (1) In the same relocation table
346 // (2) Are consecutive
347 // (3) Are sorted in memory address order
[email protected]39ed9732013-06-20 10:17:53348 //
huangsdda11d062016-03-14 16:35:39349 // Happily, this is normally the case, but it's not required by spec so we
350 // check, and just don't do it if we don't match up.
[email protected]a8e80412013-07-18 22:07:53351 //
huangsdda11d062016-03-14 16:35:39352 // The expectation is that one relocation section will contain all of our
353 // R_ARM_RELATIVE entries in the expected order followed by assorted other
354 // entries we can't use special handling for.
[email protected]39ed9732013-06-20 10:17:53355
356 bool match = true;
357
huangsdda11d062016-03-14 16:35:39358 // Walk all the bytes in the section, matching relocation table or not.
359 FileOffset file_offset = section_header->sh_offset;
360 FileOffset section_end = section_header->sh_offset + section_header->sh_size;
[email protected]39ed9732013-06-20 10:17:53361
huangsdda11d062016-03-14 16:35:39362 const Elf32_Rel* section_relocs_iter = reinterpret_cast<const Elf32_Rel*>(
363 FileOffsetToPointer(section_header->sh_offset));
[email protected]39ed9732013-06-20 10:17:53364
aviab98dcc92015-12-21 19:35:33365 uint32_t section_relocs_count =
366 section_header->sh_size / section_header->sh_entsize;
[email protected]39ed9732013-06-20 10:17:53367
368 if (abs32_locations_.size() > section_relocs_count)
369 match = false;
370
[email protected]a8e80412013-07-18 22:07:53371 if (!abs32_locations_.empty()) {
372 std::vector<RVA>::iterator reloc_iter = abs32_locations_.begin();
[email protected]39ed9732013-06-20 10:17:53373
huangsdda11d062016-03-14 16:35:39374 for (uint32_t i = 0; i < section_relocs_count; ++i) {
[email protected]a8e80412013-07-18 22:07:53375 if (section_relocs_iter->r_offset == *reloc_iter)
376 break;
[email protected]39ed9732013-06-20 10:17:53377
huangsdda11d062016-03-14 16:35:39378 if (!ParseSimpleRegion(file_offset,
379 file_offset + sizeof(Elf32_Rel),
380 program)) {
[email protected]a8e80412013-07-18 22:07:53381 return false;
huangsdda11d062016-03-14 16:35:39382 }
[email protected]a8e80412013-07-18 22:07:53383
384 file_offset += sizeof(Elf32_Rel);
385 ++section_relocs_iter;
386 }
387
388 while (match && (reloc_iter != abs32_locations_.end())) {
389 if (section_relocs_iter->r_info != R_ARM_RELATIVE ||
huangsdda11d062016-03-14 16:35:39390 section_relocs_iter->r_offset != *reloc_iter) {
[email protected]a8e80412013-07-18 22:07:53391 match = false;
huangsdda11d062016-03-14 16:35:39392 }
[email protected]a8e80412013-07-18 22:07:53393
huangsdda11d062016-03-14 16:35:39394 ++section_relocs_iter;
395 ++reloc_iter;
[email protected]a8e80412013-07-18 22:07:53396 file_offset += sizeof(Elf32_Rel);
397 }
398
399 if (match) {
400 // Skip over relocation tables
401 if (!program->EmitElfARMRelocationInstruction())
402 return false;
403 }
[email protected]39ed9732013-06-20 10:17:53404 }
405
406 return ParseSimpleRegion(file_offset, section_end, program);
407}
408
huangsdda11d062016-03-14 16:35:39409// TODO(huangs): Detect and avoid overlap with abs32 addresses.
[email protected]39ed9732013-06-20 10:17:53410CheckBool DisassemblerElf32ARM::ParseRel32RelocsFromSection(
411 const Elf32_Shdr* section_header) {
huangsdda11d062016-03-14 16:35:39412 FileOffset start_file_offset = section_header->sh_offset;
413 FileOffset end_file_offset = start_file_offset + section_header->sh_size;
[email protected]2b637b62013-08-01 00:11:24414
huangsdda11d062016-03-14 16:35:39415 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
416 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
[email protected]2b637b62013-08-01 00:11:24417
418 // Quick way to convert from Pointer to RVA within a single Section is to
huangsdda11d062016-03-14 16:35:39419 // subtract |pointer_to_rva|.
aviab98dcc92015-12-21 19:35:33420 const uint8_t* const adjust_pointer_to_rva =
421 start_pointer - section_header->sh_addr;
[email protected]2b637b62013-08-01 00:11:24422
423 // Find the rel32 relocations.
aviab98dcc92015-12-21 19:35:33424 const uint8_t* p = start_pointer;
huangsdda11d062016-03-14 16:35:39425 bool on_32bit = 1; // 32-bit ARM ops appear on 32-bit boundaries, so track it
[email protected]2b637b62013-08-01 00:11:24426 while (p < end_pointer) {
427 // Heuristic discovery of rel32 locations in instruction stream: are the
428 // next few bytes the start of an instruction containing a rel32
429 // addressing mode?
huangsdda11d062016-03-14 16:35:39430 scoped_ptr<TypedRVAARM> rel32_rva;
[email protected]093688992014-04-03 11:35:46431 RVA target_rva = 0;
[email protected]2b637b62013-08-01 00:11:24432 bool found = false;
433
434 // 16-bit thumb ops
huangsdda11d062016-03-14 16:35:39435 if (!found && p + 3 <= end_pointer) {
aviab98dcc92015-12-21 19:35:33436 uint16_t pval = Read16LittleEndian(p);
[email protected]2b637b62013-08-01 00:11:24437 if ((pval & 0xF000) == 0xD000) {
438 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
439
huangsdda11d062016-03-14 16:35:39440 rel32_rva.reset(new TypedRVAARM(ARM_OFF8, rva));
441 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24442 return false;
huangsdda11d062016-03-14 16:35:39443
[email protected]2b637b62013-08-01 00:11:24444 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
445 found = true;
446 } else if ((pval & 0xF800) == 0xE000) {
447 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
448
huangsdda11d062016-03-14 16:35:39449 rel32_rva.reset(new TypedRVAARM(ARM_OFF11, rva));
450 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24451 return false;
huangsdda11d062016-03-14 16:35:39452
[email protected]2b637b62013-08-01 00:11:24453 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
454 found = true;
455 }
456 }
457
huangsdda11d062016-03-14 16:35:39458 // thumb-2 ops comprised of two 16-bit words.
459 if (!found && p + 5 <= end_pointer) {
[email protected]2b637b62013-08-01 00:11:24460 // This is really two 16-bit words, not one 32-bit word.
aviab98dcc92015-12-21 19:35:33461 uint32_t pval = (Read16LittleEndian(p) << 16) | Read16LittleEndian(p + 2);
[email protected]2b637b62013-08-01 00:11:24462 if ((pval & 0xF8008000) == 0xF0008000) {
463 // Covers thumb-2's 32-bit conditional/unconditional branches
huangsdda11d062016-03-14 16:35:39464 if ((pval & (1 << 14)) || (pval & (1 << 12))) {
[email protected]2b637b62013-08-01 00:11:24465 // A branch, with link, or with link and exchange.
466 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
467
huangsdda11d062016-03-14 16:35:39468 rel32_rva.reset(new TypedRVAARM(ARM_OFF25, rva));
469 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24470 return false;
huangsdda11d062016-03-14 16:35:39471
[email protected]2b637b62013-08-01 00:11:24472 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
473 found = true;
huangsdda11d062016-03-14 16:35:39474
[email protected]2b637b62013-08-01 00:11:24475 } else {
476 // TODO(paulgazz) make sure cond is not 111
477 // A conditional branch instruction
478 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
479
huangsdda11d062016-03-14 16:35:39480 rel32_rva.reset(new TypedRVAARM(ARM_OFF21, rva));
481 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24482 return false;
huangsdda11d062016-03-14 16:35:39483
[email protected]2b637b62013-08-01 00:11:24484 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
485 found = true;
486 }
487 }
488 }
489
huangsdda11d062016-03-14 16:35:39490 // 32-bit ARM ops.
[email protected]2b637b62013-08-01 00:11:24491 if (!found && on_32bit && (p + 5) <= end_pointer) {
aviab98dcc92015-12-21 19:35:33492 uint32_t pval = Read32LittleEndian(p);
[email protected]2b637b62013-08-01 00:11:24493 if ((pval & 0x0E000000) == 0x0A000000) {
494 // Covers both 0x0A 0x0B ARM relative branches
495 RVA rva = static_cast<RVA>(p - adjust_pointer_to_rva);
496
huangsdda11d062016-03-14 16:35:39497 rel32_rva.reset(new TypedRVAARM(ARM_OFF24, rva));
498 if (!rel32_rva->ComputeRelativeTarget(p))
[email protected]2b637b62013-08-01 00:11:24499 return false;
huangsdda11d062016-03-14 16:35:39500
[email protected]2b637b62013-08-01 00:11:24501 target_rva = rel32_rva->rva() + rel32_rva->relative_target();
502 found = true;
503 }
504 }
505
huangsdda11d062016-03-14 16:35:39506 if (found && IsValidTargetRVA(target_rva)) {
507 uint16_t op_size = rel32_rva->op_size();
508 rel32_locations_.push_back(rel32_rva.release());
[email protected]2b637b62013-08-01 00:11:24509#if COURGETTE_HISTOGRAM_TARGETS
510 ++rel32_target_rvas_[target_rva];
511#endif
huangsdda11d062016-03-14 16:35:39512 p += op_size;
[email protected]2b637b62013-08-01 00:11:24513
huangsdda11d062016-03-14 16:35:39514 // A tricky way to update the on_32bit flag. Here is the truth table:
[email protected]2b637b62013-08-01 00:11:24515 // on_32bit | on_32bit size is 4
516 // ---------+---------------------
517 // 1 | 0 0
518 // 0 | 0 1
519 // 0 | 1 0
520 // 1 | 1 1
huangsdda11d062016-03-14 16:35:39521 on_32bit = (~(on_32bit ^ (op_size == 4))) != 0;
[email protected]2b637b62013-08-01 00:11:24522 } else {
523 // Move 2 bytes at a time, but track 32-bit boundaries
524 p += 2;
525 on_32bit = ((on_32bit + 1) % 2) != 0;
526 }
527 }
528
[email protected]39ed9732013-06-20 10:17:53529 return true;
530}
531
532} // namespace courgette