/[gcl]/gcl/binutils/bfd/dwarf2.c
ViewVC logotype

Diff of /gcl/binutils/bfd/dwarf2.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by camm, Fri Aug 9 05:35:03 2002 UTC revision 1.1.1.1.20.1 by camm, Fri Sep 30 02:08:56 2005 UTC
# Line 1  Line 1 
1  /* DWARF 2 support.  /* DWARF 2 support.
2     Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002     Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3     Free Software Foundation, Inc.     2004, 2005 Free Software Foundation, Inc.
4    
5     Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions     Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6     (gavin@cygnus.com).     (gavin@cygnus.com).
# Line 13  Line 13 
13     based on Fred Fish's (Cygnus Support) implementation of DWARF 1     based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14     support in dwarfread.c     support in dwarfread.c
15    
16  This file is part of BFD.     This file is part of BFD.
17    
18  This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
19  it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
20  the Free Software Foundation; either version 2 of the License, or (at     the Free Software Foundation; either version 2 of the License, or (at
21  your option) any later version.     your option) any later version.
22    
23  This program is distributed in the hope that it will be useful, but     This program is distributed in the hope that it will be useful, but
24  WITHOUT ANY WARRANTY; without even the implied warranty of     WITHOUT ANY WARRANTY; without even the implied warranty of
25  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  General Public License for more details.     General Public License for more details.
27    
28  You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License
29  along with this program; if not, write to the Free Software     along with this program; if not, write to the Free Software
30  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
31    
32  #include "bfd.h"  #include "bfd.h"
33  #include "sysdep.h"  #include "sysdep.h"
# Line 61  struct attribute Line 61  struct attribute
61    {    {
62      char *str;      char *str;
63      struct dwarf_block *blk;      struct dwarf_block *blk;
64      unsigned int unsnd;      bfd_uint64_t val;
65      int snd;      bfd_int64_t sval;
     bfd_vma addr;  
66    }    }
67    u;    u;
68  };  };
69    
 /* Get at parts of an attribute structure.  */  
   
 #define DW_STRING(attr)    ((attr)->u.str)  
 #define DW_UNSND(attr)     ((attr)->u.unsnd)  
 #define DW_BLOCK(attr)     ((attr)->u.blk)  
 #define DW_SND(attr)       ((attr)->u.snd)  
 #define DW_ADDR(attr)      ((attr)->u.addr)  
   
70  /* Blocks are a bunch of untyped bytes.  */  /* Blocks are a bunch of untyped bytes.  */
71  struct dwarf_block  struct dwarf_block
72  {  {
73    unsigned int size;    unsigned int size;
74    char *data;    bfd_byte *data;
75  };  };
76    
77  struct dwarf2_debug  struct dwarf2_debug
78  {  {
79    /* A list of all previously read comp_units.  */    /* A list of all previously read comp_units.  */
80    struct comp_unit* all_comp_units;    struct comp_unit *all_comp_units;
81    
82    /* The next unread compilation unit within the .debug_info section.    /* The next unread compilation unit within the .debug_info section.
83       Zero indicates that the .debug_info section has not been loaded       Zero indicates that the .debug_info section has not been loaded
84       into a buffer yet.  */       into a buffer yet.  */
85    char* info_ptr;    bfd_byte *info_ptr;
86    
87    /* Pointer to the end of the .debug_info section memory buffer.  */    /* Pointer to the end of the .debug_info section memory buffer.  */
88    char* info_ptr_end;    bfd_byte *info_ptr_end;
89    
90    /* Pointer to the section and address of the beginning of the    /* Pointer to the section and address of the beginning of the
91       section.  */       section.  */
92    asection* sec;    asection *sec;
93    char* sec_info_ptr;    bfd_byte *sec_info_ptr;
94    
95    /* Pointer to the symbol table.  */    /* Pointer to the symbol table.  */
96    asymbol** syms;    asymbol **syms;
97    
98    /* Pointer to the .debug_abbrev section loaded into memory.  */    /* Pointer to the .debug_abbrev section loaded into memory.  */
99    char* dwarf_abbrev_buffer;    bfd_byte *dwarf_abbrev_buffer;
100    
101    /* Length of the loaded .debug_abbrev section.  */    /* Length of the loaded .debug_abbrev section.  */
102    unsigned long dwarf_abbrev_size;    unsigned long dwarf_abbrev_size;
103    
104    /* Buffer for decode_line_info.  */    /* Buffer for decode_line_info.  */
105    char *dwarf_line_buffer;    bfd_byte *dwarf_line_buffer;
106    
107    /* Length of the loaded .debug_line section.  */    /* Length of the loaded .debug_line section.  */
108    unsigned long dwarf_line_size;    unsigned long dwarf_line_size;
109    
110    /* Pointer to the .debug_str section loaded into memory.  */    /* Pointer to the .debug_str section loaded into memory.  */
111    char* dwarf_str_buffer;    bfd_byte *dwarf_str_buffer;
112    
113    /* Length of the loaded .debug_str section.  */    /* Length of the loaded .debug_str section.  */
114    unsigned long dwarf_str_size;    unsigned long dwarf_str_size;
# Line 136  struct arange Line 127  struct arange
127  struct comp_unit  struct comp_unit
128  {  {
129    /* Chain the previously read compilation units.  */    /* Chain the previously read compilation units.  */
130    struct comp_unit* next_unit;    struct comp_unit *next_unit;
131    
132    /* Keep the bdf convenient (for memory allocation).  */    /* Keep the bdf convenient (for memory allocation).  */
133    bfd* abfd;    bfd *abfd;
134    
135    /* The lowest and higest addresses contained in this compilation    /* The lowest and higest addresses contained in this compilation
136       unit as specified in the compilation unit header.  */       unit as specified in the compilation unit header.  */
137    struct arange arange;    struct arange arange;
138    
139    /* The DW_AT_name attribute (for error messages).  */    /* The DW_AT_name attribute (for error messages).  */
140    char* name;    char *name;
141    
142    /* The abbrev hash table.  */    /* The abbrev hash table.  */
143    struct abbrev_info** abbrevs;    struct abbrev_info **abbrevs;
144    
145    /* Note that an error was found by comp_unit_find_nearest_line.  */    /* Note that an error was found by comp_unit_find_nearest_line.  */
146    int error;    int error;
147    
148    /* The DW_AT_comp_dir attribute.  */    /* The DW_AT_comp_dir attribute.  */
149    char* comp_dir;    char *comp_dir;
150    
151    /* True if there is a line number table associated with this comp. unit.  */    /* TRUE if there is a line number table associated with this comp. unit.  */
152    int stmtlist;    int stmtlist;
153    
154      /* Pointer to the current comp_unit so that we can find a given entry
155         by its reference.  */
156      bfd_byte *info_ptr_unit;
157    
158    /* The offset into .debug_line of the line number table.  */    /* The offset into .debug_line of the line number table.  */
159    unsigned long line_offset;    unsigned long line_offset;
160    
161    /* Pointer to the first child die for the comp unit.  */    /* Pointer to the first child die for the comp unit.  */
162    char *first_child_die_ptr;    bfd_byte *first_child_die_ptr;
163    
164    /* The end of the comp unit.  */    /* The end of the comp unit.  */
165    char *end_ptr;    bfd_byte *end_ptr;
166    
167    /* The decoded line number, NULL if not yet decoded.  */    /* The decoded line number, NULL if not yet decoded.  */
168    struct line_info_table* line_table;    struct line_info_table *line_table;
169    
170    /* A list of the functions found in this comp. unit.  */    /* A list of the functions found in this comp. unit.  */
171    struct funcinfo* function_table;    struct funcinfo *function_table;
172    
173    /* Pointer to dwarf2_debug structure.  */    /* Pointer to dwarf2_debug structure.  */
174    struct dwarf2_debug *stash;    struct dwarf2_debug *stash;
# Line 209  struct attr_abbrev Line 204  struct attr_abbrev
204  #define ATTR_ALLOC_CHUNK 4  #define ATTR_ALLOC_CHUNK 4
205  #endif  #endif
206    
 static unsigned int read_1_byte PARAMS ((bfd *, char *));  
 static int read_1_signed_byte PARAMS ((bfd *, char *));  
 static unsigned int read_2_bytes PARAMS ((bfd *, char *));  
 static unsigned int read_4_bytes PARAMS ((bfd *, char *));  
 static bfd_vma read_8_bytes PARAMS ((bfd *, char *));  
 static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int));  
 static char *read_string PARAMS ((bfd *, char *, unsigned int *));  
 static char *read_indirect_string PARAMS ((struct comp_unit *, char *, unsigned int *));  
 static unsigned int read_unsigned_leb128  
   PARAMS ((bfd *, char *, unsigned int *));  
 static int read_signed_leb128  
   PARAMS ((bfd *, char *, unsigned int *));  
 static bfd_vma read_address PARAMS ((struct comp_unit *, char *));  
 static struct abbrev_info *lookup_abbrev  
   PARAMS ((unsigned int, struct abbrev_info **));  
 static struct abbrev_info **read_abbrevs  
   PARAMS ((bfd *, bfd_vma, struct dwarf2_debug *));  
 static char *read_attribute  
   PARAMS ((struct attribute *, struct attr_abbrev *,  
            struct comp_unit *, char *));  
 static char *read_attribute_value  
   PARAMS ((struct attribute *, unsigned,  
            struct comp_unit *, char *));  
 static void add_line_info  
   PARAMS ((struct line_info_table *, bfd_vma, char *,  
            unsigned int, unsigned int, int));  
 static char *concat_filename PARAMS ((struct line_info_table *, unsigned int));  
 static void arange_add PARAMS ((struct comp_unit *, bfd_vma, bfd_vma));  
 static struct line_info_table *decode_line_info  
   PARAMS ((struct comp_unit *, struct dwarf2_debug *));  
 static boolean lookup_address_in_line_info_table  
   PARAMS ((struct line_info_table *, bfd_vma, struct funcinfo *,  
            const char **, unsigned int *));  
 static boolean lookup_address_in_function_table  
   PARAMS ((struct funcinfo *, bfd_vma, struct funcinfo **, const char **));  
 static boolean scan_unit_for_functions PARAMS ((struct comp_unit *));  
 static bfd_vma find_rela_addend  
   PARAMS ((bfd *, asection *, bfd_size_type, asymbol**));  
 static struct comp_unit *parse_comp_unit  
   PARAMS ((bfd *, struct dwarf2_debug *, bfd_vma, unsigned int));  
 static boolean comp_unit_contains_address  
   PARAMS ((struct comp_unit *, bfd_vma));  
 static boolean comp_unit_find_nearest_line  
   PARAMS ((struct comp_unit *, bfd_vma, const char **, const char **,  
            unsigned int *, struct dwarf2_debug *));  
 static asection *find_debug_info PARAMS ((bfd *, asection *));  
   
207  /* VERBATIM  /* VERBATIM
208     The following function up to the END VERBATIM mark are     The following function up to the END VERBATIM mark are
209     copied directly from dwarf2read.c.  */     copied directly from dwarf2read.c.  */
# Line 263  static asection *find_debug_info PARAMS Line 211  static asection *find_debug_info PARAMS
211  /* Read dwarf information from a buffer.  */  /* Read dwarf information from a buffer.  */
212    
213  static unsigned int  static unsigned int
214  read_1_byte (abfd, buf)  read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
      bfd *abfd ATTRIBUTE_UNUSED;  
      char *buf;  
215  {  {
216    return bfd_get_8 (abfd, (bfd_byte *) buf);    return bfd_get_8 (abfd, buf);
217  }  }
218    
219  static int  static int
220  read_1_signed_byte (abfd, buf)  read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
      bfd *abfd ATTRIBUTE_UNUSED;  
      char *buf;  
221  {  {
222    return bfd_get_signed_8 (abfd, (bfd_byte *) buf);    return bfd_get_signed_8 (abfd, buf);
223  }  }
224    
225  static unsigned int  static unsigned int
226  read_2_bytes (abfd, buf)  read_2_bytes (bfd *abfd, bfd_byte *buf)
      bfd *abfd;  
      char *buf;  
 {  
   return bfd_get_16 (abfd, (bfd_byte *) buf);  
 }  
   
 #if 0  /* This is not used.  */  
   
 static int  
 read_2_signed_bytes (abfd, buf)  
      bfd *abfd;  
      char *buf;  
227  {  {
228    return bfd_get_signed_16 (abfd, (bfd_byte *) buf);    return bfd_get_16 (abfd, buf);
229  }  }
230    
 #endif  
   
231  static unsigned int  static unsigned int
232  read_4_bytes (abfd, buf)  read_4_bytes (bfd *abfd, bfd_byte *buf)
      bfd *abfd;  
      char *buf;  
 {  
   return bfd_get_32 (abfd, (bfd_byte *) buf);  
 }  
   
 #if 0  /* This is not used.  */  
   
 static int  
 read_4_signed_bytes (abfd, buf)  
      bfd *abfd;  
      char *buf;  
233  {  {
234    return bfd_get_signed_32 (abfd, (bfd_byte *) buf);    return bfd_get_32 (abfd, buf);
235  }  }
236    
237  #endif  static bfd_uint64_t
238    read_8_bytes (bfd *abfd, bfd_byte *buf)
 static bfd_vma  
 read_8_bytes (abfd, buf)  
      bfd *abfd;  
      char *buf;  
239  {  {
240    return bfd_get_64 (abfd, (bfd_byte *) buf);    return bfd_get_64 (abfd, buf);
241  }  }
242    
243  static char *  static bfd_byte *
244  read_n_bytes (abfd, buf, size)  read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
245       bfd *abfd ATTRIBUTE_UNUSED;                bfd_byte *buf,
246       char *buf;                unsigned int size ATTRIBUTE_UNUSED)
      unsigned int size ATTRIBUTE_UNUSED;  
247  {  {
248    /* If the size of a host char is 8 bits, we can return a pointer    /* If the size of a host char is 8 bits, we can return a pointer
249       to the buffer, otherwise we have to copy the data to a buffer       to the buffer, otherwise we have to copy the data to a buffer
# Line 339  read_n_bytes (abfd, buf, size) Line 252  read_n_bytes (abfd, buf, size)
252  }  }
253    
254  static char *  static char *
255  read_string (abfd, buf, bytes_read_ptr)  read_string (bfd *abfd ATTRIBUTE_UNUSED,
256       bfd *abfd ATTRIBUTE_UNUSED;               bfd_byte *buf,
257       char *buf;               unsigned int *bytes_read_ptr)
      unsigned int *bytes_read_ptr;  
258  {  {
259    /* Return a pointer to the embedded string.  */    /* Return a pointer to the embedded string.  */
260    if (*buf == '\0')    char *str = (char *) buf;
261      if (*str == '\0')
262      {      {
263        *bytes_read_ptr = 1;        *bytes_read_ptr = 1;
264        return NULL;        return NULL;
265      }      }
266    
267    *bytes_read_ptr = strlen (buf) + 1;    *bytes_read_ptr = strlen (str) + 1;
268    return buf;    return str;
269  }  }
270    
271  static char *  static char *
272  read_indirect_string (unit, buf, bytes_read_ptr)  read_indirect_string (struct comp_unit* unit,
273       struct comp_unit* unit;                        bfd_byte *buf,
274       char *buf;                        unsigned int *bytes_read_ptr)
      unsigned int *bytes_read_ptr;  
275  {  {
276    bfd_vma offset;    bfd_uint64_t offset;
277    struct dwarf2_debug *stash = unit->stash;    struct dwarf2_debug *stash = unit->stash;
278      char *str;
279    
280    if (unit->offset_size == 4)    if (unit->offset_size == 4)
281      offset = read_4_bytes (unit->abfd, buf);      offset = read_4_bytes (unit->abfd, buf);
# Line 374  read_indirect_string (unit, buf, bytes_r Line 287  read_indirect_string (unit, buf, bytes_r
287      {      {
288        asection *msec;        asection *msec;
289        bfd *abfd = unit->abfd;        bfd *abfd = unit->abfd;
290          bfd_size_type sz;
291    
292        msec = bfd_get_section_by_name (abfd, ".debug_str");        msec = bfd_get_section_by_name (abfd, ".debug_str");
293        if (! msec)        if (! msec)
# Line 384  read_indirect_string (unit, buf, bytes_r Line 298  read_indirect_string (unit, buf, bytes_r
298            return NULL;            return NULL;
299          }          }
300    
301        stash->dwarf_str_size = msec->_raw_size;        sz = msec->rawsize ? msec->rawsize : msec->size;
302        stash->dwarf_str_buffer = (char*) bfd_alloc (abfd, msec->_raw_size);        stash->dwarf_str_size = sz;
303          stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
304        if (! stash->dwarf_abbrev_buffer)        if (! stash->dwarf_abbrev_buffer)
305          return NULL;          return NULL;
306    
307        if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,        if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
308                                        (bfd_vma) 0, msec->_raw_size))                                        0, sz))
309          return NULL;          return NULL;
310      }      }
311    
# Line 402  read_indirect_string (unit, buf, bytes_r Line 317  read_indirect_string (unit, buf, bytes_r
317        return NULL;        return NULL;
318      }      }
319    
320    buf = stash->dwarf_str_buffer + offset;      str = (char *) stash->dwarf_str_buffer + offset;
321    if (*buf == '\0')    if (*str == '\0')
322      return NULL;      return NULL;
323    return buf;    return str;
 }  
   
 static unsigned int  
 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)  
      bfd *abfd ATTRIBUTE_UNUSED;  
      char *buf;  
      unsigned int *bytes_read_ptr;  
 {  
   unsigned int  result;  
   unsigned int  num_read;  
   int           shift;  
   unsigned char byte;  
   
   result   = 0;  
   shift    = 0;  
   num_read = 0;  
   
   do  
     {  
       byte = bfd_get_8 (abfd, (bfd_byte *) buf);  
       buf ++;  
       num_read ++;  
       result |= ((byte & 0x7f) << shift);  
       shift += 7;  
     }  
   while (byte & 0x80);  
   
   * bytes_read_ptr = num_read;  
   
   return result;  
 }  
   
 static int  
 read_signed_leb128 (abfd, buf, bytes_read_ptr)  
      bfd *abfd ATTRIBUTE_UNUSED;  
      char *buf;  
      unsigned int * bytes_read_ptr;  
 {  
   int           result;  
   int           shift;  
   int           num_read;  
   unsigned char byte;  
   
   result = 0;  
   shift = 0;  
   num_read = 0;  
   
   do  
     {  
       byte = bfd_get_8 (abfd, (bfd_byte *) buf);  
       buf ++;  
       num_read ++;  
       result |= ((byte & 0x7f) << shift);  
       shift += 7;  
     }  
   while (byte & 0x80);  
   
   if ((shift < 32) && (byte & 0x40))  
     result |= -(1 << shift);  
   
   * bytes_read_ptr = num_read;  
   
   return result;  
324  }  }
325    
326  /* END VERBATIM */  /* END VERBATIM */
327    
328  static bfd_vma  static bfd_uint64_t
329  read_address (unit, buf)  read_address (struct comp_unit *unit, bfd_byte *buf)
      struct comp_unit* unit;  
      char *buf;  
