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