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> |
a55ce7fe | 121 | Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object) |
c261a0be | 122 | : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL), |
af674d1d | 123 | directories_(), files_(), current_header_index_(-1) |
24badc65 ILT |
124 | { |
125 | unsigned int debug_shndx; | |
126 | for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx) | |
127 | if (object->section_name(debug_shndx) == ".debug_line") | |
128 | { | |
129 | off_t buffer_size; | |
130 | this->buffer_ = object->section_contents( | |
131 | debug_shndx, &buffer_size, false); | |
132 | this->buffer_end_ = this->buffer_ + buffer_size; | |
133 | break; | |
134 | } | |
135 | if (this->buffer_ == NULL) | |
c261a0be | 136 | return; |
24badc65 ILT |
137 | |
138 | // Find the relocation section for ".debug_line". | |
af674d1d | 139 | // We expect these for relobjs (.o's) but not dynobjs (.so's). |
24badc65 ILT |
140 | bool got_relocs = false; |
141 | for (unsigned int reloc_shndx = 0; | |
142 | reloc_shndx < object->shnum(); | |
143 | ++reloc_shndx) | |
144 | { | |
145 | unsigned int reloc_sh_type = object->section_type(reloc_shndx); | |
146 | if ((reloc_sh_type == elfcpp::SHT_REL | |
147 | || reloc_sh_type == elfcpp::SHT_RELA) | |
148 | && object->section_info(reloc_shndx) == debug_shndx) | |
149 | { | |
150 | got_relocs = this->track_relocs_.initialize(object, reloc_shndx, | |
151 | reloc_sh_type); | |
152 | break; | |
153 | } | |
154 | } | |
24badc65 ILT |
155 | |
156 | // Finally, we need the symtab section to interpret the relocs. | |
af674d1d ILT |
157 | if (got_relocs) |
158 | { | |
159 | unsigned int symtab_shndx; | |
160 | for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx) | |
161 | if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB) | |
162 | { | |
163 | this->symtab_buffer_ = object->section_contents( | |
164 | symtab_shndx, &this->symtab_buffer_size_, false); | |
165 | break; | |
166 | } | |
167 | if (this->symtab_buffer_ == NULL) | |
168 | return; | |
169 | } | |
24badc65 ILT |
170 | |
171 | // Now that we have successfully read all the data, parse the debug | |
172 | // info. | |
c261a0be | 173 | this->data_valid_ = true; |
24badc65 ILT |
174 | this->read_line_mappings(); |
175 | } | |
176 | ||
5c2c6c95 ILT |
177 | // Read the DWARF header. |
178 | ||
179 | template<int size, bool big_endian> | |
180 | const unsigned char* | |
a55ce7fe | 181 | Sized_dwarf_line_info<size, big_endian>::read_header_prolog( |
e43872e9 | 182 | const unsigned char* lineptr) |
5c2c6c95 ILT |
183 | { |
184 | uint32_t initial_length = elfcpp::Swap<32, big_endian>::readval(lineptr); | |
185 | lineptr += 4; | |
186 | ||
187 | // In DWARF2/3, if the initial length is all 1 bits, then the offset | |
188 | // size is 8 and we need to read the next 8 bytes for the real length. | |
189 | if (initial_length == 0xffffffff) | |
190 | { | |
191 | header_.offset_size = 8; | |
192 | initial_length = elfcpp::Swap<64, big_endian>::readval(lineptr); | |
193 | lineptr += 8; | |
194 | } | |
195 | else | |
196 | header_.offset_size = 4; | |
197 | ||
198 | header_.total_length = initial_length; | |
199 | ||
200 | gold_assert(lineptr + header_.total_length <= buffer_end_); | |
201 | ||
202 | header_.version = elfcpp::Swap<16, big_endian>::readval(lineptr); | |
203 | lineptr += 2; | |
204 | ||
205 | if (header_.offset_size == 4) | |
206 | header_.prologue_length = elfcpp::Swap<32, big_endian>::readval(lineptr); | |
207 | else | |
208 | header_.prologue_length = elfcpp::Swap<64, big_endian>::readval(lineptr); | |
209 | lineptr += header_.offset_size; | |
210 | ||
211 | header_.min_insn_length = *lineptr; | |
212 | lineptr += 1; | |
213 | ||
214 | header_.default_is_stmt = *lineptr; | |
215 | lineptr += 1; | |
216 | ||
217 | header_.line_base = *reinterpret_cast<const signed char*>(lineptr); | |
218 | lineptr += 1; | |
219 | ||
220 | header_.line_range = *lineptr; | |
221 | lineptr += 1; | |
222 | ||
223 | header_.opcode_base = *lineptr; | |
224 | lineptr += 1; | |
225 | ||
226 | header_.std_opcode_lengths.reserve(header_.opcode_base + 1); | |
227 | header_.std_opcode_lengths[0] = 0; | |
228 | for (int i = 1; i < header_.opcode_base; i++) | |
229 | { | |
230 | header_.std_opcode_lengths[i] = *lineptr; | |
231 | lineptr += 1; | |
232 | } | |
233 | ||
234 | return lineptr; | |
235 | } | |
236 | ||
237 | // The header for a debug_line section is mildly complicated, because | |
238 | // the line info is very tightly encoded. | |
239 | ||
e43872e9 | 240 | template<int size, bool big_endian> |
5c2c6c95 | 241 | const unsigned char* |
a55ce7fe | 242 | Sized_dwarf_line_info<size, big_endian>::read_header_tables( |
e43872e9 | 243 | const unsigned char* lineptr) |
5c2c6c95 | 244 | { |
af674d1d ILT |
245 | ++this->current_header_index_; |
246 | ||
247 | // Create a new directories_ entry and a new files_ entry for our new | |
248 | // header. We initialize each with a single empty element, because | |
249 | // dwarf indexes directory and filenames starting at 1. | |
250 | gold_assert(static_cast<int>(this->directories_.size()) | |
251 | == this->current_header_index_); | |
252 | gold_assert(static_cast<int>(this->files_.size()) | |
253 | == this->current_header_index_); | |
254 | this->directories_.push_back(std::vector<std::string>(1)); | |
255 | this->files_.push_back(std::vector<std::pair<int, std::string> >(1)); | |
256 | ||
5c2c6c95 ILT |
257 | // It is legal for the directory entry table to be empty. |
258 | if (*lineptr) | |
259 | { | |
260 | int dirindex = 1; | |
261 | while (*lineptr) | |
262 | { | |
af674d1d ILT |
263 | const char* dirname = reinterpret_cast<const char*>(lineptr); |
264 | gold_assert(dirindex | |
265 | == static_cast<int>(this->directories_.back().size())); | |
266 | this->directories_.back().push_back(dirname); | |
267 | lineptr += this->directories_.back().back().size() + 1; | |
5c2c6c95 ILT |
268 | dirindex++; |
269 | } | |
270 | } | |
271 | lineptr++; | |
272 | ||
273 | // It is also legal for the file entry table to be empty. | |
274 | if (*lineptr) | |
275 | { | |
276 | int fileindex = 1; | |
277 | size_t len; | |
278 | while (*lineptr) | |
279 | { | |
280 | const char* filename = reinterpret_cast<const char*>(lineptr); | |
281 | lineptr += strlen(filename) + 1; | |
282 | ||
283 | uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len); | |
5c2c6c95 ILT |
284 | lineptr += len; |
285 | ||
af674d1d ILT |
286 | if (dirindex >= this->directories_.back().size()) |
287 | dirindex = 0; | |
288 | int dirindexi = static_cast<int>(dirindex); | |
289 | ||
5c2c6c95 ILT |
290 | read_unsigned_LEB_128(lineptr, &len); // mod_time |
291 | lineptr += len; | |
292 | ||
293 | read_unsigned_LEB_128(lineptr, &len); // filelength | |
294 | lineptr += len; | |
295 | ||
af674d1d ILT |
296 | gold_assert(fileindex |
297 | == static_cast<int>(this->files_.back().size())); | |
298 | this->files_.back().push_back(std::make_pair(dirindexi, filename)); | |
5c2c6c95 ILT |
299 | fileindex++; |
300 | } | |
301 | } | |
302 | lineptr++; | |
303 | ||
304 | return lineptr; | |
305 | } | |
306 | ||
307 | // Process a single opcode in the .debug.line structure. | |
308 | ||
309 | // Templating on size and big_endian would yield more efficient (and | |
310 | // simpler) code, but would bloat the binary. Speed isn't important | |
311 | // here. | |
312 | ||
e43872e9 | 313 | template<int size, bool big_endian> |
5c2c6c95 | 314 | bool |
a55ce7fe | 315 | Sized_dwarf_line_info<size, big_endian>::process_one_opcode( |
e43872e9 | 316 | const unsigned char* start, struct LineStateMachine* lsm, size_t* len) |
5c2c6c95 ILT |
317 | { |
318 | size_t oplen = 0; | |
319 | size_t templen; | |
320 | unsigned char opcode = *start; | |
321 | oplen++; | |
322 | start++; | |
323 | ||
324 | // If the opcode is great than the opcode_base, it is a special | |
325 | // opcode. Most line programs consist mainly of special opcodes. | |
326 | if (opcode >= header_.opcode_base) | |
327 | { | |
328 | opcode -= header_.opcode_base; | |
329 | const int advance_address = ((opcode / header_.line_range) | |
330 | * header_.min_insn_length); | |
331 | lsm->address += advance_address; | |
332 | ||
333 | const int advance_line = ((opcode % header_.line_range) | |
334 | + header_.line_base); | |
335 | lsm->line_num += advance_line; | |
336 | lsm->basic_block = true; | |
337 | *len = oplen; | |
338 | return true; | |
339 | } | |
340 | ||
341 | // Otherwise, we have the regular opcodes | |
342 | switch (opcode) | |
343 | { | |
344 | case elfcpp::DW_LNS_copy: | |
345 | lsm->basic_block = false; | |
346 | *len = oplen; | |
347 | return true; | |
348 | ||
349 | case elfcpp::DW_LNS_advance_pc: | |
350 | { | |
351 | const uint64_t advance_address | |
352 | = read_unsigned_LEB_128(start, &templen); | |
353 | oplen += templen; | |
354 | lsm->address += header_.min_insn_length * advance_address; | |
355 | } | |
356 | break; | |
357 | ||
358 | case elfcpp::DW_LNS_advance_line: | |
359 | { | |
360 | const uint64_t advance_line = read_signed_LEB_128(start, &templen); | |
361 | oplen += templen; | |
362 | lsm->line_num += advance_line; | |
363 | } | |
364 | break; | |
365 | ||
366 | case elfcpp::DW_LNS_set_file: | |
367 | { | |
368 | const uint64_t fileno = read_unsigned_LEB_128(start, &templen); | |
369 | oplen += templen; | |
370 | lsm->file_num = fileno; | |
371 | } | |
372 | break; | |
373 | ||
374 | case elfcpp::DW_LNS_set_column: | |
375 | { | |
376 | const uint64_t colno = read_unsigned_LEB_128(start, &templen); | |
377 | oplen += templen; | |
378 | lsm->column_num = colno; | |
379 | } | |
380 | break; | |
381 | ||
382 | case elfcpp::DW_LNS_negate_stmt: | |
383 | lsm->is_stmt = !lsm->is_stmt; | |
384 | break; | |
385 | ||
386 | case elfcpp::DW_LNS_set_basic_block: | |
387 | lsm->basic_block = true; | |
388 | break; | |
389 | ||
390 | case elfcpp::DW_LNS_fixed_advance_pc: | |
391 | { | |
392 | int advance_address; | |
e43872e9 | 393 | advance_address = elfcpp::Swap<16, big_endian>::readval(start); |
5c2c6c95 ILT |
394 | oplen += 2; |
395 | lsm->address += advance_address; | |
396 | } | |
397 | break; | |
398 | ||
399 | case elfcpp::DW_LNS_const_add_pc: | |
400 | { | |
401 | const int advance_address = (header_.min_insn_length | |
402 | * ((255 - header_.opcode_base) | |
403 | / header_.line_range)); | |
404 | lsm->address += advance_address; | |
405 | } | |
406 | break; | |
407 | ||
408 | case elfcpp::DW_LNS_extended_op: | |
409 | { | |
410 | const uint64_t extended_op_len | |
411 | = read_unsigned_LEB_128(start, &templen); | |
412 | start += templen; | |
413 | oplen += templen + extended_op_len; | |
414 | ||
415 | const unsigned char extended_op = *start; | |
416 | start++; | |
417 | ||
418 | switch (extended_op) | |
419 | { | |
420 | case elfcpp::DW_LNE_end_sequence: | |
124dfc89 ILT |
421 | // This means that the current byte is the one immediately |
422 | // after a set of instructions. Record the current line | |
423 | // for up to one less than the current address. | |
424 | lsm->address -= 1; | |
5c2c6c95 ILT |
425 | lsm->end_sequence = true; |
426 | *len = oplen; | |
427 | return true; | |
428 | ||
429 | case elfcpp::DW_LNE_set_address: | |
4c50553d | 430 | { |
af674d1d | 431 | lsm->address = elfcpp::Swap<size, big_endian>::readval(start); |
4c50553d ILT |
432 | typename Reloc_map::const_iterator it |
433 | = reloc_map_.find(start - this->buffer_); | |
434 | if (it != reloc_map_.end()) | |
435 | { | |
436 | // value + addend. | |
af674d1d | 437 | lsm->address += it->second.second; |
4c50553d ILT |
438 | lsm->shndx = it->second.first; |
439 | } | |
440 | else | |
441 | { | |
af674d1d ILT |
442 | // If we're a normal .o file, with relocs, every |
443 | // set_address should have an associated relocation. | |
444 | if (this->input_is_relobj()) | |
445 | this->data_valid_ = false; | |
4c50553d ILT |
446 | } |
447 | break; | |
24badc65 | 448 | } |
5c2c6c95 ILT |
449 | case elfcpp::DW_LNE_define_file: |
450 | { | |
451 | const char* filename = reinterpret_cast<const char*>(start); | |
452 | templen = strlen(filename) + 1; | |
453 | start += templen; | |
454 | ||
455 | uint64_t dirindex = read_unsigned_LEB_128(start, &templen); | |
5c2c6c95 ILT |
456 | oplen += templen; |
457 | ||
af674d1d ILT |
458 | if (dirindex >= this->directories_.back().size()) |
459 | dirindex = 0; | |
460 | int dirindexi = static_cast<int>(dirindex); | |
461 | ||
5c2c6c95 ILT |
462 | read_unsigned_LEB_128(start, &templen); // mod_time |
463 | oplen += templen; | |
464 | ||
465 | read_unsigned_LEB_128(start, &templen); // filelength | |
466 | oplen += templen; | |
467 | ||
af674d1d | 468 | this->files_.back().push_back(std::make_pair(dirindexi, |
5c2c6c95 ILT |
469 | filename)); |
470 | } | |
471 | break; | |
472 | } | |
473 | } | |
474 | break; | |
475 | ||
476 | default: | |
477 | { | |
478 | // Ignore unknown opcode silently | |
479 | for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++) | |
480 | { | |
481 | size_t templen; | |
482 | read_unsigned_LEB_128(start, &templen); | |
483 | start += templen; | |
484 | oplen += templen; | |
485 | } | |
486 | } | |
487 | break; | |
488 | } | |
489 | *len = oplen; | |
490 | return false; | |
491 | } | |
492 | ||
493 | // Read the debug information at LINEPTR and store it in the line | |
494 | // number map. | |
495 | ||
e43872e9 | 496 | template<int size, bool big_endian> |
5c2c6c95 | 497 | unsigned const char* |
a55ce7fe | 498 | Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr) |
5c2c6c95 ILT |
499 | { |
500 | struct LineStateMachine lsm; | |
501 | ||
502 | // LENGTHSTART is the place the length field is based on. It is the | |
503 | // point in the header after the initial length field. | |
504 | const unsigned char* lengthstart = buffer_; | |
505 | ||
506 | // In 64 bit dwarf, the initial length is 12 bytes, because of the | |
507 | // 0xffffffff at the start. | |
508 | if (header_.offset_size == 8) | |
509 | lengthstart += 12; | |
510 | else | |
511 | lengthstart += 4; | |
512 | ||
513 | while (lineptr < lengthstart + header_.total_length) | |
514 | { | |
515 | ResetLineStateMachine(&lsm, header_.default_is_stmt); | |
516 | while (!lsm.end_sequence) | |
517 | { | |
518 | size_t oplength; | |
e43872e9 | 519 | bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength); |
5c2c6c95 ILT |
520 | if (add_line) |
521 | { | |
522 | Offset_to_lineno_entry entry | |
af674d1d ILT |
523 | = { lsm.address, this->current_header_index_, |
524 | lsm.file_num, lsm.line_num }; | |
5c2c6c95 ILT |
525 | line_number_map_[lsm.shndx].push_back(entry); |
526 | } | |
527 | lineptr += oplength; | |
528 | } | |
529 | } | |
530 | ||
531 | return lengthstart + header_.total_length; | |
532 | } | |
533 | ||
4c50553d ILT |
534 | // Looks in the symtab to see what section a symbol is in. |
535 | ||
536 | template<int size, bool big_endian> | |
537 | unsigned int | |
a55ce7fe | 538 | Sized_dwarf_line_info<size, big_endian>::symbol_section( |
4c50553d ILT |
539 | unsigned int sym, |
540 | typename elfcpp::Elf_types<size>::Elf_Addr* value) | |
541 | { | |
542 | const int symsize = elfcpp::Elf_sizes<size>::sym_size; | |
af674d1d | 543 | gold_assert(sym * symsize < this->symtab_buffer_size_); |
4c50553d ILT |
544 | elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize); |
545 | *value = elfsym.get_st_value(); | |
546 | return elfsym.get_st_shndx(); | |
547 | } | |
548 | ||
549 | // Read the relocations into a Reloc_map. | |
550 | ||
551 | template<int size, bool big_endian> | |
552 | void | |
a55ce7fe | 553 | Sized_dwarf_line_info<size, big_endian>::read_relocs() |
4c50553d ILT |
554 | { |
555 | if (this->symtab_buffer_ == NULL) | |
556 | return; | |
557 | ||
558 | typename elfcpp::Elf_types<size>::Elf_Addr value; | |
559 | off_t reloc_offset; | |
24badc65 | 560 | while ((reloc_offset = this->track_relocs_.next_offset()) != -1) |
4c50553d | 561 | { |
24badc65 | 562 | const unsigned int sym = this->track_relocs_.next_symndx(); |
4c50553d ILT |
563 | const unsigned int shndx = this->symbol_section(sym, &value); |
564 | this->reloc_map_[reloc_offset] = std::make_pair(shndx, value); | |
24badc65 | 565 | this->track_relocs_.advance(reloc_offset + 1); |
4c50553d ILT |
566 | } |
567 | } | |
568 | ||
569 | // Read the line number info. | |
570 | ||
e43872e9 | 571 | template<int size, bool big_endian> |
5c2c6c95 | 572 | void |
a55ce7fe | 573 | Sized_dwarf_line_info<size, big_endian>::read_line_mappings() |
5c2c6c95 | 574 | { |
c261a0be | 575 | gold_assert(this->data_valid_ == true); |
24badc65 | 576 | |
4c50553d ILT |
577 | read_relocs(); |
578 | while (this->buffer_ < this->buffer_end_) | |
e43872e9 | 579 | { |
4c50553d | 580 | const unsigned char* lineptr = this->buffer_; |
e43872e9 ILT |
581 | lineptr = this->read_header_prolog(lineptr); |
582 | lineptr = this->read_header_tables(lineptr); | |
583 | lineptr = this->read_lines(lineptr); | |
4c50553d | 584 | this->buffer_ = lineptr; |
e43872e9 ILT |
585 | } |
586 | ||
587 | // Sort the lines numbers, so addr2line can use binary search. | |
588 | for (typename Lineno_map::iterator it = line_number_map_.begin(); | |
5c2c6c95 ILT |
589 | it != line_number_map_.end(); |
590 | ++it) | |
591 | // Each vector needs to be sorted by offset. | |
4c50553d | 592 | std::sort(it->second.begin(), it->second.end()); |
5c2c6c95 ILT |
593 | } |
594 | ||
af674d1d ILT |
595 | // Some processing depends on whether the input is a .o file or not. |
596 | // For instance, .o files have relocs, and have .debug_lines | |
597 | // information on a per section basis. .so files, on the other hand, | |
598 | // lack relocs, and offsets are unique, so we can ignore the section | |
599 | // information. | |
600 | ||
601 | template<int size, bool big_endian> | |
602 | bool | |
a55ce7fe | 603 | Sized_dwarf_line_info<size, big_endian>::input_is_relobj() |
af674d1d ILT |
604 | { |
605 | // Only .o files have relocs and the symtab buffer that goes with them. | |
606 | return this->symtab_buffer_ != NULL; | |
607 | } | |
608 | ||
609 | ||
5c2c6c95 ILT |
610 | // Return a string for a file name and line number. |
611 | ||
e43872e9 | 612 | template<int size, bool big_endian> |
5c2c6c95 | 613 | std::string |
a55ce7fe ILT |
614 | Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx, |
615 | off_t offset) | |
5c2c6c95 | 616 | { |
4c50553d ILT |
617 | if (this->data_valid_ == false) |
618 | return ""; | |
619 | ||
af674d1d ILT |
620 | const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 }; |
621 | const std::vector<Offset_to_lineno_entry>* offsets; | |
622 | // If we do not have reloc information, then our input is a .so or | |
623 | // some similar data structure where all the information is held in | |
624 | // the offset. In that case, we ignore the input shndx. | |
625 | if (this->input_is_relobj()) | |
626 | offsets = &this->line_number_map_[shndx]; | |
627 | else | |
628 | offsets = &this->line_number_map_[-1U]; | |
629 | if (offsets->empty()) | |
4c50553d ILT |
630 | return ""; |
631 | ||
e43872e9 | 632 | typename std::vector<Offset_to_lineno_entry>::const_iterator it |
af674d1d | 633 | = std::lower_bound(offsets->begin(), offsets->end(), lookup_key); |
5c2c6c95 ILT |
634 | |
635 | // If we found an exact match, great, otherwise find the last entry | |
636 | // before the passed-in offset. | |
a18f2bd6 | 637 | if (it == offsets->end() || it->offset > offset) |
5c2c6c95 | 638 | { |
af674d1d | 639 | if (it == offsets->begin()) |
5c2c6c95 ILT |
640 | return ""; |
641 | --it; | |
642 | gold_assert(it->offset < offset); | |
643 | } | |
644 | ||
645 | // Convert the file_num + line_num into a string. | |
646 | std::string ret; | |
af674d1d ILT |
647 | |
648 | gold_assert(it->header_num < static_cast<int>(this->files_.size())); | |
649 | gold_assert(it->file_num | |
650 | < static_cast<int>(this->files_[it->header_num].size())); | |
651 | const std::pair<int, std::string>& filename_pair | |
652 | = this->files_[it->header_num][it->file_num]; | |
5c2c6c95 | 653 | const std::string& filename = filename_pair.second; |
af674d1d ILT |
654 | |
655 | gold_assert(it->header_num < static_cast<int>(this->directories_.size())); | |
656 | gold_assert(filename_pair.first | |
657 | < static_cast<int>(this->directories_[it->header_num].size())); | |
658 | const std::string& dirname | |
659 | = this->directories_[it->header_num][filename_pair.first]; | |
660 | ||
5c2c6c95 ILT |
661 | if (!dirname.empty()) |
662 | { | |
663 | ret += dirname; | |
664 | ret += "/"; | |
665 | } | |
666 | ret += filename; | |
667 | if (ret.empty()) | |
668 | ret = "(unknown)"; | |
669 | ||
670 | char buffer[64]; // enough to hold a line number | |
671 | snprintf(buffer, sizeof(buffer), "%d", it->line_num); | |
672 | ret += ":"; | |
673 | ret += buffer; | |
674 | ||
675 | return ret; | |
676 | } | |
677 | ||
a55ce7fe ILT |
678 | // Dwarf_line_info routines. |
679 | ||
a55ce7fe ILT |
680 | std::string |
681 | Dwarf_line_info::one_addr2line(Object* object, | |
682 | unsigned int shndx, off_t offset) | |
683 | { | |
684 | if (parameters->get_size() == 32 && !parameters->is_big_endian()) | |
685 | #ifdef HAVE_TARGET_32_LITTLE | |
686 | return Sized_dwarf_line_info<32, false>(object).addr2line(shndx, offset); | |
687 | #else | |
688 | gold_unreachable(); | |
689 | #endif | |
690 | else if (parameters->get_size() == 32 && parameters->is_big_endian()) | |
691 | #ifdef HAVE_TARGET_32_BIG | |
692 | return Sized_dwarf_line_info<32, true>(object).addr2line(shndx, offset); | |
693 | #else | |
694 | gold_unreachable(); | |
695 | #endif | |
696 | else if (parameters->get_size() == 64 && !parameters->is_big_endian()) | |
697 | #ifdef HAVE_TARGET_64_LITTLE | |
698 | return Sized_dwarf_line_info<64, false>(object).addr2line(shndx, offset); | |
699 | #else | |
700 | gold_unreachable(); | |
701 | #endif | |
702 | else if (parameters->get_size() == 64 && parameters->is_big_endian()) | |
703 | #ifdef HAVE_TARGET_64_BIT | |
704 | return Sized_dwarf_line_info<64, true>(object).addr2line(shndx, offset); | |
705 | #else | |
706 | gold_unreachable(); | |
707 | #endif | |
708 | else | |
709 | gold_unreachable(); | |
710 | } | |
711 | ||
5c2c6c95 ILT |
712 | #ifdef HAVE_TARGET_32_LITTLE |
713 | template | |
a55ce7fe | 714 | class Sized_dwarf_line_info<32, false>; |
5c2c6c95 ILT |
715 | #endif |
716 | ||
717 | #ifdef HAVE_TARGET_32_BIG | |
718 | template | |
a55ce7fe | 719 | class Sized_dwarf_line_info<32, true>; |
5c2c6c95 ILT |
720 | #endif |
721 | ||
722 | #ifdef HAVE_TARGET_64_LITTLE | |
723 | template | |
a55ce7fe | 724 | class Sized_dwarf_line_info<64, false>; |
5c2c6c95 ILT |
725 | #endif |
726 | ||
727 | #ifdef HAVE_TARGET_64_BIG | |
728 | template | |
a55ce7fe | 729 | class Sized_dwarf_line_info<64, true>; |
5c2c6c95 ILT |
730 | #endif |
731 | ||
732 | } // End namespace gold. |