1 // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
3 // Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
24 #ifndef GOLD_DWARF_READER_H
25 #define GOLD_DWARF_READER_H
30 #include <sys/types.h>
33 #include "elfcpp_swap.h"
40 class Dwarf_info_reader
;
41 struct LineStateMachine
;
43 // This class is used to extract the section index and offset of
44 // the target of a relocation for a given offset within the section.
46 class Elf_reloc_mapper
56 // Initialize the relocation tracker for section RELOC_SHNDX.
58 initialize(unsigned int reloc_shndx
, unsigned int reloc_type
)
59 { return this->do_initialize(reloc_shndx
, reloc_type
); }
61 // Return the next reloc_offset.
64 { return this->do_next_offset(); }
66 // Advance to the next relocation past OFFSET.
69 { this->do_advance(offset
); }
71 // Return the section index and offset within the section of the target
72 // of the relocation for RELOC_OFFSET in the referring section.
74 get_reloc_target(off_t reloc_offset
, off_t
* target_offset
)
75 { return this->do_get_reloc_target(reloc_offset
, target_offset
); }
77 // Checkpoint the current position in the reloc section.
80 { return this->do_checkpoint(); }
82 // Reset the current position to the CHECKPOINT.
84 reset(uint64_t checkpoint
)
85 { this->do_reset(checkpoint
); }
89 do_initialize(unsigned int, unsigned int) = 0;
91 // Return the next reloc_offset.
95 // Advance to the next relocation past OFFSET.
97 do_advance(off_t offset
) = 0;
100 do_get_reloc_target(off_t reloc_offset
, off_t
* target_offset
) = 0;
102 // Checkpoint the current position in the reloc section.
104 do_checkpoint() const = 0;
106 // Reset the current position to the CHECKPOINT.
108 do_reset(uint64_t checkpoint
) = 0;
111 template<int size
, bool big_endian
>
112 class Sized_elf_reloc_mapper
: public Elf_reloc_mapper
115 Sized_elf_reloc_mapper(Object
* object
, const unsigned char* symtab
,
117 : object_(object
), symtab_(symtab
), symtab_size_(symtab_size
),
118 reloc_type_(0), track_relocs_()
123 do_initialize(unsigned int reloc_shndx
, unsigned int reloc_type
);
125 // Return the next reloc_offset.
128 { return this->track_relocs_
.next_offset(); }
130 // Advance to the next relocation past OFFSET.
132 do_advance(off_t offset
)
133 { this->track_relocs_
.advance(offset
); }
136 do_get_reloc_target(off_t reloc_offset
, off_t
* target_offset
);
138 // Checkpoint the current position in the reloc section.
140 do_checkpoint() const
141 { return this->track_relocs_
.checkpoint(); }
143 // Reset the current position to the CHECKPOINT.
145 do_reset(uint64_t checkpoint
)
146 { this->track_relocs_
.reset(checkpoint
); }
149 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
151 // Return the section index of symbol SYMNDX, and copy its value to *VALUE.
152 // Set *IS_ORDINARY true if the section index is an ordinary section index.
154 symbol_section(unsigned int symndx
, Address
* value
, bool* is_ordinary
);
158 // The ELF symbol table.
159 const unsigned char* symtab_
;
160 // The size of the ELF symbol table.
162 // Type of the relocation section (SHT_REL or SHT_RELA).
163 unsigned int reloc_type_
;
164 // Relocations for the referring section.
165 Track_relocs
<size
, big_endian
> track_relocs_
;
168 // This class is used to read the abbreviations table from the
169 // .debug_abbrev section of the object file.
171 class Dwarf_abbrev_table
174 // An attribute list entry.
177 Attribute(unsigned int a
, unsigned int f
)
184 // An abbrev code entry.
187 Abbrev_code(unsigned int t
, bool hc
)
188 : tag(t
), has_children(hc
), has_sibling_attribute(false), attributes()
190 this->attributes
.reserve(10);
194 add_attribute(unsigned int attr
, unsigned int form
)
196 this->attributes
.push_back(Attribute(attr
, form
));
201 // True if the DIE has children.
202 bool has_children
: 1;
203 // True if the DIE has a sibling attribute.
204 bool has_sibling_attribute
: 1;
205 // The list of attributes and forms.
206 std::vector
<Attribute
> attributes
;
210 : abbrev_shndx_(0), abbrev_offset_(0), buffer_(NULL
), buffer_end_(NULL
),
211 owns_buffer_(false), buffer_pos_(NULL
), high_abbrev_codes_()
213 memset(this->low_abbrev_codes_
, 0, sizeof(this->low_abbrev_codes_
));
216 ~Dwarf_abbrev_table()
218 if (this->owns_buffer_
&& this->buffer_
!= NULL
)
219 delete[] this->buffer_
;
220 this->clear_abbrev_codes();
223 // Read the abbrev table from an object file.
225 read_abbrevs(Relobj
* object
,
226 unsigned int abbrev_shndx
,
229 // If we've already read this abbrev table, return immediately.
230 if (this->abbrev_shndx_
> 0
231 && this->abbrev_shndx_
== abbrev_shndx
232 && this->abbrev_offset_
== abbrev_offset
)
234 return this->do_read_abbrevs(object
, abbrev_shndx
, abbrev_offset
);
237 // Return the abbrev code entry for CODE. This is a fast path for
238 // abbrev codes that are in the direct lookup table. If not found
239 // there, we call do_get_abbrev() to do the hard work.
241 get_abbrev(unsigned int code
)
243 if (code
< this->low_abbrev_code_max_
244 && this->low_abbrev_codes_
[code
] != NULL
)
245 return this->low_abbrev_codes_
[code
];
246 return this->do_get_abbrev(code
);
250 // Read the abbrev table from an object file.
252 do_read_abbrevs(Relobj
* object
,
253 unsigned int abbrev_shndx
,
254 off_t abbrev_offset
);
256 // Lookup the abbrev code entry for CODE.
258 do_get_abbrev(unsigned int code
);
260 // Store an abbrev code entry for CODE.
262 store_abbrev(unsigned int code
, const Abbrev_code
* entry
)
264 if (code
< this->low_abbrev_code_max_
)
265 this->low_abbrev_codes_
[code
] = entry
;
267 this->high_abbrev_codes_
[code
] = entry
;
270 // Clear the abbrev code table and release the memory it uses.
272 clear_abbrev_codes();
274 typedef Unordered_map
<unsigned int, const Abbrev_code
*> Abbrev_code_table
;
276 // The section index of the current abbrev table.
277 unsigned int abbrev_shndx_
;
278 // The offset within the section of the current abbrev table.
279 off_t abbrev_offset_
;
280 // The buffer containing the .debug_abbrev section.
281 const unsigned char* buffer_
;
282 const unsigned char* buffer_end_
;
283 // True if this object owns the buffer and needs to delete it.
285 // Pointer to the current position in the buffer.
286 const unsigned char* buffer_pos_
;
287 // The table of abbrev codes.
288 // We use a direct-lookup array for low abbrev codes,
289 // and store the rest in a hash table.
290 static const unsigned int low_abbrev_code_max_
= 256;
291 const Abbrev_code
* low_abbrev_codes_
[low_abbrev_code_max_
];
292 Abbrev_code_table high_abbrev_codes_
;
295 // A DWARF range list. The start and end offsets are relative
296 // to the input section SHNDX. Each range must lie entirely
297 // within a single section.
299 class Dwarf_range_list
304 Range(unsigned int a_shndx
, off_t a_start
, off_t a_end
)
305 : shndx(a_shndx
), start(a_start
), end(a_end
)
318 add(unsigned int shndx
, off_t start
, off_t end
)
319 { this->range_list_
.push_back(Range(shndx
, start
, end
)); }
323 { return this->range_list_
.size(); }
326 operator[](off_t i
) const
327 { return this->range_list_
[i
]; }
330 std::vector
<Range
> range_list_
;
333 // This class is used to read the ranges table from the
334 // .debug_ranges section of the object file.
336 class Dwarf_ranges_table
339 Dwarf_ranges_table(Dwarf_info_reader
* dwinfo
)
340 : dwinfo_(dwinfo
), ranges_shndx_(0), ranges_buffer_(NULL
),
341 ranges_buffer_end_(NULL
), owns_ranges_buffer_(false),
342 ranges_reloc_mapper_(NULL
), reloc_type_(0), output_section_offset_(0)
345 ~Dwarf_ranges_table()
347 if (this->owns_ranges_buffer_
&& this->ranges_buffer_
!= NULL
)
348 delete[] this->ranges_buffer_
;
349 if (this->ranges_reloc_mapper_
!= NULL
)
350 delete this->ranges_reloc_mapper_
;
353 // Read the ranges table from an object file.
355 read_ranges_table(Relobj
* object
,
356 const unsigned char* symtab
,
358 unsigned int ranges_shndx
);
360 // Read the range table from an object file.
362 read_range_list(Relobj
* object
,
363 const unsigned char* symtab
,
365 unsigned int address_size
,
366 unsigned int ranges_shndx
,
367 off_t ranges_offset
);
369 // Look for a relocation at offset OFF in the range table,
370 // and return the section index and offset of the target.
372 lookup_reloc(off_t off
, off_t
* target_off
);
375 // The Dwarf_info_reader, for reading data.
376 Dwarf_info_reader
* dwinfo_
;
377 // The section index of the ranges table.
378 unsigned int ranges_shndx_
;
379 // The buffer containing the .debug_ranges section.
380 const unsigned char* ranges_buffer_
;
381 const unsigned char* ranges_buffer_end_
;
382 // True if this object owns the buffer and needs to delete it.
383 bool owns_ranges_buffer_
;
384 // Relocation mapper for the .debug_ranges section.
385 Elf_reloc_mapper
* ranges_reloc_mapper_
;
386 // Type of the relocation section (SHT_REL or SHT_RELA).
387 unsigned int reloc_type_
;
388 // For incremental update links, this will hold the offset of the
389 // input section within the output section. Offsets read from
390 // relocated data will be relative to the output section, and need
391 // to be corrected before reading data from the input section.
392 uint64_t output_section_offset_
;
395 // This class is used to read the pubnames and pubtypes tables from the
396 // .debug_pubnames and .debug_pubtypes sections of the object file.
398 class Dwarf_pubnames_table
401 Dwarf_pubnames_table(Dwarf_info_reader
* dwinfo
, bool is_pubtypes
)
402 : dwinfo_(dwinfo
), buffer_(NULL
), buffer_end_(NULL
), owns_buffer_(false),
403 offset_size_(0), pinfo_(NULL
), end_of_table_(NULL
),
404 is_pubtypes_(is_pubtypes
), is_gnu_style_(false),
405 output_section_offset_(0), unit_length_(0), cu_offset_(0)
408 ~Dwarf_pubnames_table()
410 if (this->owns_buffer_
&& this->buffer_
!= NULL
)
411 delete[] this->buffer_
;
414 // Read the pubnames section from the object file, using the symbol
415 // table for relocating it.
417 read_section(Relobj
* object
, const unsigned char* symbol_table
,
420 // Read the header for the set at OFFSET.
422 read_header(off_t offset
);
424 // Return the offset to the cu within the info or types section.
427 { return this->cu_offset_
; }
429 // Return the size of this subsection of the table. The unit length
430 // doesn't include the size of its own field.
433 { return this->unit_length_
; }
435 // Read the next name from the set. If the pubname table is gnu-style,
436 // FLAG_BYTE is set to the high-byte of a gdb_index version 7 cu_index.
438 next_name(uint8_t* flag_byte
);
441 // The Dwarf_info_reader, for reading data.
442 Dwarf_info_reader
* dwinfo_
;
443 // The buffer containing the .debug_ranges section.
444 const unsigned char* buffer_
;
445 const unsigned char* buffer_end_
;
446 // True if this object owns the buffer and needs to delete it.
448 // The size of a DWARF offset for the current set.
449 unsigned int offset_size_
;
450 // The current position within the buffer.
451 const unsigned char* pinfo_
;
452 // The end of the current pubnames table.
453 const unsigned char* end_of_table_
;
454 // TRUE if this is a .debug_pubtypes section.
456 // Gnu-style pubnames table. This style has an extra flag byte between the
457 // offset and the name, and is used for generating version 7 of gdb-index.
459 // For incremental update links, this will hold the offset of the
460 // input section within the output section. Offsets read from
461 // relocated data will be relative to the output section, and need
462 // to be corrected before reading data from the input section.
463 uint64_t output_section_offset_
;
464 // Fields read from the header.
465 uint64_t unit_length_
;
468 // Track relocations for this table so we can find the CUs that
469 // correspond to the subsections.
470 Elf_reloc_mapper
* reloc_mapper_
;
471 // Type of the relocation section (SHT_REL or SHT_RELA).
472 unsigned int reloc_type_
;
475 // This class represents a DWARF Debug Info Entry (DIE).
480 // An attribute value.
481 struct Attribute_value
489 const char* stringval
;
490 const unsigned char* blockval
;
495 // Section index for reference forms.
497 // Block length for block forms.
498 unsigned int blocklen
;
499 // Attribute offset for DW_FORM_strp.
500 unsigned int attr_off
;
504 // A list of attribute values.
505 typedef std::vector
<Attribute_value
> Attributes
;
507 Dwarf_die(Dwarf_info_reader
* dwinfo
,
511 // Return the DWARF tag for this DIE.
515 if (this->abbrev_code_
== NULL
)
517 return this->abbrev_code_
->tag
;
520 // Return true if this DIE has children.
524 gold_assert(this->abbrev_code_
!= NULL
);
525 return this->abbrev_code_
->has_children
;
528 // Return true if this DIE has a sibling attribute.
530 has_sibling_attribute() const
532 gold_assert(this->abbrev_code_
!= NULL
);
533 return this->abbrev_code_
->has_sibling_attribute
;
536 // Return the value of attribute ATTR.
537 const Attribute_value
*
538 attribute(unsigned int attr
);
540 // Return the value of the DW_AT_name attribute.
544 if (this->name_
== NULL
)
549 // Return the value of the DW_AT_linkage_name
550 // or DW_AT_MIPS_linkage_name attribute.
554 if (this->linkage_name_
== NULL
)
555 this->set_linkage_name();
556 return this->linkage_name_
;
559 // Return the value of the DW_AT_specification attribute.
563 if (!this->attributes_read_
)
564 this->read_attributes();
565 return this->specification_
;
568 // Return the value of the DW_AT_abstract_origin attribute.
572 if (!this->attributes_read_
)
573 this->read_attributes();
574 return this->abstract_origin_
;
577 // Return the value of attribute ATTR as a string.
579 string_attribute(unsigned int attr
);
581 // Return the value of attribute ATTR as an integer.
583 int_attribute(unsigned int attr
);
585 // Return the value of attribute ATTR as an unsigned integer.
587 uint_attribute(unsigned int attr
);
589 // Return the value of attribute ATTR as a reference.
591 ref_attribute(unsigned int attr
, unsigned int* shndx
);
593 // Return the value of attribute ATTR as a address.
595 address_attribute(unsigned int attr
, unsigned int* shndx
);
597 // Return the value of attribute ATTR as a flag.
599 flag_attribute(unsigned int attr
)
600 { return this->int_attribute(attr
) != 0; }
602 // Return true if this DIE is a declaration.
605 { return this->flag_attribute(elfcpp::DW_AT_declaration
); }
607 // Return the parent of this DIE.
610 { return this->parent_
; }
612 // Return the offset of this DIE.
615 { return this->die_offset_
; }
617 // Return the offset of this DIE's first child.
621 // Set the offset of this DIE's next sibling.
623 set_sibling_offset(off_t sibling_offset
)
624 { this->sibling_offset_
= sibling_offset
; }
626 // Return the offset of this DIE's next sibling.
631 typedef Dwarf_abbrev_table::Abbrev_code Abbrev_code
;
633 // Read all the attributes of the DIE.
637 // Set the name of the DIE if present.
641 // Set the linkage name if present.
645 // Skip all the attributes of the DIE and return the offset
650 // The Dwarf_info_reader, for reading attributes.
651 Dwarf_info_reader
* dwinfo_
;
652 // The parent of this DIE.
654 // Offset of this DIE within its compilation unit.
656 // Offset of the first attribute, relative to the beginning of the DIE.
658 // Offset of the first child, relative to the compilation unit.
660 // Offset of the next sibling, relative to the compilation unit.
661 off_t sibling_offset_
;
662 // The abbreviation table entry.
663 const Abbrev_code
* abbrev_code_
;
664 // The list of attributes.
665 Attributes attributes_
;
666 // True if the attributes have been read.
667 bool attributes_read_
;
668 // The following fields hold common attributes to avoid a linear
669 // search through the attribute list.
670 // The DIE name (DW_AT_name).
672 // Offset of the name in the string table (for DW_FORM_strp).
674 // The linkage name (DW_AT_linkage_name or DW_AT_MIPS_linkage_name).
675 const char* linkage_name_
;
676 // Offset of the linkage name in the string table (for DW_FORM_strp).
677 off_t linkage_name_off_
;
678 // Section index of the string table (for DW_FORM_strp).
679 unsigned int string_shndx_
;
680 // The value of a DW_AT_specification attribute.
681 off_t specification_
;
682 // The value of a DW_AT_abstract_origin attribute.
683 off_t abstract_origin_
;
686 // This class is used to read the debug info from the .debug_info
687 // or .debug_types sections. This is a base class that implements
688 // the generic parsing of the compilation unit header and DIE
689 // structure. The parse() method parses the entire section, and
690 // calls the various visit_xxx() methods for each header. Clients
691 // should derive a new class from this one and implement the
692 // visit_compilation_unit() and visit_type_unit() functions.
694 class Dwarf_info_reader
697 Dwarf_info_reader(bool is_type_unit
,
699 const unsigned char* symtab
,
702 unsigned int reloc_shndx
,
703 unsigned int reloc_type
)
704 : is_type_unit_(is_type_unit
), object_(object
), symtab_(symtab
),
705 symtab_size_(symtab_size
), shndx_(shndx
), reloc_shndx_(reloc_shndx
),
706 reloc_type_(reloc_type
), abbrev_shndx_(0), string_shndx_(0),
707 buffer_(NULL
), buffer_end_(NULL
), cu_offset_(0), cu_length_(0),
708 offset_size_(0), address_size_(0), cu_version_(0),
709 abbrev_table_(), ranges_table_(this),
710 reloc_mapper_(NULL
), string_buffer_(NULL
), string_buffer_end_(NULL
),
711 owns_string_buffer_(false), string_output_section_offset_(0)
717 if (this->reloc_mapper_
!= NULL
)
718 delete this->reloc_mapper_
;
719 if (this->owns_string_buffer_
&& this->string_buffer_
!= NULL
)
720 delete[] this->string_buffer_
;
723 // Begin parsing the debug info. This calls visit_compilation_unit()
724 // or visit_type_unit() for each compilation or type unit found in the
725 // section, and visit_die() for each top-level DIE.
729 // Return the abbrev code entry for a CODE.
730 const Dwarf_abbrev_table::Abbrev_code
*
731 get_abbrev(unsigned int code
)
732 { return this->abbrev_table_
.get_abbrev(code
); }
734 // Return a pointer to the DWARF info buffer at OFFSET.
736 buffer_at_offset(off_t offset
) const
738 const unsigned char* p
= this->buffer_
+ this->cu_offset_
+ offset
;
739 if (this->check_buffer(p
+ 1))
744 // Read a possibly unaligned integer of SIZE.
745 template <int valsize
>
746 inline typename
elfcpp::Valtype_base
<valsize
>::Valtype
747 read_from_pointer(const unsigned char* source
);
749 // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
750 template <int valsize
>
751 inline typename
elfcpp::Valtype_base
<valsize
>::Valtype
752 read_from_pointer(const unsigned char** source
);
754 // Look for a relocation at offset ATTR_OFF in the dwarf info,
755 // and return the section index and offset of the target.
757 lookup_reloc(off_t attr_off
, off_t
* target_off
);
759 // Return a string from the DWARF string table.
761 get_string(off_t str_off
, unsigned int string_shndx
);
763 // Return the size of a DWARF offset.
766 { return this->offset_size_
; }
768 // Return the size of an address.
771 { return this->address_size_
; }
773 // Set the section index of the .debug_abbrev section.
774 // We use this if there are no relocations for the .debug_info section.
775 // If not set, the code parse() routine will search for the section by name.
777 set_abbrev_shndx(unsigned int abbrev_shndx
)
778 { this->abbrev_shndx_
= abbrev_shndx
; }
780 // Return a pointer to the object file's ELF symbol table.
783 { return this->symtab_
; }
785 // Return the size of the object file's ELF symbol table.
788 { return this->symtab_size_
; }
790 // Return the offset of the current compilation unit.
793 { return this->cu_offset_
; }
796 // Begin parsing the debug info. This calls visit_compilation_unit()
797 // or visit_type_unit() for each compilation or type unit found in the
798 // section, and visit_die() for each top-level DIE.
799 template<bool big_endian
>
803 // The following methods are hooks that are meant to be implemented
804 // by a derived class. A default, do-nothing, implementation of
805 // each is provided for this base class.
807 // Visit a compilation unit.
809 visit_compilation_unit(off_t cu_offset
, off_t cu_length
, Dwarf_die
* root_die
);
811 // Visit a type unit.
813 visit_type_unit(off_t tu_offset
, off_t tu_length
, off_t type_offset
,
814 uint64_t signature
, Dwarf_die
* root_die
);
816 // Read the range table.
818 read_range_list(unsigned int ranges_shndx
, off_t ranges_offset
)
820 return this->ranges_table_
.read_range_list(this->object_
,
828 // Return the object.
831 { return this->object_
; }
833 // Checkpoint the relocation tracker.
835 get_reloc_checkpoint() const
836 { return this->reloc_mapper_
->checkpoint(); }
838 // Reset the relocation tracker to the CHECKPOINT.
840 reset_relocs(uint64_t checkpoint
)
841 { this->reloc_mapper_
->reset(checkpoint
); }
844 // Print a warning about a corrupt debug section.
846 warn_corrupt_debug_section() const;
848 // Check that P is within the bounds of the current section.
850 check_buffer(const unsigned char* p
) const
852 if (p
> this->buffer_
+ this->cu_offset_
+ this->cu_length_
)
854 this->warn_corrupt_debug_section();
860 // Read the DWARF string table.
862 read_string_table(unsigned int string_shndx
)
864 // If we've already read this string table, return immediately.
865 if (this->string_shndx_
> 0 && this->string_shndx_
== string_shndx
)
867 if (string_shndx
== 0 && this->string_shndx_
> 0)
869 return this->do_read_string_table(string_shndx
);
873 do_read_string_table(unsigned int string_shndx
);
875 // True if this is a type unit; false for a compilation unit.
877 // The object containing the .debug_info or .debug_types input section.
879 // The ELF symbol table.
880 const unsigned char* symtab_
;
881 // The size of the ELF symbol table.
883 // Index of the .debug_info or .debug_types section.
885 // Index of the relocation section.
886 unsigned int reloc_shndx_
;
887 // Type of the relocation section (SHT_REL or SHT_RELA).
888 unsigned int reloc_type_
;
889 // Index of the .debug_abbrev section (0 if not known).
890 unsigned int abbrev_shndx_
;
891 // Index of the .debug_str section.
892 unsigned int string_shndx_
;
893 // The buffer for the debug info.
894 const unsigned char* buffer_
;
895 const unsigned char* buffer_end_
;
896 // Offset of the current compilation unit.
898 // Length of the current compilation unit.
900 // Size of a DWARF offset for the current compilation unit.
901 unsigned int offset_size_
;
902 // Size of an address for the target architecture.
903 unsigned int address_size_
;
904 // Compilation unit version number.
905 unsigned int cu_version_
;
906 // Abbreviations table for current compilation unit.
907 Dwarf_abbrev_table abbrev_table_
;
908 // Ranges table for the current compilation unit.
909 Dwarf_ranges_table ranges_table_
;
910 // Relocation mapper for the section.
911 Elf_reloc_mapper
* reloc_mapper_
;
912 // The buffer for the debug string table.
913 const char* string_buffer_
;
914 const char* string_buffer_end_
;
915 // True if this object owns the buffer and needs to delete it.
916 bool owns_string_buffer_
;
917 // For incremental update links, this will hold the offset of the
918 // input .debug_str section within the output section. Offsets read
919 // from relocated data will be relative to the output section, and need
920 // to be corrected before reading data from the input section.
921 uint64_t string_output_section_offset_
;
924 // We can't do better than to keep the offsets in a sorted vector.
925 // Here, offset is the key, and file_num/line_num is the value.
926 struct Offset_to_lineno_entry
929 int header_num
; // which file-list to use (i.e. which .o file are we in)
930 // A pointer into files_.
931 unsigned int file_num
: sizeof(int) * CHAR_BIT
- 1;
932 // True if this was the last entry for the current offset, meaning
933 // it's the line that actually applies.
934 unsigned int last_line_for_offset
: 1;
935 // The line number in the source file. -1 to indicate end-of-function.
938 // This sorts by offsets first, and then puts the correct line to
939 // report for a given offset at the beginning of the run of equal
940 // offsets (so that asking for 1 line gives the best answer). This
941 // is not a total ordering.
942 bool operator<(const Offset_to_lineno_entry
& that
) const
944 if (this->offset
!= that
.offset
)
945 return this->offset
< that
.offset
;
946 // Note the '>' which makes this sort 'true' first.
947 return this->last_line_for_offset
> that
.last_line_for_offset
;
951 // This class is used to read the line information from the debugging
952 // section of an object file.
954 class Dwarf_line_info
964 // Given a section number and an offset, returns the associated
965 // file and line-number, as a string: "file:lineno". If unable
966 // to do the mapping, returns the empty string. You must call
967 // read_line_mappings() before calling this function. If
968 // 'other_lines' is non-NULL, fills that in with other line
969 // numbers assigned to the same offset.
971 addr2line(unsigned int shndx
, off_t offset
,
972 std::vector
<std::string
>* other_lines
)
973 { return this->do_addr2line(shndx
, offset
, other_lines
); }
975 // A helper function for a single addr2line lookup. It also keeps a
976 // cache of the last CACHE_SIZE Dwarf_line_info objects it created;
977 // set to 0 not to cache at all. The larger CACHE_SIZE is, the more
978 // chance this routine won't have to re-create a Dwarf_line_info
979 // object for its addr2line computation; such creations are slow.
980 // NOTE: Not thread-safe, so only call from one thread at a time.
982 one_addr2line(Object
* object
, unsigned int shndx
, off_t offset
,
983 size_t cache_size
, std::vector
<std::string
>* other_lines
);
985 // This reclaims all the memory that one_addr2line may have cached.
986 // Use this when you know you will not be calling one_addr2line again.
988 clear_addr2line_cache();
992 do_addr2line(unsigned int shndx
, off_t offset
,
993 std::vector
<std::string
>* other_lines
) = 0;
996 template<int size
, bool big_endian
>
997 class Sized_dwarf_line_info
: public Dwarf_line_info
1000 // Initializes a .debug_line reader for a given object file.
1001 // If SHNDX is specified and non-negative, only read the debug
1002 // information that pertains to the specified section.
1003 Sized_dwarf_line_info(Object
* object
, unsigned int read_shndx
= -1U);
1006 ~Sized_dwarf_line_info()
1008 if (this->buffer_start_
!= NULL
)
1009 delete[] this->buffer_start_
;
1014 do_addr2line(unsigned int shndx
, off_t offset
,
1015 std::vector
<std::string
>* other_lines
);
1017 // Formats a file and line number to a string like "dirname/filename:lineno".
1019 format_file_lineno(const Offset_to_lineno_entry
& lineno
) const;
1021 // Start processing line info, and populates the offset_map_.
1022 // If SHNDX is non-negative, only store debug information that
1023 // pertains to the specified section.
1025 read_line_mappings(unsigned int shndx
);
1027 // Reads the relocation section associated with .debug_line and
1028 // stores relocation information in reloc_map_.
1032 // Reads the DWARF2/3 header for this line info. Each takes as input
1033 // a starting buffer position, and returns the ending position.
1034 const unsigned char*
1035 read_header_prolog(const unsigned char* lineptr
);
1037 const unsigned char*
1038 read_header_tables(const unsigned char* lineptr
);
1040 // Reads the DWARF2/3 line information. If shndx is non-negative,
1041 // discard all line information that doesn't pertain to the given
1043 const unsigned char*
1044 read_lines(const unsigned char* lineptr
, unsigned int shndx
);
1046 // Process a single line info opcode at START using the state
1047 // machine at LSM. Return true if we should define a line using the
1048 // current state of the line state machine. Place the length of the
1051 process_one_opcode(const unsigned char* start
,
1052 struct LineStateMachine
* lsm
, size_t* len
);
1054 // Some parts of processing differ depending on whether the input
1055 // was a .o file or not.
1056 bool input_is_relobj();
1058 // If we saw anything amiss while parsing, we set this to false.
1059 // Then addr2line will always fail (rather than return possibly-
1063 // A DWARF2/3 line info header. This is not the same size as in the
1064 // actual file, as the one in the file may have a 32 bit or 64 bit
1067 struct Dwarf_line_infoHeader
1071 off_t prologue_length
;
1072 int min_insn_length
; // insn stands for instructin
1073 bool default_is_stmt
; // stmt stands for statement
1074 signed char line_base
;
1076 unsigned char opcode_base
;
1077 std::vector
<unsigned char> std_opcode_lengths
;
1081 // buffer is the buffer for our line info, starting at exactly where
1082 // the line info to read is.
1083 const unsigned char* buffer_
;
1084 const unsigned char* buffer_end_
;
1085 // If the buffer was allocated temporarily, and therefore must be
1086 // deallocated in the dtor, this contains a pointer to the start
1088 const unsigned char* buffer_start_
;
1090 // This has relocations that point into buffer.
1091 Sized_elf_reloc_mapper
<size
, big_endian
>* reloc_mapper_
;
1092 // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
1093 unsigned int track_relocs_type_
;
1095 // This is used to figure out what section to apply a relocation to.
1096 const unsigned char* symtab_buffer_
;
1097 section_size_type symtab_buffer_size_
;
1099 // Holds the directories and files as we see them. We have an array
1100 // of directory-lists, one for each .o file we're reading (usually
1101 // there will just be one, but there may be more if input is a .so).
1102 std::vector
<std::vector
<std::string
> > directories_
;
1103 // The first part is an index into directories_, the second the filename.
1104 std::vector
<std::vector
< std::pair
<int, std::string
> > > files_
;
1106 // An index into the current directories_ and files_ vectors.
1107 int current_header_index_
;
1109 // A sorted map from offset of the relocation target to the shndx
1110 // and addend for the relocation.
1111 typedef std::map
<off_t
, std::pair
<unsigned int, off_t
> >
1113 Reloc_map reloc_map_
;
1115 // We have a vector of offset->lineno entries for every input section.
1116 typedef Unordered_map
<unsigned int, std::vector
<Offset_to_lineno_entry
> >
1119 Lineno_map line_number_map_
;
1122 } // End namespace gold.
1124 #endif // !defined(GOLD_DWARF_READER_H)