diff options
Diffstat (limited to 'courgette/disassembler_elf_32_x86.cc')
-rw-r--r-- | courgette/disassembler_elf_32_x86.cc | 111 |
1 files changed, 56 insertions, 55 deletions
diff --git a/courgette/disassembler_elf_32_x86.cc b/courgette/disassembler_elf_32_x86.cc index 98084c1..45f7cf6 100644 --- a/courgette/disassembler_elf_32_x86.cc +++ b/courgette/disassembler_elf_32_x86.cc @@ -4,37 +4,45 @@ #include "courgette/disassembler_elf_32_x86.h" -#include <stddef.h> -#include <stdint.h> - -#include <algorithm> -#include <string> #include <vector> #include "base/logging.h" - +#include "base/memory/scoped_ptr.h" #include "courgette/assembly_program.h" #include "courgette/courgette.h" -#include "courgette/encoded_program.h" namespace courgette { +CheckBool DisassemblerElf32X86::TypedRVAX86::ComputeRelativeTarget( + const uint8_t* op_pointer) { + set_relative_target(Read32LittleEndian(op_pointer) + 4); + return true; +} + +CheckBool DisassemblerElf32X86::TypedRVAX86::EmitInstruction( + AssemblyProgram* program, + RVA target_rva) { + return program->EmitRel32(program->FindOrMakeRel32Label(target_rva)); +} + +uint16_t DisassemblerElf32X86::TypedRVAX86::op_size() const { + return 4; +} + DisassemblerElf32X86::DisassemblerElf32X86(const void* start, size_t length) - : DisassemblerElf32(start, length) { + : DisassemblerElf32(start, length) { } -// Convert an ELF relocation struction into an RVA +// Convert an ELF relocation struction into an RVA. CheckBool DisassemblerElf32X86::RelToRVA(Elf32_Rel rel, RVA* result) const { - - // The rightmost byte of r_info is the type... + // The rightmost byte of r_info is the type. elf32_rel_386_type_values type = - (elf32_rel_386_type_values)(unsigned char)rel.r_info; + static_cast<elf32_rel_386_type_values>(rel.r_info & 0xFF); - // The other 3 bytes of r_info are the symbol + // The other 3 bytes of r_info are the symbol. uint32_t symbol = rel.r_info >> 8; - switch(type) - { + switch (type) { case R_386_NONE: case R_386_32: case R_386_PC32: @@ -49,7 +57,7 @@ CheckBool DisassemblerElf32X86::RelToRVA(Elf32_Rel rel, RVA* result) const { if (symbol != 0) return false; - // This is a basic ABS32 relocation address + // This is a basic ABS32 relocation address. *result = rel.r_offset; return true; @@ -63,32 +71,31 @@ CheckBool DisassemblerElf32X86::RelToRVA(Elf32_Rel rel, RVA* result) const { } CheckBool DisassemblerElf32X86::ParseRelocationSection( - const Elf32_Shdr *section_header, - AssemblyProgram* program) { - // We can reproduce the R_386_RELATIVE entries in one of the relocation - // table based on other information in the patch, given these - // conditions.... + const Elf32_Shdr* section_header, + AssemblyProgram* program) { + // We can reproduce the R_386_RELATIVE entries in one of the relocation table + // based on other information in the patch, given these conditions: // // All R_386_RELATIVE entries are: // 1) In the same relocation table // 2) Are consecutive // 3) Are sorted in memory address order // - // Happily, this is normally the case, but it's not required by spec - // so we check, and just don't do it if we don't match up. + // Happily, this is normally the case, but it's not required by spec, so we + // check, and just don't do it if we don't match up. - // The expectation is that one relocation section will contain - // all of our R_386_RELATIVE entries in the expected order followed - // by assorted other entries we can't use special handling for. + // The expectation is that one relocation section will contain all of our + // R_386_RELATIVE entries in the expected order followed by assorted other + // entries we can't use special handling for. bool match = true; - // Walk all the bytes in the section, matching relocation table or not - size_t file_offset = section_header->sh_offset; - size_t section_end = section_header->sh_offset + section_header->sh_size; + // Walk all the bytes in the section, matching relocation table or not. + FileOffset file_offset = section_header->sh_offset; + FileOffset section_end = file_offset + section_header->sh_size; - Elf32_Rel *section_relocs_iter = - (Elf32_Rel *)OffsetToPointer(section_header->sh_offset); + const Elf32_Rel* section_relocs_iter = reinterpret_cast<const Elf32_Rel*>( + FileOffsetToPointer(section_header->sh_offset)); uint32_t section_relocs_count = section_header->sh_size / section_header->sh_entsize; @@ -101,16 +108,17 @@ CheckBool DisassemblerElf32X86::ParseRelocationSection( std::vector<RVA>::iterator reloc_iter = abs32_locations_.begin(); - while (match && (reloc_iter != abs32_locations_.end())) { + while (match && (reloc_iter != abs32_locations_.end())) { if (section_relocs_iter->r_info != R_386_RELATIVE || - section_relocs_iter->r_offset != *reloc_iter) + section_relocs_iter->r_offset != *reloc_iter) { match = false; - section_relocs_iter++; - reloc_iter++; + } + ++section_relocs_iter; + ++reloc_iter; } if (match) { - // Skip over relocation tables + // Skip over relocation tables. if (!program->EmitElfRelocationInstruction()) return false; file_offset += sizeof(Elf32_Rel) * abs32_locations_.size(); @@ -119,28 +127,27 @@ CheckBool DisassemblerElf32X86::ParseRelocationSection( return ParseSimpleRegion(file_offset, section_end, program); } +// TODO(huangs): Detect and avoid overlap with abs32 addresses. CheckBool DisassemblerElf32X86::ParseRel32RelocsFromSection( const Elf32_Shdr* section_header) { - uint32_t start_file_offset = section_header->sh_offset; - uint32_t end_file_offset = start_file_offset + section_header->sh_size; + FileOffset start_file_offset = section_header->sh_offset; + FileOffset end_file_offset = start_file_offset + section_header->sh_size; - const uint8_t* start_pointer = OffsetToPointer(start_file_offset); - const uint8_t* end_pointer = OffsetToPointer(end_file_offset); + const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset); + const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset); // Quick way to convert from Pointer to RVA within a single Section is to - // subtract 'pointer_to_rva'. + // subtract |pointer_to_rva|. const uint8_t* const adjust_pointer_to_rva = start_pointer - section_header->sh_addr; // Find the rel32 relocations. const uint8_t* p = start_pointer; while (p < end_pointer) { - //RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva); - // Heuristic discovery of rel32 locations in instruction stream: are the // next few bytes the start of an instruction containing a rel32 // addressing mode? - const uint8_t* rel32 = NULL; + const uint8_t* rel32 = nullptr; if (p + 5 <= end_pointer) { if (*p == 0xE8 || *p == 0xE9) { // jmp rel32 and call rel32 @@ -148,32 +155,26 @@ CheckBool DisassemblerElf32X86::ParseRel32RelocsFromSection( } } if (p + 6 <= end_pointer) { - if (*p == 0x0F && (*(p+1) & 0xF0) == 0x80) { // Jcc long form + if (*p == 0x0F && (p[1] & 0xF0) == 0x80) { // Jcc long form if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely rel32 = p + 2; } } if (rel32) { RVA rva = static_cast<RVA>(rel32 - adjust_pointer_to_rva); - TypedRVAX86* rel32_rva = new TypedRVAX86(rva); + scoped_ptr<TypedRVAX86> rel32_rva(new TypedRVAX86(rva)); - if (!rel32_rva->ComputeRelativeTarget(rel32)) { - delete rel32_rva; + if (!rel32_rva->ComputeRelativeTarget(rel32)) return false; - } RVA target_rva = rel32_rva->rva() + rel32_rva->relative_target(); - // To be valid, rel32 target must be within image, and within this - // section. - if (IsValidRVA(target_rva)) { - rel32_locations_.push_back(rel32_rva); + if (IsValidTargetRVA(target_rva)) { + rel32_locations_.push_back(rel32_rva.release()); #if COURGETTE_HISTOGRAM_TARGETS ++rel32_target_rvas_[target_rva]; #endif p = rel32 + 4; continue; - } else { - delete rel32_rva; } } p += 1; |