arm: document -march=armv9.[123]-a binutils options
[binutils-gdb.git] / gold / dwarf_reader.cc
blob78111397d3136aa6a8103071e70365a11c398651
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.
23 #include "gold.h"
25 #include <algorithm>
26 #include <utility>
27 #include <vector>
29 #include "debug.h"
30 #include "elfcpp_swap.h"
31 #include "dwarf.h"
32 #include "object.h"
33 #include "reloc.h"
34 #include "dwarf_reader.h"
35 #include "int_encoding.h"
36 #include "compressed_output.h"
38 namespace gold {
40 // Class Sized_elf_reloc_mapper
42 // Initialize the relocation tracker for section RELOC_SHNDX.
44 template<int size, bool big_endian>
45 bool
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,
51 reloc_type);
54 // Looks in the symtab to see what section a symbol is in.
56 template<int size, bool big_endian>
57 unsigned int
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(),
66 is_ordinary);
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>
73 unsigned int
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())
79 return 0;
80 unsigned int symndx = this->track_relocs_.next_symndx();
81 typename elfcpp::Elf_types<size>::Elf_Addr value;
82 bool is_ordinary;
83 unsigned int target_shndx = this->symbol_section(symndx, &value,
84 &is_ordinary);
85 if (!is_ordinary)
86 return 0;
87 if (this->reloc_type_ == elfcpp::SHT_RELA)
88 value += this->track_relocs_.next_addend();
89 *target_offset = value;
90 return target_shndx;
93 static inline Elf_reloc_mapper*
94 make_elf_reloc_mapper(Relobj* object, const unsigned char* symtab,
95 off_t symtab_size)
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,
103 symtab_size);
104 #else
105 gold_unreachable();
106 #endif
108 else
110 #ifdef HAVE_TARGET_32_LITTLE
111 return new Sized_elf_reloc_mapper<32, false>(object, symtab,
112 symtab_size);
113 #else
114 gold_unreachable();
115 #endif
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,
124 symtab_size);
125 #else
126 gold_unreachable();
127 #endif
129 else
131 #ifdef HAVE_TARGET_64_LITTLE
132 return new Sized_elf_reloc_mapper<64, false>(object, symtab,
133 symtab_size);
134 #else
135 gold_unreachable();
136 #endif
139 else
140 gold_unreachable();
143 // class Dwarf_abbrev_table
145 void
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();
158 ++it)
160 if (it->second != NULL)
161 delete it->second;
163 this->high_abbrev_codes_.clear();
166 // Read the abbrev table from an object file.
168 bool
169 Dwarf_abbrev_table::do_read_abbrevs(
170 Relobj* object,
171 unsigned int abbrev_shndx,
172 off_t abbrev_offset)
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")
187 abbrev_shndx = i;
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);
192 break;
195 if (abbrev_shndx == 0)
196 return false;
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;
209 this->buffer_ =
210 object->decompressed_section_contents(abbrev_shndx,
211 &buffer_size,
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;
218 return true;
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())
232 return it->second;
234 // Read and store abbrev code definitions until we find the
235 // one we're looking for.
236 for (;;)
238 // Read the abbrev code. A zero here indicates the end of the
239 // abbrev table.
240 size_t len;
241 if (this->buffer_pos_ >= this->buffer_end_)
242 return NULL;
243 uint64_t nextcode = read_unsigned_LEB_128(this->buffer_pos_, &len);
244 if (nextcode == 0)
246 this->buffer_pos_ = this->buffer_end_;
247 return NULL;
249 this->buffer_pos_ += len;
251 // Read the tag.
252 if (this->buffer_pos_ >= this->buffer_end_)
253 return NULL;
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_)
259 return NULL;
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);
265 for (;;)
267 // Read the attribute.
268 if (this->buffer_pos_ >= this->buffer_end_)
269 return NULL;
270 uint64_t attr = read_unsigned_LEB_128(this->buffer_pos_, &len);
271 this->buffer_pos_ += len;
273 // Read the form.
274 if (this->buffer_pos_ >= this->buffer_end_)
275 return NULL;
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)
289 break;
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)
299 return entry;
302 return NULL;
305 // class Dwarf_ranges_table
307 // Read the ranges table from an object file.
309 bool
310 Dwarf_ranges_table::read_ranges_table(
311 Relobj* object,
312 const unsigned char* symtab,
313 off_t symtab_size,
314 unsigned int ranges_shndx,
315 unsigned int version)
317 const std::string section_name(version < 5
318 ? ".debug_ranges"
319 : ".debug_rnglists");
320 const std::string compressed_section_name(version < 5
321 ? ".zdebug_ranges"
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)
327 return true;
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)
340 ranges_shndx = i;
341 this->output_section_offset_ = object->output_section_offset(i);
342 break;
345 if (ranges_shndx == 0)
346 return false;
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,
361 &buffer_size,
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())
375 return true;
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)
387 reloc_shndx = i;
388 break;
392 this->ranges_reloc_mapper_ = make_elf_reloc_mapper(object, symtab,
393 symtab_size);
394 this->ranges_reloc_mapper_->initialize(reloc_shndx, reloc_type);
395 this->reloc_type_ = reloc_type;
397 return true;
400 // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
402 Dwarf_range_list*
403 Dwarf_ranges_table::read_range_list(
404 Relobj* object,
405 const unsigned char* symtab,
406 off_t symtab_size,
407 unsigned int addr_size,
408 unsigned int ranges_shndx,
409 off_t offset)
411 Dwarf_range_list* ranges;
413 if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx, 4))
414 return NULL;
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();
423 off_t base = 0;
424 for (;
425 this->ranges_buffer_ + offset < this->ranges_buffer_end_;
426 offset += 2 * addr_size)
428 off_t start;
429 off_t end;
431 // Read the raw contents of the section.
432 if (addr_size == 4)
434 start = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
435 + offset);
436 end = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
437 + offset + 4);
439 else
441 start = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
442 + offset);
443 end = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
444 + offset + 8);
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)
458 break;
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)
464 base = end;
465 else if (shndx1 == shndx2)
467 if (shndx1 == 0 || object->is_section_included(shndx1))
468 ranges->add(shndx1, base + start, base + end);
470 else
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());
476 return ranges;
479 // Read a DWARF 5 range list from section RANGES_SHNDX at offset RANGES_OFFSET.
481 Dwarf_range_list*
482 Dwarf_ranges_table::read_range_list_v5(
483 Relobj* object,
484 const unsigned char* symtab,
485 off_t symtab_size,
486 unsigned int addr_size,
487 unsigned int ranges_shndx,
488 off_t offset)
490 Dwarf_range_list* ranges;
492 if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx, 5))
493 return NULL;
495 ranges = new Dwarf_range_list();
496 off_t base = 0;
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_)
508 off_t start;
509 off_t end;
510 unsigned int shndx1 = 0;
511 unsigned int shndx2 = 0;
512 size_t len;
514 // Read the entry type.
515 unsigned int rle_type = *prle++;
516 offset += 1;
518 if (rle_type == elfcpp::DW_RLE_end_of_list)
519 break;
521 switch (rle_type)
523 case elfcpp::DW_RLE_base_address:
524 if (addr_size == 4)
525 base = this->dwinfo_->read_from_pointer<32>(prle);
526 else
527 base = this->dwinfo_->read_from_pointer<64>(prle);
528 if (this->ranges_reloc_mapper_ != NULL)
529 shndx0 = this->lookup_reloc(offset, &base);
530 prle += addr_size;
531 offset += addr_size;
532 break;
534 case elfcpp::DW_RLE_offset_pair:
535 start = read_unsigned_LEB_128(prle, &len);
536 prle += len;
537 offset += len;
538 end = read_unsigned_LEB_128(prle, &len);
539 prle += len;
540 offset += len;
541 if (shndx0 == 0 || object->is_section_included(shndx0))
542 ranges->add(shndx0, base + start, base + end);
543 break;
545 case elfcpp::DW_RLE_start_end:
546 if (addr_size == 4)
548 start = this->dwinfo_->read_from_pointer<32>(prle);
549 end = this->dwinfo_->read_from_pointer<32>(prle + 4);
551 else
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);
569 break;
571 case elfcpp::DW_RLE_start_length:
572 if (addr_size == 4)
573 start = this->dwinfo_->read_from_pointer<32>(prle);
574 else
575 start = this->dwinfo_->read_from_pointer<64>(prle);
576 if (this->ranges_reloc_mapper_ != NULL)
577 shndx1 = this->lookup_reloc(offset, &start);
578 prle += addr_size;
579 offset += addr_size;
580 end = start + read_unsigned_LEB_128(prle, &len);
581 prle += len;
582 offset += len;
583 if (shndx1 == 0 || object->is_section_included(shndx1))
584 ranges->add(shndx1, start, end);
585 break;
587 default:
588 gold_warning(_("%s: DWARF range list contains "
589 "unsupported entry type (%d)"),
590 object->name().c_str(), rle_type);
591 break;
595 return ranges;
598 // Look for a relocation at offset OFF in the range table,
599 // and return the section index and offset of the target.
601 unsigned int
602 Dwarf_ranges_table::lookup_reloc(off_t off, off_t* target_off)
604 off_t value;
605 unsigned int shndx =
606 this->ranges_reloc_mapper_->get_reloc_target(off, &value);
607 if (shndx == 0)
608 return 0;
609 if (this->reloc_type_ == elfcpp::SHT_REL)
610 *target_off += value;
611 else
612 *target_off = value;
613 return shndx;
616 // class Dwarf_pubnames_table
618 // Read the pubnames section from the object file.
620 bool
621 Dwarf_pubnames_table::read_section(Relobj* object, const unsigned char* symtab,
622 off_t symtab_size)
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_
628 ? "gnu_pubtypes"
629 : "gnu_pubnames");
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;
639 else
640 continue;
641 if (strcmp(section_name_suffix, name) == 0)
643 shndx = i;
644 break;
646 else if (strcmp(section_name_suffix, gnu_name) == 0)
648 shndx = i;
649 this->is_gnu_style_ = true;
650 break;
653 if (shndx == 0)
654 return false;
656 this->buffer_ = object->decompressed_section_contents(shndx,
657 &buffer_size,
658 &this->owns_buffer_);
659 if (this->buffer_ == NULL)
660 return false;
661 this->buffer_end_ = this->buffer_ + buffer_size;
663 // For incremental objects, we have no relocations.
664 if (object->is_incremental())
665 return true;
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)
677 reloc_shndx = i;
678 break;
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;
686 return true;
689 // Read the header for the set at OFFSET.
691 bool
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_)
698 return false;
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);
704 pinfo += 4;
705 if (unit_length == 0xffffffff)
707 unit_length = this->dwinfo_->read_from_pointer<64>(pinfo);
708 this->unit_length_ = unit_length + 12;
709 pinfo += 8;
710 this->offset_size_ = 8;
712 else
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);
727 pinfo += 2;
728 if (version != 2)
729 return false;
731 this->reloc_mapper_->get_reloc_target(pinfo - this->buffer_,
732 &this->cu_offset_);
734 // Skip the debug_info_offset and debug_info_size fields.
735 pinfo += 2 * this->offset_size_;
737 if (pinfo >= this->buffer_end_)
738 return false;
740 this->pinfo_ = pinfo;
741 return true;
744 // Read the next name from the set.
746 const char*
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_)
754 return NULL;
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,
760 // it's harmless.
761 pinfo += this->offset_size_;
763 if (this->is_gnu_style_)
764 *flag_byte = *pinfo++;
765 else
766 *flag_byte = 0;
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')
772 ++pinfo;
773 if (pinfo < this->buffer_end_)
774 ++pinfo;
776 this->pinfo_ = pinfo;
777 return ret;
780 // class Dwarf_die
782 Dwarf_die::Dwarf_die(
783 Dwarf_info_reader* dwinfo,
784 off_t die_offset,
785 Dwarf_die* parent)
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),
790 abstract_origin_(0)
792 size_t len;
793 const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset);
794 if (pdie == NULL)
795 return;
796 unsigned int code = read_unsigned_LEB_128(pdie, &len);
797 if (code == 0)
799 if (parent != NULL)
800 parent->set_sibling_offset(die_offset + len);
801 return;
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.
811 bool
812 Dwarf_die::read_attributes()
814 if (this->attributes_read_)
815 return true;
817 gold_assert(this->abbrev_code_ != NULL);
819 const unsigned char* pdie =
820 this->dwinfo_->buffer_at_offset(this->die_offset_);
821 if (pdie == NULL)
822 return false;
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)
829 size_t len;
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);
835 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;
843 switch(form)
845 case elfcpp::DW_FORM_flag_present:
846 attr_value.val.intval = 1;
847 break;
848 case elfcpp::DW_FORM_implicit_const:
849 attr_value.val.intval =
850 this->abbrev_code_->attributes[i].implicit_const;
851 break;
852 case elfcpp::DW_FORM_strp:
853 case elfcpp::DW_FORM_strp_sup:
854 case elfcpp::DW_FORM_line_strp:
856 off_t str_off;
857 if (this->dwinfo_->offset_size() == 4)
858 str_off = this->dwinfo_->read_from_pointer<32>(&pattr);
859 else
860 str_off = this->dwinfo_->read_from_pointer<64>(&pattr);
861 unsigned int shndx =
862 this->dwinfo_->lookup_reloc(attr_off, &str_off);
863 attr_value.aux.shndx = shndx;
864 attr_value.val.refval = str_off;
865 break;
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);
870 pattr += len;
871 break;
872 case elfcpp::DW_FORM_strx1:
873 attr_value.val.uintval = *pattr++;
874 break;
875 case elfcpp::DW_FORM_strx2:
876 attr_value.val.uintval =
877 this->dwinfo_->read_from_pointer<16>(&pattr);
878 break;
879 case elfcpp::DW_FORM_strx3:
880 attr_value.val.uintval =
881 this->dwinfo_->read_3bytes_from_pointer(&pattr);
882 break;
883 case elfcpp::DW_FORM_strx4:
884 attr_value.val.uintval =
885 this->dwinfo_->read_from_pointer<32>(&pattr);
886 break;
887 case elfcpp::DW_FORM_sec_offset:
889 off_t sec_off;
890 if (this->dwinfo_->offset_size() == 4)
891 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
892 else
893 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
894 unsigned int shndx =
895 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
896 attr_value.aux.shndx = shndx;
897 attr_value.val.refval = sec_off;
898 ref_form = true;
899 break;
901 case elfcpp::DW_FORM_addr:
903 off_t sec_off;
904 if (this->dwinfo_->address_size() == 4)
905 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
906 else
907 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
908 unsigned int shndx =
909 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
910 attr_value.aux.shndx = shndx;
911 attr_value.val.refval = sec_off;
912 break;
914 case elfcpp::DW_FORM_ref_addr:
916 off_t sec_off;
917 if (this->dwinfo_->ref_addr_size() == 4)
918 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
919 else
920 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
921 unsigned int shndx =
922 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
923 attr_value.aux.shndx = shndx;
924 attr_value.val.refval = sec_off;
925 ref_form = true;
926 break;
928 case elfcpp::DW_FORM_block1:
929 attr_value.aux.blocklen = *pattr++;
930 attr_value.val.blockval = pattr;
931 pattr += attr_value.aux.blocklen;
932 break;
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;
938 break;
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;
944 break;
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;
950 break;
951 case elfcpp::DW_FORM_data1:
952 case elfcpp::DW_FORM_flag:
953 attr_value.val.intval = *pattr++;
954 break;
955 case elfcpp::DW_FORM_ref1:
956 attr_value.val.refval = *pattr++;
957 ref_form = true;
958 break;
959 case elfcpp::DW_FORM_data2:
960 attr_value.val.intval =
961 this->dwinfo_->read_from_pointer<16>(&pattr);
962 break;
963 case elfcpp::DW_FORM_ref2:
964 attr_value.val.refval =
965 this->dwinfo_->read_from_pointer<16>(&pattr);
966 ref_form = true;
967 break;
968 case elfcpp::DW_FORM_data4:
970 off_t sec_off;
971 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
972 unsigned int shndx =
973 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
974 attr_value.aux.shndx = shndx;
975 attr_value.val.intval = sec_off;
976 break;
978 case elfcpp::DW_FORM_ref4:
979 case elfcpp::DW_FORM_ref_sup4:
981 off_t sec_off;
982 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
983 unsigned int shndx =
984 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
985 attr_value.aux.shndx = shndx;
986 attr_value.val.refval = sec_off;
987 ref_form = true;
988 break;
990 case elfcpp::DW_FORM_data8:
992 off_t sec_off;
993 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
994 unsigned int shndx =
995 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
996 attr_value.aux.shndx = shndx;
997 attr_value.val.intval = sec_off;
998 break;
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;
1005 pattr += 16;
1006 break;
1008 case elfcpp::DW_FORM_ref_sig8:
1009 attr_value.val.uintval =
1010 this->dwinfo_->read_from_pointer<64>(&pattr);
1011 break;
1012 case elfcpp::DW_FORM_ref8:
1013 case elfcpp::DW_FORM_ref_sup8:
1015 off_t sec_off;
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;
1021 ref_form = true;
1022 break;
1024 case elfcpp::DW_FORM_ref_udata:
1025 attr_value.val.refval = read_unsigned_LEB_128(pattr, &len);
1026 ref_form = true;
1027 pattr += len;
1028 break;
1029 case elfcpp::DW_FORM_udata:
1030 attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
1031 pattr += len;
1032 break;
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);
1036 pattr += len;
1037 break;
1038 case elfcpp::DW_FORM_addrx1:
1039 attr_value.val.uintval = *pattr++;
1040 break;
1041 case elfcpp::DW_FORM_addrx2:
1042 attr_value.val.uintval =
1043 this->dwinfo_->read_from_pointer<16>(&pattr);
1044 break;
1045 case elfcpp::DW_FORM_addrx3:
1046 attr_value.val.uintval =
1047 this->dwinfo_->read_3bytes_from_pointer(&pattr);
1048 break;
1049 case elfcpp::DW_FORM_addrx4:
1050 attr_value.val.uintval =
1051 this->dwinfo_->read_from_pointer<32>(&pattr);
1052 break;
1053 case elfcpp::DW_FORM_sdata:
1054 attr_value.val.intval = read_signed_LEB_128(pattr, &len);
1055 pattr += len;
1056 break;
1057 case elfcpp::DW_FORM_string:
1058 attr_value.val.stringval = reinterpret_cast<const char*>(pattr);
1059 len = strlen(attr_value.val.stringval);
1060 pattr += len + 1;
1061 break;
1062 case elfcpp::DW_FORM_loclistx:
1063 case elfcpp::DW_FORM_rnglistx:
1064 attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
1065 pattr += len;
1066 break;
1067 default:
1068 return false;
1071 // Cache the most frequently-requested attributes.
1072 switch (attr)
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;
1084 break;
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;
1096 break;
1097 case elfcpp::DW_AT_specification:
1098 if (ref_form)
1099 this->specification_ = attr_value.val.refval;
1100 break;
1101 case elfcpp::DW_AT_abstract_origin:
1102 if (ref_form)
1103 this->abstract_origin_ = attr_value.val.refval;
1104 break;
1105 case elfcpp::DW_AT_sibling:
1106 if (ref_form && attr_value.aux.shndx == 0)
1107 this->sibling_offset_ = attr_value.val.refval;
1108 default:
1109 break;
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);
1119 else
1120 this->sibling_offset_ = this->die_offset_ + (pattr - pdie);
1122 this->attributes_read_ = true;
1123 return true;
1126 // Skip all the attributes of the DIE and return the offset of the next DIE.
1128 off_t
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_);
1135 if (pdie == NULL)
1136 return 0;
1137 const unsigned char* pattr = pdie + this->attr_offset_;
1139 for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i)
1141 size_t len;
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);
1146 pattr += len;
1148 switch(form)
1150 case elfcpp::DW_FORM_flag_present:
1151 case elfcpp::DW_FORM_implicit_const:
1152 break;
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();
1158 break;
1159 case elfcpp::DW_FORM_addr:
1160 pattr += this->dwinfo_->address_size();
1161 break;
1162 case elfcpp::DW_FORM_ref_addr:
1163 pattr += this->dwinfo_->ref_addr_size();
1164 break;
1165 case elfcpp::DW_FORM_block1:
1166 pattr += 1 + *pattr;
1167 break;
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;
1173 break;
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;
1180 break;
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;
1188 break;
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:
1195 pattr += 1;
1196 break;
1197 case elfcpp::DW_FORM_data2:
1198 case elfcpp::DW_FORM_ref2:
1199 case elfcpp::DW_FORM_strx2:
1200 case elfcpp::DW_FORM_addrx2:
1201 pattr += 2;
1202 break;
1203 case elfcpp::DW_FORM_strx3:
1204 case elfcpp::DW_FORM_addrx3:
1205 pattr += 3;
1206 break;
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:
1212 pattr += 4;
1213 break;
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:
1218 pattr += 8;
1219 break;
1220 case elfcpp::DW_FORM_data16:
1221 pattr += 16;
1222 break;
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);
1232 pattr += len;
1233 break;
1234 case elfcpp::DW_FORM_sdata:
1235 read_signed_LEB_128(pattr, &len);
1236 pattr += len;
1237 break;
1238 case elfcpp::DW_FORM_string:
1239 len = strlen(reinterpret_cast<const char*>(pattr));
1240 pattr += len + 1;
1241 break;
1242 default:
1243 return 0;
1247 return this->die_offset_ + (pattr - pdie);
1250 // Get the name of the DIE and cache it.
1252 void
1253 Dwarf_die::set_name()
1255 if (this->name_ != NULL || !this->read_attributes())
1256 return;
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.
1264 void
1265 Dwarf_die::set_linkage_name()
1267 if (this->linkage_name_ != NULL || !this->read_attributes())
1268 return;
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())
1280 return NULL;
1281 for (unsigned int i = 0; i < this->attributes_.size(); ++i)
1283 if (this->attributes_[i].attr == attr)
1284 return &this->attributes_[i];
1286 return NULL;
1289 const char*
1290 Dwarf_die::string_attribute(unsigned int attr)
1292 const Attribute_value* attr_val = this->attribute(attr);
1293 if (attr_val == NULL)
1294 return 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);
1302 default:
1303 return NULL;
1307 int64_t
1308 Dwarf_die::int_attribute(unsigned int attr)
1310 const Attribute_value* attr_val = this->attribute(attr);
1311 if (attr_val == NULL)
1312 return 0;
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;
1323 default:
1324 return 0;
1328 uint64_t
1329 Dwarf_die::uint_attribute(unsigned int attr)
1331 const Attribute_value* attr_val = this->attribute(attr);
1332 if (attr_val == NULL)
1333 return 0;
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;
1344 default:
1345 return 0;
1349 off_t
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)
1354 return -1;
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;
1374 default:
1375 return -1;
1379 off_t
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)
1384 return -1;
1386 *shndx = attr_val->aux.shndx;
1387 return attr_val->val.refval;
1390 // Return the offset of this DIE's first child.
1392 off_t
1393 Dwarf_die::child_offset()
1395 gold_assert(this->abbrev_code_ != NULL);
1396 if (!this->has_children())
1397 return 0;
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.
1405 off_t
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())
1422 return 0;
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.
1434 if (die.tag() == 0)
1435 break;
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.
1450 void
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>();
1457 #else
1458 gold_unreachable();
1459 #endif
1461 else
1463 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1464 this->do_parse<false>();
1465 #else
1466 gold_unreachable();
1467 #endif
1471 template<bool big_endian>
1472 void
1473 Dwarf_info_reader::do_parse()
1475 // Get the section contents and decompress if necessary.
1476 section_size_type buffer_size;
1477 bool buffer_is_new;
1478 this->buffer_ = this->object_->decompressed_section_contents(this->shndx_,
1479 &buffer_size,
1480 &buffer_is_new);
1481 if (this->buffer_ == NULL || buffer_size == 0)
1482 return;
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))
1506 break;
1507 uint32_t unit_length =
1508 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1509 pinfo += 4;
1510 if (unit_length == 0xffffffff)
1512 if (!this->check_buffer(pinfo + 8))
1513 break;
1514 unit_length = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1515 pinfo += 8;
1516 this->offset_size_ = 8;
1518 else
1519 this->offset_size_ = 4;
1520 if (!this->check_buffer(pinfo + unit_length))
1521 break;
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))
1525 break;
1527 // Read version (2 bytes).
1528 this->cu_version_ =
1529 elfcpp::Swap_unaligned<16, big_endian>::readval(pinfo);
1530 pinfo += 2;
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);
1542 else
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_;
1547 off_t value;
1548 abbrev_shndx =
1549 this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
1550 if (abbrev_shndx == 0)
1551 return;
1552 if (this->reloc_type_ == elfcpp::SHT_REL)
1553 abbrev_offset += value;
1554 else
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_))
1569 break;
1571 // Read type_signature (8 bytes).
1572 signature = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1573 pinfo += 8;
1575 // Read type_offset (4 or 8 bytes).
1576 if (this->offset_size_ == 4)
1577 type_offset =
1578 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1579 else
1580 type_offset =
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,
1587 abbrev_offset);
1589 // Visit the root DIE.
1590 Dwarf_die root_die(this,
1591 pinfo - (this->buffer_ + this->cu_offset_),
1592 NULL);
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,
1599 &root_die);
1600 else
1601 this->visit_compilation_unit(section_offset + this->cu_offset_,
1602 cu_end - cu_start, &root_die);
1605 // Advance to the next CU.
1606 pinfo = cu_end;
1609 if (buffer_is_new)
1611 delete[] this->buffer_;
1612 this->buffer_ = NULL;
1616 // Read the DWARF string table.
1618 bool
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")
1632 string_shndx = i;
1633 this->string_output_section_offset_ =
1634 object->output_section_offset(i);
1635 break;
1638 if (string_shndx == 0)
1639 return false;
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,
1652 &buffer_size,
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;
1657 return true;
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);
1668 else
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);
1681 else
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];
1694 else
1695 return_value = ((*source)[2] << 16) | ((*source)[1] << 8) | (*source)[0];
1696 *source += 3;
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.
1703 unsigned int
1704 Dwarf_info_reader::lookup_reloc(off_t attr_off, off_t* target_off)
1706 off_t value;
1707 attr_off += this->cu_offset_;
1708 unsigned int shndx = this->reloc_mapper_->get_reloc_target(attr_off, &value);
1709 if (shndx == 0)
1710 return 0;
1711 if (this->reloc_type_ == elfcpp::SHT_REL)
1712 *target_off += value;
1713 else
1714 *target_off = value;
1715 return shndx;
1718 // Return a string from the DWARF string table.
1720 const char*
1721 Dwarf_info_reader::get_string(off_t str_off, unsigned int string_shndx)
1723 if (!this->read_string_table(string_shndx))
1724 return NULL;
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_)
1734 return NULL;
1736 return p;
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
1743 // to use.
1745 // Process a compilation unit and parse its child DIE.
1747 void
1748 Dwarf_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die*)
1752 // Process a type unit and parse its child DIE.
1754 void
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.
1761 void
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
1773 int file_num;
1774 uint64_t address;
1775 int line_num;
1776 int column_num;
1777 unsigned int shndx; // the section address refers to
1778 bool is_stmt; // stmt means statement.
1779 bool basic_block;
1780 bool end_sequence;
1783 static void
1784 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
1786 lsm->file_num = 1;
1787 lsm->address = 0;
1788 lsm->line_num = 1;
1789 lsm->column_num = 0;
1790 lsm->shndx = -1U;
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(
1798 Object* object,
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")
1817 this->buffer_ =
1818 object->decompressed_section_contents(i, &buffer_size, &is_new);
1819 if (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")
1826 this->str_buffer_ =
1827 object->decompressed_section_contents(i, &buffer_size, &is_new);
1828 if (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)
1834 break;
1836 if (this->buffer_ == NULL)
1837 return;
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)
1849 reloc_shndx = i;
1850 this->track_relocs_type_ = reloc_sh_type;
1851 break;
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);
1864 break;
1866 if (this->symtab_buffer_ == NULL)
1867 return;
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_))
1875 return;
1877 // Now that we have successfully read all the data, parse the debug
1878 // info.
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);
1891 lineptr += 4;
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);
1899 lineptr += 8;
1901 else
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);
1911 lineptr += 2;
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.
1923 lineptr += 2;
1926 if (this->header_.offset_size == 4)
1927 this->header_.prologue_length =
1928 elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
1929 else
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;
1937 lineptr += 1;
1939 if (this->header_.version < 4)
1940 this->header_.max_ops_per_insn = 1;
1941 else
1943 // DWARF 4 added the maximum_operations_per_instruction field.
1944 this->header_.max_ops_per_insn = *lineptr;
1945 lineptr += 1;
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;
1951 lineptr += 1;
1953 this->header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
1954 lineptr += 1;
1956 this->header_.line_range = *lineptr;
1957 lineptr += 1;
1959 this->header_.opcode_base = *lineptr;
1960 lineptr += 1;
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;
1967 lineptr += 1;
1970 return 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.
1995 if (*lineptr)
1997 int dirindex = 1;
1998 while (*lineptr)
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;
2005 dirindex++;
2008 lineptr++;
2010 // It is also legal for the file entry table to be empty.
2011 if (*lineptr)
2013 int fileindex = 1;
2014 size_t len;
2015 while (*lineptr)
2017 const char* filename = reinterpret_cast<const char*>(lineptr);
2018 lineptr += strlen(filename) + 1;
2020 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
2021 lineptr += len;
2023 if (dirindex >= this->directories_.back().size())
2024 dirindex = 0;
2025 int dirindexi = static_cast<int>(dirindex);
2027 read_unsigned_LEB_128(lineptr, &len); // mod_time
2028 lineptr += len;
2030 read_unsigned_LEB_128(lineptr, &len); // filelength
2031 lineptr += len;
2033 gold_assert(fileindex
2034 == static_cast<int>(this->files_.back().size()));
2035 this->files_.back().push_back(std::make_pair(dirindexi, filename));
2036 fileindex++;
2039 lineptr++;
2041 return lineptr;
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)
2051 size_t len;
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;
2062 lineptr += 1;
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);
2070 lineptr += len;
2071 forms[i] = read_unsigned_LEB_128(lineptr, &len);
2072 lineptr += len;
2075 uint64_t entry_count = read_unsigned_LEB_128(lineptr, &len);
2076 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)
2095 uint64_t offset;
2096 if (this->header_.offset_size == 4)
2097 offset =
2098 elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
2099 else
2100 offset =
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)
2107 offset = 0;
2108 offset += it->second.second;
2110 lineptr += this->header_.offset_size;
2111 dirname = reinterpret_cast<const char*>(this->str_buffer_
2112 + offset);
2114 else
2115 return lineptr;
2117 else
2118 return lineptr;
2120 dir_list.push_back(dirname);
2123 delete[] types;
2124 delete[] forms;
2126 // Read the filenames list.
2127 format_count = *lineptr;
2128 lineptr += 1;
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);
2136 lineptr += len;
2137 forms[i] = read_unsigned_LEB_128(lineptr, &len);
2138 lineptr += len;
2141 entry_count = read_unsigned_LEB_128(lineptr, &len);
2142 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;
2150 int dirindex = 0;
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)
2163 uint64_t offset;
2164 if (this->header_.offset_size == 4)
2165 offset = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
2166 else
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)
2173 offset = 0;
2174 offset += it->second.second;
2176 lineptr += this->header_.offset_size;
2177 path = reinterpret_cast<const char*>(this->str_buffer_
2178 + offset);
2180 else
2181 return lineptr;
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);
2188 lineptr += len;
2190 else
2191 return lineptr;
2193 else
2194 return lineptr;
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));
2201 delete[] types;
2202 delete[] forms;
2204 return lineptr;
2207 // Process a single opcode in the .debug.line structure.
2209 template<int size, bool big_endian>
2210 bool
2211 Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
2212 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
2214 size_t oplen = 0;
2215 size_t templen;
2216 unsigned char opcode = *start;
2217 oplen++;
2218 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;
2233 *len = oplen;
2234 return true;
2237 // Otherwise, we have the regular opcodes
2238 switch (opcode)
2240 case elfcpp::DW_LNS_copy:
2241 lsm->basic_block = false;
2242 *len = oplen;
2243 return true;
2245 case elfcpp::DW_LNS_advance_pc:
2247 const uint64_t advance_address
2248 = read_unsigned_LEB_128(start, &templen);
2249 oplen += templen;
2250 lsm->address += this->header_.min_insn_length * advance_address;
2252 break;
2254 case elfcpp::DW_LNS_advance_line:
2256 const int64_t advance_line = read_signed_LEB_128(start, &templen);
2257 oplen += templen;
2258 lsm->line_num += advance_line;
2260 break;
2262 case elfcpp::DW_LNS_set_file:
2264 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
2265 oplen += templen;
2266 lsm->file_num = fileno;
2268 break;
2270 case elfcpp::DW_LNS_set_column:
2272 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
2273 oplen += templen;
2274 lsm->column_num = colno;
2276 break;
2278 case elfcpp::DW_LNS_negate_stmt:
2279 lsm->is_stmt = !lsm->is_stmt;
2280 break;
2282 case elfcpp::DW_LNS_set_basic_block:
2283 lsm->basic_block = true;
2284 break;
2286 case elfcpp::DW_LNS_fixed_advance_pc:
2288 int advance_address;
2289 advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
2290 oplen += 2;
2291 lsm->address += advance_address;
2293 break;
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;
2302 break;
2304 case elfcpp::DW_LNS_extended_op:
2306 const uint64_t extended_op_len
2307 = read_unsigned_LEB_128(start, &templen);
2308 start += templen;
2309 oplen += templen + extended_op_len;
2311 const unsigned char extended_op = *start;
2312 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.
2320 lsm->line_num = -1;
2321 lsm->end_sequence = true;
2322 *len = oplen;
2323 return true;
2325 case elfcpp::DW_LNE_set_address:
2327 lsm->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
2337 // symbol value.
2338 if (this->track_relocs_type_ == elfcpp::SHT_RELA)
2339 lsm->address = 0;
2340 // Add in the symbol value.
2341 lsm->address += it->second.second;
2342 lsm->shndx = it->second.first;
2344 else
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;
2351 break;
2353 case elfcpp::DW_LNE_define_file:
2355 const char* filename = reinterpret_cast<const char*>(start);
2356 templen = strlen(filename) + 1;
2357 start += templen;
2359 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
2361 if (dirindex >= this->directories_.back().size())
2362 dirindex = 0;
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
2369 // them at all.
2371 this->files_.back().push_back(std::make_pair(dirindexi,
2372 filename));
2374 break;
2377 break;
2379 default:
2381 // Ignore unknown opcode silently
2382 for (int i = 0; i < this->header_.std_opcode_lengths[opcode]; i++)
2384 size_t templen;
2385 read_unsigned_LEB_128(start, &templen);
2386 start += templen;
2387 oplen += templen;
2390 break;
2392 *len = oplen;
2393 return false;
2396 // Read the debug information at LINEPTR and store it in the line
2397 // number map.
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,
2403 unsigned int shndx)
2405 struct LineStateMachine lsm;
2407 while (lineptr < endptr)
2409 ResetLineStateMachine(&lsm, this->header_.default_is_stmt);
2410 while (!lsm.end_sequence)
2412 size_t oplength;
2414 if (lineptr >= endptr)
2415 break;
2417 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
2418 lineptr += oplength;
2420 if (add_line
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.
2433 if (!map.empty()
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);
2443 return endptr;
2446 // Read the relocations into a Reloc_map.
2448 template<int size, bool big_endian>
2449 void
2450 Sized_dwarf_line_info<size, big_endian>::read_relocs()
2452 if (this->symtab_buffer_ == NULL)
2453 return;
2455 off_t value;
2456 off_t reloc_offset;
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.
2464 if (shndx != 0)
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>
2474 void
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();
2500 ++it)
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
2509 // information.
2511 template<int size, bool big_endian>
2512 bool
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,
2527 off_t offset)
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)
2612 ++it;
2613 if (it == offsets->end() || it->offset != offset)
2614 return offsets->end();
2615 else
2616 return it;
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;
2623 --it;
2624 const off_t range_value = it->offset;
2625 while (it != offsets->begin() && (it-1)->offset == range_value)
2626 --it;
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
2632 // it's present.
2633 for (; it != range_end; ++it)
2634 if (it->line_num != -1)
2635 return it;
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>
2644 std::string
2645 Sized_dwarf_line_info<size, big_endian>::do_addr2line(
2646 unsigned int shndx,
2647 off_t offset,
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)
2654 return "";
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];
2662 else
2663 offsets = &this->line_number_map_[-1U];
2664 if (offsets->empty())
2665 return "";
2667 typename std::vector<Offset_to_lineno_entry>::const_iterator it
2668 = offset_to_iterator(offsets, offset);
2669 if (it == offsets->end())
2670 return "";
2672 std::string result = this->format_file_lineno(*it);
2673 gold_debug(DEBUG_LOCATION, "do_addr2line: canonical result: %s",
2674 result.c_str());
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)
2696 break;
2700 return result;
2703 // Convert the file_num + line_num into a string.
2705 template<int size, bool big_endian>
2706 std::string
2707 Sized_dwarf_line_info<size, big_endian>::format_file_lineno(
2708 const Offset_to_lineno_entry& loc) const
2710 std::string ret;
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())
2727 ret += dirname;
2728 ret += "/";
2730 ret += filename;
2731 if (ret.empty())
2732 ret = "(unknown)";
2734 char buffer[64]; // enough to hold a line number
2735 snprintf(buffer, sizeof(buffer), "%d", loc.line_num);
2736 ret += ":";
2737 ret += buffer;
2739 return ret;
2742 // Dwarf_line_info routines.
2744 static unsigned int next_generation_count = 0;
2746 struct Addr2line_cache_entry
2748 Object* object;
2749 unsigned int shndx;
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;
2766 std::string
2767 Dwarf_line_info::one_addr2line(Object* object,
2768 unsigned int shndx, off_t offset,
2769 size_t cache_size,
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)
2787 ++it->access_count;
2788 break;
2792 // If we don't hit the cache, create a new object and insert into the
2793 // cache.
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;
2801 #endif
2802 #ifdef HAVE_TARGET_32_BIG
2803 case Parameters::TARGET_32_BIG:
2804 lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
2805 #endif
2806 #ifdef HAVE_TARGET_64_LITTLE
2807 case Parameters::TARGET_64_LITTLE:
2808 lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
2809 #endif
2810 #ifdef HAVE_TARGET_64_BIG
2811 case Parameters::TARGET_64_BIG:
2812 lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
2813 #endif
2814 default:
2815 gold_unreachable();
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;
2838 lowest = it;
2841 if (lowest != addr2line_cache.end())
2843 delete lowest->dwarf_line_info;
2844 addr2line_cache.erase(lowest);
2848 return retval;
2851 void
2852 Dwarf_line_info::clear_addr2line_cache()
2854 for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
2855 it != addr2line_cache.end();
2856 ++it)
2857 delete it->dwarf_line_info;
2858 addr2line_cache.clear();
2861 #ifdef HAVE_TARGET_32_LITTLE
2862 template
2863 class Sized_dwarf_line_info<32, false>;
2864 #endif
2866 #ifdef HAVE_TARGET_32_BIG
2867 template
2868 class Sized_dwarf_line_info<32, true>;
2869 #endif
2871 #ifdef HAVE_TARGET_64_LITTLE
2872 template
2873 class Sized_dwarf_line_info<64, false>;
2874 #endif
2876 #ifdef HAVE_TARGET_64_BIG
2877 template
2878 class Sized_dwarf_line_info<64, true>;
2879 #endif
2881 } // End namespace gold.