Commit | Line | Data |
---|---|---|
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" |
a2e47362 | 35 | #include "compressed_output.h" |
5c2c6c95 | 36 | |
62b01cb5 | 37 | namespace gold { |
5c2c6c95 | 38 | |
5c2c6c95 ILT |
39 | struct LineStateMachine |
40 | { | |
41 | int file_num; | |
42 | uint64_t address; | |
43 | int line_num; | |
44 | int column_num; | |
45 | unsigned int shndx; // the section address refers to | |
46 | bool is_stmt; // stmt means statement. | |
47 | bool basic_block; | |
48 | bool end_sequence; | |
49 | }; | |
50 | ||
51 | static void | |
52 | ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt) | |
53 | { | |
54 | lsm->file_num = 1; | |
55 | lsm->address = 0; | |
56 | lsm->line_num = 1; | |
57 | lsm->column_num = 0; | |
338f2eba | 58 | lsm->shndx = -1U; |
5c2c6c95 ILT |
59 | lsm->is_stmt = default_is_stmt; |
60 | lsm->basic_block = false; | |
61 | lsm->end_sequence = false; | |
62 | } | |
63 | ||
24badc65 | 64 | template<int size, bool big_endian> |
9430daf8 | 65 | Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object, |
75aea3d0 | 66 | unsigned int read_shndx) |
c261a0be | 67 | : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL), |
af674d1d | 68 | directories_(), files_(), current_header_index_(-1) |
24badc65 ILT |
69 | { |
70 | unsigned int debug_shndx; | |
71 | for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx) | |
10600224 | 72 | // FIXME: do this more efficiently: section_name() isn't super-fast |
24badc65 ILT |
73 | if (object->section_name(debug_shndx) == ".debug_line") |
74 | { | |
8383303e ILT |
75 | section_size_type buffer_size; |
76 | this->buffer_ = object->section_contents(debug_shndx, &buffer_size, | |
77 | false); | |
24badc65 ILT |
78 | this->buffer_end_ = this->buffer_ + buffer_size; |
79 | break; | |
80 | } | |
81 | if (this->buffer_ == NULL) | |
c261a0be | 82 | return; |
24badc65 | 83 | |
a2e47362 CC |
84 | section_size_type uncompressed_size = 0; |
85 | unsigned char* uncompressed_data = NULL; | |
86 | if (object->section_is_compressed(debug_shndx, &uncompressed_size)) | |
87 | { | |
88 | uncompressed_data = new unsigned char[uncompressed_size]; | |
89 | if (!decompress_input_section(this->buffer_, | |
90 | this->buffer_end_ - this->buffer_, | |
91 | uncompressed_data, | |
92 | uncompressed_size)) | |
93 | object->error(_("could not decompress section %s"), | |
94 | object->section_name(debug_shndx).c_str()); | |
95 | this->buffer_ = uncompressed_data; | |
96 | this->buffer_end_ = this->buffer_ + uncompressed_size; | |
97 | } | |
98 | ||
24badc65 | 99 | // Find the relocation section for ".debug_line". |
af674d1d | 100 | // We expect these for relobjs (.o's) but not dynobjs (.so's). |
24badc65 ILT |
101 | bool got_relocs = false; |
102 | for (unsigned int reloc_shndx = 0; | |
103 | reloc_shndx < object->shnum(); | |
104 | ++reloc_shndx) | |
105 | { | |
106 | unsigned int reloc_sh_type = object->section_type(reloc_shndx); | |
107 | if ((reloc_sh_type == elfcpp::SHT_REL | |
108 | || reloc_sh_type == elfcpp::SHT_RELA) | |
109 | && object->section_info(reloc_shndx) == debug_shndx) | |
110 | { | |
111 | got_relocs = this->track_relocs_.initialize(object, reloc_shndx, | |
112 | reloc_sh_type); | |
113 | break; | |
114 | } | |
115 | } | |
24badc65 ILT |
116 | |
117 | // Finally, we need the symtab section to interpret the relocs. | |
af674d1d ILT |
118 | if (got_relocs) |
119 | { | |
120 | unsigned int symtab_shndx; | |
121 | for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx) | |
122 | if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB) | |
123 | { | |
124 | this->symtab_buffer_ = object->section_contents( | |
125 | symtab_shndx, &this->symtab_buffer_size_, false); | |
126 | break; | |
127 | } | |
128 | if (this->symtab_buffer_ == NULL) | |
129 | return; | |
130 | } | |
24badc65 ILT |
131 | |
132 | // Now that we have successfully read all the data, parse the debug | |
133 | // info. | |
c261a0be | 134 | this->data_valid_ = true; |
d491d34e | 135 | this->read_line_mappings(object, read_shndx); |
24badc65 ILT |
136 | } |
137 | ||
5c2c6c95 ILT |
138 | // Read the DWARF header. |
139 | ||
140 | template<int size, bool big_endian> | |
141 | const unsigned char* | |
a55ce7fe | 142 | Sized_dwarf_line_info<size, big_endian>::read_header_prolog( |
e43872e9 | 143 | const unsigned char* lineptr) |
5c2c6c95 | 144 | { |
deae2a14 | 145 | uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr); |
5c2c6c95 ILT |
146 | lineptr += 4; |
147 | ||
148 | // In DWARF2/3, if the initial length is all 1 bits, then the offset | |
149 | // size is 8 and we need to read the next 8 bytes for the real length. | |
150 | if (initial_length == 0xffffffff) | |
151 | { | |
152 | header_.offset_size = 8; | |
deae2a14 | 153 | initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr); |
5c2c6c95 ILT |
154 | lineptr += 8; |
155 | } | |
156 | else | |
157 | header_.offset_size = 4; | |
158 | ||
159 | header_.total_length = initial_length; | |
160 | ||
161 | gold_assert(lineptr + header_.total_length <= buffer_end_); | |
162 | ||
deae2a14 | 163 | header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr); |
5c2c6c95 ILT |
164 | lineptr += 2; |
165 | ||
166 | if (header_.offset_size == 4) | |
deae2a14 | 167 | header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr); |
5c2c6c95 | 168 | else |
deae2a14 | 169 | header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr); |
5c2c6c95 ILT |
170 | lineptr += header_.offset_size; |
171 | ||
172 | header_.min_insn_length = *lineptr; | |
173 | lineptr += 1; | |
174 | ||
175 | header_.default_is_stmt = *lineptr; | |
176 | lineptr += 1; | |
177 | ||
178 | header_.line_base = *reinterpret_cast<const signed char*>(lineptr); | |
179 | lineptr += 1; | |
180 | ||
181 | header_.line_range = *lineptr; | |
182 | lineptr += 1; | |
183 | ||
184 | header_.opcode_base = *lineptr; | |
185 | lineptr += 1; | |
186 | ||
187 | header_.std_opcode_lengths.reserve(header_.opcode_base + 1); | |
188 | header_.std_opcode_lengths[0] = 0; | |
189 | for (int i = 1; i < header_.opcode_base; i++) | |
190 | { | |
191 | header_.std_opcode_lengths[i] = *lineptr; | |
192 | lineptr += 1; | |
193 | } | |
194 | ||
195 | return lineptr; | |
196 | } | |
197 | ||
198 | // The header for a debug_line section is mildly complicated, because | |
199 | // the line info is very tightly encoded. | |
200 | ||
e43872e9 | 201 | template<int size, bool big_endian> |
5c2c6c95 | 202 | const unsigned char* |
a55ce7fe | 203 | Sized_dwarf_line_info<size, big_endian>::read_header_tables( |
e43872e9 | 204 | const unsigned char* lineptr) |
5c2c6c95 | 205 | { |
af674d1d ILT |
206 | ++this->current_header_index_; |
207 | ||
208 | // Create a new directories_ entry and a new files_ entry for our new | |
209 | // header. We initialize each with a single empty element, because | |
210 | // dwarf indexes directory and filenames starting at 1. | |
211 | gold_assert(static_cast<int>(this->directories_.size()) | |
212 | == this->current_header_index_); | |
213 | gold_assert(static_cast<int>(this->files_.size()) | |
214 | == this->current_header_index_); | |
215 | this->directories_.push_back(std::vector<std::string>(1)); | |
216 | this->files_.push_back(std::vector<std::pair<int, std::string> >(1)); | |
217 | ||
5c2c6c95 ILT |
218 | // It is legal for the directory entry table to be empty. |
219 | if (*lineptr) | |
220 | { | |
221 | int dirindex = 1; | |
222 | while (*lineptr) | |
223 | { | |
af674d1d ILT |
224 | const char* dirname = reinterpret_cast<const char*>(lineptr); |
225 | gold_assert(dirindex | |
226 | == static_cast<int>(this->directories_.back().size())); | |
227 | this->directories_.back().push_back(dirname); | |
228 | lineptr += this->directories_.back().back().size() + 1; | |
5c2c6c95 ILT |
229 | dirindex++; |
230 | } | |
231 | } | |
232 | lineptr++; | |
233 | ||
234 | // It is also legal for the file entry table to be empty. | |
235 | if (*lineptr) | |
236 | { | |
237 | int fileindex = 1; | |
238 | size_t len; | |
239 | while (*lineptr) | |
240 | { | |
241 | const char* filename = reinterpret_cast<const char*>(lineptr); | |
242 | lineptr += strlen(filename) + 1; | |
243 | ||
244 | uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len); | |
5c2c6c95 ILT |
245 | lineptr += len; |
246 | ||
af674d1d ILT |
247 | if (dirindex >= this->directories_.back().size()) |
248 | dirindex = 0; | |
249 | int dirindexi = static_cast<int>(dirindex); | |
250 | ||
5c2c6c95 ILT |
251 | read_unsigned_LEB_128(lineptr, &len); // mod_time |
252 | lineptr += len; | |
253 | ||
254 | read_unsigned_LEB_128(lineptr, &len); // filelength | |
255 | lineptr += len; | |
256 | ||
af674d1d ILT |
257 | gold_assert(fileindex |
258 | == static_cast<int>(this->files_.back().size())); | |
259 | this->files_.back().push_back(std::make_pair(dirindexi, filename)); | |
5c2c6c95 ILT |
260 | fileindex++; |
261 | } | |
262 | } | |
263 | lineptr++; | |
264 | ||
265 | return lineptr; | |
266 | } | |
267 | ||
268 | // Process a single opcode in the .debug.line structure. | |
269 | ||
270 | // Templating on size and big_endian would yield more efficient (and | |
271 | // simpler) code, but would bloat the binary. Speed isn't important | |
272 | // here. | |
273 | ||
e43872e9 | 274 | template<int size, bool big_endian> |
5c2c6c95 | 275 | bool |
a55ce7fe | 276 | Sized_dwarf_line_info<size, big_endian>::process_one_opcode( |
e43872e9 | 277 | const unsigned char* start, struct LineStateMachine* lsm, size_t* len) |
5c2c6c95 ILT |
278 | { |
279 | size_t oplen = 0; | |
280 | size_t templen; | |
281 | unsigned char opcode = *start; | |
282 | oplen++; | |
283 | start++; | |
284 | ||
285 | // If the opcode is great than the opcode_base, it is a special | |
286 | // opcode. Most line programs consist mainly of special opcodes. | |
287 | if (opcode >= header_.opcode_base) | |
288 | { | |
289 | opcode -= header_.opcode_base; | |
290 | const int advance_address = ((opcode / header_.line_range) | |
291 | * header_.min_insn_length); | |
292 | lsm->address += advance_address; | |
293 | ||
294 | const int advance_line = ((opcode % header_.line_range) | |
295 | + header_.line_base); | |
296 | lsm->line_num += advance_line; | |
297 | lsm->basic_block = true; | |
298 | *len = oplen; | |
299 | return true; | |
300 | } | |
301 | ||
302 | // Otherwise, we have the regular opcodes | |
303 | switch (opcode) | |
304 | { | |
305 | case elfcpp::DW_LNS_copy: | |
306 | lsm->basic_block = false; | |
307 | *len = oplen; | |
308 | return true; | |
309 | ||
310 | case elfcpp::DW_LNS_advance_pc: | |
311 | { | |
312 | const uint64_t advance_address | |
2ea97941 | 313 | = read_unsigned_LEB_128(start, &templen); |
5c2c6c95 ILT |
314 | oplen += templen; |
315 | lsm->address += header_.min_insn_length * advance_address; | |
316 | } | |
317 | break; | |
318 | ||
319 | case elfcpp::DW_LNS_advance_line: | |
320 | { | |
321 | const uint64_t advance_line = read_signed_LEB_128(start, &templen); | |
322 | oplen += templen; | |
323 | lsm->line_num += advance_line; | |
324 | } | |
325 | break; | |
326 | ||
327 | case elfcpp::DW_LNS_set_file: | |
328 | { | |
329 | const uint64_t fileno = read_unsigned_LEB_128(start, &templen); | |
330 | oplen += templen; | |
331 | lsm->file_num = fileno; | |
332 | } | |
333 | break; | |
334 | ||
335 | case elfcpp::DW_LNS_set_column: | |
336 | { | |
337 | const uint64_t colno = read_unsigned_LEB_128(start, &templen); | |
338 | oplen += templen; | |
339 | lsm->column_num = colno; | |
340 | } | |
341 | break; | |
342 | ||
343 | case elfcpp::DW_LNS_negate_stmt: | |
344 | lsm->is_stmt = !lsm->is_stmt; | |
345 | break; | |
346 | ||
347 | case elfcpp::DW_LNS_set_basic_block: | |
348 | lsm->basic_block = true; | |
349 | break; | |
350 | ||
351 | case elfcpp::DW_LNS_fixed_advance_pc: | |
352 | { | |
353 | int advance_address; | |
deae2a14 | 354 | advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start); |
5c2c6c95 ILT |
355 | oplen += 2; |
356 | lsm->address += advance_address; | |
357 | } | |
358 | break; | |
359 | ||
360 | case elfcpp::DW_LNS_const_add_pc: | |
361 | { | |
362 | const int advance_address = (header_.min_insn_length | |
363 | * ((255 - header_.opcode_base) | |
364 | / header_.line_range)); | |
365 | lsm->address += advance_address; | |
366 | } | |
367 | break; | |
368 | ||
369 | case elfcpp::DW_LNS_extended_op: | |
370 | { | |
371 | const uint64_t extended_op_len | |
2ea97941 | 372 | = read_unsigned_LEB_128(start, &templen); |
5c2c6c95 ILT |
373 | start += templen; |
374 | oplen += templen + extended_op_len; | |
375 | ||
376 | const unsigned char extended_op = *start; | |
377 | start++; | |
378 | ||
379 | switch (extended_op) | |
380 | { | |
381 | case elfcpp::DW_LNE_end_sequence: | |
124dfc89 ILT |
382 | // This means that the current byte is the one immediately |
383 | // after a set of instructions. Record the current line | |
384 | // for up to one less than the current address. | |
79e052ea | 385 | lsm->line_num = -1; |
5c2c6c95 ILT |
386 | lsm->end_sequence = true; |
387 | *len = oplen; | |
388 | return true; | |
389 | ||
390 | case elfcpp::DW_LNE_set_address: | |
4c50553d | 391 | { |
deae2a14 | 392 | lsm->address = elfcpp::Swap_unaligned<size, big_endian>::readval(start); |
4c50553d | 393 | typename Reloc_map::const_iterator it |
2ea97941 | 394 | = reloc_map_.find(start - this->buffer_); |
4c50553d ILT |
395 | if (it != reloc_map_.end()) |
396 | { | |
397 | // value + addend. | |
af674d1d | 398 | lsm->address += it->second.second; |
4c50553d ILT |
399 | lsm->shndx = it->second.first; |
400 | } | |
401 | else | |
402 | { | |
af674d1d ILT |
403 | // If we're a normal .o file, with relocs, every |
404 | // set_address should have an associated relocation. | |
405 | if (this->input_is_relobj()) | |
406 | this->data_valid_ = false; | |
4c50553d ILT |
407 | } |
408 | break; | |
24badc65 | 409 | } |
5c2c6c95 ILT |
410 | case elfcpp::DW_LNE_define_file: |
411 | { | |
412 | const char* filename = reinterpret_cast<const char*>(start); | |
413 | templen = strlen(filename) + 1; | |
414 | start += templen; | |
415 | ||
416 | uint64_t dirindex = read_unsigned_LEB_128(start, &templen); | |
5c2c6c95 ILT |
417 | oplen += templen; |
418 | ||
af674d1d ILT |
419 | if (dirindex >= this->directories_.back().size()) |
420 | dirindex = 0; | |
421 | int dirindexi = static_cast<int>(dirindex); | |
422 | ||
5c2c6c95 ILT |
423 | read_unsigned_LEB_128(start, &templen); // mod_time |
424 | oplen += templen; | |
425 | ||
426 | read_unsigned_LEB_128(start, &templen); // filelength | |
427 | oplen += templen; | |
428 | ||
af674d1d | 429 | this->files_.back().push_back(std::make_pair(dirindexi, |
5c2c6c95 ILT |
430 | filename)); |
431 | } | |
432 | break; | |
433 | } | |
434 | } | |
435 | break; | |
436 | ||
437 | default: | |
438 | { | |
2ea97941 | 439 | // Ignore unknown opcode silently |
5c2c6c95 ILT |
440 | for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++) |
441 | { | |
2ea97941 | 442 | size_t templen; |
5c2c6c95 ILT |
443 | read_unsigned_LEB_128(start, &templen); |
444 | start += templen; | |
445 | oplen += templen; | |
446 | } | |
447 | } | |
448 | break; | |
2ea97941 | 449 | } |
5c2c6c95 ILT |
450 | *len = oplen; |
451 | return false; | |
452 | } | |
453 | ||
454 | // Read the debug information at LINEPTR and store it in the line | |
455 | // number map. | |
456 | ||
e43872e9 | 457 | template<int size, bool big_endian> |
5c2c6c95 | 458 | unsigned const char* |
9430daf8 | 459 | Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr, |
75aea3d0 | 460 | unsigned int shndx) |
5c2c6c95 ILT |
461 | { |
462 | struct LineStateMachine lsm; | |
463 | ||
464 | // LENGTHSTART is the place the length field is based on. It is the | |
465 | // point in the header after the initial length field. | |
466 | const unsigned char* lengthstart = buffer_; | |
467 | ||
468 | // In 64 bit dwarf, the initial length is 12 bytes, because of the | |
469 | // 0xffffffff at the start. | |
470 | if (header_.offset_size == 8) | |
471 | lengthstart += 12; | |
472 | else | |
473 | lengthstart += 4; | |
474 | ||
475 | while (lineptr < lengthstart + header_.total_length) | |
476 | { | |
477 | ResetLineStateMachine(&lsm, header_.default_is_stmt); | |
478 | while (!lsm.end_sequence) | |
479 | { | |
480 | size_t oplength; | |
e43872e9 | 481 | bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength); |
9430daf8 ILT |
482 | if (add_line |
483 | && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx)) | |
5c2c6c95 ILT |
484 | { |
485 | Offset_to_lineno_entry entry | |
af674d1d ILT |
486 | = { lsm.address, this->current_header_index_, |
487 | lsm.file_num, lsm.line_num }; | |
5c2c6c95 ILT |
488 | line_number_map_[lsm.shndx].push_back(entry); |
489 | } | |
490 | lineptr += oplength; | |
491 | } | |
492 | } | |
493 | ||
494 | return lengthstart + header_.total_length; | |
495 | } | |
496 | ||
4c50553d ILT |
497 | // Looks in the symtab to see what section a symbol is in. |
498 | ||
499 | template<int size, bool big_endian> | |
500 | unsigned int | |
a55ce7fe | 501 | Sized_dwarf_line_info<size, big_endian>::symbol_section( |
d491d34e | 502 | Object* object, |
4c50553d | 503 | unsigned int sym, |
d491d34e ILT |
504 | typename elfcpp::Elf_types<size>::Elf_Addr* value, |
505 | bool* is_ordinary) | |
4c50553d ILT |
506 | { |
507 | const int symsize = elfcpp::Elf_sizes<size>::sym_size; | |
af674d1d | 508 | gold_assert(sym * symsize < this->symtab_buffer_size_); |
4c50553d ILT |
509 | elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize); |
510 | *value = elfsym.get_st_value(); | |
d491d34e | 511 | return object->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary); |
4c50553d ILT |
512 | } |
513 | ||
514 | // Read the relocations into a Reloc_map. | |
515 | ||
516 | template<int size, bool big_endian> | |
517 | void | |
d491d34e | 518 | Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object) |
4c50553d ILT |
519 | { |
520 | if (this->symtab_buffer_ == NULL) | |
521 | return; | |
522 | ||
523 | typename elfcpp::Elf_types<size>::Elf_Addr value; | |
524 | off_t reloc_offset; | |
24badc65 | 525 | while ((reloc_offset = this->track_relocs_.next_offset()) != -1) |
4c50553d | 526 | { |
24badc65 | 527 | const unsigned int sym = this->track_relocs_.next_symndx(); |
d491d34e ILT |
528 | |
529 | bool is_ordinary; | |
530 | const unsigned int shndx = this->symbol_section(object, sym, &value, | |
531 | &is_ordinary); | |
532 | ||
533 | // There is no reason to record non-ordinary section indexes, or | |
534 | // SHN_UNDEF, because they will never match the real section. | |
535 | if (is_ordinary && shndx != elfcpp::SHN_UNDEF) | |
536 | this->reloc_map_[reloc_offset] = std::make_pair(shndx, value); | |
537 | ||
24badc65 | 538 | this->track_relocs_.advance(reloc_offset + 1); |
4c50553d ILT |
539 | } |
540 | } | |
541 | ||
542 | // Read the line number info. | |
543 | ||
e43872e9 | 544 | template<int size, bool big_endian> |
5c2c6c95 | 545 | void |
d491d34e | 546 | Sized_dwarf_line_info<size, big_endian>::read_line_mappings(Object* object, |
75aea3d0 | 547 | unsigned int shndx) |
5c2c6c95 | 548 | { |
c261a0be | 549 | gold_assert(this->data_valid_ == true); |
24badc65 | 550 | |
d491d34e | 551 | this->read_relocs(object); |
4c50553d | 552 | while (this->buffer_ < this->buffer_end_) |
e43872e9 | 553 | { |
4c50553d | 554 | const unsigned char* lineptr = this->buffer_; |
e43872e9 ILT |
555 | lineptr = this->read_header_prolog(lineptr); |
556 | lineptr = this->read_header_tables(lineptr); | |
9430daf8 | 557 | lineptr = this->read_lines(lineptr, shndx); |
4c50553d | 558 | this->buffer_ = lineptr; |
e43872e9 ILT |
559 | } |
560 | ||
561 | // Sort the lines numbers, so addr2line can use binary search. | |
562 | for (typename Lineno_map::iterator it = line_number_map_.begin(); | |
5c2c6c95 ILT |
563 | it != line_number_map_.end(); |
564 | ++it) | |
565 | // Each vector needs to be sorted by offset. | |
4c50553d | 566 | std::sort(it->second.begin(), it->second.end()); |
5c2c6c95 ILT |
567 | } |
568 | ||
af674d1d ILT |
569 | // Some processing depends on whether the input is a .o file or not. |
570 | // For instance, .o files have relocs, and have .debug_lines | |
571 | // information on a per section basis. .so files, on the other hand, | |
572 | // lack relocs, and offsets are unique, so we can ignore the section | |
573 | // information. | |
574 | ||
575 | template<int size, bool big_endian> | |
576 | bool | |
a55ce7fe | 577 | Sized_dwarf_line_info<size, big_endian>::input_is_relobj() |
af674d1d ILT |
578 | { |
579 | // Only .o files have relocs and the symtab buffer that goes with them. | |
580 | return this->symtab_buffer_ != NULL; | |
581 | } | |
582 | ||
79e052ea ILT |
583 | // Given an Offset_to_lineno_entry vector, and an offset, figure out |
584 | // if the offset points into a function according to the vector (see | |
585 | // comments below for the algorithm). If it does, return an iterator | |
586 | // into the vector that points to the line-number that contains that | |
587 | // offset. If not, it returns vector::end(). | |
588 | ||
589 | static std::vector<Offset_to_lineno_entry>::const_iterator | |
590 | offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets, | |
591 | off_t offset) | |
592 | { | |
593 | const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 }; | |
594 | ||
595 | // lower_bound() returns the smallest offset which is >= lookup_key. | |
596 | // If no offset in offsets is >= lookup_key, returns end(). | |
597 | std::vector<Offset_to_lineno_entry>::const_iterator it | |
598 | = std::lower_bound(offsets->begin(), offsets->end(), lookup_key); | |
599 | ||
600 | // This code is easiest to understand with a concrete example. | |
601 | // Here's a possible offsets array: | |
602 | // {{offset = 3211, header_num = 0, file_num = 1, line_num = 16}, // 0 | |
603 | // {offset = 3224, header_num = 0, file_num = 1, line_num = 20}, // 1 | |
604 | // {offset = 3226, header_num = 0, file_num = 1, line_num = 22}, // 2 | |
605 | // {offset = 3231, header_num = 0, file_num = 1, line_num = 25}, // 3 | |
606 | // {offset = 3232, header_num = 0, file_num = 1, line_num = -1}, // 4 | |
607 | // {offset = 3232, header_num = 0, file_num = 1, line_num = 65}, // 5 | |
608 | // {offset = 3235, header_num = 0, file_num = 1, line_num = 66}, // 6 | |
609 | // {offset = 3236, header_num = 0, file_num = 1, line_num = -1}, // 7 | |
610 | // {offset = 5764, header_num = 0, file_num = 1, line_num = 47}, // 8 | |
611 | // {offset = 5765, header_num = 0, file_num = 1, line_num = 48}, // 9 | |
612 | // {offset = 5767, header_num = 0, file_num = 1, line_num = 49}, // 10 | |
613 | // {offset = 5768, header_num = 0, file_num = 1, line_num = 50}, // 11 | |
614 | // {offset = 5773, header_num = 0, file_num = 1, line_num = -1}, // 12 | |
615 | // {offset = 5787, header_num = 1, file_num = 1, line_num = 19}, // 13 | |
616 | // {offset = 5790, header_num = 1, file_num = 1, line_num = 20}, // 14 | |
617 | // {offset = 5793, header_num = 1, file_num = 1, line_num = 67}, // 15 | |
618 | // {offset = 5793, header_num = 1, file_num = 1, line_num = -1}, // 16 | |
619 | // {offset = 5795, header_num = 1, file_num = 1, line_num = 68}, // 17 | |
ef04e392 | 620 | // {offset = 5798, header_num = 1, file_num = 1, line_num = -1}, // 18 |
79e052ea ILT |
621 | // The entries with line_num == -1 mark the end of a function: the |
622 | // associated offset is one past the last instruction in the | |
623 | // function. This can correspond to the beginning of the next | |
624 | // function (as is true for offset 3232); alternately, there can be | |
625 | // a gap between the end of one function and the start of the next | |
ef04e392 | 626 | // (as is true for some others, most obviously from 3236->5764). |
79e052ea ILT |
627 | // |
628 | // Case 1: lookup_key has offset == 10. lower_bound returns | |
629 | // offsets[0]. Since it's not an exact match and we're | |
ef04e392 | 630 | // at the beginning of offsets, we return end() (invalid). |
79e052ea | 631 | // Case 2: lookup_key has offset 10000. lower_bound returns |
ef04e392 | 632 | // offset[19] (end()). We return end() (invalid). |
79e052ea ILT |
633 | // Case 3: lookup_key has offset == 3211. lower_bound matches |
634 | // offsets[0] exactly, and that's the entry we return. | |
635 | // Case 4: lookup_key has offset == 3232. lower_bound returns | |
636 | // offsets[4]. That's an exact match, but indicates | |
637 | // end-of-function. We check if offsets[5] is also an | |
638 | // exact match but not end-of-function. It is, so we | |
639 | // return offsets[5]. | |
640 | // Case 5: lookup_key has offset == 3214. lower_bound returns | |
641 | // offsets[1]. Since it's not an exact match, we back | |
642 | // up to the offset that's < lookup_key, offsets[0]. | |
643 | // We note offsets[0] is a valid entry (not end-of-function), | |
644 | // so that's the entry we return. | |
645 | // Case 6: lookup_key has offset == 4000. lower_bound returns | |
646 | // offsets[8]. Since it's not an exact match, we back | |
647 | // up to offsets[7]. Since offsets[7] indicates | |
648 | // end-of-function, we know lookup_key is between | |
ef04e392 | 649 | // functions, so we return end() (not a valid offset). |
79e052ea ILT |
650 | // Case 7: lookup_key has offset == 5794. lower_bound returns |
651 | // offsets[17]. Since it's not an exact match, we back | |
652 | // up to offsets[15]. Note we back up to the *first* | |
653 | // entry with offset 5793, not just offsets[17-1]. | |
654 | // We note offsets[15] is a valid entry, so we return it. | |
655 | // If offsets[15] had had line_num == -1, we would have | |
656 | // checked offsets[16]. The reason for this is that | |
657 | // 15 and 16 can be in an arbitrary order, since we sort | |
658 | // only by offset. (Note it doesn't help to use line_number | |
659 | // as a secondary sort key, since sometimes we want the -1 | |
660 | // to be first and sometimes we want it to be last.) | |
661 | ||
662 | // This deals with cases (1) and (2). | |
663 | if ((it == offsets->begin() && offset < it->offset) | |
664 | || it == offsets->end()) | |
665 | return offsets->end(); | |
666 | ||
667 | // This deals with cases (3) and (4). | |
668 | if (offset == it->offset) | |
669 | { | |
670 | while (it != offsets->end() | |
671 | && it->offset == offset | |
672 | && it->line_num == -1) | |
673 | ++it; | |
674 | if (it == offsets->end() || it->offset != offset) | |
675 | return offsets->end(); | |
676 | else | |
677 | return it; | |
678 | } | |
679 | ||
680 | // This handles the first part of case (7) -- we back up to the | |
681 | // *first* entry that has the offset that's behind us. | |
682 | gold_assert(it != offsets->begin()); | |
683 | std::vector<Offset_to_lineno_entry>::const_iterator range_end = it; | |
684 | --it; | |
685 | const off_t range_value = it->offset; | |
686 | while (it != offsets->begin() && (it-1)->offset == range_value) | |
687 | --it; | |
688 | ||
689 | // This handles cases (5), (6), and (7): if any entry in the | |
690 | // equal_range [it, range_end) has a line_num != -1, it's a valid | |
691 | // match. If not, we're not in a function. | |
692 | for (; it != range_end; ++it) | |
693 | if (it->line_num != -1) | |
694 | return it; | |
695 | return offsets->end(); | |
696 | } | |
af674d1d | 697 | |
5c2c6c95 ILT |
698 | // Return a string for a file name and line number. |
699 | ||
e43872e9 | 700 | template<int size, bool big_endian> |
5c2c6c95 | 701 | std::string |
a55ce7fe ILT |
702 | Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx, |
703 | off_t offset) | |
5c2c6c95 | 704 | { |
4c50553d ILT |
705 | if (this->data_valid_ == false) |
706 | return ""; | |
707 | ||
af674d1d ILT |
708 | const std::vector<Offset_to_lineno_entry>* offsets; |
709 | // If we do not have reloc information, then our input is a .so or | |
710 | // some similar data structure where all the information is held in | |
711 | // the offset. In that case, we ignore the input shndx. | |
712 | if (this->input_is_relobj()) | |
713 | offsets = &this->line_number_map_[shndx]; | |
714 | else | |
715 | offsets = &this->line_number_map_[-1U]; | |
716 | if (offsets->empty()) | |
4c50553d ILT |
717 | return ""; |
718 | ||
e43872e9 | 719 | typename std::vector<Offset_to_lineno_entry>::const_iterator it |
79e052ea ILT |
720 | = offset_to_iterator(offsets, offset); |
721 | if (it == offsets->end()) | |
722 | return ""; | |
5c2c6c95 ILT |
723 | |
724 | // Convert the file_num + line_num into a string. | |
725 | std::string ret; | |
af674d1d ILT |
726 | |
727 | gold_assert(it->header_num < static_cast<int>(this->files_.size())); | |
728 | gold_assert(it->file_num | |
729 | < static_cast<int>(this->files_[it->header_num].size())); | |
730 | const std::pair<int, std::string>& filename_pair | |
731 | = this->files_[it->header_num][it->file_num]; | |
5c2c6c95 | 732 | const std::string& filename = filename_pair.second; |
af674d1d ILT |
733 | |
734 | gold_assert(it->header_num < static_cast<int>(this->directories_.size())); | |
735 | gold_assert(filename_pair.first | |
736 | < static_cast<int>(this->directories_[it->header_num].size())); | |
737 | const std::string& dirname | |
738 | = this->directories_[it->header_num][filename_pair.first]; | |
739 | ||
5c2c6c95 ILT |
740 | if (!dirname.empty()) |
741 | { | |
742 | ret += dirname; | |
743 | ret += "/"; | |
744 | } | |
745 | ret += filename; | |
746 | if (ret.empty()) | |
747 | ret = "(unknown)"; | |
748 | ||
749 | char buffer[64]; // enough to hold a line number | |
750 | snprintf(buffer, sizeof(buffer), "%d", it->line_num); | |
751 | ret += ":"; | |
752 | ret += buffer; | |
753 | ||
754 | return ret; | |
755 | } | |
756 | ||
a55ce7fe ILT |
757 | // Dwarf_line_info routines. |
758 | ||
e4e5049b CS |
759 | static unsigned int next_generation_count = 0; |
760 | ||
761 | struct Addr2line_cache_entry | |
762 | { | |
763 | Object* object; | |
764 | unsigned int shndx; | |
765 | Dwarf_line_info* dwarf_line_info; | |
766 | unsigned int generation_count; | |
767 | unsigned int access_count; | |
768 | ||
769 | Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d) | |
770 | : object(o), shndx(s), dwarf_line_info(d), | |
771 | generation_count(next_generation_count), access_count(0) | |
772 | { | |
773 | if (next_generation_count < (1U << 31)) | |
774 | ++next_generation_count; | |
775 | } | |
776 | }; | |
777 | // We expect this cache to be small, so don't bother with a hashtable | |
778 | // or priority queue or anything: just use a simple vector. | |
779 | static std::vector<Addr2line_cache_entry> addr2line_cache; | |
780 | ||
a55ce7fe ILT |
781 | std::string |
782 | Dwarf_line_info::one_addr2line(Object* object, | |
e4e5049b CS |
783 | unsigned int shndx, off_t offset, |
784 | size_t cache_size) | |
a55ce7fe | 785 | { |
e4e5049b CS |
786 | Dwarf_line_info* lineinfo = NULL; |
787 | std::vector<Addr2line_cache_entry>::iterator it; | |
788 | ||
789 | // First, check the cache. If we hit, update the counts. | |
790 | for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it) | |
8851ecca | 791 | { |
e4e5049b CS |
792 | if (it->object == object && it->shndx == shndx) |
793 | { | |
794 | lineinfo = it->dwarf_line_info; | |
795 | it->generation_count = next_generation_count; | |
796 | // We cap generation_count at 2^31 -1 to avoid overflow. | |
797 | if (next_generation_count < (1U << 31)) | |
798 | ++next_generation_count; | |
799 | // We cap access_count at 31 so 2^access_count doesn't overflow | |
800 | if (it->access_count < 31) | |
801 | ++it->access_count; | |
802 | break; | |
803 | } | |
804 | } | |
805 | ||
806 | // If we don't hit the cache, create a new object and insert into the | |
807 | // cache. | |
808 | if (lineinfo == NULL) | |
809 | { | |
810 | switch (parameters->size_and_endianness()) | |
811 | { | |
a55ce7fe | 812 | #ifdef HAVE_TARGET_32_LITTLE |
e4e5049b CS |
813 | case Parameters::TARGET_32_LITTLE: |
814 | lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break; | |
a55ce7fe | 815 | #endif |
a55ce7fe | 816 | #ifdef HAVE_TARGET_32_BIG |
e4e5049b CS |
817 | case Parameters::TARGET_32_BIG: |
818 | lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break; | |
a55ce7fe | 819 | #endif |
a55ce7fe | 820 | #ifdef HAVE_TARGET_64_LITTLE |
e4e5049b CS |
821 | case Parameters::TARGET_64_LITTLE: |
822 | lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break; | |
a55ce7fe | 823 | #endif |
8851ecca | 824 | #ifdef HAVE_TARGET_64_BIG |
e4e5049b CS |
825 | case Parameters::TARGET_64_BIG: |
826 | lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break; | |
a55ce7fe | 827 | #endif |
e4e5049b CS |
828 | default: |
829 | gold_unreachable(); | |
830 | } | |
831 | addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo)); | |
832 | } | |
833 | ||
834 | // Now that we have our object, figure out the answer | |
835 | std::string retval = lineinfo->addr2line(shndx, offset); | |
836 | ||
837 | // Finally, if our cache has grown too big, delete old objects. We | |
838 | // assume the common (probably only) case is deleting only one object. | |
839 | // We use a pretty simple scheme to evict: function of LRU and MFU. | |
840 | while (addr2line_cache.size() > cache_size) | |
841 | { | |
842 | unsigned int lowest_score = ~0U; | |
843 | std::vector<Addr2line_cache_entry>::iterator lowest | |
844 | = addr2line_cache.end(); | |
845 | for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it) | |
846 | { | |
847 | const unsigned int score = (it->generation_count | |
848 | + (1U << it->access_count)); | |
849 | if (score < lowest_score) | |
850 | { | |
851 | lowest_score = score; | |
852 | lowest = it; | |
853 | } | |
854 | } | |
855 | if (lowest != addr2line_cache.end()) | |
856 | { | |
857 | delete lowest->dwarf_line_info; | |
858 | addr2line_cache.erase(lowest); | |
859 | } | |
8851ecca | 860 | } |
e4e5049b CS |
861 | |
862 | return retval; | |
863 | } | |
864 | ||
865 | void | |
866 | Dwarf_line_info::clear_addr2line_cache() | |
867 | { | |
868 | for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin(); | |
869 | it != addr2line_cache.end(); | |
870 | ++it) | |
871 | delete it->dwarf_line_info; | |
872 | addr2line_cache.clear(); | |
a55ce7fe ILT |
873 | } |
874 | ||
5c2c6c95 ILT |
875 | #ifdef HAVE_TARGET_32_LITTLE |
876 | template | |
a55ce7fe | 877 | class Sized_dwarf_line_info<32, false>; |
5c2c6c95 ILT |
878 | #endif |
879 | ||
880 | #ifdef HAVE_TARGET_32_BIG | |
881 | template | |
a55ce7fe | 882 | class Sized_dwarf_line_info<32, true>; |
5c2c6c95 ILT |
883 | #endif |
884 | ||
885 | #ifdef HAVE_TARGET_64_LITTLE | |
886 | template | |
a55ce7fe | 887 | class Sized_dwarf_line_info<64, false>; |
5c2c6c95 ILT |
888 | #endif |
889 | ||
890 | #ifdef HAVE_TARGET_64_BIG | |
891 | template | |
a55ce7fe | 892 | class Sized_dwarf_line_info<64, true>; |
5c2c6c95 ILT |
893 | #endif |
894 | ||
895 | } // End namespace gold. |