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