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

Side by Side Diff: courgette/disassembler_elf_32.h

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