1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/disassembler_elf_32_x86.h"
11 #include "base/logging.h"
12 #include "courgette/assembly_program.h"
13 #include "courgette/courgette.h"
17 CheckBool DisassemblerElf32X86::TypedRVAX86::ComputeRelativeTarget(
18 const uint8_t* op_pointer) {
19 set_relative_target(Read32LittleEndian(op_pointer) + 4);
23 CheckBool DisassemblerElf32X86::TypedRVAX86::EmitInstruction(
25 InstructionReceptor* receptor) {
26 return receptor->EmitRel32(label);
29 uint16_t DisassemblerElf32X86::TypedRVAX86::op_size() const {
33 DisassemblerElf32X86::DisassemblerElf32X86(const uint8_t* start, size_t length)
34 : DisassemblerElf32(start, length) {}
36 // Convert an ELF relocation struction into an RVA.
37 CheckBool DisassemblerElf32X86::RelToRVA(Elf32_Rel rel, RVA* result) const {
38 // The rightmost byte of r_info is the type.
39 elf32_rel_386_type_values type =
40 static_cast<elf32_rel_386_type_values>(rel.r_info & 0xFF);
42 // The other 3 bytes of r_info are the symbol.
43 uint32_t symbol = rel.r_info >> 8;
60 // This is a basic ABS32 relocation address.
61 *result = rel.r_offset;
73 CheckBool DisassemblerElf32X86::ParseRelocationSection(
74 const Elf32_Shdr* section_header,
75 InstructionReceptor* receptor) const {
76 // We can reproduce the R_386_RELATIVE entries in one of the relocation table
77 // based on other information in the patch, given these conditions:
79 // All R_386_RELATIVE entries are:
80 // 1) In the same relocation table
82 // 3) Are sorted in memory address order
84 // Happily, this is normally the case, but it's not required by spec, so we
85 // check, and just don't do it if we don't match up.
87 // The expectation is that one relocation section will contain all of our
88 // R_386_RELATIVE entries in the expected order followed by assorted other
89 // entries we can't use special handling for.
93 // Walk all the bytes in the section, matching relocation table or not.
94 FileOffset file_offset = section_header->sh_offset;
95 FileOffset section_end = file_offset + section_header->sh_size;
97 const Elf32_Rel* section_relocs_iter = reinterpret_cast<const Elf32_Rel*>(
98 FileOffsetToPointer(section_header->sh_offset));
100 uint32_t section_relocs_count =
101 section_header->sh_size / section_header->sh_entsize;
103 if (abs32_locations_.empty())
106 if (abs32_locations_.size() > section_relocs_count)
109 std::vector<RVA>::const_iterator reloc_iter = abs32_locations_.begin();
111 while (match && (reloc_iter != abs32_locations_.end())) {
112 if (section_relocs_iter->r_info != R_386_RELATIVE ||
113 section_relocs_iter->r_offset != *reloc_iter) {
116 ++section_relocs_iter;
121 // Skip over relocation tables.
122 if (!receptor->EmitElfRelocation())
124 file_offset += sizeof(Elf32_Rel) * abs32_locations_.size();
127 return ParseSimpleRegion(file_offset, section_end, receptor);
130 CheckBool DisassemblerElf32X86::ParseRel32RelocsFromSection(
131 const Elf32_Shdr* section_header) {
132 FileOffset start_file_offset = section_header->sh_offset;
133 FileOffset end_file_offset = start_file_offset + section_header->sh_size;
135 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
136 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
138 // Quick way to convert from Pointer to RVA within a single Section is to
139 // subtract |pointer_to_rva|.
140 const uint8_t* const adjust_pointer_to_rva =
141 start_pointer - section_header->sh_addr;
143 std::vector<RVA>::iterator abs32_pos = abs32_locations_.begin();
145 // Find the rel32 relocations.
146 const uint8_t* p = start_pointer;
147 while (p < end_pointer) {
148 // Heuristic discovery of rel32 locations in instruction stream: are the
149 // next few bytes the start of an instruction containing a rel32
151 const uint8_t* rel32 = nullptr;
153 if (p + 5 <= end_pointer) {
154 if (*p == 0xE8 || *p == 0xE9) { // jmp rel32 and call rel32
158 if (p + 6 <= end_pointer) {
159 if (*p == 0x0F && (p[1] & 0xF0) == 0x80) { // Jcc long form
160 if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely
165 RVA rel32_rva = static_cast<RVA>(rel32 - adjust_pointer_to_rva);
166 // Is there an abs32 reloc overlapping the candidate?
167 while (abs32_pos != abs32_locations_.end() && *abs32_pos < rel32_rva - 3)
169 // Now: (*abs32_pos > rel32_rva - 4) i.e. the lowest addressed 4-byte
170 // region that could overlap rel32_rva.
171 if (abs32_pos != abs32_locations_.end()) {
172 if (*abs32_pos < rel32_rva + 4) {
173 // Beginning of abs32 reloc is before end of rel32 reloc so they
174 // overlap. Skip four bytes past the abs32 reloc.
175 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva);
176 p += (*abs32_pos + 4) - current_rva;
181 std::unique_ptr<TypedRVAX86> typed_rel32_rva(new TypedRVAX86(rel32_rva));
182 if (!typed_rel32_rva->ComputeRelativeTarget(rel32))
185 RVA target_rva = typed_rel32_rva->rva() +
186 typed_rel32_rva->relative_target();
187 if (IsValidTargetRVA(target_rva)) {
188 rel32_locations_.push_back(std::move(typed_rel32_rva));
189 #if COURGETTE_HISTOGRAM_TARGETS
190 ++rel32_target_rvas_[target_rva];
202 } // namespace courgette