-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdbg_elf.h
More file actions
219 lines (196 loc) · 6.18 KB
/
Copy pathdbg_elf.h
File metadata and controls
219 lines (196 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at
* http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at src/CDDL.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
* Copyright (c) 2004-2005 PathScale, Inc. All rights reserved.
* Use is subject to license terms.
file: dbg_elf.h
created on: Fri Aug 13 11:02:27 PDT 2004
author: David Allison <[email protected]>
*/
#ifndef dbg_elf_h_included
#define dbg_elf_h_included
#include <elf.h>
#include "dbg_types.h"
#include "bstream.h"
#include "pstream.h"
#include "map_range.h"
#include "arch.h"
#include "os.h"
// classes
class ELF ;
class ProgramSegment {
public:
ProgramSegment(ELF *elf, Address baseaddr) ;
~ProgramSegment() ;
void read (std::istream & stream) ;
void *map (int fd) ;
int32_t get_type() { return type ; }
Address get_start() { return vaddr ; }
Address get_end() { return vaddr+filesz-1; }
int64_t get_size() { return memsz ; }
BVector get_contents (std::istream & stream) ;
protected:
private:
ELF *elf ;
int32_t type ;
int32_t flags ;
Offset offset ;
Address vaddr ;
Address paddr ;
int64_t filesz ;
int64_t memsz ;
int64_t align ;
Address baseaddr ;
} ;
class ELFSymbol ;
typedef std::map<std::string, ELFSymbol *> ELFSymbolMap ;
class Section {
public:
Section(ELF *elf, int index) ;
~Section() ;
std::string get_name () ;
Address get_addr () ;
int get_index () ;
int read (std::istream & stream) ;
void print () ;
int get_offset () ;
int get_size () ;
void set_name (std::istream & stream, Section * nametable) ;
std::string read_string (std::istream & stream, int stroffset) ;
int32_t read_word4(std::istream & stream, int stroffset);
BVector get_contents (std::istream & stream) ;
protected:
private:
ELF *elf ;
int index ;
std::string name ;
int32_t nameindex ;
int32_t type ;
int64_t flags ;
Address addr ;
Offset offset ;
int64_t size ;
int32_t link ;
int32_t info ;
int64_t addralign ;
int64_t entsize ;
} ;
class ELFSymbol {
public:
ELFSymbol(ELF *elf) ;
~ELFSymbol() ;
std::string get_name () ;
const char *get_c_name () { return name.c_str() ; }
Address get_value () ;
Section* get_section () ;
int read (std::istream & stream, Address baseaddr) ;
void set_name (std::istream & stream, Section * nametable) ;
int get_size () ;
void print () ;
Address value ;
bool operator < (ELFSymbol &sym) { return value < sym.value ; }
byte get_info() { return info ; }
byte get_other() { return other ; }
int get_section_index() { return shndx ; }
protected:
private:
ELF *elf ;
std::string name ;
int32_t nameindex ;
int64_t size ;
byte info ;
byte other ;
int16_t shndx ;
Section* section ;
} ;
class ELF {
friend class ELFSymbol ;
friend class Section ;
friend class ProgramSegment ;
public:
ELF(std::string name, Offset mainoffset=0) ;
~ELF() ;
bool is_elf64 () ;
bool is_little_endian () ;
std::istream *open (Address baseaddress=0) ;
BVector get_section (std::istream & stream, std::string name) ;
Section *find_section (std::string name) ;
Section *find_section_by_index (int index) ;
void read_symbol_table (std::istream & stream, Address baseaddr) ;
Address find_symbol (std::string name, bool caseblind = false) ;
Section *find_symbol_section (std::string name, bool caseblind = false) ;
void find_symbol_at_address (Address addr, std::string &name, int &offset) ;
Section *find_section_at_addr (Address addr) ;
void list_symbols (PStream &os) ;
void list_functions (PStream &os) ;
void list_variables (PStream &os) ;
int get_num_segments() { return segments.size() ; }
ProgramSegment *get_segment (int i) { return segments[i]; }
Address get_base() { return base ; } // base address
Map_Range<Address,ELFSymbol*>& get_symbols() {
return symmap;
}
ProgramSegment *find_segment (Address addr) ;
std::string get_name() { return name ; }
Architecture *new_arch();
void prstatus_to_thread(BStream *stream, int size, struct CoreThread *thread);
void prstatus_to_pname(BStream *stream, int size, std::string &pname);
protected:
private:
void read_symtab (std::istream &stream, Section *symtab, Address baseaddr, Section *strtab) ;
byte read_byte (std::istream & stream) ;
int64_t read_word8 (std::istream & stream) ;
int32_t read_word4 (std::istream & stream) ;
int16_t read_word2 (std::istream & stream) ;
Address read_address (std::istream & stream) ;
Offset read_offset (std::istream & stream) ;
int64_t read_xword (std::istream & stream) ;
void read_header (std::istream & stream, Address baseaddr) ;
void print_header() ;
std::string name ;
Offset mainoffset ;
byte ident[4] ;
byte fileclass ;
byte dataencoding ;
byte elfversion ;
byte abi ;
byte abiversion ;
int16_t type ;
int16_t machine ;
int32_t version ;
Address entry ;
Offset phoff ;
Offset shoff ;
int32_t flags ;
int16_t ehsize ;
int16_t phentsize ;
int16_t phnum ;
int16_t shentsize ;
int16_t shnum ;
int16_t shstrndx ;
std::vector<Section *> sections ;
std::vector<ProgramSegment *> segments ;
Map_Range<Address,ELFSymbol*> symmap;
ELFSymbolMap symbols ; // map of name vs ELFSymbol
ELFSymbolMap cbsymbols ; // map of name vs ELFSymbol (case blind)
bool caseblind_ok ;
void make_cb_symbol_table() ;
Address base ;
} ;
#endif