blob: 45f7cf6d78d7cc3eb1a0b51774aa0188a468ad79 [file] [log] [blame]
[email protected]c23654ea2012-07-26 18:34:241// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4b3d192b2011-11-08 20:32:262// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "courgette/disassembler_elf_32_x86.h"
6
[email protected]4b3d192b2011-11-08 20:32:267#include <vector>
8
[email protected]4b3d192b2011-11-08 20:32:269#include "base/logging.h"
huangsdda11d062016-03-14 16:35:3910#include "base/memory/scoped_ptr.h"
[email protected]4b3d192b2011-11-08 20:32:2611#include "courgette/assembly_program.h"
12#include "courgette/courgette.h"
[email protected]4b3d192b2011-11-08 20:32:2613
14namespace courgette {
15
huangsdda11d062016-03-14 16:35:3916CheckBool DisassemblerElf32X86::TypedRVAX86::ComputeRelativeTarget(
17 const uint8_t* op_pointer) {
18 set_relative_target(Read32LittleEndian(op_pointer) + 4);
19 return true;
huangs58b822d42016-03-12 20:56:1120}
21
huangsdda11d062016-03-14 16:35:3922CheckBool DisassemblerElf32X86::TypedRVAX86::EmitInstruction(
23 AssemblyProgram* program,
24 RVA target_rva) {
25 return program->EmitRel32(program->FindOrMakeRel32Label(target_rva));
26}
27
28uint16_t DisassemblerElf32X86::TypedRVAX86::op_size() const {
29 return 4;
30}
31
32DisassemblerElf32X86::DisassemblerElf32X86(const void* start, size_t length)
33 : DisassemblerElf32(start, length) {
34}
35
36// Convert an ELF relocation struction into an RVA.
[email protected]4b3d192b2011-11-08 20:32:2637CheckBool DisassemblerElf32X86::RelToRVA(Elf32_Rel rel, RVA* result) const {
huangsdda11d062016-03-14 16:35:3938 // The rightmost byte of r_info is the type.
scottmg4a95ca52016-03-12 23:54:5639 elf32_rel_386_type_values type =
huangsdda11d062016-03-14 16:35:3940 static_cast<elf32_rel_386_type_values>(rel.r_info & 0xFF);
scottmg4a95ca52016-03-12 23:54:5641
huangsdda11d062016-03-14 16:35:3942 // The other 3 bytes of r_info are the symbol.
aviab98dcc92015-12-21 19:35:3343 uint32_t symbol = rel.r_info >> 8;
[email protected]4b3d192b2011-11-08 20:32:2644
huangsdda11d062016-03-14 16:35:3945 switch (type) {
[email protected]4b3d192b2011-11-08 20:32:2646 case R_386_NONE:
47 case R_386_32:
48 case R_386_PC32:
49 case R_386_GOT32:
50 case R_386_PLT32:
51 case R_386_COPY:
52 case R_386_GLOB_DAT:
53 case R_386_JMP_SLOT:
54 return false;
55
56 case R_386_RELATIVE:
57 if (symbol != 0)
58 return false;
59
huangsdda11d062016-03-14 16:35:3960 // This is a basic ABS32 relocation address.
[email protected]4b3d192b2011-11-08 20:32:2661 *result = rel.r_offset;
62 return true;
63
64 case R_386_GOTOFF:
65 case R_386_GOTPC:
66 case R_386_TLS_TPOFF:
67 return false;
68 }
69
70 return false;
71}
72
[email protected]4b3d192b2011-11-08 20:32:2673CheckBool DisassemblerElf32X86::ParseRelocationSection(
huangsdda11d062016-03-14 16:35:3974 const Elf32_Shdr* section_header,
75 AssemblyProgram* program) {
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:
[email protected]4b3d192b2011-11-08 20:32:2678 //
79 // All R_386_RELATIVE entries are:
80 // 1) In the same relocation table
81 // 2) Are consecutive
82 // 3) Are sorted in memory address order
83 //
huangsdda11d062016-03-14 16:35:3984 // 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.
[email protected]4b3d192b2011-11-08 20:32:2686
huangsdda11d062016-03-14 16:35:3987 // 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.
[email protected]4b3d192b2011-11-08 20:32:2690
[email protected]4b3d192b2011-11-08 20:32:2691 bool match = true;
92
huangsdda11d062016-03-14 16:35:3993 // 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;
[email protected]4b3d192b2011-11-08 20:32:2696
huangsdda11d062016-03-14 16:35:3997 const Elf32_Rel* section_relocs_iter = reinterpret_cast<const Elf32_Rel*>(
98 FileOffsetToPointer(section_header->sh_offset));
[email protected]4b3d192b2011-11-08 20:32:2699
aviab98dcc92015-12-21 19:35:33100 uint32_t section_relocs_count =
101 section_header->sh_size / section_header->sh_entsize;
[email protected]4b3d192b2011-11-08 20:32:26102
Will Harris3e6fa972015-03-02 21:14:25103 if (abs32_locations_.empty())
104 match = false;
105
[email protected]4b3d192b2011-11-08 20:32:26106 if (abs32_locations_.size() > section_relocs_count)
107 match = false;
108
109 std::vector<RVA>::iterator reloc_iter = abs32_locations_.begin();
110
huangsdda11d062016-03-14 16:35:39111 while (match && (reloc_iter != abs32_locations_.end())) {
[email protected]4b3d192b2011-11-08 20:32:26112 if (section_relocs_iter->r_info != R_386_RELATIVE ||
huangsdda11d062016-03-14 16:35:39113 section_relocs_iter->r_offset != *reloc_iter) {
[email protected]4b3d192b2011-11-08 20:32:26114 match = false;
huangsdda11d062016-03-14 16:35:39115 }
116 ++section_relocs_iter;
117 ++reloc_iter;
[email protected]4b3d192b2011-11-08 20:32:26118 }
119
120 if (match) {
huangsdda11d062016-03-14 16:35:39121 // Skip over relocation tables.
[email protected]811ccb22011-11-09 00:09:27122 if (!program->EmitElfRelocationInstruction())
123 return false;
[email protected]4b3d192b2011-11-08 20:32:26124 file_offset += sizeof(Elf32_Rel) * abs32_locations_.size();
125 }
126
[email protected]811ccb22011-11-09 00:09:27127 return ParseSimpleRegion(file_offset, section_end, program);
[email protected]4b3d192b2011-11-08 20:32:26128}
129
huangsdda11d062016-03-14 16:35:39130// TODO(huangs): Detect and avoid overlap with abs32 addresses.
[email protected]4b3d192b2011-11-08 20:32:26131CheckBool DisassemblerElf32X86::ParseRel32RelocsFromSection(
132 const Elf32_Shdr* section_header) {
huangsdda11d062016-03-14 16:35:39133 FileOffset start_file_offset = section_header->sh_offset;
134 FileOffset end_file_offset = start_file_offset + section_header->sh_size;
[email protected]4b3d192b2011-11-08 20:32:26135
huangsdda11d062016-03-14 16:35:39136 const uint8_t* start_pointer = FileOffsetToPointer(start_file_offset);
137 const uint8_t* end_pointer = FileOffsetToPointer(end_file_offset);
[email protected]4b3d192b2011-11-08 20:32:26138
139 // Quick way to convert from Pointer to RVA within a single Section is to
huangsdda11d062016-03-14 16:35:39140 // subtract |pointer_to_rva|.
aviab98dcc92015-12-21 19:35:33141 const uint8_t* const adjust_pointer_to_rva =
142 start_pointer - section_header->sh_addr;
[email protected]4b3d192b2011-11-08 20:32:26143
144 // Find the rel32 relocations.
aviab98dcc92015-12-21 19:35:33145 const uint8_t* p = start_pointer;
[email protected]4b3d192b2011-11-08 20:32:26146 while (p < end_pointer) {
[email protected]4b3d192b2011-11-08 20:32:26147 // Heuristic discovery of rel32 locations in instruction stream: are the
148 // next few bytes the start of an instruction containing a rel32
149 // addressing mode?
huangsdda11d062016-03-14 16:35:39150 const uint8_t* rel32 = nullptr;
[email protected]4b3d192b2011-11-08 20:32:26151
[email protected]17b5edc2011-11-09 02:15:48152 if (p + 5 <= end_pointer) {
[email protected]4b3d192b2011-11-08 20:32:26153 if (*p == 0xE8 || *p == 0xE9) { // jmp rel32 and call rel32
154 rel32 = p + 1;
155 }
156 }
[email protected]17b5edc2011-11-09 02:15:48157 if (p + 6 <= end_pointer) {
huangsdda11d062016-03-14 16:35:39158 if (*p == 0x0F && (p[1] & 0xF0) == 0x80) { // Jcc long form
[email protected]4b3d192b2011-11-08 20:32:26159 if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely
160 rel32 = p + 2;
161 }
162 }
163 if (rel32) {
[email protected]144c8e92013-07-23 21:18:19164 RVA rva = static_cast<RVA>(rel32 - adjust_pointer_to_rva);
huangsdda11d062016-03-14 16:35:39165 scoped_ptr<TypedRVAX86> rel32_rva(new TypedRVAX86(rva));
[email protected]4b3d192b2011-11-08 20:32:26166
huangsdda11d062016-03-14 16:35:39167 if (!rel32_rva->ComputeRelativeTarget(rel32))
[email protected]144c8e92013-07-23 21:18:19168 return false;
[email protected]144c8e92013-07-23 21:18:19169
170 RVA target_rva = rel32_rva->rva() + rel32_rva->relative_target();
huangsdda11d062016-03-14 16:35:39171 if (IsValidTargetRVA(target_rva)) {
172 rel32_locations_.push_back(rel32_rva.release());
[email protected]4b3d192b2011-11-08 20:32:26173#if COURGETTE_HISTOGRAM_TARGETS
174 ++rel32_target_rvas_[target_rva];
175#endif
[email protected]17b5edc2011-11-09 02:15:48176 p = rel32 + 4;
[email protected]4b3d192b2011-11-08 20:32:26177 continue;
[email protected]4b3d192b2011-11-08 20:32:26178 }
179 }
180 p += 1;
181 }
182
183 return true;
184}
185
186} // namespace courgette