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