1 // dwarf_reader.cc -- parse dwarf2/3 debug information
3 // Copyright 2007, 2008, 2009, 2010, 2011 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(
67 unsigned int read_shndx
)
68 : data_valid_(false), buffer_(NULL
), buffer_start_(NULL
),
69 symtab_buffer_(NULL
), directories_(), files_(), current_header_index_(-1)
71 unsigned int debug_shndx
;
73 for (debug_shndx
= 1; debug_shndx
< object
->shnum(); ++debug_shndx
)
75 // FIXME: do this more efficiently: section_name() isn't super-fast
76 std::string name
= object
->section_name(debug_shndx
);
77 if (name
== ".debug_line" || name
== ".zdebug_line")
79 section_size_type buffer_size
;
81 this->buffer_
= object
->decompressed_section_contents(debug_shndx
,
85 this->buffer_start_
= this->buffer_
;
86 this->buffer_end_
= this->buffer_
+ buffer_size
;
90 if (this->buffer_
== NULL
)
93 // Find the relocation section for ".debug_line".
94 // We expect these for relobjs (.o's) but not dynobjs (.so's).
95 bool got_relocs
= false;
96 for (unsigned int reloc_shndx
= 0;
97 reloc_shndx
< object
->shnum();
100 unsigned int reloc_sh_type
= object
->section_type(reloc_shndx
);
101 if ((reloc_sh_type
== elfcpp::SHT_REL
102 || reloc_sh_type
== elfcpp::SHT_RELA
)
103 && object
->section_info(reloc_shndx
) == debug_shndx
)
105 got_relocs
= this->track_relocs_
.initialize(object
, reloc_shndx
,
107 this->track_relocs_type_
= reloc_sh_type
;
112 // Finally, we need the symtab section to interpret the relocs.
115 unsigned int symtab_shndx
;
116 for (symtab_shndx
= 0; symtab_shndx
< object
->shnum(); ++symtab_shndx
)
117 if (object
->section_type(symtab_shndx
) == elfcpp::SHT_SYMTAB
)
119 this->symtab_buffer_
= object
->section_contents(
120 symtab_shndx
, &this->symtab_buffer_size_
, false);
123 if (this->symtab_buffer_
== NULL
)
127 // Now that we have successfully read all the data, parse the debug
129 this->data_valid_
= true;
130 this->read_line_mappings(object
, read_shndx
);
133 // Read the DWARF header.
135 template<int size
, bool big_endian
>
137 Sized_dwarf_line_info
<size
, big_endian
>::read_header_prolog(
138 const unsigned char* lineptr
)
140 uint32_t initial_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
143 // In DWARF2/3, if the initial length is all 1 bits, then the offset
144 // size is 8 and we need to read the next 8 bytes for the real length.
145 if (initial_length
== 0xffffffff)
147 header_
.offset_size
= 8;
148 initial_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
152 header_
.offset_size
= 4;
154 header_
.total_length
= initial_length
;
156 gold_assert(lineptr
+ header_
.total_length
<= buffer_end_
);
158 header_
.version
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(lineptr
);
161 if (header_
.offset_size
== 4)
162 header_
.prologue_length
= elfcpp::Swap_unaligned
<32, big_endian
>::readval(lineptr
);
164 header_
.prologue_length
= elfcpp::Swap_unaligned
<64, big_endian
>::readval(lineptr
);
165 lineptr
+= header_
.offset_size
;
167 header_
.min_insn_length
= *lineptr
;
170 header_
.default_is_stmt
= *lineptr
;
173 header_
.line_base
= *reinterpret_cast<const signed char*>(lineptr
);
176 header_
.line_range
= *lineptr
;
179 header_
.opcode_base
= *lineptr
;
182 header_
.std_opcode_lengths
.resize(header_
.opcode_base
+ 1);
183 header_
.std_opcode_lengths
[0] = 0;
184 for (int i
= 1; i
< header_
.opcode_base
; i
++)
186 header_
.std_opcode_lengths
[i
] = *lineptr
;
193 // The header for a debug_line section is mildly complicated, because
194 // the line info is very tightly encoded.
196 template<int size
, bool big_endian
>
198 Sized_dwarf_line_info
<size
, big_endian
>::read_header_tables(
199 const unsigned char* lineptr
)
201 ++this->current_header_index_
;
203 // Create a new directories_ entry and a new files_ entry for our new
204 // header. We initialize each with a single empty element, because
205 // dwarf indexes directory and filenames starting at 1.
206 gold_assert(static_cast<int>(this->directories_
.size())
207 == this->current_header_index_
);
208 gold_assert(static_cast<int>(this->files_
.size())
209 == this->current_header_index_
);
210 this->directories_
.push_back(std::vector
<std::string
>(1));
211 this->files_
.push_back(std::vector
<std::pair
<int, std::string
> >(1));
213 // It is legal for the directory entry table to be empty.
219 const char* dirname
= reinterpret_cast<const char*>(lineptr
);
221 == static_cast<int>(this->directories_
.back().size()));
222 this->directories_
.back().push_back(dirname
);
223 lineptr
+= this->directories_
.back().back().size() + 1;
229 // It is also legal for the file entry table to be empty.
236 const char* filename
= reinterpret_cast<const char*>(lineptr
);
237 lineptr
+= strlen(filename
) + 1;
239 uint64_t dirindex
= read_unsigned_LEB_128(lineptr
, &len
);
242 if (dirindex
>= this->directories_
.back().size())
244 int dirindexi
= static_cast<int>(dirindex
);
246 read_unsigned_LEB_128(lineptr
, &len
); // mod_time
249 read_unsigned_LEB_128(lineptr
, &len
); // filelength
252 gold_assert(fileindex
253 == static_cast<int>(this->files_
.back().size()));
254 this->files_
.back().push_back(std::make_pair(dirindexi
, filename
));
263 // Process a single opcode in the .debug.line structure.
265 template<int size
, bool big_endian
>
267 Sized_dwarf_line_info
<size
, big_endian
>::process_one_opcode(
268 const unsigned char* start
, struct LineStateMachine
* lsm
, size_t* len
)
272 unsigned char opcode
= *start
;
276 // If the opcode is great than the opcode_base, it is a special
277 // opcode. Most line programs consist mainly of special opcodes.
278 if (opcode
>= header_
.opcode_base
)
280 opcode
-= header_
.opcode_base
;
281 const int advance_address
= ((opcode
/ header_
.line_range
)
282 * header_
.min_insn_length
);
283 lsm
->address
+= advance_address
;
285 const int advance_line
= ((opcode
% header_
.line_range
)
286 + header_
.line_base
);
287 lsm
->line_num
+= advance_line
;
288 lsm
->basic_block
= true;
293 // Otherwise, we have the regular opcodes
296 case elfcpp::DW_LNS_copy
:
297 lsm
->basic_block
= false;
301 case elfcpp::DW_LNS_advance_pc
:
303 const uint64_t advance_address
304 = read_unsigned_LEB_128(start
, &templen
);
306 lsm
->address
+= header_
.min_insn_length
* advance_address
;
310 case elfcpp::DW_LNS_advance_line
:
312 const uint64_t advance_line
= read_signed_LEB_128(start
, &templen
);
314 lsm
->line_num
+= advance_line
;
318 case elfcpp::DW_LNS_set_file
:
320 const uint64_t fileno
= read_unsigned_LEB_128(start
, &templen
);
322 lsm
->file_num
= fileno
;
326 case elfcpp::DW_LNS_set_column
:
328 const uint64_t colno
= read_unsigned_LEB_128(start
, &templen
);
330 lsm
->column_num
= colno
;
334 case elfcpp::DW_LNS_negate_stmt
:
335 lsm
->is_stmt
= !lsm
->is_stmt
;
338 case elfcpp::DW_LNS_set_basic_block
:
339 lsm
->basic_block
= true;
342 case elfcpp::DW_LNS_fixed_advance_pc
:
345 advance_address
= elfcpp::Swap_unaligned
<16, big_endian
>::readval(start
);
347 lsm
->address
+= advance_address
;
351 case elfcpp::DW_LNS_const_add_pc
:
353 const int advance_address
= (header_
.min_insn_length
354 * ((255 - header_
.opcode_base
)
355 / header_
.line_range
));
356 lsm
->address
+= advance_address
;
360 case elfcpp::DW_LNS_extended_op
:
362 const uint64_t extended_op_len
363 = read_unsigned_LEB_128(start
, &templen
);
365 oplen
+= templen
+ extended_op_len
;
367 const unsigned char extended_op
= *start
;
372 case elfcpp::DW_LNE_end_sequence
:
373 // This means that the current byte is the one immediately
374 // after a set of instructions. Record the current line
375 // for up to one less than the current address.
377 lsm
->end_sequence
= true;
381 case elfcpp::DW_LNE_set_address
:
384 elfcpp::Swap_unaligned
<size
, big_endian
>::readval(start
);
385 typename
Reloc_map::const_iterator it
386 = this->reloc_map_
.find(start
- this->buffer_
);
387 if (it
!= reloc_map_
.end())
389 // If this is a SHT_RELA section, then ignore the
390 // section contents. This assumes that this is a
391 // straight reloc which just uses the reloc addend.
392 // The reloc addend has already been included in the
394 if (this->track_relocs_type_
== elfcpp::SHT_RELA
)
396 // Add in the symbol value.
397 lsm
->address
+= it
->second
.second
;
398 lsm
->shndx
= it
->second
.first
;
402 // If we're a normal .o file, with relocs, every
403 // set_address should have an associated relocation.
404 if (this->input_is_relobj())
405 this->data_valid_
= false;
409 case elfcpp::DW_LNE_define_file
:
411 const char* filename
= reinterpret_cast<const char*>(start
);
412 templen
= strlen(filename
) + 1;
415 uint64_t dirindex
= read_unsigned_LEB_128(start
, &templen
);
418 if (dirindex
>= this->directories_
.back().size())
420 int dirindexi
= static_cast<int>(dirindex
);
422 read_unsigned_LEB_128(start
, &templen
); // mod_time
425 read_unsigned_LEB_128(start
, &templen
); // filelength
428 this->files_
.back().push_back(std::make_pair(dirindexi
,
438 // Ignore unknown opcode silently
439 for (int i
= 0; i
< header_
.std_opcode_lengths
[opcode
]; i
++)
442 read_unsigned_LEB_128(start
, &templen
);
453 // Read the debug information at LINEPTR and store it in the line
456 template<int size
, bool big_endian
>
458 Sized_dwarf_line_info
<size
, big_endian
>::read_lines(unsigned const char* lineptr
,
461 struct LineStateMachine lsm
;
463 // LENGTHSTART is the place the length field is based on. It is the
464 // point in the header after the initial length field.
465 const unsigned char* lengthstart
= buffer_
;
467 // In 64 bit dwarf, the initial length is 12 bytes, because of the
468 // 0xffffffff at the start.
469 if (header_
.offset_size
== 8)
474 while (lineptr
< lengthstart
+ header_
.total_length
)
476 ResetLineStateMachine(&lsm
, header_
.default_is_stmt
);
477 while (!lsm
.end_sequence
)
480 bool add_line
= this->process_one_opcode(lineptr
, &lsm
, &oplength
);
482 && (shndx
== -1U || lsm
.shndx
== -1U || shndx
== lsm
.shndx
))
484 Offset_to_lineno_entry entry
485 = { static_cast<off_t
>(lsm
.address
),
486 this->current_header_index_
,
487 static_cast<unsigned int>(lsm
.file_num
),
488 true, lsm
.line_num
};
489 std::vector
<Offset_to_lineno_entry
>&
490 map(this->line_number_map_
[lsm
.shndx
]);
491 // If we see two consecutive entries with the same
492 // offset and a real line number, then mark the first
493 // one as non-canonical.
495 && (map
.back().offset
== static_cast<off_t
>(lsm
.address
))
496 && lsm
.line_num
!= -1
497 && map
.back().line_num
!= -1)
498 map
.back().last_line_for_offset
= false;
499 map
.push_back(entry
);
505 return lengthstart
+ header_
.total_length
;
508 // Looks in the symtab to see what section a symbol is in.
510 template<int size
, bool big_endian
>
512 Sized_dwarf_line_info
<size
, big_endian
>::symbol_section(
515 typename
elfcpp::Elf_types
<size
>::Elf_Addr
* value
,
518 const int symsize
= elfcpp::Elf_sizes
<size
>::sym_size
;
519 gold_assert(sym
* symsize
< this->symtab_buffer_size_
);
520 elfcpp::Sym
<size
, big_endian
> elfsym(this->symtab_buffer_
+ sym
* symsize
);
521 *value
= elfsym
.get_st_value();
522 return object
->adjust_sym_shndx(sym
, elfsym
.get_st_shndx(), is_ordinary
);
525 // Read the relocations into a Reloc_map.
527 template<int size
, bool big_endian
>
529 Sized_dwarf_line_info
<size
, big_endian
>::read_relocs(Object
* object
)
531 if (this->symtab_buffer_
== NULL
)
534 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
536 while ((reloc_offset
= this->track_relocs_
.next_offset()) != -1)
538 const unsigned int sym
= this->track_relocs_
.next_symndx();
541 const unsigned int shndx
= this->symbol_section(object
, sym
, &value
,
544 // There is no reason to record non-ordinary section indexes, or
545 // SHN_UNDEF, because they will never match the real section.
546 if (is_ordinary
&& shndx
!= elfcpp::SHN_UNDEF
)
548 value
+= this->track_relocs_
.next_addend();
549 this->reloc_map_
[reloc_offset
] = std::make_pair(shndx
, value
);
552 this->track_relocs_
.advance(reloc_offset
+ 1);
556 // Read the line number info.
558 template<int size
, bool big_endian
>
560 Sized_dwarf_line_info
<size
, big_endian
>::read_line_mappings(Object
* object
,
563 gold_assert(this->data_valid_
== true);
565 this->read_relocs(object
);
566 while (this->buffer_
< this->buffer_end_
)
568 const unsigned char* lineptr
= this->buffer_
;
569 lineptr
= this->read_header_prolog(lineptr
);
570 lineptr
= this->read_header_tables(lineptr
);
571 lineptr
= this->read_lines(lineptr
, shndx
);
572 this->buffer_
= lineptr
;
575 // Sort the lines numbers, so addr2line can use binary search.
576 for (typename
Lineno_map::iterator it
= line_number_map_
.begin();
577 it
!= line_number_map_
.end();
579 // Each vector needs to be sorted by offset.
580 std::sort(it
->second
.begin(), it
->second
.end());
583 // Some processing depends on whether the input is a .o file or not.
584 // For instance, .o files have relocs, and have .debug_lines
585 // information on a per section basis. .so files, on the other hand,
586 // lack relocs, and offsets are unique, so we can ignore the section
589 template<int size
, bool big_endian
>
591 Sized_dwarf_line_info
<size
, big_endian
>::input_is_relobj()
593 // Only .o files have relocs and the symtab buffer that goes with them.
594 return this->symtab_buffer_
!= NULL
;
597 // Given an Offset_to_lineno_entry vector, and an offset, figure out
598 // if the offset points into a function according to the vector (see
599 // comments below for the algorithm). If it does, return an iterator
600 // into the vector that points to the line-number that contains that
601 // offset. If not, it returns vector::end().
603 static std::vector
<Offset_to_lineno_entry
>::const_iterator
604 offset_to_iterator(const std::vector
<Offset_to_lineno_entry
>* offsets
,
607 const Offset_to_lineno_entry lookup_key
= { offset
, 0, 0, true, 0 };
609 // lower_bound() returns the smallest offset which is >= lookup_key.
610 // If no offset in offsets is >= lookup_key, returns end().
611 std::vector
<Offset_to_lineno_entry
>::const_iterator it
612 = std::lower_bound(offsets
->begin(), offsets
->end(), lookup_key
);
614 // This code is easiest to understand with a concrete example.
615 // Here's a possible offsets array:
616 // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16}, // 0
617 // {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20}, // 1
618 // {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22}, // 2
619 // {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25}, // 3
620 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1}, // 4
621 // {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65}, // 5
622 // {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66}, // 6
623 // {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1}, // 7
624 // {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48}, // 8
625 // {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47}, // 9
626 // {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49}, // 10
627 // {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50}, // 11
628 // {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51}, // 12
629 // {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1}, // 13
630 // {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19}, // 14
631 // {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20}, // 15
632 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67}, // 16
633 // {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1}, // 17
634 // {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66}, // 18
635 // {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68}, // 19
636 // {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1}, // 20
637 // The entries with line_num == -1 mark the end of a function: the
638 // associated offset is one past the last instruction in the
639 // function. This can correspond to the beginning of the next
640 // function (as is true for offset 3232); alternately, there can be
641 // a gap between the end of one function and the start of the next
642 // (as is true for some others, most obviously from 3236->5764).
644 // Case 1: lookup_key has offset == 10. lower_bound returns
645 // offsets[0]. Since it's not an exact match and we're
646 // at the beginning of offsets, we return end() (invalid).
647 // Case 2: lookup_key has offset 10000. lower_bound returns
648 // offset[21] (end()). We return end() (invalid).
649 // Case 3: lookup_key has offset == 3211. lower_bound matches
650 // offsets[0] exactly, and that's the entry we return.
651 // Case 4: lookup_key has offset == 3232. lower_bound returns
652 // offsets[4]. That's an exact match, but indicates
653 // end-of-function. We check if offsets[5] is also an
654 // exact match but not end-of-function. It is, so we
655 // return offsets[5].
656 // Case 5: lookup_key has offset == 3214. lower_bound returns
657 // offsets[1]. Since it's not an exact match, we back
658 // up to the offset that's < lookup_key, offsets[0].
659 // We note offsets[0] is a valid entry (not end-of-function),
660 // so that's the entry we return.
661 // Case 6: lookup_key has offset == 4000. lower_bound returns
662 // offsets[8]. Since it's not an exact match, we back
663 // up to offsets[7]. Since offsets[7] indicates
664 // end-of-function, we know lookup_key is between
665 // functions, so we return end() (not a valid offset).
666 // Case 7: lookup_key has offset == 5794. lower_bound returns
667 // offsets[19]. Since it's not an exact match, we back
668 // up to offsets[16]. Note we back up to the *first*
669 // entry with offset 5793, not just offsets[19-1].
670 // We note offsets[16] is a valid entry, so we return it.
671 // If offsets[16] had had line_num == -1, we would have
672 // checked offsets[17]. The reason for this is that
673 // 16 and 17 can be in an arbitrary order, since we sort
674 // only by offset and last_line_for_offset. (Note it
675 // doesn't help to use line_number as a tertiary sort key,
676 // since sometimes we want the -1 to be first and sometimes
677 // we want it to be last.)
679 // This deals with cases (1) and (2).
680 if ((it
== offsets
->begin() && offset
< it
->offset
)
681 || it
== offsets
->end())
682 return offsets
->end();
684 // This deals with cases (3) and (4).
685 if (offset
== it
->offset
)
687 while (it
!= offsets
->end()
688 && it
->offset
== offset
689 && it
->line_num
== -1)
691 if (it
== offsets
->end() || it
->offset
!= offset
)
692 return offsets
->end();
697 // This handles the first part of case (7) -- we back up to the
698 // *first* entry that has the offset that's behind us.
699 gold_assert(it
!= offsets
->begin());
700 std::vector
<Offset_to_lineno_entry
>::const_iterator range_end
= it
;
702 const off_t range_value
= it
->offset
;
703 while (it
!= offsets
->begin() && (it
-1)->offset
== range_value
)
706 // This handles cases (5), (6), and (7): if any entry in the
707 // equal_range [it, range_end) has a line_num != -1, it's a valid
708 // match. If not, we're not in a function. The line number we saw
709 // last for an offset will be sorted first, so it'll get returned if
711 for (; it
!= range_end
; ++it
)
712 if (it
->line_num
!= -1)
714 return offsets
->end();
717 // Returns the canonical filename:lineno for the address passed in.
718 // If other_lines is not NULL, appends the non-canonical lines
719 // assigned to the same address.
721 template<int size
, bool big_endian
>
723 Sized_dwarf_line_info
<size
, big_endian
>::do_addr2line(
726 std::vector
<std::string
>* other_lines
)
728 if (this->data_valid_
== false)
731 const std::vector
<Offset_to_lineno_entry
>* offsets
;
732 // If we do not have reloc information, then our input is a .so or
733 // some similar data structure where all the information is held in
734 // the offset. In that case, we ignore the input shndx.
735 if (this->input_is_relobj())
736 offsets
= &this->line_number_map_
[shndx
];
738 offsets
= &this->line_number_map_
[-1U];
739 if (offsets
->empty())
742 typename
std::vector
<Offset_to_lineno_entry
>::const_iterator it
743 = offset_to_iterator(offsets
, offset
);
744 if (it
== offsets
->end())
747 std::string result
= this->format_file_lineno(*it
);
748 if (other_lines
!= NULL
)
749 for (++it
; it
!= offsets
->end() && it
->offset
== offset
; ++it
)
751 if (it
->line_num
== -1)
752 continue; // The end of a previous function.
753 other_lines
->push_back(this->format_file_lineno(*it
));
758 // Convert the file_num + line_num into a string.
760 template<int size
, bool big_endian
>
762 Sized_dwarf_line_info
<size
, big_endian
>::format_file_lineno(
763 const Offset_to_lineno_entry
& loc
) const
767 gold_assert(loc
.header_num
< static_cast<int>(this->files_
.size()));
768 gold_assert(loc
.file_num
769 < static_cast<int>(this->files_
[loc
.header_num
].size()));
770 const std::pair
<int, std::string
>& filename_pair
771 = this->files_
[loc
.header_num
][loc
.file_num
];
772 const std::string
& filename
= filename_pair
.second
;
774 gold_assert(loc
.header_num
< static_cast<int>(this->directories_
.size()));
775 gold_assert(filename_pair
.first
776 < static_cast<int>(this->directories_
[loc
.header_num
].size()));
777 const std::string
& dirname
778 = this->directories_
[loc
.header_num
][filename_pair
.first
];
780 if (!dirname
.empty())
789 char buffer
[64]; // enough to hold a line number
790 snprintf(buffer
, sizeof(buffer
), "%d", loc
.line_num
);
797 // Dwarf_line_info routines.
799 static unsigned int next_generation_count
= 0;
801 struct Addr2line_cache_entry
805 Dwarf_line_info
* dwarf_line_info
;
806 unsigned int generation_count
;
807 unsigned int access_count
;
809 Addr2line_cache_entry(Object
* o
, unsigned int s
, Dwarf_line_info
* d
)
810 : object(o
), shndx(s
), dwarf_line_info(d
),
811 generation_count(next_generation_count
), access_count(0)
813 if (next_generation_count
< (1U << 31))
814 ++next_generation_count
;
817 // We expect this cache to be small, so don't bother with a hashtable
818 // or priority queue or anything: just use a simple vector.
819 static std::vector
<Addr2line_cache_entry
> addr2line_cache
;
822 Dwarf_line_info::one_addr2line(Object
* object
,
823 unsigned int shndx
, off_t offset
,
825 std::vector
<std::string
>* other_lines
)
827 Dwarf_line_info
* lineinfo
= NULL
;
828 std::vector
<Addr2line_cache_entry
>::iterator it
;
830 // First, check the cache. If we hit, update the counts.
831 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
833 if (it
->object
== object
&& it
->shndx
== shndx
)
835 lineinfo
= it
->dwarf_line_info
;
836 it
->generation_count
= next_generation_count
;
837 // We cap generation_count at 2^31 -1 to avoid overflow.
838 if (next_generation_count
< (1U << 31))
839 ++next_generation_count
;
840 // We cap access_count at 31 so 2^access_count doesn't overflow
841 if (it
->access_count
< 31)
847 // If we don't hit the cache, create a new object and insert into the
849 if (lineinfo
== NULL
)
851 switch (parameters
->size_and_endianness())
853 #ifdef HAVE_TARGET_32_LITTLE
854 case Parameters::TARGET_32_LITTLE
:
855 lineinfo
= new Sized_dwarf_line_info
<32, false>(object
, shndx
); break;
857 #ifdef HAVE_TARGET_32_BIG
858 case Parameters::TARGET_32_BIG
:
859 lineinfo
= new Sized_dwarf_line_info
<32, true>(object
, shndx
); break;
861 #ifdef HAVE_TARGET_64_LITTLE
862 case Parameters::TARGET_64_LITTLE
:
863 lineinfo
= new Sized_dwarf_line_info
<64, false>(object
, shndx
); break;
865 #ifdef HAVE_TARGET_64_BIG
866 case Parameters::TARGET_64_BIG
:
867 lineinfo
= new Sized_dwarf_line_info
<64, true>(object
, shndx
); break;
872 addr2line_cache
.push_back(Addr2line_cache_entry(object
, shndx
, lineinfo
));
875 // Now that we have our object, figure out the answer
876 std::string retval
= lineinfo
->addr2line(shndx
, offset
, other_lines
);
878 // Finally, if our cache has grown too big, delete old objects. We
879 // assume the common (probably only) case is deleting only one object.
880 // We use a pretty simple scheme to evict: function of LRU and MFU.
881 while (addr2line_cache
.size() > cache_size
)
883 unsigned int lowest_score
= ~0U;
884 std::vector
<Addr2line_cache_entry
>::iterator lowest
885 = addr2line_cache
.end();
886 for (it
= addr2line_cache
.begin(); it
!= addr2line_cache
.end(); ++it
)
888 const unsigned int score
= (it
->generation_count
889 + (1U << it
->access_count
));
890 if (score
< lowest_score
)
892 lowest_score
= score
;
896 if (lowest
!= addr2line_cache
.end())
898 delete lowest
->dwarf_line_info
;
899 addr2line_cache
.erase(lowest
);
907 Dwarf_line_info::clear_addr2line_cache()
909 for (std::vector
<Addr2line_cache_entry
>::iterator it
= addr2line_cache
.begin();
910 it
!= addr2line_cache
.end();
912 delete it
->dwarf_line_info
;
913 addr2line_cache
.clear();
916 #ifdef HAVE_TARGET_32_LITTLE
918 class Sized_dwarf_line_info
<32, false>;
921 #ifdef HAVE_TARGET_32_BIG
923 class Sized_dwarf_line_info
<32, true>;
926 #ifdef HAVE_TARGET_64_LITTLE
928 class Sized_dwarf_line_info
<64, false>;
931 #ifdef HAVE_TARGET_64_BIG
933 class Sized_dwarf_line_info
<64, true>;
936 } // End namespace gold.