1 // dwarf_reader.cc -- parse dwarf2/3 debug information
3 // Copyright 2007, 2008, 2009, 2010 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.
28 #include "elfcpp_swap.h"
31 #include "parameters.h"
33 #include "dwarf_reader.h"
34 #include "int_encoding.h"
35 #include "compressed_output.h"
39 struct LineStateMachine
45 unsigned int shndx
; // the section address refers to
46 bool is_stmt
; // stmt means statement.
52 ResetLineStateMachine(struct LineStateMachine
* lsm
, bool default_is_stmt
)
59 lsm
->is_stmt
= default_is_stmt
;
60 lsm
->basic_block
= false;
61 lsm
->end_sequence
= false;
64 template<int size
, bool big_endian
>
65 Sized_dwarf_line_info
<size
, big_endian
>::Sized_dwarf_line_info(Object
* object
,
66 unsigned int read_shndx
)
67 : data_valid_(false), buffer_(NULL
), symtab_buffer_(NULL
),
68 directories_(), files_(), current_header_index_(-1)
70 unsigned int debug_shndx
;
71 for (debug_shndx
= 1; debug_shndx
< object
->shnum(); ++debug_shndx
)
73 // FIXME: do this more efficiently: section_name() isn't super-fast
74 std::string name
= object
->section_name(debug_shndx
);
75 if (name
== ".debug_line" || name
== ".zdebug_line")
77 section_size_type buffer_size
;
78 this->buffer_
= object
->section_contents(debug_shndx
, &buffer_size
,
80 this->buffer_end_
= this->buffer_
+ buffer_size
;
84 if (this->buffer_
== NULL
)
87 section_size_type uncompressed_size
= 0;
88 unsigned char* uncompressed_data
= NULL
;
89 if (object
->section_is_compressed(debug_shndx
, &uncompressed_size
))
91 uncompressed_data
= new unsigned char[uncompressed_size
];
92 if (!decompress_input_section(this->buffer_
,
93 this->buffer_end_
- this->buffer_
,
96 object
->error(_("could not decompress section %s"),
97 object
->section_name(debug_shndx
).c_str());
98 this->buffer_
= uncompressed_data
;
99 this->buffer_end_
= this->buffer_
+ uncompressed_size
;
102 // Find the relocation section for ".debug_line".
103 // We expect these for relobjs (.o's) but not dynobjs (.so's).
104 bool got_relocs
= false;
105 for (unsigned int reloc_shndx
= 0;
106 reloc_shndx
< object
->shnum();
109 unsigned int reloc_sh_type
= object
->section_type(reloc_shndx
);
110 if ((reloc_sh_type
== elfcpp::SHT_REL
111 || reloc_sh_type
== elfcpp::SHT_RELA
)
112 && object
->section_info(reloc_shndx
) == debug_shndx
)
114 got_relocs
= this->track_relocs_
.initialize(object
, reloc_shndx
,
116 this->track_relocs_type_
= reloc_sh_type
;
121 // Finally, we need the symtab section to interpret the relocs.
124 unsigned int symtab_shndx
;
125 for (symtab_shndx
= 0; symtab_shndx
< object
->shnum(); ++symtab_shndx
)
126 if (object
->section_type(symtab_shndx
) == elfcpp::SHT_SYMTAB
)
128 this->symtab_buffer_
= object
->section_contents(
129 symtab_shndx
, &this->symtab_buffer_size_
, false);
132 if (this->symtab_buffer_
== NULL
)
136 // Now that we have successfully read all the data, parse the debug
138 this->data_valid_
= true;
139 this->read_line_mappings(object
, read_shndx
);
142 // Read the DWARF header.
144 template<int size
, bool big_endian
>
146 Sized_dwarf_line_info
<size
, big_endian
>::read_header_prolog(
147 const unsigned char* lineptr
)
149 uint32_t initial_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
152 // In DWARF2/3, if the initial length is all 1 bits, then the offset
153 // size is 8 and we need to read the next 8 bytes for the real length.
154 if (initial_length
== 0xffffffff)
156 header_
.offset_size
= 8;
157 initial_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
161 header_
.offset_size
= 4;
163 header_
.total_length
= initial_length
;
165 gold_assert(lineptr
+ header_
.total_length
<= buffer_end_
);
167 header_
.version
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(lineptr
);
170 if (header_
.offset_size
== 4)
171 header_
.prologue_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
173 header_
.prologue_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
174 lineptr
+= header_
.offset_size
;
176 header_
.min_insn_length
= *lineptr
;
179 header_
.default_is_stmt
= *lineptr
;
182 header_
.line_base
= *reinterpret_cast<const signed char*>(lineptr
);
185 header_
.line_range
= *lineptr
;
188 header_
.opcode_base
= *lineptr
;
191 header_
.std_opcode_lengths
.resize(header_
.opcode_base
+ 1);
192 header_
.std_opcode_lengths
[0] = 0;
193 for (int i
= 1; i
< header_
.opcode_base
; i
++)
195 header_
.std_opcode_lengths
[i
] = *lineptr
;
202 // The header for a debug_line section is mildly complicated, because
203 // the line info is very tightly encoded.
205 template<int size
, bool big_endian
>
207 Sized_dwarf_line_info
<size
, big_endian
>::read_header_tables(
208 const unsigned char* lineptr
)
210 ++this->current_header_index_
;
212 // Create a new directories_ entry and a new files_ entry for our new
213 // header. We initialize each with a single empty element, because
214 // dwarf indexes directory and filenames starting at 1.
215 gold_assert(static_cast<int>(this->directories_
.size())
216 == this->current_header_index_
);
217 gold_assert(static_cast<int>(this->files_
.size())
218 == this->current_header_index_
);
219 this->directories_
.push_back(std::vector
<std::string
>(1));
220 this->files_
.push_back(std::vector
<std::pair
<int, std::string
> >(1));
222 // It is legal for the directory entry table to be empty.
228 const char* dirname
= reinterpret_cast<const char*>(lineptr
);
230 == static_cast<int>(this->directories_
.back().size()));
231 this->directories_
.back().push_back(dirname
);
232 lineptr
+= this->directories_
.back().back().size() + 1;
238 // It is also legal for the file entry table to be empty.
245 const char* filename
= reinterpret_cast<const char*>(lineptr
);
246 lineptr
+= strlen(filename
) + 1;
248 uint64_t dirindex
= read_unsigned_LEB_128(lineptr
, &len
);
251 if (dirindex
>= this->directories_
.back().size())
253 int dirindexi
= static_cast<int>(dirindex
);
255 read_unsigned_LEB_128(lineptr
, &len
); // mod_time
258 read_unsigned_LEB_128(lineptr
, &len
); // filelength
261 gold_assert(fileindex
262 == static_cast<int>(this->files_
.back().size()));
263 this->files_
.back().push_back(std::make_pair(dirindexi
, filename
));
272 // Process a single opcode in the .debug.line structure.
274 template<int size
, bool big_endian
>
276 Sized_dwarf_line_info
<size
, big_endian
>::process_one_opcode(
277 const unsigned char* start
, struct LineStateMachine
* lsm
, size_t* len
)
281 unsigned char opcode
= *start
;
285 // If the opcode is great than the opcode_base, it is a special
286 // opcode. Most line programs consist mainly of special opcodes.
287 if (opcode
>= header_
.opcode_base
)
289 opcode
-= header_
.opcode_base
;
290 const int advance_address
= ((opcode
/ header_
.line_range
)
291 * header_
.min_insn_length
);
292 lsm
->address
+= advance_address
;
294 const int advance_line
= ((opcode
% header_
.line_range
)
295 + header_
.line_base
);
296 lsm
->line_num
+= advance_line
;
297 lsm
->basic_block
= true;
302 // Otherwise, we have the regular opcodes
305 case elfcpp::DW_LNS_copy
:
306 lsm
->basic_block
= false;
310 case elfcpp::DW_LNS_advance_pc
:
312 const uint64_t advance_address
313 = read_unsigned_LEB_128(start
, &templen
);
315 lsm
->address
+= header_
.min_insn_length
* advance_address
;
319 case elfcpp::DW_LNS_advance_line
:
321 const uint64_t advance_line
= read_signed_LEB_128(start
, &templen
);
323 lsm
->line_num
+= advance_line
;
327 case elfcpp::DW_LNS_set_file
:
329 const uint64_t fileno
= read_unsigned_LEB_128(start
, &templen
);
331 lsm
->file_num
= fileno
;
335 case elfcpp::DW_LNS_set_column
:
337 const uint64_t colno
= read_unsigned_LEB_128(start
, &templen
);
339 lsm
->column_num
= colno
;
343 case elfcpp::DW_LNS_negate_stmt
:
344 lsm
->is_stmt
= !lsm
->is_stmt
;
347 case elfcpp::DW_LNS_set_basic_block
:
348 lsm
->basic_block
= true;
351 case elfcpp::DW_LNS_fixed_advance_pc
:
354 advance_address
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(start
);
356 lsm
->address
+= advance_address
;
360 case elfcpp::DW_LNS_const_add_pc
:
362 const int advance_address
= (header_
.min_insn_length
363 * ((255 - header_
.opcode_base
)
364 / header_
.line_range
));
365 lsm
->address
+= advance_address
;
369 case elfcpp::DW_LNS_extended_op
:
371 const uint64_t extended_op_len
372 = read_unsigned_LEB_128(start
, &templen
);
374 oplen
+= templen
+ extended_op_len
;
376 const unsigned char extended_op
= *start
;
381 case elfcpp::DW_LNE_end_sequence
:
382 // This means that the current byte is the one immediately
383 // after a set of instructions. Record the current line
384 // for up to one less than the current address.
386 lsm
->end_sequence
= true;
390 case elfcpp::DW_LNE_set_address
:
393 elfcpp::Swap_unaligned
<size
, big_endian
>::readval(start
);
394 typename
Reloc_map::const_iterator it
395 = this->reloc_map_
.find(start
- this->buffer_
);
396 if (it
!= reloc_map_
.end())
398 // If this is a SHT_RELA section, then ignore the
399 // section contents. This assumes that this is a
400 // straight reloc which just uses the reloc addend.
401 // The reloc addend has already been included in the
403 if (this->track_relocs_type_
== elfcpp::SHT_RELA
)
405 // Add in the symbol value.
406 lsm
->address
+= it
->second
.second
;
407 lsm
->shndx
= it
->second
.first
;
411 // If we're a normal .o file, with relocs, every
412 // set_address should have an associated relocation.
413 if (this->input_is_relobj())
414 this->data_valid_
= false;
418 case elfcpp::DW_LNE_define_file
:
420 const char* filename
= reinterpret_cast<const char*>(start
);
421 templen
= strlen(filename
) + 1;
424 uint64_t dirindex
= read_unsigned_LEB_128(start
, &templen
);
427 if (dirindex
>= this->directories_
.back().size())
429 int dirindexi
= static_cast<int>(dirindex
);
431 read_unsigned_LEB_128(start
, &templen
); // mod_time
434 read_unsigned_LEB_128(start
, &templen
); // filelength
437 this->files_
.back().push_back(std::make_pair(dirindexi
,
447 // Ignore unknown opcode silently
448 for (int i
= 0; i
< header_
.std_opcode_lengths
[opcode
]; i
++)
451 read_unsigned_LEB_128(start
, &templen
);
462 // Read the debug information at LINEPTR and store it in the line
465 template<int size
, bool big_endian
>
467 Sized_dwarf_line_info
<size
, big_endian
>::read_lines(unsigned const char* lineptr
,
470 struct LineStateMachine lsm
;
472 // LENGTHSTART is the place the length field is based on. It is the
473 // point in the header after the initial length field.
474 const unsigned char* lengthstart
= buffer_
;
476 // In 64 bit dwarf, the initial length is 12 bytes, because of the
477 // 0xffffffff at the start.
478 if (header_
.offset_size
== 8)
483 while (lineptr
< lengthstart
+ header_
.total_length
)
485 ResetLineStateMachine(&lsm
, header_
.default_is_stmt
);
486 while (!lsm
.end_sequence
)
489 bool add_line
= this->process_one_opcode(lineptr
, &lsm
, &oplength
);
491 && (shndx
== -1U || lsm
.shndx
== -1U || shndx
== lsm
.shndx
))
493 Offset_to_lineno_entry entry
494 = { lsm
.address
, this->current_header_index_
,
495 lsm
.file_num
, true, lsm
.line_num
};
496 std::vector
<Offset_to_lineno_entry
>&
497 map(this->line_number_map_
[lsm
.shndx
]);
498 // If we see two consecutive entries with the same
499 // offset and a real line number, then mark the first
500 // one as non-canonical.
502 && (map
.back().offset
== static_cast<off_t
>(lsm
.address
))
503 && lsm
.line_num
!= -1
504 && map
.back().line_num
!= -1)
505 map
.back().last_line_for_offset
= false;
506 map
.push_back(entry
);
512 return lengthstart
+ header_
.total_length
;
515 // Looks in the symtab to see what section a symbol is in.
517 template<int size
, bool big_endian
>
519 Sized_dwarf_line_info
<size
, big_endian
>::symbol_section(
522 typename
elfcpp::Elf_types
<size
>::Elf_Addr
* value
,
525 const int symsize
= elfcpp::Elf_sizes
<size
>::sym_size
;
526 gold_assert(sym
* symsize
< this->symtab_buffer_size_
);
527 elfcpp::Sym
<size
, big_endian
> elfsym(this->symtab_buffer_
+ sym
* symsize
);
528 *value
= elfsym
.get_st_value();
529 return object
->adjust_sym_shndx(sym
, elfsym
.get_st_shndx(), is_ordinary
);
532 // Read the relocations into a Reloc_map.
534 template<int size
, bool big_endian
>
536 Sized_dwarf_line_info
<size
, big_endian
>::read_relocs(Object
* object
)
538 if (this->symtab_buffer_
== NULL
)
541 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
543 while ((reloc_offset
= this->track_relocs_
.next_offset()) != -1)
545 const unsigned int sym
= this->track_relocs_
.next_symndx();
548 const unsigned int shndx
= this->symbol_section(object
, sym
, &value
,
551 // There is no reason to record non-ordinary section indexes, or
552 // SHN_UNDEF, because they will never match the real section.
553 if (is_ordinary
&& shndx
!= elfcpp::SHN_UNDEF
)
555 value
+= this->track_relocs_
.next_addend();
556 this->reloc_map_
[reloc_offset
] = std::make_pair(shndx
, value
);
559 this->track_relocs_
.advance(reloc_offset
+ 1);
563 // Read the line number info.
565 template<int size
, bool big_endian
>
567 Sized_dwarf_line_info
<size
, big_endian
>::read_line_mappings(Object
* object
,
570 gold_assert(this->data_valid_
== true);
572 this->read_relocs(object
);
573 while (this->buffer_
< this->buffer_end_
)
575 const unsigned char* lineptr
= this->buffer_
;
576 lineptr
= this->read_header_prolog(lineptr
);
577 lineptr
= this->read_header_tables(lineptr
);
578 lineptr
= this->read_lines(lineptr
, shndx
);
579 this->buffer_
= lineptr
;
582 // Sort the lines numbers, so addr2line can use binary search.
583 for (typename
Lineno_map::iterator it
= line_number_map_
.begin();
584 it
!= line_number_map_
.end();
586 // Each vector needs to be sorted by offset.
587 std::sort(it
->second
.begin(), it
->second
.end());
590 // Some processing depends on whether the input is a .o file or not.
591 // For instance, .o files have relocs, and have .debug_lines
592 // information on a per section basis. .so files, on the other hand,
593 // lack relocs, and offsets are unique, so we can ignore the section
596 template<int size
, bool big_endian
>
598 Sized_dwarf_line_info
<size
, big_endian
>::input_is_relobj()
600 // Only .o files have relocs and the symtab buffer that goes with them.
601 return this->symtab_buffer_
!= NULL
;
604 // Given an Offset_to_lineno_entry vector, and an offset, figure out
605 // if the offset points into a function according to the vector (see
606 // comments below for the algorithm). If it does, return an iterator
607 // into the vector that points to the line-number that contains that
608 // offset. If not, it returns vector::end().
610 static std::vector
<Offset_to_lineno_entry
>::const_iterator
611 offset_to_iterator(const std::vector
<Offset_to_lineno_entry
>* offsets
,
614 const Offset_to_lineno_entry lookup_key
= { offset
, 0, 0, true, 0 };
616 // lower_bound() returns the smallest offset which is >= lookup_key.
617 // If no offset in offsets is >= lookup_key, returns end().
618 std::vector
<Offset_to_lineno_entry
>::const_iterator it
619 = std::lower_bound(offsets
->begin(), offsets
->end(), lookup_key
);
621 // This code is easiest to understand with a concrete example.
622 // Here's a possible offsets array:
623 // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0
624 // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1
625 // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2
626 // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3
627 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4
628 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5
629 // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6
630 // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7
631 // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8
632 // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9
633 // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10
634 // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11
635 // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12
636 // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13
637 // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14
638 // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15
639 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16
640 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17
641 // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18
642 // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19
643 // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20
644 // The entries with line_num == -1 mark the end of a function: the
645 // associated offset is one past the last instruction in the
646 // function. This can correspond to the beginning of the next
647 // function (as is true for offset 3232); alternately, there can be
648 // a gap between the end of one function and the start of the next
649 // (as is true for some others, most obviously from 3236->5764).
651 // Case 1: lookup_key has offset == 10. lower_bound returns
652 // offsets[0]. Since it's not an exact match and we're
653 // at the beginning of offsets, we return end() (invalid).
654 // Case 2: lookup_key has offset 10000. lower_bound returns
655 // offset[21] (end()). We return end() (invalid).
656 // Case 3: lookup_key has offset == 3211. lower_bound matches
657 // offsets[0] exactly, and that's the entry we return.
658 // Case 4: lookup_key has offset == 3232. lower_bound returns
659 // offsets[4]. That's an exact match, but indicates
660 // end-of-function. We check if offsets[5] is also an
661 // exact match but not end-of-function. It is, so we
662 // return offsets[5].
663 // Case 5: lookup_key has offset == 3214. lower_bound returns
664 // offsets[1]. Since it's not an exact match, we back
665 // up to the offset that's < lookup_key, offsets[0].
666 // We note offsets[0] is a valid entry (not end-of-function),
667 // so that's the entry we return.
668 // Case 6: lookup_key has offset == 4000. lower_bound returns
669 // offsets[8]. Since it's not an exact match, we back
670 // up to offsets[7]. Since offsets[7] indicates
671 // end-of-function, we know lookup_key is between
672 // functions, so we return end() (not a valid offset).
673 // Case 7: lookup_key has offset == 5794. lower_bound returns
674 // offsets[19]. Since it's not an exact match, we back
675 // up to offsets[16]. Note we back up to the *first*
676 // entry with offset 5793, not just offsets[19-1].
677 // We note offsets[16] is a valid entry, so we return it.
678 // If offsets[16] had had line_num == -1, we would have
679 // checked offsets[17]. The reason for this is that
680 // 16 and 17 can be in an arbitrary order, since we sort
681 // only by offset and last_line_for_offset. (Note it
682 // doesn't help to use line_number as a tertiary sort key,
683 // since sometimes we want the -1 to be first and sometimes
684 // we want it to be last.)
686 // This deals with cases (1) and (2).
687 if ((it
== offsets
->begin() && offset
< it
->offset
)
688 || it
== offsets
->end())
689 return offsets
->end();
691 // This deals with cases (3) and (4).
692 if (offset
== it
->offset
)
694 while (it
!= offsets
->end()
695 && it
->offset
== offset
696 && it
->line_num
== -1)
698 if (it
== offsets
->end() || it
->offset
!= offset
)
699 return offsets
->end();
704 // This handles the first part of case (7) -- we back up to the
705 // *first* entry that has the offset that's behind us.
706 gold_assert(it
!= offsets
->begin());
707 std::vector
<Offset_to_lineno_entry
>::const_iterator range_end
= it
;
709 const off_t range_value
= it
->offset
;
710 while (it
!= offsets
->begin() && (it
-1)->offset
== range_value
)
713 // This handles cases (5), (6), and (7): if any entry in the
714 // equal_range [it, range_end) has a line_num != -1, it's a valid
715 // match. If not, we're not in a function. The line number we saw
716 // last for an offset will be sorted first, so it'll get returned if
718 for (; it
!= range_end
; ++it
)
719 if (it
->line_num
!= -1)
721 return offsets
->end();
724 // Returns the canonical filename:lineno for the address passed in.
725 // If other_lines is not NULL, appends the non-canonical lines
726 // assigned to the same address.
728 template<int size
, bool big_endian
>
730 Sized_dwarf_line_info
<size
, big_endian
>::do_addr2line(
733 std::vector
<std::string
>* other_lines
)
735 if (this->data_valid_
== false)
738 const std::vector
<Offset_to_lineno_entry
>* offsets
;
739 // If we do not have reloc information, then our input is a .so or
740 // some similar data structure where all the information is held in
741 // the offset. In that case, we ignore the input shndx.
742 if (this->input_is_relobj())
743 offsets
= &this->line_number_map_
[shndx
];
745 offsets
= &this->line_number_map_
[-1U];
746 if (offsets
->empty())
749 typename
std::vector
<Offset_to_lineno_entry
>::const_iterator it
750 = offset_to_iterator(offsets
, offset
);
751 if (it
== offsets
->end())
754 std::string result
= this->format_file_lineno(*it
);
755 if (other_lines
!= NULL
)
756 for (++it
; it
!= offsets
->end() && it
->offset
== offset
; ++it
)
758 if (it
->line_num
== -1)
759 continue; // The end of a previous function.
760 other_lines
->push_back(this->format_file_lineno(*it
));
765 // Convert the file_num + line_num into a string.
767 template<int size
, bool big_endian
>
769 Sized_dwarf_line_info
<size
, big_endian
>::format_file_lineno(
770 const Offset_to_lineno_entry
& loc
) const
774 gold_assert(loc
.header_num
< static_cast<int>(this->files_
.size()));
775 gold_assert(loc
.file_num
776 < static_cast<int>(this->files_
[loc
.header_num
].size()));
777 const std::pair
<int, std::string
>& filename_pair
778 = this->files_
[loc
.header_num
][loc
.file_num
];
779 const std::string
& filename
= filename_pair
.second
;
781 gold_assert(loc
.header_num
< static_cast<int>(this->directories_
.size()));
782 gold_assert(filename_pair
.first
783 < static_cast<int>(this->directories_
[loc
.header_num
].size()));
784 const std::string
& dirname
785 = this->directories_
[loc
.header_num
][filename_pair
.first
];
787 if (!dirname
.empty())
796 char buffer
[64]; // enough to hold a line number
797 snprintf(buffer
, sizeof(buffer
), "%d", loc
.line_num
);
804 // Dwarf_line_info routines.
806 static unsigned int next_generation_count
= 0;
808 struct Addr2line_cache_entry
812 Dwarf_line_info
* dwarf_line_info
;
813 unsigned int generation_count
;
814 unsigned int access_count
;
816 Addr2line_cache_entry(Object
* o
, unsigned int s
, Dwarf_line_info
* d
)
817 : object(o
), shndx(s
), dwarf_line_info(d
),
818 generation_count(next_generation_count
), access_count(0)
820 if (next_generation_count
< (1U << 31))
821 ++next_generation_count
;
824 // We expect this cache to be small, so don't bother with a hashtable
825 // or priority queue or anything: just use a simple vector.
826 static std::vector
<Addr2line_cache_entry
> addr2line_cache
;
829 Dwarf_line_info::one_addr2line(Object
* object
,
830 unsigned int shndx
, off_t offset
,
832 std::vector
<std::string
>* other_lines
)
834 Dwarf_line_info
* lineinfo
= NULL
;
835 std::vector
<Addr2line_cache_entry
>::iterator it
;
837 // First, check the cache. If we hit, update the counts.
838 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
840 if (it
->object
== object
&& it
->shndx
== shndx
)
842 lineinfo
= it
->dwarf_line_info
;
843 it
->generation_count
= next_generation_count
;
844 // We cap generation_count at 2^31 -1 to avoid overflow.
845 if (next_generation_count
< (1U << 31))
846 ++next_generation_count
;
847 // We cap access_count at 31 so 2^access_count doesn't overflow
848 if (it
->access_count
< 31)
854 // If we don't hit the cache, create a new object and insert into the
856 if (lineinfo
== NULL
)
858 switch (parameters
->size_and_endianness())
860 #ifdef HAVE_TARGET_32_LITTLE
861 case Parameters::TARGET_32_LITTLE
:
862 lineinfo
= new Sized_dwarf_line_info
<32, false>(object
, shndx
); break;
864 #ifdef HAVE_TARGET_32_BIG
865 case Parameters::TARGET_32_BIG
:
866 lineinfo
= new Sized_dwarf_line_info
<32, true>(object
, shndx
); break;
868 #ifdef HAVE_TARGET_64_LITTLE
869 case Parameters::TARGET_64_LITTLE
:
870 lineinfo
= new Sized_dwarf_line_info
<64, false>(object
, shndx
); break;
872 #ifdef HAVE_TARGET_64_BIG
873 case Parameters::TARGET_64_BIG
:
874 lineinfo
= new Sized_dwarf_line_info
<64, true>(object
, shndx
); break;
879 addr2line_cache
.push_back(Addr2line_cache_entry(object
, shndx
, lineinfo
));
882 // Now that we have our object, figure out the answer
883 std::string retval
= lineinfo
->addr2line(shndx
, offset
, other_lines
);
885 // Finally, if our cache has grown too big, delete old objects. We
886 // assume the common (probably only) case is deleting only one object.
887 // We use a pretty simple scheme to evict: function of LRU and MFU.
888 while (addr2line_cache
.size() > cache_size
)
890 unsigned int lowest_score
= ~0U;
891 std::vector
<Addr2line_cache_entry
>::iterator lowest
892 = addr2line_cache
.end();
893 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
895 const unsigned int score
= (it
->generation_count
896 + (1U << it
->access_count
));
897 if (score
< lowest_score
)
899 lowest_score
= score
;
903 if (lowest
!= addr2line_cache
.end())
905 delete lowest
->dwarf_line_info
;
906 addr2line_cache
.erase(lowest
);
914 Dwarf_line_info::clear_addr2line_cache()
916 for (std::vector
<Addr2line_cache_entry
>::iterator it
= addr2line_cache
.begin();
917 it
!= addr2line_cache
.end();
919 delete it
->dwarf_line_info
;
920 addr2line_cache
.clear();
923 #ifdef HAVE_TARGET_32_LITTLE
925 class Sized_dwarf_line_info
<32, false>;
928 #ifdef HAVE_TARGET_32_BIG
930 class Sized_dwarf_line_info
<32, true>;
933 #ifdef HAVE_TARGET_64_LITTLE
935 class Sized_dwarf_line_info
<64, false>;
938 #ifdef HAVE_TARGET_64_BIG
940 class Sized_dwarf_line_info
<64, true>;
943 } // End namespace gold.