[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 | #include "courgette/disassembler_elf_32.h" |
| 6 | |
| 7 | #include <algorithm> |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 8 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 10 | #include "courgette/assembly_program.h" |
| 11 | #include "courgette/courgette.h" |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 12 | |
| 13 | namespace courgette { |
| 14 | |
| 15 | DisassemblerElf32::DisassemblerElf32(const void* start, size_t length) |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 16 | : Disassembler(start, length), |
| 17 | header_(nullptr), |
| 18 | section_header_table_(nullptr), |
| 19 | section_header_table_size_(0), |
| 20 | program_header_table_(nullptr), |
| 21 | program_header_table_size_(0), |
| 22 | default_string_section_(nullptr) { |
| 23 | } |
| 24 | |
| 25 | RVA DisassemblerElf32::FileOffsetToRVA(FileOffset offset) const { |
| 26 | // File offsets can be 64-bit values, but we are dealing with 32-bit |
| 27 | // executables and so only need to support 32-bit file sizes. |
| 28 | uint32_t offset32 = static_cast<uint32_t>(offset); |
| 29 | |
| 30 | for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); |
| 31 | ++section_id) { |
| 32 | const Elf32_Shdr* section_header = SectionHeader(section_id); |
| 33 | // These can appear to have a size in the file, but don't. |
| 34 | if (section_header->sh_type == SHT_NOBITS) |
| 35 | continue; |
| 36 | |
| 37 | Elf32_Off section_begin = section_header->sh_offset; |
| 38 | Elf32_Off section_end = section_begin + section_header->sh_size; |
| 39 | |
| 40 | if (offset32 >= section_begin && offset32 < section_end) { |
| 41 | return section_header->sh_addr + (offset32 - section_begin); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | FileOffset DisassemblerElf32::RVAToFileOffset(RVA rva) const { |
| 49 | for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); |
| 50 | ++section_id) { |
| 51 | const Elf32_Shdr* section_header = SectionHeader(section_id); |
| 52 | // These can appear to have a size in the file, but don't. |
| 53 | if (section_header->sh_type == SHT_NOBITS) |
| 54 | continue; |
| 55 | Elf32_Addr begin = section_header->sh_addr; |
| 56 | Elf32_Addr end = begin + section_header->sh_size; |
| 57 | |
| 58 | if (rva >= begin && rva < end) |
| 59 | return section_header->sh_offset + (rva - begin); |
| 60 | } |
| 61 | return kNoFileOffset; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool DisassemblerElf32::ParseHeader() { |
| 65 | if (length() < sizeof(Elf32_Ehdr)) |
| 66 | return Bad("Too small"); |
| 67 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 68 | header_ = reinterpret_cast<const Elf32_Ehdr*>(start()); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 69 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 70 | // Have magic for ELF header? |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 71 | if (header_->e_ident[0] != 0x7f || |
| 72 | header_->e_ident[1] != 'E' || |
| 73 | header_->e_ident[2] != 'L' || |
| 74 | header_->e_ident[3] != 'F') |
| 75 | return Bad("No Magic Number"); |
| 76 | |
| 77 | if (header_->e_type != ET_EXEC && |
| 78 | header_->e_type != ET_DYN) |
| 79 | return Bad("Not an executable file or shared library"); |
| 80 | |
| 81 | if (header_->e_machine != ElfEM()) |
| 82 | return Bad("Not a supported architecture"); |
| 83 | |
| 84 | if (header_->e_version != 1) |
| 85 | return Bad("Unknown file version"); |
| 86 | |
| 87 | if (header_->e_shentsize != sizeof(Elf32_Shdr)) |
| 88 | return Bad("Unexpected section header size"); |
| 89 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 90 | if (!IsArrayInBounds(header_->e_shoff, header_->e_shnum, sizeof(Elf32_Shdr))) |
| 91 | return Bad("Out of bounds section header table"); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 92 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 93 | section_header_table_ = reinterpret_cast<const Elf32_Shdr*>( |
| 94 | FileOffsetToPointer(header_->e_shoff)); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 95 | section_header_table_size_ = header_->e_shnum; |
| 96 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 97 | if (!IsArrayInBounds(header_->e_phoff, header_->e_phnum, sizeof(Elf32_Phdr))) |
| 98 | return Bad("Out of bounds program header table"); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 99 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 100 | program_header_table_ = reinterpret_cast<const Elf32_Phdr*>( |
| 101 | FileOffsetToPointer(header_->e_phoff)); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 102 | program_header_table_size_ = header_->e_phnum; |
| 103 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 104 | if (header_->e_shstrndx >= header_->e_shnum) |
| 105 | return Bad("Out of bounds string section index"); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 106 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 107 | default_string_section_ = reinterpret_cast<const char*>( |
| 108 | SectionBody(static_cast<int>(header_->e_shstrndx))); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 109 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 110 | if (!UpdateLength()) |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 111 | return Bad("Out of bounds section or segment"); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 112 | |
| 113 | return Good(); |
| 114 | } |
| 115 | |
| 116 | bool DisassemblerElf32::Disassemble(AssemblyProgram* target) { |
| 117 | if (!ok()) |
| 118 | return false; |
| 119 | |
| 120 | // The Image Base is always 0 for ELF Executables |
| 121 | target->set_image_base(0); |
| 122 | |
| 123 | if (!ParseAbs32Relocs()) |
| 124 | return false; |
| 125 | |
| 126 | if (!ParseRel32RelocsFromSections()) |
| 127 | return false; |
| 128 | |
| 129 | if (!ParseFile(target)) |
| 130 | return false; |
| 131 | |
| 132 | target->DefaultAssignIndexes(); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 136 | bool DisassemblerElf32::UpdateLength() { |
| 137 | Elf32_Off result = 0; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 138 | |
| 139 | // Find the end of the last section |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 140 | for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); |
| 141 | ++section_id) { |
| 142 | const Elf32_Shdr* section_header = SectionHeader(section_id); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 143 | |
| 144 | if (section_header->sh_type == SHT_NOBITS) |
| 145 | continue; |
| 146 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 147 | if (!IsArrayInBounds(section_header->sh_offset, section_header->sh_size, 1)) |
| 148 | return false; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 149 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 150 | Elf32_Off section_end = section_header->sh_offset + section_header->sh_size; |
| 151 | result = std::max(result, section_end); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Find the end of the last segment |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 155 | for (Elf32_Half segment_id = 0; segment_id < ProgramSegmentHeaderCount(); |
| 156 | ++segment_id) { |
| 157 | const Elf32_Phdr* segment_header = ProgramSegmentHeader(segment_id); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 158 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 159 | if (!IsArrayInBounds(segment_header->p_offset, segment_header->p_filesz, 1)) |
| 160 | return false; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 161 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 162 | Elf32_Off segment_end = segment_header->p_offset + segment_header->p_filesz; |
| 163 | result = std::max(result, segment_end); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 164 | } |
| 165 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 166 | Elf32_Off section_table_end = |
| 167 | header_->e_shoff + (header_->e_shnum * sizeof(Elf32_Shdr)); |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 168 | result = std::max(result, section_table_end); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 169 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 170 | Elf32_Off segment_table_end = |
| 171 | header_->e_phoff + (header_->e_phnum * sizeof(Elf32_Phdr)); |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 172 | result = std::max(result, segment_table_end); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 173 | |
halyavin | c9de6f7 | 2015-03-24 15:40:12 | [diff] [blame] | 174 | ReduceLength(result); |
| 175 | return true; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 176 | } |
| 177 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 178 | CheckBool DisassemblerElf32::IsValidTargetRVA(RVA rva) const { |
huangs | bb4b8a9 | 2016-01-19 22:09:03 | [diff] [blame] | 179 | if (rva == kUnassignedRVA) |
| 180 | return false; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 181 | |
| 182 | // It's valid if it's contained in any program segment |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 183 | for (Elf32_Half segment_id = 0; segment_id < ProgramSegmentHeaderCount(); |
| 184 | ++segment_id) { |
| 185 | const Elf32_Phdr* segment_header = ProgramSegmentHeader(segment_id); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 186 | |
| 187 | if (segment_header->p_type != PT_LOAD) |
| 188 | continue; |
| 189 | |
| 190 | Elf32_Addr begin = segment_header->p_vaddr; |
| 191 | Elf32_Addr end = segment_header->p_vaddr + segment_header->p_memsz; |
| 192 | |
| 193 | if (rva >= begin && rva < end) |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 200 | CheckBool DisassemblerElf32::RVAsToFileOffsets( |
| 201 | const std::vector<RVA>& rvas, |
| 202 | std::vector<FileOffset>* file_offsets) { |
| 203 | file_offsets->clear(); |
| 204 | for (RVA rva : rvas) { |
| 205 | FileOffset file_offset = RVAToFileOffset(rva); |
| 206 | if (file_offset == kNoFileOffset) |
scottmg | 4a95ca5 | 2016-03-12 23:54:56 | [diff] [blame] | 207 | return false; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 208 | file_offsets->push_back(file_offset); |
scottmg | 4a95ca5 | 2016-03-12 23:54:56 | [diff] [blame] | 209 | } |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 213 | CheckBool DisassemblerElf32::RVAsToFileOffsets( |
| 214 | ScopedVector<TypedRVA>* typed_rvas) { |
| 215 | for (TypedRVA* typed_rva : *typed_rvas) { |
| 216 | FileOffset file_offset = RVAToFileOffset(typed_rva->rva()); |
| 217 | if (file_offset == kNoFileOffset) |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 218 | return false; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 219 | typed_rva->set_file_offset(file_offset); |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 220 | } |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 224 | CheckBool DisassemblerElf32::ParseFile(AssemblyProgram* program) { |
| 225 | // Walk all the bytes in the file, whether or not in a section. |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 226 | FileOffset file_offset = 0; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 227 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 228 | std::vector<FileOffset> abs_offsets; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 229 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 230 | if (!RVAsToFileOffsets(abs32_locations_, &abs_offsets)) |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 231 | return false; |
| 232 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 233 | if (!RVAsToFileOffsets(&rel32_locations_)) |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 234 | return false; |
| 235 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 236 | std::vector<FileOffset>::iterator current_abs_offset = abs_offsets.begin(); |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 237 | ScopedVector<TypedRVA>::iterator current_rel = rel32_locations_.begin(); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 238 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 239 | std::vector<FileOffset>::iterator end_abs_offset = abs_offsets.end(); |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 240 | ScopedVector<TypedRVA>::iterator end_rel = rel32_locations_.end(); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 241 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 242 | for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); |
| 243 | ++section_id) { |
| 244 | const Elf32_Shdr* section_header = SectionHeader(section_id); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 245 | |
Will Harris | 3e6fa97 | 2015-03-02 21:14:25 | [diff] [blame] | 246 | if (section_header->sh_type == SHT_NOBITS) |
| 247 | continue; |
| 248 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 249 | if (!ParseSimpleRegion(file_offset, section_header->sh_offset, program)) |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 250 | return false; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 251 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 252 | file_offset = section_header->sh_offset; |
| 253 | |
| 254 | switch (section_header->sh_type) { |
| 255 | case SHT_REL: |
| 256 | if (!ParseRelocationSection(section_header, program)) |
| 257 | return false; |
| 258 | file_offset = section_header->sh_offset + section_header->sh_size; |
| 259 | break; |
| 260 | case SHT_PROGBITS: |
| 261 | if (!ParseProgbitsSection(section_header, |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 262 | ¤t_abs_offset, |
| 263 | end_abs_offset, |
| 264 | ¤t_rel, |
| 265 | end_rel, |
| 266 | program)) { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 267 | return false; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 268 | } |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 269 | file_offset = section_header->sh_offset + section_header->sh_size; |
| 270 | break; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 271 | case SHT_INIT_ARRAY: |
| 272 | // Fall through |
| 273 | case SHT_FINI_ARRAY: |
| 274 | while (current_abs_offset != end_abs_offset && |
| 275 | *current_abs_offset >= section_header->sh_offset && |
| 276 | *current_abs_offset < |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 277 | section_header->sh_offset + section_header->sh_size) { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 278 | // Skip any abs_offsets appear in the unsupported INIT_ARRAY section |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 279 | VLOG(1) << "Skipping relocation entry for unsupported section: " |
| 280 | << section_header->sh_type; |
| 281 | ++current_abs_offset; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 282 | } |
| 283 | break; |
| 284 | default: |
| 285 | if (current_abs_offset != end_abs_offset && |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 286 | *current_abs_offset >= section_header->sh_offset && |
| 287 | *current_abs_offset < |
| 288 | section_header->sh_offset + section_header->sh_size) { |
| 289 | VLOG(1) << "Relocation address in unrecognized ELF section: " |
| 290 | << section_header->sh_type; |
| 291 | } |
| 292 | break; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | // Rest of the file past the last section |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 297 | if (!ParseSimpleRegion(file_offset, length(), program)) |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 298 | return false; |
| 299 | |
| 300 | // Make certain we consume all of the relocations as expected |
| 301 | return (current_abs_offset == end_abs_offset); |
| 302 | } |
| 303 | |
| 304 | CheckBool DisassemblerElf32::ParseProgbitsSection( |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 305 | const Elf32_Shdr* section_header, |
| 306 | std::vector<FileOffset>::iterator* current_abs_offset, |
| 307 | std::vector<FileOffset>::iterator end_abs_offset, |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 308 | ScopedVector<TypedRVA>::iterator* current_rel, |
| 309 | ScopedVector<TypedRVA>::iterator end_rel, |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 310 | AssemblyProgram* program) { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 311 | // Walk all the bytes in the file, whether or not in a section. |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 312 | FileOffset file_offset = section_header->sh_offset; |
| 313 | FileOffset section_end = section_header->sh_offset + section_header->sh_size; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 314 | |
| 315 | Elf32_Addr origin = section_header->sh_addr; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 316 | FileOffset origin_offset = section_header->sh_offset; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 317 | if (!program->EmitOriginInstruction(origin)) |
| 318 | return false; |
| 319 | |
| 320 | while (file_offset < section_end) { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 321 | if (*current_abs_offset != end_abs_offset && |
| 322 | file_offset > **current_abs_offset) |
| 323 | return false; |
| 324 | |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 325 | while (*current_rel != end_rel && |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 326 | file_offset > (**current_rel)->file_offset()) { |
| 327 | ++(*current_rel); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 328 | } |
| 329 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 330 | FileOffset next_relocation = section_end; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 331 | |
| 332 | if (*current_abs_offset != end_abs_offset && |
| 333 | next_relocation > **current_abs_offset) |
| 334 | next_relocation = **current_abs_offset; |
| 335 | |
| 336 | // Rel offsets are heuristically derived, and might (incorrectly) overlap |
| 337 | // an Abs value, or the end of the section, so +3 to make sure there is |
| 338 | // room for the full 4 byte value. |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 339 | if (*current_rel != end_rel && |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 340 | next_relocation > ((**current_rel)->file_offset() + 3)) |
| 341 | next_relocation = (**current_rel)->file_offset(); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 342 | |
| 343 | if (next_relocation > file_offset) { |
| 344 | if (!ParseSimpleRegion(file_offset, next_relocation, program)) |
| 345 | return false; |
| 346 | |
| 347 | file_offset = next_relocation; |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | if (*current_abs_offset != end_abs_offset && |
| 352 | file_offset == **current_abs_offset) { |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 353 | const uint8_t* p = FileOffsetToPointer(file_offset); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 354 | RVA target_rva = Read32LittleEndian(p); |
| 355 | |
| 356 | if (!program->EmitAbs32(program->FindOrMakeAbs32Label(target_rva))) |
| 357 | return false; |
| 358 | file_offset += sizeof(RVA); |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 359 | ++(*current_abs_offset); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 360 | continue; |
| 361 | } |
| 362 | |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 363 | if (*current_rel != end_rel && |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 364 | file_offset == (**current_rel)->file_offset()) { |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 365 | uint32_t relative_target = (**current_rel)->relative_target(); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 366 | // This cast is for 64 bit systems, and is only safe because we |
| 367 | // are working on 32 bit executables. |
| 368 | RVA target_rva = (RVA)(origin + (file_offset - origin_offset) + |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 369 | relative_target); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 370 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 371 | if (!(**current_rel)->EmitInstruction(program, target_rva)) |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 372 | return false; |
[email protected] | 2b637b6 | 2013-08-01 00:11:24 | [diff] [blame] | 373 | file_offset += (**current_rel)->op_size(); |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 374 | ++(*current_rel); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 375 | continue; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Rest of the section (if any) |
| 380 | return ParseSimpleRegion(file_offset, section_end, program); |
| 381 | } |
| 382 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 383 | CheckBool DisassemblerElf32::ParseSimpleRegion(FileOffset start_file_offset, |
| 384 | FileOffset end_file_offset, |
| 385 | AssemblyProgram* program) { |
[email protected] | c092858a | 2013-08-13 00:46:30 | [diff] [blame] | 386 | // Callers don't guarantee start < end |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 387 | if (start_file_offset >= end_file_offset) |
| 388 | return true; |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 389 | |
pkasting | 8e3a26a | 2014-10-03 18:52:29 | [diff] [blame] | 390 | const size_t len = end_file_offset - start_file_offset; |
[email protected] | c092858a | 2013-08-13 00:46:30 | [diff] [blame] | 391 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 392 | if (!program->EmitBytesInstruction(FileOffsetToPointer(start_file_offset), |
| 393 | len)) { |
[email protected] | c092858a | 2013-08-13 00:46:30 | [diff] [blame] | 394 | return false; |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 395 | } |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 396 | |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | CheckBool DisassemblerElf32::ParseAbs32Relocs() { |
| 401 | abs32_locations_.clear(); |
| 402 | |
| 403 | // Loop through sections for relocation sections |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 404 | for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); |
| 405 | ++section_id) { |
| 406 | const Elf32_Shdr* section_header = SectionHeader(section_id); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 407 | |
| 408 | if (section_header->sh_type == SHT_REL) { |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 409 | const Elf32_Rel* relocs_table = |
| 410 | reinterpret_cast<const Elf32_Rel*>(SectionBody(section_id)); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 411 | |
| 412 | int relocs_table_count = section_header->sh_size / |
| 413 | section_header->sh_entsize; |
| 414 | |
| 415 | // Elf32_Word relocation_section_id = section_header->sh_info; |
| 416 | |
| 417 | // Loop through relocation objects in the relocation section |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 418 | for (int rel_id = 0; rel_id < relocs_table_count; ++rel_id) { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 419 | RVA rva; |
| 420 | |
| 421 | // Quite a few of these conversions fail, and we simply skip |
| 422 | // them, that's okay. |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 423 | if (RelToRVA(relocs_table[rel_id], &rva) && CheckSection(rva)) |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 424 | abs32_locations_.push_back(rva); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | std::sort(abs32_locations_.begin(), abs32_locations_.end()); |
huangs | bb4b8a9 | 2016-01-19 22:09:03 | [diff] [blame] | 430 | DCHECK(abs32_locations_.empty() || |
| 431 | abs32_locations_.back() != kUnassignedRVA); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 432 | return true; |
| 433 | } |
| 434 | |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 435 | CheckBool DisassemblerElf32::CheckSection(RVA rva) { |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 436 | FileOffset file_offset = RVAToFileOffset(rva); |
| 437 | if (file_offset == kNoFileOffset) |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 438 | return false; |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 439 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 440 | for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); |
| 441 | ++section_id) { |
| 442 | const Elf32_Shdr* section_header = SectionHeader(section_id); |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 443 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 444 | if (file_offset >= section_header->sh_offset && |
| 445 | file_offset < (section_header->sh_offset + section_header->sh_size)) { |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 446 | switch (section_header->sh_type) { |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 447 | case SHT_REL: // Falls through. |
[email protected] | a8e8041 | 2013-07-18 22:07:53 | [diff] [blame] | 448 | case SHT_PROGBITS: |
| 449 | return true; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | return false; |
| 455 | } |
| 456 | |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 457 | CheckBool DisassemblerElf32::ParseRel32RelocsFromSections() { |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 458 | rel32_locations_.clear(); |
| 459 | |
| 460 | // Loop through sections for relocation sections |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 461 | for (Elf32_Half section_id = 0; section_id < SectionHeaderCount(); |
| 462 | ++section_id) { |
| 463 | const Elf32_Shdr* section_header = SectionHeader(section_id); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 464 | |
huangs | dda11d06 | 2016-03-14 16:35:39 | [diff] [blame^] | 465 | // TODO(huangs): Add better checks to skip non-code sections. |
huangs | 7ec152c | 2016-02-04 22:26:43 | [diff] [blame] | 466 | // Some debug sections can have sh_type=SHT_PROGBITS but sh_addr=0. |
| 467 | if (section_header->sh_type != SHT_PROGBITS || |
| 468 | section_header->sh_addr == 0) |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 469 | continue; |
| 470 | |
| 471 | if (!ParseRel32RelocsFromSection(section_header)) |
| 472 | return false; |
| 473 | } |
| 474 | |
[email protected] | 144c8e9 | 2013-07-23 21:18:19 | [diff] [blame] | 475 | std::sort(rel32_locations_.begin(), |
| 476 | rel32_locations_.end(), |
| 477 | TypedRVA::IsLessThan); |
huangs | bb4b8a9 | 2016-01-19 22:09:03 | [diff] [blame] | 478 | DCHECK(rel32_locations_.empty() || |
| 479 | rel32_locations_.back()->rva() != kUnassignedRVA); |
[email protected] | 39ed973 | 2013-06-20 10:17:53 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | |
| 483 | } // namespace courgette |