1 // Copyright (c) 2011 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_X86_H_
6 #define COURGETTE_DISASSEMBLER_ELF_32_X86_H_
8 #include "base/basictypes.h"
9 #include "courgette/disassembler.h"
10 #include "courgette/memory_allocator.h"
11 #include "courgette/types_elf.h"
15 class AssemblyProgram
;
17 class DisassemblerElf32X86
: public Disassembler
{
19 explicit DisassemblerElf32X86(const void* start
, size_t length
);
21 virtual ExecutableType
kind() { return EXE_ELF_32_X86
; }
23 // Returns 'true' if the buffer appears to point to a valid ELF executable
24 // for X86 32 bitWindows 32 bit. If ParseHeader() succeeds, other member
25 // functions may be called.
26 virtual bool ParseHeader();
28 virtual bool Disassemble(AssemblyProgram
* target
);
30 // Public for unittests only
31 std::vector
<RVA
> &Abs32Locations() { return abs32_locations_
; }
32 std::vector
<RVA
> &Rel32Locations() { return rel32_locations_
; }
36 uint32
DiscoverLength();
38 // Misc Section Helpers
40 const Elf32_Half
SectionHeaderCount() const {
41 return section_header_table_size_
;
44 const Elf32_Shdr
*SectionHeader(int id
) const {
45 assert(id
>= 0 && id
< SectionHeaderCount());
46 return section_header_table_
+ id
;
49 const uint8
*SectionBody(int id
) const {
50 return OffsetToPointer(SectionHeader(id
)->sh_offset
);
53 const Elf32_Word
SectionBodySize(int id
) const {
54 return SectionHeader(id
)->sh_size
;
57 // Misc Segment Helpers
59 const Elf32_Half
ProgramSegmentHeaderCount() const {
60 return program_header_table_size_
;
63 const Elf32_Phdr
*ProgramSegmentHeader(int id
) const {
64 assert(id
>= 0 && id
< ProgramSegmentHeaderCount());
65 return program_header_table_
+ id
;
68 // The virtual memory address at which this program segment will be loaded
69 const Elf32_Addr
ProgramSegmentMemoryBegin(int id
) const {
70 return ProgramSegmentHeader(id
)->p_vaddr
;
73 // The number of virtual memory bytes for this program segment
74 const Elf32_Word
ProgramSegmentMemorySize(int id
) const {
75 return ProgramSegmentHeader(id
)->p_memsz
;
78 // Pointer into the source file for this program segment
79 const Elf32_Addr
ProgramSegmentFileOffset(int id
) const {
80 return ProgramSegmentHeader(id
)->p_offset
;
83 // Number of file bytes for this program segment. Is <= ProgramMemorySize.
84 const Elf32_Word
ProgramSegmentFileSize(int id
) const {
85 return ProgramSegmentHeader(id
)->p_filesz
;
88 // Misc address space helpers
90 CheckBool
IsValidRVA(RVA rva
) const WARN_UNUSED_RESULT
;
92 // Convert an ELF relocation struction into an RVA
93 CheckBool
RelToRVA(Elf32_Rel rel
, RVA
* result
) const WARN_UNUSED_RESULT
;
95 // Returns kNoOffset if there is no file offset corresponding to 'rva'.
96 CheckBool
RVAToFileOffset(RVA rva
, size_t* result
) const WARN_UNUSED_RESULT
;
98 RVA
FileOffsetToRVA(size_t offset
) const WARN_UNUSED_RESULT
;
100 CheckBool
RVAsToOffsets(std::vector
<RVA
>* rvas
/*in*/,
101 std::vector
<size_t>* offsets
/*out*/);
103 // Parsing Code used to really implement Disassemble
105 CheckBool
ParseFile(AssemblyProgram
* target
) WARN_UNUSED_RESULT
;
106 CheckBool
ParseRelocationSection(
107 const Elf32_Shdr
*section_header
,
108 AssemblyProgram
* program
) WARN_UNUSED_RESULT
;
109 CheckBool
ParseProgbitsSection(
110 const Elf32_Shdr
*section_header
,
111 std::vector
<size_t>::iterator
* current_abs_offset
,
112 std::vector
<size_t>::iterator end_abs_offset
,
113 std::vector
<size_t>::iterator
* current_rel_offset
,
114 std::vector
<size_t>::iterator end_rel_offset
,
115 AssemblyProgram
* program
) WARN_UNUSED_RESULT
;
116 CheckBool
ParseSimpleRegion(size_t start_file_offset
,
117 size_t end_file_offset
,
118 AssemblyProgram
* program
) WARN_UNUSED_RESULT
;
120 CheckBool
ParseAbs32Relocs() WARN_UNUSED_RESULT
;
121 CheckBool
ParseRel32RelocsFromSections() WARN_UNUSED_RESULT
;
122 CheckBool
ParseRel32RelocsFromSection(
123 const Elf32_Shdr
* section
) WARN_UNUSED_RESULT
;
126 Elf32_Shdr
*section_header_table_
;
127 Elf32_Half section_header_table_size_
;
129 Elf32_Phdr
*program_header_table_
;
130 Elf32_Half program_header_table_size_
;
132 // Section header for default
133 const char *default_string_section_
;
135 std::vector
<RVA
> abs32_locations_
;
136 std::vector
<RVA
> rel32_locations_
;
138 DISALLOW_COPY_AND_ASSIGN(DisassemblerElf32X86
);
141 } // namespace courgette
143 #endif // COURGETTE_DISASSEMBLER_ELF_32_X86_H_