1a9fa9459Szrj // dwarf_reader.cc -- parse dwarf2/3 debug information
2a9fa9459Szrj 
3a9fa9459Szrj // Copyright (C) 2007-2016 Free Software Foundation, Inc.
4a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5a9fa9459Szrj 
6a9fa9459Szrj // This file is part of gold.
7a9fa9459Szrj 
8a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11a9fa9459Szrj // (at your option) any later version.
12a9fa9459Szrj 
13a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16a9fa9459Szrj // GNU General Public License for more details.
17a9fa9459Szrj 
18a9fa9459Szrj // You should have received a copy of the GNU General Public License
19a9fa9459Szrj // along with this program; if not, write to the Free Software
20a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21a9fa9459Szrj // MA 02110-1301, USA.
22a9fa9459Szrj 
23a9fa9459Szrj #include "gold.h"
24a9fa9459Szrj 
25a9fa9459Szrj #include <algorithm>
26a9fa9459Szrj #include <utility>
27a9fa9459Szrj #include <vector>
28a9fa9459Szrj 
29a9fa9459Szrj #include "elfcpp_swap.h"
30a9fa9459Szrj #include "dwarf.h"
31a9fa9459Szrj #include "object.h"
32a9fa9459Szrj #include "reloc.h"
33a9fa9459Szrj #include "dwarf_reader.h"
34a9fa9459Szrj #include "int_encoding.h"
35a9fa9459Szrj #include "compressed_output.h"
36a9fa9459Szrj 
37a9fa9459Szrj namespace gold {
38a9fa9459Szrj 
39a9fa9459Szrj // Class Sized_elf_reloc_mapper
40a9fa9459Szrj 
41a9fa9459Szrj // Initialize the relocation tracker for section RELOC_SHNDX.
42a9fa9459Szrj 
43a9fa9459Szrj template<int size, bool big_endian>
44a9fa9459Szrj bool
do_initialize(unsigned int reloc_shndx,unsigned int reloc_type)45a9fa9459Szrj Sized_elf_reloc_mapper<size, big_endian>::do_initialize(
46a9fa9459Szrj     unsigned int reloc_shndx, unsigned int reloc_type)
47a9fa9459Szrj {
48a9fa9459Szrj   this->reloc_type_ = reloc_type;
49a9fa9459Szrj   return this->track_relocs_.initialize(this->object_, reloc_shndx,
50a9fa9459Szrj 					reloc_type);
51a9fa9459Szrj }
52a9fa9459Szrj 
53a9fa9459Szrj // Looks in the symtab to see what section a symbol is in.
54a9fa9459Szrj 
55a9fa9459Szrj template<int size, bool big_endian>
56a9fa9459Szrj unsigned int
symbol_section(unsigned int symndx,Address * value,bool * is_ordinary)57a9fa9459Szrj Sized_elf_reloc_mapper<size, big_endian>::symbol_section(
58a9fa9459Szrj     unsigned int symndx, Address* value, bool* is_ordinary)
59a9fa9459Szrj {
60a9fa9459Szrj   const int symsize = elfcpp::Elf_sizes<size>::sym_size;
61a9fa9459Szrj   gold_assert(static_cast<off_t>((symndx + 1) * symsize) <= this->symtab_size_);
62a9fa9459Szrj   elfcpp::Sym<size, big_endian> elfsym(this->symtab_ + symndx * symsize);
63a9fa9459Szrj   *value = elfsym.get_st_value();
64a9fa9459Szrj   return this->object_->adjust_sym_shndx(symndx, elfsym.get_st_shndx(),
65a9fa9459Szrj 					 is_ordinary);
66a9fa9459Szrj }
67a9fa9459Szrj 
68a9fa9459Szrj // Return the section index and offset within the section of
69a9fa9459Szrj // the target of the relocation for RELOC_OFFSET.
70a9fa9459Szrj 
71a9fa9459Szrj template<int size, bool big_endian>
72a9fa9459Szrj unsigned int
do_get_reloc_target(off_t reloc_offset,off_t * target_offset)73a9fa9459Szrj Sized_elf_reloc_mapper<size, big_endian>::do_get_reloc_target(
74a9fa9459Szrj     off_t reloc_offset, off_t* target_offset)
75a9fa9459Szrj {
76a9fa9459Szrj   this->track_relocs_.advance(reloc_offset);
77a9fa9459Szrj   if (reloc_offset != this->track_relocs_.next_offset())
78a9fa9459Szrj     return 0;
79a9fa9459Szrj   unsigned int symndx = this->track_relocs_.next_symndx();
80a9fa9459Szrj   typename elfcpp::Elf_types<size>::Elf_Addr value;
81a9fa9459Szrj   bool is_ordinary;
82a9fa9459Szrj   unsigned int target_shndx = this->symbol_section(symndx, &value,
83a9fa9459Szrj 						   &is_ordinary);
84a9fa9459Szrj   if (!is_ordinary)
85a9fa9459Szrj     return 0;
86a9fa9459Szrj   if (this->reloc_type_ == elfcpp::SHT_RELA)
87a9fa9459Szrj     value += this->track_relocs_.next_addend();
88a9fa9459Szrj   *target_offset = value;
89a9fa9459Szrj   return target_shndx;
90a9fa9459Szrj }
91a9fa9459Szrj 
92a9fa9459Szrj static inline Elf_reloc_mapper*
make_elf_reloc_mapper(Relobj * object,const unsigned char * symtab,off_t symtab_size)93a9fa9459Szrj make_elf_reloc_mapper(Relobj* object, const unsigned char* symtab,
94a9fa9459Szrj 		      off_t symtab_size)
95a9fa9459Szrj {
96a9fa9459Szrj   if (object->elfsize() == 32)
97a9fa9459Szrj     {
98a9fa9459Szrj       if (object->is_big_endian())
99a9fa9459Szrj         {
100a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
101a9fa9459Szrj 	  return new Sized_elf_reloc_mapper<32, true>(object, symtab,
102a9fa9459Szrj 						      symtab_size);
103a9fa9459Szrj #else
104a9fa9459Szrj 	  gold_unreachable();
105a9fa9459Szrj #endif
106a9fa9459Szrj         }
107a9fa9459Szrj       else
108a9fa9459Szrj         {
109a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
110a9fa9459Szrj 	  return new Sized_elf_reloc_mapper<32, false>(object, symtab,
111a9fa9459Szrj 						       symtab_size);
112a9fa9459Szrj #else
113a9fa9459Szrj 	  gold_unreachable();
114a9fa9459Szrj #endif
115a9fa9459Szrj         }
116a9fa9459Szrj     }
117a9fa9459Szrj   else if (object->elfsize() == 64)
118a9fa9459Szrj     {
119a9fa9459Szrj       if (object->is_big_endian())
120a9fa9459Szrj         {
121a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
122a9fa9459Szrj 	  return new Sized_elf_reloc_mapper<64, true>(object, symtab,
123a9fa9459Szrj 						      symtab_size);
124a9fa9459Szrj #else
125a9fa9459Szrj 	  gold_unreachable();
126a9fa9459Szrj #endif
127a9fa9459Szrj         }
128a9fa9459Szrj       else
129a9fa9459Szrj         {
130a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
131a9fa9459Szrj 	  return new Sized_elf_reloc_mapper<64, false>(object, symtab,
132a9fa9459Szrj 						       symtab_size);
133a9fa9459Szrj #else
134a9fa9459Szrj 	  gold_unreachable();
135a9fa9459Szrj #endif
136a9fa9459Szrj         }
137a9fa9459Szrj     }
138a9fa9459Szrj   else
139a9fa9459Szrj     gold_unreachable();
140a9fa9459Szrj }
141a9fa9459Szrj 
142a9fa9459Szrj // class Dwarf_abbrev_table
143a9fa9459Szrj 
144a9fa9459Szrj void
clear_abbrev_codes()145a9fa9459Szrj Dwarf_abbrev_table::clear_abbrev_codes()
146a9fa9459Szrj {
147a9fa9459Szrj   for (unsigned int code = 0; code < this->low_abbrev_code_max_; ++code)
148a9fa9459Szrj     {
149a9fa9459Szrj       if (this->low_abbrev_codes_[code] != NULL)
150a9fa9459Szrj 	{
151a9fa9459Szrj 	  delete this->low_abbrev_codes_[code];
152a9fa9459Szrj 	  this->low_abbrev_codes_[code] = NULL;
153a9fa9459Szrj 	}
154a9fa9459Szrj     }
155a9fa9459Szrj   for (Abbrev_code_table::iterator it = this->high_abbrev_codes_.begin();
156a9fa9459Szrj        it != this->high_abbrev_codes_.end();
157a9fa9459Szrj        ++it)
158a9fa9459Szrj     {
159a9fa9459Szrj       if (it->second != NULL)
160a9fa9459Szrj 	delete it->second;
161a9fa9459Szrj     }
162a9fa9459Szrj   this->high_abbrev_codes_.clear();
163a9fa9459Szrj }
164a9fa9459Szrj 
165a9fa9459Szrj // Read the abbrev table from an object file.
166a9fa9459Szrj 
167a9fa9459Szrj bool
do_read_abbrevs(Relobj * object,unsigned int abbrev_shndx,off_t abbrev_offset)168a9fa9459Szrj Dwarf_abbrev_table::do_read_abbrevs(
169a9fa9459Szrj     Relobj* object,
170a9fa9459Szrj     unsigned int abbrev_shndx,
171a9fa9459Szrj     off_t abbrev_offset)
172a9fa9459Szrj {
173a9fa9459Szrj   this->clear_abbrev_codes();
174a9fa9459Szrj 
175a9fa9459Szrj   // If we don't have relocations, abbrev_shndx will be 0, and
176a9fa9459Szrj   // we'll have to hunt for the .debug_abbrev section.
177a9fa9459Szrj   if (abbrev_shndx == 0 && this->abbrev_shndx_ > 0)
178a9fa9459Szrj     abbrev_shndx = this->abbrev_shndx_;
179a9fa9459Szrj   else if (abbrev_shndx == 0)
180a9fa9459Szrj     {
181a9fa9459Szrj       for (unsigned int i = 1; i < object->shnum(); ++i)
182a9fa9459Szrj 	{
183a9fa9459Szrj 	  std::string name = object->section_name(i);
184a9fa9459Szrj 	  if (name == ".debug_abbrev" || name == ".zdebug_abbrev")
185a9fa9459Szrj 	    {
186a9fa9459Szrj 	      abbrev_shndx = i;
187a9fa9459Szrj 	      // Correct the offset.  For incremental update links, we have a
188a9fa9459Szrj 	      // relocated offset that is relative to the output section, but
189a9fa9459Szrj 	      // here we need an offset relative to the input section.
190a9fa9459Szrj 	      abbrev_offset -= object->output_section_offset(i);
191a9fa9459Szrj 	      break;
192a9fa9459Szrj 	    }
193a9fa9459Szrj 	}
194a9fa9459Szrj       if (abbrev_shndx == 0)
195a9fa9459Szrj 	return false;
196a9fa9459Szrj     }
197a9fa9459Szrj 
198a9fa9459Szrj   // Get the section contents and decompress if necessary.
199a9fa9459Szrj   if (abbrev_shndx != this->abbrev_shndx_)
200a9fa9459Szrj     {
201a9fa9459Szrj       if (this->owns_buffer_ && this->buffer_ != NULL)
202a9fa9459Szrj         {
203a9fa9459Szrj 	  delete[] this->buffer_;
204a9fa9459Szrj 	  this->owns_buffer_ = false;
205a9fa9459Szrj         }
206a9fa9459Szrj 
207a9fa9459Szrj       section_size_type buffer_size;
208a9fa9459Szrj       this->buffer_ =
209a9fa9459Szrj 	  object->decompressed_section_contents(abbrev_shndx,
210a9fa9459Szrj 						&buffer_size,
211a9fa9459Szrj 						&this->owns_buffer_);
212a9fa9459Szrj       this->buffer_end_ = this->buffer_ + buffer_size;
213a9fa9459Szrj       this->abbrev_shndx_ = abbrev_shndx;
214a9fa9459Szrj     }
215a9fa9459Szrj 
216a9fa9459Szrj   this->buffer_pos_ = this->buffer_ + abbrev_offset;
217a9fa9459Szrj   return true;
218a9fa9459Szrj }
219a9fa9459Szrj 
220a9fa9459Szrj // Lookup the abbrev code entry for CODE.  This function is called
221a9fa9459Szrj // only when the abbrev code is not in the direct lookup table.
222a9fa9459Szrj // It may be in the hash table, it may not have been read yet,
223a9fa9459Szrj // or it may not exist in the abbrev table.
224a9fa9459Szrj 
225a9fa9459Szrj const Dwarf_abbrev_table::Abbrev_code*
do_get_abbrev(unsigned int code)226a9fa9459Szrj Dwarf_abbrev_table::do_get_abbrev(unsigned int code)
227a9fa9459Szrj {
228a9fa9459Szrj   // See if the abbrev code is already in the hash table.
229a9fa9459Szrj   Abbrev_code_table::const_iterator it = this->high_abbrev_codes_.find(code);
230a9fa9459Szrj   if (it != this->high_abbrev_codes_.end())
231a9fa9459Szrj     return it->second;
232a9fa9459Szrj 
233a9fa9459Szrj   // Read and store abbrev code definitions until we find the
234a9fa9459Szrj   // one we're looking for.
235a9fa9459Szrj   for (;;)
236a9fa9459Szrj     {
237a9fa9459Szrj       // Read the abbrev code.  A zero here indicates the end of the
238a9fa9459Szrj       // abbrev table.
239a9fa9459Szrj       size_t len;
240a9fa9459Szrj       if (this->buffer_pos_ >= this->buffer_end_)
241a9fa9459Szrj 	return NULL;
242a9fa9459Szrj       uint64_t nextcode = read_unsigned_LEB_128(this->buffer_pos_, &len);
243a9fa9459Szrj       if (nextcode == 0)
244a9fa9459Szrj 	{
245a9fa9459Szrj 	  this->buffer_pos_ = this->buffer_end_;
246a9fa9459Szrj 	  return NULL;
247a9fa9459Szrj 	}
248a9fa9459Szrj       this->buffer_pos_ += len;
249a9fa9459Szrj 
250a9fa9459Szrj       // Read the tag.
251a9fa9459Szrj       if (this->buffer_pos_ >= this->buffer_end_)
252a9fa9459Szrj 	return NULL;
253a9fa9459Szrj       uint64_t tag = read_unsigned_LEB_128(this->buffer_pos_, &len);
254a9fa9459Szrj       this->buffer_pos_ += len;
255a9fa9459Szrj 
256a9fa9459Szrj       // Read the has_children flag.
257a9fa9459Szrj       if (this->buffer_pos_ >= this->buffer_end_)
258a9fa9459Szrj 	return NULL;
259a9fa9459Szrj       bool has_children = *this->buffer_pos_ == elfcpp::DW_CHILDREN_yes;
260a9fa9459Szrj       this->buffer_pos_ += 1;
261a9fa9459Szrj 
262a9fa9459Szrj       // Read the list of (attribute, form) pairs.
263a9fa9459Szrj       Abbrev_code* entry = new Abbrev_code(tag, has_children);
264a9fa9459Szrj       for (;;)
265a9fa9459Szrj 	{
266a9fa9459Szrj 	  // Read the attribute.
267a9fa9459Szrj 	  if (this->buffer_pos_ >= this->buffer_end_)
268a9fa9459Szrj 	    return NULL;
269a9fa9459Szrj 	  uint64_t attr = read_unsigned_LEB_128(this->buffer_pos_, &len);
270a9fa9459Szrj 	  this->buffer_pos_ += len;
271a9fa9459Szrj 
272a9fa9459Szrj 	  // Read the form.
273a9fa9459Szrj 	  if (this->buffer_pos_ >= this->buffer_end_)
274a9fa9459Szrj 	    return NULL;
275a9fa9459Szrj 	  uint64_t form = read_unsigned_LEB_128(this->buffer_pos_, &len);
276a9fa9459Szrj 	  this->buffer_pos_ += len;
277a9fa9459Szrj 
278a9fa9459Szrj 	  // A (0,0) pair terminates the list.
279a9fa9459Szrj 	  if (attr == 0 && form == 0)
280a9fa9459Szrj 	    break;
281a9fa9459Szrj 
282a9fa9459Szrj 	  if (attr == elfcpp::DW_AT_sibling)
283a9fa9459Szrj 	    entry->has_sibling_attribute = true;
284a9fa9459Szrj 
285a9fa9459Szrj 	  entry->add_attribute(attr, form);
286a9fa9459Szrj 	}
287a9fa9459Szrj 
288a9fa9459Szrj       this->store_abbrev(nextcode, entry);
289a9fa9459Szrj       if (nextcode == code)
290a9fa9459Szrj 	return entry;
291a9fa9459Szrj     }
292a9fa9459Szrj 
293a9fa9459Szrj   return NULL;
294a9fa9459Szrj }
295a9fa9459Szrj 
296a9fa9459Szrj // class Dwarf_ranges_table
297a9fa9459Szrj 
298a9fa9459Szrj // Read the ranges table from an object file.
299a9fa9459Szrj 
300a9fa9459Szrj bool
read_ranges_table(Relobj * object,const unsigned char * symtab,off_t symtab_size,unsigned int ranges_shndx)301a9fa9459Szrj Dwarf_ranges_table::read_ranges_table(
302a9fa9459Szrj     Relobj* object,
303a9fa9459Szrj     const unsigned char* symtab,
304a9fa9459Szrj     off_t symtab_size,
305a9fa9459Szrj     unsigned int ranges_shndx)
306a9fa9459Szrj {
307a9fa9459Szrj   // If we've already read this abbrev table, return immediately.
308a9fa9459Szrj   if (this->ranges_shndx_ > 0
309a9fa9459Szrj       && this->ranges_shndx_ == ranges_shndx)
310a9fa9459Szrj     return true;
311a9fa9459Szrj 
312a9fa9459Szrj   // If we don't have relocations, ranges_shndx will be 0, and
313a9fa9459Szrj   // we'll have to hunt for the .debug_ranges section.
314a9fa9459Szrj   if (ranges_shndx == 0 && this->ranges_shndx_ > 0)
315a9fa9459Szrj     ranges_shndx = this->ranges_shndx_;
316a9fa9459Szrj   else if (ranges_shndx == 0)
317a9fa9459Szrj     {
318a9fa9459Szrj       for (unsigned int i = 1; i < object->shnum(); ++i)
319a9fa9459Szrj 	{
320a9fa9459Szrj 	  std::string name = object->section_name(i);
321a9fa9459Szrj 	  if (name == ".debug_ranges" || name == ".zdebug_ranges")
322a9fa9459Szrj 	    {
323a9fa9459Szrj 	      ranges_shndx = i;
324a9fa9459Szrj 	      this->output_section_offset_ = object->output_section_offset(i);
325a9fa9459Szrj 	      break;
326a9fa9459Szrj 	    }
327a9fa9459Szrj 	}
328a9fa9459Szrj       if (ranges_shndx == 0)
329a9fa9459Szrj 	return false;
330a9fa9459Szrj     }
331a9fa9459Szrj 
332a9fa9459Szrj   // Get the section contents and decompress if necessary.
333a9fa9459Szrj   if (ranges_shndx != this->ranges_shndx_)
334a9fa9459Szrj     {
335a9fa9459Szrj       if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
336a9fa9459Szrj         {
337a9fa9459Szrj 	  delete[] this->ranges_buffer_;
338a9fa9459Szrj 	  this->owns_ranges_buffer_ = false;
339a9fa9459Szrj         }
340a9fa9459Szrj 
341a9fa9459Szrj       section_size_type buffer_size;
342a9fa9459Szrj       this->ranges_buffer_ =
343a9fa9459Szrj 	  object->decompressed_section_contents(ranges_shndx,
344a9fa9459Szrj 						&buffer_size,
345a9fa9459Szrj 						&this->owns_ranges_buffer_);
346a9fa9459Szrj       this->ranges_buffer_end_ = this->ranges_buffer_ + buffer_size;
347a9fa9459Szrj       this->ranges_shndx_ = ranges_shndx;
348a9fa9459Szrj     }
349a9fa9459Szrj 
350a9fa9459Szrj   if (this->ranges_reloc_mapper_ != NULL)
351a9fa9459Szrj     {
352a9fa9459Szrj       delete this->ranges_reloc_mapper_;
353a9fa9459Szrj       this->ranges_reloc_mapper_ = NULL;
354a9fa9459Szrj     }
355a9fa9459Szrj 
356a9fa9459Szrj   // For incremental objects, we have no relocations.
357a9fa9459Szrj   if (object->is_incremental())
358a9fa9459Szrj     return true;
359a9fa9459Szrj 
360a9fa9459Szrj   // Find the relocation section for ".debug_ranges".
361a9fa9459Szrj   unsigned int reloc_shndx = 0;
362a9fa9459Szrj   unsigned int reloc_type = 0;
363a9fa9459Szrj   for (unsigned int i = 0; i < object->shnum(); ++i)
364a9fa9459Szrj     {
365a9fa9459Szrj       reloc_type = object->section_type(i);
366a9fa9459Szrj       if ((reloc_type == elfcpp::SHT_REL
367a9fa9459Szrj 	   || reloc_type == elfcpp::SHT_RELA)
368a9fa9459Szrj 	  && object->section_info(i) == ranges_shndx)
369a9fa9459Szrj 	{
370a9fa9459Szrj 	  reloc_shndx = i;
371a9fa9459Szrj 	  break;
372a9fa9459Szrj 	}
373a9fa9459Szrj     }
374a9fa9459Szrj 
375a9fa9459Szrj   this->ranges_reloc_mapper_ = make_elf_reloc_mapper(object, symtab,
376a9fa9459Szrj 						     symtab_size);
377a9fa9459Szrj   this->ranges_reloc_mapper_->initialize(reloc_shndx, reloc_type);
378a9fa9459Szrj   this->reloc_type_ = reloc_type;
379a9fa9459Szrj 
380a9fa9459Szrj   return true;
381a9fa9459Szrj }
382a9fa9459Szrj 
383a9fa9459Szrj // Read a range list from section RANGES_SHNDX at offset RANGES_OFFSET.
384a9fa9459Szrj 
385a9fa9459Szrj Dwarf_range_list*
read_range_list(Relobj * object,const unsigned char * symtab,off_t symtab_size,unsigned int addr_size,unsigned int ranges_shndx,off_t offset)386a9fa9459Szrj Dwarf_ranges_table::read_range_list(
387a9fa9459Szrj     Relobj* object,
388a9fa9459Szrj     const unsigned char* symtab,
389a9fa9459Szrj     off_t symtab_size,
390a9fa9459Szrj     unsigned int addr_size,
391a9fa9459Szrj     unsigned int ranges_shndx,
392a9fa9459Szrj     off_t offset)
393a9fa9459Szrj {
394a9fa9459Szrj   Dwarf_range_list* ranges;
395a9fa9459Szrj 
396a9fa9459Szrj   if (!this->read_ranges_table(object, symtab, symtab_size, ranges_shndx))
397a9fa9459Szrj     return NULL;
398a9fa9459Szrj 
399a9fa9459Szrj   // Correct the offset.  For incremental update links, we have a
400a9fa9459Szrj   // relocated offset that is relative to the output section, but
401a9fa9459Szrj   // here we need an offset relative to the input section.
402a9fa9459Szrj   offset -= this->output_section_offset_;
403a9fa9459Szrj 
404a9fa9459Szrj   // Read the range list at OFFSET.
405a9fa9459Szrj   ranges = new Dwarf_range_list();
406a9fa9459Szrj   off_t base = 0;
407a9fa9459Szrj   for (;
408a9fa9459Szrj        this->ranges_buffer_ + offset < this->ranges_buffer_end_;
409a9fa9459Szrj        offset += 2 * addr_size)
410a9fa9459Szrj     {
411a9fa9459Szrj       off_t start;
412a9fa9459Szrj       off_t end;
413a9fa9459Szrj 
414a9fa9459Szrj       // Read the raw contents of the section.
415a9fa9459Szrj       if (addr_size == 4)
416a9fa9459Szrj 	{
417a9fa9459Szrj 	  start = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
418a9fa9459Szrj 						       + offset);
419a9fa9459Szrj 	  end = this->dwinfo_->read_from_pointer<32>(this->ranges_buffer_
420a9fa9459Szrj 						     + offset + 4);
421a9fa9459Szrj 	}
422a9fa9459Szrj       else
423a9fa9459Szrj 	{
424a9fa9459Szrj 	  start = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
425a9fa9459Szrj 						       + offset);
426a9fa9459Szrj 	  end = this->dwinfo_->read_from_pointer<64>(this->ranges_buffer_
427a9fa9459Szrj 						     + offset + 8);
428a9fa9459Szrj 	}
429a9fa9459Szrj 
430a9fa9459Szrj       // Check for relocations and adjust the values.
431a9fa9459Szrj       unsigned int shndx1 = 0;
432a9fa9459Szrj       unsigned int shndx2 = 0;
433a9fa9459Szrj       if (this->ranges_reloc_mapper_ != NULL)
434a9fa9459Szrj         {
435a9fa9459Szrj 	  shndx1 = this->lookup_reloc(offset, &start);
436a9fa9459Szrj 	  shndx2 = this->lookup_reloc(offset + addr_size, &end);
437a9fa9459Szrj         }
438a9fa9459Szrj 
439a9fa9459Szrj       // End of list is marked by a pair of zeroes.
440a9fa9459Szrj       if (shndx1 == 0 && start == 0 && end == 0)
441a9fa9459Szrj         break;
442a9fa9459Szrj 
443a9fa9459Szrj       // A "base address selection entry" is identified by
444a9fa9459Szrj       // 0xffffffff for the first value of the pair.  The second
445a9fa9459Szrj       // value is used as a base for subsequent range list entries.
446a9fa9459Szrj       if (shndx1 == 0 && start == -1)
447a9fa9459Szrj 	base = end;
448a9fa9459Szrj       else if (shndx1 == shndx2)
449a9fa9459Szrj 	{
450a9fa9459Szrj 	  if (shndx1 == 0 || object->is_section_included(shndx1))
451a9fa9459Szrj 	    ranges->add(shndx1, base + start, base + end);
452a9fa9459Szrj 	}
453a9fa9459Szrj       else
454a9fa9459Szrj 	gold_warning(_("%s: DWARF info may be corrupt; offsets in a "
455a9fa9459Szrj 		       "range list entry are in different sections"),
456a9fa9459Szrj 		     object->name().c_str());
457a9fa9459Szrj     }
458a9fa9459Szrj 
459a9fa9459Szrj   return ranges;
460a9fa9459Szrj }
461a9fa9459Szrj 
462a9fa9459Szrj // Look for a relocation at offset OFF in the range table,
463a9fa9459Szrj // and return the section index and offset of the target.
464a9fa9459Szrj 
465a9fa9459Szrj unsigned int
lookup_reloc(off_t off,off_t * target_off)466a9fa9459Szrj Dwarf_ranges_table::lookup_reloc(off_t off, off_t* target_off)
467a9fa9459Szrj {
468a9fa9459Szrj   off_t value;
469a9fa9459Szrj   unsigned int shndx =
470a9fa9459Szrj       this->ranges_reloc_mapper_->get_reloc_target(off, &value);
471a9fa9459Szrj   if (shndx == 0)
472a9fa9459Szrj     return 0;
473a9fa9459Szrj   if (this->reloc_type_ == elfcpp::SHT_REL)
474a9fa9459Szrj     *target_off += value;
475a9fa9459Szrj   else
476a9fa9459Szrj     *target_off = value;
477a9fa9459Szrj   return shndx;
478a9fa9459Szrj }
479a9fa9459Szrj 
480a9fa9459Szrj // class Dwarf_pubnames_table
481a9fa9459Szrj 
482a9fa9459Szrj // Read the pubnames section from the object file.
483a9fa9459Szrj 
484a9fa9459Szrj bool
read_section(Relobj * object,const unsigned char * symtab,off_t symtab_size)485a9fa9459Szrj Dwarf_pubnames_table::read_section(Relobj* object, const unsigned char* symtab,
486a9fa9459Szrj                                    off_t symtab_size)
487a9fa9459Szrj {
488a9fa9459Szrj   section_size_type buffer_size;
489a9fa9459Szrj   unsigned int shndx = 0;
490a9fa9459Szrj   const char* name = this->is_pubtypes_ ? "pubtypes" : "pubnames";
491a9fa9459Szrj   const char* gnu_name = (this->is_pubtypes_
492a9fa9459Szrj 			  ? "gnu_pubtypes"
493a9fa9459Szrj 			  : "gnu_pubnames");
494a9fa9459Szrj 
495a9fa9459Szrj   for (unsigned int i = 1; i < object->shnum(); ++i)
496a9fa9459Szrj     {
497a9fa9459Szrj       std::string section_name = object->section_name(i);
498a9fa9459Szrj       const char* section_name_suffix = section_name.c_str();
499a9fa9459Szrj       if (is_prefix_of(".debug_", section_name_suffix))
500a9fa9459Szrj 	section_name_suffix += 7;
501a9fa9459Szrj       else if (is_prefix_of(".zdebug_", section_name_suffix))
502a9fa9459Szrj 	section_name_suffix += 8;
503a9fa9459Szrj       else
504a9fa9459Szrj 	continue;
505a9fa9459Szrj       if (strcmp(section_name_suffix, name) == 0)
506a9fa9459Szrj         {
507a9fa9459Szrj           shndx = i;
508a9fa9459Szrj           break;
509a9fa9459Szrj         }
510a9fa9459Szrj       else if (strcmp(section_name_suffix, gnu_name) == 0)
511a9fa9459Szrj         {
512a9fa9459Szrj           shndx = i;
513a9fa9459Szrj           this->is_gnu_style_ = true;
514a9fa9459Szrj           break;
515a9fa9459Szrj         }
516a9fa9459Szrj     }
517a9fa9459Szrj   if (shndx == 0)
518a9fa9459Szrj     return false;
519a9fa9459Szrj 
520a9fa9459Szrj   this->buffer_ = object->decompressed_section_contents(shndx,
521a9fa9459Szrj 							&buffer_size,
522a9fa9459Szrj 							&this->owns_buffer_);
523a9fa9459Szrj   if (this->buffer_ == NULL)
524a9fa9459Szrj     return false;
525a9fa9459Szrj   this->buffer_end_ = this->buffer_ + buffer_size;
526a9fa9459Szrj 
527a9fa9459Szrj   // For incremental objects, we have no relocations.
528a9fa9459Szrj   if (object->is_incremental())
529a9fa9459Szrj     return true;
530a9fa9459Szrj 
531a9fa9459Szrj   // Find the relocation section
532a9fa9459Szrj   unsigned int reloc_shndx = 0;
533a9fa9459Szrj   unsigned int reloc_type = 0;
534a9fa9459Szrj   for (unsigned int i = 0; i < object->shnum(); ++i)
535a9fa9459Szrj     {
536a9fa9459Szrj       reloc_type = object->section_type(i);
537a9fa9459Szrj       if ((reloc_type == elfcpp::SHT_REL
538a9fa9459Szrj 	   || reloc_type == elfcpp::SHT_RELA)
539a9fa9459Szrj 	  && object->section_info(i) == shndx)
540a9fa9459Szrj 	{
541a9fa9459Szrj 	  reloc_shndx = i;
542a9fa9459Szrj 	  break;
543a9fa9459Szrj 	}
544a9fa9459Szrj     }
545a9fa9459Szrj 
546a9fa9459Szrj   this->reloc_mapper_ = make_elf_reloc_mapper(object, symtab, symtab_size);
547a9fa9459Szrj   this->reloc_mapper_->initialize(reloc_shndx, reloc_type);
548a9fa9459Szrj   this->reloc_type_ = reloc_type;
549a9fa9459Szrj 
550a9fa9459Szrj   return true;
551a9fa9459Szrj }
552a9fa9459Szrj 
553a9fa9459Szrj // Read the header for the set at OFFSET.
554a9fa9459Szrj 
555a9fa9459Szrj bool
read_header(off_t offset)556a9fa9459Szrj Dwarf_pubnames_table::read_header(off_t offset)
557a9fa9459Szrj {
558a9fa9459Szrj   // Make sure we have actually read the section.
559a9fa9459Szrj   gold_assert(this->buffer_ != NULL);
560a9fa9459Szrj 
561a9fa9459Szrj   if (offset < 0 || offset + 14 >= this->buffer_end_ - this->buffer_)
562a9fa9459Szrj     return false;
563a9fa9459Szrj 
564a9fa9459Szrj   const unsigned char* pinfo = this->buffer_ + offset;
565a9fa9459Szrj 
566a9fa9459Szrj   // Read the unit_length field.
567a9fa9459Szrj   uint64_t unit_length = this->dwinfo_->read_from_pointer<32>(pinfo);
568a9fa9459Szrj   pinfo += 4;
569a9fa9459Szrj   if (unit_length == 0xffffffff)
570a9fa9459Szrj     {
571a9fa9459Szrj       unit_length = this->dwinfo_->read_from_pointer<64>(pinfo);
572a9fa9459Szrj       this->unit_length_ = unit_length + 12;
573a9fa9459Szrj       pinfo += 8;
574a9fa9459Szrj       this->offset_size_ = 8;
575a9fa9459Szrj     }
576a9fa9459Szrj   else
577a9fa9459Szrj     {
578a9fa9459Szrj       this->unit_length_ = unit_length + 4;
579a9fa9459Szrj       this->offset_size_ = 4;
580a9fa9459Szrj     }
581a9fa9459Szrj   this->end_of_table_ = pinfo + unit_length;
582a9fa9459Szrj 
583a9fa9459Szrj   // If unit_length is too big, maybe we should reject the whole table,
584a9fa9459Szrj   // but in cases we know about, it seems OK to assume that the table
585a9fa9459Szrj   // is valid through the actual end of the section.
586a9fa9459Szrj   if (this->end_of_table_ > this->buffer_end_)
587a9fa9459Szrj     this->end_of_table_ = this->buffer_end_;
588a9fa9459Szrj 
589a9fa9459Szrj   // Check the version.
590a9fa9459Szrj   unsigned int version = this->dwinfo_->read_from_pointer<16>(pinfo);
591a9fa9459Szrj   pinfo += 2;
592a9fa9459Szrj   if (version != 2)
593a9fa9459Szrj     return false;
594a9fa9459Szrj 
595a9fa9459Szrj   this->reloc_mapper_->get_reloc_target(pinfo - this->buffer_,
596a9fa9459Szrj                                         &this->cu_offset_);
597a9fa9459Szrj 
598a9fa9459Szrj   // Skip the debug_info_offset and debug_info_size fields.
599a9fa9459Szrj   pinfo += 2 * this->offset_size_;
600a9fa9459Szrj 
601a9fa9459Szrj   if (pinfo >= this->buffer_end_)
602a9fa9459Szrj     return false;
603a9fa9459Szrj 
604a9fa9459Szrj   this->pinfo_ = pinfo;
605a9fa9459Szrj   return true;
606a9fa9459Szrj }
607a9fa9459Szrj 
608a9fa9459Szrj // Read the next name from the set.
609a9fa9459Szrj 
610a9fa9459Szrj const char*
next_name(uint8_t * flag_byte)611a9fa9459Szrj Dwarf_pubnames_table::next_name(uint8_t* flag_byte)
612a9fa9459Szrj {
613a9fa9459Szrj   const unsigned char* pinfo = this->pinfo_;
614a9fa9459Szrj 
615a9fa9459Szrj   // Check for end of list.  The table should be terminated by an
616a9fa9459Szrj   // entry containing nothing but a DIE offset of 0.
617a9fa9459Szrj   if (pinfo + this->offset_size_ >= this->end_of_table_)
618a9fa9459Szrj     return NULL;
619a9fa9459Szrj 
620a9fa9459Szrj   // Skip the offset within the CU.  If this is zero, but we're not
621a9fa9459Szrj   // at the end of the table, then we have a real pubnames entry
622a9fa9459Szrj   // whose DIE offset is 0 (likely to be a GCC bug).  Since we
623a9fa9459Szrj   // don't actually use the DIE offset in building .gdb_index,
624a9fa9459Szrj   // it's harmless.
625a9fa9459Szrj   pinfo += this->offset_size_;
626a9fa9459Szrj 
627a9fa9459Szrj   if (this->is_gnu_style_)
628a9fa9459Szrj     *flag_byte = *pinfo++;
629a9fa9459Szrj   else
630a9fa9459Szrj     *flag_byte = 0;
631a9fa9459Szrj 
632a9fa9459Szrj   // Return a pointer to the string at the current location,
633a9fa9459Szrj   // and advance the pointer to the next entry.
634a9fa9459Szrj   const char* ret = reinterpret_cast<const char*>(pinfo);
635a9fa9459Szrj   while (pinfo < this->buffer_end_ && *pinfo != '\0')
636a9fa9459Szrj     ++pinfo;
637a9fa9459Szrj   if (pinfo < this->buffer_end_)
638a9fa9459Szrj     ++pinfo;
639a9fa9459Szrj 
640a9fa9459Szrj   this->pinfo_ = pinfo;
641a9fa9459Szrj   return ret;
642a9fa9459Szrj }
643a9fa9459Szrj 
644a9fa9459Szrj // class Dwarf_die
645a9fa9459Szrj 
Dwarf_die(Dwarf_info_reader * dwinfo,off_t die_offset,Dwarf_die * parent)646a9fa9459Szrj Dwarf_die::Dwarf_die(
647a9fa9459Szrj     Dwarf_info_reader* dwinfo,
648a9fa9459Szrj     off_t die_offset,
649a9fa9459Szrj     Dwarf_die* parent)
650a9fa9459Szrj   : dwinfo_(dwinfo), parent_(parent), die_offset_(die_offset),
651a9fa9459Szrj     child_offset_(0), sibling_offset_(0), abbrev_code_(NULL), attributes_(),
652a9fa9459Szrj     attributes_read_(false), name_(NULL), name_off_(-1), linkage_name_(NULL),
653a9fa9459Szrj     linkage_name_off_(-1), string_shndx_(0), specification_(0),
654a9fa9459Szrj     abstract_origin_(0)
655a9fa9459Szrj {
656a9fa9459Szrj   size_t len;
657a9fa9459Szrj   const unsigned char* pdie = dwinfo->buffer_at_offset(die_offset);
658a9fa9459Szrj   if (pdie == NULL)
659a9fa9459Szrj     return;
660a9fa9459Szrj   unsigned int code = read_unsigned_LEB_128(pdie, &len);
661a9fa9459Szrj   if (code == 0)
662a9fa9459Szrj     {
663a9fa9459Szrj       if (parent != NULL)
664a9fa9459Szrj 	parent->set_sibling_offset(die_offset + len);
665a9fa9459Szrj       return;
666a9fa9459Szrj     }
667a9fa9459Szrj   this->attr_offset_ = len;
668a9fa9459Szrj 
669a9fa9459Szrj   // Lookup the abbrev code in the abbrev table.
670a9fa9459Szrj   this->abbrev_code_ = dwinfo->get_abbrev(code);
671a9fa9459Szrj }
672a9fa9459Szrj 
673a9fa9459Szrj // Read all the attributes of the DIE.
674a9fa9459Szrj 
675a9fa9459Szrj bool
read_attributes()676a9fa9459Szrj Dwarf_die::read_attributes()
677a9fa9459Szrj {
678a9fa9459Szrj   if (this->attributes_read_)
679a9fa9459Szrj     return true;
680a9fa9459Szrj 
681a9fa9459Szrj   gold_assert(this->abbrev_code_ != NULL);
682a9fa9459Szrj 
683a9fa9459Szrj   const unsigned char* pdie =
684a9fa9459Szrj       this->dwinfo_->buffer_at_offset(this->die_offset_);
685a9fa9459Szrj   if (pdie == NULL)
686a9fa9459Szrj     return false;
687a9fa9459Szrj   const unsigned char* pattr = pdie + this->attr_offset_;
688a9fa9459Szrj 
689a9fa9459Szrj   unsigned int nattr = this->abbrev_code_->attributes.size();
690a9fa9459Szrj   this->attributes_.reserve(nattr);
691a9fa9459Szrj   for (unsigned int i = 0; i < nattr; ++i)
692a9fa9459Szrj     {
693a9fa9459Szrj       size_t len;
694a9fa9459Szrj       unsigned int attr = this->abbrev_code_->attributes[i].attr;
695a9fa9459Szrj       unsigned int form = this->abbrev_code_->attributes[i].form;
696a9fa9459Szrj       if (form == elfcpp::DW_FORM_indirect)
697a9fa9459Szrj         {
698a9fa9459Szrj           form = read_unsigned_LEB_128(pattr, &len);
699a9fa9459Szrj           pattr += len;
700a9fa9459Szrj         }
701a9fa9459Szrj       off_t attr_off = this->die_offset_ + (pattr - pdie);
702a9fa9459Szrj       bool ref_form = false;
703a9fa9459Szrj       Attribute_value attr_value;
704a9fa9459Szrj       attr_value.attr = attr;
705a9fa9459Szrj       attr_value.form = form;
706a9fa9459Szrj       attr_value.aux.shndx = 0;
707a9fa9459Szrj       switch(form)
708a9fa9459Szrj 	{
709a9fa9459Szrj 	  case elfcpp::DW_FORM_flag_present:
710a9fa9459Szrj 	    attr_value.val.intval = 1;
711a9fa9459Szrj 	    break;
712a9fa9459Szrj 	  case elfcpp::DW_FORM_strp:
713a9fa9459Szrj 	    {
714a9fa9459Szrj 	      off_t str_off;
715a9fa9459Szrj 	      if (this->dwinfo_->offset_size() == 4)
716a9fa9459Szrj 		str_off = this->dwinfo_->read_from_pointer<32>(&pattr);
717a9fa9459Szrj 	      else
718a9fa9459Szrj 		str_off = this->dwinfo_->read_from_pointer<64>(&pattr);
719a9fa9459Szrj 	      unsigned int shndx =
720a9fa9459Szrj 		  this->dwinfo_->lookup_reloc(attr_off, &str_off);
721a9fa9459Szrj 	      attr_value.aux.shndx = shndx;
722a9fa9459Szrj 	      attr_value.val.refval = str_off;
723a9fa9459Szrj 	      break;
724a9fa9459Szrj 	    }
725a9fa9459Szrj 	  case elfcpp::DW_FORM_sec_offset:
726a9fa9459Szrj 	    {
727a9fa9459Szrj 	      off_t sec_off;
728a9fa9459Szrj 	      if (this->dwinfo_->offset_size() == 4)
729a9fa9459Szrj 		sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
730a9fa9459Szrj 	      else
731a9fa9459Szrj 		sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
732a9fa9459Szrj 	      unsigned int shndx =
733a9fa9459Szrj 		  this->dwinfo_->lookup_reloc(attr_off, &sec_off);
734a9fa9459Szrj 	      attr_value.aux.shndx = shndx;
735a9fa9459Szrj 	      attr_value.val.refval = sec_off;
736a9fa9459Szrj 	      ref_form = true;
737a9fa9459Szrj 	      break;
738a9fa9459Szrj 	    }
739a9fa9459Szrj 	  case elfcpp::DW_FORM_addr:
740a9fa9459Szrj 	  case elfcpp::DW_FORM_ref_addr:
741a9fa9459Szrj 	    {
742a9fa9459Szrj 	      off_t sec_off;
743a9fa9459Szrj 	      if (this->dwinfo_->address_size() == 4)
744a9fa9459Szrj 		sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
745a9fa9459Szrj 	      else
746a9fa9459Szrj 		sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
747a9fa9459Szrj 	      unsigned int shndx =
748a9fa9459Szrj 		  this->dwinfo_->lookup_reloc(attr_off, &sec_off);
749a9fa9459Szrj 	      attr_value.aux.shndx = shndx;
750a9fa9459Szrj 	      attr_value.val.refval = sec_off;
751a9fa9459Szrj 	      ref_form = true;
752a9fa9459Szrj 	      break;
753a9fa9459Szrj 	    }
754a9fa9459Szrj 	  case elfcpp::DW_FORM_block1:
755a9fa9459Szrj 	    attr_value.aux.blocklen = *pattr++;
756a9fa9459Szrj 	    attr_value.val.blockval = pattr;
757a9fa9459Szrj 	    pattr += attr_value.aux.blocklen;
758a9fa9459Szrj 	    break;
759a9fa9459Szrj 	  case elfcpp::DW_FORM_block2:
760a9fa9459Szrj 	    attr_value.aux.blocklen =
761a9fa9459Szrj 		this->dwinfo_->read_from_pointer<16>(&pattr);
762a9fa9459Szrj 	    attr_value.val.blockval = pattr;
763a9fa9459Szrj 	    pattr += attr_value.aux.blocklen;
764a9fa9459Szrj 	    break;
765a9fa9459Szrj 	  case elfcpp::DW_FORM_block4:
766a9fa9459Szrj 	    attr_value.aux.blocklen =
767a9fa9459Szrj 		this->dwinfo_->read_from_pointer<32>(&pattr);
768a9fa9459Szrj 	    attr_value.val.blockval = pattr;
769a9fa9459Szrj 	    pattr += attr_value.aux.blocklen;
770a9fa9459Szrj 	    break;
771a9fa9459Szrj 	  case elfcpp::DW_FORM_block:
772a9fa9459Szrj 	  case elfcpp::DW_FORM_exprloc:
773a9fa9459Szrj 	    attr_value.aux.blocklen = read_unsigned_LEB_128(pattr, &len);
774a9fa9459Szrj 	    attr_value.val.blockval = pattr + len;
775a9fa9459Szrj 	    pattr += len + attr_value.aux.blocklen;
776a9fa9459Szrj 	    break;
777a9fa9459Szrj 	  case elfcpp::DW_FORM_data1:
778a9fa9459Szrj 	  case elfcpp::DW_FORM_flag:
779a9fa9459Szrj 	    attr_value.val.intval = *pattr++;
780a9fa9459Szrj 	    break;
781a9fa9459Szrj 	  case elfcpp::DW_FORM_ref1:
782a9fa9459Szrj 	    attr_value.val.refval = *pattr++;
783a9fa9459Szrj 	    ref_form = true;
784a9fa9459Szrj 	    break;
785a9fa9459Szrj 	  case elfcpp::DW_FORM_data2:
786a9fa9459Szrj 	    attr_value.val.intval =
787a9fa9459Szrj 		this->dwinfo_->read_from_pointer<16>(&pattr);
788a9fa9459Szrj 	    break;
789a9fa9459Szrj 	  case elfcpp::DW_FORM_ref2:
790a9fa9459Szrj 	    attr_value.val.refval =
791a9fa9459Szrj 		this->dwinfo_->read_from_pointer<16>(&pattr);
792a9fa9459Szrj 	    ref_form = true;
793a9fa9459Szrj 	    break;
794a9fa9459Szrj 	  case elfcpp::DW_FORM_data4:
795a9fa9459Szrj 	    {
796a9fa9459Szrj 	      off_t sec_off;
797a9fa9459Szrj 	      sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
798a9fa9459Szrj 	      unsigned int shndx =
799a9fa9459Szrj 		  this->dwinfo_->lookup_reloc(attr_off, &sec_off);
800a9fa9459Szrj 	      attr_value.aux.shndx = shndx;
801a9fa9459Szrj 	      attr_value.val.intval = sec_off;
802a9fa9459Szrj 	      break;
803a9fa9459Szrj 	    }
804a9fa9459Szrj 	  case elfcpp::DW_FORM_ref4:
805a9fa9459Szrj 	    {
806a9fa9459Szrj 	      off_t sec_off;
807a9fa9459Szrj 	      sec_off = this->dwinfo_->read_from_pointer<32>(&pattr);
808a9fa9459Szrj 	      unsigned int shndx =
809a9fa9459Szrj 		  this->dwinfo_->lookup_reloc(attr_off, &sec_off);
810a9fa9459Szrj 	      attr_value.aux.shndx = shndx;
811a9fa9459Szrj 	      attr_value.val.refval = sec_off;
812a9fa9459Szrj 	      ref_form = true;
813a9fa9459Szrj 	      break;
814a9fa9459Szrj 	    }
815a9fa9459Szrj 	  case elfcpp::DW_FORM_data8:
816a9fa9459Szrj 	    {
817a9fa9459Szrj 	      off_t sec_off;
818a9fa9459Szrj 	      sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
819a9fa9459Szrj 	      unsigned int shndx =
820a9fa9459Szrj 		  this->dwinfo_->lookup_reloc(attr_off, &sec_off);
821a9fa9459Szrj 	      attr_value.aux.shndx = shndx;
822a9fa9459Szrj 	      attr_value.val.intval = sec_off;
823a9fa9459Szrj 	      break;
824a9fa9459Szrj 	    }
825a9fa9459Szrj 	  case elfcpp::DW_FORM_ref_sig8:
826a9fa9459Szrj 	    attr_value.val.uintval =
827a9fa9459Szrj 		this->dwinfo_->read_from_pointer<64>(&pattr);
828a9fa9459Szrj 	    break;
829a9fa9459Szrj 	  case elfcpp::DW_FORM_ref8:
830a9fa9459Szrj 	    {
831a9fa9459Szrj 	      off_t sec_off;
832a9fa9459Szrj 	      sec_off = this->dwinfo_->read_from_pointer<64>(&pattr);
833a9fa9459Szrj 	      unsigned int shndx =
834a9fa9459Szrj 		  this->dwinfo_->lookup_reloc(attr_off, &sec_off);
835a9fa9459Szrj 	      attr_value.aux.shndx = shndx;
836a9fa9459Szrj 	      attr_value.val.refval = sec_off;
837a9fa9459Szrj 	      ref_form = true;
838a9fa9459Szrj 	      break;
839a9fa9459Szrj 	    }
840a9fa9459Szrj 	  case elfcpp::DW_FORM_ref_udata:
841a9fa9459Szrj 	    attr_value.val.refval = read_unsigned_LEB_128(pattr, &len);
842a9fa9459Szrj 	    ref_form = true;
843a9fa9459Szrj 	    pattr += len;
844a9fa9459Szrj 	    break;
845a9fa9459Szrj 	  case elfcpp::DW_FORM_udata:
846a9fa9459Szrj 	  case elfcpp::DW_FORM_GNU_addr_index:
847a9fa9459Szrj 	  case elfcpp::DW_FORM_GNU_str_index:
848a9fa9459Szrj 	    attr_value.val.uintval = read_unsigned_LEB_128(pattr, &len);
849a9fa9459Szrj 	    pattr += len;
850a9fa9459Szrj 	    break;
851a9fa9459Szrj 	  case elfcpp::DW_FORM_sdata:
852a9fa9459Szrj 	    attr_value.val.intval = read_signed_LEB_128(pattr, &len);
853a9fa9459Szrj 	    pattr += len;
854a9fa9459Szrj 	    break;
855a9fa9459Szrj 	  case elfcpp::DW_FORM_string:
856a9fa9459Szrj 	    attr_value.val.stringval = reinterpret_cast<const char*>(pattr);
857a9fa9459Szrj 	    len = strlen(attr_value.val.stringval);
858a9fa9459Szrj 	    pattr += len + 1;
859a9fa9459Szrj 	    break;
860a9fa9459Szrj 	  default:
861a9fa9459Szrj 	    return false;
862a9fa9459Szrj 	}
863a9fa9459Szrj 
864a9fa9459Szrj       // Cache the most frequently-requested attributes.
865a9fa9459Szrj       switch (attr)
866a9fa9459Szrj 	{
867a9fa9459Szrj 	  case elfcpp::DW_AT_name:
868a9fa9459Szrj 	    if (form == elfcpp::DW_FORM_string)
869a9fa9459Szrj 	      this->name_ = attr_value.val.stringval;
870a9fa9459Szrj 	    else if (form == elfcpp::DW_FORM_strp)
871a9fa9459Szrj 	      {
872a9fa9459Szrj 		// All indirect strings should refer to the same
873a9fa9459Szrj 		// string section, so we just save the last one seen.
874a9fa9459Szrj 		this->string_shndx_ = attr_value.aux.shndx;
875a9fa9459Szrj 		this->name_off_ = attr_value.val.refval;
876a9fa9459Szrj 	      }
877a9fa9459Szrj 	    break;
878a9fa9459Szrj 	  case elfcpp::DW_AT_linkage_name:
879a9fa9459Szrj 	  case elfcpp::DW_AT_MIPS_linkage_name:
880a9fa9459Szrj 	    if (form == elfcpp::DW_FORM_string)
881a9fa9459Szrj 	      this->linkage_name_ = attr_value.val.stringval;
882a9fa9459Szrj 	    else if (form == elfcpp::DW_FORM_strp)
883a9fa9459Szrj 	      {
884a9fa9459Szrj 		// All indirect strings should refer to the same
885a9fa9459Szrj 		// string section, so we just save the last one seen.
886a9fa9459Szrj 		this->string_shndx_ = attr_value.aux.shndx;
887a9fa9459Szrj 		this->linkage_name_off_ = attr_value.val.refval;
888a9fa9459Szrj 	      }
889a9fa9459Szrj 	    break;
890a9fa9459Szrj 	  case elfcpp::DW_AT_specification:
891a9fa9459Szrj 	    if (ref_form)
892a9fa9459Szrj 	      this->specification_ = attr_value.val.refval;
893a9fa9459Szrj 	    break;
894a9fa9459Szrj 	  case elfcpp::DW_AT_abstract_origin:
895a9fa9459Szrj 	    if (ref_form)
896a9fa9459Szrj 	      this->abstract_origin_ = attr_value.val.refval;
897a9fa9459Szrj 	    break;
898a9fa9459Szrj 	  case elfcpp::DW_AT_sibling:
899a9fa9459Szrj 	    if (ref_form && attr_value.aux.shndx == 0)
900a9fa9459Szrj 	      this->sibling_offset_ = attr_value.val.refval;
901a9fa9459Szrj 	  default:
902a9fa9459Szrj 	    break;
903a9fa9459Szrj 	}
904a9fa9459Szrj 
905a9fa9459Szrj       this->attributes_.push_back(attr_value);
906a9fa9459Szrj     }
907a9fa9459Szrj 
908a9fa9459Szrj   // Now that we know where the next DIE begins, record the offset
909a9fa9459Szrj   // to avoid later recalculation.
910a9fa9459Szrj   if (this->has_children())
911a9fa9459Szrj     this->child_offset_ = this->die_offset_ + (pattr - pdie);
912a9fa9459Szrj   else
913a9fa9459Szrj     this->sibling_offset_ = this->die_offset_ + (pattr - pdie);
914a9fa9459Szrj 
915a9fa9459Szrj   this->attributes_read_ = true;
916a9fa9459Szrj   return true;
917a9fa9459Szrj }
918a9fa9459Szrj 
919a9fa9459Szrj // Skip all the attributes of the DIE and return the offset of the next DIE.
920a9fa9459Szrj 
921a9fa9459Szrj off_t
skip_attributes()922a9fa9459Szrj Dwarf_die::skip_attributes()
923a9fa9459Szrj {
924a9fa9459Szrj   gold_assert(this->abbrev_code_ != NULL);
925a9fa9459Szrj 
926a9fa9459Szrj   const unsigned char* pdie =
927a9fa9459Szrj       this->dwinfo_->buffer_at_offset(this->die_offset_);
928a9fa9459Szrj   if (pdie == NULL)
929a9fa9459Szrj     return 0;
930a9fa9459Szrj   const unsigned char* pattr = pdie + this->attr_offset_;
931a9fa9459Szrj 
932a9fa9459Szrj   for (unsigned int i = 0; i < this->abbrev_code_->attributes.size(); ++i)
933a9fa9459Szrj     {
934a9fa9459Szrj       size_t len;
935a9fa9459Szrj       unsigned int form = this->abbrev_code_->attributes[i].form;
936a9fa9459Szrj       if (form == elfcpp::DW_FORM_indirect)
937a9fa9459Szrj         {
938a9fa9459Szrj           form = read_unsigned_LEB_128(pattr, &len);
939a9fa9459Szrj           pattr += len;
940a9fa9459Szrj         }
941a9fa9459Szrj       switch(form)
942a9fa9459Szrj 	{
943a9fa9459Szrj 	  case elfcpp::DW_FORM_flag_present:
944a9fa9459Szrj 	    break;
945a9fa9459Szrj 	  case elfcpp::DW_FORM_strp:
946a9fa9459Szrj 	  case elfcpp::DW_FORM_sec_offset:
947a9fa9459Szrj 	    pattr += this->dwinfo_->offset_size();
948a9fa9459Szrj 	    break;
949a9fa9459Szrj 	  case elfcpp::DW_FORM_addr:
950a9fa9459Szrj 	  case elfcpp::DW_FORM_ref_addr:
951a9fa9459Szrj 	    pattr += this->dwinfo_->address_size();
952a9fa9459Szrj 	    break;
953a9fa9459Szrj 	  case elfcpp::DW_FORM_block1:
954a9fa9459Szrj 	    pattr += 1 + *pattr;
955a9fa9459Szrj 	    break;
956a9fa9459Szrj 	  case elfcpp::DW_FORM_block2:
957a9fa9459Szrj 	    {
958a9fa9459Szrj 	      uint16_t block_size;
959a9fa9459Szrj 	      block_size = this->dwinfo_->read_from_pointer<16>(&pattr);
960a9fa9459Szrj 	      pattr += block_size;
961a9fa9459Szrj 	      break;
962a9fa9459Szrj 	    }
963a9fa9459Szrj 	  case elfcpp::DW_FORM_block4:
964a9fa9459Szrj 	    {
965a9fa9459Szrj 	      uint32_t block_size;
966a9fa9459Szrj 	      block_size = this->dwinfo_->read_from_pointer<32>(&pattr);
967a9fa9459Szrj 	      pattr += block_size;
968a9fa9459Szrj 	      break;
969a9fa9459Szrj 	    }
970a9fa9459Szrj 	  case elfcpp::DW_FORM_block:
971a9fa9459Szrj 	  case elfcpp::DW_FORM_exprloc:
972a9fa9459Szrj 	    {
973a9fa9459Szrj 	      uint64_t block_size;
974a9fa9459Szrj 	      block_size = read_unsigned_LEB_128(pattr, &len);
975a9fa9459Szrj 	      pattr += len + block_size;
976a9fa9459Szrj 	      break;
977a9fa9459Szrj 	    }
978a9fa9459Szrj 	  case elfcpp::DW_FORM_data1:
979a9fa9459Szrj 	  case elfcpp::DW_FORM_ref1:
980a9fa9459Szrj 	  case elfcpp::DW_FORM_flag:
981a9fa9459Szrj 	    pattr += 1;
982a9fa9459Szrj 	    break;
983a9fa9459Szrj 	  case elfcpp::DW_FORM_data2:
984a9fa9459Szrj 	  case elfcpp::DW_FORM_ref2:
985a9fa9459Szrj 	    pattr += 2;
986a9fa9459Szrj 	    break;
987a9fa9459Szrj 	  case elfcpp::DW_FORM_data4:
988a9fa9459Szrj 	  case elfcpp::DW_FORM_ref4:
989a9fa9459Szrj 	    pattr += 4;
990a9fa9459Szrj 	    break;
991a9fa9459Szrj 	  case elfcpp::DW_FORM_data8:
992a9fa9459Szrj 	  case elfcpp::DW_FORM_ref8:
993a9fa9459Szrj 	  case elfcpp::DW_FORM_ref_sig8:
994a9fa9459Szrj 	    pattr += 8;
995a9fa9459Szrj 	    break;
996a9fa9459Szrj 	  case elfcpp::DW_FORM_ref_udata:
997a9fa9459Szrj 	  case elfcpp::DW_FORM_udata:
998a9fa9459Szrj 	  case elfcpp::DW_FORM_GNU_addr_index:
999a9fa9459Szrj 	  case elfcpp::DW_FORM_GNU_str_index:
1000a9fa9459Szrj 	    read_unsigned_LEB_128(pattr, &len);
1001a9fa9459Szrj 	    pattr += len;
1002a9fa9459Szrj 	    break;
1003a9fa9459Szrj 	  case elfcpp::DW_FORM_sdata:
1004a9fa9459Szrj 	    read_signed_LEB_128(pattr, &len);
1005a9fa9459Szrj 	    pattr += len;
1006a9fa9459Szrj 	    break;
1007a9fa9459Szrj 	  case elfcpp::DW_FORM_string:
1008a9fa9459Szrj 	    len = strlen(reinterpret_cast<const char*>(pattr));
1009a9fa9459Szrj 	    pattr += len + 1;
1010a9fa9459Szrj 	    break;
1011a9fa9459Szrj 	  default:
1012a9fa9459Szrj 	    return 0;
1013a9fa9459Szrj 	}
1014a9fa9459Szrj     }
1015a9fa9459Szrj 
1016a9fa9459Szrj   return this->die_offset_ + (pattr - pdie);
1017a9fa9459Szrj }
1018a9fa9459Szrj 
1019a9fa9459Szrj // Get the name of the DIE and cache it.
1020a9fa9459Szrj 
1021a9fa9459Szrj void
set_name()1022a9fa9459Szrj Dwarf_die::set_name()
1023a9fa9459Szrj {
1024a9fa9459Szrj   if (this->name_ != NULL || !this->read_attributes())
1025a9fa9459Szrj     return;
1026a9fa9459Szrj   if (this->name_off_ != -1)
1027a9fa9459Szrj     this->name_ = this->dwinfo_->get_string(this->name_off_,
1028a9fa9459Szrj 					    this->string_shndx_);
1029a9fa9459Szrj }
1030a9fa9459Szrj 
1031a9fa9459Szrj // Get the linkage name of the DIE and cache it.
1032a9fa9459Szrj 
1033a9fa9459Szrj void
set_linkage_name()1034a9fa9459Szrj Dwarf_die::set_linkage_name()
1035a9fa9459Szrj {
1036a9fa9459Szrj   if (this->linkage_name_ != NULL || !this->read_attributes())
1037a9fa9459Szrj     return;
1038a9fa9459Szrj   if (this->linkage_name_off_ != -1)
1039a9fa9459Szrj     this->linkage_name_ = this->dwinfo_->get_string(this->linkage_name_off_,
1040a9fa9459Szrj 						    this->string_shndx_);
1041a9fa9459Szrj }
1042a9fa9459Szrj 
1043a9fa9459Szrj // Return the value of attribute ATTR.
1044a9fa9459Szrj 
1045a9fa9459Szrj const Dwarf_die::Attribute_value*
attribute(unsigned int attr)1046a9fa9459Szrj Dwarf_die::attribute(unsigned int attr)
1047a9fa9459Szrj {
1048a9fa9459Szrj   if (!this->read_attributes())
1049a9fa9459Szrj     return NULL;
1050a9fa9459Szrj   for (unsigned int i = 0; i < this->attributes_.size(); ++i)
1051a9fa9459Szrj     {
1052a9fa9459Szrj       if (this->attributes_[i].attr == attr)
1053a9fa9459Szrj         return &this->attributes_[i];
1054a9fa9459Szrj     }
1055a9fa9459Szrj   return NULL;
1056a9fa9459Szrj }
1057a9fa9459Szrj 
1058a9fa9459Szrj const char*
string_attribute(unsigned int attr)1059a9fa9459Szrj Dwarf_die::string_attribute(unsigned int attr)
1060a9fa9459Szrj {
1061a9fa9459Szrj   const Attribute_value* attr_val = this->attribute(attr);
1062a9fa9459Szrj   if (attr_val == NULL)
1063a9fa9459Szrj     return NULL;
1064a9fa9459Szrj   switch (attr_val->form)
1065a9fa9459Szrj     {
1066a9fa9459Szrj       case elfcpp::DW_FORM_string:
1067a9fa9459Szrj         return attr_val->val.stringval;
1068a9fa9459Szrj       case elfcpp::DW_FORM_strp:
1069a9fa9459Szrj 	return this->dwinfo_->get_string(attr_val->val.refval,
1070a9fa9459Szrj 					 attr_val->aux.shndx);
1071a9fa9459Szrj       default:
1072a9fa9459Szrj         return NULL;
1073a9fa9459Szrj     }
1074a9fa9459Szrj }
1075a9fa9459Szrj 
1076a9fa9459Szrj int64_t
int_attribute(unsigned int attr)1077a9fa9459Szrj Dwarf_die::int_attribute(unsigned int attr)
1078a9fa9459Szrj {
1079a9fa9459Szrj   const Attribute_value* attr_val = this->attribute(attr);
1080a9fa9459Szrj   if (attr_val == NULL)
1081a9fa9459Szrj     return 0;
1082a9fa9459Szrj   switch (attr_val->form)
1083a9fa9459Szrj     {
1084a9fa9459Szrj       case elfcpp::DW_FORM_flag_present:
1085a9fa9459Szrj       case elfcpp::DW_FORM_data1:
1086a9fa9459Szrj       case elfcpp::DW_FORM_flag:
1087a9fa9459Szrj       case elfcpp::DW_FORM_data2:
1088a9fa9459Szrj       case elfcpp::DW_FORM_data4:
1089a9fa9459Szrj       case elfcpp::DW_FORM_data8:
1090a9fa9459Szrj       case elfcpp::DW_FORM_sdata:
1091a9fa9459Szrj         return attr_val->val.intval;
1092a9fa9459Szrj       default:
1093a9fa9459Szrj         return 0;
1094a9fa9459Szrj     }
1095a9fa9459Szrj }
1096a9fa9459Szrj 
1097a9fa9459Szrj uint64_t
uint_attribute(unsigned int attr)1098a9fa9459Szrj Dwarf_die::uint_attribute(unsigned int attr)
1099a9fa9459Szrj {
1100a9fa9459Szrj   const Attribute_value* attr_val = this->attribute(attr);
1101a9fa9459Szrj   if (attr_val == NULL)
1102a9fa9459Szrj     return 0;
1103a9fa9459Szrj   switch (attr_val->form)
1104a9fa9459Szrj     {
1105a9fa9459Szrj       case elfcpp::DW_FORM_flag_present:
1106a9fa9459Szrj       case elfcpp::DW_FORM_data1:
1107a9fa9459Szrj       case elfcpp::DW_FORM_flag:
1108a9fa9459Szrj       case elfcpp::DW_FORM_data4:
1109a9fa9459Szrj       case elfcpp::DW_FORM_data8:
1110a9fa9459Szrj       case elfcpp::DW_FORM_ref_sig8:
1111a9fa9459Szrj       case elfcpp::DW_FORM_udata:
1112a9fa9459Szrj         return attr_val->val.uintval;
1113a9fa9459Szrj       default:
1114a9fa9459Szrj         return 0;
1115a9fa9459Szrj     }
1116a9fa9459Szrj }
1117a9fa9459Szrj 
1118a9fa9459Szrj off_t
ref_attribute(unsigned int attr,unsigned int * shndx)1119a9fa9459Szrj Dwarf_die::ref_attribute(unsigned int attr, unsigned int* shndx)
1120a9fa9459Szrj {
1121a9fa9459Szrj   const Attribute_value* attr_val = this->attribute(attr);
1122a9fa9459Szrj   if (attr_val == NULL)
1123a9fa9459Szrj     return -1;
1124a9fa9459Szrj   switch (attr_val->form)
1125a9fa9459Szrj     {
1126a9fa9459Szrj       case elfcpp::DW_FORM_sec_offset:
1127a9fa9459Szrj       case elfcpp::DW_FORM_addr:
1128a9fa9459Szrj       case elfcpp::DW_FORM_ref_addr:
1129a9fa9459Szrj       case elfcpp::DW_FORM_ref1:
1130a9fa9459Szrj       case elfcpp::DW_FORM_ref2:
1131a9fa9459Szrj       case elfcpp::DW_FORM_ref4:
1132a9fa9459Szrj       case elfcpp::DW_FORM_ref8:
1133a9fa9459Szrj       case elfcpp::DW_FORM_ref_udata:
1134a9fa9459Szrj         *shndx = attr_val->aux.shndx;
1135a9fa9459Szrj         return attr_val->val.refval;
1136a9fa9459Szrj       case elfcpp::DW_FORM_ref_sig8:
1137a9fa9459Szrj         *shndx = attr_val->aux.shndx;
1138a9fa9459Szrj         return attr_val->val.uintval;
1139a9fa9459Szrj       case elfcpp::DW_FORM_data4:
1140a9fa9459Szrj       case elfcpp::DW_FORM_data8:
1141a9fa9459Szrj         *shndx = attr_val->aux.shndx;
1142a9fa9459Szrj         return attr_val->val.intval;
1143a9fa9459Szrj       default:
1144a9fa9459Szrj         return -1;
1145a9fa9459Szrj     }
1146a9fa9459Szrj }
1147a9fa9459Szrj 
1148a9fa9459Szrj off_t
address_attribute(unsigned int attr,unsigned int * shndx)1149a9fa9459Szrj Dwarf_die::address_attribute(unsigned int attr, unsigned int* shndx)
1150a9fa9459Szrj {
1151a9fa9459Szrj   const Attribute_value* attr_val = this->attribute(attr);
1152a9fa9459Szrj   if (attr_val == NULL || attr_val->form != elfcpp::DW_FORM_addr)
1153a9fa9459Szrj     return -1;
1154a9fa9459Szrj 
1155a9fa9459Szrj   *shndx = attr_val->aux.shndx;
1156a9fa9459Szrj   return attr_val->val.refval;
1157a9fa9459Szrj }
1158a9fa9459Szrj 
1159a9fa9459Szrj // Return the offset of this DIE's first child.
1160a9fa9459Szrj 
1161a9fa9459Szrj off_t
child_offset()1162a9fa9459Szrj Dwarf_die::child_offset()
1163a9fa9459Szrj {
1164a9fa9459Szrj   gold_assert(this->abbrev_code_ != NULL);
1165a9fa9459Szrj   if (!this->has_children())
1166a9fa9459Szrj     return 0;
1167a9fa9459Szrj   if (this->child_offset_ == 0)
1168a9fa9459Szrj     this->child_offset_ = this->skip_attributes();
1169a9fa9459Szrj   return this->child_offset_;
1170a9fa9459Szrj }
1171a9fa9459Szrj 
1172a9fa9459Szrj // Return the offset of this DIE's next sibling.
1173a9fa9459Szrj 
1174a9fa9459Szrj off_t
sibling_offset()1175a9fa9459Szrj Dwarf_die::sibling_offset()
1176a9fa9459Szrj {
1177a9fa9459Szrj   gold_assert(this->abbrev_code_ != NULL);
1178a9fa9459Szrj 
1179a9fa9459Szrj   if (this->sibling_offset_ != 0)
1180a9fa9459Szrj     return this->sibling_offset_;
1181a9fa9459Szrj 
1182a9fa9459Szrj   if (!this->has_children())
1183a9fa9459Szrj     {
1184a9fa9459Szrj       this->sibling_offset_ = this->skip_attributes();
1185a9fa9459Szrj       return this->sibling_offset_;
1186a9fa9459Szrj     }
1187a9fa9459Szrj 
1188a9fa9459Szrj   if (this->has_sibling_attribute())
1189a9fa9459Szrj     {
1190a9fa9459Szrj       if (!this->read_attributes())
1191a9fa9459Szrj 	return 0;
1192a9fa9459Szrj       if (this->sibling_offset_ != 0)
1193a9fa9459Szrj 	return this->sibling_offset_;
1194a9fa9459Szrj     }
1195a9fa9459Szrj 
1196a9fa9459Szrj   // Skip over the children.
1197a9fa9459Szrj   off_t child_offset = this->child_offset();
1198a9fa9459Szrj   while (child_offset > 0)
1199a9fa9459Szrj     {
1200a9fa9459Szrj       Dwarf_die die(this->dwinfo_, child_offset, this);
1201a9fa9459Szrj       // The Dwarf_die ctor will set this DIE's sibling offset
1202a9fa9459Szrj       // when it reads a zero abbrev code.
1203a9fa9459Szrj       if (die.tag() == 0)
1204a9fa9459Szrj 	break;
1205a9fa9459Szrj       child_offset = die.sibling_offset();
1206a9fa9459Szrj     }
1207a9fa9459Szrj 
1208a9fa9459Szrj   // This should be set by now.  If not, there was a problem reading
1209a9fa9459Szrj   // the DWARF info, and we return 0.
1210a9fa9459Szrj   return this->sibling_offset_;
1211a9fa9459Szrj }
1212a9fa9459Szrj 
1213a9fa9459Szrj // class Dwarf_info_reader
1214a9fa9459Szrj 
1215a9fa9459Szrj // Begin parsing the debug info.  This calls visit_compilation_unit()
1216a9fa9459Szrj // or visit_type_unit() for each compilation or type unit found in the
1217a9fa9459Szrj // section, and visit_die() for each top-level DIE.
1218a9fa9459Szrj 
1219a9fa9459Szrj void
parse()1220a9fa9459Szrj Dwarf_info_reader::parse()
1221a9fa9459Szrj {
1222a9fa9459Szrj   if (this->object_->is_big_endian())
1223a9fa9459Szrj     {
1224a9fa9459Szrj #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1225a9fa9459Szrj       this->do_parse<true>();
1226a9fa9459Szrj #else
1227a9fa9459Szrj       gold_unreachable();
1228a9fa9459Szrj #endif
1229a9fa9459Szrj     }
1230a9fa9459Szrj   else
1231a9fa9459Szrj     {
1232a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1233a9fa9459Szrj       this->do_parse<false>();
1234a9fa9459Szrj #else
1235a9fa9459Szrj       gold_unreachable();
1236a9fa9459Szrj #endif
1237a9fa9459Szrj     }
1238a9fa9459Szrj }
1239a9fa9459Szrj 
1240a9fa9459Szrj template<bool big_endian>
1241a9fa9459Szrj void
do_parse()1242a9fa9459Szrj Dwarf_info_reader::do_parse()
1243a9fa9459Szrj {
1244a9fa9459Szrj   // Get the section contents and decompress if necessary.
1245a9fa9459Szrj   section_size_type buffer_size;
1246a9fa9459Szrj   bool buffer_is_new;
1247a9fa9459Szrj   this->buffer_ = this->object_->decompressed_section_contents(this->shndx_,
1248a9fa9459Szrj 							       &buffer_size,
1249a9fa9459Szrj 							       &buffer_is_new);
1250a9fa9459Szrj   if (this->buffer_ == NULL || buffer_size == 0)
1251a9fa9459Szrj     return;
1252a9fa9459Szrj   this->buffer_end_ = this->buffer_ + buffer_size;
1253a9fa9459Szrj 
1254a9fa9459Szrj   // The offset of this input section in the output section.
1255a9fa9459Szrj   off_t section_offset = this->object_->output_section_offset(this->shndx_);
1256a9fa9459Szrj 
1257a9fa9459Szrj   // Start tracking relocations for this section.
1258a9fa9459Szrj   this->reloc_mapper_ = make_elf_reloc_mapper(this->object_, this->symtab_,
1259a9fa9459Szrj 					      this->symtab_size_);
1260a9fa9459Szrj   this->reloc_mapper_->initialize(this->reloc_shndx_, this->reloc_type_);
1261a9fa9459Szrj 
1262a9fa9459Szrj   // Loop over compilation units (or type units).
1263a9fa9459Szrj   unsigned int abbrev_shndx = this->abbrev_shndx_;
1264a9fa9459Szrj   off_t abbrev_offset = 0;
1265a9fa9459Szrj   const unsigned char* pinfo = this->buffer_;
1266a9fa9459Szrj   while (pinfo < this->buffer_end_)
1267a9fa9459Szrj     {
1268a9fa9459Szrj       // Read the compilation (or type) unit header.
1269a9fa9459Szrj       const unsigned char* cu_start = pinfo;
1270a9fa9459Szrj       this->cu_offset_ = cu_start - this->buffer_;
1271a9fa9459Szrj       this->cu_length_ = this->buffer_end_ - cu_start;
1272a9fa9459Szrj 
1273a9fa9459Szrj       // Read unit_length (4 or 12 bytes).
1274a9fa9459Szrj       if (!this->check_buffer(pinfo + 4))
1275a9fa9459Szrj 	break;
1276a9fa9459Szrj       uint32_t unit_length =
1277a9fa9459Szrj           elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1278a9fa9459Szrj       pinfo += 4;
1279a9fa9459Szrj       if (unit_length == 0xffffffff)
1280a9fa9459Szrj 	{
1281a9fa9459Szrj 	  if (!this->check_buffer(pinfo + 8))
1282a9fa9459Szrj 	    break;
1283a9fa9459Szrj 	  unit_length = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1284a9fa9459Szrj 	  pinfo += 8;
1285a9fa9459Szrj 	  this->offset_size_ = 8;
1286a9fa9459Szrj 	}
1287a9fa9459Szrj       else
1288a9fa9459Szrj 	this->offset_size_ = 4;
1289a9fa9459Szrj       if (!this->check_buffer(pinfo + unit_length))
1290a9fa9459Szrj 	break;
1291a9fa9459Szrj       const unsigned char* cu_end = pinfo + unit_length;
1292a9fa9459Szrj       this->cu_length_ = cu_end - cu_start;
1293a9fa9459Szrj       if (!this->check_buffer(pinfo + 2 + this->offset_size_ + 1))
1294a9fa9459Szrj 	break;
1295a9fa9459Szrj 
1296a9fa9459Szrj       // Read version (2 bytes).
1297a9fa9459Szrj       this->cu_version_ =
1298a9fa9459Szrj 	  elfcpp::Swap_unaligned<16, big_endian>::readval(pinfo);
1299a9fa9459Szrj       pinfo += 2;
1300a9fa9459Szrj 
1301a9fa9459Szrj       // Read debug_abbrev_offset (4 or 8 bytes).
1302a9fa9459Szrj       if (this->offset_size_ == 4)
1303a9fa9459Szrj 	abbrev_offset = elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1304a9fa9459Szrj       else
1305a9fa9459Szrj 	abbrev_offset = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1306a9fa9459Szrj       if (this->reloc_shndx_ > 0)
1307a9fa9459Szrj 	{
1308a9fa9459Szrj 	  off_t reloc_offset = pinfo - this->buffer_;
1309a9fa9459Szrj 	  off_t value;
1310a9fa9459Szrj 	  abbrev_shndx =
1311a9fa9459Szrj 	      this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
1312a9fa9459Szrj 	  if (abbrev_shndx == 0)
1313a9fa9459Szrj 	    return;
1314a9fa9459Szrj 	  if (this->reloc_type_ == elfcpp::SHT_REL)
1315a9fa9459Szrj 	    abbrev_offset += value;
1316a9fa9459Szrj 	  else
1317a9fa9459Szrj 	    abbrev_offset = value;
1318a9fa9459Szrj 	}
1319a9fa9459Szrj       pinfo += this->offset_size_;
1320a9fa9459Szrj 
1321a9fa9459Szrj       // Read address_size (1 byte).
1322a9fa9459Szrj       this->address_size_ = *pinfo++;
1323a9fa9459Szrj 
1324a9fa9459Szrj       // For type units, read the two extra fields.
1325a9fa9459Szrj       uint64_t signature = 0;
1326a9fa9459Szrj       off_t type_offset = 0;
1327a9fa9459Szrj       if (this->is_type_unit_)
1328a9fa9459Szrj         {
1329a9fa9459Szrj 	  if (!this->check_buffer(pinfo + 8 + this->offset_size_))
1330a9fa9459Szrj 	    break;
1331a9fa9459Szrj 
1332a9fa9459Szrj 	  // Read type_signature (8 bytes).
1333a9fa9459Szrj 	  signature = elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1334a9fa9459Szrj 	  pinfo += 8;
1335a9fa9459Szrj 
1336a9fa9459Szrj 	  // Read type_offset (4 or 8 bytes).
1337a9fa9459Szrj 	  if (this->offset_size_ == 4)
1338a9fa9459Szrj 	    type_offset =
1339a9fa9459Szrj 		elfcpp::Swap_unaligned<32, big_endian>::readval(pinfo);
1340a9fa9459Szrj 	  else
1341a9fa9459Szrj 	    type_offset =
1342a9fa9459Szrj 		elfcpp::Swap_unaligned<64, big_endian>::readval(pinfo);
1343a9fa9459Szrj 	  pinfo += this->offset_size_;
1344a9fa9459Szrj 	}
1345a9fa9459Szrj 
1346a9fa9459Szrj       // Read the .debug_abbrev table.
1347a9fa9459Szrj       this->abbrev_table_.read_abbrevs(this->object_, abbrev_shndx,
1348a9fa9459Szrj 				       abbrev_offset);
1349a9fa9459Szrj 
1350a9fa9459Szrj       // Visit the root DIE.
1351a9fa9459Szrj       Dwarf_die root_die(this,
1352a9fa9459Szrj 			 pinfo - (this->buffer_ + this->cu_offset_),
1353a9fa9459Szrj 			 NULL);
1354a9fa9459Szrj       if (root_die.tag() != 0)
1355a9fa9459Szrj 	{
1356a9fa9459Szrj 	  // Visit the CU or TU.
1357a9fa9459Szrj 	  if (this->is_type_unit_)
1358a9fa9459Szrj 	    this->visit_type_unit(section_offset + this->cu_offset_,
1359a9fa9459Szrj 				  cu_end - cu_start, type_offset, signature,
1360a9fa9459Szrj 				  &root_die);
1361a9fa9459Szrj 	  else
1362a9fa9459Szrj 	    this->visit_compilation_unit(section_offset + this->cu_offset_,
1363a9fa9459Szrj 					 cu_end - cu_start, &root_die);
1364a9fa9459Szrj 	}
1365a9fa9459Szrj 
1366a9fa9459Szrj       // Advance to the next CU.
1367a9fa9459Szrj       pinfo = cu_end;
1368a9fa9459Szrj     }
1369a9fa9459Szrj 
1370a9fa9459Szrj   if (buffer_is_new)
1371a9fa9459Szrj     {
1372a9fa9459Szrj       delete[] this->buffer_;
1373a9fa9459Szrj       this->buffer_ = NULL;
1374a9fa9459Szrj     }
1375a9fa9459Szrj }
1376a9fa9459Szrj 
1377a9fa9459Szrj // Read the DWARF string table.
1378a9fa9459Szrj 
1379a9fa9459Szrj bool
do_read_string_table(unsigned int string_shndx)1380a9fa9459Szrj Dwarf_info_reader::do_read_string_table(unsigned int string_shndx)
1381a9fa9459Szrj {
1382a9fa9459Szrj   Relobj* object = this->object_;
1383a9fa9459Szrj 
1384a9fa9459Szrj   // If we don't have relocations, string_shndx will be 0, and
1385a9fa9459Szrj   // we'll have to hunt for the .debug_str section.
1386a9fa9459Szrj   if (string_shndx == 0)
1387a9fa9459Szrj     {
1388a9fa9459Szrj       for (unsigned int i = 1; i < this->object_->shnum(); ++i)
1389a9fa9459Szrj 	{
1390a9fa9459Szrj 	  std::string name = object->section_name(i);
1391a9fa9459Szrj 	  if (name == ".debug_str" || name == ".zdebug_str")
1392a9fa9459Szrj 	    {
1393a9fa9459Szrj 	      string_shndx = i;
1394a9fa9459Szrj 	      this->string_output_section_offset_ =
1395a9fa9459Szrj 		  object->output_section_offset(i);
1396a9fa9459Szrj 	      break;
1397a9fa9459Szrj 	    }
1398a9fa9459Szrj 	}
1399a9fa9459Szrj       if (string_shndx == 0)
1400a9fa9459Szrj 	return false;
1401a9fa9459Szrj     }
1402a9fa9459Szrj 
1403a9fa9459Szrj   if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
1404a9fa9459Szrj     {
1405a9fa9459Szrj       delete[] this->string_buffer_;
1406a9fa9459Szrj       this->owns_string_buffer_ = false;
1407a9fa9459Szrj     }
1408a9fa9459Szrj 
1409a9fa9459Szrj   // Get the secton contents and decompress if necessary.
1410a9fa9459Szrj   section_size_type buffer_size;
1411a9fa9459Szrj   const unsigned char* buffer =
1412a9fa9459Szrj       object->decompressed_section_contents(string_shndx,
1413a9fa9459Szrj 					    &buffer_size,
1414a9fa9459Szrj 					    &this->owns_string_buffer_);
1415a9fa9459Szrj   this->string_buffer_ = reinterpret_cast<const char*>(buffer);
1416a9fa9459Szrj   this->string_buffer_end_ = this->string_buffer_ + buffer_size;
1417a9fa9459Szrj   this->string_shndx_ = string_shndx;
1418a9fa9459Szrj   return true;
1419a9fa9459Szrj }
1420a9fa9459Szrj 
1421a9fa9459Szrj // Read a possibly unaligned integer of SIZE.
1422a9fa9459Szrj template <int valsize>
1423a9fa9459Szrj inline typename elfcpp::Valtype_base<valsize>::Valtype
read_from_pointer(const unsigned char * source)1424a9fa9459Szrj Dwarf_info_reader::read_from_pointer(const unsigned char* source)
1425a9fa9459Szrj {
1426a9fa9459Szrj   typename elfcpp::Valtype_base<valsize>::Valtype return_value;
1427a9fa9459Szrj   if (this->object_->is_big_endian())
1428a9fa9459Szrj     return_value = elfcpp::Swap_unaligned<valsize, true>::readval(source);
1429a9fa9459Szrj   else
1430a9fa9459Szrj     return_value = elfcpp::Swap_unaligned<valsize, false>::readval(source);
1431a9fa9459Szrj   return return_value;
1432a9fa9459Szrj }
1433a9fa9459Szrj 
1434a9fa9459Szrj // Read a possibly unaligned integer of SIZE.  Update SOURCE after read.
1435a9fa9459Szrj template <int valsize>
1436a9fa9459Szrj inline typename elfcpp::Valtype_base<valsize>::Valtype
read_from_pointer(const unsigned char ** source)1437a9fa9459Szrj Dwarf_info_reader::read_from_pointer(const unsigned char** source)
1438a9fa9459Szrj {
1439a9fa9459Szrj   typename elfcpp::Valtype_base<valsize>::Valtype return_value;
1440a9fa9459Szrj   if (this->object_->is_big_endian())
1441a9fa9459Szrj     return_value = elfcpp::Swap_unaligned<valsize, true>::readval(*source);
1442a9fa9459Szrj   else
1443a9fa9459Szrj     return_value = elfcpp::Swap_unaligned<valsize, false>::readval(*source);
1444a9fa9459Szrj   *source += valsize / 8;
1445a9fa9459Szrj   return return_value;
1446a9fa9459Szrj }
1447a9fa9459Szrj 
1448a9fa9459Szrj // Look for a relocation at offset ATTR_OFF in the dwarf info,
1449a9fa9459Szrj // and return the section index and offset of the target.
1450a9fa9459Szrj 
1451a9fa9459Szrj unsigned int
lookup_reloc(off_t attr_off,off_t * target_off)1452a9fa9459Szrj Dwarf_info_reader::lookup_reloc(off_t attr_off, off_t* target_off)
1453a9fa9459Szrj {
1454a9fa9459Szrj   off_t value;
1455a9fa9459Szrj   attr_off += this->cu_offset_;
1456a9fa9459Szrj   unsigned int shndx = this->reloc_mapper_->get_reloc_target(attr_off, &value);
1457a9fa9459Szrj   if (shndx == 0)
1458a9fa9459Szrj     return 0;
1459a9fa9459Szrj   if (this->reloc_type_ == elfcpp::SHT_REL)
1460a9fa9459Szrj     *target_off += value;
1461a9fa9459Szrj   else
1462a9fa9459Szrj     *target_off = value;
1463a9fa9459Szrj   return shndx;
1464a9fa9459Szrj }
1465a9fa9459Szrj 
1466a9fa9459Szrj // Return a string from the DWARF string table.
1467a9fa9459Szrj 
1468a9fa9459Szrj const char*
get_string(off_t str_off,unsigned int string_shndx)1469a9fa9459Szrj Dwarf_info_reader::get_string(off_t str_off, unsigned int string_shndx)
1470a9fa9459Szrj {
1471a9fa9459Szrj   if (!this->read_string_table(string_shndx))
1472a9fa9459Szrj     return NULL;
1473a9fa9459Szrj 
1474a9fa9459Szrj   // Correct the offset.  For incremental update links, we have a
1475a9fa9459Szrj   // relocated offset that is relative to the output section, but
1476a9fa9459Szrj   // here we need an offset relative to the input section.
1477a9fa9459Szrj   str_off -= this->string_output_section_offset_;
1478a9fa9459Szrj 
1479a9fa9459Szrj   const char* p = this->string_buffer_ + str_off;
1480a9fa9459Szrj 
1481a9fa9459Szrj   if (p < this->string_buffer_ || p >= this->string_buffer_end_)
1482a9fa9459Szrj     return NULL;
1483a9fa9459Szrj 
1484a9fa9459Szrj   return p;
1485a9fa9459Szrj }
1486a9fa9459Szrj 
1487a9fa9459Szrj // The following are default, do-nothing, implementations of the
1488a9fa9459Szrj // hook methods normally provided by a derived class.  We provide
1489a9fa9459Szrj // default implementations rather than no implementation so that
1490a9fa9459Szrj // a derived class needs to implement only the hooks that it needs
1491a9fa9459Szrj // to use.
1492a9fa9459Szrj 
1493a9fa9459Szrj // Process a compilation unit and parse its child DIE.
1494a9fa9459Szrj 
1495a9fa9459Szrj void
visit_compilation_unit(off_t,off_t,Dwarf_die *)1496a9fa9459Szrj Dwarf_info_reader::visit_compilation_unit(off_t, off_t, Dwarf_die*)
1497a9fa9459Szrj {
1498a9fa9459Szrj }
1499a9fa9459Szrj 
1500a9fa9459Szrj // Process a type unit and parse its child DIE.
1501a9fa9459Szrj 
1502a9fa9459Szrj void
visit_type_unit(off_t,off_t,off_t,uint64_t,Dwarf_die *)1503a9fa9459Szrj Dwarf_info_reader::visit_type_unit(off_t, off_t, off_t, uint64_t, Dwarf_die*)
1504a9fa9459Szrj {
1505a9fa9459Szrj }
1506a9fa9459Szrj 
1507a9fa9459Szrj // Print a warning about a corrupt debug section.
1508a9fa9459Szrj 
1509a9fa9459Szrj void
warn_corrupt_debug_section() const1510a9fa9459Szrj Dwarf_info_reader::warn_corrupt_debug_section() const
1511a9fa9459Szrj {
1512a9fa9459Szrj   gold_warning(_("%s: corrupt debug info in %s"),
1513a9fa9459Szrj 	       this->object_->name().c_str(),
1514a9fa9459Szrj 	       this->object_->section_name(this->shndx_).c_str());
1515a9fa9459Szrj }
1516a9fa9459Szrj 
1517a9fa9459Szrj // class Sized_dwarf_line_info
1518a9fa9459Szrj 
1519a9fa9459Szrj struct LineStateMachine
1520a9fa9459Szrj {
1521a9fa9459Szrj   int file_num;
1522a9fa9459Szrj   uint64_t address;
1523a9fa9459Szrj   int line_num;
1524a9fa9459Szrj   int column_num;
1525a9fa9459Szrj   unsigned int shndx;    // the section address refers to
1526a9fa9459Szrj   bool is_stmt;          // stmt means statement.
1527a9fa9459Szrj   bool basic_block;
1528a9fa9459Szrj   bool end_sequence;
1529a9fa9459Szrj };
1530a9fa9459Szrj 
1531a9fa9459Szrj static void
ResetLineStateMachine(struct LineStateMachine * lsm,bool default_is_stmt)1532a9fa9459Szrj ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
1533a9fa9459Szrj {
1534a9fa9459Szrj   lsm->file_num = 1;
1535a9fa9459Szrj   lsm->address = 0;
1536a9fa9459Szrj   lsm->line_num = 1;
1537a9fa9459Szrj   lsm->column_num = 0;
1538a9fa9459Szrj   lsm->shndx = -1U;
1539a9fa9459Szrj   lsm->is_stmt = default_is_stmt;
1540a9fa9459Szrj   lsm->basic_block = false;
1541a9fa9459Szrj   lsm->end_sequence = false;
1542a9fa9459Szrj }
1543a9fa9459Szrj 
1544a9fa9459Szrj template<int size, bool big_endian>
Sized_dwarf_line_info(Object * object,unsigned int read_shndx)1545a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(
1546a9fa9459Szrj     Object* object,
1547a9fa9459Szrj     unsigned int read_shndx)
1548a9fa9459Szrj   : data_valid_(false), buffer_(NULL), buffer_start_(NULL),
1549a9fa9459Szrj     reloc_mapper_(NULL), symtab_buffer_(NULL), directories_(), files_(),
1550a9fa9459Szrj     current_header_index_(-1)
1551a9fa9459Szrj {
1552a9fa9459Szrj   unsigned int debug_shndx;
1553a9fa9459Szrj 
1554a9fa9459Szrj   for (debug_shndx = 1; debug_shndx < object->shnum(); ++debug_shndx)
1555a9fa9459Szrj     {
1556a9fa9459Szrj       // FIXME: do this more efficiently: section_name() isn't super-fast
1557a9fa9459Szrj       std::string name = object->section_name(debug_shndx);
1558a9fa9459Szrj       if (name == ".debug_line" || name == ".zdebug_line")
1559a9fa9459Szrj 	{
1560a9fa9459Szrj 	  section_size_type buffer_size;
1561a9fa9459Szrj 	  bool is_new = false;
1562a9fa9459Szrj 	  this->buffer_ = object->decompressed_section_contents(debug_shndx,
1563a9fa9459Szrj 								&buffer_size,
1564a9fa9459Szrj 								&is_new);
1565a9fa9459Szrj 	  if (is_new)
1566a9fa9459Szrj 	    this->buffer_start_ = this->buffer_;
1567a9fa9459Szrj 	  this->buffer_end_ = this->buffer_ + buffer_size;
1568a9fa9459Szrj 	  break;
1569a9fa9459Szrj 	}
1570a9fa9459Szrj     }
1571a9fa9459Szrj   if (this->buffer_ == NULL)
1572a9fa9459Szrj     return;
1573a9fa9459Szrj 
1574a9fa9459Szrj   // Find the relocation section for ".debug_line".
1575a9fa9459Szrj   // We expect these for relobjs (.o's) but not dynobjs (.so's).
1576a9fa9459Szrj   unsigned int reloc_shndx = 0;
1577a9fa9459Szrj   for (unsigned int i = 0; i < object->shnum(); ++i)
1578a9fa9459Szrj     {
1579a9fa9459Szrj       unsigned int reloc_sh_type = object->section_type(i);
1580a9fa9459Szrj       if ((reloc_sh_type == elfcpp::SHT_REL
1581a9fa9459Szrj 	   || reloc_sh_type == elfcpp::SHT_RELA)
1582a9fa9459Szrj 	  && object->section_info(i) == debug_shndx)
1583a9fa9459Szrj 	{
1584a9fa9459Szrj 	  reloc_shndx = i;
1585a9fa9459Szrj 	  this->track_relocs_type_ = reloc_sh_type;
1586a9fa9459Szrj 	  break;
1587a9fa9459Szrj 	}
1588a9fa9459Szrj     }
1589a9fa9459Szrj 
1590a9fa9459Szrj   // Finally, we need the symtab section to interpret the relocs.
1591a9fa9459Szrj   if (reloc_shndx != 0)
1592a9fa9459Szrj     {
1593a9fa9459Szrj       unsigned int symtab_shndx;
1594a9fa9459Szrj       for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
1595a9fa9459Szrj         if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
1596a9fa9459Szrj           {
1597a9fa9459Szrj 	    this->symtab_buffer_ = object->section_contents(
1598a9fa9459Szrj 		symtab_shndx, &this->symtab_buffer_size_, false);
1599a9fa9459Szrj             break;
1600a9fa9459Szrj           }
1601a9fa9459Szrj       if (this->symtab_buffer_ == NULL)
1602a9fa9459Szrj         return;
1603a9fa9459Szrj     }
1604a9fa9459Szrj 
1605a9fa9459Szrj   this->reloc_mapper_ =
1606a9fa9459Szrj       new Sized_elf_reloc_mapper<size, big_endian>(object,
1607a9fa9459Szrj 						   this->symtab_buffer_,
1608a9fa9459Szrj 						   this->symtab_buffer_size_);
1609a9fa9459Szrj   if (!this->reloc_mapper_->initialize(reloc_shndx, this->track_relocs_type_))
1610a9fa9459Szrj     return;
1611a9fa9459Szrj 
1612a9fa9459Szrj   // Now that we have successfully read all the data, parse the debug
1613a9fa9459Szrj   // info.
1614a9fa9459Szrj   this->data_valid_ = true;
1615a9fa9459Szrj   this->read_line_mappings(read_shndx);
1616a9fa9459Szrj }
1617a9fa9459Szrj 
1618a9fa9459Szrj // Read the DWARF header.
1619a9fa9459Szrj 
1620a9fa9459Szrj template<int size, bool big_endian>
1621a9fa9459Szrj const unsigned char*
read_header_prolog(const unsigned char * lineptr)1622a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
1623a9fa9459Szrj     const unsigned char* lineptr)
1624a9fa9459Szrj {
1625a9fa9459Szrj   uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
1626a9fa9459Szrj   lineptr += 4;
1627a9fa9459Szrj 
1628a9fa9459Szrj   // In DWARF2/3, if the initial length is all 1 bits, then the offset
1629a9fa9459Szrj   // size is 8 and we need to read the next 8 bytes for the real length.
1630a9fa9459Szrj   if (initial_length == 0xffffffff)
1631a9fa9459Szrj     {
1632a9fa9459Szrj       header_.offset_size = 8;
1633a9fa9459Szrj       initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
1634a9fa9459Szrj       lineptr += 8;
1635a9fa9459Szrj     }
1636a9fa9459Szrj   else
1637a9fa9459Szrj     header_.offset_size = 4;
1638a9fa9459Szrj 
1639a9fa9459Szrj   header_.total_length = initial_length;
1640a9fa9459Szrj 
1641a9fa9459Szrj   gold_assert(lineptr + header_.total_length <= buffer_end_);
1642a9fa9459Szrj 
1643a9fa9459Szrj   header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
1644a9fa9459Szrj   lineptr += 2;
1645a9fa9459Szrj 
1646a9fa9459Szrj   if (header_.offset_size == 4)
1647a9fa9459Szrj     header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
1648a9fa9459Szrj   else
1649a9fa9459Szrj     header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
1650a9fa9459Szrj   lineptr += header_.offset_size;
1651a9fa9459Szrj 
1652a9fa9459Szrj   header_.min_insn_length = *lineptr;
1653a9fa9459Szrj   lineptr += 1;
1654a9fa9459Szrj 
1655*8c87cab6Szrj   if (header_.version < 4)
1656*8c87cab6Szrj     header_.max_ops_per_insn = 1;
1657*8c87cab6Szrj   else
1658*8c87cab6Szrj     {
1659*8c87cab6Szrj       // DWARF 4 added the maximum_operations_per_instruction field.
1660*8c87cab6Szrj       header_.max_ops_per_insn = *lineptr;
1661*8c87cab6Szrj       lineptr += 1;
1662*8c87cab6Szrj       // TODO: Add support for values other than 1.
1663*8c87cab6Szrj       gold_assert(header_.max_ops_per_insn == 1);
1664*8c87cab6Szrj     }
1665*8c87cab6Szrj 
1666a9fa9459Szrj   header_.default_is_stmt = *lineptr;
1667a9fa9459Szrj   lineptr += 1;
1668a9fa9459Szrj 
1669a9fa9459Szrj   header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
1670a9fa9459Szrj   lineptr += 1;
1671a9fa9459Szrj 
1672a9fa9459Szrj   header_.line_range = *lineptr;
1673a9fa9459Szrj   lineptr += 1;
1674a9fa9459Szrj 
1675a9fa9459Szrj   header_.opcode_base = *lineptr;
1676a9fa9459Szrj   lineptr += 1;
1677a9fa9459Szrj 
1678a9fa9459Szrj   header_.std_opcode_lengths.resize(header_.opcode_base + 1);
1679a9fa9459Szrj   header_.std_opcode_lengths[0] = 0;
1680a9fa9459Szrj   for (int i = 1; i < header_.opcode_base; i++)
1681a9fa9459Szrj     {
1682a9fa9459Szrj       header_.std_opcode_lengths[i] = *lineptr;
1683a9fa9459Szrj       lineptr += 1;
1684a9fa9459Szrj     }
1685a9fa9459Szrj 
1686a9fa9459Szrj   return lineptr;
1687a9fa9459Szrj }
1688a9fa9459Szrj 
1689a9fa9459Szrj // The header for a debug_line section is mildly complicated, because
1690a9fa9459Szrj // the line info is very tightly encoded.
1691a9fa9459Szrj 
1692a9fa9459Szrj template<int size, bool big_endian>
1693a9fa9459Szrj const unsigned char*
read_header_tables(const unsigned char * lineptr)1694a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::read_header_tables(
1695a9fa9459Szrj     const unsigned char* lineptr)
1696a9fa9459Szrj {
1697a9fa9459Szrj   ++this->current_header_index_;
1698a9fa9459Szrj 
1699a9fa9459Szrj   // Create a new directories_ entry and a new files_ entry for our new
1700a9fa9459Szrj   // header.  We initialize each with a single empty element, because
1701a9fa9459Szrj   // dwarf indexes directory and filenames starting at 1.
1702a9fa9459Szrj   gold_assert(static_cast<int>(this->directories_.size())
1703a9fa9459Szrj 	      == this->current_header_index_);
1704a9fa9459Szrj   gold_assert(static_cast<int>(this->files_.size())
1705a9fa9459Szrj 	      == this->current_header_index_);
1706a9fa9459Szrj   this->directories_.push_back(std::vector<std::string>(1));
1707a9fa9459Szrj   this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
1708a9fa9459Szrj 
1709a9fa9459Szrj   // It is legal for the directory entry table to be empty.
1710a9fa9459Szrj   if (*lineptr)
1711a9fa9459Szrj     {
1712a9fa9459Szrj       int dirindex = 1;
1713a9fa9459Szrj       while (*lineptr)
1714a9fa9459Szrj         {
1715a9fa9459Szrj 	  const char* dirname = reinterpret_cast<const char*>(lineptr);
1716a9fa9459Szrj           gold_assert(dirindex
1717a9fa9459Szrj 		      == static_cast<int>(this->directories_.back().size()));
1718a9fa9459Szrj           this->directories_.back().push_back(dirname);
1719a9fa9459Szrj           lineptr += this->directories_.back().back().size() + 1;
1720a9fa9459Szrj           dirindex++;
1721a9fa9459Szrj         }
1722a9fa9459Szrj     }
1723a9fa9459Szrj   lineptr++;
1724a9fa9459Szrj 
1725a9fa9459Szrj   // It is also legal for the file entry table to be empty.
1726a9fa9459Szrj   if (*lineptr)
1727a9fa9459Szrj     {
1728a9fa9459Szrj       int fileindex = 1;
1729a9fa9459Szrj       size_t len;
1730a9fa9459Szrj       while (*lineptr)
1731a9fa9459Szrj         {
1732a9fa9459Szrj           const char* filename = reinterpret_cast<const char*>(lineptr);
1733a9fa9459Szrj           lineptr += strlen(filename) + 1;
1734a9fa9459Szrj 
1735a9fa9459Szrj           uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
1736a9fa9459Szrj           lineptr += len;
1737a9fa9459Szrj 
1738a9fa9459Szrj           if (dirindex >= this->directories_.back().size())
1739a9fa9459Szrj             dirindex = 0;
1740a9fa9459Szrj 	  int dirindexi = static_cast<int>(dirindex);
1741a9fa9459Szrj 
1742a9fa9459Szrj           read_unsigned_LEB_128(lineptr, &len);   // mod_time
1743a9fa9459Szrj           lineptr += len;
1744a9fa9459Szrj 
1745a9fa9459Szrj           read_unsigned_LEB_128(lineptr, &len);   // filelength
1746a9fa9459Szrj           lineptr += len;
1747a9fa9459Szrj 
1748a9fa9459Szrj           gold_assert(fileindex
1749a9fa9459Szrj 		      == static_cast<int>(this->files_.back().size()));
1750a9fa9459Szrj           this->files_.back().push_back(std::make_pair(dirindexi, filename));
1751a9fa9459Szrj           fileindex++;
1752a9fa9459Szrj         }
1753a9fa9459Szrj     }
1754a9fa9459Szrj   lineptr++;
1755a9fa9459Szrj 
1756a9fa9459Szrj   return lineptr;
1757a9fa9459Szrj }
1758a9fa9459Szrj 
1759a9fa9459Szrj // Process a single opcode in the .debug.line structure.
1760a9fa9459Szrj 
1761a9fa9459Szrj template<int size, bool big_endian>
1762a9fa9459Szrj bool
process_one_opcode(const unsigned char * start,struct LineStateMachine * lsm,size_t * len)1763a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
1764a9fa9459Szrj     const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
1765a9fa9459Szrj {
1766a9fa9459Szrj   size_t oplen = 0;
1767a9fa9459Szrj   size_t templen;
1768a9fa9459Szrj   unsigned char opcode = *start;
1769a9fa9459Szrj   oplen++;
1770a9fa9459Szrj   start++;
1771a9fa9459Szrj 
1772a9fa9459Szrj   // If the opcode is great than the opcode_base, it is a special
1773a9fa9459Szrj   // opcode. Most line programs consist mainly of special opcodes.
1774a9fa9459Szrj   if (opcode >= header_.opcode_base)
1775a9fa9459Szrj     {
1776a9fa9459Szrj       opcode -= header_.opcode_base;
1777a9fa9459Szrj       const int advance_address = ((opcode / header_.line_range)
1778a9fa9459Szrj                                    * header_.min_insn_length);
1779a9fa9459Szrj       lsm->address += advance_address;
1780a9fa9459Szrj 
1781a9fa9459Szrj       const int advance_line = ((opcode % header_.line_range)
1782a9fa9459Szrj                                 + header_.line_base);
1783a9fa9459Szrj       lsm->line_num += advance_line;
1784a9fa9459Szrj       lsm->basic_block = true;
1785a9fa9459Szrj       *len = oplen;
1786a9fa9459Szrj       return true;
1787a9fa9459Szrj     }
1788a9fa9459Szrj 
1789a9fa9459Szrj   // Otherwise, we have the regular opcodes
1790a9fa9459Szrj   switch (opcode)
1791a9fa9459Szrj     {
1792a9fa9459Szrj     case elfcpp::DW_LNS_copy:
1793a9fa9459Szrj       lsm->basic_block = false;
1794a9fa9459Szrj       *len = oplen;
1795a9fa9459Szrj       return true;
1796a9fa9459Szrj 
1797a9fa9459Szrj     case elfcpp::DW_LNS_advance_pc:
1798a9fa9459Szrj       {
1799a9fa9459Szrj         const uint64_t advance_address
1800a9fa9459Szrj             = read_unsigned_LEB_128(start, &templen);
1801a9fa9459Szrj         oplen += templen;
1802a9fa9459Szrj         lsm->address += header_.min_insn_length * advance_address;
1803a9fa9459Szrj       }
1804a9fa9459Szrj       break;
1805a9fa9459Szrj 
1806a9fa9459Szrj     case elfcpp::DW_LNS_advance_line:
1807a9fa9459Szrj       {
1808a9fa9459Szrj         const uint64_t advance_line = read_signed_LEB_128(start, &templen);
1809a9fa9459Szrj         oplen += templen;
1810a9fa9459Szrj         lsm->line_num += advance_line;
1811a9fa9459Szrj       }
1812a9fa9459Szrj       break;
1813a9fa9459Szrj 
1814a9fa9459Szrj     case elfcpp::DW_LNS_set_file:
1815a9fa9459Szrj       {
1816a9fa9459Szrj         const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
1817a9fa9459Szrj         oplen += templen;
1818a9fa9459Szrj         lsm->file_num = fileno;
1819a9fa9459Szrj       }
1820a9fa9459Szrj       break;
1821a9fa9459Szrj 
1822a9fa9459Szrj     case elfcpp::DW_LNS_set_column:
1823a9fa9459Szrj       {
1824a9fa9459Szrj         const uint64_t colno = read_unsigned_LEB_128(start, &templen);
1825a9fa9459Szrj         oplen += templen;
1826a9fa9459Szrj         lsm->column_num = colno;
1827a9fa9459Szrj       }
1828a9fa9459Szrj       break;
1829a9fa9459Szrj 
1830a9fa9459Szrj     case elfcpp::DW_LNS_negate_stmt:
1831a9fa9459Szrj       lsm->is_stmt = !lsm->is_stmt;
1832a9fa9459Szrj       break;
1833a9fa9459Szrj 
1834a9fa9459Szrj     case elfcpp::DW_LNS_set_basic_block:
1835a9fa9459Szrj       lsm->basic_block = true;
1836a9fa9459Szrj       break;
1837a9fa9459Szrj 
1838a9fa9459Szrj     case elfcpp::DW_LNS_fixed_advance_pc:
1839a9fa9459Szrj       {
1840a9fa9459Szrj         int advance_address;
1841a9fa9459Szrj         advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
1842a9fa9459Szrj         oplen += 2;
1843a9fa9459Szrj         lsm->address += advance_address;
1844a9fa9459Szrj       }
1845a9fa9459Szrj       break;
1846a9fa9459Szrj 
1847a9fa9459Szrj     case elfcpp::DW_LNS_const_add_pc:
1848a9fa9459Szrj       {
1849a9fa9459Szrj         const int advance_address = (header_.min_insn_length
1850a9fa9459Szrj                                      * ((255 - header_.opcode_base)
1851a9fa9459Szrj                                         / header_.line_range));
1852a9fa9459Szrj         lsm->address += advance_address;
1853a9fa9459Szrj       }
1854a9fa9459Szrj       break;
1855a9fa9459Szrj 
1856a9fa9459Szrj     case elfcpp::DW_LNS_extended_op:
1857a9fa9459Szrj       {
1858a9fa9459Szrj         const uint64_t extended_op_len
1859a9fa9459Szrj             = read_unsigned_LEB_128(start, &templen);
1860a9fa9459Szrj         start += templen;
1861a9fa9459Szrj         oplen += templen + extended_op_len;
1862a9fa9459Szrj 
1863a9fa9459Szrj         const unsigned char extended_op = *start;
1864a9fa9459Szrj         start++;
1865a9fa9459Szrj 
1866a9fa9459Szrj         switch (extended_op)
1867a9fa9459Szrj           {
1868a9fa9459Szrj           case elfcpp::DW_LNE_end_sequence:
1869a9fa9459Szrj             // This means that the current byte is the one immediately
1870a9fa9459Szrj             // after a set of instructions.  Record the current line
1871a9fa9459Szrj             // for up to one less than the current address.
1872a9fa9459Szrj             lsm->line_num = -1;
1873a9fa9459Szrj             lsm->end_sequence = true;
1874a9fa9459Szrj             *len = oplen;
1875a9fa9459Szrj             return true;
1876a9fa9459Szrj 
1877a9fa9459Szrj           case elfcpp::DW_LNE_set_address:
1878a9fa9459Szrj             {
1879a9fa9459Szrj               lsm->address =
1880a9fa9459Szrj 		elfcpp::Swap_unaligned<size, big_endian>::readval(start);
1881a9fa9459Szrj               typename Reloc_map::const_iterator it
1882a9fa9459Szrj                   = this->reloc_map_.find(start - this->buffer_);
1883a9fa9459Szrj               if (it != reloc_map_.end())
1884a9fa9459Szrj                 {
1885a9fa9459Szrj 		  // If this is a SHT_RELA section, then ignore the
1886a9fa9459Szrj 		  // section contents.  This assumes that this is a
1887a9fa9459Szrj 		  // straight reloc which just uses the reloc addend.
1888a9fa9459Szrj 		  // The reloc addend has already been included in the
1889a9fa9459Szrj 		  // symbol value.
1890a9fa9459Szrj 		  if (this->track_relocs_type_ == elfcpp::SHT_RELA)
1891a9fa9459Szrj 		    lsm->address = 0;
1892a9fa9459Szrj 		  // Add in the symbol value.
1893a9fa9459Szrj 		  lsm->address += it->second.second;
1894a9fa9459Szrj                   lsm->shndx = it->second.first;
1895a9fa9459Szrj                 }
1896a9fa9459Szrj               else
1897a9fa9459Szrj                 {
1898a9fa9459Szrj                   // If we're a normal .o file, with relocs, every
1899a9fa9459Szrj                   // set_address should have an associated relocation.
1900a9fa9459Szrj 		  if (this->input_is_relobj())
1901a9fa9459Szrj                     this->data_valid_ = false;
1902a9fa9459Szrj                 }
1903a9fa9459Szrj               break;
1904a9fa9459Szrj             }
1905a9fa9459Szrj           case elfcpp::DW_LNE_define_file:
1906a9fa9459Szrj             {
1907a9fa9459Szrj               const char* filename  = reinterpret_cast<const char*>(start);
1908a9fa9459Szrj               templen = strlen(filename) + 1;
1909a9fa9459Szrj               start += templen;
1910a9fa9459Szrj 
1911a9fa9459Szrj               uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
1912a9fa9459Szrj 
1913a9fa9459Szrj               if (dirindex >= this->directories_.back().size())
1914a9fa9459Szrj                 dirindex = 0;
1915a9fa9459Szrj 	      int dirindexi = static_cast<int>(dirindex);
1916a9fa9459Szrj 
1917a9fa9459Szrj               // This opcode takes two additional ULEB128 parameters
1918a9fa9459Szrj               // (mod_time and filelength), but we don't use those
1919a9fa9459Szrj               // values.  Because OPLEN already tells us how far to
1920a9fa9459Szrj               // skip to the next opcode, we don't need to read
1921a9fa9459Szrj               // them at all.
1922a9fa9459Szrj 
1923a9fa9459Szrj               this->files_.back().push_back(std::make_pair(dirindexi,
1924a9fa9459Szrj 							   filename));
1925a9fa9459Szrj             }
1926a9fa9459Szrj             break;
1927a9fa9459Szrj           }
1928a9fa9459Szrj       }
1929a9fa9459Szrj       break;
1930a9fa9459Szrj 
1931a9fa9459Szrj     default:
1932a9fa9459Szrj       {
1933a9fa9459Szrj         // Ignore unknown opcode  silently
1934a9fa9459Szrj         for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
1935a9fa9459Szrj           {
1936a9fa9459Szrj             size_t templen;
1937a9fa9459Szrj             read_unsigned_LEB_128(start, &templen);
1938a9fa9459Szrj             start += templen;
1939a9fa9459Szrj             oplen += templen;
1940a9fa9459Szrj           }
1941a9fa9459Szrj       }
1942a9fa9459Szrj       break;
1943a9fa9459Szrj   }
1944a9fa9459Szrj   *len = oplen;
1945a9fa9459Szrj   return false;
1946a9fa9459Szrj }
1947a9fa9459Szrj 
1948a9fa9459Szrj // Read the debug information at LINEPTR and store it in the line
1949a9fa9459Szrj // number map.
1950a9fa9459Szrj 
1951a9fa9459Szrj template<int size, bool big_endian>
1952a9fa9459Szrj unsigned const char*
read_lines(unsigned const char * lineptr,unsigned int shndx)1953a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
1954a9fa9459Szrj                                                     unsigned int shndx)
1955a9fa9459Szrj {
1956a9fa9459Szrj   struct LineStateMachine lsm;
1957a9fa9459Szrj 
1958a9fa9459Szrj   // LENGTHSTART is the place the length field is based on.  It is the
1959a9fa9459Szrj   // point in the header after the initial length field.
1960a9fa9459Szrj   const unsigned char* lengthstart = buffer_;
1961a9fa9459Szrj 
1962a9fa9459Szrj   // In 64 bit dwarf, the initial length is 12 bytes, because of the
1963a9fa9459Szrj   // 0xffffffff at the start.
1964a9fa9459Szrj   if (header_.offset_size == 8)
1965a9fa9459Szrj     lengthstart += 12;
1966a9fa9459Szrj   else
1967a9fa9459Szrj     lengthstart += 4;
1968a9fa9459Szrj 
1969a9fa9459Szrj   while (lineptr < lengthstart + header_.total_length)
1970a9fa9459Szrj     {
1971a9fa9459Szrj       ResetLineStateMachine(&lsm, header_.default_is_stmt);
1972a9fa9459Szrj       while (!lsm.end_sequence)
1973a9fa9459Szrj         {
1974a9fa9459Szrj           size_t oplength;
1975a9fa9459Szrj           bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
1976a9fa9459Szrj           if (add_line
1977a9fa9459Szrj               && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
1978a9fa9459Szrj             {
1979a9fa9459Szrj               Offset_to_lineno_entry entry
1980a9fa9459Szrj                   = { static_cast<off_t>(lsm.address),
1981a9fa9459Szrj 		      this->current_header_index_,
1982a9fa9459Szrj 		      static_cast<unsigned int>(lsm.file_num),
1983a9fa9459Szrj 		      true, lsm.line_num };
1984a9fa9459Szrj 	      std::vector<Offset_to_lineno_entry>&
1985a9fa9459Szrj 		map(this->line_number_map_[lsm.shndx]);
1986a9fa9459Szrj 	      // If we see two consecutive entries with the same
1987a9fa9459Szrj 	      // offset and a real line number, then mark the first
1988a9fa9459Szrj 	      // one as non-canonical.
1989a9fa9459Szrj 	      if (!map.empty()
1990a9fa9459Szrj 		  && (map.back().offset == static_cast<off_t>(lsm.address))
1991a9fa9459Szrj 		  && lsm.line_num != -1
1992a9fa9459Szrj 		  && map.back().line_num != -1)
1993a9fa9459Szrj 		map.back().last_line_for_offset = false;
1994a9fa9459Szrj 	      map.push_back(entry);
1995a9fa9459Szrj             }
1996a9fa9459Szrj           lineptr += oplength;
1997a9fa9459Szrj         }
1998a9fa9459Szrj     }
1999a9fa9459Szrj 
2000a9fa9459Szrj   return lengthstart + header_.total_length;
2001a9fa9459Szrj }
2002a9fa9459Szrj 
2003a9fa9459Szrj // Read the relocations into a Reloc_map.
2004a9fa9459Szrj 
2005a9fa9459Szrj template<int size, bool big_endian>
2006a9fa9459Szrj void
read_relocs()2007a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::read_relocs()
2008a9fa9459Szrj {
2009a9fa9459Szrj   if (this->symtab_buffer_ == NULL)
2010a9fa9459Szrj     return;
2011a9fa9459Szrj 
2012a9fa9459Szrj   off_t value;
2013a9fa9459Szrj   off_t reloc_offset;
2014a9fa9459Szrj   while ((reloc_offset = this->reloc_mapper_->next_offset()) != -1)
2015a9fa9459Szrj     {
2016a9fa9459Szrj       const unsigned int shndx =
2017a9fa9459Szrj           this->reloc_mapper_->get_reloc_target(reloc_offset, &value);
2018a9fa9459Szrj 
2019a9fa9459Szrj       // There is no reason to record non-ordinary section indexes, or
2020a9fa9459Szrj       // SHN_UNDEF, because they will never match the real section.
2021a9fa9459Szrj       if (shndx != 0)
2022a9fa9459Szrj 	this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
2023a9fa9459Szrj 
2024a9fa9459Szrj       this->reloc_mapper_->advance(reloc_offset + 1);
2025a9fa9459Szrj     }
2026a9fa9459Szrj }
2027a9fa9459Szrj 
2028a9fa9459Szrj // Read the line number info.
2029a9fa9459Szrj 
2030a9fa9459Szrj template<int size, bool big_endian>
2031a9fa9459Szrj void
read_line_mappings(unsigned int shndx)2032a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::read_line_mappings(unsigned int shndx)
2033a9fa9459Szrj {
2034a9fa9459Szrj   gold_assert(this->data_valid_ == true);
2035a9fa9459Szrj 
2036a9fa9459Szrj   this->read_relocs();
2037a9fa9459Szrj   while (this->buffer_ < this->buffer_end_)
2038a9fa9459Szrj     {
2039a9fa9459Szrj       const unsigned char* lineptr = this->buffer_;
2040a9fa9459Szrj       lineptr = this->read_header_prolog(lineptr);
2041a9fa9459Szrj       lineptr = this->read_header_tables(lineptr);
2042a9fa9459Szrj       lineptr = this->read_lines(lineptr, shndx);
2043a9fa9459Szrj       this->buffer_ = lineptr;
2044a9fa9459Szrj     }
2045a9fa9459Szrj 
2046a9fa9459Szrj   // Sort the lines numbers, so addr2line can use binary search.
2047a9fa9459Szrj   for (typename Lineno_map::iterator it = line_number_map_.begin();
2048a9fa9459Szrj        it != line_number_map_.end();
2049a9fa9459Szrj        ++it)
2050a9fa9459Szrj     // Each vector needs to be sorted by offset.
2051a9fa9459Szrj     std::sort(it->second.begin(), it->second.end());
2052a9fa9459Szrj }
2053a9fa9459Szrj 
2054a9fa9459Szrj // Some processing depends on whether the input is a .o file or not.
2055a9fa9459Szrj // For instance, .o files have relocs, and have .debug_lines
2056a9fa9459Szrj // information on a per section basis.  .so files, on the other hand,
2057a9fa9459Szrj // lack relocs, and offsets are unique, so we can ignore the section
2058a9fa9459Szrj // information.
2059a9fa9459Szrj 
2060a9fa9459Szrj template<int size, bool big_endian>
2061a9fa9459Szrj bool
input_is_relobj()2062a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
2063a9fa9459Szrj {
2064a9fa9459Szrj   // Only .o files have relocs and the symtab buffer that goes with them.
2065a9fa9459Szrj   return this->symtab_buffer_ != NULL;
2066a9fa9459Szrj }
2067a9fa9459Szrj 
2068a9fa9459Szrj // Given an Offset_to_lineno_entry vector, and an offset, figure out
2069a9fa9459Szrj // if the offset points into a function according to the vector (see
2070a9fa9459Szrj // comments below for the algorithm).  If it does, return an iterator
2071a9fa9459Szrj // into the vector that points to the line-number that contains that
2072a9fa9459Szrj // offset.  If not, it returns vector::end().
2073a9fa9459Szrj 
2074a9fa9459Szrj static std::vector<Offset_to_lineno_entry>::const_iterator
offset_to_iterator(const std::vector<Offset_to_lineno_entry> * offsets,off_t offset)2075a9fa9459Szrj offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
2076a9fa9459Szrj                    off_t offset)
2077a9fa9459Szrj {
2078a9fa9459Szrj   const Offset_to_lineno_entry lookup_key = { offset, 0, 0, true, 0 };
2079a9fa9459Szrj 
2080a9fa9459Szrj   // lower_bound() returns the smallest offset which is >= lookup_key.
2081a9fa9459Szrj   // If no offset in offsets is >= lookup_key, returns end().
2082a9fa9459Szrj   std::vector<Offset_to_lineno_entry>::const_iterator it
2083a9fa9459Szrj       = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
2084a9fa9459Szrj 
2085a9fa9459Szrj   // This code is easiest to understand with a concrete example.
2086a9fa9459Szrj   // Here's a possible offsets array:
2087a9fa9459Szrj   // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16},  // 0
2088a9fa9459Szrj   //  {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20},  // 1
2089a9fa9459Szrj   //  {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22},  // 2
2090a9fa9459Szrj   //  {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25},  // 3
2091a9fa9459Szrj   //  {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1},  // 4
2092a9fa9459Szrj   //  {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65},  // 5
2093a9fa9459Szrj   //  {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66},  // 6
2094a9fa9459Szrj   //  {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1},  // 7
2095a9fa9459Szrj   //  {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48},  // 8
2096a9fa9459Szrj   //  {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47},  // 9
2097a9fa9459Szrj   //  {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49},  // 10
2098a9fa9459Szrj   //  {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50},  // 11
2099a9fa9459Szrj   //  {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51},  // 12
2100a9fa9459Szrj   //  {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1},  // 13
2101a9fa9459Szrj   //  {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19},  // 14
2102a9fa9459Szrj   //  {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20},  // 15
2103a9fa9459Szrj   //  {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67},  // 16
2104a9fa9459Szrj   //  {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1},  // 17
2105a9fa9459Szrj   //  {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66},  // 18
2106a9fa9459Szrj   //  {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68},  // 19
2107a9fa9459Szrj   //  {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1},  // 20
2108a9fa9459Szrj   // The entries with line_num == -1 mark the end of a function: the
2109a9fa9459Szrj   // associated offset is one past the last instruction in the
2110a9fa9459Szrj   // function.  This can correspond to the beginning of the next
2111a9fa9459Szrj   // function (as is true for offset 3232); alternately, there can be
2112a9fa9459Szrj   // a gap between the end of one function and the start of the next
2113a9fa9459Szrj   // (as is true for some others, most obviously from 3236->5764).
2114a9fa9459Szrj   //
2115a9fa9459Szrj   // Case 1: lookup_key has offset == 10.  lower_bound returns
2116a9fa9459Szrj   //         offsets[0].  Since it's not an exact match and we're
2117a9fa9459Szrj   //         at the beginning of offsets, we return end() (invalid).
2118a9fa9459Szrj   // Case 2: lookup_key has offset 10000.  lower_bound returns
2119a9fa9459Szrj   //         offset[21] (end()).  We return end() (invalid).
2120a9fa9459Szrj   // Case 3: lookup_key has offset == 3211.  lower_bound matches
2121a9fa9459Szrj   //         offsets[0] exactly, and that's the entry we return.
2122a9fa9459Szrj   // Case 4: lookup_key has offset == 3232.  lower_bound returns
2123a9fa9459Szrj   //         offsets[4].  That's an exact match, but indicates
2124a9fa9459Szrj   //         end-of-function.  We check if offsets[5] is also an
2125a9fa9459Szrj   //         exact match but not end-of-function.  It is, so we
2126a9fa9459Szrj   //         return offsets[5].
2127a9fa9459Szrj   // Case 5: lookup_key has offset == 3214.  lower_bound returns
2128a9fa9459Szrj   //         offsets[1].  Since it's not an exact match, we back
2129a9fa9459Szrj   //         up to the offset that's < lookup_key, offsets[0].
2130a9fa9459Szrj   //         We note offsets[0] is a valid entry (not end-of-function),
2131a9fa9459Szrj   //         so that's the entry we return.
2132a9fa9459Szrj   // Case 6: lookup_key has offset == 4000.  lower_bound returns
2133a9fa9459Szrj   //         offsets[8].  Since it's not an exact match, we back
2134a9fa9459Szrj   //         up to offsets[7].  Since offsets[7] indicates
2135a9fa9459Szrj   //         end-of-function, we know lookup_key is between
2136a9fa9459Szrj   //         functions, so we return end() (not a valid offset).
2137a9fa9459Szrj   // Case 7: lookup_key has offset == 5794.  lower_bound returns
2138a9fa9459Szrj   //         offsets[19].  Since it's not an exact match, we back
2139a9fa9459Szrj   //         up to offsets[16].  Note we back up to the *first*
2140a9fa9459Szrj   //         entry with offset 5793, not just offsets[19-1].
2141a9fa9459Szrj   //         We note offsets[16] is a valid entry, so we return it.
2142a9fa9459Szrj   //         If offsets[16] had had line_num == -1, we would have
2143a9fa9459Szrj   //         checked offsets[17].  The reason for this is that
2144a9fa9459Szrj   //         16 and 17 can be in an arbitrary order, since we sort
2145a9fa9459Szrj   //         only by offset and last_line_for_offset.  (Note it
2146a9fa9459Szrj   //         doesn't help to use line_number as a tertiary sort key,
2147a9fa9459Szrj   //         since sometimes we want the -1 to be first and sometimes
2148a9fa9459Szrj   //         we want it to be last.)
2149a9fa9459Szrj 
2150a9fa9459Szrj   // This deals with cases (1) and (2).
2151a9fa9459Szrj   if ((it == offsets->begin() && offset < it->offset)
2152a9fa9459Szrj       || it == offsets->end())
2153a9fa9459Szrj     return offsets->end();
2154a9fa9459Szrj 
2155a9fa9459Szrj   // This deals with cases (3) and (4).
2156a9fa9459Szrj   if (offset == it->offset)
2157a9fa9459Szrj     {
2158a9fa9459Szrj       while (it != offsets->end()
2159a9fa9459Szrj              && it->offset == offset
2160a9fa9459Szrj              && it->line_num == -1)
2161a9fa9459Szrj         ++it;
2162a9fa9459Szrj       if (it == offsets->end() || it->offset != offset)
2163a9fa9459Szrj         return offsets->end();
2164a9fa9459Szrj       else
2165a9fa9459Szrj         return it;
2166a9fa9459Szrj     }
2167a9fa9459Szrj 
2168a9fa9459Szrj   // This handles the first part of case (7) -- we back up to the
2169a9fa9459Szrj   // *first* entry that has the offset that's behind us.
2170a9fa9459Szrj   gold_assert(it != offsets->begin());
2171a9fa9459Szrj   std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
2172a9fa9459Szrj   --it;
2173a9fa9459Szrj   const off_t range_value = it->offset;
2174a9fa9459Szrj   while (it != offsets->begin() && (it-1)->offset == range_value)
2175a9fa9459Szrj     --it;
2176a9fa9459Szrj 
2177a9fa9459Szrj   // This handles cases (5), (6), and (7): if any entry in the
2178a9fa9459Szrj   // equal_range [it, range_end) has a line_num != -1, it's a valid
2179a9fa9459Szrj   // match.  If not, we're not in a function.  The line number we saw
2180a9fa9459Szrj   // last for an offset will be sorted first, so it'll get returned if
2181a9fa9459Szrj   // it's present.
2182a9fa9459Szrj   for (; it != range_end; ++it)
2183a9fa9459Szrj     if (it->line_num != -1)
2184a9fa9459Szrj       return it;
2185a9fa9459Szrj   return offsets->end();
2186a9fa9459Szrj }
2187a9fa9459Szrj 
2188a9fa9459Szrj // Returns the canonical filename:lineno for the address passed in.
2189a9fa9459Szrj // If other_lines is not NULL, appends the non-canonical lines
2190a9fa9459Szrj // assigned to the same address.
2191a9fa9459Szrj 
2192a9fa9459Szrj template<int size, bool big_endian>
2193a9fa9459Szrj std::string
do_addr2line(unsigned int shndx,off_t offset,std::vector<std::string> * other_lines)2194a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::do_addr2line(
2195a9fa9459Szrj     unsigned int shndx,
2196a9fa9459Szrj     off_t offset,
2197a9fa9459Szrj     std::vector<std::string>* other_lines)
2198a9fa9459Szrj {
2199a9fa9459Szrj   if (this->data_valid_ == false)
2200a9fa9459Szrj     return "";
2201a9fa9459Szrj 
2202a9fa9459Szrj   const std::vector<Offset_to_lineno_entry>* offsets;
2203a9fa9459Szrj   // If we do not have reloc information, then our input is a .so or
2204a9fa9459Szrj   // some similar data structure where all the information is held in
2205a9fa9459Szrj   // the offset.  In that case, we ignore the input shndx.
2206a9fa9459Szrj   if (this->input_is_relobj())
2207a9fa9459Szrj     offsets = &this->line_number_map_[shndx];
2208a9fa9459Szrj   else
2209a9fa9459Szrj     offsets = &this->line_number_map_[-1U];
2210a9fa9459Szrj   if (offsets->empty())
2211a9fa9459Szrj     return "";
2212a9fa9459Szrj 
2213a9fa9459Szrj   typename std::vector<Offset_to_lineno_entry>::const_iterator it
2214a9fa9459Szrj       = offset_to_iterator(offsets, offset);
2215a9fa9459Szrj   if (it == offsets->end())
2216a9fa9459Szrj     return "";
2217a9fa9459Szrj 
2218a9fa9459Szrj   std::string result = this->format_file_lineno(*it);
2219a9fa9459Szrj   gold_debug(DEBUG_LOCATION, "do_addr2line: canonical result: %s",
2220a9fa9459Szrj 	     result.c_str());
2221a9fa9459Szrj   if (other_lines != NULL)
2222a9fa9459Szrj     {
2223a9fa9459Szrj       unsigned int last_file_num = it->file_num;
2224a9fa9459Szrj       int last_line_num = it->line_num;
2225a9fa9459Szrj       // Return up to 4 more locations from the beginning of the function
2226a9fa9459Szrj       // for fuzzy matching.
2227a9fa9459Szrj       for (++it; it != offsets->end(); ++it)
2228a9fa9459Szrj 	{
2229a9fa9459Szrj 	  if (it->offset == offset && it->line_num == -1)
2230a9fa9459Szrj 	    continue;  // The end of a previous function.
2231a9fa9459Szrj 	  if (it->line_num == -1)
2232a9fa9459Szrj 	    break;  // The end of the current function.
2233a9fa9459Szrj 	  if (it->file_num != last_file_num || it->line_num != last_line_num)
2234a9fa9459Szrj 	    {
2235a9fa9459Szrj 	      other_lines->push_back(this->format_file_lineno(*it));
2236a9fa9459Szrj 	      gold_debug(DEBUG_LOCATION, "do_addr2line: other: %s",
2237a9fa9459Szrj 			 other_lines->back().c_str());
2238a9fa9459Szrj 	      last_file_num = it->file_num;
2239a9fa9459Szrj 	      last_line_num = it->line_num;
2240a9fa9459Szrj 	    }
2241a9fa9459Szrj 	  if (it->offset > offset && other_lines->size() >= 4)
2242a9fa9459Szrj 	    break;
2243a9fa9459Szrj 	}
2244a9fa9459Szrj     }
2245a9fa9459Szrj 
2246a9fa9459Szrj   return result;
2247a9fa9459Szrj }
2248a9fa9459Szrj 
2249a9fa9459Szrj // Convert the file_num + line_num into a string.
2250a9fa9459Szrj 
2251a9fa9459Szrj template<int size, bool big_endian>
2252a9fa9459Szrj std::string
format_file_lineno(const Offset_to_lineno_entry & loc) const2253a9fa9459Szrj Sized_dwarf_line_info<size, big_endian>::format_file_lineno(
2254a9fa9459Szrj     const Offset_to_lineno_entry& loc) const
2255a9fa9459Szrj {
2256a9fa9459Szrj   std::string ret;
2257a9fa9459Szrj 
2258a9fa9459Szrj   gold_assert(loc.header_num < static_cast<int>(this->files_.size()));
2259a9fa9459Szrj   gold_assert(loc.file_num
2260a9fa9459Szrj 	      < static_cast<unsigned int>(this->files_[loc.header_num].size()));
2261a9fa9459Szrj   const std::pair<int, std::string>& filename_pair
2262a9fa9459Szrj       = this->files_[loc.header_num][loc.file_num];
2263a9fa9459Szrj   const std::string& filename = filename_pair.second;
2264a9fa9459Szrj 
2265a9fa9459Szrj   gold_assert(loc.header_num < static_cast<int>(this->directories_.size()));
2266a9fa9459Szrj   gold_assert(filename_pair.first
2267a9fa9459Szrj               < static_cast<int>(this->directories_[loc.header_num].size()));
2268a9fa9459Szrj   const std::string& dirname
2269a9fa9459Szrj       = this->directories_[loc.header_num][filename_pair.first];
2270a9fa9459Szrj 
2271a9fa9459Szrj   if (!dirname.empty())
2272a9fa9459Szrj     {
2273a9fa9459Szrj       ret += dirname;
2274a9fa9459Szrj       ret += "/";
2275a9fa9459Szrj     }
2276a9fa9459Szrj   ret += filename;
2277a9fa9459Szrj   if (ret.empty())
2278a9fa9459Szrj     ret = "(unknown)";
2279a9fa9459Szrj 
2280a9fa9459Szrj   char buffer[64];   // enough to hold a line number
2281a9fa9459Szrj   snprintf(buffer, sizeof(buffer), "%d", loc.line_num);
2282a9fa9459Szrj   ret += ":";
2283a9fa9459Szrj   ret += buffer;
2284a9fa9459Szrj 
2285a9fa9459Szrj   return ret;
2286a9fa9459Szrj }
2287a9fa9459Szrj 
2288a9fa9459Szrj // Dwarf_line_info routines.
2289a9fa9459Szrj 
2290a9fa9459Szrj static unsigned int next_generation_count = 0;
2291a9fa9459Szrj 
2292a9fa9459Szrj struct Addr2line_cache_entry
2293a9fa9459Szrj {
2294a9fa9459Szrj   Object* object;
2295a9fa9459Szrj   unsigned int shndx;
2296a9fa9459Szrj   Dwarf_line_info* dwarf_line_info;
2297a9fa9459Szrj   unsigned int generation_count;
2298a9fa9459Szrj   unsigned int access_count;
2299a9fa9459Szrj 
Addr2line_cache_entrygold::Addr2line_cache_entry2300a9fa9459Szrj   Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
2301a9fa9459Szrj       : object(o), shndx(s), dwarf_line_info(d),
2302a9fa9459Szrj         generation_count(next_generation_count), access_count(0)
2303a9fa9459Szrj   {
2304a9fa9459Szrj     if (next_generation_count < (1U << 31))
2305a9fa9459Szrj       ++next_generation_count;
2306a9fa9459Szrj   }
2307a9fa9459Szrj };
2308a9fa9459Szrj // We expect this cache to be small, so don't bother with a hashtable
2309a9fa9459Szrj // or priority queue or anything: just use a simple vector.
2310a9fa9459Szrj static std::vector<Addr2line_cache_entry> addr2line_cache;
2311a9fa9459Szrj 
2312a9fa9459Szrj std::string
one_addr2line(Object * object,unsigned int shndx,off_t offset,size_t cache_size,std::vector<std::string> * other_lines)2313a9fa9459Szrj Dwarf_line_info::one_addr2line(Object* object,
2314a9fa9459Szrj                                unsigned int shndx, off_t offset,
2315a9fa9459Szrj                                size_t cache_size,
2316a9fa9459Szrj                                std::vector<std::string>* other_lines)
2317a9fa9459Szrj {
2318a9fa9459Szrj   Dwarf_line_info* lineinfo = NULL;
2319a9fa9459Szrj   std::vector<Addr2line_cache_entry>::iterator it;
2320a9fa9459Szrj 
2321a9fa9459Szrj   // First, check the cache.  If we hit, update the counts.
2322a9fa9459Szrj   for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
2323a9fa9459Szrj     {
2324a9fa9459Szrj       if (it->object == object && it->shndx == shndx)
2325a9fa9459Szrj         {
2326a9fa9459Szrj           lineinfo = it->dwarf_line_info;
2327a9fa9459Szrj           it->generation_count = next_generation_count;
2328a9fa9459Szrj           // We cap generation_count at 2^31 -1 to avoid overflow.
2329a9fa9459Szrj           if (next_generation_count < (1U << 31))
2330a9fa9459Szrj             ++next_generation_count;
2331a9fa9459Szrj           // We cap access_count at 31 so 2^access_count doesn't overflow
2332a9fa9459Szrj           if (it->access_count < 31)
2333a9fa9459Szrj             ++it->access_count;
2334a9fa9459Szrj           break;
2335a9fa9459Szrj         }
2336a9fa9459Szrj     }
2337a9fa9459Szrj 
2338a9fa9459Szrj   // If we don't hit the cache, create a new object and insert into the
2339a9fa9459Szrj   // cache.
2340a9fa9459Szrj   if (lineinfo == NULL)
2341a9fa9459Szrj   {
2342a9fa9459Szrj     switch (parameters->size_and_endianness())
2343a9fa9459Szrj       {
2344a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
2345a9fa9459Szrj         case Parameters::TARGET_32_LITTLE:
2346a9fa9459Szrj           lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
2347a9fa9459Szrj #endif
2348a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
2349a9fa9459Szrj         case Parameters::TARGET_32_BIG:
2350a9fa9459Szrj           lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
2351a9fa9459Szrj #endif
2352a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
2353a9fa9459Szrj         case Parameters::TARGET_64_LITTLE:
2354a9fa9459Szrj           lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
2355a9fa9459Szrj #endif
2356a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
2357a9fa9459Szrj         case Parameters::TARGET_64_BIG:
2358a9fa9459Szrj           lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
2359a9fa9459Szrj #endif
2360a9fa9459Szrj         default:
2361a9fa9459Szrj           gold_unreachable();
2362a9fa9459Szrj       }
2363a9fa9459Szrj     addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
2364a9fa9459Szrj   }
2365a9fa9459Szrj 
2366a9fa9459Szrj   // Now that we have our object, figure out the answer
2367a9fa9459Szrj   std::string retval = lineinfo->addr2line(shndx, offset, other_lines);
2368a9fa9459Szrj 
2369a9fa9459Szrj   // Finally, if our cache has grown too big, delete old objects.  We
2370a9fa9459Szrj   // assume the common (probably only) case is deleting only one object.
2371a9fa9459Szrj   // We use a pretty simple scheme to evict: function of LRU and MFU.
2372a9fa9459Szrj   while (addr2line_cache.size() > cache_size)
2373a9fa9459Szrj     {
2374a9fa9459Szrj       unsigned int lowest_score = ~0U;
2375a9fa9459Szrj       std::vector<Addr2line_cache_entry>::iterator lowest
2376a9fa9459Szrj           = addr2line_cache.end();
2377a9fa9459Szrj       for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
2378a9fa9459Szrj         {
2379a9fa9459Szrj           const unsigned int score = (it->generation_count
2380a9fa9459Szrj                                       + (1U << it->access_count));
2381a9fa9459Szrj           if (score < lowest_score)
2382a9fa9459Szrj             {
2383a9fa9459Szrj               lowest_score = score;
2384a9fa9459Szrj               lowest = it;
2385a9fa9459Szrj             }
2386a9fa9459Szrj         }
2387a9fa9459Szrj       if (lowest != addr2line_cache.end())
2388a9fa9459Szrj         {
2389a9fa9459Szrj           delete lowest->dwarf_line_info;
2390a9fa9459Szrj           addr2line_cache.erase(lowest);
2391a9fa9459Szrj         }
2392a9fa9459Szrj     }
2393a9fa9459Szrj 
2394a9fa9459Szrj   return retval;
2395a9fa9459Szrj }
2396a9fa9459Szrj 
2397a9fa9459Szrj void
clear_addr2line_cache()2398a9fa9459Szrj Dwarf_line_info::clear_addr2line_cache()
2399a9fa9459Szrj {
2400a9fa9459Szrj   for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
2401a9fa9459Szrj        it != addr2line_cache.end();
2402a9fa9459Szrj        ++it)
2403a9fa9459Szrj     delete it->dwarf_line_info;
2404a9fa9459Szrj   addr2line_cache.clear();
2405a9fa9459Szrj }
2406a9fa9459Szrj 
2407a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
2408a9fa9459Szrj template
2409a9fa9459Szrj class Sized_dwarf_line_info<32, false>;
2410a9fa9459Szrj #endif
2411a9fa9459Szrj 
2412a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
2413a9fa9459Szrj template
2414a9fa9459Szrj class Sized_dwarf_line_info<32, true>;
2415a9fa9459Szrj #endif
2416a9fa9459Szrj 
2417a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
2418a9fa9459Szrj template
2419a9fa9459Szrj class Sized_dwarf_line_info<64, false>;
2420a9fa9459Szrj #endif
2421a9fa9459Szrj 
2422a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
2423a9fa9459Szrj template
2424a9fa9459Szrj class Sized_dwarf_line_info<64, true>;
2425a9fa9459Szrj #endif
2426a9fa9459Szrj 
2427a9fa9459Szrj } // End namespace gold.
2428