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