Remove partial implementation that was never completed. This was
[external/binutils.git] / gold / dwarf_reader.cc
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"
27 #include "object.h"
28 #include "parameters.h"
29 #include "reloc.h"
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;
114   lsm->shndx = -1U;
115   lsm->is_stmt = default_is_stmt;
116   lsm->basic_block = false;
117   lsm->end_sequence = false;
118 }
119
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)
125 {
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")
130       {
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;
136       }
137   if (this->buffer_ == NULL)
138     return;
139
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)
146     {
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)
151         {
152           got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
153                                                       reloc_sh_type);
154           break;
155         }
156     }
157
158   // Finally, we need the symtab section to interpret the relocs.
159   if (got_relocs)
160     {
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)
164           {
165             this->symtab_buffer_ = object->section_contents(
166                 symtab_shndx, &this->symtab_buffer_size_, false);
167             break;
168           }
169       if (this->symtab_buffer_ == NULL)
170         return;
171     }
172
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);
177 }
178
179 // Read the DWARF header.
180
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)
185 {
186   uint32_t initial_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
187   lineptr += 4;
188
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)
192     {
193       header_.offset_size = 8;
194       initial_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
195       lineptr += 8;
196     }
197   else
198     header_.offset_size = 4;
199
200   header_.total_length = initial_length;
201
202   gold_assert(lineptr + header_.total_length <= buffer_end_);
203
204   header_.version = elfcpp::Swap<16, big_endian>::readval(lineptr);
205   lineptr += 2;
206
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;
212
213   header_.min_insn_length = *lineptr;
214   lineptr += 1;
215
216   header_.default_is_stmt = *lineptr;
217   lineptr += 1;
218
219   header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
220   lineptr += 1;
221
222   header_.line_range = *lineptr;
223   lineptr += 1;
224
225   header_.opcode_base = *lineptr;
226   lineptr += 1;
227
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++)
231     {
232       header_.std_opcode_lengths[i] = *lineptr;
233       lineptr += 1;
234     }
235
236   return lineptr;
237 }
238
239 // The header for a debug_line section is mildly complicated, because
240 // the line info is very tightly encoded.
241
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)
246 {
247   ++this->current_header_index_;
248
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));
258
259   // It is legal for the directory entry table to be empty.
260   if (*lineptr)
261     {
262       int dirindex = 1;
263       while (*lineptr)
264         {
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++;
271         }
272     }
273   lineptr++;
274
275   // It is also legal for the file entry table to be empty.
276   if (*lineptr)
277     {
278       int fileindex = 1;
279       size_t len;
280       while (*lineptr)
281         {
282           const char* filename = reinterpret_cast<const char*>(lineptr);
283           lineptr += strlen(filename) + 1;
284
285           uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
286           lineptr += len;
287
288           if (dirindex >= this->directories_.back().size())
289             dirindex = 0;
290           int dirindexi = static_cast<int>(dirindex);
291
292           read_unsigned_LEB_128(lineptr, &len);   // mod_time
293           lineptr += len;
294
295           read_unsigned_LEB_128(lineptr, &len);   // filelength
296           lineptr += len;
297
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++;
302         }
303     }
304   lineptr++;
305
306   return lineptr;
307 }
308
309 // Process a single opcode in the .debug.line structure.
310
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.
314
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)
319 {
320   size_t oplen = 0;
321   size_t templen;
322   unsigned char opcode = *start;
323   oplen++;
324   start++;
325
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)
329     {
330       opcode -= header_.opcode_base;
331       const int advance_address = ((opcode / header_.line_range)
332                                    * header_.min_insn_length);
333       lsm->address += advance_address;
334
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;
341     }
342
343   // Otherwise, we have the regular opcodes
344   switch (opcode)
345     {
346     case elfcpp::DW_LNS_copy:
347       lsm->basic_block = false;
348       *len = oplen;
349       return true;
350
351     case elfcpp::DW_LNS_advance_pc:
352       {
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;
357       }
358       break;
359
360     case elfcpp::DW_LNS_advance_line:
361       {
362         const uint64_t advance_line = read_signed_LEB_128(start, &templen);
363         oplen += templen;
364         lsm->line_num += advance_line;
365       }
366       break;
367
368     case elfcpp::DW_LNS_set_file:
369       {
370         const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
371         oplen += templen;
372         lsm->file_num = fileno;
373       }
374       break;
375
376     case elfcpp::DW_LNS_set_column:
377       {
378         const uint64_t colno = read_unsigned_LEB_128(start, &templen);
379         oplen += templen;
380         lsm->column_num = colno;
381       }
382       break;
383
384     case elfcpp::DW_LNS_negate_stmt:
385       lsm->is_stmt = !lsm->is_stmt;
386       break;
387
388     case elfcpp::DW_LNS_set_basic_block:
389       lsm->basic_block = true;
390       break;
391
392     case elfcpp::DW_LNS_fixed_advance_pc:
393       {
394         int advance_address;
395         advance_address = elfcpp::Swap<16, big_endian>::readval(start);
396         oplen += 2;
397         lsm->address += advance_address;
398       }
399       break;
400
401     case elfcpp::DW_LNS_const_add_pc:
402       {
403         const int advance_address = (header_.min_insn_length
404                                      * ((255 - header_.opcode_base)
405                                         / header_.line_range));
406         lsm->address += advance_address;
407       }
408       break;
409
410     case elfcpp::DW_LNS_extended_op:
411       {
412         const uint64_t extended_op_len
413             = read_unsigned_LEB_128(start, &templen);
414         start += templen;
415         oplen += templen + extended_op_len;
416
417         const unsigned char extended_op = *start;
418         start++;
419
420         switch (extended_op)
421           {
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;
430
431           case elfcpp::DW_LNE_set_address:
432             {
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())
437                 {
438                   // value + addend.
439                   lsm->address += it->second.second;
440                   lsm->shndx = it->second.first;
441                 }
442               else
443                 {
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;
448                 }
449               break;
450             }
451           case elfcpp::DW_LNE_define_file:
452             {
453               const char* filename  = reinterpret_cast<const char*>(start);
454               templen = strlen(filename) + 1;
455               start += templen;
456
457               uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
458               oplen += templen;
459
460               if (dirindex >= this->directories_.back().size())
461                 dirindex = 0;
462               int dirindexi = static_cast<int>(dirindex);
463
464               read_unsigned_LEB_128(start, &templen);   // mod_time
465               oplen += templen;
466
467               read_unsigned_LEB_128(start, &templen);   // filelength
468               oplen += templen;
469
470               this->files_.back().push_back(std::make_pair(dirindexi,
471                                                            filename));
472             }
473             break;
474           }
475       }
476       break;
477
478     default:
479       {
480         // Ignore unknown opcode  silently
481         for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
482           {
483             size_t templen;
484             read_unsigned_LEB_128(start, &templen);
485             start += templen;
486             oplen += templen;
487           }
488       }
489       break;
490   }
491   *len = oplen;
492   return false;
493 }
494
495 // Read the debug information at LINEPTR and store it in the line
496 // number map.
497
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)
502 {
503   struct LineStateMachine lsm;
504
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_;
508
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;
515
516   while (lineptr < lengthstart + header_.total_length)
517     {
518       ResetLineStateMachine(&lsm, header_.default_is_stmt);
519       while (!lsm.end_sequence)
520         {
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))
525             {
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);
530             }
531           lineptr += oplength;
532         }
533     }
534
535   return lengthstart + header_.total_length;
536 }
537
538 // Looks in the symtab to see what section a symbol is in.
539
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)
545 {
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();
551 }
552
553 // Read the relocations into a Reloc_map.
554
555 template<int size, bool big_endian>
556 void
557 Sized_dwarf_line_info<size, big_endian>::read_relocs()
558 {
559   if (this->symtab_buffer_ == NULL)
560     return;
561
562   typename elfcpp::Elf_types<size>::Elf_Addr value;
563   off_t reloc_offset;
564   while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
565     {
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);
570     }
571 }
572
573 // Read the line number info.
574
575 template<int size, bool big_endian>
576 void
577 Sized_dwarf_line_info<size, big_endian>::read_line_mappings(off_t shndx)
578 {
579   gold_assert(this->data_valid_ == true);
580
581   read_relocs();
582   while (this->buffer_ < this->buffer_end_)
583     {
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;
589     }
590
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());
597 }
598
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.
604
605 template<int size, bool big_endian>
606 bool
607 Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
608 {
609   // Only .o files have relocs and the symtab buffer that goes with them.
610   return this->symtab_buffer_ != NULL;
611 }
612
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().
618
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)
622 {
623   const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 };
624
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);
629
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).
657   //
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.)
691
692   // This deals with cases (1) and (2).
693   if ((it == offsets->begin() && offset < it->offset)
694       || it == offsets->end())
695     return offsets->end();
696
697   // This deals with cases (3) and (4).
698   if (offset == it->offset)
699     {
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;
708     }
709
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;
718
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();
726 }
727
728 // Return a string for a file name and line number.
729
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)
734 {
735   if (this->data_valid_ == false)
736     return "";
737
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 "";
748
749   typename std::vector<Offset_to_lineno_entry>::const_iterator it
750       = offset_to_iterator(offsets, offset);
751   if (it == offsets->end())
752     return "";
753
754   // Convert the file_num + line_num into a string.
755   std::string ret;
756
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;
763
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];
769
770   if (!dirname.empty())
771     {
772       ret += dirname;
773       ret += "/";
774     }
775   ret += filename;
776   if (ret.empty())
777     ret = "(unknown)";
778
779   char buffer[64];   // enough to hold a line number
780   snprintf(buffer, sizeof(buffer), "%d", it->line_num);
781   ret += ":";
782   ret += buffer;
783
784   return ret;
785 }
786
787 // Dwarf_line_info routines.
788
789 std::string
790 Dwarf_line_info::one_addr2line(Object* object,
791                                unsigned int shndx, off_t offset)
792 {
793   switch (parameters->size_and_endianness())
794     {
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();
817     }
818 }
819
820 #ifdef HAVE_TARGET_32_LITTLE
821 template
822 class Sized_dwarf_line_info<32, false>;
823 #endif
824
825 #ifdef HAVE_TARGET_32_BIG
826 template
827 class Sized_dwarf_line_info<32, true>;
828 #endif
829
830 #ifdef HAVE_TARGET_64_LITTLE
831 template
832 class Sized_dwarf_line_info<64, false>;
833 #endif
834
835 #ifdef HAVE_TARGET_64_BIG
836 template
837 class Sized_dwarf_line_info<64, true>;
838 #endif
839
840 } // End namespace gold.