[6893b5]: libutil++ / op_bfd.h Maximize Restore History

Download this file

op_bfd.h    330 lines (277 with data), 10.6 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* @file op_bfd.h
* Encapsulation of bfd objects
*
* @remark Copyright 2002 OProfile authors
* @remark Read the file COPYING
*
* @author Philippe Elie
* @author John Levon
*/
#ifndef OP_BFD_H
#define OP_BFD_H
#include "config.h"
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include "bfd_support.h"
#include "locate_images.h"
#include "utility.h"
#include "cached_value.h"
#include "op_types.h"
class op_bfd;
class string_filter;
class extra_images;
/// all symbol vector indexing uses this type
typedef size_t symbol_index_t;
/**
* A symbol description from a bfd point of view. This duplicate
* information pointed by an asymbol, we need this duplication in case
* the symbol is an artificial symbol
*/
class op_bfd_symbol {
public:
/// ctor for real symbols
op_bfd_symbol(asymbol const * a);
/// ctor for artificial symbols
op_bfd_symbol(bfd_vma vma, size_t size, std::string const & name);
bfd_vma vma() const { return symb_value + section_vma; }
unsigned long value() const { return symb_value; }
unsigned long filepos() const { return symb_value + section_filepos; }
unsigned long symbol_endpos(void) const;
asection const * section(void) const { return bfd_symbol->section; }
std::string const & name() const { return symb_name; }
asymbol const * symbol() const { return bfd_symbol; }
size_t size() const { return symb_size; }
void size(size_t s) { symb_size = s; }
bool hidden() const { return symb_hidden; }
bool weak() const { return symb_weak; }
bool artificial() const { return symb_artificial; }
/// compare two symbols by their filepos()
bool operator<(op_bfd_symbol const & lhs) const;
private:
/// the original bfd symbol, this can be null if the symbol is an
/// artificial symbol
asymbol const * bfd_symbol;
/// the offset of this symbol relative to the begin of the section's
/// symbol
unsigned long symb_value;
/// the section filepos for this symbol
unsigned long section_filepos;
/// the section vma for this symbol
bfd_vma section_vma;
/// the size of this symbol
size_t symb_size;
/// the name of the symbol
std::string symb_name;
/// normally not externally visible symbol
bool symb_hidden;
/// whether other symbols can override it
bool symb_weak;
/// symbol is artificially created
bool symb_artificial;
/// code bytes corresponding to symbol -- used for XML generation
std::string symb_bytes;
};
/**
* Encapsulation of a bfd object. Simplifies open/close of bfd, enumerating
* symbols and retrieving informations for symbols or vma.
*
* Use of this class relies on a std::ostream cverb
*/
class op_bfd {
public:
/**
* @param filename the name of the image file
* @param symbol_filter filter to apply to symbols
* @param extra_images container where all extra candidate filenames
* are stored
* @param ok in-out parameter: on in, if not set, don't
* open the bfd (because it's not there or whatever). On out,
* it's set to false if the bfd couldn't be loaded.
*/
op_bfd(std::string const & filename,
string_filter const & symbol_filter,
extra_images const & extra_images,
bool & ok);
/**
* This constructor is used when processing an SPU profile
* where the SPU ELF is embedded within the PPE binary.
*/
op_bfd(uint64_t spu_offset,
std::string const & filename,
string_filter const & symbol_filter,
extra_images const & extra_images,
bool & ok);
std::string get_embedding_filename() const { return embedding_filename; }
/// close an opened bfd image and free all related resources
~op_bfd();
/**
* @param sym_idx index of the symbol
* @param offset fentry number
* @param filename output parameter to store filename
* @param linenr output parameter to store linenr.
*
* Retrieve the relevant finename:linenr information for the sym_idx
* at offset. If the lookup fails, return false. In some cases this
* function can retrieve the filename and return true but fail to
* retrieve the linenr and so can return zero in linenr
*/
bool get_linenr(symbol_index_t sym_idx, bfd_vma offset,
std::string & filename, unsigned int & linenr) const;
/**
* @param sym_idx symbol index
* @param start reference to start var
* @param end reference to end var
*
* Calculates the range of sample file entries covered by sym. start
* and end will be filled in appropriately. If index is the last entry
* in symbol table, all entries up to the end of the sample file will
* be used. After calculating start and end they are sanitized
*
* All errors are fatal.
*/
void get_symbol_range(symbol_index_t sym_idx,
unsigned long long & start, unsigned long long & end) const;
/**
* @param start reference to the start vma
* @param end reference to the end vma
*
* return in start, end the vma range for this binary object.
*/
void get_vma_range(bfd_vma & start, bfd_vma & end) const;
/** return the relocated PC value for the given file offset */
bfd_vma offset_to_pc(bfd_vma offset) const;
/**
* If passed 0, return the file position of the .text section.
* Otherwise, return the filepos of a section with a matching
* vma.
*/
unsigned long get_start_offset(bfd_vma vma = 0) const;
/**
* Return the image name of the underlying binary image. For an
* archive, this returns the path *within* the archive, not the
* full path of the file.
*/
std::string get_filename() const;
/// sorted vector by vma of interesting symbol.
std::vector<op_bfd_symbol> syms;
/// return in bits the bfd_vma size for this binary. This is needed
/// because gprof output depend on the bfd_vma for *this* binary
/// and do not depend on sizeof(bfd_vma)
size_t bfd_arch_bits_per_address() const;
/// return true if binary contain some debug information
bool has_debug_info() const;
/**
* @param sym_idx symbol index
*
* Return true or false, indicating whether or not the
* symbol referenced by the passed sym_idx has code available.
* Some symbols have no code associated with them; for example,
* artificial symbols created for anonymous memory samples or for
* stripped binaries with no symbol debug info. Additionally,
* if the bfd object associated with the symbol is not valid,
* this function will also return false.
*
* NOTE: This call should be made prior to invoking
* get_symbol_contents to avoid unnecessarily allocating
* memory for the symbol contents.
*/
bool symbol_has_contents(symbol_index_t sym_idx);
bool get_symbol_contents(symbol_index_t sym_index,
unsigned char * contents) const;
bool valid() const { return ibfd.valid(); }
bfd_vma get_vma_adj(void) const { return vma_adj; }
private:
/// temporary container type for getting symbols
typedef std::list<op_bfd_symbol> symbols_found_t;
/**
* Parse and sort in ascending order all symbols
* in the file pointed to by abfd that reside in
* a %SEC_CODE section.
*
* The symbols are filtered through
* the interesting_symbol() predicate and sorted
* with op_bfd_symbol::operator<() comparator.
*/
void get_symbols(symbols_found_t & symbols);
/**
* Helper function for get_symbols.
* Populates bfd_syms and extracts the "interesting_symbol"s.
*/
void get_symbols_from_file(bfd_info & bfd, size_t start,
op_bfd::symbols_found_t & symbols,
bool debug_file);
/**
* Add the symbols in the binary, applying filtering,
* and handling artificial symbols.
*/
void add_symbols(symbols_found_t & symbols,
string_filter const & symbol_filter);
/**
* symbol_size - return the size of a symbol
* @param sym symbol to get size
* @param next next symbol in vma order if any
*/
size_t symbol_size(op_bfd_symbol const & sym,
op_bfd_symbol const * next) const;
/// create an artificial symbol for a symbolless binary
op_bfd_symbol const create_artificial_symbol();
/* Generate symbols using bfd functions for
* the image file associated with the ibfd arg.
*/
uint process_symtab(bfd_info * bfd, uint start);
/// filename we open (not including archive path)
std::string filename;
/// path to archive
std::string archive_path;
/// reference to extra_images
extra_images const & extra_found_images;
/// file size in bytes
off_t file_size;
/// corresponding debug file name
mutable std::string debug_filename;
/// true if at least one section has (flags & SEC_DEBUGGING) != 0
mutable cached_value<bool> debug_info;
/// our main bfd object: .bfd may be NULL
bfd_info ibfd;
// corresponding debug bfd object, if one is found
mutable bfd_info dbfd;
/// sections we will avoid to use symbol from, this is needed
/// because elf file allows sections with identical vma and we can't
/// allow overlapping symbols. Such elf layout is used actually by
/// kernel modules where all code section vma are set to 0.
std::vector<asection const *> filtered_section;
typedef std::map<std::string, u32> filepos_map_t;
// mapping of section names to filepos in the original binary
filepos_map_t filepos_map;
/**
* If spu_offset is non-zero, embedding_filename is the file containing
* the embedded SPU image.
*/
std::string embedding_filename;
bool anon_obj;
/**
* If a runtime binary is prelinked, then its p_vaddr field in the
* first PT_LOAD segment will give the address where the binary will
* be loaded into memory. However, the corresponding debuginfo file
* may have a different p_vaddr value. In profile_container::add_samples,
* this difference is handled by adding the "base_vma" to the sample
* vma. However, if the runtime binary has no symbol information at all,
* then the "base_vma" is obtained from the debuginfo symbol information.
* For opreport, this works OK, since under such conditions, ALL symbol
* and debug data is then obtained from the debuginfo files, and the sample
* vma's should match up fine with the symbol vma's in the debuginfo file.
* But when doing 'opannoate --assembly', the real (runtime) image is used for
* annotation, and, thus, we may have a mis-match between real image p_vaddr
* and the impliled p_vaddr stored with the samples. To handle this case,
* we do the following: When a bfd_info is created for a debuginfo
* file, we set vma_adj to the difference between runtime load address
* and the p_vaddr of the first PT_LOAD segment of the debuginfo file, if and
* only if the real image has no symbol info; otherwise vma_adj is set to 0.
*/
bfd_vma vma_adj;
};
#endif /* !OP_BFD_H */