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