[email protected] | 4b3d192b | 2011-11-08 20:32:26 | [diff] [blame] | 1 | // Copyright (c) 2011 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_X86_H_ |
| 6 | #define COURGETTE_DISASSEMBLER_ELF_32_X86_H_ |
| 7 | |
| 8 | #include "base/basictypes.h" |
| 9 | #include "courgette/disassembler.h" |
| 10 | #include "courgette/memory_allocator.h" |
| 11 | #include "courgette/types_elf.h" |
| 12 | |
| 13 | namespace courgette { |
| 14 | |
| 15 | class AssemblyProgram; |
| 16 | |
| 17 | class DisassemblerElf32X86 : public Disassembler { |
| 18 | public: |
| 19 | explicit DisassemblerElf32X86(const void* start, size_t length); |
| 20 | |
| 21 | virtual ExecutableType kind() { return EXE_ELF_32_X86; } |
| 22 | |
| 23 | // Returns 'true' if the buffer appears to point to a valid ELF executable |
| 24 | // for X86 32 bitWindows 32 bit. If ParseHeader() succeeds, other member |
| 25 | // functions may be called. |
| 26 | virtual bool ParseHeader(); |
| 27 | |
| 28 | virtual bool Disassemble(AssemblyProgram* target); |
| 29 | |
| 30 | // Public for unittests only |
| 31 | std::vector<RVA> &Abs32Locations() { return abs32_locations_; } |
| 32 | std::vector<RVA> &Rel32Locations() { return rel32_locations_; } |
| 33 | |
| 34 | protected: |
| 35 | |
| 36 | uint32 DiscoverLength(); |
| 37 | |
| 38 | // Misc Section Helpers |
| 39 | |
| 40 | const Elf32_Half SectionHeaderCount() const { |
| 41 | return section_header_table_size_; |
| 42 | } |
| 43 | |
| 44 | const Elf32_Shdr *SectionHeader(int id) const { |
| 45 | assert(id >= 0 && id < SectionHeaderCount()); |
| 46 | return section_header_table_ + id; |
| 47 | } |
| 48 | |
| 49 | const uint8 *SectionBody(int id) const { |
| 50 | return OffsetToPointer(SectionHeader(id)->sh_offset); |
| 51 | } |
| 52 | |
| 53 | const Elf32_Word SectionBodySize(int id) const { |
| 54 | return SectionHeader(id)->sh_size; |
| 55 | } |
| 56 | |
| 57 | // Misc Segment Helpers |
| 58 | |
| 59 | const Elf32_Half ProgramSegmentHeaderCount() const { |
| 60 | return program_header_table_size_; |
| 61 | } |
| 62 | |
| 63 | const Elf32_Phdr *ProgramSegmentHeader(int id) const { |
| 64 | assert(id >= 0 && id < ProgramSegmentHeaderCount()); |
| 65 | return program_header_table_ + id; |
| 66 | } |
| 67 | |
| 68 | // The virtual memory address at which this program segment will be loaded |
| 69 | const Elf32_Addr ProgramSegmentMemoryBegin(int id) const { |
| 70 | return ProgramSegmentHeader(id)->p_vaddr; |
| 71 | } |
| 72 | |
| 73 | // The number of virtual memory bytes for this program segment |
| 74 | const Elf32_Word ProgramSegmentMemorySize(int id) const { |
| 75 | return ProgramSegmentHeader(id)->p_memsz; |
| 76 | } |
| 77 | |
| 78 | // Pointer into the source file for this program segment |
| 79 | const Elf32_Addr ProgramSegmentFileOffset(int id) const { |
| 80 | return ProgramSegmentHeader(id)->p_offset; |
| 81 | } |
| 82 | |
| 83 | // Number of file bytes for this program segment. Is <= ProgramMemorySize. |
| 84 | const Elf32_Word ProgramSegmentFileSize(int id) const { |
| 85 | return ProgramSegmentHeader(id)->p_filesz; |
| 86 | } |
| 87 | |
| 88 | // Misc address space helpers |
| 89 | |
| 90 | CheckBool IsValidRVA(RVA rva) const WARN_UNUSED_RESULT; |
| 91 | |
| 92 | // Convert an ELF relocation struction into an RVA |
| 93 | CheckBool RelToRVA(Elf32_Rel rel, RVA* result) const WARN_UNUSED_RESULT; |
| 94 | |
| 95 | // Returns kNoOffset if there is no file offset corresponding to 'rva'. |
| 96 | CheckBool RVAToFileOffset(RVA rva, size_t* result) const WARN_UNUSED_RESULT; |
| 97 | |
| 98 | RVA FileOffsetToRVA(size_t offset) const WARN_UNUSED_RESULT; |
| 99 | |
| 100 | CheckBool RVAsToOffsets(std::vector<RVA>* rvas /*in*/, |
| 101 | std::vector<size_t>* offsets /*out*/); |
| 102 | |
| 103 | // Parsing Code used to really implement Disassemble |
| 104 | |
| 105 | CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; |
| 106 | CheckBool ParseRelocationSection( |
| 107 | const Elf32_Shdr *section_header, |
| 108 | AssemblyProgram* program) WARN_UNUSED_RESULT; |
| 109 | CheckBool ParseProgbitsSection( |
| 110 | const Elf32_Shdr *section_header, |
| 111 | std::vector<size_t>::iterator* current_abs_offset, |
| 112 | std::vector<size_t>::iterator end_abs_offset, |
| 113 | std::vector<size_t>::iterator* current_rel_offset, |
| 114 | std::vector<size_t>::iterator end_rel_offset, |
| 115 | AssemblyProgram* program) WARN_UNUSED_RESULT; |
| 116 | CheckBool ParseSimpleRegion(size_t start_file_offset, |
| 117 | size_t end_file_offset, |
| 118 | AssemblyProgram* program) WARN_UNUSED_RESULT; |
| 119 | |
| 120 | CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; |
| 121 | CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT; |
| 122 | CheckBool ParseRel32RelocsFromSection( |
| 123 | const Elf32_Shdr* section) WARN_UNUSED_RESULT; |
| 124 | |
| 125 | Elf32_Ehdr *header_; |
| 126 | Elf32_Shdr *section_header_table_; |
| 127 | Elf32_Half section_header_table_size_; |
| 128 | |
| 129 | Elf32_Phdr *program_header_table_; |
| 130 | Elf32_Half program_header_table_size_; |
| 131 | |
| 132 | // Section header for default |
| 133 | const char *default_string_section_; |
| 134 | |
| 135 | std::vector<RVA> abs32_locations_; |
| 136 | std::vector<RVA> rel32_locations_; |
| 137 | |
| 138 | DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32X86); |
| 139 | }; |
| 140 | |
| 141 | } // namespace courgette |
| 142 | |
| 143 | #endif // COURGETTE_DISASSEMBLER_ELF_32_X86_H_ |