330  {  {
331    switch (unit->addr_size)    switch (unit->addr_size)
332      {      {
333      case 8:      case 8:
334        return bfd_get_64 (unit->abfd, (bfd_byte *) buf);        return bfd_get_64 (unit->abfd, buf);
335      case 4:      case 4:
336        return bfd_get_32 (unit->abfd, (bfd_byte *) buf);        return bfd_get_32 (unit->abfd, buf);
337      case 2:      case 2:
338        return bfd_get_16 (unit->abfd, (bfd_byte *) buf);        return bfd_get_16 (unit->abfd, buf);
339      default:      default:
340        abort ();        abort ();
341      }      }
# Line 494  read_address (unit, buf) Line 344  read_address (unit, buf)
344  /* Lookup an abbrev_info structure in the abbrev hash table.  */  /* Lookup an abbrev_info structure in the abbrev hash table.  */
345    
346  static struct abbrev_info *  static struct abbrev_info *
347  lookup_abbrev (number,abbrevs)  lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
      unsigned int number;  
      struct abbrev_info **abbrevs;  
348  {  {
349    unsigned int hash_number;    unsigned int hash_number;
350    struct abbrev_info *abbrev;    struct abbrev_info *abbrev;
# Line 521  lookup_abbrev (number,abbrevs) Line 369  lookup_abbrev (number,abbrevs)
369     in a hash table.  */     in a hash table.  */
370    
371  static struct abbrev_info**  static struct abbrev_info**
372  read_abbrevs (abfd, offset, stash)  read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
      bfd * abfd;  
      bfd_vma offset;  
      struct dwarf2_debug *stash;  
373  {  {
374    struct abbrev_info **abbrevs;    struct abbrev_info **abbrevs;
375    char *abbrev_ptr;    bfd_byte *abbrev_ptr;
376    struct abbrev_info *cur_abbrev;    struct abbrev_info *cur_abbrev;
377    unsigned int abbrev_number, bytes_read, abbrev_name;    unsigned int abbrev_number, bytes_read, abbrev_name;
378    unsigned int abbrev_form, hash_number;    unsigned int abbrev_form, hash_number;
# Line 545  read_abbrevs (abfd, offset, stash) Line 390  read_abbrevs (abfd, offset, stash)
390            return 0;            return 0;
391          }          }
392    
393        stash->dwarf_abbrev_size = msec->_raw_size;        stash->dwarf_abbrev_size = msec->size;
394        stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, msec->_raw_size);        stash->dwarf_abbrev_buffer
395            = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
396                                                         stash->syms);
397        if (! stash->dwarf_abbrev_buffer)        if (! stash->dwarf_abbrev_buffer)
398            return 0;            return 0;
   
       if (! bfd_get_section_contents (abfd, msec, stash->dwarf_abbrev_buffer,  
                                       (bfd_vma) 0, msec->_raw_size))  
         return 0;  
399      }      }
400    
401    if (offset >= stash->dwarf_abbrev_size)    if (offset >= stash->dwarf_abbrev_size)
# Line 564  read_abbrevs (abfd, offset, stash) Line 407  read_abbrevs (abfd, offset, stash)
407      }      }
408    
409    amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;    amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
410    abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, amt);    abbrevs = bfd_zalloc (abfd, amt);
411    
412    abbrev_ptr = stash->dwarf_abbrev_buffer + offset;    abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
413    abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);    abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
# Line 574  read_abbrevs (abfd, offset, stash) Line 417  read_abbrevs (abfd, offset, stash)
417    while (abbrev_number)    while (abbrev_number)
418      {      {
419        amt = sizeof (struct abbrev_info);        amt = sizeof (struct abbrev_info);
420        cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);        cur_abbrev = bfd_zalloc (abfd, amt);
421    
422        /* Read in abbrev header.  */        /* Read in abbrev header.  */
423        cur_abbrev->number = abbrev_number;        cur_abbrev->number = abbrev_number;
424        cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);        cur_abbrev->tag = (enum dwarf_tag)
425            read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
426        abbrev_ptr += bytes_read;        abbrev_ptr += bytes_read;
427        cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);        cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
428        abbrev_ptr += 1;        abbrev_ptr += 1;
# Line 595  read_abbrevs (abfd, offset, stash) Line 439  read_abbrevs (abfd, offset, stash)
439              {              {
440                amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;                amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
441                amt *= sizeof (struct attr_abbrev);                amt *= sizeof (struct attr_abbrev);
442                cur_abbrev->attrs = ((struct attr_abbrev *)                cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
                                    bfd_realloc (cur_abbrev->attrs, amt));  
443                if (! cur_abbrev->attrs)                if (! cur_abbrev->attrs)
444                  return 0;                  return 0;
445              }              }
446    
447            cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;            cur_abbrev->attrs[cur_abbrev->num_attrs].name
448            cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;              = (enum dwarf_attribute) abbrev_name;
449              cur_abbrev->attrs[cur_abbrev->num_attrs++].form
450                = (enum dwarf_form) abbrev_form;
451            abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);            abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
452            abbrev_ptr += bytes_read;            abbrev_ptr += bytes_read;
453            abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);            abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
# Line 614  read_abbrevs (abfd, offset, stash) Line 459  read_abbrevs (abfd, offset, stash)
459        abbrevs[hash_number] = cur_abbrev;        abbrevs[hash_number] = cur_abbrev;
460    
461        /* Get next abbreviation.        /* Get next abbreviation.
462           Under Irix6 the abbreviations for a compilation unit are not           Under Irix6 the abbreviations for a compilation unit are not
463           always properly terminated with an abbrev number of 0.           always properly terminated with an abbrev number of 0.
464           Exit loop if we encounter an abbreviation which we have           Exit loop if we encounter an abbreviation which we have
465           already read (which means we are about to read the abbreviations           already read (which means we are about to read the abbreviations
# Line 634  read_abbrevs (abfd, offset, stash) Line 479  read_abbrevs (abfd, offset, stash)
479    
480  /* Read an attribute value described by an attribute form.  */  /* Read an attribute value described by an attribute form.  */
481    
482  static char *  static bfd_byte *
483  read_attribute_value (attr, form, unit, info_ptr)  read_attribute_value (struct attribute *attr,
484       struct attribute   *attr;                        unsigned form,
485       unsigned form;                        struct comp_unit *unit,
486       struct comp_unit   *unit;                        bfd_byte *info_ptr)
      char               *info_ptr;  
