blob: c0f42e61052244b544cf882f4dcddb9ce57a584b [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
165// Returns RVA for an in memory address, or NULL.
166CheckBool DisassemblerElf32::RVAToFileOffset(Elf32_Addr addr,
167 size_t* result) const {
168
169 for (int i = 0; i < ProgramSegmentHeaderCount(); i++) {
170 Elf32_Addr begin = ProgramSegmentMemoryBegin(i);
171 Elf32_Addr end = begin + ProgramSegmentMemorySize(i);
172
173 if (addr >= begin && addr < end) {
174 Elf32_Addr offset = addr - begin;
175
176 if (offset < ProgramSegmentFileSize(i)) {
177 *result = ProgramSegmentFileOffset(i) + offset;
178 return true;
179 }
180 }
181 }
182
183 return false;
184}
185
186RVA DisassemblerElf32::FileOffsetToRVA(size_t offset) const {
187 // File offsets can be 64 bit values, but we are dealing with 32
188 // bit executables and so only need to support 32bit file sizes.
aviab98dcc92015-12-21 19:35:33189 uint32_t offset32 = (uint32_t)offset;
[email protected]39ed9732013-06-20 10:17:53190
191 for (int i = 0; i < SectionHeaderCount(); i++) {
192
193 const Elf32_Shdr *section_header = SectionHeader(i);
194
195 // These can appear to have a size in the file, but don't.
196 if (section_header->sh_type == SHT_NOBITS)
197 continue;
198
199 Elf32_Off section_begin = section_header->sh_offset;
200 Elf32_Off section_end = section_begin + section_header->sh_size;
201
202 if (offset32 >= section_begin && offset32 < section_end) {
203 return section_header->sh_addr + (offset32 - section_begin);
204 }
205 }
206
207 return 0;
208}
209
210CheckBool DisassemblerElf32::RVAsToOffsets(std::vector<RVA>* rvas,
[email protected]144c8e92013-07-23 21:18:19211 std::vector<size_t>* offsets) {
[email protected]39ed9732013-06-20 10:17:53212 offsets->clear();
213
214 for (std::vector<RVA>::iterator rva = rvas->begin();
215 rva != rvas->end();
216 rva++) {
217
218 size_t offset;
219
220 if (!RVAToFileOffset(*rva, &offset))
221 return false;
222
223 offsets->push_back(offset);
224 }
225
226 return true;
227}
228
[email protected]144c8e92013-07-23 21:18:19229CheckBool DisassemblerElf32::RVAsToOffsets(ScopedVector<TypedRVA>* rvas) {
230 for (ScopedVector<TypedRVA>::iterator rva = rvas->begin();
231 rva != rvas->end();
232 rva++) {
233
234 size_t offset;
235
236 if (!RVAToFileOffset((*rva)->rva(), &offset))
237 return false;
238
239 (*rva)->set_offset(offset);
240 }
241
242 return true;
243}
244
[email protected]39ed9732013-06-20 10:17:53245CheckBool DisassemblerElf32::ParseFile(AssemblyProgram* program) {
246 // Walk all the bytes in the file, whether or not in a section.
aviab98dcc92015-12-21 19:35:33247 uint32_t file_offset = 0;
[email protected]39ed9732013-06-20 10:17:53248
249 std::vector<size_t> abs_offsets;
[email protected]39ed9732013-06-20 10:17:53250
251 if (!RVAsToOffsets(&abs32_locations_, &abs_offsets))
252 return false;
253
[email protected]144c8e92013-07-23 21:18:19254 if (!RVAsToOffsets(&rel32_locations_))
[email protected]39ed9732013-06-20 10:17:53255 return false;
256
257 std::vector<size_t>::iterator current_abs_offset = abs_offsets.begin();
[email protected]144c8e92013-07-23 21:18:19258 ScopedVector<TypedRVA>::iterator current_rel = rel32_locations_.begin();
[email protected]39ed9732013-06-20 10:17:53259
260 std::vector<size_t>::iterator end_abs_offset = abs_offsets.end();
[email protected]144c8e92013-07-23 21:18:19261 ScopedVector<TypedRVA>::iterator end_rel = rel32_locations_.end();
[email protected]39ed9732013-06-20 10:17:53262
263 for (int section_id = 0;
264 section_id < SectionHeaderCount();
265 section_id++) {
266
267 const Elf32_Shdr *section_header = SectionHeader(section_id);
268
Will Harris3e6fa972015-03-02 21:14:25269 if (section_header->sh_type == SHT_NOBITS)
270 continue;
271
[email protected]39ed9732013-06-20 10:17:53272 if (!ParseSimpleRegion(file_offset,
273 section_header->sh_offset,
274 program))
275 return false;
276 file_offset = section_header->sh_offset;
277
278 switch (section_header->sh_type) {
279 case SHT_REL:
280 if (!ParseRelocationSection(section_header, program))
281 return false;
282 file_offset = section_header->sh_offset + section_header->sh_size;
283 break;
284 case SHT_PROGBITS:
285 if (!ParseProgbitsSection(section_header,
286 &current_abs_offset, end_abs_offset,
[email protected]144c8e92013-07-23 21:18:19287 &current_rel, end_rel,
[email protected]39ed9732013-06-20 10:17:53288 program))
289 return false;
290 file_offset = section_header->sh_offset + section_header->sh_size;
291 break;
[email protected]39ed9732013-06-20 10:17:53292 case SHT_INIT_ARRAY:
293 // Fall through
294 case SHT_FINI_ARRAY:
295 while (current_abs_offset != end_abs_offset &&
296 *current_abs_offset >= section_header->sh_offset &&
297 *current_abs_offset <
298 (section_header->sh_offset + section_header->sh_size)) {
299 // Skip any abs_offsets appear in the unsupported INIT_ARRAY section
300 VLOG(1) << "Skipping relocation entry for unsupported section: " <<
301 section_header->sh_type;
302 current_abs_offset++;
303 }
304 break;
305 default:
306 if (current_abs_offset != end_abs_offset &&
307 *current_abs_offset >= section_header->sh_offset &&
308 *current_abs_offset <
309 (section_header->sh_offset + section_header->sh_size))
310 VLOG(1) << "Relocation address in unrecognized ELF section: " << \
311 section_header->sh_type;
312 break;
313 }
314 }
315
316 // Rest of the file past the last section
317 if (!ParseSimpleRegion(file_offset,
318 length(),
319 program))
320 return false;
321
322 // Make certain we consume all of the relocations as expected
323 return (current_abs_offset == end_abs_offset);
324}
325
326CheckBool DisassemblerElf32::ParseProgbitsSection(
327 const Elf32_Shdr *section_header,
328 std::vector<size_t>::iterator* current_abs_offset,
329 std::vector<size_t>::iterator end_abs_offset,
[email protected]144c8e92013-07-23 21:18:19330 ScopedVector<TypedRVA>::iterator* current_rel,
331 ScopedVector<TypedRVA>::iterator end_rel,
[email protected]39ed9732013-06-20 10:17:53332 AssemblyProgram* program) {
333
334 // Walk all the bytes in the file, whether or not in a section.
335 size_t file_offset = section_header->sh_offset;
336 size_t section_end = section_header->sh_offset + section_header->sh_size;
337
338 Elf32_Addr origin = section_header->sh_addr;
339 size_t origin_offset = section_header->sh_offset;
340 if (!program->EmitOriginInstruction(origin))
341 return false;
342
343 while (file_offset < section_end) {
344
345 if (*current_abs_offset != end_abs_offset &&
346 file_offset > **current_abs_offset)
347 return false;
348
[email protected]144c8e92013-07-23 21:18:19349 while (*current_rel != end_rel &&
350 file_offset > (**current_rel)->get_offset()) {
351 (*current_rel)++;
[email protected]39ed9732013-06-20 10:17:53352 }
353
354 size_t next_relocation = section_end;
355
356 if (*current_abs_offset != end_abs_offset &&
357 next_relocation > **current_abs_offset)
358 next_relocation = **current_abs_offset;
359
360 // Rel offsets are heuristically derived, and might (incorrectly) overlap
361 // an Abs value, or the end of the section, so +3 to make sure there is
362 // room for the full 4 byte value.
[email protected]144c8e92013-07-23 21:18:19363 if (*current_rel != end_rel &&
364 next_relocation > ((**current_rel)->get_offset() + 3))
365 next_relocation = (**current_rel)->get_offset();
[email protected]39ed9732013-06-20 10:17:53366
367 if (next_relocation > file_offset) {
368 if (!ParseSimpleRegion(file_offset, next_relocation, program))
369 return false;
370
371 file_offset = next_relocation;
372 continue;
373 }
374
375 if (*current_abs_offset != end_abs_offset &&
376 file_offset == **current_abs_offset) {
aviab98dcc92015-12-21 19:35:33377 const uint8_t* p = OffsetToPointer(file_offset);
[email protected]39ed9732013-06-20 10:17:53378 RVA target_rva = Read32LittleEndian(p);
379
380 if (!program->EmitAbs32(program->FindOrMakeAbs32Label(target_rva)))
381 return false;
382 file_offset += sizeof(RVA);
383 (*current_abs_offset)++;
384 continue;
385 }
386
[email protected]144c8e92013-07-23 21:18:19387 if (*current_rel != end_rel &&
388 file_offset == (**current_rel)->get_offset()) {
aviab98dcc92015-12-21 19:35:33389 uint32_t relative_target = (**current_rel)->relative_target();
[email protected]39ed9732013-06-20 10:17:53390 // This cast is for 64 bit systems, and is only safe because we
391 // are working on 32 bit executables.
392 RVA target_rva = (RVA)(origin + (file_offset - origin_offset) +
[email protected]144c8e92013-07-23 21:18:19393 relative_target);
[email protected]39ed9732013-06-20 10:17:53394
[email protected]2b637b62013-08-01 00:11:24395 if (! (**current_rel)->EmitInstruction(program, target_rva))
[email protected]39ed9732013-06-20 10:17:53396 return false;
[email protected]2b637b62013-08-01 00:11:24397 file_offset += (**current_rel)->op_size();
[email protected]144c8e92013-07-23 21:18:19398 (*current_rel)++;
[email protected]39ed9732013-06-20 10:17:53399 continue;
400 }
401 }
402
403 // Rest of the section (if any)
404 return ParseSimpleRegion(file_offset, section_end, program);
405}
406
407CheckBool DisassemblerElf32::ParseSimpleRegion(
408 size_t start_file_offset,
409 size_t end_file_offset,
410 AssemblyProgram* program) {
[email protected]c092858a2013-08-13 00:46:30411 // Callers don't guarantee start < end
pkasting8e3a26a2014-10-03 18:52:29412 if (start_file_offset >= end_file_offset) return true;
[email protected]39ed9732013-06-20 10:17:53413
pkasting8e3a26a2014-10-03 18:52:29414 const size_t len = end_file_offset - start_file_offset;
[email protected]c092858a2013-08-13 00:46:30415
pkasting8e3a26a2014-10-03 18:52:29416 if (!program->EmitBytesInstruction(OffsetToPointer(start_file_offset), len))
[email protected]c092858a2013-08-13 00:46:30417 return false;
[email protected]39ed9732013-06-20 10:17:53418
419 return true;
420}
421
422CheckBool DisassemblerElf32::ParseAbs32Relocs() {
423 abs32_locations_.clear();
424
425 // Loop through sections for relocation sections
426 for (int section_id = 0; section_id < SectionHeaderCount(); section_id++) {
427 const Elf32_Shdr *section_header = SectionHeader(section_id);
428
429 if (section_header->sh_type == SHT_REL) {
430
431 Elf32_Rel *relocs_table = (Elf32_Rel *)SectionBody(section_id);
432
433 int relocs_table_count = section_header->sh_size /
434 section_header->sh_entsize;
435
436 // Elf32_Word relocation_section_id = section_header->sh_info;
437
438 // Loop through relocation objects in the relocation section
439 for (int rel_id = 0; rel_id < relocs_table_count; rel_id++) {
440 RVA rva;
441
442 // Quite a few of these conversions fail, and we simply skip
443 // them, that's okay.
[email protected]a8e80412013-07-18 22:07:53444 if (RelToRVA(relocs_table[rel_id], &rva) && CheckSection(rva))
[email protected]39ed9732013-06-20 10:17:53445 abs32_locations_.push_back(rva);
446 }
447 }
448 }
449
450 std::sort(abs32_locations_.begin(), abs32_locations_.end());
huangsbb4b8a92016-01-19 22:09:03451 DCHECK(abs32_locations_.empty() ||
452 abs32_locations_.back() != kUnassignedRVA);
[email protected]39ed9732013-06-20 10:17:53453 return true;
454}
455
[email protected]a8e80412013-07-18 22:07:53456CheckBool DisassemblerElf32::CheckSection(RVA rva) {
457 size_t offset;
458
459 if (!RVAToFileOffset(rva, &offset)) {
460 return false;
461 }
462
463 for (int section_id = 0;
464 section_id < SectionHeaderCount();
465 section_id++) {
466
467 const Elf32_Shdr *section_header = SectionHeader(section_id);
468
469 if (offset >= section_header->sh_offset &&
470 offset < (section_header->sh_offset + section_header->sh_size)) {
471 switch (section_header->sh_type) {
472 case SHT_REL:
473 // Fall-through
474 case SHT_PROGBITS:
475 return true;
476 }
477 }
478 }
479
480 return false;
481}
482
[email protected]39ed9732013-06-20 10:17:53483CheckBool DisassemblerElf32::ParseRel32RelocsFromSections() {
484
485 rel32_locations_.clear();
486
487 // Loop through sections for relocation sections
488 for (int section_id = 0;
489 section_id < SectionHeaderCount();
490 section_id++) {
491
492 const Elf32_Shdr *section_header = SectionHeader(section_id);
493
494 if (section_header->sh_type != SHT_PROGBITS)
495 continue;
496
497 if (!ParseRel32RelocsFromSection(section_header))
498 return false;
499 }
500
[email protected]144c8e92013-07-23 21:18:19501 std::sort(rel32_locations_.begin(),
502 rel32_locations_.end(),
503 TypedRVA::IsLessThan);
huangsbb4b8a92016-01-19 22:09:03504 DCHECK(rel32_locations_.empty() ||
505 rel32_locations_.back()->rva() != kUnassignedRVA);
[email protected]39ed9732013-06-20 10:17:53506 return true;
507}
508
509} // namespace courgette