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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « courgette/disassembler.cc ('k') | courgette/disassembler_elf_32.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: courgette/disassembler_elf_32.h
diff --git a/courgette/disassembler_elf_32.h b/courgette/disassembler_elf_32.h
index e9d00cae5466cac3c1100aee5196c98ab21f218e..8483ce3b54903a1b35f2839a796cf5b0e573feec 100644
--- a/courgette/disassembler_elf_32.h
+++ b/courgette/disassembler_elf_32.h
@@ -8,12 +8,10 @@
#include <stddef.h>
#include <stdint.h>
-#include <vector>
-
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
+#include "courgette/assembly_program.h"
#include "courgette/disassembler.h"
-#include "courgette/image_utils.h"
#include "courgette/memory_allocator.h"
#include "courgette/types_elf.h"
@@ -21,33 +19,43 @@
class AssemblyProgram;
-// A Courgette disassembler for 32-bit ELF files. This is only a partial
-// implementation that admits subclasses for the architecture-specific parts of
-// 32-bit ELF file processing. Specifically:
-// - RelToRVA() processes entries in ELF relocation table.
-// - ParseRelocationSection() verifies the organization of the ELF relocation
-// table.
-// - ParseRel32RelocsFromSection() finds branch targets by looking for relative
-// branch/call opcodes in the particular architecture's machine code.
+// A courgette disassembler for 32-bit ELF files. This class is only a
+// partial implementation. Subclasses implement the
+// architecture-specific parts of processing 32-bit ELF files. Specifically,
+// RelToRVA processes entries in ELF relocation table,
+// ParseRelocationSection verifies the organization of the ELF
+// relocation table, and ParseRel32RelocsFromSection finds branch
+// targets by looking for relative jump/call opcodes in the particular
+// architecture's machine code.
class DisassemblerElf32 : public Disassembler {
public:
// Different instructions encode the target rva differently. This
// class encapsulates this behavior. public for use in unit tests.
class TypedRVA {
public:
- explicit TypedRVA(RVA rva) : rva_(rva) { }
+ explicit TypedRVA(RVA rva) : rva_(rva), offset_(static_cast<size_t>(-1)) {
+ }
- virtual ~TypedRVA() { }
+ virtual ~TypedRVA() { };
- RVA rva() const { return rva_; }
- RVA relative_target() const { return relative_target_; }
- FileOffset file_offset() const { return file_offset_; }
+ RVA rva() {
+ return rva_;
+ }
+
+ RVA relative_target() {
+ return relative_target_;
+ }
void set_relative_target(RVA relative_target) {
relative_target_ = relative_target;
}
- void set_file_offset(FileOffset file_offset) {
- file_offset_ = file_offset;
+
+ size_t get_offset() {
+ return offset_;
+ }
+
+ void set_offset(size_t offset) {
+ offset_ = offset;
}
// Computes the relative jump's offset from the op in p.
@@ -57,33 +65,33 @@
virtual CheckBool EmitInstruction(AssemblyProgram* program,
RVA target_rva) = 0;
- // Returns the size of the instruction containing the RVA.
virtual uint16_t op_size() const = 0;
- // Comparator for sorting, which assumes uniqueness of RVAs.
- static bool IsLessThan(TypedRVA* a, TypedRVA* b) {
+ static bool IsLessThan(TypedRVA *a, TypedRVA *b) {
return a->rva() < b->rva();
}
private:
const RVA rva_;
- RVA relative_target_ = kNoRVA;
- FileOffset file_offset_ = kNoFileOffset;
+ RVA relative_target_;
+ size_t offset_;
};
public:
- DisassemblerElf32(const void* start, size_t length);
+ explicit DisassemblerElf32(const void* start, size_t length);
- ~DisassemblerElf32() override { }
+ virtual ~DisassemblerElf32() { };
- // Disassembler interfaces.
- RVA FileOffsetToRVA(FileOffset file_offset) const override;
- FileOffset RVAToFileOffset(RVA rva) const override;
- virtual ExecutableType kind() const override = 0;
- bool ParseHeader() override;
- bool Disassemble(AssemblyProgram* target) override;
+ virtual ExecutableType kind() = 0;
- virtual e_machine_values ElfEM() const = 0;
+ virtual e_machine_values ElfEM() = 0;
+
+ // Returns 'true' if the buffer appears to point to a valid ELF executable
+ // for 32 bit. If ParseHeader() succeeds, other member
+ // functions may be called.
+ virtual bool ParseHeader();
+
+ virtual bool Disassemble(AssemblyProgram* target);
// Public for unittests only
std::vector<RVA> &Abs32Locations() { return abs32_locations_; }
@@ -99,13 +107,13 @@
return section_header_table_size_;
}
- const Elf32_Shdr* SectionHeader(Elf32_Half id) const {
+ const Elf32_Shdr *SectionHeader(int id) const {
assert(id >= 0 && id < SectionHeaderCount());
return section_header_table_ + id;
}
- const uint8_t* SectionBody(Elf32_Half id) const {
- return FileOffsetToPointer(SectionHeader(id)->sh_offset);
+ const uint8_t* SectionBody(int id) const {
+ return OffsetToPointer(SectionHeader(id)->sh_offset);
}
// Misc Segment Helpers
@@ -114,62 +122,61 @@
return program_header_table_size_;
}
- const Elf32_Phdr* ProgramSegmentHeader(Elf32_Half id) const {
+ const Elf32_Phdr *ProgramSegmentHeader(int id) const {
assert(id >= 0 && id < ProgramSegmentHeaderCount());
return program_header_table_ + id;
}
// Misc address space helpers
- CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT;
+ CheckBool IsValidRVA(RVA rva) const WARN_UNUSED_RESULT;
- // Converts an ELF relocation instruction into an RVA.
+ // Convert an ELF relocation struction into an RVA
virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result)
const WARN_UNUSED_RESULT = 0;
- CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas,
- std::vector<FileOffset>* file_offsets);
+ // Returns kNoOffset if there is no file offset corresponding to 'rva'.
+ CheckBool RVAToFileOffset(RVA rva, size_t* result) const WARN_UNUSED_RESULT;
- CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas);
+ RVA FileOffsetToRVA(size_t offset) const WARN_UNUSED_RESULT;
- // Parsing code for Disassemble().
+ CheckBool RVAsToOffsets(std::vector<RVA>* rvas /*in*/,
+ std::vector<size_t>* offsets /*out*/);
- virtual CheckBool ParseRelocationSection(const Elf32_Shdr* section_header,
- AssemblyProgram* program)
- WARN_UNUSED_RESULT = 0;
+ CheckBool RVAsToOffsets(ScopedVector<TypedRVA>* rvas /*in and out*/);
- virtual CheckBool ParseRel32RelocsFromSection(const Elf32_Shdr* section)
- WARN_UNUSED_RESULT = 0;
+ // Parsing Code used to really implement Disassemble
CheckBool ParseFile(AssemblyProgram* target) WARN_UNUSED_RESULT;
-
+ virtual CheckBool ParseRelocationSection(
+ const Elf32_Shdr *section_header,
+ AssemblyProgram* program) WARN_UNUSED_RESULT = 0;
CheckBool ParseProgbitsSection(
- const Elf32_Shdr* section_header,
- std::vector<FileOffset>::iterator* current_abs_offset,
- std::vector<FileOffset>::iterator end_abs_offset,
+ const Elf32_Shdr *section_header,
+ std::vector<size_t>::iterator* current_abs_offset,
+ std::vector<size_t>::iterator end_abs_offset,
ScopedVector<TypedRVA>::iterator* current_rel,
ScopedVector<TypedRVA>::iterator end_rel,
AssemblyProgram* program) WARN_UNUSED_RESULT;
-
- CheckBool ParseSimpleRegion(FileOffset start_file_offset,
- FileOffset end_file_offset,
+ CheckBool ParseSimpleRegion(size_t start_file_offset,
+ size_t end_file_offset,
AssemblyProgram* program) WARN_UNUSED_RESULT;
CheckBool ParseAbs32Relocs() WARN_UNUSED_RESULT;
+ CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT;
+ CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT;
+ virtual CheckBool ParseRel32RelocsFromSection(
+ const Elf32_Shdr* section) WARN_UNUSED_RESULT = 0;
- CheckBool CheckSection(RVA rva) WARN_UNUSED_RESULT;
-
- CheckBool ParseRel32RelocsFromSections() WARN_UNUSED_RESULT;
-
- const Elf32_Ehdr* header_;
- const Elf32_Shdr* section_header_table_;
+ Elf32_Ehdr *header_;
+ Elf32_Shdr *section_header_table_;
Elf32_Half section_header_table_size_;
- const Elf32_Phdr* program_header_table_;
+ Elf32_Phdr *program_header_table_;
Elf32_Half program_header_table_size_;
// Section header for default
- const char* default_string_section_;
+ const char *default_string_section_;
std::vector<RVA> abs32_locations_;
ScopedVector<TypedRVA> rel32_locations_;
« 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