487  {  {
488    bfd *abfd = unit->abfd;    bfd *abfd = unit->abfd;
489    unsigned int bytes_read;    unsigned int bytes_read;
490    struct dwarf_block *blk;    struct dwarf_block *blk;
491    bfd_size_type amt;    bfd_size_type amt;
492    
493    attr->form = form;    attr->form = (enum dwarf_form) form;
494    
495    switch (form)    switch (form)
496      {      {
497      case DW_FORM_addr:      case DW_FORM_addr:
498        /* FIXME: DWARF3 draft sais DW_FORM_ref_addr is offset_size.  */        /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size.  */
499      case DW_FORM_ref_addr:      case DW_FORM_ref_addr:
500        DW_ADDR (attr) = read_address (unit, info_ptr);        attr->u.val = read_address (unit, info_ptr);
501        info_ptr += unit->addr_size;        info_ptr += unit->addr_size;
502        break;        break;
503      case DW_FORM_block2:      case DW_FORM_block2:
504        amt = sizeof (struct dwarf_block);        amt = sizeof (struct dwarf_block);
505        blk = (struct dwarf_block *) bfd_alloc (abfd, amt);        blk = bfd_alloc (abfd, amt);
506        blk->size = read_2_bytes (abfd, info_ptr);        blk->size = read_2_bytes (abfd, info_ptr);
507        info_ptr += 2;        info_ptr += 2;
508        blk->data = read_n_bytes (abfd, info_ptr, blk->size);        blk->data = read_n_bytes (abfd, info_ptr, blk->size);
509        info_ptr += blk->size;        info_ptr += blk->size;
510        DW_BLOCK (attr) = blk;        attr->u.blk = blk;
511        break;        break;
512      case DW_FORM_block4:      case DW_FORM_block4:
513        amt = sizeof (struct dwarf_block);        amt = sizeof (struct dwarf_block);
514        blk = (struct dwarf_block *) bfd_alloc (abfd, amt);        blk = bfd_alloc (abfd, amt);
515        blk->size = read_4_bytes (abfd, info_ptr);        blk->size = read_4_bytes (abfd, info_ptr);
516        info_ptr += 4;        info_ptr += 4;
517        blk->data = read_n_bytes (abfd, info_ptr, blk->size);        blk->data = read_n_bytes (abfd, info_ptr, blk->size);
518        info_ptr += blk->size;        info_ptr += blk->size;
519        DW_BLOCK (attr) = blk;        attr->u.blk = blk;
520        break;        break;
521      case DW_FORM_data2:      case DW_FORM_data2:
522        DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);        attr->u.val = read_2_bytes (abfd, info_ptr);
523        info_ptr += 2;        info_ptr += 2;
524        break;        break;
525      case DW_FORM_data4:      case DW_FORM_data4:
526        DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);        attr->u.val = read_4_bytes (abfd, info_ptr);
527        info_ptr += 4;        info_ptr += 4;
528        break;        break;
529      case DW_FORM_data8:      case DW_FORM_data8:
530        DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);        attr->u.val = read_8_bytes (abfd, info_ptr);
531        info_ptr += 8;        info_ptr += 8;
532        break;        break;
533      case DW_FORM_string:      case DW_FORM_string:
534        DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);        attr->u.str = read_string (abfd, info_ptr, &bytes_read);
535        info_ptr += bytes_read;        info_ptr += bytes_read;
536        break;        break;
537      case DW_FORM_strp:      case DW_FORM_strp:
538        DW_STRING (attr) = read_indirect_string (unit, info_ptr, &bytes_read);        attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
539        info_ptr += bytes_read;        info_ptr += bytes_read;
540        break;        break;
541      case DW_FORM_block:      case DW_FORM_block:
542        amt = sizeof (struct dwarf_block);        amt = sizeof (struct dwarf_block);
543        blk = (struct dwarf_block *) bfd_alloc (abfd, amt);        blk = bfd_alloc (abfd, amt);
544        blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);        blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
545        info_ptr += bytes_read;        info_ptr += bytes_read;
546        blk->data = read_n_bytes (abfd, info_ptr, blk->size);        blk->data = read_n_bytes (abfd, info_ptr, blk->size);
547        info_ptr += blk->size;        info_ptr += blk->size;
548        DW_BLOCK (attr) = blk;        attr->u.blk = blk;
549        break;        break;
550      case DW_FORM_block1:      case DW_FORM_block1:
551        amt = sizeof (struct dwarf_block);        amt = sizeof (struct dwarf_block);
552        blk = (struct dwarf_block *) bfd_alloc (abfd, amt);        blk = bfd_alloc (abfd, amt);
553        blk->size = read_1_byte (abfd, info_ptr);        blk->size = read_1_byte (abfd, info_ptr);
554        info_ptr += 1;        info_ptr += 1;
555        blk->data = read_n_bytes (abfd, info_ptr, blk->size);        blk->data = read_n_bytes (abfd, info_ptr, blk->size);
556        info_ptr += blk->size;        info_ptr += blk->size;
557        DW_BLOCK (attr) = blk;        attr->u.blk = blk;
558        break;        break;
559      case DW_FORM_data1:      case DW_FORM_data1:
560        DW_UNSND (attr) = read_1_byte (abfd, info_ptr);        attr->u.val = read_1_byte (abfd, info_ptr);
561        info_ptr += 1;        info_ptr += 1;
562        break;        break;
563      case DW_FORM_flag:      case DW_FORM_flag:
564        DW_UNSND (attr) = read_1_byte (abfd, info_ptr);        attr->u.val = read_1_byte (abfd, info_ptr);
565        info_ptr += 1;        info_ptr += 1;
566        break;        break;
567      case DW_FORM_sdata:      case DW_FORM_sdata:
568        DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);        attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
569        info_ptr += bytes_read;        info_ptr += bytes_read;
570        break;        break;
571      case DW_FORM_udata:      case DW_FORM_udata:
572        DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);        attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
573        info_ptr += bytes_read;        info_ptr += bytes_read;
574        break;        break;
575      case DW_FORM_ref1:      case DW_FORM_ref1:
576        DW_UNSND (attr) = read_1_byte (abfd, info_ptr);        attr->u.val = read_1_byte (abfd, info_ptr);
577        info_ptr += 1;        info_ptr += 1;
578        break;        break;
579      case DW_FORM_ref2:      case DW_FORM_ref2:
580        DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);        attr->u.val = read_2_bytes (abfd, info_ptr);
581        info_ptr += 2;        info_ptr += 2;
582        break;        break;
583      case DW_FORM_ref4:      case DW_FORM_ref4:
584        DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);        attr->u.val = read_4_bytes (abfd, info_ptr);
585        info_ptr += 4;        info_ptr += 4;
586        break;        break;
587      case DW_FORM_ref8:      case DW_FORM_ref8:
588        DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);        attr->u.val = read_8_bytes (abfd, info_ptr);
589        info_ptr += 8;        info_ptr += 8;
590        break;        break;
591      case DW_FORM_ref_udata:      case DW_FORM_ref_udata:
592        DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);        attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
593        info_ptr += bytes_read;        info_ptr += bytes_read;
594        break;        break;
595      case DW_FORM_indirect:      case DW_FORM_indirect:
# Line 763  read_attribute_value (attr, form, unit, Line 607  read_attribute_value (attr, form, unit,
607    
608  /* Read an attribute described by an abbreviated attribute.  */  /* Read an attribute described by an abbreviated attribute.  */
609    
610  static char *  static bfd_byte *
611  read_attribute (attr, abbrev, unit, info_ptr)  read_attribute (struct attribute *attr,
612       struct attribute   *attr;                  struct attr_abbrev *abbrev,
613       struct attr_abbrev *abbrev;                  struct comp_unit *unit,
614       struct comp_unit   *unit;                  bfd_byte *info_ptr)
      char               *info_ptr;  
