]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/dwarf_reader.cc
Strip .debug_gnu_pubnames and .debug_gnu_pubtypes when building .gdb_index.
[thirdparty/binutils-gdb.git] / gold / dwarf_reader.cc
1 // dwarf_reader.cc -- parse dwarf2/3 debug information
2
3 // Copyright (C) 2007-2014 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <algorithm>
26 #include <utility>
27 #include <vector>
28
29 #include "elfcpp_swap.h"
30 #include "dwarf.h"
31 #include "object.h"
32 #include "reloc.h"
33 #include "dwarf_reader.h"
34 #include "int_encoding.h"
35 #include "compressed_output.h"
36
37 namespace gold {
38
39 // Class Sized_elf_reloc_mapper
40
41 // Initialize the relocation tracker for section RELOC_SHNDX.
42
43 template<int size, bool big_endian>
44 bool
45 Sized_elf_reloc_mapper<size, big_endian>::do_initialize(
46 unsigned int reloc_shndx, unsigned int reloc_type)
47 {
48 this->reloc_type_ = reloc_type;
49 return this->track_relocs_.initialize(this->object_, reloc_shndx,
50 reloc_type);
51 }
52
53 // Looks in the symtab to see what section a symbol is in.
54
55 template<int size, bool big_endian>
56 unsigned int
57 Sized_elf_reloc_mapper<size, big_endian>::symbol_section(
58 unsigned int symndx, Address* value, bool* is_ordinary)
59 {
60 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
61 gold_assert(static_cast<off_t>((symndx + 1) * symsize) <= this->symtab_size_);
62 elfcpp::Sym<size, big_endian> elfsym(this->symtab_ + symndx * symsize);
63 *value = elfsym.get_st_value();
64 return this->object_->adjust_sym_shndx(symndx, elfsym.get_st_shndx(),
65 is_ordinary);
66 }
67
68 // Return the section index and offset within the section of
69 // the target of the relocation for RELOC_OFFSET.
70
71 template<int size, bool big_endian>
72 unsigned int
73 Sized_elf_reloc_mapper<size, big_endian>::do_get_reloc_target(
74 off_t reloc_offset, off_t* target_offset)
75 {
76 this->track_relocs_.advance(reloc_offset);
77 if (reloc_offset != this->track_relocs_.next_offset())
78 return 0;
79 unsigned int symndx = this->track_relocs_.next_symndx();
80 typename elfcpp::Elf_types<size>::Elf_Addr value;
81 bool is_ordinary;
82 unsigned int target_shndx = this->symbol_section(symndx, &value,
83 &is_ordinary);
84 if (!is_ordinary)
85 return 0;
86 if (this->reloc_type_ == elfcpp::SHT_RELA)
87 value += this->track_relocs_.next_addend();
88 *target_offset = value;
89 return target_shndx;
90 }
91
92 static inline Elf_reloc_mapper*
93 make_elf_reloc_mapper(Relobj* object, const unsigned char* symtab,
94 off_t symtab_size)
95 {
96 if (object->elfsize() == 32)
97 {
98 if (object->is_big_endian())
99 {
100 #ifdef HAVE_TARGET_32_BIG
101 return new Sized_elf_reloc_mapper<32, true>(object, symtab,
102 symtab_size);
103 #else
104 gold_unreachable();
105 #endif
106 }
107 else
108 {
109 #ifdef HAVE_TARGET_32_LITTLE
110 return new Sized_elf_reloc_mapper<32, false>(object, symtab,
111 symtab_size);
112 #else
113 gold_unreachable();
114 #endif
115 }
116 }
117 else if (object->elfsize() == 64)
118 {
119 if (object->is_big_endian())
120 {
121 #ifdef HAVE_TARGET_64_BIG
122 return new Sized_elf_reloc_mapper<64, true>(object, symtab,
123 symtab_size);
124 #else
125 gold_unreachable();
126 #endif
127 }
128 else
129 {
130 #ifdef HAVE_TARGET_64_LITTLE
131 return new Sized_elf_reloc_mapper<64, false>(object, symtab,
132 symtab_size);
133 #else
134 gold_unreachable();
135 #endif
136 }
137 }
138 else
139 gold_unreachable();
140 }
141
142 // class Dwarf_abbrev_table
143
144 void
145 Dwarf_abbrev_table::clear_abbrev_codes()
146 {
147 for (unsigned int code = 0; code < this->low_abbrev_code_max_; ++code)
148 {
149 if (this->low_abbrev_codes_[code] != NULL)
150 {
151 delete this->low_abbrev_codes_[code];
152 this->low_abbrev_codes_[code] = NULL;
153 }
154 }
155 for (Abbrev_code_table::iterator it = this->high_abbrev_codes_.begin();
156 it != this->high_abbrev_codes_.end();
157 ++it)
158 {
159 if (it->second != NULL)
160 delete it->second;
161 }
162 this->high_abbrev_codes_.clear();
163 }
164
165 // Read the abbrev table from an object file.
166
167 bool
168 Dwarf_abbrev_table::do_read_abbrevs(
169 Relobj* object,
170 unsigned int abbrev_shndx,
171 off_t abbrev_offset)
172 {
173 this->clear_abbrev_codes();
174
175 // If we don't have relocations, abbrev_shndx will be 0, and
176 // we'll have to hunt for the .debug_abbrev section.
177 if (abbrev_shndx == 0 && this->abbrev_shndx_ > 0)
178 abbrev_shndx = this->abbrev_shndx_;
179 else if (abbrev_shndx == 0)
180 {
181 for (unsigned int i = 1; i < object->shnum(); ++i)
182 {
183 std::string name = object->section_name(i);
184 if (name == ".debug_abbrev" || name == ".zdebug_abbrev")
185 {
186 abbrev_shndx = i;
187 // Correct the offset. For incremental update links, we have a
188 // relocated offset that is relative to the output section, but
189 // here we need an offset relative to the input section.
190 abbrev_offset -= object->output_section_offset(i);
191 break;
192 }
193 }
194 if (abbrev_shndx == 0)
195 return false;
196 }
197
198 // Get the section contents and decompress if necessary.
199 if (abbrev_shndx != this->abbrev_shndx_)
200 {
201 if (this->owns_buffer_ && this->buffer_ != NULL)
202 {
203 delete[] this->buffer_;
204 this->owns_buffer_ = false;
205 }
206
207 section_size_type buffer_size;
208 this->buffer_ =
209 object->decompressed_section_contents(abbrev_shndx,
210 &buffer_size,
211 &this->owns_buffer_);
212 this->buffer_end_ = this->buffer_ + buffer_size;
213 this->abbrev_shndx_ = abbrev_shndx;
214 }
215
216 this->buffer_pos_ = this->buffer_ + abbrev_offset;
217 return true;
218 }
219
220 // Lookup the abbrev code entry for CODE. This function is called
221 // only when the abbrev code is not in the direct lookup table.
222 // It may be in the hash table, it may not have been read yet,
223 // or it may not exist in the abbrev table.
224
225 const Dwarf_abbrev_table::Abbrev_code*
226 Dwarf_abbrev_table::do_get_abbrev(unsigned int code)
227 {
228 // See if the abbrev code is already in the hash table.
229 Abbrev_code_table::const_iterator it = this->high_abbrev_codes_.find(code);
230 if (it != this->high_abbrev_codes_.end())
231 return it->second;
232
233 // Read and store abbrev code definitions until we find the
234 // one we're looking for.
235 for (;;)
236 {
237 // Read the abbrev code. A zero here indicates the end of the
238 // abbrev table.
239 size_t len;
240 if (this->buffer_pos_ >= this->buffer_end_)
241 return NULL;
242 uint64_t nextcode = read_unsigned_LEB_128(this->buffer_pos_, &len);
243 if (nextcode == 0)
244 {
245 this->buffer_pos_ = this->buffer_end_;
246 return NULL;
247 }
248 this->buffer_pos_ += len;
249
250 // Read the tag.
251 if (this->buffer_pos_ >= this->buffer_end_)
252 return NULL;
253 uint64_t tag = read_unsigned_LEB_128(this->buffer_pos_, &len);
254 this->buffer_pos_ += len;
255
256 // Read the has_children flag.
257 if (this->buffer_pos_ >= this->buffer_end_)
258 return NULL;
259 bool has_children = *this->buffer_pos_ == elfcpp::DW_CHILDREN_yes;
260 this->buffer_pos_ += 1;
261
262 // Read the list of (attribute, form) pairs.
263 Abbrev_code* entry = new Abbrev_code(tag, has_children);
264 for (;;)
265 {
266 // Read the attribute.
267 if (this->buffer_pos_ >= this->buffer_end_)
268 return NULL;
269 uint64_t attr = read_unsigned_LEB_128(this->buffer_pos_, &len);
270 this->buffer_pos_ += len;
271
272 // Read the form.
273 if (this->buffer_pos_ >= this->buffer_end_)
274 return NULL;
275 uint64_t form = read_unsigned_LEB_128(this->buffer_pos_, &len);
276 this->buffer_pos_ += len;
277
278 // A (0,0) pair terminates the list.
279 if (attr == 0 && form == 0)
280 break;
281
282 if (attr == elfcpp::DW_AT_sibling)
283 entry->has_sibling_attribute = true;
284
285 entry->add_attribute(attr, form);
286 }
287
288 this->store_abbrev(nextcode, entry);
289 if (nextcode == code)
290 return entry;
291 }
292
293 return NULL;
294 }
295
296 // class Dwarf_ranges_table
297
298 // Read the ranges table from an object file.
299
300 bool
301 Dwarf_ranges_table::read_ranges_table(
302 Relobj* object,
303 const unsigned char* symtab,
304 off_t symtab_size,
305 unsigned int ranges_shndx)
306 {
307 // If we've already read this abbrev table, return immediately.
308 if (this->ranges_shndx_ > 0
309 && this->ranges_shndx_ == ranges_shndx)
310 return true;
311
312 // If we don't have relocations, ranges_shndx will be 0, and
313 // we'll have to hunt for the .debug_ranges section.
314 if (ranges_shndx == 0 && this->ranges_shndx_ > 0)
315 ranges_shndx = this->ranges_shndx_;
316 else if (ranges_shndx == 0)
317 {
318 for (unsigned int i = 1; i < object->shnum(); ++i)
319 {
320 std::string name = object->section_name(i);
321 if (name == ".debug_ranges" || name == ".zdebug_ranges")
322 {
323 ranges_shndx = i;
324 this->output_section_offset_ = object->output_section_offset(i);
325 break;
326 }
327 }
328 if (ranges_shndx == 0)
329 return false;
330 }
331
332 // Get the section contents and decompress if necessary.
333 if (ranges_shndx != this->ranges_shndx_)
334 {
335 if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
336 {
337 delete[] this->ranges_buffer_;
338 this->owns_ranges_buffer_ = false;
339 }
340
341 section_size_type buffer_size;
342 this->ranges_buffer_ =
343 object->decompressed_section_contents(ranges_shndx,
344 &buffer_size,
345 &this->owns_ranges_buffer_);
346 this->ranges_buffer_end_ = this->ranges_buffer_ + buffer_size;
347 this->ranges_shndx_ = ranges_shndx;
348 }
349
350 if (this->ranges_reloc_mapper_ != NULL)
351 {
352 delete this->ranges_reloc_mapper_;
353 this->ranges_reloc_mapper_ = NULL;
354 }
355
356 // For incremental objects, we have no relocations.
357 if (object->is_incremental())
358 return true;
359
360 // Find the relocation section for ".debug_ranges".
361 unsigned int reloc_shndx = 0;
362 unsigned int reloc_type = 0;
363 for (unsigned int i = 0; i < object->shnum(); ++i)
364 {
365 reloc_type = object->section_type(i);
366 if ((reloc_type == elfcpp::SHT_REL
367 || reloc_type == elfcpp::SHT_RELA)
368 && object->section_info(i) == ranges_shndx)
369 {
370 reloc_shndx = i;
371 break;
372 }
373 }
374
375 this->ranges_reloc_mapper_ = make_elf_reloc_mapper(object, symtab,
376 symtab_size);
377 this->ranges_reloc_mapper_->initialize(reloc_shndx, reloc_type);
378 this->reloc_type_ = reloc_type;
379
380 return true;
381 }
382
383 // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
384
385 Dwarf_range_list*
386 Dwarf_ranges_table::read_range_list(
387 Relobj* object,
388 const unsigned char* symtab,
389 off_t symtab_size,
390 unsigned int addr_size,
391 unsigned int ranges_shndx,
392 off_t offset)
393 {
394 Dwarf_range_list* ranges;
395
396 if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx))
397 return NULL;
398
399 // Correct the offset. For incremental update links, we have a
400 // relocated offset that is relative to the output section, but
401 // here we need an offset relative to the input section.
402 offset -= this->output_section_offset_;
403
404 // Read the range list at OFFSET.
405 ranges = new Dwarf_range_list();
406 off_t base = 0;
407 for (;
408 this->ranges_buffer_ + offset < this->ranges_buffer_end_;
409 offset += 2 * addr_size)
410 {
411 off_t start;
412 off_t end;
413
414 // Read the raw contents of the section.
415 if (addr_size == 4)
416 {
417 start = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
418 + offset);
419 end = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
420 + offset + 4);
421 }
422 else
423 {
424 start = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
425 + offset);
426 end = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
427 + offset + 8);
428 }
429
430 // Check for relocations and adjust the values.
431 unsigned int shndx1 = 0;
432 unsigned int shndx2 = 0;
433 if (this->ranges_reloc_mapper_ != NULL)
434 {
435 shndx1 = this->lookup_reloc(offset, &start);
436 shndx2 = this->lookup_reloc(offset + addr_size, &end);
437 }
438
439 // End of list is marked by a pair of zeroes.
440 if (shndx1 == 0 && start == 0 && end == 0)
441 break;
442
443 // A "base address selection entry" is identified by
444 // 0xffffffff for the first value of the pair. The second
445 // value is used as a base for subsequent range list entries.
446 if (shndx1 == 0 && start == -1)
447 base = end;
448 else if (shndx1 == shndx2)
449 {
450 if (shndx1 == 0 || object->is_section_included(shndx1))
451 ranges->add(shndx1, base + start, base + end);
452 }
453 else
454 gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
455 "range list entry are in different sections"),
456 object->name().c_str());
457 }
458
459 return ranges;
460 }
461
462 // Look for a relocation at offset OFF in the range table,
463 // and return the section index and offset of the target.
464
465 unsigned int
466 Dwarf_ranges_table::lookup_reloc(off_t off, off_t* target_off)
467 {
468 off_t value;
469 unsigned int shndx =
470 this->ranges_reloc_mapper_->get_reloc_target(off, &value);
471 if (shndx == 0)
472 return 0;
473 if (this->reloc_type_ == elfcpp::SHT_REL)
474 *target_off += value;
475 else
476 *target_off = value;
477 return shndx;
478 }
479
480 // class Dwarf_pubnames_table
481
482 // Read the pubnames section from the object file.
483
484 bool
485 Dwarf_pubnames_table::read_section(Relobj* object, const unsigned char* symtab,
486 off_t symtab_size)
487 {
488 section_size_type buffer_size;
489 unsigned int shndx = 0;
490 const char* name = this->is_pubtypes_ ? "pubtypes" : "pubnames";
491 const char* gnu_name = (this->is_pubtypes_
492 ? "gnu_pubtypes"
493 : "gnu_pubnames");
494
495 for (unsigned int i = 1; i < object->shnum(); ++i)
496 {
497 std::string section_name = object->section_name(i);
498 const char* section_name_suffix = section_name.c_str();
499 if (is_prefix_of(".debug_", section_name_suffix))
500 section_name_suffix += 7;
501 else if (is_prefix_of(".zdebug_", section_name_suffix))
502 section_name_suffix += 8;
503 else
504 continue;
505 if (strcmp(section_name_suffix, name) == 0)
506 {
507 shndx = i;
508 break;
509 }
510 else if (strcmp(section_name_suffix, gnu_name) == 0)
511 {
512 shndx = i;
513 this->is_gnu_style_ = true;
514 break;
515 }
516 }
517 if (shndx == 0)
518 return false;
519
520 this->buffer_ = object->decompressed_section_contents(shndx,
521 &buffer_size,
522 &this->owns_buffer_);
523 if (this->buffer_ == NULL)
524 return false;
525 this->buffer_end_ = this->buffer_ + buffer_size;
526
527 // For incremental objects, we have no relocations.
528 if (object->is_incremental())
529 return true;
530
531 // Find the relocation section
532 unsigned int reloc_shndx = 0;
533 unsigned int reloc_type = 0;
534 for (unsigned int i = 0; i < object->shnum(); ++i)
535 {
536 reloc_type = object->section_type(i);
537 if ((reloc_type == elfcpp::SHT_REL
538 || reloc_type == elfcpp::SHT_RELA)
539 && object->section_info(i) == shndx)
540 {
541 reloc_shndx = i;
542 break;
543 }
544 }
545
546 this->reloc_mapper_ = make_elf_reloc_mapper(object, symtab, symtab_size);
547 this->reloc_mapper_->initialize(reloc_shndx, reloc_type);
548 this->reloc_type_ = reloc_type;
549
550 return true;
551 }
552
553 // Read the header for the set at OFFSET.
554
555 bool
556 Dwarf_pubnames_table::read_header(off_t offset)
557 {
558 // Make sure we have actually read the section.
559 gold_assert(this->buffer_ != NULL);
560
561 if (offset < 0 || offset + 14 >= this->buffer_end_ - this->buffer_)
562 return false;
563
564 const unsigned char* pinfo = this->buffer_ + offset;
565
566 // Read the unit_length field.
567 uint64_t unit_length = this->dwinfo_->read_from_pointer<32>(pinfo);
568 pinfo += 4;
569 if (unit_length == 0xffffffff)
570 {
571 unit_length = this->dwinfo_->read_from_pointer<64>(pinfo);
572 this->unit_length_ = unit_length + 12;
573 pinfo += 8;
574 this->offset_size_ = 8;
575 }
576 else
577 {
578 this->unit_length_ = unit_length + 4;
579 this->offset_size_ = 4;
580 }
581 this->end_of_table_ = pinfo + unit_length;
582
583 // Check the version.
584 unsigned int version = this->dwinfo_->read_from_pointer<16>(pinfo);
585 pinfo += 2;
586 if (version != 2)
587 return false;
588
589 this->reloc_mapper_->get_reloc_target(pinfo - this->buffer_,
590 &this->cu_offset_);
591
592 // Skip the debug_info_offset and debug_info_size fields.
593 pinfo += 2 * this->offset_size_;
594
595 if (pinfo >= this->buffer_end_)
596 return false;
597
598 this->pinfo_ = pinfo;
599 return true;
600 }
601
602 // Read the next name from the set.
603
604 const char*
605 Dwarf_pubnames_table::next_name(uint8_t* flag_byte)
606 {
607 const unsigned char* pinfo = this->pinfo_;
608
609 // Check for end of list. The table should be terminated by an
610 // entry containing nothing but a DIE offset of 0.
611 if (pinfo + this->offset_size_ >= this->end_of_table_)
612 return NULL;
613
614 // Skip the offset within the CU. If this is zero, but we're not
615 // at the end of the table, then we have a real pubnames entry
616 // whose DIE offset is 0 (likely to be a GCC bug). Since we
617 // don't actually use the DIE offset in building .gdb_index,
618 // it's harmless.
619 pinfo += this->offset_size_;
620
621 if (this->is_gnu_style_)
622 *flag_byte = *pinfo++;
623 else
624 *flag_byte = 0;
625
626 // Return a pointer to the string at the current location,
627 // and advance the pointer to the next entry.
628 const char* ret = reinterpret_cast<const char*>(pinfo);
629 while (pinfo < this->buffer_end_ && *pinfo != '\0')
630 ++pinfo;
631 if (pinfo < this->buffer_end_)
632 ++pinfo;
633
634 this->pinfo_ = pinfo;
635 return ret;
636 }
637
638 // class Dwarf_die
639
640 Dwarf_die::Dwarf_die(
641 Dwarf_info_reader* dwinfo,
642 off_t die_offset,
643 Dwarf_die* parent)
644 : dwinfo_(dwinfo), parent_(parent), die_offset_(die_offset),
645 child_offset_(0), sibling_offset_(0), abbrev_code_(NULL), attributes_(),
646 attributes_read_(false), name_(NULL), name_off_(-1), linkage_name_(NULL),
647 linkage_name_off_(-1), string_shndx_(0), specification_(0),
648 abstract_origin_(0)
649 {
650 size_t len;
651 const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset);
652 if (pdie == NULL)
653 return;
654 unsigned int code = read_unsigned_LEB_128(pdie, &len);
655 if (code == 0)
656 {
657 if (parent != NULL)
658 parent->set_sibling_offset(die_offset + len);
659 return;
660 }
661 this->attr_offset_ = len;
662
663 // Lookup the abbrev code in the abbrev table.
664 this->abbrev_code_ = dwinfo->get_abbrev(code);
665 }
666
667 // Read all the attributes of the DIE.
668
669 bool
670 Dwarf_die::read_attributes()
671 {
672 if (this->attributes_read_)
673 return true;
674
675 gold_assert(this->abbrev_code_ != NULL);
676
677 const unsigned char* pdie =
678 this->dwinfo_->buffer_at_offset(this->die_offset_);
679 if (pdie == NULL)
680 return false;
681 const unsigned char* pattr = pdie + this->attr_offset_;
682
683 unsigned int nattr = this->abbrev_code_->attributes.size();
684 this->attributes_.reserve(nattr);
685 for (unsigned int i = 0; i < nattr; ++i)
686 {
687 size_t len;
688 unsigned int attr = this->abbrev_code_->attributes[i].attr;
689 unsigned int form = this->abbrev_code_->attributes[i].form;
690 if (form == elfcpp::DW_FORM_indirect)
691 {
692 form = read_unsigned_LEB_128(pattr, &len);
693 pattr += len;
694 }
695 off_t attr_off = this->die_offset_ + (pattr - pdie);
696 bool ref_form = false;
697 Attribute_value attr_value;
698 attr_value.attr = attr;
699 attr_value.form = form;
700 attr_value.aux.shndx = 0;
701 switch(form)
702 {
703 case elfcpp::DW_FORM_flag_present:
704 attr_value.val.intval = 1;
705 break;
706 case elfcpp::DW_FORM_strp:
707 {
708 off_t str_off;
709 if (this->dwinfo_->offset_size() == 4)
710 str_off = this->dwinfo_->read_from_pointer<32>(&pattr);
711 else
712 str_off = this->dwinfo_->read_from_pointer<64>(&pattr);
713 unsigned int shndx =
714 this->dwinfo_->lookup_reloc(attr_off, &str_off);
715 attr_value.aux.shndx = shndx;
716 attr_value.val.refval = str_off;
717 break;
718 }
719 case elfcpp::DW_FORM_sec_offset:
720 {
721 off_t sec_off;
722 if (this->dwinfo_->offset_size() == 4)
723 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
724 else
725 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
726 unsigned int shndx =
727 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
728 attr_value.aux.shndx = shndx;
729 attr_value.val.refval = sec_off;
730 ref_form = true;
731 break;
732 }
733 case elfcpp::DW_FORM_addr:
734 case elfcpp::DW_FORM_ref_addr:
735 {
736 off_t sec_off;
737 if (this->dwinfo_->address_size() == 4)
738 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
739 else
740 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
741 unsigned int shndx =
742 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
743 attr_value.aux.shndx = shndx;
744 attr_value.val.refval = sec_off;
745 ref_form = true;
746 break;
747 }
748 case elfcpp::DW_FORM_block1:
749 attr_value.aux.blocklen = *pattr++;
750 attr_value.val.blockval = pattr;
751 pattr += attr_value.aux.blocklen;
752 break;
753 case elfcpp::DW_FORM_block2:
754 attr_value.aux.blocklen =
755 this->dwinfo_->read_from_pointer<16>(&pattr);
756 attr_value.val.blockval = pattr;
757 pattr += attr_value.aux.blocklen;
758 break;
759 case elfcpp::DW_FORM_block4:
760 attr_value.aux.blocklen =
761 this->dwinfo_->read_from_pointer<32>(&pattr);
762 attr_value.val.blockval = pattr;
763 pattr += attr_value.aux.blocklen;
764 break;
765 case elfcpp::DW_FORM_block:
766 case elfcpp::DW_FORM_exprloc:
767 attr_value.aux.blocklen = read_unsigned_LEB_128(pattr, &len);
768 attr_value.val.blockval = pattr + len;
769 pattr += len + attr_value.aux.blocklen;
770 break;
771 case elfcpp::DW_FORM_data1:
772 case elfcpp::DW_FORM_flag:
773 attr_value.val.intval = *pattr++;
774 break;
775 case elfcpp::DW_FORM_ref1:
776 attr_value.val.refval = *pattr++;
777 ref_form = true;
778 break;
779 case elfcpp::DW_FORM_data2:
780 attr_value.val.intval =
781 this->dwinfo_->read_from_pointer<16>(&pattr);
782 break;
783 case elfcpp::DW_FORM_ref2:
784 attr_value.val.refval =
785 this->dwinfo_->read_from_pointer<16>(&pattr);
786 ref_form = true;
787 break;
788 case elfcpp::DW_FORM_data4:
789 {
790 off_t sec_off;
791 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
792 unsigned int shndx =
793 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
794 attr_value.aux.shndx = shndx;
795 attr_value.val.intval = sec_off;
796 break;
797 }
798 case elfcpp::DW_FORM_ref4:
799 {
800 off_t sec_off;
801 sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
802 unsigned int shndx =
803 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
804 attr_value.aux.shndx = shndx;
805 attr_value.val.refval = sec_off;
806 ref_form = true;
807 break;
808 }
809 case elfcpp::DW_FORM_data8:
810 {
811 off_t sec_off;
812 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
813 unsigned int shndx =
814 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
815 attr_value.aux.shndx = shndx;
816 attr_value.val.intval = sec_off;
817 break;
818 }
819 case elfcpp::DW_FORM_ref_sig8:
820 attr_value.val.uintval =
821 this->dwinfo_->read_from_pointer<64>(&pattr);
822 break;
823 case elfcpp::DW_FORM_ref8:
824 {
825 off_t sec_off;
826 sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
827 unsigned int shndx =
828 this->dwinfo_->lookup_reloc(attr_off, &sec_off);
829 attr_value.aux.shndx = shndx;
830 attr_value.val.refval = sec_off;
831 ref_form = true;
832 break;
833 }
834 case elfcpp::DW_FORM_ref_udata:
835 attr_value.val.refval = read_unsigned_LEB_128(pattr, &len);
836 ref_form = true;
837 pattr += len;
838 break;
839 case elfcpp::DW_FORM_udata:
840 case elfcpp::DW_FORM_GNU_addr_index:
841 case elfcpp::DW_FORM_GNU_str_index:
842 attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
843 pattr += len;
844 break;
845 case elfcpp::DW_FORM_sdata:
846 attr_value.val.intval = read_signed_LEB_128(pattr, &len);
847 pattr += len;
848 break;
849 case elfcpp::DW_FORM_string:
850 attr_value.val.stringval = reinterpret_cast<const char*>(pattr);
851 len = strlen(attr_value.val.stringval);
852 pattr += len + 1;
853 break;
854 default:
855 return false;
856 }
857
858 // Cache the most frequently-requested attributes.
859 switch (attr)
860 {
861 case elfcpp::DW_AT_name:
862 if (form == elfcpp::DW_FORM_string)
863 this->name_ = attr_value.val.stringval;
864 else if (form == elfcpp::DW_FORM_strp)
865 {
866 // All indirect strings should refer to the same
867 // string section, so we just save the last one seen.
868 this->string_shndx_ = attr_value.aux.shndx;
869 this->name_off_ = attr_value.val.refval;
870 }
871 break;
872 case elfcpp::DW_AT_linkage_name:
873 case elfcpp::DW_AT_MIPS_linkage_name:
874 if (form == elfcpp::DW_FORM_string)
875 this->linkage_name_ = attr_value.val.stringval;
876 else if (form == elfcpp::DW_FORM_strp)
877 {
878 // All indirect strings should refer to the same
879 // string section, so we just save the last one seen.
880 this->string_shndx_ = attr_value.aux.shndx;
881 this->linkage_name_off_ = attr_value.val.refval;
882 }
883 break;
884 case elfcpp::DW_AT_specification:
885 if (ref_form)
886 this->specification_ = attr_value.val.refval;
887 break;
888 case elfcpp::DW_AT_abstract_origin:
889 if (ref_form)
890 this->abstract_origin_ = attr_value.val.refval;
891 break;
892 case elfcpp::DW_AT_sibling:
893 if (ref_form && attr_value.aux.shndx == 0)
894 this->sibling_offset_ = attr_value.val.refval;
895 default:
896 break;
897 }
898
899 this->attributes_.push_back(attr_value);
900 }
901
902 // Now that we know where the next DIE begins, record the offset
903 // to avoid later recalculation.
904 if (this->has_children())
905 this->child_offset_ = this->die_offset_ + (pattr - pdie);
906 else
907 this->sibling_offset_ = this->die_offset_ + (pattr - pdie);
908
909 this->attributes_read_ = true;
910 return true;
911 }
912
913 // Skip all the attributes of the DIE and return the offset of the next DIE.
914
915 off_t
916 Dwarf_die::skip_attributes()
917 {
918 gold_assert(this->abbrev_code_ != NULL);
919
920 const unsigned char* pdie =
921 this->dwinfo_->buffer_at_offset(this->die_offset_);
922 if (pdie == NULL)
923 return 0;
924 const unsigned char* pattr = pdie + this->attr_offset_;
925
926 for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i)
927 {
928 size_t len;
929 unsigned int form = this->abbrev_code_->attributes[i].form;
930 if (form == elfcpp::DW_FORM_indirect)
931 {
932 form = read_unsigned_LEB_128(pattr, &len);
933 pattr += len;
934 }
935 switch(form)
936 {
937 case elfcpp::DW_FORM_flag_present:
938 break;
939 case elfcpp::DW_FORM_strp:
940 case elfcpp::DW_FORM_sec_offset:
941 pattr += this->dwinfo_->offset_size();
942 break;
943 case elfcpp::DW_FORM_addr:
944 case elfcpp::DW_FORM_ref_addr:
945 pattr += this->dwinfo_->address_size();
946 break;
947 case elfcpp::DW_FORM_block1:
948 pattr += 1 + *pattr;
949 break;
950 case elfcpp::DW_FORM_block2:
951 {
952 uint16_t block_size;
953 block_size = this->dwinfo_->read_from_pointer<16>(&pattr);
954 pattr += block_size;
955 break;
956 }
957 case elfcpp::DW_FORM_block4:
958 {
959 uint32_t block_size;
960 block_size = this->dwinfo_->read_from_pointer<32>(&pattr);
961 pattr += block_size;
962 break;
963 }
964 case elfcpp::DW_FORM_block:
965 case elfcpp::DW_FORM_exprloc:
966 {
967 uint64_t block_size;
968 block_size = read_unsigned_LEB_128(pattr, &len);
969 pattr += len + block_size;
970 break;
971 }
972 case elfcpp::DW_FORM_data1:
973 case elfcpp::DW_FORM_ref1:
974 case elfcpp::DW_FORM_flag:
975 pattr += 1;
976 break;
977 case elfcpp::DW_FORM_data2:
978 case elfcpp::DW_FORM_ref2:
979 pattr += 2;
980 break;
981 case elfcpp::DW_FORM_data4:
982 case elfcpp::DW_FORM_ref4:
983 pattr += 4;
984 break;
985 case elfcpp::DW_FORM_data8:
986 case elfcpp::DW_FORM_ref8:
987 case elfcpp::DW_FORM_ref_sig8:
988 pattr += 8;
989 break;
990 case elfcpp::DW_FORM_ref_udata:
991 case elfcpp::DW_FORM_udata:
992 case elfcpp::DW_FORM_GNU_addr_index:
993 case elfcpp::DW_FORM_GNU_str_index:
994 read_unsigned_LEB_128(pattr, &len);
995 pattr += len;
996 break;
997 case elfcpp::DW_FORM_sdata:
998 read_signed_LEB_128(pattr, &len);
999 pattr += len;
1000 break;
1001 case elfcpp::DW_FORM_string:
1002 len = strlen(reinterpret_cast<const char*>(pattr));
1003 pattr += len + 1;
1004 break;
1005 default:
1006 return 0;
1007 }
1008 }
1009
1010 return this->die_offset_ + (pattr - pdie);
1011 }
1012
1013 // Get the name of the DIE and cache it.
1014
1015 void
1016 Dwarf_die::set_name()
1017 {
1018 if (this->name_ != NULL || !this->read_attributes())
1019 return;
1020 if (this->name_off_ != -1)
1021 this->name_ = this->dwinfo_->get_string(this->name_off_,
1022 this->string_shndx_);
1023 }
1024
1025 // Get the linkage name of the DIE and cache it.
1026
1027 void
1028 Dwarf_die::set_linkage_name()
1029 {
1030 if (this->linkage_name_ != NULL || !this->read_attributes())
1031 return;
1032 if (this->linkage_name_off_ != -1)
1033 this->linkage_name_ = this->dwinfo_->get_string(this->linkage_name_off_,
1034 this->string_shndx_);
1035 }
1036
1037 // Return the value of attribute ATTR.
1038
1039 const Dwarf_die::Attribute_value*
1040 Dwarf_die::attribute(unsigned int attr)
1041 {
1042 if (!this->read_attributes())
1043 return NULL;
1044 for (unsigned int i = 0; i < this->attributes_.size(); ++i)
1045 {
1046 if (this->attributes_[i].attr == attr)
1047 return &this->attributes_[i];
1048 }
1049 return NULL;
1050 }
1051
1052 const char*
1053 Dwarf_die::string_attribute(unsigned int attr)
1054 {
1055 const Attribute_value* attr_val = this->attribute(attr);
1056 if (attr_val == NULL)
1057 return NULL;
1058 switch (attr_val->form)
1059 {
1060 case elfcpp::DW_FORM_string:
1061 return attr_val->val.stringval;
1062 case elfcpp::DW_FORM_strp:
1063 return this->dwinfo_->get_string(attr_val->val.refval,
1064 attr_val->aux.shndx);
1065 default:
1066 return NULL;
1067 }
1068 }
1069
1070 int64_t
1071 Dwarf_die::int_attribute(unsigned int attr)
1072 {
1073 const Attribute_value* attr_val = this->attribute(attr);
1074 if (attr_val == NULL)
1075 return 0;
1076 switch (attr_val->form)
1077 {
1078 case elfcpp::DW_FORM_flag_present:
1079 case elfcpp::DW_FORM_data1:
1080 case elfcpp::DW_FORM_flag:
1081 case elfcpp::DW_FORM_data2:
1082 case elfcpp::DW_FORM_data4:
1083 case elfcpp::DW_FORM_data8:
1084 case elfcpp::DW_FORM_sdata:
1085 return attr_val->val.intval;
1086 default:
1087 return 0;
1088 }
1089 }
1090
1091 uint64_t
1092 Dwarf_die::uint_attribute(unsigned int attr)
1093 {
1094 const Attribute_value* attr_val = this->attribute(attr);
1095 if (attr_val == NULL)
1096 return 0;
1097 switch (attr_val->form)
1098 {
1099 case elfcpp::DW_FORM_flag_present:
1100 case elfcpp::DW_FORM_data1:
1101 case elfcpp::DW_FORM_flag:
1102 case elfcpp::DW_FORM_data4:
1103 case elfcpp::DW_FORM_data8:
1104 case elfcpp::DW_FORM_ref_sig8:
1105 case elfcpp::DW_FORM_udata:
1106 return attr_val->val.uintval;
1107 default:
1108 return 0;
1109 }
1110 }
1111
1112 off_t
1113 Dwarf_die::ref_attribute(unsigned int attr, unsigned int* shndx)
1114 {
1115 const Attribute_value* attr_val = this->attribute(attr);
1116 if (attr_val == NULL)
1117 return -1;
1118 switch (attr_val->form)
1119 {
1120 case elfcpp::DW_FORM_sec_offset:
1121 case elfcpp::DW_FORM_addr:
1122 case elfcpp::DW_FORM_ref_addr:
1123 case elfcpp::DW_FORM_ref1:
1124 case elfcpp::DW_FORM_ref2:
1125 case elfcpp::DW_FORM_ref4:
1126 case elfcpp::DW_FORM_ref8:
1127 case elfcpp::DW_FORM_ref_udata:
1128 *shndx = attr_val->aux.shndx;
1129 return attr_val->val.refval;
1130 case elfcpp::DW_FORM_ref_sig8:
1131 *shndx = attr_val->aux.shndx;
1132 return attr_val->val.uintval;
1133 case elfcpp::DW_FORM_data4:
1134 case elfcpp::DW_FORM_data8:
1135 *shndx = attr_val->aux.shndx;
1136 return attr_val->val.intval;
1137 default:
1138 return -1;
1139 }
1140 }
1141
1142 off_t
1143 Dwarf_die::address_attribute(unsigned int attr, unsigned int* shndx)
1144 {
1145 const Attribute_value* attr_val = this->attribute(attr);
1146 if (attr_val == NULL || attr_val->form != elfcpp::DW_FORM_addr)
1147 return -1;
1148
1149 *shndx = attr_val->aux.shndx;
1150 return attr_val->val.refval;
1151 }
1152
1153 // Return the offset of this DIE's first child.
1154
1155 off_t
1156 Dwarf_die::child_offset()
1157 {
1158 gold_assert(this->abbrev_code_ != NULL);
1159 if (!this->has_children())
1160 return 0;
1161 if (this->child_offset_ == 0)
1162 this->child_offset_ = this->skip_attributes();
1163 return this->child_offset_;
1164 }
1165
1166 // Return the offset of this DIE's next sibling.
1167
1168 off_t
1169 Dwarf_die::sibling_offset()
1170 {
1171 gold_assert(this->abbrev_code_ != NULL);
1172
1173 if (this->sibling_offset_ != 0)
1174 return this->sibling_offset_;
1175
1176 if (!this->has_children())
1177 {
1178 this->sibling_offset_ = this->skip_attributes();
1179 return this->sibling_offset_;
1180 }
1181
1182 if (this->has_sibling_attribute())
1183 {
1184 if (!this->read_attributes())
1185 return 0;
1186 if (this->sibling_offset_ != 0)
1187 return this->sibling_offset_;
1188 }
1189
1190 // Skip over the children.
1191 off_t child_offset = this->child_offset();
1192 while (child_offset > 0)
1193 {
1194 Dwarf_die die(this->dwinfo_, child_offset, this);
1195 // The Dwarf_die ctor will set this DIE's sibling offset
1196 // when it reads a zero abbrev code.
1197 if (die.tag() == 0)
1198 break;
1199 child_offset = die.sibling_offset();
1200 }
1201
1202 // This should be set by now. If not, there was a problem reading
1203 // the DWARF info, and we return 0.
1204 return this->sibling_offset_;
1205 }
1206
1207 // class Dwarf_info_reader
1208
1209 // Begin parsing the debug info. This calls visit_compilation_unit()
1210 // or visit_type_unit() for each compilation or type unit found in the
1211 // section, and visit_die() for each top-level DIE.
1212
1213 void
1214 Dwarf_info_reader::parse()
1215 {
1216 if (this->object_->is_big_endian())
1217 {
1218 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1219 this->do_parse<true>();
1220 #else
1221 gold_unreachable();
1222 #endif
1223 }
1224 else
1225 {
1226 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1227 this->do_parse<false>();
1228 #else
1229 gold_unreachable();
1230 #endif
1231 }
1232 }
1233
1234 template<bool big_endian>
1235 void
1236 Dwarf_info_reader::do_parse()
1237 {
1238 // Get the section contents and decompress if necessary.
1239 section_size_type buffer_size;
1240 bool buffer_is_new;
1241 this->buffer_ = this->object_->decompressed_section_contents(this->shndx_,
1242 &buffer_size,
1243 &buffer_is_new);
1244 if (this->buffer_ == NULL || buffer_size == 0)
1245 return;
1246 this->buffer_end_ = this->buffer_ + buffer_size;
1247
1248 // The offset of this input section in the output section.
1249 off_t section_offset = this->object_->output_section_offset(this->shndx_);
1250
1251 // Start tracking relocations for this section.
1252 this->reloc_mapper_ = make_elf_reloc_mapper(this->object_, this->symtab_,
1253 this->symtab_size_);
1254 this->reloc_mapper_->initialize(this->reloc_shndx_, this->reloc_type_);
1255
1256 // Loop over compilation units (or type units).
1257 unsigned int abbrev_shndx = this->abbrev_shndx_;
1258 off_t abbrev_offset = 0;
1259 const unsigned char* pinfo = this->buffer_;
1260 while (pinfo < this->buffer_end_)
1261 {
1262 // Read the compilation (or type) unit header.
1263 const unsigned char* cu_start = pinfo;
1264 this->cu_offset_ = cu_start - this->buffer_;
1265 this->cu_length_ = this->buffer_end_ - cu_start;
1266
1267 // Read unit_length (4 or 12 bytes).
1268 if (!this->check_buffer(pinfo + 4))
1269 break;
1270 uint32_t unit_length =
1271 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1272 pinfo += 4;
1273 if (unit_length == 0xffffffff)
1274 {
1275 if (!this->check_buffer(pinfo + 8))
1276 break;
1277 unit_length = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1278 pinfo += 8;
1279 this->offset_size_ = 8;
1280 }
1281 else
1282 this->offset_size_ = 4;
1283 if (!this->check_buffer(pinfo + unit_length))
1284 break;
1285 const unsigned char* cu_end = pinfo + unit_length;
1286 this->cu_length_ = cu_end - cu_start;
1287 if (!this->check_buffer(pinfo + 2 + this->offset_size_ + 1))
1288 break;
1289
1290 // Read version (2 bytes).
1291 this->cu_version_ =
1292 elfcpp::Swap_unaligned<16, big_endian>::readval(pinfo);
1293 pinfo += 2;
1294
1295 // Read debug_abbrev_offset (4 or 8 bytes).
1296 if (this->offset_size_ == 4)
1297 abbrev_offset = elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1298 else
1299 abbrev_offset = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1300 if (this->reloc_shndx_ > 0)
1301 {
1302 off_t reloc_offset = pinfo - this->buffer_;
1303 off_t value;
1304 abbrev_shndx =
1305 this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
1306 if (abbrev_shndx == 0)
1307 return;
1308 if (this->reloc_type_ == elfcpp::SHT_REL)
1309 abbrev_offset += value;
1310 else
1311 abbrev_offset = value;
1312 }
1313 pinfo += this->offset_size_;
1314
1315 // Read address_size (1 byte).
1316 this->address_size_ = *pinfo++;
1317
1318 // For type units, read the two extra fields.
1319 uint64_t signature = 0;
1320 off_t type_offset = 0;
1321 if (this->is_type_unit_)
1322 {
1323 if (!this->check_buffer(pinfo + 8 + this->offset_size_))
1324 break;
1325
1326 // Read type_signature (8 bytes).
1327 signature = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1328 pinfo += 8;
1329
1330 // Read type_offset (4 or 8 bytes).
1331 if (this->offset_size_ == 4)
1332 type_offset =
1333 elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1334 else
1335 type_offset =
1336 elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1337 pinfo += this->offset_size_;
1338 }
1339
1340 // Read the .debug_abbrev table.
1341 this->abbrev_table_.read_abbrevs(this->object_, abbrev_shndx,
1342 abbrev_offset);
1343
1344 // Visit the root DIE.
1345 Dwarf_die root_die(this,
1346 pinfo - (this->buffer_ + this->cu_offset_),
1347 NULL);
1348 if (root_die.tag() != 0)
1349 {
1350 // Visit the CU or TU.
1351 if (this->is_type_unit_)
1352 this->visit_type_unit(section_offset + this->cu_offset_,
1353 cu_end - cu_start, type_offset, signature,
1354 &root_die);
1355 else
1356 this->visit_compilation_unit(section_offset + this->cu_offset_,
1357 cu_end - cu_start, &root_die);
1358 }
1359
1360 // Advance to the next CU.
1361 pinfo = cu_end;
1362 }
1363
1364 if (buffer_is_new)
1365 {
1366 delete[] this->buffer_;
1367 this->buffer_ = NULL;
1368 }
1369 }
1370
1371 // Read the DWARF string table.
1372
1373 bool
1374 Dwarf_info_reader::do_read_string_table(unsigned int string_shndx)
1375 {
1376 Relobj* object = this->object_;
1377
1378 // If we don't have relocations, string_shndx will be 0, and
1379 // we'll have to hunt for the .debug_str section.
1380 if (string_shndx == 0)
1381 {
1382 for (unsigned int i = 1; i < this->object_->shnum(); ++i)
1383 {
1384 std::string name = object->section_name(i);
1385 if (name == ".debug_str" || name == ".zdebug_str")
1386 {
1387 string_shndx = i;
1388 this->string_output_section_offset_ =
1389 object->output_section_offset(i);
1390 break;
1391 }
1392 }
1393 if (string_shndx == 0)
1394 return false;
1395 }
1396
1397 if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
1398 {
1399 delete[] this->string_buffer_;
1400 this->owns_string_buffer_ = false;
1401 }
1402
1403 // Get the secton contents and decompress if necessary.
1404 section_size_type buffer_size;
1405 const unsigned char* buffer =
1406 object->decompressed_section_contents(string_shndx,
1407 &buffer_size,
1408 &this->owns_string_buffer_);
1409 this->string_buffer_ = reinterpret_cast<const char*>(buffer);
1410 this->string_buffer_end_ = this->string_buffer_ + buffer_size;
1411 this->string_shndx_ = string_shndx;
1412 return true;
1413 }
1414
1415 // Read a possibly unaligned integer of SIZE.
1416 template <int valsize>
1417 inline typename elfcpp::Valtype_base<valsize>::Valtype
1418 Dwarf_info_reader::read_from_pointer(const unsigned char* source)
1419 {
1420 typename elfcpp::Valtype_base<valsize>::Valtype return_value;
1421 if (this->object_->is_big_endian())
1422 return_value = elfcpp::Swap_unaligned<valsize, true>::readval(source);
1423 else
1424 return_value = elfcpp::Swap_unaligned<valsize, false>::readval(source);
1425 return return_value;
1426 }
1427
1428 // Read a possibly unaligned integer of SIZE. Update SOURCE after read.
1429 template <int valsize>
1430 inline typename elfcpp::Valtype_base<valsize>::Valtype
1431 Dwarf_info_reader::read_from_pointer(const unsigned char** source)
1432 {
1433 typename elfcpp::Valtype_base<valsize>::Valtype return_value;
1434 if (this->object_->is_big_endian())
1435 return_value = elfcpp::Swap_unaligned<valsize, true>::readval(*source);
1436 else
1437 return_value = elfcpp::Swap_unaligned<valsize, false>::readval(*source);
1438 *source += valsize / 8;
1439 return return_value;
1440 }
1441
1442 // Look for a relocation at offset ATTR_OFF in the dwarf info,
1443 // and return the section index and offset of the target.
1444
1445 unsigned int
1446 Dwarf_info_reader::lookup_reloc(off_t attr_off, off_t* target_off)
1447 {
1448 off_t value;
1449 attr_off += this->cu_offset_;
1450 unsigned int shndx = this->reloc_mapper_->get_reloc_target(attr_off, &value);
1451 if (shndx == 0)
1452 return 0;
1453 if (this->reloc_type_ == elfcpp::SHT_REL)
1454 *target_off += value;
1455 else
1456 *target_off = value;
1457 return shndx;
1458 }
1459
1460 // Return a string from the DWARF string table.
1461
1462 const char*
1463 Dwarf_info_reader::get_string(off_t str_off, unsigned int string_shndx)
1464 {
1465 if (!this->read_string_table(string_shndx))
1466 return NULL;
1467
1468 // Correct the offset. For incremental update links, we have a
1469 // relocated offset that is relative to the output section, but
1470 // here we need an offset relative to the input section.
1471 str_off -= this->string_output_section_offset_;
1472
1473 const char* p = this->string_buffer_ + str_off;
1474
1475 if (p < this->string_buffer_ || p >= this->string_buffer_end_)
1476 return NULL;
1477
1478 return p;
1479 }
1480
1481 // The following are default, do-nothing, implementations of the
1482 // hook methods normally provided by a derived class. We provide
1483 // default implementations rather than no implementation so that
1484 // a derived class needs to implement only the hooks that it needs
1485 // to use.
1486
1487 // Process a compilation unit and parse its child DIE.
1488
1489 void
1490 Dwarf_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die*)
1491 {
1492 }
1493
1494 // Process a type unit and parse its child DIE.
1495
1496 void
1497 Dwarf_info_reader::visit_type_unit(off_t, off_t, off_t, uint64_t, Dwarf_die*)
1498 {
1499 }
1500
1501 // Print a warning about a corrupt debug section.
1502
1503 void
1504 Dwarf_info_reader::warn_corrupt_debug_section() const
1505 {
1506 gold_warning(_("%s: corrupt debug info in %s"),
1507 this->object_->name().c_str(),
1508 this->object_->section_name(this->shndx_).c_str());
1509 }
1510
1511 // class Sized_dwarf_line_info
1512
1513 struct LineStateMachine
1514 {
1515 int file_num;
1516 uint64_t address;
1517 int line_num;
1518 int column_num;
1519 unsigned int shndx; // the section address refers to
1520 bool is_stmt; // stmt means statement.
1521 bool basic_block;
1522 bool end_sequence;
1523 };
1524
1525 static void
1526 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
1527 {
1528 lsm->file_num = 1;
1529 lsm->address = 0;
1530 lsm->line_num = 1;
1531 lsm->column_num = 0;
1532 lsm->shndx = -1U;
1533 lsm->is_stmt = default_is_stmt;
1534 lsm->basic_block = false;
1535 lsm->end_sequence = false;
1536 }
1537
1538 template<int size, bool big_endian>
1539 Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(
1540 Object* object,
1541 unsigned int read_shndx)
1542 : data_valid_(false), buffer_(NULL), buffer_start_(NULL),
1543 reloc_mapper_(NULL), symtab_buffer_(NULL), directories_(), files_(),
1544 current_header_index_(-1)
1545 {
1546 unsigned int debug_shndx;
1547
1548 for (debug_shndx = 1; debug_shndx < object->shnum(); ++debug_shndx)
1549 {
1550 // FIXME: do this more efficiently: section_name() isn't super-fast
1551 std::string name = object->section_name(debug_shndx);
1552 if (name == ".debug_line" || name == ".zdebug_line")
1553 {
1554 section_size_type buffer_size;
1555 bool is_new = false;
1556 this->buffer_ = object->decompressed_section_contents(debug_shndx,
1557 &buffer_size,
1558 &is_new);
1559 if (is_new)
1560 this->buffer_start_ = this->buffer_;
1561 this->buffer_end_ = this->buffer_ + buffer_size;
1562 break;
1563 }
1564 }
1565 if (this->buffer_ == NULL)
1566 return;
1567
1568 // Find the relocation section for ".debug_line".
1569 // We expect these for relobjs (.o's) but not dynobjs (.so's).
1570 unsigned int reloc_shndx = 0;
1571 for (unsigned int i = 0; i < object->shnum(); ++i)
1572 {
1573 unsigned int reloc_sh_type = object->section_type(i);
1574 if ((reloc_sh_type == elfcpp::SHT_REL
1575 || reloc_sh_type == elfcpp::SHT_RELA)
1576 && object->section_info(i) == debug_shndx)
1577 {
1578 reloc_shndx = i;
1579 this->track_relocs_type_ = reloc_sh_type;
1580 break;
1581 }
1582 }
1583
1584 // Finally, we need the symtab section to interpret the relocs.
1585 if (reloc_shndx != 0)
1586 {
1587 unsigned int symtab_shndx;
1588 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
1589 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
1590 {
1591 this->symtab_buffer_ = object->section_contents(
1592 symtab_shndx, &this->symtab_buffer_size_, false);
1593 break;
1594 }
1595 if (this->symtab_buffer_ == NULL)
1596 return;
1597 }
1598
1599 this->reloc_mapper_ =
1600 new Sized_elf_reloc_mapper<size, big_endian>(object,
1601 this->symtab_buffer_,
1602 this->symtab_buffer_size_);
1603 if (!this->reloc_mapper_->initialize(reloc_shndx, this->track_relocs_type_))
1604 return;
1605
1606 // Now that we have successfully read all the data, parse the debug
1607 // info.
1608 this->data_valid_ = true;
1609 this->read_line_mappings(read_shndx);
1610 }
1611
1612 // Read the DWARF header.
1613
1614 template<int size, bool big_endian>
1615 const unsigned char*
1616 Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
1617 const unsigned char* lineptr)
1618 {
1619 uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
1620 lineptr += 4;
1621
1622 // In DWARF2/3, if the initial length is all 1 bits, then the offset
1623 // size is 8 and we need to read the next 8 bytes for the real length.
1624 if (initial_length == 0xffffffff)
1625 {
1626 header_.offset_size = 8;
1627 initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
1628 lineptr += 8;
1629 }
1630 else
1631 header_.offset_size = 4;
1632
1633 header_.total_length = initial_length;
1634
1635 gold_assert(lineptr + header_.total_length <= buffer_end_);
1636
1637 header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
1638 lineptr += 2;
1639
1640 if (header_.offset_size == 4)
1641 header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
1642 else
1643 header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
1644 lineptr += header_.offset_size;
1645
1646 header_.min_insn_length = *lineptr;
1647 lineptr += 1;
1648
1649 header_.default_is_stmt = *lineptr;
1650 lineptr += 1;
1651
1652 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
1653 lineptr += 1;
1654
1655 header_.line_range = *lineptr;
1656 lineptr += 1;
1657
1658 header_.opcode_base = *lineptr;
1659 lineptr += 1;
1660
1661 header_.std_opcode_lengths.resize(header_.opcode_base + 1);
1662 header_.std_opcode_lengths[0] = 0;
1663 for (int i = 1; i < header_.opcode_base; i++)
1664 {
1665 header_.std_opcode_lengths[i] = *lineptr;
1666 lineptr += 1;
1667 }
1668
1669 return lineptr;
1670 }
1671
1672 // The header for a debug_line section is mildly complicated, because
1673 // the line info is very tightly encoded.
1674
1675 template<int size, bool big_endian>
1676 const unsigned char*
1677 Sized_dwarf_line_info<size, big_endian>::read_header_tables(
1678 const unsigned char* lineptr)
1679 {
1680 ++this->current_header_index_;
1681
1682 // Create a new directories_ entry and a new files_ entry for our new
1683 // header. We initialize each with a single empty element, because
1684 // dwarf indexes directory and filenames starting at 1.
1685 gold_assert(static_cast<int>(this->directories_.size())
1686 == this->current_header_index_);
1687 gold_assert(static_cast<int>(this->files_.size())
1688 == this->current_header_index_);
1689 this->directories_.push_back(std::vector<std::string>(1));
1690 this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
1691
1692 // It is legal for the directory entry table to be empty.
1693 if (*lineptr)
1694 {
1695 int dirindex = 1;
1696 while (*lineptr)
1697 {
1698 const char* dirname = reinterpret_cast<const char*>(lineptr);
1699 gold_assert(dirindex
1700 == static_cast<int>(this->directories_.back().size()));
1701 this->directories_.back().push_back(dirname);
1702 lineptr += this->directories_.back().back().size() + 1;
1703 dirindex++;
1704 }
1705 }
1706 lineptr++;
1707
1708 // It is also legal for the file entry table to be empty.
1709 if (*lineptr)
1710 {
1711 int fileindex = 1;
1712 size_t len;
1713 while (*lineptr)
1714 {
1715 const char* filename = reinterpret_cast<const char*>(lineptr);
1716 lineptr += strlen(filename) + 1;
1717
1718 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
1719 lineptr += len;
1720
1721 if (dirindex >= this->directories_.back().size())
1722 dirindex = 0;
1723 int dirindexi = static_cast<int>(dirindex);
1724
1725 read_unsigned_LEB_128(lineptr, &len); // mod_time
1726 lineptr += len;
1727
1728 read_unsigned_LEB_128(lineptr, &len); // filelength
1729 lineptr += len;
1730
1731 gold_assert(fileindex
1732 == static_cast<int>(this->files_.back().size()));
1733 this->files_.back().push_back(std::make_pair(dirindexi, filename));
1734 fileindex++;
1735 }
1736 }
1737 lineptr++;
1738
1739 return lineptr;
1740 }
1741
1742 // Process a single opcode in the .debug.line structure.
1743
1744 template<int size, bool big_endian>
1745 bool
1746 Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
1747 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
1748 {
1749 size_t oplen = 0;
1750 size_t templen;
1751 unsigned char opcode = *start;
1752 oplen++;
1753 start++;
1754
1755 // If the opcode is great than the opcode_base, it is a special
1756 // opcode. Most line programs consist mainly of special opcodes.
1757 if (opcode >= header_.opcode_base)
1758 {
1759 opcode -= header_.opcode_base;
1760 const int advance_address = ((opcode / header_.line_range)
1761 * header_.min_insn_length);
1762 lsm->address += advance_address;
1763
1764 const int advance_line = ((opcode % header_.line_range)
1765 + header_.line_base);
1766 lsm->line_num += advance_line;
1767 lsm->basic_block = true;
1768 *len = oplen;
1769 return true;
1770 }
1771
1772 // Otherwise, we have the regular opcodes
1773 switch (opcode)
1774 {
1775 case elfcpp::DW_LNS_copy:
1776 lsm->basic_block = false;
1777 *len = oplen;
1778 return true;
1779
1780 case elfcpp::DW_LNS_advance_pc:
1781 {
1782 const uint64_t advance_address
1783 = read_unsigned_LEB_128(start, &templen);
1784 oplen += templen;
1785 lsm->address += header_.min_insn_length * advance_address;
1786 }
1787 break;
1788
1789 case elfcpp::DW_LNS_advance_line:
1790 {
1791 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
1792 oplen += templen;
1793 lsm->line_num += advance_line;
1794 }
1795 break;
1796
1797 case elfcpp::DW_LNS_set_file:
1798 {
1799 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
1800 oplen += templen;
1801 lsm->file_num = fileno;
1802 }
1803 break;
1804
1805 case elfcpp::DW_LNS_set_column:
1806 {
1807 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
1808 oplen += templen;
1809 lsm->column_num = colno;
1810 }
1811 break;
1812
1813 case elfcpp::DW_LNS_negate_stmt:
1814 lsm->is_stmt = !lsm->is_stmt;
1815 break;
1816
1817 case elfcpp::DW_LNS_set_basic_block:
1818 lsm->basic_block = true;
1819 break;
1820
1821 case elfcpp::DW_LNS_fixed_advance_pc:
1822 {
1823 int advance_address;
1824 advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
1825 oplen += 2;
1826 lsm->address += advance_address;
1827 }
1828 break;
1829
1830 case elfcpp::DW_LNS_const_add_pc:
1831 {
1832 const int advance_address = (header_.min_insn_length
1833 * ((255 - header_.opcode_base)
1834 / header_.line_range));
1835 lsm->address += advance_address;
1836 }
1837 break;
1838
1839 case elfcpp::DW_LNS_extended_op:
1840 {
1841 const uint64_t extended_op_len
1842 = read_unsigned_LEB_128(start, &templen);
1843 start += templen;
1844 oplen += templen + extended_op_len;
1845
1846 const unsigned char extended_op = *start;
1847 start++;
1848
1849 switch (extended_op)
1850 {
1851 case elfcpp::DW_LNE_end_sequence:
1852 // This means that the current byte is the one immediately
1853 // after a set of instructions. Record the current line
1854 // for up to one less than the current address.
1855 lsm->line_num = -1;
1856 lsm->end_sequence = true;
1857 *len = oplen;
1858 return true;
1859
1860 case elfcpp::DW_LNE_set_address:
1861 {
1862 lsm->address =
1863 elfcpp::Swap_unaligned<size, big_endian>::readval(start);
1864 typename Reloc_map::const_iterator it
1865 = this->reloc_map_.find(start - this->buffer_);
1866 if (it != reloc_map_.end())
1867 {
1868 // If this is a SHT_RELA section, then ignore the
1869 // section contents. This assumes that this is a
1870 // straight reloc which just uses the reloc addend.
1871 // The reloc addend has already been included in the
1872 // symbol value.
1873 if (this->track_relocs_type_ == elfcpp::SHT_RELA)
1874 lsm->address = 0;
1875 // Add in the symbol value.
1876 lsm->address += it->second.second;
1877 lsm->shndx = it->second.first;
1878 }
1879 else
1880 {
1881 // If we're a normal .o file, with relocs, every
1882 // set_address should have an associated relocation.
1883 if (this->input_is_relobj())
1884 this->data_valid_ = false;
1885 }
1886 break;
1887 }
1888 case elfcpp::DW_LNE_define_file:
1889 {
1890 const char* filename = reinterpret_cast<const char*>(start);
1891 templen = strlen(filename) + 1;
1892 start += templen;
1893
1894 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
1895
1896 if (dirindex >= this->directories_.back().size())
1897 dirindex = 0;
1898 int dirindexi = static_cast<int>(dirindex);
1899
1900 // This opcode takes two additional ULEB128 parameters
1901 // (mod_time and filelength), but we don't use those
1902 // values. Because OPLEN already tells us how far to
1903 // skip to the next opcode, we don't need to read
1904 // them at all.
1905
1906 this->files_.back().push_back(std::make_pair(dirindexi,
1907 filename));
1908 }
1909 break;
1910 }
1911 }
1912 break;
1913
1914 default:
1915 {
1916 // Ignore unknown opcode silently
1917 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
1918 {
1919 size_t templen;
1920 read_unsigned_LEB_128(start, &templen);
1921 start += templen;
1922 oplen += templen;
1923 }
1924 }
1925 break;
1926 }
1927 *len = oplen;
1928 return false;
1929 }
1930
1931 // Read the debug information at LINEPTR and store it in the line
1932 // number map.
1933
1934 template<int size, bool big_endian>
1935 unsigned const char*
1936 Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
1937 unsigned int shndx)
1938 {
1939 struct LineStateMachine lsm;
1940
1941 // LENGTHSTART is the place the length field is based on. It is the
1942 // point in the header after the initial length field.
1943 const unsigned char* lengthstart = buffer_;
1944
1945 // In 64 bit dwarf, the initial length is 12 bytes, because of the
1946 // 0xffffffff at the start.
1947 if (header_.offset_size == 8)
1948 lengthstart += 12;
1949 else
1950 lengthstart += 4;
1951
1952 while (lineptr < lengthstart + header_.total_length)
1953 {
1954 ResetLineStateMachine(&lsm, header_.default_is_stmt);
1955 while (!lsm.end_sequence)
1956 {
1957 size_t oplength;
1958 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
1959 if (add_line
1960 && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
1961 {
1962 Offset_to_lineno_entry entry
1963 = { static_cast<off_t>(lsm.address),
1964 this->current_header_index_,
1965 static_cast<unsigned int>(lsm.file_num),
1966 true, lsm.line_num };
1967 std::vector<Offset_to_lineno_entry>&
1968 map(this->line_number_map_[lsm.shndx]);
1969 // If we see two consecutive entries with the same
1970 // offset and a real line number, then mark the first
1971 // one as non-canonical.
1972 if (!map.empty()
1973 && (map.back().offset == static_cast<off_t>(lsm.address))
1974 && lsm.line_num != -1
1975 && map.back().line_num != -1)
1976 map.back().last_line_for_offset = false;
1977 map.push_back(entry);
1978 }
1979 lineptr += oplength;
1980 }
1981 }
1982
1983 return lengthstart + header_.total_length;
1984 }
1985
1986 // Read the relocations into a Reloc_map.
1987
1988 template<int size, bool big_endian>
1989 void
1990 Sized_dwarf_line_info<size, big_endian>::read_relocs()
1991 {
1992 if (this->symtab_buffer_ == NULL)
1993 return;
1994
1995 off_t value;
1996 off_t reloc_offset;
1997 while ((reloc_offset = this->reloc_mapper_->next_offset()) != -1)
1998 {
1999 const unsigned int shndx =
2000 this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
2001
2002 // There is no reason to record non-ordinary section indexes, or
2003 // SHN_UNDEF, because they will never match the real section.
2004 if (shndx != 0)
2005 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
2006
2007 this->reloc_mapper_->advance(reloc_offset + 1);
2008 }
2009 }
2010
2011 // Read the line number info.
2012
2013 template<int size, bool big_endian>
2014 void
2015 Sized_dwarf_line_info<size, big_endian>::read_line_mappings(unsigned int shndx)
2016 {
2017 gold_assert(this->data_valid_ == true);
2018
2019 this->read_relocs();
2020 while (this->buffer_ < this->buffer_end_)
2021 {
2022 const unsigned char* lineptr = this->buffer_;
2023 lineptr = this->read_header_prolog(lineptr);
2024 lineptr = this->read_header_tables(lineptr);
2025 lineptr = this->read_lines(lineptr, shndx);
2026 this->buffer_ = lineptr;
2027 }
2028
2029 // Sort the lines numbers, so addr2line can use binary search.
2030 for (typename Lineno_map::iterator it = line_number_map_.begin();
2031 it != line_number_map_.end();
2032 ++it)
2033 // Each vector needs to be sorted by offset.
2034 std::sort(it->second.begin(), it->second.end());
2035 }
2036
2037 // Some processing depends on whether the input is a .o file or not.
2038 // For instance, .o files have relocs, and have .debug_lines
2039 // information on a per section basis. .so files, on the other hand,
2040 // lack relocs, and offsets are unique, so we can ignore the section
2041 // information.
2042
2043 template<int size, bool big_endian>
2044 bool
2045 Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
2046 {
2047 // Only .o files have relocs and the symtab buffer that goes with them.
2048 return this->symtab_buffer_ != NULL;
2049 }
2050
2051 // Given an Offset_to_lineno_entry vector, and an offset, figure out
2052 // if the offset points into a function according to the vector (see
2053 // comments below for the algorithm). If it does, return an iterator
2054 // into the vector that points to the line-number that contains that
2055 // offset. If not, it returns vector::end().
2056
2057 static std::vector<Offset_to_lineno_entry>::const_iterator
2058 offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
2059 off_t offset)
2060 {
2061 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, true, 0 };
2062
2063 // lower_bound() returns the smallest offset which is >= lookup_key.
2064 // If no offset in offsets is >= lookup_key, returns end().
2065 std::vector<Offset_to_lineno_entry>::const_iterator it
2066 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
2067
2068 // This code is easiest to understand with a concrete example.
2069 // Here's a possible offsets array:
2070 // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0
2071 // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1
2072 // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2
2073 // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3
2074 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4
2075 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5
2076 // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6
2077 // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7
2078 // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8
2079 // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9
2080 // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10
2081 // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11
2082 // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12
2083 // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13
2084 // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14
2085 // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15
2086 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16
2087 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17
2088 // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18
2089 // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19
2090 // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20
2091 // The entries with line_num == -1 mark the end of a function: the
2092 // associated offset is one past the last instruction in the
2093 // function. This can correspond to the beginning of the next
2094 // function (as is true for offset 3232); alternately, there can be
2095 // a gap between the end of one function and the start of the next
2096 // (as is true for some others, most obviously from 3236->5764).
2097 //
2098 // Case 1: lookup_key has offset == 10. lower_bound returns
2099 // offsets[0]. Since it's not an exact match and we're
2100 // at the beginning of offsets, we return end() (invalid).
2101 // Case 2: lookup_key has offset 10000. lower_bound returns
2102 // offset[21] (end()). We return end() (invalid).
2103 // Case 3: lookup_key has offset == 3211. lower_bound matches
2104 // offsets[0] exactly, and that's the entry we return.
2105 // Case 4: lookup_key has offset == 3232. lower_bound returns
2106 // offsets[4]. That's an exact match, but indicates
2107 // end-of-function. We check if offsets[5] is also an
2108 // exact match but not end-of-function. It is, so we
2109 // return offsets[5].
2110 // Case 5: lookup_key has offset == 3214. lower_bound returns
2111 // offsets[1]. Since it's not an exact match, we back
2112 // up to the offset that's < lookup_key, offsets[0].
2113 // We note offsets[0] is a valid entry (not end-of-function),
2114 // so that's the entry we return.
2115 // Case 6: lookup_key has offset == 4000. lower_bound returns
2116 // offsets[8]. Since it's not an exact match, we back
2117 // up to offsets[7]. Since offsets[7] indicates
2118 // end-of-function, we know lookup_key is between
2119 // functions, so we return end() (not a valid offset).
2120 // Case 7: lookup_key has offset == 5794. lower_bound returns
2121 // offsets[19]. Since it's not an exact match, we back
2122 // up to offsets[16]. Note we back up to the *first*
2123 // entry with offset 5793, not just offsets[19-1].
2124 // We note offsets[16] is a valid entry, so we return it.
2125 // If offsets[16] had had line_num == -1, we would have
2126 // checked offsets[17]. The reason for this is that
2127 // 16 and 17 can be in an arbitrary order, since we sort
2128 // only by offset and last_line_for_offset. (Note it
2129 // doesn't help to use line_number as a tertiary sort key,
2130 // since sometimes we want the -1 to be first and sometimes
2131 // we want it to be last.)
2132
2133 // This deals with cases (1) and (2).
2134 if ((it == offsets->begin() && offset < it->offset)
2135 || it == offsets->end())
2136 return offsets->end();
2137
2138 // This deals with cases (3) and (4).
2139 if (offset == it->offset)
2140 {
2141 while (it != offsets->end()
2142 && it->offset == offset
2143 && it->line_num == -1)
2144 ++it;
2145 if (it == offsets->end() || it->offset != offset)
2146 return offsets->end();
2147 else
2148 return it;
2149 }
2150
2151 // This handles the first part of case (7) -- we back up to the
2152 // *first* entry that has the offset that's behind us.
2153 gold_assert(it != offsets->begin());
2154 std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
2155 --it;
2156 const off_t range_value = it->offset;
2157 while (it != offsets->begin() && (it-1)->offset == range_value)
2158 --it;
2159
2160 // This handles cases (5), (6), and (7): if any entry in the
2161 // equal_range [it, range_end) has a line_num != -1, it's a valid
2162 // match. If not, we're not in a function. The line number we saw
2163 // last for an offset will be sorted first, so it'll get returned if
2164 // it's present.
2165 for (; it != range_end; ++it)
2166 if (it->line_num != -1)
2167 return it;
2168 return offsets->end();
2169 }
2170
2171 // Returns the canonical filename:lineno for the address passed in.
2172 // If other_lines is not NULL, appends the non-canonical lines
2173 // assigned to the same address.
2174
2175 template<int size, bool big_endian>
2176 std::string
2177 Sized_dwarf_line_info<size, big_endian>::do_addr2line(
2178 unsigned int shndx,
2179 off_t offset,
2180 std::vector<std::string>* other_lines)
2181 {
2182 if (this->data_valid_ == false)
2183 return "";
2184
2185 const std::vector<Offset_to_lineno_entry>* offsets;
2186 // If we do not have reloc information, then our input is a .so or
2187 // some similar data structure where all the information is held in
2188 // the offset. In that case, we ignore the input shndx.
2189 if (this->input_is_relobj())
2190 offsets = &this->line_number_map_[shndx];
2191 else
2192 offsets = &this->line_number_map_[-1U];
2193 if (offsets->empty())
2194 return "";
2195
2196 typename std::vector<Offset_to_lineno_entry>::const_iterator it
2197 = offset_to_iterator(offsets, offset);
2198 if (it == offsets->end())
2199 return "";
2200
2201 std::string result = this->format_file_lineno(*it);
2202 if (other_lines != NULL)
2203 for (++it; it != offsets->end() && it->offset == offset; ++it)
2204 {
2205 if (it->line_num == -1)
2206 continue; // The end of a previous function.
2207 other_lines->push_back(this->format_file_lineno(*it));
2208 }
2209 return result;
2210 }
2211
2212 // Convert the file_num + line_num into a string.
2213
2214 template<int size, bool big_endian>
2215 std::string
2216 Sized_dwarf_line_info<size, big_endian>::format_file_lineno(
2217 const Offset_to_lineno_entry& loc) const
2218 {
2219 std::string ret;
2220
2221 gold_assert(loc.header_num < static_cast<int>(this->files_.size()));
2222 gold_assert(loc.file_num
2223 < static_cast<unsigned int>(this->files_[loc.header_num].size()));
2224 const std::pair<int, std::string>& filename_pair
2225 = this->files_[loc.header_num][loc.file_num];
2226 const std::string& filename = filename_pair.second;
2227
2228 gold_assert(loc.header_num < static_cast<int>(this->directories_.size()));
2229 gold_assert(filename_pair.first
2230 < static_cast<int>(this->directories_[loc.header_num].size()));
2231 const std::string& dirname
2232 = this->directories_[loc.header_num][filename_pair.first];
2233
2234 if (!dirname.empty())
2235 {
2236 ret += dirname;
2237 ret += "/";
2238 }
2239 ret += filename;
2240 if (ret.empty())
2241 ret = "(unknown)";
2242
2243 char buffer[64]; // enough to hold a line number
2244 snprintf(buffer, sizeof(buffer), "%d", loc.line_num);
2245 ret += ":";
2246 ret += buffer;
2247
2248 return ret;
2249 }
2250
2251 // Dwarf_line_info routines.
2252
2253 static unsigned int next_generation_count = 0;
2254
2255 struct Addr2line_cache_entry
2256 {
2257 Object* object;
2258 unsigned int shndx;
2259 Dwarf_line_info* dwarf_line_info;
2260 unsigned int generation_count;
2261 unsigned int access_count;
2262
2263 Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
2264 : object(o), shndx(s), dwarf_line_info(d),
2265 generation_count(next_generation_count), access_count(0)
2266 {
2267 if (next_generation_count < (1U << 31))
2268 ++next_generation_count;
2269 }
2270 };
2271 // We expect this cache to be small, so don't bother with a hashtable
2272 // or priority queue or anything: just use a simple vector.
2273 static std::vector<Addr2line_cache_entry> addr2line_cache;
2274
2275 std::string
2276 Dwarf_line_info::one_addr2line(Object* object,
2277 unsigned int shndx, off_t offset,
2278 size_t cache_size,
2279 std::vector<std::string>* other_lines)
2280 {
2281 Dwarf_line_info* lineinfo = NULL;
2282 std::vector<Addr2line_cache_entry>::iterator it;
2283
2284 // First, check the cache. If we hit, update the counts.
2285 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
2286 {
2287 if (it->object == object && it->shndx == shndx)
2288 {
2289 lineinfo = it->dwarf_line_info;
2290 it->generation_count = next_generation_count;
2291 // We cap generation_count at 2^31 -1 to avoid overflow.
2292 if (next_generation_count < (1U << 31))
2293 ++next_generation_count;
2294 // We cap access_count at 31 so 2^access_count doesn't overflow
2295 if (it->access_count < 31)
2296 ++it->access_count;
2297 break;
2298 }
2299 }
2300
2301 // If we don't hit the cache, create a new object and insert into the
2302 // cache.
2303 if (lineinfo == NULL)
2304 {
2305 switch (parameters->size_and_endianness())
2306 {
2307 #ifdef HAVE_TARGET_32_LITTLE
2308 case Parameters::TARGET_32_LITTLE:
2309 lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
2310 #endif
2311 #ifdef HAVE_TARGET_32_BIG
2312 case Parameters::TARGET_32_BIG:
2313 lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
2314 #endif
2315 #ifdef HAVE_TARGET_64_LITTLE
2316 case Parameters::TARGET_64_LITTLE:
2317 lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
2318 #endif
2319 #ifdef HAVE_TARGET_64_BIG
2320 case Parameters::TARGET_64_BIG:
2321 lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
2322 #endif
2323 default:
2324 gold_unreachable();
2325 }
2326 addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
2327 }
2328
2329 // Now that we have our object, figure out the answer
2330 std::string retval = lineinfo->addr2line(shndx, offset, other_lines);
2331
2332 // Finally, if our cache has grown too big, delete old objects. We
2333 // assume the common (probably only) case is deleting only one object.
2334 // We use a pretty simple scheme to evict: function of LRU and MFU.
2335 while (addr2line_cache.size() > cache_size)
2336 {
2337 unsigned int lowest_score = ~0U;
2338 std::vector<Addr2line_cache_entry>::iterator lowest
2339 = addr2line_cache.end();
2340 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
2341 {
2342 const unsigned int score = (it->generation_count
2343 + (1U << it->access_count));
2344 if (score < lowest_score)
2345 {
2346 lowest_score = score;
2347 lowest = it;
2348 }
2349 }
2350 if (lowest != addr2line_cache.end())
2351 {
2352 delete lowest->dwarf_line_info;
2353 addr2line_cache.erase(lowest);
2354 }
2355 }
2356
2357 return retval;
2358 }
2359
2360 void
2361 Dwarf_line_info::clear_addr2line_cache()
2362 {
2363 for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
2364 it != addr2line_cache.end();
2365 ++it)
2366 delete it->dwarf_line_info;
2367 addr2line_cache.clear();
2368 }
2369
2370 #ifdef HAVE_TARGET_32_LITTLE
2371 template
2372 class Sized_dwarf_line_info<32, false>;
2373 #endif
2374
2375 #ifdef HAVE_TARGET_32_BIG
2376 template
2377 class Sized_dwarf_line_info<32, true>;
2378 #endif
2379
2380 #ifdef HAVE_TARGET_64_LITTLE
2381 template
2382 class Sized_dwarf_line_info<64, false>;
2383 #endif
2384
2385 #ifdef HAVE_TARGET_64_BIG
2386 template
2387 class Sized_dwarf_line_info<64, true>;
2388 #endif
2389
2390 } // End namespace gold.