[go: nahoru, domu]

blob: 56895bd570cc6e23c0ce511ac11644b40ea75735 [file] [log] [blame]
paulgazz@chromium.org39ed9732013-06-20 10:17:531// 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
aviab98dcc92015-12-21 19:35:338#include <stddef.h>
9#include <stdint.h>
10
huangsdda11d062016-03-14 16:35:3911#include <vector>
12
aviab98dcc92015-12-21 19:35:3313#include "base/macros.h"
paulgazz@chromium.org144c8e92013-07-23 21:18:1914#include "base/memory/scoped_vector.h"
paulgazz@chromium.org39ed9732013-06-20 10:17:5315#include "courgette/disassembler.h"
huangsdda11d062016-03-14 16:35:3916#include "courgette/image_utils.h"
paulgazz@chromium.org39ed9732013-06-20 10:17:5317#include "courgette/memory_allocator.h"
18#include "courgette/types_elf.h"
19
20namespace courgette {
21
22class AssemblyProgram;
23
huangsdda11d062016-03-14 16:35:3924// 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.org39ed9732013-06-20 10:17:5332class DisassemblerElf32 : public Disassembler {
33 public:
paulgazz@chromium.org144c8e92013-07-23 21:18:1934 // Different instructions encode the target rva differently. This
35 // class encapsulates this behavior. public for use in unit tests.
36 class TypedRVA {
37 public:
huangsdda11d062016-03-14 16:35:3938 explicit TypedRVA(RVA rva) : rva_(rva) { }
paulgazz@chromium.org144c8e92013-07-23 21:18:1939
huangsdda11d062016-03-14 16:35:3940 virtual ~TypedRVA() { }
paulgazz@chromium.org144c8e92013-07-23 21:18:1941
huangsdda11d062016-03-14 16:35:3942 RVA rva() const { return rva_; }
43 RVA relative_target() const { return relative_target_; }
44 FileOffset file_offset() const { return file_offset_; }
paulgazz@chromium.org144c8e92013-07-23 21:18:1945
46 void set_relative_target(RVA relative_target) {
47 relative_target_ = relative_target;
48 }
huangsdda11d062016-03-14 16:35:3949 void set_file_offset(FileOffset file_offset) {
50 file_offset_ = file_offset;
paulgazz@chromium.org144c8e92013-07-23 21:18:1951 }
52
paulgazz@chromium.org2b637b62013-08-01 00:11:2453 // Computes the relative jump's offset from the op in p.
aviab98dcc92015-12-21 19:35:3354 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0;
paulgazz@chromium.org144c8e92013-07-23 21:18:1955
paulgazz@chromium.org2b637b62013-08-01 00:11:2456 // Emits the courgette instruction corresponding to the RVA type.
57 virtual CheckBool EmitInstruction(AssemblyProgram* program,
58 RVA target_rva) = 0;
59
huangsdda11d062016-03-14 16:35:3960 // Returns the size of the instruction containing the RVA.
aviab98dcc92015-12-21 19:35:3361 virtual uint16_t op_size() const = 0;
paulgazz@chromium.org2b637b62013-08-01 00:11:2462
huangsdda11d062016-03-14 16:35:3963 // Comparator for sorting, which assumes uniqueness of RVAs.
64 static bool IsLessThan(TypedRVA* a, TypedRVA* b) {
paulgazz@chromium.org144c8e92013-07-23 21:18:1965 return a->rva() < b->rva();
66 }
67
68 private:
69 const RVA rva_;
huangsdda11d062016-03-14 16:35:3970 RVA relative_target_ = kNoRVA;
71 FileOffset file_offset_ = kNoFileOffset;
paulgazz@chromium.org144c8e92013-07-23 21:18:1972 };
73
74 public:
huangsdda11d062016-03-14 16:35:3975 DisassemblerElf32(const void* start, size_t length);
paulgazz@chromium.org39ed9732013-06-20 10:17:5376
huangsdda11d062016-03-14 16:35:3977 ~DisassemblerElf32() override { }
paulgazz@chromium.org144c8e92013-07-23 21:18:1978
huangsdda11d062016-03-14 16:35:3979 // Disassembler interfaces.
80 RVA FileOffsetToRVA(FileOffset file_offset) const override;
81 FileOffset RVAToFileOffset(RVA rva) const override;
huangsf940a8c92016-03-23 20:40:3582 RVA PointerToTargetRVA(const uint8_t* p) const override;
huangsdda11d062016-03-14 16:35:3983 virtual ExecutableType kind() const override = 0;
84 bool ParseHeader() override;
85 bool Disassemble(AssemblyProgram* target) override;
paulgazz@chromium.org39ed9732013-06-20 10:17:5386
huangsdda11d062016-03-14 16:35:3987 virtual e_machine_values ElfEM() const = 0;
paulgazz@chromium.org39ed9732013-06-20 10:17:5388
89 // Public for unittests only
90 std::vector<RVA> &Abs32Locations() { return abs32_locations_; }
paulgazz@chromium.org144c8e92013-07-23 21:18:1991 ScopedVector<TypedRVA> &Rel32Locations() { return rel32_locations_; }
paulgazz@chromium.org39ed9732013-06-20 10:17:5392
93 protected:
halyavinc9de6f72015-03-24 15:40:1294 bool UpdateLength();
paulgazz@chromium.org39ed9732013-06-20 10:17:5395
96 // Misc Section Helpers
97
sorin@chromium.orge3e696d32013-06-21 20:41:3698 Elf32_Half SectionHeaderCount() const {
paulgazz@chromium.org39ed9732013-06-20 10:17:5399 return section_header_table_size_;
100 }
101
huangsdda11d062016-03-14 16:35:39102 const Elf32_Shdr* SectionHeader(Elf32_Half id) const {
paulgazz@chromium.org39ed9732013-06-20 10:17:53103 assert(id >= 0 && id < SectionHeaderCount());
104 return section_header_table_ + id;
105 }
106
huangsdda11d062016-03-14 16:35:39107 const uint8_t* SectionBody(Elf32_Half id) const {
108 return FileOffsetToPointer(SectionHeader(id)->sh_offset);
paulgazz@chromium.org39ed9732013-06-20 10:17:53109 }
110
paulgazz@chromium.org39ed9732013-06-20 10:17:53111 // Misc Segment Helpers
112
sorin@chromium.orge3e696d32013-06-21 20:41:36113 Elf32_Half ProgramSegmentHeaderCount() const {
paulgazz@chromium.org39ed9732013-06-20 10:17:53114 return program_header_table_size_;
115 }
116
huangsdda11d062016-03-14 16:35:39117 const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const {
paulgazz@chromium.org39ed9732013-06-20 10:17:53118 assert(id >= 0 && id < ProgramSegmentHeaderCount());
119 return program_header_table_ + id;
120 }
121
paulgazz@chromium.org39ed9732013-06-20 10:17:53122 // Misc address space helpers
123
huangsdda11d062016-03-14 16:35:39124 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT;
paulgazz@chromium.org39ed9732013-06-20 10:17:53125
huangsdda11d062016-03-14 16:35:39126 // Converts an ELF relocation instruction into an RVA.
paulgazz@chromium.org39ed9732013-06-20 10:17:53127 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result)
128 const WARN_UNUSED_RESULT = 0;
129
huangsdda11d062016-03-14 16:35:39130 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas,
131 std::vector<FileOffset>* file_offsets);
paulgazz@chromium.org39ed9732013-06-20 10:17:53132
huangsdda11d062016-03-14 16:35:39133 CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas);
paulgazz@chromium.org39ed9732013-06-20 10:17:53134
huangsdda11d062016-03-14 16:35:39135 // Parsing code for Disassemble().
paulgazz@chromium.org39ed9732013-06-20 10:17:53136
huangsdda11d062016-03-14 16:35:39137 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header,
138 AssemblyProgram* program)
139 WARN_UNUSED_RESULT = 0;
paulgazz@chromium.org144c8e92013-07-23 21:18:19140
huangsdda11d062016-03-14 16:35:39141 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section)
142 WARN_UNUSED_RESULT = 0;
paulgazz@chromium.org39ed9732013-06-20 10:17:53143
144 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT;
huangsdda11d062016-03-14 16:35:39145
paulgazz@chromium.org39ed9732013-06-20 10:17:53146 CheckBool ParseProgbitsSection(
huangsdda11d062016-03-14 16:35:39147 const Elf32_Shdr* section_header,
148 std::vector<FileOffset>::iterator* current_abs_offset,
149 std::vector<FileOffset>::iterator end_abs_offset,
paulgazz@chromium.org144c8e92013-07-23 21:18:19150 ScopedVector<TypedRVA>::iterator* current_rel,
151 ScopedVector<TypedRVA>::iterator end_rel,
paulgazz@chromium.org39ed9732013-06-20 10:17:53152 AssemblyProgram* program) WARN_UNUSED_RESULT;
huangsdda11d062016-03-14 16:35:39153
154 CheckBool ParseSimpleRegion(FileOffset start_file_offset,
155 FileOffset end_file_offset,
paulgazz@chromium.org39ed9732013-06-20 10:17:53156 AssemblyProgram* program) WARN_UNUSED_RESULT;
157
158 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT;
huangs58b822d42016-03-12 20:56:11159
huangsdda11d062016-03-14 16:35:39160 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.org39ed9732013-06-20 10:17:53166 Elf32_Half section_header_table_size_;
167
huangsdda11d062016-03-14 16:35:39168 const Elf32_Phdr* program_header_table_;
paulgazz@chromium.org39ed9732013-06-20 10:17:53169 Elf32_Half program_header_table_size_;
170
171 // Section header for default
huangsdda11d062016-03-14 16:35:39172 const char* default_string_section_;
paulgazz@chromium.org39ed9732013-06-20 10:17:53173
174 std::vector<RVA> abs32_locations_;
paulgazz@chromium.org144c8e92013-07-23 21:18:19175 ScopedVector<TypedRVA> rel32_locations_;
paulgazz@chromium.org39ed9732013-06-20 10:17:53176
177 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32);
178};
179
180} // namespace courgette
181
182#endif // COURGETTE_DISASSEMBLER_ELF_32_H_