daily update
[deliverable/binutils-gdb.git] / gold / dwarf_reader.cc
CommitLineData
5c2c6c95
ILT
1// dwarf_reader.cc -- parse dwarf2/3 debug information
2
75aea3d0 3// Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
5c2c6c95
ILT
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
04bf7072 25#include <algorithm>
e4e5049b 26#include <vector>
04bf7072 27
5c2c6c95
ILT
28#include "elfcpp_swap.h"
29#include "dwarf.h"
24badc65 30#include "object.h"
a55ce7fe 31#include "parameters.h"
4c50553d 32#include "reloc.h"
5c2c6c95 33#include "dwarf_reader.h"
4f787271 34#include "int_encoding.h"
5c2c6c95 35
62b01cb5 36namespace gold {
5c2c6c95 37
5c2c6c95
ILT
38struct LineStateMachine
39{
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;
48};
49
50static void
51ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
52{
53 lsm->file_num = 1;
54 lsm->address = 0;
55 lsm->line_num = 1;
56 lsm->column_num = 0;
338f2eba 57 lsm->shndx = -1U;
5c2c6c95
ILT
58 lsm->is_stmt = default_is_stmt;
59 lsm->basic_block = false;
60 lsm->end_sequence = false;
61}
62
24badc65 63template<int size, bool big_endian>
9430daf8 64Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object,
75aea3d0 65 unsigned int read_shndx)
c261a0be 66 : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL),
af674d1d 67 directories_(), files_(), current_header_index_(-1)
24badc65
ILT
68{
69 unsigned int debug_shndx;
70 for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx)
10600224 71 // FIXME: do this more efficiently: section_name() isn't super-fast
24badc65
ILT
72 if (object->section_name(debug_shndx) == ".debug_line")
73 {
8383303e
ILT
74 section_size_type buffer_size;
75 this->buffer_ = object->section_contents(debug_shndx, &buffer_size,
76 false);
24badc65
ILT
77 this->buffer_end_ = this->buffer_ + buffer_size;
78 break;
79 }
80 if (this->buffer_ == NULL)
c261a0be 81 return;
24badc65
ILT
82
83 // Find the relocation section for ".debug_line".
af674d1d 84 // We expect these for relobjs (.o's) but not dynobjs (.so's).
24badc65
ILT
85 bool got_relocs = false;
86 for (unsigned int reloc_shndx = 0;
87 reloc_shndx < object->shnum();
88 ++reloc_shndx)
89 {
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)
94 {
95 got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
96 reloc_sh_type);
97 break;
98 }
99 }
24badc65
ILT
100
101 // Finally, we need the symtab section to interpret the relocs.
af674d1d
ILT
102 if (got_relocs)
103 {
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)
107 {
108 this->symtab_buffer_ = object->section_contents(
109 symtab_shndx, &this->symtab_buffer_size_, false);
110 break;
111 }
112 if (this->symtab_buffer_ == NULL)
113 return;
114 }
24badc65
ILT
115
116 // Now that we have successfully read all the data, parse the debug
117 // info.
c261a0be 118 this->data_valid_ = true;
d491d34e 119 this->read_line_mappings(object, read_shndx);
24badc65
ILT
120}
121
5c2c6c95
ILT
122// Read the DWARF header.
123
124template<int size, bool big_endian>
125const unsigned char*
a55ce7fe 126Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
e43872e9 127 const unsigned char* lineptr)
5c2c6c95 128{
deae2a14 129 uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
5c2c6c95
ILT
130 lineptr += 4;
131
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)
135 {
136 header_.offset_size = 8;
deae2a14 137 initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
5c2c6c95
ILT
138 lineptr += 8;
139 }
140 else
141 header_.offset_size = 4;
142
143 header_.total_length = initial_length;
144
145 gold_assert(lineptr + header_.total_length <= buffer_end_);
146
deae2a14 147 header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
5c2c6c95
ILT
148 lineptr += 2;
149
150 if (header_.offset_size == 4)
deae2a14 151 header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
5c2c6c95 152 else
deae2a14 153 header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
5c2c6c95
ILT
154 lineptr += header_.offset_size;
155
156 header_.min_insn_length = *lineptr;
157 lineptr += 1;
158
159 header_.default_is_stmt = *lineptr;
160 lineptr += 1;
161
162 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
163 lineptr += 1;
164
165 header_.line_range = *lineptr;
166 lineptr += 1;
167
168 header_.opcode_base = *lineptr;
169 lineptr += 1;
170
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++)
174 {
175 header_.std_opcode_lengths[i] = *lineptr;
176 lineptr += 1;
177 }
178
179 return lineptr;
180}
181
182// The header for a debug_line section is mildly complicated, because
183// the line info is very tightly encoded.
184
e43872e9 185template<int size, bool big_endian>
5c2c6c95 186const unsigned char*
a55ce7fe 187Sized_dwarf_line_info<size, big_endian>::read_header_tables(
e43872e9 188 const unsigned char* lineptr)
5c2c6c95 189{
af674d1d
ILT
190 ++this->current_header_index_;
191
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));
201
5c2c6c95
ILT
202 // It is legal for the directory entry table to be empty.
203 if (*lineptr)
204 {
205 int dirindex = 1;
206 while (*lineptr)
207 {
af674d1d
ILT
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;
5c2c6c95
ILT
213 dirindex++;
214 }
215 }
216 lineptr++;
217
218 // It is also legal for the file entry table to be empty.
219 if (*lineptr)
220 {
221 int fileindex = 1;
222 size_t len;
223 while (*lineptr)
224 {
225 const char* filename = reinterpret_cast<const char*>(lineptr);
226 lineptr += strlen(filename) + 1;
227
228 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
5c2c6c95
ILT
229 lineptr += len;
230
af674d1d
ILT
231 if (dirindex >= this->directories_.back().size())
232 dirindex = 0;
233 int dirindexi = static_cast<int>(dirindex);
234
5c2c6c95
ILT
235 read_unsigned_LEB_128(lineptr, &len); // mod_time
236 lineptr += len;
237
238 read_unsigned_LEB_128(lineptr, &len); // filelength
239 lineptr += len;
240
af674d1d
ILT
241 gold_assert(fileindex
242 == static_cast<int>(this->files_.back().size()));
243 this->files_.back().push_back(std::make_pair(dirindexi, filename));
5c2c6c95
ILT
244 fileindex++;
245 }
246 }
247 lineptr++;
248
249 return lineptr;
250}
251
252// Process a single opcode in the .debug.line structure.
253
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.
257
e43872e9 258template<int size, bool big_endian>
5c2c6c95 259bool
a55ce7fe 260Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
e43872e9 261 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
5c2c6c95
ILT
262{
263 size_t oplen = 0;
264 size_t templen;
265 unsigned char opcode = *start;
266 oplen++;
267 start++;
268
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)
272 {
273 opcode -= header_.opcode_base;
274 const int advance_address = ((opcode / header_.line_range)
275 * header_.min_insn_length);
276 lsm->address += advance_address;
277
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;
284 }
285
286 // Otherwise, we have the regular opcodes
287 switch (opcode)
288 {
289 case elfcpp::DW_LNS_copy:
290 lsm->basic_block = false;
291 *len = oplen;
292 return true;
293
294 case elfcpp::DW_LNS_advance_pc:
295 {
296 const uint64_t advance_address
2ea97941 297 = read_unsigned_LEB_128(start, &templen);
5c2c6c95
ILT
298 oplen += templen;
299 lsm->address += header_.min_insn_length * advance_address;
300 }
301 break;
302
303 case elfcpp::DW_LNS_advance_line:
304 {
305 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
306 oplen += templen;
307 lsm->line_num += advance_line;
308 }
309 break;
310
311 case elfcpp::DW_LNS_set_file:
312 {
313 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
314 oplen += templen;
315 lsm->file_num = fileno;
316 }
317 break;
318
319 case elfcpp::DW_LNS_set_column:
320 {
321 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
322 oplen += templen;
323 lsm->column_num = colno;
324 }
325 break;
326
327 case elfcpp::DW_LNS_negate_stmt:
328 lsm->is_stmt = !lsm->is_stmt;
329 break;
330
331 case elfcpp::DW_LNS_set_basic_block:
332 lsm->basic_block = true;
333 break;
334
335 case elfcpp::DW_LNS_fixed_advance_pc:
336 {
337 int advance_address;
deae2a14 338 advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
5c2c6c95
ILT
339 oplen += 2;
340 lsm->address += advance_address;
341 }
342 break;
343
344 case elfcpp::DW_LNS_const_add_pc:
345 {
346 const int advance_address = (header_.min_insn_length
347 * ((255 - header_.opcode_base)
348 / header_.line_range));
349 lsm->address += advance_address;
350 }
351 break;
352
353 case elfcpp::DW_LNS_extended_op:
354 {
355 const uint64_t extended_op_len
2ea97941 356 = read_unsigned_LEB_128(start, &templen);
5c2c6c95
ILT
357 start += templen;
358 oplen += templen + extended_op_len;
359
360 const unsigned char extended_op = *start;
361 start++;
362
363 switch (extended_op)
364 {
365 case elfcpp::DW_LNE_end_sequence:
124dfc89
ILT
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.
79e052ea 369 lsm->line_num = -1;
5c2c6c95
ILT
370 lsm->end_sequence = true;
371 *len = oplen;
372 return true;
373
374 case elfcpp::DW_LNE_set_address:
4c50553d 375 {
deae2a14 376 lsm->address = elfcpp::Swap_unaligned<size, big_endian>::readval(start);
4c50553d 377 typename Reloc_map::const_iterator it
2ea97941 378 = reloc_map_.find(start - this->buffer_);
4c50553d
ILT
379 if (it != reloc_map_.end())
380 {
381 // value + addend.
af674d1d 382 lsm->address += it->second.second;
4c50553d
ILT
383 lsm->shndx = it->second.first;
384 }
385 else
386 {
af674d1d
ILT
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;
4c50553d
ILT
391 }
392 break;
24badc65 393 }
5c2c6c95
ILT
394 case elfcpp::DW_LNE_define_file:
395 {
396 const char* filename = reinterpret_cast<const char*>(start);
397 templen = strlen(filename) + 1;
398 start += templen;
399
400 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
5c2c6c95
ILT
401 oplen += templen;
402
af674d1d
ILT
403 if (dirindex >= this->directories_.back().size())
404 dirindex = 0;
405 int dirindexi = static_cast<int>(dirindex);
406
5c2c6c95
ILT
407 read_unsigned_LEB_128(start, &templen); // mod_time
408 oplen += templen;
409
410 read_unsigned_LEB_128(start, &templen); // filelength
411 oplen += templen;
412
af674d1d 413 this->files_.back().push_back(std::make_pair(dirindexi,
5c2c6c95
ILT
414 filename));
415 }
416 break;
417 }
418 }
419 break;
420
421 default:
422 {
2ea97941 423 // Ignore unknown opcode silently
5c2c6c95
ILT
424 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
425 {
2ea97941 426 size_t templen;
5c2c6c95
ILT
427 read_unsigned_LEB_128(start, &templen);
428 start += templen;
429 oplen += templen;
430 }
431 }
432 break;
2ea97941 433 }
5c2c6c95
ILT
434 *len = oplen;
435 return false;
436}
437
438// Read the debug information at LINEPTR and store it in the line
439// number map.
440
e43872e9 441template<int size, bool big_endian>
5c2c6c95 442unsigned const char*
9430daf8 443Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
75aea3d0 444 unsigned int shndx)
5c2c6c95
ILT
445{
446 struct LineStateMachine lsm;
447
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_;
451
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;
458
459 while (lineptr < lengthstart + header_.total_length)
460 {
461 ResetLineStateMachine(&lsm, header_.default_is_stmt);
462 while (!lsm.end_sequence)
463 {
464 size_t oplength;
e43872e9 465 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
9430daf8
ILT
466 if (add_line
467 && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
5c2c6c95
ILT
468 {
469 Offset_to_lineno_entry entry
af674d1d
ILT
470 = { lsm.address, this->current_header_index_,
471 lsm.file_num, lsm.line_num };
5c2c6c95
ILT
472 line_number_map_[lsm.shndx].push_back(entry);
473 }
474 lineptr += oplength;
475 }
476 }
477
478 return lengthstart + header_.total_length;
479}
480
4c50553d
ILT
481// Looks in the symtab to see what section a symbol is in.
482
483template<int size, bool big_endian>
484unsigned int
a55ce7fe 485Sized_dwarf_line_info<size, big_endian>::symbol_section(
d491d34e 486 Object* object,
4c50553d 487 unsigned int sym,
d491d34e
ILT
488 typename elfcpp::Elf_types<size>::Elf_Addr* value,
489 bool* is_ordinary)
4c50553d
ILT
490{
491 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
af674d1d 492 gold_assert(sym * symsize < this->symtab_buffer_size_);
4c50553d
ILT
493 elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
494 *value = elfsym.get_st_value();
d491d34e 495 return object->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
4c50553d
ILT
496}
497
498// Read the relocations into a Reloc_map.
499
500template<int size, bool big_endian>
501void
d491d34e 502Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object)
4c50553d
ILT
503{
504 if (this->symtab_buffer_ == NULL)
505 return;
506
507 typename elfcpp::Elf_types<size>::Elf_Addr value;
508 off_t reloc_offset;
24badc65 509 while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
4c50553d 510 {
24badc65 511 const unsigned int sym = this->track_relocs_.next_symndx();
d491d34e
ILT
512
513 bool is_ordinary;
514 const unsigned int shndx = this->symbol_section(object, sym, &value,
515 &is_ordinary);
516
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);
521
24badc65 522 this->track_relocs_.advance(reloc_offset + 1);
4c50553d
ILT
523 }
524}
525
526// Read the line number info.
527
e43872e9 528template<int size, bool big_endian>
5c2c6c95 529void
d491d34e 530Sized_dwarf_line_info<size, big_endian>::read_line_mappings(Object* object,
75aea3d0 531 unsigned int shndx)
5c2c6c95 532{
c261a0be 533 gold_assert(this->data_valid_ == true);
24badc65 534
d491d34e 535 this->read_relocs(object);
4c50553d 536 while (this->buffer_ < this->buffer_end_)
e43872e9 537 {
4c50553d 538 const unsigned char* lineptr = this->buffer_;
e43872e9
ILT
539 lineptr = this->read_header_prolog(lineptr);
540 lineptr = this->read_header_tables(lineptr);
9430daf8 541 lineptr = this->read_lines(lineptr, shndx);
4c50553d 542 this->buffer_ = lineptr;
e43872e9
ILT
543 }
544
545 // Sort the lines numbers, so addr2line can use binary search.
546 for (typename Lineno_map::iterator it = line_number_map_.begin();
5c2c6c95
ILT
547 it != line_number_map_.end();
548 ++it)
549 // Each vector needs to be sorted by offset.
4c50553d 550 std::sort(it->second.begin(), it->second.end());
5c2c6c95
ILT
551}
552
af674d1d
ILT
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.
558
559template<int size, bool big_endian>
560bool
a55ce7fe 561Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
af674d1d
ILT
562{
563 // Only .o files have relocs and the symtab buffer that goes with them.
564 return this->symtab_buffer_ != NULL;
565}
566
79e052ea
ILT
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().
572
573static std::vector<Offset_to_lineno_entry>::const_iterator
574offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
575 off_t offset)
576{
577 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 };
578
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);
583
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
ef04e392 604 // {offset = 5798, header_num = 1, file_num = 1, line_num = -1}, // 18
79e052ea
ILT
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
ef04e392 610 // (as is true for some others, most obviously from 3236->5764).
79e052ea
ILT
611 //
612 // Case 1: lookup_key has offset == 10. lower_bound returns
613 // offsets[0]. Since it's not an exact match and we're
ef04e392 614 // at the beginning of offsets, we return end() (invalid).
79e052ea 615 // Case 2: lookup_key has offset 10000. lower_bound returns
ef04e392 616 // offset[19] (end()). We return end() (invalid).
79e052ea
ILT
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
ef04e392 633 // functions, so we return end() (not a valid offset).
79e052ea
ILT
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.)
645
646 // This deals with cases (1) and (2).
647 if ((it == offsets->begin() && offset < it->offset)
648 || it == offsets->end())
649 return offsets->end();
650
651 // This deals with cases (3) and (4).
652 if (offset == it->offset)
653 {
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;
662 }
663
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;
672
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();
680}
af674d1d 681
5c2c6c95
ILT
682// Return a string for a file name and line number.
683
e43872e9 684template<int size, bool big_endian>
5c2c6c95 685std::string
a55ce7fe
ILT
686Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx,
687 off_t offset)
5c2c6c95 688{
4c50553d
ILT
689 if (this->data_valid_ == false)
690 return "";
691
af674d1d
ILT
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())
4c50553d
ILT
701 return "";
702
e43872e9 703 typename std::vector<Offset_to_lineno_entry>::const_iterator it
79e052ea
ILT
704 = offset_to_iterator(offsets, offset);
705 if (it == offsets->end())
706 return "";
5c2c6c95
ILT
707
708 // Convert the file_num + line_num into a string.
709 std::string ret;
af674d1d
ILT
710
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];
5c2c6c95 716 const std::string& filename = filename_pair.second;
af674d1d
ILT
717
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];
723
5c2c6c95
ILT
724 if (!dirname.empty())
725 {
726 ret += dirname;
727 ret += "/";
728 }
729 ret += filename;
730 if (ret.empty())
731 ret = "(unknown)";
732
733 char buffer[64]; // enough to hold a line number
734 snprintf(buffer, sizeof(buffer), "%d", it->line_num);
735 ret += ":";
736 ret += buffer;
737
738 return ret;
739}
740
a55ce7fe
ILT
741// Dwarf_line_info routines.
742
e4e5049b
CS
743static unsigned int next_generation_count = 0;
744
745struct Addr2line_cache_entry
746{
747 Object* object;
748 unsigned int shndx;
749 Dwarf_line_info* dwarf_line_info;
750 unsigned int generation_count;
751 unsigned int access_count;
752
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)
756 {
757 if (next_generation_count < (1U << 31))
758 ++next_generation_count;
759 }
760};
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.
763static std::vector<Addr2line_cache_entry> addr2line_cache;
764
a55ce7fe
ILT
765std::string
766Dwarf_line_info::one_addr2line(Object* object,
e4e5049b
CS
767 unsigned int shndx, off_t offset,
768 size_t cache_size)
a55ce7fe 769{
e4e5049b
CS
770 Dwarf_line_info* lineinfo = NULL;
771 std::vector<Addr2line_cache_entry>::iterator it;
772
773 // First, check the cache. If we hit, update the counts.
774 for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
8851ecca 775 {
e4e5049b
CS
776 if (it->object == object && it->shndx == shndx)
777 {
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;
787 }
788 }
789
790 // If we don't hit the cache, create a new object and insert into the
791 // cache.
792 if (lineinfo == NULL)
793 {
794 switch (parameters->size_and_endianness())
795 {
a55ce7fe 796#ifdef HAVE_TARGET_32_LITTLE
e4e5049b
CS
797 case Parameters::TARGET_32_LITTLE:
798 lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
a55ce7fe 799#endif
a55ce7fe 800#ifdef HAVE_TARGET_32_BIG
e4e5049b
CS
801 case Parameters::TARGET_32_BIG:
802 lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
a55ce7fe 803#endif
a55ce7fe 804#ifdef HAVE_TARGET_64_LITTLE
e4e5049b
CS
805 case Parameters::TARGET_64_LITTLE:
806 lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
a55ce7fe 807#endif
8851ecca 808#ifdef HAVE_TARGET_64_BIG
e4e5049b
CS
809 case Parameters::TARGET_64_BIG:
810 lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
a55ce7fe 811#endif
e4e5049b
CS
812 default:
813 gold_unreachable();
814 }
815 addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
816 }
817
818 // Now that we have our object, figure out the answer
819 std::string retval = lineinfo->addr2line(shndx, offset);
820
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)
825 {
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)
830 {
831 const unsigned int score = (it->generation_count
832 + (1U << it->access_count));
833 if (score < lowest_score)
834 {
835 lowest_score = score;
836 lowest = it;
837 }
838 }
839 if (lowest != addr2line_cache.end())
840 {
841 delete lowest->dwarf_line_info;
842 addr2line_cache.erase(lowest);
843 }
8851ecca 844 }
e4e5049b
CS
845
846 return retval;
847}
848
849void
850Dwarf_line_info::clear_addr2line_cache()
851{
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();
a55ce7fe
ILT
857}
858
5c2c6c95
ILT
859#ifdef HAVE_TARGET_32_LITTLE
860template
a55ce7fe 861class Sized_dwarf_line_info<32, false>;
5c2c6c95
ILT
862#endif
863
864#ifdef HAVE_TARGET_32_BIG
865template
a55ce7fe 866class Sized_dwarf_line_info<32, true>;
5c2c6c95
ILT
867#endif
868
869#ifdef HAVE_TARGET_64_LITTLE
870template
a55ce7fe 871class Sized_dwarf_line_info<64, false>;
5c2c6c95
ILT
872#endif
873
874#ifdef HAVE_TARGET_64_BIG
875template
a55ce7fe 876class Sized_dwarf_line_info<64, true>;
5c2c6c95
ILT
877#endif
878
879} // End namespace gold.
This page took 0.178344 seconds and 4 git commands to generate.