[0d0e60]: libutil++ / op_bfd.cpp  Maximize  Restore  History

Download this file

708 lines (566 with data), 17.9 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/**
* @file op_bfd.cpp
* Encapsulation of bfd objects
*
* @remark Copyright 2002 OProfile authors
* @remark Read the file COPYING
*
* @author Philippe Elie
* @author John Levon
*/
#include "op_file.h"
#include "op_config.h"
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include "op_exception.h"
#include "op_bfd.h"
#include "string_filter.h"
#include "stream_util.h"
#include "cverb.h"
#include "op_fileio.h"
using namespace std;
namespace {
bfd * open_bfd(string const & file)
{
/* bfd keeps its own reference to the filename char *,
* so it must have a lifetime longer than the ibfd */
bfd * ibfd = bfd_openr(file.c_str(), NULL);
if (!ibfd) {
cverb << "bfd_openr failed for " << file << endl;
goto out_fail;
}
char ** matching;
if (!bfd_check_format_matches(ibfd, bfd_object, &matching)) {
cverb << "BFD format failure for " << file << endl;
ibfd = NULL;
}
out_fail:
return ibfd;
}
bool
separate_debug_file_exists(string const & name,
unsigned long const crc)
{
unsigned long file_crc = 0;
// The size of 8*1024 element for the buffer is arbitrary.
char buffer[8*1024];
ifstream file(name.c_str());
if (!file)
return false;
cverb << "found " << name;
while (file) {
file.read(buffer, sizeof(buffer));
file_crc = calc_crc32(file_crc,
reinterpret_cast<unsigned char *>(&buffer[0]),
file.gcount());
}
cverb << " with crc32 = " << hex << file_crc << endl;
return crc == file_crc;
}
bool
get_debug_link_info(bfd * ibfd,
string & filename,
unsigned long & crc32)
{
asection * sect;
cverb << "fetching .gnu_debuglink section" << endl;
sect = bfd_get_section_by_name(ibfd, ".gnu_debuglink");
if (sect == NULL)
return false;
bfd_size_type debuglink_size = bfd_section_size(ibfd, sect);
char contents[debuglink_size];
cverb << ".gnu_debuglink section has size " << debuglink_size << endl;
bfd_get_section_contents(ibfd, sect,
reinterpret_cast<unsigned char *>(contents),
static_cast<file_ptr>(0), debuglink_size);
/* CRC value is stored after the filename, aligned up to 4 bytes. */
size_t filename_len = strlen(contents);
size_t crc_offset = filename_len + 1;
crc_offset = (crc_offset + 3) & ~3;
crc32 = bfd_get_32(ibfd,
reinterpret_cast<bfd_byte *>(contents + crc_offset));
filename = string(contents, filename_len);
cverb << ".gnu_debuglink filename is " << filename << endl;
return true;
}
} // namespace anon
bool
find_separate_debug_file(bfd * ibfd,
string const & dir_in,
string const & global_in,
string & filename)
{
string dir(dir_in);
string global(global_in);
string basename;
unsigned long crc32;
if (!get_debug_link_info(ibfd, basename, crc32))
return false;
if (dir.size() > 0 && dir.at(dir.size() - 1) != '/')
dir += '/';
if (global.size() > 0 && global.at(global.size() - 1) != '/')
global += '/';
cverb << "looking for debugging file " << basename
<< " with crc32 = " << hex << crc32 << endl;
string first_try(dir + basename);
string second_try(dir + ".debug/" + basename);
if (dir.size() > 0 && dir[0] == '/')
dir = dir.substr(1);
string third_try(global + dir + basename);
if (separate_debug_file_exists(first_try, crc32))
filename = first_try;
else if (separate_debug_file_exists(second_try, crc32))
filename = second_try;
else if (separate_debug_file_exists(third_try, crc32))
filename = third_try;
else
return false;
return true;
}
op_bfd_symbol::op_bfd_symbol(asymbol const * a)
: bfd_symbol(a), symb_value(a->value),
section_filepos(a->section->filepos),
section_vma(a->section->vma),
symb_size(0)
{
// Some sections have unnamed symbols in them. If
// we just ignore them then we end up sticking
// things like .plt hits inside of _init. So instead
// we name the symbol after the section.
if (a->name && a->name[0] != '\0') {
symb_name = a->name;
} else {
symb_name = string("??") + a->section->name;
}
}
op_bfd_symbol::op_bfd_symbol(bfd_vma vma, size_t size, string const & name)
: bfd_symbol(0), symb_value(vma),
section_filepos(0), section_vma(0),
symb_size(size), symb_name(name)
{
}
op_bfd::op_bfd(string const & fname, string_filter const & symbol_filter,
bool & ok)
:
filename(fname),
file_size(-1),
ibfd(0),
dbfd(0),
text_offset(0),
debug_info(false)
{
// after creating all symbol it's convenient for user code to access
// symbols through a vector. We use an intermediate list to avoid a
// O(N�) behavior when we will filter vector element below
symbols_found_t symbols;
// if there's a problem already, don't try to open it
if (!ok)
goto out_fail;
op_get_fsize(filename.c_str(), &file_size);
ibfd = open_bfd(filename);
if (!ibfd) {
cverb << "open_bfd failed for " << filename << endl;
ok = false;
goto out_fail;
}
{
asection const * sect = bfd_get_section_by_name(ibfd, ".text");
if (sect) {
text_offset = sect->filepos;
io_state state(cverb);
cverb << ".text filepos " << hex << text_offset << endl;
}
for (sect = ibfd->sections; sect; sect = sect->next) {
if (sect->flags & SEC_DEBUGGING) {
debug_info = true;
break;
}
}
// if no debugging section check to see if there is an .debug file
if (!debug_info) {
string global(DEBUGDIR);
string dirname(filename.substr(0, filename.rfind('/')));
if (find_separate_debug_file (ibfd, dirname, global,
debug_filename)) {
cverb << "now loading: " << debug_filename << endl;
dbfd = open_bfd(debug_filename);
if (dbfd) {
for (sect = dbfd->sections; sect;
sect = sect->next) {
if (sect->flags & SEC_DEBUGGING) {
debug_info = true;
break;
}
}
} else {
// .debug is optional, so will not fail if
// problem opening file.
cverb << "unable to open: " << debug_filename
<< endl;
// debug_filename = NULL;
}
}
}
}
get_symbols(symbols);
out:
add_symbols(symbols, symbol_filter);
return;
out_fail:
if (ibfd)
bfd_close(ibfd);
ibfd = NULL;
if (dbfd)
bfd_close(dbfd);
dbfd = NULL;
// make the fake symbol fit within the fake file
file_size = -1;
goto out;
}
op_bfd::~op_bfd()
{
if (ibfd)
bfd_close(ibfd);
if (dbfd)
bfd_close(dbfd);
}
bool op_bfd_symbol::operator<(op_bfd_symbol const & rhs) const
{
return filepos() < rhs.filepos();
}
namespace {
/**
* Return true if the symbol is worth looking at
*/
bool interesting_symbol(asymbol * sym)
{
// #717720 some binutils are miscompiled by gcc 2.95, one of the
// typical symptom can be catched here.
if (!sym->section) {
ostringstream os;
os << "Your version of binutils seems to have a bug.\n"
<< "Read http://oprofile.sf.net/faq/#binutilsbug\n";
throw op_runtime_error(os.str());
}
if (!(sym->section->flags & SEC_CODE))
return false;
// returning true for fix up in op_bfd_symbol()
if (!sym->name || sym->name[0] == '\0')
return true;
// C++ exception stuff
if (sym->name[0] == '.' && sym->name[1] == 'L')
return false;
/* This case cannot be moved to boring_symbol(),
* because that's only used for duplicate VMAs,
* and sometimes this symbol appears at an address
* different from all other symbols.
*/
if (!strcmp("gcc2_compiled.", sym->name))
return false;
return true;
}
/**
* return true if the symbol is boring, boring symbol are eliminated
* when multiple symbol exist at the same vma
*/
bool boring_symbol(string const & name)
{
// FIXME: check if we can't do a better job, this heuristic fix all
// case I'm aware
if (name == "Letext")
return true;
if (name.substr(0, 2) == "??")
return true;
return false;
}
/// function object for filtering symbols to remove
struct remove_filter {
remove_filter(string_filter const & filter)
: filter_(filter) {}
bool operator()(op_bfd_symbol const & symbol) {
return !filter_.match(symbol.name());
}
string_filter filter_;
};
} // namespace anon
void op_bfd::get_symbols_from_file(bfd * ibfd, size_t start,
op_bfd::symbols_found_t & symbols)
{
uint nr_all_syms;
nr_all_syms = bfd_canonicalize_symtab(ibfd, bfd_syms.get()+start);
if (nr_all_syms < 1)
return;
for (symbol_index_t i = start; i < start+nr_all_syms; i++) {
if (interesting_symbol(bfd_syms[i])) {
// need to use filepos of original file
bfd_syms[i]->section->filepos = text_offset;
symbols.push_back(op_bfd_symbol(bfd_syms[i]));
}
}
}
void op_bfd::get_symbols(op_bfd::symbols_found_t & symbols)
{
size_t size;
size_t size_binary = 0;
size_t size_debug = 0;
if (bfd_get_file_flags(ibfd) & HAS_SYMS)
size_binary = bfd_get_symtab_upper_bound(ibfd);
if (dbfd && (bfd_get_file_flags(dbfd) & HAS_SYMS))
size_debug += bfd_get_symtab_upper_bound(dbfd);
size = size_binary + size_debug;
/* HAS_SYMS can be set with no symbols */
if (size < 1)
return;
bfd_syms.reset(new asymbol*[size]);
if (size_binary > 0)
get_symbols_from_file(ibfd, 0, symbols);
if (size_debug > 0)
get_symbols_from_file(dbfd, size_binary, symbols);
symbols.sort();
symbols_found_t::iterator it = symbols.begin();
// we need to ensure than for a given vma only one symbol exist else
// we read more than one time some samples. Fix #526098
for (; it != symbols.end();) {
symbols_found_t::iterator temp = it;
++temp;
if (temp != symbols.end() && (it->vma() == temp->vma())) {
if (boring_symbol(it->name())) {
it = symbols.erase(it);
} else {
symbols.erase(temp);
}
} else {
++it;
}
}
// now we can calculate the symbol size, we can't first include/exclude
// symbols because the size of symbol is calculated from the difference
// between the vma of a symbol and the next one.
for (it = symbols.begin() ; it != symbols.end(); ++it) {
op_bfd_symbol const * next = 0;
symbols_found_t::iterator temp = it;
++temp;
if (temp != symbols.end())
next = &*temp;
it->size(symbol_size(*it, next));
}
}
void op_bfd::add_symbols(op_bfd::symbols_found_t & symbols,
string_filter const & symbol_filter)
{
// images with no symbols debug info available get a placeholder symbol
if (symbols.empty())
symbols.push_back(create_artificial_symbol());
cverb << "number of symbols before filtering " << symbols.size() << endl;
symbols_found_t::iterator it;
it = remove_if(symbols.begin(), symbols.end(), remove_filter(symbol_filter));
symbols.erase(it, symbols.end());
copy(symbols.begin(), symbols.end(), back_inserter(syms));
cverb << "number of symbols now " << syms.size() << endl;
}
u32 op_bfd::sym_offset(symbol_index_t sym_index, u32 num) const
{
/* take off section offset and symb value */
return num - syms[sym_index].filepos();
}
bfd_vma op_bfd::offset_to_pc(bfd_vma offset) const
{
asection const * sect = ibfd->sections;
for (; sect; sect = sect->next) {
if (offset >= bfd_vma(sect->filepos) &&
(!sect->next || offset < bfd_vma(sect->next->filepos))) {
return sect->vma + (offset - sect->filepos);
}
}
return 0;
}
bool op_bfd::get_linenr(symbol_index_t sym_idx, unsigned int offset,
string & source_filename, unsigned int & linenr) const
{
linenr = 0;
if (!debug_info)
return false;
char const * functionname;
char const * cfilename = "";
bfd_vma pc;
op_bfd_symbol const & sym = syms[sym_idx];
// take care about artificial symbol
if (sym.symbol() == 0)
return false;
asection* section = sym.symbol()->section;
if ((bfd_get_section_flags (ibfd, section) & SEC_ALLOC) == 0)
return false;
pc = sym_offset(sym_idx, offset) + sym.value();
if (pc >= bfd_section_size(ibfd, section))
return false;
// Try finding line information in binary first.
bool ret = bfd_find_nearest_line(ibfd, section, bfd_syms.get(), pc,
&cfilename, &functionname, &linenr);
// Try finding line information in debug info file second.
if ((!ret) && dbfd)
ret = bfd_find_nearest_line(dbfd, section, bfd_syms.get(), pc,
&cfilename, &functionname, &linenr);
if (cfilename == 0 || !ret) {
cfilename = "";
linenr = 0;
ret = false;
}
// functionname and symbol name can be different if we accept it we
// can get samples for the wrong symbol (#484660)
// Note this break static inline function, since for these functions we
// get a different symbol name than symbol name but we recover later.
if (ret && functionname && sym.name() != string(functionname)) {
// gcc doesn't emit mangled name for C++ static function so we
// try to recover by accepting this linenr info if functionname
// is a substring of sym.name, this is not a bug see gcc
// bugzilla #11774. Check agaisnt the filename part of the
// is error prone error (e.g. namespace A { static int f1(); })
// so we check only for a substring and warn the user.
static bool warned = false;
if (!warned) {
// FIXME: enough precise message ? We will get this
// message for static C++ function too, must we
// warn only if the following check fails ?
cerr << "warning: \"" << get_filename() << "\" some "
<< "functions compiled without debug information "
<< "may have incorrect source line attributions"
<< endl;
warned = true;
}
if (sym.name().find(functionname) == string::npos)
ret = false;
}
/* binutils 2.12 and below have a small bug where functions without a
* debug entry at the prologue start do not give a useful line number
* from bfd_find_nearest_line(). This can happen with certain gcc
* versions such as 2.95.
*
* We work around this problem by scanning forward for a vma with
* valid linenr info, if we can't get a valid line number.
* Problem uncovered by Norbert Kaufmann. The work-around decreases,
* on the tincas application, the number of failure to retrieve linenr
* info from 835 to 173. Most of the remaining are c++ inline functions
* mainly from the STL library. Fix #529622
*/
if (linenr == 0) {
// FIXME: looking at debug info for all gcc version shows
// than the same problems can -perhaps- occur for epilog code:
// find a samples files with samples in epilog and try oreport
// -l -g on it, check it also with opannotate.
// first restrict the search on a sensible range of vma,
// 16 is an intuitive value based on epilog code look
size_t max_search = 16;
size_t section_size = bfd_section_size(ibfd, section);
if (pc + max_search > section_size)
max_search = section_size - pc;
for (size_t i = 1 ; i < max_search ; ++i) {
bool ret = bfd_find_nearest_line(ibfd, section,
bfd_syms.get(), pc+i,
&cfilename,
&functionname,
&linenr);
if (ret && linenr != 0
&& sym.name() == string(functionname)) {
return true;
}
}
// We lose it's pointless to try more.
// bfd_find_nearest_line clobber the memory pointed by filename
// from a previous call when the filename change across
// multiple calls. The more easy way to recover is to reissue
// the first call, we don't need to recheck return value, we
// know that the call will succeed.
// As mentionned above a previous work-around break static
// inline function. We recover here by not checking than
// functionname == sym.name
bfd_find_nearest_line(ibfd, section, bfd_syms.get(), pc,
&cfilename, &functionname, &linenr);
}
if (cfilename) {
source_filename = cfilename;
} else {
source_filename = "";
linenr = 0;
}
return ret;
}
size_t op_bfd::symbol_size(op_bfd_symbol const & sym,
op_bfd_symbol const * next) const
{
u32 start = sym.filepos();
u32 end = next ? next->filepos() : file_size;
return end - start;
}
void op_bfd::get_symbol_range(symbol_index_t sym_idx,
u32 & start, u32 & end) const
{
op_bfd_symbol const & sym = syms[sym_idx];
io_state state(cverb);
cverb << "symbol " << sym.name() << ", value " << hex << sym.value() << endl;
start = sym.filepos();
if (sym.symbol()) {
cverb << "in section " << sym.symbol()->section->name
<< ", filepos " << hex << sym.symbol()->section->filepos << endl;
}
end = start + syms[sym_idx].size();
cverb << "start " << hex << start << ", end " << end << endl;
if (start >= file_size + text_offset) {
ostringstream os;
os << "start " << hex << start
<< " out of range (max " << file_size << ")\n";
throw op_runtime_error(os.str());
}
if (end > file_size + text_offset) {
ostringstream os;
os << "end " << hex << end
<< " out of range (max " << file_size << ")\n";
throw op_runtime_error(os.str());
}
if (start > end) {
ostringstream os;
os << "start " << hex << start
<< " is more than end" << end << endl;
throw op_runtime_error(os.str());
}
}
void op_bfd::get_vma_range(bfd_vma & start, bfd_vma & end) const
{
if (!syms.empty()) {
// syms are sorted by vma so vma of the first symbol and vma +
// size of the last symbol give the vma range for gprof output
op_bfd_symbol const & last_symb = syms[syms.size() - 1];
start = syms[0].vma();
// end is excluded from range so + 1 *if* last_symb.size() != 0
end = last_symb.vma() + last_symb.size() + (last_symb.size() != 0);
} else {
start = 0;
end = file_size;
}
}
op_bfd_symbol const op_bfd::create_artificial_symbol()
{
// FIXME: prefer a bool artificial; to this ??
string symname = "?";
symname += get_filename();
bfd_vma start, end;
get_vma_range(start, end);
return op_bfd_symbol(start, end - start, symname);
}
string op_bfd::get_filename() const
{
return filename;
}
size_t op_bfd::bfd_arch_bits_per_address() const
{
if (ibfd)
return ::bfd_arch_bits_per_address(ibfd);
// FIXME: this function should be called only if the underlined ibfd
// is ok, must we throw ?
return sizeof(bfd_vma);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks