[email protected] | 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 | |
etiennep | 7d4e8ee | 2016-05-11 20:13:36 | [diff] [blame] | 11 | #include <memory> |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame^] | 12 | #include <string> |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 16 | #include "courgette/disassembler.h" |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 17 | #include "courgette/image_utils.h" |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 18 | #include "courgette/memory_allocator.h" |
| 19 | #include "courgette/types_elf.h" |
| 20 | |
| 21 | namespace courgette { |
| 22 | |
| 23 | class AssemblyProgram; |
| 24 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 25 | // A Courgette disassembler for 32-bit ELF files. This is only a partial |
| 26 | // implementation that admits subclasses for the architecture-specific parts of |
| 27 | // 32-bit ELF file processing. Specifically: |
| 28 | // - RelToRVA() processes entries in ELF relocation table. |
| 29 | // - ParseRelocationSection() verifies the organization of the ELF relocation |
| 30 | // table. |
| 31 | // - ParseRel32RelocsFromSection() finds branch targets by looking for relative |
| 32 | // branch/call opcodes in the particular architecture's machine code. |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 33 | class DisassemblerElf32 : public Disassembler { |
| 34 | public: |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 35 | // Different instructions encode the target rva differently. This |
| 36 | // class encapsulates this behavior. public for use in unit tests. |
| 37 | class TypedRVA { |
| 38 | public: |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 39 | explicit TypedRVA(RVA rva) : rva_(rva) { } |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 40 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 41 | virtual ~TypedRVA() { } |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 42 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 43 | RVA rva() const { return rva_; } |
| 44 | RVA relative_target() const { return relative_target_; } |
| 45 | FileOffset file_offset() const { return file_offset_; } |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 46 | |
| 47 | void set_relative_target(RVA relative_target) { |
| 48 | relative_target_ = relative_target; |
| 49 | } |
etiennep | 7d4e8ee | 2016-05-11 20:13:36 | [diff] [blame] | 50 | void set_file_offset(FileOffset file_offset) { file_offset_ = file_offset; } |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 51 | |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 52 | // Computes the relative jump's offset from the op in p. |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 53 | virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 54 | |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame^] | 55 | // Emits the assembly instruction corresponding to |label|. |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 56 | virtual CheckBool EmitInstruction(AssemblyProgram* program, |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame^] | 57 | Label* label) = 0; |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 58 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 59 | // Returns the size of the instruction containing the RVA. |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 60 | virtual uint16_t op_size() const = 0; |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 61 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 62 | // Comparator for sorting, which assumes uniqueness of RVAs. |
etiennep | 7d4e8ee | 2016-05-11 20:13:36 | [diff] [blame] | 63 | static bool IsLessThanByRVA(const std::unique_ptr<TypedRVA>& a, |
| 64 | const std::unique_ptr<TypedRVA>& b) { |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 65 | return a->rva() < b->rva(); |
| 66 | } |
| 67 | |
huangs | 3da0dd93 | 2016-04-28 22:14:45 | [diff] [blame] | 68 | // Comparator for sorting, which assumes uniqueness of file offsets. |
etiennep | 7d4e8ee | 2016-05-11 20:13:36 | [diff] [blame] | 69 | static bool IsLessThanByFileOffset(const std::unique_ptr<TypedRVA>& a, |
| 70 | const std::unique_ptr<TypedRVA>& b) { |
huangs | 3da0dd93 | 2016-04-28 22:14:45 | [diff] [blame] | 71 | return a->file_offset() < b->file_offset(); |
| 72 | } |
| 73 | |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 74 | private: |
| 75 | const RVA rva_; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 76 | RVA relative_target_ = kNoRVA; |
| 77 | FileOffset file_offset_ = kNoFileOffset; |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 78 | }; |
| 79 | |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame^] | 80 | // Visitor/adaptor to translate RVA to target RVA. This is the ELF |
| 81 | // counterpart to RvaVisitor_Rel32 that uses TypedRVA. |
| 82 | class Elf32RvaVisitor_Rel32 : |
| 83 | public VectorRvaVisitor<std::unique_ptr<TypedRVA>> { |
| 84 | public: |
| 85 | Elf32RvaVisitor_Rel32( |
| 86 | const std::vector<std::unique_ptr<TypedRVA>>& rva_locations); |
| 87 | ~Elf32RvaVisitor_Rel32() override { } |
| 88 | |
| 89 | // VectorRvaVisitor<TypedRVA*> interfaces. |
| 90 | RVA Get() const override; |
| 91 | |
| 92 | private: |
| 93 | DISALLOW_COPY_AND_ASSIGN(Elf32RvaVisitor_Rel32); |
| 94 | }; |
| 95 | |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 96 | public: |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 97 | DisassemblerElf32(const void* start, size_t length); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 98 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 99 | ~DisassemblerElf32() override { } |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 100 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 101 | // Disassembler interfaces. |
| 102 | RVA FileOffsetToRVA(FileOffset file_offset) const override; |
| 103 | FileOffset RVAToFileOffset(RVA rva) const override; |
huangs | f940a8c9 | 2016-03-23 20:40:35 | [diff] [blame] | 104 | RVA PointerToTargetRVA(const uint8_t* p) const override; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 105 | virtual ExecutableType kind() const override = 0; |
| 106 | bool ParseHeader() override; |
| 107 | bool Disassemble(AssemblyProgram* target) override; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 108 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 109 | virtual e_machine_values ElfEM() const = 0; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 110 | |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame^] | 111 | CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; |
| 112 | |
| 113 | // Converts an ELF relocation instruction into an RVA. |
| 114 | virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) |
| 115 | const WARN_UNUSED_RESULT = 0; |
| 116 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 117 | // Public for unittests only |
etiennep | 7d4e8ee | 2016-05-11 20:13:36 | [diff] [blame] | 118 | std::vector<RVA>& Abs32Locations() { return abs32_locations_; } |
| 119 | std::vector<std::unique_ptr<TypedRVA>>& Rel32Locations() { |
| 120 | return rel32_locations_; |
| 121 | } |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 122 | |
| 123 | protected: |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 124 | bool UpdateLength(); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 125 | |
| 126 | // Misc Section Helpers |
| 127 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 128 | Elf32_Half SectionHeaderCount() const { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 129 | return section_header_table_size_; |
| 130 | } |
| 131 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 132 | const Elf32_Shdr* SectionHeader(Elf32_Half id) const { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 133 | assert(id >= 0 && id < SectionHeaderCount()); |
huangs | 8cffb28 | 2016-04-09 19:43:50 | [diff] [blame] | 134 | return §ion_header_table_[id]; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 135 | } |
| 136 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 137 | const uint8_t* SectionBody(Elf32_Half id) const { |
huangs | 16f01e0 | 2016-04-21 19:38:38 | [diff] [blame] | 138 | // TODO(huangs): Assert that section does not have SHT_NOBITS. |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 139 | return FileOffsetToPointer(SectionHeader(id)->sh_offset); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 140 | } |
| 141 | |
huangs | 16f01e0 | 2016-04-21 19:38:38 | [diff] [blame] | 142 | // Gets the |name| of section |shdr|. Returns true on success. |
| 143 | CheckBool SectionName(const Elf32_Shdr& shdr, std::string* name) const; |
| 144 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 145 | // Misc Segment Helpers |
| 146 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 147 | Elf32_Half ProgramSegmentHeaderCount() const { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 148 | return program_header_table_size_; |
| 149 | } |
| 150 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 151 | const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 152 | assert(id >= 0 && id < ProgramSegmentHeaderCount()); |
| 153 | return program_header_table_ + id; |
| 154 | } |
| 155 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 156 | // Misc address space helpers |
| 157 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 158 | CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, |
| 159 | std::vector<FileOffset>* file_offsets); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 160 | |
etiennep | 7d4e8ee | 2016-05-11 20:13:36 | [diff] [blame] | 161 | CheckBool RVAsToFileOffsets( |
| 162 | std::vector<std::unique_ptr<TypedRVA>>* typed_rvas); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 163 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 164 | // Parsing code for Disassemble(). |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 165 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 166 | virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, |
| 167 | AssemblyProgram* program) |
| 168 | WARN_UNUSED_RESULT = 0; |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 169 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 170 | virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) |
| 171 | WARN_UNUSED_RESULT = 0; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 172 | |
huangs | c803763 | 2016-05-19 18:16:40 | [diff] [blame^] | 173 | // Disassembler interfaces. |
| 174 | RvaVisitor* CreateAbs32TargetRvaVisitor() override; |
| 175 | RvaVisitor* CreateRel32TargetRvaVisitor() override; |
| 176 | void RemoveUnusedRel32Locations(AssemblyProgram* program) override; |
| 177 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 178 | CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 179 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 180 | CheckBool ParseProgbitsSection( |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 181 | const Elf32_Shdr* section_header, |
| 182 | std::vector<FileOffset>::iterator* current_abs_offset, |
| 183 | std::vector<FileOffset>::iterator end_abs_offset, |
etiennep | 7d4e8ee | 2016-05-11 20:13:36 | [diff] [blame] | 184 | std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, |
| 185 | std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 186 | AssemblyProgram* program) WARN_UNUSED_RESULT; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 187 | |
| 188 | CheckBool ParseSimpleRegion(FileOffset start_file_offset, |
| 189 | FileOffset end_file_offset, |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 190 | AssemblyProgram* program) WARN_UNUSED_RESULT; |
| 191 | |
| 192 | CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; |
huangs | 58b822d4 | 2016-03-12 20:56:11 | [diff] [blame] | 193 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 194 | CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT; |
| 195 | |
huangs | 3da0dd93 | 2016-04-28 22:14:45 | [diff] [blame] | 196 | // Extracts all rel32 TypedRVAs. Does not sort the result. |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 197 | CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT; |
| 198 | |
| 199 | const Elf32_Ehdr* header_; |
huangs | 69189300 | 2016-04-19 20:04:33 | [diff] [blame] | 200 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 201 | Elf32_Half section_header_table_size_; |
| 202 | |
huangs | 69189300 | 2016-04-19 20:04:33 | [diff] [blame] | 203 | // Section header table, ordered by section id. |
| 204 | std::vector<Elf32_Shdr> section_header_table_; |
| 205 | |
| 206 | // An ordering of |section_header_table_|, sorted by file offset. |
| 207 | std::vector<Elf32_Half> section_header_file_offset_order_; |
| 208 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 209 | const Elf32_Phdr* program_header_table_; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 210 | Elf32_Half program_header_table_size_; |
| 211 | |
huangs | 16f01e0 | 2016-04-21 19:38:38 | [diff] [blame] | 212 | // Pointer to string table containing section names. |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame] | 213 | const char* default_string_section_; |
huangs | 16f01e0 | 2016-04-21 19:38:38 | [diff] [blame] | 214 | size_t default_string_section_size_; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 215 | |
| 216 | std::vector<RVA> abs32_locations_; |
etiennep | 7d4e8ee | 2016-05-11 20:13:36 | [diff] [blame] | 217 | std::vector<std::unique_ptr<TypedRVA>> rel32_locations_; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 218 | |
| 219 | DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); |
| 220 | }; |
| 221 | |
| 222 | } // namespace courgette |
| 223 | |
| 224 | #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ |