[a4c754]: libutil++ / bfd_support.cpp  Maximize  Restore  History

Download this file

578 lines (454 with data), 13.8 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
/**
* @file bfd_support.cpp
* BFD muck we have to deal with.
*
* @remark Copyright 2005 OProfile authors
* @remark Read the file COPYING
*
* @author John Levon
*/
#include "bfd_support.h"
#include "op_bfd.h"
#include "op_fileio.h"
#include "string_manip.h"
#include "cverb.h"
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
extern verbose vbfd;
namespace {
void check_format(string const & file, bfd ** ibfd)
{
if (!bfd_check_format_matches(*ibfd, bfd_object, NULL)) {
cverb << vbfd << "BFD format failure for " << file << endl;
bfd_close(*ibfd);
*ibfd = NULL;
}
}
bool separate_debug_file_exists(string const & name, unsigned long const crc)
{
unsigned long file_crc = 0;
// The size of 2 * 1024 elements for the buffer is arbitrary.
char buffer[2 * 1024];
ifstream file(name.c_str());
if (!file)
return false;
cverb << vbfd << "found " << name;
while (file) {
file.read(buffer, sizeof(buffer));
file_crc = calc_crc32(file_crc,
reinterpret_cast<unsigned char *>(&buffer[0]),
file.gcount());
}
cverb << vbfd << " 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 << vbfd << "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 << vbfd
<< ".gnu_debuglink section has size " << debuglink_size << endl;
if (!bfd_get_section_contents(ibfd, sect,
reinterpret_cast<unsigned char *>(contents),
static_cast<file_ptr>(0), debuglink_size)) {
bfd_perror("bfd_get_section_contents:get_debug:");
exit(2);
}
/* 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 << vbfd << ".gnu_debuglink filename is " << filename << endl;
return true;
}
/**
* With Objective C, we'll get strings like:
*
* _i_GSUnicodeString__rangeOfCharacterSetFromSet_options_range
*
* for the symbol name, and:
* -[GSUnicodeString rangeOfCharacterFromSet:options:range:]
*
* for the function name, so we have to do some looser matching
* than for other languages (unfortunately, it's not possible
* to demangle Objective C symbols).
*/
bool objc_match(string const & sym, string const & method)
{
if (method.length() < 3)
return false;
string mangled;
if (is_prefix(method, "-[")) {
mangled += "_i_";
} else if (is_prefix(method, "+[")) {
mangled += "_c_";
} else {
return false;
}
string::const_iterator it = method.begin() + 2;
string::const_iterator const end = method.end();
bool found_paren = false;
for (; it != end; ++it) {
switch (*it) {
case ' ':
mangled += '_';
if (!found_paren)
mangled += '_';
break;
case ':':
mangled += '_';
break;
case ')':
case ']':
break;
case '(':
found_paren = true;
mangled += '_';
break;
default:
mangled += *it;
}
}
return sym == mangled;
}
/*
* With a binary image where some objects are missing debug
* info, we can end up attributing to a completely different
* function (#484660): bfd_nearest_line() will happily move from one
* symbol to the nearest one it can find with debug information.
* To mitigate this problem, we check that the symbol name
* matches the returned function name.
*
* However, this check fails in some cases it shouldn't:
* Objective C, and C++ static inline functions (as discussed in
* GCC bugzilla #11774). So, we have a looser check that
* accepts merely a substring, plus some magic for Objective C.
*
* If even the loose check fails, then we give up.
*/
bool is_correct_function(string const & function, string const & name)
{
if (name == function)
return true;
if (objc_match(name, function))
return true;
// warn the user if we had to use the loose check
if (name.find(function) != string::npos) {
static bool warned = false;
if (!warned) {
cerr << "warning: some functions compiled without "
<< "debug information may have incorrect source "
<< "line attributions" << endl;
warned = true;
}
cverb << vbfd << "is_correct_function(" << function << ", "
<< name << ") fuzzy match." << endl;
return true;
}
return 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
*/
void fixup_linenr(bfd * abfd, asection * section, asymbol ** syms,
string const & name, bfd_vma pc,
char const ** filename, unsigned int * line)
{
char const * cfilename;
char const * function;
unsigned int linenr;
// 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 opreport -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(abfd, 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(abfd, section, syms, pc + i,
&cfilename, &function,
&linenr);
if (ret && cfilename && function && linenr != 0
&& is_correct_function(function, name)) {
*filename = cfilename;
*line = linenr;
return;
}
}
}
} // namespace anon
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 << vbfd << "bfd_openr failed for " << file << endl;
return NULL;
}
check_format(file, &ibfd);
return ibfd;
}
bfd * fdopen_bfd(string const & file, int fd)
{
/* bfd keeps its own reference to the filename char *,
* so it must have a lifetime longer than the ibfd */
bfd * ibfd = bfd_fdopenr(file.c_str(), NULL, fd);
if (!ibfd) {
cverb << vbfd << "bfd_openr failed for " << file << endl;
return NULL;
}
check_format(file, &ibfd);
return ibfd;
}
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 << vbfd << "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;
}
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;
/* ARM assembler internal mapping symbols aren't interesting */
if ((strcmp("$a", sym->name) == 0) ||
(strcmp("$t", sym->name) == 0) ||
(strcmp("$d", sym->name) == 0))
return false;
// 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;
}
bool boring_symbol(op_bfd_symbol const & first, op_bfd_symbol const & second)
{
if (first.name() == "Letext")
return true;
else if (second.name() == "Letext")
return false;
if (first.name().substr(0, 2) == "??")
return true;
else if (second.name().substr(0, 2) == "??")
return false;
if (first.hidden() && !second.hidden())
return true;
else if (!first.hidden() && second.hidden())
return false;
if (first.name()[0] == '_' && second.name()[0] != '_')
return true;
else if (first.name()[0] != '_' && second.name()[0] == '_')
return false;
if (first.weak() && !second.weak())
return true;
else if (!first.weak() && second.weak())
return false;
return false;
}
bool bfd_info::has_debug_info() const
{
if (!valid())
return false;
for (asection const * sect = abfd->sections; sect; sect = sect->next) {
if (sect->flags & SEC_DEBUGGING)
return true;
}
return false;
}
bfd_info::~bfd_info()
{
free(synth_syms);
close();
}
void bfd_info::close()
{
if (abfd)
bfd_close(abfd);
}
#if SYNTHESIZE_SYMBOLS
bool bfd_info::get_synth_symbols()
{
extern const bfd_target bfd_elf64_powerpc_vec;
extern const bfd_target bfd_elf64_powerpcle_vec;
bool is_elf64_powerpc_target = (abfd->xvec == &bfd_elf64_powerpc_vec)
|| (abfd->xvec == &bfd_elf64_powerpcle_vec);
if (!is_elf64_powerpc_target)
return false;
void * buf;
uint tmp;
long nr_mini_syms = bfd_read_minisymbols(abfd, 0, &buf, &tmp);
if (nr_mini_syms < 1)
return false;
asymbol ** mini_syms = (asymbol **)buf;
buf = NULL;
long nr_synth_syms = bfd_get_synthetic_symtab(abfd, nr_mini_syms,
mini_syms, 0,
NULL, &synth_syms);
if (nr_synth_syms < 0) {
free(mini_syms);
return false;
}
cverb << vbfd << "mini_syms: " << dec << nr_mini_syms << hex << endl;
cverb << vbfd << "synth_syms: " << dec << nr_synth_syms << hex << endl;
nr_syms = nr_mini_syms + nr_synth_syms;
syms.reset(new asymbol *[nr_syms + 1]);
for (size_t i = 0; i < (size_t)nr_mini_syms; ++i)
syms[i] = mini_syms[i];
for (size_t i = 0; i < (size_t)nr_synth_syms; ++i)
syms[nr_mini_syms + i] = synth_syms + i;
free(mini_syms);
// bfd_canonicalize_symtab does this, so shall we
syms[nr_syms] = NULL;
return true;
}
#else
bool bfd_info::get_synth_symbols()
{
return false;
}
#endif /* SYNTHESIZE_SYMBOLS */
void bfd_info::get_symbols()
{
if (!abfd)
return;
cverb << vbfd << "bfd_info::get_symbols() for "
<< bfd_get_filename(abfd) << endl;
if (get_synth_symbols())
return;
if (bfd_get_file_flags(abfd) & HAS_SYMS)
nr_syms = bfd_get_symtab_upper_bound(abfd);
cverb << vbfd << "bfd_get_symtab_upper_bound: " << dec
<< nr_syms << hex << endl;
nr_syms /= sizeof(asymbol *);
if (nr_syms < 1)
return;
syms.reset(new asymbol *[nr_syms]);
nr_syms = bfd_canonicalize_symtab(abfd, syms.get());
cverb << vbfd << "bfd_canonicalize_symtab: " << dec
<< nr_syms << hex << endl;
}
linenr_info const
find_nearest_line(bfd_info const & b, op_bfd_symbol const & sym,
unsigned int offset, bool anon_obj)
{
char const * function = "";
char const * cfilename = "";
unsigned int linenr = 0;
linenr_info info;
bfd * abfd;
asymbol ** syms;
asection * section;
bfd_vma pc;
bool ret;
if (!b.valid())
goto fail;
// take care about artificial symbol
if (!sym.symbol())
goto fail;
abfd = b.abfd;
syms = b.syms.get();
section = sym.symbol()->section;
if (anon_obj)
pc = offset - sym.symbol()->section->vma;
else
pc = (sym.value() + offset) - sym.filepos();
if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
goto fail;
if (pc >= bfd_section_size(abfd, section))
goto fail;
ret = bfd_find_nearest_line(abfd, section, syms, pc, &cfilename,
&function, &linenr);
if (!ret || !cfilename || !function)
goto fail;
/*
* is_correct_function does not handle the case of static inlines,
* but if the linenr is non-zero in the inline case, it is the correct
* line number.
*/
if (linenr == 0 && !is_correct_function(function, sym.name()))
goto fail;
if (linenr == 0) {
fixup_linenr(abfd, section, syms, sym.name(), pc, &cfilename,
&linenr);
}
info.found = true;
info.filename = cfilename;
info.line = linenr;
return info;
fail:
info.found = false;
// some stl lacks string::clear()
info.filename.erase(info.filename.begin(), info.filename.end());
info.line = 0;
return info;
}

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

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks