OSDN Git Service

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