615  {  {
616    attr->name = abbrev->name;    attr->name = abbrev->name;
617    info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);    info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
# Line 784  struct line_info Line 627  struct line_info
627  {  {
628    struct line_info* prev_line;    struct line_info* prev_line;
629    bfd_vma address;    bfd_vma address;
630    char* filename;    char *filename;
631    unsigned int line;    unsigned int line;
632    unsigned int column;    unsigned int column;
633    int end_sequence;             /* End of (sequential) code sequence.  */    int end_sequence;             /* End of (sequential) code sequence.  */
# Line 803  struct line_info_table Line 646  struct line_info_table
646    bfd* abfd;    bfd* abfd;
647    unsigned int num_files;    unsigned int num_files;
648    unsigned int num_dirs;    unsigned int num_dirs;
649    char* comp_dir;    char *comp_dir;
650    char** dirs;    char **dirs;
651    struct fileinfo* files;    struct fileinfo* files;
652    struct line_info* last_line;    struct line_info* last_line;  /* largest VMA */
653      struct line_info* lcl_head;   /* local head; used in 'add_line_info' */
654  };  };
655    
656  struct funcinfo  struct funcinfo
657  {  {
658    struct funcinfo *prev_func;    struct funcinfo *prev_func;
659    char* name;    char *name;
660    bfd_vma low;    bfd_vma low;
661    bfd_vma high;    bfd_vma high;
662  };  };
663    
664    /* Adds a new entry to the line_info list in the line_info_table, ensuring
665       that the list is sorted.  Note that the line_info list is sorted from
666       highest to lowest VMA (with possible duplicates); that is,
667       line_info->prev_line always accesses an equal or smaller VMA.  */
668    
669  static void  static void
670  add_line_info (table, address, filename, line, column, end_sequence)  add_line_info (struct line_info_table *table,
671       struct line_info_table* table;                 bfd_vma address,
672       bfd_vma address;                 char *filename,
673       char* filename;                 unsigned int line,
674       unsigned int line;                 unsigned int column,
675       unsigned int column;                 int end_sequence)
      int end_sequence;  
676  {  {
677    bfd_size_type amt = sizeof (struct line_info);    bfd_size_type amt = sizeof (struct line_info);
678    struct line_info* info = (struct line_info*) bfd_alloc (table->abfd, amt);    struct line_info* info = bfd_alloc (table->abfd, amt);
679    
680    info->prev_line = table->last_line;    /* Find the correct location for 'info'.  Normally we will receive
681    table->last_line = info;       new line_info data 1) in order and 2) with increasing VMAs.
682         However some compilers break the rules (cf. decode_line_info) and
683         so we include some heuristics for quickly finding the correct
684         location for 'info'. In particular, these heuristics optimize for
685         the common case in which the VMA sequence that we receive is a
686         list of locally sorted VMAs such as
687           p...z a...j  (where a < j < p < z)
688    
689         Note: table->lcl_head is used to head an *actual* or *possible*
690         sequence within the list (such as a...j) that is not directly
691         headed by table->last_line
692    
693         Note: we may receive duplicate entries from 'decode_line_info'.  */
694    
695      while (1)
696        if (!table->last_line
697            || address >= table->last_line->address)
698          {
699            /* Normal case: add 'info' to the beginning of the list */
700            info->prev_line = table->last_line;
701            table->last_line = info;
702    
703            /* lcl_head: initialize to head a *possible* sequence at the end.  */
704            if (!table->lcl_head)
705              table->lcl_head = info;
706            break;
707          }
708        else if (!table->lcl_head->prev_line
709                 && table->lcl_head->address > address)
710          {
711            /* Abnormal but easy: lcl_head is 1) at the *end* of the line
712               list and 2) the head of 'info'.  */
713            info->prev_line = NULL;
714            table->lcl_head->prev_line = info;
715            break;
716          }
717        else if (table->lcl_head->prev_line
718                 && table->lcl_head->address > address
719                 && address >= table->lcl_head->prev_line->address)
720          {
721            /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
722               list and 2) the head of 'info'.  */
723            info->prev_line = table->lcl_head->prev_line;
724            table->lcl_head->prev_line = info;
725            break;
726          }
727        else
728          {
729            /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
730               heads for 'info'.  Reset 'lcl_head' and repeat.  */
731            struct line_info* li2 = table->last_line; /* always non-NULL */
732            struct line_info* li1 = li2->prev_line;
733    
734            while (li1)
735              {
736                if (li2->address > address && address >= li1->address)
737                  break;
738    
739                li2 = li1; /* always non-NULL */
740                li1 = li1->prev_line;
741              }
742            table->lcl_head = li2;
743          }
744    
745      /* Set member data of 'info'.  */
746    info->address = address;    info->address = address;
   info->filename = filename;  
747    info->line = line;    info->line = line;
748    info->column = column;    info->column = column;
749    info->end_sequence = end_sequence;    info->end_sequence = end_sequence;
750    
751      if (filename && filename[0])
752        {
753          info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
754          if (info->filename)
755            strcpy (info->filename, filename);
756        }
757      else
758        info->filename = NULL;
759  }  }
760    
761    /* Extract a fully qualified filename from a line info table.
762       The returned string has been malloc'ed and it is the caller's
763       responsibility to free it.  */
764    
765  static char *  static char *
766  concat_filename (table, file)  concat_filename (struct line_info_table *table, unsigned int file)
      struct line_info_table* table;  
      unsigned int file;  
767  {  {
768    char* filename;    char *filename;
769    
770    if (file - 1 >= table->num_files)    if (file - 1 >= table->num_files)
771      {      {
772        (*_bfd_error_handler)        (*_bfd_error_handler)
773          (_("Dwarf Error: mangled line number section (bad file number)."));          (_("Dwarf Error: mangled line number section (bad file number)."));
774        return "<unknown>";        return strdup ("<unknown>");
775      }      }
776    
777    filename = table->files[file - 1].name;    filename = table->files[file - 1].name;
778    if (IS_ABSOLUTE_PATH(filename))  
779      return filename;    if (! IS_ABSOLUTE_PATH (filename))
   else  
780      {      {
781        char* dirname = (table->files[file - 1].dir        char *dirname = (table->files[file - 1].dir
782                         ? table->dirs[table->files[file - 1].dir - 1]                         ? table->dirs[table->files[file - 1].dir - 1]
783                         : table->comp_dir);                         : table->comp_dir);
784    
785        /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.  The        /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
786           best we can do is return the filename part.  */           The best we can do is return the filename part.  */
787        if (dirname == NULL)        if (dirname != NULL)
788          return filename;          {
789        else            unsigned int len = strlen (dirname) + strlen (filename) + 2;
790          return (char*) concat (dirname, "/", filename, NULL);            char * name;
791    
792              name = bfd_malloc (len);
793              if (name)
794                sprintf (name, "%s/%s", dirname, filename);
795              return name;
796            }
797      }      }
798    
799      return strdup (filename);
800  }  }
801    
802  static void  static void
803  arange_add (unit, low_pc, high_pc)  arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
      struct comp_unit *unit;  
      bfd_vma low_pc;  
      bfd_vma high_pc;  
804  {  {
805    struct arange *arange;    struct arange *arange;
806    
# Line 908  arange_add (unit, low_pc, high_pc) Line 833  arange_add (unit, low_pc, high_pc)
833      }      }
834    
835    /* Need to allocate a new arange and insert it into the arange list.  */    /* Need to allocate a new arange and insert it into the arange list.  */
836    arange = bfd_zalloc (unit->abfd, (bfd_size_type) sizeof (*arange));    arange = bfd_zalloc (unit->abfd, sizeof (*arange));
837    arange->low = low_pc;    arange->low = low_pc;
838    arange->high = high_pc;    arange->high = high_pc;
839    
# Line 919  arange_add (unit, low_pc, high_pc) Line 844  arange_add (unit, low_pc, high_pc)
844  /* Decode the line number information for UNIT.  */  /* Decode the line number information for UNIT.  */
845    
846  static struct line_info_table*  static struct line_info_table*
847  decode_line_info (unit, stash)  decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
      struct comp_unit *unit;  
      struct dwarf2_debug *stash;  
848  {  {
849    bfd *abfd = unit->abfd;    bfd *abfd = unit->abfd;
850    struct line_info_table* table;    struct line_info_table* table;
851    char *line_ptr;    bfd_byte *line_ptr;
852    char *line_end;    bfd_byte *line_end;
853    struct line_head lh;    struct line_head lh;
854    unsigned int i, bytes_read, offset_size;    unsigned int i, bytes_read, offset_size;
855    char *cur_file, *cur_dir;    char *cur_file, *cur_dir;
# Line 945  decode_line_info (unit, stash) Line 868  decode_line_info (unit, stash)
868            return 0;            return 0;
869          }          }
870    
871        stash->dwarf_line_size = msec->_raw_size;        stash->dwarf_line_size = msec->size;
872        stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, msec->_raw_size);        stash->dwarf_line_buffer
873            = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
874                                                         stash->syms);
875        if (! stash->dwarf_line_buffer)        if (! stash->dwarf_line_buffer)
876          return 0;          return 0;
   
       if (! bfd_get_section_contents (abfd, msec, stash->dwarf_line_buffer,  
                                       (bfd_vma) 0, msec->_raw_size))  
         return 0;  
   
       /* FIXME: We ought to apply the relocs against this section before  
          we process it...  */  
877      }      }
878    
879    /* Since we are using un-relocated data, it is possible to get a bad value    /* It is possible to get a bad value for the line_offset.  Validate
880       for the line_offset.  Validate it here so that we won't get a segfault       it here so that we won't get a segfault below.  */
      below.  */  
881    if (unit->line_offset >= stash->dwarf_line_size)    if (unit->line_offset >= stash->dwarf_line_size)
882      {      {
883        (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),        (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
# Line 970  decode_line_info (unit, stash) Line 887  decode_line_info (unit, stash)
887      }      }
888    
889    amt = sizeof (struct line_info_table);    amt = sizeof (struct line_info_table);
890    table = (struct line_info_table*) bfd_alloc (abfd, amt);    table = bfd_alloc (abfd, amt);
891    table->abfd = abfd;    table->abfd = abfd;
892    table->comp_dir = unit->comp_dir;    table->comp_dir = unit->comp_dir;
893    
# Line 982  decode_line_info (unit, stash) Line 899  decode_line_info (unit, stash)
899    
900    table->files = NULL;    table->files = NULL;
901    table->last_line = NULL;    table->last_line = NULL;
902      table->lcl_head = NULL;
903    
904    line_ptr = stash->dwarf_line_buffer + unit->line_offset;    line_ptr = stash->dwarf_line_buffer + unit->line_offset;
905    
# Line 1021  decode_line_info (unit, stash) Line 939  decode_line_info (unit, stash)
939    lh.opcode_base = read_1_byte (abfd, line_ptr);    lh.opcode_base = read_1_byte (abfd, line_ptr);
940    line_ptr += 1;    line_ptr += 1;
941    amt = lh.opcode_base * sizeof (unsigned char);    amt = lh.opcode_base * sizeof (unsigned char);
942    lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);    lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
943    
944    lh.standard_opcode_lengths[0] = 1;    lh.standard_opcode_lengths[0] = 1;
945    
# Line 1040  decode_line_info (unit, stash) Line 958  decode_line_info (unit, stash)
958          {          {
959            amt = table->num_dirs + DIR_ALLOC_CHUNK;            amt = table->num_dirs + DIR_ALLOC_CHUNK;
960            amt *= sizeof (char *);            amt *= sizeof (char *);
961            table->dirs = (char **) bfd_realloc (table->dirs, amt);            table->dirs = bfd_realloc (table->dirs, amt);
962            if (! table->dirs)            if (! table->dirs)
963              return 0;              return 0;
964          }          }
# Line 1059  decode_line_info (unit, stash) Line 977  decode_line_info (unit, stash)
977          {          {
978            amt = table->num_files + FILE_ALLOC_CHUNK;            amt = table->num_files + FILE_ALLOC_CHUNK;
979            amt *= sizeof (struct fileinfo);            amt *= sizeof (struct fileinfo);
980            table->files = (struct fileinfo *) bfd_realloc (table->files, amt);            table->files = bfd_realloc (table->files, amt);
981            if (! table->files)            if (! table->files)
982              return 0;              return 0;
983          }          }
# Line 1084  decode_line_info (unit, stash) Line 1002  decode_line_info (unit, stash)
1002      {      {
1003        /* State machine registers.  */        /* State machine registers.  */
1004        bfd_vma address = 0;        bfd_vma address = 0;
1005        char* filename = concat_filename (table, 1);        char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1006        unsigned int line = 1;        unsigned int line = 1;
1007        unsigned int column = 0;        unsigned int column = 0;
1008        int is_stmt = lh.default_is_stmt;        int is_stmt = lh.default_is_stmt;
1009        int basic_block = 0;        int basic_block = 0;
1010        int end_sequence = 0, need_low_pc = 1;        int end_sequence = 0;
1011        bfd_vma low_pc = 0;        /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1012             compilers generate address sequences that are wildly out of
1013             order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1014             for ia64-Linux).  Thus, to determine the low and high
1015             address, we must compare on every DW_LNS_copy, etc.  */
1016          bfd_vma low_pc  = 0;
1017          bfd_vma high_pc = 0;
1018    
1019        /* Decode the table.  */        /* Decode the table.  */
1020        while (! end_sequence)        while (! end_sequence)
# Line 1099  decode_line_info (unit, stash) Line 1023  decode_line_info (unit, stash)
1023            line_ptr += 1;            line_ptr += 1;
1024    
1025            if (op_code >= lh.opcode_base)            if (op_code >= lh.opcode_base)
1026              {           /* Special operand.  */              {
1027                  /* Special operand.  */
1028                adj_opcode = op_code - lh.opcode_base;                adj_opcode = op_code - lh.opcode_base;
1029                address += (adj_opcode / lh.line_range)                address += (adj_opcode / lh.line_range)
1030                  * lh.minimum_instruction_length;                  * lh.minimum_instruction_length;
# Line 1107  decode_line_info (unit, stash) Line 1032  decode_line_info (unit, stash)
1032                /* Append row to matrix using current values.  */                /* Append row to matrix using current values.  */
1033                add_line_info (table, address, filename, line, column, 0);                add_line_info (table, address, filename, line, column, 0);
1034                basic_block = 1;                basic_block = 1;
1035                if (need_low_pc)                if (low_pc == 0 || address < low_pc)
1036                  {                  low_pc = address;
1037                    need_low_pc = 0;                if (address > high_pc)
1038                    low_pc = address;                  high_pc = address;
                 }  
1039              }              }
1040            else switch (op_code)            else switch (op_code)
1041              {              {
1042              case DW_LNS_extended_op:              case DW_LNS_extended_op:
1043                line_ptr += 1;    /* Ignore length.  */                /* Ignore length.  */
1044                  line_ptr += 1;
1045                extended_op = read_1_byte (abfd, line_ptr);                extended_op = read_1_byte (abfd, line_ptr);
1046                line_ptr += 1;                line_ptr += 1;
1047    
1048                switch (extended_op)                switch (extended_op)
1049                  {                  {
1050                  case DW_LNE_end_sequence:                  case DW_LNE_end_sequence:
1051                    end_sequence = 1;                    end_sequence = 1;
1052                    add_line_info (table, address, filename, line, column,                    add_line_info (table, address, filename, line, column,
1053                                   end_sequence);                                   end_sequence);
1054                    if (need_low_pc)                    if (low_pc == 0 || address < low_pc)
1055                      {                      low_pc = address;
1056                        need_low_pc = 0;                    if (address > high_pc)
1057                        low_pc = address;                      high_pc = address;
1058                      }                    arange_add (unit, low_pc, high_pc);
                   arange_add (unit, low_pc, address);  
1059                    break;                    break;
1060                  case DW_LNE_set_address:                  case DW_LNE_set_address:
1061                    address = read_address (unit, line_ptr);                    address = read_address (unit, line_ptr);
# Line 1143  decode_line_info (unit, stash) Line 1068  decode_line_info (unit, stash)
1068                      {                      {
1069                        amt = table->num_files + FILE_ALLOC_CHUNK;                        amt = table->num_files + FILE_ALLOC_CHUNK;
1070                        amt *= sizeof (struct fileinfo);                        amt *= sizeof (struct fileinfo);
1071                        table->files =                        table->files = bfd_realloc (table->files, amt);
                         (struct fileinfo *) bfd_realloc (table->files, amt);  
1072                        if (! table->files)                        if (! table->files)
1073                          return 0;                          return 0;
1074                      }                      }
# Line 1169  decode_line_info (unit, stash) Line 1093  decode_line_info (unit, stash)
1093              case DW_LNS_copy:              case DW_LNS_copy:
1094                add_line_info (table, address, filename, line, column, 0);                add_line_info (table, address, filename, line, column, 0);
1095                basic_block = 0;                basic_block = 0;
1096                if (need_low_pc)                if (low_pc == 0 || address < low_pc)
1097                  {                  low_pc = address;
1098                    need_low_pc = 0;                if (address > high_pc)
1099                    low_pc = address;                  high_pc = address;
                 }  
1100                break;                break;
1101              case DW_LNS_advance_pc:              case DW_LNS_advance_pc:
1102                address += lh.minimum_instruction_length                address += lh.minimum_instruction_length
# Line 1188  decode_line_info (unit, stash) Line 1111  decode_line_info (unit, stash)
1111                {                {
1112                  unsigned int file;                  unsigned int file;
1113    
1114                  /* The file and directory tables are 0 based, the references                  /* The file and directory tables are 0
1115                     are 1 based.  */                     based, the references are 1 based.  */
1116                  file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);                  file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1117                  line_ptr += bytes_read;                  line_ptr += bytes_read;
1118                    if (filename)
1119                      free (filename);
1120                  filename = concat_filename (table, file);                  filename = concat_filename (table, file);
1121                  break;                  break;
1122                }                }
# Line 1214  decode_line_info (unit, stash) Line 1139  decode_line_info (unit, stash)
1139                line_ptr += 2;                line_ptr += 2;
1140                break;                break;
1141              default:              default:
1142                {  /* Unknown standard opcode, ignore it.  */                {
1143                  int i;                  int i;
1144    
1145                    /* Unknown standard opcode, ignore it.  */
1146                  for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)                  for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1147                    {                    {
1148                      (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);                      (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
# Line 1224  decode_line_info (unit, stash) Line 1151  decode_line_info (unit, stash)
1151                }                }
1152              }              }
1153          }          }
1154    
1155          if (filename)
1156            free (filename);
1157      }      }
1158    
1159    return table;    return table;
1160  }  }
1161    
1162  /* If ADDR is within TABLE set the output parameters and return true,  /* If ADDR is within TABLE set the output parameters and return TRUE,
1163     otherwise return false.  The output parameters, FILENAME_PTR and     otherwise return FALSE.  The output parameters, FILENAME_PTR and
1164     LINENUMBER_PTR, are pointers to the objects to be filled in.  */     LINENUMBER_PTR, are pointers to the objects to be filled in.  */
1165    
1166  static boolean  static bfd_boolean
1167  lookup_address_in_line_info_table (table,  lookup_address_in_line_info_table (struct line_info_table *table,
1168                                     addr,                                     bfd_vma addr,
1169                                     function,                                     struct funcinfo *function,
1170                                     filename_ptr,                                     const char **filename_ptr,
1171                                     linenumber_ptr)                                     unsigned int *linenumber_ptr)
      struct line_info_table* table;  
      bfd_vma addr;  
      struct funcinfo *function;  
      const char **filename_ptr;  
      unsigned int *linenumber_ptr;  
1172  {  {
1173      /* Note: table->last_line should be a descendingly sorted list. */
1174    struct line_info* next_line = table->last_line;    struct line_info* next_line = table->last_line;
1175    struct line_info* each_line;    struct line_info* each_line = NULL;
1176      *filename_ptr = NULL;
1177    
1178    if (!next_line)    if (!next_line)
1179      return false;      return FALSE;
1180    
1181    each_line = next_line->prev_line;    each_line = next_line->prev_line;
1182    
1183      /* Check for large addresses */
1184      if (addr > next_line->address)
1185        each_line = NULL; /* ensure we skip over the normal case */
1186    
1187      /* Normal case: search the list; save  */
1188    while (each_line && next_line)    while (each_line && next_line)
1189      {      {
1190        if (!each_line->end_sequence        /* If we have an address match, save this info.  This allows us
1191            && addr >= each_line->address && addr < next_line->address)           to return as good as results as possible for strange debugging
1192             info.  */
1193          bfd_boolean addr_match = FALSE;
1194          if (each_line->address <= addr && addr <= next_line->address)
1195          {          {
1196              addr_match = TRUE;
1197    
1198            /* If this line appears to span functions, and addr is in the            /* If this line appears to span functions, and addr is in the
1199               later function, return the first line of that function instead               later function, return the first line of that function instead
1200               of the last line of the earlier one.  This check is for GCC               of the last line of the earlier one.  This check is for GCC
# Line 1274  lookup_address_in_line_info_table (table Line 1211  lookup_address_in_line_info_table (table
1211                *filename_ptr = each_line->filename;                *filename_ptr = each_line->filename;
1212                *linenumber_ptr = each_line->line;                *linenumber_ptr = each_line->line;
1213              }              }
           return true;  
1214          }          }
1215    
1216          if (addr_match && !each_line->end_sequence)
1217            return TRUE; /* we have definitely found what we want */
1218    
1219        next_line = each_line;        next_line = each_line;
1220        each_line = each_line->prev_line;        each_line = each_line->prev_line;
1221      }      }
1222    
1223    /* At this point each_line is NULL but next_line is not.  If we found the    /* At this point each_line is NULL but next_line is not.  If we found
1224       containing function in this compilation unit, return the first line we       a candidate end-of-sequence point in the loop above, we can return
1225       have a number for.  This is also for compatibility with GCC 2.95.  */       that (compatibility with a bug in the Intel compiler); otherwise,
1226    if (function != NULL)       assuming that we found the containing function for this address in
1227         this compilation unit, return the first line we have a number for
1228         (compatibility with GCC 2.95).  */
1229      if (*filename_ptr == NULL && function != NULL)
1230      {      {
1231        *filename_ptr = next_line->filename;        *filename_ptr = next_line->filename;
1232        *linenumber_ptr = next_line->line;        *linenumber_ptr = next_line->line;
1233        return true;        return TRUE;
1234      }      }
1235    
1236    return false;    return FALSE;
1237  }  }
1238    
1239  /* Function table functions.  */  /* Function table functions.  */
1240    
1241  /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true.  */  /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.  */
1242    
1243  static boolean  static bfd_boolean
1244  lookup_address_in_function_table (table,  lookup_address_in_function_table (struct funcinfo *table,
1245                                    addr,                                    bfd_vma addr,
1246                                    function_ptr,                                    struct funcinfo **function_ptr,
1247                                    functionname_ptr)                                    const char **functionname_ptr)
      struct funcinfo* table;  
      bfd_vma addr;  
      struct funcinfo** function_ptr;  
      const char **functionname_ptr;  
1248  {  {
1249    struct funcinfo* each_func;    struct funcinfo* each_func;
1250    
# Line 1317  lookup_address_in_function_table (table, Line 1256  lookup_address_in_function_table (table,
1256          {          {
1257            *functionname_ptr = each_func->name;            *functionname_ptr = each_func->name;
1258            *function_ptr = each_func;            *function_ptr = each_func;
1259            return true;            return TRUE;
1260          }          }
1261      }      }
1262    
1263    return false;    return FALSE;
1264    }
1265    
1266    static char *
1267    find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1268    {
1269      bfd *abfd = unit->abfd;
1270      bfd_byte *info_ptr;
1271      unsigned int abbrev_number, bytes_read, i;
1272      struct abbrev_info *abbrev;
1273      struct attribute attr;
1274      char *name = 0;
1275    
1276      info_ptr = unit->info_ptr_unit + die_ref;
1277      abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1278      info_ptr += bytes_read;
1279    
1280      if (abbrev_number)
1281        {
1282          abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1283          if (! abbrev)
1284            {
1285              (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1286                                     abbrev_number);
1287              bfd_set_error (bfd_error_bad_value);
1288            }
1289          else
1290            {
1291              for (i = 0; i < abbrev->num_attrs && !name; ++i)
1292                {
1293                  info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1294                  switch (attr.name)
1295                    {
1296                    case DW_AT_name:
1297                      name = attr.u.str;
1298                      break;
1299                    case DW_AT_specification:
1300                      name = find_abstract_instance_name (unit, attr.u.val);
1301                      break;
1302                    default:
1303                      break;
1304                    }
1305                }
1306            }
1307        }
1308      return (name);
1309  }  }
1310    
1311  /* DWARF2 Compilation unit functions.  */  /* DWARF2 Compilation unit functions.  */
# Line 1329  lookup_address_in_function_table (table, Line 1313  lookup_address_in_function_table (table,
1313  /* Scan over each die in a comp. unit looking for functions to add  /* Scan over each die in a comp. unit looking for functions to add
1314     to the function table.  */     to the function table.  */
1315    
1316  static boolean  static bfd_boolean
1317  scan_unit_for_functions (unit)  scan_unit_for_functions (struct comp_unit *unit)
      struct comp_unit *unit;  
1318  {  {
1319    bfd *abfd = unit->abfd;    bfd *abfd = unit->abfd;
1320    char *info_ptr = unit->first_child_die_ptr;    bfd_byte *info_ptr = unit->first_child_die_ptr;
1321    int nesting_level = 1;    int nesting_level = 1;
1322    
1323    while (nesting_level)    while (nesting_level)
# Line 1343  scan_unit_for_functions (unit) Line 1326  scan_unit_for_functions (unit)
1326        struct abbrev_info *abbrev;        struct abbrev_info *abbrev;
1327        struct attribute attr;        struct attribute attr;
1328        struct funcinfo *func;        struct funcinfo *func;
1329        char* name = 0;        char *name = 0;
1330    
1331        abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);        abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1332        info_ptr += bytes_read;        info_ptr += bytes_read;
# Line 1360  scan_unit_for_functions (unit) Line 1343  scan_unit_for_functions (unit)
1343            (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),            (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1344                               abbrev_number);                               abbrev_number);
1345            bfd_set_error (bfd_error_bad_value);            bfd_set_error (bfd_error_bad_value);
1346            return false;            return FALSE;
1347          }          }
1348    
1349        if (abbrev->tag == DW_TAG_subprogram)        if (abbrev->tag == DW_TAG_subprogram
1350              || abbrev->tag == DW_TAG_inlined_subroutine)
1351          {          {
1352            bfd_size_type amt = sizeof (struct funcinfo);            bfd_size_type amt = sizeof (struct funcinfo);
1353            func = (struct funcinfo *) bfd_zalloc (abfd, amt);            func = bfd_zalloc (abfd, amt);
1354            func->prev_func = unit->function_table;            func->prev_func = unit->function_table;
1355            unit->function_table = func;            unit->function_table = func;
1356          }          }
# Line 1381  scan_unit_for_functions (unit) Line 1365  scan_unit_for_functions (unit)
1365              {              {
1366                switch (attr.name)                switch (attr.name)
1367                  {                  {
1368                    case DW_AT_abstract_origin:
1369                      func->name = find_abstract_instance_name (unit, attr.u.val);
1370                      break;
1371    
1372                  case DW_AT_name:                  case DW_AT_name:
1373    
1374                    name = DW_STRING (&attr);                    name = attr.u.str;
1375    
1376                    /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */                    /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1377                    if (func->name == NULL)                    if (func->name == NULL)
1378                      func->name = DW_STRING (&attr);                      func->name = attr.u.str;
1379                    break;                    break;
1380    
1381                  case DW_AT_MIPS_linkage_name:                  case DW_AT_MIPS_linkage_name:
1382                    func->name = DW_STRING (&attr);                    func->name = attr.u.str;
1383                    break;                    break;
1384    
1385                  case DW_AT_low_pc:                  case DW_AT_low_pc:
1386                    func->low = DW_ADDR (&attr);                    func->low = attr.u.val;
1387                    break;                    break;
1388    
1389                  case DW_AT_high_pc:                  case DW_AT_high_pc:
1390                    func->high = DW_ADDR (&attr);                    func->high = attr.u.val;
1391                    break;                    break;
1392    
1393                  default:                  default:
# Line 1411  scan_unit_for_functions (unit) Line 1399  scan_unit_for_functions (unit)
1399                switch (attr.name)                switch (attr.name)
1400                  {                  {
1401                  case DW_AT_name:                  case DW_AT_name:
1402                    name = DW_STRING (&attr);                    name = attr.u.str;
1403                    break;                    break;
1404    
1405                  default:                  default:
# Line 1424  scan_unit_for_functions (unit) Line 1412  scan_unit_for_functions (unit)
1412          nesting_level++;          nesting_level++;
1413      }      }
1414    
1415    return true;    return TRUE;
 }  
   
 /* Look for a RELA relocation to be applied on OFFSET of section SEC,  
    and return the addend if such a relocation is found.  Since this is  
    only used to find relocations referring to the .debug_abbrev  
    section, we make sure the relocation refers to this section, but  
    this is not strictly necessary, and it can probably be safely  
    removed if needed.  However, it is important to note that this  
    function only returns the addend, it doesn't serve the purpose of  
    applying a generic relocation.  
   
    If no suitable relocation is found, or if it is not a real RELA  
    relocation, this function returns 0.  */  
   
 static bfd_vma  
 find_rela_addend (abfd, sec, offset, syms)  
      bfd* abfd;  
      asection* sec;  
      bfd_size_type offset;  
      asymbol** syms;  
 {  
   long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);  
   arelent **relocs = NULL;  
   long reloc_count, relc;  
   
   if (reloc_size <= 0)  
     return 0;  
   
   relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);  
   if (relocs == NULL)  
     return 0;  
   
   reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, syms);  
   
   if (reloc_count <= 0)  
     {  
       free (relocs);  
       return 0;  
     }  
   
   for (relc = 0; relc < reloc_count; relc++)  
     if (relocs[relc]->address == offset  
         && (*relocs[relc]->sym_ptr_ptr)->flags & BSF_SECTION_SYM  
         && strcmp ((*relocs[relc]->sym_ptr_ptr)->name,  
                    ".debug_abbrev") == 0)  
       {  
         bfd_vma addend = (relocs[relc]->howto->partial_inplace  
                           ? 0 : relocs[relc]->addend);  
         free (relocs);  
         return addend;  
       }  
   
   free (relocs);  
   return 0;  
1416  }  }
1417    
1418  /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This  /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
1419     includes the compilation unit header that proceeds the DIE's, but     includes the compilation unit header that proceeds the DIE's, but
1420     does not include the length field that preceeds each compilation     does not include the length field that precedes each compilation
1421     unit header.  END_PTR points one past the end of this comp unit.     unit header.  END_PTR points one past the end of this comp unit.
1422     OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).     OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1423    
# Line 1491  find_rela_addend (abfd, sec, offset, sym Line 1425  find_rela_addend (abfd, sec, offset, sym
1425     to get to the line number information for the compilation unit.  */     to get to the line number information for the compilation unit.  */
1426    
1427  static struct comp_unit *  static struct comp_unit *
1428  parse_comp_unit (abfd, stash, unit_length, offset_size)  parse_comp_unit (bfd *abfd,
1429       bfd* abfd;                   struct dwarf2_debug *stash,
1430       struct dwarf2_debug *stash;                   bfd_vma unit_length,
1431       bfd_vma unit_length;                   bfd_byte *info_ptr_unit,
1432       unsigned int offset_size;                   unsigned int offset_size)
1433  {  {
1434    struct comp_unit* unit;    struct comp_unit* unit;
1435    unsigned int version;    unsigned int version;
1436    bfd_vma abbrev_offset = 0;    bfd_uint64_t abbrev_offset = 0;
1437    unsigned int addr_size;    unsigned int addr_size;
1438    struct abbrev_info** abbrevs;    struct abbrev_info** abbrevs;
1439    unsigned int abbrev_number, bytes_read, i;    unsigned int abbrev_number, bytes_read, i;
1440    struct abbrev_info *abbrev;    struct abbrev_info *abbrev;
1441    struct attribute attr;    struct attribute attr;
1442    char *info_ptr = stash->info_ptr;    bfd_byte *info_ptr = stash->info_ptr;
1443    char *end_ptr = info_ptr + unit_length;    bfd_byte *end_ptr = info_ptr + unit_length;
1444    bfd_size_type amt;    bfd_size_type amt;
   bfd_size_type off;  
1445    
1446    version = read_2_bytes (abfd, info_ptr);    version = read_2_bytes (abfd, info_ptr);
1447    info_ptr += 2;    info_ptr += 2;
# Line 1517  parse_comp_unit (abfd, stash, unit_lengt Line 1450  parse_comp_unit (abfd, stash, unit_lengt
1450      abbrev_offset = read_4_bytes (abfd, info_ptr);      abbrev_offset = read_4_bytes (abfd, info_ptr);
1451    else    else
1452      abbrev_offset = read_8_bytes (abfd, info_ptr);      abbrev_offset = read_8_bytes (abfd, info_ptr);
   /* The abbrev offset is generally a relocation pointing to  
      .debug_abbrev+offset.  On RELA targets, we have to find the  
      relocation and extract the addend to obtain the actual  
      abbrev_offset, so do it here.  */  
   off = info_ptr - stash->sec_info_ptr;  
   abbrev_offset += find_rela_addend (abfd, stash->sec, off, stash->syms);  
1453    info_ptr += offset_size;    info_ptr += offset_size;
1454    addr_size = read_1_byte (abfd, info_ptr);    addr_size = read_1_byte (abfd, info_ptr);
1455    info_ptr += 1;    info_ptr += 1;
# Line 1575  parse_comp_unit (abfd, stash, unit_lengt Line 1502  parse_comp_unit (abfd, stash, unit_lengt
1502      }      }
1503    
1504    amt = sizeof (struct comp_unit);    amt = sizeof (struct comp_unit);
1505    unit = (struct comp_unit*) bfd_zalloc (abfd, amt);    unit = bfd_zalloc (abfd, amt);
1506    unit->abfd = abfd;    unit->abfd = abfd;
1507    unit->addr_size = addr_size;    unit->addr_size = addr_size;
1508    unit->offset_size = offset_size;    unit->offset_size = offset_size;
1509    unit->abbrevs = abbrevs;    unit->abbrevs = abbrevs;
1510    unit->end_ptr = end_ptr;    unit->end_ptr = end_ptr;
1511    unit->stash = stash;    unit->stash = stash;
1512      unit->info_ptr_unit = info_ptr_unit;
1513    
1514    for (i = 0; i < abbrev->num_attrs; ++i)    for (i = 0; i < abbrev->num_attrs; ++i)
1515      {      {
# Line 1593  parse_comp_unit (abfd, stash, unit_lengt Line 1521  parse_comp_unit (abfd, stash, unit_lengt
1521          {          {
1522          case DW_AT_stmt_list:          case DW_AT_stmt_list:
1523            unit->stmtlist = 1;            unit->stmtlist = 1;
1524            unit->line_offset = DW_UNSND (&attr);            unit->line_offset = attr.u.val;
1525            break;            break;
1526    
1527          case DW_AT_name:          case DW_AT_name:
1528            unit->name = DW_STRING (&attr);            unit->name = attr.u.str;
1529            break;            break;
1530    
1531          case DW_AT_low_pc:          case DW_AT_low_pc:
1532            unit->arange.low = DW_ADDR (&attr);            unit->arange.low = attr.u.val;
1533            break;            break;
1534    
1535          case DW_AT_high_pc:          case DW_AT_high_pc:
1536            unit->arange.high = DW_ADDR (&attr);            unit->arange.high = attr.u.val;
1537            break;            break;
1538    
1539          case DW_AT_comp_dir:          case DW_AT_comp_dir:
1540            {            {
1541              char* comp_dir = DW_STRING (&attr);              char *comp_dir = attr.u.str;
1542              if (comp_dir)              if (comp_dir)
1543                {                {
1544                  /* Irix 6.2 native cc prepends <machine>.: to the compilation                  /* Irix 6.2 native cc prepends <machine>.: to the compilation
1545                     directory, get rid of it.  */                     directory, get rid of it.  */
1546                  char *cp = (char*) strchr (comp_dir, ':');                  char *cp = strchr (comp_dir, ':');
1547    
1548                  if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')                  if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1549                    comp_dir = cp + 1;                    comp_dir = cp + 1;
# Line 1633  parse_comp_unit (abfd, stash, unit_lengt Line 1561  parse_comp_unit (abfd, stash, unit_lengt
1561    return unit;    return unit;
1562  }  }
1563    
1564  /* Return true if UNIT contains the address given by ADDR.  */  /* Return TRUE if UNIT contains the address given by ADDR.  */
1565    
1566  static boolean  static bfd_boolean
1567  comp_unit_contains_address (unit, addr)  comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
      struct comp_unit* unit;  
      bfd_vma addr;  
1568  {  {
1569    struct arange *arange;    struct arange *arange;
1570    
1571    if (unit->error)    if (unit->error)
1572      return 0;      return FALSE;
1573    
1574    arange = &unit->arange;    arange = &unit->arange;
1575    do    do
1576      {      {
1577        if (addr >= arange->low && addr < arange->high)        if (addr >= arange->low && addr < arange->high)
1578          return 1;          return TRUE;
1579        arange = arange->next;        arange = arange->next;
1580      }      }
1581    while (arange);    while (arange);
1582    
1583    return 0;    return FALSE;
1584  }  }
1585    
1586  /* If UNIT contains ADDR, set the output parameters to the values for  /* If UNIT contains ADDR, set the output parameters to the values for
# Line 1662  comp_unit_contains_address (unit, addr) Line 1588  comp_unit_contains_address (unit, addr)
1588     FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects     FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1589     to be filled in.     to be filled in.
1590    
1591     Return true of UNIT contains ADDR, and no errors were encountered;     Return TRUE if UNIT contains ADDR, and no errors were encountered;
1592     false otherwise.  */     FALSE otherwise.  */
1593    
1594  static boolean  static bfd_boolean
1595  comp_unit_find_nearest_line (unit, addr,  comp_unit_find_nearest_line (struct comp_unit *unit,
1596                               filename_ptr, functionname_ptr, linenumber_ptr,                               bfd_vma addr,
1597                               stash)                               const char **filename_ptr,
1598       struct comp_unit* unit;                               const char **functionname_ptr,
1599       bfd_vma addr;                               unsigned int *linenumber_ptr,
1600       const char **filename_ptr;                               struct dwarf2_debug *stash)
      const char **functionname_ptr;  
      unsigned int *linenumber_ptr;  
      struct dwarf2_debug *stash;  
1601  {  {
1602    boolean line_p;    bfd_boolean line_p;
1603    boolean func_p;    bfd_boolean func_p;
1604    struct funcinfo *function;    struct funcinfo *function;
1605    
1606    if (unit->error)    if (unit->error)
1607      return false;      return FALSE;
1608    
1609    if (! unit->line_table)    if (! unit->line_table)
1610      {      {
1611        if (! unit->stmtlist)        if (! unit->stmtlist)
1612          {          {
1613            unit->error = 1;            unit->error = 1;
1614            return false;            return FALSE;
1615          }          }
1616    
1617        unit->line_table = decode_line_info (unit, stash);        unit->line_table = decode_line_info (unit, stash);
# Line 1696  comp_unit_find_nearest_line (unit, addr, Line 1619  comp_unit_find_nearest_line (unit, addr,
1619        if (! unit->line_table)        if (! unit->line_table)
1620          {          {
1621            unit->error = 1;            unit->error = 1;
1622            return false;            return FALSE;
1623          }          }
1624    
1625        if (unit->first_child_die_ptr < unit->end_ptr        if (unit->first_child_die_ptr < unit->end_ptr
1626            && ! scan_unit_for_functions (unit))            && ! scan_unit_for_functions (unit))
1627          {          {
1628            unit->error = 1;            unit->error = 1;
1629            return false;            return FALSE;
1630          }          }
1631      }      }
1632    
1633    function = NULL;    function = NULL;
1634    func_p = lookup_address_in_function_table (unit->function_table,    func_p = lookup_address_in_function_table (unit->function_table, addr,
1635                                               addr,                                               &function, functionname_ptr);
1636                                               &function,    line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1637                                               functionname_ptr);                                                function, filename_ptr,
   line_p = lookup_address_in_line_info_table (unit->line_table,  
                                               addr,  
                                               function,  
                                               filename_ptr,  
1638                                                linenumber_ptr);                                                linenumber_ptr);
1639    return line_p || func_p;    return line_p || func_p;
1640  }  }
1641    
1642  /* Locate a section in a BFD containing debugging info.  The search starts from the  /* Locate a section in a BFD containing debugging info.  The search starts
1643     section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is     from the section after AFTER_SEC, or from the first section in the BFD if
1644     NULL.  The search works by examining the names of the sections.  There are two     AFTER_SEC is NULL.  The search works by examining the names of the
1645     permissiable names.  The first is .debug_info.  This is the standard DWARF2 name.     sections.  There are two permissiable names.  The first is .debug_info.
1646     The second is a prefix .gnu.linkonce.wi.  This is a variation on the .debug_info     This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
1647     section which has a checksum describing the contents appended onto the name.  This     This is a variation on the .debug_info section which has a checksum
1648     allows the linker to identify and discard duplicate debugging sections for     describing the contents appended onto the name.  This allows the linker to
1649     different compilation units.  */     identify and discard duplicate debugging sections for different
1650       compilation units.  */
1651  #define DWARF2_DEBUG_INFO ".debug_info"  #define DWARF2_DEBUG_INFO ".debug_info"
1652  #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."  #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1653    
1654  static asection *  static asection *
1655  find_debug_info (abfd, after_sec)  find_debug_info (bfd *abfd, asection *after_sec)
      bfd * abfd;  
      asection * after_sec;  
1656  {  {
1657    asection * msec;    asection * msec;
1658    
# Line 1757  find_debug_info (abfd, after_sec) Line 1675  find_debug_info (abfd, after_sec)
1675    return NULL;    return NULL;
1676  }  }
1677    
1678  /* The DWARF2 version of find_nearest line.  Return true if the line  /* The DWARF2 version of find_nearest_line.  Return TRUE if the line
1679     is found without error.  ADDR_SIZE is the number of bytes in the     is found without error.  ADDR_SIZE is the number of bytes in the
1680     initial .debug_info length field and in the abbreviation offset.     initial .debug_info length field and in the abbreviation offset.
1681     You may use zero to indicate that the default value should be     You may use zero to indicate that the default value should be
1682     used.  */     used.  */
1683    
1684  boolean  bfd_boolean
1685  _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,  _bfd_dwarf2_find_nearest_line (bfd *abfd,
1686                                 filename_ptr, functionname_ptr,                                 asection *section,
1687                                 linenumber_ptr,                                 asymbol **symbols,
1688                                 addr_size, pinfo)                                 bfd_vma offset,
1689       bfd *abfd;                                 const char **filename_ptr,
1690       asection *section;                                 const char **functionname_ptr,
1691       asymbol **symbols;                                 unsigned int *linenumber_ptr,
1692       bfd_vma offset;                                 unsigned int addr_size,
1693       const char **filename_ptr;                                 void **pinfo)
      const char **functionname_ptr;  
      unsigned int *linenumber_ptr;  
      unsigned int addr_size;  
      PTR *pinfo;  
1694  {  {
1695    /* Read each compilation unit from the section .debug_info, and check    /* Read each compilation unit from the section .debug_info, and check
1696       to see if it contains the address we are searching for.  If yes,       to see if it contains the address we are searching for.  If yes,
# Line 1786  _bfd_dwarf2_find_nearest_line (abfd, sec Line 1700  _bfd_dwarf2_find_nearest_line (abfd, sec
1700       We keep a list of all the previously read compilation units, and       We keep a list of all the previously read compilation units, and
1701       a pointer to the next un-read compilation unit.  Check the       a pointer to the next un-read compilation unit.  Check the
1702       previously read units before reading more.  */       previously read units before reading more.  */
1703    struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;    struct dwarf2_debug *stash;
1704    
1705    /* What address are we looking for?  */    /* What address are we looking for?  */
1706    bfd_vma addr = offset + section->vma;    bfd_vma addr;
1707    
1708    struct comp_unit* each;    struct comp_unit* each;
1709    
1710      stash = *pinfo;
1711      addr = offset;
1712      if (section->output_section)
1713        addr += section->output_section->vma + section->output_offset;
1714      else
1715        addr += section->vma;
1716    *filename_ptr = NULL;    *filename_ptr = NULL;
1717    *functionname_ptr = NULL;    *functionname_ptr = NULL;
1718    *linenumber_ptr = 0;    *linenumber_ptr = 0;
# Line 1810  _bfd_dwarf2_find_nearest_line (abfd, sec Line 1730  _bfd_dwarf2_find_nearest_line (abfd, sec
1730        asection *msec;        asection *msec;
1731        bfd_size_type amt = sizeof (struct dwarf2_debug);        bfd_size_type amt = sizeof (struct dwarf2_debug);
1732    
1733        stash = (struct dwarf2_debug*) bfd_zalloc (abfd, amt);        stash = bfd_zalloc (abfd, amt);
1734        if (! stash)        if (! stash)
1735          return false;          return FALSE;
1736    
1737        *pinfo = (PTR) stash;        *pinfo = stash;
1738    
1739        msec = find_debug_info (abfd, NULL);        msec = find_debug_info (abfd, NULL);
1740        if (! msec)        if (! msec)
1741          /* No dwarf2 info.  Note that at this point the stash          /* No dwarf2 info.  Note that at this point the stash
1742             has been allocated, but contains zeros, this lets             has been allocated, but contains zeros, this lets
1743             future calls to this function fail quicker.  */             future calls to this function fail quicker.  */
1744           return false;           return FALSE;
1745    
1746        /* There can be more than one DWARF2 info section in a BFD these days.        /* There can be more than one DWARF2 info section in a BFD these days.
1747           Read them all in and produce one large stash.  We do this in two           Read them all in and produce one large stash.  We do this in two
1748           passes - in the first pass we just accumulate the section sizes.           passes - in the first pass we just accumulate the section sizes.
1749           In the second pass we read in the section's contents.  The allows           In the second pass we read in the section's contents.  The allows
1750           us to avoid reallocing the data as we add sections to the stash.  */           us to avoid reallocing the data as we add sections to the stash.  */
1751        for (total_size = 0; msec; msec = find_debug_info (abfd, msec))        for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1752          total_size += msec->_raw_size;          total_size += msec->size;
1753    
1754        stash->info_ptr = (char *) bfd_alloc (abfd, total_size);        stash->info_ptr = bfd_alloc (abfd, total_size);
1755        if (stash->info_ptr == NULL)        if (stash->info_ptr == NULL)
1756          return false;          return FALSE;
1757    
1758        stash->info_ptr_end = stash->info_ptr;        stash->info_ptr_end = stash->info_ptr;
1759    
# Line 1844  _bfd_dwarf2_find_nearest_line (abfd, sec Line 1764  _bfd_dwarf2_find_nearest_line (abfd, sec
1764            bfd_size_type size;            bfd_size_type size;
1765            bfd_size_type start;            bfd_size_type start;
1766    
1767            size = msec->_raw_size;            size = msec->size;
1768            if (size == 0)            if (size == 0)
1769              continue;              continue;
1770    
1771            start = stash->info_ptr_end - stash->info_ptr;            start = stash->info_ptr_end - stash->info_ptr;
1772    
1773            if (! bfd_get_section_contents (abfd, msec, stash->info_ptr + start,            if ((bfd_simple_get_relocated_section_contents
1774                                            (bfd_vma) 0, size))                 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1775              continue;              continue;
1776    
1777            stash->info_ptr_end = stash->info_ptr + start + size;            stash->info_ptr_end = stash->info_ptr + start + size;
# Line 1864  _bfd_dwarf2_find_nearest_line (abfd, sec Line 1784  _bfd_dwarf2_find_nearest_line (abfd, sec
1784        stash->syms = symbols;        stash->syms = symbols;
1785      }      }
1786    
   /* FIXME: There is a problem with the contents of the  
      .debug_info section.  The 'low' and 'high' addresses of the  
      comp_units are computed by relocs against symbols in the  
      .text segment.  We need these addresses in order to determine  
      the nearest line number, and so we have to resolve the  
      relocs.  There is a similar problem when the .debug_line  
      section is processed as well (e.g., there may be relocs  
      against the operand of the DW_LNE_set_address operator).  
   
      Unfortunately getting hold of the reloc information is hard...  
   
      For now, this means that disassembling object files (as  
      opposed to fully executables) does not always work as well as  
      we would like.  */  
   
1787    /* A null info_ptr indicates that there is no dwarf2 info    /* A null info_ptr indicates that there is no dwarf2 info
1788       (or that an error occured while setting up the stash).  */       (or that an error occured while setting up the stash).  */
1789    if (! stash->info_ptr)    if (! stash->info_ptr)
1790      return false;      return FALSE;
1791    
1792    /* Check the previously read comp. units first.  */    /* Check the previously read comp. units first.  */
1793    for (each = stash->all_comp_units; each; each = each->next_unit)    for (each = stash->all_comp_units; each; each = each->next_unit)
# Line 1895  _bfd_dwarf2_find_nearest_line (abfd, sec Line 1800  _bfd_dwarf2_find_nearest_line (abfd, sec
1800    while (stash->info_ptr < stash->info_ptr_end)    while (stash->info_ptr < stash->info_ptr_end)
1801      {      {
1802        bfd_vma length;        bfd_vma length;
1803        boolean found;        bfd_boolean found;
1804        unsigned int offset_size = addr_size;        unsigned int offset_size = addr_size;
1805          bfd_byte *info_ptr_unit = stash->info_ptr;
1806    
1807        if (addr_size == 4)        length = read_4_bytes (abfd, stash->info_ptr);
1808          /* A 0xffffff length is the DWARF3 way of indicating we use
1809             64-bit offsets, instead of 32-bit offsets.  */
1810          if (length == 0xffffffff)
1811            {
1812              offset_size = 8;
1813              length = read_8_bytes (abfd, stash->info_ptr + 4);
1814              stash->info_ptr += 12;
1815            }
1816          /* A zero length is the IRIX way of indicating 64-bit offsets,
1817             mostly because the 64-bit length will generally fit in 32
1818             bits, and the endianness helps.  */
1819          else if (length == 0)
1820            {
1821              offset_size = 8;
1822              length = read_4_bytes (abfd, stash->info_ptr + 4);
1823              stash->info_ptr += 8;
1824            }
1825          /* In the absence of the hints above, we assume addr_size-sized
1826             offsets, for backward-compatibility with pre-DWARF3 64-bit
1827             platforms.  */
1828          else if (addr_size == 8)
1829          {          {
1830            length = read_4_bytes (abfd, stash->info_ptr);            length = read_8_bytes (abfd, stash->info_ptr);
1831            if (length == 0xffffffff)            stash->info_ptr += 8;
             {  
               offset_size = 8;  
               length = read_8_bytes (abfd, stash->info_ptr + 4);  
               stash->info_ptr += 8;  
             }  
1832          }          }
1833        else        else
1834          length = read_8_bytes (abfd, stash->info_ptr);          stash->info_ptr += 4;
       stash->info_ptr += addr_size;  
1835    
1836        if (length > 0)        if (length > 0)
1837          {          {
1838            each = parse_comp_unit (abfd, stash, length, offset_size);            each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
1839                                      offset_size);
1840            stash->info_ptr += length;            stash->info_ptr += length;
1841    
1842            if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)            if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1843                == stash->sec->_raw_size)                == stash->sec->size)
1844              {              {
1845                stash->sec = find_debug_info (abfd, stash->sec);                stash->sec = find_debug_info (abfd, stash->sec);
1846                stash->sec_info_ptr = stash->info_ptr;                stash->sec_info_ptr = stash->info_ptr;
# Line 1938  _bfd_dwarf2_find_nearest_line (abfd, sec Line 1860  _bfd_dwarf2_find_nearest_line (abfd, sec
1860                  {                  {
1861                    if (comp_unit_contains_address (each, addr))                    if (comp_unit_contains_address (each, addr))
1862                      return comp_unit_find_nearest_line (each, addr,                      return comp_unit_find_nearest_line (each, addr,
1863                                                         filename_ptr,                                                          filename_ptr,
1864                                                         functionname_ptr,                                                          functionname_ptr,
1865                                                         linenumber_ptr,                                                          linenumber_ptr,
1866                                                         stash);                                                          stash);
1867                  }                  }
1868                else                else
1869                  {                  {
# Line 1951  _bfd_dwarf2_find_nearest_line (abfd, sec Line 1873  _bfd_dwarf2_find_nearest_line (abfd, sec
1873                                                         linenumber_ptr,                                                         linenumber_ptr,
1874                                                         stash);                                                         stash);
1875                    if (found)                    if (found)
1876                      return true;                      return TRUE;
1877                  }                  }
1878              }              }
1879          }          }
1880      }      }
1881    
1882    return false;    return FALSE;
1883  }  }

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.1.20.1

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26