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