1 // Copyright 2013 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 #ifndef COURGETTE_DISASSEMBLER_ELF_32_H_
6 #define COURGETTE_DISASSEMBLER_ELF_32_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_vector.h"
10 #include "courgette/disassembler.h"
11 #include "courgette/memory_allocator.h"
12 #include "courgette/types_elf.h"
16 class AssemblyProgram
;
18 // A courgette disassembler for 32-bit ELF files. This class is only a
19 // partial implementation. Subclasses implement the
20 // architecture-specific parts of processing 32-bit ELF files. Specifically,
21 // RelToRVA processes entries in ELF relocation table,
22 // ParseRelocationSection verifies the organization of the ELF
23 // relocation table, and ParseRel32RelocsFromSection finds branch
24 // targets by looking for relative jump/call opcodes in the particular
25 // architecture's machine code.
26 class DisassemblerElf32
: public Disassembler
{
28 // Different instructions encode the target rva differently. This
29 // class encapsulates this behavior. public for use in unit tests.
32 explicit TypedRVA(RVA rva
) : rva_(rva
), offset_(-1) {
35 virtual ~TypedRVA() { };
41 RVA
relative_target() {
42 return relative_target_
;
45 void set_relative_target(RVA relative_target
) {
46 relative_target_
= relative_target
;
53 void set_offset(size_t offset
) {
57 virtual CheckBool
ComputeRelativeTarget(const uint8
* op_pointer
) = 0;
59 static bool IsLessThan(TypedRVA
*a
, TypedRVA
*b
) {
60 return a
->rva() < b
->rva();
70 explicit DisassemblerElf32(const void* start
, size_t length
);
72 virtual ~DisassemblerElf32() { };
74 virtual ExecutableType
kind() = 0;
76 virtual e_machine_values
ElfEM() = 0;
78 // Returns 'true' if the buffer appears to point to a valid ELF executable
79 // for 32 bit. If ParseHeader() succeeds, other member
80 // functions may be called.
81 virtual bool ParseHeader();
83 virtual bool Disassemble(AssemblyProgram
* target
);
85 // Public for unittests only
86 std::vector
<RVA
> &Abs32Locations() { return abs32_locations_
; }
87 ScopedVector
<TypedRVA
> &Rel32Locations() { return rel32_locations_
; }
91 uint32
DiscoverLength();
93 // Misc Section Helpers
95 Elf32_Half
SectionHeaderCount() const {
96 return section_header_table_size_
;
99 const Elf32_Shdr
*SectionHeader(int id
) const {
100 assert(id
>= 0 && id
< SectionHeaderCount());
101 return section_header_table_
+ id
;
104 const uint8
*SectionBody(int id
) const {
105 return OffsetToPointer(SectionHeader(id
)->sh_offset
);
108 Elf32_Word
SectionBodySize(int id
) const {
109 return SectionHeader(id
)->sh_size
;
112 // Misc Segment Helpers
114 Elf32_Half
ProgramSegmentHeaderCount() const {
115 return program_header_table_size_
;
118 const Elf32_Phdr
*ProgramSegmentHeader(int id
) const {
119 assert(id
>= 0 && id
< ProgramSegmentHeaderCount());
120 return program_header_table_
+ id
;
123 // The virtual memory address at which this program segment will be loaded
124 Elf32_Addr
ProgramSegmentMemoryBegin(int id
) const {
125 return ProgramSegmentHeader(id
)->p_vaddr
;
128 // The number of virtual memory bytes for this program segment
129 Elf32_Word
ProgramSegmentMemorySize(int id
) const {
130 return ProgramSegmentHeader(id
)->p_memsz
;
133 // Pointer into the source file for this program segment
134 Elf32_Addr
ProgramSegmentFileOffset(int id
) const {
135 return ProgramSegmentHeader(id
)->p_offset
;
138 // Number of file bytes for this program segment. Is <= ProgramMemorySize.
139 Elf32_Word
ProgramSegmentFileSize(int id
) const {
140 return ProgramSegmentHeader(id
)->p_filesz
;
143 // Misc address space helpers
145 CheckBool
IsValidRVA(RVA rva
) const WARN_UNUSED_RESULT
;
147 // Convert an ELF relocation struction into an RVA
148 virtual CheckBool
RelToRVA(Elf32_Rel rel
, RVA
* result
)
149 const WARN_UNUSED_RESULT
= 0;
151 // Returns kNoOffset if there is no file offset corresponding to 'rva'.
152 CheckBool
RVAToFileOffset(RVA rva
, size_t* result
) const WARN_UNUSED_RESULT
;
154 RVA
FileOffsetToRVA(size_t offset
) const WARN_UNUSED_RESULT
;
156 CheckBool
RVAsToOffsets(std::vector
<RVA
>* rvas
/*in*/,
157 std::vector
<size_t>* offsets
/*out*/);
159 CheckBool
RVAsToOffsets(ScopedVector
<TypedRVA
>* rvas
/*in and out*/);
161 // Parsing Code used to really implement Disassemble
163 CheckBool
ParseFile(AssemblyProgram
* target
) WARN_UNUSED_RESULT
;
164 virtual CheckBool
ParseRelocationSection(
165 const Elf32_Shdr
*section_header
,
166 AssemblyProgram
* program
) WARN_UNUSED_RESULT
= 0;
167 CheckBool
ParseProgbitsSection(
168 const Elf32_Shdr
*section_header
,
169 std::vector
<size_t>::iterator
* current_abs_offset
,
170 std::vector
<size_t>::iterator end_abs_offset
,
171 ScopedVector
<TypedRVA
>::iterator
* current_rel
,
172 ScopedVector
<TypedRVA
>::iterator end_rel
,
173 AssemblyProgram
* program
) WARN_UNUSED_RESULT
;
174 CheckBool
ParseSimpleRegion(size_t start_file_offset
,
175 size_t end_file_offset
,
176 AssemblyProgram
* program
) WARN_UNUSED_RESULT
;
178 CheckBool
ParseAbs32Relocs() WARN_UNUSED_RESULT
;
179 CheckBool
CheckSection(RVA rva
) WARN_UNUSED_RESULT
;
180 CheckBool
ParseRel32RelocsFromSections() WARN_UNUSED_RESULT
;
181 virtual CheckBool
ParseRel32RelocsFromSection(
182 const Elf32_Shdr
* section
) WARN_UNUSED_RESULT
= 0;
185 Elf32_Shdr
*section_header_table_
;
186 Elf32_Half section_header_table_size_
;
188 Elf32_Phdr
*program_header_table_
;
189 Elf32_Half program_header_table_size_
;
191 // Section header for default
192 const char *default_string_section_
;
194 std::vector
<RVA
> abs32_locations_
;
195 ScopedVector
<TypedRVA
> rel32_locations_
;
197 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32
);
200 } // namespace courgette
202 #endif // COURGETTE_DISASSEMBLER_ELF_32_H_