Fix handling of RELA relative relocs against local symbols in merge
[binutils.git] / gold / dwarf_reader.cc
blobf2ae53a955fa1be46484e2d5ecd4ab2ec9cd029e
1 // dwarf_reader.cc -- parse dwarf2/3 debug information
3 // Copyright 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include "elfcpp_swap.h"
26 #include "dwarf.h"
27 #include "object.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "dwarf_reader.h"
32 namespace {
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.
38 uint64_t
39 read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
41 uint64_t result = 0;
42 size_t num_read = 0;
43 unsigned int shift = 0;
44 unsigned char byte;
48 byte = *buffer++;
49 num_read++;
50 result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
51 shift += 7;
53 while (byte & 0x80);
55 *len = num_read;
57 return result;
60 // Read a signed LEB128 number. These are like regular LEB128
61 // numbers, except the last byte may have a sign bit set.
63 int64_t
64 read_signed_LEB_128(const unsigned char* buffer, size_t* len)
66 int64_t result = 0;
67 int shift = 0;
68 size_t num_read = 0;
69 unsigned char byte;
73 byte = *buffer++;
74 num_read++;
75 result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
76 shift += 7;
78 while (byte & 0x80);
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;
86 } // End anonymous namespace.
89 namespace gold {
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.
95 struct LineStateMachine
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;
107 static void
108 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
110 lsm->file_num = 1;
111 lsm->address = 0;
112 lsm->line_num = 1;
113 lsm->column_num = 0;
114 lsm->shndx = -1U;
115 lsm->is_stmt = default_is_stmt;
116 lsm->basic_block = false;
117 lsm->end_sequence = false;
120 template<int size, bool big_endian>
121 Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object,
122 off_t read_shndx)
123 : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL),
124 directories_(), files_(), current_header_index_(-1)
126 unsigned int debug_shndx;
127 for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx)
128 // FIXME: do this more efficiently: section_name() isn't super-fast
129 if (object->section_name(debug_shndx) == ".debug_line")
131 section_size_type buffer_size;
132 this->buffer_ = object->section_contents(debug_shndx, &buffer_size,
133 false);
134 this->buffer_end_ = this->buffer_ + buffer_size;
135 break;
137 if (this->buffer_ == NULL)
138 return;
140 // Find the relocation section for ".debug_line".
141 // We expect these for relobjs (.o's) but not dynobjs (.so's).
142 bool got_relocs = false;
143 for (unsigned int reloc_shndx = 0;
144 reloc_shndx < object->shnum();
145 ++reloc_shndx)
147 unsigned int reloc_sh_type = object->section_type(reloc_shndx);
148 if ((reloc_sh_type == elfcpp::SHT_REL
149 || reloc_sh_type == elfcpp::SHT_RELA)
150 && object->section_info(reloc_shndx) == debug_shndx)
152 got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
153 reloc_sh_type);
154 break;
158 // Finally, we need the symtab section to interpret the relocs.
159 if (got_relocs)
161 unsigned int symtab_shndx;
162 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
163 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
165 this->symtab_buffer_ = object->section_contents(
166 symtab_shndx, &this->symtab_buffer_size_, false);
167 break;
169 if (this->symtab_buffer_ == NULL)
170 return;
173 // Now that we have successfully read all the data, parse the debug
174 // info.
175 this->data_valid_ = true;
176 this->read_line_mappings(read_shndx);
179 // Read the DWARF header.
181 template<int size, bool big_endian>
182 const unsigned char*
183 Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
184 const unsigned char* lineptr)
186 uint32_t initial_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
187 lineptr += 4;
189 // In DWARF2/3, if the initial length is all 1 bits, then the offset
190 // size is 8 and we need to read the next 8 bytes for the real length.
191 if (initial_length == 0xffffffff)
193 header_.offset_size = 8;
194 initial_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
195 lineptr += 8;
197 else
198 header_.offset_size = 4;
200 header_.total_length = initial_length;
202 gold_assert(lineptr + header_.total_length <= buffer_end_);
204 header_.version = elfcpp::Swap<16, big_endian>::readval(lineptr);
205 lineptr += 2;
207 if (header_.offset_size == 4)
208 header_.prologue_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
209 else
210 header_.prologue_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
211 lineptr += header_.offset_size;
213 header_.min_insn_length = *lineptr;
214 lineptr += 1;
216 header_.default_is_stmt = *lineptr;
217 lineptr += 1;
219 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
220 lineptr += 1;
222 header_.line_range = *lineptr;
223 lineptr += 1;
225 header_.opcode_base = *lineptr;
226 lineptr += 1;
228 header_.std_opcode_lengths.reserve(header_.opcode_base + 1);
229 header_.std_opcode_lengths[0] = 0;
230 for (int i = 1; i < header_.opcode_base; i++)
232 header_.std_opcode_lengths[i] = *lineptr;
233 lineptr += 1;
236 return lineptr;
239 // The header for a debug_line section is mildly complicated, because
240 // the line info is very tightly encoded.
242 template<int size, bool big_endian>
243 const unsigned char*
244 Sized_dwarf_line_info<size, big_endian>::read_header_tables(
245 const unsigned char* lineptr)
247 ++this->current_header_index_;
249 // Create a new directories_ entry and a new files_ entry for our new
250 // header. We initialize each with a single empty element, because
251 // dwarf indexes directory and filenames starting at 1.
252 gold_assert(static_cast<int>(this->directories_.size())
253 == this->current_header_index_);
254 gold_assert(static_cast<int>(this->files_.size())
255 == this->current_header_index_);
256 this->directories_.push_back(std::vector<std::string>(1));
257 this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
259 // It is legal for the directory entry table to be empty.
260 if (*lineptr)
262 int dirindex = 1;
263 while (*lineptr)
265 const char* dirname = reinterpret_cast<const char*>(lineptr);
266 gold_assert(dirindex
267 == static_cast<int>(this->directories_.back().size()));
268 this->directories_.back().push_back(dirname);
269 lineptr += this->directories_.back().back().size() + 1;
270 dirindex++;
273 lineptr++;
275 // It is also legal for the file entry table to be empty.
276 if (*lineptr)
278 int fileindex = 1;
279 size_t len;
280 while (*lineptr)
282 const char* filename = reinterpret_cast<const char*>(lineptr);
283 lineptr += strlen(filename) + 1;
285 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
286 lineptr += len;
288 if (dirindex >= this->directories_.back().size())
289 dirindex = 0;
290 int dirindexi = static_cast<int>(dirindex);
292 read_unsigned_LEB_128(lineptr, &len); // mod_time
293 lineptr += len;
295 read_unsigned_LEB_128(lineptr, &len); // filelength
296 lineptr += len;
298 gold_assert(fileindex
299 == static_cast<int>(this->files_.back().size()));
300 this->files_.back().push_back(std::make_pair(dirindexi, filename));
301 fileindex++;
304 lineptr++;
306 return lineptr;
309 // Process a single opcode in the .debug.line structure.
311 // Templating on size and big_endian would yield more efficient (and
312 // simpler) code, but would bloat the binary. Speed isn't important
313 // here.
315 template<int size, bool big_endian>
316 bool
317 Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
318 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
320 size_t oplen = 0;
321 size_t templen;
322 unsigned char opcode = *start;
323 oplen++;
324 start++;
326 // If the opcode is great than the opcode_base, it is a special
327 // opcode. Most line programs consist mainly of special opcodes.
328 if (opcode >= header_.opcode_base)
330 opcode -= header_.opcode_base;
331 const int advance_address = ((opcode / header_.line_range)
332 * header_.min_insn_length);
333 lsm->address += advance_address;
335 const int advance_line = ((opcode % header_.line_range)
336 + header_.line_base);
337 lsm->line_num += advance_line;
338 lsm->basic_block = true;
339 *len = oplen;
340 return true;
343 // Otherwise, we have the regular opcodes
344 switch (opcode)
346 case elfcpp::DW_LNS_copy:
347 lsm->basic_block = false;
348 *len = oplen;
349 return true;
351 case elfcpp::DW_LNS_advance_pc:
353 const uint64_t advance_address
354 = read_unsigned_LEB_128(start, &templen);
355 oplen += templen;
356 lsm->address += header_.min_insn_length * advance_address;
358 break;
360 case elfcpp::DW_LNS_advance_line:
362 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
363 oplen += templen;
364 lsm->line_num += advance_line;
366 break;
368 case elfcpp::DW_LNS_set_file:
370 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
371 oplen += templen;
372 lsm->file_num = fileno;
374 break;
376 case elfcpp::DW_LNS_set_column:
378 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
379 oplen += templen;
380 lsm->column_num = colno;
382 break;
384 case elfcpp::DW_LNS_negate_stmt:
385 lsm->is_stmt = !lsm->is_stmt;
386 break;
388 case elfcpp::DW_LNS_set_basic_block:
389 lsm->basic_block = true;
390 break;
392 case elfcpp::DW_LNS_fixed_advance_pc:
394 int advance_address;
395 advance_address = elfcpp::Swap<16, big_endian>::readval(start);
396 oplen += 2;
397 lsm->address += advance_address;
399 break;
401 case elfcpp::DW_LNS_const_add_pc:
403 const int advance_address = (header_.min_insn_length
404 * ((255 - header_.opcode_base)
405 / header_.line_range));
406 lsm->address += advance_address;
408 break;
410 case elfcpp::DW_LNS_extended_op:
412 const uint64_t extended_op_len
413 = read_unsigned_LEB_128(start, &templen);
414 start += templen;
415 oplen += templen + extended_op_len;
417 const unsigned char extended_op = *start;
418 start++;
420 switch (extended_op)
422 case elfcpp::DW_LNE_end_sequence:
423 // This means that the current byte is the one immediately
424 // after a set of instructions. Record the current line
425 // for up to one less than the current address.
426 lsm->line_num = -1;
427 lsm->end_sequence = true;
428 *len = oplen;
429 return true;
431 case elfcpp::DW_LNE_set_address:
433 lsm->address = elfcpp::Swap<size, big_endian>::readval(start);
434 typename Reloc_map::const_iterator it
435 = reloc_map_.find(start - this->buffer_);
436 if (it != reloc_map_.end())
438 // value + addend.
439 lsm->address += it->second.second;
440 lsm->shndx = it->second.first;
442 else
444 // If we're a normal .o file, with relocs, every
445 // set_address should have an associated relocation.
446 if (this->input_is_relobj())
447 this->data_valid_ = false;
449 break;
451 case elfcpp::DW_LNE_define_file:
453 const char* filename = reinterpret_cast<const char*>(start);
454 templen = strlen(filename) + 1;
455 start += templen;
457 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
458 oplen += templen;
460 if (dirindex >= this->directories_.back().size())
461 dirindex = 0;
462 int dirindexi = static_cast<int>(dirindex);
464 read_unsigned_LEB_128(start, &templen); // mod_time
465 oplen += templen;
467 read_unsigned_LEB_128(start, &templen); // filelength
468 oplen += templen;
470 this->files_.back().push_back(std::make_pair(dirindexi,
471 filename));
473 break;
476 break;
478 default:
480 // Ignore unknown opcode silently
481 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
483 size_t templen;
484 read_unsigned_LEB_128(start, &templen);
485 start += templen;
486 oplen += templen;
489 break;
491 *len = oplen;
492 return false;
495 // Read the debug information at LINEPTR and store it in the line
496 // number map.
498 template<int size, bool big_endian>
499 unsigned const char*
500 Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
501 off_t shndx)
503 struct LineStateMachine lsm;
505 // LENGTHSTART is the place the length field is based on. It is the
506 // point in the header after the initial length field.
507 const unsigned char* lengthstart = buffer_;
509 // In 64 bit dwarf, the initial length is 12 bytes, because of the
510 // 0xffffffff at the start.
511 if (header_.offset_size == 8)
512 lengthstart += 12;
513 else
514 lengthstart += 4;
516 while (lineptr < lengthstart + header_.total_length)
518 ResetLineStateMachine(&lsm, header_.default_is_stmt);
519 while (!lsm.end_sequence)
521 size_t oplength;
522 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
523 if (add_line
524 && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
526 Offset_to_lineno_entry entry
527 = { lsm.address, this->current_header_index_,
528 lsm.file_num, lsm.line_num };
529 line_number_map_[lsm.shndx].push_back(entry);
531 lineptr += oplength;
535 return lengthstart + header_.total_length;
538 // Looks in the symtab to see what section a symbol is in.
540 template<int size, bool big_endian>
541 unsigned int
542 Sized_dwarf_line_info<size, big_endian>::symbol_section(
543 unsigned int sym,
544 typename elfcpp::Elf_types<size>::Elf_Addr* value)
546 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
547 gold_assert(sym * symsize < this->symtab_buffer_size_);
548 elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
549 *value = elfsym.get_st_value();
550 return elfsym.get_st_shndx();
553 // Read the relocations into a Reloc_map.
555 template<int size, bool big_endian>
556 void
557 Sized_dwarf_line_info<size, big_endian>::read_relocs()
559 if (this->symtab_buffer_ == NULL)
560 return;
562 typename elfcpp::Elf_types<size>::Elf_Addr value;
563 off_t reloc_offset;
564 while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
566 const unsigned int sym = this->track_relocs_.next_symndx();
567 const unsigned int shndx = this->symbol_section(sym, &value);
568 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
569 this->track_relocs_.advance(reloc_offset + 1);
573 // Read the line number info.
575 template<int size, bool big_endian>
576 void
577 Sized_dwarf_line_info<size, big_endian>::read_line_mappings(off_t shndx)
579 gold_assert(this->data_valid_ == true);
581 read_relocs();
582 while (this->buffer_ < this->buffer_end_)
584 const unsigned char* lineptr = this->buffer_;
585 lineptr = this->read_header_prolog(lineptr);
586 lineptr = this->read_header_tables(lineptr);
587 lineptr = this->read_lines(lineptr, shndx);
588 this->buffer_ = lineptr;
591 // Sort the lines numbers, so addr2line can use binary search.
592 for (typename Lineno_map::iterator it = line_number_map_.begin();
593 it != line_number_map_.end();
594 ++it)
595 // Each vector needs to be sorted by offset.
596 std::sort(it->second.begin(), it->second.end());
599 // Some processing depends on whether the input is a .o file or not.
600 // For instance, .o files have relocs, and have .debug_lines
601 // information on a per section basis. .so files, on the other hand,
602 // lack relocs, and offsets are unique, so we can ignore the section
603 // information.
605 template<int size, bool big_endian>
606 bool
607 Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
609 // Only .o files have relocs and the symtab buffer that goes with them.
610 return this->symtab_buffer_ != NULL;
613 // Given an Offset_to_lineno_entry vector, and an offset, figure out
614 // if the offset points into a function according to the vector (see
615 // comments below for the algorithm). If it does, return an iterator
616 // into the vector that points to the line-number that contains that
617 // offset. If not, it returns vector::end().
619 static std::vector<Offset_to_lineno_entry>::const_iterator
620 offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
621 off_t offset)
623 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 };
625 // lower_bound() returns the smallest offset which is >= lookup_key.
626 // If no offset in offsets is >= lookup_key, returns end().
627 std::vector<Offset_to_lineno_entry>::const_iterator it
628 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
630 // This code is easiest to understand with a concrete example.
631 // Here's a possible offsets array:
632 // {{offset = 3211, header_num = 0, file_num = 1, line_num = 16}, // 0
633 // {offset = 3224, header_num = 0, file_num = 1, line_num = 20}, // 1
634 // {offset = 3226, header_num = 0, file_num = 1, line_num = 22}, // 2
635 // {offset = 3231, header_num = 0, file_num = 1, line_num = 25}, // 3
636 // {offset = 3232, header_num = 0, file_num = 1, line_num = -1}, // 4
637 // {offset = 3232, header_num = 0, file_num = 1, line_num = 65}, // 5
638 // {offset = 3235, header_num = 0, file_num = 1, line_num = 66}, // 6
639 // {offset = 3236, header_num = 0, file_num = 1, line_num = -1}, // 7
640 // {offset = 5764, header_num = 0, file_num = 1, line_num = 47}, // 8
641 // {offset = 5765, header_num = 0, file_num = 1, line_num = 48}, // 9
642 // {offset = 5767, header_num = 0, file_num = 1, line_num = 49}, // 10
643 // {offset = 5768, header_num = 0, file_num = 1, line_num = 50}, // 11
644 // {offset = 5773, header_num = 0, file_num = 1, line_num = -1}, // 12
645 // {offset = 5787, header_num = 1, file_num = 1, line_num = 19}, // 13
646 // {offset = 5790, header_num = 1, file_num = 1, line_num = 20}, // 14
647 // {offset = 5793, header_num = 1, file_num = 1, line_num = 67}, // 15
648 // {offset = 5793, header_num = 1, file_num = 1, line_num = -1}, // 16
649 // {offset = 5795, header_num = 1, file_num = 1, line_num = 68}, // 17
650 // {offset = 5798, header_num = 1, file_num = 1, line_num = -1}, // 18
651 // The entries with line_num == -1 mark the end of a function: the
652 // associated offset is one past the last instruction in the
653 // function. This can correspond to the beginning of the next
654 // function (as is true for offset 3232); alternately, there can be
655 // a gap between the end of one function and the start of the next
656 // (as is true for some others, most obviously from 3236->5764).
658 // Case 1: lookup_key has offset == 10. lower_bound returns
659 // offsets[0]. Since it's not an exact match and we're
660 // at the beginning of offsets, we return end() (invalid).
661 // Case 2: lookup_key has offset 10000. lower_bound returns
662 // offset[19] (end()). We return end() (invalid).
663 // Case 3: lookup_key has offset == 3211. lower_bound matches
664 // offsets[0] exactly, and that's the entry we return.
665 // Case 4: lookup_key has offset == 3232. lower_bound returns
666 // offsets[4]. That's an exact match, but indicates
667 // end-of-function. We check if offsets[5] is also an
668 // exact match but not end-of-function. It is, so we
669 // return offsets[5].
670 // Case 5: lookup_key has offset == 3214. lower_bound returns
671 // offsets[1]. Since it's not an exact match, we back
672 // up to the offset that's < lookup_key, offsets[0].
673 // We note offsets[0] is a valid entry (not end-of-function),
674 // so that's the entry we return.
675 // Case 6: lookup_key has offset == 4000. lower_bound returns
676 // offsets[8]. Since it's not an exact match, we back
677 // up to offsets[7]. Since offsets[7] indicates
678 // end-of-function, we know lookup_key is between
679 // functions, so we return end() (not a valid offset).
680 // Case 7: lookup_key has offset == 5794. lower_bound returns
681 // offsets[17]. Since it's not an exact match, we back
682 // up to offsets[15]. Note we back up to the *first*
683 // entry with offset 5793, not just offsets[17-1].
684 // We note offsets[15] is a valid entry, so we return it.
685 // If offsets[15] had had line_num == -1, we would have
686 // checked offsets[16]. The reason for this is that
687 // 15 and 16 can be in an arbitrary order, since we sort
688 // only by offset. (Note it doesn't help to use line_number
689 // as a secondary sort key, since sometimes we want the -1
690 // to be first and sometimes we want it to be last.)
692 // This deals with cases (1) and (2).
693 if ((it == offsets->begin() && offset < it->offset)
694 || it == offsets->end())
695 return offsets->end();
697 // This deals with cases (3) and (4).
698 if (offset == it->offset)
700 while (it != offsets->end()
701 && it->offset == offset
702 && it->line_num == -1)
703 ++it;
704 if (it == offsets->end() || it->offset != offset)
705 return offsets->end();
706 else
707 return it;
710 // This handles the first part of case (7) -- we back up to the
711 // *first* entry that has the offset that's behind us.
712 gold_assert(it != offsets->begin());
713 std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
714 --it;
715 const off_t range_value = it->offset;
716 while (it != offsets->begin() && (it-1)->offset == range_value)
717 --it;
719 // This handles cases (5), (6), and (7): if any entry in the
720 // equal_range [it, range_end) has a line_num != -1, it's a valid
721 // match. If not, we're not in a function.
722 for (; it != range_end; ++it)
723 if (it->line_num != -1)
724 return it;
725 return offsets->end();
728 // Return a string for a file name and line number.
730 template<int size, bool big_endian>
731 std::string
732 Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx,
733 off_t offset)
735 if (this->data_valid_ == false)
736 return "";
738 const std::vector<Offset_to_lineno_entry>* offsets;
739 // If we do not have reloc information, then our input is a .so or
740 // some similar data structure where all the information is held in
741 // the offset. In that case, we ignore the input shndx.
742 if (this->input_is_relobj())
743 offsets = &this->line_number_map_[shndx];
744 else
745 offsets = &this->line_number_map_[-1U];
746 if (offsets->empty())
747 return "";
749 typename std::vector<Offset_to_lineno_entry>::const_iterator it
750 = offset_to_iterator(offsets, offset);
751 if (it == offsets->end())
752 return "";
754 // Convert the file_num + line_num into a string.
755 std::string ret;
757 gold_assert(it->header_num < static_cast<int>(this->files_.size()));
758 gold_assert(it->file_num
759 < static_cast<int>(this->files_[it->header_num].size()));
760 const std::pair<int, std::string>& filename_pair
761 = this->files_[it->header_num][it->file_num];
762 const std::string& filename = filename_pair.second;
764 gold_assert(it->header_num < static_cast<int>(this->directories_.size()));
765 gold_assert(filename_pair.first
766 < static_cast<int>(this->directories_[it->header_num].size()));
767 const std::string& dirname
768 = this->directories_[it->header_num][filename_pair.first];
770 if (!dirname.empty())
772 ret += dirname;
773 ret += "/";
775 ret += filename;
776 if (ret.empty())
777 ret = "(unknown)";
779 char buffer[64]; // enough to hold a line number
780 snprintf(buffer, sizeof(buffer), "%d", it->line_num);
781 ret += ":";
782 ret += buffer;
784 return ret;
787 // Dwarf_line_info routines.
789 std::string
790 Dwarf_line_info::one_addr2line(Object* object,
791 unsigned int shndx, off_t offset)
793 switch (parameters->size_and_endianness())
795 #ifdef HAVE_TARGET_32_LITTLE
796 case Parameters::TARGET_32_LITTLE:
797 return Sized_dwarf_line_info<32, false>(object, shndx).addr2line(shndx,
798 offset);
799 #endif
800 #ifdef HAVE_TARGET_32_BIG
801 case Parameters::TARGET_32_BIG:
802 return Sized_dwarf_line_info<32, true>(object, shndx).addr2line(shndx,
803 offset);
804 #endif
805 #ifdef HAVE_TARGET_64_LITTLE
806 case Parameters::TARGET_64_LITTLE:
807 return Sized_dwarf_line_info<64, false>(object, shndx).addr2line(shndx,
808 offset);
809 #endif
810 #ifdef HAVE_TARGET_64_BIG
811 case Parameters::TARGET_64_BIG:
812 return Sized_dwarf_line_info<64, true>(object, shndx).addr2line(shndx,
813 offset);
814 #endif
815 default:
816 gold_unreachable();
820 #ifdef HAVE_TARGET_32_LITTLE
821 template
822 class Sized_dwarf_line_info<32, false>;
823 #endif
825 #ifdef HAVE_TARGET_32_BIG
826 template
827 class Sized_dwarf_line_info<32, true>;
828 #endif
830 #ifdef HAVE_TARGET_64_LITTLE
831 template
832 class Sized_dwarf_line_info<64, false>;
833 #endif
835 #ifdef HAVE_TARGET_64_BIG
836 template
837 class Sized_dwarf_line_info<64, true>;
838 #endif
840 } // End namespace gold.