xref: /dflybsd-src/contrib/gdb-7/bfd/dwarf2.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* DWARF 2 support.
2*5796c8dcSSimon Schubert    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3*5796c8dcSSimon Schubert    2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4*5796c8dcSSimon Schubert 
5*5796c8dcSSimon Schubert    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6*5796c8dcSSimon Schubert    (gavin@cygnus.com).
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    From the dwarf2read.c header:
9*5796c8dcSSimon Schubert    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10*5796c8dcSSimon Schubert    Inc.  with support from Florida State University (under contract
11*5796c8dcSSimon Schubert    with the Ada Joint Program Office), and Silicon Graphics, Inc.
12*5796c8dcSSimon Schubert    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13*5796c8dcSSimon Schubert    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14*5796c8dcSSimon Schubert    support in dwarfread.c
15*5796c8dcSSimon Schubert 
16*5796c8dcSSimon Schubert    This file is part of BFD.
17*5796c8dcSSimon Schubert 
18*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
19*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
20*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or (at
21*5796c8dcSSimon Schubert    your option) any later version.
22*5796c8dcSSimon Schubert 
23*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful, but
24*5796c8dcSSimon Schubert    WITHOUT ANY WARRANTY; without even the implied warranty of
25*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26*5796c8dcSSimon Schubert    General Public License for more details.
27*5796c8dcSSimon Schubert 
28*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
29*5796c8dcSSimon Schubert    along with this program; if not, write to the Free Software
30*5796c8dcSSimon Schubert    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31*5796c8dcSSimon Schubert    MA 02110-1301, USA.  */
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert #include "sysdep.h"
34*5796c8dcSSimon Schubert #include "bfd.h"
35*5796c8dcSSimon Schubert #include "libiberty.h"
36*5796c8dcSSimon Schubert #include "libbfd.h"
37*5796c8dcSSimon Schubert #include "elf-bfd.h"
38*5796c8dcSSimon Schubert #include "dwarf2.h"
39*5796c8dcSSimon Schubert 
40*5796c8dcSSimon Schubert /* The data in the .debug_line statement prologue looks like this.  */
41*5796c8dcSSimon Schubert 
42*5796c8dcSSimon Schubert struct line_head
43*5796c8dcSSimon Schubert {
44*5796c8dcSSimon Schubert   bfd_vma total_length;
45*5796c8dcSSimon Schubert   unsigned short version;
46*5796c8dcSSimon Schubert   bfd_vma prologue_length;
47*5796c8dcSSimon Schubert   unsigned char minimum_instruction_length;
48*5796c8dcSSimon Schubert   unsigned char default_is_stmt;
49*5796c8dcSSimon Schubert   int line_base;
50*5796c8dcSSimon Schubert   unsigned char line_range;
51*5796c8dcSSimon Schubert   unsigned char opcode_base;
52*5796c8dcSSimon Schubert   unsigned char *standard_opcode_lengths;
53*5796c8dcSSimon Schubert };
54*5796c8dcSSimon Schubert 
55*5796c8dcSSimon Schubert /* Attributes have a name and a value.  */
56*5796c8dcSSimon Schubert 
57*5796c8dcSSimon Schubert struct attribute
58*5796c8dcSSimon Schubert {
59*5796c8dcSSimon Schubert   enum dwarf_attribute name;
60*5796c8dcSSimon Schubert   enum dwarf_form form;
61*5796c8dcSSimon Schubert   union
62*5796c8dcSSimon Schubert   {
63*5796c8dcSSimon Schubert     char *str;
64*5796c8dcSSimon Schubert     struct dwarf_block *blk;
65*5796c8dcSSimon Schubert     bfd_uint64_t val;
66*5796c8dcSSimon Schubert     bfd_int64_t sval;
67*5796c8dcSSimon Schubert   }
68*5796c8dcSSimon Schubert   u;
69*5796c8dcSSimon Schubert };
70*5796c8dcSSimon Schubert 
71*5796c8dcSSimon Schubert /* Blocks are a bunch of untyped bytes.  */
72*5796c8dcSSimon Schubert struct dwarf_block
73*5796c8dcSSimon Schubert {
74*5796c8dcSSimon Schubert   unsigned int size;
75*5796c8dcSSimon Schubert   bfd_byte *data;
76*5796c8dcSSimon Schubert };
77*5796c8dcSSimon Schubert 
78*5796c8dcSSimon Schubert struct adjusted_section
79*5796c8dcSSimon Schubert {
80*5796c8dcSSimon Schubert   asection *section;
81*5796c8dcSSimon Schubert   bfd_vma adj_vma;
82*5796c8dcSSimon Schubert };
83*5796c8dcSSimon Schubert 
84*5796c8dcSSimon Schubert struct dwarf2_debug
85*5796c8dcSSimon Schubert {
86*5796c8dcSSimon Schubert   /* A list of all previously read comp_units.  */
87*5796c8dcSSimon Schubert   struct comp_unit *all_comp_units;
88*5796c8dcSSimon Schubert 
89*5796c8dcSSimon Schubert   /* Last comp unit in list above.  */
90*5796c8dcSSimon Schubert   struct comp_unit *last_comp_unit;
91*5796c8dcSSimon Schubert 
92*5796c8dcSSimon Schubert   /* The next unread compilation unit within the .debug_info section.
93*5796c8dcSSimon Schubert      Zero indicates that the .debug_info section has not been loaded
94*5796c8dcSSimon Schubert      into a buffer yet.  */
95*5796c8dcSSimon Schubert   bfd_byte *info_ptr;
96*5796c8dcSSimon Schubert 
97*5796c8dcSSimon Schubert   /* Pointer to the end of the .debug_info section memory buffer.  */
98*5796c8dcSSimon Schubert   bfd_byte *info_ptr_end;
99*5796c8dcSSimon Schubert 
100*5796c8dcSSimon Schubert   /* Pointer to the bfd, section and address of the beginning of the
101*5796c8dcSSimon Schubert      section.  The bfd might be different than expected because of
102*5796c8dcSSimon Schubert      gnu_debuglink sections.  */
103*5796c8dcSSimon Schubert   bfd *bfd_ptr;
104*5796c8dcSSimon Schubert   asection *sec;
105*5796c8dcSSimon Schubert   bfd_byte *sec_info_ptr;
106*5796c8dcSSimon Schubert 
107*5796c8dcSSimon Schubert   /* A pointer to the memory block allocated for info_ptr.  Neither
108*5796c8dcSSimon Schubert      info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
109*5796c8dcSSimon Schubert      beginning of the malloc block.  This is used only to free the
110*5796c8dcSSimon Schubert      memory later.  */
111*5796c8dcSSimon Schubert   bfd_byte *info_ptr_memory;
112*5796c8dcSSimon Schubert 
113*5796c8dcSSimon Schubert   /* Pointer to the symbol table.  */
114*5796c8dcSSimon Schubert   asymbol **syms;
115*5796c8dcSSimon Schubert 
116*5796c8dcSSimon Schubert   /* Pointer to the .debug_abbrev section loaded into memory.  */
117*5796c8dcSSimon Schubert   bfd_byte *dwarf_abbrev_buffer;
118*5796c8dcSSimon Schubert 
119*5796c8dcSSimon Schubert   /* Length of the loaded .debug_abbrev section.  */
120*5796c8dcSSimon Schubert   bfd_size_type dwarf_abbrev_size;
121*5796c8dcSSimon Schubert 
122*5796c8dcSSimon Schubert   /* Buffer for decode_line_info.  */
123*5796c8dcSSimon Schubert   bfd_byte *dwarf_line_buffer;
124*5796c8dcSSimon Schubert 
125*5796c8dcSSimon Schubert   /* Length of the loaded .debug_line section.  */
126*5796c8dcSSimon Schubert   bfd_size_type dwarf_line_size;
127*5796c8dcSSimon Schubert 
128*5796c8dcSSimon Schubert   /* Pointer to the .debug_str section loaded into memory.  */
129*5796c8dcSSimon Schubert   bfd_byte *dwarf_str_buffer;
130*5796c8dcSSimon Schubert 
131*5796c8dcSSimon Schubert   /* Length of the loaded .debug_str section.  */
132*5796c8dcSSimon Schubert   bfd_size_type dwarf_str_size;
133*5796c8dcSSimon Schubert 
134*5796c8dcSSimon Schubert   /* Pointer to the .debug_ranges section loaded into memory. */
135*5796c8dcSSimon Schubert   bfd_byte *dwarf_ranges_buffer;
136*5796c8dcSSimon Schubert 
137*5796c8dcSSimon Schubert   /* Length of the loaded .debug_ranges section. */
138*5796c8dcSSimon Schubert   bfd_size_type dwarf_ranges_size;
139*5796c8dcSSimon Schubert 
140*5796c8dcSSimon Schubert   /* If the most recent call to bfd_find_nearest_line was given an
141*5796c8dcSSimon Schubert      address in an inlined function, preserve a pointer into the
142*5796c8dcSSimon Schubert      calling chain for subsequent calls to bfd_find_inliner_info to
143*5796c8dcSSimon Schubert      use. */
144*5796c8dcSSimon Schubert   struct funcinfo *inliner_chain;
145*5796c8dcSSimon Schubert 
146*5796c8dcSSimon Schubert   /* Number of sections whose VMA we must adjust.  */
147*5796c8dcSSimon Schubert   unsigned int adjusted_section_count;
148*5796c8dcSSimon Schubert 
149*5796c8dcSSimon Schubert   /* Array of sections with adjusted VMA.  */
150*5796c8dcSSimon Schubert   struct adjusted_section *adjusted_sections;
151*5796c8dcSSimon Schubert 
152*5796c8dcSSimon Schubert   /* Number of times find_line is called.  This is used in
153*5796c8dcSSimon Schubert      the heuristic for enabling the info hash tables.  */
154*5796c8dcSSimon Schubert   int info_hash_count;
155*5796c8dcSSimon Schubert 
156*5796c8dcSSimon Schubert #define STASH_INFO_HASH_TRIGGER    100
157*5796c8dcSSimon Schubert 
158*5796c8dcSSimon Schubert   /* Hash table mapping symbol names to function infos.  */
159*5796c8dcSSimon Schubert   struct info_hash_table *funcinfo_hash_table;
160*5796c8dcSSimon Schubert 
161*5796c8dcSSimon Schubert   /* Hash table mapping symbol names to variable infos.  */
162*5796c8dcSSimon Schubert   struct info_hash_table *varinfo_hash_table;
163*5796c8dcSSimon Schubert 
164*5796c8dcSSimon Schubert   /* Head of comp_unit list in the last hash table update.  */
165*5796c8dcSSimon Schubert   struct comp_unit *hash_units_head;
166*5796c8dcSSimon Schubert 
167*5796c8dcSSimon Schubert   /* Status of info hash.  */
168*5796c8dcSSimon Schubert   int info_hash_status;
169*5796c8dcSSimon Schubert #define STASH_INFO_HASH_OFF        0
170*5796c8dcSSimon Schubert #define STASH_INFO_HASH_ON         1
171*5796c8dcSSimon Schubert #define STASH_INFO_HASH_DISABLED   2
172*5796c8dcSSimon Schubert };
173*5796c8dcSSimon Schubert 
174*5796c8dcSSimon Schubert struct arange
175*5796c8dcSSimon Schubert {
176*5796c8dcSSimon Schubert   struct arange *next;
177*5796c8dcSSimon Schubert   bfd_vma low;
178*5796c8dcSSimon Schubert   bfd_vma high;
179*5796c8dcSSimon Schubert };
180*5796c8dcSSimon Schubert 
181*5796c8dcSSimon Schubert /* A minimal decoding of DWARF2 compilation units.  We only decode
182*5796c8dcSSimon Schubert    what's needed to get to the line number information.  */
183*5796c8dcSSimon Schubert 
184*5796c8dcSSimon Schubert struct comp_unit
185*5796c8dcSSimon Schubert {
186*5796c8dcSSimon Schubert   /* Chain the previously read compilation units.  */
187*5796c8dcSSimon Schubert   struct comp_unit *next_unit;
188*5796c8dcSSimon Schubert 
189*5796c8dcSSimon Schubert   /* Likewise, chain the compilation unit read after this one.
190*5796c8dcSSimon Schubert      The comp units are stored in reversed reading order.  */
191*5796c8dcSSimon Schubert   struct comp_unit *prev_unit;
192*5796c8dcSSimon Schubert 
193*5796c8dcSSimon Schubert   /* Keep the bfd convenient (for memory allocation).  */
194*5796c8dcSSimon Schubert   bfd *abfd;
195*5796c8dcSSimon Schubert 
196*5796c8dcSSimon Schubert   /* The lowest and highest addresses contained in this compilation
197*5796c8dcSSimon Schubert      unit as specified in the compilation unit header.  */
198*5796c8dcSSimon Schubert   struct arange arange;
199*5796c8dcSSimon Schubert 
200*5796c8dcSSimon Schubert   /* The DW_AT_name attribute (for error messages).  */
201*5796c8dcSSimon Schubert   char *name;
202*5796c8dcSSimon Schubert 
203*5796c8dcSSimon Schubert   /* The abbrev hash table.  */
204*5796c8dcSSimon Schubert   struct abbrev_info **abbrevs;
205*5796c8dcSSimon Schubert 
206*5796c8dcSSimon Schubert   /* Note that an error was found by comp_unit_find_nearest_line.  */
207*5796c8dcSSimon Schubert   int error;
208*5796c8dcSSimon Schubert 
209*5796c8dcSSimon Schubert   /* The DW_AT_comp_dir attribute.  */
210*5796c8dcSSimon Schubert   char *comp_dir;
211*5796c8dcSSimon Schubert 
212*5796c8dcSSimon Schubert   /* TRUE if there is a line number table associated with this comp. unit.  */
213*5796c8dcSSimon Schubert   int stmtlist;
214*5796c8dcSSimon Schubert 
215*5796c8dcSSimon Schubert   /* Pointer to the current comp_unit so that we can find a given entry
216*5796c8dcSSimon Schubert      by its reference.  */
217*5796c8dcSSimon Schubert   bfd_byte *info_ptr_unit;
218*5796c8dcSSimon Schubert 
219*5796c8dcSSimon Schubert   /* The offset into .debug_line of the line number table.  */
220*5796c8dcSSimon Schubert   unsigned long line_offset;
221*5796c8dcSSimon Schubert 
222*5796c8dcSSimon Schubert   /* Pointer to the first child die for the comp unit.  */
223*5796c8dcSSimon Schubert   bfd_byte *first_child_die_ptr;
224*5796c8dcSSimon Schubert 
225*5796c8dcSSimon Schubert   /* The end of the comp unit.  */
226*5796c8dcSSimon Schubert   bfd_byte *end_ptr;
227*5796c8dcSSimon Schubert 
228*5796c8dcSSimon Schubert   /* The decoded line number, NULL if not yet decoded.  */
229*5796c8dcSSimon Schubert   struct line_info_table *line_table;
230*5796c8dcSSimon Schubert 
231*5796c8dcSSimon Schubert   /* A list of the functions found in this comp. unit.  */
232*5796c8dcSSimon Schubert   struct funcinfo *function_table;
233*5796c8dcSSimon Schubert 
234*5796c8dcSSimon Schubert   /* A list of the variables found in this comp. unit.  */
235*5796c8dcSSimon Schubert   struct varinfo *variable_table;
236*5796c8dcSSimon Schubert 
237*5796c8dcSSimon Schubert   /* Pointer to dwarf2_debug structure.  */
238*5796c8dcSSimon Schubert   struct dwarf2_debug *stash;
239*5796c8dcSSimon Schubert 
240*5796c8dcSSimon Schubert   /* DWARF format version for this unit - from unit header.  */
241*5796c8dcSSimon Schubert   int version;
242*5796c8dcSSimon Schubert 
243*5796c8dcSSimon Schubert   /* Address size for this unit - from unit header.  */
244*5796c8dcSSimon Schubert   unsigned char addr_size;
245*5796c8dcSSimon Schubert 
246*5796c8dcSSimon Schubert   /* Offset size for this unit - from unit header.  */
247*5796c8dcSSimon Schubert   unsigned char offset_size;
248*5796c8dcSSimon Schubert 
249*5796c8dcSSimon Schubert   /* Base address for this unit - from DW_AT_low_pc attribute of
250*5796c8dcSSimon Schubert      DW_TAG_compile_unit DIE */
251*5796c8dcSSimon Schubert   bfd_vma base_address;
252*5796c8dcSSimon Schubert 
253*5796c8dcSSimon Schubert   /* TRUE if symbols are cached in hash table for faster lookup by name.  */
254*5796c8dcSSimon Schubert   bfd_boolean cached;
255*5796c8dcSSimon Schubert };
256*5796c8dcSSimon Schubert 
257*5796c8dcSSimon Schubert /* This data structure holds the information of an abbrev.  */
258*5796c8dcSSimon Schubert struct abbrev_info
259*5796c8dcSSimon Schubert {
260*5796c8dcSSimon Schubert   unsigned int number;		/* Number identifying abbrev.  */
261*5796c8dcSSimon Schubert   enum dwarf_tag tag;		/* DWARF tag.  */
262*5796c8dcSSimon Schubert   int has_children;		/* Boolean.  */
263*5796c8dcSSimon Schubert   unsigned int num_attrs;	/* Number of attributes.  */
264*5796c8dcSSimon Schubert   struct attr_abbrev *attrs;	/* An array of attribute descriptions.  */
265*5796c8dcSSimon Schubert   struct abbrev_info *next;	/* Next in chain.  */
266*5796c8dcSSimon Schubert };
267*5796c8dcSSimon Schubert 
268*5796c8dcSSimon Schubert struct attr_abbrev
269*5796c8dcSSimon Schubert {
270*5796c8dcSSimon Schubert   enum dwarf_attribute name;
271*5796c8dcSSimon Schubert   enum dwarf_form form;
272*5796c8dcSSimon Schubert };
273*5796c8dcSSimon Schubert 
274*5796c8dcSSimon Schubert #ifndef ABBREV_HASH_SIZE
275*5796c8dcSSimon Schubert #define ABBREV_HASH_SIZE 121
276*5796c8dcSSimon Schubert #endif
277*5796c8dcSSimon Schubert #ifndef ATTR_ALLOC_CHUNK
278*5796c8dcSSimon Schubert #define ATTR_ALLOC_CHUNK 4
279*5796c8dcSSimon Schubert #endif
280*5796c8dcSSimon Schubert 
281*5796c8dcSSimon Schubert /* Variable and function hash tables.  This is used to speed up look-up
282*5796c8dcSSimon Schubert    in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
283*5796c8dcSSimon Schubert    In order to share code between variable and function infos, we use
284*5796c8dcSSimon Schubert    a list of untyped pointer for all variable/function info associated with
285*5796c8dcSSimon Schubert    a symbol.  We waste a bit of memory for list with one node but that
286*5796c8dcSSimon Schubert    simplifies the code.  */
287*5796c8dcSSimon Schubert 
288*5796c8dcSSimon Schubert struct info_list_node
289*5796c8dcSSimon Schubert {
290*5796c8dcSSimon Schubert   struct info_list_node *next;
291*5796c8dcSSimon Schubert   void *info;
292*5796c8dcSSimon Schubert };
293*5796c8dcSSimon Schubert 
294*5796c8dcSSimon Schubert /* Info hash entry.  */
295*5796c8dcSSimon Schubert struct info_hash_entry
296*5796c8dcSSimon Schubert {
297*5796c8dcSSimon Schubert   struct bfd_hash_entry root;
298*5796c8dcSSimon Schubert   struct info_list_node *head;
299*5796c8dcSSimon Schubert };
300*5796c8dcSSimon Schubert 
301*5796c8dcSSimon Schubert struct info_hash_table
302*5796c8dcSSimon Schubert {
303*5796c8dcSSimon Schubert   struct bfd_hash_table base;
304*5796c8dcSSimon Schubert };
305*5796c8dcSSimon Schubert 
306*5796c8dcSSimon Schubert /* Function to create a new entry in info hash table. */
307*5796c8dcSSimon Schubert 
308*5796c8dcSSimon Schubert static struct bfd_hash_entry *
309*5796c8dcSSimon Schubert info_hash_table_newfunc (struct bfd_hash_entry *entry,
310*5796c8dcSSimon Schubert 			 struct bfd_hash_table *table,
311*5796c8dcSSimon Schubert 			 const char *string)
312*5796c8dcSSimon Schubert {
313*5796c8dcSSimon Schubert   struct info_hash_entry *ret = (struct info_hash_entry *) entry;
314*5796c8dcSSimon Schubert 
315*5796c8dcSSimon Schubert   /* Allocate the structure if it has not already been allocated by a
316*5796c8dcSSimon Schubert      derived class.  */
317*5796c8dcSSimon Schubert   if (ret == NULL)
318*5796c8dcSSimon Schubert     {
319*5796c8dcSSimon Schubert       ret = (struct info_hash_entry *) bfd_hash_allocate (table,
320*5796c8dcSSimon Schubert                                                           sizeof (* ret));
321*5796c8dcSSimon Schubert       if (ret == NULL)
322*5796c8dcSSimon Schubert 	return NULL;
323*5796c8dcSSimon Schubert     }
324*5796c8dcSSimon Schubert 
325*5796c8dcSSimon Schubert   /* Call the allocation method of the base class.  */
326*5796c8dcSSimon Schubert   ret = ((struct info_hash_entry *)
327*5796c8dcSSimon Schubert 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
328*5796c8dcSSimon Schubert 
329*5796c8dcSSimon Schubert   /* Initialize the local fields here.  */
330*5796c8dcSSimon Schubert   if (ret)
331*5796c8dcSSimon Schubert     ret->head = NULL;
332*5796c8dcSSimon Schubert 
333*5796c8dcSSimon Schubert   return (struct bfd_hash_entry *) ret;
334*5796c8dcSSimon Schubert }
335*5796c8dcSSimon Schubert 
336*5796c8dcSSimon Schubert /* Function to create a new info hash table.  It returns a pointer to the
337*5796c8dcSSimon Schubert    newly created table or NULL if there is any error.  We need abfd
338*5796c8dcSSimon Schubert    solely for memory allocation.  */
339*5796c8dcSSimon Schubert 
340*5796c8dcSSimon Schubert static struct info_hash_table *
341*5796c8dcSSimon Schubert create_info_hash_table (bfd *abfd)
342*5796c8dcSSimon Schubert {
343*5796c8dcSSimon Schubert   struct info_hash_table *hash_table;
344*5796c8dcSSimon Schubert 
345*5796c8dcSSimon Schubert   hash_table = (struct info_hash_table *)
346*5796c8dcSSimon Schubert       bfd_alloc (abfd, sizeof (struct info_hash_table));
347*5796c8dcSSimon Schubert   if (!hash_table)
348*5796c8dcSSimon Schubert     return hash_table;
349*5796c8dcSSimon Schubert 
350*5796c8dcSSimon Schubert   if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
351*5796c8dcSSimon Schubert 			    sizeof (struct info_hash_entry)))
352*5796c8dcSSimon Schubert     {
353*5796c8dcSSimon Schubert       bfd_release (abfd, hash_table);
354*5796c8dcSSimon Schubert       return NULL;
355*5796c8dcSSimon Schubert     }
356*5796c8dcSSimon Schubert 
357*5796c8dcSSimon Schubert   return hash_table;
358*5796c8dcSSimon Schubert }
359*5796c8dcSSimon Schubert 
360*5796c8dcSSimon Schubert /* Insert an info entry into an info hash table.  We do not check of
361*5796c8dcSSimon Schubert    duplicate entries.  Also, the caller need to guarantee that the
362*5796c8dcSSimon Schubert    right type of info in inserted as info is passed as a void* pointer.
363*5796c8dcSSimon Schubert    This function returns true if there is no error.  */
364*5796c8dcSSimon Schubert 
365*5796c8dcSSimon Schubert static bfd_boolean
366*5796c8dcSSimon Schubert insert_info_hash_table (struct info_hash_table *hash_table,
367*5796c8dcSSimon Schubert 			const char *key,
368*5796c8dcSSimon Schubert 			void *info,
369*5796c8dcSSimon Schubert 			bfd_boolean copy_p)
370*5796c8dcSSimon Schubert {
371*5796c8dcSSimon Schubert   struct info_hash_entry *entry;
372*5796c8dcSSimon Schubert   struct info_list_node *node;
373*5796c8dcSSimon Schubert 
374*5796c8dcSSimon Schubert   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
375*5796c8dcSSimon Schubert 						     key, TRUE, copy_p);
376*5796c8dcSSimon Schubert   if (!entry)
377*5796c8dcSSimon Schubert     return FALSE;
378*5796c8dcSSimon Schubert 
379*5796c8dcSSimon Schubert   node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
380*5796c8dcSSimon Schubert                                                       sizeof (*node));
381*5796c8dcSSimon Schubert   if (!node)
382*5796c8dcSSimon Schubert     return FALSE;
383*5796c8dcSSimon Schubert 
384*5796c8dcSSimon Schubert   node->info = info;
385*5796c8dcSSimon Schubert   node->next = entry->head;
386*5796c8dcSSimon Schubert   entry->head = node;
387*5796c8dcSSimon Schubert 
388*5796c8dcSSimon Schubert   return TRUE;
389*5796c8dcSSimon Schubert }
390*5796c8dcSSimon Schubert 
391*5796c8dcSSimon Schubert /* Look up an info entry list from an info hash table.  Return NULL
392*5796c8dcSSimon Schubert    if there is none. */
393*5796c8dcSSimon Schubert 
394*5796c8dcSSimon Schubert static struct info_list_node *
395*5796c8dcSSimon Schubert lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
396*5796c8dcSSimon Schubert {
397*5796c8dcSSimon Schubert   struct info_hash_entry *entry;
398*5796c8dcSSimon Schubert 
399*5796c8dcSSimon Schubert   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
400*5796c8dcSSimon Schubert 						     FALSE, FALSE);
401*5796c8dcSSimon Schubert   return entry ? entry->head : NULL;
402*5796c8dcSSimon Schubert }
403*5796c8dcSSimon Schubert 
404*5796c8dcSSimon Schubert /* Read a section into its appropriate place in the dwarf2_debug
405*5796c8dcSSimon Schubert    struct (indicated by SECTION_BUFFER and SECTION_SIZE).  If SYMS is
406*5796c8dcSSimon Schubert    not NULL, use bfd_simple_get_relocated_section_contents to read the
407*5796c8dcSSimon Schubert    section contents, otherwise use bfd_get_section_contents.  Fail if
408*5796c8dcSSimon Schubert    the located section does not contain at least OFFSET bytes.  */
409*5796c8dcSSimon Schubert 
410*5796c8dcSSimon Schubert static bfd_boolean
411*5796c8dcSSimon Schubert read_section (bfd *           abfd,
412*5796c8dcSSimon Schubert 	      const char *    section_name,
413*5796c8dcSSimon Schubert 	      const char *    compressed_section_name,
414*5796c8dcSSimon Schubert 	      asymbol **      syms,
415*5796c8dcSSimon Schubert 	      bfd_uint64_t    offset,
416*5796c8dcSSimon Schubert 	      bfd_byte **     section_buffer,
417*5796c8dcSSimon Schubert 	      bfd_size_type * section_size)
418*5796c8dcSSimon Schubert {
419*5796c8dcSSimon Schubert   asection *msec;
420*5796c8dcSSimon Schubert   bfd_boolean section_is_compressed = FALSE;
421*5796c8dcSSimon Schubert 
422*5796c8dcSSimon Schubert   /* read_section is a noop if the section has already been read.  */
423*5796c8dcSSimon Schubert   if (!*section_buffer)
424*5796c8dcSSimon Schubert     {
425*5796c8dcSSimon Schubert       msec = bfd_get_section_by_name (abfd, section_name);
426*5796c8dcSSimon Schubert       if (! msec && compressed_section_name)
427*5796c8dcSSimon Schubert 	{
428*5796c8dcSSimon Schubert 	  msec = bfd_get_section_by_name (abfd, compressed_section_name);
429*5796c8dcSSimon Schubert 	  section_is_compressed = TRUE;
430*5796c8dcSSimon Schubert 	}
431*5796c8dcSSimon Schubert       if (! msec)
432*5796c8dcSSimon Schubert 	{
433*5796c8dcSSimon Schubert 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
434*5796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
435*5796c8dcSSimon Schubert 	  return FALSE;
436*5796c8dcSSimon Schubert 	}
437*5796c8dcSSimon Schubert 
438*5796c8dcSSimon Schubert       *section_size = msec->rawsize ? msec->rawsize : msec->size;
439*5796c8dcSSimon Schubert       if (syms)
440*5796c8dcSSimon Schubert 	{
441*5796c8dcSSimon Schubert 	  *section_buffer
442*5796c8dcSSimon Schubert 	      = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
443*5796c8dcSSimon Schubert 	  if (! *section_buffer)
444*5796c8dcSSimon Schubert 	    return FALSE;
445*5796c8dcSSimon Schubert 	}
446*5796c8dcSSimon Schubert       else
447*5796c8dcSSimon Schubert 	{
448*5796c8dcSSimon Schubert 	  *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
449*5796c8dcSSimon Schubert 	  if (! *section_buffer)
450*5796c8dcSSimon Schubert 	    return FALSE;
451*5796c8dcSSimon Schubert 	  if (! bfd_get_section_contents (abfd, msec, *section_buffer,
452*5796c8dcSSimon Schubert 					  0, *section_size))
453*5796c8dcSSimon Schubert 	    return FALSE;
454*5796c8dcSSimon Schubert 	}
455*5796c8dcSSimon Schubert 
456*5796c8dcSSimon Schubert       if (section_is_compressed)
457*5796c8dcSSimon Schubert 	{
458*5796c8dcSSimon Schubert 	  if (! bfd_uncompress_section_contents (section_buffer, section_size))
459*5796c8dcSSimon Schubert 	    {
460*5796c8dcSSimon Schubert 	      (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name);
461*5796c8dcSSimon Schubert 	      bfd_set_error (bfd_error_bad_value);
462*5796c8dcSSimon Schubert 	      return FALSE;
463*5796c8dcSSimon Schubert 	    }
464*5796c8dcSSimon Schubert 	}
465*5796c8dcSSimon Schubert     }
466*5796c8dcSSimon Schubert 
467*5796c8dcSSimon Schubert   /* It is possible to get a bad value for the offset into the section
468*5796c8dcSSimon Schubert      that the client wants.  Validate it here to avoid trouble later.  */
469*5796c8dcSSimon Schubert   if (offset != 0 && offset >= *section_size)
470*5796c8dcSSimon Schubert     {
471*5796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
472*5796c8dcSSimon Schubert 			     (long) offset, section_name, *section_size);
473*5796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
474*5796c8dcSSimon Schubert       return FALSE;
475*5796c8dcSSimon Schubert     }
476*5796c8dcSSimon Schubert 
477*5796c8dcSSimon Schubert   return TRUE;
478*5796c8dcSSimon Schubert }
479*5796c8dcSSimon Schubert 
480*5796c8dcSSimon Schubert /* VERBATIM
481*5796c8dcSSimon Schubert    The following function up to the END VERBATIM mark are
482*5796c8dcSSimon Schubert    copied directly from dwarf2read.c.  */
483*5796c8dcSSimon Schubert 
484*5796c8dcSSimon Schubert /* Read dwarf information from a buffer.  */
485*5796c8dcSSimon Schubert 
486*5796c8dcSSimon Schubert static unsigned int
487*5796c8dcSSimon Schubert read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
488*5796c8dcSSimon Schubert {
489*5796c8dcSSimon Schubert   return bfd_get_8 (abfd, buf);
490*5796c8dcSSimon Schubert }
491*5796c8dcSSimon Schubert 
492*5796c8dcSSimon Schubert static int
493*5796c8dcSSimon Schubert read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
494*5796c8dcSSimon Schubert {
495*5796c8dcSSimon Schubert   return bfd_get_signed_8 (abfd, buf);
496*5796c8dcSSimon Schubert }
497*5796c8dcSSimon Schubert 
498*5796c8dcSSimon Schubert static unsigned int
499*5796c8dcSSimon Schubert read_2_bytes (bfd *abfd, bfd_byte *buf)
500*5796c8dcSSimon Schubert {
501*5796c8dcSSimon Schubert   return bfd_get_16 (abfd, buf);
502*5796c8dcSSimon Schubert }
503*5796c8dcSSimon Schubert 
504*5796c8dcSSimon Schubert static unsigned int
505*5796c8dcSSimon Schubert read_4_bytes (bfd *abfd, bfd_byte *buf)
506*5796c8dcSSimon Schubert {
507*5796c8dcSSimon Schubert   return bfd_get_32 (abfd, buf);
508*5796c8dcSSimon Schubert }
509*5796c8dcSSimon Schubert 
510*5796c8dcSSimon Schubert static bfd_uint64_t
511*5796c8dcSSimon Schubert read_8_bytes (bfd *abfd, bfd_byte *buf)
512*5796c8dcSSimon Schubert {
513*5796c8dcSSimon Schubert   return bfd_get_64 (abfd, buf);
514*5796c8dcSSimon Schubert }
515*5796c8dcSSimon Schubert 
516*5796c8dcSSimon Schubert static bfd_byte *
517*5796c8dcSSimon Schubert read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
518*5796c8dcSSimon Schubert 	      bfd_byte *buf,
519*5796c8dcSSimon Schubert 	      unsigned int size ATTRIBUTE_UNUSED)
520*5796c8dcSSimon Schubert {
521*5796c8dcSSimon Schubert   return buf;
522*5796c8dcSSimon Schubert }
523*5796c8dcSSimon Schubert 
524*5796c8dcSSimon Schubert static char *
525*5796c8dcSSimon Schubert read_string (bfd *abfd ATTRIBUTE_UNUSED,
526*5796c8dcSSimon Schubert 	     bfd_byte *buf,
527*5796c8dcSSimon Schubert 	     unsigned int *bytes_read_ptr)
528*5796c8dcSSimon Schubert {
529*5796c8dcSSimon Schubert   /* Return a pointer to the embedded string.  */
530*5796c8dcSSimon Schubert   char *str = (char *) buf;
531*5796c8dcSSimon Schubert 
532*5796c8dcSSimon Schubert   if (*str == '\0')
533*5796c8dcSSimon Schubert     {
534*5796c8dcSSimon Schubert       *bytes_read_ptr = 1;
535*5796c8dcSSimon Schubert       return NULL;
536*5796c8dcSSimon Schubert     }
537*5796c8dcSSimon Schubert 
538*5796c8dcSSimon Schubert   *bytes_read_ptr = strlen (str) + 1;
539*5796c8dcSSimon Schubert   return str;
540*5796c8dcSSimon Schubert }
541*5796c8dcSSimon Schubert 
542*5796c8dcSSimon Schubert /* END VERBATIM */
543*5796c8dcSSimon Schubert 
544*5796c8dcSSimon Schubert static char *
545*5796c8dcSSimon Schubert read_indirect_string (struct comp_unit * unit,
546*5796c8dcSSimon Schubert 		      bfd_byte *         buf,
547*5796c8dcSSimon Schubert 		      unsigned int *     bytes_read_ptr)
548*5796c8dcSSimon Schubert {
549*5796c8dcSSimon Schubert   bfd_uint64_t offset;
550*5796c8dcSSimon Schubert   struct dwarf2_debug *stash = unit->stash;
551*5796c8dcSSimon Schubert   char *str;
552*5796c8dcSSimon Schubert 
553*5796c8dcSSimon Schubert   if (unit->offset_size == 4)
554*5796c8dcSSimon Schubert     offset = read_4_bytes (unit->abfd, buf);
555*5796c8dcSSimon Schubert   else
556*5796c8dcSSimon Schubert     offset = read_8_bytes (unit->abfd, buf);
557*5796c8dcSSimon Schubert 
558*5796c8dcSSimon Schubert   *bytes_read_ptr = unit->offset_size;
559*5796c8dcSSimon Schubert 
560*5796c8dcSSimon Schubert   if (! read_section (unit->abfd, ".debug_str", ".zdebug_str",
561*5796c8dcSSimon Schubert 		      stash->syms, offset,
562*5796c8dcSSimon Schubert 		      &stash->dwarf_str_buffer, &stash->dwarf_str_size))
563*5796c8dcSSimon Schubert     return NULL;
564*5796c8dcSSimon Schubert 
565*5796c8dcSSimon Schubert   str = (char *) stash->dwarf_str_buffer + offset;
566*5796c8dcSSimon Schubert   if (*str == '\0')
567*5796c8dcSSimon Schubert     return NULL;
568*5796c8dcSSimon Schubert   return str;
569*5796c8dcSSimon Schubert }
570*5796c8dcSSimon Schubert 
571*5796c8dcSSimon Schubert static bfd_uint64_t
572*5796c8dcSSimon Schubert read_address (struct comp_unit *unit, bfd_byte *buf)
573*5796c8dcSSimon Schubert {
574*5796c8dcSSimon Schubert   int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
575*5796c8dcSSimon Schubert 
576*5796c8dcSSimon Schubert   if (signed_vma)
577*5796c8dcSSimon Schubert     {
578*5796c8dcSSimon Schubert       switch (unit->addr_size)
579*5796c8dcSSimon Schubert 	{
580*5796c8dcSSimon Schubert 	case 8:
581*5796c8dcSSimon Schubert 	  return bfd_get_signed_64 (unit->abfd, buf);
582*5796c8dcSSimon Schubert 	case 4:
583*5796c8dcSSimon Schubert 	  return bfd_get_signed_32 (unit->abfd, buf);
584*5796c8dcSSimon Schubert 	case 2:
585*5796c8dcSSimon Schubert 	  return bfd_get_signed_16 (unit->abfd, buf);
586*5796c8dcSSimon Schubert 	default:
587*5796c8dcSSimon Schubert 	  abort ();
588*5796c8dcSSimon Schubert 	}
589*5796c8dcSSimon Schubert     }
590*5796c8dcSSimon Schubert   else
591*5796c8dcSSimon Schubert     {
592*5796c8dcSSimon Schubert       switch (unit->addr_size)
593*5796c8dcSSimon Schubert 	{
594*5796c8dcSSimon Schubert 	case 8:
595*5796c8dcSSimon Schubert 	  return bfd_get_64 (unit->abfd, buf);
596*5796c8dcSSimon Schubert 	case 4:
597*5796c8dcSSimon Schubert 	  return bfd_get_32 (unit->abfd, buf);
598*5796c8dcSSimon Schubert 	case 2:
599*5796c8dcSSimon Schubert 	  return bfd_get_16 (unit->abfd, buf);
600*5796c8dcSSimon Schubert 	default:
601*5796c8dcSSimon Schubert 	  abort ();
602*5796c8dcSSimon Schubert 	}
603*5796c8dcSSimon Schubert     }
604*5796c8dcSSimon Schubert }
605*5796c8dcSSimon Schubert 
606*5796c8dcSSimon Schubert /* Lookup an abbrev_info structure in the abbrev hash table.  */
607*5796c8dcSSimon Schubert 
608*5796c8dcSSimon Schubert static struct abbrev_info *
609*5796c8dcSSimon Schubert lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
610*5796c8dcSSimon Schubert {
611*5796c8dcSSimon Schubert   unsigned int hash_number;
612*5796c8dcSSimon Schubert   struct abbrev_info *abbrev;
613*5796c8dcSSimon Schubert 
614*5796c8dcSSimon Schubert   hash_number = number % ABBREV_HASH_SIZE;
615*5796c8dcSSimon Schubert   abbrev = abbrevs[hash_number];
616*5796c8dcSSimon Schubert 
617*5796c8dcSSimon Schubert   while (abbrev)
618*5796c8dcSSimon Schubert     {
619*5796c8dcSSimon Schubert       if (abbrev->number == number)
620*5796c8dcSSimon Schubert 	return abbrev;
621*5796c8dcSSimon Schubert       else
622*5796c8dcSSimon Schubert 	abbrev = abbrev->next;
623*5796c8dcSSimon Schubert     }
624*5796c8dcSSimon Schubert 
625*5796c8dcSSimon Schubert   return NULL;
626*5796c8dcSSimon Schubert }
627*5796c8dcSSimon Schubert 
628*5796c8dcSSimon Schubert /* In DWARF version 2, the description of the debugging information is
629*5796c8dcSSimon Schubert    stored in a separate .debug_abbrev section.  Before we read any
630*5796c8dcSSimon Schubert    dies from a section we read in all abbreviations and install them
631*5796c8dcSSimon Schubert    in a hash table.  */
632*5796c8dcSSimon Schubert 
633*5796c8dcSSimon Schubert static struct abbrev_info**
634*5796c8dcSSimon Schubert read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
635*5796c8dcSSimon Schubert {
636*5796c8dcSSimon Schubert   struct abbrev_info **abbrevs;
637*5796c8dcSSimon Schubert   bfd_byte *abbrev_ptr;
638*5796c8dcSSimon Schubert   struct abbrev_info *cur_abbrev;
639*5796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, abbrev_name;
640*5796c8dcSSimon Schubert   unsigned int abbrev_form, hash_number;
641*5796c8dcSSimon Schubert   bfd_size_type amt;
642*5796c8dcSSimon Schubert 
643*5796c8dcSSimon Schubert   if (! read_section (abfd, ".debug_abbrev", ".zdebug_abbrev",
644*5796c8dcSSimon Schubert 		      stash->syms, offset,
645*5796c8dcSSimon Schubert 		      &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
646*5796c8dcSSimon Schubert     return 0;
647*5796c8dcSSimon Schubert 
648*5796c8dcSSimon Schubert   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
649*5796c8dcSSimon Schubert   abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
650*5796c8dcSSimon Schubert 
651*5796c8dcSSimon Schubert   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
652*5796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
653*5796c8dcSSimon Schubert   abbrev_ptr += bytes_read;
654*5796c8dcSSimon Schubert 
655*5796c8dcSSimon Schubert   /* Loop until we reach an abbrev number of 0.  */
656*5796c8dcSSimon Schubert   while (abbrev_number)
657*5796c8dcSSimon Schubert     {
658*5796c8dcSSimon Schubert       amt = sizeof (struct abbrev_info);
659*5796c8dcSSimon Schubert       cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
660*5796c8dcSSimon Schubert 
661*5796c8dcSSimon Schubert       /* Read in abbrev header.  */
662*5796c8dcSSimon Schubert       cur_abbrev->number = abbrev_number;
663*5796c8dcSSimon Schubert       cur_abbrev->tag = (enum dwarf_tag)
664*5796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
665*5796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
666*5796c8dcSSimon Schubert       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
667*5796c8dcSSimon Schubert       abbrev_ptr += 1;
668*5796c8dcSSimon Schubert 
669*5796c8dcSSimon Schubert       /* Now read in declarations.  */
670*5796c8dcSSimon Schubert       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
671*5796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
672*5796c8dcSSimon Schubert       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
673*5796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
674*5796c8dcSSimon Schubert 
675*5796c8dcSSimon Schubert       while (abbrev_name)
676*5796c8dcSSimon Schubert 	{
677*5796c8dcSSimon Schubert 	  if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
678*5796c8dcSSimon Schubert 	    {
679*5796c8dcSSimon Schubert 	      struct attr_abbrev *tmp;
680*5796c8dcSSimon Schubert 
681*5796c8dcSSimon Schubert 	      amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
682*5796c8dcSSimon Schubert 	      amt *= sizeof (struct attr_abbrev);
683*5796c8dcSSimon Schubert 	      tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
684*5796c8dcSSimon Schubert 	      if (tmp == NULL)
685*5796c8dcSSimon Schubert 		{
686*5796c8dcSSimon Schubert 		  size_t i;
687*5796c8dcSSimon Schubert 
688*5796c8dcSSimon Schubert 		  for (i = 0; i < ABBREV_HASH_SIZE; i++)
689*5796c8dcSSimon Schubert 		    {
690*5796c8dcSSimon Schubert 		      struct abbrev_info *abbrev = abbrevs[i];
691*5796c8dcSSimon Schubert 
692*5796c8dcSSimon Schubert 		      while (abbrev)
693*5796c8dcSSimon Schubert 			{
694*5796c8dcSSimon Schubert 			  free (abbrev->attrs);
695*5796c8dcSSimon Schubert 			  abbrev = abbrev->next;
696*5796c8dcSSimon Schubert 			}
697*5796c8dcSSimon Schubert 		    }
698*5796c8dcSSimon Schubert 		  return NULL;
699*5796c8dcSSimon Schubert 		}
700*5796c8dcSSimon Schubert 	      cur_abbrev->attrs = tmp;
701*5796c8dcSSimon Schubert 	    }
702*5796c8dcSSimon Schubert 
703*5796c8dcSSimon Schubert 	  cur_abbrev->attrs[cur_abbrev->num_attrs].name
704*5796c8dcSSimon Schubert 	    = (enum dwarf_attribute) abbrev_name;
705*5796c8dcSSimon Schubert 	  cur_abbrev->attrs[cur_abbrev->num_attrs++].form
706*5796c8dcSSimon Schubert 	    = (enum dwarf_form) abbrev_form;
707*5796c8dcSSimon Schubert 	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
708*5796c8dcSSimon Schubert 	  abbrev_ptr += bytes_read;
709*5796c8dcSSimon Schubert 	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
710*5796c8dcSSimon Schubert 	  abbrev_ptr += bytes_read;
711*5796c8dcSSimon Schubert 	}
712*5796c8dcSSimon Schubert 
713*5796c8dcSSimon Schubert       hash_number = abbrev_number % ABBREV_HASH_SIZE;
714*5796c8dcSSimon Schubert       cur_abbrev->next = abbrevs[hash_number];
715*5796c8dcSSimon Schubert       abbrevs[hash_number] = cur_abbrev;
716*5796c8dcSSimon Schubert 
717*5796c8dcSSimon Schubert       /* Get next abbreviation.
718*5796c8dcSSimon Schubert 	 Under Irix6 the abbreviations for a compilation unit are not
719*5796c8dcSSimon Schubert 	 always properly terminated with an abbrev number of 0.
720*5796c8dcSSimon Schubert 	 Exit loop if we encounter an abbreviation which we have
721*5796c8dcSSimon Schubert 	 already read (which means we are about to read the abbreviations
722*5796c8dcSSimon Schubert 	 for the next compile unit) or if the end of the abbreviation
723*5796c8dcSSimon Schubert 	 table is reached.  */
724*5796c8dcSSimon Schubert       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
725*5796c8dcSSimon Schubert 	  >= stash->dwarf_abbrev_size)
726*5796c8dcSSimon Schubert 	break;
727*5796c8dcSSimon Schubert       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
728*5796c8dcSSimon Schubert       abbrev_ptr += bytes_read;
729*5796c8dcSSimon Schubert       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
730*5796c8dcSSimon Schubert 	break;
731*5796c8dcSSimon Schubert     }
732*5796c8dcSSimon Schubert 
733*5796c8dcSSimon Schubert   return abbrevs;
734*5796c8dcSSimon Schubert }
735*5796c8dcSSimon Schubert 
736*5796c8dcSSimon Schubert /* Read an attribute value described by an attribute form.  */
737*5796c8dcSSimon Schubert 
738*5796c8dcSSimon Schubert static bfd_byte *
739*5796c8dcSSimon Schubert read_attribute_value (struct attribute *attr,
740*5796c8dcSSimon Schubert 		      unsigned form,
741*5796c8dcSSimon Schubert 		      struct comp_unit *unit,
742*5796c8dcSSimon Schubert 		      bfd_byte *info_ptr)
743*5796c8dcSSimon Schubert {
744*5796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
745*5796c8dcSSimon Schubert   unsigned int bytes_read;
746*5796c8dcSSimon Schubert   struct dwarf_block *blk;
747*5796c8dcSSimon Schubert   bfd_size_type amt;
748*5796c8dcSSimon Schubert 
749*5796c8dcSSimon Schubert   attr->form = (enum dwarf_form) form;
750*5796c8dcSSimon Schubert 
751*5796c8dcSSimon Schubert   switch (form)
752*5796c8dcSSimon Schubert     {
753*5796c8dcSSimon Schubert     case DW_FORM_ref_addr:
754*5796c8dcSSimon Schubert       /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
755*5796c8dcSSimon Schubert 	 DWARF3.  */
756*5796c8dcSSimon Schubert       if (unit->version == 3)
757*5796c8dcSSimon Schubert 	{
758*5796c8dcSSimon Schubert 	  if (unit->offset_size == 4)
759*5796c8dcSSimon Schubert 	    attr->u.val = read_4_bytes (unit->abfd, info_ptr);
760*5796c8dcSSimon Schubert 	  else
761*5796c8dcSSimon Schubert 	    attr->u.val = read_8_bytes (unit->abfd, info_ptr);
762*5796c8dcSSimon Schubert 	  info_ptr += unit->offset_size;
763*5796c8dcSSimon Schubert 	  break;
764*5796c8dcSSimon Schubert 	}
765*5796c8dcSSimon Schubert       /* FALLTHROUGH */
766*5796c8dcSSimon Schubert     case DW_FORM_addr:
767*5796c8dcSSimon Schubert       attr->u.val = read_address (unit, info_ptr);
768*5796c8dcSSimon Schubert       info_ptr += unit->addr_size;
769*5796c8dcSSimon Schubert       break;
770*5796c8dcSSimon Schubert     case DW_FORM_block2:
771*5796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
772*5796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
773*5796c8dcSSimon Schubert       blk->size = read_2_bytes (abfd, info_ptr);
774*5796c8dcSSimon Schubert       info_ptr += 2;
775*5796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
776*5796c8dcSSimon Schubert       info_ptr += blk->size;
777*5796c8dcSSimon Schubert       attr->u.blk = blk;
778*5796c8dcSSimon Schubert       break;
779*5796c8dcSSimon Schubert     case DW_FORM_block4:
780*5796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
781*5796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
782*5796c8dcSSimon Schubert       blk->size = read_4_bytes (abfd, info_ptr);
783*5796c8dcSSimon Schubert       info_ptr += 4;
784*5796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
785*5796c8dcSSimon Schubert       info_ptr += blk->size;
786*5796c8dcSSimon Schubert       attr->u.blk = blk;
787*5796c8dcSSimon Schubert       break;
788*5796c8dcSSimon Schubert     case DW_FORM_data2:
789*5796c8dcSSimon Schubert       attr->u.val = read_2_bytes (abfd, info_ptr);
790*5796c8dcSSimon Schubert       info_ptr += 2;
791*5796c8dcSSimon Schubert       break;
792*5796c8dcSSimon Schubert     case DW_FORM_data4:
793*5796c8dcSSimon Schubert       attr->u.val = read_4_bytes (abfd, info_ptr);
794*5796c8dcSSimon Schubert       info_ptr += 4;
795*5796c8dcSSimon Schubert       break;
796*5796c8dcSSimon Schubert     case DW_FORM_data8:
797*5796c8dcSSimon Schubert       attr->u.val = read_8_bytes (abfd, info_ptr);
798*5796c8dcSSimon Schubert       info_ptr += 8;
799*5796c8dcSSimon Schubert       break;
800*5796c8dcSSimon Schubert     case DW_FORM_string:
801*5796c8dcSSimon Schubert       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
802*5796c8dcSSimon Schubert       info_ptr += bytes_read;
803*5796c8dcSSimon Schubert       break;
804*5796c8dcSSimon Schubert     case DW_FORM_strp:
805*5796c8dcSSimon Schubert       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
806*5796c8dcSSimon Schubert       info_ptr += bytes_read;
807*5796c8dcSSimon Schubert       break;
808*5796c8dcSSimon Schubert     case DW_FORM_block:
809*5796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
810*5796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
811*5796c8dcSSimon Schubert       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
812*5796c8dcSSimon Schubert       info_ptr += bytes_read;
813*5796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
814*5796c8dcSSimon Schubert       info_ptr += blk->size;
815*5796c8dcSSimon Schubert       attr->u.blk = blk;
816*5796c8dcSSimon Schubert       break;
817*5796c8dcSSimon Schubert     case DW_FORM_block1:
818*5796c8dcSSimon Schubert       amt = sizeof (struct dwarf_block);
819*5796c8dcSSimon Schubert       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
820*5796c8dcSSimon Schubert       blk->size = read_1_byte (abfd, info_ptr);
821*5796c8dcSSimon Schubert       info_ptr += 1;
822*5796c8dcSSimon Schubert       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
823*5796c8dcSSimon Schubert       info_ptr += blk->size;
824*5796c8dcSSimon Schubert       attr->u.blk = blk;
825*5796c8dcSSimon Schubert       break;
826*5796c8dcSSimon Schubert     case DW_FORM_data1:
827*5796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
828*5796c8dcSSimon Schubert       info_ptr += 1;
829*5796c8dcSSimon Schubert       break;
830*5796c8dcSSimon Schubert     case DW_FORM_flag:
831*5796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
832*5796c8dcSSimon Schubert       info_ptr += 1;
833*5796c8dcSSimon Schubert       break;
834*5796c8dcSSimon Schubert     case DW_FORM_sdata:
835*5796c8dcSSimon Schubert       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
836*5796c8dcSSimon Schubert       info_ptr += bytes_read;
837*5796c8dcSSimon Schubert       break;
838*5796c8dcSSimon Schubert     case DW_FORM_udata:
839*5796c8dcSSimon Schubert       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
840*5796c8dcSSimon Schubert       info_ptr += bytes_read;
841*5796c8dcSSimon Schubert       break;
842*5796c8dcSSimon Schubert     case DW_FORM_ref1:
843*5796c8dcSSimon Schubert       attr->u.val = read_1_byte (abfd, info_ptr);
844*5796c8dcSSimon Schubert       info_ptr += 1;
845*5796c8dcSSimon Schubert       break;
846*5796c8dcSSimon Schubert     case DW_FORM_ref2:
847*5796c8dcSSimon Schubert       attr->u.val = read_2_bytes (abfd, info_ptr);
848*5796c8dcSSimon Schubert       info_ptr += 2;
849*5796c8dcSSimon Schubert       break;
850*5796c8dcSSimon Schubert     case DW_FORM_ref4:
851*5796c8dcSSimon Schubert       attr->u.val = read_4_bytes (abfd, info_ptr);
852*5796c8dcSSimon Schubert       info_ptr += 4;
853*5796c8dcSSimon Schubert       break;
854*5796c8dcSSimon Schubert     case DW_FORM_ref8:
855*5796c8dcSSimon Schubert       attr->u.val = read_8_bytes (abfd, info_ptr);
856*5796c8dcSSimon Schubert       info_ptr += 8;
857*5796c8dcSSimon Schubert       break;
858*5796c8dcSSimon Schubert     case DW_FORM_ref_udata:
859*5796c8dcSSimon Schubert       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
860*5796c8dcSSimon Schubert       info_ptr += bytes_read;
861*5796c8dcSSimon Schubert       break;
862*5796c8dcSSimon Schubert     case DW_FORM_indirect:
863*5796c8dcSSimon Schubert       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
864*5796c8dcSSimon Schubert       info_ptr += bytes_read;
865*5796c8dcSSimon Schubert       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
866*5796c8dcSSimon Schubert       break;
867*5796c8dcSSimon Schubert     default:
868*5796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
869*5796c8dcSSimon Schubert 			     form);
870*5796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
871*5796c8dcSSimon Schubert     }
872*5796c8dcSSimon Schubert   return info_ptr;
873*5796c8dcSSimon Schubert }
874*5796c8dcSSimon Schubert 
875*5796c8dcSSimon Schubert /* Read an attribute described by an abbreviated attribute.  */
876*5796c8dcSSimon Schubert 
877*5796c8dcSSimon Schubert static bfd_byte *
878*5796c8dcSSimon Schubert read_attribute (struct attribute *attr,
879*5796c8dcSSimon Schubert 		struct attr_abbrev *abbrev,
880*5796c8dcSSimon Schubert 		struct comp_unit *unit,
881*5796c8dcSSimon Schubert 		bfd_byte *info_ptr)
882*5796c8dcSSimon Schubert {
883*5796c8dcSSimon Schubert   attr->name = abbrev->name;
884*5796c8dcSSimon Schubert   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
885*5796c8dcSSimon Schubert   return info_ptr;
886*5796c8dcSSimon Schubert }
887*5796c8dcSSimon Schubert 
888*5796c8dcSSimon Schubert /* Source line information table routines.  */
889*5796c8dcSSimon Schubert 
890*5796c8dcSSimon Schubert #define FILE_ALLOC_CHUNK 5
891*5796c8dcSSimon Schubert #define DIR_ALLOC_CHUNK 5
892*5796c8dcSSimon Schubert 
893*5796c8dcSSimon Schubert struct line_info
894*5796c8dcSSimon Schubert {
895*5796c8dcSSimon Schubert   struct line_info* prev_line;
896*5796c8dcSSimon Schubert   bfd_vma address;
897*5796c8dcSSimon Schubert   char *filename;
898*5796c8dcSSimon Schubert   unsigned int line;
899*5796c8dcSSimon Schubert   unsigned int column;
900*5796c8dcSSimon Schubert   int end_sequence;		/* End of (sequential) code sequence.  */
901*5796c8dcSSimon Schubert };
902*5796c8dcSSimon Schubert 
903*5796c8dcSSimon Schubert struct fileinfo
904*5796c8dcSSimon Schubert {
905*5796c8dcSSimon Schubert   char *name;
906*5796c8dcSSimon Schubert   unsigned int dir;
907*5796c8dcSSimon Schubert   unsigned int time;
908*5796c8dcSSimon Schubert   unsigned int size;
909*5796c8dcSSimon Schubert };
910*5796c8dcSSimon Schubert 
911*5796c8dcSSimon Schubert struct line_info_table
912*5796c8dcSSimon Schubert {
913*5796c8dcSSimon Schubert   bfd* abfd;
914*5796c8dcSSimon Schubert   unsigned int num_files;
915*5796c8dcSSimon Schubert   unsigned int num_dirs;
916*5796c8dcSSimon Schubert   char *comp_dir;
917*5796c8dcSSimon Schubert   char **dirs;
918*5796c8dcSSimon Schubert   struct fileinfo* files;
919*5796c8dcSSimon Schubert   struct line_info* last_line;  /* largest VMA */
920*5796c8dcSSimon Schubert   struct line_info* lcl_head;   /* local head; used in 'add_line_info' */
921*5796c8dcSSimon Schubert };
922*5796c8dcSSimon Schubert 
923*5796c8dcSSimon Schubert /* Remember some information about each function.  If the function is
924*5796c8dcSSimon Schubert    inlined (DW_TAG_inlined_subroutine) it may have two additional
925*5796c8dcSSimon Schubert    attributes, DW_AT_call_file and DW_AT_call_line, which specify the
926*5796c8dcSSimon Schubert    source code location where this function was inlined. */
927*5796c8dcSSimon Schubert 
928*5796c8dcSSimon Schubert struct funcinfo
929*5796c8dcSSimon Schubert {
930*5796c8dcSSimon Schubert   struct funcinfo *prev_func;		/* Pointer to previous function in list of all functions */
931*5796c8dcSSimon Schubert   struct funcinfo *caller_func;		/* Pointer to function one scope higher */
932*5796c8dcSSimon Schubert   char *caller_file;			/* Source location file name where caller_func inlines this func */
933*5796c8dcSSimon Schubert   int caller_line;			/* Source location line number where caller_func inlines this func */
934*5796c8dcSSimon Schubert   char *file;				/* Source location file name */
935*5796c8dcSSimon Schubert   int line;				/* Source location line number */
936*5796c8dcSSimon Schubert   int tag;
937*5796c8dcSSimon Schubert   char *name;
938*5796c8dcSSimon Schubert   struct arange arange;
939*5796c8dcSSimon Schubert   asection *sec;			/* Where the symbol is defined */
940*5796c8dcSSimon Schubert };
941*5796c8dcSSimon Schubert 
942*5796c8dcSSimon Schubert struct varinfo
943*5796c8dcSSimon Schubert {
944*5796c8dcSSimon Schubert   /* Pointer to previous variable in list of all variables */
945*5796c8dcSSimon Schubert   struct varinfo *prev_var;
946*5796c8dcSSimon Schubert   /* Source location file name */
947*5796c8dcSSimon Schubert   char *file;
948*5796c8dcSSimon Schubert   /* Source location line number */
949*5796c8dcSSimon Schubert   int line;
950*5796c8dcSSimon Schubert   int tag;
951*5796c8dcSSimon Schubert   char *name;
952*5796c8dcSSimon Schubert   bfd_vma addr;
953*5796c8dcSSimon Schubert   /* Where the symbol is defined */
954*5796c8dcSSimon Schubert   asection *sec;
955*5796c8dcSSimon Schubert   /* Is this a stack variable? */
956*5796c8dcSSimon Schubert   unsigned int stack: 1;
957*5796c8dcSSimon Schubert };
958*5796c8dcSSimon Schubert 
959*5796c8dcSSimon Schubert /* Return TRUE if NEW_LINE should sort after LINE.  */
960*5796c8dcSSimon Schubert 
961*5796c8dcSSimon Schubert static inline bfd_boolean
962*5796c8dcSSimon Schubert new_line_sorts_after (struct line_info *new_line, struct line_info *line)
963*5796c8dcSSimon Schubert {
964*5796c8dcSSimon Schubert   return (new_line->address > line->address
965*5796c8dcSSimon Schubert 	  || (new_line->address == line->address
966*5796c8dcSSimon Schubert 	      && new_line->end_sequence < line->end_sequence));
967*5796c8dcSSimon Schubert }
968*5796c8dcSSimon Schubert 
969*5796c8dcSSimon Schubert 
970*5796c8dcSSimon Schubert /* Adds a new entry to the line_info list in the line_info_table, ensuring
971*5796c8dcSSimon Schubert    that the list is sorted.  Note that the line_info list is sorted from
972*5796c8dcSSimon Schubert    highest to lowest VMA (with possible duplicates); that is,
973*5796c8dcSSimon Schubert    line_info->prev_line always accesses an equal or smaller VMA.  */
974*5796c8dcSSimon Schubert 
975*5796c8dcSSimon Schubert static void
976*5796c8dcSSimon Schubert add_line_info (struct line_info_table *table,
977*5796c8dcSSimon Schubert 	       bfd_vma address,
978*5796c8dcSSimon Schubert 	       char *filename,
979*5796c8dcSSimon Schubert 	       unsigned int line,
980*5796c8dcSSimon Schubert 	       unsigned int column,
981*5796c8dcSSimon Schubert 	       int end_sequence)
982*5796c8dcSSimon Schubert {
983*5796c8dcSSimon Schubert   bfd_size_type amt = sizeof (struct line_info);
984*5796c8dcSSimon Schubert   struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
985*5796c8dcSSimon Schubert 
986*5796c8dcSSimon Schubert   /* Set member data of 'info'.  */
987*5796c8dcSSimon Schubert   info->address = address;
988*5796c8dcSSimon Schubert   info->line = line;
989*5796c8dcSSimon Schubert   info->column = column;
990*5796c8dcSSimon Schubert   info->end_sequence = end_sequence;
991*5796c8dcSSimon Schubert 
992*5796c8dcSSimon Schubert   if (filename && filename[0])
993*5796c8dcSSimon Schubert     {
994*5796c8dcSSimon Schubert       info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
995*5796c8dcSSimon Schubert       if (info->filename)
996*5796c8dcSSimon Schubert 	strcpy (info->filename, filename);
997*5796c8dcSSimon Schubert     }
998*5796c8dcSSimon Schubert   else
999*5796c8dcSSimon Schubert     info->filename = NULL;
1000*5796c8dcSSimon Schubert 
1001*5796c8dcSSimon Schubert   /* Find the correct location for 'info'.  Normally we will receive
1002*5796c8dcSSimon Schubert      new line_info data 1) in order and 2) with increasing VMAs.
1003*5796c8dcSSimon Schubert      However some compilers break the rules (cf. decode_line_info) and
1004*5796c8dcSSimon Schubert      so we include some heuristics for quickly finding the correct
1005*5796c8dcSSimon Schubert      location for 'info'. In particular, these heuristics optimize for
1006*5796c8dcSSimon Schubert      the common case in which the VMA sequence that we receive is a
1007*5796c8dcSSimon Schubert      list of locally sorted VMAs such as
1008*5796c8dcSSimon Schubert        p...z a...j  (where a < j < p < z)
1009*5796c8dcSSimon Schubert 
1010*5796c8dcSSimon Schubert      Note: table->lcl_head is used to head an *actual* or *possible*
1011*5796c8dcSSimon Schubert      sequence within the list (such as a...j) that is not directly
1012*5796c8dcSSimon Schubert      headed by table->last_line
1013*5796c8dcSSimon Schubert 
1014*5796c8dcSSimon Schubert      Note: we may receive duplicate entries from 'decode_line_info'.  */
1015*5796c8dcSSimon Schubert 
1016*5796c8dcSSimon Schubert   if (table->last_line
1017*5796c8dcSSimon Schubert       && table->last_line->address == address
1018*5796c8dcSSimon Schubert       && table->last_line->end_sequence == end_sequence)
1019*5796c8dcSSimon Schubert     {
1020*5796c8dcSSimon Schubert       /* We only keep the last entry with the same address and end
1021*5796c8dcSSimon Schubert 	 sequence.  See PR ld/4986.  */
1022*5796c8dcSSimon Schubert       if (table->lcl_head == table->last_line)
1023*5796c8dcSSimon Schubert 	table->lcl_head = info;
1024*5796c8dcSSimon Schubert       info->prev_line = table->last_line->prev_line;
1025*5796c8dcSSimon Schubert       table->last_line = info;
1026*5796c8dcSSimon Schubert     }
1027*5796c8dcSSimon Schubert   else if (!table->last_line
1028*5796c8dcSSimon Schubert       || new_line_sorts_after (info, table->last_line))
1029*5796c8dcSSimon Schubert     {
1030*5796c8dcSSimon Schubert       /* Normal case: add 'info' to the beginning of the list */
1031*5796c8dcSSimon Schubert       info->prev_line = table->last_line;
1032*5796c8dcSSimon Schubert       table->last_line = info;
1033*5796c8dcSSimon Schubert 
1034*5796c8dcSSimon Schubert       /* lcl_head: initialize to head a *possible* sequence at the end.  */
1035*5796c8dcSSimon Schubert       if (!table->lcl_head)
1036*5796c8dcSSimon Schubert 	table->lcl_head = info;
1037*5796c8dcSSimon Schubert     }
1038*5796c8dcSSimon Schubert   else if (!new_line_sorts_after (info, table->lcl_head)
1039*5796c8dcSSimon Schubert 	   && (!table->lcl_head->prev_line
1040*5796c8dcSSimon Schubert 	       || new_line_sorts_after (info, table->lcl_head->prev_line)))
1041*5796c8dcSSimon Schubert     {
1042*5796c8dcSSimon Schubert       /* Abnormal but easy: lcl_head is the head of 'info'.  */
1043*5796c8dcSSimon Schubert       info->prev_line = table->lcl_head->prev_line;
1044*5796c8dcSSimon Schubert       table->lcl_head->prev_line = info;
1045*5796c8dcSSimon Schubert     }
1046*5796c8dcSSimon Schubert   else
1047*5796c8dcSSimon Schubert     {
1048*5796c8dcSSimon Schubert       /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1049*5796c8dcSSimon Schubert 	 heads for 'info'.  Reset 'lcl_head'.  */
1050*5796c8dcSSimon Schubert       struct line_info* li2 = table->last_line; /* always non-NULL */
1051*5796c8dcSSimon Schubert       struct line_info* li1 = li2->prev_line;
1052*5796c8dcSSimon Schubert 
1053*5796c8dcSSimon Schubert       while (li1)
1054*5796c8dcSSimon Schubert 	{
1055*5796c8dcSSimon Schubert 	  if (!new_line_sorts_after (info, li2)
1056*5796c8dcSSimon Schubert 	      && new_line_sorts_after (info, li1))
1057*5796c8dcSSimon Schubert 	    break;
1058*5796c8dcSSimon Schubert 
1059*5796c8dcSSimon Schubert 	  li2 = li1; /* always non-NULL */
1060*5796c8dcSSimon Schubert 	  li1 = li1->prev_line;
1061*5796c8dcSSimon Schubert 	}
1062*5796c8dcSSimon Schubert       table->lcl_head = li2;
1063*5796c8dcSSimon Schubert       info->prev_line = table->lcl_head->prev_line;
1064*5796c8dcSSimon Schubert       table->lcl_head->prev_line = info;
1065*5796c8dcSSimon Schubert     }
1066*5796c8dcSSimon Schubert }
1067*5796c8dcSSimon Schubert 
1068*5796c8dcSSimon Schubert /* Extract a fully qualified filename from a line info table.
1069*5796c8dcSSimon Schubert    The returned string has been malloc'ed and it is the caller's
1070*5796c8dcSSimon Schubert    responsibility to free it.  */
1071*5796c8dcSSimon Schubert 
1072*5796c8dcSSimon Schubert static char *
1073*5796c8dcSSimon Schubert concat_filename (struct line_info_table *table, unsigned int file)
1074*5796c8dcSSimon Schubert {
1075*5796c8dcSSimon Schubert   char *filename;
1076*5796c8dcSSimon Schubert 
1077*5796c8dcSSimon Schubert   if (file - 1 >= table->num_files)
1078*5796c8dcSSimon Schubert     {
1079*5796c8dcSSimon Schubert       /* FILE == 0 means unknown.  */
1080*5796c8dcSSimon Schubert       if (file)
1081*5796c8dcSSimon Schubert 	(*_bfd_error_handler)
1082*5796c8dcSSimon Schubert 	  (_("Dwarf Error: mangled line number section (bad file number)."));
1083*5796c8dcSSimon Schubert       return strdup ("<unknown>");
1084*5796c8dcSSimon Schubert     }
1085*5796c8dcSSimon Schubert 
1086*5796c8dcSSimon Schubert   filename = table->files[file - 1].name;
1087*5796c8dcSSimon Schubert 
1088*5796c8dcSSimon Schubert   if (!IS_ABSOLUTE_PATH (filename))
1089*5796c8dcSSimon Schubert     {
1090*5796c8dcSSimon Schubert       char *dirname = NULL;
1091*5796c8dcSSimon Schubert       char *subdirname = NULL;
1092*5796c8dcSSimon Schubert       char *name;
1093*5796c8dcSSimon Schubert       size_t len;
1094*5796c8dcSSimon Schubert 
1095*5796c8dcSSimon Schubert       if (table->files[file - 1].dir)
1096*5796c8dcSSimon Schubert 	subdirname = table->dirs[table->files[file - 1].dir - 1];
1097*5796c8dcSSimon Schubert 
1098*5796c8dcSSimon Schubert       if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1099*5796c8dcSSimon Schubert 	dirname = table->comp_dir;
1100*5796c8dcSSimon Schubert 
1101*5796c8dcSSimon Schubert       if (!dirname)
1102*5796c8dcSSimon Schubert 	{
1103*5796c8dcSSimon Schubert 	  dirname = subdirname;
1104*5796c8dcSSimon Schubert 	  subdirname = NULL;
1105*5796c8dcSSimon Schubert 	}
1106*5796c8dcSSimon Schubert 
1107*5796c8dcSSimon Schubert       if (!dirname)
1108*5796c8dcSSimon Schubert 	return strdup (filename);
1109*5796c8dcSSimon Schubert 
1110*5796c8dcSSimon Schubert       len = strlen (dirname) + strlen (filename) + 2;
1111*5796c8dcSSimon Schubert 
1112*5796c8dcSSimon Schubert       if (subdirname)
1113*5796c8dcSSimon Schubert 	{
1114*5796c8dcSSimon Schubert 	  len += strlen (subdirname) + 1;
1115*5796c8dcSSimon Schubert 	  name = (char *) bfd_malloc (len);
1116*5796c8dcSSimon Schubert 	  if (name)
1117*5796c8dcSSimon Schubert 	    sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1118*5796c8dcSSimon Schubert 	}
1119*5796c8dcSSimon Schubert       else
1120*5796c8dcSSimon Schubert 	{
1121*5796c8dcSSimon Schubert 	  name = (char *) bfd_malloc (len);
1122*5796c8dcSSimon Schubert 	  if (name)
1123*5796c8dcSSimon Schubert 	    sprintf (name, "%s/%s", dirname, filename);
1124*5796c8dcSSimon Schubert 	}
1125*5796c8dcSSimon Schubert 
1126*5796c8dcSSimon Schubert       return name;
1127*5796c8dcSSimon Schubert     }
1128*5796c8dcSSimon Schubert 
1129*5796c8dcSSimon Schubert   return strdup (filename);
1130*5796c8dcSSimon Schubert }
1131*5796c8dcSSimon Schubert 
1132*5796c8dcSSimon Schubert static void
1133*5796c8dcSSimon Schubert arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
1134*5796c8dcSSimon Schubert {
1135*5796c8dcSSimon Schubert   struct arange *arange;
1136*5796c8dcSSimon Schubert 
1137*5796c8dcSSimon Schubert   /* If the first arange is empty, use it. */
1138*5796c8dcSSimon Schubert   if (first_arange->high == 0)
1139*5796c8dcSSimon Schubert     {
1140*5796c8dcSSimon Schubert       first_arange->low = low_pc;
1141*5796c8dcSSimon Schubert       first_arange->high = high_pc;
1142*5796c8dcSSimon Schubert       return;
1143*5796c8dcSSimon Schubert     }
1144*5796c8dcSSimon Schubert 
1145*5796c8dcSSimon Schubert   /* Next see if we can cheaply extend an existing range.  */
1146*5796c8dcSSimon Schubert   arange = first_arange;
1147*5796c8dcSSimon Schubert   do
1148*5796c8dcSSimon Schubert     {
1149*5796c8dcSSimon Schubert       if (low_pc == arange->high)
1150*5796c8dcSSimon Schubert 	{
1151*5796c8dcSSimon Schubert 	  arange->high = high_pc;
1152*5796c8dcSSimon Schubert 	  return;
1153*5796c8dcSSimon Schubert 	}
1154*5796c8dcSSimon Schubert       if (high_pc == arange->low)
1155*5796c8dcSSimon Schubert 	{
1156*5796c8dcSSimon Schubert 	  arange->low = low_pc;
1157*5796c8dcSSimon Schubert 	  return;
1158*5796c8dcSSimon Schubert 	}
1159*5796c8dcSSimon Schubert       arange = arange->next;
1160*5796c8dcSSimon Schubert     }
1161*5796c8dcSSimon Schubert   while (arange);
1162*5796c8dcSSimon Schubert 
1163*5796c8dcSSimon Schubert   /* Need to allocate a new arange and insert it into the arange list.
1164*5796c8dcSSimon Schubert      Order isn't significant, so just insert after the first arange. */
1165*5796c8dcSSimon Schubert   arange = (struct arange *) bfd_zalloc (abfd, sizeof (*arange));
1166*5796c8dcSSimon Schubert   arange->low = low_pc;
1167*5796c8dcSSimon Schubert   arange->high = high_pc;
1168*5796c8dcSSimon Schubert   arange->next = first_arange->next;
1169*5796c8dcSSimon Schubert   first_arange->next = arange;
1170*5796c8dcSSimon Schubert }
1171*5796c8dcSSimon Schubert 
1172*5796c8dcSSimon Schubert /* Decode the line number information for UNIT.  */
1173*5796c8dcSSimon Schubert 
1174*5796c8dcSSimon Schubert static struct line_info_table*
1175*5796c8dcSSimon Schubert decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1176*5796c8dcSSimon Schubert {
1177*5796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
1178*5796c8dcSSimon Schubert   struct line_info_table* table;
1179*5796c8dcSSimon Schubert   bfd_byte *line_ptr;
1180*5796c8dcSSimon Schubert   bfd_byte *line_end;
1181*5796c8dcSSimon Schubert   struct line_head lh;
1182*5796c8dcSSimon Schubert   unsigned int i, bytes_read, offset_size;
1183*5796c8dcSSimon Schubert   char *cur_file, *cur_dir;
1184*5796c8dcSSimon Schubert   unsigned char op_code, extended_op, adj_opcode;
1185*5796c8dcSSimon Schubert   bfd_size_type amt;
1186*5796c8dcSSimon Schubert 
1187*5796c8dcSSimon Schubert   if (! read_section (abfd, ".debug_line", ".zdebug_line",
1188*5796c8dcSSimon Schubert 		      stash->syms, unit->line_offset,
1189*5796c8dcSSimon Schubert 		      &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1190*5796c8dcSSimon Schubert     return 0;
1191*5796c8dcSSimon Schubert 
1192*5796c8dcSSimon Schubert   amt = sizeof (struct line_info_table);
1193*5796c8dcSSimon Schubert   table = (struct line_info_table *) bfd_alloc (abfd, amt);
1194*5796c8dcSSimon Schubert   table->abfd = abfd;
1195*5796c8dcSSimon Schubert   table->comp_dir = unit->comp_dir;
1196*5796c8dcSSimon Schubert 
1197*5796c8dcSSimon Schubert   table->num_files = 0;
1198*5796c8dcSSimon Schubert   table->files = NULL;
1199*5796c8dcSSimon Schubert 
1200*5796c8dcSSimon Schubert   table->num_dirs = 0;
1201*5796c8dcSSimon Schubert   table->dirs = NULL;
1202*5796c8dcSSimon Schubert 
1203*5796c8dcSSimon Schubert   table->files = NULL;
1204*5796c8dcSSimon Schubert   table->last_line = NULL;
1205*5796c8dcSSimon Schubert   table->lcl_head = NULL;
1206*5796c8dcSSimon Schubert 
1207*5796c8dcSSimon Schubert   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1208*5796c8dcSSimon Schubert 
1209*5796c8dcSSimon Schubert   /* Read in the prologue.  */
1210*5796c8dcSSimon Schubert   lh.total_length = read_4_bytes (abfd, line_ptr);
1211*5796c8dcSSimon Schubert   line_ptr += 4;
1212*5796c8dcSSimon Schubert   offset_size = 4;
1213*5796c8dcSSimon Schubert   if (lh.total_length == 0xffffffff)
1214*5796c8dcSSimon Schubert     {
1215*5796c8dcSSimon Schubert       lh.total_length = read_8_bytes (abfd, line_ptr);
1216*5796c8dcSSimon Schubert       line_ptr += 8;
1217*5796c8dcSSimon Schubert       offset_size = 8;
1218*5796c8dcSSimon Schubert     }
1219*5796c8dcSSimon Schubert   else if (lh.total_length == 0 && unit->addr_size == 8)
1220*5796c8dcSSimon Schubert     {
1221*5796c8dcSSimon Schubert       /* Handle (non-standard) 64-bit DWARF2 formats.  */
1222*5796c8dcSSimon Schubert       lh.total_length = read_4_bytes (abfd, line_ptr);
1223*5796c8dcSSimon Schubert       line_ptr += 4;
1224*5796c8dcSSimon Schubert       offset_size = 8;
1225*5796c8dcSSimon Schubert     }
1226*5796c8dcSSimon Schubert   line_end = line_ptr + lh.total_length;
1227*5796c8dcSSimon Schubert   lh.version = read_2_bytes (abfd, line_ptr);
1228*5796c8dcSSimon Schubert   line_ptr += 2;
1229*5796c8dcSSimon Schubert   if (offset_size == 4)
1230*5796c8dcSSimon Schubert     lh.prologue_length = read_4_bytes (abfd, line_ptr);
1231*5796c8dcSSimon Schubert   else
1232*5796c8dcSSimon Schubert     lh.prologue_length = read_8_bytes (abfd, line_ptr);
1233*5796c8dcSSimon Schubert   line_ptr += offset_size;
1234*5796c8dcSSimon Schubert   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1235*5796c8dcSSimon Schubert   line_ptr += 1;
1236*5796c8dcSSimon Schubert   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1237*5796c8dcSSimon Schubert   line_ptr += 1;
1238*5796c8dcSSimon Schubert   lh.line_base = read_1_signed_byte (abfd, line_ptr);
1239*5796c8dcSSimon Schubert   line_ptr += 1;
1240*5796c8dcSSimon Schubert   lh.line_range = read_1_byte (abfd, line_ptr);
1241*5796c8dcSSimon Schubert   line_ptr += 1;
1242*5796c8dcSSimon Schubert   lh.opcode_base = read_1_byte (abfd, line_ptr);
1243*5796c8dcSSimon Schubert   line_ptr += 1;
1244*5796c8dcSSimon Schubert   amt = lh.opcode_base * sizeof (unsigned char);
1245*5796c8dcSSimon Schubert   lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1246*5796c8dcSSimon Schubert 
1247*5796c8dcSSimon Schubert   lh.standard_opcode_lengths[0] = 1;
1248*5796c8dcSSimon Schubert 
1249*5796c8dcSSimon Schubert   for (i = 1; i < lh.opcode_base; ++i)
1250*5796c8dcSSimon Schubert     {
1251*5796c8dcSSimon Schubert       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1252*5796c8dcSSimon Schubert       line_ptr += 1;
1253*5796c8dcSSimon Schubert     }
1254*5796c8dcSSimon Schubert 
1255*5796c8dcSSimon Schubert   /* Read directory table.  */
1256*5796c8dcSSimon Schubert   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1257*5796c8dcSSimon Schubert     {
1258*5796c8dcSSimon Schubert       line_ptr += bytes_read;
1259*5796c8dcSSimon Schubert 
1260*5796c8dcSSimon Schubert       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1261*5796c8dcSSimon Schubert 	{
1262*5796c8dcSSimon Schubert 	  char **tmp;
1263*5796c8dcSSimon Schubert 
1264*5796c8dcSSimon Schubert 	  amt = table->num_dirs + DIR_ALLOC_CHUNK;
1265*5796c8dcSSimon Schubert 	  amt *= sizeof (char *);
1266*5796c8dcSSimon Schubert 
1267*5796c8dcSSimon Schubert 	  tmp = (char **) bfd_realloc (table->dirs, amt);
1268*5796c8dcSSimon Schubert 	  if (tmp == NULL)
1269*5796c8dcSSimon Schubert 	    {
1270*5796c8dcSSimon Schubert 	      free (table->dirs);
1271*5796c8dcSSimon Schubert 	      return NULL;
1272*5796c8dcSSimon Schubert 	    }
1273*5796c8dcSSimon Schubert 	  table->dirs = tmp;
1274*5796c8dcSSimon Schubert 	}
1275*5796c8dcSSimon Schubert 
1276*5796c8dcSSimon Schubert       table->dirs[table->num_dirs++] = cur_dir;
1277*5796c8dcSSimon Schubert     }
1278*5796c8dcSSimon Schubert 
1279*5796c8dcSSimon Schubert   line_ptr += bytes_read;
1280*5796c8dcSSimon Schubert 
1281*5796c8dcSSimon Schubert   /* Read file name table.  */
1282*5796c8dcSSimon Schubert   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1283*5796c8dcSSimon Schubert     {
1284*5796c8dcSSimon Schubert       line_ptr += bytes_read;
1285*5796c8dcSSimon Schubert 
1286*5796c8dcSSimon Schubert       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1287*5796c8dcSSimon Schubert 	{
1288*5796c8dcSSimon Schubert 	  struct fileinfo *tmp;
1289*5796c8dcSSimon Schubert 
1290*5796c8dcSSimon Schubert 	  amt = table->num_files + FILE_ALLOC_CHUNK;
1291*5796c8dcSSimon Schubert 	  amt *= sizeof (struct fileinfo);
1292*5796c8dcSSimon Schubert 
1293*5796c8dcSSimon Schubert 	  tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1294*5796c8dcSSimon Schubert 	  if (tmp == NULL)
1295*5796c8dcSSimon Schubert 	    {
1296*5796c8dcSSimon Schubert 	      free (table->files);
1297*5796c8dcSSimon Schubert 	      free (table->dirs);
1298*5796c8dcSSimon Schubert 	      return NULL;
1299*5796c8dcSSimon Schubert 	    }
1300*5796c8dcSSimon Schubert 	  table->files = tmp;
1301*5796c8dcSSimon Schubert 	}
1302*5796c8dcSSimon Schubert 
1303*5796c8dcSSimon Schubert       table->files[table->num_files].name = cur_file;
1304*5796c8dcSSimon Schubert       table->files[table->num_files].dir =
1305*5796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1306*5796c8dcSSimon Schubert       line_ptr += bytes_read;
1307*5796c8dcSSimon Schubert       table->files[table->num_files].time =
1308*5796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1309*5796c8dcSSimon Schubert       line_ptr += bytes_read;
1310*5796c8dcSSimon Schubert       table->files[table->num_files].size =
1311*5796c8dcSSimon Schubert 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1312*5796c8dcSSimon Schubert       line_ptr += bytes_read;
1313*5796c8dcSSimon Schubert       table->num_files++;
1314*5796c8dcSSimon Schubert     }
1315*5796c8dcSSimon Schubert 
1316*5796c8dcSSimon Schubert   line_ptr += bytes_read;
1317*5796c8dcSSimon Schubert 
1318*5796c8dcSSimon Schubert   /* Read the statement sequences until there's nothing left.  */
1319*5796c8dcSSimon Schubert   while (line_ptr < line_end)
1320*5796c8dcSSimon Schubert     {
1321*5796c8dcSSimon Schubert       /* State machine registers.  */
1322*5796c8dcSSimon Schubert       bfd_vma address = 0;
1323*5796c8dcSSimon Schubert       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1324*5796c8dcSSimon Schubert       unsigned int line = 1;
1325*5796c8dcSSimon Schubert       unsigned int column = 0;
1326*5796c8dcSSimon Schubert       int is_stmt = lh.default_is_stmt;
1327*5796c8dcSSimon Schubert       int end_sequence = 0;
1328*5796c8dcSSimon Schubert       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1329*5796c8dcSSimon Schubert 	 compilers generate address sequences that are wildly out of
1330*5796c8dcSSimon Schubert 	 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1331*5796c8dcSSimon Schubert 	 for ia64-Linux).  Thus, to determine the low and high
1332*5796c8dcSSimon Schubert 	 address, we must compare on every DW_LNS_copy, etc.  */
1333*5796c8dcSSimon Schubert       bfd_vma low_pc  = (bfd_vma) -1;
1334*5796c8dcSSimon Schubert       bfd_vma high_pc = 0;
1335*5796c8dcSSimon Schubert 
1336*5796c8dcSSimon Schubert       /* Decode the table.  */
1337*5796c8dcSSimon Schubert       while (! end_sequence)
1338*5796c8dcSSimon Schubert 	{
1339*5796c8dcSSimon Schubert 	  op_code = read_1_byte (abfd, line_ptr);
1340*5796c8dcSSimon Schubert 	  line_ptr += 1;
1341*5796c8dcSSimon Schubert 
1342*5796c8dcSSimon Schubert 	  if (op_code >= lh.opcode_base)
1343*5796c8dcSSimon Schubert 	    {
1344*5796c8dcSSimon Schubert 	      /* Special operand.  */
1345*5796c8dcSSimon Schubert 	      adj_opcode = op_code - lh.opcode_base;
1346*5796c8dcSSimon Schubert 	      address += (adj_opcode / lh.line_range)
1347*5796c8dcSSimon Schubert 		* lh.minimum_instruction_length;
1348*5796c8dcSSimon Schubert 	      line += lh.line_base + (adj_opcode % lh.line_range);
1349*5796c8dcSSimon Schubert 	      /* Append row to matrix using current values.  */
1350*5796c8dcSSimon Schubert 	      add_line_info (table, address, filename, line, column, 0);
1351*5796c8dcSSimon Schubert 	      if (address < low_pc)
1352*5796c8dcSSimon Schubert 		low_pc = address;
1353*5796c8dcSSimon Schubert 	      if (address > high_pc)
1354*5796c8dcSSimon Schubert 		high_pc = address;
1355*5796c8dcSSimon Schubert 	    }
1356*5796c8dcSSimon Schubert 	  else switch (op_code)
1357*5796c8dcSSimon Schubert 	    {
1358*5796c8dcSSimon Schubert 	    case DW_LNS_extended_op:
1359*5796c8dcSSimon Schubert 	      /* Ignore length.  */
1360*5796c8dcSSimon Schubert 	      line_ptr += 1;
1361*5796c8dcSSimon Schubert 	      extended_op = read_1_byte (abfd, line_ptr);
1362*5796c8dcSSimon Schubert 	      line_ptr += 1;
1363*5796c8dcSSimon Schubert 
1364*5796c8dcSSimon Schubert 	      switch (extended_op)
1365*5796c8dcSSimon Schubert 		{
1366*5796c8dcSSimon Schubert 		case DW_LNE_end_sequence:
1367*5796c8dcSSimon Schubert 		  end_sequence = 1;
1368*5796c8dcSSimon Schubert 		  add_line_info (table, address, filename, line, column,
1369*5796c8dcSSimon Schubert 				 end_sequence);
1370*5796c8dcSSimon Schubert 		  if (address < low_pc)
1371*5796c8dcSSimon Schubert 		    low_pc = address;
1372*5796c8dcSSimon Schubert 		  if (address > high_pc)
1373*5796c8dcSSimon Schubert 		    high_pc = address;
1374*5796c8dcSSimon Schubert 		  arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1375*5796c8dcSSimon Schubert 		  break;
1376*5796c8dcSSimon Schubert 		case DW_LNE_set_address:
1377*5796c8dcSSimon Schubert 		  address = read_address (unit, line_ptr);
1378*5796c8dcSSimon Schubert 		  line_ptr += unit->addr_size;
1379*5796c8dcSSimon Schubert 		  break;
1380*5796c8dcSSimon Schubert 		case DW_LNE_define_file:
1381*5796c8dcSSimon Schubert 		  cur_file = read_string (abfd, line_ptr, &bytes_read);
1382*5796c8dcSSimon Schubert 		  line_ptr += bytes_read;
1383*5796c8dcSSimon Schubert 		  if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1384*5796c8dcSSimon Schubert 		    {
1385*5796c8dcSSimon Schubert 		      struct fileinfo *tmp;
1386*5796c8dcSSimon Schubert 
1387*5796c8dcSSimon Schubert 		      amt = table->num_files + FILE_ALLOC_CHUNK;
1388*5796c8dcSSimon Schubert 		      amt *= sizeof (struct fileinfo);
1389*5796c8dcSSimon Schubert 		      tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1390*5796c8dcSSimon Schubert 		      if (tmp == NULL)
1391*5796c8dcSSimon Schubert 			{
1392*5796c8dcSSimon Schubert 			  free (table->files);
1393*5796c8dcSSimon Schubert 			  free (table->dirs);
1394*5796c8dcSSimon Schubert 			  free (filename);
1395*5796c8dcSSimon Schubert 			  return NULL;
1396*5796c8dcSSimon Schubert 			}
1397*5796c8dcSSimon Schubert 		      table->files = tmp;
1398*5796c8dcSSimon Schubert 		    }
1399*5796c8dcSSimon Schubert 		  table->files[table->num_files].name = cur_file;
1400*5796c8dcSSimon Schubert 		  table->files[table->num_files].dir =
1401*5796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1402*5796c8dcSSimon Schubert 		  line_ptr += bytes_read;
1403*5796c8dcSSimon Schubert 		  table->files[table->num_files].time =
1404*5796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1405*5796c8dcSSimon Schubert 		  line_ptr += bytes_read;
1406*5796c8dcSSimon Schubert 		  table->files[table->num_files].size =
1407*5796c8dcSSimon Schubert 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1408*5796c8dcSSimon Schubert 		  line_ptr += bytes_read;
1409*5796c8dcSSimon Schubert 		  table->num_files++;
1410*5796c8dcSSimon Schubert 		  break;
1411*5796c8dcSSimon Schubert 		case DW_LNE_set_discriminator:
1412*5796c8dcSSimon Schubert 		  (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1413*5796c8dcSSimon Schubert 		  line_ptr += bytes_read;
1414*5796c8dcSSimon Schubert 		  break;
1415*5796c8dcSSimon Schubert 		default:
1416*5796c8dcSSimon Schubert 		  (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1417*5796c8dcSSimon Schubert 		  bfd_set_error (bfd_error_bad_value);
1418*5796c8dcSSimon Schubert 		  free (filename);
1419*5796c8dcSSimon Schubert 		  free (table->files);
1420*5796c8dcSSimon Schubert 		  free (table->dirs);
1421*5796c8dcSSimon Schubert 		  return NULL;
1422*5796c8dcSSimon Schubert 		}
1423*5796c8dcSSimon Schubert 	      break;
1424*5796c8dcSSimon Schubert 	    case DW_LNS_copy:
1425*5796c8dcSSimon Schubert 	      add_line_info (table, address, filename, line, column, 0);
1426*5796c8dcSSimon Schubert 	      if (address < low_pc)
1427*5796c8dcSSimon Schubert 		low_pc = address;
1428*5796c8dcSSimon Schubert 	      if (address > high_pc)
1429*5796c8dcSSimon Schubert 		high_pc = address;
1430*5796c8dcSSimon Schubert 	      break;
1431*5796c8dcSSimon Schubert 	    case DW_LNS_advance_pc:
1432*5796c8dcSSimon Schubert 	      address += lh.minimum_instruction_length
1433*5796c8dcSSimon Schubert 		* read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1434*5796c8dcSSimon Schubert 	      line_ptr += bytes_read;
1435*5796c8dcSSimon Schubert 	      break;
1436*5796c8dcSSimon Schubert 	    case DW_LNS_advance_line:
1437*5796c8dcSSimon Schubert 	      line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1438*5796c8dcSSimon Schubert 	      line_ptr += bytes_read;
1439*5796c8dcSSimon Schubert 	      break;
1440*5796c8dcSSimon Schubert 	    case DW_LNS_set_file:
1441*5796c8dcSSimon Schubert 	      {
1442*5796c8dcSSimon Schubert 		unsigned int file;
1443*5796c8dcSSimon Schubert 
1444*5796c8dcSSimon Schubert 		/* The file and directory tables are 0
1445*5796c8dcSSimon Schubert 		   based, the references are 1 based.  */
1446*5796c8dcSSimon Schubert 		file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1447*5796c8dcSSimon Schubert 		line_ptr += bytes_read;
1448*5796c8dcSSimon Schubert 		if (filename)
1449*5796c8dcSSimon Schubert 		  free (filename);
1450*5796c8dcSSimon Schubert 		filename = concat_filename (table, file);
1451*5796c8dcSSimon Schubert 		break;
1452*5796c8dcSSimon Schubert 	      }
1453*5796c8dcSSimon Schubert 	    case DW_LNS_set_column:
1454*5796c8dcSSimon Schubert 	      column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1455*5796c8dcSSimon Schubert 	      line_ptr += bytes_read;
1456*5796c8dcSSimon Schubert 	      break;
1457*5796c8dcSSimon Schubert 	    case DW_LNS_negate_stmt:
1458*5796c8dcSSimon Schubert 	      is_stmt = (!is_stmt);
1459*5796c8dcSSimon Schubert 	      break;
1460*5796c8dcSSimon Schubert 	    case DW_LNS_set_basic_block:
1461*5796c8dcSSimon Schubert 	      break;
1462*5796c8dcSSimon Schubert 	    case DW_LNS_const_add_pc:
1463*5796c8dcSSimon Schubert 	      address += lh.minimum_instruction_length
1464*5796c8dcSSimon Schubert 		      * ((255 - lh.opcode_base) / lh.line_range);
1465*5796c8dcSSimon Schubert 	      break;
1466*5796c8dcSSimon Schubert 	    case DW_LNS_fixed_advance_pc:
1467*5796c8dcSSimon Schubert 	      address += read_2_bytes (abfd, line_ptr);
1468*5796c8dcSSimon Schubert 	      line_ptr += 2;
1469*5796c8dcSSimon Schubert 	      break;
1470*5796c8dcSSimon Schubert 	    default:
1471*5796c8dcSSimon Schubert 	      {
1472*5796c8dcSSimon Schubert 		int i;
1473*5796c8dcSSimon Schubert 
1474*5796c8dcSSimon Schubert 		/* Unknown standard opcode, ignore it.  */
1475*5796c8dcSSimon Schubert 		for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1476*5796c8dcSSimon Schubert 		  {
1477*5796c8dcSSimon Schubert 		    (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1478*5796c8dcSSimon Schubert 		    line_ptr += bytes_read;
1479*5796c8dcSSimon Schubert 		  }
1480*5796c8dcSSimon Schubert 	      }
1481*5796c8dcSSimon Schubert 	    }
1482*5796c8dcSSimon Schubert 	}
1483*5796c8dcSSimon Schubert 
1484*5796c8dcSSimon Schubert       if (filename)
1485*5796c8dcSSimon Schubert 	free (filename);
1486*5796c8dcSSimon Schubert     }
1487*5796c8dcSSimon Schubert 
1488*5796c8dcSSimon Schubert   return table;
1489*5796c8dcSSimon Schubert }
1490*5796c8dcSSimon Schubert 
1491*5796c8dcSSimon Schubert /* If ADDR is within TABLE set the output parameters and return TRUE,
1492*5796c8dcSSimon Schubert    otherwise return FALSE.  The output parameters, FILENAME_PTR and
1493*5796c8dcSSimon Schubert    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
1494*5796c8dcSSimon Schubert 
1495*5796c8dcSSimon Schubert static bfd_boolean
1496*5796c8dcSSimon Schubert lookup_address_in_line_info_table (struct line_info_table *table,
1497*5796c8dcSSimon Schubert 				   bfd_vma addr,
1498*5796c8dcSSimon Schubert 				   struct funcinfo *function,
1499*5796c8dcSSimon Schubert 				   const char **filename_ptr,
1500*5796c8dcSSimon Schubert 				   unsigned int *linenumber_ptr)
1501*5796c8dcSSimon Schubert {
1502*5796c8dcSSimon Schubert   /* Note: table->last_line should be a descendingly sorted list. */
1503*5796c8dcSSimon Schubert   struct line_info* next_line = table->last_line;
1504*5796c8dcSSimon Schubert   struct line_info* each_line = NULL;
1505*5796c8dcSSimon Schubert   *filename_ptr = NULL;
1506*5796c8dcSSimon Schubert 
1507*5796c8dcSSimon Schubert   if (!next_line)
1508*5796c8dcSSimon Schubert     return FALSE;
1509*5796c8dcSSimon Schubert 
1510*5796c8dcSSimon Schubert   each_line = next_line->prev_line;
1511*5796c8dcSSimon Schubert 
1512*5796c8dcSSimon Schubert   /* Check for large addresses */
1513*5796c8dcSSimon Schubert   if (addr > next_line->address)
1514*5796c8dcSSimon Schubert     each_line = NULL; /* ensure we skip over the normal case */
1515*5796c8dcSSimon Schubert 
1516*5796c8dcSSimon Schubert   /* Normal case: search the list; save  */
1517*5796c8dcSSimon Schubert   while (each_line && next_line)
1518*5796c8dcSSimon Schubert     {
1519*5796c8dcSSimon Schubert       /* If we have an address match, save this info.  This allows us
1520*5796c8dcSSimon Schubert 	 to return as good as results as possible for strange debugging
1521*5796c8dcSSimon Schubert 	 info.  */
1522*5796c8dcSSimon Schubert       bfd_boolean addr_match = FALSE;
1523*5796c8dcSSimon Schubert       if (each_line->address <= addr && addr < next_line->address)
1524*5796c8dcSSimon Schubert 	{
1525*5796c8dcSSimon Schubert 	  addr_match = TRUE;
1526*5796c8dcSSimon Schubert 
1527*5796c8dcSSimon Schubert 	  /* If this line appears to span functions, and addr is in the
1528*5796c8dcSSimon Schubert 	     later function, return the first line of that function instead
1529*5796c8dcSSimon Schubert 	     of the last line of the earlier one.  This check is for GCC
1530*5796c8dcSSimon Schubert 	     2.95, which emits the first line number for a function late.  */
1531*5796c8dcSSimon Schubert 
1532*5796c8dcSSimon Schubert 	  if (function != NULL)
1533*5796c8dcSSimon Schubert 	    {
1534*5796c8dcSSimon Schubert 	      bfd_vma lowest_pc;
1535*5796c8dcSSimon Schubert 	      struct arange *arange;
1536*5796c8dcSSimon Schubert 
1537*5796c8dcSSimon Schubert 	      /* Find the lowest address in the function's range list */
1538*5796c8dcSSimon Schubert 	      lowest_pc = function->arange.low;
1539*5796c8dcSSimon Schubert 	      for (arange = &function->arange;
1540*5796c8dcSSimon Schubert 		   arange;
1541*5796c8dcSSimon Schubert 		   arange = arange->next)
1542*5796c8dcSSimon Schubert 		{
1543*5796c8dcSSimon Schubert 		  if (function->arange.low < lowest_pc)
1544*5796c8dcSSimon Schubert 		    lowest_pc = function->arange.low;
1545*5796c8dcSSimon Schubert 		}
1546*5796c8dcSSimon Schubert 	      /* Check for spanning function and set outgoing line info */
1547*5796c8dcSSimon Schubert 	      if (addr >= lowest_pc
1548*5796c8dcSSimon Schubert 		  && each_line->address < lowest_pc
1549*5796c8dcSSimon Schubert 		  && next_line->address > lowest_pc)
1550*5796c8dcSSimon Schubert 		{
1551*5796c8dcSSimon Schubert 		  *filename_ptr = next_line->filename;
1552*5796c8dcSSimon Schubert 		  *linenumber_ptr = next_line->line;
1553*5796c8dcSSimon Schubert 		}
1554*5796c8dcSSimon Schubert 	      else
1555*5796c8dcSSimon Schubert 		{
1556*5796c8dcSSimon Schubert 		  *filename_ptr = each_line->filename;
1557*5796c8dcSSimon Schubert 		  *linenumber_ptr = each_line->line;
1558*5796c8dcSSimon Schubert 		}
1559*5796c8dcSSimon Schubert 	    }
1560*5796c8dcSSimon Schubert 	  else
1561*5796c8dcSSimon Schubert 	    {
1562*5796c8dcSSimon Schubert 	      *filename_ptr = each_line->filename;
1563*5796c8dcSSimon Schubert 	      *linenumber_ptr = each_line->line;
1564*5796c8dcSSimon Schubert 	    }
1565*5796c8dcSSimon Schubert 	}
1566*5796c8dcSSimon Schubert 
1567*5796c8dcSSimon Schubert       if (addr_match && !each_line->end_sequence)
1568*5796c8dcSSimon Schubert 	return TRUE; /* we have definitely found what we want */
1569*5796c8dcSSimon Schubert 
1570*5796c8dcSSimon Schubert       next_line = each_line;
1571*5796c8dcSSimon Schubert       each_line = each_line->prev_line;
1572*5796c8dcSSimon Schubert     }
1573*5796c8dcSSimon Schubert 
1574*5796c8dcSSimon Schubert   /* At this point each_line is NULL but next_line is not.  If we found
1575*5796c8dcSSimon Schubert      a candidate end-of-sequence point in the loop above, we can return
1576*5796c8dcSSimon Schubert      that (compatibility with a bug in the Intel compiler); otherwise,
1577*5796c8dcSSimon Schubert      assuming that we found the containing function for this address in
1578*5796c8dcSSimon Schubert      this compilation unit, return the first line we have a number for
1579*5796c8dcSSimon Schubert      (compatibility with GCC 2.95).  */
1580*5796c8dcSSimon Schubert   if (*filename_ptr == NULL && function != NULL)
1581*5796c8dcSSimon Schubert     {
1582*5796c8dcSSimon Schubert       *filename_ptr = next_line->filename;
1583*5796c8dcSSimon Schubert       *linenumber_ptr = next_line->line;
1584*5796c8dcSSimon Schubert       return TRUE;
1585*5796c8dcSSimon Schubert     }
1586*5796c8dcSSimon Schubert 
1587*5796c8dcSSimon Schubert   return FALSE;
1588*5796c8dcSSimon Schubert }
1589*5796c8dcSSimon Schubert 
1590*5796c8dcSSimon Schubert /* Read in the .debug_ranges section for future reference */
1591*5796c8dcSSimon Schubert 
1592*5796c8dcSSimon Schubert static bfd_boolean
1593*5796c8dcSSimon Schubert read_debug_ranges (struct comp_unit *unit)
1594*5796c8dcSSimon Schubert {
1595*5796c8dcSSimon Schubert   struct dwarf2_debug *stash = unit->stash;
1596*5796c8dcSSimon Schubert   return read_section (unit->abfd, ".debug_ranges", ".zdebug_ranges",
1597*5796c8dcSSimon Schubert 		       stash->syms, 0,
1598*5796c8dcSSimon Schubert 		       &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1599*5796c8dcSSimon Schubert }
1600*5796c8dcSSimon Schubert 
1601*5796c8dcSSimon Schubert /* Function table functions.  */
1602*5796c8dcSSimon Schubert 
1603*5796c8dcSSimon Schubert /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1604*5796c8dcSSimon Schubert    Note that we need to find the function that has the smallest
1605*5796c8dcSSimon Schubert    range that contains ADDR, to handle inlined functions without
1606*5796c8dcSSimon Schubert    depending upon them being ordered in TABLE by increasing range. */
1607*5796c8dcSSimon Schubert 
1608*5796c8dcSSimon Schubert static bfd_boolean
1609*5796c8dcSSimon Schubert lookup_address_in_function_table (struct comp_unit *unit,
1610*5796c8dcSSimon Schubert 				  bfd_vma addr,
1611*5796c8dcSSimon Schubert 				  struct funcinfo **function_ptr,
1612*5796c8dcSSimon Schubert 				  const char **functionname_ptr)
1613*5796c8dcSSimon Schubert {
1614*5796c8dcSSimon Schubert   struct funcinfo* each_func;
1615*5796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
1616*5796c8dcSSimon Schubert   struct arange *arange;
1617*5796c8dcSSimon Schubert 
1618*5796c8dcSSimon Schubert   for (each_func = unit->function_table;
1619*5796c8dcSSimon Schubert        each_func;
1620*5796c8dcSSimon Schubert        each_func = each_func->prev_func)
1621*5796c8dcSSimon Schubert     {
1622*5796c8dcSSimon Schubert       for (arange = &each_func->arange;
1623*5796c8dcSSimon Schubert 	   arange;
1624*5796c8dcSSimon Schubert 	   arange = arange->next)
1625*5796c8dcSSimon Schubert 	{
1626*5796c8dcSSimon Schubert 	  if (addr >= arange->low && addr < arange->high)
1627*5796c8dcSSimon Schubert 	    {
1628*5796c8dcSSimon Schubert 	      if (!best_fit ||
1629*5796c8dcSSimon Schubert 		  ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1630*5796c8dcSSimon Schubert 		best_fit = each_func;
1631*5796c8dcSSimon Schubert 	    }
1632*5796c8dcSSimon Schubert 	}
1633*5796c8dcSSimon Schubert     }
1634*5796c8dcSSimon Schubert 
1635*5796c8dcSSimon Schubert   if (best_fit)
1636*5796c8dcSSimon Schubert     {
1637*5796c8dcSSimon Schubert       *functionname_ptr = best_fit->name;
1638*5796c8dcSSimon Schubert       *function_ptr = best_fit;
1639*5796c8dcSSimon Schubert       return TRUE;
1640*5796c8dcSSimon Schubert     }
1641*5796c8dcSSimon Schubert   else
1642*5796c8dcSSimon Schubert     {
1643*5796c8dcSSimon Schubert       return FALSE;
1644*5796c8dcSSimon Schubert     }
1645*5796c8dcSSimon Schubert }
1646*5796c8dcSSimon Schubert 
1647*5796c8dcSSimon Schubert /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1648*5796c8dcSSimon Schubert    and LINENUMBER_PTR, and return TRUE.  */
1649*5796c8dcSSimon Schubert 
1650*5796c8dcSSimon Schubert static bfd_boolean
1651*5796c8dcSSimon Schubert lookup_symbol_in_function_table (struct comp_unit *unit,
1652*5796c8dcSSimon Schubert 				 asymbol *sym,
1653*5796c8dcSSimon Schubert 				 bfd_vma addr,
1654*5796c8dcSSimon Schubert 				 const char **filename_ptr,
1655*5796c8dcSSimon Schubert 				 unsigned int *linenumber_ptr)
1656*5796c8dcSSimon Schubert {
1657*5796c8dcSSimon Schubert   struct funcinfo* each_func;
1658*5796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
1659*5796c8dcSSimon Schubert   struct arange *arange;
1660*5796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
1661*5796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
1662*5796c8dcSSimon Schubert 
1663*5796c8dcSSimon Schubert   for (each_func = unit->function_table;
1664*5796c8dcSSimon Schubert        each_func;
1665*5796c8dcSSimon Schubert        each_func = each_func->prev_func)
1666*5796c8dcSSimon Schubert     {
1667*5796c8dcSSimon Schubert       for (arange = &each_func->arange;
1668*5796c8dcSSimon Schubert 	   arange;
1669*5796c8dcSSimon Schubert 	   arange = arange->next)
1670*5796c8dcSSimon Schubert 	{
1671*5796c8dcSSimon Schubert 	  if ((!each_func->sec || each_func->sec == sec)
1672*5796c8dcSSimon Schubert 	      && addr >= arange->low
1673*5796c8dcSSimon Schubert 	      && addr < arange->high
1674*5796c8dcSSimon Schubert 	      && each_func->name
1675*5796c8dcSSimon Schubert 	      && strcmp (name, each_func->name) == 0
1676*5796c8dcSSimon Schubert 	      && (!best_fit
1677*5796c8dcSSimon Schubert 		  || ((arange->high - arange->low)
1678*5796c8dcSSimon Schubert 		      < (best_fit->arange.high - best_fit->arange.low))))
1679*5796c8dcSSimon Schubert 	    best_fit = each_func;
1680*5796c8dcSSimon Schubert 	}
1681*5796c8dcSSimon Schubert     }
1682*5796c8dcSSimon Schubert 
1683*5796c8dcSSimon Schubert   if (best_fit)
1684*5796c8dcSSimon Schubert     {
1685*5796c8dcSSimon Schubert       best_fit->sec = sec;
1686*5796c8dcSSimon Schubert       *filename_ptr = best_fit->file;
1687*5796c8dcSSimon Schubert       *linenumber_ptr = best_fit->line;
1688*5796c8dcSSimon Schubert       return TRUE;
1689*5796c8dcSSimon Schubert     }
1690*5796c8dcSSimon Schubert   else
1691*5796c8dcSSimon Schubert     return FALSE;
1692*5796c8dcSSimon Schubert }
1693*5796c8dcSSimon Schubert 
1694*5796c8dcSSimon Schubert /* Variable table functions.  */
1695*5796c8dcSSimon Schubert 
1696*5796c8dcSSimon Schubert /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1697*5796c8dcSSimon Schubert    LINENUMBER_PTR, and return TRUE.  */
1698*5796c8dcSSimon Schubert 
1699*5796c8dcSSimon Schubert static bfd_boolean
1700*5796c8dcSSimon Schubert lookup_symbol_in_variable_table (struct comp_unit *unit,
1701*5796c8dcSSimon Schubert 				 asymbol *sym,
1702*5796c8dcSSimon Schubert 				 bfd_vma addr,
1703*5796c8dcSSimon Schubert 				 const char **filename_ptr,
1704*5796c8dcSSimon Schubert 				 unsigned int *linenumber_ptr)
1705*5796c8dcSSimon Schubert {
1706*5796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
1707*5796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
1708*5796c8dcSSimon Schubert   struct varinfo* each;
1709*5796c8dcSSimon Schubert 
1710*5796c8dcSSimon Schubert   for (each = unit->variable_table; each; each = each->prev_var)
1711*5796c8dcSSimon Schubert     if (each->stack == 0
1712*5796c8dcSSimon Schubert 	&& each->file != NULL
1713*5796c8dcSSimon Schubert 	&& each->name != NULL
1714*5796c8dcSSimon Schubert 	&& each->addr == addr
1715*5796c8dcSSimon Schubert 	&& (!each->sec || each->sec == sec)
1716*5796c8dcSSimon Schubert 	&& strcmp (name, each->name) == 0)
1717*5796c8dcSSimon Schubert       break;
1718*5796c8dcSSimon Schubert 
1719*5796c8dcSSimon Schubert   if (each)
1720*5796c8dcSSimon Schubert     {
1721*5796c8dcSSimon Schubert       each->sec = sec;
1722*5796c8dcSSimon Schubert       *filename_ptr = each->file;
1723*5796c8dcSSimon Schubert       *linenumber_ptr = each->line;
1724*5796c8dcSSimon Schubert       return TRUE;
1725*5796c8dcSSimon Schubert     }
1726*5796c8dcSSimon Schubert   else
1727*5796c8dcSSimon Schubert     return FALSE;
1728*5796c8dcSSimon Schubert }
1729*5796c8dcSSimon Schubert 
1730*5796c8dcSSimon Schubert static char *
1731*5796c8dcSSimon Schubert find_abstract_instance_name (struct comp_unit *unit,
1732*5796c8dcSSimon Schubert 			     struct attribute *attr_ptr)
1733*5796c8dcSSimon Schubert {
1734*5796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
1735*5796c8dcSSimon Schubert   bfd_byte *info_ptr;
1736*5796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, i;
1737*5796c8dcSSimon Schubert   struct abbrev_info *abbrev;
1738*5796c8dcSSimon Schubert   bfd_uint64_t die_ref = attr_ptr->u.val;
1739*5796c8dcSSimon Schubert   struct attribute attr;
1740*5796c8dcSSimon Schubert   char *name = 0;
1741*5796c8dcSSimon Schubert 
1742*5796c8dcSSimon Schubert   /* DW_FORM_ref_addr can reference an entry in a different CU. It
1743*5796c8dcSSimon Schubert      is an offset from the .debug_info section, not the current CU.  */
1744*5796c8dcSSimon Schubert   if (attr_ptr->form == DW_FORM_ref_addr)
1745*5796c8dcSSimon Schubert     {
1746*5796c8dcSSimon Schubert       /* We only support DW_FORM_ref_addr within the same file, so
1747*5796c8dcSSimon Schubert 	 any relocations should be resolved already.  */
1748*5796c8dcSSimon Schubert       if (!die_ref)
1749*5796c8dcSSimon Schubert 	abort ();
1750*5796c8dcSSimon Schubert 
1751*5796c8dcSSimon Schubert       info_ptr = unit->stash->sec_info_ptr + die_ref;
1752*5796c8dcSSimon Schubert     }
1753*5796c8dcSSimon Schubert   else
1754*5796c8dcSSimon Schubert     info_ptr = unit->info_ptr_unit + die_ref;
1755*5796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1756*5796c8dcSSimon Schubert   info_ptr += bytes_read;
1757*5796c8dcSSimon Schubert 
1758*5796c8dcSSimon Schubert   if (abbrev_number)
1759*5796c8dcSSimon Schubert     {
1760*5796c8dcSSimon Schubert       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1761*5796c8dcSSimon Schubert       if (! abbrev)
1762*5796c8dcSSimon Schubert 	{
1763*5796c8dcSSimon Schubert 	  (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1764*5796c8dcSSimon Schubert 				 abbrev_number);
1765*5796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
1766*5796c8dcSSimon Schubert 	}
1767*5796c8dcSSimon Schubert       else
1768*5796c8dcSSimon Schubert 	{
1769*5796c8dcSSimon Schubert 	  for (i = 0; i < abbrev->num_attrs; ++i)
1770*5796c8dcSSimon Schubert 	    {
1771*5796c8dcSSimon Schubert 	      info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1772*5796c8dcSSimon Schubert 	      switch (attr.name)
1773*5796c8dcSSimon Schubert 		{
1774*5796c8dcSSimon Schubert 		case DW_AT_name:
1775*5796c8dcSSimon Schubert 		  /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1776*5796c8dcSSimon Schubert 		  if (name == NULL)
1777*5796c8dcSSimon Schubert 		    name = attr.u.str;
1778*5796c8dcSSimon Schubert 		  break;
1779*5796c8dcSSimon Schubert 		case DW_AT_specification:
1780*5796c8dcSSimon Schubert 		  name = find_abstract_instance_name (unit, &attr);
1781*5796c8dcSSimon Schubert 		  break;
1782*5796c8dcSSimon Schubert 		case DW_AT_MIPS_linkage_name:
1783*5796c8dcSSimon Schubert 		  name = attr.u.str;
1784*5796c8dcSSimon Schubert 		  break;
1785*5796c8dcSSimon Schubert 		default:
1786*5796c8dcSSimon Schubert 		  break;
1787*5796c8dcSSimon Schubert 		}
1788*5796c8dcSSimon Schubert 	    }
1789*5796c8dcSSimon Schubert 	}
1790*5796c8dcSSimon Schubert     }
1791*5796c8dcSSimon Schubert   return (name);
1792*5796c8dcSSimon Schubert }
1793*5796c8dcSSimon Schubert 
1794*5796c8dcSSimon Schubert static void
1795*5796c8dcSSimon Schubert read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1796*5796c8dcSSimon Schubert {
1797*5796c8dcSSimon Schubert   bfd_byte *ranges_ptr;
1798*5796c8dcSSimon Schubert   bfd_vma base_address = unit->base_address;
1799*5796c8dcSSimon Schubert 
1800*5796c8dcSSimon Schubert   if (! unit->stash->dwarf_ranges_buffer)
1801*5796c8dcSSimon Schubert     {
1802*5796c8dcSSimon Schubert       if (! read_debug_ranges (unit))
1803*5796c8dcSSimon Schubert 	return;
1804*5796c8dcSSimon Schubert     }
1805*5796c8dcSSimon Schubert   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1806*5796c8dcSSimon Schubert 
1807*5796c8dcSSimon Schubert   for (;;)
1808*5796c8dcSSimon Schubert     {
1809*5796c8dcSSimon Schubert       bfd_vma low_pc;
1810*5796c8dcSSimon Schubert       bfd_vma high_pc;
1811*5796c8dcSSimon Schubert 
1812*5796c8dcSSimon Schubert       low_pc = read_address (unit, ranges_ptr);
1813*5796c8dcSSimon Schubert       ranges_ptr += unit->addr_size;
1814*5796c8dcSSimon Schubert       high_pc = read_address (unit, ranges_ptr);
1815*5796c8dcSSimon Schubert       ranges_ptr += unit->addr_size;
1816*5796c8dcSSimon Schubert 
1817*5796c8dcSSimon Schubert       if (low_pc == 0 && high_pc == 0)
1818*5796c8dcSSimon Schubert 	break;
1819*5796c8dcSSimon Schubert       if (low_pc == -1UL && high_pc != -1UL)
1820*5796c8dcSSimon Schubert 	base_address = high_pc;
1821*5796c8dcSSimon Schubert       else
1822*5796c8dcSSimon Schubert 	arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1823*5796c8dcSSimon Schubert     }
1824*5796c8dcSSimon Schubert }
1825*5796c8dcSSimon Schubert 
1826*5796c8dcSSimon Schubert /* DWARF2 Compilation unit functions.  */
1827*5796c8dcSSimon Schubert 
1828*5796c8dcSSimon Schubert /* Scan over each die in a comp. unit looking for functions to add
1829*5796c8dcSSimon Schubert    to the function table and variables to the variable table.  */
1830*5796c8dcSSimon Schubert 
1831*5796c8dcSSimon Schubert static bfd_boolean
1832*5796c8dcSSimon Schubert scan_unit_for_symbols (struct comp_unit *unit)
1833*5796c8dcSSimon Schubert {
1834*5796c8dcSSimon Schubert   bfd *abfd = unit->abfd;
1835*5796c8dcSSimon Schubert   bfd_byte *info_ptr = unit->first_child_die_ptr;
1836*5796c8dcSSimon Schubert   int nesting_level = 1;
1837*5796c8dcSSimon Schubert   struct funcinfo **nested_funcs;
1838*5796c8dcSSimon Schubert   int nested_funcs_size;
1839*5796c8dcSSimon Schubert 
1840*5796c8dcSSimon Schubert   /* Maintain a stack of in-scope functions and inlined functions, which we
1841*5796c8dcSSimon Schubert      can use to set the caller_func field.  */
1842*5796c8dcSSimon Schubert   nested_funcs_size = 32;
1843*5796c8dcSSimon Schubert   nested_funcs = (struct funcinfo **)
1844*5796c8dcSSimon Schubert       bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1845*5796c8dcSSimon Schubert   if (nested_funcs == NULL)
1846*5796c8dcSSimon Schubert     return FALSE;
1847*5796c8dcSSimon Schubert   nested_funcs[nesting_level] = 0;
1848*5796c8dcSSimon Schubert 
1849*5796c8dcSSimon Schubert   while (nesting_level)
1850*5796c8dcSSimon Schubert     {
1851*5796c8dcSSimon Schubert       unsigned int abbrev_number, bytes_read, i;
1852*5796c8dcSSimon Schubert       struct abbrev_info *abbrev;
1853*5796c8dcSSimon Schubert       struct attribute attr;
1854*5796c8dcSSimon Schubert       struct funcinfo *func;
1855*5796c8dcSSimon Schubert       struct varinfo *var;
1856*5796c8dcSSimon Schubert       bfd_vma low_pc = 0;
1857*5796c8dcSSimon Schubert       bfd_vma high_pc = 0;
1858*5796c8dcSSimon Schubert 
1859*5796c8dcSSimon Schubert       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1860*5796c8dcSSimon Schubert       info_ptr += bytes_read;
1861*5796c8dcSSimon Schubert 
1862*5796c8dcSSimon Schubert       if (! abbrev_number)
1863*5796c8dcSSimon Schubert 	{
1864*5796c8dcSSimon Schubert 	  nesting_level--;
1865*5796c8dcSSimon Schubert 	  continue;
1866*5796c8dcSSimon Schubert 	}
1867*5796c8dcSSimon Schubert 
1868*5796c8dcSSimon Schubert       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1869*5796c8dcSSimon Schubert       if (! abbrev)
1870*5796c8dcSSimon Schubert 	{
1871*5796c8dcSSimon Schubert 	  (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1872*5796c8dcSSimon Schubert 			     abbrev_number);
1873*5796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_bad_value);
1874*5796c8dcSSimon Schubert 	  free (nested_funcs);
1875*5796c8dcSSimon Schubert 	  return FALSE;
1876*5796c8dcSSimon Schubert 	}
1877*5796c8dcSSimon Schubert 
1878*5796c8dcSSimon Schubert       var = NULL;
1879*5796c8dcSSimon Schubert       if (abbrev->tag == DW_TAG_subprogram
1880*5796c8dcSSimon Schubert 	  || abbrev->tag == DW_TAG_entry_point
1881*5796c8dcSSimon Schubert 	  || abbrev->tag == DW_TAG_inlined_subroutine)
1882*5796c8dcSSimon Schubert 	{
1883*5796c8dcSSimon Schubert 	  bfd_size_type amt = sizeof (struct funcinfo);
1884*5796c8dcSSimon Schubert 	  func = (struct funcinfo *) bfd_zalloc (abfd, amt);
1885*5796c8dcSSimon Schubert 	  func->tag = abbrev->tag;
1886*5796c8dcSSimon Schubert 	  func->prev_func = unit->function_table;
1887*5796c8dcSSimon Schubert 	  unit->function_table = func;
1888*5796c8dcSSimon Schubert 	  BFD_ASSERT (!unit->cached);
1889*5796c8dcSSimon Schubert 
1890*5796c8dcSSimon Schubert 	  if (func->tag == DW_TAG_inlined_subroutine)
1891*5796c8dcSSimon Schubert 	    for (i = nesting_level - 1; i >= 1; i--)
1892*5796c8dcSSimon Schubert 	      if (nested_funcs[i])
1893*5796c8dcSSimon Schubert 		{
1894*5796c8dcSSimon Schubert 		  func->caller_func = nested_funcs[i];
1895*5796c8dcSSimon Schubert 		  break;
1896*5796c8dcSSimon Schubert 		}
1897*5796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = func;
1898*5796c8dcSSimon Schubert 	}
1899*5796c8dcSSimon Schubert       else
1900*5796c8dcSSimon Schubert 	{
1901*5796c8dcSSimon Schubert 	  func = NULL;
1902*5796c8dcSSimon Schubert 	  if (abbrev->tag == DW_TAG_variable)
1903*5796c8dcSSimon Schubert 	    {
1904*5796c8dcSSimon Schubert 	      bfd_size_type amt = sizeof (struct varinfo);
1905*5796c8dcSSimon Schubert 	      var = (struct varinfo *) bfd_zalloc (abfd, amt);
1906*5796c8dcSSimon Schubert 	      var->tag = abbrev->tag;
1907*5796c8dcSSimon Schubert 	      var->stack = 1;
1908*5796c8dcSSimon Schubert 	      var->prev_var = unit->variable_table;
1909*5796c8dcSSimon Schubert 	      unit->variable_table = var;
1910*5796c8dcSSimon Schubert 	      BFD_ASSERT (!unit->cached);
1911*5796c8dcSSimon Schubert 	    }
1912*5796c8dcSSimon Schubert 
1913*5796c8dcSSimon Schubert 	  /* No inline function in scope at this nesting level.  */
1914*5796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = 0;
1915*5796c8dcSSimon Schubert 	}
1916*5796c8dcSSimon Schubert 
1917*5796c8dcSSimon Schubert       for (i = 0; i < abbrev->num_attrs; ++i)
1918*5796c8dcSSimon Schubert 	{
1919*5796c8dcSSimon Schubert 	  info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1920*5796c8dcSSimon Schubert 
1921*5796c8dcSSimon Schubert 	  if (func)
1922*5796c8dcSSimon Schubert 	    {
1923*5796c8dcSSimon Schubert 	      switch (attr.name)
1924*5796c8dcSSimon Schubert 		{
1925*5796c8dcSSimon Schubert 		case DW_AT_call_file:
1926*5796c8dcSSimon Schubert 		  func->caller_file = concat_filename (unit->line_table, attr.u.val);
1927*5796c8dcSSimon Schubert 		  break;
1928*5796c8dcSSimon Schubert 
1929*5796c8dcSSimon Schubert 		case DW_AT_call_line:
1930*5796c8dcSSimon Schubert 		  func->caller_line = attr.u.val;
1931*5796c8dcSSimon Schubert 		  break;
1932*5796c8dcSSimon Schubert 
1933*5796c8dcSSimon Schubert 		case DW_AT_abstract_origin:
1934*5796c8dcSSimon Schubert 		  func->name = find_abstract_instance_name (unit, &attr);
1935*5796c8dcSSimon Schubert 		  break;
1936*5796c8dcSSimon Schubert 
1937*5796c8dcSSimon Schubert 		case DW_AT_name:
1938*5796c8dcSSimon Schubert 		  /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1939*5796c8dcSSimon Schubert 		  if (func->name == NULL)
1940*5796c8dcSSimon Schubert 		    func->name = attr.u.str;
1941*5796c8dcSSimon Schubert 		  break;
1942*5796c8dcSSimon Schubert 
1943*5796c8dcSSimon Schubert 		case DW_AT_MIPS_linkage_name:
1944*5796c8dcSSimon Schubert 		  func->name = attr.u.str;
1945*5796c8dcSSimon Schubert 		  break;
1946*5796c8dcSSimon Schubert 
1947*5796c8dcSSimon Schubert 		case DW_AT_low_pc:
1948*5796c8dcSSimon Schubert 		  low_pc = attr.u.val;
1949*5796c8dcSSimon Schubert 		  break;
1950*5796c8dcSSimon Schubert 
1951*5796c8dcSSimon Schubert 		case DW_AT_high_pc:
1952*5796c8dcSSimon Schubert 		  high_pc = attr.u.val;
1953*5796c8dcSSimon Schubert 		  break;
1954*5796c8dcSSimon Schubert 
1955*5796c8dcSSimon Schubert 		case DW_AT_ranges:
1956*5796c8dcSSimon Schubert 		  read_rangelist (unit, &func->arange, attr.u.val);
1957*5796c8dcSSimon Schubert 		  break;
1958*5796c8dcSSimon Schubert 
1959*5796c8dcSSimon Schubert 		case DW_AT_decl_file:
1960*5796c8dcSSimon Schubert 		  func->file = concat_filename (unit->line_table,
1961*5796c8dcSSimon Schubert 						attr.u.val);
1962*5796c8dcSSimon Schubert 		  break;
1963*5796c8dcSSimon Schubert 
1964*5796c8dcSSimon Schubert 		case DW_AT_decl_line:
1965*5796c8dcSSimon Schubert 		  func->line = attr.u.val;
1966*5796c8dcSSimon Schubert 		  break;
1967*5796c8dcSSimon Schubert 
1968*5796c8dcSSimon Schubert 		default:
1969*5796c8dcSSimon Schubert 		  break;
1970*5796c8dcSSimon Schubert 		}
1971*5796c8dcSSimon Schubert 	    }
1972*5796c8dcSSimon Schubert 	  else if (var)
1973*5796c8dcSSimon Schubert 	    {
1974*5796c8dcSSimon Schubert 	      switch (attr.name)
1975*5796c8dcSSimon Schubert 		{
1976*5796c8dcSSimon Schubert 		case DW_AT_name:
1977*5796c8dcSSimon Schubert 		  var->name = attr.u.str;
1978*5796c8dcSSimon Schubert 		  break;
1979*5796c8dcSSimon Schubert 
1980*5796c8dcSSimon Schubert 		case DW_AT_decl_file:
1981*5796c8dcSSimon Schubert 		  var->file = concat_filename (unit->line_table,
1982*5796c8dcSSimon Schubert 					       attr.u.val);
1983*5796c8dcSSimon Schubert 		  break;
1984*5796c8dcSSimon Schubert 
1985*5796c8dcSSimon Schubert 		case DW_AT_decl_line:
1986*5796c8dcSSimon Schubert 		  var->line = attr.u.val;
1987*5796c8dcSSimon Schubert 		  break;
1988*5796c8dcSSimon Schubert 
1989*5796c8dcSSimon Schubert 		case DW_AT_external:
1990*5796c8dcSSimon Schubert 		  if (attr.u.val != 0)
1991*5796c8dcSSimon Schubert 		    var->stack = 0;
1992*5796c8dcSSimon Schubert 		  break;
1993*5796c8dcSSimon Schubert 
1994*5796c8dcSSimon Schubert 		case DW_AT_location:
1995*5796c8dcSSimon Schubert 		  switch (attr.form)
1996*5796c8dcSSimon Schubert 		    {
1997*5796c8dcSSimon Schubert 		    case DW_FORM_block:
1998*5796c8dcSSimon Schubert 		    case DW_FORM_block1:
1999*5796c8dcSSimon Schubert 		    case DW_FORM_block2:
2000*5796c8dcSSimon Schubert 		    case DW_FORM_block4:
2001*5796c8dcSSimon Schubert 		      if (*attr.u.blk->data == DW_OP_addr)
2002*5796c8dcSSimon Schubert 			{
2003*5796c8dcSSimon Schubert 			  var->stack = 0;
2004*5796c8dcSSimon Schubert 
2005*5796c8dcSSimon Schubert 			  /* Verify that DW_OP_addr is the only opcode in the
2006*5796c8dcSSimon Schubert 			     location, in which case the block size will be 1
2007*5796c8dcSSimon Schubert 			     plus the address size.  */
2008*5796c8dcSSimon Schubert 			  /* ??? For TLS variables, gcc can emit
2009*5796c8dcSSimon Schubert 			     DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2010*5796c8dcSSimon Schubert 			     which we don't handle here yet.  */
2011*5796c8dcSSimon Schubert 			  if (attr.u.blk->size == unit->addr_size + 1U)
2012*5796c8dcSSimon Schubert 			    var->addr = bfd_get (unit->addr_size * 8,
2013*5796c8dcSSimon Schubert 						 unit->abfd,
2014*5796c8dcSSimon Schubert 						 attr.u.blk->data + 1);
2015*5796c8dcSSimon Schubert 			}
2016*5796c8dcSSimon Schubert 		      break;
2017*5796c8dcSSimon Schubert 
2018*5796c8dcSSimon Schubert 		    default:
2019*5796c8dcSSimon Schubert 		      break;
2020*5796c8dcSSimon Schubert 		    }
2021*5796c8dcSSimon Schubert 		  break;
2022*5796c8dcSSimon Schubert 
2023*5796c8dcSSimon Schubert 		default:
2024*5796c8dcSSimon Schubert 		  break;
2025*5796c8dcSSimon Schubert 		}
2026*5796c8dcSSimon Schubert 	    }
2027*5796c8dcSSimon Schubert 	}
2028*5796c8dcSSimon Schubert 
2029*5796c8dcSSimon Schubert       if (func && high_pc != 0)
2030*5796c8dcSSimon Schubert 	{
2031*5796c8dcSSimon Schubert 	  arange_add (unit->abfd, &func->arange, low_pc, high_pc);
2032*5796c8dcSSimon Schubert 	}
2033*5796c8dcSSimon Schubert 
2034*5796c8dcSSimon Schubert       if (abbrev->has_children)
2035*5796c8dcSSimon Schubert 	{
2036*5796c8dcSSimon Schubert 	  nesting_level++;
2037*5796c8dcSSimon Schubert 
2038*5796c8dcSSimon Schubert 	  if (nesting_level >= nested_funcs_size)
2039*5796c8dcSSimon Schubert 	    {
2040*5796c8dcSSimon Schubert 	      struct funcinfo **tmp;
2041*5796c8dcSSimon Schubert 
2042*5796c8dcSSimon Schubert 	      nested_funcs_size *= 2;
2043*5796c8dcSSimon Schubert 	      tmp = (struct funcinfo **)
2044*5796c8dcSSimon Schubert                  bfd_realloc (nested_funcs,
2045*5796c8dcSSimon Schubert                               (nested_funcs_size * sizeof (struct funcinfo *)));
2046*5796c8dcSSimon Schubert 	      if (tmp == NULL)
2047*5796c8dcSSimon Schubert 		{
2048*5796c8dcSSimon Schubert 		  free (nested_funcs);
2049*5796c8dcSSimon Schubert 		  return FALSE;
2050*5796c8dcSSimon Schubert 		}
2051*5796c8dcSSimon Schubert 	      nested_funcs = tmp;
2052*5796c8dcSSimon Schubert 	    }
2053*5796c8dcSSimon Schubert 	  nested_funcs[nesting_level] = 0;
2054*5796c8dcSSimon Schubert 	}
2055*5796c8dcSSimon Schubert     }
2056*5796c8dcSSimon Schubert 
2057*5796c8dcSSimon Schubert   free (nested_funcs);
2058*5796c8dcSSimon Schubert   return TRUE;
2059*5796c8dcSSimon Schubert }
2060*5796c8dcSSimon Schubert 
2061*5796c8dcSSimon Schubert /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
2062*5796c8dcSSimon Schubert    includes the compilation unit header that proceeds the DIE's, but
2063*5796c8dcSSimon Schubert    does not include the length field that precedes each compilation
2064*5796c8dcSSimon Schubert    unit header.  END_PTR points one past the end of this comp unit.
2065*5796c8dcSSimon Schubert    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2066*5796c8dcSSimon Schubert 
2067*5796c8dcSSimon Schubert    This routine does not read the whole compilation unit; only enough
2068*5796c8dcSSimon Schubert    to get to the line number information for the compilation unit.  */
2069*5796c8dcSSimon Schubert 
2070*5796c8dcSSimon Schubert static struct comp_unit *
2071*5796c8dcSSimon Schubert parse_comp_unit (struct dwarf2_debug *stash,
2072*5796c8dcSSimon Schubert 		 bfd_vma unit_length,
2073*5796c8dcSSimon Schubert 		 bfd_byte *info_ptr_unit,
2074*5796c8dcSSimon Schubert 		 unsigned int offset_size)
2075*5796c8dcSSimon Schubert {
2076*5796c8dcSSimon Schubert   struct comp_unit* unit;
2077*5796c8dcSSimon Schubert   unsigned int version;
2078*5796c8dcSSimon Schubert   bfd_uint64_t abbrev_offset = 0;
2079*5796c8dcSSimon Schubert   unsigned int addr_size;
2080*5796c8dcSSimon Schubert   struct abbrev_info** abbrevs;
2081*5796c8dcSSimon Schubert   unsigned int abbrev_number, bytes_read, i;
2082*5796c8dcSSimon Schubert   struct abbrev_info *abbrev;
2083*5796c8dcSSimon Schubert   struct attribute attr;
2084*5796c8dcSSimon Schubert   bfd_byte *info_ptr = stash->info_ptr;
2085*5796c8dcSSimon Schubert   bfd_byte *end_ptr = info_ptr + unit_length;
2086*5796c8dcSSimon Schubert   bfd_size_type amt;
2087*5796c8dcSSimon Schubert   bfd_vma low_pc = 0;
2088*5796c8dcSSimon Schubert   bfd_vma high_pc = 0;
2089*5796c8dcSSimon Schubert   bfd *abfd = stash->bfd_ptr;
2090*5796c8dcSSimon Schubert 
2091*5796c8dcSSimon Schubert   version = read_2_bytes (abfd, info_ptr);
2092*5796c8dcSSimon Schubert   info_ptr += 2;
2093*5796c8dcSSimon Schubert   BFD_ASSERT (offset_size == 4 || offset_size == 8);
2094*5796c8dcSSimon Schubert   if (offset_size == 4)
2095*5796c8dcSSimon Schubert     abbrev_offset = read_4_bytes (abfd, info_ptr);
2096*5796c8dcSSimon Schubert   else
2097*5796c8dcSSimon Schubert     abbrev_offset = read_8_bytes (abfd, info_ptr);
2098*5796c8dcSSimon Schubert   info_ptr += offset_size;
2099*5796c8dcSSimon Schubert   addr_size = read_1_byte (abfd, info_ptr);
2100*5796c8dcSSimon Schubert   info_ptr += 1;
2101*5796c8dcSSimon Schubert 
2102*5796c8dcSSimon Schubert   if (version != 2 && version != 3)
2103*5796c8dcSSimon Schubert     {
2104*5796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 and 3 information."), version);
2105*5796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
2106*5796c8dcSSimon Schubert       return 0;
2107*5796c8dcSSimon Schubert     }
2108*5796c8dcSSimon Schubert 
2109*5796c8dcSSimon Schubert   if (addr_size > sizeof (bfd_vma))
2110*5796c8dcSSimon Schubert     {
2111*5796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2112*5796c8dcSSimon Schubert 			 addr_size,
2113*5796c8dcSSimon Schubert 			 (unsigned int) sizeof (bfd_vma));
2114*5796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
2115*5796c8dcSSimon Schubert       return 0;
2116*5796c8dcSSimon Schubert     }
2117*5796c8dcSSimon Schubert 
2118*5796c8dcSSimon Schubert   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2119*5796c8dcSSimon Schubert     {
2120*5796c8dcSSimon Schubert       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2121*5796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
2122*5796c8dcSSimon Schubert       return 0;
2123*5796c8dcSSimon Schubert     }
2124*5796c8dcSSimon Schubert 
2125*5796c8dcSSimon Schubert   /* Read the abbrevs for this compilation unit into a table.  */
2126*5796c8dcSSimon Schubert   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2127*5796c8dcSSimon Schubert   if (! abbrevs)
2128*5796c8dcSSimon Schubert       return 0;
2129*5796c8dcSSimon Schubert 
2130*5796c8dcSSimon Schubert   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2131*5796c8dcSSimon Schubert   info_ptr += bytes_read;
2132*5796c8dcSSimon Schubert   if (! abbrev_number)
2133*5796c8dcSSimon Schubert     {
2134*5796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2135*5796c8dcSSimon Schubert 			 abbrev_number);
2136*5796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
2137*5796c8dcSSimon Schubert       return 0;
2138*5796c8dcSSimon Schubert     }
2139*5796c8dcSSimon Schubert 
2140*5796c8dcSSimon Schubert   abbrev = lookup_abbrev (abbrev_number, abbrevs);
2141*5796c8dcSSimon Schubert   if (! abbrev)
2142*5796c8dcSSimon Schubert     {
2143*5796c8dcSSimon Schubert       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2144*5796c8dcSSimon Schubert 			 abbrev_number);
2145*5796c8dcSSimon Schubert       bfd_set_error (bfd_error_bad_value);
2146*5796c8dcSSimon Schubert       return 0;
2147*5796c8dcSSimon Schubert     }
2148*5796c8dcSSimon Schubert 
2149*5796c8dcSSimon Schubert   amt = sizeof (struct comp_unit);
2150*5796c8dcSSimon Schubert   unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2151*5796c8dcSSimon Schubert   unit->abfd = abfd;
2152*5796c8dcSSimon Schubert   unit->version = version;
2153*5796c8dcSSimon Schubert   unit->addr_size = addr_size;
2154*5796c8dcSSimon Schubert   unit->offset_size = offset_size;
2155*5796c8dcSSimon Schubert   unit->abbrevs = abbrevs;
2156*5796c8dcSSimon Schubert   unit->end_ptr = end_ptr;
2157*5796c8dcSSimon Schubert   unit->stash = stash;
2158*5796c8dcSSimon Schubert   unit->info_ptr_unit = info_ptr_unit;
2159*5796c8dcSSimon Schubert 
2160*5796c8dcSSimon Schubert   for (i = 0; i < abbrev->num_attrs; ++i)
2161*5796c8dcSSimon Schubert     {
2162*5796c8dcSSimon Schubert       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2163*5796c8dcSSimon Schubert 
2164*5796c8dcSSimon Schubert       /* Store the data if it is of an attribute we want to keep in a
2165*5796c8dcSSimon Schubert 	 partial symbol table.  */
2166*5796c8dcSSimon Schubert       switch (attr.name)
2167*5796c8dcSSimon Schubert 	{
2168*5796c8dcSSimon Schubert 	case DW_AT_stmt_list:
2169*5796c8dcSSimon Schubert 	  unit->stmtlist = 1;
2170*5796c8dcSSimon Schubert 	  unit->line_offset = attr.u.val;
2171*5796c8dcSSimon Schubert 	  break;
2172*5796c8dcSSimon Schubert 
2173*5796c8dcSSimon Schubert 	case DW_AT_name:
2174*5796c8dcSSimon Schubert 	  unit->name = attr.u.str;
2175*5796c8dcSSimon Schubert 	  break;
2176*5796c8dcSSimon Schubert 
2177*5796c8dcSSimon Schubert 	case DW_AT_low_pc:
2178*5796c8dcSSimon Schubert 	  low_pc = attr.u.val;
2179*5796c8dcSSimon Schubert 	  /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2180*5796c8dcSSimon Schubert 	     this is the base address to use when reading location
2181*5796c8dcSSimon Schubert 	     lists or range lists. */
2182*5796c8dcSSimon Schubert 	  unit->base_address = low_pc;
2183*5796c8dcSSimon Schubert 	  break;
2184*5796c8dcSSimon Schubert 
2185*5796c8dcSSimon Schubert 	case DW_AT_high_pc:
2186*5796c8dcSSimon Schubert 	  high_pc = attr.u.val;
2187*5796c8dcSSimon Schubert 	  break;
2188*5796c8dcSSimon Schubert 
2189*5796c8dcSSimon Schubert 	case DW_AT_ranges:
2190*5796c8dcSSimon Schubert 	  read_rangelist (unit, &unit->arange, attr.u.val);
2191*5796c8dcSSimon Schubert 	  break;
2192*5796c8dcSSimon Schubert 
2193*5796c8dcSSimon Schubert 	case DW_AT_comp_dir:
2194*5796c8dcSSimon Schubert 	  {
2195*5796c8dcSSimon Schubert 	    char *comp_dir = attr.u.str;
2196*5796c8dcSSimon Schubert 	    if (comp_dir)
2197*5796c8dcSSimon Schubert 	      {
2198*5796c8dcSSimon Schubert 		/* Irix 6.2 native cc prepends <machine>.: to the compilation
2199*5796c8dcSSimon Schubert 		   directory, get rid of it.  */
2200*5796c8dcSSimon Schubert 		char *cp = strchr (comp_dir, ':');
2201*5796c8dcSSimon Schubert 
2202*5796c8dcSSimon Schubert 		if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2203*5796c8dcSSimon Schubert 		  comp_dir = cp + 1;
2204*5796c8dcSSimon Schubert 	      }
2205*5796c8dcSSimon Schubert 	    unit->comp_dir = comp_dir;
2206*5796c8dcSSimon Schubert 	    break;
2207*5796c8dcSSimon Schubert 	  }
2208*5796c8dcSSimon Schubert 
2209*5796c8dcSSimon Schubert 	default:
2210*5796c8dcSSimon Schubert 	  break;
2211*5796c8dcSSimon Schubert 	}
2212*5796c8dcSSimon Schubert     }
2213*5796c8dcSSimon Schubert   if (high_pc != 0)
2214*5796c8dcSSimon Schubert     {
2215*5796c8dcSSimon Schubert       arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2216*5796c8dcSSimon Schubert     }
2217*5796c8dcSSimon Schubert 
2218*5796c8dcSSimon Schubert   unit->first_child_die_ptr = info_ptr;
2219*5796c8dcSSimon Schubert   return unit;
2220*5796c8dcSSimon Schubert }
2221*5796c8dcSSimon Schubert 
2222*5796c8dcSSimon Schubert /* Return TRUE if UNIT may contain the address given by ADDR.  When
2223*5796c8dcSSimon Schubert    there are functions written entirely with inline asm statements, the
2224*5796c8dcSSimon Schubert    range info in the compilation unit header may not be correct.  We
2225*5796c8dcSSimon Schubert    need to consult the line info table to see if a compilation unit
2226*5796c8dcSSimon Schubert    really contains the given address.  */
2227*5796c8dcSSimon Schubert 
2228*5796c8dcSSimon Schubert static bfd_boolean
2229*5796c8dcSSimon Schubert comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2230*5796c8dcSSimon Schubert {
2231*5796c8dcSSimon Schubert   struct arange *arange;
2232*5796c8dcSSimon Schubert 
2233*5796c8dcSSimon Schubert   if (unit->error)
2234*5796c8dcSSimon Schubert     return FALSE;
2235*5796c8dcSSimon Schubert 
2236*5796c8dcSSimon Schubert   arange = &unit->arange;
2237*5796c8dcSSimon Schubert   do
2238*5796c8dcSSimon Schubert     {
2239*5796c8dcSSimon Schubert       if (addr >= arange->low && addr < arange->high)
2240*5796c8dcSSimon Schubert 	return TRUE;
2241*5796c8dcSSimon Schubert       arange = arange->next;
2242*5796c8dcSSimon Schubert     }
2243*5796c8dcSSimon Schubert   while (arange);
2244*5796c8dcSSimon Schubert 
2245*5796c8dcSSimon Schubert   return FALSE;
2246*5796c8dcSSimon Schubert }
2247*5796c8dcSSimon Schubert 
2248*5796c8dcSSimon Schubert /* If UNIT contains ADDR, set the output parameters to the values for
2249*5796c8dcSSimon Schubert    the line containing ADDR.  The output parameters, FILENAME_PTR,
2250*5796c8dcSSimon Schubert    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2251*5796c8dcSSimon Schubert    to be filled in.
2252*5796c8dcSSimon Schubert 
2253*5796c8dcSSimon Schubert    Return TRUE if UNIT contains ADDR, and no errors were encountered;
2254*5796c8dcSSimon Schubert    FALSE otherwise.  */
2255*5796c8dcSSimon Schubert 
2256*5796c8dcSSimon Schubert static bfd_boolean
2257*5796c8dcSSimon Schubert comp_unit_find_nearest_line (struct comp_unit *unit,
2258*5796c8dcSSimon Schubert 			     bfd_vma addr,
2259*5796c8dcSSimon Schubert 			     const char **filename_ptr,
2260*5796c8dcSSimon Schubert 			     const char **functionname_ptr,
2261*5796c8dcSSimon Schubert 			     unsigned int *linenumber_ptr,
2262*5796c8dcSSimon Schubert 			     struct dwarf2_debug *stash)
2263*5796c8dcSSimon Schubert {
2264*5796c8dcSSimon Schubert   bfd_boolean line_p;
2265*5796c8dcSSimon Schubert   bfd_boolean func_p;
2266*5796c8dcSSimon Schubert   struct funcinfo *function;
2267*5796c8dcSSimon Schubert 
2268*5796c8dcSSimon Schubert   if (unit->error)
2269*5796c8dcSSimon Schubert     return FALSE;
2270*5796c8dcSSimon Schubert 
2271*5796c8dcSSimon Schubert   if (! unit->line_table)
2272*5796c8dcSSimon Schubert     {
2273*5796c8dcSSimon Schubert       if (! unit->stmtlist)
2274*5796c8dcSSimon Schubert 	{
2275*5796c8dcSSimon Schubert 	  unit->error = 1;
2276*5796c8dcSSimon Schubert 	  return FALSE;
2277*5796c8dcSSimon Schubert 	}
2278*5796c8dcSSimon Schubert 
2279*5796c8dcSSimon Schubert       unit->line_table = decode_line_info (unit, stash);
2280*5796c8dcSSimon Schubert 
2281*5796c8dcSSimon Schubert       if (! unit->line_table)
2282*5796c8dcSSimon Schubert 	{
2283*5796c8dcSSimon Schubert 	  unit->error = 1;
2284*5796c8dcSSimon Schubert 	  return FALSE;
2285*5796c8dcSSimon Schubert 	}
2286*5796c8dcSSimon Schubert 
2287*5796c8dcSSimon Schubert       if (unit->first_child_die_ptr < unit->end_ptr
2288*5796c8dcSSimon Schubert 	  && ! scan_unit_for_symbols (unit))
2289*5796c8dcSSimon Schubert 	{
2290*5796c8dcSSimon Schubert 	  unit->error = 1;
2291*5796c8dcSSimon Schubert 	  return FALSE;
2292*5796c8dcSSimon Schubert 	}
2293*5796c8dcSSimon Schubert     }
2294*5796c8dcSSimon Schubert 
2295*5796c8dcSSimon Schubert   function = NULL;
2296*5796c8dcSSimon Schubert   func_p = lookup_address_in_function_table (unit, addr,
2297*5796c8dcSSimon Schubert 					     &function, functionname_ptr);
2298*5796c8dcSSimon Schubert   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2299*5796c8dcSSimon Schubert     stash->inliner_chain = function;
2300*5796c8dcSSimon Schubert   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2301*5796c8dcSSimon Schubert 					      function, filename_ptr,
2302*5796c8dcSSimon Schubert 					      linenumber_ptr);
2303*5796c8dcSSimon Schubert   return line_p || func_p;
2304*5796c8dcSSimon Schubert }
2305*5796c8dcSSimon Schubert 
2306*5796c8dcSSimon Schubert /* Check to see if line info is already decoded in a comp_unit.
2307*5796c8dcSSimon Schubert    If not, decode it.  Returns TRUE if no errors were encountered;
2308*5796c8dcSSimon Schubert    FALSE otherwise.  */
2309*5796c8dcSSimon Schubert 
2310*5796c8dcSSimon Schubert static bfd_boolean
2311*5796c8dcSSimon Schubert comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2312*5796c8dcSSimon Schubert 				  struct dwarf2_debug *stash)
2313*5796c8dcSSimon Schubert {
2314*5796c8dcSSimon Schubert   if (unit->error)
2315*5796c8dcSSimon Schubert     return FALSE;
2316*5796c8dcSSimon Schubert 
2317*5796c8dcSSimon Schubert   if (! unit->line_table)
2318*5796c8dcSSimon Schubert     {
2319*5796c8dcSSimon Schubert       if (! unit->stmtlist)
2320*5796c8dcSSimon Schubert 	{
2321*5796c8dcSSimon Schubert 	  unit->error = 1;
2322*5796c8dcSSimon Schubert 	  return FALSE;
2323*5796c8dcSSimon Schubert 	}
2324*5796c8dcSSimon Schubert 
2325*5796c8dcSSimon Schubert       unit->line_table = decode_line_info (unit, stash);
2326*5796c8dcSSimon Schubert 
2327*5796c8dcSSimon Schubert       if (! unit->line_table)
2328*5796c8dcSSimon Schubert 	{
2329*5796c8dcSSimon Schubert 	  unit->error = 1;
2330*5796c8dcSSimon Schubert 	  return FALSE;
2331*5796c8dcSSimon Schubert 	}
2332*5796c8dcSSimon Schubert 
2333*5796c8dcSSimon Schubert       if (unit->first_child_die_ptr < unit->end_ptr
2334*5796c8dcSSimon Schubert 	  && ! scan_unit_for_symbols (unit))
2335*5796c8dcSSimon Schubert 	{
2336*5796c8dcSSimon Schubert 	  unit->error = 1;
2337*5796c8dcSSimon Schubert 	  return FALSE;
2338*5796c8dcSSimon Schubert 	}
2339*5796c8dcSSimon Schubert     }
2340*5796c8dcSSimon Schubert 
2341*5796c8dcSSimon Schubert   return TRUE;
2342*5796c8dcSSimon Schubert }
2343*5796c8dcSSimon Schubert 
2344*5796c8dcSSimon Schubert /* If UNIT contains SYM at ADDR, set the output parameters to the
2345*5796c8dcSSimon Schubert    values for the line containing SYM.  The output parameters,
2346*5796c8dcSSimon Schubert    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2347*5796c8dcSSimon Schubert    filled in.
2348*5796c8dcSSimon Schubert 
2349*5796c8dcSSimon Schubert    Return TRUE if UNIT contains SYM, and no errors were encountered;
2350*5796c8dcSSimon Schubert    FALSE otherwise.  */
2351*5796c8dcSSimon Schubert 
2352*5796c8dcSSimon Schubert static bfd_boolean
2353*5796c8dcSSimon Schubert comp_unit_find_line (struct comp_unit *unit,
2354*5796c8dcSSimon Schubert 		     asymbol *sym,
2355*5796c8dcSSimon Schubert 		     bfd_vma addr,
2356*5796c8dcSSimon Schubert 		     const char **filename_ptr,
2357*5796c8dcSSimon Schubert 		     unsigned int *linenumber_ptr,
2358*5796c8dcSSimon Schubert 		     struct dwarf2_debug *stash)
2359*5796c8dcSSimon Schubert {
2360*5796c8dcSSimon Schubert   if (!comp_unit_maybe_decode_line_info (unit, stash))
2361*5796c8dcSSimon Schubert     return FALSE;
2362*5796c8dcSSimon Schubert 
2363*5796c8dcSSimon Schubert   if (sym->flags & BSF_FUNCTION)
2364*5796c8dcSSimon Schubert     return lookup_symbol_in_function_table (unit, sym, addr,
2365*5796c8dcSSimon Schubert 					    filename_ptr,
2366*5796c8dcSSimon Schubert 					    linenumber_ptr);
2367*5796c8dcSSimon Schubert 
2368*5796c8dcSSimon Schubert   return lookup_symbol_in_variable_table (unit, sym, addr,
2369*5796c8dcSSimon Schubert 					  filename_ptr,
2370*5796c8dcSSimon Schubert 					  linenumber_ptr);
2371*5796c8dcSSimon Schubert }
2372*5796c8dcSSimon Schubert 
2373*5796c8dcSSimon Schubert static struct funcinfo *
2374*5796c8dcSSimon Schubert reverse_funcinfo_list (struct funcinfo *head)
2375*5796c8dcSSimon Schubert {
2376*5796c8dcSSimon Schubert   struct funcinfo *rhead;
2377*5796c8dcSSimon Schubert   struct funcinfo *temp;
2378*5796c8dcSSimon Schubert 
2379*5796c8dcSSimon Schubert   for (rhead = NULL; head; head = temp)
2380*5796c8dcSSimon Schubert     {
2381*5796c8dcSSimon Schubert       temp = head->prev_func;
2382*5796c8dcSSimon Schubert       head->prev_func = rhead;
2383*5796c8dcSSimon Schubert       rhead = head;
2384*5796c8dcSSimon Schubert     }
2385*5796c8dcSSimon Schubert   return rhead;
2386*5796c8dcSSimon Schubert }
2387*5796c8dcSSimon Schubert 
2388*5796c8dcSSimon Schubert static struct varinfo *
2389*5796c8dcSSimon Schubert reverse_varinfo_list (struct varinfo *head)
2390*5796c8dcSSimon Schubert {
2391*5796c8dcSSimon Schubert   struct varinfo *rhead;
2392*5796c8dcSSimon Schubert   struct varinfo *temp;
2393*5796c8dcSSimon Schubert 
2394*5796c8dcSSimon Schubert   for (rhead = NULL; head; head = temp)
2395*5796c8dcSSimon Schubert     {
2396*5796c8dcSSimon Schubert       temp = head->prev_var;
2397*5796c8dcSSimon Schubert       head->prev_var = rhead;
2398*5796c8dcSSimon Schubert       rhead = head;
2399*5796c8dcSSimon Schubert     }
2400*5796c8dcSSimon Schubert   return rhead;
2401*5796c8dcSSimon Schubert }
2402*5796c8dcSSimon Schubert 
2403*5796c8dcSSimon Schubert /* Extract all interesting funcinfos and varinfos of a compilation
2404*5796c8dcSSimon Schubert    unit into hash tables for faster lookup.  Returns TRUE if no
2405*5796c8dcSSimon Schubert    errors were enountered; FALSE otherwise.  */
2406*5796c8dcSSimon Schubert 
2407*5796c8dcSSimon Schubert static bfd_boolean
2408*5796c8dcSSimon Schubert comp_unit_hash_info (struct dwarf2_debug *stash,
2409*5796c8dcSSimon Schubert 		     struct comp_unit *unit,
2410*5796c8dcSSimon Schubert 		     struct info_hash_table *funcinfo_hash_table,
2411*5796c8dcSSimon Schubert 		     struct info_hash_table *varinfo_hash_table)
2412*5796c8dcSSimon Schubert {
2413*5796c8dcSSimon Schubert   struct funcinfo* each_func;
2414*5796c8dcSSimon Schubert   struct varinfo* each_var;
2415*5796c8dcSSimon Schubert   bfd_boolean okay = TRUE;
2416*5796c8dcSSimon Schubert 
2417*5796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2418*5796c8dcSSimon Schubert 
2419*5796c8dcSSimon Schubert   if (!comp_unit_maybe_decode_line_info (unit, stash))
2420*5796c8dcSSimon Schubert     return FALSE;
2421*5796c8dcSSimon Schubert 
2422*5796c8dcSSimon Schubert   BFD_ASSERT (!unit->cached);
2423*5796c8dcSSimon Schubert 
2424*5796c8dcSSimon Schubert   /* To preserve the original search order, we went to visit the function
2425*5796c8dcSSimon Schubert      infos in the reversed order of the list.  However, making the list
2426*5796c8dcSSimon Schubert      bi-directional use quite a bit of extra memory.  So we reverse
2427*5796c8dcSSimon Schubert      the list first, traverse the list in the now reversed order and
2428*5796c8dcSSimon Schubert      finally reverse the list again to get back the original order.  */
2429*5796c8dcSSimon Schubert   unit->function_table = reverse_funcinfo_list (unit->function_table);
2430*5796c8dcSSimon Schubert   for (each_func = unit->function_table;
2431*5796c8dcSSimon Schubert        each_func && okay;
2432*5796c8dcSSimon Schubert        each_func = each_func->prev_func)
2433*5796c8dcSSimon Schubert     {
2434*5796c8dcSSimon Schubert       /* Skip nameless functions. */
2435*5796c8dcSSimon Schubert       if (each_func->name)
2436*5796c8dcSSimon Schubert 	/* There is no need to copy name string into hash table as
2437*5796c8dcSSimon Schubert 	   name string is either in the dwarf string buffer or
2438*5796c8dcSSimon Schubert 	   info in the stash.  */
2439*5796c8dcSSimon Schubert 	okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2440*5796c8dcSSimon Schubert 				       (void*) each_func, FALSE);
2441*5796c8dcSSimon Schubert     }
2442*5796c8dcSSimon Schubert   unit->function_table = reverse_funcinfo_list (unit->function_table);
2443*5796c8dcSSimon Schubert   if (!okay)
2444*5796c8dcSSimon Schubert     return FALSE;
2445*5796c8dcSSimon Schubert 
2446*5796c8dcSSimon Schubert   /* We do the same for variable infos.  */
2447*5796c8dcSSimon Schubert   unit->variable_table = reverse_varinfo_list (unit->variable_table);
2448*5796c8dcSSimon Schubert   for (each_var = unit->variable_table;
2449*5796c8dcSSimon Schubert        each_var && okay;
2450*5796c8dcSSimon Schubert        each_var = each_var->prev_var)
2451*5796c8dcSSimon Schubert     {
2452*5796c8dcSSimon Schubert       /* Skip stack vars and vars with no files or names.  */
2453*5796c8dcSSimon Schubert       if (each_var->stack == 0
2454*5796c8dcSSimon Schubert 	  && each_var->file != NULL
2455*5796c8dcSSimon Schubert 	  && each_var->name != NULL)
2456*5796c8dcSSimon Schubert 	/* There is no need to copy name string into hash table as
2457*5796c8dcSSimon Schubert 	   name string is either in the dwarf string buffer or
2458*5796c8dcSSimon Schubert 	   info in the stash.  */
2459*5796c8dcSSimon Schubert 	okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2460*5796c8dcSSimon Schubert 				       (void*) each_var, FALSE);
2461*5796c8dcSSimon Schubert     }
2462*5796c8dcSSimon Schubert 
2463*5796c8dcSSimon Schubert   unit->variable_table = reverse_varinfo_list (unit->variable_table);
2464*5796c8dcSSimon Schubert   unit->cached = TRUE;
2465*5796c8dcSSimon Schubert   return okay;
2466*5796c8dcSSimon Schubert }
2467*5796c8dcSSimon Schubert 
2468*5796c8dcSSimon Schubert /* Locate a section in a BFD containing debugging info.  The search starts
2469*5796c8dcSSimon Schubert    from the section after AFTER_SEC, or from the first section in the BFD if
2470*5796c8dcSSimon Schubert    AFTER_SEC is NULL.  The search works by examining the names of the
2471*5796c8dcSSimon Schubert    sections.  There are two permissiable names.  The first is .debug_info.
2472*5796c8dcSSimon Schubert    This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
2473*5796c8dcSSimon Schubert    This is a variation on the .debug_info section which has a checksum
2474*5796c8dcSSimon Schubert    describing the contents appended onto the name.  This allows the linker to
2475*5796c8dcSSimon Schubert    identify and discard duplicate debugging sections for different
2476*5796c8dcSSimon Schubert    compilation units.  */
2477*5796c8dcSSimon Schubert #define DWARF2_DEBUG_INFO ".debug_info"
2478*5796c8dcSSimon Schubert #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2479*5796c8dcSSimon Schubert #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2480*5796c8dcSSimon Schubert 
2481*5796c8dcSSimon Schubert static asection *
2482*5796c8dcSSimon Schubert find_debug_info (bfd *abfd, asection *after_sec)
2483*5796c8dcSSimon Schubert {
2484*5796c8dcSSimon Schubert   asection * msec;
2485*5796c8dcSSimon Schubert 
2486*5796c8dcSSimon Schubert   msec = after_sec != NULL ? after_sec->next : abfd->sections;
2487*5796c8dcSSimon Schubert 
2488*5796c8dcSSimon Schubert   while (msec)
2489*5796c8dcSSimon Schubert     {
2490*5796c8dcSSimon Schubert       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2491*5796c8dcSSimon Schubert 	return msec;
2492*5796c8dcSSimon Schubert 
2493*5796c8dcSSimon Schubert       if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2494*5796c8dcSSimon Schubert 	return msec;
2495*5796c8dcSSimon Schubert 
2496*5796c8dcSSimon Schubert       if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2497*5796c8dcSSimon Schubert 	return msec;
2498*5796c8dcSSimon Schubert 
2499*5796c8dcSSimon Schubert       msec = msec->next;
2500*5796c8dcSSimon Schubert     }
2501*5796c8dcSSimon Schubert 
2502*5796c8dcSSimon Schubert   return NULL;
2503*5796c8dcSSimon Schubert }
2504*5796c8dcSSimon Schubert 
2505*5796c8dcSSimon Schubert /* Unset vmas for adjusted sections in STASH.  */
2506*5796c8dcSSimon Schubert 
2507*5796c8dcSSimon Schubert static void
2508*5796c8dcSSimon Schubert unset_sections (struct dwarf2_debug *stash)
2509*5796c8dcSSimon Schubert {
2510*5796c8dcSSimon Schubert   unsigned int i;
2511*5796c8dcSSimon Schubert   struct adjusted_section *p;
2512*5796c8dcSSimon Schubert 
2513*5796c8dcSSimon Schubert   i = stash->adjusted_section_count;
2514*5796c8dcSSimon Schubert   p = stash->adjusted_sections;
2515*5796c8dcSSimon Schubert   for (; i > 0; i--, p++)
2516*5796c8dcSSimon Schubert     p->section->vma = 0;
2517*5796c8dcSSimon Schubert }
2518*5796c8dcSSimon Schubert 
2519*5796c8dcSSimon Schubert /* Set unique VMAs for loadable and DWARF sections in ABFD and save
2520*5796c8dcSSimon Schubert    VMAs in STASH for unset_sections.  */
2521*5796c8dcSSimon Schubert 
2522*5796c8dcSSimon Schubert static bfd_boolean
2523*5796c8dcSSimon Schubert place_sections (bfd *abfd, struct dwarf2_debug *stash)
2524*5796c8dcSSimon Schubert {
2525*5796c8dcSSimon Schubert   struct adjusted_section *p;
2526*5796c8dcSSimon Schubert   unsigned int i;
2527*5796c8dcSSimon Schubert 
2528*5796c8dcSSimon Schubert   if (stash->adjusted_section_count != 0)
2529*5796c8dcSSimon Schubert     {
2530*5796c8dcSSimon Schubert       i = stash->adjusted_section_count;
2531*5796c8dcSSimon Schubert       p = stash->adjusted_sections;
2532*5796c8dcSSimon Schubert       for (; i > 0; i--, p++)
2533*5796c8dcSSimon Schubert 	p->section->vma = p->adj_vma;
2534*5796c8dcSSimon Schubert     }
2535*5796c8dcSSimon Schubert   else
2536*5796c8dcSSimon Schubert     {
2537*5796c8dcSSimon Schubert       asection *sect;
2538*5796c8dcSSimon Schubert       bfd_vma last_vma = 0, last_dwarf = 0;
2539*5796c8dcSSimon Schubert       bfd_size_type amt;
2540*5796c8dcSSimon Schubert       struct adjusted_section *p;
2541*5796c8dcSSimon Schubert 
2542*5796c8dcSSimon Schubert       i = 0;
2543*5796c8dcSSimon Schubert       for (sect = abfd->sections; sect != NULL; sect = sect->next)
2544*5796c8dcSSimon Schubert 	{
2545*5796c8dcSSimon Schubert 	  bfd_size_type sz;
2546*5796c8dcSSimon Schubert 	  int is_debug_info;
2547*5796c8dcSSimon Schubert 
2548*5796c8dcSSimon Schubert 	  if (sect->vma != 0)
2549*5796c8dcSSimon Schubert 	    continue;
2550*5796c8dcSSimon Schubert 
2551*5796c8dcSSimon Schubert 	  /* We need to adjust the VMAs of any .debug_info sections.
2552*5796c8dcSSimon Schubert 	     Skip compressed ones, since no relocations could target
2553*5796c8dcSSimon Schubert 	     them - they should not appear in object files anyway.  */
2554*5796c8dcSSimon Schubert 	  if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
2555*5796c8dcSSimon Schubert 	    is_debug_info = 1;
2556*5796c8dcSSimon Schubert 	  else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2557*5796c8dcSSimon Schubert 	    is_debug_info = 1;
2558*5796c8dcSSimon Schubert 	  else
2559*5796c8dcSSimon Schubert 	    is_debug_info = 0;
2560*5796c8dcSSimon Schubert 
2561*5796c8dcSSimon Schubert 	  if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2562*5796c8dcSSimon Schubert 	    continue;
2563*5796c8dcSSimon Schubert 
2564*5796c8dcSSimon Schubert 	  sz = sect->rawsize ? sect->rawsize : sect->size;
2565*5796c8dcSSimon Schubert 	  if (sz == 0)
2566*5796c8dcSSimon Schubert 	    continue;
2567*5796c8dcSSimon Schubert 
2568*5796c8dcSSimon Schubert 	  i++;
2569*5796c8dcSSimon Schubert 	}
2570*5796c8dcSSimon Schubert 
2571*5796c8dcSSimon Schubert       amt = i * sizeof (struct adjusted_section);
2572*5796c8dcSSimon Schubert       p = (struct adjusted_section *) bfd_zalloc (abfd, amt);
2573*5796c8dcSSimon Schubert       if (! p)
2574*5796c8dcSSimon Schubert 	return FALSE;
2575*5796c8dcSSimon Schubert 
2576*5796c8dcSSimon Schubert       stash->adjusted_sections = p;
2577*5796c8dcSSimon Schubert       stash->adjusted_section_count = i;
2578*5796c8dcSSimon Schubert 
2579*5796c8dcSSimon Schubert       for (sect = abfd->sections; sect != NULL; sect = sect->next)
2580*5796c8dcSSimon Schubert 	{
2581*5796c8dcSSimon Schubert 	  bfd_size_type sz;
2582*5796c8dcSSimon Schubert 	  int is_debug_info;
2583*5796c8dcSSimon Schubert 
2584*5796c8dcSSimon Schubert 	  if (sect->vma != 0)
2585*5796c8dcSSimon Schubert 	    continue;
2586*5796c8dcSSimon Schubert 
2587*5796c8dcSSimon Schubert 	  /* We need to adjust the VMAs of any .debug_info sections.
2588*5796c8dcSSimon Schubert 	     Skip compressed ones, since no relocations could target
2589*5796c8dcSSimon Schubert 	     them - they should not appear in object files anyway.  */
2590*5796c8dcSSimon Schubert 	  if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
2591*5796c8dcSSimon Schubert 	    is_debug_info = 1;
2592*5796c8dcSSimon Schubert 	  else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2593*5796c8dcSSimon Schubert 	    is_debug_info = 1;
2594*5796c8dcSSimon Schubert 	  else
2595*5796c8dcSSimon Schubert 	    is_debug_info = 0;
2596*5796c8dcSSimon Schubert 
2597*5796c8dcSSimon Schubert 	  if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2598*5796c8dcSSimon Schubert 	    continue;
2599*5796c8dcSSimon Schubert 
2600*5796c8dcSSimon Schubert 	  sz = sect->rawsize ? sect->rawsize : sect->size;
2601*5796c8dcSSimon Schubert 	  if (sz == 0)
2602*5796c8dcSSimon Schubert 	    continue;
2603*5796c8dcSSimon Schubert 
2604*5796c8dcSSimon Schubert 	  p->section = sect;
2605*5796c8dcSSimon Schubert 	  if (is_debug_info)
2606*5796c8dcSSimon Schubert 	    {
2607*5796c8dcSSimon Schubert 	      BFD_ASSERT (sect->alignment_power == 0);
2608*5796c8dcSSimon Schubert 	      sect->vma = last_dwarf;
2609*5796c8dcSSimon Schubert 	      last_dwarf += sz;
2610*5796c8dcSSimon Schubert 	    }
2611*5796c8dcSSimon Schubert 	  else if (last_vma != 0)
2612*5796c8dcSSimon Schubert 	    {
2613*5796c8dcSSimon Schubert 	      /* Align the new address to the current section
2614*5796c8dcSSimon Schubert 		 alignment.  */
2615*5796c8dcSSimon Schubert 	      last_vma = ((last_vma
2616*5796c8dcSSimon Schubert 			   + ~((bfd_vma) -1 << sect->alignment_power))
2617*5796c8dcSSimon Schubert 			  & ((bfd_vma) -1 << sect->alignment_power));
2618*5796c8dcSSimon Schubert 	      sect->vma = last_vma;
2619*5796c8dcSSimon Schubert 	      last_vma += sect->vma + sz;
2620*5796c8dcSSimon Schubert 	    }
2621*5796c8dcSSimon Schubert 	  else
2622*5796c8dcSSimon Schubert 	    last_vma += sect->vma + sz;
2623*5796c8dcSSimon Schubert 
2624*5796c8dcSSimon Schubert 	  p->adj_vma = sect->vma;
2625*5796c8dcSSimon Schubert 
2626*5796c8dcSSimon Schubert 	  p++;
2627*5796c8dcSSimon Schubert 	}
2628*5796c8dcSSimon Schubert     }
2629*5796c8dcSSimon Schubert 
2630*5796c8dcSSimon Schubert   return TRUE;
2631*5796c8dcSSimon Schubert }
2632*5796c8dcSSimon Schubert 
2633*5796c8dcSSimon Schubert /* Look up a funcinfo by name using the given info hash table.  If found,
2634*5796c8dcSSimon Schubert    also update the locations pointed to by filename_ptr and linenumber_ptr.
2635*5796c8dcSSimon Schubert 
2636*5796c8dcSSimon Schubert    This function returns TRUE if a funcinfo that matches the given symbol
2637*5796c8dcSSimon Schubert    and address is found with any error; otherwise it returns FALSE.  */
2638*5796c8dcSSimon Schubert 
2639*5796c8dcSSimon Schubert static bfd_boolean
2640*5796c8dcSSimon Schubert info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2641*5796c8dcSSimon Schubert 			   asymbol *sym,
2642*5796c8dcSSimon Schubert 			   bfd_vma addr,
2643*5796c8dcSSimon Schubert 			   const char **filename_ptr,
2644*5796c8dcSSimon Schubert 			   unsigned int *linenumber_ptr)
2645*5796c8dcSSimon Schubert {
2646*5796c8dcSSimon Schubert   struct funcinfo* each_func;
2647*5796c8dcSSimon Schubert   struct funcinfo* best_fit = NULL;
2648*5796c8dcSSimon Schubert   struct info_list_node *node;
2649*5796c8dcSSimon Schubert   struct arange *arange;
2650*5796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
2651*5796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
2652*5796c8dcSSimon Schubert 
2653*5796c8dcSSimon Schubert   for (node = lookup_info_hash_table (hash_table, name);
2654*5796c8dcSSimon Schubert        node;
2655*5796c8dcSSimon Schubert        node = node->next)
2656*5796c8dcSSimon Schubert     {
2657*5796c8dcSSimon Schubert       each_func = (struct funcinfo *) node->info;
2658*5796c8dcSSimon Schubert       for (arange = &each_func->arange;
2659*5796c8dcSSimon Schubert 	   arange;
2660*5796c8dcSSimon Schubert 	   arange = arange->next)
2661*5796c8dcSSimon Schubert 	{
2662*5796c8dcSSimon Schubert 	  if ((!each_func->sec || each_func->sec == sec)
2663*5796c8dcSSimon Schubert 	      && addr >= arange->low
2664*5796c8dcSSimon Schubert 	      && addr < arange->high
2665*5796c8dcSSimon Schubert 	      && (!best_fit
2666*5796c8dcSSimon Schubert 		  || ((arange->high - arange->low)
2667*5796c8dcSSimon Schubert 		      < (best_fit->arange.high - best_fit->arange.low))))
2668*5796c8dcSSimon Schubert 	    best_fit = each_func;
2669*5796c8dcSSimon Schubert 	}
2670*5796c8dcSSimon Schubert     }
2671*5796c8dcSSimon Schubert 
2672*5796c8dcSSimon Schubert   if (best_fit)
2673*5796c8dcSSimon Schubert     {
2674*5796c8dcSSimon Schubert       best_fit->sec = sec;
2675*5796c8dcSSimon Schubert       *filename_ptr = best_fit->file;
2676*5796c8dcSSimon Schubert       *linenumber_ptr = best_fit->line;
2677*5796c8dcSSimon Schubert       return TRUE;
2678*5796c8dcSSimon Schubert     }
2679*5796c8dcSSimon Schubert 
2680*5796c8dcSSimon Schubert   return FALSE;
2681*5796c8dcSSimon Schubert }
2682*5796c8dcSSimon Schubert 
2683*5796c8dcSSimon Schubert /* Look up a varinfo by name using the given info hash table.  If found,
2684*5796c8dcSSimon Schubert    also update the locations pointed to by filename_ptr and linenumber_ptr.
2685*5796c8dcSSimon Schubert 
2686*5796c8dcSSimon Schubert    This function returns TRUE if a varinfo that matches the given symbol
2687*5796c8dcSSimon Schubert    and address is found with any error; otherwise it returns FALSE.  */
2688*5796c8dcSSimon Schubert 
2689*5796c8dcSSimon Schubert static bfd_boolean
2690*5796c8dcSSimon Schubert info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2691*5796c8dcSSimon Schubert 			  asymbol *sym,
2692*5796c8dcSSimon Schubert 			  bfd_vma addr,
2693*5796c8dcSSimon Schubert 			  const char **filename_ptr,
2694*5796c8dcSSimon Schubert 			  unsigned int *linenumber_ptr)
2695*5796c8dcSSimon Schubert {
2696*5796c8dcSSimon Schubert   const char *name = bfd_asymbol_name (sym);
2697*5796c8dcSSimon Schubert   asection *sec = bfd_get_section (sym);
2698*5796c8dcSSimon Schubert   struct varinfo* each;
2699*5796c8dcSSimon Schubert   struct info_list_node *node;
2700*5796c8dcSSimon Schubert 
2701*5796c8dcSSimon Schubert   for (node = lookup_info_hash_table (hash_table, name);
2702*5796c8dcSSimon Schubert        node;
2703*5796c8dcSSimon Schubert        node = node->next)
2704*5796c8dcSSimon Schubert     {
2705*5796c8dcSSimon Schubert       each = (struct varinfo *) node->info;
2706*5796c8dcSSimon Schubert       if (each->addr == addr
2707*5796c8dcSSimon Schubert 	  && (!each->sec || each->sec == sec))
2708*5796c8dcSSimon Schubert 	{
2709*5796c8dcSSimon Schubert 	  each->sec = sec;
2710*5796c8dcSSimon Schubert 	  *filename_ptr = each->file;
2711*5796c8dcSSimon Schubert 	  *linenumber_ptr = each->line;
2712*5796c8dcSSimon Schubert 	  return TRUE;
2713*5796c8dcSSimon Schubert 	}
2714*5796c8dcSSimon Schubert     }
2715*5796c8dcSSimon Schubert 
2716*5796c8dcSSimon Schubert   return FALSE;
2717*5796c8dcSSimon Schubert }
2718*5796c8dcSSimon Schubert 
2719*5796c8dcSSimon Schubert /* Update the funcinfo and varinfo info hash tables if they are
2720*5796c8dcSSimon Schubert    not up to date.  Returns TRUE if there is no error; otherwise
2721*5796c8dcSSimon Schubert    returns FALSE and disable the info hash tables.  */
2722*5796c8dcSSimon Schubert 
2723*5796c8dcSSimon Schubert static bfd_boolean
2724*5796c8dcSSimon Schubert stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2725*5796c8dcSSimon Schubert {
2726*5796c8dcSSimon Schubert   struct comp_unit *each;
2727*5796c8dcSSimon Schubert 
2728*5796c8dcSSimon Schubert   /* Exit if hash tables are up-to-date.  */
2729*5796c8dcSSimon Schubert   if (stash->all_comp_units == stash->hash_units_head)
2730*5796c8dcSSimon Schubert     return TRUE;
2731*5796c8dcSSimon Schubert 
2732*5796c8dcSSimon Schubert   if (stash->hash_units_head)
2733*5796c8dcSSimon Schubert     each = stash->hash_units_head->prev_unit;
2734*5796c8dcSSimon Schubert   else
2735*5796c8dcSSimon Schubert     each = stash->last_comp_unit;
2736*5796c8dcSSimon Schubert 
2737*5796c8dcSSimon Schubert   while (each)
2738*5796c8dcSSimon Schubert     {
2739*5796c8dcSSimon Schubert       if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2740*5796c8dcSSimon Schubert 				stash->varinfo_hash_table))
2741*5796c8dcSSimon Schubert 	{
2742*5796c8dcSSimon Schubert 	  stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2743*5796c8dcSSimon Schubert 	  return FALSE;
2744*5796c8dcSSimon Schubert 	}
2745*5796c8dcSSimon Schubert       each = each->prev_unit;
2746*5796c8dcSSimon Schubert     }
2747*5796c8dcSSimon Schubert 
2748*5796c8dcSSimon Schubert   stash->hash_units_head = stash->all_comp_units;
2749*5796c8dcSSimon Schubert   return TRUE;
2750*5796c8dcSSimon Schubert }
2751*5796c8dcSSimon Schubert 
2752*5796c8dcSSimon Schubert /* Check consistency of info hash tables.  This is for debugging only. */
2753*5796c8dcSSimon Schubert 
2754*5796c8dcSSimon Schubert static void ATTRIBUTE_UNUSED
2755*5796c8dcSSimon Schubert stash_verify_info_hash_table (struct dwarf2_debug *stash)
2756*5796c8dcSSimon Schubert {
2757*5796c8dcSSimon Schubert   struct comp_unit *each_unit;
2758*5796c8dcSSimon Schubert   struct funcinfo *each_func;
2759*5796c8dcSSimon Schubert   struct varinfo *each_var;
2760*5796c8dcSSimon Schubert   struct info_list_node *node;
2761*5796c8dcSSimon Schubert   bfd_boolean found;
2762*5796c8dcSSimon Schubert 
2763*5796c8dcSSimon Schubert   for (each_unit = stash->all_comp_units;
2764*5796c8dcSSimon Schubert        each_unit;
2765*5796c8dcSSimon Schubert        each_unit = each_unit->next_unit)
2766*5796c8dcSSimon Schubert     {
2767*5796c8dcSSimon Schubert       for (each_func = each_unit->function_table;
2768*5796c8dcSSimon Schubert 	   each_func;
2769*5796c8dcSSimon Schubert 	   each_func = each_func->prev_func)
2770*5796c8dcSSimon Schubert 	{
2771*5796c8dcSSimon Schubert 	  if (!each_func->name)
2772*5796c8dcSSimon Schubert 	    continue;
2773*5796c8dcSSimon Schubert 	  node = lookup_info_hash_table (stash->funcinfo_hash_table,
2774*5796c8dcSSimon Schubert 					 each_func->name);
2775*5796c8dcSSimon Schubert 	  BFD_ASSERT (node);
2776*5796c8dcSSimon Schubert 	  found = FALSE;
2777*5796c8dcSSimon Schubert 	  while (node && !found)
2778*5796c8dcSSimon Schubert 	    {
2779*5796c8dcSSimon Schubert 	      found = node->info == each_func;
2780*5796c8dcSSimon Schubert 	      node = node->next;
2781*5796c8dcSSimon Schubert 	    }
2782*5796c8dcSSimon Schubert 	  BFD_ASSERT (found);
2783*5796c8dcSSimon Schubert 	}
2784*5796c8dcSSimon Schubert 
2785*5796c8dcSSimon Schubert       for (each_var = each_unit->variable_table;
2786*5796c8dcSSimon Schubert 	   each_var;
2787*5796c8dcSSimon Schubert 	   each_var = each_var->prev_var)
2788*5796c8dcSSimon Schubert 	{
2789*5796c8dcSSimon Schubert 	  if (!each_var->name || !each_var->file || each_var->stack)
2790*5796c8dcSSimon Schubert 	    continue;
2791*5796c8dcSSimon Schubert 	  node = lookup_info_hash_table (stash->varinfo_hash_table,
2792*5796c8dcSSimon Schubert 					 each_var->name);
2793*5796c8dcSSimon Schubert 	  BFD_ASSERT (node);
2794*5796c8dcSSimon Schubert 	  found = FALSE;
2795*5796c8dcSSimon Schubert 	  while (node && !found)
2796*5796c8dcSSimon Schubert 	    {
2797*5796c8dcSSimon Schubert 	      found = node->info == each_var;
2798*5796c8dcSSimon Schubert 	      node = node->next;
2799*5796c8dcSSimon Schubert 	    }
2800*5796c8dcSSimon Schubert 	  BFD_ASSERT (found);
2801*5796c8dcSSimon Schubert 	}
2802*5796c8dcSSimon Schubert     }
2803*5796c8dcSSimon Schubert }
2804*5796c8dcSSimon Schubert 
2805*5796c8dcSSimon Schubert /* Check to see if we want to enable the info hash tables, which consume
2806*5796c8dcSSimon Schubert    quite a bit of memory.  Currently we only check the number times
2807*5796c8dcSSimon Schubert    bfd_dwarf2_find_line is called.  In the future, we may also want to
2808*5796c8dcSSimon Schubert    take the number of symbols into account.  */
2809*5796c8dcSSimon Schubert 
2810*5796c8dcSSimon Schubert static void
2811*5796c8dcSSimon Schubert stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2812*5796c8dcSSimon Schubert {
2813*5796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2814*5796c8dcSSimon Schubert 
2815*5796c8dcSSimon Schubert   if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2816*5796c8dcSSimon Schubert     return;
2817*5796c8dcSSimon Schubert 
2818*5796c8dcSSimon Schubert   /* FIXME: Maybe we should check the reduce_memory_overheads
2819*5796c8dcSSimon Schubert      and optimize fields in the bfd_link_info structure ?  */
2820*5796c8dcSSimon Schubert 
2821*5796c8dcSSimon Schubert   /* Create hash tables.  */
2822*5796c8dcSSimon Schubert   stash->funcinfo_hash_table = create_info_hash_table (abfd);
2823*5796c8dcSSimon Schubert   stash->varinfo_hash_table = create_info_hash_table (abfd);
2824*5796c8dcSSimon Schubert   if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2825*5796c8dcSSimon Schubert     {
2826*5796c8dcSSimon Schubert       /* Turn off info hashes if any allocation above fails.  */
2827*5796c8dcSSimon Schubert       stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2828*5796c8dcSSimon Schubert       return;
2829*5796c8dcSSimon Schubert     }
2830*5796c8dcSSimon Schubert   /* We need a forced update so that the info hash tables will
2831*5796c8dcSSimon Schubert      be created even though there is no compilation unit.  That
2832*5796c8dcSSimon Schubert      happens if STASH_INFO_HASH_TRIGGER is 0.  */
2833*5796c8dcSSimon Schubert   stash_maybe_update_info_hash_tables (stash);
2834*5796c8dcSSimon Schubert   stash->info_hash_status = STASH_INFO_HASH_ON;
2835*5796c8dcSSimon Schubert }
2836*5796c8dcSSimon Schubert 
2837*5796c8dcSSimon Schubert /* Find the file and line associated with a symbol and address using the
2838*5796c8dcSSimon Schubert    info hash tables of a stash. If there is a match, the function returns
2839*5796c8dcSSimon Schubert    TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2840*5796c8dcSSimon Schubert    otherwise it returns FALSE.  */
2841*5796c8dcSSimon Schubert 
2842*5796c8dcSSimon Schubert static bfd_boolean
2843*5796c8dcSSimon Schubert stash_find_line_fast (struct dwarf2_debug *stash,
2844*5796c8dcSSimon Schubert 		      asymbol *sym,
2845*5796c8dcSSimon Schubert 		      bfd_vma addr,
2846*5796c8dcSSimon Schubert 		      const char **filename_ptr,
2847*5796c8dcSSimon Schubert 		      unsigned int *linenumber_ptr)
2848*5796c8dcSSimon Schubert {
2849*5796c8dcSSimon Schubert   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2850*5796c8dcSSimon Schubert 
2851*5796c8dcSSimon Schubert   if (sym->flags & BSF_FUNCTION)
2852*5796c8dcSSimon Schubert     return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2853*5796c8dcSSimon Schubert 				      filename_ptr, linenumber_ptr);
2854*5796c8dcSSimon Schubert   return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2855*5796c8dcSSimon Schubert 				   filename_ptr, linenumber_ptr);
2856*5796c8dcSSimon Schubert }
2857*5796c8dcSSimon Schubert 
2858*5796c8dcSSimon Schubert /* Find the source code location of SYMBOL.  If SYMBOL is NULL
2859*5796c8dcSSimon Schubert    then find the nearest source code location corresponding to
2860*5796c8dcSSimon Schubert    the address SECTION + OFFSET.
2861*5796c8dcSSimon Schubert    Returns TRUE if the line is found without error and fills in
2862*5796c8dcSSimon Schubert    FILENAME_PTR and LINENUMBER_PTR.  In the case where SYMBOL was
2863*5796c8dcSSimon Schubert    NULL the FUNCTIONNAME_PTR is also filled in.
2864*5796c8dcSSimon Schubert    SYMBOLS contains the symbol table for ABFD.
2865*5796c8dcSSimon Schubert    ADDR_SIZE is the number of bytes in the initial .debug_info length
2866*5796c8dcSSimon Schubert    field and in the abbreviation offset, or zero to indicate that the
2867*5796c8dcSSimon Schubert    default value should be used.  */
2868*5796c8dcSSimon Schubert 
2869*5796c8dcSSimon Schubert static bfd_boolean
2870*5796c8dcSSimon Schubert find_line (bfd *abfd,
2871*5796c8dcSSimon Schubert 	   asection *section,
2872*5796c8dcSSimon Schubert 	   bfd_vma offset,
2873*5796c8dcSSimon Schubert 	   asymbol *symbol,
2874*5796c8dcSSimon Schubert 	   asymbol **symbols,
2875*5796c8dcSSimon Schubert 	   const char **filename_ptr,
2876*5796c8dcSSimon Schubert 	   const char **functionname_ptr,
2877*5796c8dcSSimon Schubert 	   unsigned int *linenumber_ptr,
2878*5796c8dcSSimon Schubert 	   unsigned int addr_size,
2879*5796c8dcSSimon Schubert 	   void **pinfo)
2880*5796c8dcSSimon Schubert {
2881*5796c8dcSSimon Schubert   /* Read each compilation unit from the section .debug_info, and check
2882*5796c8dcSSimon Schubert      to see if it contains the address we are searching for.  If yes,
2883*5796c8dcSSimon Schubert      lookup the address, and return the line number info.  If no, go
2884*5796c8dcSSimon Schubert      on to the next compilation unit.
2885*5796c8dcSSimon Schubert 
2886*5796c8dcSSimon Schubert      We keep a list of all the previously read compilation units, and
2887*5796c8dcSSimon Schubert      a pointer to the next un-read compilation unit.  Check the
2888*5796c8dcSSimon Schubert      previously read units before reading more.  */
2889*5796c8dcSSimon Schubert   struct dwarf2_debug *stash;
2890*5796c8dcSSimon Schubert   /* What address are we looking for?  */
2891*5796c8dcSSimon Schubert   bfd_vma addr;
2892*5796c8dcSSimon Schubert   struct comp_unit* each;
2893*5796c8dcSSimon Schubert   bfd_vma found = FALSE;
2894*5796c8dcSSimon Schubert   bfd_boolean do_line;
2895*5796c8dcSSimon Schubert 
2896*5796c8dcSSimon Schubert   stash = (struct dwarf2_debug *) *pinfo;
2897*5796c8dcSSimon Schubert 
2898*5796c8dcSSimon Schubert   if (! stash)
2899*5796c8dcSSimon Schubert     {
2900*5796c8dcSSimon Schubert       bfd_size_type amt = sizeof (struct dwarf2_debug);
2901*5796c8dcSSimon Schubert 
2902*5796c8dcSSimon Schubert       stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
2903*5796c8dcSSimon Schubert       if (! stash)
2904*5796c8dcSSimon Schubert 	return FALSE;
2905*5796c8dcSSimon Schubert     }
2906*5796c8dcSSimon Schubert 
2907*5796c8dcSSimon Schubert   /* In a relocatable file, 2 functions may have the same address.
2908*5796c8dcSSimon Schubert      We change the section vma so that they won't overlap.  */
2909*5796c8dcSSimon Schubert   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2910*5796c8dcSSimon Schubert     {
2911*5796c8dcSSimon Schubert       if (! place_sections (abfd, stash))
2912*5796c8dcSSimon Schubert 	return FALSE;
2913*5796c8dcSSimon Schubert     }
2914*5796c8dcSSimon Schubert 
2915*5796c8dcSSimon Schubert   do_line = (section == NULL
2916*5796c8dcSSimon Schubert 	     && offset == 0
2917*5796c8dcSSimon Schubert 	     && functionname_ptr == NULL
2918*5796c8dcSSimon Schubert 	     && symbol != NULL);
2919*5796c8dcSSimon Schubert   if (do_line)
2920*5796c8dcSSimon Schubert     {
2921*5796c8dcSSimon Schubert       addr = symbol->value;
2922*5796c8dcSSimon Schubert       section = bfd_get_section (symbol);
2923*5796c8dcSSimon Schubert     }
2924*5796c8dcSSimon Schubert   else if (section != NULL
2925*5796c8dcSSimon Schubert 	   && functionname_ptr != NULL
2926*5796c8dcSSimon Schubert 	   && symbol == NULL)
2927*5796c8dcSSimon Schubert     addr = offset;
2928*5796c8dcSSimon Schubert   else
2929*5796c8dcSSimon Schubert     abort ();
2930*5796c8dcSSimon Schubert 
2931*5796c8dcSSimon Schubert   if (section->output_section)
2932*5796c8dcSSimon Schubert     addr += section->output_section->vma + section->output_offset;
2933*5796c8dcSSimon Schubert   else
2934*5796c8dcSSimon Schubert     addr += section->vma;
2935*5796c8dcSSimon Schubert   *filename_ptr = NULL;
2936*5796c8dcSSimon Schubert   if (! do_line)
2937*5796c8dcSSimon Schubert     *functionname_ptr = NULL;
2938*5796c8dcSSimon Schubert   *linenumber_ptr = 0;
2939*5796c8dcSSimon Schubert 
2940*5796c8dcSSimon Schubert   if (! *pinfo)
2941*5796c8dcSSimon Schubert     {
2942*5796c8dcSSimon Schubert       bfd *debug_bfd;
2943*5796c8dcSSimon Schubert       bfd_size_type total_size;
2944*5796c8dcSSimon Schubert       asection *msec;
2945*5796c8dcSSimon Schubert 
2946*5796c8dcSSimon Schubert       *pinfo = stash;
2947*5796c8dcSSimon Schubert 
2948*5796c8dcSSimon Schubert       msec = find_debug_info (abfd, NULL);
2949*5796c8dcSSimon Schubert       if (msec == NULL)
2950*5796c8dcSSimon Schubert 	{
2951*5796c8dcSSimon Schubert 	  char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2952*5796c8dcSSimon Schubert 
2953*5796c8dcSSimon Schubert 	  if (debug_filename == NULL)
2954*5796c8dcSSimon Schubert 	    /* No dwarf2 info, and no gnu_debuglink to follow.
2955*5796c8dcSSimon Schubert 	       Note that at this point the stash has been allocated, but
2956*5796c8dcSSimon Schubert 	       contains zeros.  This lets future calls to this function
2957*5796c8dcSSimon Schubert 	       fail more quickly.  */
2958*5796c8dcSSimon Schubert 	    goto done;
2959*5796c8dcSSimon Schubert 
2960*5796c8dcSSimon Schubert 	  if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2961*5796c8dcSSimon Schubert 	      || ! bfd_check_format (debug_bfd, bfd_object)
2962*5796c8dcSSimon Schubert 	      || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2963*5796c8dcSSimon Schubert 	    {
2964*5796c8dcSSimon Schubert 	      if (debug_bfd)
2965*5796c8dcSSimon Schubert 		bfd_close (debug_bfd);
2966*5796c8dcSSimon Schubert 	      /* FIXME: Should we report our failure to follow the debuglink ?  */
2967*5796c8dcSSimon Schubert 	      free (debug_filename);
2968*5796c8dcSSimon Schubert 	      goto done;
2969*5796c8dcSSimon Schubert 	    }
2970*5796c8dcSSimon Schubert 	}
2971*5796c8dcSSimon Schubert       else
2972*5796c8dcSSimon Schubert 	debug_bfd = abfd;
2973*5796c8dcSSimon Schubert 
2974*5796c8dcSSimon Schubert       /* There can be more than one DWARF2 info section in a BFD these
2975*5796c8dcSSimon Schubert 	 days.  First handle the easy case when there's only one.  If
2976*5796c8dcSSimon Schubert 	 there's more than one, try case two: none of the sections is
2977*5796c8dcSSimon Schubert 	 compressed.  In that case, read them all in and produce one
2978*5796c8dcSSimon Schubert 	 large stash.  We do this in two passes - in the first pass we
2979*5796c8dcSSimon Schubert 	 just accumulate the section sizes, and in the second pass we
2980*5796c8dcSSimon Schubert 	 read in the section's contents.  (The allows us to avoid
2981*5796c8dcSSimon Schubert 	 reallocing the data as we add sections to the stash.)  If
2982*5796c8dcSSimon Schubert 	 some or all sections are compressed, then do things the slow
2983*5796c8dcSSimon Schubert 	 way, with a bunch of reallocs.  */
2984*5796c8dcSSimon Schubert 
2985*5796c8dcSSimon Schubert       if (! find_debug_info (debug_bfd, msec))
2986*5796c8dcSSimon Schubert 	{
2987*5796c8dcSSimon Schubert 	  /* Case 1: only one info section.  */
2988*5796c8dcSSimon Schubert 	  total_size = msec->size;
2989*5796c8dcSSimon Schubert 	  if (! read_section (debug_bfd, ".debug_info", ".zdebug_info",
2990*5796c8dcSSimon Schubert 			      symbols, 0,
2991*5796c8dcSSimon Schubert 			      &stash->info_ptr_memory, &total_size))
2992*5796c8dcSSimon Schubert 	    goto done;
2993*5796c8dcSSimon Schubert 	}
2994*5796c8dcSSimon Schubert       else
2995*5796c8dcSSimon Schubert 	{
2996*5796c8dcSSimon Schubert 	  int all_uncompressed = 1;
2997*5796c8dcSSimon Schubert 	  for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
2998*5796c8dcSSimon Schubert 	    {
2999*5796c8dcSSimon Schubert 	      total_size += msec->size;
3000*5796c8dcSSimon Schubert 	      if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
3001*5796c8dcSSimon Schubert 		all_uncompressed = 0;
3002*5796c8dcSSimon Schubert 	    }
3003*5796c8dcSSimon Schubert 	  if (all_uncompressed)
3004*5796c8dcSSimon Schubert 	    {
3005*5796c8dcSSimon Schubert 	      /* Case 2: multiple sections, but none is compressed.  */
3006*5796c8dcSSimon Schubert 	      stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
3007*5796c8dcSSimon Schubert 	      if (stash->info_ptr_memory == NULL)
3008*5796c8dcSSimon Schubert 		goto done;
3009*5796c8dcSSimon Schubert 
3010*5796c8dcSSimon Schubert 	      total_size = 0;
3011*5796c8dcSSimon Schubert 	      for (msec = find_debug_info (debug_bfd, NULL);
3012*5796c8dcSSimon Schubert 		   msec;
3013*5796c8dcSSimon Schubert 		   msec = find_debug_info (debug_bfd, msec))
3014*5796c8dcSSimon Schubert 		{
3015*5796c8dcSSimon Schubert 		  bfd_size_type size;
3016*5796c8dcSSimon Schubert 
3017*5796c8dcSSimon Schubert 		  size = msec->size;
3018*5796c8dcSSimon Schubert 		  if (size == 0)
3019*5796c8dcSSimon Schubert 		    continue;
3020*5796c8dcSSimon Schubert 
3021*5796c8dcSSimon Schubert 		  if (!(bfd_simple_get_relocated_section_contents
3022*5796c8dcSSimon Schubert 			(debug_bfd, msec, stash->info_ptr_memory + total_size,
3023*5796c8dcSSimon Schubert 			 symbols)))
3024*5796c8dcSSimon Schubert 		    goto done;
3025*5796c8dcSSimon Schubert 
3026*5796c8dcSSimon Schubert 		  total_size += size;
3027*5796c8dcSSimon Schubert 		}
3028*5796c8dcSSimon Schubert 	    }
3029*5796c8dcSSimon Schubert 	  else
3030*5796c8dcSSimon Schubert 	    {
3031*5796c8dcSSimon Schubert 	      /* Case 3: multiple sections, some or all compressed.  */
3032*5796c8dcSSimon Schubert 	      stash->info_ptr_memory = NULL;
3033*5796c8dcSSimon Schubert 	      total_size = 0;
3034*5796c8dcSSimon Schubert 	      for (msec = find_debug_info (debug_bfd, NULL);
3035*5796c8dcSSimon Schubert 		   msec;
3036*5796c8dcSSimon Schubert 		   msec = find_debug_info (debug_bfd, msec))
3037*5796c8dcSSimon Schubert 		{
3038*5796c8dcSSimon Schubert 		  bfd_size_type size = msec->size;
3039*5796c8dcSSimon Schubert 		  bfd_byte* buffer;
3040*5796c8dcSSimon Schubert 
3041*5796c8dcSSimon Schubert 		  if (size == 0)
3042*5796c8dcSSimon Schubert 		    continue;
3043*5796c8dcSSimon Schubert 
3044*5796c8dcSSimon Schubert 		  buffer = (bfd_simple_get_relocated_section_contents
3045*5796c8dcSSimon Schubert 			    (debug_bfd, msec, NULL, symbols));
3046*5796c8dcSSimon Schubert 		  if (! buffer)
3047*5796c8dcSSimon Schubert 		    goto done;
3048*5796c8dcSSimon Schubert 
3049*5796c8dcSSimon Schubert 		  if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
3050*5796c8dcSSimon Schubert 		    {
3051*5796c8dcSSimon Schubert 		      if (! bfd_uncompress_section_contents (&buffer, &size))
3052*5796c8dcSSimon Schubert 			{
3053*5796c8dcSSimon Schubert 			  free (buffer);
3054*5796c8dcSSimon Schubert 			  goto done;
3055*5796c8dcSSimon Schubert 			}
3056*5796c8dcSSimon Schubert 		    }
3057*5796c8dcSSimon Schubert 		  stash->info_ptr_memory =  (bfd_byte *)
3058*5796c8dcSSimon Schubert                       bfd_realloc (stash->info_ptr_memory, total_size + size);
3059*5796c8dcSSimon Schubert 		  memcpy (stash->info_ptr_memory + total_size, buffer, size);
3060*5796c8dcSSimon Schubert 		  free (buffer);
3061*5796c8dcSSimon Schubert 		  total_size += size;
3062*5796c8dcSSimon Schubert 		}
3063*5796c8dcSSimon Schubert 	    }
3064*5796c8dcSSimon Schubert 	}
3065*5796c8dcSSimon Schubert 
3066*5796c8dcSSimon Schubert       stash->info_ptr = stash->info_ptr_memory;
3067*5796c8dcSSimon Schubert       stash->info_ptr_end = stash->info_ptr + total_size;
3068*5796c8dcSSimon Schubert       stash->sec = find_debug_info (debug_bfd, NULL);
3069*5796c8dcSSimon Schubert       stash->sec_info_ptr = stash->info_ptr;
3070*5796c8dcSSimon Schubert       stash->syms = symbols;
3071*5796c8dcSSimon Schubert       stash->bfd_ptr = debug_bfd;
3072*5796c8dcSSimon Schubert     }
3073*5796c8dcSSimon Schubert 
3074*5796c8dcSSimon Schubert   /* A null info_ptr indicates that there is no dwarf2 info
3075*5796c8dcSSimon Schubert      (or that an error occured while setting up the stash).  */
3076*5796c8dcSSimon Schubert   if (! stash->info_ptr)
3077*5796c8dcSSimon Schubert     goto done;
3078*5796c8dcSSimon Schubert 
3079*5796c8dcSSimon Schubert   stash->inliner_chain = NULL;
3080*5796c8dcSSimon Schubert 
3081*5796c8dcSSimon Schubert   /* Check the previously read comp. units first.  */
3082*5796c8dcSSimon Schubert   if (do_line)
3083*5796c8dcSSimon Schubert     {
3084*5796c8dcSSimon Schubert       /* The info hash tables use quite a bit of memory.  We may not want to
3085*5796c8dcSSimon Schubert 	 always use them.  We use some heuristics to decide if and when to
3086*5796c8dcSSimon Schubert 	 turn it on.  */
3087*5796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3088*5796c8dcSSimon Schubert 	stash_maybe_enable_info_hash_tables (abfd, stash);
3089*5796c8dcSSimon Schubert 
3090*5796c8dcSSimon Schubert       /* Keep info hash table up to date if they are available.  Note that we
3091*5796c8dcSSimon Schubert 	 may disable the hash tables if there is any error duing update. */
3092*5796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_ON)
3093*5796c8dcSSimon Schubert 	stash_maybe_update_info_hash_tables (stash);
3094*5796c8dcSSimon Schubert 
3095*5796c8dcSSimon Schubert       if (stash->info_hash_status == STASH_INFO_HASH_ON)
3096*5796c8dcSSimon Schubert 	{
3097*5796c8dcSSimon Schubert 	  found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3098*5796c8dcSSimon Schubert 					linenumber_ptr);
3099*5796c8dcSSimon Schubert 	  if (found)
3100*5796c8dcSSimon Schubert 	    goto done;
3101*5796c8dcSSimon Schubert 	}
3102*5796c8dcSSimon Schubert       else
3103*5796c8dcSSimon Schubert 	{
3104*5796c8dcSSimon Schubert 	  /* Check the previously read comp. units first.  */
3105*5796c8dcSSimon Schubert 	  for (each = stash->all_comp_units; each; each = each->next_unit)
3106*5796c8dcSSimon Schubert 	    if ((symbol->flags & BSF_FUNCTION) == 0
3107*5796c8dcSSimon Schubert 		|| comp_unit_contains_address (each, addr))
3108*5796c8dcSSimon Schubert 	      {
3109*5796c8dcSSimon Schubert 		found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3110*5796c8dcSSimon Schubert 					     linenumber_ptr, stash);
3111*5796c8dcSSimon Schubert 		if (found)
3112*5796c8dcSSimon Schubert 		  goto done;
3113*5796c8dcSSimon Schubert 	      }
3114*5796c8dcSSimon Schubert 	}
3115*5796c8dcSSimon Schubert     }
3116*5796c8dcSSimon Schubert   else
3117*5796c8dcSSimon Schubert     {
3118*5796c8dcSSimon Schubert       for (each = stash->all_comp_units; each; each = each->next_unit)
3119*5796c8dcSSimon Schubert 	{
3120*5796c8dcSSimon Schubert 	  found = (comp_unit_contains_address (each, addr)
3121*5796c8dcSSimon Schubert 		   && comp_unit_find_nearest_line (each, addr,
3122*5796c8dcSSimon Schubert 						   filename_ptr,
3123*5796c8dcSSimon Schubert 						   functionname_ptr,
3124*5796c8dcSSimon Schubert 						   linenumber_ptr,
3125*5796c8dcSSimon Schubert 						   stash));
3126*5796c8dcSSimon Schubert 	  if (found)
3127*5796c8dcSSimon Schubert 	    goto done;
3128*5796c8dcSSimon Schubert 	}
3129*5796c8dcSSimon Schubert     }
3130*5796c8dcSSimon Schubert 
3131*5796c8dcSSimon Schubert   /* The DWARF2 spec says that the initial length field, and the
3132*5796c8dcSSimon Schubert      offset of the abbreviation table, should both be 4-byte values.
3133*5796c8dcSSimon Schubert      However, some compilers do things differently.  */
3134*5796c8dcSSimon Schubert   if (addr_size == 0)
3135*5796c8dcSSimon Schubert     addr_size = 4;
3136*5796c8dcSSimon Schubert   BFD_ASSERT (addr_size == 4 || addr_size == 8);
3137*5796c8dcSSimon Schubert 
3138*5796c8dcSSimon Schubert   /* Read each remaining comp. units checking each as they are read.  */
3139*5796c8dcSSimon Schubert   while (stash->info_ptr < stash->info_ptr_end)
3140*5796c8dcSSimon Schubert     {
3141*5796c8dcSSimon Schubert       bfd_vma length;
3142*5796c8dcSSimon Schubert       unsigned int offset_size = addr_size;
3143*5796c8dcSSimon Schubert       bfd_byte *info_ptr_unit = stash->info_ptr;
3144*5796c8dcSSimon Schubert 
3145*5796c8dcSSimon Schubert       length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
3146*5796c8dcSSimon Schubert       /* A 0xffffff length is the DWARF3 way of indicating
3147*5796c8dcSSimon Schubert 	 we use 64-bit offsets, instead of 32-bit offsets.  */
3148*5796c8dcSSimon Schubert       if (length == 0xffffffff)
3149*5796c8dcSSimon Schubert 	{
3150*5796c8dcSSimon Schubert 	  offset_size = 8;
3151*5796c8dcSSimon Schubert 	  length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3152*5796c8dcSSimon Schubert 	  stash->info_ptr += 12;
3153*5796c8dcSSimon Schubert 	}
3154*5796c8dcSSimon Schubert       /* A zero length is the IRIX way of indicating 64-bit offsets,
3155*5796c8dcSSimon Schubert 	 mostly because the 64-bit length will generally fit in 32
3156*5796c8dcSSimon Schubert 	 bits, and the endianness helps.  */
3157*5796c8dcSSimon Schubert       else if (length == 0)
3158*5796c8dcSSimon Schubert 	{
3159*5796c8dcSSimon Schubert 	  offset_size = 8;
3160*5796c8dcSSimon Schubert 	  length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3161*5796c8dcSSimon Schubert 	  stash->info_ptr += 8;
3162*5796c8dcSSimon Schubert 	}
3163*5796c8dcSSimon Schubert       /* In the absence of the hints above, we assume 32-bit DWARF2
3164*5796c8dcSSimon Schubert 	 offsets even for targets with 64-bit addresses, because:
3165*5796c8dcSSimon Schubert 	   a) most of the time these targets will not have generated
3166*5796c8dcSSimon Schubert 	      more than 2Gb of debug info and so will not need 64-bit
3167*5796c8dcSSimon Schubert 	      offsets,
3168*5796c8dcSSimon Schubert 	 and
3169*5796c8dcSSimon Schubert 	   b) if they do use 64-bit offsets but they are not using
3170*5796c8dcSSimon Schubert 	      the size hints that are tested for above then they are
3171*5796c8dcSSimon Schubert 	      not conforming to the DWARF3 standard anyway.  */
3172*5796c8dcSSimon Schubert       else if (addr_size == 8)
3173*5796c8dcSSimon Schubert 	{
3174*5796c8dcSSimon Schubert 	  offset_size = 4;
3175*5796c8dcSSimon Schubert 	  stash->info_ptr += 4;
3176*5796c8dcSSimon Schubert 	}
3177*5796c8dcSSimon Schubert       else
3178*5796c8dcSSimon Schubert 	stash->info_ptr += 4;
3179*5796c8dcSSimon Schubert 
3180*5796c8dcSSimon Schubert       if (length > 0)
3181*5796c8dcSSimon Schubert 	{
3182*5796c8dcSSimon Schubert 	  each = parse_comp_unit (stash, length, info_ptr_unit,
3183*5796c8dcSSimon Schubert 				  offset_size);
3184*5796c8dcSSimon Schubert 	  if (!each)
3185*5796c8dcSSimon Schubert 	    /* The dwarf information is damaged, don't trust it any
3186*5796c8dcSSimon Schubert 	       more.  */
3187*5796c8dcSSimon Schubert 	    break;
3188*5796c8dcSSimon Schubert 	  stash->info_ptr += length;
3189*5796c8dcSSimon Schubert 
3190*5796c8dcSSimon Schubert 	  if (stash->all_comp_units)
3191*5796c8dcSSimon Schubert 	    stash->all_comp_units->prev_unit = each;
3192*5796c8dcSSimon Schubert 	  else
3193*5796c8dcSSimon Schubert 	    stash->last_comp_unit = each;
3194*5796c8dcSSimon Schubert 
3195*5796c8dcSSimon Schubert 	  each->next_unit = stash->all_comp_units;
3196*5796c8dcSSimon Schubert 	  stash->all_comp_units = each;
3197*5796c8dcSSimon Schubert 
3198*5796c8dcSSimon Schubert 	  /* DW_AT_low_pc and DW_AT_high_pc are optional for
3199*5796c8dcSSimon Schubert 	     compilation units.  If we don't have them (i.e.,
3200*5796c8dcSSimon Schubert 	     unit->high == 0), we need to consult the line info table
3201*5796c8dcSSimon Schubert 	     to see if a compilation unit contains the given
3202*5796c8dcSSimon Schubert 	     address.  */
3203*5796c8dcSSimon Schubert 	  if (do_line)
3204*5796c8dcSSimon Schubert 	    found = (((symbol->flags & BSF_FUNCTION) == 0
3205*5796c8dcSSimon Schubert 		      || each->arange.high == 0
3206*5796c8dcSSimon Schubert 		      || comp_unit_contains_address (each, addr))
3207*5796c8dcSSimon Schubert 		     && comp_unit_find_line (each, symbol, addr,
3208*5796c8dcSSimon Schubert 					     filename_ptr,
3209*5796c8dcSSimon Schubert 					     linenumber_ptr,
3210*5796c8dcSSimon Schubert 					     stash));
3211*5796c8dcSSimon Schubert 	  else
3212*5796c8dcSSimon Schubert 	    found = ((each->arange.high == 0
3213*5796c8dcSSimon Schubert 		      || comp_unit_contains_address (each, addr))
3214*5796c8dcSSimon Schubert 		     && comp_unit_find_nearest_line (each, addr,
3215*5796c8dcSSimon Schubert 						     filename_ptr,
3216*5796c8dcSSimon Schubert 						     functionname_ptr,
3217*5796c8dcSSimon Schubert 						     linenumber_ptr,
3218*5796c8dcSSimon Schubert 						     stash));
3219*5796c8dcSSimon Schubert 
3220*5796c8dcSSimon Schubert 	  if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3221*5796c8dcSSimon Schubert 	      == stash->sec->size)
3222*5796c8dcSSimon Schubert 	    {
3223*5796c8dcSSimon Schubert 	      stash->sec = find_debug_info (stash->bfd_ptr, stash->sec);
3224*5796c8dcSSimon Schubert 	      stash->sec_info_ptr = stash->info_ptr;
3225*5796c8dcSSimon Schubert 	    }
3226*5796c8dcSSimon Schubert 
3227*5796c8dcSSimon Schubert 	  if (found)
3228*5796c8dcSSimon Schubert 	    goto done;
3229*5796c8dcSSimon Schubert 	}
3230*5796c8dcSSimon Schubert     }
3231*5796c8dcSSimon Schubert 
3232*5796c8dcSSimon Schubert done:
3233*5796c8dcSSimon Schubert   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3234*5796c8dcSSimon Schubert     unset_sections (stash);
3235*5796c8dcSSimon Schubert 
3236*5796c8dcSSimon Schubert   return found;
3237*5796c8dcSSimon Schubert }
3238*5796c8dcSSimon Schubert 
3239*5796c8dcSSimon Schubert /* The DWARF2 version of find_nearest_line.
3240*5796c8dcSSimon Schubert    Return TRUE if the line is found without error.  */
3241*5796c8dcSSimon Schubert 
3242*5796c8dcSSimon Schubert bfd_boolean
3243*5796c8dcSSimon Schubert _bfd_dwarf2_find_nearest_line (bfd *abfd,
3244*5796c8dcSSimon Schubert 			       asection *section,
3245*5796c8dcSSimon Schubert 			       asymbol **symbols,
3246*5796c8dcSSimon Schubert 			       bfd_vma offset,
3247*5796c8dcSSimon Schubert 			       const char **filename_ptr,
3248*5796c8dcSSimon Schubert 			       const char **functionname_ptr,
3249*5796c8dcSSimon Schubert 			       unsigned int *linenumber_ptr,
3250*5796c8dcSSimon Schubert 			       unsigned int addr_size,
3251*5796c8dcSSimon Schubert 			       void **pinfo)
3252*5796c8dcSSimon Schubert {
3253*5796c8dcSSimon Schubert   return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3254*5796c8dcSSimon Schubert 		    functionname_ptr, linenumber_ptr, addr_size,
3255*5796c8dcSSimon Schubert 		    pinfo);
3256*5796c8dcSSimon Schubert }
3257*5796c8dcSSimon Schubert 
3258*5796c8dcSSimon Schubert /* The DWARF2 version of find_line.
3259*5796c8dcSSimon Schubert    Return TRUE if the line is found without error.  */
3260*5796c8dcSSimon Schubert 
3261*5796c8dcSSimon Schubert bfd_boolean
3262*5796c8dcSSimon Schubert _bfd_dwarf2_find_line (bfd *abfd,
3263*5796c8dcSSimon Schubert 		       asymbol **symbols,
3264*5796c8dcSSimon Schubert 		       asymbol *symbol,
3265*5796c8dcSSimon Schubert 		       const char **filename_ptr,
3266*5796c8dcSSimon Schubert 		       unsigned int *linenumber_ptr,
3267*5796c8dcSSimon Schubert 		       unsigned int addr_size,
3268*5796c8dcSSimon Schubert 		       void **pinfo)
3269*5796c8dcSSimon Schubert {
3270*5796c8dcSSimon Schubert   return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3271*5796c8dcSSimon Schubert 		    NULL, linenumber_ptr, addr_size,
3272*5796c8dcSSimon Schubert 		    pinfo);
3273*5796c8dcSSimon Schubert }
3274*5796c8dcSSimon Schubert 
3275*5796c8dcSSimon Schubert bfd_boolean
3276*5796c8dcSSimon Schubert _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3277*5796c8dcSSimon Schubert 			       const char **filename_ptr,
3278*5796c8dcSSimon Schubert 			       const char **functionname_ptr,
3279*5796c8dcSSimon Schubert 			       unsigned int *linenumber_ptr,
3280*5796c8dcSSimon Schubert 			       void **pinfo)
3281*5796c8dcSSimon Schubert {
3282*5796c8dcSSimon Schubert   struct dwarf2_debug *stash;
3283*5796c8dcSSimon Schubert 
3284*5796c8dcSSimon Schubert   stash = (struct dwarf2_debug *) *pinfo;
3285*5796c8dcSSimon Schubert   if (stash)
3286*5796c8dcSSimon Schubert     {
3287*5796c8dcSSimon Schubert       struct funcinfo *func = stash->inliner_chain;
3288*5796c8dcSSimon Schubert 
3289*5796c8dcSSimon Schubert       if (func && func->caller_func)
3290*5796c8dcSSimon Schubert 	{
3291*5796c8dcSSimon Schubert 	  *filename_ptr = func->caller_file;
3292*5796c8dcSSimon Schubert 	  *functionname_ptr = func->caller_func->name;
3293*5796c8dcSSimon Schubert 	  *linenumber_ptr = func->caller_line;
3294*5796c8dcSSimon Schubert 	  stash->inliner_chain = func->caller_func;
3295*5796c8dcSSimon Schubert 	  return TRUE;
3296*5796c8dcSSimon Schubert 	}
3297*5796c8dcSSimon Schubert     }
3298*5796c8dcSSimon Schubert 
3299*5796c8dcSSimon Schubert   return FALSE;
3300*5796c8dcSSimon Schubert }
3301*5796c8dcSSimon Schubert 
3302*5796c8dcSSimon Schubert void
3303*5796c8dcSSimon Schubert _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3304*5796c8dcSSimon Schubert {
3305*5796c8dcSSimon Schubert   struct comp_unit *each;
3306*5796c8dcSSimon Schubert   struct dwarf2_debug *stash;
3307*5796c8dcSSimon Schubert 
3308*5796c8dcSSimon Schubert   if (abfd == NULL || elf_tdata (abfd) == NULL)
3309*5796c8dcSSimon Schubert     return;
3310*5796c8dcSSimon Schubert 
3311*5796c8dcSSimon Schubert   stash = (struct dwarf2_debug *) elf_tdata (abfd)->dwarf2_find_line_info;
3312*5796c8dcSSimon Schubert 
3313*5796c8dcSSimon Schubert   if (stash == NULL)
3314*5796c8dcSSimon Schubert     return;
3315*5796c8dcSSimon Schubert 
3316*5796c8dcSSimon Schubert   for (each = stash->all_comp_units; each; each = each->next_unit)
3317*5796c8dcSSimon Schubert     {
3318*5796c8dcSSimon Schubert       struct abbrev_info **abbrevs = each->abbrevs;
3319*5796c8dcSSimon Schubert       struct funcinfo *function_table = each->function_table;
3320*5796c8dcSSimon Schubert       struct varinfo *variable_table = each->variable_table;
3321*5796c8dcSSimon Schubert       size_t i;
3322*5796c8dcSSimon Schubert 
3323*5796c8dcSSimon Schubert       for (i = 0; i < ABBREV_HASH_SIZE; i++)
3324*5796c8dcSSimon Schubert 	{
3325*5796c8dcSSimon Schubert 	  struct abbrev_info *abbrev = abbrevs[i];
3326*5796c8dcSSimon Schubert 
3327*5796c8dcSSimon Schubert 	  while (abbrev)
3328*5796c8dcSSimon Schubert 	    {
3329*5796c8dcSSimon Schubert 	      free (abbrev->attrs);
3330*5796c8dcSSimon Schubert 	      abbrev = abbrev->next;
3331*5796c8dcSSimon Schubert 	    }
3332*5796c8dcSSimon Schubert 	}
3333*5796c8dcSSimon Schubert 
3334*5796c8dcSSimon Schubert       if (each->line_table)
3335*5796c8dcSSimon Schubert 	{
3336*5796c8dcSSimon Schubert 	  free (each->line_table->dirs);
3337*5796c8dcSSimon Schubert 	  free (each->line_table->files);
3338*5796c8dcSSimon Schubert 	}
3339*5796c8dcSSimon Schubert 
3340*5796c8dcSSimon Schubert       while (function_table)
3341*5796c8dcSSimon Schubert 	{
3342*5796c8dcSSimon Schubert 	  if (function_table->file)
3343*5796c8dcSSimon Schubert 	    {
3344*5796c8dcSSimon Schubert 	      free (function_table->file);
3345*5796c8dcSSimon Schubert 	      function_table->file = NULL;
3346*5796c8dcSSimon Schubert 	    }
3347*5796c8dcSSimon Schubert 
3348*5796c8dcSSimon Schubert 	  if (function_table->caller_file)
3349*5796c8dcSSimon Schubert 	    {
3350*5796c8dcSSimon Schubert 	      free (function_table->caller_file);
3351*5796c8dcSSimon Schubert 	      function_table->caller_file = NULL;
3352*5796c8dcSSimon Schubert 	    }
3353*5796c8dcSSimon Schubert 	  function_table = function_table->prev_func;
3354*5796c8dcSSimon Schubert 	}
3355*5796c8dcSSimon Schubert 
3356*5796c8dcSSimon Schubert       while (variable_table)
3357*5796c8dcSSimon Schubert 	{
3358*5796c8dcSSimon Schubert 	  if (variable_table->file)
3359*5796c8dcSSimon Schubert 	    {
3360*5796c8dcSSimon Schubert 	      free (variable_table->file);
3361*5796c8dcSSimon Schubert 	      variable_table->file = NULL;
3362*5796c8dcSSimon Schubert 	    }
3363*5796c8dcSSimon Schubert 
3364*5796c8dcSSimon Schubert 	  variable_table = variable_table->prev_var;
3365*5796c8dcSSimon Schubert 	}
3366*5796c8dcSSimon Schubert     }
3367*5796c8dcSSimon Schubert 
3368*5796c8dcSSimon Schubert   if (stash->dwarf_abbrev_buffer)
3369*5796c8dcSSimon Schubert     free (stash->dwarf_abbrev_buffer);
3370*5796c8dcSSimon Schubert   if (stash->dwarf_line_buffer)
3371*5796c8dcSSimon Schubert     free (stash->dwarf_line_buffer);
3372*5796c8dcSSimon Schubert   if (stash->dwarf_str_buffer)
3373*5796c8dcSSimon Schubert     free (stash->dwarf_str_buffer);
3374*5796c8dcSSimon Schubert   if (stash->dwarf_ranges_buffer)
3375*5796c8dcSSimon Schubert     free (stash->dwarf_ranges_buffer);
3376*5796c8dcSSimon Schubert   if (stash->info_ptr_memory)
3377*5796c8dcSSimon Schubert     free (stash->info_ptr_memory);
3378*5796c8dcSSimon Schubert }
3379