Fix PE-COFF archiving long name tables
[binutils/dougsmingw.git] / gold / dwarf_reader.cc
blob4062fe67c3157377b5429f53244b8b3f2ee67923
1 // dwarf_reader.cc -- parse dwarf2/3 debug information
3 // Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <algorithm>
26 #include <vector>
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"
36 namespace gold {
38 struct LineStateMachine
40 int file_num;
41 uint64_t address;
42 int line_num;
43 int column_num;
44 unsigned int shndx; // the section address refers to
45 bool is_stmt; // stmt means statement.
46 bool basic_block;
47 bool end_sequence;
50 static void
51 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
53 lsm->file_num = 1;
54 lsm->address = 0;
55 lsm->line_num = 1;
56 lsm->column_num = 0;
57 lsm->shndx = -1U;
58 lsm->is_stmt = default_is_stmt;
59 lsm->basic_block = false;
60 lsm->end_sequence = false;
63 template<int size, bool big_endian>
64 Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object,
65 unsigned int read_shndx)
66 : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL),
67 directories_(), files_(), current_header_index_(-1)
69 unsigned int debug_shndx;
70 for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx)
71 // FIXME: do this more efficiently: section_name() isn't super-fast
72 if (object->section_name(debug_shndx) == ".debug_line")
74 section_size_type buffer_size;
75 this->buffer_ = object->section_contents(debug_shndx, &buffer_size,
76 false);
77 this->buffer_end_ = this->buffer_ + buffer_size;
78 break;
80 if (this->buffer_ == NULL)
81 return;
83 // Find the relocation section for ".debug_line".
84 // We expect these for relobjs (.o's) but not dynobjs (.so's).
85 bool got_relocs = false;
86 for (unsigned int reloc_shndx = 0;
87 reloc_shndx < object->shnum();
88 ++reloc_shndx)
90 unsigned int reloc_sh_type = object->section_type(reloc_shndx);
91 if ((reloc_sh_type == elfcpp::SHT_REL
92 || reloc_sh_type == elfcpp::SHT_RELA)
93 && object->section_info(reloc_shndx) == debug_shndx)
95 got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
96 reloc_sh_type);
97 break;
101 // Finally, we need the symtab section to interpret the relocs.
102 if (got_relocs)
104 unsigned int symtab_shndx;
105 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
106 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
108 this->symtab_buffer_ = object->section_contents(
109 symtab_shndx, &this->symtab_buffer_size_, false);
110 break;
112 if (this->symtab_buffer_ == NULL)
113 return;
116 // Now that we have successfully read all the data, parse the debug
117 // info.
118 this->data_valid_ = true;
119 this->read_line_mappings(object, read_shndx);
122 // Read the DWARF header.
124 template<int size, bool big_endian>
125 const unsigned char*
126 Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
127 const unsigned char* lineptr)
129 uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
130 lineptr += 4;
132 // In DWARF2/3, if the initial length is all 1 bits, then the offset
133 // size is 8 and we need to read the next 8 bytes for the real length.
134 if (initial_length == 0xffffffff)
136 header_.offset_size = 8;
137 initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
138 lineptr += 8;
140 else
141 header_.offset_size = 4;
143 header_.total_length = initial_length;
145 gold_assert(lineptr + header_.total_length <= buffer_end_);
147 header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
148 lineptr += 2;
150 if (header_.offset_size == 4)
151 header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
152 else
153 header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
154 lineptr += header_.offset_size;
156 header_.min_insn_length = *lineptr;
157 lineptr += 1;
159 header_.default_is_stmt = *lineptr;
160 lineptr += 1;
162 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
163 lineptr += 1;
165 header_.line_range = *lineptr;
166 lineptr += 1;
168 header_.opcode_base = *lineptr;
169 lineptr += 1;
171 header_.std_opcode_lengths.reserve(header_.opcode_base + 1);
172 header_.std_opcode_lengths[0] = 0;
173 for (int i = 1; i < header_.opcode_base; i++)
175 header_.std_opcode_lengths[i] = *lineptr;
176 lineptr += 1;
179 return lineptr;
182 // The header for a debug_line section is mildly complicated, because
183 // the line info is very tightly encoded.
185 template<int size, bool big_endian>
186 const unsigned char*
187 Sized_dwarf_line_info<size, big_endian>::read_header_tables(
188 const unsigned char* lineptr)
190 ++this->current_header_index_;
192 // Create a new directories_ entry and a new files_ entry for our new
193 // header. We initialize each with a single empty element, because
194 // dwarf indexes directory and filenames starting at 1.
195 gold_assert(static_cast<int>(this->directories_.size())
196 == this->current_header_index_);
197 gold_assert(static_cast<int>(this->files_.size())
198 == this->current_header_index_);
199 this->directories_.push_back(std::vector<std::string>(1));
200 this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
202 // It is legal for the directory entry table to be empty.
203 if (*lineptr)
205 int dirindex = 1;
206 while (*lineptr)
208 const char* dirname = reinterpret_cast<const char*>(lineptr);
209 gold_assert(dirindex
210 == static_cast<int>(this->directories_.back().size()));
211 this->directories_.back().push_back(dirname);
212 lineptr += this->directories_.back().back().size() + 1;
213 dirindex++;
216 lineptr++;
218 // It is also legal for the file entry table to be empty.
219 if (*lineptr)
221 int fileindex = 1;
222 size_t len;
223 while (*lineptr)
225 const char* filename = reinterpret_cast<const char*>(lineptr);
226 lineptr += strlen(filename) + 1;
228 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
229 lineptr += len;
231 if (dirindex >= this->directories_.back().size())
232 dirindex = 0;
233 int dirindexi = static_cast<int>(dirindex);
235 read_unsigned_LEB_128(lineptr, &len); // mod_time
236 lineptr += len;
238 read_unsigned_LEB_128(lineptr, &len); // filelength
239 lineptr += len;
241 gold_assert(fileindex
242 == static_cast<int>(this->files_.back().size()));
243 this->files_.back().push_back(std::make_pair(dirindexi, filename));
244 fileindex++;
247 lineptr++;
249 return lineptr;
252 // Process a single opcode in the .debug.line structure.
254 // Templating on size and big_endian would yield more efficient (and
255 // simpler) code, but would bloat the binary. Speed isn't important
256 // here.
258 template<int size, bool big_endian>
259 bool
260 Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
261 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
263 size_t oplen = 0;
264 size_t templen;
265 unsigned char opcode = *start;
266 oplen++;
267 start++;
269 // If the opcode is great than the opcode_base, it is a special
270 // opcode. Most line programs consist mainly of special opcodes.
271 if (opcode >= header_.opcode_base)
273 opcode -= header_.opcode_base;
274 const int advance_address = ((opcode / header_.line_range)
275 * header_.min_insn_length);
276 lsm->address += advance_address;
278 const int advance_line = ((opcode % header_.line_range)
279 + header_.line_base);
280 lsm->line_num += advance_line;
281 lsm->basic_block = true;
282 *len = oplen;
283 return true;
286 // Otherwise, we have the regular opcodes
287 switch (opcode)
289 case elfcpp::DW_LNS_copy:
290 lsm->basic_block = false;
291 *len = oplen;
292 return true;
294 case elfcpp::DW_LNS_advance_pc:
296 const uint64_t advance_address
297 = read_unsigned_LEB_128(start, &templen);
298 oplen += templen;
299 lsm->address += header_.min_insn_length * advance_address;
301 break;
303 case elfcpp::DW_LNS_advance_line:
305 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
306 oplen += templen;
307 lsm->line_num += advance_line;
309 break;
311 case elfcpp::DW_LNS_set_file:
313 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
314 oplen += templen;
315 lsm->file_num = fileno;
317 break;
319 case elfcpp::DW_LNS_set_column:
321 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
322 oplen += templen;
323 lsm->column_num = colno;
325 break;
327 case elfcpp::DW_LNS_negate_stmt:
328 lsm->is_stmt = !lsm->is_stmt;
329 break;
331 case elfcpp::DW_LNS_set_basic_block:
332 lsm->basic_block = true;
333 break;
335 case elfcpp::DW_LNS_fixed_advance_pc:
337 int advance_address;
338 advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
339 oplen += 2;
340 lsm->address += advance_address;
342 break;
344 case elfcpp::DW_LNS_const_add_pc:
346 const int advance_address = (header_.min_insn_length
347 * ((255 - header_.opcode_base)
348 / header_.line_range));
349 lsm->address += advance_address;
351 break;
353 case elfcpp::DW_LNS_extended_op:
355 const uint64_t extended_op_len
356 = read_unsigned_LEB_128(start, &templen);
357 start += templen;
358 oplen += templen + extended_op_len;
360 const unsigned char extended_op = *start;
361 start++;
363 switch (extended_op)
365 case elfcpp::DW_LNE_end_sequence:
366 // This means that the current byte is the one immediately
367 // after a set of instructions. Record the current line
368 // for up to one less than the current address.
369 lsm->line_num = -1;
370 lsm->end_sequence = true;
371 *len = oplen;
372 return true;
374 case elfcpp::DW_LNE_set_address:
376 lsm->address = elfcpp::Swap_unaligned<size, big_endian>::readval(start);
377 typename Reloc_map::const_iterator it
378 = reloc_map_.find(start - this->buffer_);
379 if (it != reloc_map_.end())
381 // value + addend.
382 lsm->address += it->second.second;
383 lsm->shndx = it->second.first;
385 else
387 // If we're a normal .o file, with relocs, every
388 // set_address should have an associated relocation.
389 if (this->input_is_relobj())
390 this->data_valid_ = false;
392 break;
394 case elfcpp::DW_LNE_define_file:
396 const char* filename = reinterpret_cast<const char*>(start);
397 templen = strlen(filename) + 1;
398 start += templen;
400 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
401 oplen += templen;
403 if (dirindex >= this->directories_.back().size())
404 dirindex = 0;
405 int dirindexi = static_cast<int>(dirindex);
407 read_unsigned_LEB_128(start, &templen); // mod_time
408 oplen += templen;
410 read_unsigned_LEB_128(start, &templen); // filelength
411 oplen += templen;
413 this->files_.back().push_back(std::make_pair(dirindexi,
414 filename));
416 break;
419 break;
421 default:
423 // Ignore unknown opcode silently
424 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
426 size_t templen;
427 read_unsigned_LEB_128(start, &templen);
428 start += templen;
429 oplen += templen;
432 break;
434 *len = oplen;
435 return false;
438 // Read the debug information at LINEPTR and store it in the line
439 // number map.
441 template<int size, bool big_endian>
442 unsigned const char*
443 Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
444 unsigned int shndx)
446 struct LineStateMachine lsm;
448 // LENGTHSTART is the place the length field is based on. It is the
449 // point in the header after the initial length field.
450 const unsigned char* lengthstart = buffer_;
452 // In 64 bit dwarf, the initial length is 12 bytes, because of the
453 // 0xffffffff at the start.
454 if (header_.offset_size == 8)
455 lengthstart += 12;
456 else
457 lengthstart += 4;
459 while (lineptr < lengthstart + header_.total_length)
461 ResetLineStateMachine(&lsm, header_.default_is_stmt);
462 while (!lsm.end_sequence)
464 size_t oplength;
465 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
466 if (add_line
467 && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
469 Offset_to_lineno_entry entry
470 = { lsm.address, this->current_header_index_,
471 lsm.file_num, lsm.line_num };
472 line_number_map_[lsm.shndx].push_back(entry);
474 lineptr += oplength;
478 return lengthstart + header_.total_length;
481 // Looks in the symtab to see what section a symbol is in.
483 template<int size, bool big_endian>
484 unsigned int
485 Sized_dwarf_line_info<size, big_endian>::symbol_section(
486 Object* object,
487 unsigned int sym,
488 typename elfcpp::Elf_types<size>::Elf_Addr* value,
489 bool* is_ordinary)
491 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
492 gold_assert(sym * symsize < this->symtab_buffer_size_);
493 elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
494 *value = elfsym.get_st_value();
495 return object->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
498 // Read the relocations into a Reloc_map.
500 template<int size, bool big_endian>
501 void
502 Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object)
504 if (this->symtab_buffer_ == NULL)
505 return;
507 typename elfcpp::Elf_types<size>::Elf_Addr value;
508 off_t reloc_offset;
509 while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
511 const unsigned int sym = this->track_relocs_.next_symndx();
513 bool is_ordinary;
514 const unsigned int shndx = this->symbol_section(object, sym, &value,
515 &is_ordinary);
517 // There is no reason to record non-ordinary section indexes, or
518 // SHN_UNDEF, because they will never match the real section.
519 if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
520 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
522 this->track_relocs_.advance(reloc_offset + 1);
526 // Read the line number info.
528 template<int size, bool big_endian>
529 void
530 Sized_dwarf_line_info<size, big_endian>::read_line_mappings(Object* object,
531 unsigned int shndx)
533 gold_assert(this->data_valid_ == true);
535 this->read_relocs(object);
536 while (this->buffer_ < this->buffer_end_)
538 const unsigned char* lineptr = this->buffer_;
539 lineptr = this->read_header_prolog(lineptr);
540 lineptr = this->read_header_tables(lineptr);
541 lineptr = this->read_lines(lineptr, shndx);
542 this->buffer_ = lineptr;
545 // Sort the lines numbers, so addr2line can use binary search.
546 for (typename Lineno_map::iterator it = line_number_map_.begin();
547 it != line_number_map_.end();
548 ++it)
549 // Each vector needs to be sorted by offset.
550 std::sort(it->second.begin(), it->second.end());
553 // Some processing depends on whether the input is a .o file or not.
554 // For instance, .o files have relocs, and have .debug_lines
555 // information on a per section basis. .so files, on the other hand,
556 // lack relocs, and offsets are unique, so we can ignore the section
557 // information.
559 template<int size, bool big_endian>
560 bool
561 Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
563 // Only .o files have relocs and the symtab buffer that goes with them.
564 return this->symtab_buffer_ != NULL;
567 // Given an Offset_to_lineno_entry vector, and an offset, figure out
568 // if the offset points into a function according to the vector (see
569 // comments below for the algorithm). If it does, return an iterator
570 // into the vector that points to the line-number that contains that
571 // offset. If not, it returns vector::end().
573 static std::vector<Offset_to_lineno_entry>::const_iterator
574 offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
575 off_t offset)
577 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 };
579 // lower_bound() returns the smallest offset which is >= lookup_key.
580 // If no offset in offsets is >= lookup_key, returns end().
581 std::vector<Offset_to_lineno_entry>::const_iterator it
582 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
584 // This code is easiest to understand with a concrete example.
585 // Here's a possible offsets array:
586 // {{offset = 3211, header_num = 0, file_num = 1, line_num = 16}, // 0
587 // {offset = 3224, header_num = 0, file_num = 1, line_num = 20}, // 1
588 // {offset = 3226, header_num = 0, file_num = 1, line_num = 22}, // 2
589 // {offset = 3231, header_num = 0, file_num = 1, line_num = 25}, // 3
590 // {offset = 3232, header_num = 0, file_num = 1, line_num = -1}, // 4
591 // {offset = 3232, header_num = 0, file_num = 1, line_num = 65}, // 5
592 // {offset = 3235, header_num = 0, file_num = 1, line_num = 66}, // 6
593 // {offset = 3236, header_num = 0, file_num = 1, line_num = -1}, // 7
594 // {offset = 5764, header_num = 0, file_num = 1, line_num = 47}, // 8
595 // {offset = 5765, header_num = 0, file_num = 1, line_num = 48}, // 9
596 // {offset = 5767, header_num = 0, file_num = 1, line_num = 49}, // 10
597 // {offset = 5768, header_num = 0, file_num = 1, line_num = 50}, // 11
598 // {offset = 5773, header_num = 0, file_num = 1, line_num = -1}, // 12
599 // {offset = 5787, header_num = 1, file_num = 1, line_num = 19}, // 13
600 // {offset = 5790, header_num = 1, file_num = 1, line_num = 20}, // 14
601 // {offset = 5793, header_num = 1, file_num = 1, line_num = 67}, // 15
602 // {offset = 5793, header_num = 1, file_num = 1, line_num = -1}, // 16
603 // {offset = 5795, header_num = 1, file_num = 1, line_num = 68}, // 17
604 // {offset = 5798, header_num = 1, file_num = 1, line_num = -1}, // 18
605 // The entries with line_num == -1 mark the end of a function: the
606 // associated offset is one past the last instruction in the
607 // function. This can correspond to the beginning of the next
608 // function (as is true for offset 3232); alternately, there can be
609 // a gap between the end of one function and the start of the next
610 // (as is true for some others, most obviously from 3236->5764).
612 // Case 1: lookup_key has offset == 10. lower_bound returns
613 // offsets[0]. Since it's not an exact match and we're
614 // at the beginning of offsets, we return end() (invalid).
615 // Case 2: lookup_key has offset 10000. lower_bound returns
616 // offset[19] (end()). We return end() (invalid).
617 // Case 3: lookup_key has offset == 3211. lower_bound matches
618 // offsets[0] exactly, and that's the entry we return.
619 // Case 4: lookup_key has offset == 3232. lower_bound returns
620 // offsets[4]. That's an exact match, but indicates
621 // end-of-function. We check if offsets[5] is also an
622 // exact match but not end-of-function. It is, so we
623 // return offsets[5].
624 // Case 5: lookup_key has offset == 3214. lower_bound returns
625 // offsets[1]. Since it's not an exact match, we back
626 // up to the offset that's < lookup_key, offsets[0].
627 // We note offsets[0] is a valid entry (not end-of-function),
628 // so that's the entry we return.
629 // Case 6: lookup_key has offset == 4000. lower_bound returns
630 // offsets[8]. Since it's not an exact match, we back
631 // up to offsets[7]. Since offsets[7] indicates
632 // end-of-function, we know lookup_key is between
633 // functions, so we return end() (not a valid offset).
634 // Case 7: lookup_key has offset == 5794. lower_bound returns
635 // offsets[17]. Since it's not an exact match, we back
636 // up to offsets[15]. Note we back up to the *first*
637 // entry with offset 5793, not just offsets[17-1].
638 // We note offsets[15] is a valid entry, so we return it.
639 // If offsets[15] had had line_num == -1, we would have
640 // checked offsets[16]. The reason for this is that
641 // 15 and 16 can be in an arbitrary order, since we sort
642 // only by offset. (Note it doesn't help to use line_number
643 // as a secondary sort key, since sometimes we want the -1
644 // to be first and sometimes we want it to be last.)
646 // This deals with cases (1) and (2).
647 if ((it == offsets->begin() && offset < it->offset)
648 || it == offsets->end())
649 return offsets->end();
651 // This deals with cases (3) and (4).
652 if (offset == it->offset)
654 while (it != offsets->end()
655 && it->offset == offset
656 && it->line_num == -1)
657 ++it;
658 if (it == offsets->end() || it->offset != offset)
659 return offsets->end();
660 else
661 return it;
664 // This handles the first part of case (7) -- we back up to the
665 // *first* entry that has the offset that's behind us.
666 gold_assert(it != offsets->begin());
667 std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
668 --it;
669 const off_t range_value = it->offset;
670 while (it != offsets->begin() && (it-1)->offset == range_value)
671 --it;
673 // This handles cases (5), (6), and (7): if any entry in the
674 // equal_range [it, range_end) has a line_num != -1, it's a valid
675 // match. If not, we're not in a function.
676 for (; it != range_end; ++it)
677 if (it->line_num != -1)
678 return it;
679 return offsets->end();
682 // Return a string for a file name and line number.
684 template<int size, bool big_endian>
685 std::string
686 Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx,
687 off_t offset)
689 if (this->data_valid_ == false)
690 return "";
692 const std::vector<Offset_to_lineno_entry>* offsets;
693 // If we do not have reloc information, then our input is a .so or
694 // some similar data structure where all the information is held in
695 // the offset. In that case, we ignore the input shndx.
696 if (this->input_is_relobj())
697 offsets = &this->line_number_map_[shndx];
698 else
699 offsets = &this->line_number_map_[-1U];
700 if (offsets->empty())
701 return "";
703 typename std::vector<Offset_to_lineno_entry>::const_iterator it
704 = offset_to_iterator(offsets, offset);
705 if (it == offsets->end())
706 return "";
708 // Convert the file_num + line_num into a string.
709 std::string ret;
711 gold_assert(it->header_num < static_cast<int>(this->files_.size()));
712 gold_assert(it->file_num
713 < static_cast<int>(this->files_[it->header_num].size()));
714 const std::pair<int, std::string>& filename_pair
715 = this->files_[it->header_num][it->file_num];
716 const std::string& filename = filename_pair.second;
718 gold_assert(it->header_num < static_cast<int>(this->directories_.size()));
719 gold_assert(filename_pair.first
720 < static_cast<int>(this->directories_[it->header_num].size()));
721 const std::string& dirname
722 = this->directories_[it->header_num][filename_pair.first];
724 if (!dirname.empty())
726 ret += dirname;
727 ret += "/";
729 ret += filename;
730 if (ret.empty())
731 ret = "(unknown)";
733 char buffer[64]; // enough to hold a line number
734 snprintf(buffer, sizeof(buffer), "%d", it->line_num);
735 ret += ":";
736 ret += buffer;
738 return ret;
741 // Dwarf_line_info routines.
743 static unsigned int next_generation_count = 0;
745 struct Addr2line_cache_entry
747 Object* object;
748 unsigned int shndx;
749 Dwarf_line_info* dwarf_line_info;
750 unsigned int generation_count;
751 unsigned int access_count;
753 Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
754 : object(o), shndx(s), dwarf_line_info(d),
755 generation_count(next_generation_count), access_count(0)
757 if (next_generation_count < (1U << 31))
758 ++next_generation_count;
761 // We expect this cache to be small, so don't bother with a hashtable
762 // or priority queue or anything: just use a simple vector.
763 static std::vector<Addr2line_cache_entry> addr2line_cache;
765 std::string
766 Dwarf_line_info::one_addr2line(Object* object,
767 unsigned int shndx, off_t offset,
768 size_t cache_size)
770 Dwarf_line_info* lineinfo = NULL;
771 std::vector<Addr2line_cache_entry>::iterator it;
773 // First, check the cache. If we hit, update the counts.
774 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
776 if (it->object == object && it->shndx == shndx)
778 lineinfo = it->dwarf_line_info;
779 it->generation_count = next_generation_count;
780 // We cap generation_count at 2^31 -1 to avoid overflow.
781 if (next_generation_count < (1U << 31))
782 ++next_generation_count;
783 // We cap access_count at 31 so 2^access_count doesn't overflow
784 if (it->access_count < 31)
785 ++it->access_count;
786 break;
790 // If we don't hit the cache, create a new object and insert into the
791 // cache.
792 if (lineinfo == NULL)
794 switch (parameters->size_and_endianness())
796 #ifdef HAVE_TARGET_32_LITTLE
797 case Parameters::TARGET_32_LITTLE:
798 lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
799 #endif
800 #ifdef HAVE_TARGET_32_BIG
801 case Parameters::TARGET_32_BIG:
802 lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
803 #endif
804 #ifdef HAVE_TARGET_64_LITTLE
805 case Parameters::TARGET_64_LITTLE:
806 lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
807 #endif
808 #ifdef HAVE_TARGET_64_BIG
809 case Parameters::TARGET_64_BIG:
810 lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
811 #endif
812 default:
813 gold_unreachable();
815 addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
818 // Now that we have our object, figure out the answer
819 std::string retval = lineinfo->addr2line(shndx, offset);
821 // Finally, if our cache has grown too big, delete old objects. We
822 // assume the common (probably only) case is deleting only one object.
823 // We use a pretty simple scheme to evict: function of LRU and MFU.
824 while (addr2line_cache.size() > cache_size)
826 unsigned int lowest_score = ~0U;
827 std::vector<Addr2line_cache_entry>::iterator lowest
828 = addr2line_cache.end();
829 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
831 const unsigned int score = (it->generation_count
832 + (1U << it->access_count));
833 if (score < lowest_score)
835 lowest_score = score;
836 lowest = it;
839 if (lowest != addr2line_cache.end())
841 delete lowest->dwarf_line_info;
842 addr2line_cache.erase(lowest);
846 return retval;
849 void
850 Dwarf_line_info::clear_addr2line_cache()
852 for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
853 it != addr2line_cache.end();
854 ++it)
855 delete it->dwarf_line_info;
856 addr2line_cache.clear();
859 #ifdef HAVE_TARGET_32_LITTLE
860 template
861 class Sized_dwarf_line_info<32, false>;
862 #endif
864 #ifdef HAVE_TARGET_32_BIG
865 template
866 class Sized_dwarf_line_info<32, true>;
867 #endif
869 #ifdef HAVE_TARGET_64_LITTLE
870 template
871 class Sized_dwarf_line_info<64, false>;
872 #endif
874 #ifdef HAVE_TARGET_64_BIG
875 template
876 class Sized_dwarf_line_info<64, true>;
877 #endif
879 } // End namespace gold.