Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: courgette/disassembler_elf_32.h

Issue 1676683002: [Courgette] Clean up Disassembler; fix ELF Memory leaks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Wrap #include <iostream> under #if COURGETTE_HISTOGRAM_TARGETS. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « courgette/disassembler.cc ('k') | courgette/disassembler_elf_32.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <vector>
12
11 #include "base/macros.h" 13 #include "base/macros.h"
12 #include "base/memory/scoped_vector.h" 14 #include "base/memory/scoped_vector.h"
13 #include "courgette/assembly_program.h"
14 #include "courgette/disassembler.h" 15 #include "courgette/disassembler.h"
16 #include "courgette/image_utils.h"
15 #include "courgette/memory_allocator.h" 17 #include "courgette/memory_allocator.h"
16 #include "courgette/types_elf.h" 18 #include "courgette/types_elf.h"
17 19
18 namespace courgette { 20 namespace courgette {
19 21
20 class AssemblyProgram; 22 class AssemblyProgram;
21 23
22 // A courgette disassembler for 32-bit ELF files. This class is only a 24 // A Courgette disassembler for 32-bit ELF files. This is only a partial
23 // partial implementation. Subclasses implement the 25 // implementation that admits subclasses for the architecture-specific parts of
24 // architecture-specific parts of processing 32-bit ELF files. Specifically, 26 // 32-bit ELF file processing. Specifically:
25 // RelToRVA processes entries in ELF relocation table, 27 // - RelToRVA() processes entries in ELF relocation table.
26 // ParseRelocationSection verifies the organization of the ELF 28 // - ParseRelocationSection() verifies the organization of the ELF relocation
27 // relocation table, and ParseRel32RelocsFromSection finds branch 29 // table.
28 // targets by looking for relative jump/call opcodes in the particular 30 // - ParseRel32RelocsFromSection() finds branch targets by looking for relative
29 // architecture's machine code. 31 // branch/call opcodes in the particular architecture's machine code.
30 class DisassemblerElf32 : public Disassembler { 32 class DisassemblerElf32 : public Disassembler {
31 public: 33 public:
32 // Different instructions encode the target rva differently. This 34 // Different instructions encode the target rva differently. This
33 // class encapsulates this behavior. public for use in unit tests. 35 // class encapsulates this behavior. public for use in unit tests.
34 class TypedRVA { 36 class TypedRVA {
35 public: 37 public:
36 explicit TypedRVA(RVA rva) : rva_(rva), offset_(static_cast<size_t>(-1)) { 38 explicit TypedRVA(RVA rva) : rva_(rva) { }
37 }
38 39
39 virtual ~TypedRVA() { }; 40 virtual ~TypedRVA() { }
40 41
41 RVA rva() { 42 RVA rva() const { return rva_; }
42 return rva_; 43 RVA relative_target() const { return relative_target_; }
43 } 44 FileOffset file_offset() const { return file_offset_; }
44
45 RVA relative_target() {
46 return relative_target_;
47 }
48 45
49 void set_relative_target(RVA relative_target) { 46 void set_relative_target(RVA relative_target) {
50 relative_target_ = relative_target; 47 relative_target_ = relative_target;
51 } 48 }
52 49 void set_file_offset(FileOffset file_offset) {
53 size_t get_offset() { 50 file_offset_ = file_offset;
54 return offset_;
55 }
56
57 void set_offset(size_t offset) {
58 offset_ = offset;
59 } 51 }
60 52
61 // Computes the relative jump's offset from the op in p. 53 // Computes the relative jump's offset from the op in p.
62 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0; 54 virtual CheckBool ComputeRelativeTarget(const uint8_t* op_pointer) = 0;
63 55
64 // Emits the courgette instruction corresponding to the RVA type. 56 // Emits the courgette instruction corresponding to the RVA type.
65 virtual CheckBool EmitInstruction(AssemblyProgram* program, 57 virtual CheckBool EmitInstruction(AssemblyProgram* program,
66 RVA target_rva) = 0; 58 RVA target_rva) = 0;
67 59
60 // Returns the size of the instruction containing the RVA.
68 virtual uint16_t op_size() const = 0; 61 virtual uint16_t op_size() const = 0;
69 62
70 static bool IsLessThan(TypedRVA *a, TypedRVA *b) { 63 // Comparator for sorting, which assumes uniqueness of RVAs.
64 static bool IsLessThan(TypedRVA* a, TypedRVA* b) {
71 return a->rva() < b->rva(); 65 return a->rva() < b->rva();
72 } 66 }
73 67
74 private: 68 private:
75 const RVA rva_; 69 const RVA rva_;
76 RVA relative_target_; 70 RVA relative_target_ = kNoRVA;
77 size_t offset_; 71 FileOffset file_offset_ = kNoFileOffset;
78 }; 72 };
79 73
80 public: 74 public:
81 explicit DisassemblerElf32(const void* start, size_t length); 75 DisassemblerElf32(const void* start, size_t length);
82 76
83 virtual ~DisassemblerElf32() { }; 77 ~DisassemblerElf32() override { }
84 78
85 virtual ExecutableType kind() = 0; 79 // Disassembler interfaces.
80 RVA FileOffsetToRVA(FileOffset file_offset) const override;
81 FileOffset RVAToFileOffset(RVA rva) const override;
82 virtual ExecutableType kind() const override = 0;
83 bool ParseHeader() override;
84 bool Disassemble(AssemblyProgram* target) override;
86 85
87 virtual e_machine_values ElfEM() = 0; 86 virtual e_machine_values ElfEM() const = 0;
88
89 // Returns 'true' if the buffer appears to point to a valid ELF executable
90 // for 32 bit. If ParseHeader() succeeds, other member
91 // functions may be called.
92 virtual bool ParseHeader();
93
94 virtual bool Disassemble(AssemblyProgram* target);
95 87
96 // Public for unittests only 88 // Public for unittests only
97 std::vector<RVA> &Abs32Locations() { return abs32_locations_; } 89 std::vector<RVA> &Abs32Locations() { return abs32_locations_; }
98 ScopedVector<TypedRVA> &Rel32Locations() { return rel32_locations_; } 90 ScopedVector<TypedRVA> &Rel32Locations() { return rel32_locations_; }
99 91
100 protected: 92 protected:
101 93
102 bool UpdateLength(); 94 bool UpdateLength();
103 95
104 // Misc Section Helpers 96 // Misc Section Helpers
105 97
106 Elf32_Half SectionHeaderCount() const { 98 Elf32_Half SectionHeaderCount() const {
107 return section_header_table_size_; 99 return section_header_table_size_;
108 } 100 }
109 101
110 const Elf32_Shdr *SectionHeader(int id) const { 102 const Elf32_Shdr* SectionHeader(Elf32_Half id) const {
111 assert(id >= 0 && id < SectionHeaderCount()); 103 assert(id >= 0 && id < SectionHeaderCount());
112 return section_header_table_ + id; 104 return section_header_table_ + id;
113 } 105 }
114 106
115 const uint8_t* SectionBody(int id) const { 107 const uint8_t* SectionBody(Elf32_Half id) const {
116 return OffsetToPointer(SectionHeader(id)->sh_offset); 108 return FileOffsetToPointer(SectionHeader(id)->sh_offset);
117 } 109 }
118 110
119 // Misc Segment Helpers 111 // Misc Segment Helpers
120 112
121 Elf32_Half ProgramSegmentHeaderCount() const { 113 Elf32_Half ProgramSegmentHeaderCount() const {
122 return program_header_table_size_; 114 return program_header_table_size_;
123 } 115 }
124 116
125 const Elf32_Phdr *ProgramSegmentHeader(int id) const { 117 const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const {
126 assert(id >= 0 && id < ProgramSegmentHeaderCount()); 118 assert(id >= 0 && id < ProgramSegmentHeaderCount());
127 return program_header_table_ + id; 119 return program_header_table_ + id;
128 } 120 }
129 121
130 // Misc address space helpers 122 // Misc address space helpers
131 123
132 CheckBool IsValidRVA(RVA rva) const WARN_UNUSED_RESULT; 124 CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT;
133 125
134 // Convert an ELF relocation struction into an RVA 126 // Converts an ELF relocation instruction into an RVA.
135 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result) 127 virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result)
136 const WARN_UNUSED_RESULT = 0; 128 const WARN_UNUSED_RESULT = 0;
137 129
138 // Returns kNoOffset if there is no file offset corresponding to 'rva'. 130 CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas,
139 CheckBool RVAToFileOffset(RVA rva, size_t* result) const WARN_UNUSED_RESULT; 131 std::vector<FileOffset>* file_offsets);
140 132
141 RVA FileOffsetToRVA(size_t offset) const WARN_UNUSED_RESULT; 133 CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas);
142 134
143 CheckBool RVAsToOffsets(std::vector<RVA>* rvas /*in*/, 135 // Parsing code for Disassemble().
144 std::vector<size_t>* offsets /*out*/);
145 136
146 CheckBool RVAsToOffsets(ScopedVector<TypedRVA>* rvas /*in and out*/); 137 virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header,
138 AssemblyProgram* program)
139 WARN_UNUSED_RESULT = 0;
147 140
148 // Parsing Code used to really implement Disassemble 141 virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section)
142 WARN_UNUSED_RESULT = 0;
149 143
150 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT; 144 CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT;
151 virtual CheckBool ParseRelocationSection( 145
152 const Elf32_Shdr *section_header,
153 AssemblyProgram* program) WARN_UNUSED_RESULT = 0;
154 CheckBool ParseProgbitsSection( 146 CheckBool ParseProgbitsSection(
155 const Elf32_Shdr *section_header, 147 const Elf32_Shdr* section_header,
156 std::vector<size_t>::iterator* current_abs_offset, 148 std::vector<FileOffset>::iterator* current_abs_offset,
157 std::vector<size_t>::iterator end_abs_offset, 149 std::vector<FileOffset>::iterator end_abs_offset,
158 ScopedVector<TypedRVA>::iterator* current_rel, 150 ScopedVector<TypedRVA>::iterator* current_rel,
159 ScopedVector<TypedRVA>::iterator end_rel, 151 ScopedVector<TypedRVA>::iterator end_rel,
160 AssemblyProgram* program) WARN_UNUSED_RESULT; 152 AssemblyProgram* program) WARN_UNUSED_RESULT;
161 CheckBool ParseSimpleRegion(size_t start_file_offset, 153
162 size_t end_file_offset, 154 CheckBool ParseSimpleRegion(FileOffset start_file_offset,
155 FileOffset end_file_offset,
163 AssemblyProgram* program) WARN_UNUSED_RESULT; 156 AssemblyProgram* program) WARN_UNUSED_RESULT;
164 157
165 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT; 158 CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT;
159
166 CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT; 160 CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT;
161
167 CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT; 162 CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT;
168 virtual CheckBool ParseRel32RelocsFromSection(
169 const Elf32_Shdr* section) WARN_UNUSED_RESULT = 0;
170 163
171 Elf32_Ehdr *header_; 164 const Elf32_Ehdr* header_;
172 Elf32_Shdr *section_header_table_; 165 const Elf32_Shdr* section_header_table_;
173 Elf32_Half section_header_table_size_; 166 Elf32_Half section_header_table_size_;
174 167
175 Elf32_Phdr *program_header_table_; 168 const Elf32_Phdr* program_header_table_;
176 Elf32_Half program_header_table_size_; 169 Elf32_Half program_header_table_size_;
177 170
178 // Section header for default 171 // Section header for default
179 const char *default_string_section_; 172 const char* default_string_section_;
180 173
181 std::vector<RVA> abs32_locations_; 174 std::vector<RVA> abs32_locations_;
182 ScopedVector<TypedRVA> rel32_locations_; 175 ScopedVector<TypedRVA> rel32_locations_;
183 176
184 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32); 177 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32);
185 }; 178 };
186 179
187 } // namespace courgette 180 } // namespace courgette
188 181
189 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_ 182 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_
OLDNEW
« no previous file with comments | « courgette/disassembler.cc ('k') | courgette/disassembler_elf_32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698