1 // dwarf_reader.cc -- parse dwarf2/3 debug information
3 // Copyright 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
28 #include "elfcpp_swap.h"
32 #include "dwarf_reader.h"
33 #include "int_encoding.h"
34 #include "compressed_output.h"
38 // Class Sized_elf_reloc_mapper
40 // Initialize the relocation tracker for section RELOC_SHNDX.
42 template<int size
, bool big_endian
>
44 Sized_elf_reloc_mapper
<size
, big_endian
>::do_initialize(
45 unsigned int reloc_shndx
, unsigned int reloc_type
)
47 this->reloc_type_
= reloc_type
;
48 return this->track_relocs_
.initialize(this->object_
, reloc_shndx
,
52 // Looks in the symtab to see what section a symbol is in.
54 template<int size
, bool big_endian
>
56 Sized_elf_reloc_mapper
<size
, big_endian
>::symbol_section(
57 unsigned int symndx
, Address
* value
, bool* is_ordinary
)
59 const int symsize
= elfcpp::Elf_sizes
<size
>::sym_size
;
60 gold_assert(static_cast<off_t
>((symndx
+ 1) * symsize
) <= this->symtab_size_
);
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(),
67 // Return the section index and offset within the section of
68 // the target of the relocation for RELOC_OFFSET.
70 template<int size
, bool big_endian
>
72 Sized_elf_reloc_mapper
<size
, big_endian
>::do_get_reloc_target(
73 off_t reloc_offset
, off_t
* target_offset
)
75 this->track_relocs_
.advance(reloc_offset
);
76 if (reloc_offset
!= this->track_relocs_
.next_offset())
78 unsigned int symndx
= this->track_relocs_
.next_symndx();
79 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
81 unsigned int target_shndx
= this->symbol_section(symndx
, &value
,
85 if (this->reloc_type_
== elfcpp::SHT_RELA
)
86 value
+= this->track_relocs_
.next_addend();
87 *target_offset
= value
;
91 static inline Elf_reloc_mapper
*
92 make_elf_reloc_mapper(Relobj
* object
, const unsigned char* symtab
,
95 if (object
->elfsize() == 32)
97 if (object
->is_big_endian())
99 #ifdef HAVE_TARGET_32_BIG
100 return new Sized_elf_reloc_mapper
<32, true>(object
, symtab
,
108 #ifdef HAVE_TARGET_32_LITTLE
109 return new Sized_elf_reloc_mapper
<32, false>(object
, symtab
,
116 else if (object
->elfsize() == 64)
118 if (object
->is_big_endian())
120 #ifdef HAVE_TARGET_64_BIG
121 return new Sized_elf_reloc_mapper
<64, true>(object
, symtab
,
129 #ifdef HAVE_TARGET_64_LITTLE
130 return new Sized_elf_reloc_mapper
<64, false>(object
, symtab
,
141 // class Dwarf_abbrev_table
144 Dwarf_abbrev_table::clear_abbrev_codes()
146 for (unsigned int code
= 0; code
< this->low_abbrev_code_max_
; ++code
)
148 if (this->low_abbrev_codes_
[code
] != NULL
)
150 delete this->low_abbrev_codes_
[code
];
151 this->low_abbrev_codes_
[code
] = NULL
;
154 for (Abbrev_code_table::iterator it
= this->high_abbrev_codes_
.begin();
155 it
!= this->high_abbrev_codes_
.end();
158 if (it
->second
!= NULL
)
161 this->high_abbrev_codes_
.clear();
164 // Read the abbrev table from an object file.
167 Dwarf_abbrev_table::do_read_abbrevs(
169 unsigned int abbrev_shndx
,
172 this->clear_abbrev_codes();
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)
180 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
182 std::string name
= object
->section_name(i
);
183 if (name
== ".debug_abbrev")
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
);
193 if (abbrev_shndx
== 0)
197 // Get the section contents and decompress if necessary.
198 if (abbrev_shndx
!= this->abbrev_shndx_
)
200 if (this->owns_buffer_
&& this->buffer_
!= NULL
)
202 delete[] this->buffer_
;
203 this->owns_buffer_
= false;
206 section_size_type buffer_size
;
208 object
->decompressed_section_contents(abbrev_shndx
,
210 &this->owns_buffer_
);
211 this->buffer_end_
= this->buffer_
+ buffer_size
;
212 this->abbrev_shndx_
= abbrev_shndx
;
215 this->buffer_pos_
= this->buffer_
+ abbrev_offset
;
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.
224 const Dwarf_abbrev_table::Abbrev_code
*
225 Dwarf_abbrev_table::do_get_abbrev(unsigned int code
)
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())
232 // Read and store abbrev code definitions until we find the
233 // one we're looking for.
236 // Read the abbrev code. A zero here indicates the end of the
239 if (this->buffer_pos_
>= this->buffer_end_
)
241 uint64_t nextcode
= read_unsigned_LEB_128(this->buffer_pos_
, &len
);
244 this->buffer_pos_
= this->buffer_end_
;
247 this->buffer_pos_
+= len
;
250 if (this->buffer_pos_
>= this->buffer_end_
)
252 uint64_t tag
= read_unsigned_LEB_128(this->buffer_pos_
, &len
);
253 this->buffer_pos_
+= len
;
255 // Read the has_children flag.
256 if (this->buffer_pos_
>= this->buffer_end_
)
258 bool has_children
= *this->buffer_pos_
== elfcpp::DW_CHILDREN_yes
;
259 this->buffer_pos_
+= 1;
261 // Read the list of (attribute, form) pairs.
262 Abbrev_code
* entry
= new Abbrev_code(tag
, has_children
);
265 // Read the attribute.
266 if (this->buffer_pos_
>= this->buffer_end_
)
268 uint64_t attr
= read_unsigned_LEB_128(this->buffer_pos_
, &len
);
269 this->buffer_pos_
+= len
;
272 if (this->buffer_pos_
>= this->buffer_end_
)
274 uint64_t form
= read_unsigned_LEB_128(this->buffer_pos_
, &len
);
275 this->buffer_pos_
+= len
;
277 // A (0,0) pair terminates the list.
278 if (attr
== 0 && form
== 0)
281 if (attr
== elfcpp::DW_AT_sibling
)
282 entry
->has_sibling_attribute
= true;
284 entry
->add_attribute(attr
, form
);
287 this->store_abbrev(nextcode
, entry
);
288 if (nextcode
== code
)
295 // class Dwarf_ranges_table
297 // Read the ranges table from an object file.
300 Dwarf_ranges_table::read_ranges_table(
302 const unsigned char* symtab
,
304 unsigned int ranges_shndx
)
306 // If we've already read this abbrev table, return immediately.
307 if (this->ranges_shndx_
> 0
308 && this->ranges_shndx_
== ranges_shndx
)
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)
317 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
319 std::string name
= object
->section_name(i
);
320 if (name
== ".debug_ranges")
323 this->output_section_offset_
= object
->output_section_offset(i
);
327 if (ranges_shndx
== 0)
331 // Get the section contents and decompress if necessary.
332 if (ranges_shndx
!= this->ranges_shndx_
)
334 if (this->owns_ranges_buffer_
&& this->ranges_buffer_
!= NULL
)
336 delete[] this->ranges_buffer_
;
337 this->owns_ranges_buffer_
= false;
340 section_size_type buffer_size
;
341 this->ranges_buffer_
=
342 object
->decompressed_section_contents(ranges_shndx
,
344 &this->owns_ranges_buffer_
);
345 this->ranges_buffer_end_
= this->ranges_buffer_
+ buffer_size
;
346 this->ranges_shndx_
= ranges_shndx
;
349 if (this->ranges_reloc_mapper_
!= NULL
)
351 delete this->ranges_reloc_mapper_
;
352 this->ranges_reloc_mapper_
= NULL
;
355 // For incremental objects, we have no relocations.
356 if (object
->is_incremental())
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
)
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
)
374 this->ranges_reloc_mapper_
= make_elf_reloc_mapper(object
, symtab
,
376 this->ranges_reloc_mapper_
->initialize(reloc_shndx
, reloc_type
);
377 this->reloc_type_
= reloc_type
;
382 // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
385 Dwarf_ranges_table::read_range_list(
387 const unsigned char* symtab
,
389 unsigned int addr_size
,
390 unsigned int ranges_shndx
,
393 Dwarf_range_list
* ranges
;
395 if (!this->read_ranges_table(object
, symtab
, symtab_size
, ranges_shndx
))
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_
;
403 // Read the range list at OFFSET.
404 ranges
= new Dwarf_range_list();
407 this->ranges_buffer_
+ offset
< this->ranges_buffer_end_
;
408 offset
+= 2 * addr_size
)
413 // Read the raw contents of the section.
416 start
= this->dwinfo_
->read_from_pointer
<32>(this->ranges_buffer_
418 end
= this->dwinfo_
->read_from_pointer
<32>(this->ranges_buffer_
423 start
= this->dwinfo_
->read_from_pointer
<64>(this->ranges_buffer_
425 end
= this->dwinfo_
->read_from_pointer
<64>(this->ranges_buffer_
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
)
434 shndx1
= this->lookup_reloc(offset
, &start
);
435 shndx2
= this->lookup_reloc(offset
+ addr_size
, &end
);
438 // End of list is marked by a pair of zeroes.
439 if (shndx1
== 0 && start
== 0 && end
== 0)
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)
447 else if (shndx1
== shndx2
)
449 if (shndx1
== 0 || object
->is_section_included(shndx1
))
450 ranges
->add(shndx1
, base
+ start
, base
+ end
);
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());
461 // Look for a relocation at offset OFF in the range table,
462 // and return the section index and offset of the target.
465 Dwarf_ranges_table::lookup_reloc(off_t off
, off_t
* target_off
)
469 this->ranges_reloc_mapper_
->get_reloc_target(off
, &value
);
472 if (this->reloc_type_
== elfcpp::SHT_REL
)
473 *target_off
+= value
;
479 // class Dwarf_pubnames_table
481 // Read the pubnames section SHNDX from the object file.
484 Dwarf_pubnames_table::read_section(Relobj
* object
, unsigned int shndx
)
486 section_size_type buffer_size
;
488 // If we don't have relocations, shndx will be 0, and
489 // we'll have to hunt for the .debug_pubnames/pubtypes section.
492 const char* name
= (this->is_pubtypes_
494 : ".debug_pubnames");
495 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
497 if (object
->section_name(i
) == name
)
500 this->output_section_offset_
= object
->output_section_offset(i
);
508 this->buffer_
= object
->decompressed_section_contents(shndx
,
510 &this->owns_buffer_
);
511 if (this->buffer_
== NULL
)
513 this->buffer_end_
= this->buffer_
+ buffer_size
;
517 // Read the header for the set at OFFSET.
520 Dwarf_pubnames_table::read_header(off_t offset
)
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_
;
527 if (offset
< 0 || offset
+ 14 >= this->buffer_end_
- this->buffer_
)
530 const unsigned char* pinfo
= this->buffer_
+ offset
;
532 // Read the unit_length field.
533 uint32_t unit_length
= this->dwinfo_
->read_from_pointer
<32>(pinfo
);
535 if (unit_length
== 0xffffffff)
537 unit_length
= this->dwinfo_
->read_from_pointer
<64>(pinfo
);
539 this->offset_size_
= 8;
542 this->offset_size_
= 4;
544 // Check the version.
545 unsigned int version
= this->dwinfo_
->read_from_pointer
<16>(pinfo
);
550 // Skip the debug_info_offset and debug_info_size fields.
551 pinfo
+= 2 * this->offset_size_
;
553 if (pinfo
>= this->buffer_end_
)
556 this->pinfo_
= pinfo
;
560 // Read the next name from the set.
563 Dwarf_pubnames_table::next_name()
565 const unsigned char* pinfo
= this->pinfo_
;
567 // Read the offset within the CU. If this is zero, we have reached
568 // the end of the list.
570 if (this->offset_size_
== 4)
571 offset
= this->dwinfo_
->read_from_pointer
<32>(&pinfo
);
573 offset
= this->dwinfo_
->read_from_pointer
<64>(&pinfo
);
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')
582 if (pinfo
< this->buffer_end_
)
585 this->pinfo_
= pinfo
;
591 Dwarf_die::Dwarf_die(
592 Dwarf_info_reader
* dwinfo
,
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),
602 const unsigned char* pdie
= dwinfo
->buffer_at_offset(die_offset
);
605 unsigned int code
= read_unsigned_LEB_128(pdie
, &len
);
609 parent
->set_sibling_offset(die_offset
+ len
);
612 this->attr_offset_
= len
;
614 // Lookup the abbrev code in the abbrev table.
615 this->abbrev_code_
= dwinfo
->get_abbrev(code
);
618 // Read all the attributes of the DIE.
621 Dwarf_die::read_attributes()
623 if (this->attributes_read_
)
626 gold_assert(this->abbrev_code_
!= NULL
);
628 const unsigned char* pdie
=
629 this->dwinfo_
->buffer_at_offset(this->die_offset_
);
632 const unsigned char* pattr
= pdie
+ this->attr_offset_
;
634 unsigned int nattr
= this->abbrev_code_
->attributes
.size();
635 this->attributes_
.reserve(nattr
);
636 for (unsigned int i
= 0; i
< nattr
; ++i
)
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
)
643 form
= read_unsigned_LEB_128(pattr
, &len
);
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;
654 case elfcpp::DW_FORM_flag_present
:
655 attr_value
.val
.intval
= 1;
657 case elfcpp::DW_FORM_strp
:
660 if (this->dwinfo_
->offset_size() == 4)
661 str_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
663 str_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
665 this->dwinfo_
->lookup_reloc(attr_off
, &str_off
);
666 attr_value
.aux
.shndx
= shndx
;
667 attr_value
.val
.refval
= str_off
;
670 case elfcpp::DW_FORM_sec_offset
:
673 if (this->dwinfo_
->offset_size() == 4)
674 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
676 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
678 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
679 attr_value
.aux
.shndx
= shndx
;
680 attr_value
.val
.refval
= sec_off
;
684 case elfcpp::DW_FORM_addr
:
685 case elfcpp::DW_FORM_ref_addr
:
688 if (this->dwinfo_
->address_size() == 4)
689 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
691 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
693 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
694 attr_value
.aux
.shndx
= shndx
;
695 attr_value
.val
.refval
= sec_off
;
699 case elfcpp::DW_FORM_block1
:
700 attr_value
.aux
.blocklen
= *pattr
++;
701 attr_value
.val
.blockval
= pattr
;
702 pattr
+= attr_value
.aux
.blocklen
;
704 case elfcpp::DW_FORM_block2
:
705 attr_value
.aux
.blocklen
=
706 this->dwinfo_
->read_from_pointer
<16>(&pattr
);
707 attr_value
.val
.blockval
= pattr
;
708 pattr
+= attr_value
.aux
.blocklen
;
710 case elfcpp::DW_FORM_block4
:
711 attr_value
.aux
.blocklen
=
712 this->dwinfo_
->read_from_pointer
<32>(&pattr
);
713 attr_value
.val
.blockval
= pattr
;
714 pattr
+= attr_value
.aux
.blocklen
;
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
;
722 case elfcpp::DW_FORM_data1
:
723 case elfcpp::DW_FORM_flag
:
724 attr_value
.val
.intval
= *pattr
++;
726 case elfcpp::DW_FORM_ref1
:
727 attr_value
.val
.refval
= *pattr
++;
730 case elfcpp::DW_FORM_data2
:
731 attr_value
.val
.intval
=
732 this->dwinfo_
->read_from_pointer
<16>(&pattr
);
734 case elfcpp::DW_FORM_ref2
:
735 attr_value
.val
.refval
=
736 this->dwinfo_
->read_from_pointer
<16>(&pattr
);
739 case elfcpp::DW_FORM_data4
:
742 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
744 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
745 attr_value
.aux
.shndx
= shndx
;
746 attr_value
.val
.intval
= sec_off
;
749 case elfcpp::DW_FORM_ref4
:
752 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
754 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
755 attr_value
.aux
.shndx
= shndx
;
756 attr_value
.val
.refval
= sec_off
;
760 case elfcpp::DW_FORM_data8
:
763 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
765 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
766 attr_value
.aux
.shndx
= shndx
;
767 attr_value
.val
.intval
= sec_off
;
770 case elfcpp::DW_FORM_ref_sig8
:
771 attr_value
.val
.uintval
=
772 this->dwinfo_
->read_from_pointer
<64>(&pattr
);
774 case elfcpp::DW_FORM_ref8
:
777 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
779 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
780 attr_value
.aux
.shndx
= shndx
;
781 attr_value
.val
.refval
= sec_off
;
785 case elfcpp::DW_FORM_ref_udata
:
786 attr_value
.val
.refval
= read_unsigned_LEB_128(pattr
, &len
);
790 case elfcpp::DW_FORM_udata
:
791 case elfcpp::DW_FORM_GNU_addr_index
:
792 case elfcpp::DW_FORM_GNU_str_index
:
793 attr_value
.val
.uintval
= read_unsigned_LEB_128(pattr
, &len
);
796 case elfcpp::DW_FORM_sdata
:
797 attr_value
.val
.intval
= read_signed_LEB_128(pattr
, &len
);
800 case elfcpp::DW_FORM_string
:
801 attr_value
.val
.stringval
= reinterpret_cast<const char*>(pattr
);
802 len
= strlen(attr_value
.val
.stringval
);
809 // Cache the most frequently-requested attributes.
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
)
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
;
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
)
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
;
835 case elfcpp::DW_AT_specification
:
837 this->specification_
= attr_value
.val
.refval
;
839 case elfcpp::DW_AT_abstract_origin
:
841 this->abstract_origin_
= attr_value
.val
.refval
;
843 case elfcpp::DW_AT_sibling
:
844 if (ref_form
&& attr_value
.aux
.shndx
== 0)
845 this->sibling_offset_
= attr_value
.val
.refval
;
850 this->attributes_
.push_back(attr_value
);
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
);
858 this->sibling_offset_
= this->die_offset_
+ (pattr
- pdie
);
860 this->attributes_read_
= true;
864 // Skip all the attributes of the DIE and return the offset of the next DIE.
867 Dwarf_die::skip_attributes()
869 gold_assert(this->abbrev_code_
!= NULL
);
871 const unsigned char* pdie
=
872 this->dwinfo_
->buffer_at_offset(this->die_offset_
);
875 const unsigned char* pattr
= pdie
+ this->attr_offset_
;
877 for (unsigned int i
= 0; i
< this->abbrev_code_
->attributes
.size(); ++i
)
880 unsigned int form
= this->abbrev_code_
->attributes
[i
].form
;
881 if (form
== elfcpp::DW_FORM_indirect
)
883 form
= read_unsigned_LEB_128(pattr
, &len
);
888 case elfcpp::DW_FORM_flag_present
:
890 case elfcpp::DW_FORM_strp
:
891 case elfcpp::DW_FORM_sec_offset
:
892 pattr
+= this->dwinfo_
->offset_size();
894 case elfcpp::DW_FORM_addr
:
895 case elfcpp::DW_FORM_ref_addr
:
896 pattr
+= this->dwinfo_
->address_size();
898 case elfcpp::DW_FORM_block1
:
901 case elfcpp::DW_FORM_block2
:
904 block_size
= this->dwinfo_
->read_from_pointer
<16>(&pattr
);
908 case elfcpp::DW_FORM_block4
:
911 block_size
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
915 case elfcpp::DW_FORM_block
:
916 case elfcpp::DW_FORM_exprloc
:
919 block_size
= read_unsigned_LEB_128(pattr
, &len
);
920 pattr
+= len
+ block_size
;
923 case elfcpp::DW_FORM_data1
:
924 case elfcpp::DW_FORM_ref1
:
925 case elfcpp::DW_FORM_flag
:
928 case elfcpp::DW_FORM_data2
:
929 case elfcpp::DW_FORM_ref2
:
932 case elfcpp::DW_FORM_data4
:
933 case elfcpp::DW_FORM_ref4
:
936 case elfcpp::DW_FORM_data8
:
937 case elfcpp::DW_FORM_ref8
:
938 case elfcpp::DW_FORM_ref_sig8
:
941 case elfcpp::DW_FORM_ref_udata
:
942 case elfcpp::DW_FORM_udata
:
943 case elfcpp::DW_FORM_GNU_addr_index
:
944 case elfcpp::DW_FORM_GNU_str_index
:
945 read_unsigned_LEB_128(pattr
, &len
);
948 case elfcpp::DW_FORM_sdata
:
949 read_signed_LEB_128(pattr
, &len
);
952 case elfcpp::DW_FORM_string
:
953 len
= strlen(reinterpret_cast<const char*>(pattr
));
961 return this->die_offset_
+ (pattr
- pdie
);
964 // Get the name of the DIE and cache it.
967 Dwarf_die::set_name()
969 if (this->name_
!= NULL
|| !this->read_attributes())
971 if (this->name_off_
!= -1)
972 this->name_
= this->dwinfo_
->get_string(this->name_off_
,
973 this->string_shndx_
);
976 // Get the linkage name of the DIE and cache it.
979 Dwarf_die::set_linkage_name()
981 if (this->linkage_name_
!= NULL
|| !this->read_attributes())
983 if (this->linkage_name_off_
!= -1)
984 this->linkage_name_
= this->dwinfo_
->get_string(this->linkage_name_off_
,
985 this->string_shndx_
);
988 // Return the value of attribute ATTR.
990 const Dwarf_die::Attribute_value
*
991 Dwarf_die::attribute(unsigned int attr
)
993 if (!this->read_attributes())
995 for (unsigned int i
= 0; i
< this->attributes_
.size(); ++i
)
997 if (this->attributes_
[i
].attr
== attr
)
998 return &this->attributes_
[i
];
1004 Dwarf_die::string_attribute(unsigned int attr
)
1006 const Attribute_value
* attr_val
= this->attribute(attr
);
1007 if (attr_val
== NULL
)
1009 switch (attr_val
->form
)
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
);
1022 Dwarf_die::int_attribute(unsigned int attr
)
1024 const Attribute_value
* attr_val
= this->attribute(attr
);
1025 if (attr_val
== NULL
)
1027 switch (attr_val
->form
)
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
;
1043 Dwarf_die::uint_attribute(unsigned int attr
)
1045 const Attribute_value
* attr_val
= this->attribute(attr
);
1046 if (attr_val
== NULL
)
1048 switch (attr_val
->form
)
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
;
1064 Dwarf_die::ref_attribute(unsigned int attr
, unsigned int* shndx
)
1066 const Attribute_value
* attr_val
= this->attribute(attr
);
1067 if (attr_val
== NULL
)
1069 switch (attr_val
->form
)
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
;
1094 Dwarf_die::address_attribute(unsigned int attr
, unsigned int* shndx
)
1096 const Attribute_value
* attr_val
= this->attribute(attr
);
1097 if (attr_val
== NULL
|| attr_val
->form
!= elfcpp::DW_FORM_addr
)
1100 *shndx
= attr_val
->aux
.shndx
;
1101 return attr_val
->val
.refval
;
1104 // Return the offset of this DIE's first child.
1107 Dwarf_die::child_offset()
1109 gold_assert(this->abbrev_code_
!= NULL
);
1110 if (!this->has_children())
1112 if (this->child_offset_
== 0)
1113 this->child_offset_
= this->skip_attributes();
1114 return this->child_offset_
;
1117 // Return the offset of this DIE's next sibling.
1120 Dwarf_die::sibling_offset()
1122 gold_assert(this->abbrev_code_
!= NULL
);
1124 if (this->sibling_offset_
!= 0)
1125 return this->sibling_offset_
;
1127 if (!this->has_children())
1129 this->sibling_offset_
= this->skip_attributes();
1130 return this->sibling_offset_
;
1133 if (this->has_sibling_attribute())
1135 if (!this->read_attributes())
1137 if (this->sibling_offset_
!= 0)
1138 return this->sibling_offset_
;
1141 // Skip over the children.
1142 off_t child_offset
= this->child_offset();
1143 while (child_offset
> 0)
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.
1150 child_offset
= die
.sibling_offset();
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_
;
1158 // class Dwarf_info_reader
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.
1165 Dwarf_info_reader::parse()
1167 if (this->object_
->is_big_endian())
1169 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1170 this->do_parse
<true>();
1177 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1178 this->do_parse
<false>();
1185 template<bool big_endian
>
1187 Dwarf_info_reader::do_parse()
1189 // Get the section contents and decompress if necessary.
1190 section_size_type buffer_size
;
1192 this->buffer_
= this->object_
->decompressed_section_contents(this->shndx_
,
1195 if (this->buffer_
== NULL
|| buffer_size
== 0)
1197 this->buffer_end_
= this->buffer_
+ buffer_size
;
1199 // The offset of this input section in the output section.
1200 off_t section_offset
= this->object_
->output_section_offset(this->shndx_
);
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_
);
1207 // Loop over compilation units (or type units).
1208 unsigned int abbrev_shndx
= this->abbrev_shndx_
;
1209 off_t abbrev_offset
= 0;
1210 const unsigned char* pinfo
= this->buffer_
;
1211 while (pinfo
< this->buffer_end_
)
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
;
1218 // Read unit_length (4 or 12 bytes).
1219 if (!this->check_buffer(pinfo
+ 4))
1221 uint32_t unit_length
=
1222 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pinfo
);
1224 if (unit_length
== 0xffffffff)
1226 if (!this->check_buffer(pinfo
+ 8))
1228 unit_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1230 this->offset_size_
= 8;
1233 this->offset_size_
= 4;
1234 if (!this->check_buffer(pinfo
+ unit_length
))
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))
1241 // Read version (2 bytes).
1243 elfcpp::Swap_unaligned
<16, big_endian
>::readval(pinfo
);
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
);
1250 abbrev_offset
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1251 if (this->reloc_shndx_
> 0)
1253 off_t reloc_offset
= pinfo
- this->buffer_
;
1256 this->reloc_mapper_
->get_reloc_target(reloc_offset
, &value
);
1257 if (abbrev_shndx
== 0)
1259 if (this->reloc_type_
== elfcpp::SHT_REL
)
1260 abbrev_offset
+= value
;
1262 abbrev_offset
= value
;
1264 pinfo
+= this->offset_size_
;
1266 // Read address_size (1 byte).
1267 this->address_size_
= *pinfo
++;
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_
)
1274 if (!this->check_buffer(pinfo
+ 8 + this->offset_size_
))
1277 // Read type_signature (8 bytes).
1278 signature
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1281 // Read type_offset (4 or 8 bytes).
1282 if (this->offset_size_
== 4)
1284 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pinfo
);
1287 elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1288 pinfo
+= this->offset_size_
;
1291 // Read the .debug_abbrev table.
1292 this->abbrev_table_
.read_abbrevs(this->object_
, abbrev_shndx
,
1295 // Visit the root DIE.
1296 Dwarf_die
root_die(this,
1297 pinfo
- (this->buffer_
+ this->cu_offset_
),
1299 if (root_die
.tag() != 0)
1301 // Visit the CU or TU.
1302 if (this->is_type_unit_
)
1303 this->visit_type_unit(section_offset
+ this->cu_offset_
,
1304 cu_end
- cu_start
, type_offset
, signature
,
1307 this->visit_compilation_unit(section_offset
+ this->cu_offset_
,
1308 cu_end
- cu_start
, &root_die
);
1311 // Advance to the next CU.
1317 delete[] this->buffer_
;
1318 this->buffer_
= NULL
;
1322 // Read the DWARF string table.
1325 Dwarf_info_reader::do_read_string_table(unsigned int string_shndx
)
1327 Relobj
* object
= this->object_
;
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)
1333 for (unsigned int i
= 1; i
< this->object_
->shnum(); ++i
)
1335 std::string name
= object
->section_name(i
);
1336 if (name
== ".debug_str")
1339 this->string_output_section_offset_
=
1340 object
->output_section_offset(i
);
1344 if (string_shndx
== 0)
1348 if (this->owns_string_buffer_
&& this->string_buffer_
!= NULL
)
1350 delete[] this->string_buffer_
;
1351 this->owns_string_buffer_
= false;
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
,
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
;
1366 // Read a possibly unaligned integer of SIZE.
1367 template <int valsize
>
1368 inline typename
elfcpp::Valtype_base
<valsize
>::Valtype
1369 Dwarf_info_reader::read_from_pointer(const unsigned char* source
)
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
);
1375 return_value
= elfcpp::Swap_unaligned
<valsize
, false>::readval(source
);
1376 return return_value
;
1379 // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
1380 template <int valsize
>
1381 inline typename
elfcpp::Valtype_base
<valsize
>::Valtype
1382 Dwarf_info_reader::read_from_pointer(const unsigned char** source
)
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
);
1388 return_value
= elfcpp::Swap_unaligned
<valsize
, false>::readval(*source
);
1389 *source
+= valsize
/ 8;
1390 return return_value
;
1393 // Look for a relocation at offset ATTR_OFF in the dwarf info,
1394 // and return the section index and offset of the target.
1397 Dwarf_info_reader::lookup_reloc(off_t attr_off
, off_t
* target_off
)
1400 attr_off
+= this->cu_offset_
;
1401 unsigned int shndx
= this->reloc_mapper_
->get_reloc_target(attr_off
, &value
);
1404 if (this->reloc_type_
== elfcpp::SHT_REL
)
1405 *target_off
+= value
;
1407 *target_off
= value
;
1411 // Return a string from the DWARF string table.
1414 Dwarf_info_reader::get_string(off_t str_off
, unsigned int string_shndx
)
1416 if (!this->read_string_table(string_shndx
))
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_
;
1424 const char* p
= this->string_buffer_
+ str_off
;
1426 if (p
< this->string_buffer_
|| p
>= this->string_buffer_end_
)
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
1438 // Process a compilation unit and parse its child DIE.
1441 Dwarf_info_reader::visit_compilation_unit(off_t
, off_t
, Dwarf_die
*)
1445 // Process a type unit and parse its child DIE.
1448 Dwarf_info_reader::visit_type_unit(off_t
, off_t
, off_t
, uint64_t, Dwarf_die
*)
1452 // Print a warning about a corrupt debug section.
1455 Dwarf_info_reader::warn_corrupt_debug_section() const
1457 gold_warning(_("%s: corrupt debug info in %s"),
1458 this->object_
->name().c_str(),
1459 this->object_
->section_name(this->shndx_
).c_str());
1462 // class Sized_dwarf_line_info
1464 struct LineStateMachine
1470 unsigned int shndx
; // the section address refers to
1471 bool is_stmt
; // stmt means statement.
1477 ResetLineStateMachine(struct LineStateMachine
* lsm
, bool default_is_stmt
)
1482 lsm
->column_num
= 0;
1484 lsm
->is_stmt
= default_is_stmt
;
1485 lsm
->basic_block
= false;
1486 lsm
->end_sequence
= false;
1489 template<int size
, bool big_endian
>
1490 Sized_dwarf_line_info
<size
, big_endian
>::Sized_dwarf_line_info(
1492 unsigned int read_shndx
)
1493 : data_valid_(false), buffer_(NULL
), buffer_start_(NULL
),
1494 reloc_mapper_(NULL
), symtab_buffer_(NULL
), directories_(), files_(),
1495 current_header_index_(-1)
1497 unsigned int debug_shndx
;
1499 for (debug_shndx
= 1; debug_shndx
< object
->shnum(); ++debug_shndx
)
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")
1505 section_size_type buffer_size
;
1506 bool is_new
= false;
1507 this->buffer_
= object
->decompressed_section_contents(debug_shndx
,
1511 this->buffer_start_
= this->buffer_
;
1512 this->buffer_end_
= this->buffer_
+ buffer_size
;
1516 if (this->buffer_
== NULL
)
1519 // Find the relocation section for ".debug_line".
1520 // We expect these for relobjs (.o's) but not dynobjs (.so's).
1521 unsigned int reloc_shndx
= 0;
1522 for (unsigned int i
= 0; i
< object
->shnum(); ++i
)
1524 unsigned int reloc_sh_type
= object
->section_type(i
);
1525 if ((reloc_sh_type
== elfcpp::SHT_REL
1526 || reloc_sh_type
== elfcpp::SHT_RELA
)
1527 && object
->section_info(i
) == debug_shndx
)
1530 this->track_relocs_type_
= reloc_sh_type
;
1535 // Finally, we need the symtab section to interpret the relocs.
1536 if (reloc_shndx
!= 0)
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
)
1542 this->symtab_buffer_
= object
->section_contents(
1543 symtab_shndx
, &this->symtab_buffer_size_
, false);
1546 if (this->symtab_buffer_
== NULL
)
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_
))
1557 // Now that we have successfully read all the data, parse the debug
1559 this->data_valid_
= true;
1560 this->read_line_mappings(read_shndx
);
1563 // Read the DWARF header.
1565 template<int size
, bool big_endian
>
1566 const unsigned char*
1567 Sized_dwarf_line_info
<size
, big_endian
>::read_header_prolog(
1568 const unsigned char* lineptr
)
1570 uint32_t initial_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
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)
1577 header_
.offset_size
= 8;
1578 initial_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
1582 header_
.offset_size
= 4;
1584 header_
.total_length
= initial_length
;
1586 gold_assert(lineptr
+ header_
.total_length
<= buffer_end_
);
1588 header_
.version
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(lineptr
);
1591 if (header_
.offset_size
== 4)
1592 header_
.prologue_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
1594 header_
.prologue_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
1595 lineptr
+= header_
.offset_size
;
1597 header_
.min_insn_length
= *lineptr
;
1600 header_
.default_is_stmt
= *lineptr
;
1603 header_
.line_base
= *reinterpret_cast<const signed char*>(lineptr
);
1606 header_
.line_range
= *lineptr
;
1609 header_
.opcode_base
= *lineptr
;
1612 header_
.std_opcode_lengths
.resize(header_
.opcode_base
+ 1);
1613 header_
.std_opcode_lengths
[0] = 0;
1614 for (int i
= 1; i
< header_
.opcode_base
; i
++)
1616 header_
.std_opcode_lengths
[i
] = *lineptr
;
1623 // The header for a debug_line section is mildly complicated, because
1624 // the line info is very tightly encoded.
1626 template<int size
, bool big_endian
>
1627 const unsigned char*
1628 Sized_dwarf_line_info
<size
, big_endian
>::read_header_tables(
1629 const unsigned char* lineptr
)
1631 ++this->current_header_index_
;
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));
1643 // It is legal for the directory entry table to be empty.
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;
1659 // It is also legal for the file entry table to be empty.
1666 const char* filename
= reinterpret_cast<const char*>(lineptr
);
1667 lineptr
+= strlen(filename
) + 1;
1669 uint64_t dirindex
= read_unsigned_LEB_128(lineptr
, &len
);
1672 if (dirindex
>= this->directories_
.back().size())
1674 int dirindexi
= static_cast<int>(dirindex
);
1676 read_unsigned_LEB_128(lineptr
, &len
); // mod_time
1679 read_unsigned_LEB_128(lineptr
, &len
); // filelength
1682 gold_assert(fileindex
1683 == static_cast<int>(this->files_
.back().size()));
1684 this->files_
.back().push_back(std::make_pair(dirindexi
, filename
));
1693 // Process a single opcode in the .debug.line structure.
1695 template<int size
, bool big_endian
>
1697 Sized_dwarf_line_info
<size
, big_endian
>::process_one_opcode(
1698 const unsigned char* start
, struct LineStateMachine
* lsm
, size_t* len
)
1702 unsigned char opcode
= *start
;
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
)
1710 opcode
-= header_
.opcode_base
;
1711 const int advance_address
= ((opcode
/ header_
.line_range
)
1712 * header_
.min_insn_length
);
1713 lsm
->address
+= advance_address
;
1715 const int advance_line
= ((opcode
% header_
.line_range
)
1716 + header_
.line_base
);
1717 lsm
->line_num
+= advance_line
;
1718 lsm
->basic_block
= true;
1723 // Otherwise, we have the regular opcodes
1726 case elfcpp::DW_LNS_copy
:
1727 lsm
->basic_block
= false;
1731 case elfcpp::DW_LNS_advance_pc
:
1733 const uint64_t advance_address
1734 = read_unsigned_LEB_128(start
, &templen
);
1736 lsm
->address
+= header_
.min_insn_length
* advance_address
;
1740 case elfcpp::DW_LNS_advance_line
:
1742 const uint64_t advance_line
= read_signed_LEB_128(start
, &templen
);
1744 lsm
->line_num
+= advance_line
;
1748 case elfcpp::DW_LNS_set_file
:
1750 const uint64_t fileno
= read_unsigned_LEB_128(start
, &templen
);
1752 lsm
->file_num
= fileno
;
1756 case elfcpp::DW_LNS_set_column
:
1758 const uint64_t colno
= read_unsigned_LEB_128(start
, &templen
);
1760 lsm
->column_num
= colno
;
1764 case elfcpp::DW_LNS_negate_stmt
:
1765 lsm
->is_stmt
= !lsm
->is_stmt
;
1768 case elfcpp::DW_LNS_set_basic_block
:
1769 lsm
->basic_block
= true;
1772 case elfcpp::DW_LNS_fixed_advance_pc
:
1774 int advance_address
;
1775 advance_address
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(start
);
1777 lsm
->address
+= advance_address
;
1781 case elfcpp::DW_LNS_const_add_pc
:
1783 const int advance_address
= (header_
.min_insn_length
1784 * ((255 - header_
.opcode_base
)
1785 / header_
.line_range
));
1786 lsm
->address
+= advance_address
;
1790 case elfcpp::DW_LNS_extended_op
:
1792 const uint64_t extended_op_len
1793 = read_unsigned_LEB_128(start
, &templen
);
1795 oplen
+= templen
+ extended_op_len
;
1797 const unsigned char extended_op
= *start
;
1800 switch (extended_op
)
1802 case elfcpp::DW_LNE_end_sequence
:
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.
1807 lsm
->end_sequence
= true;
1811 case elfcpp::DW_LNE_set_address
:
1814 elfcpp::Swap_unaligned
<size
, big_endian
>::readval(start
);
1815 typename
Reloc_map::const_iterator it
1816 = this->reloc_map_
.find(start
- this->buffer_
);
1817 if (it
!= reloc_map_
.end())
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
1824 if (this->track_relocs_type_
== elfcpp::SHT_RELA
)
1826 // Add in the symbol value.
1827 lsm
->address
+= it
->second
.second
;
1828 lsm
->shndx
= it
->second
.first
;
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;
1839 case elfcpp::DW_LNE_define_file
:
1841 const char* filename
= reinterpret_cast<const char*>(start
);
1842 templen
= strlen(filename
) + 1;
1845 uint64_t dirindex
= read_unsigned_LEB_128(start
, &templen
);
1847 if (dirindex
>= this->directories_
.back().size())
1849 int dirindexi
= static_cast<int>(dirindex
);
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
1857 this->files_
.back().push_back(std::make_pair(dirindexi
,
1867 // Ignore unknown opcode silently
1868 for (int i
= 0; i
< header_
.std_opcode_lengths
[opcode
]; i
++)
1871 read_unsigned_LEB_128(start
, &templen
);
1882 // Read the debug information at LINEPTR and store it in the line
1885 template<int size
, bool big_endian
>
1886 unsigned const char*
1887 Sized_dwarf_line_info
<size
, big_endian
>::read_lines(unsigned const char* lineptr
,
1890 struct LineStateMachine lsm
;
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_
;
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)
1903 while (lineptr
< lengthstart
+ header_
.total_length
)
1905 ResetLineStateMachine(&lsm
, header_
.default_is_stmt
);
1906 while (!lsm
.end_sequence
)
1909 bool add_line
= this->process_one_opcode(lineptr
, &lsm
, &oplength
);
1911 && (shndx
== -1U || lsm
.shndx
== -1U || shndx
== lsm
.shndx
))
1913 Offset_to_lineno_entry entry
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
};
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
1921 // offset and a real line number, then mark the first
1922 // one as non-canonical.
1924 && (map
.back().offset
== static_cast<off_t
>(lsm
.address
))
1925 && lsm
.line_num
!= -1
1926 && map
.back().line_num
!= -1)
1927 map
.back().last_line_for_offset
= false;
1928 map
.push_back(entry
);
1930 lineptr
+= oplength
;
1934 return lengthstart
+ header_
.total_length
;
1937 // Read the relocations into a Reloc_map.
1939 template<int size
, bool big_endian
>
1941 Sized_dwarf_line_info
<size
, big_endian
>::read_relocs()
1943 if (this->symtab_buffer_
== NULL
)
1948 while ((reloc_offset
= this->reloc_mapper_
->next_offset()) != -1)
1950 const unsigned int shndx
=
1951 this->reloc_mapper_
->get_reloc_target(reloc_offset
, &value
);
1953 // There is no reason to record non-ordinary section indexes, or
1954 // SHN_UNDEF, because they will never match the real section.
1956 this->reloc_map_
[reloc_offset
] = std::make_pair(shndx
, value
);
1958 this->reloc_mapper_
->advance(reloc_offset
+ 1);
1962 // Read the line number info.
1964 template<int size
, bool big_endian
>
1966 Sized_dwarf_line_info
<size
, big_endian
>::read_line_mappings(unsigned int shndx
)
1968 gold_assert(this->data_valid_
== true);
1970 this->read_relocs();
1971 while (this->buffer_
< this->buffer_end_
)
1973 const unsigned char* lineptr
= this->buffer_
;
1974 lineptr
= this->read_header_prolog(lineptr
);
1975 lineptr
= this->read_header_tables(lineptr
);
1976 lineptr
= this->read_lines(lineptr
, shndx
);
1977 this->buffer_
= lineptr
;
1980 // Sort the lines numbers, so addr2line can use binary search.
1981 for (typename
Lineno_map::iterator it
= line_number_map_
.begin();
1982 it
!= line_number_map_
.end();
1984 // Each vector needs to be sorted by offset.
1985 std::sort(it
->second
.begin(), it
->second
.end());
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
1994 template<int size
, bool big_endian
>
1996 Sized_dwarf_line_info
<size
, big_endian
>::input_is_relobj()
1998 // Only .o files have relocs and the symtab buffer that goes with them.
1999 return this->symtab_buffer_
!= NULL
;
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().
2008 static std::vector
<Offset_to_lineno_entry
>::const_iterator
2009 offset_to_iterator(const std::vector
<Offset_to_lineno_entry
>* offsets
,
2012 const Offset_to_lineno_entry lookup_key
= { offset
, 0, 0, true, 0 };
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
);
2019 // This code is easiest to understand with a concrete example.
2020 // Here's a possible offsets array:
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
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
2047 // (as is true for some others, most obviously from 3236->5764).
2049 // Case 1: lookup_key has offset == 10. lower_bound returns
2050 // offsets[0]. Since it's not an exact match and we're
2051 // at the beginning of offsets, we return end() (invalid).
2052 // Case 2: lookup_key has offset 10000. lower_bound returns
2053 // offset[21] (end()). We return end() (invalid).
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
2070 // functions, so we return end() (not a valid offset).
2071 // Case 7: lookup_key has offset == 5794. lower_bound returns
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.)
2084 // This deals with cases (1) and (2).
2085 if ((it
== offsets
->begin() && offset
< it
->offset
)
2086 || it
== offsets
->end())
2087 return offsets
->end();
2089 // This deals with cases (3) and (4).
2090 if (offset
== it
->offset
)
2092 while (it
!= offsets
->end()
2093 && it
->offset
== offset
2094 && it
->line_num
== -1)
2096 if (it
== offsets
->end() || it
->offset
!= offset
)
2097 return offsets
->end();
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
;
2107 const off_t range_value
= it
->offset
;
2108 while (it
!= offsets
->begin() && (it
-1)->offset
== range_value
)
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
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
2116 for (; it
!= range_end
; ++it
)
2117 if (it
->line_num
!= -1)
2119 return offsets
->end();
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.
2126 template<int size
, bool big_endian
>
2128 Sized_dwarf_line_info
<size
, big_endian
>::do_addr2line(
2131 std::vector
<std::string
>* other_lines
)
2133 if (this->data_valid_
== false)
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
];
2143 offsets
= &this->line_number_map_
[-1U];
2144 if (offsets
->empty())
2147 typename
std::vector
<Offset_to_lineno_entry
>::const_iterator it
2148 = offset_to_iterator(offsets
, offset
);
2149 if (it
== offsets
->end())
2152 std::string result
= this->format_file_lineno(*it
);
2153 if (other_lines
!= NULL
)
2154 for (++it
; it
!= offsets
->end() && it
->offset
== offset
; ++it
)
2156 if (it
->line_num
== -1)
2157 continue; // The end of a previous function.
2158 other_lines
->push_back(this->format_file_lineno(*it
));
2163 // Convert the file_num + line_num into a string.
2165 template<int size
, bool big_endian
>
2167 Sized_dwarf_line_info
<size
, big_endian
>::format_file_lineno(
2168 const Offset_to_lineno_entry
& loc
) const
2172 gold_assert(loc
.header_num
< static_cast<int>(this->files_
.size()));
2173 gold_assert(loc
.file_num
2174 < static_cast<unsigned int>(this->files_
[loc
.header_num
].size()));
2175 const std::pair
<int, std::string
>& filename_pair
2176 = this->files_
[loc
.header_num
][loc
.file_num
];
2177 const std::string
& filename
= filename_pair
.second
;
2179 gold_assert(loc
.header_num
< static_cast<int>(this->directories_
.size()));
2180 gold_assert(filename_pair
.first
2181 < static_cast<int>(this->directories_
[loc
.header_num
].size()));
2182 const std::string
& dirname
2183 = this->directories_
[loc
.header_num
][filename_pair
.first
];
2185 if (!dirname
.empty())
2194 char buffer
[64]; // enough to hold a line number
2195 snprintf(buffer
, sizeof(buffer
), "%d", loc
.line_num
);
2202 // Dwarf_line_info routines.
2204 static unsigned int next_generation_count
= 0;
2206 struct Addr2line_cache_entry
2210 Dwarf_line_info
* dwarf_line_info
;
2211 unsigned int generation_count
;
2212 unsigned int access_count
;
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)
2218 if (next_generation_count
< (1U << 31))
2219 ++next_generation_count
;
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.
2224 static std::vector
<Addr2line_cache_entry
> addr2line_cache
;
2227 Dwarf_line_info::one_addr2line(Object
* object
,
2228 unsigned int shndx
, off_t offset
,
2230 std::vector
<std::string
>* other_lines
)
2232 Dwarf_line_info
* lineinfo
= NULL
;
2233 std::vector
<Addr2line_cache_entry
>::iterator it
;
2235 // First, check the cache. If we hit, update the counts.
2236 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
2238 if (it
->object
== object
&& it
->shndx
== shndx
)
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)
2252 // If we don't hit the cache, create a new object and insert into the
2254 if (lineinfo
== NULL
)
2256 switch (parameters
->size_and_endianness())
2258 #ifdef HAVE_TARGET_32_LITTLE
2259 case Parameters::TARGET_32_LITTLE
:
2260 lineinfo
= new Sized_dwarf_line_info
<32, false>(object
, shndx
); break;
2262 #ifdef HAVE_TARGET_32_BIG
2263 case Parameters::TARGET_32_BIG
:
2264 lineinfo
= new Sized_dwarf_line_info
<32, true>(object
, shndx
); break;
2266 #ifdef HAVE_TARGET_64_LITTLE
2267 case Parameters::TARGET_64_LITTLE
:
2268 lineinfo
= new Sized_dwarf_line_info
<64, false>(object
, shndx
); break;
2270 #ifdef HAVE_TARGET_64_BIG
2271 case Parameters::TARGET_64_BIG
:
2272 lineinfo
= new Sized_dwarf_line_info
<64, true>(object
, shndx
); break;
2277 addr2line_cache
.push_back(Addr2line_cache_entry(object
, shndx
, lineinfo
));
2280 // Now that we have our object, figure out the answer
2281 std::string retval
= lineinfo
->addr2line(shndx
, offset
, other_lines
);
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
)
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
)
2293 const unsigned int score
= (it
->generation_count
2294 + (1U << it
->access_count
));
2295 if (score
< lowest_score
)
2297 lowest_score
= score
;
2301 if (lowest
!= addr2line_cache
.end())
2303 delete lowest
->dwarf_line_info
;
2304 addr2line_cache
.erase(lowest
);
2312 Dwarf_line_info::clear_addr2line_cache()
2314 for (std::vector
<Addr2line_cache_entry
>::iterator it
= addr2line_cache
.begin();
2315 it
!= addr2line_cache
.end();
2317 delete it
->dwarf_line_info
;
2318 addr2line_cache
.clear();
2321 #ifdef HAVE_TARGET_32_LITTLE
2323 class Sized_dwarf_line_info
<32, false>;
2326 #ifdef HAVE_TARGET_32_BIG
2328 class Sized_dwarf_line_info
<32, true>;
2331 #ifdef HAVE_TARGET_64_LITTLE
2333 class Sized_dwarf_line_info
<64, false>;
2336 #ifdef HAVE_TARGET_64_BIG
2338 class Sized_dwarf_line_info
<64, true>;
2341 } // End namespace gold.