OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COURGETTE_DISASSEMBLER_ELF_32_H_ | 5 #ifndef COURGETTE_DISASSEMBLER_ELF_32_H_ |
6 #define COURGETTE_DISASSEMBLER_ELF_32_H_ | 6 #define COURGETTE_DISASSEMBLER_ELF_32_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
| 11 #include <memory> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/scoped_vector.h" | |
15 #include "courgette/disassembler.h" | 15 #include "courgette/disassembler.h" |
16 #include "courgette/image_utils.h" | 16 #include "courgette/image_utils.h" |
17 #include "courgette/memory_allocator.h" | 17 #include "courgette/memory_allocator.h" |
18 #include "courgette/types_elf.h" | 18 #include "courgette/types_elf.h" |
19 | 19 |
20 namespace courgette { | 20 namespace courgette { |
21 | 21 |
22 class AssemblyProgram; | 22 class AssemblyProgram; |
23 | 23 |
24 // A Courgette disassembler for 32-bit ELF files. This is only a partial | 24 // A Courgette disassembler for 32-bit ELF files. This is only a partial |
(...skipping 14 matching lines...) Expand all Loading... |
39 | 39 |
40 virtual ~TypedRVA() { } | 40 virtual ~TypedRVA() { } |
41 | 41 |
42 RVA rva() const { return rva_; } | 42 RVA rva() const { return rva_; } |
43 RVA relative_target() const { return relative_target_; } | 43 RVA relative_target() const { return relative_target_; } |
44 FileOffset file_offset() const { return file_offset_; } | 44 FileOffset file_offset() const { return file_offset_; } |
45 | 45 |
46 void set_relative_target(RVA relative_target) { | 46 void set_relative_target(RVA relative_target) { |
47 relative_target_ = relative_target; | 47 relative_target_ = relative_target; |
48 } | 48 } |
49 void set_file_offset(FileOffset file_offset) { | 49 void set_file_offset(FileOffset file_offset) { file_offset_ = file_offset; } |
50 file_offset_ = file_offset; | |
51 } | |
52 | 50 |
53 // Computes the relative jump's offset from the op in p. | 51 // Computes the relative jump's offset from the op in p. |
54 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; | 52 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; |
55 | 53 |
56 // Emits the courgette instruction corresponding to the RVA type. | 54 // Emits the courgette instruction corresponding to the RVA type. |
57 virtual CheckBool EmitInstruction(AssemblyProgram* program, | 55 virtual CheckBool EmitInstruction(AssemblyProgram* program, |
58 RVA target_rva) = 0; | 56 RVA target_rva) = 0; |
59 | 57 |
60 // Returns the size of the instruction containing the RVA. | 58 // Returns the size of the instruction containing the RVA. |
61 virtual uint16_t op_size() const = 0; | 59 virtual uint16_t op_size() const = 0; |
62 | 60 |
63 // Comparator for sorting, which assumes uniqueness of RVAs. | 61 // Comparator for sorting, which assumes uniqueness of RVAs. |
64 static bool IsLessThanByRVA(TypedRVA* a, TypedRVA* b) { | 62 static bool IsLessThanByRVA(const std::unique_ptr<TypedRVA>& a, |
| 63 const std::unique_ptr<TypedRVA>& b) { |
65 return a->rva() < b->rva(); | 64 return a->rva() < b->rva(); |
66 } | 65 } |
67 | 66 |
68 // Comparator for sorting, which assumes uniqueness of file offsets. | 67 // Comparator for sorting, which assumes uniqueness of file offsets. |
69 static bool IsLessThanByFileOffset(TypedRVA* a, TypedRVA* b) { | 68 static bool IsLessThanByFileOffset(const std::unique_ptr<TypedRVA>& a, |
| 69 const std::unique_ptr<TypedRVA>& b) { |
70 return a->file_offset() < b->file_offset(); | 70 return a->file_offset() < b->file_offset(); |
71 } | 71 } |
72 | 72 |
73 private: | 73 private: |
74 const RVA rva_; | 74 const RVA rva_; |
75 RVA relative_target_ = kNoRVA; | 75 RVA relative_target_ = kNoRVA; |
76 FileOffset file_offset_ = kNoFileOffset; | 76 FileOffset file_offset_ = kNoFileOffset; |
77 }; | 77 }; |
78 | 78 |
79 public: | 79 public: |
80 DisassemblerElf32(const void* start, size_t length); | 80 DisassemblerElf32(const void* start, size_t length); |
81 | 81 |
82 ~DisassemblerElf32() override { } | 82 ~DisassemblerElf32() override { } |
83 | 83 |
84 // Disassembler interfaces. | 84 // Disassembler interfaces. |
85 RVA FileOffsetToRVA(FileOffset file_offset) const override; | 85 RVA FileOffsetToRVA(FileOffset file_offset) const override; |
86 FileOffset RVAToFileOffset(RVA rva) const override; | 86 FileOffset RVAToFileOffset(RVA rva) const override; |
87 RVA PointerToTargetRVA(const uint8_t* p) const override; | 87 RVA PointerToTargetRVA(const uint8_t* p) const override; |
88 virtual ExecutableType kind() const override = 0; | 88 virtual ExecutableType kind() const override = 0; |
89 bool ParseHeader() override; | 89 bool ParseHeader() override; |
90 bool Disassemble(AssemblyProgram* target) override; | 90 bool Disassemble(AssemblyProgram* target) override; |
91 | 91 |
92 virtual e_machine_values ElfEM() const = 0; | 92 virtual e_machine_values ElfEM() const = 0; |
93 | 93 |
94 // Public for unittests only | 94 // Public for unittests only |
95 std::vector<RVA> &Abs32Locations() { return abs32_locations_; } | 95 std::vector<RVA>& Abs32Locations() { return abs32_locations_; } |
96 ScopedVector<TypedRVA> &Rel32Locations() { return rel32_locations_; } | 96 std::vector<std::unique_ptr<TypedRVA>>& Rel32Locations() { |
| 97 return rel32_locations_; |
| 98 } |
97 | 99 |
98 protected: | 100 protected: |
99 bool UpdateLength(); | 101 bool UpdateLength(); |
100 | 102 |
101 // Misc Section Helpers | 103 // Misc Section Helpers |
102 | 104 |
103 Elf32_Half SectionHeaderCount() const { | 105 Elf32_Half SectionHeaderCount() const { |
104 return section_header_table_size_; | 106 return section_header_table_size_; |
105 } | 107 } |
106 | 108 |
(...skipping 25 matching lines...) Expand all Loading... |
132 | 134 |
133 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; | 135 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT; |
134 | 136 |
135 // Converts an ELF relocation instruction into an RVA. | 137 // Converts an ELF relocation instruction into an RVA. |
136 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) | 138 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) |
137 const WARN_UNUSED_RESULT = 0; | 139 const WARN_UNUSED_RESULT = 0; |
138 | 140 |
139 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, | 141 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas, |
140 std::vector<FileOffset>* file_offsets); | 142 std::vector<FileOffset>* file_offsets); |
141 | 143 |
142 CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas); | 144 CheckBool RVAsToFileOffsets( |
| 145 std::vector<std::unique_ptr<TypedRVA>>* typed_rvas); |
143 | 146 |
144 // Parsing code for Disassemble(). | 147 // Parsing code for Disassemble(). |
145 | 148 |
146 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, | 149 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header, |
147 AssemblyProgram* program) | 150 AssemblyProgram* program) |
148 WARN_UNUSED_RESULT = 0; | 151 WARN_UNUSED_RESULT = 0; |
149 | 152 |
150 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) | 153 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section) |
151 WARN_UNUSED_RESULT = 0; | 154 WARN_UNUSED_RESULT = 0; |
152 | 155 |
153 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; | 156 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; |
154 | 157 |
155 CheckBool ParseProgbitsSection( | 158 CheckBool ParseProgbitsSection( |
156 const Elf32_Shdr* section_header, | 159 const Elf32_Shdr* section_header, |
157 std::vector<FileOffset>::iterator* current_abs_offset, | 160 std::vector<FileOffset>::iterator* current_abs_offset, |
158 std::vector<FileOffset>::iterator end_abs_offset, | 161 std::vector<FileOffset>::iterator end_abs_offset, |
159 ScopedVector<TypedRVA>::iterator* current_rel, | 162 std::vector<std::unique_ptr<TypedRVA>>::iterator* current_rel, |
160 ScopedVector<TypedRVA>::iterator end_rel, | 163 std::vector<std::unique_ptr<TypedRVA>>::iterator end_rel, |
161 AssemblyProgram* program) WARN_UNUSED_RESULT; | 164 AssemblyProgram* program) WARN_UNUSED_RESULT; |
162 | 165 |
163 CheckBool ParseSimpleRegion(FileOffset start_file_offset, | 166 CheckBool ParseSimpleRegion(FileOffset start_file_offset, |
164 FileOffset end_file_offset, | 167 FileOffset end_file_offset, |
165 AssemblyProgram* program) WARN_UNUSED_RESULT; | 168 AssemblyProgram* program) WARN_UNUSED_RESULT; |
166 | 169 |
167 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; | 170 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; |
168 | 171 |
169 CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT; | 172 CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT; |
170 | 173 |
(...skipping 11 matching lines...) Expand all Loading... |
182 std::vector<Elf32_Half> section_header_file_offset_order_; | 185 std::vector<Elf32_Half> section_header_file_offset_order_; |
183 | 186 |
184 const Elf32_Phdr* program_header_table_; | 187 const Elf32_Phdr* program_header_table_; |
185 Elf32_Half program_header_table_size_; | 188 Elf32_Half program_header_table_size_; |
186 | 189 |
187 // Pointer to string table containing section names. | 190 // Pointer to string table containing section names. |
188 const char* default_string_section_; | 191 const char* default_string_section_; |
189 size_t default_string_section_size_; | 192 size_t default_string_section_size_; |
190 | 193 |
191 std::vector<RVA> abs32_locations_; | 194 std::vector<RVA> abs32_locations_; |
192 ScopedVector<TypedRVA> rel32_locations_; | 195 std::vector<std::unique_ptr<TypedRVA>> rel32_locations_; |
193 | 196 |
194 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); | 197 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); |
195 }; | 198 }; |
196 | 199 |
197 } // namespace courgette | 200 } // namespace courgette |
198 | 201 |
199 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ | 202 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ |
OLD | NEW |