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((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
);
381 // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
384 Dwarf_ranges_table::read_range_list(
386 const unsigned char* symtab
,
388 unsigned int addr_size
,
389 unsigned int ranges_shndx
,
392 Dwarf_range_list
* ranges
;
394 if (!this->read_ranges_table(object
, symtab
, symtab_size
, ranges_shndx
))
397 // Correct the offset. For incremental update links, we have a
398 // relocated offset that is relative to the output section, but
399 // here we need an offset relative to the input section.
400 offset
-= this->output_section_offset_
;
402 // Read the range list at OFFSET.
403 ranges
= new Dwarf_range_list();
406 this->ranges_buffer_
+ offset
< this->ranges_buffer_end_
;
407 offset
+= 2 * addr_size
)
412 // Read the raw contents of the section.
415 start
= read_from_pointer
<32>(this->ranges_buffer_
+ offset
);
416 end
= read_from_pointer
<32>(this->ranges_buffer_
+ offset
+ 4);
420 start
= read_from_pointer
<64>(this->ranges_buffer_
+ offset
);
421 end
= read_from_pointer
<64>(this->ranges_buffer_
+ offset
+ 8);
424 // Check for relocations and adjust the values.
425 unsigned int shndx1
= 0;
426 unsigned int shndx2
= 0;
427 if (this->ranges_reloc_mapper_
!= NULL
)
430 this->ranges_reloc_mapper_
->get_reloc_target(offset
, &start
);
432 this->ranges_reloc_mapper_
->get_reloc_target(offset
+ addr_size
,
436 // End of list is marked by a pair of zeroes.
437 if (shndx1
== 0 && start
== 0 && end
== 0)
440 // A "base address selection entry" is identified by
441 // 0xffffffff for the first value of the pair. The second
442 // value is used as a base for subsequent range list entries.
443 if (shndx1
== 0 && start
== -1)
445 else if (shndx1
== shndx2
)
447 if (shndx1
== 0 || object
->is_section_included(shndx1
))
448 ranges
->add(shndx1
, base
+ start
, base
+ end
);
451 gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
452 "range list entry are in different sections"),
453 object
->name().c_str());
459 // class Dwarf_pubnames_table
461 // Read the pubnames section SHNDX from the object file.
464 Dwarf_pubnames_table::read_section(Relobj
* object
, unsigned int shndx
)
466 section_size_type buffer_size
;
468 // If we don't have relocations, shndx will be 0, and
469 // we'll have to hunt for the .debug_pubnames/pubtypes section.
472 const char* name
= (this->is_pubtypes_
474 : ".debug_pubnames");
475 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
477 if (object
->section_name(i
) == name
)
480 this->output_section_offset_
= object
->output_section_offset(i
);
488 this->buffer_
= object
->decompressed_section_contents(shndx
,
490 &this->owns_buffer_
);
491 if (this->buffer_
== NULL
)
493 this->buffer_end_
= this->buffer_
+ buffer_size
;
497 // Read the header for the set at OFFSET.
500 Dwarf_pubnames_table::read_header(off_t offset
)
502 // Correct the offset. For incremental update links, we have a
503 // relocated offset that is relative to the output section, but
504 // here we need an offset relative to the input section.
505 offset
-= this->output_section_offset_
;
507 if (offset
< 0 || offset
+ 14 >= this->buffer_end_
- this->buffer_
)
510 const unsigned char* pinfo
= this->buffer_
+ offset
;
512 // Read the unit_length field.
513 uint32_t unit_length
= read_from_pointer
<32>(pinfo
);
515 if (unit_length
== 0xffffffff)
517 unit_length
= read_from_pointer
<64>(pinfo
);
519 this->offset_size_
= 8;
522 this->offset_size_
= 4;
524 // Check the version.
525 unsigned int version
= read_from_pointer
<16>(pinfo
);
530 // Skip the debug_info_offset and debug_info_size fields.
531 pinfo
+= 2 * this->offset_size_
;
533 if (pinfo
>= this->buffer_end_
)
536 this->pinfo_
= pinfo
;
540 // Read the next name from the set.
543 Dwarf_pubnames_table::next_name()
545 const unsigned char* pinfo
= this->pinfo_
;
547 // Read the offset within the CU. If this is zero, we have reached
548 // the end of the list.
550 if (this->offset_size_
== 4)
551 offset
= read_from_pointer
<32>(&pinfo
);
553 offset
= read_from_pointer
<64>(&pinfo
);
557 // Return a pointer to the string at the current location,
558 // and advance the pointer to the next entry.
559 const char* ret
= reinterpret_cast<const char*>(pinfo
);
560 while (pinfo
< this->buffer_end_
&& *pinfo
!= '\0')
562 if (pinfo
< this->buffer_end_
)
565 this->pinfo_
= pinfo
;
571 Dwarf_die::Dwarf_die(
572 Dwarf_info_reader
* dwinfo
,
575 : dwinfo_(dwinfo
), parent_(parent
), die_offset_(die_offset
),
576 child_offset_(0), sibling_offset_(0), abbrev_code_(NULL
), attributes_(),
577 attributes_read_(false), name_(NULL
), name_off_(-1), linkage_name_(NULL
),
578 linkage_name_off_(-1), string_shndx_(0), specification_(0),
582 const unsigned char* pdie
= dwinfo
->buffer_at_offset(die_offset
);
585 unsigned int code
= read_unsigned_LEB_128(pdie
, &len
);
589 parent
->set_sibling_offset(die_offset
+ len
);
592 this->attr_offset_
= len
;
594 // Lookup the abbrev code in the abbrev table.
595 this->abbrev_code_
= dwinfo
->get_abbrev(code
);
598 // Read all the attributes of the DIE.
601 Dwarf_die::read_attributes()
603 if (this->attributes_read_
)
606 gold_assert(this->abbrev_code_
!= NULL
);
608 const unsigned char* pdie
=
609 this->dwinfo_
->buffer_at_offset(this->die_offset_
);
612 const unsigned char* pattr
= pdie
+ this->attr_offset_
;
614 unsigned int nattr
= this->abbrev_code_
->attributes
.size();
615 this->attributes_
.reserve(nattr
);
616 for (unsigned int i
= 0; i
< nattr
; ++i
)
619 unsigned int attr
= this->abbrev_code_
->attributes
[i
].attr
;
620 unsigned int form
= this->abbrev_code_
->attributes
[i
].form
;
621 if (form
== elfcpp::DW_FORM_indirect
)
623 form
= read_unsigned_LEB_128(pattr
, &len
);
626 off_t attr_off
= this->die_offset_
+ (pattr
- pdie
);
627 bool ref_form
= false;
628 Attribute_value attr_value
;
629 attr_value
.attr
= attr
;
630 attr_value
.form
= form
;
631 attr_value
.aux
.shndx
= 0;
634 case elfcpp::DW_FORM_flag_present
:
635 attr_value
.val
.intval
= 1;
637 case elfcpp::DW_FORM_strp
:
640 if (this->dwinfo_
->offset_size() == 4)
641 str_off
= read_from_pointer
<32>(&pattr
);
643 str_off
= read_from_pointer
<64>(&pattr
);
645 this->dwinfo_
->lookup_reloc(attr_off
, &str_off
);
646 attr_value
.aux
.shndx
= shndx
;
647 attr_value
.val
.refval
= str_off
;
650 case elfcpp::DW_FORM_sec_offset
:
653 if (this->dwinfo_
->offset_size() == 4)
654 sec_off
= read_from_pointer
<32>(&pattr
);
656 sec_off
= read_from_pointer
<64>(&pattr
);
658 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
659 attr_value
.aux
.shndx
= shndx
;
660 attr_value
.val
.refval
= sec_off
;
664 case elfcpp::DW_FORM_addr
:
665 case elfcpp::DW_FORM_ref_addr
:
668 if (this->dwinfo_
->address_size() == 4)
669 sec_off
= read_from_pointer
<32>(&pattr
);
671 sec_off
= read_from_pointer
<64>(&pattr
);
673 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
674 attr_value
.aux
.shndx
= shndx
;
675 attr_value
.val
.refval
= sec_off
;
679 case elfcpp::DW_FORM_block1
:
680 attr_value
.aux
.blocklen
= *pattr
++;
681 attr_value
.val
.blockval
= pattr
;
682 pattr
+= attr_value
.aux
.blocklen
;
684 case elfcpp::DW_FORM_block2
:
685 attr_value
.aux
.blocklen
= read_from_pointer
<16>(&pattr
);
686 attr_value
.val
.blockval
= pattr
;
687 pattr
+= attr_value
.aux
.blocklen
;
689 case elfcpp::DW_FORM_block4
:
690 attr_value
.aux
.blocklen
= read_from_pointer
<32>(&pattr
);
691 attr_value
.val
.blockval
= pattr
;
692 pattr
+= attr_value
.aux
.blocklen
;
694 case elfcpp::DW_FORM_block
:
695 case elfcpp::DW_FORM_exprloc
:
696 attr_value
.aux
.blocklen
= read_unsigned_LEB_128(pattr
, &len
);
697 attr_value
.val
.blockval
= pattr
+ len
;
698 pattr
+= len
+ attr_value
.aux
.blocklen
;
700 case elfcpp::DW_FORM_data1
:
701 case elfcpp::DW_FORM_flag
:
702 attr_value
.val
.intval
= *pattr
++;
704 case elfcpp::DW_FORM_ref1
:
705 attr_value
.val
.refval
= *pattr
++;
708 case elfcpp::DW_FORM_data2
:
709 attr_value
.val
.intval
= read_from_pointer
<16>(&pattr
);
711 case elfcpp::DW_FORM_ref2
:
712 attr_value
.val
.refval
= read_from_pointer
<16>(&pattr
);
715 case elfcpp::DW_FORM_data4
:
718 sec_off
= read_from_pointer
<32>(&pattr
);
720 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
721 attr_value
.aux
.shndx
= shndx
;
722 attr_value
.val
.intval
= sec_off
;
725 case elfcpp::DW_FORM_ref4
:
728 sec_off
= read_from_pointer
<32>(&pattr
);
730 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
731 attr_value
.aux
.shndx
= shndx
;
732 attr_value
.val
.refval
= sec_off
;
736 case elfcpp::DW_FORM_data8
:
739 sec_off
= read_from_pointer
<64>(&pattr
);
741 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
742 attr_value
.aux
.shndx
= shndx
;
743 attr_value
.val
.intval
= sec_off
;
746 case elfcpp::DW_FORM_ref_sig8
:
747 attr_value
.val
.uintval
= read_from_pointer
<64>(&pattr
);
749 case elfcpp::DW_FORM_ref8
:
752 sec_off
= read_from_pointer
<64>(&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_ref_udata
:
761 attr_value
.val
.refval
= read_unsigned_LEB_128(pattr
, &len
);
765 case elfcpp::DW_FORM_udata
:
766 case elfcpp::DW_FORM_GNU_addr_index
:
767 case elfcpp::DW_FORM_GNU_str_index
:
768 attr_value
.val
.uintval
= read_unsigned_LEB_128(pattr
, &len
);
771 case elfcpp::DW_FORM_sdata
:
772 attr_value
.val
.intval
= read_signed_LEB_128(pattr
, &len
);
775 case elfcpp::DW_FORM_string
:
776 attr_value
.val
.stringval
= reinterpret_cast<const char*>(pattr
);
777 len
= strlen(attr_value
.val
.stringval
);
784 // Cache the most frequently-requested attributes.
787 case elfcpp::DW_AT_name
:
788 if (form
== elfcpp::DW_FORM_string
)
789 this->name_
= attr_value
.val
.stringval
;
790 else if (form
== elfcpp::DW_FORM_strp
)
792 // All indirect strings should refer to the same
793 // string section, so we just save the last one seen.
794 this->string_shndx_
= attr_value
.aux
.shndx
;
795 this->name_off_
= attr_value
.val
.refval
;
798 case elfcpp::DW_AT_linkage_name
:
799 case elfcpp::DW_AT_MIPS_linkage_name
:
800 if (form
== elfcpp::DW_FORM_string
)
801 this->linkage_name_
= attr_value
.val
.stringval
;
802 else if (form
== elfcpp::DW_FORM_strp
)
804 // All indirect strings should refer to the same
805 // string section, so we just save the last one seen.
806 this->string_shndx_
= attr_value
.aux
.shndx
;
807 this->linkage_name_off_
= attr_value
.val
.refval
;
810 case elfcpp::DW_AT_specification
:
812 this->specification_
= attr_value
.val
.refval
;
814 case elfcpp::DW_AT_abstract_origin
:
816 this->abstract_origin_
= attr_value
.val
.refval
;
818 case elfcpp::DW_AT_sibling
:
819 if (ref_form
&& attr_value
.aux
.shndx
== 0)
820 this->sibling_offset_
= attr_value
.val
.refval
;
825 this->attributes_
.push_back(attr_value
);
828 // Now that we know where the next DIE begins, record the offset
829 // to avoid later recalculation.
830 if (this->has_children())
831 this->child_offset_
= this->die_offset_
+ (pattr
- pdie
);
833 this->sibling_offset_
= this->die_offset_
+ (pattr
- pdie
);
835 this->attributes_read_
= true;
839 // Skip all the attributes of the DIE and return the offset of the next DIE.
842 Dwarf_die::skip_attributes()
844 gold_assert(this->abbrev_code_
!= NULL
);
846 const unsigned char* pdie
=
847 this->dwinfo_
->buffer_at_offset(this->die_offset_
);
850 const unsigned char* pattr
= pdie
+ this->attr_offset_
;
852 for (unsigned int i
= 0; i
< this->abbrev_code_
->attributes
.size(); ++i
)
855 unsigned int form
= this->abbrev_code_
->attributes
[i
].form
;
856 if (form
== elfcpp::DW_FORM_indirect
)
858 form
= read_unsigned_LEB_128(pattr
, &len
);
863 case elfcpp::DW_FORM_flag_present
:
865 case elfcpp::DW_FORM_strp
:
866 case elfcpp::DW_FORM_sec_offset
:
867 pattr
+= this->dwinfo_
->offset_size();
869 case elfcpp::DW_FORM_addr
:
870 case elfcpp::DW_FORM_ref_addr
:
871 pattr
+= this->dwinfo_
->address_size();
873 case elfcpp::DW_FORM_block1
:
876 case elfcpp::DW_FORM_block2
:
879 block_size
= read_from_pointer
<16>(&pattr
);
883 case elfcpp::DW_FORM_block4
:
886 block_size
= read_from_pointer
<32>(&pattr
);
890 case elfcpp::DW_FORM_block
:
891 case elfcpp::DW_FORM_exprloc
:
894 block_size
= read_unsigned_LEB_128(pattr
, &len
);
895 pattr
+= len
+ block_size
;
898 case elfcpp::DW_FORM_data1
:
899 case elfcpp::DW_FORM_ref1
:
900 case elfcpp::DW_FORM_flag
:
903 case elfcpp::DW_FORM_data2
:
904 case elfcpp::DW_FORM_ref2
:
907 case elfcpp::DW_FORM_data4
:
908 case elfcpp::DW_FORM_ref4
:
911 case elfcpp::DW_FORM_data8
:
912 case elfcpp::DW_FORM_ref8
:
913 case elfcpp::DW_FORM_ref_sig8
:
916 case elfcpp::DW_FORM_ref_udata
:
917 case elfcpp::DW_FORM_udata
:
918 case elfcpp::DW_FORM_GNU_addr_index
:
919 case elfcpp::DW_FORM_GNU_str_index
:
920 read_unsigned_LEB_128(pattr
, &len
);
923 case elfcpp::DW_FORM_sdata
:
924 read_signed_LEB_128(pattr
, &len
);
927 case elfcpp::DW_FORM_string
:
928 len
= strlen(reinterpret_cast<const char*>(pattr
));
936 return this->die_offset_
+ (pattr
- pdie
);
939 // Get the name of the DIE and cache it.
942 Dwarf_die::set_name()
944 if (this->name_
!= NULL
|| !this->read_attributes())
946 if (this->name_off_
!= -1)
947 this->name_
= this->dwinfo_
->get_string(this->name_off_
,
948 this->string_shndx_
);
951 // Get the linkage name of the DIE and cache it.
954 Dwarf_die::set_linkage_name()
956 if (this->linkage_name_
!= NULL
|| !this->read_attributes())
958 if (this->linkage_name_off_
!= -1)
959 this->linkage_name_
= this->dwinfo_
->get_string(this->linkage_name_off_
,
960 this->string_shndx_
);
963 // Return the value of attribute ATTR.
965 const Dwarf_die::Attribute_value
*
966 Dwarf_die::attribute(unsigned int attr
)
968 if (!this->read_attributes())
970 for (unsigned int i
= 0; i
< this->attributes_
.size(); ++i
)
972 if (this->attributes_
[i
].attr
== attr
)
973 return &this->attributes_
[i
];
979 Dwarf_die::string_attribute(unsigned int attr
)
981 const Attribute_value
* attr_val
= this->attribute(attr
);
982 if (attr_val
== NULL
)
984 switch (attr_val
->form
)
986 case elfcpp::DW_FORM_string
:
987 return attr_val
->val
.stringval
;
988 case elfcpp::DW_FORM_strp
:
989 return this->dwinfo_
->get_string(attr_val
->val
.refval
,
990 attr_val
->aux
.shndx
);
997 Dwarf_die::int_attribute(unsigned int attr
)
999 const Attribute_value
* attr_val
= this->attribute(attr
);
1000 if (attr_val
== NULL
)
1002 switch (attr_val
->form
)
1004 case elfcpp::DW_FORM_flag_present
:
1005 case elfcpp::DW_FORM_data1
:
1006 case elfcpp::DW_FORM_flag
:
1007 case elfcpp::DW_FORM_data2
:
1008 case elfcpp::DW_FORM_data4
:
1009 case elfcpp::DW_FORM_data8
:
1010 case elfcpp::DW_FORM_sdata
:
1011 return attr_val
->val
.intval
;
1018 Dwarf_die::uint_attribute(unsigned int attr
)
1020 const Attribute_value
* attr_val
= this->attribute(attr
);
1021 if (attr_val
== NULL
)
1023 switch (attr_val
->form
)
1025 case elfcpp::DW_FORM_flag_present
:
1026 case elfcpp::DW_FORM_data1
:
1027 case elfcpp::DW_FORM_flag
:
1028 case elfcpp::DW_FORM_data4
:
1029 case elfcpp::DW_FORM_data8
:
1030 case elfcpp::DW_FORM_ref_sig8
:
1031 case elfcpp::DW_FORM_udata
:
1032 return attr_val
->val
.uintval
;
1039 Dwarf_die::ref_attribute(unsigned int attr
, unsigned int* shndx
)
1041 const Attribute_value
* attr_val
= this->attribute(attr
);
1042 if (attr_val
== NULL
)
1044 switch (attr_val
->form
)
1046 case elfcpp::DW_FORM_sec_offset
:
1047 case elfcpp::DW_FORM_addr
:
1048 case elfcpp::DW_FORM_ref_addr
:
1049 case elfcpp::DW_FORM_ref1
:
1050 case elfcpp::DW_FORM_ref2
:
1051 case elfcpp::DW_FORM_ref4
:
1052 case elfcpp::DW_FORM_ref8
:
1053 case elfcpp::DW_FORM_ref_udata
:
1054 *shndx
= attr_val
->aux
.shndx
;
1055 return attr_val
->val
.refval
;
1056 case elfcpp::DW_FORM_ref_sig8
:
1057 *shndx
= attr_val
->aux
.shndx
;
1058 return attr_val
->val
.uintval
;
1059 case elfcpp::DW_FORM_data4
:
1060 case elfcpp::DW_FORM_data8
:
1061 *shndx
= attr_val
->aux
.shndx
;
1062 return attr_val
->val
.intval
;
1069 Dwarf_die::address_attribute(unsigned int attr
, unsigned int* shndx
)
1071 const Attribute_value
* attr_val
= this->attribute(attr
);
1072 if (attr_val
== NULL
|| attr_val
->form
!= elfcpp::DW_FORM_addr
)
1075 *shndx
= attr_val
->aux
.shndx
;
1076 return attr_val
->val
.refval
;
1079 // Return the offset of this DIE's first child.
1082 Dwarf_die::child_offset()
1084 gold_assert(this->abbrev_code_
!= NULL
);
1085 if (!this->has_children())
1087 if (this->child_offset_
== 0)
1088 this->child_offset_
= this->skip_attributes();
1089 return this->child_offset_
;
1092 // Return the offset of this DIE's next sibling.
1095 Dwarf_die::sibling_offset()
1097 gold_assert(this->abbrev_code_
!= NULL
);
1099 if (this->sibling_offset_
!= 0)
1100 return this->sibling_offset_
;
1102 if (!this->has_children())
1104 this->sibling_offset_
= this->skip_attributes();
1105 return this->sibling_offset_
;
1108 if (this->has_sibling_attribute())
1110 if (!this->read_attributes())
1112 if (this->sibling_offset_
!= 0)
1113 return this->sibling_offset_
;
1116 // Skip over the children.
1117 off_t child_offset
= this->child_offset();
1118 while (child_offset
> 0)
1120 Dwarf_die
die(this->dwinfo_
, child_offset
, this);
1121 // The Dwarf_die ctor will set this DIE's sibling offset
1122 // when it reads a zero abbrev code.
1125 child_offset
= die
.sibling_offset();
1128 // This should be set by now. If not, there was a problem reading
1129 // the DWARF info, and we return 0.
1130 return this->sibling_offset_
;
1133 // class Dwarf_info_reader
1135 // Check that the pointer P is within the current compilation unit.
1138 Dwarf_info_reader::check_buffer(const unsigned char* p
) const
1140 if (p
> this->buffer_
+ this->cu_offset_
+ this->cu_length_
)
1142 gold_warning(_("%s: corrupt debug info in %s"),
1143 this->object_
->name().c_str(),
1144 this->object_
->section_name(this->shndx_
).c_str());
1150 // Begin parsing the debug info. This calls visit_compilation_unit()
1151 // or visit_type_unit() for each compilation or type unit found in the
1152 // section, and visit_die() for each top-level DIE.
1155 Dwarf_info_reader::parse()
1157 if (this->object_
->is_big_endian())
1159 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1160 this->do_parse
<true>();
1167 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1168 this->do_parse
<false>();
1175 template<bool big_endian
>
1177 Dwarf_info_reader::do_parse()
1179 // Get the section contents and decompress if necessary.
1180 section_size_type buffer_size
;
1182 this->buffer_
= this->object_
->decompressed_section_contents(this->shndx_
,
1185 if (this->buffer_
== NULL
|| buffer_size
== 0)
1187 this->buffer_end_
= this->buffer_
+ buffer_size
;
1189 // The offset of this input section in the output section.
1190 off_t section_offset
= this->object_
->output_section_offset(this->shndx_
);
1192 // Start tracking relocations for this section.
1193 this->reloc_mapper_
= make_elf_reloc_mapper(this->object_
, this->symtab_
,
1194 this->symtab_size_
);
1195 this->reloc_mapper_
->initialize(this->reloc_shndx_
, this->reloc_type_
);
1197 // Loop over compilation units (or type units).
1198 unsigned int abbrev_shndx
= 0;
1199 off_t abbrev_offset
= 0;
1200 const unsigned char* pinfo
= this->buffer_
;
1201 while (pinfo
< this->buffer_end_
)
1203 // Read the compilation (or type) unit header.
1204 const unsigned char* cu_start
= pinfo
;
1205 this->cu_offset_
= cu_start
- this->buffer_
;
1206 this->cu_length_
= this->buffer_end_
- cu_start
;
1208 // Read unit_length (4 or 12 bytes).
1209 if (!this->check_buffer(pinfo
+ 4))
1211 uint32_t unit_length
=
1212 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pinfo
);
1214 if (unit_length
== 0xffffffff)
1216 if (!this->check_buffer(pinfo
+ 8))
1218 unit_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1220 this->offset_size_
= 8;
1223 this->offset_size_
= 4;
1224 if (!this->check_buffer(pinfo
+ unit_length
))
1226 const unsigned char* cu_end
= pinfo
+ unit_length
;
1227 this->cu_length_
= cu_end
- cu_start
;
1228 if (!this->check_buffer(pinfo
+ 2 + this->offset_size_
+ 1))
1231 // Read version (2 bytes).
1233 elfcpp::Swap_unaligned
<16, big_endian
>::readval(pinfo
);
1236 // Read debug_abbrev_offset (4 or 8 bytes).
1237 if (this->offset_size_
== 4)
1238 abbrev_offset
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(pinfo
);
1240 abbrev_offset
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1241 if (this->reloc_shndx_
> 0)
1243 off_t reloc_offset
= pinfo
- this->buffer_
;
1246 this->reloc_mapper_
->get_reloc_target(reloc_offset
, &value
);
1247 if (abbrev_shndx
== 0)
1249 if (this->reloc_type_
== elfcpp::SHT_REL
)
1250 abbrev_offset
+= value
;
1252 abbrev_offset
= value
;
1254 pinfo
+= this->offset_size_
;
1256 // Read address_size (1 byte).
1257 this->address_size_
= *pinfo
++;
1259 // For type units, read the two extra fields.
1260 uint64_t signature
= 0;
1261 off_t type_offset
= 0;
1262 if (this->is_type_unit_
)
1264 if (!this->check_buffer(pinfo
+ 8 + this->offset_size_
))
1267 // Read type_signature (8 bytes).
1268 signature
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1271 // Read type_offset (4 or 8 bytes).
1272 if (this->offset_size_
== 4)
1274 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pinfo
);
1277 elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1278 pinfo
+= this->offset_size_
;
1281 // Read the .debug_abbrev table.
1282 this->abbrev_table_
.read_abbrevs(this->object_
, abbrev_shndx
,
1285 // Visit the root DIE.
1286 Dwarf_die
root_die(this,
1287 pinfo
- (this->buffer_
+ this->cu_offset_
),
1289 if (root_die
.tag() != 0)
1291 // Visit the CU or TU.
1292 if (this->is_type_unit_
)
1293 this->visit_type_unit(section_offset
+ this->cu_offset_
,
1294 type_offset
, signature
, &root_die
);
1296 this->visit_compilation_unit(section_offset
+ this->cu_offset_
,
1297 cu_end
- cu_start
, &root_die
);
1300 // Advance to the next CU.
1306 delete[] this->buffer_
;
1307 this->buffer_
= NULL
;
1311 // Read the DWARF string table.
1314 Dwarf_info_reader::do_read_string_table(unsigned int string_shndx
)
1316 Relobj
* object
= this->object_
;
1318 // If we don't have relocations, string_shndx will be 0, and
1319 // we'll have to hunt for the .debug_str section.
1320 if (string_shndx
== 0)
1322 for (unsigned int i
= 1; i
< this->object_
->shnum(); ++i
)
1324 std::string name
= object
->section_name(i
);
1325 if (name
== ".debug_str")
1328 this->string_output_section_offset_
=
1329 object
->output_section_offset(i
);
1333 if (string_shndx
== 0)
1337 if (this->owns_string_buffer_
&& this->string_buffer_
!= NULL
)
1339 delete[] this->string_buffer_
;
1340 this->owns_string_buffer_
= false;
1343 // Get the secton contents and decompress if necessary.
1344 section_size_type buffer_size
;
1345 const unsigned char* buffer
=
1346 object
->decompressed_section_contents(string_shndx
,
1348 &this->owns_string_buffer_
);
1349 this->string_buffer_
= reinterpret_cast<const char*>(buffer
);
1350 this->string_buffer_end_
= this->string_buffer_
+ buffer_size
;
1351 this->string_shndx_
= string_shndx
;
1355 // Look for a relocation at offset ATTR_OFF in the dwarf info,
1356 // and return the section index and offset of the target.
1359 Dwarf_info_reader::lookup_reloc(off_t attr_off
, off_t
* target_off
)
1362 attr_off
+= this->cu_offset_
;
1363 unsigned int shndx
= this->reloc_mapper_
->get_reloc_target(attr_off
, &value
);
1366 if (this->reloc_type_
== elfcpp::SHT_REL
)
1367 *target_off
+= value
;
1369 *target_off
= value
;
1373 // Return a string from the DWARF string table.
1376 Dwarf_info_reader::get_string(off_t str_off
, unsigned int string_shndx
)
1378 if (!this->read_string_table(string_shndx
))
1381 // Correct the offset. For incremental update links, we have a
1382 // relocated offset that is relative to the output section, but
1383 // here we need an offset relative to the input section.
1384 str_off
-= this->string_output_section_offset_
;
1386 const char* p
= this->string_buffer_
+ str_off
;
1388 if (p
< this->string_buffer_
|| p
>= this->string_buffer_end_
)
1394 // The following are default, do-nothing, implementations of the
1395 // hook methods normally provided by a derived class. We provide
1396 // default implementations rather than no implementation so that
1397 // a derived class needs to implement only the hooks that it needs
1400 // Process a compilation unit and parse its child DIE.
1403 Dwarf_info_reader::visit_compilation_unit(off_t
, off_t
, Dwarf_die
*)
1407 // Process a type unit and parse its child DIE.
1410 Dwarf_info_reader::visit_type_unit(off_t
, off_t
, uint64_t, Dwarf_die
*)
1414 // class Sized_dwarf_line_info
1416 struct LineStateMachine
1422 unsigned int shndx
; // the section address refers to
1423 bool is_stmt
; // stmt means statement.
1429 ResetLineStateMachine(struct LineStateMachine
* lsm
, bool default_is_stmt
)
1434 lsm
->column_num
= 0;
1436 lsm
->is_stmt
= default_is_stmt
;
1437 lsm
->basic_block
= false;
1438 lsm
->end_sequence
= false;
1441 template<int size
, bool big_endian
>
1442 Sized_dwarf_line_info
<size
, big_endian
>::Sized_dwarf_line_info(
1444 unsigned int read_shndx
)
1445 : data_valid_(false), buffer_(NULL
), buffer_start_(NULL
),
1446 reloc_mapper_(NULL
), symtab_buffer_(NULL
), directories_(), files_(),
1447 current_header_index_(-1)
1449 unsigned int debug_shndx
;
1451 for (debug_shndx
= 1; debug_shndx
< object
->shnum(); ++debug_shndx
)
1453 // FIXME: do this more efficiently: section_name() isn't super-fast
1454 std::string name
= object
->section_name(debug_shndx
);
1455 if (name
== ".debug_line" || name
== ".zdebug_line")
1457 section_size_type buffer_size
;
1458 bool is_new
= false;
1459 this->buffer_
= object
->decompressed_section_contents(debug_shndx
,
1463 this->buffer_start_
= this->buffer_
;
1464 this->buffer_end_
= this->buffer_
+ buffer_size
;
1468 if (this->buffer_
== NULL
)
1471 // Find the relocation section for ".debug_line".
1472 // We expect these for relobjs (.o's) but not dynobjs (.so's).
1473 unsigned int reloc_shndx
= 0;
1474 for (unsigned int i
= 0; i
< object
->shnum(); ++i
)
1476 unsigned int reloc_sh_type
= object
->section_type(i
);
1477 if ((reloc_sh_type
== elfcpp::SHT_REL
1478 || reloc_sh_type
== elfcpp::SHT_RELA
)
1479 && object
->section_info(i
) == debug_shndx
)
1482 this->track_relocs_type_
= reloc_sh_type
;
1487 // Finally, we need the symtab section to interpret the relocs.
1488 if (reloc_shndx
!= 0)
1490 unsigned int symtab_shndx
;
1491 for (symtab_shndx
= 0; symtab_shndx
< object
->shnum(); ++symtab_shndx
)
1492 if (object
->section_type(symtab_shndx
) == elfcpp::SHT_SYMTAB
)
1494 this->symtab_buffer_
= object
->section_contents(
1495 symtab_shndx
, &this->symtab_buffer_size_
, false);
1498 if (this->symtab_buffer_
== NULL
)
1502 this->reloc_mapper_
=
1503 new Sized_elf_reloc_mapper
<size
, big_endian
>(object
,
1504 this->symtab_buffer_
,
1505 this->symtab_buffer_size_
);
1506 if (!this->reloc_mapper_
->initialize(reloc_shndx
, this->track_relocs_type_
))
1509 // Now that we have successfully read all the data, parse the debug
1511 this->data_valid_
= true;
1512 this->read_line_mappings(read_shndx
);
1515 // Read the DWARF header.
1517 template<int size
, bool big_endian
>
1518 const unsigned char*
1519 Sized_dwarf_line_info
<size
, big_endian
>::read_header_prolog(
1520 const unsigned char* lineptr
)
1522 uint32_t initial_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
1525 // In DWARF2/3, if the initial length is all 1 bits, then the offset
1526 // size is 8 and we need to read the next 8 bytes for the real length.
1527 if (initial_length
== 0xffffffff)
1529 header_
.offset_size
= 8;
1530 initial_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
1534 header_
.offset_size
= 4;
1536 header_
.total_length
= initial_length
;
1538 gold_assert(lineptr
+ header_
.total_length
<= buffer_end_
);
1540 header_
.version
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(lineptr
);
1543 if (header_
.offset_size
== 4)
1544 header_
.prologue_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
1546 header_
.prologue_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
1547 lineptr
+= header_
.offset_size
;
1549 header_
.min_insn_length
= *lineptr
;
1552 header_
.default_is_stmt
= *lineptr
;
1555 header_
.line_base
= *reinterpret_cast<const signed char*>(lineptr
);
1558 header_
.line_range
= *lineptr
;
1561 header_
.opcode_base
= *lineptr
;
1564 header_
.std_opcode_lengths
.resize(header_
.opcode_base
+ 1);
1565 header_
.std_opcode_lengths
[0] = 0;
1566 for (int i
= 1; i
< header_
.opcode_base
; i
++)
1568 header_
.std_opcode_lengths
[i
] = *lineptr
;
1575 // The header for a debug_line section is mildly complicated, because
1576 // the line info is very tightly encoded.
1578 template<int size
, bool big_endian
>
1579 const unsigned char*
1580 Sized_dwarf_line_info
<size
, big_endian
>::read_header_tables(
1581 const unsigned char* lineptr
)
1583 ++this->current_header_index_
;
1585 // Create a new directories_ entry and a new files_ entry for our new
1586 // header. We initialize each with a single empty element, because
1587 // dwarf indexes directory and filenames starting at 1.
1588 gold_assert(static_cast<int>(this->directories_
.size())
1589 == this->current_header_index_
);
1590 gold_assert(static_cast<int>(this->files_
.size())
1591 == this->current_header_index_
);
1592 this->directories_
.push_back(std::vector
<std::string
>(1));
1593 this->files_
.push_back(std::vector
<std::pair
<int, std::string
> >(1));
1595 // It is legal for the directory entry table to be empty.
1601 const char* dirname
= reinterpret_cast<const char*>(lineptr
);
1602 gold_assert(dirindex
1603 == static_cast<int>(this->directories_
.back().size()));
1604 this->directories_
.back().push_back(dirname
);
1605 lineptr
+= this->directories_
.back().back().size() + 1;
1611 // It is also legal for the file entry table to be empty.
1618 const char* filename
= reinterpret_cast<const char*>(lineptr
);
1619 lineptr
+= strlen(filename
) + 1;
1621 uint64_t dirindex
= read_unsigned_LEB_128(lineptr
, &len
);
1624 if (dirindex
>= this->directories_
.back().size())
1626 int dirindexi
= static_cast<int>(dirindex
);
1628 read_unsigned_LEB_128(lineptr
, &len
); // mod_time
1631 read_unsigned_LEB_128(lineptr
, &len
); // filelength
1634 gold_assert(fileindex
1635 == static_cast<int>(this->files_
.back().size()));
1636 this->files_
.back().push_back(std::make_pair(dirindexi
, filename
));
1645 // Process a single opcode in the .debug.line structure.
1647 template<int size
, bool big_endian
>
1649 Sized_dwarf_line_info
<size
, big_endian
>::process_one_opcode(
1650 const unsigned char* start
, struct LineStateMachine
* lsm
, size_t* len
)
1654 unsigned char opcode
= *start
;
1658 // If the opcode is great than the opcode_base, it is a special
1659 // opcode. Most line programs consist mainly of special opcodes.
1660 if (opcode
>= header_
.opcode_base
)
1662 opcode
-= header_
.opcode_base
;
1663 const int advance_address
= ((opcode
/ header_
.line_range
)
1664 * header_
.min_insn_length
);
1665 lsm
->address
+= advance_address
;
1667 const int advance_line
= ((opcode
% header_
.line_range
)
1668 + header_
.line_base
);
1669 lsm
->line_num
+= advance_line
;
1670 lsm
->basic_block
= true;
1675 // Otherwise, we have the regular opcodes
1678 case elfcpp::DW_LNS_copy
:
1679 lsm
->basic_block
= false;
1683 case elfcpp::DW_LNS_advance_pc
:
1685 const uint64_t advance_address
1686 = read_unsigned_LEB_128(start
, &templen
);
1688 lsm
->address
+= header_
.min_insn_length
* advance_address
;
1692 case elfcpp::DW_LNS_advance_line
:
1694 const uint64_t advance_line
= read_signed_LEB_128(start
, &templen
);
1696 lsm
->line_num
+= advance_line
;
1700 case elfcpp::DW_LNS_set_file
:
1702 const uint64_t fileno
= read_unsigned_LEB_128(start
, &templen
);
1704 lsm
->file_num
= fileno
;
1708 case elfcpp::DW_LNS_set_column
:
1710 const uint64_t colno
= read_unsigned_LEB_128(start
, &templen
);
1712 lsm
->column_num
= colno
;
1716 case elfcpp::DW_LNS_negate_stmt
:
1717 lsm
->is_stmt
= !lsm
->is_stmt
;
1720 case elfcpp::DW_LNS_set_basic_block
:
1721 lsm
->basic_block
= true;
1724 case elfcpp::DW_LNS_fixed_advance_pc
:
1726 int advance_address
;
1727 advance_address
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(start
);
1729 lsm
->address
+= advance_address
;
1733 case elfcpp::DW_LNS_const_add_pc
:
1735 const int advance_address
= (header_
.min_insn_length
1736 * ((255 - header_
.opcode_base
)
1737 / header_
.line_range
));
1738 lsm
->address
+= advance_address
;
1742 case elfcpp::DW_LNS_extended_op
:
1744 const uint64_t extended_op_len
1745 = read_unsigned_LEB_128(start
, &templen
);
1747 oplen
+= templen
+ extended_op_len
;
1749 const unsigned char extended_op
= *start
;
1752 switch (extended_op
)
1754 case elfcpp::DW_LNE_end_sequence
:
1755 // This means that the current byte is the one immediately
1756 // after a set of instructions. Record the current line
1757 // for up to one less than the current address.
1759 lsm
->end_sequence
= true;
1763 case elfcpp::DW_LNE_set_address
:
1766 elfcpp::Swap_unaligned
<size
, big_endian
>::readval(start
);
1767 typename
Reloc_map::const_iterator it
1768 = this->reloc_map_
.find(start
- this->buffer_
);
1769 if (it
!= reloc_map_
.end())
1771 // If this is a SHT_RELA section, then ignore the
1772 // section contents. This assumes that this is a
1773 // straight reloc which just uses the reloc addend.
1774 // The reloc addend has already been included in the
1776 if (this->track_relocs_type_
== elfcpp::SHT_RELA
)
1778 // Add in the symbol value.
1779 lsm
->address
+= it
->second
.second
;
1780 lsm
->shndx
= it
->second
.first
;
1784 // If we're a normal .o file, with relocs, every
1785 // set_address should have an associated relocation.
1786 if (this->input_is_relobj())
1787 this->data_valid_
= false;
1791 case elfcpp::DW_LNE_define_file
:
1793 const char* filename
= reinterpret_cast<const char*>(start
);
1794 templen
= strlen(filename
) + 1;
1797 uint64_t dirindex
= read_unsigned_LEB_128(start
, &templen
);
1799 if (dirindex
>= this->directories_
.back().size())
1801 int dirindexi
= static_cast<int>(dirindex
);
1803 // This opcode takes two additional ULEB128 parameters
1804 // (mod_time and filelength), but we don't use those
1805 // values. Because OPLEN already tells us how far to
1806 // skip to the next opcode, we don't need to read
1809 this->files_
.back().push_back(std::make_pair(dirindexi
,
1819 // Ignore unknown opcode silently
1820 for (int i
= 0; i
< header_
.std_opcode_lengths
[opcode
]; i
++)
1823 read_unsigned_LEB_128(start
, &templen
);
1834 // Read the debug information at LINEPTR and store it in the line
1837 template<int size
, bool big_endian
>
1838 unsigned const char*
1839 Sized_dwarf_line_info
<size
, big_endian
>::read_lines(unsigned const char* lineptr
,
1842 struct LineStateMachine lsm
;
1844 // LENGTHSTART is the place the length field is based on. It is the
1845 // point in the header after the initial length field.
1846 const unsigned char* lengthstart
= buffer_
;
1848 // In 64 bit dwarf, the initial length is 12 bytes, because of the
1849 // 0xffffffff at the start.
1850 if (header_
.offset_size
== 8)
1855 while (lineptr
< lengthstart
+ header_
.total_length
)
1857 ResetLineStateMachine(&lsm
, header_
.default_is_stmt
);
1858 while (!lsm
.end_sequence
)
1861 bool add_line
= this->process_one_opcode(lineptr
, &lsm
, &oplength
);
1863 && (shndx
== -1U || lsm
.shndx
== -1U || shndx
== lsm
.shndx
))
1865 Offset_to_lineno_entry entry
1866 = { static_cast<off_t
>(lsm
.address
),
1867 this->current_header_index_
,
1868 static_cast<unsigned int>(lsm
.file_num
),
1869 true, lsm
.line_num
};
1870 std::vector
<Offset_to_lineno_entry
>&
1871 map(this->line_number_map_
[lsm
.shndx
]);
1872 // If we see two consecutive entries with the same
1873 // offset and a real line number, then mark the first
1874 // one as non-canonical.
1876 && (map
.back().offset
== static_cast<off_t
>(lsm
.address
))
1877 && lsm
.line_num
!= -1
1878 && map
.back().line_num
!= -1)
1879 map
.back().last_line_for_offset
= false;
1880 map
.push_back(entry
);
1882 lineptr
+= oplength
;
1886 return lengthstart
+ header_
.total_length
;
1889 // Read the relocations into a Reloc_map.
1891 template<int size
, bool big_endian
>
1893 Sized_dwarf_line_info
<size
, big_endian
>::read_relocs()
1895 if (this->symtab_buffer_
== NULL
)
1900 while ((reloc_offset
= this->reloc_mapper_
->next_offset()) != -1)
1902 const unsigned int shndx
=
1903 this->reloc_mapper_
->get_reloc_target(reloc_offset
, &value
);
1905 // There is no reason to record non-ordinary section indexes, or
1906 // SHN_UNDEF, because they will never match the real section.
1908 this->reloc_map_
[reloc_offset
] = std::make_pair(shndx
, value
);
1910 this->reloc_mapper_
->advance(reloc_offset
+ 1);
1914 // Read the line number info.
1916 template<int size
, bool big_endian
>
1918 Sized_dwarf_line_info
<size
, big_endian
>::read_line_mappings(unsigned int shndx
)
1920 gold_assert(this->data_valid_
== true);
1922 this->read_relocs();
1923 while (this->buffer_
< this->buffer_end_
)
1925 const unsigned char* lineptr
= this->buffer_
;
1926 lineptr
= this->read_header_prolog(lineptr
);
1927 lineptr
= this->read_header_tables(lineptr
);
1928 lineptr
= this->read_lines(lineptr
, shndx
);
1929 this->buffer_
= lineptr
;
1932 // Sort the lines numbers, so addr2line can use binary search.
1933 for (typename
Lineno_map::iterator it
= line_number_map_
.begin();
1934 it
!= line_number_map_
.end();
1936 // Each vector needs to be sorted by offset.
1937 std::sort(it
->second
.begin(), it
->second
.end());
1940 // Some processing depends on whether the input is a .o file or not.
1941 // For instance, .o files have relocs, and have .debug_lines
1942 // information on a per section basis. .so files, on the other hand,
1943 // lack relocs, and offsets are unique, so we can ignore the section
1946 template<int size
, bool big_endian
>
1948 Sized_dwarf_line_info
<size
, big_endian
>::input_is_relobj()
1950 // Only .o files have relocs and the symtab buffer that goes with them.
1951 return this->symtab_buffer_
!= NULL
;
1954 // Given an Offset_to_lineno_entry vector, and an offset, figure out
1955 // if the offset points into a function according to the vector (see
1956 // comments below for the algorithm). If it does, return an iterator
1957 // into the vector that points to the line-number that contains that
1958 // offset. If not, it returns vector::end().
1960 static std::vector
<Offset_to_lineno_entry
>::const_iterator
1961 offset_to_iterator(const std::vector
<Offset_to_lineno_entry
>* offsets
,
1964 const Offset_to_lineno_entry lookup_key
= { offset
, 0, 0, true, 0 };
1966 // lower_bound() returns the smallest offset which is >= lookup_key.
1967 // If no offset in offsets is >= lookup_key, returns end().
1968 std::vector
<Offset_to_lineno_entry
>::const_iterator it
1969 = std::lower_bound(offsets
->begin(), offsets
->end(), lookup_key
);
1971 // This code is easiest to understand with a concrete example.
1972 // Here's a possible offsets array:
1973 // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0
1974 // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1
1975 // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2
1976 // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3
1977 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4
1978 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5
1979 // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6
1980 // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7
1981 // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8
1982 // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9
1983 // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10
1984 // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11
1985 // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12
1986 // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13
1987 // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14
1988 // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15
1989 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16
1990 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17
1991 // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18
1992 // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19
1993 // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20
1994 // The entries with line_num == -1 mark the end of a function: the
1995 // associated offset is one past the last instruction in the
1996 // function. This can correspond to the beginning of the next
1997 // function (as is true for offset 3232); alternately, there can be
1998 // a gap between the end of one function and the start of the next
1999 // (as is true for some others, most obviously from 3236->5764).
2001 // Case 1: lookup_key has offset == 10. lower_bound returns
2002 // offsets[0]. Since it's not an exact match and we're
2003 // at the beginning of offsets, we return end() (invalid).
2004 // Case 2: lookup_key has offset 10000. lower_bound returns
2005 // offset[21] (end()). We return end() (invalid).
2006 // Case 3: lookup_key has offset == 3211. lower_bound matches
2007 // offsets[0] exactly, and that's the entry we return.
2008 // Case 4: lookup_key has offset == 3232. lower_bound returns
2009 // offsets[4]. That's an exact match, but indicates
2010 // end-of-function. We check if offsets[5] is also an
2011 // exact match but not end-of-function. It is, so we
2012 // return offsets[5].
2013 // Case 5: lookup_key has offset == 3214. lower_bound returns
2014 // offsets[1]. Since it's not an exact match, we back
2015 // up to the offset that's < lookup_key, offsets[0].
2016 // We note offsets[0] is a valid entry (not end-of-function),
2017 // so that's the entry we return.
2018 // Case 6: lookup_key has offset == 4000. lower_bound returns
2019 // offsets[8]. Since it's not an exact match, we back
2020 // up to offsets[7]. Since offsets[7] indicates
2021 // end-of-function, we know lookup_key is between
2022 // functions, so we return end() (not a valid offset).
2023 // Case 7: lookup_key has offset == 5794. lower_bound returns
2024 // offsets[19]. Since it's not an exact match, we back
2025 // up to offsets[16]. Note we back up to the *first*
2026 // entry with offset 5793, not just offsets[19-1].
2027 // We note offsets[16] is a valid entry, so we return it.
2028 // If offsets[16] had had line_num == -1, we would have
2029 // checked offsets[17]. The reason for this is that
2030 // 16 and 17 can be in an arbitrary order, since we sort
2031 // only by offset and last_line_for_offset. (Note it
2032 // doesn't help to use line_number as a tertiary sort key,
2033 // since sometimes we want the -1 to be first and sometimes
2034 // we want it to be last.)
2036 // This deals with cases (1) and (2).
2037 if ((it
== offsets
->begin() && offset
< it
->offset
)
2038 || it
== offsets
->end())
2039 return offsets
->end();
2041 // This deals with cases (3) and (4).
2042 if (offset
== it
->offset
)
2044 while (it
!= offsets
->end()
2045 && it
->offset
== offset
2046 && it
->line_num
== -1)
2048 if (it
== offsets
->end() || it
->offset
!= offset
)
2049 return offsets
->end();
2054 // This handles the first part of case (7) -- we back up to the
2055 // *first* entry that has the offset that's behind us.
2056 gold_assert(it
!= offsets
->begin());
2057 std::vector
<Offset_to_lineno_entry
>::const_iterator range_end
= it
;
2059 const off_t range_value
= it
->offset
;
2060 while (it
!= offsets
->begin() && (it
-1)->offset
== range_value
)
2063 // This handles cases (5), (6), and (7): if any entry in the
2064 // equal_range [it, range_end) has a line_num != -1, it's a valid
2065 // match. If not, we're not in a function. The line number we saw
2066 // last for an offset will be sorted first, so it'll get returned if
2068 for (; it
!= range_end
; ++it
)
2069 if (it
->line_num
!= -1)
2071 return offsets
->end();
2074 // Returns the canonical filename:lineno for the address passed in.
2075 // If other_lines is not NULL, appends the non-canonical lines
2076 // assigned to the same address.
2078 template<int size
, bool big_endian
>
2080 Sized_dwarf_line_info
<size
, big_endian
>::do_addr2line(
2083 std::vector
<std::string
>* other_lines
)
2085 if (this->data_valid_
== false)
2088 const std::vector
<Offset_to_lineno_entry
>* offsets
;
2089 // If we do not have reloc information, then our input is a .so or
2090 // some similar data structure where all the information is held in
2091 // the offset. In that case, we ignore the input shndx.
2092 if (this->input_is_relobj())
2093 offsets
= &this->line_number_map_
[shndx
];
2095 offsets
= &this->line_number_map_
[-1U];
2096 if (offsets
->empty())
2099 typename
std::vector
<Offset_to_lineno_entry
>::const_iterator it
2100 = offset_to_iterator(offsets
, offset
);
2101 if (it
== offsets
->end())
2104 std::string result
= this->format_file_lineno(*it
);
2105 if (other_lines
!= NULL
)
2106 for (++it
; it
!= offsets
->end() && it
->offset
== offset
; ++it
)
2108 if (it
->line_num
== -1)
2109 continue; // The end of a previous function.
2110 other_lines
->push_back(this->format_file_lineno(*it
));
2115 // Convert the file_num + line_num into a string.
2117 template<int size
, bool big_endian
>
2119 Sized_dwarf_line_info
<size
, big_endian
>::format_file_lineno(
2120 const Offset_to_lineno_entry
& loc
) const
2124 gold_assert(loc
.header_num
< static_cast<int>(this->files_
.size()));
2125 gold_assert(loc
.file_num
2126 < static_cast<unsigned int>(this->files_
[loc
.header_num
].size()));
2127 const std::pair
<int, std::string
>& filename_pair
2128 = this->files_
[loc
.header_num
][loc
.file_num
];
2129 const std::string
& filename
= filename_pair
.second
;
2131 gold_assert(loc
.header_num
< static_cast<int>(this->directories_
.size()));
2132 gold_assert(filename_pair
.first
2133 < static_cast<int>(this->directories_
[loc
.header_num
].size()));
2134 const std::string
& dirname
2135 = this->directories_
[loc
.header_num
][filename_pair
.first
];
2137 if (!dirname
.empty())
2146 char buffer
[64]; // enough to hold a line number
2147 snprintf(buffer
, sizeof(buffer
), "%d", loc
.line_num
);
2154 // Dwarf_line_info routines.
2156 static unsigned int next_generation_count
= 0;
2158 struct Addr2line_cache_entry
2162 Dwarf_line_info
* dwarf_line_info
;
2163 unsigned int generation_count
;
2164 unsigned int access_count
;
2166 Addr2line_cache_entry(Object
* o
, unsigned int s
, Dwarf_line_info
* d
)
2167 : object(o
), shndx(s
), dwarf_line_info(d
),
2168 generation_count(next_generation_count
), access_count(0)
2170 if (next_generation_count
< (1U << 31))
2171 ++next_generation_count
;
2174 // We expect this cache to be small, so don't bother with a hashtable
2175 // or priority queue or anything: just use a simple vector.
2176 static std::vector
<Addr2line_cache_entry
> addr2line_cache
;
2179 Dwarf_line_info::one_addr2line(Object
* object
,
2180 unsigned int shndx
, off_t offset
,
2182 std::vector
<std::string
>* other_lines
)
2184 Dwarf_line_info
* lineinfo
= NULL
;
2185 std::vector
<Addr2line_cache_entry
>::iterator it
;
2187 // First, check the cache. If we hit, update the counts.
2188 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
2190 if (it
->object
== object
&& it
->shndx
== shndx
)
2192 lineinfo
= it
->dwarf_line_info
;
2193 it
->generation_count
= next_generation_count
;
2194 // We cap generation_count at 2^31 -1 to avoid overflow.
2195 if (next_generation_count
< (1U << 31))
2196 ++next_generation_count
;
2197 // We cap access_count at 31 so 2^access_count doesn't overflow
2198 if (it
->access_count
< 31)
2204 // If we don't hit the cache, create a new object and insert into the
2206 if (lineinfo
== NULL
)
2208 switch (parameters
->size_and_endianness())
2210 #ifdef HAVE_TARGET_32_LITTLE
2211 case Parameters::TARGET_32_LITTLE
:
2212 lineinfo
= new Sized_dwarf_line_info
<32, false>(object
, shndx
); break;
2214 #ifdef HAVE_TARGET_32_BIG
2215 case Parameters::TARGET_32_BIG
:
2216 lineinfo
= new Sized_dwarf_line_info
<32, true>(object
, shndx
); break;
2218 #ifdef HAVE_TARGET_64_LITTLE
2219 case Parameters::TARGET_64_LITTLE
:
2220 lineinfo
= new Sized_dwarf_line_info
<64, false>(object
, shndx
); break;
2222 #ifdef HAVE_TARGET_64_BIG
2223 case Parameters::TARGET_64_BIG
:
2224 lineinfo
= new Sized_dwarf_line_info
<64, true>(object
, shndx
); break;
2229 addr2line_cache
.push_back(Addr2line_cache_entry(object
, shndx
, lineinfo
));
2232 // Now that we have our object, figure out the answer
2233 std::string retval
= lineinfo
->addr2line(shndx
, offset
, other_lines
);
2235 // Finally, if our cache has grown too big, delete old objects. We
2236 // assume the common (probably only) case is deleting only one object.
2237 // We use a pretty simple scheme to evict: function of LRU and MFU.
2238 while (addr2line_cache
.size() > cache_size
)
2240 unsigned int lowest_score
= ~0U;
2241 std::vector
<Addr2line_cache_entry
>::iterator lowest
2242 = addr2line_cache
.end();
2243 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
2245 const unsigned int score
= (it
->generation_count
2246 + (1U << it
->access_count
));
2247 if (score
< lowest_score
)
2249 lowest_score
= score
;
2253 if (lowest
!= addr2line_cache
.end())
2255 delete lowest
->dwarf_line_info
;
2256 addr2line_cache
.erase(lowest
);
2264 Dwarf_line_info::clear_addr2line_cache()
2266 for (std::vector
<Addr2line_cache_entry
>::iterator it
= addr2line_cache
.begin();
2267 it
!= addr2line_cache
.end();
2269 delete it
->dwarf_line_info
;
2270 addr2line_cache
.clear();
2273 #ifdef HAVE_TARGET_32_LITTLE
2275 class Sized_dwarf_line_info
<32, false>;
2278 #ifdef HAVE_TARGET_32_BIG
2280 class Sized_dwarf_line_info
<32, true>;
2283 #ifdef HAVE_TARGET_64_LITTLE
2285 class Sized_dwarf_line_info
<64, false>;
2288 #ifdef HAVE_TARGET_64_BIG
2290 class Sized_dwarf_line_info
<64, true>;
2293 } // End namespace gold.