paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 1 | // 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 | #ifndef COURGETTE_DISASSEMBLER_ELF_32_H_ |
| 6 | #define COURGETTE_DISASSEMBLER_ELF_32_H_ |
| 7 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 13 | #include "base/macros.h" |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 14 | #include "base/memory/scoped_vector.h" |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 15 | #include "courgette/disassembler.h" |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 16 | #include "courgette/image_utils.h" |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 17 | #include "courgette/memory_allocator.h" |
| 18 | #include "courgette/types_elf.h" |
| 19 | |
| 20 | namespace courgette { |
| 21 | |
| 22 | class AssemblyProgram; |
| 23 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 24 | // A Courgette disassembler for 32-bit ELF files. This is only a partial |
| 25 | // implementation that admits subclasses for the architecture-specific parts of |
| 26 | // 32-bit ELF file processing. Specifically: |
| 27 | // - RelToRVA() processes entries in ELF relocation table. |
| 28 | // - ParseRelocationSection() verifies the organization of the ELF relocation |
| 29 | // table. |
| 30 | // - ParseRel32RelocsFromSection() finds branch targets by looking for relative |
| 31 | // branch/call opcodes in the particular architecture's machine code. |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 32 | class DisassemblerElf32 : public Disassembler { |
| 33 | public: |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 34 | // Different instructions encode the target rva differently. This |
| 35 | // class encapsulates this behavior. public for use in unit tests. |
| 36 | class TypedRVA { |
| 37 | public: |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 38 | explicit TypedRVA(RVA rva) : rva_(rva) { } |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 39 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 40 | virtual ~TypedRVA() { } |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 41 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 42 | RVA rva() const { return rva_; } |
| 43 | RVA relative_target() const { return relative_target_; } |
| 44 | FileOffset file_offset() const { return file_offset_; } |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 45 | |
| 46 | void set_relative_target(RVA relative_target) { |
| 47 | relative_target_ = relative_target; |
| 48 | } |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 49 | void set_file_offset(FileOffset file_offset) { |
| 50 | file_offset_ = file_offset; |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 51 | } |
| 52 | |
paulgazz@chromium.org | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 53 | // Computes the relative jump's offset from the op in p. |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 54 | virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 55 | |
paulgazz@chromium.org | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 56 | // Emits the courgette instruction corresponding to the RVA type. |
| 57 | virtual CheckBool EmitInstruction(AssemblyProgram* program, |
| 58 | RVA target_rva) = 0; |
| 59 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 60 | // Returns the size of the instruction containing the RVA. |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 61 | virtual uint16_t op_size() const = 0; |
paulgazz@chromium.org | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 62 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 63 | // Comparator for sorting, which assumes uniqueness of RVAs. |
| 64 | static bool IsLessThan(TypedRVA* a, TypedRVA* b) { |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 65 | return a->rva() < b->rva(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | const RVA rva_; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 70 | RVA relative_target_ = kNoRVA; |
| 71 | FileOffset file_offset_ = kNoFileOffset; |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | public: |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 75 | DisassemblerElf32(const void* start, size_t length); |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 76 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 77 | ~DisassemblerElf32() override { } |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 78 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 79 | // Disassembler interfaces. |
| 80 | RVA FileOffsetToRVA(FileOffset file_offset) const override; |
| 81 | FileOffset RVAToFileOffset(RVA rva) const override; |
huangs | f940a8c9 | 2016-03-23 20:40:35 | [diff] [blame^] | 82 | RVA PointerToTargetRVA(const uint8_t* p) const override; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 83 | virtual ExecutableType kind() const override = 0; |
| 84 | bool ParseHeader() override; |
| 85 | bool Disassemble(AssemblyProgram* target) override; |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 86 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 87 | virtual e_machine_values ElfEM() const = 0; |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 88 | |
| 89 | // Public for unittests only |
| 90 | std::vector<RVA> &Abs32Locations() { return abs32_locations_; } |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 91 | ScopedVector<TypedRVA> &Rel32Locations() { return rel32_locations_; } |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 92 | |
| 93 | protected: |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 94 | bool UpdateLength(); |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 95 | |
| 96 | // Misc Section Helpers |
| 97 | |
sorin@chromium.org | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 98 | Elf32_Half SectionHeaderCount() const { |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 99 | return section_header_table_size_; |
| 100 | } |
| 101 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 102 | const Elf32_Shdr* SectionHeader(Elf32_Half id) const { |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 103 | assert(id >= 0 && id < SectionHeaderCount()); |
| 104 | return section_header_table_ + id; |
| 105 | } |
| 106 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 107 | const uint8_t* SectionBody(Elf32_Half id) const { |
| 108 | return FileOffsetToPointer(SectionHeader(id)->sh_offset); |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 109 | } |
| 110 | |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 111 | // Misc Segment Helpers |
| 112 | |
sorin@chromium.org | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 113 | Elf32_Half ProgramSegmentHeaderCount() const { |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 114 | return program_header_table_size_; |
| 115 | } |
| 116 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 117 | const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const { |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 118 | assert(id >= 0 && id < ProgramSegmentHeaderCount()); |
| 119 | return program_header_table_ + id; |
| 120 | } |
| 121 | |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 122 | // Misc address space helpers |
| 123 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 124 | CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 125 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 126 | // Converts an ELF relocation instruction into an RVA. |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 127 | virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) |
| 128 | const WARN_UNUSED_RESULT = 0; |
| 129 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 130 | CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, |
| 131 | std::vector<FileOffset>* file_offsets); |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 132 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 133 | CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas); |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 134 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 135 | // Parsing code for Disassemble(). |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 136 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 137 | virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, |
| 138 | AssemblyProgram* program) |
| 139 | WARN_UNUSED_RESULT = 0; |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 140 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 141 | virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) |
| 142 | WARN_UNUSED_RESULT = 0; |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 143 | |
| 144 | CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 145 | |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 146 | CheckBool ParseProgbitsSection( |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 147 | const Elf32_Shdr* section_header, |
| 148 | std::vector<FileOffset>::iterator* current_abs_offset, |
| 149 | std::vector<FileOffset>::iterator end_abs_offset, |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 150 | ScopedVector<TypedRVA>::iterator* current_rel, |
| 151 | ScopedVector<TypedRVA>::iterator end_rel, |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 152 | AssemblyProgram* program) WARN_UNUSED_RESULT; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 153 | |
| 154 | CheckBool ParseSimpleRegion(FileOffset start_file_offset, |
| 155 | FileOffset end_file_offset, |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 156 | AssemblyProgram* program) WARN_UNUSED_RESULT; |
| 157 | |
| 158 | CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; |
huangs | 58b822d4 | 2016-03-12 20:56:11 | [diff] [blame] | 159 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 160 | CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT; |
| 161 | |
| 162 | CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT; |
| 163 | |
| 164 | const Elf32_Ehdr* header_; |
| 165 | const Elf32_Shdr* section_header_table_; |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 166 | Elf32_Half section_header_table_size_; |
| 167 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 168 | const Elf32_Phdr* program_header_table_; |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 169 | Elf32_Half program_header_table_size_; |
| 170 | |
| 171 | // Section header for default |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 172 | const char* default_string_section_; |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 173 | |
| 174 | std::vector<RVA> abs32_locations_; |
paulgazz@chromium.org | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 175 | ScopedVector<TypedRVA> rel32_locations_; |
paulgazz@chromium.org | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 176 | |
| 177 | DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); |
| 178 | }; |
| 179 | |
| 180 | } // namespace courgette |
| 181 | |
| 182 | #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ |