xref: /openbsd-src/gnu/usr.bin/binutils/bfd/dwarf2.c (revision cf2f2c5620d6d9a4fd01930983c4b9a1f76d7aa3)
1f7cc78ecSespie /* DWARF 2 support.
2*cf2f2c56Smiod    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3*cf2f2c56Smiod    2004 Free Software Foundation, Inc.
4f7cc78ecSespie 
5f7cc78ecSespie    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6f7cc78ecSespie    (gavin@cygnus.com).
7f7cc78ecSespie 
8f7cc78ecSespie    From the dwarf2read.c header:
9f7cc78ecSespie    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10f7cc78ecSespie    Inc.  with support from Florida State University (under contract
11f7cc78ecSespie    with the Ada Joint Program Office), and Silicon Graphics, Inc.
12f7cc78ecSespie    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13f7cc78ecSespie    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14f7cc78ecSespie    support in dwarfread.c
15f7cc78ecSespie 
16f7cc78ecSespie    This file is part of BFD.
17f7cc78ecSespie 
18f7cc78ecSespie    This program is free software; you can redistribute it and/or modify
19f7cc78ecSespie    it under the terms of the GNU General Public License as published by
20f7cc78ecSespie    the Free Software Foundation; either version 2 of the License, or (at
21f7cc78ecSespie    your option) any later version.
22f7cc78ecSespie 
23f7cc78ecSespie    This program is distributed in the hope that it will be useful, but
24f7cc78ecSespie    WITHOUT ANY WARRANTY; without even the implied warranty of
25f7cc78ecSespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26f7cc78ecSespie    General Public License for more details.
27f7cc78ecSespie 
28f7cc78ecSespie    You should have received a copy of the GNU General Public License
29f7cc78ecSespie    along with this program; if not, write to the Free Software
30f7cc78ecSespie    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
31f7cc78ecSespie 
32f7cc78ecSespie #include "bfd.h"
33f7cc78ecSespie #include "sysdep.h"
34f7cc78ecSespie #include "libiberty.h"
35f7cc78ecSespie #include "libbfd.h"
36f7cc78ecSespie #include "elf-bfd.h"
37f7cc78ecSespie #include "elf/dwarf2.h"
38f7cc78ecSespie 
39f7cc78ecSespie /* The data in the .debug_line statement prologue looks like this.  */
405f210c2aSfgsch 
41f7cc78ecSespie struct line_head
42f7cc78ecSespie {
43d2201f2fSdrahn   bfd_vma total_length;
44f7cc78ecSespie   unsigned short version;
45d2201f2fSdrahn   bfd_vma prologue_length;
46f7cc78ecSespie   unsigned char minimum_instruction_length;
47f7cc78ecSespie   unsigned char default_is_stmt;
48f7cc78ecSespie   int line_base;
49f7cc78ecSespie   unsigned char line_range;
50f7cc78ecSespie   unsigned char opcode_base;
51f7cc78ecSespie   unsigned char *standard_opcode_lengths;
52f7cc78ecSespie };
53f7cc78ecSespie 
545f210c2aSfgsch /* Attributes have a name and a value.  */
555f210c2aSfgsch 
56f7cc78ecSespie struct attribute
57f7cc78ecSespie {
58f7cc78ecSespie   enum dwarf_attribute name;
59f7cc78ecSespie   enum dwarf_form form;
60f7cc78ecSespie   union
61f7cc78ecSespie   {
62f7cc78ecSespie     char *str;
63f7cc78ecSespie     struct dwarf_block *blk;
64*cf2f2c56Smiod     bfd_uint64_t val;
65*cf2f2c56Smiod     bfd_int64_t sval;
66f7cc78ecSespie   }
67f7cc78ecSespie   u;
68f7cc78ecSespie };
69f7cc78ecSespie 
70f7cc78ecSespie /* Blocks are a bunch of untyped bytes.  */
71f7cc78ecSespie struct dwarf_block
72f7cc78ecSespie {
73f7cc78ecSespie   unsigned int size;
74f7cc78ecSespie   char *data;
75f7cc78ecSespie };
76f7cc78ecSespie 
775f210c2aSfgsch struct dwarf2_debug
785f210c2aSfgsch {
79f7cc78ecSespie   /* A list of all previously read comp_units.  */
80f7cc78ecSespie   struct comp_unit* all_comp_units;
81f7cc78ecSespie 
82f7cc78ecSespie   /* The next unread compilation unit within the .debug_info section.
83f7cc78ecSespie      Zero indicates that the .debug_info section has not been loaded
84f7cc78ecSespie      into a buffer yet.  */
85f7cc78ecSespie   char* info_ptr;
86f7cc78ecSespie 
87f7cc78ecSespie   /* Pointer to the end of the .debug_info section memory buffer.  */
88f7cc78ecSespie   char* info_ptr_end;
89f7cc78ecSespie 
90d2201f2fSdrahn   /* Pointer to the section and address of the beginning of the
91d2201f2fSdrahn      section.  */
92d2201f2fSdrahn   asection* sec;
93d2201f2fSdrahn   char* sec_info_ptr;
94d2201f2fSdrahn 
95d2201f2fSdrahn   /* Pointer to the symbol table.  */
96d2201f2fSdrahn   asymbol** syms;
97d2201f2fSdrahn 
98f7cc78ecSespie   /* Pointer to the .debug_abbrev section loaded into memory.  */
99f7cc78ecSespie   char* dwarf_abbrev_buffer;
100f7cc78ecSespie 
101f7cc78ecSespie   /* Length of the loaded .debug_abbrev section.  */
102f7cc78ecSespie   unsigned long dwarf_abbrev_size;
103f7cc78ecSespie 
104f7cc78ecSespie   /* Buffer for decode_line_info.  */
105f7cc78ecSespie   char *dwarf_line_buffer;
1065f210c2aSfgsch 
1075f210c2aSfgsch   /* Length of the loaded .debug_line section.  */
1085f210c2aSfgsch   unsigned long dwarf_line_size;
109d2201f2fSdrahn 
110d2201f2fSdrahn   /* Pointer to the .debug_str section loaded into memory.  */
111d2201f2fSdrahn   char* dwarf_str_buffer;
112d2201f2fSdrahn 
113d2201f2fSdrahn   /* Length of the loaded .debug_str section.  */
114d2201f2fSdrahn   unsigned long dwarf_str_size;
115f7cc78ecSespie };
116f7cc78ecSespie 
1175f210c2aSfgsch struct arange
1185f210c2aSfgsch {
119f7cc78ecSespie   struct arange *next;
120f7cc78ecSespie   bfd_vma low;
121f7cc78ecSespie   bfd_vma high;
122f7cc78ecSespie };
123f7cc78ecSespie 
124f7cc78ecSespie /* A minimal decoding of DWARF2 compilation units.  We only decode
125f7cc78ecSespie    what's needed to get to the line number information.  */
126f7cc78ecSespie 
1275f210c2aSfgsch struct comp_unit
1285f210c2aSfgsch {
129f7cc78ecSespie   /* Chain the previously read compilation units.  */
130f7cc78ecSespie   struct comp_unit* next_unit;
131f7cc78ecSespie 
132f7cc78ecSespie   /* Keep the bdf convenient (for memory allocation).  */
133f7cc78ecSespie   bfd* abfd;
134f7cc78ecSespie 
135f7cc78ecSespie   /* The lowest and higest addresses contained in this compilation
136f7cc78ecSespie      unit as specified in the compilation unit header.  */
137f7cc78ecSespie   struct arange arange;
138f7cc78ecSespie 
139f7cc78ecSespie   /* The DW_AT_name attribute (for error messages).  */
140f7cc78ecSespie   char* name;
141f7cc78ecSespie 
142f7cc78ecSespie   /* The abbrev hash table.  */
143f7cc78ecSespie   struct abbrev_info** abbrevs;
144f7cc78ecSespie 
145f7cc78ecSespie   /* Note that an error was found by comp_unit_find_nearest_line.  */
146f7cc78ecSespie   int error;
147f7cc78ecSespie 
1485f210c2aSfgsch   /* The DW_AT_comp_dir attribute.  */
149f7cc78ecSespie   char* comp_dir;
150f7cc78ecSespie 
151d2201f2fSdrahn   /* TRUE if there is a line number table associated with this comp. unit.  */
152f7cc78ecSespie   int stmtlist;
153f7cc78ecSespie 
154f7cc78ecSespie   /* The offset into .debug_line of the line number table.  */
155f7cc78ecSespie   unsigned long line_offset;
156f7cc78ecSespie 
157f7cc78ecSespie   /* Pointer to the first child die for the comp unit.  */
158f7cc78ecSespie   char *first_child_die_ptr;
159f7cc78ecSespie 
160f7cc78ecSespie   /* The end of the comp unit.  */
161f7cc78ecSespie   char *end_ptr;
162f7cc78ecSespie 
163f7cc78ecSespie   /* The decoded line number, NULL if not yet decoded.  */
164f7cc78ecSespie   struct line_info_table* line_table;
165f7cc78ecSespie 
166f7cc78ecSespie   /* A list of the functions found in this comp. unit.  */
167f7cc78ecSespie   struct funcinfo* function_table;
168f7cc78ecSespie 
169d2201f2fSdrahn   /* Pointer to dwarf2_debug structure.  */
170d2201f2fSdrahn   struct dwarf2_debug *stash;
171d2201f2fSdrahn 
1725f210c2aSfgsch   /* Address size for this unit - from unit header.  */
173f7cc78ecSespie   unsigned char addr_size;
174d2201f2fSdrahn 
175d2201f2fSdrahn   /* Offset size for this unit - from unit header.  */
176d2201f2fSdrahn   unsigned char offset_size;
177f7cc78ecSespie };
178f7cc78ecSespie 
179d2201f2fSdrahn /* This data structure holds the information of an abbrev.  */
180d2201f2fSdrahn struct abbrev_info
181d2201f2fSdrahn {
182d2201f2fSdrahn   unsigned int number;		/* Number identifying abbrev.  */
183d2201f2fSdrahn   enum dwarf_tag tag;		/* DWARF tag.  */
184d2201f2fSdrahn   int has_children;		/* Boolean.  */
185d2201f2fSdrahn   unsigned int num_attrs;	/* Number of attributes.  */
186d2201f2fSdrahn   struct attr_abbrev *attrs;	/* An array of attribute descriptions.  */
187d2201f2fSdrahn   struct abbrev_info *next;	/* Next in chain.  */
188d2201f2fSdrahn };
189d2201f2fSdrahn 
190d2201f2fSdrahn struct attr_abbrev
191d2201f2fSdrahn {
192d2201f2fSdrahn   enum dwarf_attribute name;
193d2201f2fSdrahn   enum dwarf_form form;
194d2201f2fSdrahn };
195d2201f2fSdrahn 
196d2201f2fSdrahn #ifndef ABBREV_HASH_SIZE
197d2201f2fSdrahn #define ABBREV_HASH_SIZE 121
198d2201f2fSdrahn #endif
199d2201f2fSdrahn #ifndef ATTR_ALLOC_CHUNK
200d2201f2fSdrahn #define ATTR_ALLOC_CHUNK 4
201d2201f2fSdrahn #endif
202d2201f2fSdrahn 
203f7cc78ecSespie /* VERBATIM
204f7cc78ecSespie    The following function up to the END VERBATIM mark are
205f7cc78ecSespie    copied directly from dwarf2read.c.  */
206f7cc78ecSespie 
2075f210c2aSfgsch /* Read dwarf information from a buffer.  */
208f7cc78ecSespie 
209f7cc78ecSespie static unsigned int
read_1_byte(bfd * abfd ATTRIBUTE_UNUSED,char * buf)210*cf2f2c56Smiod read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
211f7cc78ecSespie {
212*cf2f2c56Smiod   return bfd_get_8 (abfd, buf);
213f7cc78ecSespie }
214f7cc78ecSespie 
215f7cc78ecSespie static int
read_1_signed_byte(bfd * abfd ATTRIBUTE_UNUSED,char * buf)216*cf2f2c56Smiod read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
217f7cc78ecSespie {
218*cf2f2c56Smiod   return bfd_get_signed_8 (abfd, buf);
219f7cc78ecSespie }
220f7cc78ecSespie 
221f7cc78ecSespie static unsigned int
read_2_bytes(bfd * abfd,char * buf)222*cf2f2c56Smiod read_2_bytes (bfd *abfd, char *buf)
223f7cc78ecSespie {
224*cf2f2c56Smiod   return bfd_get_16 (abfd, buf);
225f7cc78ecSespie }
226f7cc78ecSespie 
227f7cc78ecSespie static unsigned int
read_4_bytes(bfd * abfd,char * buf)228*cf2f2c56Smiod read_4_bytes (bfd *abfd, char *buf)
229f7cc78ecSespie {
230*cf2f2c56Smiod   return bfd_get_32 (abfd, buf);
231f7cc78ecSespie }
232f7cc78ecSespie 
233*cf2f2c56Smiod static bfd_uint64_t
read_8_bytes(bfd * abfd,char * buf)234*cf2f2c56Smiod read_8_bytes (bfd *abfd, char *buf)
235f7cc78ecSespie {
236*cf2f2c56Smiod   return bfd_get_64 (abfd, buf);
237f7cc78ecSespie }
238f7cc78ecSespie 
239f7cc78ecSespie static char *
read_n_bytes(bfd * abfd ATTRIBUTE_UNUSED,char * buf,unsigned int size ATTRIBUTE_UNUSED)240*cf2f2c56Smiod read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
241*cf2f2c56Smiod 	      char *buf,
242*cf2f2c56Smiod 	      unsigned int size ATTRIBUTE_UNUSED)
243f7cc78ecSespie {
244f7cc78ecSespie   /* If the size of a host char is 8 bits, we can return a pointer
245f7cc78ecSespie      to the buffer, otherwise we have to copy the data to a buffer
246f7cc78ecSespie      allocated on the temporary obstack.  */
247f7cc78ecSespie   return buf;
248f7cc78ecSespie }
249f7cc78ecSespie 
250f7cc78ecSespie static char *
read_string(bfd * abfd ATTRIBUTE_UNUSED,char * buf,unsigned int * bytes_read_ptr)251*cf2f2c56Smiod read_string (bfd *abfd ATTRIBUTE_UNUSED,
252*cf2f2c56Smiod 	     char *buf,
253*cf2f2c56Smiod 	     unsigned int *bytes_read_ptr)
254f7cc78ecSespie {
255d2201f2fSdrahn   /* Return a pointer to the embedded string.  */
256f7cc78ecSespie   if (*buf == '\0')
257f7cc78ecSespie     {
258f7cc78ecSespie       *bytes_read_ptr = 1;
259f7cc78ecSespie       return NULL;
260f7cc78ecSespie     }
2615f210c2aSfgsch 
262f7cc78ecSespie   *bytes_read_ptr = strlen (buf) + 1;
263f7cc78ecSespie   return buf;
264f7cc78ecSespie }
265f7cc78ecSespie 
266d2201f2fSdrahn static char *
read_indirect_string(struct comp_unit * unit,char * buf,unsigned int * bytes_read_ptr)267*cf2f2c56Smiod read_indirect_string (struct comp_unit* unit,
268*cf2f2c56Smiod 		      char *buf,
269*cf2f2c56Smiod 		      unsigned int *bytes_read_ptr)
270d2201f2fSdrahn {
271*cf2f2c56Smiod   bfd_uint64_t offset;
272d2201f2fSdrahn   struct dwarf2_debug *stash = unit->stash;
273d2201f2fSdrahn 
274d2201f2fSdrahn   if (unit->offset_size == 4)
275d2201f2fSdrahn     offset = read_4_bytes (unit->abfd, buf);
276d2201f2fSdrahn   else
277d2201f2fSdrahn     offset = read_8_bytes (unit->abfd, buf);
278d2201f2fSdrahn   *bytes_read_ptr = unit->offset_size;
279d2201f2fSdrahn 
280d2201f2fSdrahn   if (! stash->dwarf_str_buffer)
281d2201f2fSdrahn     {
282d2201f2fSdrahn       asection *msec;
283d2201f2fSdrahn       bfd *abfd = unit->abfd;
284d2201f2fSdrahn 
285d2201f2fSdrahn       msec = bfd_get_section_by_name (abfd, ".debug_str");
286d2201f2fSdrahn       if (! msec)
287d2201f2fSdrahn 	{
288d2201f2fSdrahn 	  (*_bfd_error_handler)
289d2201f2fSdrahn 	    (_("Dwarf Error: Can't find .debug_str section."));
290d2201f2fSdrahn 	  bfd_set_error (bfd_error_bad_value);
291d2201f2fSdrahn 	  return NULL;
292d2201f2fSdrahn 	}
293d2201f2fSdrahn 
294d2201f2fSdrahn       stash->dwarf_str_size = msec->_raw_size;
295*cf2f2c56Smiod       stash->dwarf_str_buffer = bfd_alloc (abfd, msec->_raw_size);
296d2201f2fSdrahn       if (! stash->dwarf_abbrev_buffer)
297d2201f2fSdrahn 	return NULL;
298d2201f2fSdrahn 
299d2201f2fSdrahn       if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
300*cf2f2c56Smiod 				      0, msec->_raw_size))
301d2201f2fSdrahn 	return NULL;
302d2201f2fSdrahn     }
303d2201f2fSdrahn 
304d2201f2fSdrahn   if (offset >= stash->dwarf_str_size)
305d2201f2fSdrahn     {
306d2201f2fSdrahn       (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
307d2201f2fSdrahn 			     (unsigned long) offset, stash->dwarf_str_size);
308d2201f2fSdrahn       bfd_set_error (bfd_error_bad_value);
309d2201f2fSdrahn       return NULL;
310d2201f2fSdrahn     }
311d2201f2fSdrahn 
312d2201f2fSdrahn   buf = stash->dwarf_str_buffer + offset;
313d2201f2fSdrahn   if (*buf == '\0')
314d2201f2fSdrahn     return NULL;
315d2201f2fSdrahn   return buf;
316d2201f2fSdrahn }
317d2201f2fSdrahn 
318f7cc78ecSespie static unsigned int
read_unsigned_leb128(bfd * abfd ATTRIBUTE_UNUSED,char * buf,unsigned int * bytes_read_ptr)319*cf2f2c56Smiod read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
320*cf2f2c56Smiod 		      char *buf,
321*cf2f2c56Smiod 		      unsigned int *bytes_read_ptr)
322f7cc78ecSespie {
323f7cc78ecSespie   unsigned int  result;
324f7cc78ecSespie   unsigned int  num_read;
325f7cc78ecSespie   int           shift;
326f7cc78ecSespie   unsigned char byte;
327f7cc78ecSespie 
328f7cc78ecSespie   result   = 0;
329f7cc78ecSespie   shift    = 0;
330f7cc78ecSespie   num_read = 0;
331f7cc78ecSespie 
332f7cc78ecSespie   do
333f7cc78ecSespie     {
334*cf2f2c56Smiod       byte = bfd_get_8 (abfd, buf);
335f7cc78ecSespie       buf ++;
336f7cc78ecSespie       num_read ++;
337f7cc78ecSespie       result |= ((byte & 0x7f) << shift);
338f7cc78ecSespie       shift += 7;
339f7cc78ecSespie     }
340f7cc78ecSespie   while (byte & 0x80);
341f7cc78ecSespie 
342f7cc78ecSespie   * bytes_read_ptr = num_read;
343f7cc78ecSespie 
344f7cc78ecSespie   return result;
345f7cc78ecSespie }
346f7cc78ecSespie 
347f7cc78ecSespie static int
read_signed_leb128(bfd * abfd ATTRIBUTE_UNUSED,char * buf,unsigned int * bytes_read_ptr)348*cf2f2c56Smiod read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
349*cf2f2c56Smiod 		    char *buf,
350*cf2f2c56Smiod 		    unsigned int * bytes_read_ptr)
351f7cc78ecSespie {
352f7cc78ecSespie   int           result;
353f7cc78ecSespie   int           shift;
354f7cc78ecSespie   int           num_read;
355f7cc78ecSespie   unsigned char byte;
356f7cc78ecSespie 
357f7cc78ecSespie   result = 0;
358f7cc78ecSespie   shift = 0;
359f7cc78ecSespie   num_read = 0;
360f7cc78ecSespie 
361f7cc78ecSespie   do
362f7cc78ecSespie     {
363*cf2f2c56Smiod       byte = bfd_get_8 (abfd, buf);
364f7cc78ecSespie       buf ++;
365f7cc78ecSespie       num_read ++;
366f7cc78ecSespie       result |= ((byte & 0x7f) << shift);
367f7cc78ecSespie       shift += 7;
368f7cc78ecSespie     }
369f7cc78ecSespie   while (byte & 0x80);
370f7cc78ecSespie 
371f7cc78ecSespie   if ((shift < 32) && (byte & 0x40))
372f7cc78ecSespie     result |= -(1 << shift);
373f7cc78ecSespie 
374f7cc78ecSespie   * bytes_read_ptr = num_read;
375f7cc78ecSespie 
376f7cc78ecSespie   return result;
377f7cc78ecSespie }
378f7cc78ecSespie 
379f7cc78ecSespie /* END VERBATIM */
380f7cc78ecSespie 
381*cf2f2c56Smiod static bfd_uint64_t
read_address(struct comp_unit * unit,char * buf)382*cf2f2c56Smiod read_address (struct comp_unit *unit, char *buf)
383f7cc78ecSespie {
384f7cc78ecSespie   switch (unit->addr_size)
385f7cc78ecSespie     {
386f7cc78ecSespie     case 8:
387*cf2f2c56Smiod       return bfd_get_64 (unit->abfd, buf);
388f7cc78ecSespie     case 4:
389*cf2f2c56Smiod       return bfd_get_32 (unit->abfd, buf);
390f7cc78ecSespie     case 2:
391*cf2f2c56Smiod       return bfd_get_16 (unit->abfd, buf);
392f7cc78ecSespie     default:
393f7cc78ecSespie       abort ();
394f7cc78ecSespie     }
395f7cc78ecSespie }
396f7cc78ecSespie 
397f7cc78ecSespie /* Lookup an abbrev_info structure in the abbrev hash table.  */
398f7cc78ecSespie 
399f7cc78ecSespie static struct abbrev_info *
lookup_abbrev(unsigned int number,struct abbrev_info ** abbrevs)400*cf2f2c56Smiod lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
401f7cc78ecSespie {
402f7cc78ecSespie   unsigned int hash_number;
403f7cc78ecSespie   struct abbrev_info *abbrev;
404f7cc78ecSespie 
405f7cc78ecSespie   hash_number = number % ABBREV_HASH_SIZE;
406f7cc78ecSespie   abbrev = abbrevs[hash_number];
407f7cc78ecSespie 
408f7cc78ecSespie   while (abbrev)
409f7cc78ecSespie     {
410f7cc78ecSespie       if (abbrev->number == number)
411f7cc78ecSespie 	return abbrev;
412f7cc78ecSespie       else
413f7cc78ecSespie 	abbrev = abbrev->next;
414f7cc78ecSespie     }
4155f210c2aSfgsch 
416f7cc78ecSespie   return NULL;
417f7cc78ecSespie }
418f7cc78ecSespie 
419f7cc78ecSespie /* In DWARF version 2, the description of the debugging information is
420f7cc78ecSespie    stored in a separate .debug_abbrev section.  Before we read any
421f7cc78ecSespie    dies from a section we read in all abbreviations and install them
422f7cc78ecSespie    in a hash table.  */
423f7cc78ecSespie 
424f7cc78ecSespie static struct abbrev_info**
read_abbrevs(bfd * abfd,bfd_uint64_t offset,struct dwarf2_debug * stash)425*cf2f2c56Smiod read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
426f7cc78ecSespie {
427f7cc78ecSespie   struct abbrev_info **abbrevs;
428f7cc78ecSespie   char *abbrev_ptr;
429f7cc78ecSespie   struct abbrev_info *cur_abbrev;
430f7cc78ecSespie   unsigned int abbrev_number, bytes_read, abbrev_name;
431f7cc78ecSespie   unsigned int abbrev_form, hash_number;
432d2201f2fSdrahn   bfd_size_type amt;
433f7cc78ecSespie 
434f7cc78ecSespie   if (! stash->dwarf_abbrev_buffer)
435f7cc78ecSespie     {
436f7cc78ecSespie       asection *msec;
437f7cc78ecSespie 
438f7cc78ecSespie       msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
439f7cc78ecSespie       if (! msec)
440f7cc78ecSespie 	{
441f7cc78ecSespie 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
442f7cc78ecSespie 	  bfd_set_error (bfd_error_bad_value);
443f7cc78ecSespie 	  return 0;
444f7cc78ecSespie 	}
445f7cc78ecSespie 
446f7cc78ecSespie       stash->dwarf_abbrev_size = msec->_raw_size;
447d2201f2fSdrahn       stash->dwarf_abbrev_buffer
448d2201f2fSdrahn 	= bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
449d2201f2fSdrahn 						     stash->syms);
450f7cc78ecSespie       if (! stash->dwarf_abbrev_buffer)
451f7cc78ecSespie 	  return 0;
452f7cc78ecSespie     }
453f7cc78ecSespie 
4545f210c2aSfgsch   if (offset >= stash->dwarf_abbrev_size)
455f7cc78ecSespie     {
456d2201f2fSdrahn       (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
457d2201f2fSdrahn 			     (unsigned long) offset, stash->dwarf_abbrev_size);
458f7cc78ecSespie       bfd_set_error (bfd_error_bad_value);
459f7cc78ecSespie       return 0;
460f7cc78ecSespie     }
461f7cc78ecSespie 
462d2201f2fSdrahn   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
463*cf2f2c56Smiod   abbrevs = bfd_zalloc (abfd, amt);
464f7cc78ecSespie 
465f7cc78ecSespie   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
466f7cc78ecSespie   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
467f7cc78ecSespie   abbrev_ptr += bytes_read;
468f7cc78ecSespie 
4695f210c2aSfgsch   /* Loop until we reach an abbrev number of 0.  */
470f7cc78ecSespie   while (abbrev_number)
471f7cc78ecSespie     {
472d2201f2fSdrahn       amt = sizeof (struct abbrev_info);
473*cf2f2c56Smiod       cur_abbrev = bfd_zalloc (abfd, amt);
474f7cc78ecSespie 
4755f210c2aSfgsch       /* Read in abbrev header.  */
476f7cc78ecSespie       cur_abbrev->number = abbrev_number;
477d2201f2fSdrahn       cur_abbrev->tag = (enum dwarf_tag)
478d2201f2fSdrahn 	read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
479f7cc78ecSespie       abbrev_ptr += bytes_read;
480f7cc78ecSespie       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
481f7cc78ecSespie       abbrev_ptr += 1;
482f7cc78ecSespie 
4835f210c2aSfgsch       /* Now read in declarations.  */
484f7cc78ecSespie       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
485f7cc78ecSespie       abbrev_ptr += bytes_read;
486f7cc78ecSespie       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
487f7cc78ecSespie       abbrev_ptr += bytes_read;
4885f210c2aSfgsch 
489f7cc78ecSespie       while (abbrev_name)
490f7cc78ecSespie 	{
491f7cc78ecSespie 	  if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
492f7cc78ecSespie 	    {
493d2201f2fSdrahn 	      amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
494d2201f2fSdrahn 	      amt *= sizeof (struct attr_abbrev);
495*cf2f2c56Smiod 	      cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
496f7cc78ecSespie 	      if (! cur_abbrev->attrs)
497f7cc78ecSespie 		return 0;
498f7cc78ecSespie 	    }
4995f210c2aSfgsch 
500d2201f2fSdrahn 	  cur_abbrev->attrs[cur_abbrev->num_attrs].name
501d2201f2fSdrahn 	    = (enum dwarf_attribute) abbrev_name;
502d2201f2fSdrahn 	  cur_abbrev->attrs[cur_abbrev->num_attrs++].form
503d2201f2fSdrahn 	    = (enum dwarf_form) abbrev_form;
504f7cc78ecSespie 	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
505f7cc78ecSespie 	  abbrev_ptr += bytes_read;
506f7cc78ecSespie 	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
507f7cc78ecSespie 	  abbrev_ptr += bytes_read;
508f7cc78ecSespie 	}
509f7cc78ecSespie 
510f7cc78ecSespie       hash_number = abbrev_number % ABBREV_HASH_SIZE;
511f7cc78ecSespie       cur_abbrev->next = abbrevs[hash_number];
512f7cc78ecSespie       abbrevs[hash_number] = cur_abbrev;
513f7cc78ecSespie 
514f7cc78ecSespie       /* Get next abbreviation.
515f7cc78ecSespie 	 Under Irix6 the abbreviations for a compilation unit are not
516f7cc78ecSespie 	 always properly terminated with an abbrev number of 0.
517f7cc78ecSespie 	 Exit loop if we encounter an abbreviation which we have
518f7cc78ecSespie 	 already read (which means we are about to read the abbreviations
519f7cc78ecSespie 	 for the next compile unit) or if the end of the abbreviation
520f7cc78ecSespie 	 table is reached.  */
521f7cc78ecSespie       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
522f7cc78ecSespie 	    >= stash->dwarf_abbrev_size)
523f7cc78ecSespie 	break;
524f7cc78ecSespie       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
525f7cc78ecSespie       abbrev_ptr += bytes_read;
526f7cc78ecSespie       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
527f7cc78ecSespie 	break;
528f7cc78ecSespie     }
529f7cc78ecSespie 
530f7cc78ecSespie   return abbrevs;
531f7cc78ecSespie }
532f7cc78ecSespie 
533d2201f2fSdrahn /* Read an attribute value described by an attribute form.  */
534f7cc78ecSespie 
535f7cc78ecSespie static char *
read_attribute_value(struct attribute * attr,unsigned form,struct comp_unit * unit,char * info_ptr)536*cf2f2c56Smiod read_attribute_value (struct attribute *attr,
537*cf2f2c56Smiod 		      unsigned form,
538*cf2f2c56Smiod 		      struct comp_unit *unit,
539*cf2f2c56Smiod 		      char *info_ptr)
540f7cc78ecSespie {
541f7cc78ecSespie   bfd *abfd = unit->abfd;
542f7cc78ecSespie   unsigned int bytes_read;
543f7cc78ecSespie   struct dwarf_block *blk;
544d2201f2fSdrahn   bfd_size_type amt;
545f7cc78ecSespie 
546d2201f2fSdrahn   attr->form = (enum dwarf_form) form;
5475f210c2aSfgsch 
548d2201f2fSdrahn   switch (form)
549f7cc78ecSespie     {
550f7cc78ecSespie     case DW_FORM_addr:
551d2201f2fSdrahn       /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size.  */
552f7cc78ecSespie     case DW_FORM_ref_addr:
553*cf2f2c56Smiod       attr->u.val = read_address (unit, info_ptr);
554f7cc78ecSespie       info_ptr += unit->addr_size;
555f7cc78ecSespie       break;
556f7cc78ecSespie     case DW_FORM_block2:
557d2201f2fSdrahn       amt = sizeof (struct dwarf_block);
558*cf2f2c56Smiod       blk = bfd_alloc (abfd, amt);
559f7cc78ecSespie       blk->size = read_2_bytes (abfd, info_ptr);
560f7cc78ecSespie       info_ptr += 2;
561f7cc78ecSespie       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
562f7cc78ecSespie       info_ptr += blk->size;
563*cf2f2c56Smiod       attr->u.blk = blk;
564f7cc78ecSespie       break;
565f7cc78ecSespie     case DW_FORM_block4:
566d2201f2fSdrahn       amt = sizeof (struct dwarf_block);
567*cf2f2c56Smiod       blk = bfd_alloc (abfd, amt);
568f7cc78ecSespie       blk->size = read_4_bytes (abfd, info_ptr);
569f7cc78ecSespie       info_ptr += 4;
570f7cc78ecSespie       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
571f7cc78ecSespie       info_ptr += blk->size;
572*cf2f2c56Smiod       attr->u.blk = blk;
573f7cc78ecSespie       break;
574f7cc78ecSespie     case DW_FORM_data2:
575*cf2f2c56Smiod       attr->u.val = read_2_bytes (abfd, info_ptr);
576f7cc78ecSespie       info_ptr += 2;
577f7cc78ecSespie       break;
578f7cc78ecSespie     case DW_FORM_data4:
579*cf2f2c56Smiod       attr->u.val = read_4_bytes (abfd, info_ptr);
580f7cc78ecSespie       info_ptr += 4;
581f7cc78ecSespie       break;
582f7cc78ecSespie     case DW_FORM_data8:
583*cf2f2c56Smiod       attr->u.val = read_8_bytes (abfd, info_ptr);
584f7cc78ecSespie       info_ptr += 8;
585f7cc78ecSespie       break;
586f7cc78ecSespie     case DW_FORM_string:
587*cf2f2c56Smiod       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
588f7cc78ecSespie       info_ptr += bytes_read;
589f7cc78ecSespie       break;
590d2201f2fSdrahn     case DW_FORM_strp:
591*cf2f2c56Smiod       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
592d2201f2fSdrahn       info_ptr += bytes_read;
593d2201f2fSdrahn       break;
594f7cc78ecSespie     case DW_FORM_block:
595d2201f2fSdrahn       amt = sizeof (struct dwarf_block);
596*cf2f2c56Smiod       blk = bfd_alloc (abfd, amt);
597f7cc78ecSespie       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
598f7cc78ecSespie       info_ptr += bytes_read;
599f7cc78ecSespie       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
600f7cc78ecSespie       info_ptr += blk->size;
601*cf2f2c56Smiod       attr->u.blk = blk;
602f7cc78ecSespie       break;
603f7cc78ecSespie     case DW_FORM_block1:
604d2201f2fSdrahn       amt = sizeof (struct dwarf_block);
605*cf2f2c56Smiod       blk = bfd_alloc (abfd, amt);
606f7cc78ecSespie       blk->size = read_1_byte (abfd, info_ptr);
607f7cc78ecSespie       info_ptr += 1;
608f7cc78ecSespie       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
609f7cc78ecSespie       info_ptr += blk->size;
610*cf2f2c56Smiod       attr->u.blk = blk;
611f7cc78ecSespie       break;
612f7cc78ecSespie     case DW_FORM_data1:
613*cf2f2c56Smiod       attr->u.val = read_1_byte (abfd, info_ptr);
614f7cc78ecSespie       info_ptr += 1;
615f7cc78ecSespie       break;
616f7cc78ecSespie     case DW_FORM_flag:
617*cf2f2c56Smiod       attr->u.val = read_1_byte (abfd, info_ptr);
618f7cc78ecSespie       info_ptr += 1;
619f7cc78ecSespie       break;
620f7cc78ecSespie     case DW_FORM_sdata:
621*cf2f2c56Smiod       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
622f7cc78ecSespie       info_ptr += bytes_read;
623f7cc78ecSespie       break;
624f7cc78ecSespie     case DW_FORM_udata:
625*cf2f2c56Smiod       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
626f7cc78ecSespie       info_ptr += bytes_read;
627f7cc78ecSespie       break;
628f7cc78ecSespie     case DW_FORM_ref1:
629*cf2f2c56Smiod       attr->u.val = read_1_byte (abfd, info_ptr);
630f7cc78ecSespie       info_ptr += 1;
631f7cc78ecSespie       break;
632f7cc78ecSespie     case DW_FORM_ref2:
633*cf2f2c56Smiod       attr->u.val = read_2_bytes (abfd, info_ptr);
634f7cc78ecSespie       info_ptr += 2;
635f7cc78ecSespie       break;
636f7cc78ecSespie     case DW_FORM_ref4:
637*cf2f2c56Smiod       attr->u.val = read_4_bytes (abfd, info_ptr);
638f7cc78ecSespie       info_ptr += 4;
639f7cc78ecSespie       break;
640f7cc78ecSespie     case DW_FORM_ref8:
641*cf2f2c56Smiod       attr->u.val = read_8_bytes (abfd, info_ptr);
642f7cc78ecSespie       info_ptr += 8;
643f7cc78ecSespie       break;
644f7cc78ecSespie     case DW_FORM_ref_udata:
645*cf2f2c56Smiod       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
646f7cc78ecSespie       info_ptr += bytes_read;
647f7cc78ecSespie       break;
648f7cc78ecSespie     case DW_FORM_indirect:
649d2201f2fSdrahn       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
650d2201f2fSdrahn       info_ptr += bytes_read;
651d2201f2fSdrahn       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
652d2201f2fSdrahn       break;
653f7cc78ecSespie     default:
654d2201f2fSdrahn       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
655d2201f2fSdrahn 			     form);
656f7cc78ecSespie       bfd_set_error (bfd_error_bad_value);
657f7cc78ecSespie     }
658f7cc78ecSespie   return info_ptr;
659f7cc78ecSespie }
660f7cc78ecSespie 
661d2201f2fSdrahn /* Read an attribute described by an abbreviated attribute.  */
662d2201f2fSdrahn 
663d2201f2fSdrahn static char *
read_attribute(struct attribute * attr,struct attr_abbrev * abbrev,struct comp_unit * unit,char * info_ptr)664*cf2f2c56Smiod read_attribute (struct attribute *attr,
665*cf2f2c56Smiod 		struct attr_abbrev *abbrev,
666*cf2f2c56Smiod 		struct comp_unit *unit,
667*cf2f2c56Smiod 		char *info_ptr)
668d2201f2fSdrahn {
669d2201f2fSdrahn   attr->name = abbrev->name;
670d2201f2fSdrahn   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
671d2201f2fSdrahn   return info_ptr;
672d2201f2fSdrahn }
673d2201f2fSdrahn 
674f7cc78ecSespie /* Source line information table routines.  */
675f7cc78ecSespie 
676f7cc78ecSespie #define FILE_ALLOC_CHUNK 5
677f7cc78ecSespie #define DIR_ALLOC_CHUNK 5
678f7cc78ecSespie 
6795f210c2aSfgsch struct line_info
6805f210c2aSfgsch {
681f7cc78ecSespie   struct line_info* prev_line;
682f7cc78ecSespie   bfd_vma address;
683f7cc78ecSespie   char* filename;
684f7cc78ecSespie   unsigned int line;
685f7cc78ecSespie   unsigned int column;
6865f210c2aSfgsch   int end_sequence;		/* End of (sequential) code sequence.  */
687f7cc78ecSespie };
688f7cc78ecSespie 
6895f210c2aSfgsch struct fileinfo
6905f210c2aSfgsch {
691f7cc78ecSespie   char *name;
692f7cc78ecSespie   unsigned int dir;
693f7cc78ecSespie   unsigned int time;
694f7cc78ecSespie   unsigned int size;
695f7cc78ecSespie };
696f7cc78ecSespie 
6975f210c2aSfgsch struct line_info_table
6985f210c2aSfgsch {
699f7cc78ecSespie   bfd* abfd;
700f7cc78ecSespie   unsigned int num_files;
701f7cc78ecSespie   unsigned int num_dirs;
702f7cc78ecSespie   char* comp_dir;
703f7cc78ecSespie   char** dirs;
704f7cc78ecSespie   struct fileinfo* files;
705d2201f2fSdrahn   struct line_info* last_line;  /* largest VMA */
706d2201f2fSdrahn   struct line_info* lcl_head;   /* local head; used in 'add_line_info' */
707f7cc78ecSespie };
708f7cc78ecSespie 
709d2201f2fSdrahn struct funcinfo
710d2201f2fSdrahn {
711d2201f2fSdrahn   struct funcinfo *prev_func;
712d2201f2fSdrahn   char* name;
713d2201f2fSdrahn   bfd_vma low;
714d2201f2fSdrahn   bfd_vma high;
715d2201f2fSdrahn };
716d2201f2fSdrahn 
717d2201f2fSdrahn /* Adds a new entry to the line_info list in the line_info_table, ensuring
718d2201f2fSdrahn    that the list is sorted.  Note that the line_info list is sorted from
719d2201f2fSdrahn    highest to lowest VMA (with possible duplicates); that is,
720d2201f2fSdrahn    line_info->prev_line always accesses an equal or smaller VMA.  */
721d2201f2fSdrahn 
722f7cc78ecSespie static void
add_line_info(struct line_info_table * table,bfd_vma address,char * filename,unsigned int line,unsigned int column,int end_sequence)723*cf2f2c56Smiod add_line_info (struct line_info_table *table,
724*cf2f2c56Smiod 	       bfd_vma address,
725*cf2f2c56Smiod 	       char *filename,
726*cf2f2c56Smiod 	       unsigned int line,
727*cf2f2c56Smiod 	       unsigned int column,
728*cf2f2c56Smiod 	       int end_sequence)
729f7cc78ecSespie {
730d2201f2fSdrahn   bfd_size_type amt = sizeof (struct line_info);
731*cf2f2c56Smiod   struct line_info* info = bfd_alloc (table->abfd, amt);
732f7cc78ecSespie 
733d2201f2fSdrahn   /* Find the correct location for 'info'.  Normally we will receive
734d2201f2fSdrahn      new line_info data 1) in order and 2) with increasing VMAs.
735d2201f2fSdrahn      However some compilers break the rules (cf. decode_line_info) and
736d2201f2fSdrahn      so we include some heuristics for quickly finding the correct
737d2201f2fSdrahn      location for 'info'. In particular, these heuristics optimize for
738d2201f2fSdrahn      the common case in which the VMA sequence that we receive is a
739d2201f2fSdrahn      list of locally sorted VMAs such as
740d2201f2fSdrahn        p...z a...j  (where a < j < p < z)
741d2201f2fSdrahn 
742d2201f2fSdrahn      Note: table->lcl_head is used to head an *actual* or *possible*
743d2201f2fSdrahn      sequence within the list (such as a...j) that is not directly
744d2201f2fSdrahn      headed by table->last_line
745d2201f2fSdrahn 
746d2201f2fSdrahn      Note: we may receive duplicate entries from 'decode_line_info'.  */
747d2201f2fSdrahn 
748d2201f2fSdrahn   while (1)
749d2201f2fSdrahn     if (!table->last_line
750d2201f2fSdrahn 	|| address >= table->last_line->address)
751d2201f2fSdrahn       {
752d2201f2fSdrahn 	/* Normal case: add 'info' to the beginning of the list */
753f7cc78ecSespie 	info->prev_line = table->last_line;
754f7cc78ecSespie 	table->last_line = info;
755f7cc78ecSespie 
756d2201f2fSdrahn 	/* lcl_head: initialize to head a *possible* sequence at the end.  */
757d2201f2fSdrahn 	if (!table->lcl_head)
758d2201f2fSdrahn 	  table->lcl_head = info;
759d2201f2fSdrahn 	break;
760d2201f2fSdrahn       }
761d2201f2fSdrahn     else if (!table->lcl_head->prev_line
762d2201f2fSdrahn 	     && table->lcl_head->address > address)
763d2201f2fSdrahn       {
764d2201f2fSdrahn 	/* Abnormal but easy: lcl_head is 1) at the *end* of the line
765d2201f2fSdrahn 	   list and 2) the head of 'info'.  */
766d2201f2fSdrahn 	info->prev_line = NULL;
767d2201f2fSdrahn 	table->lcl_head->prev_line = info;
768d2201f2fSdrahn 	break;
769d2201f2fSdrahn       }
770d2201f2fSdrahn     else if (table->lcl_head->prev_line
771d2201f2fSdrahn 	     && table->lcl_head->address > address
772d2201f2fSdrahn 	     && address >= table->lcl_head->prev_line->address)
773d2201f2fSdrahn       {
774d2201f2fSdrahn 	/* Abnormal but easy: lcl_head is 1) in the *middle* of the line
775d2201f2fSdrahn 	   list and 2) the head of 'info'.  */
776d2201f2fSdrahn 	info->prev_line = table->lcl_head->prev_line;
777d2201f2fSdrahn 	table->lcl_head->prev_line = info;
778d2201f2fSdrahn 	break;
779d2201f2fSdrahn       }
780d2201f2fSdrahn     else
781d2201f2fSdrahn       {
782d2201f2fSdrahn 	/* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
783d2201f2fSdrahn 	   heads for 'info'.  Reset 'lcl_head' and repeat.  */
784d2201f2fSdrahn 	struct line_info* li2 = table->last_line; /* always non-NULL */
785d2201f2fSdrahn 	struct line_info* li1 = li2->prev_line;
786d2201f2fSdrahn 
787d2201f2fSdrahn 	while (li1)
788d2201f2fSdrahn 	  {
789d2201f2fSdrahn 	    if (li2->address > address && address >= li1->address)
790d2201f2fSdrahn 	      break;
791d2201f2fSdrahn 
792d2201f2fSdrahn 	    li2 = li1; /* always non-NULL */
793d2201f2fSdrahn 	    li1 = li1->prev_line;
794d2201f2fSdrahn 	  }
795d2201f2fSdrahn 	table->lcl_head = li2;
796d2201f2fSdrahn       }
797d2201f2fSdrahn 
798d2201f2fSdrahn   /* Set member data of 'info'.  */
799f7cc78ecSespie   info->address = address;
800f7cc78ecSespie   info->line = line;
801f7cc78ecSespie   info->column = column;
802f7cc78ecSespie   info->end_sequence = end_sequence;
803d2201f2fSdrahn 
804*cf2f2c56Smiod   if (filename && filename[0])
805d2201f2fSdrahn     {
806*cf2f2c56Smiod       info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
807d2201f2fSdrahn       if (info->filename)
808d2201f2fSdrahn 	strcpy (info->filename, filename);
809f7cc78ecSespie     }
810d2201f2fSdrahn   else
811d2201f2fSdrahn     info->filename = NULL;
812d2201f2fSdrahn }
813d2201f2fSdrahn 
814d2201f2fSdrahn /* Extract a fully qualified filename from a line info table.
815d2201f2fSdrahn    The returned string has been malloc'ed and it is the caller's
816d2201f2fSdrahn    responsibility to free it.  */
817f7cc78ecSespie 
818f7cc78ecSespie static char *
concat_filename(struct line_info_table * table,unsigned int file)819*cf2f2c56Smiod concat_filename (struct line_info_table *table, unsigned int file)
820f7cc78ecSespie {
821f7cc78ecSespie   char* filename;
822f7cc78ecSespie 
823f7cc78ecSespie   if (file - 1 >= table->num_files)
824f7cc78ecSespie     {
825f7cc78ecSespie       (*_bfd_error_handler)
826f7cc78ecSespie 	(_("Dwarf Error: mangled line number section (bad file number)."));
827d2201f2fSdrahn       return strdup ("<unknown>");
828f7cc78ecSespie     }
829f7cc78ecSespie 
830f7cc78ecSespie   filename = table->files[file - 1].name;
831f7cc78ecSespie 
832d2201f2fSdrahn   if (! IS_ABSOLUTE_PATH (filename))
833f7cc78ecSespie     {
834f7cc78ecSespie       char* dirname = (table->files[file - 1].dir
835f7cc78ecSespie 		       ? table->dirs[table->files[file - 1].dir - 1]
836f7cc78ecSespie 		       : table->comp_dir);
837d2201f2fSdrahn 
838d2201f2fSdrahn       /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
839d2201f2fSdrahn 	 The best we can do is return the filename part.  */
840d2201f2fSdrahn       if (dirname != NULL)
841d2201f2fSdrahn 	{
842d2201f2fSdrahn 	  unsigned int len = strlen (dirname) + strlen (filename) + 2;
843d2201f2fSdrahn 	  char * name;
844d2201f2fSdrahn 
845d2201f2fSdrahn 	  name = bfd_malloc (len);
846d2201f2fSdrahn 	  if (name)
847d2201f2fSdrahn 	    sprintf (name, "%s/%s", dirname, filename);
848d2201f2fSdrahn 	  return name;
849f7cc78ecSespie 	}
850f7cc78ecSespie     }
851f7cc78ecSespie 
852d2201f2fSdrahn   return strdup (filename);
853d2201f2fSdrahn }
854d2201f2fSdrahn 
855f7cc78ecSespie static void
arange_add(struct comp_unit * unit,bfd_vma low_pc,bfd_vma high_pc)856*cf2f2c56Smiod arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
857f7cc78ecSespie {
858f7cc78ecSespie   struct arange *arange;
859f7cc78ecSespie 
8605f210c2aSfgsch   /* First see if we can cheaply extend an existing range.  */
861f7cc78ecSespie   arange = &unit->arange;
8625f210c2aSfgsch 
863f7cc78ecSespie   do
864f7cc78ecSespie     {
865f7cc78ecSespie       if (low_pc == arange->high)
866f7cc78ecSespie 	{
867f7cc78ecSespie 	  arange->high = high_pc;
868f7cc78ecSespie 	  return;
869f7cc78ecSespie 	}
870f7cc78ecSespie       if (high_pc == arange->low)
871f7cc78ecSespie 	{
872f7cc78ecSespie 	  arange->low = low_pc;
873f7cc78ecSespie 	  return;
874f7cc78ecSespie 	}
875f7cc78ecSespie       arange = arange->next;
876f7cc78ecSespie     }
877f7cc78ecSespie   while (arange);
878f7cc78ecSespie 
879f7cc78ecSespie   if (unit->arange.high == 0)
880f7cc78ecSespie     {
8815f210c2aSfgsch       /* This is the first address range: store it in unit->arange.  */
882f7cc78ecSespie       unit->arange.next = 0;
883f7cc78ecSespie       unit->arange.low = low_pc;
884f7cc78ecSespie       unit->arange.high = high_pc;
885f7cc78ecSespie       return;
886f7cc78ecSespie     }
887f7cc78ecSespie 
8885f210c2aSfgsch   /* Need to allocate a new arange and insert it into the arange list.  */
889*cf2f2c56Smiod   arange = bfd_zalloc (unit->abfd, sizeof (*arange));
890f7cc78ecSespie   arange->low = low_pc;
891f7cc78ecSespie   arange->high = high_pc;
892f7cc78ecSespie 
893f7cc78ecSespie   arange->next = unit->arange.next;
894f7cc78ecSespie   unit->arange.next = arange;
895f7cc78ecSespie }
896f7cc78ecSespie 
897f7cc78ecSespie /* Decode the line number information for UNIT.  */
898f7cc78ecSespie 
899f7cc78ecSespie static struct line_info_table*
decode_line_info(struct comp_unit * unit,struct dwarf2_debug * stash)900*cf2f2c56Smiod decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
901f7cc78ecSespie {
902f7cc78ecSespie   bfd *abfd = unit->abfd;
903f7cc78ecSespie   struct line_info_table* table;
904f7cc78ecSespie   char *line_ptr;
905f7cc78ecSespie   char *line_end;
906f7cc78ecSespie   struct line_head lh;
907d2201f2fSdrahn   unsigned int i, bytes_read, offset_size;
908f7cc78ecSespie   char *cur_file, *cur_dir;
909f7cc78ecSespie   unsigned char op_code, extended_op, adj_opcode;
910d2201f2fSdrahn   bfd_size_type amt;
911f7cc78ecSespie 
912f7cc78ecSespie   if (! stash->dwarf_line_buffer)
913f7cc78ecSespie     {
914f7cc78ecSespie       asection *msec;
915f7cc78ecSespie 
916f7cc78ecSespie       msec = bfd_get_section_by_name (abfd, ".debug_line");
917f7cc78ecSespie       if (! msec)
918f7cc78ecSespie 	{
919f7cc78ecSespie 	  (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
920f7cc78ecSespie 	  bfd_set_error (bfd_error_bad_value);
921f7cc78ecSespie 	  return 0;
922f7cc78ecSespie 	}
923f7cc78ecSespie 
9245f210c2aSfgsch       stash->dwarf_line_size = msec->_raw_size;
925d2201f2fSdrahn       stash->dwarf_line_buffer
926d2201f2fSdrahn 	= bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
927d2201f2fSdrahn 						     stash->syms);
928f7cc78ecSespie       if (! stash->dwarf_line_buffer)
929f7cc78ecSespie 	return 0;
9305f210c2aSfgsch     }
9315f210c2aSfgsch 
932d2201f2fSdrahn   /* It is possible to get a bad value for the line_offset.  Validate
933d2201f2fSdrahn      it here so that we won't get a segfault below.  */
9345f210c2aSfgsch   if (unit->line_offset >= stash->dwarf_line_size)
9355f210c2aSfgsch     {
936d2201f2fSdrahn       (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
9375f210c2aSfgsch 			     unit->line_offset, stash->dwarf_line_size);
9385f210c2aSfgsch       bfd_set_error (bfd_error_bad_value);
9395f210c2aSfgsch       return 0;
940f7cc78ecSespie     }
941f7cc78ecSespie 
942d2201f2fSdrahn   amt = sizeof (struct line_info_table);
943*cf2f2c56Smiod   table = bfd_alloc (abfd, amt);
944f7cc78ecSespie   table->abfd = abfd;
945f7cc78ecSespie   table->comp_dir = unit->comp_dir;
946f7cc78ecSespie 
947f7cc78ecSespie   table->num_files = 0;
948f7cc78ecSespie   table->files = NULL;
949f7cc78ecSespie 
950f7cc78ecSespie   table->num_dirs = 0;
951f7cc78ecSespie   table->dirs = NULL;
952f7cc78ecSespie 
953f7cc78ecSespie   table->files = NULL;
954f7cc78ecSespie   table->last_line = NULL;
955d2201f2fSdrahn   table->lcl_head = NULL;
956f7cc78ecSespie 
957f7cc78ecSespie   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
958f7cc78ecSespie 
9595f210c2aSfgsch   /* Read in the prologue.  */
960f7cc78ecSespie   lh.total_length = read_4_bytes (abfd, line_ptr);
961f7cc78ecSespie   line_ptr += 4;
962d2201f2fSdrahn   offset_size = 4;
963d2201f2fSdrahn   if (lh.total_length == 0xffffffff)
964d2201f2fSdrahn     {
965d2201f2fSdrahn       lh.total_length = read_8_bytes (abfd, line_ptr);
966d2201f2fSdrahn       line_ptr += 8;
967d2201f2fSdrahn       offset_size = 8;
968d2201f2fSdrahn     }
969d2201f2fSdrahn   else if (lh.total_length == 0 && unit->addr_size == 8)
970d2201f2fSdrahn     {
971d2201f2fSdrahn       /* Handle (non-standard) 64-bit DWARF2 formats.  */
972d2201f2fSdrahn       lh.total_length = read_4_bytes (abfd, line_ptr);
973d2201f2fSdrahn       line_ptr += 4;
974d2201f2fSdrahn       offset_size = 8;
975d2201f2fSdrahn     }
976f7cc78ecSespie   line_end = line_ptr + lh.total_length;
977f7cc78ecSespie   lh.version = read_2_bytes (abfd, line_ptr);
978f7cc78ecSespie   line_ptr += 2;
979d2201f2fSdrahn   if (offset_size == 4)
980f7cc78ecSespie     lh.prologue_length = read_4_bytes (abfd, line_ptr);
981d2201f2fSdrahn   else
982d2201f2fSdrahn     lh.prologue_length = read_8_bytes (abfd, line_ptr);
983d2201f2fSdrahn   line_ptr += offset_size;
984f7cc78ecSespie   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
985f7cc78ecSespie   line_ptr += 1;
986f7cc78ecSespie   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
987f7cc78ecSespie   line_ptr += 1;
988f7cc78ecSespie   lh.line_base = read_1_signed_byte (abfd, line_ptr);
989f7cc78ecSespie   line_ptr += 1;
990f7cc78ecSespie   lh.line_range = read_1_byte (abfd, line_ptr);
991f7cc78ecSespie   line_ptr += 1;
992f7cc78ecSespie   lh.opcode_base = read_1_byte (abfd, line_ptr);
993f7cc78ecSespie   line_ptr += 1;
994d2201f2fSdrahn   amt = lh.opcode_base * sizeof (unsigned char);
995*cf2f2c56Smiod   lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
996f7cc78ecSespie 
997f7cc78ecSespie   lh.standard_opcode_lengths[0] = 1;
9985f210c2aSfgsch 
999f7cc78ecSespie   for (i = 1; i < lh.opcode_base; ++i)
1000f7cc78ecSespie     {
1001f7cc78ecSespie       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1002f7cc78ecSespie       line_ptr += 1;
1003f7cc78ecSespie     }
1004f7cc78ecSespie 
10055f210c2aSfgsch   /* Read directory table.  */
1006f7cc78ecSespie   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1007f7cc78ecSespie     {
1008f7cc78ecSespie       line_ptr += bytes_read;
10095f210c2aSfgsch 
1010f7cc78ecSespie       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1011f7cc78ecSespie 	{
1012d2201f2fSdrahn 	  amt = table->num_dirs + DIR_ALLOC_CHUNK;
1013d2201f2fSdrahn 	  amt *= sizeof (char *);
1014*cf2f2c56Smiod 	  table->dirs = bfd_realloc (table->dirs, amt);
1015f7cc78ecSespie 	  if (! table->dirs)
1016f7cc78ecSespie 	    return 0;
1017f7cc78ecSespie 	}
10185f210c2aSfgsch 
1019f7cc78ecSespie       table->dirs[table->num_dirs++] = cur_dir;
1020f7cc78ecSespie     }
10215f210c2aSfgsch 
1022f7cc78ecSespie   line_ptr += bytes_read;
1023f7cc78ecSespie 
10245f210c2aSfgsch   /* Read file name table.  */
1025f7cc78ecSespie   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1026f7cc78ecSespie     {
1027f7cc78ecSespie       line_ptr += bytes_read;
10285f210c2aSfgsch 
1029f7cc78ecSespie       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1030f7cc78ecSespie 	{
1031d2201f2fSdrahn 	  amt = table->num_files + FILE_ALLOC_CHUNK;
1032d2201f2fSdrahn 	  amt *= sizeof (struct fileinfo);
1033*cf2f2c56Smiod 	  table->files = bfd_realloc (table->files, amt);
1034f7cc78ecSespie 	  if (! table->files)
1035f7cc78ecSespie 	    return 0;
1036f7cc78ecSespie 	}
10375f210c2aSfgsch 
1038f7cc78ecSespie       table->files[table->num_files].name = cur_file;
1039f7cc78ecSespie       table->files[table->num_files].dir =
1040f7cc78ecSespie 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1041f7cc78ecSespie       line_ptr += bytes_read;
1042f7cc78ecSespie       table->files[table->num_files].time =
1043f7cc78ecSespie 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1044f7cc78ecSespie       line_ptr += bytes_read;
1045f7cc78ecSespie       table->files[table->num_files].size =
1046f7cc78ecSespie 	read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1047f7cc78ecSespie       line_ptr += bytes_read;
1048f7cc78ecSespie       table->num_files++;
1049f7cc78ecSespie     }
10505f210c2aSfgsch 
1051f7cc78ecSespie   line_ptr += bytes_read;
1052f7cc78ecSespie 
1053f7cc78ecSespie   /* Read the statement sequences until there's nothing left.  */
1054f7cc78ecSespie   while (line_ptr < line_end)
1055f7cc78ecSespie     {
10565f210c2aSfgsch       /* State machine registers.  */
1057f7cc78ecSespie       bfd_vma address = 0;
1058*cf2f2c56Smiod       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1059f7cc78ecSespie       unsigned int line = 1;
1060f7cc78ecSespie       unsigned int column = 0;
1061f7cc78ecSespie       int is_stmt = lh.default_is_stmt;
1062f7cc78ecSespie       int basic_block = 0;
1063d2201f2fSdrahn       int end_sequence = 0;
1064d2201f2fSdrahn       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1065d2201f2fSdrahn 	 compilers generate address sequences that are wildly out of
1066d2201f2fSdrahn 	 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1067d2201f2fSdrahn 	 for ia64-Linux).  Thus, to determine the low and high
1068d2201f2fSdrahn 	 address, we must compare on every DW_LNS_copy, etc.  */
1069f7cc78ecSespie       bfd_vma low_pc  = 0;
1070d2201f2fSdrahn       bfd_vma high_pc = 0;
1071f7cc78ecSespie 
1072f7cc78ecSespie       /* Decode the table.  */
1073f7cc78ecSespie       while (! end_sequence)
1074f7cc78ecSespie 	{
1075f7cc78ecSespie 	  op_code = read_1_byte (abfd, line_ptr);
1076f7cc78ecSespie 	  line_ptr += 1;
10775f210c2aSfgsch 
1078d2201f2fSdrahn 	  if (op_code >= lh.opcode_base)
1079d2201f2fSdrahn 	    {
1080d2201f2fSdrahn 	      /* Special operand.  */
1081d2201f2fSdrahn 	      adj_opcode = op_code - lh.opcode_base;
1082d2201f2fSdrahn 	      address += (adj_opcode / lh.line_range)
1083d2201f2fSdrahn 		* lh.minimum_instruction_length;
1084d2201f2fSdrahn 	      line += lh.line_base + (adj_opcode % lh.line_range);
1085d2201f2fSdrahn 	      /* Append row to matrix using current values.  */
1086d2201f2fSdrahn 	      add_line_info (table, address, filename, line, column, 0);
1087d2201f2fSdrahn 	      basic_block = 1;
1088d2201f2fSdrahn 	      if (low_pc == 0 || address < low_pc)
1089d2201f2fSdrahn 		low_pc = address;
1090d2201f2fSdrahn 	      if (address > high_pc)
1091d2201f2fSdrahn 		high_pc = address;
1092d2201f2fSdrahn 	    }
1093d2201f2fSdrahn 	  else switch (op_code)
1094f7cc78ecSespie 	    {
1095f7cc78ecSespie 	    case DW_LNS_extended_op:
1096d2201f2fSdrahn 	      /* Ignore length.  */
1097d2201f2fSdrahn 	      line_ptr += 1;
1098f7cc78ecSespie 	      extended_op = read_1_byte (abfd, line_ptr);
1099f7cc78ecSespie 	      line_ptr += 1;
1100d2201f2fSdrahn 
1101f7cc78ecSespie 	      switch (extended_op)
1102f7cc78ecSespie 		{
1103f7cc78ecSespie 		case DW_LNE_end_sequence:
1104f7cc78ecSespie 		  end_sequence = 1;
1105f7cc78ecSespie 		  add_line_info (table, address, filename, line, column,
1106f7cc78ecSespie 				 end_sequence);
1107d2201f2fSdrahn 		  if (low_pc == 0 || address < low_pc)
1108f7cc78ecSespie 		    low_pc = address;
1109d2201f2fSdrahn 		  if (address > high_pc)
1110d2201f2fSdrahn 		    high_pc = address;
1111d2201f2fSdrahn 		  arange_add (unit, low_pc, high_pc);
1112f7cc78ecSespie 		  break;
1113f7cc78ecSespie 		case DW_LNE_set_address:
1114f7cc78ecSespie 		  address = read_address (unit, line_ptr);
1115f7cc78ecSespie 		  line_ptr += unit->addr_size;
1116f7cc78ecSespie 		  break;
1117f7cc78ecSespie 		case DW_LNE_define_file:
1118f7cc78ecSespie 		  cur_file = read_string (abfd, line_ptr, &bytes_read);
1119f7cc78ecSespie 		  line_ptr += bytes_read;
1120f7cc78ecSespie 		  if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1121f7cc78ecSespie 		    {
1122d2201f2fSdrahn 		      amt = table->num_files + FILE_ALLOC_CHUNK;
1123d2201f2fSdrahn 		      amt *= sizeof (struct fileinfo);
1124*cf2f2c56Smiod 		      table->files = bfd_realloc (table->files, amt);
1125f7cc78ecSespie 		      if (! table->files)
1126f7cc78ecSespie 			return 0;
1127f7cc78ecSespie 		    }
1128f7cc78ecSespie 		  table->files[table->num_files].name = cur_file;
1129f7cc78ecSespie 		  table->files[table->num_files].dir =
1130f7cc78ecSespie 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1131f7cc78ecSespie 		  line_ptr += bytes_read;
1132f7cc78ecSespie 		  table->files[table->num_files].time =
1133f7cc78ecSespie 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1134f7cc78ecSespie 		  line_ptr += bytes_read;
1135f7cc78ecSespie 		  table->files[table->num_files].size =
1136f7cc78ecSespie 		    read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1137f7cc78ecSespie 		  line_ptr += bytes_read;
1138f7cc78ecSespie 		  table->num_files++;
1139f7cc78ecSespie 		  break;
1140f7cc78ecSespie 		default:
1141f7cc78ecSespie 		  (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1142f7cc78ecSespie 		  bfd_set_error (bfd_error_bad_value);
1143f7cc78ecSespie 		  return 0;
1144f7cc78ecSespie 		}
1145f7cc78ecSespie 	      break;
1146f7cc78ecSespie 	    case DW_LNS_copy:
1147f7cc78ecSespie 	      add_line_info (table, address, filename, line, column, 0);
1148f7cc78ecSespie 	      basic_block = 0;
1149d2201f2fSdrahn 	      if (low_pc == 0 || address < low_pc)
1150f7cc78ecSespie 		low_pc = address;
1151d2201f2fSdrahn 	      if (address > high_pc)
1152d2201f2fSdrahn 		high_pc = address;
1153f7cc78ecSespie 	      break;
1154f7cc78ecSespie 	    case DW_LNS_advance_pc:
1155f7cc78ecSespie 	      address += lh.minimum_instruction_length
1156f7cc78ecSespie 		* read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1157f7cc78ecSespie 	      line_ptr += bytes_read;
1158f7cc78ecSespie 	      break;
1159f7cc78ecSespie 	    case DW_LNS_advance_line:
1160f7cc78ecSespie 	      line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1161f7cc78ecSespie 	      line_ptr += bytes_read;
1162f7cc78ecSespie 	      break;
1163f7cc78ecSespie 	    case DW_LNS_set_file:
1164f7cc78ecSespie 	      {
1165f7cc78ecSespie 		unsigned int file;
1166f7cc78ecSespie 
1167d2201f2fSdrahn 		/* The file and directory tables are 0
1168d2201f2fSdrahn 		   based, the references are 1 based.  */
1169f7cc78ecSespie 		file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1170f7cc78ecSespie 		line_ptr += bytes_read;
1171d2201f2fSdrahn 		if (filename)
1172d2201f2fSdrahn 		  free (filename);
1173f7cc78ecSespie 		filename = concat_filename (table, file);
1174f7cc78ecSespie 		break;
1175f7cc78ecSespie 	      }
1176f7cc78ecSespie 	    case DW_LNS_set_column:
1177f7cc78ecSespie 	      column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1178f7cc78ecSespie 	      line_ptr += bytes_read;
1179f7cc78ecSespie 	      break;
1180f7cc78ecSespie 	    case DW_LNS_negate_stmt:
1181f7cc78ecSespie 	      is_stmt = (!is_stmt);
1182f7cc78ecSespie 	      break;
1183f7cc78ecSespie 	    case DW_LNS_set_basic_block:
1184f7cc78ecSespie 	      basic_block = 1;
1185f7cc78ecSespie 	      break;
1186f7cc78ecSespie 	    case DW_LNS_const_add_pc:
1187f7cc78ecSespie 	      address += lh.minimum_instruction_length
1188f7cc78ecSespie 		      * ((255 - lh.opcode_base) / lh.line_range);
1189f7cc78ecSespie 	      break;
1190f7cc78ecSespie 	    case DW_LNS_fixed_advance_pc:
1191f7cc78ecSespie 	      address += read_2_bytes (abfd, line_ptr);
1192f7cc78ecSespie 	      line_ptr += 2;
1193f7cc78ecSespie 	      break;
1194d2201f2fSdrahn 	    default:
1195f7cc78ecSespie 	      {
1196d2201f2fSdrahn 		int i;
1197d2201f2fSdrahn 
1198d2201f2fSdrahn 		/* Unknown standard opcode, ignore it.  */
1199d2201f2fSdrahn 		for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1200d2201f2fSdrahn 		  {
1201d2201f2fSdrahn 		    (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1202d2201f2fSdrahn 		    line_ptr += bytes_read;
1203f7cc78ecSespie 		  }
1204f7cc78ecSespie 	      }
1205f7cc78ecSespie 	    }
1206f7cc78ecSespie 	}
1207f7cc78ecSespie 
1208d2201f2fSdrahn       if (filename)
1209d2201f2fSdrahn 	free (filename);
1210d2201f2fSdrahn     }
1211d2201f2fSdrahn 
1212f7cc78ecSespie   return table;
1213f7cc78ecSespie }
1214f7cc78ecSespie 
1215d2201f2fSdrahn /* If ADDR is within TABLE set the output parameters and return TRUE,
1216d2201f2fSdrahn    otherwise return FALSE.  The output parameters, FILENAME_PTR and
1217f7cc78ecSespie    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
1218f7cc78ecSespie 
1219d2201f2fSdrahn static bfd_boolean
lookup_address_in_line_info_table(struct line_info_table * table,bfd_vma addr,struct funcinfo * function,const char ** filename_ptr,unsigned int * linenumber_ptr)1220*cf2f2c56Smiod lookup_address_in_line_info_table (struct line_info_table *table,
1221*cf2f2c56Smiod 				   bfd_vma addr,
1222*cf2f2c56Smiod 				   struct funcinfo *function,
1223*cf2f2c56Smiod 				   const char **filename_ptr,
1224*cf2f2c56Smiod 				   unsigned int *linenumber_ptr)
1225f7cc78ecSespie {
1226d2201f2fSdrahn   /* Note: table->last_line should be a descendingly sorted list. */
1227f7cc78ecSespie   struct line_info* next_line = table->last_line;
1228d2201f2fSdrahn   struct line_info* each_line = NULL;
1229d2201f2fSdrahn   *filename_ptr = NULL;
1230f7cc78ecSespie 
1231f7cc78ecSespie   if (!next_line)
1232d2201f2fSdrahn     return FALSE;
1233f7cc78ecSespie 
1234f7cc78ecSespie   each_line = next_line->prev_line;
1235f7cc78ecSespie 
1236d2201f2fSdrahn   /* Check for large addresses */
1237d2201f2fSdrahn   if (addr > next_line->address)
1238d2201f2fSdrahn     each_line = NULL; /* ensure we skip over the normal case */
1239d2201f2fSdrahn 
1240d2201f2fSdrahn   /* Normal case: search the list; save  */
1241f7cc78ecSespie   while (each_line && next_line)
1242f7cc78ecSespie     {
1243d2201f2fSdrahn       /* If we have an address match, save this info.  This allows us
1244d2201f2fSdrahn 	 to return as good as results as possible for strange debugging
1245d2201f2fSdrahn 	 info.  */
1246d2201f2fSdrahn       bfd_boolean addr_match = FALSE;
1247d2201f2fSdrahn       if (each_line->address <= addr && addr <= next_line->address)
1248d2201f2fSdrahn 	{
1249d2201f2fSdrahn 	  addr_match = TRUE;
1250d2201f2fSdrahn 
1251d2201f2fSdrahn 	  /* If this line appears to span functions, and addr is in the
1252d2201f2fSdrahn 	     later function, return the first line of that function instead
1253d2201f2fSdrahn 	     of the last line of the earlier one.  This check is for GCC
1254d2201f2fSdrahn 	     2.95, which emits the first line number for a function late.  */
1255d2201f2fSdrahn 	  if (function != NULL
1256d2201f2fSdrahn 	      && each_line->address < function->low
1257d2201f2fSdrahn 	      && next_line->address > function->low)
1258d2201f2fSdrahn 	    {
1259d2201f2fSdrahn 	      *filename_ptr = next_line->filename;
1260d2201f2fSdrahn 	      *linenumber_ptr = next_line->line;
1261d2201f2fSdrahn 	    }
1262d2201f2fSdrahn 	  else
1263f7cc78ecSespie 	    {
1264f7cc78ecSespie 	      *filename_ptr = each_line->filename;
1265f7cc78ecSespie 	      *linenumber_ptr = each_line->line;
1266f7cc78ecSespie 	    }
1267d2201f2fSdrahn 	}
1268d2201f2fSdrahn 
1269d2201f2fSdrahn       if (addr_match && !each_line->end_sequence)
1270d2201f2fSdrahn 	return TRUE; /* we have definitely found what we want */
1271d2201f2fSdrahn 
1272f7cc78ecSespie       next_line = each_line;
1273f7cc78ecSespie       each_line = each_line->prev_line;
1274f7cc78ecSespie     }
1275f7cc78ecSespie 
1276d2201f2fSdrahn   /* At this point each_line is NULL but next_line is not.  If we found
1277d2201f2fSdrahn      a candidate end-of-sequence point in the loop above, we can return
1278d2201f2fSdrahn      that (compatibility with a bug in the Intel compiler); otherwise,
1279d2201f2fSdrahn      assuming that we found the containing function for this address in
1280d2201f2fSdrahn      this compilation unit, return the first line we have a number for
1281d2201f2fSdrahn      (compatibility with GCC 2.95).  */
1282d2201f2fSdrahn   if (*filename_ptr == NULL && function != NULL)
1283d2201f2fSdrahn     {
1284d2201f2fSdrahn       *filename_ptr = next_line->filename;
1285d2201f2fSdrahn       *linenumber_ptr = next_line->line;
1286d2201f2fSdrahn       return TRUE;
1287d2201f2fSdrahn     }
1288d2201f2fSdrahn 
1289d2201f2fSdrahn   return FALSE;
1290f7cc78ecSespie }
1291f7cc78ecSespie 
1292f7cc78ecSespie /* Function table functions.  */
1293f7cc78ecSespie 
1294d2201f2fSdrahn /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.  */
1295f7cc78ecSespie 
1296d2201f2fSdrahn static bfd_boolean
lookup_address_in_function_table(struct funcinfo * table,bfd_vma addr,struct funcinfo ** function_ptr,const char ** functionname_ptr)1297*cf2f2c56Smiod lookup_address_in_function_table (struct funcinfo *table,
1298*cf2f2c56Smiod 				  bfd_vma addr,
1299*cf2f2c56Smiod 				  struct funcinfo **function_ptr,
1300*cf2f2c56Smiod 				  const char **functionname_ptr)
1301f7cc78ecSespie {
1302f7cc78ecSespie   struct funcinfo* each_func;
1303f7cc78ecSespie 
1304f7cc78ecSespie   for (each_func = table;
1305f7cc78ecSespie        each_func;
1306f7cc78ecSespie        each_func = each_func->prev_func)
1307f7cc78ecSespie     {
1308f7cc78ecSespie       if (addr >= each_func->low && addr < each_func->high)
1309f7cc78ecSespie 	{
1310f7cc78ecSespie 	  *functionname_ptr = each_func->name;
1311d2201f2fSdrahn 	  *function_ptr = each_func;
1312d2201f2fSdrahn 	  return TRUE;
1313f7cc78ecSespie 	}
1314f7cc78ecSespie     }
1315f7cc78ecSespie 
1316d2201f2fSdrahn   return FALSE;
1317f7cc78ecSespie }
1318f7cc78ecSespie 
1319f7cc78ecSespie /* DWARF2 Compilation unit functions.  */
1320f7cc78ecSespie 
1321f7cc78ecSespie /* Scan over each die in a comp. unit looking for functions to add
1322f7cc78ecSespie    to the function table.  */
1323f7cc78ecSespie 
1324d2201f2fSdrahn static bfd_boolean
scan_unit_for_functions(struct comp_unit * unit)1325*cf2f2c56Smiod scan_unit_for_functions (struct comp_unit *unit)
1326f7cc78ecSespie {
1327f7cc78ecSespie   bfd *abfd = unit->abfd;
1328f7cc78ecSespie   char *info_ptr = unit->first_child_die_ptr;
1329f7cc78ecSespie   int nesting_level = 1;
1330f7cc78ecSespie 
1331f7cc78ecSespie   while (nesting_level)
1332f7cc78ecSespie     {
1333f7cc78ecSespie       unsigned int abbrev_number, bytes_read, i;
1334f7cc78ecSespie       struct abbrev_info *abbrev;
1335f7cc78ecSespie       struct attribute attr;
1336f7cc78ecSespie       struct funcinfo *func;
1337f7cc78ecSespie       char* name = 0;
1338f7cc78ecSespie 
1339f7cc78ecSespie       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1340f7cc78ecSespie       info_ptr += bytes_read;
1341f7cc78ecSespie 
1342f7cc78ecSespie       if (! abbrev_number)
1343f7cc78ecSespie 	{
1344f7cc78ecSespie 	  nesting_level--;
1345f7cc78ecSespie 	  continue;
1346f7cc78ecSespie 	}
1347f7cc78ecSespie 
1348f7cc78ecSespie       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1349f7cc78ecSespie       if (! abbrev)
1350f7cc78ecSespie 	{
1351d2201f2fSdrahn 	  (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1352f7cc78ecSespie 			     abbrev_number);
1353f7cc78ecSespie 	  bfd_set_error (bfd_error_bad_value);
1354d2201f2fSdrahn 	  return FALSE;
1355f7cc78ecSespie 	}
1356f7cc78ecSespie 
1357f7cc78ecSespie       if (abbrev->tag == DW_TAG_subprogram)
1358f7cc78ecSespie 	{
1359d2201f2fSdrahn 	  bfd_size_type amt = sizeof (struct funcinfo);
1360*cf2f2c56Smiod 	  func = bfd_zalloc (abfd, amt);
1361f7cc78ecSespie 	  func->prev_func = unit->function_table;
1362f7cc78ecSespie 	  unit->function_table = func;
1363f7cc78ecSespie 	}
1364f7cc78ecSespie       else
1365f7cc78ecSespie 	func = NULL;
1366f7cc78ecSespie 
1367f7cc78ecSespie       for (i = 0; i < abbrev->num_attrs; ++i)
1368f7cc78ecSespie 	{
1369f7cc78ecSespie 	  info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1370f7cc78ecSespie 
1371f7cc78ecSespie 	  if (func)
1372f7cc78ecSespie 	    {
1373f7cc78ecSespie 	      switch (attr.name)
1374f7cc78ecSespie 		{
1375f7cc78ecSespie 		case DW_AT_name:
1376f7cc78ecSespie 
1377*cf2f2c56Smiod 		  name = attr.u.str;
1378f7cc78ecSespie 
1379f7cc78ecSespie 		  /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1380f7cc78ecSespie 		  if (func->name == NULL)
1381*cf2f2c56Smiod 		    func->name = attr.u.str;
1382f7cc78ecSespie 		  break;
1383f7cc78ecSespie 
1384f7cc78ecSespie 		case DW_AT_MIPS_linkage_name:
1385*cf2f2c56Smiod 		  func->name = attr.u.str;
1386f7cc78ecSespie 		  break;
1387f7cc78ecSespie 
1388f7cc78ecSespie 		case DW_AT_low_pc:
1389*cf2f2c56Smiod 		  func->low = attr.u.val;
1390f7cc78ecSespie 		  break;
1391f7cc78ecSespie 
1392f7cc78ecSespie 		case DW_AT_high_pc:
1393*cf2f2c56Smiod 		  func->high = attr.u.val;
1394f7cc78ecSespie 		  break;
1395f7cc78ecSespie 
1396f7cc78ecSespie 		default:
1397f7cc78ecSespie 		  break;
1398f7cc78ecSespie 		}
1399f7cc78ecSespie 	    }
1400f7cc78ecSespie 	  else
1401f7cc78ecSespie 	    {
1402f7cc78ecSespie 	      switch (attr.name)
1403f7cc78ecSespie 		{
1404f7cc78ecSespie 		case DW_AT_name:
1405*cf2f2c56Smiod 		  name = attr.u.str;
1406f7cc78ecSespie 		  break;
1407f7cc78ecSespie 
1408f7cc78ecSespie 		default:
1409f7cc78ecSespie 		  break;
1410f7cc78ecSespie 		}
1411f7cc78ecSespie 	    }
1412f7cc78ecSespie 	}
1413f7cc78ecSespie 
1414f7cc78ecSespie       if (abbrev->has_children)
1415f7cc78ecSespie 	nesting_level++;
1416f7cc78ecSespie     }
1417f7cc78ecSespie 
1418d2201f2fSdrahn   return TRUE;
1419f7cc78ecSespie }
1420f7cc78ecSespie 
1421f7cc78ecSespie /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
1422f7cc78ecSespie    includes the compilation unit header that proceeds the DIE's, but
1423*cf2f2c56Smiod    does not include the length field that precedes each compilation
1424f7cc78ecSespie    unit header.  END_PTR points one past the end of this comp unit.
1425d2201f2fSdrahn    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1426f7cc78ecSespie 
1427f7cc78ecSespie    This routine does not read the whole compilation unit; only enough
1428f7cc78ecSespie    to get to the line number information for the compilation unit.  */
1429f7cc78ecSespie 
1430f7cc78ecSespie static struct comp_unit *
parse_comp_unit(bfd * abfd,struct dwarf2_debug * stash,bfd_vma unit_length,unsigned int offset_size)1431*cf2f2c56Smiod parse_comp_unit (bfd *abfd,
1432*cf2f2c56Smiod 		 struct dwarf2_debug *stash,
1433*cf2f2c56Smiod 		 bfd_vma unit_length,
1434*cf2f2c56Smiod 		 unsigned int offset_size)
1435f7cc78ecSespie {
1436f7cc78ecSespie   struct comp_unit* unit;
1437d2201f2fSdrahn   unsigned int version;
1438*cf2f2c56Smiod   bfd_uint64_t abbrev_offset = 0;
1439d2201f2fSdrahn   unsigned int addr_size;
1440f7cc78ecSespie   struct abbrev_info** abbrevs;
1441f7cc78ecSespie   unsigned int abbrev_number, bytes_read, i;
1442f7cc78ecSespie   struct abbrev_info *abbrev;
1443f7cc78ecSespie   struct attribute attr;
14445f210c2aSfgsch   char *info_ptr = stash->info_ptr;
14455f210c2aSfgsch   char *end_ptr = info_ptr + unit_length;
1446d2201f2fSdrahn   bfd_size_type amt;
14475f210c2aSfgsch 
1448f7cc78ecSespie   version = read_2_bytes (abfd, info_ptr);
1449f7cc78ecSespie   info_ptr += 2;
1450d2201f2fSdrahn   BFD_ASSERT (offset_size == 4 || offset_size == 8);
1451d2201f2fSdrahn   if (offset_size == 4)
1452f7cc78ecSespie     abbrev_offset = read_4_bytes (abfd, info_ptr);
1453d2201f2fSdrahn   else
1454f7cc78ecSespie     abbrev_offset = read_8_bytes (abfd, info_ptr);
1455d2201f2fSdrahn   info_ptr += offset_size;
1456f7cc78ecSespie   addr_size = read_1_byte (abfd, info_ptr);
1457f7cc78ecSespie   info_ptr += 1;
1458f7cc78ecSespie 
1459f7cc78ecSespie   if (version != 2)
1460f7cc78ecSespie     {
1461d2201f2fSdrahn       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1462f7cc78ecSespie       bfd_set_error (bfd_error_bad_value);
1463f7cc78ecSespie       return 0;
1464f7cc78ecSespie     }
1465f7cc78ecSespie 
1466f7cc78ecSespie   if (addr_size > sizeof (bfd_vma))
1467f7cc78ecSespie     {
1468f7cc78ecSespie       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1469f7cc78ecSespie 			 addr_size,
1470d2201f2fSdrahn 			 (unsigned int) sizeof (bfd_vma));
1471f7cc78ecSespie       bfd_set_error (bfd_error_bad_value);
1472f7cc78ecSespie       return 0;
1473f7cc78ecSespie     }
1474f7cc78ecSespie 
1475f7cc78ecSespie   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1476f7cc78ecSespie     {
1477f7cc78ecSespie       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1478f7cc78ecSespie       bfd_set_error (bfd_error_bad_value);
1479f7cc78ecSespie       return 0;
1480f7cc78ecSespie     }
1481f7cc78ecSespie 
14825f210c2aSfgsch   /* Read the abbrevs for this compilation unit into a table.  */
14835f210c2aSfgsch   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1484f7cc78ecSespie   if (! abbrevs)
1485f7cc78ecSespie       return 0;
1486f7cc78ecSespie 
1487f7cc78ecSespie   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1488f7cc78ecSespie   info_ptr += bytes_read;
1489f7cc78ecSespie   if (! abbrev_number)
1490f7cc78ecSespie     {
1491d2201f2fSdrahn       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1492f7cc78ecSespie 			 abbrev_number);
1493f7cc78ecSespie       bfd_set_error (bfd_error_bad_value);
1494f7cc78ecSespie       return 0;
1495f7cc78ecSespie     }
1496f7cc78ecSespie 
1497f7cc78ecSespie   abbrev = lookup_abbrev (abbrev_number, abbrevs);
1498f7cc78ecSespie   if (! abbrev)
1499f7cc78ecSespie     {
1500d2201f2fSdrahn       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1501f7cc78ecSespie 			 abbrev_number);
1502f7cc78ecSespie       bfd_set_error (bfd_error_bad_value);
1503f7cc78ecSespie       return 0;
1504f7cc78ecSespie     }
1505f7cc78ecSespie 
1506d2201f2fSdrahn   amt = sizeof (struct comp_unit);
1507*cf2f2c56Smiod   unit = bfd_zalloc (abfd, amt);
1508f7cc78ecSespie   unit->abfd = abfd;
1509f7cc78ecSespie   unit->addr_size = addr_size;
1510d2201f2fSdrahn   unit->offset_size = offset_size;
1511f7cc78ecSespie   unit->abbrevs = abbrevs;
1512f7cc78ecSespie   unit->end_ptr = end_ptr;
1513d2201f2fSdrahn   unit->stash = stash;
1514f7cc78ecSespie 
1515f7cc78ecSespie   for (i = 0; i < abbrev->num_attrs; ++i)
1516f7cc78ecSespie     {
1517f7cc78ecSespie       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1518f7cc78ecSespie 
1519f7cc78ecSespie       /* Store the data if it is of an attribute we want to keep in a
1520f7cc78ecSespie 	 partial symbol table.  */
1521f7cc78ecSespie       switch (attr.name)
1522f7cc78ecSespie 	{
1523f7cc78ecSespie 	case DW_AT_stmt_list:
1524f7cc78ecSespie 	  unit->stmtlist = 1;
1525*cf2f2c56Smiod 	  unit->line_offset = attr.u.val;
1526f7cc78ecSespie 	  break;
1527f7cc78ecSespie 
1528f7cc78ecSespie 	case DW_AT_name:
1529*cf2f2c56Smiod 	  unit->name = attr.u.str;
1530f7cc78ecSespie 	  break;
1531f7cc78ecSespie 
1532f7cc78ecSespie 	case DW_AT_low_pc:
1533*cf2f2c56Smiod 	  unit->arange.low = attr.u.val;
1534f7cc78ecSespie 	  break;
1535f7cc78ecSespie 
1536f7cc78ecSespie 	case DW_AT_high_pc:
1537*cf2f2c56Smiod 	  unit->arange.high = attr.u.val;
1538f7cc78ecSespie 	  break;
1539f7cc78ecSespie 
1540f7cc78ecSespie 	case DW_AT_comp_dir:
1541f7cc78ecSespie 	  {
1542*cf2f2c56Smiod 	    char* comp_dir = attr.u.str;
1543f7cc78ecSespie 	    if (comp_dir)
1544f7cc78ecSespie 	      {
1545f7cc78ecSespie 		/* Irix 6.2 native cc prepends <machine>.: to the compilation
1546f7cc78ecSespie 		   directory, get rid of it.  */
1547*cf2f2c56Smiod 		char *cp = strchr (comp_dir, ':');
1548f7cc78ecSespie 
1549f7cc78ecSespie 		if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1550f7cc78ecSespie 		  comp_dir = cp + 1;
1551f7cc78ecSespie 	      }
1552f7cc78ecSespie 	    unit->comp_dir = comp_dir;
1553f7cc78ecSespie 	    break;
1554f7cc78ecSespie 	  }
1555f7cc78ecSespie 
1556f7cc78ecSespie 	default:
1557f7cc78ecSespie 	  break;
1558f7cc78ecSespie 	}
1559f7cc78ecSespie     }
1560f7cc78ecSespie 
1561f7cc78ecSespie   unit->first_child_die_ptr = info_ptr;
1562f7cc78ecSespie   return unit;
1563f7cc78ecSespie }
1564f7cc78ecSespie 
1565d2201f2fSdrahn /* Return TRUE if UNIT contains the address given by ADDR.  */
1566f7cc78ecSespie 
1567d2201f2fSdrahn static bfd_boolean
comp_unit_contains_address(struct comp_unit * unit,bfd_vma addr)1568*cf2f2c56Smiod comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1569f7cc78ecSespie {
1570f7cc78ecSespie   struct arange *arange;
1571f7cc78ecSespie 
1572f7cc78ecSespie   if (unit->error)
1573d2201f2fSdrahn     return FALSE;
1574f7cc78ecSespie 
1575f7cc78ecSespie   arange = &unit->arange;
1576f7cc78ecSespie   do
1577f7cc78ecSespie     {
1578f7cc78ecSespie       if (addr >= arange->low && addr < arange->high)
1579d2201f2fSdrahn 	return TRUE;
1580f7cc78ecSespie       arange = arange->next;
1581f7cc78ecSespie     }
1582f7cc78ecSespie   while (arange);
15835f210c2aSfgsch 
1584d2201f2fSdrahn   return FALSE;
1585f7cc78ecSespie }
1586f7cc78ecSespie 
1587f7cc78ecSespie /* If UNIT contains ADDR, set the output parameters to the values for
1588f7cc78ecSespie    the line containing ADDR.  The output parameters, FILENAME_PTR,
1589f7cc78ecSespie    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1590f7cc78ecSespie    to be filled in.
1591f7cc78ecSespie 
1592d2201f2fSdrahn    Return TRUE if UNIT contains ADDR, and no errors were encountered;
1593d2201f2fSdrahn    FALSE otherwise.  */
1594f7cc78ecSespie 
1595d2201f2fSdrahn static bfd_boolean
comp_unit_find_nearest_line(struct comp_unit * unit,bfd_vma addr,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,struct dwarf2_debug * stash)1596*cf2f2c56Smiod comp_unit_find_nearest_line (struct comp_unit *unit,
1597*cf2f2c56Smiod 			     bfd_vma addr,
1598*cf2f2c56Smiod 			     const char **filename_ptr,
1599*cf2f2c56Smiod 			     const char **functionname_ptr,
1600*cf2f2c56Smiod 			     unsigned int *linenumber_ptr,
1601*cf2f2c56Smiod 			     struct dwarf2_debug *stash)
1602f7cc78ecSespie {
1603d2201f2fSdrahn   bfd_boolean line_p;
1604d2201f2fSdrahn   bfd_boolean func_p;
1605d2201f2fSdrahn   struct funcinfo *function;
1606f7cc78ecSespie 
1607f7cc78ecSespie   if (unit->error)
1608d2201f2fSdrahn     return FALSE;
1609f7cc78ecSespie 
1610f7cc78ecSespie   if (! unit->line_table)
1611f7cc78ecSespie     {
1612f7cc78ecSespie       if (! unit->stmtlist)
1613f7cc78ecSespie 	{
1614f7cc78ecSespie 	  unit->error = 1;
1615d2201f2fSdrahn 	  return FALSE;
1616f7cc78ecSespie 	}
1617f7cc78ecSespie 
16185f210c2aSfgsch       unit->line_table = decode_line_info (unit, stash);
1619f7cc78ecSespie 
1620f7cc78ecSespie       if (! unit->line_table)
1621f7cc78ecSespie 	{
1622f7cc78ecSespie 	  unit->error = 1;
1623d2201f2fSdrahn 	  return FALSE;
1624f7cc78ecSespie 	}
1625f7cc78ecSespie 
1626d2201f2fSdrahn       if (unit->first_child_die_ptr < unit->end_ptr
1627d2201f2fSdrahn 	  && ! scan_unit_for_functions (unit))
1628f7cc78ecSespie 	{
1629f7cc78ecSespie 	  unit->error = 1;
1630d2201f2fSdrahn 	  return FALSE;
1631f7cc78ecSespie 	}
1632f7cc78ecSespie     }
1633f7cc78ecSespie 
1634d2201f2fSdrahn   function = NULL;
1635d2201f2fSdrahn   func_p = lookup_address_in_function_table (unit->function_table, addr,
1636d2201f2fSdrahn 					     &function, functionname_ptr);
1637d2201f2fSdrahn   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1638d2201f2fSdrahn 					      function, filename_ptr,
1639f7cc78ecSespie 					      linenumber_ptr);
1640f7cc78ecSespie   return line_p || func_p;
1641f7cc78ecSespie }
1642f7cc78ecSespie 
1643d2201f2fSdrahn /* Locate a section in a BFD containing debugging info.  The search starts
1644d2201f2fSdrahn    from the section after AFTER_SEC, or from the first section in the BFD if
1645d2201f2fSdrahn    AFTER_SEC is NULL.  The search works by examining the names of the
1646d2201f2fSdrahn    sections.  There are two permissiable names.  The first is .debug_info.
1647d2201f2fSdrahn    This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
1648d2201f2fSdrahn    This is a variation on the .debug_info section which has a checksum
1649d2201f2fSdrahn    describing the contents appended onto the name.  This allows the linker to
1650d2201f2fSdrahn    identify and discard duplicate debugging sections for different
1651d2201f2fSdrahn    compilation units.  */
16525f210c2aSfgsch #define DWARF2_DEBUG_INFO ".debug_info"
16535f210c2aSfgsch #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
16545f210c2aSfgsch 
16555f210c2aSfgsch static asection *
find_debug_info(bfd * abfd,asection * after_sec)1656*cf2f2c56Smiod find_debug_info (bfd *abfd, asection *after_sec)
16575f210c2aSfgsch {
16585f210c2aSfgsch   asection * msec;
16595f210c2aSfgsch 
16605f210c2aSfgsch   if (after_sec)
16615f210c2aSfgsch     msec = after_sec->next;
16625f210c2aSfgsch   else
16635f210c2aSfgsch     msec = abfd->sections;
16645f210c2aSfgsch 
16655f210c2aSfgsch   while (msec)
16665f210c2aSfgsch     {
16675f210c2aSfgsch       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
16685f210c2aSfgsch 	return msec;
16695f210c2aSfgsch 
16705f210c2aSfgsch       if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
16715f210c2aSfgsch 	return msec;
16725f210c2aSfgsch 
16735f210c2aSfgsch       msec = msec->next;
16745f210c2aSfgsch     }
16755f210c2aSfgsch 
16765f210c2aSfgsch   return NULL;
16775f210c2aSfgsch }
16785f210c2aSfgsch 
1679d2201f2fSdrahn /* The DWARF2 version of find_nearest line.  Return TRUE if the line
1680f7cc78ecSespie    is found without error.  ADDR_SIZE is the number of bytes in the
1681f7cc78ecSespie    initial .debug_info length field and in the abbreviation offset.
1682f7cc78ecSespie    You may use zero to indicate that the default value should be
1683f7cc78ecSespie    used.  */
1684f7cc78ecSespie 
1685d2201f2fSdrahn bfd_boolean
_bfd_dwarf2_find_nearest_line(bfd * abfd,asection * section,asymbol ** symbols,bfd_vma offset,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * linenumber_ptr,unsigned int addr_size,void ** pinfo)1686*cf2f2c56Smiod _bfd_dwarf2_find_nearest_line (bfd *abfd,
1687*cf2f2c56Smiod 			       asection *section,
1688*cf2f2c56Smiod 			       asymbol **symbols,
1689*cf2f2c56Smiod 			       bfd_vma offset,
1690*cf2f2c56Smiod 			       const char **filename_ptr,
1691*cf2f2c56Smiod 			       const char **functionname_ptr,
1692*cf2f2c56Smiod 			       unsigned int *linenumber_ptr,
1693*cf2f2c56Smiod 			       unsigned int addr_size,
1694*cf2f2c56Smiod 			       void **pinfo)
1695f7cc78ecSespie {
1696f7cc78ecSespie   /* Read each compilation unit from the section .debug_info, and check
1697f7cc78ecSespie      to see if it contains the address we are searching for.  If yes,
1698f7cc78ecSespie      lookup the address, and return the line number info.  If no, go
1699f7cc78ecSespie      on to the next compilation unit.
1700f7cc78ecSespie 
1701f7cc78ecSespie      We keep a list of all the previously read compilation units, and
1702f7cc78ecSespie      a pointer to the next un-read compilation unit.  Check the
17035f210c2aSfgsch      previously read units before reading more.  */
1704*cf2f2c56Smiod   struct dwarf2_debug *stash = *pinfo;
1705f7cc78ecSespie 
1706f7cc78ecSespie   /* What address are we looking for?  */
1707f7cc78ecSespie   bfd_vma addr = offset + section->vma;
1708f7cc78ecSespie 
1709f7cc78ecSespie   struct comp_unit* each;
1710f7cc78ecSespie 
1711f7cc78ecSespie   *filename_ptr = NULL;
1712f7cc78ecSespie   *functionname_ptr = NULL;
1713f7cc78ecSespie   *linenumber_ptr = 0;
1714f7cc78ecSespie 
1715f7cc78ecSespie   /* The DWARF2 spec says that the initial length field, and the
1716f7cc78ecSespie      offset of the abbreviation table, should both be 4-byte values.
1717f7cc78ecSespie      However, some compilers do things differently.  */
1718f7cc78ecSespie   if (addr_size == 0)
1719f7cc78ecSespie     addr_size = 4;
1720f7cc78ecSespie   BFD_ASSERT (addr_size == 4 || addr_size == 8);
1721f7cc78ecSespie 
1722f7cc78ecSespie   if (! stash)
1723f7cc78ecSespie     {
1724d2201f2fSdrahn       bfd_size_type total_size;
1725f7cc78ecSespie       asection *msec;
1726d2201f2fSdrahn       bfd_size_type amt = sizeof (struct dwarf2_debug);
1727f7cc78ecSespie 
1728*cf2f2c56Smiod       stash = bfd_zalloc (abfd, amt);
1729f7cc78ecSespie       if (! stash)
1730d2201f2fSdrahn 	return FALSE;
1731f7cc78ecSespie 
1732*cf2f2c56Smiod       *pinfo = stash;
17335f210c2aSfgsch 
17345f210c2aSfgsch       msec = find_debug_info (abfd, NULL);
1735f7cc78ecSespie       if (! msec)
1736f7cc78ecSespie 	/* No dwarf2 info.  Note that at this point the stash
1737f7cc78ecSespie 	   has been allocated, but contains zeros, this lets
1738f7cc78ecSespie 	   future calls to this function fail quicker.  */
1739d2201f2fSdrahn 	 return FALSE;
17405f210c2aSfgsch 
17415f210c2aSfgsch       /* There can be more than one DWARF2 info section in a BFD these days.
17425f210c2aSfgsch 	 Read them all in and produce one large stash.  We do this in two
17435f210c2aSfgsch 	 passes - in the first pass we just accumulate the section sizes.
17445f210c2aSfgsch 	 In the second pass we read in the section's contents.  The allows
17455f210c2aSfgsch 	 us to avoid reallocing the data as we add sections to the stash.  */
17465f210c2aSfgsch       for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
17475f210c2aSfgsch 	total_size += msec->_raw_size;
17485f210c2aSfgsch 
1749*cf2f2c56Smiod       stash->info_ptr = bfd_alloc (abfd, total_size);
17505f210c2aSfgsch       if (stash->info_ptr == NULL)
1751d2201f2fSdrahn 	return FALSE;
17525f210c2aSfgsch 
17535f210c2aSfgsch       stash->info_ptr_end = stash->info_ptr;
17545f210c2aSfgsch 
17555f210c2aSfgsch       for (msec = find_debug_info (abfd, NULL);
17565f210c2aSfgsch 	   msec;
17575f210c2aSfgsch 	   msec = find_debug_info (abfd, msec))
17585f210c2aSfgsch 	{
1759d2201f2fSdrahn 	  bfd_size_type size;
1760d2201f2fSdrahn 	  bfd_size_type start;
1761f7cc78ecSespie 
1762f7cc78ecSespie 	  size = msec->_raw_size;
1763f7cc78ecSespie 	  if (size == 0)
17645f210c2aSfgsch 	    continue;
1765f7cc78ecSespie 
17665f210c2aSfgsch 	  start = stash->info_ptr_end - stash->info_ptr;
1767f7cc78ecSespie 
1768d2201f2fSdrahn 	  if ((bfd_simple_get_relocated_section_contents
1769d2201f2fSdrahn 	       (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
17705f210c2aSfgsch 	    continue;
1771f7cc78ecSespie 
17725f210c2aSfgsch 	  stash->info_ptr_end = stash->info_ptr + start + size;
1773f7cc78ecSespie 	}
1774f7cc78ecSespie 
1775d2201f2fSdrahn       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1776d2201f2fSdrahn 
1777d2201f2fSdrahn       stash->sec = find_debug_info (abfd, NULL);
1778d2201f2fSdrahn       stash->sec_info_ptr = stash->info_ptr;
1779d2201f2fSdrahn       stash->syms = symbols;
17805f210c2aSfgsch     }
1781f7cc78ecSespie 
1782f7cc78ecSespie   /* A null info_ptr indicates that there is no dwarf2 info
1783f7cc78ecSespie      (or that an error occured while setting up the stash).  */
1784f7cc78ecSespie   if (! stash->info_ptr)
1785d2201f2fSdrahn     return FALSE;
1786f7cc78ecSespie 
1787f7cc78ecSespie   /* Check the previously read comp. units first.  */
1788f7cc78ecSespie   for (each = stash->all_comp_units; each; each = each->next_unit)
1789f7cc78ecSespie     if (comp_unit_contains_address (each, addr))
1790f7cc78ecSespie       return comp_unit_find_nearest_line (each, addr, filename_ptr,
17915f210c2aSfgsch 					  functionname_ptr, linenumber_ptr,
17925f210c2aSfgsch 					  stash);
1793f7cc78ecSespie 
1794f7cc78ecSespie   /* Read each remaining comp. units checking each as they are read.  */
1795f7cc78ecSespie   while (stash->info_ptr < stash->info_ptr_end)
1796f7cc78ecSespie     {
1797f7cc78ecSespie       bfd_vma length;
1798d2201f2fSdrahn       bfd_boolean found;
1799d2201f2fSdrahn       unsigned int offset_size = addr_size;
1800f7cc78ecSespie 
1801f7cc78ecSespie       length = read_4_bytes (abfd, stash->info_ptr);
1802d2201f2fSdrahn       /* A 0xffffff length is the DWARF3 way of indicating we use
1803d2201f2fSdrahn 	 64-bit offsets, instead of 32-bit offsets.  */
1804d2201f2fSdrahn       if (length == 0xffffffff)
1805d2201f2fSdrahn 	{
1806d2201f2fSdrahn 	  offset_size = 8;
1807d2201f2fSdrahn 	  length = read_8_bytes (abfd, stash->info_ptr + 4);
1808d2201f2fSdrahn 	  stash->info_ptr += 12;
1809d2201f2fSdrahn 	}
1810d2201f2fSdrahn       /* A zero length is the IRIX way of indicating 64-bit offsets,
1811d2201f2fSdrahn 	 mostly because the 64-bit length will generally fit in 32
1812d2201f2fSdrahn 	 bits, and the endianness helps.  */
1813d2201f2fSdrahn       else if (length == 0)
1814d2201f2fSdrahn 	{
1815d2201f2fSdrahn 	  offset_size = 8;
1816d2201f2fSdrahn 	  length = read_4_bytes (abfd, stash->info_ptr + 4);
1817d2201f2fSdrahn 	  stash->info_ptr += 8;
1818d2201f2fSdrahn 	}
1819d2201f2fSdrahn       /* In the absence of the hints above, we assume addr_size-sized
1820d2201f2fSdrahn 	 offsets, for backward-compatibility with pre-DWARF3 64-bit
1821d2201f2fSdrahn 	 platforms.  */
1822d2201f2fSdrahn       else if (addr_size == 8)
1823d2201f2fSdrahn 	{
1824f7cc78ecSespie 	  length = read_8_bytes (abfd, stash->info_ptr);
1825d2201f2fSdrahn 	  stash->info_ptr += 8;
1826d2201f2fSdrahn 	}
1827d2201f2fSdrahn       else
1828d2201f2fSdrahn 	stash->info_ptr += 4;
1829f7cc78ecSespie 
1830f7cc78ecSespie       if (length > 0)
1831f7cc78ecSespie 	{
1832d2201f2fSdrahn 	  each = parse_comp_unit (abfd, stash, length, offset_size);
1833f7cc78ecSespie 	  stash->info_ptr += length;
1834f7cc78ecSespie 
1835d2201f2fSdrahn 	  if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1836d2201f2fSdrahn 	      == stash->sec->_raw_size)
1837d2201f2fSdrahn 	    {
1838d2201f2fSdrahn 	      stash->sec = find_debug_info (abfd, stash->sec);
1839d2201f2fSdrahn 	      stash->sec_info_ptr = stash->info_ptr;
1840d2201f2fSdrahn 	    }
1841d2201f2fSdrahn 
1842f7cc78ecSespie 	  if (each)
1843f7cc78ecSespie 	    {
1844f7cc78ecSespie 	      each->next_unit = stash->all_comp_units;
1845f7cc78ecSespie 	      stash->all_comp_units = each;
1846f7cc78ecSespie 
1847f7cc78ecSespie 	      /* DW_AT_low_pc and DW_AT_high_pc are optional for
1848f7cc78ecSespie 		 compilation units.  If we don't have them (i.e.,
1849f7cc78ecSespie 		 unit->high == 0), we need to consult the line info
1850f7cc78ecSespie 		 table to see if a compilation unit contains the given
1851f7cc78ecSespie 		 address.  */
1852f7cc78ecSespie 	      if (each->arange.high > 0)
1853f7cc78ecSespie 		{
1854f7cc78ecSespie 		  if (comp_unit_contains_address (each, addr))
1855f7cc78ecSespie 		    return comp_unit_find_nearest_line (each, addr,
1856f7cc78ecSespie 							filename_ptr,
1857f7cc78ecSespie 							functionname_ptr,
18585f210c2aSfgsch 							linenumber_ptr,
18595f210c2aSfgsch 							stash);
1860f7cc78ecSespie 		}
1861f7cc78ecSespie 	      else
1862f7cc78ecSespie 		{
1863f7cc78ecSespie 		  found = comp_unit_find_nearest_line (each, addr,
1864f7cc78ecSespie 						       filename_ptr,
1865f7cc78ecSespie 						       functionname_ptr,
18665f210c2aSfgsch 						       linenumber_ptr,
18675f210c2aSfgsch 						       stash);
1868f7cc78ecSespie 		  if (found)
1869d2201f2fSdrahn 		    return TRUE;
1870f7cc78ecSespie 		}
1871f7cc78ecSespie 	    }
1872f7cc78ecSespie 	}
1873f7cc78ecSespie     }
1874f7cc78ecSespie 
1875d2201f2fSdrahn   return FALSE;
1876f7cc78ecSespie }
1877