1 // dwarf_reader.cc -- parse dwarf2/3 debug information
3 // Copyright (C) 2007-2023 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.
30 #include "elfcpp_swap.h"
34 #include "dwarf_reader.h"
35 #include "int_encoding.h"
36 #include "compressed_output.h"
40 // Class Sized_elf_reloc_mapper
42 // Initialize the relocation tracker for section RELOC_SHNDX.
44 template<int size
, bool big_endian
>
46 Sized_elf_reloc_mapper
<size
, big_endian
>::do_initialize(
47 unsigned int reloc_shndx
, unsigned int reloc_type
)
49 this->reloc_type_
= reloc_type
;
50 return this->track_relocs_
.initialize(this->object_
, reloc_shndx
,
54 // Looks in the symtab to see what section a symbol is in.
56 template<int size
, bool big_endian
>
58 Sized_elf_reloc_mapper
<size
, big_endian
>::symbol_section(
59 unsigned int symndx
, Address
* value
, bool* is_ordinary
)
61 const int symsize
= elfcpp::Elf_sizes
<size
>::sym_size
;
62 gold_assert(static_cast<off_t
>((symndx
+ 1) * symsize
) <= this->symtab_size_
);
63 elfcpp::Sym
<size
, big_endian
> elfsym(this->symtab_
+ symndx
* symsize
);
64 *value
= elfsym
.get_st_value();
65 return this->object_
->adjust_sym_shndx(symndx
, elfsym
.get_st_shndx(),
69 // Return the section index and offset within the section of
70 // the target of the relocation for RELOC_OFFSET.
72 template<int size
, bool big_endian
>
74 Sized_elf_reloc_mapper
<size
, big_endian
>::do_get_reloc_target(
75 off_t reloc_offset
, off_t
* target_offset
)
77 this->track_relocs_
.advance(reloc_offset
);
78 if (reloc_offset
!= this->track_relocs_
.next_offset())
80 unsigned int symndx
= this->track_relocs_
.next_symndx();
81 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
83 unsigned int target_shndx
= this->symbol_section(symndx
, &value
,
87 if (this->reloc_type_
== elfcpp::SHT_RELA
)
88 value
+= this->track_relocs_
.next_addend();
89 *target_offset
= value
;
93 static inline Elf_reloc_mapper
*
94 make_elf_reloc_mapper(Relobj
* object
, const unsigned char* symtab
,
97 if (object
->elfsize() == 32)
99 if (object
->is_big_endian())
101 #ifdef HAVE_TARGET_32_BIG
102 return new Sized_elf_reloc_mapper
<32, true>(object
, symtab
,
110 #ifdef HAVE_TARGET_32_LITTLE
111 return new Sized_elf_reloc_mapper
<32, false>(object
, symtab
,
118 else if (object
->elfsize() == 64)
120 if (object
->is_big_endian())
122 #ifdef HAVE_TARGET_64_BIG
123 return new Sized_elf_reloc_mapper
<64, true>(object
, symtab
,
131 #ifdef HAVE_TARGET_64_LITTLE
132 return new Sized_elf_reloc_mapper
<64, false>(object
, symtab
,
143 // class Dwarf_abbrev_table
146 Dwarf_abbrev_table::clear_abbrev_codes()
148 for (unsigned int code
= 0; code
< this->low_abbrev_code_max_
; ++code
)
150 if (this->low_abbrev_codes_
[code
] != NULL
)
152 delete this->low_abbrev_codes_
[code
];
153 this->low_abbrev_codes_
[code
] = NULL
;
156 for (Abbrev_code_table::iterator it
= this->high_abbrev_codes_
.begin();
157 it
!= this->high_abbrev_codes_
.end();
160 if (it
->second
!= NULL
)
163 this->high_abbrev_codes_
.clear();
166 // Read the abbrev table from an object file.
169 Dwarf_abbrev_table::do_read_abbrevs(
171 unsigned int abbrev_shndx
,
174 this->clear_abbrev_codes();
176 // If we don't have relocations, abbrev_shndx will be 0, and
177 // we'll have to hunt for the .debug_abbrev section.
178 if (abbrev_shndx
== 0 && this->abbrev_shndx_
> 0)
179 abbrev_shndx
= this->abbrev_shndx_
;
180 else if (abbrev_shndx
== 0)
182 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
184 std::string name
= object
->section_name(i
);
185 if (name
== ".debug_abbrev" || name
== ".zdebug_abbrev")
188 // Correct the offset. For incremental update links, we have a
189 // relocated offset that is relative to the output section, but
190 // here we need an offset relative to the input section.
191 abbrev_offset
-= object
->output_section_offset(i
);
195 if (abbrev_shndx
== 0)
199 // Get the section contents and decompress if necessary.
200 if (abbrev_shndx
!= this->abbrev_shndx_
)
202 if (this->owns_buffer_
&& this->buffer_
!= NULL
)
204 delete[] this->buffer_
;
205 this->owns_buffer_
= false;
208 section_size_type buffer_size
;
210 object
->decompressed_section_contents(abbrev_shndx
,
212 &this->owns_buffer_
);
213 this->buffer_end_
= this->buffer_
+ buffer_size
;
214 this->abbrev_shndx_
= abbrev_shndx
;
217 this->buffer_pos_
= this->buffer_
+ abbrev_offset
;
221 // Lookup the abbrev code entry for CODE. This function is called
222 // only when the abbrev code is not in the direct lookup table.
223 // It may be in the hash table, it may not have been read yet,
224 // or it may not exist in the abbrev table.
226 const Dwarf_abbrev_table::Abbrev_code
*
227 Dwarf_abbrev_table::do_get_abbrev(unsigned int code
)
229 // See if the abbrev code is already in the hash table.
230 Abbrev_code_table::const_iterator it
= this->high_abbrev_codes_
.find(code
);
231 if (it
!= this->high_abbrev_codes_
.end())
234 // Read and store abbrev code definitions until we find the
235 // one we're looking for.
238 // Read the abbrev code. A zero here indicates the end of the
241 if (this->buffer_pos_
>= this->buffer_end_
)
243 uint64_t nextcode
= read_unsigned_LEB_128(this->buffer_pos_
, &len
);
246 this->buffer_pos_
= this->buffer_end_
;
249 this->buffer_pos_
+= len
;
252 if (this->buffer_pos_
>= this->buffer_end_
)
254 uint64_t tag
= read_unsigned_LEB_128(this->buffer_pos_
, &len
);
255 this->buffer_pos_
+= len
;
257 // Read the has_children flag.
258 if (this->buffer_pos_
>= this->buffer_end_
)
260 bool has_children
= *this->buffer_pos_
== elfcpp::DW_CHILDREN_yes
;
261 this->buffer_pos_
+= 1;
263 // Read the list of (attribute, form) pairs.
264 Abbrev_code
* entry
= new Abbrev_code(tag
, has_children
);
267 // Read the attribute.
268 if (this->buffer_pos_
>= this->buffer_end_
)
270 uint64_t attr
= read_unsigned_LEB_128(this->buffer_pos_
, &len
);
271 this->buffer_pos_
+= len
;
274 if (this->buffer_pos_
>= this->buffer_end_
)
276 uint64_t form
= read_unsigned_LEB_128(this->buffer_pos_
, &len
);
277 this->buffer_pos_
+= len
;
279 // For DW_FORM_implicit_const, read the constant.
280 int64_t implicit_const
= 0;
281 if (form
== elfcpp::DW_FORM_implicit_const
)
283 implicit_const
= read_signed_LEB_128(this->buffer_pos_
, &len
);
284 this->buffer_pos_
+= len
;
287 // A (0,0) pair terminates the list.
288 if (attr
== 0 && form
== 0)
291 if (attr
== elfcpp::DW_AT_sibling
)
292 entry
->has_sibling_attribute
= true;
294 entry
->add_attribute(attr
, form
, implicit_const
);
297 this->store_abbrev(nextcode
, entry
);
298 if (nextcode
== code
)
305 // class Dwarf_ranges_table
307 // Read the ranges table from an object file.
310 Dwarf_ranges_table::read_ranges_table(
312 const unsigned char* symtab
,
314 unsigned int ranges_shndx
,
315 unsigned int version
)
317 const std::string
section_name(version
< 5
319 : ".debug_rnglists");
320 const std::string
compressed_section_name(version
< 5
322 : ".zdebug_rnglists");
324 // If we've already read this abbrev table, return immediately.
325 if (this->ranges_shndx_
> 0
326 && this->ranges_shndx_
== ranges_shndx
)
329 // If we don't have relocations, ranges_shndx will be 0, and
330 // we'll have to hunt for the .debug_ranges section.
331 if (ranges_shndx
== 0 && this->ranges_shndx_
> 0)
332 ranges_shndx
= this->ranges_shndx_
;
333 else if (ranges_shndx
== 0)
335 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
337 std::string name
= object
->section_name(i
);
338 if (name
== section_name
|| name
== compressed_section_name
)
341 this->output_section_offset_
= object
->output_section_offset(i
);
345 if (ranges_shndx
== 0)
349 // Get the section contents and decompress if necessary.
350 if (ranges_shndx
!= this->ranges_shndx_
)
352 if (this->owns_ranges_buffer_
&& this->ranges_buffer_
!= NULL
)
354 delete[] this->ranges_buffer_
;
355 this->owns_ranges_buffer_
= false;
358 section_size_type buffer_size
;
359 this->ranges_buffer_
=
360 object
->decompressed_section_contents(ranges_shndx
,
362 &this->owns_ranges_buffer_
);
363 this->ranges_buffer_end_
= this->ranges_buffer_
+ buffer_size
;
364 this->ranges_shndx_
= ranges_shndx
;
367 if (this->ranges_reloc_mapper_
!= NULL
)
369 delete this->ranges_reloc_mapper_
;
370 this->ranges_reloc_mapper_
= NULL
;
373 // For incremental objects, we have no relocations.
374 if (object
->is_incremental())
377 // Find the relocation section for ".debug_ranges".
378 unsigned int reloc_shndx
= 0;
379 unsigned int reloc_type
= 0;
380 for (unsigned int i
= 0; i
< object
->shnum(); ++i
)
382 reloc_type
= object
->section_type(i
);
383 if ((reloc_type
== elfcpp::SHT_REL
384 || reloc_type
== elfcpp::SHT_RELA
)
385 && object
->section_info(i
) == ranges_shndx
)
392 this->ranges_reloc_mapper_
= make_elf_reloc_mapper(object
, symtab
,
394 this->ranges_reloc_mapper_
->initialize(reloc_shndx
, reloc_type
);
395 this->reloc_type_
= reloc_type
;
400 // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
403 Dwarf_ranges_table::read_range_list(
405 const unsigned char* symtab
,
407 unsigned int addr_size
,
408 unsigned int ranges_shndx
,
411 Dwarf_range_list
* ranges
;
413 if (!this->read_ranges_table(object
, symtab
, symtab_size
, ranges_shndx
, 4))
416 // Correct the offset. For incremental update links, we have a
417 // relocated offset that is relative to the output section, but
418 // here we need an offset relative to the input section.
419 offset
-= this->output_section_offset_
;
421 // Read the range list at OFFSET.
422 ranges
= new Dwarf_range_list();
425 this->ranges_buffer_
+ offset
< this->ranges_buffer_end_
;
426 offset
+= 2 * addr_size
)
431 // Read the raw contents of the section.
434 start
= this->dwinfo_
->read_from_pointer
<32>(this->ranges_buffer_
436 end
= this->dwinfo_
->read_from_pointer
<32>(this->ranges_buffer_
441 start
= this->dwinfo_
->read_from_pointer
<64>(this->ranges_buffer_
443 end
= this->dwinfo_
->read_from_pointer
<64>(this->ranges_buffer_
447 // Check for relocations and adjust the values.
448 unsigned int shndx1
= 0;
449 unsigned int shndx2
= 0;
450 if (this->ranges_reloc_mapper_
!= NULL
)
452 shndx1
= this->lookup_reloc(offset
, &start
);
453 shndx2
= this->lookup_reloc(offset
+ addr_size
, &end
);
456 // End of list is marked by a pair of zeroes.
457 if (shndx1
== 0 && start
== 0 && end
== 0)
460 // A "base address selection entry" is identified by
461 // 0xffffffff for the first value of the pair. The second
462 // value is used as a base for subsequent range list entries.
463 if (shndx1
== 0 && start
== -1)
465 else if (shndx1
== shndx2
)
467 if (shndx1
== 0 || object
->is_section_included(shndx1
))
468 ranges
->add(shndx1
, base
+ start
, base
+ end
);
471 gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
472 "range list entry are in different sections"),
473 object
->name().c_str());
479 // Read a DWARF 5 range list from section RANGES_SHNDX at offset RANGES_OFFSET.
482 Dwarf_ranges_table::read_range_list_v5(
484 const unsigned char* symtab
,
486 unsigned int addr_size
,
487 unsigned int ranges_shndx
,
490 Dwarf_range_list
* ranges
;
492 if (!this->read_ranges_table(object
, symtab
, symtab_size
, ranges_shndx
, 5))
495 ranges
= new Dwarf_range_list();
497 unsigned int shndx0
= 0;
499 // Correct the offset. For incremental update links, we have a
500 // relocated offset that is relative to the output section, but
501 // here we need an offset relative to the input section.
502 offset
-= this->output_section_offset_
;
504 // Read the range list at OFFSET.
505 const unsigned char* prle
= this->ranges_buffer_
+ offset
;
506 while (prle
< this->ranges_buffer_end_
)
510 unsigned int shndx1
= 0;
511 unsigned int shndx2
= 0;
514 // Read the entry type.
515 unsigned int rle_type
= *prle
++;
518 if (rle_type
== elfcpp::DW_RLE_end_of_list
)
523 case elfcpp::DW_RLE_base_address
:
525 base
= this->dwinfo_
->read_from_pointer
<32>(prle
);
527 base
= this->dwinfo_
->read_from_pointer
<64>(prle
);
528 if (this->ranges_reloc_mapper_
!= NULL
)
529 shndx0
= this->lookup_reloc(offset
, &base
);
534 case elfcpp::DW_RLE_offset_pair
:
535 start
= read_unsigned_LEB_128(prle
, &len
);
538 end
= read_unsigned_LEB_128(prle
, &len
);
541 if (shndx0
== 0 || object
->is_section_included(shndx0
))
542 ranges
->add(shndx0
, base
+ start
, base
+ end
);
545 case elfcpp::DW_RLE_start_end
:
548 start
= this->dwinfo_
->read_from_pointer
<32>(prle
);
549 end
= this->dwinfo_
->read_from_pointer
<32>(prle
+ 4);
553 start
= this->dwinfo_
->read_from_pointer
<64>(prle
);
554 end
= this->dwinfo_
->read_from_pointer
<64>(prle
+ 8);
556 if (this->ranges_reloc_mapper_
!= NULL
)
558 shndx1
= this->lookup_reloc(offset
, &start
);
559 shndx2
= this->lookup_reloc(offset
+ addr_size
, &end
);
560 if (shndx1
!= shndx2
)
561 gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
562 "range list entry are in different sections"),
563 object
->name().c_str());
565 prle
+= addr_size
* 2;
566 offset
+= addr_size
* 2;
567 if (shndx1
== 0 || object
->is_section_included(shndx1
))
568 ranges
->add(shndx1
, start
, end
);
571 case elfcpp::DW_RLE_start_length
:
573 start
= this->dwinfo_
->read_from_pointer
<32>(prle
);
575 start
= this->dwinfo_
->read_from_pointer
<64>(prle
);
576 if (this->ranges_reloc_mapper_
!= NULL
)
577 shndx1
= this->lookup_reloc(offset
, &start
);
580 end
= start
+ read_unsigned_LEB_128(prle
, &len
);
583 if (shndx1
== 0 || object
->is_section_included(shndx1
))
584 ranges
->add(shndx1
, start
, end
);
588 gold_warning(_("%s: DWARF range list contains "
589 "unsupported entry type (%d)"),
590 object
->name().c_str(), rle_type
);
598 // Look for a relocation at offset OFF in the range table,
599 // and return the section index and offset of the target.
602 Dwarf_ranges_table::lookup_reloc(off_t off
, off_t
* target_off
)
606 this->ranges_reloc_mapper_
->get_reloc_target(off
, &value
);
609 if (this->reloc_type_
== elfcpp::SHT_REL
)
610 *target_off
+= value
;
616 // class Dwarf_pubnames_table
618 // Read the pubnames section from the object file.
621 Dwarf_pubnames_table::read_section(Relobj
* object
, const unsigned char* symtab
,
624 section_size_type buffer_size
;
625 unsigned int shndx
= 0;
626 const char* name
= this->is_pubtypes_
? "pubtypes" : "pubnames";
627 const char* gnu_name
= (this->is_pubtypes_
631 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
633 std::string section_name
= object
->section_name(i
);
634 const char* section_name_suffix
= section_name
.c_str();
635 if (is_prefix_of(".debug_", section_name_suffix
))
636 section_name_suffix
+= 7;
637 else if (is_prefix_of(".zdebug_", section_name_suffix
))
638 section_name_suffix
+= 8;
641 if (strcmp(section_name_suffix
, name
) == 0)
646 else if (strcmp(section_name_suffix
, gnu_name
) == 0)
649 this->is_gnu_style_
= true;
656 this->buffer_
= object
->decompressed_section_contents(shndx
,
658 &this->owns_buffer_
);
659 if (this->buffer_
== NULL
)
661 this->buffer_end_
= this->buffer_
+ buffer_size
;
663 // For incremental objects, we have no relocations.
664 if (object
->is_incremental())
667 // Find the relocation section
668 unsigned int reloc_shndx
= 0;
669 unsigned int reloc_type
= 0;
670 for (unsigned int i
= 0; i
< object
->shnum(); ++i
)
672 reloc_type
= object
->section_type(i
);
673 if ((reloc_type
== elfcpp::SHT_REL
674 || reloc_type
== elfcpp::SHT_RELA
)
675 && object
->section_info(i
) == shndx
)
682 this->reloc_mapper_
= make_elf_reloc_mapper(object
, symtab
, symtab_size
);
683 this->reloc_mapper_
->initialize(reloc_shndx
, reloc_type
);
684 this->reloc_type_
= reloc_type
;
689 // Read the header for the set at OFFSET.
692 Dwarf_pubnames_table::read_header(off_t offset
)
694 // Make sure we have actually read the section.
695 gold_assert(this->buffer_
!= NULL
);
697 if (offset
< 0 || offset
+ 14 >= this->buffer_end_
- this->buffer_
)
700 const unsigned char* pinfo
= this->buffer_
+ offset
;
702 // Read the unit_length field.
703 uint64_t unit_length
= this->dwinfo_
->read_from_pointer
<32>(pinfo
);
705 if (unit_length
== 0xffffffff)
707 unit_length
= this->dwinfo_
->read_from_pointer
<64>(pinfo
);
708 this->unit_length_
= unit_length
+ 12;
710 this->offset_size_
= 8;
714 this->unit_length_
= unit_length
+ 4;
715 this->offset_size_
= 4;
717 this->end_of_table_
= pinfo
+ unit_length
;
719 // If unit_length is too big, maybe we should reject the whole table,
720 // but in cases we know about, it seems OK to assume that the table
721 // is valid through the actual end of the section.
722 if (this->end_of_table_
> this->buffer_end_
)
723 this->end_of_table_
= this->buffer_end_
;
725 // Check the version.
726 unsigned int version
= this->dwinfo_
->read_from_pointer
<16>(pinfo
);
731 this->reloc_mapper_
->get_reloc_target(pinfo
- this->buffer_
,
734 // Skip the debug_info_offset and debug_info_size fields.
735 pinfo
+= 2 * this->offset_size_
;
737 if (pinfo
>= this->buffer_end_
)
740 this->pinfo_
= pinfo
;
744 // Read the next name from the set.
747 Dwarf_pubnames_table::next_name(uint8_t* flag_byte
)
749 const unsigned char* pinfo
= this->pinfo_
;
751 // Check for end of list. The table should be terminated by an
752 // entry containing nothing but a DIE offset of 0.
753 if (pinfo
+ this->offset_size_
>= this->end_of_table_
)
756 // Skip the offset within the CU. If this is zero, but we're not
757 // at the end of the table, then we have a real pubnames entry
758 // whose DIE offset is 0 (likely to be a GCC bug). Since we
759 // don't actually use the DIE offset in building .gdb_index,
761 pinfo
+= this->offset_size_
;
763 if (this->is_gnu_style_
)
764 *flag_byte
= *pinfo
++;
768 // Return a pointer to the string at the current location,
769 // and advance the pointer to the next entry.
770 const char* ret
= reinterpret_cast<const char*>(pinfo
);
771 while (pinfo
< this->buffer_end_
&& *pinfo
!= '\0')
773 if (pinfo
< this->buffer_end_
)
776 this->pinfo_
= pinfo
;
782 Dwarf_die::Dwarf_die(
783 Dwarf_info_reader
* dwinfo
,
786 : dwinfo_(dwinfo
), parent_(parent
), die_offset_(die_offset
),
787 child_offset_(0), sibling_offset_(0), abbrev_code_(NULL
), attributes_(),
788 attributes_read_(false), name_(NULL
), name_off_(-1), linkage_name_(NULL
),
789 linkage_name_off_(-1), string_shndx_(0), specification_(0),
793 const unsigned char* pdie
= dwinfo
->buffer_at_offset(die_offset
);
796 unsigned int code
= read_unsigned_LEB_128(pdie
, &len
);
800 parent
->set_sibling_offset(die_offset
+ len
);
803 this->attr_offset_
= len
;
805 // Lookup the abbrev code in the abbrev table.
806 this->abbrev_code_
= dwinfo
->get_abbrev(code
);
809 // Read all the attributes of the DIE.
812 Dwarf_die::read_attributes()
814 if (this->attributes_read_
)
817 gold_assert(this->abbrev_code_
!= NULL
);
819 const unsigned char* pdie
=
820 this->dwinfo_
->buffer_at_offset(this->die_offset_
);
823 const unsigned char* pattr
= pdie
+ this->attr_offset_
;
825 unsigned int nattr
= this->abbrev_code_
->attributes
.size();
826 this->attributes_
.reserve(nattr
);
827 for (unsigned int i
= 0; i
< nattr
; ++i
)
830 unsigned int attr
= this->abbrev_code_
->attributes
[i
].attr
;
831 unsigned int form
= this->abbrev_code_
->attributes
[i
].form
;
832 if (form
== elfcpp::DW_FORM_indirect
)
834 form
= read_unsigned_LEB_128(pattr
, &len
);
837 off_t attr_off
= this->die_offset_
+ (pattr
- pdie
);
838 bool ref_form
= false;
839 Attribute_value attr_value
;
840 attr_value
.attr
= attr
;
841 attr_value
.form
= form
;
842 attr_value
.aux
.shndx
= 0;
845 case elfcpp::DW_FORM_flag_present
:
846 attr_value
.val
.intval
= 1;
848 case elfcpp::DW_FORM_implicit_const
:
849 attr_value
.val
.intval
=
850 this->abbrev_code_
->attributes
[i
].implicit_const
;
852 case elfcpp::DW_FORM_strp
:
853 case elfcpp::DW_FORM_strp_sup
:
854 case elfcpp::DW_FORM_line_strp
:
857 if (this->dwinfo_
->offset_size() == 4)
858 str_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
860 str_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
862 this->dwinfo_
->lookup_reloc(attr_off
, &str_off
);
863 attr_value
.aux
.shndx
= shndx
;
864 attr_value
.val
.refval
= str_off
;
867 case elfcpp::DW_FORM_strx
:
868 case elfcpp::DW_FORM_GNU_str_index
:
869 attr_value
.val
.uintval
= read_unsigned_LEB_128(pattr
, &len
);
872 case elfcpp::DW_FORM_strx1
:
873 attr_value
.val
.uintval
= *pattr
++;
875 case elfcpp::DW_FORM_strx2
:
876 attr_value
.val
.uintval
=
877 this->dwinfo_
->read_from_pointer
<16>(&pattr
);
879 case elfcpp::DW_FORM_strx3
:
880 attr_value
.val
.uintval
=
881 this->dwinfo_
->read_3bytes_from_pointer(&pattr
);
883 case elfcpp::DW_FORM_strx4
:
884 attr_value
.val
.uintval
=
885 this->dwinfo_
->read_from_pointer
<32>(&pattr
);
887 case elfcpp::DW_FORM_sec_offset
:
890 if (this->dwinfo_
->offset_size() == 4)
891 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
893 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
895 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
896 attr_value
.aux
.shndx
= shndx
;
897 attr_value
.val
.refval
= sec_off
;
901 case elfcpp::DW_FORM_addr
:
904 if (this->dwinfo_
->address_size() == 4)
905 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
907 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
909 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
910 attr_value
.aux
.shndx
= shndx
;
911 attr_value
.val
.refval
= sec_off
;
914 case elfcpp::DW_FORM_ref_addr
:
917 if (this->dwinfo_
->ref_addr_size() == 4)
918 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
920 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
922 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
923 attr_value
.aux
.shndx
= shndx
;
924 attr_value
.val
.refval
= sec_off
;
928 case elfcpp::DW_FORM_block1
:
929 attr_value
.aux
.blocklen
= *pattr
++;
930 attr_value
.val
.blockval
= pattr
;
931 pattr
+= attr_value
.aux
.blocklen
;
933 case elfcpp::DW_FORM_block2
:
934 attr_value
.aux
.blocklen
=
935 this->dwinfo_
->read_from_pointer
<16>(&pattr
);
936 attr_value
.val
.blockval
= pattr
;
937 pattr
+= attr_value
.aux
.blocklen
;
939 case elfcpp::DW_FORM_block4
:
940 attr_value
.aux
.blocklen
=
941 this->dwinfo_
->read_from_pointer
<32>(&pattr
);
942 attr_value
.val
.blockval
= pattr
;
943 pattr
+= attr_value
.aux
.blocklen
;
945 case elfcpp::DW_FORM_block
:
946 case elfcpp::DW_FORM_exprloc
:
947 attr_value
.aux
.blocklen
= read_unsigned_LEB_128(pattr
, &len
);
948 attr_value
.val
.blockval
= pattr
+ len
;
949 pattr
+= len
+ attr_value
.aux
.blocklen
;
951 case elfcpp::DW_FORM_data1
:
952 case elfcpp::DW_FORM_flag
:
953 attr_value
.val
.intval
= *pattr
++;
955 case elfcpp::DW_FORM_ref1
:
956 attr_value
.val
.refval
= *pattr
++;
959 case elfcpp::DW_FORM_data2
:
960 attr_value
.val
.intval
=
961 this->dwinfo_
->read_from_pointer
<16>(&pattr
);
963 case elfcpp::DW_FORM_ref2
:
964 attr_value
.val
.refval
=
965 this->dwinfo_
->read_from_pointer
<16>(&pattr
);
968 case elfcpp::DW_FORM_data4
:
971 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
973 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
974 attr_value
.aux
.shndx
= shndx
;
975 attr_value
.val
.intval
= sec_off
;
978 case elfcpp::DW_FORM_ref4
:
979 case elfcpp::DW_FORM_ref_sup4
:
982 sec_off
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
984 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
985 attr_value
.aux
.shndx
= shndx
;
986 attr_value
.val
.refval
= sec_off
;
990 case elfcpp::DW_FORM_data8
:
993 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
995 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
996 attr_value
.aux
.shndx
= shndx
;
997 attr_value
.val
.intval
= sec_off
;
1000 case elfcpp::DW_FORM_data16
:
1002 // For now, treat this as a 16-byte block.
1003 attr_value
.val
.blockval
= pattr
;
1004 attr_value
.aux
.blocklen
= 16;
1008 case elfcpp::DW_FORM_ref_sig8
:
1009 attr_value
.val
.uintval
=
1010 this->dwinfo_
->read_from_pointer
<64>(&pattr
);
1012 case elfcpp::DW_FORM_ref8
:
1013 case elfcpp::DW_FORM_ref_sup8
:
1016 sec_off
= this->dwinfo_
->read_from_pointer
<64>(&pattr
);
1017 unsigned int shndx
=
1018 this->dwinfo_
->lookup_reloc(attr_off
, &sec_off
);
1019 attr_value
.aux
.shndx
= shndx
;
1020 attr_value
.val
.refval
= sec_off
;
1024 case elfcpp::DW_FORM_ref_udata
:
1025 attr_value
.val
.refval
= read_unsigned_LEB_128(pattr
, &len
);
1029 case elfcpp::DW_FORM_udata
:
1030 attr_value
.val
.uintval
= read_unsigned_LEB_128(pattr
, &len
);
1033 case elfcpp::DW_FORM_addrx
:
1034 case elfcpp::DW_FORM_GNU_addr_index
:
1035 attr_value
.val
.uintval
= read_unsigned_LEB_128(pattr
, &len
);
1038 case elfcpp::DW_FORM_addrx1
:
1039 attr_value
.val
.uintval
= *pattr
++;
1041 case elfcpp::DW_FORM_addrx2
:
1042 attr_value
.val
.uintval
=
1043 this->dwinfo_
->read_from_pointer
<16>(&pattr
);
1045 case elfcpp::DW_FORM_addrx3
:
1046 attr_value
.val
.uintval
=
1047 this->dwinfo_
->read_3bytes_from_pointer(&pattr
);
1049 case elfcpp::DW_FORM_addrx4
:
1050 attr_value
.val
.uintval
=
1051 this->dwinfo_
->read_from_pointer
<32>(&pattr
);
1053 case elfcpp::DW_FORM_sdata
:
1054 attr_value
.val
.intval
= read_signed_LEB_128(pattr
, &len
);
1057 case elfcpp::DW_FORM_string
:
1058 attr_value
.val
.stringval
= reinterpret_cast<const char*>(pattr
);
1059 len
= strlen(attr_value
.val
.stringval
);
1062 case elfcpp::DW_FORM_loclistx
:
1063 case elfcpp::DW_FORM_rnglistx
:
1064 attr_value
.val
.uintval
= read_unsigned_LEB_128(pattr
, &len
);
1071 // Cache the most frequently-requested attributes.
1074 case elfcpp::DW_AT_name
:
1075 if (form
== elfcpp::DW_FORM_string
)
1076 this->name_
= attr_value
.val
.stringval
;
1077 else if (form
== elfcpp::DW_FORM_strp
)
1079 // All indirect strings should refer to the same
1080 // string section, so we just save the last one seen.
1081 this->string_shndx_
= attr_value
.aux
.shndx
;
1082 this->name_off_
= attr_value
.val
.refval
;
1085 case elfcpp::DW_AT_linkage_name
:
1086 case elfcpp::DW_AT_MIPS_linkage_name
:
1087 if (form
== elfcpp::DW_FORM_string
)
1088 this->linkage_name_
= attr_value
.val
.stringval
;
1089 else if (form
== elfcpp::DW_FORM_strp
)
1091 // All indirect strings should refer to the same
1092 // string section, so we just save the last one seen.
1093 this->string_shndx_
= attr_value
.aux
.shndx
;
1094 this->linkage_name_off_
= attr_value
.val
.refval
;
1097 case elfcpp::DW_AT_specification
:
1099 this->specification_
= attr_value
.val
.refval
;
1101 case elfcpp::DW_AT_abstract_origin
:
1103 this->abstract_origin_
= attr_value
.val
.refval
;
1105 case elfcpp::DW_AT_sibling
:
1106 if (ref_form
&& attr_value
.aux
.shndx
== 0)
1107 this->sibling_offset_
= attr_value
.val
.refval
;
1112 this->attributes_
.push_back(attr_value
);
1115 // Now that we know where the next DIE begins, record the offset
1116 // to avoid later recalculation.
1117 if (this->has_children())
1118 this->child_offset_
= this->die_offset_
+ (pattr
- pdie
);
1120 this->sibling_offset_
= this->die_offset_
+ (pattr
- pdie
);
1122 this->attributes_read_
= true;
1126 // Skip all the attributes of the DIE and return the offset of the next DIE.
1129 Dwarf_die::skip_attributes()
1131 gold_assert(this->abbrev_code_
!= NULL
);
1133 const unsigned char* pdie
=
1134 this->dwinfo_
->buffer_at_offset(this->die_offset_
);
1137 const unsigned char* pattr
= pdie
+ this->attr_offset_
;
1139 for (unsigned int i
= 0; i
< this->abbrev_code_
->attributes
.size(); ++i
)
1142 unsigned int form
= this->abbrev_code_
->attributes
[i
].form
;
1143 if (form
== elfcpp::DW_FORM_indirect
)
1145 form
= read_unsigned_LEB_128(pattr
, &len
);
1150 case elfcpp::DW_FORM_flag_present
:
1151 case elfcpp::DW_FORM_implicit_const
:
1153 case elfcpp::DW_FORM_strp
:
1154 case elfcpp::DW_FORM_sec_offset
:
1155 case elfcpp::DW_FORM_strp_sup
:
1156 case elfcpp::DW_FORM_line_strp
:
1157 pattr
+= this->dwinfo_
->offset_size();
1159 case elfcpp::DW_FORM_addr
:
1160 pattr
+= this->dwinfo_
->address_size();
1162 case elfcpp::DW_FORM_ref_addr
:
1163 pattr
+= this->dwinfo_
->ref_addr_size();
1165 case elfcpp::DW_FORM_block1
:
1166 pattr
+= 1 + *pattr
;
1168 case elfcpp::DW_FORM_block2
:
1170 uint16_t block_size
;
1171 block_size
= this->dwinfo_
->read_from_pointer
<16>(&pattr
);
1172 pattr
+= block_size
;
1175 case elfcpp::DW_FORM_block4
:
1177 uint32_t block_size
;
1178 block_size
= this->dwinfo_
->read_from_pointer
<32>(&pattr
);
1179 pattr
+= block_size
;
1182 case elfcpp::DW_FORM_block
:
1183 case elfcpp::DW_FORM_exprloc
:
1185 uint64_t block_size
;
1186 block_size
= read_unsigned_LEB_128(pattr
, &len
);
1187 pattr
+= len
+ block_size
;
1190 case elfcpp::DW_FORM_data1
:
1191 case elfcpp::DW_FORM_ref1
:
1192 case elfcpp::DW_FORM_flag
:
1193 case elfcpp::DW_FORM_strx1
:
1194 case elfcpp::DW_FORM_addrx1
:
1197 case elfcpp::DW_FORM_data2
:
1198 case elfcpp::DW_FORM_ref2
:
1199 case elfcpp::DW_FORM_strx2
:
1200 case elfcpp::DW_FORM_addrx2
:
1203 case elfcpp::DW_FORM_strx3
:
1204 case elfcpp::DW_FORM_addrx3
:
1207 case elfcpp::DW_FORM_data4
:
1208 case elfcpp::DW_FORM_ref4
:
1209 case elfcpp::DW_FORM_ref_sup4
:
1210 case elfcpp::DW_FORM_strx4
:
1211 case elfcpp::DW_FORM_addrx4
:
1214 case elfcpp::DW_FORM_data8
:
1215 case elfcpp::DW_FORM_ref8
:
1216 case elfcpp::DW_FORM_ref_sig8
:
1217 case elfcpp::DW_FORM_ref_sup8
:
1220 case elfcpp::DW_FORM_data16
:
1223 case elfcpp::DW_FORM_ref_udata
:
1224 case elfcpp::DW_FORM_udata
:
1225 case elfcpp::DW_FORM_addrx
:
1226 case elfcpp::DW_FORM_strx
:
1227 case elfcpp::DW_FORM_loclistx
:
1228 case elfcpp::DW_FORM_rnglistx
:
1229 case elfcpp::DW_FORM_GNU_addr_index
:
1230 case elfcpp::DW_FORM_GNU_str_index
:
1231 read_unsigned_LEB_128(pattr
, &len
);
1234 case elfcpp::DW_FORM_sdata
:
1235 read_signed_LEB_128(pattr
, &len
);
1238 case elfcpp::DW_FORM_string
:
1239 len
= strlen(reinterpret_cast<const char*>(pattr
));
1247 return this->die_offset_
+ (pattr
- pdie
);
1250 // Get the name of the DIE and cache it.
1253 Dwarf_die::set_name()
1255 if (this->name_
!= NULL
|| !this->read_attributes())
1257 if (this->name_off_
!= -1)
1258 this->name_
= this->dwinfo_
->get_string(this->name_off_
,
1259 this->string_shndx_
);
1262 // Get the linkage name of the DIE and cache it.
1265 Dwarf_die::set_linkage_name()
1267 if (this->linkage_name_
!= NULL
|| !this->read_attributes())
1269 if (this->linkage_name_off_
!= -1)
1270 this->linkage_name_
= this->dwinfo_
->get_string(this->linkage_name_off_
,
1271 this->string_shndx_
);
1274 // Return the value of attribute ATTR.
1276 const Dwarf_die::Attribute_value
*
1277 Dwarf_die::attribute(unsigned int attr
)
1279 if (!this->read_attributes())
1281 for (unsigned int i
= 0; i
< this->attributes_
.size(); ++i
)
1283 if (this->attributes_
[i
].attr
== attr
)
1284 return &this->attributes_
[i
];
1290 Dwarf_die::string_attribute(unsigned int attr
)
1292 const Attribute_value
* attr_val
= this->attribute(attr
);
1293 if (attr_val
== NULL
)
1295 switch (attr_val
->form
)
1297 case elfcpp::DW_FORM_string
:
1298 return attr_val
->val
.stringval
;
1299 case elfcpp::DW_FORM_strp
:
1300 return this->dwinfo_
->get_string(attr_val
->val
.refval
,
1301 attr_val
->aux
.shndx
);
1308 Dwarf_die::int_attribute(unsigned int attr
)
1310 const Attribute_value
* attr_val
= this->attribute(attr
);
1311 if (attr_val
== NULL
)
1313 switch (attr_val
->form
)
1315 case elfcpp::DW_FORM_flag_present
:
1316 case elfcpp::DW_FORM_data1
:
1317 case elfcpp::DW_FORM_flag
:
1318 case elfcpp::DW_FORM_data2
:
1319 case elfcpp::DW_FORM_data4
:
1320 case elfcpp::DW_FORM_data8
:
1321 case elfcpp::DW_FORM_sdata
:
1322 return attr_val
->val
.intval
;
1329 Dwarf_die::uint_attribute(unsigned int attr
)
1331 const Attribute_value
* attr_val
= this->attribute(attr
);
1332 if (attr_val
== NULL
)
1334 switch (attr_val
->form
)
1336 case elfcpp::DW_FORM_flag_present
:
1337 case elfcpp::DW_FORM_data1
:
1338 case elfcpp::DW_FORM_flag
:
1339 case elfcpp::DW_FORM_data4
:
1340 case elfcpp::DW_FORM_data8
:
1341 case elfcpp::DW_FORM_ref_sig8
:
1342 case elfcpp::DW_FORM_udata
:
1343 return attr_val
->val
.uintval
;
1350 Dwarf_die::ref_attribute(unsigned int attr
, unsigned int* shndx
)
1352 const Attribute_value
* attr_val
= this->attribute(attr
);
1353 if (attr_val
== NULL
)
1355 switch (attr_val
->form
)
1357 case elfcpp::DW_FORM_sec_offset
:
1358 case elfcpp::DW_FORM_addr
:
1359 case elfcpp::DW_FORM_ref_addr
:
1360 case elfcpp::DW_FORM_ref1
:
1361 case elfcpp::DW_FORM_ref2
:
1362 case elfcpp::DW_FORM_ref4
:
1363 case elfcpp::DW_FORM_ref8
:
1364 case elfcpp::DW_FORM_ref_udata
:
1365 *shndx
= attr_val
->aux
.shndx
;
1366 return attr_val
->val
.refval
;
1367 case elfcpp::DW_FORM_ref_sig8
:
1368 *shndx
= attr_val
->aux
.shndx
;
1369 return attr_val
->val
.uintval
;
1370 case elfcpp::DW_FORM_data4
:
1371 case elfcpp::DW_FORM_data8
:
1372 *shndx
= attr_val
->aux
.shndx
;
1373 return attr_val
->val
.intval
;
1380 Dwarf_die::address_attribute(unsigned int attr
, unsigned int* shndx
)
1382 const Attribute_value
* attr_val
= this->attribute(attr
);
1383 if (attr_val
== NULL
|| attr_val
->form
!= elfcpp::DW_FORM_addr
)
1386 *shndx
= attr_val
->aux
.shndx
;
1387 return attr_val
->val
.refval
;
1390 // Return the offset of this DIE's first child.
1393 Dwarf_die::child_offset()
1395 gold_assert(this->abbrev_code_
!= NULL
);
1396 if (!this->has_children())
1398 if (this->child_offset_
== 0)
1399 this->child_offset_
= this->skip_attributes();
1400 return this->child_offset_
;
1403 // Return the offset of this DIE's next sibling.
1406 Dwarf_die::sibling_offset()
1408 gold_assert(this->abbrev_code_
!= NULL
);
1410 if (this->sibling_offset_
!= 0)
1411 return this->sibling_offset_
;
1413 if (!this->has_children())
1415 this->sibling_offset_
= this->skip_attributes();
1416 return this->sibling_offset_
;
1419 if (this->has_sibling_attribute())
1421 if (!this->read_attributes())
1423 if (this->sibling_offset_
!= 0)
1424 return this->sibling_offset_
;
1427 // Skip over the children.
1428 off_t child_offset
= this->child_offset();
1429 while (child_offset
> 0)
1431 Dwarf_die
die(this->dwinfo_
, child_offset
, this);
1432 // The Dwarf_die ctor will set this DIE's sibling offset
1433 // when it reads a zero abbrev code.
1436 child_offset
= die
.sibling_offset();
1439 // This should be set by now. If not, there was a problem reading
1440 // the DWARF info, and we return 0.
1441 return this->sibling_offset_
;
1444 // class Dwarf_info_reader
1446 // Begin parsing the debug info. This calls visit_compilation_unit()
1447 // or visit_type_unit() for each compilation or type unit found in the
1448 // section, and visit_die() for each top-level DIE.
1451 Dwarf_info_reader::parse()
1453 if (this->object_
->is_big_endian())
1455 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1456 this->do_parse
<true>();
1463 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1464 this->do_parse
<false>();
1471 template<bool big_endian
>
1473 Dwarf_info_reader::do_parse()
1475 // Get the section contents and decompress if necessary.
1476 section_size_type buffer_size
;
1478 this->buffer_
= this->object_
->decompressed_section_contents(this->shndx_
,
1481 if (this->buffer_
== NULL
|| buffer_size
== 0)
1483 this->buffer_end_
= this->buffer_
+ buffer_size
;
1485 // The offset of this input section in the output section.
1486 off_t section_offset
= this->object_
->output_section_offset(this->shndx_
);
1488 // Start tracking relocations for this section.
1489 this->reloc_mapper_
= make_elf_reloc_mapper(this->object_
, this->symtab_
,
1490 this->symtab_size_
);
1491 this->reloc_mapper_
->initialize(this->reloc_shndx_
, this->reloc_type_
);
1493 // Loop over compilation units (or type units).
1494 unsigned int abbrev_shndx
= this->abbrev_shndx_
;
1495 off_t abbrev_offset
= 0;
1496 const unsigned char* pinfo
= this->buffer_
;
1497 while (pinfo
< this->buffer_end_
)
1499 // Read the compilation (or type) unit header.
1500 const unsigned char* cu_start
= pinfo
;
1501 this->cu_offset_
= cu_start
- this->buffer_
;
1502 this->cu_length_
= this->buffer_end_
- cu_start
;
1504 // Read unit_length (4 or 12 bytes).
1505 if (!this->check_buffer(pinfo
+ 4))
1507 uint32_t unit_length
=
1508 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pinfo
);
1510 if (unit_length
== 0xffffffff)
1512 if (!this->check_buffer(pinfo
+ 8))
1514 unit_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1516 this->offset_size_
= 8;
1519 this->offset_size_
= 4;
1520 if (!this->check_buffer(pinfo
+ unit_length
))
1522 const unsigned char* cu_end
= pinfo
+ unit_length
;
1523 this->cu_length_
= cu_end
- cu_start
;
1524 if (!this->check_buffer(pinfo
+ 2 + this->offset_size_
+ 1))
1527 // Read version (2 bytes).
1529 elfcpp::Swap_unaligned
<16, big_endian
>::readval(pinfo
);
1532 // DWARF 5: Read the unit type (1 byte) and address size (1 byte).
1533 if (this->cu_version_
>= 5)
1535 this->unit_type_
= *pinfo
++;
1536 this->address_size_
= *pinfo
++;
1539 // Read debug_abbrev_offset (4 or 8 bytes).
1540 if (this->offset_size_
== 4)
1541 abbrev_offset
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(pinfo
);
1543 abbrev_offset
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1544 if (this->reloc_shndx_
> 0)
1546 off_t reloc_offset
= pinfo
- this->buffer_
;
1549 this->reloc_mapper_
->get_reloc_target(reloc_offset
, &value
);
1550 if (abbrev_shndx
== 0)
1552 if (this->reloc_type_
== elfcpp::SHT_REL
)
1553 abbrev_offset
+= value
;
1555 abbrev_offset
= value
;
1557 pinfo
+= this->offset_size_
;
1559 // DWARF 2-4: Read address_size (1 byte).
1560 if (this->cu_version_
< 5)
1561 this->address_size_
= *pinfo
++;
1563 // For type units, read the two extra fields.
1564 uint64_t signature
= 0;
1565 off_t type_offset
= 0;
1566 if (this->is_type_unit())
1568 if (!this->check_buffer(pinfo
+ 8 + this->offset_size_
))
1571 // Read type_signature (8 bytes).
1572 signature
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1575 // Read type_offset (4 or 8 bytes).
1576 if (this->offset_size_
== 4)
1578 elfcpp::Swap_unaligned
<32, big_endian
>::readval(pinfo
);
1581 elfcpp::Swap_unaligned
<64, big_endian
>::readval(pinfo
);
1582 pinfo
+= this->offset_size_
;
1585 // Read the .debug_abbrev table.
1586 this->abbrev_table_
.read_abbrevs(this->object_
, abbrev_shndx
,
1589 // Visit the root DIE.
1590 Dwarf_die
root_die(this,
1591 pinfo
- (this->buffer_
+ this->cu_offset_
),
1593 if (root_die
.tag() != 0)
1595 // Visit the CU or TU.
1596 if (this->is_type_unit())
1597 this->visit_type_unit(section_offset
+ this->cu_offset_
,
1598 cu_end
- cu_start
, type_offset
, signature
,
1601 this->visit_compilation_unit(section_offset
+ this->cu_offset_
,
1602 cu_end
- cu_start
, &root_die
);
1605 // Advance to the next CU.
1611 delete[] this->buffer_
;
1612 this->buffer_
= NULL
;
1616 // Read the DWARF string table.
1619 Dwarf_info_reader::do_read_string_table(unsigned int string_shndx
)
1621 Relobj
* object
= this->object_
;
1623 // If we don't have relocations, string_shndx will be 0, and
1624 // we'll have to hunt for the .debug_str section.
1625 if (string_shndx
== 0)
1627 for (unsigned int i
= 1; i
< this->object_
->shnum(); ++i
)
1629 std::string name
= object
->section_name(i
);
1630 if (name
== ".debug_str" || name
== ".zdebug_str")
1633 this->string_output_section_offset_
=
1634 object
->output_section_offset(i
);
1638 if (string_shndx
== 0)
1642 if (this->owns_string_buffer_
&& this->string_buffer_
!= NULL
)
1644 delete[] this->string_buffer_
;
1645 this->owns_string_buffer_
= false;
1648 // Get the secton contents and decompress if necessary.
1649 section_size_type buffer_size
;
1650 const unsigned char* buffer
=
1651 object
->decompressed_section_contents(string_shndx
,
1653 &this->owns_string_buffer_
);
1654 this->string_buffer_
= reinterpret_cast<const char*>(buffer
);
1655 this->string_buffer_end_
= this->string_buffer_
+ buffer_size
;
1656 this->string_shndx_
= string_shndx
;
1660 // Read a possibly unaligned integer of SIZE.
1661 template <int valsize
>
1662 inline typename
elfcpp::Valtype_base
<valsize
>::Valtype
1663 Dwarf_info_reader::read_from_pointer(const unsigned char* source
)
1665 typename
elfcpp::Valtype_base
<valsize
>::Valtype return_value
;
1666 if (this->object_
->is_big_endian())
1667 return_value
= elfcpp::Swap_unaligned
<valsize
, true>::readval(source
);
1669 return_value
= elfcpp::Swap_unaligned
<valsize
, false>::readval(source
);
1670 return return_value
;
1673 // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
1674 template <int valsize
>
1675 inline typename
elfcpp::Valtype_base
<valsize
>::Valtype
1676 Dwarf_info_reader::read_from_pointer(const unsigned char** source
)
1678 typename
elfcpp::Valtype_base
<valsize
>::Valtype return_value
;
1679 if (this->object_
->is_big_endian())
1680 return_value
= elfcpp::Swap_unaligned
<valsize
, true>::readval(*source
);
1682 return_value
= elfcpp::Swap_unaligned
<valsize
, false>::readval(*source
);
1683 *source
+= valsize
/ 8;
1684 return return_value
;
1687 // Read a 3-byte integer. Update SOURCE after read.
1688 inline typename
elfcpp::Valtype_base
<32>::Valtype
1689 Dwarf_info_reader::read_3bytes_from_pointer(const unsigned char** source
)
1691 typename
elfcpp::Valtype_base
<32>::Valtype return_value
;
1692 if (this->object_
->is_big_endian())
1693 return_value
= ((*source
)[0] << 16) | ((*source
)[1] << 8) | (*source
)[2];
1695 return_value
= ((*source
)[2] << 16) | ((*source
)[1] << 8) | (*source
)[0];
1697 return return_value
;
1700 // Look for a relocation at offset ATTR_OFF in the dwarf info,
1701 // and return the section index and offset of the target.
1704 Dwarf_info_reader::lookup_reloc(off_t attr_off
, off_t
* target_off
)
1707 attr_off
+= this->cu_offset_
;
1708 unsigned int shndx
= this->reloc_mapper_
->get_reloc_target(attr_off
, &value
);
1711 if (this->reloc_type_
== elfcpp::SHT_REL
)
1712 *target_off
+= value
;
1714 *target_off
= value
;
1718 // Return a string from the DWARF string table.
1721 Dwarf_info_reader::get_string(off_t str_off
, unsigned int string_shndx
)
1723 if (!this->read_string_table(string_shndx
))
1726 // Correct the offset. For incremental update links, we have a
1727 // relocated offset that is relative to the output section, but
1728 // here we need an offset relative to the input section.
1729 str_off
-= this->string_output_section_offset_
;
1731 const char* p
= this->string_buffer_
+ str_off
;
1733 if (p
< this->string_buffer_
|| p
>= this->string_buffer_end_
)
1739 // The following are default, do-nothing, implementations of the
1740 // hook methods normally provided by a derived class. We provide
1741 // default implementations rather than no implementation so that
1742 // a derived class needs to implement only the hooks that it needs
1745 // Process a compilation unit and parse its child DIE.
1748 Dwarf_info_reader::visit_compilation_unit(off_t
, off_t
, Dwarf_die
*)
1752 // Process a type unit and parse its child DIE.
1755 Dwarf_info_reader::visit_type_unit(off_t
, off_t
, off_t
, uint64_t, Dwarf_die
*)
1759 // Print a warning about a corrupt debug section.
1762 Dwarf_info_reader::warn_corrupt_debug_section() const
1764 gold_warning(_("%s: corrupt debug info in %s"),
1765 this->object_
->name().c_str(),
1766 this->object_
->section_name(this->shndx_
).c_str());
1769 // class Sized_dwarf_line_info
1771 struct LineStateMachine
1777 unsigned int shndx
; // the section address refers to
1778 bool is_stmt
; // stmt means statement.
1784 ResetLineStateMachine(struct LineStateMachine
* lsm
, bool default_is_stmt
)
1789 lsm
->column_num
= 0;
1791 lsm
->is_stmt
= default_is_stmt
;
1792 lsm
->basic_block
= false;
1793 lsm
->end_sequence
= false;
1796 template<int size
, bool big_endian
>
1797 Sized_dwarf_line_info
<size
, big_endian
>::Sized_dwarf_line_info(
1799 unsigned int read_shndx
)
1800 : data_valid_(false), buffer_(NULL
), buffer_start_(NULL
),
1801 str_buffer_(NULL
), str_buffer_start_(NULL
),
1802 reloc_mapper_(NULL
), symtab_buffer_(NULL
), directories_(), files_(),
1803 current_header_index_(-1), reloc_map_(), line_number_map_()
1805 unsigned int debug_line_shndx
= 0;
1806 unsigned int debug_line_str_shndx
= 0;
1808 for (unsigned int i
= 1; i
< object
->shnum(); ++i
)
1810 section_size_type buffer_size
;
1811 bool is_new
= false;
1813 // FIXME: do this more efficiently: section_name() isn't super-fast
1814 std::string name
= object
->section_name(i
);
1815 if (name
== ".debug_line" || name
== ".zdebug_line")
1818 object
->decompressed_section_contents(i
, &buffer_size
, &is_new
);
1820 this->buffer_start_
= this->buffer_
;
1821 this->buffer_end_
= this->buffer_
+ buffer_size
;
1822 debug_line_shndx
= i
;
1824 else if (name
== ".debug_line_str" || name
== ".zdebug_line_str")
1827 object
->decompressed_section_contents(i
, &buffer_size
, &is_new
);
1829 this->str_buffer_start_
= this->str_buffer_
;
1830 this->str_buffer_end_
= this->str_buffer_
+ buffer_size
;
1831 debug_line_str_shndx
= i
;
1833 if (debug_line_shndx
> 0 && debug_line_str_shndx
> 0)
1836 if (this->buffer_
== NULL
)
1839 // Find the relocation section for ".debug_line".
1840 // We expect these for relobjs (.o's) but not dynobjs (.so's).
1841 unsigned int reloc_shndx
= 0;
1842 for (unsigned int i
= 0; i
< object
->shnum(); ++i
)
1844 unsigned int reloc_sh_type
= object
->section_type(i
);
1845 if ((reloc_sh_type
== elfcpp::SHT_REL
1846 || reloc_sh_type
== elfcpp::SHT_RELA
)
1847 && object
->section_info(i
) == debug_line_shndx
)
1850 this->track_relocs_type_
= reloc_sh_type
;
1855 // Finally, we need the symtab section to interpret the relocs.
1856 if (reloc_shndx
!= 0)
1858 unsigned int symtab_shndx
;
1859 for (symtab_shndx
= 0; symtab_shndx
< object
->shnum(); ++symtab_shndx
)
1860 if (object
->section_type(symtab_shndx
) == elfcpp::SHT_SYMTAB
)
1862 this->symtab_buffer_
= object
->section_contents(
1863 symtab_shndx
, &this->symtab_buffer_size_
, false);
1866 if (this->symtab_buffer_
== NULL
)
1870 this->reloc_mapper_
=
1871 new Sized_elf_reloc_mapper
<size
, big_endian
>(object
,
1872 this->symtab_buffer_
,
1873 this->symtab_buffer_size_
);
1874 if (!this->reloc_mapper_
->initialize(reloc_shndx
, this->track_relocs_type_
))
1877 // Now that we have successfully read all the data, parse the debug
1879 this->data_valid_
= true;
1880 this->read_line_mappings(read_shndx
);
1883 // Read the DWARF header.
1885 template<int size
, bool big_endian
>
1886 const unsigned char*
1887 Sized_dwarf_line_info
<size
, big_endian
>::read_header_prolog(
1888 const unsigned char* lineptr
)
1890 uint32_t initial_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
1893 // In DWARF, if the initial length is all 1 bits, then the offset
1894 // size is 8 and we need to read the next 8 bytes for the real length.
1895 if (initial_length
== 0xffffffff)
1897 this->header_
.offset_size
= 8;
1898 initial_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
1902 this->header_
.offset_size
= 4;
1904 this->header_
.total_length
= initial_length
;
1906 this->end_of_unit_
= lineptr
+ initial_length
;
1907 gold_assert(this->end_of_unit_
<= buffer_end_
);
1909 this->header_
.version
=
1910 elfcpp::Swap_unaligned
<16, big_endian
>::readval(lineptr
);
1913 // We can only read versions 2-5 of the DWARF line number table.
1914 // For other versions, just skip the entire line number table.
1915 if (this->header_
.version
< 2 || this->header_
.version
> 5)
1916 return this->end_of_unit_
;
1918 // DWARF 5 only: address size and segment selector.
1919 if (this->header_
.version
>= 5)
1921 this->header_
.address_size
= *lineptr
;
1922 // We ignore the segment selector.
1926 if (this->header_
.offset_size
== 4)
1927 this->header_
.prologue_length
=
1928 elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
1930 this->header_
.prologue_length
=
1931 elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
1932 lineptr
+= this->header_
.offset_size
;
1934 this->end_of_header_length_
= lineptr
;
1936 this->header_
.min_insn_length
= *lineptr
;
1939 if (this->header_
.version
< 4)
1940 this->header_
.max_ops_per_insn
= 1;
1943 // DWARF 4 added the maximum_operations_per_instruction field.
1944 this->header_
.max_ops_per_insn
= *lineptr
;
1946 // TODO: Add support for values other than 1.
1947 gold_assert(this->header_
.max_ops_per_insn
== 1);
1950 this->header_
.default_is_stmt
= *lineptr
;
1953 this->header_
.line_base
= *reinterpret_cast<const signed char*>(lineptr
);
1956 this->header_
.line_range
= *lineptr
;
1959 this->header_
.opcode_base
= *lineptr
;
1962 this->header_
.std_opcode_lengths
.resize(this->header_
.opcode_base
+ 1);
1963 this->header_
.std_opcode_lengths
[0] = 0;
1964 for (int i
= 1; i
< this->header_
.opcode_base
; i
++)
1966 this->header_
.std_opcode_lengths
[i
] = *lineptr
;
1973 // The header for a debug_line section is mildly complicated, because
1974 // the line info is very tightly encoded.
1975 // This routine is for DWARF versions 2, 3, and 4.
1977 template<int size
, bool big_endian
>
1978 const unsigned char*
1979 Sized_dwarf_line_info
<size
, big_endian
>::read_header_tables_v2(
1980 const unsigned char* lineptr
)
1982 ++this->current_header_index_
;
1984 // Create a new directories_ entry and a new files_ entry for our new
1985 // header. We initialize each with a single empty element, because
1986 // dwarf indexes directory and filenames starting at 1.
1987 gold_assert(static_cast<int>(this->directories_
.size())
1988 == this->current_header_index_
);
1989 gold_assert(static_cast<int>(this->files_
.size())
1990 == this->current_header_index_
);
1991 this->directories_
.push_back(std::vector
<std::string
>(1));
1992 this->files_
.push_back(std::vector
<std::pair
<int, std::string
> >(1));
1994 // It is legal for the directory entry table to be empty.
2000 const char* dirname
= reinterpret_cast<const char*>(lineptr
);
2001 gold_assert(dirindex
2002 == static_cast<int>(this->directories_
.back().size()));
2003 this->directories_
.back().push_back(dirname
);
2004 lineptr
+= this->directories_
.back().back().size() + 1;
2010 // It is also legal for the file entry table to be empty.
2017 const char* filename
= reinterpret_cast<const char*>(lineptr
);
2018 lineptr
+= strlen(filename
) + 1;
2020 uint64_t dirindex
= read_unsigned_LEB_128(lineptr
, &len
);
2023 if (dirindex
>= this->directories_
.back().size())
2025 int dirindexi
= static_cast<int>(dirindex
);
2027 read_unsigned_LEB_128(lineptr
, &len
); // mod_time
2030 read_unsigned_LEB_128(lineptr
, &len
); // filelength
2033 gold_assert(fileindex
2034 == static_cast<int>(this->files_
.back().size()));
2035 this->files_
.back().push_back(std::make_pair(dirindexi
, filename
));
2044 // This routine is for DWARF version 5.
2046 template<int size
, bool big_endian
>
2047 const unsigned char*
2048 Sized_dwarf_line_info
<size
, big_endian
>::read_header_tables_v5(
2049 const unsigned char* lineptr
)
2053 ++this->current_header_index_
;
2055 gold_assert(static_cast<int>(this->directories_
.size())
2056 == this->current_header_index_
);
2057 gold_assert(static_cast<int>(this->files_
.size())
2058 == this->current_header_index_
);
2060 // Read the directory list.
2061 unsigned int format_count
= *lineptr
;
2064 unsigned int *types
= new unsigned int[format_count
];
2065 unsigned int *forms
= new unsigned int[format_count
];
2067 for (unsigned int i
= 0; i
< format_count
; i
++)
2069 types
[i
] = read_unsigned_LEB_128(lineptr
, &len
);
2071 forms
[i
] = read_unsigned_LEB_128(lineptr
, &len
);
2075 uint64_t entry_count
= read_unsigned_LEB_128(lineptr
, &len
);
2077 this->directories_
.push_back(std::vector
<std::string
>(0));
2078 std::vector
<std::string
>& dir_list
= this->directories_
.back();
2080 for (unsigned int j
= 0; j
< entry_count
; j
++)
2082 std::string dirname
;
2084 for (unsigned int i
= 0; i
< format_count
; i
++)
2086 if (types
[i
] == elfcpp::DW_LNCT_path
)
2088 if (forms
[i
] == elfcpp::DW_FORM_string
)
2090 dirname
= reinterpret_cast<const char*>(lineptr
);
2091 lineptr
+= dirname
.size() + 1;
2093 else if (forms
[i
] == elfcpp::DW_FORM_line_strp
)
2096 if (this->header_
.offset_size
== 4)
2098 elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
2101 elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
2102 typename
Reloc_map::const_iterator it
2103 = this->reloc_map_
.find(lineptr
- this->buffer_
);
2104 if (it
!= reloc_map_
.end())
2106 if (this->track_relocs_type_
== elfcpp::SHT_RELA
)
2108 offset
+= it
->second
.second
;
2110 lineptr
+= this->header_
.offset_size
;
2111 dirname
= reinterpret_cast<const char*>(this->str_buffer_
2120 dir_list
.push_back(dirname
);
2126 // Read the filenames list.
2127 format_count
= *lineptr
;
2130 types
= new unsigned int[format_count
];
2131 forms
= new unsigned int[format_count
];
2133 for (unsigned int i
= 0; i
< format_count
; i
++)
2135 types
[i
] = read_unsigned_LEB_128(lineptr
, &len
);
2137 forms
[i
] = read_unsigned_LEB_128(lineptr
, &len
);
2141 entry_count
= read_unsigned_LEB_128(lineptr
, &len
);
2143 this->files_
.push_back(
2144 std::vector
<std::pair
<int, std::string
> >(0));
2145 std::vector
<std::pair
<int, std::string
> >& file_list
= this->files_
.back();
2147 for (unsigned int j
= 0; j
< entry_count
; j
++)
2149 const char* path
= NULL
;
2152 for (unsigned int i
= 0; i
< format_count
; i
++)
2154 if (types
[i
] == elfcpp::DW_LNCT_path
)
2156 if (forms
[i
] == elfcpp::DW_FORM_string
)
2158 path
= reinterpret_cast<const char*>(lineptr
);
2159 lineptr
+= strlen(path
) + 1;
2161 else if (forms
[i
] == elfcpp::DW_FORM_line_strp
)
2164 if (this->header_
.offset_size
== 4)
2165 offset
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
2167 offset
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
2168 typename
Reloc_map::const_iterator it
2169 = this->reloc_map_
.find(lineptr
- this->buffer_
);
2170 if (it
!= reloc_map_
.end())
2172 if (this->track_relocs_type_
== elfcpp::SHT_RELA
)
2174 offset
+= it
->second
.second
;
2176 lineptr
+= this->header_
.offset_size
;
2177 path
= reinterpret_cast<const char*>(this->str_buffer_
2183 else if (types
[i
] == elfcpp::DW_LNCT_directory_index
)
2185 if (forms
[i
] == elfcpp::DW_FORM_udata
)
2187 dirindex
= read_unsigned_LEB_128(lineptr
, &len
);
2196 gold_debug(DEBUG_LOCATION
, "File %3d: %s",
2197 static_cast<int>(file_list
.size()), path
);
2198 file_list
.push_back(std::make_pair(dirindex
, path
));
2207 // Process a single opcode in the .debug.line structure.
2209 template<int size
, bool big_endian
>
2211 Sized_dwarf_line_info
<size
, big_endian
>::process_one_opcode(
2212 const unsigned char* start
, struct LineStateMachine
* lsm
, size_t* len
)
2216 unsigned char opcode
= *start
;
2220 // If the opcode is great than the opcode_base, it is a special
2221 // opcode. Most line programs consist mainly of special opcodes.
2222 if (opcode
>= this->header_
.opcode_base
)
2224 opcode
-= this->header_
.opcode_base
;
2225 const int advance_address
= ((opcode
/ this->header_
.line_range
)
2226 * this->header_
.min_insn_length
);
2227 lsm
->address
+= advance_address
;
2229 const int advance_line
= ((opcode
% this->header_
.line_range
)
2230 + this->header_
.line_base
);
2231 lsm
->line_num
+= advance_line
;
2232 lsm
->basic_block
= true;
2237 // Otherwise, we have the regular opcodes
2240 case elfcpp::DW_LNS_copy
:
2241 lsm
->basic_block
= false;
2245 case elfcpp::DW_LNS_advance_pc
:
2247 const uint64_t advance_address
2248 = read_unsigned_LEB_128(start
, &templen
);
2250 lsm
->address
+= this->header_
.min_insn_length
* advance_address
;
2254 case elfcpp::DW_LNS_advance_line
:
2256 const int64_t advance_line
= read_signed_LEB_128(start
, &templen
);
2258 lsm
->line_num
+= advance_line
;
2262 case elfcpp::DW_LNS_set_file
:
2264 const uint64_t fileno
= read_unsigned_LEB_128(start
, &templen
);
2266 lsm
->file_num
= fileno
;
2270 case elfcpp::DW_LNS_set_column
:
2272 const uint64_t colno
= read_unsigned_LEB_128(start
, &templen
);
2274 lsm
->column_num
= colno
;
2278 case elfcpp::DW_LNS_negate_stmt
:
2279 lsm
->is_stmt
= !lsm
->is_stmt
;
2282 case elfcpp::DW_LNS_set_basic_block
:
2283 lsm
->basic_block
= true;
2286 case elfcpp::DW_LNS_fixed_advance_pc
:
2288 int advance_address
;
2289 advance_address
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(start
);
2291 lsm
->address
+= advance_address
;
2295 case elfcpp::DW_LNS_const_add_pc
:
2297 const int advance_address
= (this->header_
.min_insn_length
2298 * ((255 - this->header_
.opcode_base
)
2299 / this->header_
.line_range
));
2300 lsm
->address
+= advance_address
;
2304 case elfcpp::DW_LNS_extended_op
:
2306 const uint64_t extended_op_len
2307 = read_unsigned_LEB_128(start
, &templen
);
2309 oplen
+= templen
+ extended_op_len
;
2311 const unsigned char extended_op
= *start
;
2314 switch (extended_op
)
2316 case elfcpp::DW_LNE_end_sequence
:
2317 // This means that the current byte is the one immediately
2318 // after a set of instructions. Record the current line
2319 // for up to one less than the current address.
2321 lsm
->end_sequence
= true;
2325 case elfcpp::DW_LNE_set_address
:
2328 elfcpp::Swap_unaligned
<size
, big_endian
>::readval(start
);
2329 typename
Reloc_map::const_iterator it
2330 = this->reloc_map_
.find(start
- this->buffer_
);
2331 if (it
!= reloc_map_
.end())
2333 // If this is a SHT_RELA section, then ignore the
2334 // section contents. This assumes that this is a
2335 // straight reloc which just uses the reloc addend.
2336 // The reloc addend has already been included in the
2338 if (this->track_relocs_type_
== elfcpp::SHT_RELA
)
2340 // Add in the symbol value.
2341 lsm
->address
+= it
->second
.second
;
2342 lsm
->shndx
= it
->second
.first
;
2346 // If we're a normal .o file, with relocs, every
2347 // set_address should have an associated relocation.
2348 if (this->input_is_relobj())
2349 this->data_valid_
= false;
2353 case elfcpp::DW_LNE_define_file
:
2355 const char* filename
= reinterpret_cast<const char*>(start
);
2356 templen
= strlen(filename
) + 1;
2359 uint64_t dirindex
= read_unsigned_LEB_128(start
, &templen
);
2361 if (dirindex
>= this->directories_
.back().size())
2363 int dirindexi
= static_cast<int>(dirindex
);
2365 // This opcode takes two additional ULEB128 parameters
2366 // (mod_time and filelength), but we don't use those
2367 // values. Because OPLEN already tells us how far to
2368 // skip to the next opcode, we don't need to read
2371 this->files_
.back().push_back(std::make_pair(dirindexi
,
2381 // Ignore unknown opcode silently
2382 for (int i
= 0; i
< this->header_
.std_opcode_lengths
[opcode
]; i
++)
2385 read_unsigned_LEB_128(start
, &templen
);
2396 // Read the debug information at LINEPTR and store it in the line
2399 template<int size
, bool big_endian
>
2400 unsigned const char*
2401 Sized_dwarf_line_info
<size
, big_endian
>::read_lines(unsigned const char* lineptr
,
2402 unsigned const char* endptr
,
2405 struct LineStateMachine lsm
;
2407 while (lineptr
< endptr
)
2409 ResetLineStateMachine(&lsm
, this->header_
.default_is_stmt
);
2410 while (!lsm
.end_sequence
)
2414 if (lineptr
>= endptr
)
2417 bool add_line
= this->process_one_opcode(lineptr
, &lsm
, &oplength
);
2418 lineptr
+= oplength
;
2421 && (shndx
== -1U || lsm
.shndx
== -1U || shndx
== lsm
.shndx
))
2423 Offset_to_lineno_entry entry
2424 = { static_cast<off_t
>(lsm
.address
),
2425 this->current_header_index_
,
2426 static_cast<unsigned int>(lsm
.file_num
),
2427 true, lsm
.line_num
};
2428 std::vector
<Offset_to_lineno_entry
>&
2429 map(this->line_number_map_
[lsm
.shndx
]);
2430 // If we see two consecutive entries with the same
2431 // offset and a real line number, then mark the first
2432 // one as non-canonical.
2434 && (map
.back().offset
== static_cast<off_t
>(lsm
.address
))
2435 && lsm
.line_num
!= -1
2436 && map
.back().line_num
!= -1)
2437 map
.back().last_line_for_offset
= false;
2438 map
.push_back(entry
);
2446 // Read the relocations into a Reloc_map.
2448 template<int size
, bool big_endian
>
2450 Sized_dwarf_line_info
<size
, big_endian
>::read_relocs()
2452 if (this->symtab_buffer_
== NULL
)
2457 while ((reloc_offset
= this->reloc_mapper_
->next_offset()) != -1)
2459 const unsigned int shndx
=
2460 this->reloc_mapper_
->get_reloc_target(reloc_offset
, &value
);
2462 // There is no reason to record non-ordinary section indexes, or
2463 // SHN_UNDEF, because they will never match the real section.
2465 this->reloc_map_
[reloc_offset
] = std::make_pair(shndx
, value
);
2467 this->reloc_mapper_
->advance(reloc_offset
+ 1);
2471 // Read the line number info.
2473 template<int size
, bool big_endian
>
2475 Sized_dwarf_line_info
<size
, big_endian
>::read_line_mappings(unsigned int shndx
)
2477 gold_assert(this->data_valid_
== true);
2479 this->read_relocs();
2480 while (this->buffer_
< this->buffer_end_
)
2482 const unsigned char* lineptr
= this->buffer_
;
2483 lineptr
= this->read_header_prolog(lineptr
);
2484 if (this->header_
.version
>= 2 && this->header_
.version
<= 4)
2486 lineptr
= this->read_header_tables_v2(lineptr
);
2487 lineptr
= this->read_lines(lineptr
, this->end_of_unit_
, shndx
);
2489 else if (this->header_
.version
== 5)
2491 lineptr
= this->read_header_tables_v5(lineptr
);
2492 lineptr
= this->read_lines(lineptr
, this->end_of_unit_
, shndx
);
2494 this->buffer_
= this->end_of_unit_
;
2497 // Sort the lines numbers, so addr2line can use binary search.
2498 for (typename
Lineno_map::iterator it
= line_number_map_
.begin();
2499 it
!= line_number_map_
.end();
2501 // Each vector needs to be sorted by offset.
2502 std::sort(it
->second
.begin(), it
->second
.end());
2505 // Some processing depends on whether the input is a .o file or not.
2506 // For instance, .o files have relocs, and have .debug_lines
2507 // information on a per section basis. .so files, on the other hand,
2508 // lack relocs, and offsets are unique, so we can ignore the section
2511 template<int size
, bool big_endian
>
2513 Sized_dwarf_line_info
<size
, big_endian
>::input_is_relobj()
2515 // Only .o files have relocs and the symtab buffer that goes with them.
2516 return this->symtab_buffer_
!= NULL
;
2519 // Given an Offset_to_lineno_entry vector, and an offset, figure out
2520 // if the offset points into a function according to the vector (see
2521 // comments below for the algorithm). If it does, return an iterator
2522 // into the vector that points to the line-number that contains that
2523 // offset. If not, it returns vector::end().
2525 static std::vector
<Offset_to_lineno_entry
>::const_iterator
2526 offset_to_iterator(const std::vector
<Offset_to_lineno_entry
>* offsets
,
2529 const Offset_to_lineno_entry lookup_key
= { offset
, 0, 0, true, 0 };
2531 // lower_bound() returns the smallest offset which is >= lookup_key.
2532 // If no offset in offsets is >= lookup_key, returns end().
2533 std::vector
<Offset_to_lineno_entry
>::const_iterator it
2534 = std::lower_bound(offsets
->begin(), offsets
->end(), lookup_key
);
2536 // This code is easiest to understand with a concrete example.
2537 // Here's a possible offsets array:
2538 // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0
2539 // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1
2540 // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2
2541 // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3
2542 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4
2543 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5
2544 // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6
2545 // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7
2546 // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8
2547 // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9
2548 // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10
2549 // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11
2550 // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12
2551 // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13
2552 // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14
2553 // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15
2554 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16
2555 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17
2556 // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18
2557 // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19
2558 // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20
2559 // The entries with line_num == -1 mark the end of a function: the
2560 // associated offset is one past the last instruction in the
2561 // function. This can correspond to the beginning of the next
2562 // function (as is true for offset 3232); alternately, there can be
2563 // a gap between the end of one function and the start of the next
2564 // (as is true for some others, most obviously from 3236->5764).
2566 // Case 1: lookup_key has offset == 10. lower_bound returns
2567 // offsets[0]. Since it's not an exact match and we're
2568 // at the beginning of offsets, we return end() (invalid).
2569 // Case 2: lookup_key has offset 10000. lower_bound returns
2570 // offset[21] (end()). We return end() (invalid).
2571 // Case 3: lookup_key has offset == 3211. lower_bound matches
2572 // offsets[0] exactly, and that's the entry we return.
2573 // Case 4: lookup_key has offset == 3232. lower_bound returns
2574 // offsets[4]. That's an exact match, but indicates
2575 // end-of-function. We check if offsets[5] is also an
2576 // exact match but not end-of-function. It is, so we
2577 // return offsets[5].
2578 // Case 5: lookup_key has offset == 3214. lower_bound returns
2579 // offsets[1]. Since it's not an exact match, we back
2580 // up to the offset that's < lookup_key, offsets[0].
2581 // We note offsets[0] is a valid entry (not end-of-function),
2582 // so that's the entry we return.
2583 // Case 6: lookup_key has offset == 4000. lower_bound returns
2584 // offsets[8]. Since it's not an exact match, we back
2585 // up to offsets[7]. Since offsets[7] indicates
2586 // end-of-function, we know lookup_key is between
2587 // functions, so we return end() (not a valid offset).
2588 // Case 7: lookup_key has offset == 5794. lower_bound returns
2589 // offsets[19]. Since it's not an exact match, we back
2590 // up to offsets[16]. Note we back up to the *first*
2591 // entry with offset 5793, not just offsets[19-1].
2592 // We note offsets[16] is a valid entry, so we return it.
2593 // If offsets[16] had had line_num == -1, we would have
2594 // checked offsets[17]. The reason for this is that
2595 // 16 and 17 can be in an arbitrary order, since we sort
2596 // only by offset and last_line_for_offset. (Note it
2597 // doesn't help to use line_number as a tertiary sort key,
2598 // since sometimes we want the -1 to be first and sometimes
2599 // we want it to be last.)
2601 // This deals with cases (1) and (2).
2602 if ((it
== offsets
->begin() && offset
< it
->offset
)
2603 || it
== offsets
->end())
2604 return offsets
->end();
2606 // This deals with cases (3) and (4).
2607 if (offset
== it
->offset
)
2609 while (it
!= offsets
->end()
2610 && it
->offset
== offset
2611 && it
->line_num
== -1)
2613 if (it
== offsets
->end() || it
->offset
!= offset
)
2614 return offsets
->end();
2619 // This handles the first part of case (7) -- we back up to the
2620 // *first* entry that has the offset that's behind us.
2621 gold_assert(it
!= offsets
->begin());
2622 std::vector
<Offset_to_lineno_entry
>::const_iterator range_end
= it
;
2624 const off_t range_value
= it
->offset
;
2625 while (it
!= offsets
->begin() && (it
-1)->offset
== range_value
)
2628 // This handles cases (5), (6), and (7): if any entry in the
2629 // equal_range [it, range_end) has a line_num != -1, it's a valid
2630 // match. If not, we're not in a function. The line number we saw
2631 // last for an offset will be sorted first, so it'll get returned if
2633 for (; it
!= range_end
; ++it
)
2634 if (it
->line_num
!= -1)
2636 return offsets
->end();
2639 // Returns the canonical filename:lineno for the address passed in.
2640 // If other_lines is not NULL, appends the non-canonical lines
2641 // assigned to the same address.
2643 template<int size
, bool big_endian
>
2645 Sized_dwarf_line_info
<size
, big_endian
>::do_addr2line(
2648 std::vector
<std::string
>* other_lines
)
2650 gold_debug(DEBUG_LOCATION
, "do_addr2line: shndx %u offset %08x",
2651 shndx
, static_cast<int>(offset
));
2653 if (this->data_valid_
== false)
2656 const std::vector
<Offset_to_lineno_entry
>* offsets
;
2657 // If we do not have reloc information, then our input is a .so or
2658 // some similar data structure where all the information is held in
2659 // the offset. In that case, we ignore the input shndx.
2660 if (this->input_is_relobj())
2661 offsets
= &this->line_number_map_
[shndx
];
2663 offsets
= &this->line_number_map_
[-1U];
2664 if (offsets
->empty())
2667 typename
std::vector
<Offset_to_lineno_entry
>::const_iterator it
2668 = offset_to_iterator(offsets
, offset
);
2669 if (it
== offsets
->end())
2672 std::string result
= this->format_file_lineno(*it
);
2673 gold_debug(DEBUG_LOCATION
, "do_addr2line: canonical result: %s",
2675 if (other_lines
!= NULL
)
2677 unsigned int last_file_num
= it
->file_num
;
2678 int last_line_num
= it
->line_num
;
2679 // Return up to 4 more locations from the beginning of the function
2680 // for fuzzy matching.
2681 for (++it
; it
!= offsets
->end(); ++it
)
2683 if (it
->offset
== offset
&& it
->line_num
== -1)
2684 continue; // The end of a previous function.
2685 if (it
->line_num
== -1)
2686 break; // The end of the current function.
2687 if (it
->file_num
!= last_file_num
|| it
->line_num
!= last_line_num
)
2689 other_lines
->push_back(this->format_file_lineno(*it
));
2690 gold_debug(DEBUG_LOCATION
, "do_addr2line: other: %s",
2691 other_lines
->back().c_str());
2692 last_file_num
= it
->file_num
;
2693 last_line_num
= it
->line_num
;
2695 if (it
->offset
> offset
&& other_lines
->size() >= 4)
2703 // Convert the file_num + line_num into a string.
2705 template<int size
, bool big_endian
>
2707 Sized_dwarf_line_info
<size
, big_endian
>::format_file_lineno(
2708 const Offset_to_lineno_entry
& loc
) const
2712 gold_assert(loc
.header_num
< static_cast<int>(this->files_
.size()));
2713 gold_assert(loc
.file_num
2714 < static_cast<unsigned int>(this->files_
[loc
.header_num
].size()));
2715 const std::pair
<int, std::string
>& filename_pair
2716 = this->files_
[loc
.header_num
][loc
.file_num
];
2717 const std::string
& filename
= filename_pair
.second
;
2719 gold_assert(loc
.header_num
< static_cast<int>(this->directories_
.size()));
2720 gold_assert(filename_pair
.first
2721 < static_cast<int>(this->directories_
[loc
.header_num
].size()));
2722 const std::string
& dirname
2723 = this->directories_
[loc
.header_num
][filename_pair
.first
];
2725 if (!dirname
.empty())
2734 char buffer
[64]; // enough to hold a line number
2735 snprintf(buffer
, sizeof(buffer
), "%d", loc
.line_num
);
2742 // Dwarf_line_info routines.
2744 static unsigned int next_generation_count
= 0;
2746 struct Addr2line_cache_entry
2750 Dwarf_line_info
* dwarf_line_info
;
2751 unsigned int generation_count
;
2752 unsigned int access_count
;
2754 Addr2line_cache_entry(Object
* o
, unsigned int s
, Dwarf_line_info
* d
)
2755 : object(o
), shndx(s
), dwarf_line_info(d
),
2756 generation_count(next_generation_count
), access_count(0)
2758 if (next_generation_count
< (1U << 31))
2759 ++next_generation_count
;
2762 // We expect this cache to be small, so don't bother with a hashtable
2763 // or priority queue or anything: just use a simple vector.
2764 static std::vector
<Addr2line_cache_entry
> addr2line_cache
;
2767 Dwarf_line_info::one_addr2line(Object
* object
,
2768 unsigned int shndx
, off_t offset
,
2770 std::vector
<std::string
>* other_lines
)
2772 Dwarf_line_info
* lineinfo
= NULL
;
2773 std::vector
<Addr2line_cache_entry
>::iterator it
;
2775 // First, check the cache. If we hit, update the counts.
2776 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
2778 if (it
->object
== object
&& it
->shndx
== shndx
)
2780 lineinfo
= it
->dwarf_line_info
;
2781 it
->generation_count
= next_generation_count
;
2782 // We cap generation_count at 2^31 -1 to avoid overflow.
2783 if (next_generation_count
< (1U << 31))
2784 ++next_generation_count
;
2785 // We cap access_count at 31 so 2^access_count doesn't overflow
2786 if (it
->access_count
< 31)
2792 // If we don't hit the cache, create a new object and insert into the
2794 if (lineinfo
== NULL
)
2796 switch (parameters
->size_and_endianness())
2798 #ifdef HAVE_TARGET_32_LITTLE
2799 case Parameters::TARGET_32_LITTLE
:
2800 lineinfo
= new Sized_dwarf_line_info
<32, false>(object
, shndx
); break;
2802 #ifdef HAVE_TARGET_32_BIG
2803 case Parameters::TARGET_32_BIG
:
2804 lineinfo
= new Sized_dwarf_line_info
<32, true>(object
, shndx
); break;
2806 #ifdef HAVE_TARGET_64_LITTLE
2807 case Parameters::TARGET_64_LITTLE
:
2808 lineinfo
= new Sized_dwarf_line_info
<64, false>(object
, shndx
); break;
2810 #ifdef HAVE_TARGET_64_BIG
2811 case Parameters::TARGET_64_BIG
:
2812 lineinfo
= new Sized_dwarf_line_info
<64, true>(object
, shndx
); break;
2817 addr2line_cache
.push_back(Addr2line_cache_entry(object
, shndx
, lineinfo
));
2820 // Now that we have our object, figure out the answer
2821 std::string retval
= lineinfo
->addr2line(shndx
, offset
, other_lines
);
2823 // Finally, if our cache has grown too big, delete old objects. We
2824 // assume the common (probably only) case is deleting only one object.
2825 // We use a pretty simple scheme to evict: function of LRU and MFU.
2826 while (addr2line_cache
.size() > cache_size
)
2828 unsigned int lowest_score
= ~0U;
2829 std::vector
<Addr2line_cache_entry
>::iterator lowest
2830 = addr2line_cache
.end();
2831 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
2833 const unsigned int score
= (it
->generation_count
2834 + (1U << it
->access_count
));
2835 if (score
< lowest_score
)
2837 lowest_score
= score
;
2841 if (lowest
!= addr2line_cache
.end())
2843 delete lowest
->dwarf_line_info
;
2844 addr2line_cache
.erase(lowest
);
2852 Dwarf_line_info::clear_addr2line_cache()
2854 for (std::vector
<Addr2line_cache_entry
>::iterator it
= addr2line_cache
.begin();
2855 it
!= addr2line_cache
.end();
2857 delete it
->dwarf_line_info
;
2858 addr2line_cache
.clear();
2861 #ifdef HAVE_TARGET_32_LITTLE
2863 class Sized_dwarf_line_info
<32, false>;
2866 #ifdef HAVE_TARGET_32_BIG
2868 class Sized_dwarf_line_info
<32, true>;
2871 #ifdef HAVE_TARGET_64_LITTLE
2873 class Sized_dwarf_line_info
<64, false>;
2876 #ifdef HAVE_TARGET_64_BIG
2878 class Sized_dwarf_line_info
<64, true>;
2881 } // End namespace gold.