blob: 84aa971d1b264c92132c9255481e5fe7253e7a6e [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.h"
6
aviab98dcc92015-12-21 19:35:337#include <stddef.h>
8#include <stdint.h>
9
[email protected]39ed9732013-06-20 10:17:5310#include <algorithm>
11#include <string>
12#include <vector>
13
[email protected]39ed9732013-06-20 10:17:5314#include "base/logging.h"
[email protected]144c8e92013-07-23 21:18:1915#include "base/memory/scoped_vector.h"
[email protected]39ed9732013-06-20 10:17:5316
17#include "courgette/assembly_program.h"
18#include "courgette/courgette.h"
19#include "courgette/encoded_program.h"
20
21namespace courgette {
22
23DisassemblerElf32::DisassemblerElf32(const void* start, size_t length)
24 : Disassembler(start, length),
25 header_(NULL),
26 section_header_table_(NULL),
27 section_header_table_size_(0),
28 program_header_table_(NULL),
29 program_header_table_size_(0),
30 default_string_section_(NULL) {
31}
32
33bool DisassemblerElf32::ParseHeader() {
34 if (length() < sizeof(Elf32_Ehdr))
35 return Bad("Too small");
36
37 header_ = (Elf32_Ehdr *)start();
38
39 // Have magic for elf header?
40 if (header_->e_ident[0] != 0x7f ||
41 header_->e_ident[1] != 'E' ||
42 header_->e_ident[2] != 'L' ||
43 header_->e_ident[3] != 'F')
44 return Bad("No Magic Number");
45
46 if (header_->e_type != ET_EXEC &&
47 header_->e_type != ET_DYN)
48 return Bad("Not an executable file or shared library");
49
50 if (header_->e_machine != ElfEM())
51 return Bad("Not a supported architecture");
52
53 if (header_->e_version != 1)
54 return Bad("Unknown file version");
55
56 if (header_->e_shentsize != sizeof(Elf32_Shdr))
57 return Bad("Unexpected section header size");
58
halyavinc9de6f72015-03-24 15:40:1259 if (!IsArrayInBounds(header_->e_shoff, header_->e_shnum, sizeof(Elf32_Shdr)))
60 return Bad("Out of bounds section header table");
[email protected]39ed9732013-06-20 10:17:5361
62 section_header_table_ = (Elf32_Shdr *)OffsetToPointer(header_->e_shoff);
63 section_header_table_size_ = header_->e_shnum;
64
halyavinc9de6f72015-03-24 15:40:1265 if (!IsArrayInBounds(header_->e_phoff, header_->e_phnum, sizeof(Elf32_Phdr)))
66 return Bad("Out of bounds program header table");
[email protected]39ed9732013-06-20 10:17:5367
68 program_header_table_ = (Elf32_Phdr *)OffsetToPointer(header_->e_phoff);
69 program_header_table_size_ = header_->e_phnum;
70
halyavinc9de6f72015-03-24 15:40:1271 if (header_->e_shstrndx >= header_->e_shnum)
72 return Bad("Out of bounds string section index");
[email protected]39ed9732013-06-20 10:17:5373
74 default_string_section_ = (const char *)SectionBody((int)header_->e_shstrndx);
75
halyavinc9de6f72015-03-24 15:40:1276 if (!UpdateLength()) {
77 return Bad("Out of bounds section or segment");
78 }
[email protected]39ed9732013-06-20 10:17:5379
80 return Good();
81}
82
83bool DisassemblerElf32::Disassemble(AssemblyProgram* target) {
84 if (!ok())
85 return false;
86
87 // The Image Base is always 0 for ELF Executables
88 target->set_image_base(0);
89
90 if (!ParseAbs32Relocs())
91 return false;
92
93 if (!ParseRel32RelocsFromSections())
94 return false;
95
96 if (!ParseFile(target))
97 return false;
98
99 target->DefaultAssignIndexes();
100
101 return true;
102}
103
halyavinc9de6f72015-03-24 15:40:12104bool DisassemblerElf32::UpdateLength() {
105 Elf32_Off result = 0;
[email protected]39ed9732013-06-20 10:17:53106
107 // Find the end of the last section
108 for (int section_id = 0; section_id < SectionHeaderCount(); section_id++) {
109 const Elf32_Shdr *section_header = SectionHeader(section_id);
110
111 if (section_header->sh_type == SHT_NOBITS)
112 continue;
113
halyavinc9de6f72015-03-24 15:40:12114 if (!IsArrayInBounds(section_header->sh_offset, section_header->sh_size, 1))
115 return false;
[email protected]39ed9732013-06-20 10:17:53116
halyavinc9de6f72015-03-24 15:40:12117 Elf32_Off section_end = section_header->sh_offset + section_header->sh_size;
118 result = std::max(result, section_end);
[email protected]39ed9732013-06-20 10:17:53119 }
120
121 // Find the end of the last segment
122 for (int i = 0; i < ProgramSegmentHeaderCount(); i++) {
123 const Elf32_Phdr *segment_header = ProgramSegmentHeader(i);
124
halyavinc9de6f72015-03-24 15:40:12125 if (!IsArrayInBounds(segment_header->p_offset, segment_header->p_filesz, 1))
126 return false;
[email protected]39ed9732013-06-20 10:17:53127
halyavinc9de6f72015-03-24 15:40:12128 Elf32_Off segment_end = segment_header->p_offset + segment_header->p_filesz;
129 result = std::max(result, segment_end);
[email protected]39ed9732013-06-20 10:17:53130 }
131
halyavinc9de6f72015-03-24 15:40:12132 Elf32_Off section_table_end = header_->e_shoff +
133 (header_->e_shnum * sizeof(Elf32_Shdr));
134 result = std::max(result, section_table_end);
[email protected]39ed9732013-06-20 10:17:53135
halyavinc9de6f72015-03-24 15:40:12136 Elf32_Off segment_table_end = header_->e_phoff +
137 (header_->e_phnum * sizeof(Elf32_Phdr));
138 result = std::max(result, segment_table_end);
[email protected]39ed9732013-06-20 10:17:53139
halyavinc9de6f72015-03-24 15:40:12140 ReduceLength(result);
141 return true;
[email protected]39ed9732013-06-20 10:17:53142}
143
144CheckBool DisassemblerElf32::IsValidRVA(RVA rva) const {
huangsbb4b8a92016-01-19 22:09:03145 if (rva == kUnassignedRVA)
146 return false;
[email protected]39ed9732013-06-20 10:17:53147
148 // It's valid if it's contained in any program segment
149 for (int i = 0; i < ProgramSegmentHeaderCount(); i++) {
150 const Elf32_Phdr *segment_header = ProgramSegmentHeader(i);
151
152 if (segment_header->p_type != PT_LOAD)
153 continue;
154
155 Elf32_Addr begin = segment_header->p_vaddr;
156 Elf32_Addr end = segment_header->p_vaddr + segment_header->p_memsz;
157
158 if (rva >= begin && rva < end)
159 return true;
160 }
161
162 return false;
163}
164
huangs7ec152c2016-02-04 22:26:43165CheckBool DisassemblerElf32::RVAToFileOffset(RVA rva,
166 size_t* result) const {
167 for (int i = 0; i < SectionHeaderCount(); i++) {
168 const Elf32_Shdr *section_header = SectionHeader(i);
169 // These can appear to have a size in the file, but don't.
170 if (section_header->sh_type == SHT_NOBITS)
171 continue;
172 Elf32_Addr begin = section_header->sh_addr;
173 Elf32_Addr end = begin + section_header->sh_size;
[email protected]39ed9732013-06-20 10:17:53174
huangs7ec152c2016-02-04 22:26:43175 if (rva >= begin && rva < end) {
176 *result = section_header->sh_offset + (rva - begin);
177 return true;
[email protected]39ed9732013-06-20 10:17:53178 }
179 }
[email protected]39ed9732013-06-20 10:17:53180 return false;
181}
182
183RVA DisassemblerElf32::FileOffsetToRVA(size_t offset) const {
184 // File offsets can be 64 bit values, but we are dealing with 32
185 // bit executables and so only need to support 32bit file sizes.
aviab98dcc92015-12-21 19:35:33186 uint32_t offset32 = (uint32_t)offset;
[email protected]39ed9732013-06-20 10:17:53187
188 for (int i = 0; i < SectionHeaderCount(); i++) {
189
190 const Elf32_Shdr *section_header = SectionHeader(i);
191
192 // These can appear to have a size in the file, but don't.
193 if (section_header->sh_type == SHT_NOBITS)
194 continue;
195
196 Elf32_Off section_begin = section_header->sh_offset;
197 Elf32_Off section_end = section_begin + section_header->sh_size;
198
199 if (offset32 >= section_begin && offset32 < section_end) {
200 return section_header->sh_addr + (offset32 - section_begin);
201 }
202 }
203
204 return 0;
205}
206
207CheckBool DisassemblerElf32::RVAsToOffsets(std::vector<RVA>* rvas,
[email protected]144c8e92013-07-23 21:18:19208 std::vector<size_t>* offsets) {
[email protected]39ed9732013-06-20 10:17:53209 offsets->clear();
210
211 for (std::vector<RVA>::iterator rva = rvas->begin();
212 rva != rvas->end();
213 rva++) {
214
215 size_t offset;
216
217 if (!RVAToFileOffset(*rva, &offset))
218 return false;
219
220 offsets->push_back(offset);
221 }
222
223 return true;
224}
225
[email protected]144c8e92013-07-23 21:18:19226CheckBool DisassemblerElf32::RVAsToOffsets(ScopedVector<TypedRVA>* rvas) {
227 for (ScopedVector<TypedRVA>::iterator rva = rvas->begin();
228 rva != rvas->end();
229 rva++) {
230
231 size_t offset;
232
233 if (!RVAToFileOffset((*rva)->rva(), &offset))
234 return false;
235
236 (*rva)->set_offset(offset);
237 }
238
239 return true;
240}
241
[email protected]39ed9732013-06-20 10:17:53242CheckBool DisassemblerElf32::ParseFile(AssemblyProgram* program) {
243 // Walk all the bytes in the file, whether or not in a section.
aviab98dcc92015-12-21 19:35:33244 uint32_t file_offset = 0;
[email protected]39ed9732013-06-20 10:17:53245
246 std::vector<size_t> abs_offsets;
[email protected]39ed9732013-06-20 10:17:53247
248 if (!RVAsToOffsets(&abs32_locations_, &abs_offsets))
249 return false;
250
[email protected]144c8e92013-07-23 21:18:19251 if (!RVAsToOffsets(&rel32_locations_))
[email protected]39ed9732013-06-20 10:17:53252 return false;
253
254 std::vector<size_t>::iterator current_abs_offset = abs_offsets.begin();
[email protected]144c8e92013-07-23 21:18:19255 ScopedVector<TypedRVA>::iterator current_rel = rel32_locations_.begin();
[email protected]39ed9732013-06-20 10:17:53256
257 std::vector<size_t>::iterator end_abs_offset = abs_offsets.end();
[email protected]144c8e92013-07-23 21:18:19258 ScopedVector<TypedRVA>::iterator end_rel = rel32_locations_.end();
[email protected]39ed9732013-06-20 10:17:53259
260 for (int section_id = 0;
261 section_id < SectionHeaderCount();
262 section_id++) {
263
264 const Elf32_Shdr *section_header = SectionHeader(section_id);
265
Will Harris3e6fa972015-03-02 21:14:25266 if (section_header->sh_type == SHT_NOBITS)
267 continue;
268
[email protected]39ed9732013-06-20 10:17:53269 if (!ParseSimpleRegion(file_offset,
270 section_header->sh_offset,
271 program))
272 return false;
273 file_offset = section_header->sh_offset;
274
275 switch (section_header->sh_type) {
276 case SHT_REL:
277 if (!ParseRelocationSection(section_header, program))
278 return false;
279 file_offset = section_header->sh_offset + section_header->sh_size;
280 break;
281 case SHT_PROGBITS:
282 if (!ParseProgbitsSection(section_header,
283 &current_abs_offset, end_abs_offset,
[email protected]144c8e92013-07-23 21:18:19284 &current_rel, end_rel,
[email protected]39ed9732013-06-20 10:17:53285 program))
286 return false;
287 file_offset = section_header->sh_offset + section_header->sh_size;
288 break;
[email protected]39ed9732013-06-20 10:17:53289 case SHT_INIT_ARRAY:
290 // Fall through
291 case SHT_FINI_ARRAY:
292 while (current_abs_offset != end_abs_offset &&
293 *current_abs_offset >= section_header->sh_offset &&
294 *current_abs_offset <
295 (section_header->sh_offset + section_header->sh_size)) {
296 // Skip any abs_offsets appear in the unsupported INIT_ARRAY section
297 VLOG(1) << "Skipping relocation entry for unsupported section: " <<
298 section_header->sh_type;
299 current_abs_offset++;
300 }
301 break;
302 default:
303 if (current_abs_offset != end_abs_offset &&
304 *current_abs_offset >= section_header->sh_offset &&
305 *current_abs_offset <
306 (section_header->sh_offset + section_header->sh_size))
307 VLOG(1) << "Relocation address in unrecognized ELF section: " << \
308 section_header->sh_type;
309 break;
310 }
311 }
312
313 // Rest of the file past the last section
314 if (!ParseSimpleRegion(file_offset,
315 length(),
316 program))
317 return false;
318
319 // Make certain we consume all of the relocations as expected
320 return (current_abs_offset == end_abs_offset);
321}
322
323CheckBool DisassemblerElf32::ParseProgbitsSection(
324 const Elf32_Shdr *section_header,
325 std::vector<size_t>::iterator* current_abs_offset,
326 std::vector<size_t>::iterator end_abs_offset,
[email protected]144c8e92013-07-23 21:18:19327 ScopedVector<TypedRVA>::iterator* current_rel,
328 ScopedVector<TypedRVA>::iterator end_rel,
[email protected]39ed9732013-06-20 10:17:53329 AssemblyProgram* program) {
330
331 // Walk all the bytes in the file, whether or not in a section.
332 size_t file_offset = section_header->sh_offset;
333 size_t section_end = section_header->sh_offset + section_header->sh_size;
334
335 Elf32_Addr origin = section_header->sh_addr;
336 size_t origin_offset = section_header->sh_offset;
337 if (!program->EmitOriginInstruction(origin))
338 return false;
339
340 while (file_offset < section_end) {
341
342 if (*current_abs_offset != end_abs_offset &&
343 file_offset > **current_abs_offset)
344 return false;
345
[email protected]144c8e92013-07-23 21:18:19346 while (*current_rel != end_rel &&
347 file_offset > (**current_rel)->get_offset()) {
348 (*current_rel)++;
[email protected]39ed9732013-06-20 10:17:53349 }
350
351 size_t next_relocation = section_end;
352
353 if (*current_abs_offset != end_abs_offset &&
354 next_relocation > **current_abs_offset)
355 next_relocation = **current_abs_offset;
356
357 // Rel offsets are heuristically derived, and might (incorrectly) overlap
358 // an Abs value, or the end of the section, so +3 to make sure there is
359 // room for the full 4 byte value.
[email protected]144c8e92013-07-23 21:18:19360 if (*current_rel != end_rel &&
361 next_relocation > ((**current_rel)->get_offset() + 3))
362 next_relocation = (**current_rel)->get_offset();
[email protected]39ed9732013-06-20 10:17:53363
364 if (next_relocation > file_offset) {
365 if (!ParseSimpleRegion(file_offset, next_relocation, program))
366 return false;
367
368 file_offset = next_relocation;
369 continue;
370 }
371
372 if (*current_abs_offset != end_abs_offset &&
373 file_offset == **current_abs_offset) {
aviab98dcc92015-12-21 19:35:33374 const uint8_t* p = OffsetToPointer(file_offset);
[email protected]39ed9732013-06-20 10:17:53375 RVA target_rva = Read32LittleEndian(p);
376
377 if (!program->EmitAbs32(program->FindOrMakeAbs32Label(target_rva)))
378 return false;
379 file_offset += sizeof(RVA);
380 (*current_abs_offset)++;
381 continue;
382 }
383
[email protected]144c8e92013-07-23 21:18:19384 if (*current_rel != end_rel &&
385 file_offset == (**current_rel)->get_offset()) {
aviab98dcc92015-12-21 19:35:33386 uint32_t relative_target = (**current_rel)->relative_target();
[email protected]39ed9732013-06-20 10:17:53387 // This cast is for 64 bit systems, and is only safe because we
388 // are working on 32 bit executables.
389 RVA target_rva = (RVA)(origin + (file_offset - origin_offset) +
[email protected]144c8e92013-07-23 21:18:19390 relative_target);
[email protected]39ed9732013-06-20 10:17:53391
[email protected]2b637b62013-08-01 00:11:24392 if (! (**current_rel)->EmitInstruction(program, target_rva))
[email protected]39ed9732013-06-20 10:17:53393 return false;
[email protected]2b637b62013-08-01 00:11:24394 file_offset += (**current_rel)->op_size();
[email protected]144c8e92013-07-23 21:18:19395 (*current_rel)++;
[email protected]39ed9732013-06-20 10:17:53396 continue;
397 }
398 }
399
400 // Rest of the section (if any)
401 return ParseSimpleRegion(file_offset, section_end, program);
402}
403
404CheckBool DisassemblerElf32::ParseSimpleRegion(
405 size_t start_file_offset,
406 size_t end_file_offset,
407 AssemblyProgram* program) {
[email protected]c092858a2013-08-13 00:46:30408 // Callers don't guarantee start < end
pkasting8e3a26a2014-10-03 18:52:29409 if (start_file_offset >= end_file_offset) return true;
[email protected]39ed9732013-06-20 10:17:53410
pkasting8e3a26a2014-10-03 18:52:29411 const size_t len = end_file_offset - start_file_offset;
[email protected]c092858a2013-08-13 00:46:30412
pkasting8e3a26a2014-10-03 18:52:29413 if (!program->EmitBytesInstruction(OffsetToPointer(start_file_offset), len))
[email protected]c092858a2013-08-13 00:46:30414 return false;
[email protected]39ed9732013-06-20 10:17:53415
416 return true;
417}
418
419CheckBool DisassemblerElf32::ParseAbs32Relocs() {
420 abs32_locations_.clear();
421
422 // Loop through sections for relocation sections
423 for (int section_id = 0; section_id < SectionHeaderCount(); section_id++) {
424 const Elf32_Shdr *section_header = SectionHeader(section_id);
425
426 if (section_header->sh_type == SHT_REL) {
427
428 Elf32_Rel *relocs_table = (Elf32_Rel *)SectionBody(section_id);
429
430 int relocs_table_count = section_header->sh_size /
431 section_header->sh_entsize;
432
433 // Elf32_Word relocation_section_id = section_header->sh_info;
434
435 // Loop through relocation objects in the relocation section
436 for (int rel_id = 0; rel_id < relocs_table_count; rel_id++) {
437 RVA rva;
438
439 // Quite a few of these conversions fail, and we simply skip
440 // them, that's okay.
[email protected]a8e80412013-07-18 22:07:53441 if (RelToRVA(relocs_table[rel_id], &rva) && CheckSection(rva))
[email protected]39ed9732013-06-20 10:17:53442 abs32_locations_.push_back(rva);
443 }
444 }
445 }
446
447 std::sort(abs32_locations_.begin(), abs32_locations_.end());
huangsbb4b8a92016-01-19 22:09:03448 DCHECK(abs32_locations_.empty() ||
449 abs32_locations_.back() != kUnassignedRVA);
[email protected]39ed9732013-06-20 10:17:53450 return true;
451}
452
[email protected]a8e80412013-07-18 22:07:53453CheckBool DisassemblerElf32::CheckSection(RVA rva) {
454 size_t offset;
455
456 if (!RVAToFileOffset(rva, &offset)) {
457 return false;
458 }
459
460 for (int section_id = 0;
461 section_id < SectionHeaderCount();
462 section_id++) {
463
464 const Elf32_Shdr *section_header = SectionHeader(section_id);
465
466 if (offset >= section_header->sh_offset &&
467 offset < (section_header->sh_offset + section_header->sh_size)) {
468 switch (section_header->sh_type) {
469 case SHT_REL:
470 // Fall-through
471 case SHT_PROGBITS:
472 return true;
473 }
474 }
475 }
476
477 return false;
478}
479
[email protected]39ed9732013-06-20 10:17:53480CheckBool DisassemblerElf32::ParseRel32RelocsFromSections() {
481
482 rel32_locations_.clear();
483
484 // Loop through sections for relocation sections
485 for (int section_id = 0;
486 section_id < SectionHeaderCount();
487 section_id++) {
488
489 const Elf32_Shdr *section_header = SectionHeader(section_id);
490
huangs7ec152c2016-02-04 22:26:43491 // Some debug sections can have sh_type=SHT_PROGBITS but sh_addr=0.
492 if (section_header->sh_type != SHT_PROGBITS ||
493 section_header->sh_addr == 0)
[email protected]39ed9732013-06-20 10:17:53494 continue;
495
496 if (!ParseRel32RelocsFromSection(section_header))
497 return false;
498 }
499
[email protected]144c8e92013-07-23 21:18:19500 std::sort(rel32_locations_.begin(),
501 rel32_locations_.end(),
502 TypedRVA::IsLessThan);
huangsbb4b8a92016-01-19 22:09:03503 DCHECK(rel32_locations_.empty() ||
504 rel32_locations_.back()->rva() != kUnassignedRVA);
[email protected]39ed9732013-06-20 10:17:53505 return true;
506}
507
508} // namespace courgette