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

Unified 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: Sync. Created 4 years, 10 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
Index: courgette/disassembler_elf_32.h
diff --git a/courgette/disassembler_elf_32.h b/courgette/disassembler_elf_32.h
index 8483ce3b54903a1b35f2839a796cf5b0e573feec..61c64e4fe923005535227a79e93c4cc2e5230ecf 100644
--- a/courgette/disassembler_elf_32.h
+++ b/courgette/disassembler_elf_32.h
@@ -8,10 +8,12 @@
#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"
@@ -19,43 +21,33 @@ namespace courgette {
class AssemblyProgram;
-// 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.
+// 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.
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), offset_(static_cast<size_t>(-1)) {
- }
-
- virtual ~TypedRVA() { };
+ explicit TypedRVA(RVA rva) : rva_(rva) { }
- RVA rva() {
- return rva_;
- }
+ virtual ~TypedRVA() { }
- RVA relative_target() {
- return relative_target_;
- }
+ RVA rva() const { return rva_; }
+ RVA relative_target() const { return relative_target_; }
+ FileOffset file_offset() const { return file_offset_; }
void set_relative_target(RVA relative_target) {
relative_target_ = relative_target;
}
-
- size_t get_offset() {
- return offset_;
- }
-
- void set_offset(size_t offset) {
- offset_ = offset;
+ void set_file_offset(FileOffset file_offset) {
+ file_offset_ = file_offset;
}
// Computes the relative jump's offset from the op in p.
@@ -65,33 +57,33 @@ class DisassemblerElf32 : public Disassembler {
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;
- static bool IsLessThan(TypedRVA *a, TypedRVA *b) {
+ // Comparator for sorting. Assumes uniqueness of rva.
+ static bool IsLessThan(TypedRVA* a, TypedRVA* b) {
return a->rva() < b->rva();
}
private:
const RVA rva_;
- RVA relative_target_;
- size_t offset_;
+ RVA relative_target_ = kNoRVA;
+ FileOffset file_offset_ = kNoFileOffset;
};
public:
- explicit DisassemblerElf32(const void* start, size_t length);
-
- virtual ~DisassemblerElf32() { };
+ DisassemblerElf32(const void* start, size_t length);
- virtual ExecutableType kind() = 0;
+ ~DisassemblerElf32() override { };
- virtual e_machine_values ElfEM() = 0;
+ // 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;
- // 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);
+ virtual e_machine_values ElfEM() const = 0;
// Public for unittests only
std::vector<RVA> &Abs32Locations() { return abs32_locations_; }
@@ -107,13 +99,13 @@ class DisassemblerElf32 : public Disassembler {
return section_header_table_size_;
}
- const Elf32_Shdr *SectionHeader(int id) const {
+ const Elf32_Shdr* SectionHeader(int id) const {
assert(id >= 0 && id < SectionHeaderCount());
return section_header_table_ + id;
}
const uint8_t* SectionBody(int id) const {
- return OffsetToPointer(SectionHeader(id)->sh_offset);
+ return FileOffsetToPointer(SectionHeader(id)->sh_offset);
}
// Misc Segment Helpers
@@ -122,61 +114,62 @@ class DisassemblerElf32 : public Disassembler {
return program_header_table_size_;
}
- const Elf32_Phdr *ProgramSegmentHeader(int id) const {
+ const Elf32_Phdr* ProgramSegmentHeader(int id) const {
assert(id >= 0 && id < ProgramSegmentHeaderCount());
return program_header_table_ + id;
}
// Misc address space helpers
- CheckBool IsValidRVA(RVA rva) const WARN_UNUSED_RESULT;
+ CheckBool IsValidTargetRVA(RVA rva) const WARN_UNUSED_RESULT;
// Convert an ELF relocation struction into an RVA
virtual CheckBool RelToRVA(Elf32_Rel rel, RVA* result)
const WARN_UNUSED_RESULT = 0;
- // Returns kNoOffset if there is no file offset corresponding to 'rva'.
- CheckBool RVAToFileOffset(RVA rva, size_t* result) const WARN_UNUSED_RESULT;
+ CheckBool RVAsToFileOffsets(const std::vector<RVA>& rvas,
+ std::vector<FileOffset>* file_offsets);
- RVA FileOffsetToRVA(size_t offset) const WARN_UNUSED_RESULT;
+ CheckBool RVAsToFileOffsets(ScopedVector<TypedRVA>* typed_rvas);
- CheckBool RVAsToOffsets(std::vector<RVA>* rvas /*in*/,
- std::vector<size_t>* offsets /*out*/);
+ // Parsing code for Disassemble().
- CheckBool RVAsToOffsets(ScopedVector<TypedRVA>* rvas /*in and out*/);
+ virtual CheckBool ParseRelocationSection(
+ const Elf32_Shdr* section_header,
+ AssemblyProgram* program) WARN_UNUSED_RESULT = 0;
- // Parsing Code used to really implement Disassemble
+ virtual CheckBool ParseRel32RelocsFromSection(
+ const Elf32_Shdr* section) WARN_UNUSED_RESULT = 0;
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<size_t>::iterator* current_abs_offset,
- std::vector<size_t>::iterator end_abs_offset,
+ const Elf32_Shdr* section_header,
+ std::vector<FileOffset>::iterator* current_abs_offset,
+ std::vector<FileOffset>::iterator end_abs_offset,
ScopedVector<TypedRVA>::iterator* current_rel,
ScopedVector<TypedRVA>::iterator end_rel,
AssemblyProgram* program) WARN_UNUSED_RESULT;
- CheckBool ParseSimpleRegion(size_t start_file_offset,
- size_t end_file_offset,
+
+ CheckBool ParseSimpleRegion(FileOffset start_file_offset,
+ FileOffset 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;
- Elf32_Ehdr *header_;
- Elf32_Shdr *section_header_table_;
+ const Elf32_Ehdr* header_;
+ const Elf32_Shdr* section_header_table_;
Elf32_Half section_header_table_size_;
- Elf32_Phdr *program_header_table_;
+ const 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_;

Powered by Google App Engine
This is Rietveld 408576698