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