Imported Upstream version 1.1.0
[platform/upstream/oprofile.git] / libutil++ / bfd_support.cpp
1 /**
2  * @file bfd_support.cpp
3  * BFD muck we have to deal with.
4  *
5  * @remark Copyright 2005 OProfile authors
6  * @remark Read the file COPYING
7  *
8  * @author John Levon
9  */
10
11 #include "bfd_support.h"
12
13 #include "op_bfd.h"
14 #include "op_fileio.h"
15 #include "op_config.h"
16 #include "string_manip.h"
17 #include "file_manip.h"
18 #include "cverb.h"
19 #include "locate_images.h"
20 #include "op_libiberty.h"
21 #include "op_exception.h"
22
23 #include <unistd.h>
24 #include <errno.h>
25 #include <elf.h>
26 #include <cstdlib>
27 #include <cstring>
28 #include <cassert>
29 #include <iostream>
30 #include <sstream>
31 #include <fstream>
32 #include <sstream>
33 #include <string>
34 #include <cstring>
35 #include <cstdlib>
36
37 using namespace std;
38
39 extern verbose vbfd;
40
41 namespace {
42
43 #ifndef NT_GNU_BUILD_ID
44 #define NT_GNU_BUILD_ID 3
45 #endif
46 static size_t build_id_size;
47
48
49 void check_format(string const & file, bfd ** ibfd)
50 {
51         if (!bfd_check_format_matches(*ibfd, bfd_object, NULL)) {
52                 cverb << vbfd << "BFD format failure for " << file << endl;
53                 bfd_close(*ibfd);
54                 *ibfd = NULL;
55         }
56 }
57
58
59 bool separate_debug_file_exists(string & name, unsigned long const crc, 
60                                 extra_images const & extra)
61 {
62         unsigned long file_crc = 0;
63         // The size of 2 * 1024 elements for the buffer is arbitrary.
64         char buffer[2 * 1024];
65
66         image_error img_ok;
67         string const image_path = extra.find_image_path(name, img_ok, true);
68
69         if (img_ok != image_ok)
70                 return false;
71
72         name = image_path;
73
74         ifstream file(image_path.c_str());
75         if (!file)
76                 return false;
77
78         cverb << vbfd << "found " << name;
79         while (file) {
80                 file.read(buffer, sizeof(buffer));
81                 file_crc = calc_crc32(file_crc, 
82                                       reinterpret_cast<unsigned char *>(&buffer[0]),
83                                       file.gcount());
84         }
85         ostringstream message;
86         message << " with crc32 = " << hex << file_crc << endl;
87         cverb << vbfd << message.str();
88         return crc == file_crc;
89 }
90
91 static bool find_debuginfo_file_by_buildid(unsigned char * buildid, string & debug_filename)
92 {
93         size_t build_id_fname_size = strlen (DEBUGDIR) + (sizeof "/.build-id/" - 1) + 1
94                         + (2 * build_id_size) + (sizeof ".debug" - 1) + 1;
95         char * build_id_fname = (char *) xmalloc(build_id_fname_size);
96         char * sptr = build_id_fname;
97         unsigned char * bptr = buildid;
98         bool retval = false;
99         size_t build_id_segment_len = strlen("/.build-id/");
100
101
102         memcpy(sptr, DEBUGDIR, strlen(DEBUGDIR));
103         sptr += strlen(DEBUGDIR);
104         memcpy(sptr, "/.build-id/", build_id_segment_len);
105         sptr += build_id_segment_len;
106         sptr += sprintf(sptr, "%02x", (unsigned) *bptr++);
107         *sptr++ = '/';
108         for (int i = build_id_size - 1; i > 0; i--)
109                 sptr += sprintf(sptr, "%02x", (unsigned) *bptr++);
110
111         strcpy(sptr, ".debug");
112
113         if (access(build_id_fname, R_OK) == 0) {
114                 debug_filename = string(build_id_fname);
115                 retval = true;
116                 cverb << vbfd << "Using build-id file" << endl;
117         }
118         free(build_id_fname);
119         if (!retval)
120                 cverb << vbfd << "build-id file not found; falling back to CRC method." << endl;
121
122         return retval;
123 }
124
125 static bool get_build_id(bfd * ibfd, unsigned char * build_id)
126 {
127         Elf32_Nhdr op_note_hdr;
128         asection * sect;
129         char * ptr;
130         bool retval = false;
131
132         cverb << vbfd << "fetching build-id from runtime binary ...";
133         if (!(sect = bfd_get_section_by_name(ibfd, ".note.gnu.build-id"))) {
134                 if (!(sect = bfd_get_section_by_name(ibfd, ".notes"))) {
135                         cverb << vbfd << " No build-id section found" << endl;
136                         return false;
137                 }
138         }
139
140         bfd_size_type buildid_sect_size = bfd_section_size(ibfd, sect);
141         char * contents = (char *) xmalloc(buildid_sect_size);
142         errno = 0;
143         if (!bfd_get_section_contents(ibfd, sect,
144                                  reinterpret_cast<unsigned char *>(contents),
145                                  static_cast<file_ptr>(0), buildid_sect_size)) {
146                 string msg = "bfd_get_section_contents:get_build_id";
147                 if (errno) {
148                         msg += ": ";
149                         msg += strerror(errno);
150                 }
151                 throw op_fatal_error(msg);
152         }
153
154         ptr = contents;
155         while (ptr < (contents + buildid_sect_size)) {
156                 op_note_hdr.n_namesz = bfd_get_32(ibfd,
157                                                   reinterpret_cast<bfd_byte *>(contents));
158                 op_note_hdr.n_descsz = bfd_get_32(ibfd,
159                                                   reinterpret_cast<bfd_byte *>(contents + 4));
160                 op_note_hdr.n_type = bfd_get_32(ibfd,
161                                                 reinterpret_cast<bfd_byte *>(contents + 8));
162                 ptr += sizeof(op_note_hdr);
163                 if ((op_note_hdr.n_type == NT_GNU_BUILD_ID) &&
164                                 (op_note_hdr.n_namesz == sizeof("GNU")) &&
165                                 (strcmp("GNU", ptr ) == 0)) {
166                         build_id_size = op_note_hdr.n_descsz;
167                         memcpy(build_id, ptr + op_note_hdr.n_namesz, build_id_size);
168                         retval = true;
169                         cverb << vbfd << "Found build-id" << endl;
170                         break;
171                 }
172                 ptr += op_note_hdr.n_namesz + op_note_hdr.n_descsz;
173         }
174         if (!retval)
175                 cverb << vbfd << " No build-id found" << endl;
176         free(contents);
177
178         return retval;
179 }
180
181 bool get_debug_link_info(bfd * ibfd, string & filename, unsigned long & crc32)
182 {
183         asection * sect;
184
185         cverb << vbfd << "fetching .gnu_debuglink section" << endl;
186         sect = bfd_get_section_by_name(ibfd, ".gnu_debuglink");
187         
188         if (sect == NULL)
189                 return false;
190         
191         bfd_size_type debuglink_size = bfd_section_size(ibfd, sect);  
192         char * contents = (char *) xmalloc(debuglink_size);
193         cverb << vbfd
194               << ".gnu_debuglink section has size " << debuglink_size << endl;
195         
196         if (!bfd_get_section_contents(ibfd, sect, 
197                                  reinterpret_cast<unsigned char *>(contents), 
198                                  static_cast<file_ptr>(0), debuglink_size)) {
199                 string msg = "bfd_get_section_contents:get_debug";
200                 if (errno) {
201                         msg += ": ";
202                         msg += strerror(errno);
203                 }
204                 throw op_fatal_error(msg);
205         }
206         
207         /* CRC value is stored after the filename, aligned up to 4 bytes. */
208         size_t filename_len = strlen(contents);
209         size_t crc_offset = filename_len + 1;
210         crc_offset = (crc_offset + 3) & ~3;
211         
212         crc32 = bfd_get_32(ibfd, 
213                                reinterpret_cast<bfd_byte *>(contents + crc_offset));
214         filename = string(contents, filename_len);
215         cverb << vbfd << ".gnu_debuglink filename is " << filename << endl;
216         free(contents);
217         return true;
218 }
219
220
221 /**
222  * With Objective C, we'll get strings like:
223  *
224  * _i_GSUnicodeString__rangeOfCharacterSetFromSet_options_range
225  *
226  * for the symbol name, and:
227  * -[GSUnicodeString rangeOfCharacterFromSet:options:range:]
228  *
229  * for the function name, so we have to do some looser matching
230  * than for other languages (unfortunately, it's not possible
231  * to demangle Objective C symbols).
232  */
233 bool objc_match(string const & sym, string const & method)
234 {
235         if (method.length() < 3)
236                 return false;
237
238         string mangled;
239
240         if (is_prefix(method, "-[")) {
241                 mangled += "_i_";
242         } else if (is_prefix(method, "+[")) {
243                 mangled += "_c_";
244         } else {
245                 return false;
246         }
247
248         string::const_iterator it = method.begin() + 2;
249         string::const_iterator const end = method.end();
250
251         bool found_paren = false;
252
253         for (; it != end; ++it) {
254                 switch (*it) {
255                 case ' ':
256                         mangled += '_';
257                         if (!found_paren)
258                                 mangled += '_';
259                         break;
260                 case ':':
261                         mangled += '_';
262                         break;
263                 case ')':
264                 case ']':
265                         break;
266                 case '(':
267                         found_paren = true;
268                         mangled += '_';
269                         break;
270                 default:
271                         mangled += *it; 
272                 }
273         }
274
275         return sym == mangled;
276 }
277
278
279 /*
280  * With a binary image where some objects are missing debug
281  * info, we can end up attributing to a completely different
282  * function (#484660): bfd_nearest_line() will happily move from one
283  * symbol to the nearest one it can find with debug information.
284  * To mitigate this problem, we check that the symbol name
285  * matches the returned function name.
286  *
287  * However, this check fails in some cases it shouldn't:
288  * Objective C, and C++ static inline functions (as discussed in
289  * GCC bugzilla #11774). So, we have a looser check that
290  * accepts merely a substring, plus some magic for Objective C.
291  *
292  * If even the loose check fails, then we give up.
293  */
294 bool is_correct_function(string const & function, string const & name)
295 {
296         if (name == function)
297                 return true;
298
299         if (objc_match(name, function))
300                 return true;
301
302         // warn the user if we had to use the loose check
303         if (name.find(function) != string::npos) {
304                 static bool warned = false;
305                 if (!warned) {
306                         cerr << "warning: some functions compiled without "
307                              << "debug information may have incorrect source "
308                              << "line attributions" << endl;
309                                 warned = true;
310                 }
311                 cverb << vbfd << "is_correct_function(" << function << ", "
312                       << name << ") fuzzy match." << endl;
313                 return true;
314         }
315
316         return false;
317 }
318
319
320 /*
321  * binutils 2.12 and below have a small bug where functions without a
322  * debug entry at the prologue start do not give a useful line number
323  * from bfd_find_nearest_line(). This can happen with certain gcc
324  * versions such as 2.95.
325  *
326  * We work around this problem by scanning forward for a vma with valid
327  * linenr info, if we can't get a valid line number.  Problem uncovered
328  * by Norbert Kaufmann. The work-around decreases, on the tincas
329  * application, the number of failure to retrieve linenr info from 835
330  * to 173. Most of the remaining are c++ inline functions mainly from
331  * the STL library. Fix #529622
332  */
333 void fixup_linenr(bfd * abfd, asection * section, asymbol ** syms,
334                   string const & name, bfd_vma pc,
335                   char const ** filename, unsigned int * line)
336 {
337         char const * cfilename;
338         char const * function;
339         unsigned int linenr;
340
341         // FIXME: looking at debug info for all gcc version shows than
342         // the same problems can -perhaps- occur for epilog code: find a
343         // samples files with samples in epilog and try opreport -l -g
344         // on it, check it also with opannotate.
345
346         // first restrict the search on a sensible range of vma, 16 is
347         // an intuitive value based on epilog code look
348         size_t max_search = 16;
349         size_t section_size = bfd_section_size(abfd, section);
350         if (pc + max_search > section_size)
351                 max_search = section_size - pc;
352
353         for (size_t i = 1; i < max_search; ++i) {
354                 bool ret = bfd_find_nearest_line(abfd, section, syms, pc + i,
355                                                  &cfilename, &function,
356                                                  &linenr);
357
358                 if (ret && cfilename && function && linenr != 0
359                     && is_correct_function(function, name)) {
360                         *filename = cfilename;
361                         *line = linenr;
362                         return;
363                 }
364         }
365 }
366
367
368 } // namespace anon
369
370
371 bfd * open_bfd(string const & file)
372 {
373         /* bfd keeps its own reference to the filename char *,
374          * so it must have a lifetime longer than the ibfd */
375         bfd * ibfd = bfd_openr(file.c_str(), NULL);
376         if (!ibfd) {
377                 cverb << vbfd << "bfd_openr failed for " << file << endl;
378                 return NULL;
379         }
380
381         check_format(file, &ibfd);
382
383         return ibfd;
384 }
385
386
387 bfd * fdopen_bfd(string const & file, int fd)
388 {
389         /* bfd keeps its own reference to the filename char *,
390          * so it must have a lifetime longer than the ibfd */
391         bfd * ibfd = bfd_fdopenr(file.c_str(), NULL, fd);
392         if (!ibfd) {
393                 cverb << vbfd << "bfd_openr failed for " << file << endl;
394                 return NULL;
395         }
396
397         check_format(file, &ibfd);
398
399         return ibfd;
400 }
401
402
403 bool find_separate_debug_file(bfd * ibfd, string const & filepath_in, 
404                               string & debug_filename, extra_images const & extra)
405 {
406         string filepath(filepath_in);
407         string basename;
408         unsigned long crc32 = 0;
409         // The readelf program uses a char [64], so that's what we'll use.
410         // To my knowledge, the build-id should not be bigger than 20 chars.
411         unsigned char buildid[64];
412         
413         if (get_build_id(ibfd, buildid) &&
414            find_debuginfo_file_by_buildid(buildid, debug_filename))
415                 return true;
416
417         if (!get_debug_link_info(ibfd, basename, crc32))
418                 return false;
419
420         /* Use old method of finding debuginfo file by comparing runtime binary's
421          * CRC with the CRC we calculate from the debuginfo file's contents.
422          * NOTE:  This method breaks on systems where "MiniDebugInfo" is used
423          * since the CRC stored in the runtime binary won't match the compressed
424          * debuginfo file's CRC.  But in practice, we shouldn't ever run into such
425          * a scenario since the build-id should always be available.
426          */
427
428         // Work out the image file's directory prefix
429         string filedir = op_dirname(filepath);
430         // Make sure it starts with /
431         if (filedir.size() > 0 && filedir.at(filedir.size() - 1) != '/')
432                 filedir += '/';
433
434         string first_try(filedir + ".debug/" + basename);
435         string second_try(DEBUGDIR + filedir + basename);
436         string third_try(filedir + basename);
437
438         ostringstream message;
439         message << "looking for debugging file " << basename
440                 << " with crc32 = " << hex << crc32 << endl;
441         cverb << vbfd << message.str();
442
443         if (separate_debug_file_exists(first_try, crc32, extra)) 
444                 debug_filename = first_try; 
445         else if (separate_debug_file_exists(second_try, crc32, extra))
446                 debug_filename = second_try;
447         else if (separate_debug_file_exists(third_try, crc32, extra))
448                 debug_filename = third_try;
449         else
450                 return false;
451         
452         return true;
453 }
454
455
456 bool interesting_symbol(asymbol * sym)
457 {
458         // #717720 some binutils are miscompiled by gcc 2.95, one of the
459         // typical symptom can be catched here.
460         if (!sym->section) {
461                 ostringstream os;
462                 os << "Your version of binutils seems to have a bug.\n"
463                    << "Read http://oprofile.sf.net/faq/#binutilsbug\n";
464                 throw op_runtime_error(os.str());
465         }
466
467         if (!(sym->section->flags & SEC_CODE))
468                 return false;
469
470         // returning true for fix up in op_bfd_symbol()
471         if (!sym->name || sym->name[0] == '\0')
472                 return true;
473         /* ARM assembler internal mapping symbols aren't interesting */
474         if ((strcmp("$a", sym->name) == 0) ||
475             (strcmp("$t", sym->name) == 0) ||
476             (strcmp("$d", sym->name) == 0) ||
477             (strcmp("$x", sym->name) == 0))
478                 return false;
479
480         // C++ exception stuff
481         if (sym->name[0] == '.' && sym->name[1] == 'L')
482                 return false;
483
484         /* This case cannot be moved to boring_symbol(),
485          * because that's only used for duplicate VMAs,
486          * and sometimes this symbol appears at an address
487          * different from all other symbols.
488          */
489         if (!strcmp("gcc2_compiled.", sym->name))
490                 return false;
491
492         /* Commit ab45a0cc5d1cf522c1aef8f22ed512a9aae0dc1c removed a check for
493          * the SEC_LOAD bit.  See the commit message for details why this
494          * was removed.
495          */
496
497         if (sym->flags & BSF_SECTION_SYM)
498                 return false;
499
500         return true;
501 }
502
503
504 bool boring_symbol(op_bfd_symbol const & first, op_bfd_symbol const & second)
505 {
506         if (first.name() == "Letext")
507                 return true;
508         else if (second.name() == "Letext")
509                 return false;
510
511         if (first.name().substr(0, 2) == "??")
512                 return true;
513         else if (second.name().substr(0, 2) == "??")
514                 return false;
515
516         if (first.hidden() && !second.hidden())
517                 return true;
518         else if (!first.hidden() && second.hidden())
519                 return false;
520
521         if (first.name()[0] == '_' && second.name()[0] != '_')
522                 return true;
523         else if (first.name()[0] != '_' && second.name()[0] == '_')
524                 return false;
525
526         if (first.weak() && !second.weak())
527                 return true;
528         else if (!first.weak() && second.weak())
529                 return false;
530
531         return false;
532 }
533
534
535 bool bfd_info::has_debug_info() const
536 {
537         if (!valid())
538                 return false;
539
540         for (asection const * sect = abfd->sections; sect; sect = sect->next) {
541                 if (sect->flags & SEC_DEBUGGING)
542                         return true;
543         }
544
545         return false;
546 }
547
548
549 bfd_info::~bfd_info()
550 {
551         free(synth_syms);
552         close();
553 }
554
555
556 void bfd_info::close()
557 {
558         if (abfd)
559                 bfd_close(abfd);
560 }
561
562 #if SYNTHESIZE_SYMBOLS
563 /**
564  * This function is intended solely for processing ppc64 debuginfo files.
565  * On ppc64 platforms where there is no symbol information in the image bfd,
566  * the debuginfo syms need to be mapped back to the sections of the image bfd
567  * when calling bfd_get_synthetic_symtab() to gather complete symbol information.
568  * That is the purpose of the translate_debuginfo_syms() function.
569  *
570  * This function is only called when processing symbols retrieved from a
571  * debuginfo file that is separate from the actual runtime binary image.
572  * Separate debuginfo files may be needed in two different cases:
573  *   1) the real image is completely stripped, where there is no symbol
574         information at all
575  *   2) the real image has debuginfo stripped, and the user is requesting "-g"
576  *   (src file/line num info)
577 */
578 void bfd_info::translate_debuginfo_syms(asymbol ** dbg_syms, long nr_dbg_syms)
579 {
580         unsigned int img_sect_cnt = 0;
581         bfd_vma vma_adj;
582         bfd * image_bfd = image_bfd_info->abfd;
583         multimap<string, bfd_section *> image_sections;
584
585         for (bfd_section * sect = image_bfd->sections;
586              sect && img_sect_cnt < image_bfd->section_count;
587              sect = sect->next) {
588                 // A comment section marks the end of the needed sections
589                 if (strstr(sect->name, ".comment") == sect->name)
590                         break;
591                 image_sections.insert(pair<string, bfd_section *>(sect->name, sect));
592                 img_sect_cnt++;
593         }
594
595         asymbol * sym = dbg_syms[0];
596         string prev_sect_name = "";
597         bfd_section * matched_section = NULL;
598         vma_adj = image_bfd->start_address - abfd->start_address;
599         for (int i = 0; i < nr_dbg_syms; sym = dbg_syms[++i]) {
600                 bool section_switch;
601
602                 if (strcmp(prev_sect_name.c_str(), sym->section->name)) {
603                         section_switch = true;
604                         prev_sect_name = sym->section->name;
605                 } else {
606                         section_switch = false;
607                 }
608                 if (sym->section->owner && sym->section->owner == abfd) {
609                         if (section_switch ) {
610                                 matched_section = NULL;
611                                 multimap<string, bfd_section *>::iterator it;
612                                 pair<multimap<string, bfd_section *>::iterator,
613                                      multimap<string, bfd_section *>::iterator> range;
614
615                                 range = image_sections.equal_range(sym->section->name);
616                                 for (it = range.first; it != range.second; it++) {
617                                         if ((*it).second->vma == sym->section->vma + vma_adj) {
618                                                 matched_section = (*it).second;
619                                                 if (vma_adj)
620                                                         section_vma_maps[(*it).second->vma] = sym->section->vma;
621                                                 break;
622                                         }
623                                 }
624                         }
625                         if (matched_section) {
626                                 sym->section = matched_section;
627                                 sym->the_bfd = image_bfd;
628                         }
629                 }
630         }
631 }
632
633 bool bfd_info::get_synth_symbols()
634 {
635         const char* targname = bfd_get_target(abfd);
636         // Match elf64-powerpc and elf64-powerpc-freebsd, but not
637         // elf64-powerpcle.  elf64-powerpcle is a different ABI without
638         // function descriptors, so we don't need the synthetic
639         // symbols to have function code marked by a symbol.
640         bool is_elf64_powerpc_target = (!strncmp(targname, "elf64-powerpc", 13)
641                                         && (targname[13] == 0
642                                             || targname[13] == '-'));
643
644         if (!is_elf64_powerpc_target)
645                 return false;
646
647         void * buf;
648         uint tmp;
649         long nr_mini_syms = bfd_read_minisymbols(abfd, 0, &buf, &tmp);
650         if (nr_mini_syms < 1)
651                 return false;
652
653         asymbol ** mini_syms = (asymbol **)buf;
654         buf = NULL;
655         bfd * synth_bfd;
656
657         /* For ppc64, a debuginfo file by itself does not hold enough symbol
658          * information for us to properly attribute samples to symbols.  If
659          * the image file's bfd has no symbols (as in a super-stripped library),
660          * then we need to do the extra processing in translate_debuginfo_syms.
661          */
662         if (image_bfd_info && image_bfd_info->nr_syms == 0) {
663                 translate_debuginfo_syms(mini_syms, nr_mini_syms);
664                 synth_bfd = image_bfd_info->abfd;
665         } else
666                 synth_bfd = abfd;
667         
668         long nr_synth_syms = bfd_get_synthetic_symtab(synth_bfd,
669                                                       nr_mini_syms,
670                                                       mini_syms, 0,
671                                                       NULL, &synth_syms);
672
673         if (nr_synth_syms < 0) {
674                 free(mini_syms);
675                 return false;
676         }
677
678         /* If we called translate_debuginfo_syms() above, then we had to map
679          * the debuginfo symbols' sections to the sections of the runtime binary.
680          * We had to twist ourselves in this knot due to the peculiar requirements
681          * of bfd_get_synthetic_symtab().  While doing this mapping, we cached
682          * the original section VMAs because we need those original values in
683          * order to properly match up sample offsets with debug data.  So now that
684          * we're done with bfd_get_synthetic_symtab, we can restore these section
685          * VMAs.
686          */
687         if (section_vma_maps.size()) {
688                 unsigned int sect_count = 0;
689                 for (bfd_section * sect = synth_bfd->sections;
690                      sect && sect_count < synth_bfd->section_count;
691                      sect = sect->next) {
692                         sect->vma = section_vma_maps[sect->vma];
693                         sect_count++;
694                 }
695         }
696
697
698         cverb << vbfd << "mini_syms: " << dec << nr_mini_syms << hex << endl;
699         cverb << vbfd << "synth_syms: " << dec << nr_synth_syms << hex << endl;
700
701         nr_syms = nr_mini_syms + nr_synth_syms;
702         syms.reset(new asymbol *[nr_syms + 1]);
703
704         for (size_t i = 0; i < (size_t)nr_mini_syms; ++i)
705                 syms[i] = mini_syms[i];
706
707
708         for (size_t i = 0; i < (size_t)nr_synth_syms; ++i)
709                 syms[nr_mini_syms + i] = synth_syms + i;
710         
711
712         free(mini_syms);
713
714         // bfd_canonicalize_symtab does this, so shall we
715         syms[nr_syms] = NULL;
716
717         return true;
718 }
719 #else
720 bool bfd_info::get_synth_symbols()
721 {
722         return false;
723 }
724 #endif /* SYNTHESIZE_SYMBOLS */
725
726
727 void bfd_info::get_symbols()
728 {
729         if (!abfd)
730                 return;
731
732         cverb << vbfd << "bfd_info::get_symbols() for "
733               << bfd_get_filename(abfd) << endl;
734
735         if (get_synth_symbols())
736                 return;
737
738         if (bfd_get_file_flags(abfd) & HAS_SYMS)
739                 nr_syms = bfd_get_symtab_upper_bound(abfd);
740
741         ostringstream message;
742         message << "bfd_get_symtab_upper_bound: " << dec
743                 << nr_syms << hex << endl;
744         cverb << vbfd << message.str();
745
746         nr_syms /= sizeof(asymbol *);
747
748         if (nr_syms < 1) {
749                 if (!image_bfd_info)
750                         return;
751                 syms.reset();
752                 cverb << vbfd << "Debuginfo has debug data only" << endl;
753         } else {
754                 syms.reset(new asymbol *[nr_syms]);
755                 nr_syms = bfd_canonicalize_symtab(abfd, syms.get());
756                 ostringstream message;
757                 message << "bfd_canonicalize_symtab: " << dec
758                         << nr_syms << hex << endl;
759                 cverb << vbfd << message.str();
760         }
761 }
762
763
764 linenr_info const
765 find_nearest_line(bfd_info const & b, op_bfd_symbol const & sym,
766                   bfd_vma offset, bool anon_obj)
767 {
768         char const * function = "";
769         char const * cfilename = "";
770         unsigned int linenr = 0;
771         linenr_info info;
772         bfd * abfd;
773         asymbol ** syms;
774         asection * section = NULL;
775         asymbol * empty_syms[1];
776         bfd_vma pc;
777         bool ret;
778
779         if (!b.valid())
780                 goto fail;
781
782         // take care about artificial symbol
783         if (!sym.symbol())
784                 goto fail;
785
786         abfd = b.abfd;
787         syms = b.syms.get();
788         if (!syms) {
789                 // If this bfd_info object has no syms, that implies that we're
790                 // using a debuginfo bfd_info object that has only debug data.
791                 // This also implies that the passed sym is from the runtime binary,
792                 // and thus it's section is also from the runtime binary.  And
793                 // since section VMA can be different for a runtime binary (prelinked)
794                 // and its associated debuginfo, we need to obtain the debuginfo
795                 // section to pass to the libbfd functions.
796                 asection * sect_candidate;
797                 bfd_vma vma_adj = b.get_image_bfd_info()->abfd->start_address - abfd->start_address;
798                 if (vma_adj == 0)
799                         section = sym.symbol()->section;
800                 for (sect_candidate = abfd->sections;
801                      (sect_candidate != NULL) && (section == NULL);
802                      sect_candidate = sect_candidate->next) {
803                         if (sect_candidate->vma + vma_adj == sym.symbol()->section->vma) {
804                                 section = sect_candidate;
805                         }
806                 }
807                 if (section == NULL) {
808                         cerr << "ERROR: Unable to find section for symbol " << sym.symbol()->name << endl;
809                         goto fail;
810                 }
811                 syms = empty_syms;
812                 syms[0] = NULL;
813
814         } else {
815                 section = sym.symbol()->section;
816         }
817         if (anon_obj)
818                 pc = offset - sym.symbol()->section->vma;
819         else
820                 pc = (sym.value() + offset) - sym.filepos();
821
822         if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
823                 goto fail;
824
825         if (pc >= bfd_section_size(abfd, section))
826                 goto fail;
827
828         ret = bfd_find_nearest_line(abfd, section, syms, pc, &cfilename,
829                                          &function, &linenr);
830
831         if (!ret || !cfilename || !function)
832                 goto fail;
833
834         /*
835          * is_correct_function does not handle the case of static inlines,
836          * but if the linenr is non-zero in the inline case, it is the correct
837          * line number.
838          */
839         if (linenr == 0 && !is_correct_function(function, sym.name()))
840                 goto fail;
841
842         if (linenr == 0) {
843                 fixup_linenr(abfd, section, syms, sym.name(), pc, &cfilename,
844                              &linenr);
845         }
846
847         info.found = true;
848         info.filename = cfilename;
849         info.line = linenr;
850         return info;
851
852 fail:
853         info.found = false;
854         // some stl lacks string::clear()
855         info.filename.erase(info.filename.begin(), info.filename.end());
856         info.line = 0;
857         return info;
858 }