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