daily update
[deliverable/binutils-gdb.git] / gold / dwarf_reader.cc
CommitLineData
5c2c6c95
ILT
1// dwarf_reader.cc -- parse dwarf2/3 debug information
2
c1027032 3// Copyright 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5c2c6c95
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
23#include "gold.h"
24
04bf7072 25#include <algorithm>
e4e5049b 26#include <vector>
04bf7072 27
5c2c6c95
ILT
28#include "elfcpp_swap.h"
29#include "dwarf.h"
24badc65 30#include "object.h"
4c50553d 31#include "reloc.h"
5c2c6c95 32#include "dwarf_reader.h"
4f787271 33#include "int_encoding.h"
a2e47362 34#include "compressed_output.h"
5c2c6c95 35
62b01cb5 36namespace gold {
5c2c6c95 37
c1027032
CC
38// Class Sized_elf_reloc_mapper
39
40// Initialize the relocation tracker for section RELOC_SHNDX.
41
42template<int size, bool big_endian>
43bool
44Sized_elf_reloc_mapper<size, big_endian>::do_initialize(
45 unsigned int reloc_shndx, unsigned int reloc_type)
46{
47 this->reloc_type_ = reloc_type;
48 return this->track_relocs_.initialize(this->object_, reloc_shndx,
49 reloc_type);
50}
51
52// Looks in the symtab to see what section a symbol is in.
53
54template<int size, bool big_endian>
55unsigned int
56Sized_elf_reloc_mapper<size, big_endian>::symbol_section(
57 unsigned int symndx, Address* value, bool* is_ordinary)
58{
59 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
50ed5eb1 60 gold_assert(static_cast<off_t>((symndx + 1) * symsize) <= this->symtab_size_);
c1027032
CC
61 elfcpp::Sym<size, big_endian> elfsym(this->symtab_ + symndx * symsize);
62 *value = elfsym.get_st_value();
63 return this->object_->adjust_sym_shndx(symndx, elfsym.get_st_shndx(),
64 is_ordinary);
65}
66
67// Return the section index and offset within the section of
68// the target of the relocation for RELOC_OFFSET.
69
70template<int size, bool big_endian>
71unsigned int
72Sized_elf_reloc_mapper<size, big_endian>::do_get_reloc_target(
73 off_t reloc_offset, off_t* target_offset)
74{
75 this->track_relocs_.advance(reloc_offset);
76 if (reloc_offset != this->track_relocs_.next_offset())
77 return 0;
78 unsigned int symndx = this->track_relocs_.next_symndx();
79 typename elfcpp::Elf_types<size>::Elf_Addr value;
80 bool is_ordinary;
81 unsigned int target_shndx = this->symbol_section(symndx, &value,
82 &is_ordinary);
83 if (!is_ordinary)
84 return 0;
85 if (this->reloc_type_ == elfcpp::SHT_RELA)
86 value += this->track_relocs_.next_addend();
87 *target_offset = value;
88 return target_shndx;
89}
90
91static inline Elf_reloc_mapper*
9fc236f3 92make_elf_reloc_mapper(Relobj* object, const unsigned char* symtab,
c1027032
CC
93 off_t symtab_size)
94{
9fc236f3 95 if (object->elfsize() == 32)
c1027032 96 {
9fc236f3
CC
97 if (object->is_big_endian())
98 {
c1027032 99#ifdef HAVE_TARGET_32_BIG
9fc236f3
CC
100 return new Sized_elf_reloc_mapper<32, true>(object, symtab,
101 symtab_size);
102#else
103 gold_unreachable();
c1027032 104#endif
9fc236f3
CC
105 }
106 else
107 {
108#ifdef HAVE_TARGET_32_LITTLE
109 return new Sized_elf_reloc_mapper<32, false>(object, symtab,
110 symtab_size);
111#else
112 gold_unreachable();
c1027032 113#endif
9fc236f3
CC
114 }
115 }
116 else if (object->elfsize() == 64)
117 {
118 if (object->is_big_endian())
119 {
c1027032 120#ifdef HAVE_TARGET_64_BIG
9fc236f3
CC
121 return new Sized_elf_reloc_mapper<64, true>(object, symtab,
122 symtab_size);
123#else
124 gold_unreachable();
c1027032 125#endif
9fc236f3
CC
126 }
127 else
128 {
129#ifdef HAVE_TARGET_64_LITTLE
130 return new Sized_elf_reloc_mapper<64, false>(object, symtab,
131 symtab_size);
132#else
133 gold_unreachable();
134#endif
135 }
c1027032 136 }
9fc236f3
CC
137 else
138 gold_unreachable();
c1027032
CC
139}
140
141// class Dwarf_abbrev_table
142
143void
144Dwarf_abbrev_table::clear_abbrev_codes()
145{
146 for (unsigned int code = 0; code < this->low_abbrev_code_max_; ++code)
147 {
148 if (this->low_abbrev_codes_[code] != NULL)
149 {
150 delete this->low_abbrev_codes_[code];
151 this->low_abbrev_codes_[code] = NULL;
152 }
153 }
154 for (Abbrev_code_table::iterator it = this->high_abbrev_codes_.begin();
155 it != this->high_abbrev_codes_.end();
156 ++it)
157 {
158 if (it->second != NULL)
159 delete it->second;
160 }
161 this->high_abbrev_codes_.clear();
162}
163
164// Read the abbrev table from an object file.
165
166bool
167Dwarf_abbrev_table::do_read_abbrevs(
168 Relobj* object,
169 unsigned int abbrev_shndx,
170 off_t abbrev_offset)
171{
172 this->clear_abbrev_codes();
173
174 // If we don't have relocations, abbrev_shndx will be 0, and
175 // we'll have to hunt for the .debug_abbrev section.
176 if (abbrev_shndx == 0 && this->abbrev_shndx_ > 0)
177 abbrev_shndx = this->abbrev_shndx_;
178 else if (abbrev_shndx == 0)
179 {
180 for (unsigned int i = 1; i < object->shnum(); ++i)
181 {
182 std::string name = object->section_name(i);
183 if (name == ".debug_abbrev")
184 {
185 abbrev_shndx = i;
186 // Correct the offset. For incremental update links, we have a
187 // relocated offset that is relative to the output section, but
188 // here we need an offset relative to the input section.
189 abbrev_offset -= object->output_section_offset(i);
190 break;
191 }
192 }
193 if (abbrev_shndx == 0)
194 return false;
195 }
196
197 // Get the section contents and decompress if necessary.
198 if (abbrev_shndx != this->abbrev_shndx_)
199 {
200 if (this->owns_buffer_ && this->buffer_ != NULL)
201 {
202 delete[] this->buffer_;
203 this->owns_buffer_ = false;
204 }
205
206 section_size_type buffer_size;
207 this->buffer_ =
208 object->decompressed_section_contents(abbrev_shndx,
209 &buffer_size,
210 &this->owns_buffer_);
211 this->buffer_end_ = this->buffer_ + buffer_size;
212 this->abbrev_shndx_ = abbrev_shndx;
213 }
214
215 this->buffer_pos_ = this->buffer_ + abbrev_offset;
216 return true;
217}
218
219// Lookup the abbrev code entry for CODE. This function is called
220// only when the abbrev code is not in the direct lookup table.
221// It may be in the hash table, it may not have been read yet,
222// or it may not exist in the abbrev table.
223
224const Dwarf_abbrev_table::Abbrev_code*
225Dwarf_abbrev_table::do_get_abbrev(unsigned int code)
226{
227 // See if the abbrev code is already in the hash table.
228 Abbrev_code_table::const_iterator it = this->high_abbrev_codes_.find(code);
229 if (it != this->high_abbrev_codes_.end())
230 return it->second;
231
232 // Read and store abbrev code definitions until we find the
233 // one we're looking for.
234 for (;;)
235 {
236 // Read the abbrev code. A zero here indicates the end of the
237 // abbrev table.
238 size_t len;
239 if (this->buffer_pos_ >= this->buffer_end_)
240 return NULL;
241 uint64_t nextcode = read_unsigned_LEB_128(this->buffer_pos_, &len);
242 if (nextcode == 0)
243 {
244 this->buffer_pos_ = this->buffer_end_;
245 return NULL;
246 }
247 this->buffer_pos_ += len;
248
249 // Read the tag.
250 if (this->buffer_pos_ >= this->buffer_end_)
251 return NULL;
252 uint64_t tag = read_unsigned_LEB_128(this->buffer_pos_, &len);
253 this->buffer_pos_ += len;
254
255 // Read the has_children flag.
256 if (this->buffer_pos_ >= this->buffer_end_)
257 return NULL;
258 bool has_children = *this->buffer_pos_ == elfcpp::DW_CHILDREN_yes;
259 this->buffer_pos_ += 1;
260
261 // Read the list of (attribute, form) pairs.
262 Abbrev_code* entry = new Abbrev_code(tag, has_children);
263 for (;;)
264 {
265 // Read the attribute.
266 if (this->buffer_pos_ >= this->buffer_end_)
267 return NULL;
268 uint64_t attr = read_unsigned_LEB_128(this->buffer_pos_, &len);
269 this->buffer_pos_ += len;
270
271 // Read the form.
272 if (this->buffer_pos_ >= this->buffer_end_)
273 return NULL;
274 uint64_t form = read_unsigned_LEB_128(this->buffer_pos_, &len);
275 this->buffer_pos_ += len;
276
277 // A (0,0) pair terminates the list.
278 if (attr == 0 && form == 0)
279 break;
280
281 if (attr == elfcpp::DW_AT_sibling)
282 entry->has_sibling_attribute = true;
283
284 entry->add_attribute(attr, form);
285 }
286
287 this->store_abbrev(nextcode, entry);
288 if (nextcode == code)
289 return entry;
290 }
291
292 return NULL;
293}
294
295// class Dwarf_ranges_table
296
297// Read the ranges table from an object file.
298
299bool
300Dwarf_ranges_table::read_ranges_table(
301 Relobj* object,
302 const unsigned char* symtab,
303 off_t symtab_size,
304 unsigned int ranges_shndx)
305{
306 // If we've already read this abbrev table, return immediately.
307 if (this->ranges_shndx_ > 0
308 && this->ranges_shndx_ == ranges_shndx)
309 return true;
310
311 // If we don't have relocations, ranges_shndx will be 0, and
312 // we'll have to hunt for the .debug_ranges section.
313 if (ranges_shndx == 0 && this->ranges_shndx_ > 0)
314 ranges_shndx = this->ranges_shndx_;
315 else if (ranges_shndx == 0)
316 {
317 for (unsigned int i = 1; i < object->shnum(); ++i)
318 {
319 std::string name = object->section_name(i);
320 if (name == ".debug_ranges")
321 {
322 ranges_shndx = i;
323 this->output_section_offset_ = object->output_section_offset(i);
324 break;
325 }
326 }
327 if (ranges_shndx == 0)
328 return false;
329 }
330
331 // Get the section contents and decompress if necessary.
332 if (ranges_shndx != this->ranges_shndx_)
333 {
334 if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
335 {
336 delete[] this->ranges_buffer_;
337 this->owns_ranges_buffer_ = false;
338 }
339
340 section_size_type buffer_size;
341 this->ranges_buffer_ =
342 object->decompressed_section_contents(ranges_shndx,
343 &buffer_size,
344 &this->owns_ranges_buffer_);
345 this->ranges_buffer_end_ = this->ranges_buffer_ + buffer_size;
346 this->ranges_shndx_ = ranges_shndx;
347 }
348
349 if (this->ranges_reloc_mapper_ != NULL)
350 {
351 delete this->ranges_reloc_mapper_;
352 this->ranges_reloc_mapper_ = NULL;
353 }
354
355 // For incremental objects, we have no relocations.
356 if (object->is_incremental())
357 return true;
358
359 // Find the relocation section for ".debug_ranges".
360 unsigned int reloc_shndx = 0;
361 unsigned int reloc_type = 0;
362 for (unsigned int i = 0; i < object->shnum(); ++i)
363 {
364 reloc_type = object->section_type(i);
365 if ((reloc_type == elfcpp::SHT_REL
366 || reloc_type == elfcpp::SHT_RELA)
367 && object->section_info(i) == ranges_shndx)
368 {
369 reloc_shndx = i;
370 break;
371 }
372 }
373
374 this->ranges_reloc_mapper_ = make_elf_reloc_mapper(object, symtab,
375 symtab_size);
376 this->ranges_reloc_mapper_->initialize(reloc_shndx, reloc_type);
267257d2 377 this->reloc_type_ = reloc_type;
c1027032
CC
378
379 return true;
380}
381
382// Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
383
384Dwarf_range_list*
385Dwarf_ranges_table::read_range_list(
386 Relobj* object,
387 const unsigned char* symtab,
388 off_t symtab_size,
389 unsigned int addr_size,
390 unsigned int ranges_shndx,
391 off_t offset)
392{
393 Dwarf_range_list* ranges;
394
395 if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx))
396 return NULL;
397
398 // Correct the offset. For incremental update links, we have a
399 // relocated offset that is relative to the output section, but
400 // here we need an offset relative to the input section.
401 offset -= this->output_section_offset_;
402
403 // Read the range list at OFFSET.
404 ranges = new Dwarf_range_list();
405 off_t base = 0;
406 for (;
407 this->ranges_buffer_ + offset < this->ranges_buffer_end_;
408 offset += 2 * addr_size)
409 {
410 off_t start;
411 off_t end;
412
413 // Read the raw contents of the section.
414 if (addr_size == 4)
415 {
ed5d6712
CC
416 start = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
417 + offset);
418 end = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
419 + offset + 4);
c1027032
CC
420 }
421 else
422 {
ed5d6712
CC
423 start = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
424 + offset);
425 end = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
426 + offset + 8);
c1027032
CC
427 }
428
429 // Check for relocations and adjust the values.
430 unsigned int shndx1 = 0;
431 unsigned int shndx2 = 0;
432 if (this->ranges_reloc_mapper_ != NULL)
433 {
267257d2
CC
434 shndx1 = this->lookup_reloc(offset, &start);
435 shndx2 = this->lookup_reloc(offset + addr_size, &end);
c1027032
CC
436 }
437
438 // End of list is marked by a pair of zeroes.
439 if (shndx1 == 0 && start == 0 && end == 0)
440 break;
441
442 // A "base address selection entry" is identified by
443 // 0xffffffff for the first value of the pair. The second
444 // value is used as a base for subsequent range list entries.
445 if (shndx1 == 0 && start == -1)
446 base = end;
447 else if (shndx1 == shndx2)
448 {
449 if (shndx1 == 0 || object->is_section_included(shndx1))
450 ranges->add(shndx1, base + start, base + end);
451 }
452 else
453 gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
454 "range list entry are in different sections"),
455 object->name().c_str());
456 }
457
458 return ranges;
459}
460
267257d2
CC
461// Look for a relocation at offset OFF in the range table,
462// and return the section index and offset of the target.
463
464unsigned int
465Dwarf_ranges_table::lookup_reloc(off_t off, off_t* target_off)
466{
467 off_t value;
468 unsigned int shndx =
469 this->ranges_reloc_mapper_->get_reloc_target(off, &value);
470 if (shndx == 0)
471 return 0;
472 if (this->reloc_type_ == elfcpp::SHT_REL)
473 *target_off += value;
474 else
475 *target_off = value;
476 return shndx;
477}
478
c1027032
CC
479// class Dwarf_pubnames_table
480
481// Read the pubnames section SHNDX from the object file.
482
483bool
484Dwarf_pubnames_table::read_section(Relobj* object, unsigned int shndx)
485{
486 section_size_type buffer_size;
487
488 // If we don't have relocations, shndx will be 0, and
489 // we'll have to hunt for the .debug_pubnames/pubtypes section.
490 if (shndx == 0)
491 {
492 const char* name = (this->is_pubtypes_
493 ? ".debug_pubtypes"
494 : ".debug_pubnames");
495 for (unsigned int i = 1; i < object->shnum(); ++i)
496 {
497 if (object->section_name(i) == name)
498 {
499 shndx = i;
500 this->output_section_offset_ = object->output_section_offset(i);
501 break;
502 }
503 }
504 if (shndx == 0)
505 return false;
506 }
507
508 this->buffer_ = object->decompressed_section_contents(shndx,
509 &buffer_size,
510 &this->owns_buffer_);
511 if (this->buffer_ == NULL)
512 return false;
513 this->buffer_end_ = this->buffer_ + buffer_size;
514 return true;
515}
516
517// Read the header for the set at OFFSET.
518
519bool
520Dwarf_pubnames_table::read_header(off_t offset)
521{
522 // Correct the offset. For incremental update links, we have a
523 // relocated offset that is relative to the output section, but
524 // here we need an offset relative to the input section.
525 offset -= this->output_section_offset_;
526
527 if (offset < 0 || offset + 14 >= this->buffer_end_ - this->buffer_)
528 return false;
529
530 const unsigned char* pinfo = this->buffer_ + offset;
531
532 // Read the unit_length field.
ed5d6712 533 uint32_t unit_length = this->dwinfo_->read_from_pointer<32>(pinfo);
c1027032
CC
534 pinfo += 4;
535 if (unit_length == 0xffffffff)
536 {
ed5d6712 537 unit_length = this->dwinfo_->read_from_pointer<64>(pinfo);
c1027032
CC
538 pinfo += 8;
539 this->offset_size_ = 8;
540 }
541 else
542 this->offset_size_ = 4;
543
544 // Check the version.
ed5d6712 545 unsigned int version = this->dwinfo_->read_from_pointer<16>(pinfo);
c1027032
CC
546 pinfo += 2;
547 if (version != 2)
548 return false;
50ed5eb1 549
c1027032
CC
550 // Skip the debug_info_offset and debug_info_size fields.
551 pinfo += 2 * this->offset_size_;
552
553 if (pinfo >= this->buffer_end_)
554 return false;
555
556 this->pinfo_ = pinfo;
557 return true;
558}
559
560// Read the next name from the set.
561
562const char*
563Dwarf_pubnames_table::next_name()
564{
565 const unsigned char* pinfo = this->pinfo_;
566
567 // Read the offset within the CU. If this is zero, we have reached
568 // the end of the list.
569 uint32_t offset;
570 if (this->offset_size_ == 4)
ed5d6712 571 offset = this->dwinfo_->read_from_pointer<32>(&pinfo);
c1027032 572 else
ed5d6712 573 offset = this->dwinfo_->read_from_pointer<64>(&pinfo);
c1027032
CC
574 if (offset == 0)
575 return NULL;
576
577 // Return a pointer to the string at the current location,
578 // and advance the pointer to the next entry.
579 const char* ret = reinterpret_cast<const char*>(pinfo);
580 while (pinfo < this->buffer_end_ && *pinfo != '\0')
581 ++pinfo;
582 if (pinfo < this->buffer_end_)
583 ++pinfo;
584
585 this->pinfo_ = pinfo;
586 return ret;
587}
588
589// class Dwarf_die
590
591Dwarf_die::Dwarf_die(
592 Dwarf_info_reader* dwinfo,
593 off_t die_offset,
594 Dwarf_die* parent)
595 : dwinfo_(dwinfo), parent_(parent), die_offset_(die_offset),
596 child_offset_(0), sibling_offset_(0), abbrev_code_(NULL), attributes_(),
597 attributes_read_(false), name_(NULL), name_off_(-1), linkage_name_(NULL),
598 linkage_name_off_(-1), string_shndx_(0), specification_(0),
599 abstract_origin_(0)
600{
601 size_t len;
602 const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset);
603 if (pdie == NULL)
604 return;
605 unsigned int code = read_unsigned_LEB_128(pdie, &len);
606 if (code == 0)
607 {
608 if (parent != NULL)
609 parent->set_sibling_offset(die_offset + len);
610 return;
611 }
612 this->attr_offset_ = len;
613
614 // Lookup the abbrev code in the abbrev table.
615 this->abbrev_code_ = dwinfo->get_abbrev(code);
616}
617
618// Read all the attributes of the DIE.
619
620bool
621Dwarf_die::read_attributes()
622{
623 if (this->attributes_read_)
624 return true;
625
626 gold_assert(this->abbrev_code_ != NULL);
627
628 const unsigned char* pdie =
629 this->dwinfo_->buffer_at_offset(this->die_offset_);
630 if (pdie == NULL)
631 return false;
632 const unsigned char* pattr = pdie + this->attr_offset_;
633
634 unsigned int nattr = this->abbrev_code_->attributes.size();
635 this->attributes_.reserve(nattr);
636 for (unsigned int i = 0; i < nattr; ++i)
637 {
638 size_t len;
639 unsigned int attr = this->abbrev_code_->attributes[i].attr;
640 unsigned int form = this->abbrev_code_->attributes[i].form;
641 if (form == elfcpp::DW_FORM_indirect)
642 {
643 form = read_unsigned_LEB_128(pattr, &len);
644 pattr += len;
645 }
646 off_t attr_off = this->die_offset_ + (pattr - pdie);
647 bool ref_form = false;
648 Attribute_value attr_value;
649 attr_value.attr = attr;
650 attr_value.form = form;
651 attr_value.aux.shndx = 0;
652 switch(form)
653 {
c1027032
CC
654 case elfcpp::DW_FORM_flag_present:
655 attr_value.val.intval = 1;
656 break;
657 case elfcpp::DW_FORM_strp:
658 {
659 off_t str_off;
660 if (this->dwinfo_->offset_size() == 4)
ed5d6712 661 str_off = this->dwinfo_->read_from_pointer<32>(&pattr);
c1027032 662 else
ed5d6712 663 str_off = this->dwinfo_->read_from_pointer<64>(&pattr);
c1027032
CC
664 unsigned int shndx =
665 this->dwinfo_->lookup_reloc(attr_off, &str_off);
666 attr_value.aux.shndx = shndx;
667 attr_value.val.refval = str_off;
668 break;
669 }
670 case elfcpp::DW_FORM_sec_offset:
671 {
672 off_t sec_off;
673 if (this->dwinfo_->offset_size() == 4)
ed5d6712 674 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
c1027032 675 else
ed5d6712 676 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
c1027032
CC
677 unsigned int shndx =
678 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
679 attr_value.aux.shndx = shndx;
680 attr_value.val.refval = sec_off;
681 ref_form = true;
682 break;
683 }
684 case elfcpp::DW_FORM_addr:
685 case elfcpp::DW_FORM_ref_addr:
686 {
687 off_t sec_off;
688 if (this->dwinfo_->address_size() == 4)
ed5d6712 689 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
c1027032 690 else
ed5d6712 691 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
c1027032
CC
692 unsigned int shndx =
693 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
694 attr_value.aux.shndx = shndx;
695 attr_value.val.refval = sec_off;
696 ref_form = true;
697 break;
698 }
699 case elfcpp::DW_FORM_block1:
700 attr_value.aux.blocklen = *pattr++;
701 attr_value.val.blockval = pattr;
702 pattr += attr_value.aux.blocklen;
703 break;
704 case elfcpp::DW_FORM_block2:
ed5d6712
CC
705 attr_value.aux.blocklen =
706 this->dwinfo_->read_from_pointer<16>(&pattr);
c1027032
CC
707 attr_value.val.blockval = pattr;
708 pattr += attr_value.aux.blocklen;
709 break;
710 case elfcpp::DW_FORM_block4:
ed5d6712
CC
711 attr_value.aux.blocklen =
712 this->dwinfo_->read_from_pointer<32>(&pattr);
c1027032
CC
713 attr_value.val.blockval = pattr;
714 pattr += attr_value.aux.blocklen;
715 break;
716 case elfcpp::DW_FORM_block:
717 case elfcpp::DW_FORM_exprloc:
718 attr_value.aux.blocklen = read_unsigned_LEB_128(pattr, &len);
719 attr_value.val.blockval = pattr + len;
720 pattr += len + attr_value.aux.blocklen;
721 break;
722 case elfcpp::DW_FORM_data1:
723 case elfcpp::DW_FORM_flag:
724 attr_value.val.intval = *pattr++;
725 break;
726 case elfcpp::DW_FORM_ref1:
727 attr_value.val.refval = *pattr++;
728 ref_form = true;
729 break;
730 case elfcpp::DW_FORM_data2:
ed5d6712
CC
731 attr_value.val.intval =
732 this->dwinfo_->read_from_pointer<16>(&pattr);
c1027032
CC
733 break;
734 case elfcpp::DW_FORM_ref2:
ed5d6712
CC
735 attr_value.val.refval =
736 this->dwinfo_->read_from_pointer<16>(&pattr);
c1027032
CC
737 ref_form = true;
738 break;
739 case elfcpp::DW_FORM_data4:
740 {
741 off_t sec_off;
ed5d6712 742 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
c1027032
CC
743 unsigned int shndx =
744 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
745 attr_value.aux.shndx = shndx;
746 attr_value.val.intval = sec_off;
747 break;
748 }
749 case elfcpp::DW_FORM_ref4:
750 {
751 off_t sec_off;
ed5d6712 752 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
c1027032
CC
753 unsigned int shndx =
754 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
755 attr_value.aux.shndx = shndx;
756 attr_value.val.refval = sec_off;
757 ref_form = true;
758 break;
759 }
760 case elfcpp::DW_FORM_data8:
761 {
762 off_t sec_off;
ed5d6712 763 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
c1027032
CC
764 unsigned int shndx =
765 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
766 attr_value.aux.shndx = shndx;
767 attr_value.val.intval = sec_off;
768 break;
769 }
770 case elfcpp::DW_FORM_ref_sig8:
ed5d6712
CC
771 attr_value.val.uintval =
772 this->dwinfo_->read_from_pointer<64>(&pattr);
c1027032
CC
773 break;
774 case elfcpp::DW_FORM_ref8:
775 {
776 off_t sec_off;
ed5d6712 777 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
c1027032
CC
778 unsigned int shndx =
779 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
780 attr_value.aux.shndx = shndx;
781 attr_value.val.refval = sec_off;
782 ref_form = true;
783 break;
784 }
785 case elfcpp::DW_FORM_ref_udata:
786 attr_value.val.refval = read_unsigned_LEB_128(pattr, &len);
787 ref_form = true;
788 pattr += len;
789 break;
790 case elfcpp::DW_FORM_udata:
d2d60eef
CC
791 case elfcpp::DW_FORM_GNU_addr_index:
792 case elfcpp::DW_FORM_GNU_str_index:
c1027032
CC
793 attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
794 pattr += len;
795 break;
796 case elfcpp::DW_FORM_sdata:
797 attr_value.val.intval = read_signed_LEB_128(pattr, &len);
798 pattr += len;
799 break;
800 case elfcpp::DW_FORM_string:
801 attr_value.val.stringval = reinterpret_cast<const char*>(pattr);
802 len = strlen(attr_value.val.stringval);
803 pattr += len + 1;
804 break;
805 default:
806 return false;
807 }
808
809 // Cache the most frequently-requested attributes.
810 switch (attr)
811 {
812 case elfcpp::DW_AT_name:
813 if (form == elfcpp::DW_FORM_string)
814 this->name_ = attr_value.val.stringval;
815 else if (form == elfcpp::DW_FORM_strp)
816 {
817 // All indirect strings should refer to the same
818 // string section, so we just save the last one seen.
819 this->string_shndx_ = attr_value.aux.shndx;
820 this->name_off_ = attr_value.val.refval;
821 }
822 break;
823 case elfcpp::DW_AT_linkage_name:
824 case elfcpp::DW_AT_MIPS_linkage_name:
825 if (form == elfcpp::DW_FORM_string)
826 this->linkage_name_ = attr_value.val.stringval;
827 else if (form == elfcpp::DW_FORM_strp)
828 {
829 // All indirect strings should refer to the same
830 // string section, so we just save the last one seen.
831 this->string_shndx_ = attr_value.aux.shndx;
832 this->linkage_name_off_ = attr_value.val.refval;
833 }
834 break;
835 case elfcpp::DW_AT_specification:
836 if (ref_form)
837 this->specification_ = attr_value.val.refval;
838 break;
839 case elfcpp::DW_AT_abstract_origin:
840 if (ref_form)
841 this->abstract_origin_ = attr_value.val.refval;
842 break;
843 case elfcpp::DW_AT_sibling:
844 if (ref_form && attr_value.aux.shndx == 0)
845 this->sibling_offset_ = attr_value.val.refval;
846 default:
847 break;
848 }
849
850 this->attributes_.push_back(attr_value);
851 }
852
853 // Now that we know where the next DIE begins, record the offset
854 // to avoid later recalculation.
855 if (this->has_children())
856 this->child_offset_ = this->die_offset_ + (pattr - pdie);
857 else
858 this->sibling_offset_ = this->die_offset_ + (pattr - pdie);
859
860 this->attributes_read_ = true;
861 return true;
862}
863
864// Skip all the attributes of the DIE and return the offset of the next DIE.
865
866off_t
867Dwarf_die::skip_attributes()
868{
c1027032
CC
869 gold_assert(this->abbrev_code_ != NULL);
870
871 const unsigned char* pdie =
872 this->dwinfo_->buffer_at_offset(this->die_offset_);
873 if (pdie == NULL)
874 return 0;
875 const unsigned char* pattr = pdie + this->attr_offset_;
876
877 for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i)
878 {
879 size_t len;
880 unsigned int form = this->abbrev_code_->attributes[i].form;
881 if (form == elfcpp::DW_FORM_indirect)
882 {
883 form = read_unsigned_LEB_128(pattr, &len);
884 pattr += len;
885 }
886 switch(form)
887 {
c1027032
CC
888 case elfcpp::DW_FORM_flag_present:
889 break;
890 case elfcpp::DW_FORM_strp:
891 case elfcpp::DW_FORM_sec_offset:
892 pattr += this->dwinfo_->offset_size();
893 break;
894 case elfcpp::DW_FORM_addr:
895 case elfcpp::DW_FORM_ref_addr:
896 pattr += this->dwinfo_->address_size();
897 break;
898 case elfcpp::DW_FORM_block1:
899 pattr += 1 + *pattr;
900 break;
901 case elfcpp::DW_FORM_block2:
902 {
903 uint16_t block_size;
ed5d6712 904 block_size = this->dwinfo_->read_from_pointer<16>(&pattr);
c1027032
CC
905 pattr += block_size;
906 break;
907 }
908 case elfcpp::DW_FORM_block4:
909 {
910 uint32_t block_size;
ed5d6712 911 block_size = this->dwinfo_->read_from_pointer<32>(&pattr);
c1027032
CC
912 pattr += block_size;
913 break;
914 }
915 case elfcpp::DW_FORM_block:
916 case elfcpp::DW_FORM_exprloc:
917 {
918 uint64_t block_size;
919 block_size = read_unsigned_LEB_128(pattr, &len);
920 pattr += len + block_size;
921 break;
922 }
923 case elfcpp::DW_FORM_data1:
924 case elfcpp::DW_FORM_ref1:
925 case elfcpp::DW_FORM_flag:
926 pattr += 1;
927 break;
928 case elfcpp::DW_FORM_data2:
929 case elfcpp::DW_FORM_ref2:
930 pattr += 2;
931 break;
932 case elfcpp::DW_FORM_data4:
933 case elfcpp::DW_FORM_ref4:
934 pattr += 4;
935 break;
936 case elfcpp::DW_FORM_data8:
937 case elfcpp::DW_FORM_ref8:
938 case elfcpp::DW_FORM_ref_sig8:
939 pattr += 8;
940 break;
941 case elfcpp::DW_FORM_ref_udata:
942 case elfcpp::DW_FORM_udata:
d2d60eef
CC
943 case elfcpp::DW_FORM_GNU_addr_index:
944 case elfcpp::DW_FORM_GNU_str_index:
c1027032
CC
945 read_unsigned_LEB_128(pattr, &len);
946 pattr += len;
947 break;
948 case elfcpp::DW_FORM_sdata:
949 read_signed_LEB_128(pattr, &len);
950 pattr += len;
951 break;
952 case elfcpp::DW_FORM_string:
953 len = strlen(reinterpret_cast<const char*>(pattr));
954 pattr += len + 1;
955 break;
956 default:
957 return 0;
958 }
959 }
960
961 return this->die_offset_ + (pattr - pdie);
962}
963
964// Get the name of the DIE and cache it.
965
966void
967Dwarf_die::set_name()
968{
969 if (this->name_ != NULL || !this->read_attributes())
970 return;
971 if (this->name_off_ != -1)
972 this->name_ = this->dwinfo_->get_string(this->name_off_,
973 this->string_shndx_);
974}
975
976// Get the linkage name of the DIE and cache it.
977
978void
979Dwarf_die::set_linkage_name()
980{
981 if (this->linkage_name_ != NULL || !this->read_attributes())
982 return;
983 if (this->linkage_name_off_ != -1)
984 this->linkage_name_ = this->dwinfo_->get_string(this->linkage_name_off_,
985 this->string_shndx_);
986}
987
988// Return the value of attribute ATTR.
989
990const Dwarf_die::Attribute_value*
991Dwarf_die::attribute(unsigned int attr)
992{
993 if (!this->read_attributes())
994 return NULL;
995 for (unsigned int i = 0; i < this->attributes_.size(); ++i)
996 {
997 if (this->attributes_[i].attr == attr)
998 return &this->attributes_[i];
999 }
1000 return NULL;
1001}
1002
1003const char*
1004Dwarf_die::string_attribute(unsigned int attr)
1005{
1006 const Attribute_value* attr_val = this->attribute(attr);
1007 if (attr_val == NULL)
1008 return NULL;
1009 switch (attr_val->form)
1010 {
1011 case elfcpp::DW_FORM_string:
1012 return attr_val->val.stringval;
1013 case elfcpp::DW_FORM_strp:
1014 return this->dwinfo_->get_string(attr_val->val.refval,
1015 attr_val->aux.shndx);
1016 default:
1017 return NULL;
1018 }
1019}
1020
1021int64_t
1022Dwarf_die::int_attribute(unsigned int attr)
1023{
1024 const Attribute_value* attr_val = this->attribute(attr);
1025 if (attr_val == NULL)
1026 return 0;
1027 switch (attr_val->form)
1028 {
c1027032
CC
1029 case elfcpp::DW_FORM_flag_present:
1030 case elfcpp::DW_FORM_data1:
1031 case elfcpp::DW_FORM_flag:
1032 case elfcpp::DW_FORM_data2:
1033 case elfcpp::DW_FORM_data4:
1034 case elfcpp::DW_FORM_data8:
1035 case elfcpp::DW_FORM_sdata:
1036 return attr_val->val.intval;
1037 default:
1038 return 0;
1039 }
1040}
1041
1042uint64_t
1043Dwarf_die::uint_attribute(unsigned int attr)
1044{
1045 const Attribute_value* attr_val = this->attribute(attr);
1046 if (attr_val == NULL)
1047 return 0;
1048 switch (attr_val->form)
1049 {
c1027032
CC
1050 case elfcpp::DW_FORM_flag_present:
1051 case elfcpp::DW_FORM_data1:
1052 case elfcpp::DW_FORM_flag:
1053 case elfcpp::DW_FORM_data4:
1054 case elfcpp::DW_FORM_data8:
1055 case elfcpp::DW_FORM_ref_sig8:
1056 case elfcpp::DW_FORM_udata:
1057 return attr_val->val.uintval;
1058 default:
1059 return 0;
1060 }
1061}
1062
1063off_t
1064Dwarf_die::ref_attribute(unsigned int attr, unsigned int* shndx)
1065{
1066 const Attribute_value* attr_val = this->attribute(attr);
1067 if (attr_val == NULL)
1068 return -1;
1069 switch (attr_val->form)
1070 {
1071 case elfcpp::DW_FORM_sec_offset:
1072 case elfcpp::DW_FORM_addr:
1073 case elfcpp::DW_FORM_ref_addr:
1074 case elfcpp::DW_FORM_ref1:
1075 case elfcpp::DW_FORM_ref2:
1076 case elfcpp::DW_FORM_ref4:
1077 case elfcpp::DW_FORM_ref8:
1078 case elfcpp::DW_FORM_ref_udata:
1079 *shndx = attr_val->aux.shndx;
1080 return attr_val->val.refval;
1081 case elfcpp::DW_FORM_ref_sig8:
1082 *shndx = attr_val->aux.shndx;
1083 return attr_val->val.uintval;
1084 case elfcpp::DW_FORM_data4:
1085 case elfcpp::DW_FORM_data8:
1086 *shndx = attr_val->aux.shndx;
1087 return attr_val->val.intval;
1088 default:
1089 return -1;
1090 }
1091}
1092
57923f48
MW
1093off_t
1094Dwarf_die::address_attribute(unsigned int attr, unsigned int* shndx)
1095{
1096 const Attribute_value* attr_val = this->attribute(attr);
1097 if (attr_val == NULL || attr_val->form != elfcpp::DW_FORM_addr)
1098 return -1;
1099
1100 *shndx = attr_val->aux.shndx;
1101 return attr_val->val.refval;
1102}
1103
c1027032
CC
1104// Return the offset of this DIE's first child.
1105
1106off_t
1107Dwarf_die::child_offset()
1108{
1109 gold_assert(this->abbrev_code_ != NULL);
1110 if (!this->has_children())
1111 return 0;
1112 if (this->child_offset_ == 0)
1113 this->child_offset_ = this->skip_attributes();
1114 return this->child_offset_;
1115}
1116
1117// Return the offset of this DIE's next sibling.
1118
1119off_t
1120Dwarf_die::sibling_offset()
1121{
1122 gold_assert(this->abbrev_code_ != NULL);
1123
1124 if (this->sibling_offset_ != 0)
1125 return this->sibling_offset_;
1126
1127 if (!this->has_children())
1128 {
1129 this->sibling_offset_ = this->skip_attributes();
1130 return this->sibling_offset_;
1131 }
1132
1133 if (this->has_sibling_attribute())
1134 {
1135 if (!this->read_attributes())
1136 return 0;
1137 if (this->sibling_offset_ != 0)
1138 return this->sibling_offset_;
1139 }
1140
1141 // Skip over the children.
1142 off_t child_offset = this->child_offset();
1143 while (child_offset > 0)
1144 {
1145 Dwarf_die die(this->dwinfo_, child_offset, this);
1146 // The Dwarf_die ctor will set this DIE's sibling offset
1147 // when it reads a zero abbrev code.
1148 if (die.tag() == 0)
1149 break;
1150 child_offset = die.sibling_offset();
1151 }
1152
1153 // This should be set by now. If not, there was a problem reading
1154 // the DWARF info, and we return 0.
1155 return this->sibling_offset_;
1156}
1157
1158// class Dwarf_info_reader
1159
c1027032
CC
1160// Begin parsing the debug info. This calls visit_compilation_unit()
1161// or visit_type_unit() for each compilation or type unit found in the
1162// section, and visit_die() for each top-level DIE.
1163
1164void
1165Dwarf_info_reader::parse()
1166{
9fc236f3 1167 if (this->object_->is_big_endian())
c1027032 1168 {
9fc236f3
CC
1169#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1170 this->do_parse<true>();
1171#else
1172 gold_unreachable();
c1027032 1173#endif
9fc236f3
CC
1174 }
1175 else
1176 {
1177#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1178 this->do_parse<false>();
1179#else
1180 gold_unreachable();
c1027032 1181#endif
c1027032
CC
1182 }
1183}
1184
1185template<bool big_endian>
1186void
1187Dwarf_info_reader::do_parse()
1188{
1189 // Get the section contents and decompress if necessary.
1190 section_size_type buffer_size;
1191 bool buffer_is_new;
1192 this->buffer_ = this->object_->decompressed_section_contents(this->shndx_,
1193 &buffer_size,
1194 &buffer_is_new);
1195 if (this->buffer_ == NULL || buffer_size == 0)
1196 return;
1197 this->buffer_end_ = this->buffer_ + buffer_size;
1198
1199 // The offset of this input section in the output section.
1200 off_t section_offset = this->object_->output_section_offset(this->shndx_);
1201
1202 // Start tracking relocations for this section.
1203 this->reloc_mapper_ = make_elf_reloc_mapper(this->object_, this->symtab_,
1204 this->symtab_size_);
1205 this->reloc_mapper_->initialize(this->reloc_shndx_, this->reloc_type_);
1206
1207 // Loop over compilation units (or type units).
8787852d 1208 unsigned int abbrev_shndx = this->abbrev_shndx_;
c1027032
CC
1209 off_t abbrev_offset = 0;
1210 const unsigned char* pinfo = this->buffer_;
1211 while (pinfo < this->buffer_end_)
1212 {
1213 // Read the compilation (or type) unit header.
1214 const unsigned char* cu_start = pinfo;
1215 this->cu_offset_ = cu_start - this->buffer_;
1216 this->cu_length_ = this->buffer_end_ - cu_start;
1217
1218 // Read unit_length (4 or 12 bytes).
1219 if (!this->check_buffer(pinfo + 4))
1220 break;
1221 uint32_t unit_length =
1222 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1223 pinfo += 4;
1224 if (unit_length == 0xffffffff)
1225 {
1226 if (!this->check_buffer(pinfo + 8))
1227 break;
1228 unit_length = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1229 pinfo += 8;
1230 this->offset_size_ = 8;
1231 }
1232 else
1233 this->offset_size_ = 4;
1234 if (!this->check_buffer(pinfo + unit_length))
1235 break;
1236 const unsigned char* cu_end = pinfo + unit_length;
1237 this->cu_length_ = cu_end - cu_start;
1238 if (!this->check_buffer(pinfo + 2 + this->offset_size_ + 1))
1239 break;
1240
1241 // Read version (2 bytes).
1242 this->cu_version_ =
1243 elfcpp::Swap_unaligned<16, big_endian>::readval(pinfo);
1244 pinfo += 2;
1245
1246 // Read debug_abbrev_offset (4 or 8 bytes).
1247 if (this->offset_size_ == 4)
1248 abbrev_offset = elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1249 else
1250 abbrev_offset = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1251 if (this->reloc_shndx_ > 0)
1252 {
1253 off_t reloc_offset = pinfo - this->buffer_;
1254 off_t value;
1255 abbrev_shndx =
1256 this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
1257 if (abbrev_shndx == 0)
1258 return;
1259 if (this->reloc_type_ == elfcpp::SHT_REL)
1260 abbrev_offset += value;
1261 else
1262 abbrev_offset = value;
1263 }
1264 pinfo += this->offset_size_;
1265
1266 // Read address_size (1 byte).
1267 this->address_size_ = *pinfo++;
1268
1269 // For type units, read the two extra fields.
1270 uint64_t signature = 0;
1271 off_t type_offset = 0;
1272 if (this->is_type_unit_)
1273 {
1274 if (!this->check_buffer(pinfo + 8 + this->offset_size_))
1275 break;
1276
1277 // Read type_signature (8 bytes).
1278 signature = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1279 pinfo += 8;
1280
1281 // Read type_offset (4 or 8 bytes).
1282 if (this->offset_size_ == 4)
1283 type_offset =
1284 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1285 else
1286 type_offset =
1287 elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1288 pinfo += this->offset_size_;
1289 }
1290
1291 // Read the .debug_abbrev table.
1292 this->abbrev_table_.read_abbrevs(this->object_, abbrev_shndx,
1293 abbrev_offset);
1294
1295 // Visit the root DIE.
1296 Dwarf_die root_die(this,
1297 pinfo - (this->buffer_ + this->cu_offset_),
1298 NULL);
1299 if (root_die.tag() != 0)
1300 {
1301 // Visit the CU or TU.
1302 if (this->is_type_unit_)
1303 this->visit_type_unit(section_offset + this->cu_offset_,
d5834edb
CC
1304 cu_end - cu_start, type_offset, signature,
1305 &root_die);
c1027032
CC
1306 else
1307 this->visit_compilation_unit(section_offset + this->cu_offset_,
1308 cu_end - cu_start, &root_die);
1309 }
1310
1311 // Advance to the next CU.
1312 pinfo = cu_end;
1313 }
1314
1315 if (buffer_is_new)
1316 {
1317 delete[] this->buffer_;
1318 this->buffer_ = NULL;
1319 }
1320}
1321
1322// Read the DWARF string table.
1323
1324bool
1325Dwarf_info_reader::do_read_string_table(unsigned int string_shndx)
1326{
1327 Relobj* object = this->object_;
1328
1329 // If we don't have relocations, string_shndx will be 0, and
1330 // we'll have to hunt for the .debug_str section.
1331 if (string_shndx == 0)
1332 {
1333 for (unsigned int i = 1; i < this->object_->shnum(); ++i)
1334 {
1335 std::string name = object->section_name(i);
1336 if (name == ".debug_str")
1337 {
1338 string_shndx = i;
1339 this->string_output_section_offset_ =
1340 object->output_section_offset(i);
1341 break;
1342 }
1343 }
1344 if (string_shndx == 0)
1345 return false;
1346 }
1347
1348 if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
1349 {
1350 delete[] this->string_buffer_;
1351 this->owns_string_buffer_ = false;
1352 }
1353
1354 // Get the secton contents and decompress if necessary.
1355 section_size_type buffer_size;
1356 const unsigned char* buffer =
1357 object->decompressed_section_contents(string_shndx,
1358 &buffer_size,
1359 &this->owns_string_buffer_);
1360 this->string_buffer_ = reinterpret_cast<const char*>(buffer);
1361 this->string_buffer_end_ = this->string_buffer_ + buffer_size;
1362 this->string_shndx_ = string_shndx;
1363 return true;
1364}
1365
ed5d6712
CC
1366// Read a possibly unaligned integer of SIZE.
1367template <int valsize>
1368inline typename elfcpp::Valtype_base<valsize>::Valtype
1369Dwarf_info_reader::read_from_pointer(const unsigned char* source)
1370{
1371 typename elfcpp::Valtype_base<valsize>::Valtype return_value;
1372 if (this->object_->is_big_endian())
1373 return_value = elfcpp::Swap_unaligned<valsize, true>::readval(source);
1374 else
1375 return_value = elfcpp::Swap_unaligned<valsize, false>::readval(source);
1376 return return_value;
1377}
1378
1379// Read a possibly unaligned integer of SIZE. Update SOURCE after read.
1380template <int valsize>
1381inline typename elfcpp::Valtype_base<valsize>::Valtype
1382Dwarf_info_reader::read_from_pointer(const unsigned char** source)
1383{
1384 typename elfcpp::Valtype_base<valsize>::Valtype return_value;
1385 if (this->object_->is_big_endian())
1386 return_value = elfcpp::Swap_unaligned<valsize, true>::readval(*source);
1387 else
1388 return_value = elfcpp::Swap_unaligned<valsize, false>::readval(*source);
1389 *source += valsize / 8;
1390 return return_value;
1391}
1392
c1027032
CC
1393// Look for a relocation at offset ATTR_OFF in the dwarf info,
1394// and return the section index and offset of the target.
1395
1396unsigned int
1397Dwarf_info_reader::lookup_reloc(off_t attr_off, off_t* target_off)
1398{
1399 off_t value;
1400 attr_off += this->cu_offset_;
1401 unsigned int shndx = this->reloc_mapper_->get_reloc_target(attr_off, &value);
1402 if (shndx == 0)
1403 return 0;
1404 if (this->reloc_type_ == elfcpp::SHT_REL)
1405 *target_off += value;
1406 else
1407 *target_off = value;
1408 return shndx;
1409}
1410
1411// Return a string from the DWARF string table.
1412
1413const char*
1414Dwarf_info_reader::get_string(off_t str_off, unsigned int string_shndx)
1415{
1416 if (!this->read_string_table(string_shndx))
1417 return NULL;
1418
1419 // Correct the offset. For incremental update links, we have a
1420 // relocated offset that is relative to the output section, but
1421 // here we need an offset relative to the input section.
1422 str_off -= this->string_output_section_offset_;
1423
1424 const char* p = this->string_buffer_ + str_off;
1425
1426 if (p < this->string_buffer_ || p >= this->string_buffer_end_)
1427 return NULL;
1428
1429 return p;
1430}
1431
1432// The following are default, do-nothing, implementations of the
1433// hook methods normally provided by a derived class. We provide
1434// default implementations rather than no implementation so that
1435// a derived class needs to implement only the hooks that it needs
1436// to use.
1437
1438// Process a compilation unit and parse its child DIE.
1439
1440void
1441Dwarf_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die*)
1442{
1443}
1444
1445// Process a type unit and parse its child DIE.
1446
1447void
d5834edb 1448Dwarf_info_reader::visit_type_unit(off_t, off_t, off_t, uint64_t, Dwarf_die*)
c1027032
CC
1449{
1450}
1451
a68a081d
CC
1452// Print a warning about a corrupt debug section.
1453
1454void
1455Dwarf_info_reader::warn_corrupt_debug_section() const
1456{
1457 gold_warning(_("%s: corrupt debug info in %s"),
1458 this->object_->name().c_str(),
1459 this->object_->section_name(this->shndx_).c_str());
1460}
1461
c1027032
CC
1462// class Sized_dwarf_line_info
1463
5c2c6c95
ILT
1464struct LineStateMachine
1465{
1466 int file_num;
1467 uint64_t address;
1468 int line_num;
1469 int column_num;
1470 unsigned int shndx; // the section address refers to
1471 bool is_stmt; // stmt means statement.
1472 bool basic_block;
1473 bool end_sequence;
1474};
1475
1476static void
1477ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
1478{
1479 lsm->file_num = 1;
1480 lsm->address = 0;
1481 lsm->line_num = 1;
1482 lsm->column_num = 0;
338f2eba 1483 lsm->shndx = -1U;
5c2c6c95
ILT
1484 lsm->is_stmt = default_is_stmt;
1485 lsm->basic_block = false;
1486 lsm->end_sequence = false;
1487}
1488
24badc65 1489template<int size, bool big_endian>
5dd8762a
CC
1490Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(
1491 Object* object,
1492 unsigned int read_shndx)
1493 : data_valid_(false), buffer_(NULL), buffer_start_(NULL),
c1027032
CC
1494 reloc_mapper_(NULL), symtab_buffer_(NULL), directories_(), files_(),
1495 current_header_index_(-1)
24badc65
ILT
1496{
1497 unsigned int debug_shndx;
5dd8762a 1498
ab8056e0
CC
1499 for (debug_shndx = 1; debug_shndx < object->shnum(); ++debug_shndx)
1500 {
1501 // FIXME: do this more efficiently: section_name() isn't super-fast
1502 std::string name = object->section_name(debug_shndx);
1503 if (name == ".debug_line" || name == ".zdebug_line")
1504 {
1505 section_size_type buffer_size;
5dd8762a
CC
1506 bool is_new = false;
1507 this->buffer_ = object->decompressed_section_contents(debug_shndx,
1508 &buffer_size,
1509 &is_new);
1510 if (is_new)
1511 this->buffer_start_ = this->buffer_;
ab8056e0
CC
1512 this->buffer_end_ = this->buffer_ + buffer_size;
1513 break;
1514 }
1515 }
24badc65 1516 if (this->buffer_ == NULL)
c261a0be 1517 return;
24badc65
ILT
1518
1519 // Find the relocation section for ".debug_line".
af674d1d 1520 // We expect these for relobjs (.o's) but not dynobjs (.so's).
c1027032
CC
1521 unsigned int reloc_shndx = 0;
1522 for (unsigned int i = 0; i < object->shnum(); ++i)
24badc65 1523 {
c1027032 1524 unsigned int reloc_sh_type = object->section_type(i);
24badc65
ILT
1525 if ((reloc_sh_type == elfcpp::SHT_REL
1526 || reloc_sh_type == elfcpp::SHT_RELA)
c1027032 1527 && object->section_info(i) == debug_shndx)
24badc65 1528 {
c1027032 1529 reloc_shndx = i;
4dbfafcc 1530 this->track_relocs_type_ = reloc_sh_type;
24badc65
ILT
1531 break;
1532 }
1533 }
24badc65
ILT
1534
1535 // Finally, we need the symtab section to interpret the relocs.
c1027032 1536 if (reloc_shndx != 0)
af674d1d
ILT
1537 {
1538 unsigned int symtab_shndx;
1539 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
1540 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
1541 {
c1027032
CC
1542 this->symtab_buffer_ = object->section_contents(
1543 symtab_shndx, &this->symtab_buffer_size_, false);
af674d1d
ILT
1544 break;
1545 }
1546 if (this->symtab_buffer_ == NULL)
1547 return;
1548 }
24badc65 1549
c1027032
CC
1550 this->reloc_mapper_ =
1551 new Sized_elf_reloc_mapper<size, big_endian>(object,
1552 this->symtab_buffer_,
1553 this->symtab_buffer_size_);
1554 if (!this->reloc_mapper_->initialize(reloc_shndx, this->track_relocs_type_))
1555 return;
1556
24badc65
ILT
1557 // Now that we have successfully read all the data, parse the debug
1558 // info.
c261a0be 1559 this->data_valid_ = true;
c1027032 1560 this->read_line_mappings(read_shndx);
24badc65
ILT
1561}
1562
5c2c6c95
ILT
1563// Read the DWARF header.
1564
1565template<int size, bool big_endian>
1566const unsigned char*
a55ce7fe 1567Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
e43872e9 1568 const unsigned char* lineptr)
5c2c6c95 1569{
deae2a14 1570 uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
5c2c6c95
ILT
1571 lineptr += 4;
1572
1573 // In DWARF2/3, if the initial length is all 1 bits, then the offset
1574 // size is 8 and we need to read the next 8 bytes for the real length.
1575 if (initial_length == 0xffffffff)
1576 {
1577 header_.offset_size = 8;
deae2a14 1578 initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
5c2c6c95
ILT
1579 lineptr += 8;
1580 }
1581 else
1582 header_.offset_size = 4;
1583
1584 header_.total_length = initial_length;
1585
1586 gold_assert(lineptr + header_.total_length <= buffer_end_);
1587
deae2a14 1588 header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
5c2c6c95
ILT
1589 lineptr += 2;
1590
1591 if (header_.offset_size == 4)
deae2a14 1592 header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
5c2c6c95 1593 else
deae2a14 1594 header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
5c2c6c95
ILT
1595 lineptr += header_.offset_size;
1596
1597 header_.min_insn_length = *lineptr;
1598 lineptr += 1;
1599
1600 header_.default_is_stmt = *lineptr;
1601 lineptr += 1;
1602
1603 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
1604 lineptr += 1;
1605
1606 header_.line_range = *lineptr;
1607 lineptr += 1;
1608
1609 header_.opcode_base = *lineptr;
1610 lineptr += 1;
1611
a869183f 1612 header_.std_opcode_lengths.resize(header_.opcode_base + 1);
5c2c6c95
ILT
1613 header_.std_opcode_lengths[0] = 0;
1614 for (int i = 1; i < header_.opcode_base; i++)
1615 {
1616 header_.std_opcode_lengths[i] = *lineptr;
1617 lineptr += 1;
1618 }
1619
1620 return lineptr;
1621}
1622
1623// The header for a debug_line section is mildly complicated, because
1624// the line info is very tightly encoded.
1625
e43872e9 1626template<int size, bool big_endian>
5c2c6c95 1627const unsigned char*
a55ce7fe 1628Sized_dwarf_line_info<size, big_endian>::read_header_tables(
e43872e9 1629 const unsigned char* lineptr)
5c2c6c95 1630{
af674d1d
ILT
1631 ++this->current_header_index_;
1632
1633 // Create a new directories_ entry and a new files_ entry for our new
1634 // header. We initialize each with a single empty element, because
1635 // dwarf indexes directory and filenames starting at 1.
1636 gold_assert(static_cast<int>(this->directories_.size())
1637 == this->current_header_index_);
1638 gold_assert(static_cast<int>(this->files_.size())
1639 == this->current_header_index_);
1640 this->directories_.push_back(std::vector<std::string>(1));
1641 this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
1642
5c2c6c95
ILT
1643 // It is legal for the directory entry table to be empty.
1644 if (*lineptr)
1645 {
1646 int dirindex = 1;
1647 while (*lineptr)
1648 {
af674d1d
ILT
1649 const char* dirname = reinterpret_cast<const char*>(lineptr);
1650 gold_assert(dirindex
1651 == static_cast<int>(this->directories_.back().size()));
1652 this->directories_.back().push_back(dirname);
1653 lineptr += this->directories_.back().back().size() + 1;
5c2c6c95
ILT
1654 dirindex++;
1655 }
1656 }
1657 lineptr++;
1658
1659 // It is also legal for the file entry table to be empty.
1660 if (*lineptr)
1661 {
1662 int fileindex = 1;
1663 size_t len;
1664 while (*lineptr)
1665 {
1666 const char* filename = reinterpret_cast<const char*>(lineptr);
1667 lineptr += strlen(filename) + 1;
1668
1669 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
5c2c6c95
ILT
1670 lineptr += len;
1671
af674d1d
ILT
1672 if (dirindex >= this->directories_.back().size())
1673 dirindex = 0;
1674 int dirindexi = static_cast<int>(dirindex);
1675
5c2c6c95
ILT
1676 read_unsigned_LEB_128(lineptr, &len); // mod_time
1677 lineptr += len;
1678
1679 read_unsigned_LEB_128(lineptr, &len); // filelength
1680 lineptr += len;
1681
af674d1d
ILT
1682 gold_assert(fileindex
1683 == static_cast<int>(this->files_.back().size()));
1684 this->files_.back().push_back(std::make_pair(dirindexi, filename));
5c2c6c95
ILT
1685 fileindex++;
1686 }
1687 }
1688 lineptr++;
1689
1690 return lineptr;
1691}
1692
1693// Process a single opcode in the .debug.line structure.
1694
e43872e9 1695template<int size, bool big_endian>
5c2c6c95 1696bool
a55ce7fe 1697Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
e43872e9 1698 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
5c2c6c95
ILT
1699{
1700 size_t oplen = 0;
1701 size_t templen;
1702 unsigned char opcode = *start;
1703 oplen++;
1704 start++;
1705
1706 // If the opcode is great than the opcode_base, it is a special
1707 // opcode. Most line programs consist mainly of special opcodes.
1708 if (opcode >= header_.opcode_base)
1709 {
1710 opcode -= header_.opcode_base;
1711 const int advance_address = ((opcode / header_.line_range)
1712 * header_.min_insn_length);
1713 lsm->address += advance_address;
1714
1715 const int advance_line = ((opcode % header_.line_range)
1716 + header_.line_base);
1717 lsm->line_num += advance_line;
1718 lsm->basic_block = true;
1719 *len = oplen;
1720 return true;
1721 }
1722
1723 // Otherwise, we have the regular opcodes
1724 switch (opcode)
1725 {
1726 case elfcpp::DW_LNS_copy:
1727 lsm->basic_block = false;
1728 *len = oplen;
1729 return true;
1730
1731 case elfcpp::DW_LNS_advance_pc:
1732 {
1733 const uint64_t advance_address
2ea97941 1734 = read_unsigned_LEB_128(start, &templen);
5c2c6c95
ILT
1735 oplen += templen;
1736 lsm->address += header_.min_insn_length * advance_address;
1737 }
1738 break;
1739
1740 case elfcpp::DW_LNS_advance_line:
1741 {
1742 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
1743 oplen += templen;
1744 lsm->line_num += advance_line;
1745 }
1746 break;
1747
1748 case elfcpp::DW_LNS_set_file:
1749 {
1750 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
1751 oplen += templen;
1752 lsm->file_num = fileno;
1753 }
1754 break;
1755
1756 case elfcpp::DW_LNS_set_column:
1757 {
1758 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
1759 oplen += templen;
1760 lsm->column_num = colno;
1761 }
1762 break;
1763
1764 case elfcpp::DW_LNS_negate_stmt:
1765 lsm->is_stmt = !lsm->is_stmt;
1766 break;
1767
1768 case elfcpp::DW_LNS_set_basic_block:
1769 lsm->basic_block = true;
1770 break;
1771
1772 case elfcpp::DW_LNS_fixed_advance_pc:
1773 {
1774 int advance_address;
deae2a14 1775 advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
5c2c6c95
ILT
1776 oplen += 2;
1777 lsm->address += advance_address;
1778 }
1779 break;
1780
1781 case elfcpp::DW_LNS_const_add_pc:
1782 {
1783 const int advance_address = (header_.min_insn_length
1784 * ((255 - header_.opcode_base)
1785 / header_.line_range));
1786 lsm->address += advance_address;
1787 }
1788 break;
1789
1790 case elfcpp::DW_LNS_extended_op:
1791 {
1792 const uint64_t extended_op_len
2ea97941 1793 = read_unsigned_LEB_128(start, &templen);
5c2c6c95
ILT
1794 start += templen;
1795 oplen += templen + extended_op_len;
1796
1797 const unsigned char extended_op = *start;
1798 start++;
1799
1800 switch (extended_op)
1801 {
1802 case elfcpp::DW_LNE_end_sequence:
124dfc89
ILT
1803 // This means that the current byte is the one immediately
1804 // after a set of instructions. Record the current line
1805 // for up to one less than the current address.
79e052ea 1806 lsm->line_num = -1;
5c2c6c95
ILT
1807 lsm->end_sequence = true;
1808 *len = oplen;
1809 return true;
1810
1811 case elfcpp::DW_LNE_set_address:
4c50553d 1812 {
4dbfafcc
ILT
1813 lsm->address =
1814 elfcpp::Swap_unaligned<size, big_endian>::readval(start);
4c50553d 1815 typename Reloc_map::const_iterator it
4dbfafcc 1816 = this->reloc_map_.find(start - this->buffer_);
4c50553d
ILT
1817 if (it != reloc_map_.end())
1818 {
4dbfafcc
ILT
1819 // If this is a SHT_RELA section, then ignore the
1820 // section contents. This assumes that this is a
1821 // straight reloc which just uses the reloc addend.
1822 // The reloc addend has already been included in the
1823 // symbol value.
1824 if (this->track_relocs_type_ == elfcpp::SHT_RELA)
1825 lsm->address = 0;
1826 // Add in the symbol value.
1827 lsm->address += it->second.second;
4c50553d
ILT
1828 lsm->shndx = it->second.first;
1829 }
1830 else
1831 {
af674d1d
ILT
1832 // If we're a normal .o file, with relocs, every
1833 // set_address should have an associated relocation.
1834 if (this->input_is_relobj())
1835 this->data_valid_ = false;
4c50553d
ILT
1836 }
1837 break;
24badc65 1838 }
5c2c6c95
ILT
1839 case elfcpp::DW_LNE_define_file:
1840 {
1841 const char* filename = reinterpret_cast<const char*>(start);
1842 templen = strlen(filename) + 1;
1843 start += templen;
1844
1845 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
5c2c6c95 1846
af674d1d
ILT
1847 if (dirindex >= this->directories_.back().size())
1848 dirindex = 0;
1849 int dirindexi = static_cast<int>(dirindex);
1850
e8dd54e1
CC
1851 // This opcode takes two additional ULEB128 parameters
1852 // (mod_time and filelength), but we don't use those
1853 // values. Because OPLEN already tells us how far to
1854 // skip to the next opcode, we don't need to read
1855 // them at all.
5c2c6c95 1856
af674d1d 1857 this->files_.back().push_back(std::make_pair(dirindexi,
5c2c6c95
ILT
1858 filename));
1859 }
1860 break;
1861 }
1862 }
1863 break;
1864
1865 default:
1866 {
2ea97941 1867 // Ignore unknown opcode silently
5c2c6c95
ILT
1868 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
1869 {
2ea97941 1870 size_t templen;
5c2c6c95
ILT
1871 read_unsigned_LEB_128(start, &templen);
1872 start += templen;
1873 oplen += templen;
1874 }
1875 }
1876 break;
2ea97941 1877 }
5c2c6c95
ILT
1878 *len = oplen;
1879 return false;
1880}
1881
1882// Read the debug information at LINEPTR and store it in the line
1883// number map.
1884
e43872e9 1885template<int size, bool big_endian>
5c2c6c95 1886unsigned const char*
9430daf8 1887Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
75aea3d0 1888 unsigned int shndx)
5c2c6c95
ILT
1889{
1890 struct LineStateMachine lsm;
1891
1892 // LENGTHSTART is the place the length field is based on. It is the
1893 // point in the header after the initial length field.
1894 const unsigned char* lengthstart = buffer_;
1895
1896 // In 64 bit dwarf, the initial length is 12 bytes, because of the
1897 // 0xffffffff at the start.
1898 if (header_.offset_size == 8)
1899 lengthstart += 12;
1900 else
1901 lengthstart += 4;
1902
1903 while (lineptr < lengthstart + header_.total_length)
1904 {
1905 ResetLineStateMachine(&lsm, header_.default_is_stmt);
1906 while (!lsm.end_sequence)
1907 {
1908 size_t oplength;
e43872e9 1909 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
9430daf8
ILT
1910 if (add_line
1911 && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
5c2c6c95
ILT
1912 {
1913 Offset_to_lineno_entry entry
76677ad0
CC
1914 = { static_cast<off_t>(lsm.address),
1915 this->current_header_index_,
1916 static_cast<unsigned int>(lsm.file_num),
1917 true, lsm.line_num };
7500420b
ILT
1918 std::vector<Offset_to_lineno_entry>&
1919 map(this->line_number_map_[lsm.shndx]);
1920 // If we see two consecutive entries with the same
71ff8986
ILT
1921 // offset and a real line number, then mark the first
1922 // one as non-canonical.
7500420b
ILT
1923 if (!map.empty()
1924 && (map.back().offset == static_cast<off_t>(lsm.address))
1925 && lsm.line_num != -1
1926 && map.back().line_num != -1)
71ff8986
ILT
1927 map.back().last_line_for_offset = false;
1928 map.push_back(entry);
5c2c6c95
ILT
1929 }
1930 lineptr += oplength;
1931 }
1932 }
1933
1934 return lengthstart + header_.total_length;
1935}
1936
4c50553d
ILT
1937// Read the relocations into a Reloc_map.
1938
1939template<int size, bool big_endian>
1940void
c1027032 1941Sized_dwarf_line_info<size, big_endian>::read_relocs()
4c50553d
ILT
1942{
1943 if (this->symtab_buffer_ == NULL)
1944 return;
1945
c1027032 1946 off_t value;
4c50553d 1947 off_t reloc_offset;
c1027032 1948 while ((reloc_offset = this->reloc_mapper_->next_offset()) != -1)
4c50553d 1949 {
c1027032
CC
1950 const unsigned int shndx =
1951 this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
d491d34e
ILT
1952
1953 // There is no reason to record non-ordinary section indexes, or
1954 // SHN_UNDEF, because they will never match the real section.
c1027032
CC
1955 if (shndx != 0)
1956 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
d491d34e 1957
c1027032 1958 this->reloc_mapper_->advance(reloc_offset + 1);
4c50553d
ILT
1959 }
1960}
1961
1962// Read the line number info.
1963
e43872e9 1964template<int size, bool big_endian>
5c2c6c95 1965void
c1027032 1966Sized_dwarf_line_info<size, big_endian>::read_line_mappings(unsigned int shndx)
5c2c6c95 1967{
c261a0be 1968 gold_assert(this->data_valid_ == true);
24badc65 1969
c1027032 1970 this->read_relocs();
4c50553d 1971 while (this->buffer_ < this->buffer_end_)
e43872e9 1972 {
4c50553d 1973 const unsigned char* lineptr = this->buffer_;
e43872e9
ILT
1974 lineptr = this->read_header_prolog(lineptr);
1975 lineptr = this->read_header_tables(lineptr);
9430daf8 1976 lineptr = this->read_lines(lineptr, shndx);
4c50553d 1977 this->buffer_ = lineptr;
e43872e9
ILT
1978 }
1979
1980 // Sort the lines numbers, so addr2line can use binary search.
1981 for (typename Lineno_map::iterator it = line_number_map_.begin();
5c2c6c95
ILT
1982 it != line_number_map_.end();
1983 ++it)
1984 // Each vector needs to be sorted by offset.
4c50553d 1985 std::sort(it->second.begin(), it->second.end());
5c2c6c95
ILT
1986}
1987
af674d1d
ILT
1988// Some processing depends on whether the input is a .o file or not.
1989// For instance, .o files have relocs, and have .debug_lines
1990// information on a per section basis. .so files, on the other hand,
1991// lack relocs, and offsets are unique, so we can ignore the section
1992// information.
1993
1994template<int size, bool big_endian>
1995bool
a55ce7fe 1996Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
af674d1d
ILT
1997{
1998 // Only .o files have relocs and the symtab buffer that goes with them.
1999 return this->symtab_buffer_ != NULL;
2000}
2001
79e052ea
ILT
2002// Given an Offset_to_lineno_entry vector, and an offset, figure out
2003// if the offset points into a function according to the vector (see
2004// comments below for the algorithm). If it does, return an iterator
2005// into the vector that points to the line-number that contains that
2006// offset. If not, it returns vector::end().
2007
2008static std::vector<Offset_to_lineno_entry>::const_iterator
2009offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
2010 off_t offset)
2011{
71ff8986 2012 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, true, 0 };
79e052ea
ILT
2013
2014 // lower_bound() returns the smallest offset which is >= lookup_key.
2015 // If no offset in offsets is >= lookup_key, returns end().
2016 std::vector<Offset_to_lineno_entry>::const_iterator it
2017 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
2018
2019 // This code is easiest to understand with a concrete example.
2020 // Here's a possible offsets array:
71ff8986
ILT
2021 // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0
2022 // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1
2023 // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2
2024 // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3
2025 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4
2026 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5
2027 // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6
2028 // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7
2029 // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8
2030 // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9
2031 // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10
2032 // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11
2033 // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12
2034 // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13
2035 // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14
2036 // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15
2037 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16
2038 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17
2039 // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18
2040 // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19
2041 // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20
79e052ea
ILT
2042 // The entries with line_num == -1 mark the end of a function: the
2043 // associated offset is one past the last instruction in the
2044 // function. This can correspond to the beginning of the next
2045 // function (as is true for offset 3232); alternately, there can be
2046 // a gap between the end of one function and the start of the next
ef04e392 2047 // (as is true for some others, most obviously from 3236->5764).
79e052ea
ILT
2048 //
2049 // Case 1: lookup_key has offset == 10. lower_bound returns
2050 // offsets[0]. Since it's not an exact match and we're
ef04e392 2051 // at the beginning of offsets, we return end() (invalid).
79e052ea 2052 // Case 2: lookup_key has offset 10000. lower_bound returns
71ff8986 2053 // offset[21] (end()). We return end() (invalid).
79e052ea
ILT
2054 // Case 3: lookup_key has offset == 3211. lower_bound matches
2055 // offsets[0] exactly, and that's the entry we return.
2056 // Case 4: lookup_key has offset == 3232. lower_bound returns
2057 // offsets[4]. That's an exact match, but indicates
2058 // end-of-function. We check if offsets[5] is also an
2059 // exact match but not end-of-function. It is, so we
2060 // return offsets[5].
2061 // Case 5: lookup_key has offset == 3214. lower_bound returns
2062 // offsets[1]. Since it's not an exact match, we back
2063 // up to the offset that's < lookup_key, offsets[0].
2064 // We note offsets[0] is a valid entry (not end-of-function),
2065 // so that's the entry we return.
2066 // Case 6: lookup_key has offset == 4000. lower_bound returns
2067 // offsets[8]. Since it's not an exact match, we back
2068 // up to offsets[7]. Since offsets[7] indicates
2069 // end-of-function, we know lookup_key is between
ef04e392 2070 // functions, so we return end() (not a valid offset).
79e052ea 2071 // Case 7: lookup_key has offset == 5794. lower_bound returns
71ff8986
ILT
2072 // offsets[19]. Since it's not an exact match, we back
2073 // up to offsets[16]. Note we back up to the *first*
2074 // entry with offset 5793, not just offsets[19-1].
2075 // We note offsets[16] is a valid entry, so we return it.
2076 // If offsets[16] had had line_num == -1, we would have
2077 // checked offsets[17]. The reason for this is that
2078 // 16 and 17 can be in an arbitrary order, since we sort
2079 // only by offset and last_line_for_offset. (Note it
2080 // doesn't help to use line_number as a tertiary sort key,
2081 // since sometimes we want the -1 to be first and sometimes
2082 // we want it to be last.)
79e052ea
ILT
2083
2084 // This deals with cases (1) and (2).
2085 if ((it == offsets->begin() && offset < it->offset)
2086 || it == offsets->end())
2087 return offsets->end();
2088
2089 // This deals with cases (3) and (4).
2090 if (offset == it->offset)
2091 {
2092 while (it != offsets->end()
2093 && it->offset == offset
2094 && it->line_num == -1)
2095 ++it;
2096 if (it == offsets->end() || it->offset != offset)
2097 return offsets->end();
2098 else
2099 return it;
2100 }
2101
2102 // This handles the first part of case (7) -- we back up to the
2103 // *first* entry that has the offset that's behind us.
2104 gold_assert(it != offsets->begin());
2105 std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
2106 --it;
2107 const off_t range_value = it->offset;
2108 while (it != offsets->begin() && (it-1)->offset == range_value)
2109 --it;
2110
2111 // This handles cases (5), (6), and (7): if any entry in the
2112 // equal_range [it, range_end) has a line_num != -1, it's a valid
71ff8986
ILT
2113 // match. If not, we're not in a function. The line number we saw
2114 // last for an offset will be sorted first, so it'll get returned if
2115 // it's present.
79e052ea
ILT
2116 for (; it != range_end; ++it)
2117 if (it->line_num != -1)
2118 return it;
2119 return offsets->end();
2120}
af674d1d 2121
71ff8986
ILT
2122// Returns the canonical filename:lineno for the address passed in.
2123// If other_lines is not NULL, appends the non-canonical lines
2124// assigned to the same address.
5c2c6c95 2125
e43872e9 2126template<int size, bool big_endian>
5c2c6c95 2127std::string
71ff8986
ILT
2128Sized_dwarf_line_info<size, big_endian>::do_addr2line(
2129 unsigned int shndx,
2130 off_t offset,
2131 std::vector<std::string>* other_lines)
5c2c6c95 2132{
4c50553d
ILT
2133 if (this->data_valid_ == false)
2134 return "";
2135
af674d1d
ILT
2136 const std::vector<Offset_to_lineno_entry>* offsets;
2137 // If we do not have reloc information, then our input is a .so or
2138 // some similar data structure where all the information is held in
2139 // the offset. In that case, we ignore the input shndx.
2140 if (this->input_is_relobj())
2141 offsets = &this->line_number_map_[shndx];
2142 else
2143 offsets = &this->line_number_map_[-1U];
2144 if (offsets->empty())
4c50553d
ILT
2145 return "";
2146
e43872e9 2147 typename std::vector<Offset_to_lineno_entry>::const_iterator it
79e052ea
ILT
2148 = offset_to_iterator(offsets, offset);
2149 if (it == offsets->end())
2150 return "";
5c2c6c95 2151
71ff8986
ILT
2152 std::string result = this->format_file_lineno(*it);
2153 if (other_lines != NULL)
2154 for (++it; it != offsets->end() && it->offset == offset; ++it)
2155 {
2156 if (it->line_num == -1)
2157 continue; // The end of a previous function.
2158 other_lines->push_back(this->format_file_lineno(*it));
2159 }
2160 return result;
2161}
2162
2163// Convert the file_num + line_num into a string.
2164
2165template<int size, bool big_endian>
2166std::string
2167Sized_dwarf_line_info<size, big_endian>::format_file_lineno(
2168 const Offset_to_lineno_entry& loc) const
2169{
5c2c6c95 2170 std::string ret;
af674d1d 2171
71ff8986
ILT
2172 gold_assert(loc.header_num < static_cast<int>(this->files_.size()));
2173 gold_assert(loc.file_num
c1027032 2174 < static_cast<unsigned int>(this->files_[loc.header_num].size()));
af674d1d 2175 const std::pair<int, std::string>& filename_pair
71ff8986 2176 = this->files_[loc.header_num][loc.file_num];
5c2c6c95 2177 const std::string& filename = filename_pair.second;
af674d1d 2178
71ff8986 2179 gold_assert(loc.header_num < static_cast<int>(this->directories_.size()));
af674d1d 2180 gold_assert(filename_pair.first
71ff8986 2181 < static_cast<int>(this->directories_[loc.header_num].size()));
af674d1d 2182 const std::string& dirname
71ff8986 2183 = this->directories_[loc.header_num][filename_pair.first];
af674d1d 2184
5c2c6c95
ILT
2185 if (!dirname.empty())
2186 {
2187 ret += dirname;
2188 ret += "/";
2189 }
2190 ret += filename;
2191 if (ret.empty())
2192 ret = "(unknown)";
2193
2194 char buffer[64]; // enough to hold a line number
71ff8986 2195 snprintf(buffer, sizeof(buffer), "%d", loc.line_num);
5c2c6c95
ILT
2196 ret += ":";
2197 ret += buffer;
2198
2199 return ret;
2200}
2201
a55ce7fe
ILT
2202// Dwarf_line_info routines.
2203
e4e5049b
CS
2204static unsigned int next_generation_count = 0;
2205
2206struct Addr2line_cache_entry
2207{
2208 Object* object;
2209 unsigned int shndx;
2210 Dwarf_line_info* dwarf_line_info;
2211 unsigned int generation_count;
2212 unsigned int access_count;
2213
2214 Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
2215 : object(o), shndx(s), dwarf_line_info(d),
2216 generation_count(next_generation_count), access_count(0)
2217 {
2218 if (next_generation_count < (1U << 31))
2219 ++next_generation_count;
2220 }
2221};
2222// We expect this cache to be small, so don't bother with a hashtable
2223// or priority queue or anything: just use a simple vector.
2224static std::vector<Addr2line_cache_entry> addr2line_cache;
2225
a55ce7fe
ILT
2226std::string
2227Dwarf_line_info::one_addr2line(Object* object,
e4e5049b 2228 unsigned int shndx, off_t offset,
71ff8986
ILT
2229 size_t cache_size,
2230 std::vector<std::string>* other_lines)
a55ce7fe 2231{
e4e5049b
CS
2232 Dwarf_line_info* lineinfo = NULL;
2233 std::vector<Addr2line_cache_entry>::iterator it;
2234
2235 // First, check the cache. If we hit, update the counts.
2236 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
8851ecca 2237 {
e4e5049b
CS
2238 if (it->object == object && it->shndx == shndx)
2239 {
2240 lineinfo = it->dwarf_line_info;
2241 it->generation_count = next_generation_count;
2242 // We cap generation_count at 2^31 -1 to avoid overflow.
2243 if (next_generation_count < (1U << 31))
2244 ++next_generation_count;
2245 // We cap access_count at 31 so 2^access_count doesn't overflow
2246 if (it->access_count < 31)
2247 ++it->access_count;
2248 break;
2249 }
2250 }
2251
2252 // If we don't hit the cache, create a new object and insert into the
2253 // cache.
2254 if (lineinfo == NULL)
2255 {
2256 switch (parameters->size_and_endianness())
2257 {
a55ce7fe 2258#ifdef HAVE_TARGET_32_LITTLE
e4e5049b
CS
2259 case Parameters::TARGET_32_LITTLE:
2260 lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
a55ce7fe 2261#endif
a55ce7fe 2262#ifdef HAVE_TARGET_32_BIG
e4e5049b
CS
2263 case Parameters::TARGET_32_BIG:
2264 lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
a55ce7fe 2265#endif
a55ce7fe 2266#ifdef HAVE_TARGET_64_LITTLE
e4e5049b
CS
2267 case Parameters::TARGET_64_LITTLE:
2268 lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
a55ce7fe 2269#endif
8851ecca 2270#ifdef HAVE_TARGET_64_BIG
e4e5049b
CS
2271 case Parameters::TARGET_64_BIG:
2272 lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
a55ce7fe 2273#endif
e4e5049b
CS
2274 default:
2275 gold_unreachable();
2276 }
2277 addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
2278 }
2279
2280 // Now that we have our object, figure out the answer
71ff8986 2281 std::string retval = lineinfo->addr2line(shndx, offset, other_lines);
e4e5049b
CS
2282
2283 // Finally, if our cache has grown too big, delete old objects. We
2284 // assume the common (probably only) case is deleting only one object.
2285 // We use a pretty simple scheme to evict: function of LRU and MFU.
2286 while (addr2line_cache.size() > cache_size)
2287 {
2288 unsigned int lowest_score = ~0U;
2289 std::vector<Addr2line_cache_entry>::iterator lowest
2290 = addr2line_cache.end();
2291 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
2292 {
2293 const unsigned int score = (it->generation_count
2294 + (1U << it->access_count));
2295 if (score < lowest_score)
2296 {
2297 lowest_score = score;
2298 lowest = it;
2299 }
2300 }
2301 if (lowest != addr2line_cache.end())
2302 {
2303 delete lowest->dwarf_line_info;
2304 addr2line_cache.erase(lowest);
2305 }
8851ecca 2306 }
e4e5049b
CS
2307
2308 return retval;
2309}
2310
2311void
2312Dwarf_line_info::clear_addr2line_cache()
2313{
2314 for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
2315 it != addr2line_cache.end();
2316 ++it)
2317 delete it->dwarf_line_info;
2318 addr2line_cache.clear();
a55ce7fe
ILT
2319}
2320
5c2c6c95
ILT
2321#ifdef HAVE_TARGET_32_LITTLE
2322template
a55ce7fe 2323class Sized_dwarf_line_info<32, false>;
5c2c6c95
ILT
2324#endif
2325
2326#ifdef HAVE_TARGET_32_BIG
2327template
a55ce7fe 2328class Sized_dwarf_line_info<32, true>;
5c2c6c95
ILT
2329#endif
2330
2331#ifdef HAVE_TARGET_64_LITTLE
2332template
a55ce7fe 2333class Sized_dwarf_line_info<64, false>;
5c2c6c95
ILT
2334#endif
2335
2336#ifdef HAVE_TARGET_64_BIG
2337template
a55ce7fe 2338class Sized_dwarf_line_info<64, true>;
5c2c6c95
ILT
2339#endif
2340
2341} // End namespace gold.
This page took 0.358316 seconds and 4 git commands to generate.