xref: /dflybsd-src/contrib/binutils-2.27/gold/dwarf_reader.h (revision a9fa94596c0f48c0524edaac2c001d92fa9c01ac)
1*a9fa9459Szrj // dwarf_reader.h -- parse dwarf2/3 debug information for gold  -*- C++ -*-
2*a9fa9459Szrj 
3*a9fa9459Szrj // Copyright (C) 2007-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*a9fa9459Szrj 
6*a9fa9459Szrj // This file is part of gold.
7*a9fa9459Szrj 
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj 
13*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*a9fa9459Szrj // GNU General Public License for more details.
17*a9fa9459Szrj 
18*a9fa9459Szrj // You should have received a copy of the GNU General Public License
19*a9fa9459Szrj // along with this program; if not, write to the Free Software
20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*a9fa9459Szrj // MA 02110-1301, USA.
22*a9fa9459Szrj 
23*a9fa9459Szrj #ifndef GOLD_DWARF_READER_H
24*a9fa9459Szrj #define GOLD_DWARF_READER_H
25*a9fa9459Szrj 
26*a9fa9459Szrj #include <vector>
27*a9fa9459Szrj #include <map>
28*a9fa9459Szrj #include <limits.h>
29*a9fa9459Szrj #include <sys/types.h>
30*a9fa9459Szrj 
31*a9fa9459Szrj #include "elfcpp.h"
32*a9fa9459Szrj #include "elfcpp_swap.h"
33*a9fa9459Szrj #include "dwarf.h"
34*a9fa9459Szrj #include "reloc.h"
35*a9fa9459Szrj 
36*a9fa9459Szrj namespace gold
37*a9fa9459Szrj {
38*a9fa9459Szrj 
39*a9fa9459Szrj class Dwarf_info_reader;
40*a9fa9459Szrj struct LineStateMachine;
41*a9fa9459Szrj 
42*a9fa9459Szrj // This class is used to extract the section index and offset of
43*a9fa9459Szrj // the target of a relocation for a given offset within the section.
44*a9fa9459Szrj 
45*a9fa9459Szrj class Elf_reloc_mapper
46*a9fa9459Szrj {
47*a9fa9459Szrj  public:
48*a9fa9459Szrj   Elf_reloc_mapper()
49*a9fa9459Szrj   { }
50*a9fa9459Szrj 
51*a9fa9459Szrj   virtual
52*a9fa9459Szrj   ~Elf_reloc_mapper()
53*a9fa9459Szrj   { }
54*a9fa9459Szrj 
55*a9fa9459Szrj   // Initialize the relocation tracker for section RELOC_SHNDX.
56*a9fa9459Szrj   bool
57*a9fa9459Szrj   initialize(unsigned int reloc_shndx, unsigned int reloc_type)
58*a9fa9459Szrj   { return this->do_initialize(reloc_shndx, reloc_type); }
59*a9fa9459Szrj 
60*a9fa9459Szrj   // Return the next reloc_offset.
61*a9fa9459Szrj   off_t
62*a9fa9459Szrj   next_offset()
63*a9fa9459Szrj   { return this->do_next_offset(); }
64*a9fa9459Szrj 
65*a9fa9459Szrj   // Advance to the next relocation past OFFSET.
66*a9fa9459Szrj   void
67*a9fa9459Szrj   advance(off_t offset)
68*a9fa9459Szrj   { this->do_advance(offset); }
69*a9fa9459Szrj 
70*a9fa9459Szrj   // Return the section index and offset within the section of the target
71*a9fa9459Szrj   // of the relocation for RELOC_OFFSET in the referring section.
72*a9fa9459Szrj   unsigned int
73*a9fa9459Szrj   get_reloc_target(off_t reloc_offset, off_t* target_offset)
74*a9fa9459Szrj   { return this->do_get_reloc_target(reloc_offset, target_offset); }
75*a9fa9459Szrj 
76*a9fa9459Szrj   // Checkpoint the current position in the reloc section.
77*a9fa9459Szrj   uint64_t
78*a9fa9459Szrj   checkpoint() const
79*a9fa9459Szrj   { return this->do_checkpoint(); }
80*a9fa9459Szrj 
81*a9fa9459Szrj   // Reset the current position to the CHECKPOINT.
82*a9fa9459Szrj   void
83*a9fa9459Szrj   reset(uint64_t checkpoint)
84*a9fa9459Szrj   { this->do_reset(checkpoint); }
85*a9fa9459Szrj 
86*a9fa9459Szrj  protected:
87*a9fa9459Szrj   virtual bool
88*a9fa9459Szrj   do_initialize(unsigned int, unsigned int) = 0;
89*a9fa9459Szrj 
90*a9fa9459Szrj   // Return the next reloc_offset.
91*a9fa9459Szrj   virtual off_t
92*a9fa9459Szrj   do_next_offset() = 0;
93*a9fa9459Szrj 
94*a9fa9459Szrj   // Advance to the next relocation past OFFSET.
95*a9fa9459Szrj   virtual void
96*a9fa9459Szrj   do_advance(off_t offset) = 0;
97*a9fa9459Szrj 
98*a9fa9459Szrj   virtual unsigned int
99*a9fa9459Szrj   do_get_reloc_target(off_t reloc_offset, off_t* target_offset) = 0;
100*a9fa9459Szrj 
101*a9fa9459Szrj   // Checkpoint the current position in the reloc section.
102*a9fa9459Szrj   virtual uint64_t
103*a9fa9459Szrj   do_checkpoint() const = 0;
104*a9fa9459Szrj 
105*a9fa9459Szrj   // Reset the current position to the CHECKPOINT.
106*a9fa9459Szrj   virtual void
107*a9fa9459Szrj   do_reset(uint64_t checkpoint) = 0;
108*a9fa9459Szrj };
109*a9fa9459Szrj 
110*a9fa9459Szrj template<int size, bool big_endian>
111*a9fa9459Szrj class Sized_elf_reloc_mapper : public Elf_reloc_mapper
112*a9fa9459Szrj {
113*a9fa9459Szrj  public:
114*a9fa9459Szrj   Sized_elf_reloc_mapper(Object* object, const unsigned char* symtab,
115*a9fa9459Szrj 			 off_t symtab_size)
116*a9fa9459Szrj     : object_(object), symtab_(symtab), symtab_size_(symtab_size),
117*a9fa9459Szrj       reloc_type_(0), track_relocs_()
118*a9fa9459Szrj   { }
119*a9fa9459Szrj 
120*a9fa9459Szrj  protected:
121*a9fa9459Szrj   bool
122*a9fa9459Szrj   do_initialize(unsigned int reloc_shndx, unsigned int reloc_type);
123*a9fa9459Szrj 
124*a9fa9459Szrj   // Return the next reloc_offset.
125*a9fa9459Szrj   virtual off_t
126*a9fa9459Szrj   do_next_offset()
127*a9fa9459Szrj   { return this->track_relocs_.next_offset(); }
128*a9fa9459Szrj 
129*a9fa9459Szrj   // Advance to the next relocation past OFFSET.
130*a9fa9459Szrj   virtual void
131*a9fa9459Szrj   do_advance(off_t offset)
132*a9fa9459Szrj   { this->track_relocs_.advance(offset); }
133*a9fa9459Szrj 
134*a9fa9459Szrj   unsigned int
135*a9fa9459Szrj   do_get_reloc_target(off_t reloc_offset, off_t* target_offset);
136*a9fa9459Szrj 
137*a9fa9459Szrj   // Checkpoint the current position in the reloc section.
138*a9fa9459Szrj   uint64_t
139*a9fa9459Szrj   do_checkpoint() const
140*a9fa9459Szrj   { return this->track_relocs_.checkpoint(); }
141*a9fa9459Szrj 
142*a9fa9459Szrj   // Reset the current position to the CHECKPOINT.
143*a9fa9459Szrj   void
144*a9fa9459Szrj   do_reset(uint64_t checkpoint)
145*a9fa9459Szrj   { this->track_relocs_.reset(checkpoint); }
146*a9fa9459Szrj 
147*a9fa9459Szrj  private:
148*a9fa9459Szrj   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
149*a9fa9459Szrj 
150*a9fa9459Szrj   // Return the section index of symbol SYMNDX, and copy its value to *VALUE.
151*a9fa9459Szrj   // Set *IS_ORDINARY true if the section index is an ordinary section index.
152*a9fa9459Szrj   unsigned int
153*a9fa9459Szrj   symbol_section(unsigned int symndx, Address* value, bool* is_ordinary);
154*a9fa9459Szrj 
155*a9fa9459Szrj   // The object file.
156*a9fa9459Szrj   Object* object_;
157*a9fa9459Szrj   // The ELF symbol table.
158*a9fa9459Szrj   const unsigned char* symtab_;
159*a9fa9459Szrj   // The size of the ELF symbol table.
160*a9fa9459Szrj   off_t symtab_size_;
161*a9fa9459Szrj   // Type of the relocation section (SHT_REL or SHT_RELA).
162*a9fa9459Szrj   unsigned int reloc_type_;
163*a9fa9459Szrj   // Relocations for the referring section.
164*a9fa9459Szrj   Track_relocs<size, big_endian> track_relocs_;
165*a9fa9459Szrj };
166*a9fa9459Szrj 
167*a9fa9459Szrj // This class is used to read the abbreviations table from the
168*a9fa9459Szrj // .debug_abbrev section of the object file.
169*a9fa9459Szrj 
170*a9fa9459Szrj class Dwarf_abbrev_table
171*a9fa9459Szrj {
172*a9fa9459Szrj  public:
173*a9fa9459Szrj   // An attribute list entry.
174*a9fa9459Szrj   struct Attribute
175*a9fa9459Szrj   {
176*a9fa9459Szrj     Attribute(unsigned int a, unsigned int f)
177*a9fa9459Szrj       : attr(a), form(f)
178*a9fa9459Szrj     { }
179*a9fa9459Szrj     unsigned int attr;
180*a9fa9459Szrj     unsigned int form;
181*a9fa9459Szrj   };
182*a9fa9459Szrj 
183*a9fa9459Szrj   // An abbrev code entry.
184*a9fa9459Szrj   struct Abbrev_code
185*a9fa9459Szrj   {
186*a9fa9459Szrj     Abbrev_code(unsigned int t, bool hc)
187*a9fa9459Szrj       : tag(t), has_children(hc), has_sibling_attribute(false), attributes()
188*a9fa9459Szrj     {
189*a9fa9459Szrj       this->attributes.reserve(10);
190*a9fa9459Szrj     }
191*a9fa9459Szrj 
192*a9fa9459Szrj     void
193*a9fa9459Szrj     add_attribute(unsigned int attr, unsigned int form)
194*a9fa9459Szrj     {
195*a9fa9459Szrj       this->attributes.push_back(Attribute(attr, form));
196*a9fa9459Szrj     }
197*a9fa9459Szrj 
198*a9fa9459Szrj     // The DWARF tag.
199*a9fa9459Szrj     unsigned int tag;
200*a9fa9459Szrj     // True if the DIE has children.
201*a9fa9459Szrj     bool has_children : 1;
202*a9fa9459Szrj     // True if the DIE has a sibling attribute.
203*a9fa9459Szrj     bool has_sibling_attribute : 1;
204*a9fa9459Szrj     // The list of attributes and forms.
205*a9fa9459Szrj     std::vector<Attribute> attributes;
206*a9fa9459Szrj   };
207*a9fa9459Szrj 
208*a9fa9459Szrj   Dwarf_abbrev_table()
209*a9fa9459Szrj     : abbrev_shndx_(0), abbrev_offset_(0), buffer_(NULL), buffer_end_(NULL),
210*a9fa9459Szrj       owns_buffer_(false), buffer_pos_(NULL), high_abbrev_codes_()
211*a9fa9459Szrj   {
212*a9fa9459Szrj     memset(this->low_abbrev_codes_, 0, sizeof(this->low_abbrev_codes_));
213*a9fa9459Szrj   }
214*a9fa9459Szrj 
215*a9fa9459Szrj   ~Dwarf_abbrev_table()
216*a9fa9459Szrj   {
217*a9fa9459Szrj     if (this->owns_buffer_ && this->buffer_ != NULL)
218*a9fa9459Szrj       delete[] this->buffer_;
219*a9fa9459Szrj     this->clear_abbrev_codes();
220*a9fa9459Szrj   }
221*a9fa9459Szrj 
222*a9fa9459Szrj   // Read the abbrev table from an object file.
223*a9fa9459Szrj   bool
224*a9fa9459Szrj   read_abbrevs(Relobj* object,
225*a9fa9459Szrj 	       unsigned int abbrev_shndx,
226*a9fa9459Szrj 	       off_t abbrev_offset)
227*a9fa9459Szrj   {
228*a9fa9459Szrj     // If we've already read this abbrev table, return immediately.
229*a9fa9459Szrj     if (this->abbrev_shndx_ > 0
230*a9fa9459Szrj 	&& this->abbrev_shndx_ == abbrev_shndx
231*a9fa9459Szrj 	&& this->abbrev_offset_ == abbrev_offset)
232*a9fa9459Szrj       return true;
233*a9fa9459Szrj     return this->do_read_abbrevs(object, abbrev_shndx, abbrev_offset);
234*a9fa9459Szrj   }
235*a9fa9459Szrj 
236*a9fa9459Szrj   // Return the abbrev code entry for CODE.  This is a fast path for
237*a9fa9459Szrj   // abbrev codes that are in the direct lookup table.  If not found
238*a9fa9459Szrj   // there, we call do_get_abbrev() to do the hard work.
239*a9fa9459Szrj   const Abbrev_code*
240*a9fa9459Szrj   get_abbrev(unsigned int code)
241*a9fa9459Szrj   {
242*a9fa9459Szrj     if (code < this->low_abbrev_code_max_
243*a9fa9459Szrj 	&& this->low_abbrev_codes_[code] != NULL)
244*a9fa9459Szrj       return this->low_abbrev_codes_[code];
245*a9fa9459Szrj     return this->do_get_abbrev(code);
246*a9fa9459Szrj   }
247*a9fa9459Szrj 
248*a9fa9459Szrj  private:
249*a9fa9459Szrj   // Read the abbrev table from an object file.
250*a9fa9459Szrj   bool
251*a9fa9459Szrj   do_read_abbrevs(Relobj* object,
252*a9fa9459Szrj 		  unsigned int abbrev_shndx,
253*a9fa9459Szrj 		  off_t abbrev_offset);
254*a9fa9459Szrj 
255*a9fa9459Szrj   // Lookup the abbrev code entry for CODE.
256*a9fa9459Szrj   const Abbrev_code*
257*a9fa9459Szrj   do_get_abbrev(unsigned int code);
258*a9fa9459Szrj 
259*a9fa9459Szrj   // Store an abbrev code entry for CODE.
260*a9fa9459Szrj   void
261*a9fa9459Szrj   store_abbrev(unsigned int code, const Abbrev_code* entry)
262*a9fa9459Szrj   {
263*a9fa9459Szrj     if (code < this->low_abbrev_code_max_)
264*a9fa9459Szrj       this->low_abbrev_codes_[code] = entry;
265*a9fa9459Szrj     else
266*a9fa9459Szrj       this->high_abbrev_codes_[code] = entry;
267*a9fa9459Szrj   }
268*a9fa9459Szrj 
269*a9fa9459Szrj   // Clear the abbrev code table and release the memory it uses.
270*a9fa9459Szrj   void
271*a9fa9459Szrj   clear_abbrev_codes();
272*a9fa9459Szrj 
273*a9fa9459Szrj   typedef Unordered_map<unsigned int, const Abbrev_code*> Abbrev_code_table;
274*a9fa9459Szrj 
275*a9fa9459Szrj   // The section index of the current abbrev table.
276*a9fa9459Szrj   unsigned int abbrev_shndx_;
277*a9fa9459Szrj   // The offset within the section of the current abbrev table.
278*a9fa9459Szrj   off_t abbrev_offset_;
279*a9fa9459Szrj   // The buffer containing the .debug_abbrev section.
280*a9fa9459Szrj   const unsigned char* buffer_;
281*a9fa9459Szrj   const unsigned char* buffer_end_;
282*a9fa9459Szrj   // True if this object owns the buffer and needs to delete it.
283*a9fa9459Szrj   bool owns_buffer_;
284*a9fa9459Szrj   // Pointer to the current position in the buffer.
285*a9fa9459Szrj   const unsigned char* buffer_pos_;
286*a9fa9459Szrj   // The table of abbrev codes.
287*a9fa9459Szrj   // We use a direct-lookup array for low abbrev codes,
288*a9fa9459Szrj   // and store the rest in a hash table.
289*a9fa9459Szrj   static const unsigned int low_abbrev_code_max_ = 256;
290*a9fa9459Szrj   const Abbrev_code* low_abbrev_codes_[low_abbrev_code_max_];
291*a9fa9459Szrj   Abbrev_code_table high_abbrev_codes_;
292*a9fa9459Szrj };
293*a9fa9459Szrj 
294*a9fa9459Szrj // A DWARF range list.  The start and end offsets are relative
295*a9fa9459Szrj // to the input section SHNDX.  Each range must lie entirely
296*a9fa9459Szrj // within a single section.
297*a9fa9459Szrj 
298*a9fa9459Szrj class Dwarf_range_list
299*a9fa9459Szrj {
300*a9fa9459Szrj  public:
301*a9fa9459Szrj   struct Range
302*a9fa9459Szrj   {
303*a9fa9459Szrj     Range(unsigned int a_shndx, off_t a_start, off_t a_end)
304*a9fa9459Szrj       : shndx(a_shndx), start(a_start), end(a_end)
305*a9fa9459Szrj     { }
306*a9fa9459Szrj 
307*a9fa9459Szrj     unsigned int shndx;
308*a9fa9459Szrj     off_t start;
309*a9fa9459Szrj     off_t end;
310*a9fa9459Szrj   };
311*a9fa9459Szrj 
312*a9fa9459Szrj   Dwarf_range_list()
313*a9fa9459Szrj     : range_list_()
314*a9fa9459Szrj   { }
315*a9fa9459Szrj 
316*a9fa9459Szrj   void
317*a9fa9459Szrj   add(unsigned int shndx, off_t start, off_t end)
318*a9fa9459Szrj   { this->range_list_.push_back(Range(shndx, start, end)); }
319*a9fa9459Szrj 
320*a9fa9459Szrj   size_t
321*a9fa9459Szrj   size() const
322*a9fa9459Szrj   { return this->range_list_.size(); }
323*a9fa9459Szrj 
324*a9fa9459Szrj   const Range&
325*a9fa9459Szrj   operator[](off_t i) const
326*a9fa9459Szrj   { return this->range_list_[i]; }
327*a9fa9459Szrj 
328*a9fa9459Szrj  private:
329*a9fa9459Szrj   std::vector<Range> range_list_;
330*a9fa9459Szrj };
331*a9fa9459Szrj 
332*a9fa9459Szrj // This class is used to read the ranges table from the
333*a9fa9459Szrj // .debug_ranges section of the object file.
334*a9fa9459Szrj 
335*a9fa9459Szrj class Dwarf_ranges_table
336*a9fa9459Szrj {
337*a9fa9459Szrj  public:
338*a9fa9459Szrj   Dwarf_ranges_table(Dwarf_info_reader* dwinfo)
339*a9fa9459Szrj     : dwinfo_(dwinfo), ranges_shndx_(0), ranges_buffer_(NULL),
340*a9fa9459Szrj       ranges_buffer_end_(NULL), owns_ranges_buffer_(false),
341*a9fa9459Szrj       ranges_reloc_mapper_(NULL), reloc_type_(0), output_section_offset_(0)
342*a9fa9459Szrj   { }
343*a9fa9459Szrj 
344*a9fa9459Szrj   ~Dwarf_ranges_table()
345*a9fa9459Szrj   {
346*a9fa9459Szrj     if (this->owns_ranges_buffer_ && this->ranges_buffer_ != NULL)
347*a9fa9459Szrj       delete[] this->ranges_buffer_;
348*a9fa9459Szrj     if (this->ranges_reloc_mapper_ != NULL)
349*a9fa9459Szrj       delete this->ranges_reloc_mapper_;
350*a9fa9459Szrj   }
351*a9fa9459Szrj 
352*a9fa9459Szrj   // Read the ranges table from an object file.
353*a9fa9459Szrj   bool
354*a9fa9459Szrj   read_ranges_table(Relobj* object,
355*a9fa9459Szrj 		    const unsigned char* symtab,
356*a9fa9459Szrj 		    off_t symtab_size,
357*a9fa9459Szrj 		    unsigned int ranges_shndx);
358*a9fa9459Szrj 
359*a9fa9459Szrj   // Read the range table from an object file.
360*a9fa9459Szrj   Dwarf_range_list*
361*a9fa9459Szrj   read_range_list(Relobj* object,
362*a9fa9459Szrj 		  const unsigned char* symtab,
363*a9fa9459Szrj 		  off_t symtab_size,
364*a9fa9459Szrj 		  unsigned int address_size,
365*a9fa9459Szrj 		  unsigned int ranges_shndx,
366*a9fa9459Szrj 		  off_t ranges_offset);
367*a9fa9459Szrj 
368*a9fa9459Szrj   // Look for a relocation at offset OFF in the range table,
369*a9fa9459Szrj   // and return the section index and offset of the target.
370*a9fa9459Szrj   unsigned int
371*a9fa9459Szrj   lookup_reloc(off_t off, off_t* target_off);
372*a9fa9459Szrj 
373*a9fa9459Szrj  private:
374*a9fa9459Szrj   // The Dwarf_info_reader, for reading data.
375*a9fa9459Szrj   Dwarf_info_reader* dwinfo_;
376*a9fa9459Szrj   // The section index of the ranges table.
377*a9fa9459Szrj   unsigned int ranges_shndx_;
378*a9fa9459Szrj   // The buffer containing the .debug_ranges section.
379*a9fa9459Szrj   const unsigned char* ranges_buffer_;
380*a9fa9459Szrj   const unsigned char* ranges_buffer_end_;
381*a9fa9459Szrj   // True if this object owns the buffer and needs to delete it.
382*a9fa9459Szrj   bool owns_ranges_buffer_;
383*a9fa9459Szrj   // Relocation mapper for the .debug_ranges section.
384*a9fa9459Szrj   Elf_reloc_mapper* ranges_reloc_mapper_;
385*a9fa9459Szrj   // Type of the relocation section (SHT_REL or SHT_RELA).
386*a9fa9459Szrj   unsigned int reloc_type_;
387*a9fa9459Szrj   // For incremental update links, this will hold the offset of the
388*a9fa9459Szrj   // input section within the output section.  Offsets read from
389*a9fa9459Szrj   // relocated data will be relative to the output section, and need
390*a9fa9459Szrj   // to be corrected before reading data from the input section.
391*a9fa9459Szrj   uint64_t output_section_offset_;
392*a9fa9459Szrj };
393*a9fa9459Szrj 
394*a9fa9459Szrj // This class is used to read the pubnames and pubtypes tables from the
395*a9fa9459Szrj // .debug_pubnames and .debug_pubtypes sections of the object file.
396*a9fa9459Szrj 
397*a9fa9459Szrj class Dwarf_pubnames_table
398*a9fa9459Szrj {
399*a9fa9459Szrj  public:
400*a9fa9459Szrj   Dwarf_pubnames_table(Dwarf_info_reader* dwinfo, bool is_pubtypes)
401*a9fa9459Szrj     : dwinfo_(dwinfo), buffer_(NULL), buffer_end_(NULL), owns_buffer_(false),
402*a9fa9459Szrj       offset_size_(0), pinfo_(NULL), end_of_table_(NULL),
403*a9fa9459Szrj       is_pubtypes_(is_pubtypes), is_gnu_style_(false),
404*a9fa9459Szrj       unit_length_(0), cu_offset_(0)
405*a9fa9459Szrj   { }
406*a9fa9459Szrj 
407*a9fa9459Szrj   ~Dwarf_pubnames_table()
408*a9fa9459Szrj   {
409*a9fa9459Szrj     if (this->owns_buffer_ && this->buffer_ != NULL)
410*a9fa9459Szrj       delete[] this->buffer_;
411*a9fa9459Szrj   }
412*a9fa9459Szrj 
413*a9fa9459Szrj   // Read the pubnames section from the object file, using the symbol
414*a9fa9459Szrj   // table for relocating it.
415*a9fa9459Szrj   bool
416*a9fa9459Szrj   read_section(Relobj* object, const unsigned char* symbol_table,
417*a9fa9459Szrj                off_t symtab_size);
418*a9fa9459Szrj 
419*a9fa9459Szrj   // Read the header for the set at OFFSET.
420*a9fa9459Szrj   bool
421*a9fa9459Szrj   read_header(off_t offset);
422*a9fa9459Szrj 
423*a9fa9459Szrj   // Return the offset to the cu within the info or types section.
424*a9fa9459Szrj   off_t
425*a9fa9459Szrj   cu_offset()
426*a9fa9459Szrj   { return this->cu_offset_; }
427*a9fa9459Szrj 
428*a9fa9459Szrj   // Return the size of this subsection of the table.  The unit length
429*a9fa9459Szrj   // doesn't include the size of its own field.
430*a9fa9459Szrj   off_t
431*a9fa9459Szrj   subsection_size()
432*a9fa9459Szrj   { return this->unit_length_; }
433*a9fa9459Szrj 
434*a9fa9459Szrj   // Read the next name from the set.  If the pubname table is gnu-style,
435*a9fa9459Szrj   // FLAG_BYTE is set to the high-byte of a gdb_index version 7 cu_index.
436*a9fa9459Szrj   const char*
437*a9fa9459Szrj   next_name(uint8_t* flag_byte);
438*a9fa9459Szrj 
439*a9fa9459Szrj  private:
440*a9fa9459Szrj   // The Dwarf_info_reader, for reading data.
441*a9fa9459Szrj   Dwarf_info_reader* dwinfo_;
442*a9fa9459Szrj   // The buffer containing the .debug_ranges section.
443*a9fa9459Szrj   const unsigned char* buffer_;
444*a9fa9459Szrj   const unsigned char* buffer_end_;
445*a9fa9459Szrj   // True if this object owns the buffer and needs to delete it.
446*a9fa9459Szrj   bool owns_buffer_;
447*a9fa9459Szrj   // The size of a DWARF offset for the current set.
448*a9fa9459Szrj   unsigned int offset_size_;
449*a9fa9459Szrj   // The current position within the buffer.
450*a9fa9459Szrj   const unsigned char* pinfo_;
451*a9fa9459Szrj   // The end of the current pubnames table.
452*a9fa9459Szrj   const unsigned char* end_of_table_;
453*a9fa9459Szrj   // TRUE if this is a .debug_pubtypes section.
454*a9fa9459Szrj   bool is_pubtypes_;
455*a9fa9459Szrj   // Gnu-style pubnames table. This style has an extra flag byte between the
456*a9fa9459Szrj   // offset and the name, and is used for generating version 7 of gdb-index.
457*a9fa9459Szrj   bool is_gnu_style_;
458*a9fa9459Szrj   // Fields read from the header.
459*a9fa9459Szrj   uint64_t unit_length_;
460*a9fa9459Szrj   off_t cu_offset_;
461*a9fa9459Szrj 
462*a9fa9459Szrj   // Track relocations for this table so we can find the CUs that
463*a9fa9459Szrj   // correspond to the subsections.
464*a9fa9459Szrj   Elf_reloc_mapper* reloc_mapper_;
465*a9fa9459Szrj   // Type of the relocation section (SHT_REL or SHT_RELA).
466*a9fa9459Szrj   unsigned int reloc_type_;
467*a9fa9459Szrj };
468*a9fa9459Szrj 
469*a9fa9459Szrj // This class represents a DWARF Debug Info Entry (DIE).
470*a9fa9459Szrj 
471*a9fa9459Szrj class Dwarf_die
472*a9fa9459Szrj {
473*a9fa9459Szrj  public:
474*a9fa9459Szrj   // An attribute value.
475*a9fa9459Szrj   struct Attribute_value
476*a9fa9459Szrj   {
477*a9fa9459Szrj     unsigned int attr;
478*a9fa9459Szrj     unsigned int form;
479*a9fa9459Szrj     union
480*a9fa9459Szrj     {
481*a9fa9459Szrj       int64_t intval;
482*a9fa9459Szrj       uint64_t uintval;
483*a9fa9459Szrj       const char* stringval;
484*a9fa9459Szrj       const unsigned char* blockval;
485*a9fa9459Szrj       off_t refval;
486*a9fa9459Szrj     } val;
487*a9fa9459Szrj     union
488*a9fa9459Szrj     {
489*a9fa9459Szrj       // Section index for reference forms.
490*a9fa9459Szrj       unsigned int shndx;
491*a9fa9459Szrj       // Block length for block forms.
492*a9fa9459Szrj       unsigned int blocklen;
493*a9fa9459Szrj       // Attribute offset for DW_FORM_strp.
494*a9fa9459Szrj       unsigned int attr_off;
495*a9fa9459Szrj     } aux;
496*a9fa9459Szrj   };
497*a9fa9459Szrj 
498*a9fa9459Szrj   // A list of attribute values.
499*a9fa9459Szrj   typedef std::vector<Attribute_value> Attributes;
500*a9fa9459Szrj 
501*a9fa9459Szrj   Dwarf_die(Dwarf_info_reader* dwinfo,
502*a9fa9459Szrj 	    off_t die_offset,
503*a9fa9459Szrj 	    Dwarf_die* parent);
504*a9fa9459Szrj 
505*a9fa9459Szrj   // Return the DWARF tag for this DIE.
506*a9fa9459Szrj   unsigned int
507*a9fa9459Szrj   tag() const
508*a9fa9459Szrj   {
509*a9fa9459Szrj     if (this->abbrev_code_ == NULL)
510*a9fa9459Szrj       return 0;
511*a9fa9459Szrj     return this->abbrev_code_->tag;
512*a9fa9459Szrj   }
513*a9fa9459Szrj 
514*a9fa9459Szrj   // Return true if this DIE has children.
515*a9fa9459Szrj   bool
516*a9fa9459Szrj   has_children() const
517*a9fa9459Szrj   {
518*a9fa9459Szrj     gold_assert(this->abbrev_code_ != NULL);
519*a9fa9459Szrj     return this->abbrev_code_->has_children;
520*a9fa9459Szrj   }
521*a9fa9459Szrj 
522*a9fa9459Szrj   // Return true if this DIE has a sibling attribute.
523*a9fa9459Szrj   bool
524*a9fa9459Szrj   has_sibling_attribute() const
525*a9fa9459Szrj   {
526*a9fa9459Szrj     gold_assert(this->abbrev_code_ != NULL);
527*a9fa9459Szrj     return this->abbrev_code_->has_sibling_attribute;
528*a9fa9459Szrj   }
529*a9fa9459Szrj 
530*a9fa9459Szrj   // Return the value of attribute ATTR.
531*a9fa9459Szrj   const Attribute_value*
532*a9fa9459Szrj   attribute(unsigned int attr);
533*a9fa9459Szrj 
534*a9fa9459Szrj   // Return the value of the DW_AT_name attribute.
535*a9fa9459Szrj   const char*
536*a9fa9459Szrj   name()
537*a9fa9459Szrj   {
538*a9fa9459Szrj     if (this->name_ == NULL)
539*a9fa9459Szrj       this->set_name();
540*a9fa9459Szrj     return this->name_;
541*a9fa9459Szrj   }
542*a9fa9459Szrj 
543*a9fa9459Szrj   // Return the value of the DW_AT_linkage_name
544*a9fa9459Szrj   // or DW_AT_MIPS_linkage_name attribute.
545*a9fa9459Szrj   const char*
546*a9fa9459Szrj   linkage_name()
547*a9fa9459Szrj   {
548*a9fa9459Szrj     if (this->linkage_name_ == NULL)
549*a9fa9459Szrj       this->set_linkage_name();
550*a9fa9459Szrj     return this->linkage_name_;
551*a9fa9459Szrj   }
552*a9fa9459Szrj 
553*a9fa9459Szrj   // Return the value of the DW_AT_specification attribute.
554*a9fa9459Szrj   off_t
555*a9fa9459Szrj   specification()
556*a9fa9459Szrj   {
557*a9fa9459Szrj     if (!this->attributes_read_)
558*a9fa9459Szrj       this->read_attributes();
559*a9fa9459Szrj     return this->specification_;
560*a9fa9459Szrj   }
561*a9fa9459Szrj 
562*a9fa9459Szrj   // Return the value of the DW_AT_abstract_origin attribute.
563*a9fa9459Szrj   off_t
564*a9fa9459Szrj   abstract_origin()
565*a9fa9459Szrj   {
566*a9fa9459Szrj     if (!this->attributes_read_)
567*a9fa9459Szrj       this->read_attributes();
568*a9fa9459Szrj     return this->abstract_origin_;
569*a9fa9459Szrj   }
570*a9fa9459Szrj 
571*a9fa9459Szrj   // Return the value of attribute ATTR as a string.
572*a9fa9459Szrj   const char*
573*a9fa9459Szrj   string_attribute(unsigned int attr);
574*a9fa9459Szrj 
575*a9fa9459Szrj   // Return the value of attribute ATTR as an integer.
576*a9fa9459Szrj   int64_t
577*a9fa9459Szrj   int_attribute(unsigned int attr);
578*a9fa9459Szrj 
579*a9fa9459Szrj   // Return the value of attribute ATTR as an unsigned integer.
580*a9fa9459Szrj   uint64_t
581*a9fa9459Szrj   uint_attribute(unsigned int attr);
582*a9fa9459Szrj 
583*a9fa9459Szrj   // Return the value of attribute ATTR as a reference.
584*a9fa9459Szrj   off_t
585*a9fa9459Szrj   ref_attribute(unsigned int attr, unsigned int* shndx);
586*a9fa9459Szrj 
587*a9fa9459Szrj   // Return the value of attribute ATTR as a address.
588*a9fa9459Szrj   off_t
589*a9fa9459Szrj   address_attribute(unsigned int attr, unsigned int* shndx);
590*a9fa9459Szrj 
591*a9fa9459Szrj   // Return the value of attribute ATTR as a flag.
592*a9fa9459Szrj   bool
593*a9fa9459Szrj   flag_attribute(unsigned int attr)
594*a9fa9459Szrj   { return this->int_attribute(attr) != 0; }
595*a9fa9459Szrj 
596*a9fa9459Szrj   // Return true if this DIE is a declaration.
597*a9fa9459Szrj   bool
598*a9fa9459Szrj   is_declaration()
599*a9fa9459Szrj   { return this->flag_attribute(elfcpp::DW_AT_declaration); }
600*a9fa9459Szrj 
601*a9fa9459Szrj   // Return the parent of this DIE.
602*a9fa9459Szrj   Dwarf_die*
603*a9fa9459Szrj   parent() const
604*a9fa9459Szrj   { return this->parent_; }
605*a9fa9459Szrj 
606*a9fa9459Szrj   // Return the offset of this DIE.
607*a9fa9459Szrj   off_t
608*a9fa9459Szrj   offset() const
609*a9fa9459Szrj   { return this->die_offset_; }
610*a9fa9459Szrj 
611*a9fa9459Szrj   // Return the offset of this DIE's first child.
612*a9fa9459Szrj   off_t
613*a9fa9459Szrj   child_offset();
614*a9fa9459Szrj 
615*a9fa9459Szrj   // Set the offset of this DIE's next sibling.
616*a9fa9459Szrj   void
617*a9fa9459Szrj   set_sibling_offset(off_t sibling_offset)
618*a9fa9459Szrj   { this->sibling_offset_ = sibling_offset; }
619*a9fa9459Szrj 
620*a9fa9459Szrj   // Return the offset of this DIE's next sibling.
621*a9fa9459Szrj   off_t
622*a9fa9459Szrj   sibling_offset();
623*a9fa9459Szrj 
624*a9fa9459Szrj  private:
625*a9fa9459Szrj   typedef Dwarf_abbrev_table::Abbrev_code Abbrev_code;
626*a9fa9459Szrj 
627*a9fa9459Szrj   // Read all the attributes of the DIE.
628*a9fa9459Szrj   bool
629*a9fa9459Szrj   read_attributes();
630*a9fa9459Szrj 
631*a9fa9459Szrj   // Set the name of the DIE if present.
632*a9fa9459Szrj   void
633*a9fa9459Szrj   set_name();
634*a9fa9459Szrj 
635*a9fa9459Szrj   // Set the linkage name if present.
636*a9fa9459Szrj   void
637*a9fa9459Szrj   set_linkage_name();
638*a9fa9459Szrj 
639*a9fa9459Szrj   // Skip all the attributes of the DIE and return the offset
640*a9fa9459Szrj   // of the next DIE.
641*a9fa9459Szrj   off_t
642*a9fa9459Szrj   skip_attributes();
643*a9fa9459Szrj 
644*a9fa9459Szrj   // The Dwarf_info_reader, for reading attributes.
645*a9fa9459Szrj   Dwarf_info_reader* dwinfo_;
646*a9fa9459Szrj   // The parent of this DIE.
647*a9fa9459Szrj   Dwarf_die* parent_;
648*a9fa9459Szrj   // Offset of this DIE within its compilation unit.
649*a9fa9459Szrj   off_t die_offset_;
650*a9fa9459Szrj   // Offset of the first attribute, relative to the beginning of the DIE.
651*a9fa9459Szrj   off_t attr_offset_;
652*a9fa9459Szrj   // Offset of the first child, relative to the compilation unit.
653*a9fa9459Szrj   off_t child_offset_;
654*a9fa9459Szrj   // Offset of the next sibling, relative to the compilation unit.
655*a9fa9459Szrj   off_t sibling_offset_;
656*a9fa9459Szrj   // The abbreviation table entry.
657*a9fa9459Szrj   const Abbrev_code* abbrev_code_;
658*a9fa9459Szrj   // The list of attributes.
659*a9fa9459Szrj   Attributes attributes_;
660*a9fa9459Szrj   // True if the attributes have been read.
661*a9fa9459Szrj   bool attributes_read_;
662*a9fa9459Szrj   // The following fields hold common attributes to avoid a linear
663*a9fa9459Szrj   // search through the attribute list.
664*a9fa9459Szrj   // The DIE name (DW_AT_name).
665*a9fa9459Szrj   const char* name_;
666*a9fa9459Szrj   // Offset of the name in the string table (for DW_FORM_strp).
667*a9fa9459Szrj   off_t name_off_;
668*a9fa9459Szrj   // The linkage name (DW_AT_linkage_name or DW_AT_MIPS_linkage_name).
669*a9fa9459Szrj   const char* linkage_name_;
670*a9fa9459Szrj   // Offset of the linkage name in the string table (for DW_FORM_strp).
671*a9fa9459Szrj   off_t linkage_name_off_;
672*a9fa9459Szrj   // Section index of the string table (for DW_FORM_strp).
673*a9fa9459Szrj   unsigned int string_shndx_;
674*a9fa9459Szrj   // The value of a DW_AT_specification attribute.
675*a9fa9459Szrj   off_t specification_;
676*a9fa9459Szrj   // The value of a DW_AT_abstract_origin attribute.
677*a9fa9459Szrj   off_t abstract_origin_;
678*a9fa9459Szrj };
679*a9fa9459Szrj 
680*a9fa9459Szrj // This class is used to read the debug info from the .debug_info
681*a9fa9459Szrj // or .debug_types sections.  This is a base class that implements
682*a9fa9459Szrj // the generic parsing of the compilation unit header and DIE
683*a9fa9459Szrj // structure.  The parse() method parses the entire section, and
684*a9fa9459Szrj // calls the various visit_xxx() methods for each header.  Clients
685*a9fa9459Szrj // should derive a new class from this one and implement the
686*a9fa9459Szrj // visit_compilation_unit() and visit_type_unit() functions.
687*a9fa9459Szrj 
688*a9fa9459Szrj class Dwarf_info_reader
689*a9fa9459Szrj {
690*a9fa9459Szrj  public:
691*a9fa9459Szrj   Dwarf_info_reader(bool is_type_unit,
692*a9fa9459Szrj 		    Relobj* object,
693*a9fa9459Szrj 		    const unsigned char* symtab,
694*a9fa9459Szrj 		    off_t symtab_size,
695*a9fa9459Szrj 		    unsigned int shndx,
696*a9fa9459Szrj 		    unsigned int reloc_shndx,
697*a9fa9459Szrj 		    unsigned int reloc_type)
698*a9fa9459Szrj     : is_type_unit_(is_type_unit), object_(object), symtab_(symtab),
699*a9fa9459Szrj       symtab_size_(symtab_size), shndx_(shndx), reloc_shndx_(reloc_shndx),
700*a9fa9459Szrj       reloc_type_(reloc_type), abbrev_shndx_(0), string_shndx_(0),
701*a9fa9459Szrj       buffer_(NULL), buffer_end_(NULL), cu_offset_(0), cu_length_(0),
702*a9fa9459Szrj       offset_size_(0), address_size_(0), cu_version_(0),
703*a9fa9459Szrj       abbrev_table_(), ranges_table_(this),
704*a9fa9459Szrj       reloc_mapper_(NULL), string_buffer_(NULL), string_buffer_end_(NULL),
705*a9fa9459Szrj       owns_string_buffer_(false), string_output_section_offset_(0)
706*a9fa9459Szrj   { }
707*a9fa9459Szrj 
708*a9fa9459Szrj   virtual
709*a9fa9459Szrj   ~Dwarf_info_reader()
710*a9fa9459Szrj   {
711*a9fa9459Szrj     if (this->reloc_mapper_ != NULL)
712*a9fa9459Szrj       delete this->reloc_mapper_;
713*a9fa9459Szrj     if (this->owns_string_buffer_ && this->string_buffer_ != NULL)
714*a9fa9459Szrj       delete[] this->string_buffer_;
715*a9fa9459Szrj   }
716*a9fa9459Szrj 
717*a9fa9459Szrj   // Begin parsing the debug info.  This calls visit_compilation_unit()
718*a9fa9459Szrj   // or visit_type_unit() for each compilation or type unit found in the
719*a9fa9459Szrj   // section, and visit_die() for each top-level DIE.
720*a9fa9459Szrj   void
721*a9fa9459Szrj   parse();
722*a9fa9459Szrj 
723*a9fa9459Szrj   // Return the abbrev code entry for a CODE.
724*a9fa9459Szrj   const Dwarf_abbrev_table::Abbrev_code*
725*a9fa9459Szrj   get_abbrev(unsigned int code)
726*a9fa9459Szrj   { return this->abbrev_table_.get_abbrev(code); }
727*a9fa9459Szrj 
728*a9fa9459Szrj   // Return a pointer to the DWARF info buffer at OFFSET.
729*a9fa9459Szrj   const unsigned char*
730*a9fa9459Szrj   buffer_at_offset(off_t offset) const
731*a9fa9459Szrj   {
732*a9fa9459Szrj     const unsigned char* p = this->buffer_ + this->cu_offset_ + offset;
733*a9fa9459Szrj     if (this->check_buffer(p + 1))
734*a9fa9459Szrj       return p;
735*a9fa9459Szrj     return NULL;
736*a9fa9459Szrj   }
737*a9fa9459Szrj 
738*a9fa9459Szrj   // Read a possibly unaligned integer of SIZE.
739*a9fa9459Szrj   template <int valsize>
740*a9fa9459Szrj   inline typename elfcpp::Valtype_base<valsize>::Valtype
741*a9fa9459Szrj   read_from_pointer(const unsigned char* source);
742*a9fa9459Szrj 
743*a9fa9459Szrj   // Read a possibly unaligned integer of SIZE.  Update SOURCE after read.
744*a9fa9459Szrj   template <int valsize>
745*a9fa9459Szrj   inline typename elfcpp::Valtype_base<valsize>::Valtype
746*a9fa9459Szrj   read_from_pointer(const unsigned char** source);
747*a9fa9459Szrj 
748*a9fa9459Szrj   // Look for a relocation at offset ATTR_OFF in the dwarf info,
749*a9fa9459Szrj   // and return the section index and offset of the target.
750*a9fa9459Szrj   unsigned int
751*a9fa9459Szrj   lookup_reloc(off_t attr_off, off_t* target_off);
752*a9fa9459Szrj 
753*a9fa9459Szrj   // Return a string from the DWARF string table.
754*a9fa9459Szrj   const char*
755*a9fa9459Szrj   get_string(off_t str_off, unsigned int string_shndx);
756*a9fa9459Szrj 
757*a9fa9459Szrj   // Return the size of a DWARF offset.
758*a9fa9459Szrj   unsigned int
759*a9fa9459Szrj   offset_size() const
760*a9fa9459Szrj   { return this->offset_size_; }
761*a9fa9459Szrj 
762*a9fa9459Szrj   // Return the size of an address.
763*a9fa9459Szrj   unsigned int
764*a9fa9459Szrj   address_size() const
765*a9fa9459Szrj   { return this->address_size_; }
766*a9fa9459Szrj 
767*a9fa9459Szrj   // Set the section index of the .debug_abbrev section.
768*a9fa9459Szrj   // We use this if there are no relocations for the .debug_info section.
769*a9fa9459Szrj   // If not set, the code parse() routine will search for the section by name.
770*a9fa9459Szrj   void
771*a9fa9459Szrj   set_abbrev_shndx(unsigned int abbrev_shndx)
772*a9fa9459Szrj   { this->abbrev_shndx_ = abbrev_shndx; }
773*a9fa9459Szrj 
774*a9fa9459Szrj   // Return a pointer to the object file's ELF symbol table.
775*a9fa9459Szrj   const unsigned char*
776*a9fa9459Szrj   symtab() const
777*a9fa9459Szrj   { return this->symtab_; }
778*a9fa9459Szrj 
779*a9fa9459Szrj   // Return the size of the object file's ELF symbol table.
780*a9fa9459Szrj   off_t
781*a9fa9459Szrj   symtab_size() const
782*a9fa9459Szrj   { return this->symtab_size_; }
783*a9fa9459Szrj 
784*a9fa9459Szrj   // Return the offset of the current compilation unit.
785*a9fa9459Szrj   off_t
786*a9fa9459Szrj   cu_offset() const
787*a9fa9459Szrj   { return this->cu_offset_; }
788*a9fa9459Szrj 
789*a9fa9459Szrj  protected:
790*a9fa9459Szrj   // Begin parsing the debug info.  This calls visit_compilation_unit()
791*a9fa9459Szrj   // or visit_type_unit() for each compilation or type unit found in the
792*a9fa9459Szrj   // section, and visit_die() for each top-level DIE.
793*a9fa9459Szrj   template<bool big_endian>
794*a9fa9459Szrj   void
795*a9fa9459Szrj   do_parse();
796*a9fa9459Szrj 
797*a9fa9459Szrj   // The following methods are hooks that are meant to be implemented
798*a9fa9459Szrj   // by a derived class.  A default, do-nothing, implementation of
799*a9fa9459Szrj   // each is provided for this base class.
800*a9fa9459Szrj 
801*a9fa9459Szrj   // Visit a compilation unit.
802*a9fa9459Szrj   virtual void
803*a9fa9459Szrj   visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die* root_die);
804*a9fa9459Szrj 
805*a9fa9459Szrj   // Visit a type unit.
806*a9fa9459Szrj   virtual void
807*a9fa9459Szrj   visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
808*a9fa9459Szrj 		  uint64_t signature, Dwarf_die* root_die);
809*a9fa9459Szrj 
810*a9fa9459Szrj   // Read the range table.
811*a9fa9459Szrj   Dwarf_range_list*
812*a9fa9459Szrj   read_range_list(unsigned int ranges_shndx, off_t ranges_offset)
813*a9fa9459Szrj   {
814*a9fa9459Szrj     return this->ranges_table_.read_range_list(this->object_,
815*a9fa9459Szrj 					       this->symtab_,
816*a9fa9459Szrj 					       this->symtab_size_,
817*a9fa9459Szrj 					       this->address_size_,
818*a9fa9459Szrj 					       ranges_shndx,
819*a9fa9459Szrj 					       ranges_offset);
820*a9fa9459Szrj   }
821*a9fa9459Szrj 
822*a9fa9459Szrj   // Return the object.
823*a9fa9459Szrj   Relobj*
824*a9fa9459Szrj   object() const
825*a9fa9459Szrj   { return this->object_; }
826*a9fa9459Szrj 
827*a9fa9459Szrj   // Checkpoint the relocation tracker.
828*a9fa9459Szrj   uint64_t
829*a9fa9459Szrj   get_reloc_checkpoint() const
830*a9fa9459Szrj   { return this->reloc_mapper_->checkpoint(); }
831*a9fa9459Szrj 
832*a9fa9459Szrj   // Reset the relocation tracker to the CHECKPOINT.
833*a9fa9459Szrj   void
834*a9fa9459Szrj   reset_relocs(uint64_t checkpoint)
835*a9fa9459Szrj   { this->reloc_mapper_->reset(checkpoint); }
836*a9fa9459Szrj 
837*a9fa9459Szrj  private:
838*a9fa9459Szrj   // Print a warning about a corrupt debug section.
839*a9fa9459Szrj   void
840*a9fa9459Szrj   warn_corrupt_debug_section() const;
841*a9fa9459Szrj 
842*a9fa9459Szrj   // Check that P is within the bounds of the current section.
843*a9fa9459Szrj   bool
844*a9fa9459Szrj   check_buffer(const unsigned char* p) const
845*a9fa9459Szrj   {
846*a9fa9459Szrj     if (p > this->buffer_ + this->cu_offset_ + this->cu_length_)
847*a9fa9459Szrj       {
848*a9fa9459Szrj 	this->warn_corrupt_debug_section();
849*a9fa9459Szrj 	return false;
850*a9fa9459Szrj       }
851*a9fa9459Szrj     return true;
852*a9fa9459Szrj   }
853*a9fa9459Szrj 
854*a9fa9459Szrj   // Read the DWARF string table.
855*a9fa9459Szrj   bool
856*a9fa9459Szrj   read_string_table(unsigned int string_shndx)
857*a9fa9459Szrj   {
858*a9fa9459Szrj     // If we've already read this string table, return immediately.
859*a9fa9459Szrj     if (this->string_shndx_ > 0 && this->string_shndx_ == string_shndx)
860*a9fa9459Szrj       return true;
861*a9fa9459Szrj     if (string_shndx == 0 && this->string_shndx_ > 0)
862*a9fa9459Szrj       return true;
863*a9fa9459Szrj     return this->do_read_string_table(string_shndx);
864*a9fa9459Szrj   }
865*a9fa9459Szrj 
866*a9fa9459Szrj   bool
867*a9fa9459Szrj   do_read_string_table(unsigned int string_shndx);
868*a9fa9459Szrj 
869*a9fa9459Szrj   // True if this is a type unit; false for a compilation unit.
870*a9fa9459Szrj   bool is_type_unit_;
871*a9fa9459Szrj   // The object containing the .debug_info or .debug_types input section.
872*a9fa9459Szrj   Relobj* object_;
873*a9fa9459Szrj   // The ELF symbol table.
874*a9fa9459Szrj   const unsigned char* symtab_;
875*a9fa9459Szrj   // The size of the ELF symbol table.
876*a9fa9459Szrj   off_t symtab_size_;
877*a9fa9459Szrj   // Index of the .debug_info or .debug_types section.
878*a9fa9459Szrj   unsigned int shndx_;
879*a9fa9459Szrj   // Index of the relocation section.
880*a9fa9459Szrj   unsigned int reloc_shndx_;
881*a9fa9459Szrj   // Type of the relocation section (SHT_REL or SHT_RELA).
882*a9fa9459Szrj   unsigned int reloc_type_;
883*a9fa9459Szrj   // Index of the .debug_abbrev section (0 if not known).
884*a9fa9459Szrj   unsigned int abbrev_shndx_;
885*a9fa9459Szrj   // Index of the .debug_str section.
886*a9fa9459Szrj   unsigned int string_shndx_;
887*a9fa9459Szrj   // The buffer for the debug info.
888*a9fa9459Szrj   const unsigned char* buffer_;
889*a9fa9459Szrj   const unsigned char* buffer_end_;
890*a9fa9459Szrj   // Offset of the current compilation unit.
891*a9fa9459Szrj   off_t cu_offset_;
892*a9fa9459Szrj   // Length of the current compilation unit.
893*a9fa9459Szrj   off_t cu_length_;
894*a9fa9459Szrj   // Size of a DWARF offset for the current compilation unit.
895*a9fa9459Szrj   unsigned int offset_size_;
896*a9fa9459Szrj   // Size of an address for the target architecture.
897*a9fa9459Szrj   unsigned int address_size_;
898*a9fa9459Szrj   // Compilation unit version number.
899*a9fa9459Szrj   unsigned int cu_version_;
900*a9fa9459Szrj   // Abbreviations table for current compilation unit.
901*a9fa9459Szrj   Dwarf_abbrev_table abbrev_table_;
902*a9fa9459Szrj   // Ranges table for the current compilation unit.
903*a9fa9459Szrj   Dwarf_ranges_table ranges_table_;
904*a9fa9459Szrj   // Relocation mapper for the section.
905*a9fa9459Szrj   Elf_reloc_mapper* reloc_mapper_;
906*a9fa9459Szrj   // The buffer for the debug string table.
907*a9fa9459Szrj   const char* string_buffer_;
908*a9fa9459Szrj   const char* string_buffer_end_;
909*a9fa9459Szrj   // True if this object owns the buffer and needs to delete it.
910*a9fa9459Szrj   bool owns_string_buffer_;
911*a9fa9459Szrj   // For incremental update links, this will hold the offset of the
912*a9fa9459Szrj   // input .debug_str section within the output section.  Offsets read
913*a9fa9459Szrj   // from relocated data will be relative to the output section, and need
914*a9fa9459Szrj   // to be corrected before reading data from the input section.
915*a9fa9459Szrj   uint64_t string_output_section_offset_;
916*a9fa9459Szrj };
917*a9fa9459Szrj 
918*a9fa9459Szrj // We can't do better than to keep the offsets in a sorted vector.
919*a9fa9459Szrj // Here, offset is the key, and file_num/line_num is the value.
920*a9fa9459Szrj struct Offset_to_lineno_entry
921*a9fa9459Szrj {
922*a9fa9459Szrj   off_t offset;
923*a9fa9459Szrj   int header_num;  // which file-list to use (i.e. which .o file are we in)
924*a9fa9459Szrj   // A pointer into files_.
925*a9fa9459Szrj   unsigned int file_num : sizeof(int) * CHAR_BIT - 1;
926*a9fa9459Szrj   // True if this was the last entry for the current offset, meaning
927*a9fa9459Szrj   // it's the line that actually applies.
928*a9fa9459Szrj   unsigned int last_line_for_offset : 1;
929*a9fa9459Szrj   // The line number in the source file.  -1 to indicate end-of-function.
930*a9fa9459Szrj   int line_num;
931*a9fa9459Szrj 
932*a9fa9459Szrj   // This sorts by offsets first, and then puts the correct line to
933*a9fa9459Szrj   // report for a given offset at the beginning of the run of equal
934*a9fa9459Szrj   // offsets (so that asking for 1 line gives the best answer).  This
935*a9fa9459Szrj   // is not a total ordering.
936*a9fa9459Szrj   bool operator<(const Offset_to_lineno_entry& that) const
937*a9fa9459Szrj   {
938*a9fa9459Szrj     if (this->offset != that.offset)
939*a9fa9459Szrj       return this->offset < that.offset;
940*a9fa9459Szrj     // Note the '>' which makes this sort 'true' first.
941*a9fa9459Szrj     return this->last_line_for_offset > that.last_line_for_offset;
942*a9fa9459Szrj   }
943*a9fa9459Szrj };
944*a9fa9459Szrj 
945*a9fa9459Szrj // This class is used to read the line information from the debugging
946*a9fa9459Szrj // section of an object file.
947*a9fa9459Szrj 
948*a9fa9459Szrj class Dwarf_line_info
949*a9fa9459Szrj {
950*a9fa9459Szrj  public:
951*a9fa9459Szrj   Dwarf_line_info()
952*a9fa9459Szrj   { }
953*a9fa9459Szrj 
954*a9fa9459Szrj   virtual
955*a9fa9459Szrj   ~Dwarf_line_info()
956*a9fa9459Szrj   { }
957*a9fa9459Szrj 
958*a9fa9459Szrj   // Given a section number and an offset, returns the associated
959*a9fa9459Szrj   // file and line-number, as a string: "file:lineno".  If unable
960*a9fa9459Szrj   // to do the mapping, returns the empty string.  You must call
961*a9fa9459Szrj   // read_line_mappings() before calling this function.  If
962*a9fa9459Szrj   // 'other_lines' is non-NULL, fills that in with other line
963*a9fa9459Szrj   // numbers assigned to the same offset.
964*a9fa9459Szrj   std::string
965*a9fa9459Szrj   addr2line(unsigned int shndx, off_t offset,
966*a9fa9459Szrj             std::vector<std::string>* other_lines)
967*a9fa9459Szrj   { return this->do_addr2line(shndx, offset, other_lines); }
968*a9fa9459Szrj 
969*a9fa9459Szrj   // A helper function for a single addr2line lookup.  It also keeps a
970*a9fa9459Szrj   // cache of the last CACHE_SIZE Dwarf_line_info objects it created;
971*a9fa9459Szrj   // set to 0 not to cache at all.  The larger CACHE_SIZE is, the more
972*a9fa9459Szrj   // chance this routine won't have to re-create a Dwarf_line_info
973*a9fa9459Szrj   // object for its addr2line computation; such creations are slow.
974*a9fa9459Szrj   // NOTE: Not thread-safe, so only call from one thread at a time.
975*a9fa9459Szrj   static std::string
976*a9fa9459Szrj   one_addr2line(Object* object, unsigned int shndx, off_t offset,
977*a9fa9459Szrj                 size_t cache_size, std::vector<std::string>* other_lines);
978*a9fa9459Szrj 
979*a9fa9459Szrj   // This reclaims all the memory that one_addr2line may have cached.
980*a9fa9459Szrj   // Use this when you know you will not be calling one_addr2line again.
981*a9fa9459Szrj   static void
982*a9fa9459Szrj   clear_addr2line_cache();
983*a9fa9459Szrj 
984*a9fa9459Szrj  private:
985*a9fa9459Szrj   virtual std::string
986*a9fa9459Szrj   do_addr2line(unsigned int shndx, off_t offset,
987*a9fa9459Szrj                std::vector<std::string>* other_lines) = 0;
988*a9fa9459Szrj };
989*a9fa9459Szrj 
990*a9fa9459Szrj template<int size, bool big_endian>
991*a9fa9459Szrj class Sized_dwarf_line_info : public Dwarf_line_info
992*a9fa9459Szrj {
993*a9fa9459Szrj  public:
994*a9fa9459Szrj   // Initializes a .debug_line reader for a given object file.
995*a9fa9459Szrj   // If SHNDX is specified and non-negative, only read the debug
996*a9fa9459Szrj   // information that pertains to the specified section.
997*a9fa9459Szrj   Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U);
998*a9fa9459Szrj 
999*a9fa9459Szrj   virtual
1000*a9fa9459Szrj   ~Sized_dwarf_line_info()
1001*a9fa9459Szrj   {
1002*a9fa9459Szrj     if (this->buffer_start_ != NULL)
1003*a9fa9459Szrj       delete[] this->buffer_start_;
1004*a9fa9459Szrj   }
1005*a9fa9459Szrj 
1006*a9fa9459Szrj  private:
1007*a9fa9459Szrj   std::string
1008*a9fa9459Szrj   do_addr2line(unsigned int shndx, off_t offset,
1009*a9fa9459Szrj                std::vector<std::string>* other_lines);
1010*a9fa9459Szrj 
1011*a9fa9459Szrj   // Formats a file and line number to a string like "dirname/filename:lineno".
1012*a9fa9459Szrj   std::string
1013*a9fa9459Szrj   format_file_lineno(const Offset_to_lineno_entry& lineno) const;
1014*a9fa9459Szrj 
1015*a9fa9459Szrj   // Start processing line info, and populates the offset_map_.
1016*a9fa9459Szrj   // If SHNDX is non-negative, only store debug information that
1017*a9fa9459Szrj   // pertains to the specified section.
1018*a9fa9459Szrj   void
1019*a9fa9459Szrj   read_line_mappings(unsigned int shndx);
1020*a9fa9459Szrj 
1021*a9fa9459Szrj   // Reads the relocation section associated with .debug_line and
1022*a9fa9459Szrj   // stores relocation information in reloc_map_.
1023*a9fa9459Szrj   void
1024*a9fa9459Szrj   read_relocs();
1025*a9fa9459Szrj 
1026*a9fa9459Szrj   // Reads the DWARF2/3 header for this line info.  Each takes as input
1027*a9fa9459Szrj   // a starting buffer position, and returns the ending position.
1028*a9fa9459Szrj   const unsigned char*
1029*a9fa9459Szrj   read_header_prolog(const unsigned char* lineptr);
1030*a9fa9459Szrj 
1031*a9fa9459Szrj   const unsigned char*
1032*a9fa9459Szrj   read_header_tables(const unsigned char* lineptr);
1033*a9fa9459Szrj 
1034*a9fa9459Szrj   // Reads the DWARF2/3 line information.  If shndx is non-negative,
1035*a9fa9459Szrj   // discard all line information that doesn't pertain to the given
1036*a9fa9459Szrj   // section.
1037*a9fa9459Szrj   const unsigned char*
1038*a9fa9459Szrj   read_lines(const unsigned char* lineptr, unsigned int shndx);
1039*a9fa9459Szrj 
1040*a9fa9459Szrj   // Process a single line info opcode at START using the state
1041*a9fa9459Szrj   // machine at LSM.  Return true if we should define a line using the
1042*a9fa9459Szrj   // current state of the line state machine.  Place the length of the
1043*a9fa9459Szrj   // opcode in LEN.
1044*a9fa9459Szrj   bool
1045*a9fa9459Szrj   process_one_opcode(const unsigned char* start,
1046*a9fa9459Szrj                      struct LineStateMachine* lsm, size_t* len);
1047*a9fa9459Szrj 
1048*a9fa9459Szrj   // Some parts of processing differ depending on whether the input
1049*a9fa9459Szrj   // was a .o file or not.
1050*a9fa9459Szrj   bool input_is_relobj();
1051*a9fa9459Szrj 
1052*a9fa9459Szrj   // If we saw anything amiss while parsing, we set this to false.
1053*a9fa9459Szrj   // Then addr2line will always fail (rather than return possibly-
1054*a9fa9459Szrj   // corrupt data).
1055*a9fa9459Szrj   bool data_valid_;
1056*a9fa9459Szrj 
1057*a9fa9459Szrj   // A DWARF2/3 line info header.  This is not the same size as in the
1058*a9fa9459Szrj   // actual file, as the one in the file may have a 32 bit or 64 bit
1059*a9fa9459Szrj   // lengths.
1060*a9fa9459Szrj 
1061*a9fa9459Szrj   struct Dwarf_line_infoHeader
1062*a9fa9459Szrj   {
1063*a9fa9459Szrj     off_t total_length;
1064*a9fa9459Szrj     int version;
1065*a9fa9459Szrj     off_t prologue_length;
1066*a9fa9459Szrj     int min_insn_length; // insn stands for instructin
1067*a9fa9459Szrj     bool default_is_stmt; // stmt stands for statement
1068*a9fa9459Szrj     signed char line_base;
1069*a9fa9459Szrj     int line_range;
1070*a9fa9459Szrj     unsigned char opcode_base;
1071*a9fa9459Szrj     std::vector<unsigned char> std_opcode_lengths;
1072*a9fa9459Szrj     int offset_size;
1073*a9fa9459Szrj   } header_;
1074*a9fa9459Szrj 
1075*a9fa9459Szrj   // buffer is the buffer for our line info, starting at exactly where
1076*a9fa9459Szrj   // the line info to read is.
1077*a9fa9459Szrj   const unsigned char* buffer_;
1078*a9fa9459Szrj   const unsigned char* buffer_end_;
1079*a9fa9459Szrj   // If the buffer was allocated temporarily, and therefore must be
1080*a9fa9459Szrj   // deallocated in the dtor, this contains a pointer to the start
1081*a9fa9459Szrj   // of the buffer.
1082*a9fa9459Szrj   const unsigned char* buffer_start_;
1083*a9fa9459Szrj 
1084*a9fa9459Szrj   // This has relocations that point into buffer.
1085*a9fa9459Szrj   Sized_elf_reloc_mapper<size, big_endian>* reloc_mapper_;
1086*a9fa9459Szrj   // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
1087*a9fa9459Szrj   unsigned int track_relocs_type_;
1088*a9fa9459Szrj 
1089*a9fa9459Szrj   // This is used to figure out what section to apply a relocation to.
1090*a9fa9459Szrj   const unsigned char* symtab_buffer_;
1091*a9fa9459Szrj   section_size_type symtab_buffer_size_;
1092*a9fa9459Szrj 
1093*a9fa9459Szrj   // Holds the directories and files as we see them.  We have an array
1094*a9fa9459Szrj   // of directory-lists, one for each .o file we're reading (usually
1095*a9fa9459Szrj   // there will just be one, but there may be more if input is a .so).
1096*a9fa9459Szrj   std::vector<std::vector<std::string> > directories_;
1097*a9fa9459Szrj   // The first part is an index into directories_, the second the filename.
1098*a9fa9459Szrj   std::vector<std::vector< std::pair<int, std::string> > > files_;
1099*a9fa9459Szrj 
1100*a9fa9459Szrj   // An index into the current directories_ and files_ vectors.
1101*a9fa9459Szrj   int current_header_index_;
1102*a9fa9459Szrj 
1103*a9fa9459Szrj   // A sorted map from offset of the relocation target to the shndx
1104*a9fa9459Szrj   // and addend for the relocation.
1105*a9fa9459Szrj   typedef std::map<off_t, std::pair<unsigned int, off_t> >
1106*a9fa9459Szrj   Reloc_map;
1107*a9fa9459Szrj   Reloc_map reloc_map_;
1108*a9fa9459Szrj 
1109*a9fa9459Szrj   // We have a vector of offset->lineno entries for every input section.
1110*a9fa9459Szrj   typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
1111*a9fa9459Szrj   Lineno_map;
1112*a9fa9459Szrj 
1113*a9fa9459Szrj   Lineno_map line_number_map_;
1114*a9fa9459Szrj };
1115*a9fa9459Szrj 
1116*a9fa9459Szrj } // End namespace gold.
1117*a9fa9459Szrj 
1118*a9fa9459Szrj #endif // !defined(GOLD_DWARF_READER_H)
1119