Back to index

cell-binutils  2.17cvs20070401
dwarf2.c
Go to the documentation of this file.
00001 /* DWARF 2 support.
00002    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
00003    2004, 2005, 2006 Free Software Foundation, Inc.
00004 
00005    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
00006    (gavin@cygnus.com).
00007 
00008    From the dwarf2read.c header:
00009    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
00010    Inc.  with support from Florida State University (under contract
00011    with the Ada Joint Program Office), and Silicon Graphics, Inc.
00012    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
00013    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
00014    support in dwarfread.c
00015 
00016    This file is part of BFD.
00017 
00018    This program is free software; you can redistribute it and/or modify
00019    it under the terms of the GNU General Public License as published by
00020    the Free Software Foundation; either version 2 of the License, or (at
00021    your option) any later version.
00022 
00023    This program is distributed in the hope that it will be useful, but
00024    WITHOUT ANY WARRANTY; without even the implied warranty of
00025    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026    General Public License for more details.
00027 
00028    You should have received a copy of the GNU General Public License
00029    along with this program; if not, write to the Free Software
00030    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00031 
00032 #include "bfd.h"
00033 #include "sysdep.h"
00034 #include "libiberty.h"
00035 #include "libbfd.h"
00036 #include "elf-bfd.h"
00037 #include "elf/dwarf2.h"
00038 
00039 /* The data in the .debug_line statement prologue looks like this.  */
00040 
00041 struct line_head
00042 {
00043   bfd_vma total_length;
00044   unsigned short version;
00045   bfd_vma prologue_length;
00046   unsigned char minimum_instruction_length;
00047   unsigned char default_is_stmt;
00048   int line_base;
00049   unsigned char line_range;
00050   unsigned char opcode_base;
00051   unsigned char *standard_opcode_lengths;
00052 };
00053 
00054 /* Attributes have a name and a value.  */
00055 
00056 struct attribute
00057 {
00058   enum dwarf_attribute name;
00059   enum dwarf_form form;
00060   union
00061   {
00062     char *str;
00063     struct dwarf_block *blk;
00064     bfd_uint64_t val;
00065     bfd_int64_t sval;
00066   }
00067   u;
00068 };
00069 
00070 /* Blocks are a bunch of untyped bytes.  */
00071 struct dwarf_block
00072 {
00073   unsigned int size;
00074   bfd_byte *data;
00075 };
00076 
00077 struct loadable_section
00078 {
00079   asection *section;
00080   bfd_vma adj_vma;
00081 };
00082 
00083 struct dwarf2_debug
00084 {
00085   /* A list of all previously read comp_units.  */
00086   struct comp_unit *all_comp_units;
00087 
00088   /* The next unread compilation unit within the .debug_info section.
00089      Zero indicates that the .debug_info section has not been loaded
00090      into a buffer yet.  */
00091   bfd_byte *info_ptr;
00092 
00093   /* Pointer to the end of the .debug_info section memory buffer.  */
00094   bfd_byte *info_ptr_end;
00095 
00096   /* Pointer to the section and address of the beginning of the
00097      section.  */
00098   asection *sec;
00099   bfd_byte *sec_info_ptr;
00100 
00101   /* Pointer to the symbol table.  */
00102   asymbol **syms;
00103 
00104   /* Pointer to the .debug_abbrev section loaded into memory.  */
00105   bfd_byte *dwarf_abbrev_buffer;
00106 
00107   /* Length of the loaded .debug_abbrev section.  */
00108   unsigned long dwarf_abbrev_size;
00109 
00110   /* Buffer for decode_line_info.  */
00111   bfd_byte *dwarf_line_buffer;
00112 
00113   /* Length of the loaded .debug_line section.  */
00114   unsigned long dwarf_line_size;
00115 
00116   /* Pointer to the .debug_str section loaded into memory.  */
00117   bfd_byte *dwarf_str_buffer;
00118 
00119   /* Length of the loaded .debug_str section.  */
00120   unsigned long dwarf_str_size;
00121 
00122   /* Pointer to the .debug_ranges section loaded into memory. */
00123   bfd_byte *dwarf_ranges_buffer;
00124 
00125   /* Length of the loaded .debug_ranges section. */
00126   unsigned long dwarf_ranges_size;
00127 
00128   /* If the most recent call to bfd_find_nearest_line was given an
00129      address in an inlined function, preserve a pointer into the
00130      calling chain for subsequent calls to bfd_find_inliner_info to
00131      use. */
00132   struct funcinfo *inliner_chain;
00133 
00134   /* Number of loadable sections.  */
00135   unsigned int loadable_section_count;
00136 
00137   /* Array of loadable sections.  */
00138   struct loadable_section *loadable_sections;
00139 };
00140 
00141 struct arange
00142 {
00143   struct arange *next;
00144   bfd_vma low;
00145   bfd_vma high;
00146 };
00147 
00148 /* A minimal decoding of DWARF2 compilation units.  We only decode
00149    what's needed to get to the line number information.  */
00150 
00151 struct comp_unit
00152 {
00153   /* Chain the previously read compilation units.  */
00154   struct comp_unit *next_unit;
00155 
00156   /* Keep the bfd convenient (for memory allocation).  */
00157   bfd *abfd;
00158 
00159   /* The lowest and highest addresses contained in this compilation
00160      unit as specified in the compilation unit header.  */
00161   struct arange arange;
00162 
00163   /* The DW_AT_name attribute (for error messages).  */
00164   char *name;
00165 
00166   /* The abbrev hash table.  */
00167   struct abbrev_info **abbrevs;
00168 
00169   /* Note that an error was found by comp_unit_find_nearest_line.  */
00170   int error;
00171 
00172   /* The DW_AT_comp_dir attribute.  */
00173   char *comp_dir;
00174 
00175   /* TRUE if there is a line number table associated with this comp. unit.  */
00176   int stmtlist;
00177 
00178   /* Pointer to the current comp_unit so that we can find a given entry
00179      by its reference.  */
00180   bfd_byte *info_ptr_unit;
00181 
00182   /* The offset into .debug_line of the line number table.  */
00183   unsigned long line_offset;
00184 
00185   /* Pointer to the first child die for the comp unit.  */
00186   bfd_byte *first_child_die_ptr;
00187 
00188   /* The end of the comp unit.  */
00189   bfd_byte *end_ptr;
00190 
00191   /* The decoded line number, NULL if not yet decoded.  */
00192   struct line_info_table *line_table;
00193 
00194   /* A list of the functions found in this comp. unit.  */
00195   struct funcinfo *function_table;
00196 
00197   /* A list of the variables found in this comp. unit.  */
00198   struct varinfo *variable_table;
00199 
00200   /* Pointer to dwarf2_debug structure.  */
00201   struct dwarf2_debug *stash;
00202 
00203   /* Address size for this unit - from unit header.  */
00204   unsigned char addr_size;
00205 
00206   /* Offset size for this unit - from unit header.  */
00207   unsigned char offset_size;
00208 
00209   /* Base address for this unit - from DW_AT_low_pc attribute of
00210      DW_TAG_compile_unit DIE */
00211   bfd_vma base_address;
00212 };
00213 
00214 /* This data structure holds the information of an abbrev.  */
00215 struct abbrev_info
00216 {
00217   unsigned int number;             /* Number identifying abbrev.  */
00218   enum dwarf_tag tag;              /* DWARF tag.  */
00219   int has_children;         /* Boolean.  */
00220   unsigned int num_attrs;   /* Number of attributes.  */
00221   struct attr_abbrev *attrs;       /* An array of attribute descriptions.  */
00222   struct abbrev_info *next; /* Next in chain.  */
00223 };
00224 
00225 struct attr_abbrev
00226 {
00227   enum dwarf_attribute name;
00228   enum dwarf_form form;
00229 };
00230 
00231 #ifndef ABBREV_HASH_SIZE
00232 #define ABBREV_HASH_SIZE 121
00233 #endif
00234 #ifndef ATTR_ALLOC_CHUNK
00235 #define ATTR_ALLOC_CHUNK 4
00236 #endif
00237 
00238 /* VERBATIM
00239    The following function up to the END VERBATIM mark are
00240    copied directly from dwarf2read.c.  */
00241 
00242 /* Read dwarf information from a buffer.  */
00243 
00244 static unsigned int
00245 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
00246 {
00247   return bfd_get_8 (abfd, buf);
00248 }
00249 
00250 static int
00251 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
00252 {
00253   return bfd_get_signed_8 (abfd, buf);
00254 }
00255 
00256 static unsigned int
00257 read_2_bytes (bfd *abfd, bfd_byte *buf)
00258 {
00259   return bfd_get_16 (abfd, buf);
00260 }
00261 
00262 static unsigned int
00263 read_4_bytes (bfd *abfd, bfd_byte *buf)
00264 {
00265   return bfd_get_32 (abfd, buf);
00266 }
00267 
00268 static bfd_uint64_t
00269 read_8_bytes (bfd *abfd, bfd_byte *buf)
00270 {
00271   return bfd_get_64 (abfd, buf);
00272 }
00273 
00274 static bfd_byte *
00275 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
00276              bfd_byte *buf,
00277              unsigned int size ATTRIBUTE_UNUSED)
00278 {
00279   /* If the size of a host char is 8 bits, we can return a pointer
00280      to the buffer, otherwise we have to copy the data to a buffer
00281      allocated on the temporary obstack.  */
00282   return buf;
00283 }
00284 
00285 static char *
00286 read_string (bfd *abfd ATTRIBUTE_UNUSED,
00287             bfd_byte *buf,
00288             unsigned int *bytes_read_ptr)
00289 {
00290   /* Return a pointer to the embedded string.  */
00291   char *str = (char *) buf;
00292   if (*str == '\0')
00293     {
00294       *bytes_read_ptr = 1;
00295       return NULL;
00296     }
00297 
00298   *bytes_read_ptr = strlen (str) + 1;
00299   return str;
00300 }
00301 
00302 static char *
00303 read_indirect_string (struct comp_unit* unit,
00304                     bfd_byte *buf,
00305                     unsigned int *bytes_read_ptr)
00306 {
00307   bfd_uint64_t offset;
00308   struct dwarf2_debug *stash = unit->stash;
00309   char *str;
00310 
00311   if (unit->offset_size == 4)
00312     offset = read_4_bytes (unit->abfd, buf);
00313   else
00314     offset = read_8_bytes (unit->abfd, buf);
00315   *bytes_read_ptr = unit->offset_size;
00316 
00317   if (! stash->dwarf_str_buffer)
00318     {
00319       asection *msec;
00320       bfd *abfd = unit->abfd;
00321       bfd_size_type sz;
00322 
00323       msec = bfd_get_section_by_name (abfd, ".debug_str");
00324       if (! msec)
00325        {
00326          (*_bfd_error_handler)
00327            (_("Dwarf Error: Can't find .debug_str section."));
00328          bfd_set_error (bfd_error_bad_value);
00329          return NULL;
00330        }
00331 
00332       sz = msec->rawsize ? msec->rawsize : msec->size;
00333       stash->dwarf_str_size = sz;
00334       stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
00335       if (! stash->dwarf_str_buffer)
00336        return NULL;
00337 
00338       if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
00339                                   0, sz))
00340        return NULL;
00341     }
00342 
00343   if (offset >= stash->dwarf_str_size)
00344     {
00345       (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
00346                           (unsigned long) offset, stash->dwarf_str_size);
00347       bfd_set_error (bfd_error_bad_value);
00348       return NULL;
00349     }
00350 
00351   str = (char *) stash->dwarf_str_buffer + offset;
00352   if (*str == '\0')
00353     return NULL;
00354   return str;
00355 }
00356 
00357 /* END VERBATIM */
00358 
00359 static bfd_uint64_t
00360 read_address (struct comp_unit *unit, bfd_byte *buf)
00361 {
00362   int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
00363 
00364   if (signed_vma)
00365     {
00366       switch (unit->addr_size)
00367        {
00368        case 8:
00369          return bfd_get_signed_64 (unit->abfd, buf);
00370        case 4:
00371          return bfd_get_signed_32 (unit->abfd, buf);
00372        case 2:
00373          return bfd_get_signed_16 (unit->abfd, buf);
00374        default:
00375          abort ();
00376        }
00377     }
00378   else
00379     {
00380       switch (unit->addr_size)
00381        {
00382        case 8:
00383          return bfd_get_64 (unit->abfd, buf);
00384        case 4:
00385          return bfd_get_32 (unit->abfd, buf);
00386        case 2:
00387          return bfd_get_16 (unit->abfd, buf);
00388        default:
00389          abort ();
00390        }
00391     }
00392 }
00393 
00394 /* Lookup an abbrev_info structure in the abbrev hash table.  */
00395 
00396 static struct abbrev_info *
00397 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
00398 {
00399   unsigned int hash_number;
00400   struct abbrev_info *abbrev;
00401 
00402   hash_number = number % ABBREV_HASH_SIZE;
00403   abbrev = abbrevs[hash_number];
00404 
00405   while (abbrev)
00406     {
00407       if (abbrev->number == number)
00408        return abbrev;
00409       else
00410        abbrev = abbrev->next;
00411     }
00412 
00413   return NULL;
00414 }
00415 
00416 /* In DWARF version 2, the description of the debugging information is
00417    stored in a separate .debug_abbrev section.  Before we read any
00418    dies from a section we read in all abbreviations and install them
00419    in a hash table.  */
00420 
00421 static struct abbrev_info**
00422 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
00423 {
00424   struct abbrev_info **abbrevs;
00425   bfd_byte *abbrev_ptr;
00426   struct abbrev_info *cur_abbrev;
00427   unsigned int abbrev_number, bytes_read, abbrev_name;
00428   unsigned int abbrev_form, hash_number;
00429   bfd_size_type amt;
00430 
00431   if (! stash->dwarf_abbrev_buffer)
00432     {
00433       asection *msec;
00434 
00435       msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
00436       if (! msec)
00437        {
00438          (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
00439          bfd_set_error (bfd_error_bad_value);
00440          return 0;
00441        }
00442 
00443       stash->dwarf_abbrev_size = msec->size;
00444       stash->dwarf_abbrev_buffer
00445        = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
00446                                                stash->syms);
00447       if (! stash->dwarf_abbrev_buffer)
00448          return 0;
00449     }
00450 
00451   if (offset >= stash->dwarf_abbrev_size)
00452     {
00453       (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
00454                           (unsigned long) offset, stash->dwarf_abbrev_size);
00455       bfd_set_error (bfd_error_bad_value);
00456       return 0;
00457     }
00458 
00459   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
00460   abbrevs = bfd_zalloc (abfd, amt);
00461 
00462   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
00463   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
00464   abbrev_ptr += bytes_read;
00465 
00466   /* Loop until we reach an abbrev number of 0.  */
00467   while (abbrev_number)
00468     {
00469       amt = sizeof (struct abbrev_info);
00470       cur_abbrev = bfd_zalloc (abfd, amt);
00471 
00472       /* Read in abbrev header.  */
00473       cur_abbrev->number = abbrev_number;
00474       cur_abbrev->tag = (enum dwarf_tag)
00475        read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
00476       abbrev_ptr += bytes_read;
00477       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
00478       abbrev_ptr += 1;
00479 
00480       /* Now read in declarations.  */
00481       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
00482       abbrev_ptr += bytes_read;
00483       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
00484       abbrev_ptr += bytes_read;
00485 
00486       while (abbrev_name)
00487        {
00488          if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
00489            {
00490              struct attr_abbrev *tmp;
00491 
00492              amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
00493              amt *= sizeof (struct attr_abbrev);
00494              tmp = bfd_realloc (cur_abbrev->attrs, amt);
00495              if (tmp == NULL)
00496               {
00497                 size_t i;
00498 
00499                 for (i = 0; i < ABBREV_HASH_SIZE; i++)
00500                   {
00501                     struct abbrev_info *abbrev = abbrevs[i];
00502 
00503                     while (abbrev)
00504                      {
00505                        free (abbrev->attrs);
00506                        abbrev = abbrev->next;
00507                      }
00508                   }
00509                 return NULL;
00510               }
00511              cur_abbrev->attrs = tmp;
00512            }
00513 
00514          cur_abbrev->attrs[cur_abbrev->num_attrs].name
00515            = (enum dwarf_attribute) abbrev_name;
00516          cur_abbrev->attrs[cur_abbrev->num_attrs++].form
00517            = (enum dwarf_form) abbrev_form;
00518          abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
00519          abbrev_ptr += bytes_read;
00520          abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
00521          abbrev_ptr += bytes_read;
00522        }
00523 
00524       hash_number = abbrev_number % ABBREV_HASH_SIZE;
00525       cur_abbrev->next = abbrevs[hash_number];
00526       abbrevs[hash_number] = cur_abbrev;
00527 
00528       /* Get next abbreviation.
00529         Under Irix6 the abbreviations for a compilation unit are not
00530         always properly terminated with an abbrev number of 0.
00531         Exit loop if we encounter an abbreviation which we have
00532         already read (which means we are about to read the abbreviations
00533         for the next compile unit) or if the end of the abbreviation
00534         table is reached.  */
00535       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
00536          >= stash->dwarf_abbrev_size)
00537        break;
00538       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
00539       abbrev_ptr += bytes_read;
00540       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
00541        break;
00542     }
00543 
00544   return abbrevs;
00545 }
00546 
00547 /* Read an attribute value described by an attribute form.  */
00548 
00549 static bfd_byte *
00550 read_attribute_value (struct attribute *attr,
00551                     unsigned form,
00552                     struct comp_unit *unit,
00553                     bfd_byte *info_ptr)
00554 {
00555   bfd *abfd = unit->abfd;
00556   unsigned int bytes_read;
00557   struct dwarf_block *blk;
00558   bfd_size_type amt;
00559 
00560   attr->form = (enum dwarf_form) form;
00561 
00562   switch (form)
00563     {
00564     case DW_FORM_addr:
00565       /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size.  */
00566     case DW_FORM_ref_addr:
00567       attr->u.val = read_address (unit, info_ptr);
00568       info_ptr += unit->addr_size;
00569       break;
00570     case DW_FORM_block2:
00571       amt = sizeof (struct dwarf_block);
00572       blk = bfd_alloc (abfd, amt);
00573       blk->size = read_2_bytes (abfd, info_ptr);
00574       info_ptr += 2;
00575       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
00576       info_ptr += blk->size;
00577       attr->u.blk = blk;
00578       break;
00579     case DW_FORM_block4:
00580       amt = sizeof (struct dwarf_block);
00581       blk = bfd_alloc (abfd, amt);
00582       blk->size = read_4_bytes (abfd, info_ptr);
00583       info_ptr += 4;
00584       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
00585       info_ptr += blk->size;
00586       attr->u.blk = blk;
00587       break;
00588     case DW_FORM_data2:
00589       attr->u.val = read_2_bytes (abfd, info_ptr);
00590       info_ptr += 2;
00591       break;
00592     case DW_FORM_data4:
00593       attr->u.val = read_4_bytes (abfd, info_ptr);
00594       info_ptr += 4;
00595       break;
00596     case DW_FORM_data8:
00597       attr->u.val = read_8_bytes (abfd, info_ptr);
00598       info_ptr += 8;
00599       break;
00600     case DW_FORM_string:
00601       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
00602       info_ptr += bytes_read;
00603       break;
00604     case DW_FORM_strp:
00605       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
00606       info_ptr += bytes_read;
00607       break;
00608     case DW_FORM_block:
00609       amt = sizeof (struct dwarf_block);
00610       blk = bfd_alloc (abfd, amt);
00611       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
00612       info_ptr += bytes_read;
00613       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
00614       info_ptr += blk->size;
00615       attr->u.blk = blk;
00616       break;
00617     case DW_FORM_block1:
00618       amt = sizeof (struct dwarf_block);
00619       blk = bfd_alloc (abfd, amt);
00620       blk->size = read_1_byte (abfd, info_ptr);
00621       info_ptr += 1;
00622       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
00623       info_ptr += blk->size;
00624       attr->u.blk = blk;
00625       break;
00626     case DW_FORM_data1:
00627       attr->u.val = read_1_byte (abfd, info_ptr);
00628       info_ptr += 1;
00629       break;
00630     case DW_FORM_flag:
00631       attr->u.val = read_1_byte (abfd, info_ptr);
00632       info_ptr += 1;
00633       break;
00634     case DW_FORM_sdata:
00635       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
00636       info_ptr += bytes_read;
00637       break;
00638     case DW_FORM_udata:
00639       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
00640       info_ptr += bytes_read;
00641       break;
00642     case DW_FORM_ref1:
00643       attr->u.val = read_1_byte (abfd, info_ptr);
00644       info_ptr += 1;
00645       break;
00646     case DW_FORM_ref2:
00647       attr->u.val = read_2_bytes (abfd, info_ptr);
00648       info_ptr += 2;
00649       break;
00650     case DW_FORM_ref4:
00651       attr->u.val = read_4_bytes (abfd, info_ptr);
00652       info_ptr += 4;
00653       break;
00654     case DW_FORM_ref8:
00655       attr->u.val = read_8_bytes (abfd, info_ptr);
00656       info_ptr += 8;
00657       break;
00658     case DW_FORM_ref_udata:
00659       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
00660       info_ptr += bytes_read;
00661       break;
00662     case DW_FORM_indirect:
00663       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
00664       info_ptr += bytes_read;
00665       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
00666       break;
00667     default:
00668       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
00669                           form);
00670       bfd_set_error (bfd_error_bad_value);
00671     }
00672   return info_ptr;
00673 }
00674 
00675 /* Read an attribute described by an abbreviated attribute.  */
00676 
00677 static bfd_byte *
00678 read_attribute (struct attribute *attr,
00679               struct attr_abbrev *abbrev,
00680               struct comp_unit *unit,
00681               bfd_byte *info_ptr)
00682 {
00683   attr->name = abbrev->name;
00684   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
00685   return info_ptr;
00686 }
00687 
00688 /* Source line information table routines.  */
00689 
00690 #define FILE_ALLOC_CHUNK 5
00691 #define DIR_ALLOC_CHUNK 5
00692 
00693 struct line_info
00694 {
00695   struct line_info* prev_line;
00696   bfd_vma address;
00697   char *filename;
00698   unsigned int line;
00699   unsigned int column;
00700   int end_sequence;         /* End of (sequential) code sequence.  */
00701 };
00702 
00703 struct fileinfo
00704 {
00705   char *name;
00706   unsigned int dir;
00707   unsigned int time;
00708   unsigned int size;
00709 };
00710 
00711 struct line_info_table
00712 {
00713   bfd* abfd;
00714   unsigned int num_files;
00715   unsigned int num_dirs;
00716   char *comp_dir;
00717   char **dirs;
00718   struct fileinfo* files;
00719   struct line_info* last_line;  /* largest VMA */
00720   struct line_info* lcl_head;   /* local head; used in 'add_line_info' */
00721 };
00722 
00723 /* Remember some information about each function.  If the function is
00724    inlined (DW_TAG_inlined_subroutine) it may have two additional
00725    attributes, DW_AT_call_file and DW_AT_call_line, which specify the
00726    source code location where this function was inlined. */
00727 
00728 struct funcinfo
00729 {
00730   struct funcinfo *prev_func;             /* Pointer to previous function in list of all functions */
00731   struct funcinfo *caller_func;           /* Pointer to function one scope higher */
00732   char *caller_file;               /* Source location file name where caller_func inlines this func */
00733   int caller_line;                 /* Source location line number where caller_func inlines this func */
00734   char *file;                      /* Source location file name */
00735   int line;                        /* Source location line number */
00736   int tag;
00737   char *name;
00738   struct arange arange;
00739   asection *sec;                   /* Where the symbol is defined */
00740 };
00741 
00742 struct varinfo
00743 {
00744   /* Pointer to previous variable in list of all variables */
00745   struct varinfo *prev_var;
00746   /* Source location file name */
00747   char *file;
00748   /* Source location line number */
00749   int line;
00750   int tag;
00751   char *name;
00752   bfd_vma addr;
00753   /* Where the symbol is defined */
00754   asection *sec;
00755   /* Is this a stack variable? */
00756   unsigned int stack: 1;
00757 };
00758 
00759 /* Return TRUE if NEW_LINE should sort after LINE.  */
00760 
00761 static inline bfd_boolean
00762 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
00763 {
00764   return (new_line->address > line->address
00765          || (new_line->address == line->address
00766              && new_line->end_sequence < line->end_sequence));
00767 }
00768 
00769 
00770 /* Adds a new entry to the line_info list in the line_info_table, ensuring
00771    that the list is sorted.  Note that the line_info list is sorted from
00772    highest to lowest VMA (with possible duplicates); that is,
00773    line_info->prev_line always accesses an equal or smaller VMA.  */
00774 
00775 static void
00776 add_line_info (struct line_info_table *table,
00777               bfd_vma address,
00778               char *filename,
00779               unsigned int line,
00780               unsigned int column,
00781               int end_sequence)
00782 {
00783   bfd_size_type amt = sizeof (struct line_info);
00784   struct line_info* info = bfd_alloc (table->abfd, amt);
00785 
00786   /* Set member data of 'info'.  */
00787   info->address = address;
00788   info->line = line;
00789   info->column = column;
00790   info->end_sequence = end_sequence;
00791 
00792   if (filename && filename[0])
00793     {
00794       info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
00795       if (info->filename)
00796        strcpy (info->filename, filename);
00797     }
00798   else
00799     info->filename = NULL;
00800 
00801   /* Find the correct location for 'info'.  Normally we will receive
00802      new line_info data 1) in order and 2) with increasing VMAs.
00803      However some compilers break the rules (cf. decode_line_info) and
00804      so we include some heuristics for quickly finding the correct
00805      location for 'info'. In particular, these heuristics optimize for
00806      the common case in which the VMA sequence that we receive is a
00807      list of locally sorted VMAs such as
00808        p...z a...j  (where a < j < p < z)
00809 
00810      Note: table->lcl_head is used to head an *actual* or *possible*
00811      sequence within the list (such as a...j) that is not directly
00812      headed by table->last_line
00813 
00814      Note: we may receive duplicate entries from 'decode_line_info'.  */
00815 
00816   if (!table->last_line
00817       || new_line_sorts_after (info, table->last_line))
00818     {
00819       /* Normal case: add 'info' to the beginning of the list */
00820       info->prev_line = table->last_line;
00821       table->last_line = info;
00822 
00823       /* lcl_head: initialize to head a *possible* sequence at the end.  */
00824       if (!table->lcl_head)
00825        table->lcl_head = info;
00826     }
00827   else if (!new_line_sorts_after (info, table->lcl_head)
00828           && (!table->lcl_head->prev_line
00829               || new_line_sorts_after (info, table->lcl_head->prev_line)))
00830     {
00831       /* Abnormal but easy: lcl_head is the head of 'info'.  */
00832       info->prev_line = table->lcl_head->prev_line;
00833       table->lcl_head->prev_line = info;
00834     }
00835   else
00836     {
00837       /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
00838         heads for 'info'.  Reset 'lcl_head'.  */
00839       struct line_info* li2 = table->last_line; /* always non-NULL */
00840       struct line_info* li1 = li2->prev_line;
00841 
00842       while (li1)
00843        {
00844          if (!new_line_sorts_after (info, li2)
00845              && new_line_sorts_after (info, li1))
00846            break;
00847 
00848          li2 = li1; /* always non-NULL */
00849          li1 = li1->prev_line;
00850        }
00851       table->lcl_head = li2;
00852       info->prev_line = table->lcl_head->prev_line;
00853       table->lcl_head->prev_line = info;
00854     }
00855 }
00856 
00857 /* Extract a fully qualified filename from a line info table.
00858    The returned string has been malloc'ed and it is the caller's
00859    responsibility to free it.  */
00860 
00861 static char *
00862 concat_filename (struct line_info_table *table, unsigned int file)
00863 {
00864   char *filename;
00865 
00866   if (file - 1 >= table->num_files)
00867     {
00868       /* FILE == 0 means unknown.  */
00869       if (file)
00870        (*_bfd_error_handler)
00871          (_("Dwarf Error: mangled line number section (bad file number)."));
00872       return strdup ("<unknown>");
00873     }
00874 
00875   filename = table->files[file - 1].name;
00876 
00877   if (!IS_ABSOLUTE_PATH (filename))
00878     {
00879       char *dirname = NULL;
00880       char *subdirname = NULL;
00881       char *name;
00882       size_t len;
00883 
00884       if (table->files[file - 1].dir)
00885        subdirname = table->dirs[table->files[file - 1].dir - 1];
00886 
00887       if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
00888        dirname = table->comp_dir;
00889 
00890       if (!dirname)
00891        {
00892          dirname = subdirname;
00893          subdirname = NULL;
00894        }
00895 
00896       if (!dirname)
00897        return strdup (filename);
00898 
00899       len = strlen (dirname) + strlen (filename) + 2;
00900 
00901       if (subdirname)
00902        {
00903          len += strlen (subdirname) + 1;
00904          name = bfd_malloc (len);
00905          if (name)
00906            sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
00907        }
00908       else
00909        {
00910          name = bfd_malloc (len);
00911          if (name)
00912            sprintf (name, "%s/%s", dirname, filename);
00913        }
00914 
00915       return name;
00916     }
00917 
00918   return strdup (filename);
00919 }
00920 
00921 static void
00922 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
00923 {
00924   struct arange *arange;
00925 
00926   /* If the first arange is empty, use it. */
00927   if (first_arange->high == 0)
00928     {
00929       first_arange->low = low_pc;
00930       first_arange->high = high_pc;
00931       return;
00932     }
00933 
00934   /* Next see if we can cheaply extend an existing range.  */
00935   arange = first_arange;
00936   do
00937     {
00938       if (low_pc == arange->high)
00939        {
00940          arange->high = high_pc;
00941          return;
00942        }
00943       if (high_pc == arange->low)
00944        {
00945          arange->low = low_pc;
00946          return;
00947        }
00948       arange = arange->next;
00949     }
00950   while (arange);
00951 
00952   /* Need to allocate a new arange and insert it into the arange list.
00953      Order isn't significant, so just insert after the first arange. */
00954   arange = bfd_zalloc (abfd, sizeof (*arange));
00955   arange->low = low_pc;
00956   arange->high = high_pc;
00957   arange->next = first_arange->next;
00958   first_arange->next = arange;
00959 }
00960 
00961 /* Decode the line number information for UNIT.  */
00962 
00963 static struct line_info_table*
00964 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
00965 {
00966   bfd *abfd = unit->abfd;
00967   struct line_info_table* table;
00968   bfd_byte *line_ptr;
00969   bfd_byte *line_end;
00970   struct line_head lh;
00971   unsigned int i, bytes_read, offset_size;
00972   char *cur_file, *cur_dir;
00973   unsigned char op_code, extended_op, adj_opcode;
00974   bfd_size_type amt;
00975 
00976   if (! stash->dwarf_line_buffer)
00977     {
00978       asection *msec;
00979 
00980       msec = bfd_get_section_by_name (abfd, ".debug_line");
00981       if (! msec)
00982        {
00983          (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
00984          bfd_set_error (bfd_error_bad_value);
00985          return 0;
00986        }
00987 
00988       stash->dwarf_line_size = msec->size;
00989       stash->dwarf_line_buffer
00990        = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
00991                                                stash->syms);
00992       if (! stash->dwarf_line_buffer)
00993        return 0;
00994     }
00995 
00996   /* It is possible to get a bad value for the line_offset.  Validate
00997      it here so that we won't get a segfault below.  */
00998   if (unit->line_offset >= stash->dwarf_line_size)
00999     {
01000       (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
01001                           unit->line_offset, stash->dwarf_line_size);
01002       bfd_set_error (bfd_error_bad_value);
01003       return 0;
01004     }
01005 
01006   amt = sizeof (struct line_info_table);
01007   table = bfd_alloc (abfd, amt);
01008   table->abfd = abfd;
01009   table->comp_dir = unit->comp_dir;
01010 
01011   table->num_files = 0;
01012   table->files = NULL;
01013 
01014   table->num_dirs = 0;
01015   table->dirs = NULL;
01016 
01017   table->files = NULL;
01018   table->last_line = NULL;
01019   table->lcl_head = NULL;
01020 
01021   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
01022 
01023   /* Read in the prologue.  */
01024   lh.total_length = read_4_bytes (abfd, line_ptr);
01025   line_ptr += 4;
01026   offset_size = 4;
01027   if (lh.total_length == 0xffffffff)
01028     {
01029       lh.total_length = read_8_bytes (abfd, line_ptr);
01030       line_ptr += 8;
01031       offset_size = 8;
01032     }
01033   else if (lh.total_length == 0 && unit->addr_size == 8)
01034     {
01035       /* Handle (non-standard) 64-bit DWARF2 formats.  */
01036       lh.total_length = read_4_bytes (abfd, line_ptr);
01037       line_ptr += 4;
01038       offset_size = 8;
01039     }
01040   line_end = line_ptr + lh.total_length;
01041   lh.version = read_2_bytes (abfd, line_ptr);
01042   line_ptr += 2;
01043   if (offset_size == 4)
01044     lh.prologue_length = read_4_bytes (abfd, line_ptr);
01045   else
01046     lh.prologue_length = read_8_bytes (abfd, line_ptr);
01047   line_ptr += offset_size;
01048   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
01049   line_ptr += 1;
01050   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
01051   line_ptr += 1;
01052   lh.line_base = read_1_signed_byte (abfd, line_ptr);
01053   line_ptr += 1;
01054   lh.line_range = read_1_byte (abfd, line_ptr);
01055   line_ptr += 1;
01056   lh.opcode_base = read_1_byte (abfd, line_ptr);
01057   line_ptr += 1;
01058   amt = lh.opcode_base * sizeof (unsigned char);
01059   lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
01060 
01061   lh.standard_opcode_lengths[0] = 1;
01062 
01063   for (i = 1; i < lh.opcode_base; ++i)
01064     {
01065       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
01066       line_ptr += 1;
01067     }
01068 
01069   /* Read directory table.  */
01070   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
01071     {
01072       line_ptr += bytes_read;
01073 
01074       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
01075        {
01076          char **tmp;
01077 
01078          amt = table->num_dirs + DIR_ALLOC_CHUNK;
01079          amt *= sizeof (char *);
01080 
01081          tmp = bfd_realloc (table->dirs, amt);
01082          if (tmp == NULL)
01083            {
01084              free (table->dirs);
01085              return NULL;
01086            }
01087          table->dirs = tmp;
01088        }
01089 
01090       table->dirs[table->num_dirs++] = cur_dir;
01091     }
01092 
01093   line_ptr += bytes_read;
01094 
01095   /* Read file name table.  */
01096   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
01097     {
01098       line_ptr += bytes_read;
01099 
01100       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
01101        {
01102          struct fileinfo *tmp;
01103 
01104          amt = table->num_files + FILE_ALLOC_CHUNK;
01105          amt *= sizeof (struct fileinfo);
01106 
01107          tmp = bfd_realloc (table->files, amt);
01108          if (tmp == NULL)
01109            {
01110              free (table->files);
01111              free (table->dirs);
01112              return NULL;
01113            }
01114          table->files = tmp;
01115        }
01116 
01117       table->files[table->num_files].name = cur_file;
01118       table->files[table->num_files].dir =
01119        read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01120       line_ptr += bytes_read;
01121       table->files[table->num_files].time =
01122        read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01123       line_ptr += bytes_read;
01124       table->files[table->num_files].size =
01125        read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01126       line_ptr += bytes_read;
01127       table->num_files++;
01128     }
01129 
01130   line_ptr += bytes_read;
01131 
01132   /* Read the statement sequences until there's nothing left.  */
01133   while (line_ptr < line_end)
01134     {
01135       /* State machine registers.  */
01136       bfd_vma address = 0;
01137       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
01138       unsigned int line = 1;
01139       unsigned int column = 0;
01140       int is_stmt = lh.default_is_stmt;
01141       int end_sequence = 0;
01142       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
01143         compilers generate address sequences that are wildly out of
01144         order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
01145         for ia64-Linux).  Thus, to determine the low and high
01146         address, we must compare on every DW_LNS_copy, etc.  */
01147       bfd_vma low_pc  = (bfd_vma) -1;
01148       bfd_vma high_pc = 0;
01149 
01150       /* Decode the table.  */
01151       while (! end_sequence)
01152        {
01153          op_code = read_1_byte (abfd, line_ptr);
01154          line_ptr += 1;
01155 
01156          if (op_code >= lh.opcode_base)
01157            {
01158              /* Special operand.  */
01159              adj_opcode = op_code - lh.opcode_base;
01160              address += (adj_opcode / lh.line_range)
01161               * lh.minimum_instruction_length;
01162              line += lh.line_base + (adj_opcode % lh.line_range);
01163              /* Append row to matrix using current values.  */
01164              add_line_info (table, address, filename, line, column, 0);
01165              if (address < low_pc)
01166               low_pc = address;
01167              if (address > high_pc)
01168               high_pc = address;
01169            }
01170          else switch (op_code)
01171            {
01172            case DW_LNS_extended_op:
01173              /* Ignore length.  */
01174              line_ptr += 1;
01175              extended_op = read_1_byte (abfd, line_ptr);
01176              line_ptr += 1;
01177 
01178              switch (extended_op)
01179               {
01180               case DW_LNE_end_sequence:
01181                 end_sequence = 1;
01182                 add_line_info (table, address, filename, line, column,
01183                              end_sequence);
01184                 if (address < low_pc)
01185                   low_pc = address;
01186                 if (address > high_pc)
01187                   high_pc = address;
01188                 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
01189                 break;
01190               case DW_LNE_set_address:
01191                 address = read_address (unit, line_ptr);
01192                 line_ptr += unit->addr_size;
01193                 break;
01194               case DW_LNE_define_file:
01195                 cur_file = read_string (abfd, line_ptr, &bytes_read);
01196                 line_ptr += bytes_read;
01197                 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
01198                   {
01199                     struct fileinfo *tmp;
01200 
01201                     amt = table->num_files + FILE_ALLOC_CHUNK;
01202                     amt *= sizeof (struct fileinfo);
01203                     tmp = bfd_realloc (table->files, amt);
01204                     if (tmp == NULL)
01205                      {
01206                        free (table->files);
01207                        free (table->dirs);
01208                        free (filename);
01209                        return NULL;
01210                      }
01211                     table->files = tmp;
01212                   }
01213                 table->files[table->num_files].name = cur_file;
01214                 table->files[table->num_files].dir =
01215                   read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01216                 line_ptr += bytes_read;
01217                 table->files[table->num_files].time =
01218                   read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01219                 line_ptr += bytes_read;
01220                 table->files[table->num_files].size =
01221                   read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01222                 line_ptr += bytes_read;
01223                 table->num_files++;
01224                 break;
01225               default:
01226                 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
01227                 bfd_set_error (bfd_error_bad_value);
01228                 free (filename);
01229                 free (table->files);
01230                 free (table->dirs);
01231                 return NULL;
01232               }
01233              break;
01234            case DW_LNS_copy:
01235              add_line_info (table, address, filename, line, column, 0);
01236              if (address < low_pc)
01237               low_pc = address;
01238              if (address > high_pc)
01239               high_pc = address;
01240              break;
01241            case DW_LNS_advance_pc:
01242              address += lh.minimum_instruction_length
01243               * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01244              line_ptr += bytes_read;
01245              break;
01246            case DW_LNS_advance_line:
01247              line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
01248              line_ptr += bytes_read;
01249              break;
01250            case DW_LNS_set_file:
01251              {
01252               unsigned int file;
01253 
01254               /* The file and directory tables are 0
01255                  based, the references are 1 based.  */
01256               file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01257               line_ptr += bytes_read;
01258               if (filename)
01259                 free (filename);
01260               filename = concat_filename (table, file);
01261               break;
01262              }
01263            case DW_LNS_set_column:
01264              column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01265              line_ptr += bytes_read;
01266              break;
01267            case DW_LNS_negate_stmt:
01268              is_stmt = (!is_stmt);
01269              break;
01270            case DW_LNS_set_basic_block:
01271              break;
01272            case DW_LNS_const_add_pc:
01273              address += lh.minimum_instruction_length
01274                     * ((255 - lh.opcode_base) / lh.line_range);
01275              break;
01276            case DW_LNS_fixed_advance_pc:
01277              address += read_2_bytes (abfd, line_ptr);
01278              line_ptr += 2;
01279              break;
01280            default:
01281              {
01282               int i;
01283 
01284               /* Unknown standard opcode, ignore it.  */
01285               for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
01286                 {
01287                   (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
01288                   line_ptr += bytes_read;
01289                 }
01290              }
01291            }
01292        }
01293 
01294       if (filename)
01295        free (filename);
01296     }
01297 
01298   return table;
01299 }
01300 
01301 /* If ADDR is within TABLE set the output parameters and return TRUE,
01302    otherwise return FALSE.  The output parameters, FILENAME_PTR and
01303    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
01304 
01305 static bfd_boolean
01306 lookup_address_in_line_info_table (struct line_info_table *table,
01307                                bfd_vma addr,
01308                                struct funcinfo *function,
01309                                const char **filename_ptr,
01310                                unsigned int *linenumber_ptr)
01311 {
01312   /* Note: table->last_line should be a descendingly sorted list. */
01313   struct line_info* next_line = table->last_line;
01314   struct line_info* each_line = NULL;
01315   *filename_ptr = NULL;
01316 
01317   if (!next_line)
01318     return FALSE;
01319 
01320   each_line = next_line->prev_line;
01321 
01322   /* Check for large addresses */
01323   if (addr > next_line->address)
01324     each_line = NULL; /* ensure we skip over the normal case */
01325 
01326   /* Normal case: search the list; save  */
01327   while (each_line && next_line)
01328     {
01329       /* If we have an address match, save this info.  This allows us
01330         to return as good as results as possible for strange debugging
01331         info.  */
01332       bfd_boolean addr_match = FALSE;
01333       if (each_line->address <= addr && addr < next_line->address)
01334        {
01335          addr_match = TRUE;
01336 
01337          /* If this line appears to span functions, and addr is in the
01338             later function, return the first line of that function instead
01339             of the last line of the earlier one.  This check is for GCC
01340             2.95, which emits the first line number for a function late.  */
01341 
01342          if (function != NULL)
01343            {
01344              bfd_vma lowest_pc;
01345              struct arange *arange;
01346 
01347              /* Find the lowest address in the function's range list */
01348              lowest_pc = function->arange.low;
01349              for (arange = &function->arange;
01350                  arange;
01351                  arange = arange->next)
01352               {
01353                 if (function->arange.low < lowest_pc)
01354                   lowest_pc = function->arange.low;
01355               }
01356              /* Check for spanning function and set outgoing line info */
01357              if (addr >= lowest_pc
01358                 && each_line->address < lowest_pc
01359                 && next_line->address > lowest_pc)
01360               {
01361                 *filename_ptr = next_line->filename;
01362                 *linenumber_ptr = next_line->line;
01363               }
01364              else
01365               {
01366                 *filename_ptr = each_line->filename;
01367                 *linenumber_ptr = each_line->line;
01368               }
01369            }
01370          else
01371            {
01372              *filename_ptr = each_line->filename;
01373              *linenumber_ptr = each_line->line;
01374            }
01375        }
01376 
01377       if (addr_match && !each_line->end_sequence)
01378        return TRUE; /* we have definitely found what we want */
01379 
01380       next_line = each_line;
01381       each_line = each_line->prev_line;
01382     }
01383 
01384   /* At this point each_line is NULL but next_line is not.  If we found
01385      a candidate end-of-sequence point in the loop above, we can return
01386      that (compatibility with a bug in the Intel compiler); otherwise,
01387      assuming that we found the containing function for this address in
01388      this compilation unit, return the first line we have a number for
01389      (compatibility with GCC 2.95).  */
01390   if (*filename_ptr == NULL && function != NULL)
01391     {
01392       *filename_ptr = next_line->filename;
01393       *linenumber_ptr = next_line->line;
01394       return TRUE;
01395     }
01396 
01397   return FALSE;
01398 }
01399 
01400 /* Read in the .debug_ranges section for future reference */
01401 
01402 static bfd_boolean
01403 read_debug_ranges (struct comp_unit *unit)
01404 {
01405   struct dwarf2_debug *stash = unit->stash;
01406   if (! stash->dwarf_ranges_buffer)
01407     {
01408       bfd *abfd = unit->abfd;
01409       asection *msec;
01410 
01411       msec = bfd_get_section_by_name (abfd, ".debug_ranges");
01412       if (! msec)
01413        {
01414          (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
01415          bfd_set_error (bfd_error_bad_value);
01416          return FALSE;
01417        }
01418 
01419       stash->dwarf_ranges_size = msec->size;
01420       stash->dwarf_ranges_buffer
01421        = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
01422                                                stash->syms);
01423       if (! stash->dwarf_ranges_buffer)
01424        return FALSE;
01425     }
01426   return TRUE;
01427 }
01428 
01429 /* Function table functions.  */
01430 
01431 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
01432    Note that we need to find the function that has the smallest
01433    range that contains ADDR, to handle inlined functions without
01434    depending upon them being ordered in TABLE by increasing range. */
01435 
01436 static bfd_boolean
01437 lookup_address_in_function_table (struct comp_unit *unit,
01438                               bfd_vma addr,
01439                               struct funcinfo **function_ptr,
01440                               const char **functionname_ptr)
01441 {
01442   struct funcinfo* each_func;
01443   struct funcinfo* best_fit = NULL;
01444   struct arange *arange;
01445 
01446   for (each_func = unit->function_table;
01447        each_func;
01448        each_func = each_func->prev_func)
01449     {
01450       for (arange = &each_func->arange;
01451           arange;
01452           arange = arange->next)
01453        {
01454          if (addr >= arange->low && addr < arange->high)
01455            {
01456              if (!best_fit ||
01457                 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
01458               best_fit = each_func;
01459            }
01460        }
01461     }
01462 
01463   if (best_fit)
01464     {
01465       *functionname_ptr = best_fit->name;
01466       *function_ptr = best_fit;
01467       return TRUE;
01468     }
01469   else
01470     {
01471       return FALSE;
01472     }
01473 }
01474 
01475 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
01476    and LINENUMBER_PTR, and return TRUE.  */
01477 
01478 static bfd_boolean
01479 lookup_symbol_in_function_table (struct comp_unit *unit,
01480                              asymbol *sym,
01481                              bfd_vma addr,
01482                              const char **filename_ptr,
01483                              unsigned int *linenumber_ptr)
01484 {
01485   struct funcinfo* each_func;
01486   struct funcinfo* best_fit = NULL;
01487   struct arange *arange;
01488   const char *name = bfd_asymbol_name (sym);
01489   asection *sec = bfd_get_section (sym);
01490 
01491   for (each_func = unit->function_table;
01492        each_func;
01493        each_func = each_func->prev_func)
01494     {
01495       for (arange = &each_func->arange;
01496           arange;
01497           arange = arange->next)
01498        {
01499          if ((!each_func->sec || each_func->sec == sec)
01500              && addr >= arange->low
01501              && addr < arange->high
01502              && each_func->name
01503              && strcmp (name, each_func->name) == 0
01504              && (!best_fit
01505                 || ((arange->high - arange->low)
01506                     < (best_fit->arange.high - best_fit->arange.low))))
01507            best_fit = each_func;
01508        }
01509     }
01510 
01511   if (best_fit)
01512     {
01513       best_fit->sec = sec;
01514       *filename_ptr = best_fit->file;
01515       *linenumber_ptr = best_fit->line;
01516       return TRUE;
01517     }
01518   else
01519     return FALSE;
01520 }
01521 
01522 /* Variable table functions.  */
01523 
01524 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
01525    LINENUMBER_PTR, and return TRUE.  */
01526 
01527 static bfd_boolean
01528 lookup_symbol_in_variable_table (struct comp_unit *unit,
01529                              asymbol *sym,
01530                              bfd_vma addr,
01531                              const char **filename_ptr,
01532                              unsigned int *linenumber_ptr)
01533 {
01534   const char *name = bfd_asymbol_name (sym);
01535   asection *sec = bfd_get_section (sym);
01536   struct varinfo* each;
01537 
01538   for (each = unit->variable_table; each; each = each->prev_var)
01539     if (each->stack == 0
01540        && each->file != NULL
01541        && each->name != NULL
01542        && each->addr == addr
01543        && (!each->sec || each->sec == sec)
01544        && strcmp (name, each->name) == 0)
01545       break;
01546 
01547   if (each)
01548     {
01549       each->sec = sec;
01550       *filename_ptr = each->file;
01551       *linenumber_ptr = each->line;
01552       return TRUE;
01553     }
01554   else
01555     return FALSE;
01556 }
01557 
01558 static char *
01559 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
01560 {
01561   bfd *abfd = unit->abfd;
01562   bfd_byte *info_ptr;
01563   unsigned int abbrev_number, bytes_read, i;
01564   struct abbrev_info *abbrev;
01565   struct attribute attr;
01566   char *name = 0;
01567 
01568   info_ptr = unit->info_ptr_unit + die_ref;
01569   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
01570   info_ptr += bytes_read;
01571 
01572   if (abbrev_number)
01573     {
01574       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
01575       if (! abbrev)
01576        {
01577          (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
01578                              abbrev_number);
01579          bfd_set_error (bfd_error_bad_value);
01580        }
01581       else
01582        {
01583          for (i = 0; i < abbrev->num_attrs; ++i)
01584            {
01585              info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
01586              switch (attr.name)
01587               {
01588               case DW_AT_name:
01589                 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
01590                 if (name == NULL)
01591                   name = attr.u.str;
01592                 break;
01593               case DW_AT_specification:
01594                 name = find_abstract_instance_name (unit, attr.u.val);
01595                 break;
01596               case DW_AT_MIPS_linkage_name:
01597                 name = attr.u.str;
01598                 break;
01599               default:
01600                 break;
01601               }
01602            }
01603        }
01604     }
01605   return (name);
01606 }
01607 
01608 static void
01609 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
01610 {
01611   bfd_byte *ranges_ptr;
01612   bfd_vma base_address = unit->base_address;
01613 
01614   if (! unit->stash->dwarf_ranges_buffer)
01615     {
01616       if (! read_debug_ranges (unit))
01617        return;
01618     }
01619   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
01620 
01621   for (;;)
01622     {
01623       bfd_vma low_pc;
01624       bfd_vma high_pc;
01625 
01626       if (unit->addr_size == 4)
01627        {
01628          low_pc = read_4_bytes (unit->abfd, ranges_ptr);
01629          ranges_ptr += 4;
01630          high_pc = read_4_bytes (unit->abfd, ranges_ptr);
01631          ranges_ptr += 4;
01632        }
01633       else
01634        {
01635          low_pc = read_8_bytes (unit->abfd, ranges_ptr);
01636          ranges_ptr += 8;
01637          high_pc = read_8_bytes (unit->abfd, ranges_ptr);
01638          ranges_ptr += 8;
01639        }
01640       if (low_pc == 0 && high_pc == 0)
01641        break;
01642       if (low_pc == -1UL && high_pc != -1UL)
01643        base_address = high_pc;
01644       else
01645        arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
01646     }
01647 }
01648 
01649 /* DWARF2 Compilation unit functions.  */
01650 
01651 /* Scan over each die in a comp. unit looking for functions to add
01652    to the function table and variables to the variable table.  */
01653 
01654 static bfd_boolean
01655 scan_unit_for_symbols (struct comp_unit *unit)
01656 {
01657   bfd *abfd = unit->abfd;
01658   bfd_byte *info_ptr = unit->first_child_die_ptr;
01659   int nesting_level = 1;
01660   struct funcinfo **nested_funcs;
01661   int nested_funcs_size;
01662 
01663   /* Maintain a stack of in-scope functions and inlined functions, which we
01664      can use to set the caller_func field.  */
01665   nested_funcs_size = 32;
01666   nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
01667   if (nested_funcs == NULL)
01668     return FALSE;
01669   nested_funcs[nesting_level] = 0;
01670 
01671   while (nesting_level)
01672     {
01673       unsigned int abbrev_number, bytes_read, i;
01674       struct abbrev_info *abbrev;
01675       struct attribute attr;
01676       struct funcinfo *func;
01677       struct varinfo *var;
01678       bfd_vma low_pc = 0;
01679       bfd_vma high_pc = 0;
01680 
01681       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
01682       info_ptr += bytes_read;
01683 
01684       if (! abbrev_number)
01685        {
01686          nesting_level--;
01687          continue;
01688        }
01689 
01690       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
01691       if (! abbrev)
01692        {
01693          (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
01694                           abbrev_number);
01695          bfd_set_error (bfd_error_bad_value);
01696          free (nested_funcs);
01697          return FALSE;
01698        }
01699 
01700       var = NULL;
01701       if (abbrev->tag == DW_TAG_subprogram
01702          || abbrev->tag == DW_TAG_entry_point
01703          || abbrev->tag == DW_TAG_inlined_subroutine)
01704        {
01705          bfd_size_type amt = sizeof (struct funcinfo);
01706          func = bfd_zalloc (abfd, amt);
01707          func->tag = abbrev->tag;
01708          func->prev_func = unit->function_table;
01709          unit->function_table = func;
01710 
01711          if (func->tag == DW_TAG_inlined_subroutine)
01712            for (i = nesting_level - 1; i >= 1; i--)
01713              if (nested_funcs[i])
01714               {
01715                 func->caller_func = nested_funcs[i];
01716                 break;
01717               }
01718          nested_funcs[nesting_level] = func;
01719        }
01720       else
01721        {
01722          func = NULL;
01723          if (abbrev->tag == DW_TAG_variable)
01724            {
01725              bfd_size_type amt = sizeof (struct varinfo);
01726              var = bfd_zalloc (abfd, amt);
01727              var->tag = abbrev->tag;
01728              var->stack = 1;
01729              var->prev_var = unit->variable_table;
01730              unit->variable_table = var;
01731            }
01732 
01733          /* No inline function in scope at this nesting level.  */
01734          nested_funcs[nesting_level] = 0;
01735        }
01736 
01737       for (i = 0; i < abbrev->num_attrs; ++i)
01738        {
01739          info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
01740 
01741          if (func)
01742            {
01743              switch (attr.name)
01744               {
01745               case DW_AT_call_file:
01746                 func->caller_file = concat_filename (unit->line_table, attr.u.val);
01747                 break;
01748 
01749               case DW_AT_call_line:
01750                 func->caller_line = attr.u.val;
01751                 break;
01752 
01753               case DW_AT_abstract_origin:
01754                 func->name = find_abstract_instance_name (unit, attr.u.val);
01755                 break;
01756 
01757               case DW_AT_name:
01758                 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
01759                 if (func->name == NULL)
01760                   func->name = attr.u.str;
01761                 break;
01762 
01763               case DW_AT_MIPS_linkage_name:
01764                 func->name = attr.u.str;
01765                 break;
01766 
01767               case DW_AT_low_pc:
01768                 low_pc = attr.u.val;
01769                 break;
01770 
01771               case DW_AT_high_pc:
01772                 high_pc = attr.u.val;
01773                 break;
01774 
01775               case DW_AT_ranges:
01776                 read_rangelist (unit, &func->arange, attr.u.val);
01777                 break;
01778 
01779               case DW_AT_decl_file:
01780                 func->file = concat_filename (unit->line_table,
01781                                           attr.u.val);
01782                 break;
01783 
01784               case DW_AT_decl_line:
01785                 func->line = attr.u.val;
01786                 break;
01787 
01788               default:
01789                 break;
01790               }
01791            }
01792          else if (var)
01793            {
01794              switch (attr.name)
01795               {
01796               case DW_AT_name:
01797                 var->name = attr.u.str;
01798                 break;
01799 
01800               case DW_AT_decl_file:
01801                 var->file = concat_filename (unit->line_table,
01802                                           attr.u.val);
01803                 break;
01804 
01805               case DW_AT_decl_line:
01806                 var->line = attr.u.val;
01807                 break;
01808 
01809               case DW_AT_external:
01810                 if (attr.u.val != 0)
01811                   var->stack = 0;
01812                 break;
01813 
01814               case DW_AT_location:
01815                 switch (attr.form)
01816                   {
01817                   case DW_FORM_block:
01818                   case DW_FORM_block1:
01819                   case DW_FORM_block2:
01820                   case DW_FORM_block4:
01821                     if (*attr.u.blk->data == DW_OP_addr)
01822                      {
01823                        var->stack = 0;
01824 
01825                        /* Verify that DW_OP_addr is the only opcode in the
01826                           location, in which case the block size will be 1
01827                           plus the address size.  */
01828                        /* ??? For TLS variables, gcc can emit
01829                           DW_OP_addr <addr> DW_OP_GNU_push_tls_address
01830                           which we don't handle here yet.  */
01831                        if (attr.u.blk->size == unit->addr_size + 1U)
01832                          var->addr = bfd_get (unit->addr_size * 8,
01833                                            unit->abfd,
01834                                            attr.u.blk->data + 1);
01835                      }
01836                     break;
01837 
01838                   default:
01839                     break;
01840                   }
01841                 break;
01842 
01843               default:
01844                 break;
01845               }
01846            }
01847        }
01848 
01849       if (func && high_pc != 0)
01850        {
01851          arange_add (unit->abfd, &func->arange, low_pc, high_pc);
01852        }
01853 
01854       if (abbrev->has_children)
01855        {
01856          nesting_level++;
01857 
01858          if (nesting_level >= nested_funcs_size)
01859            {
01860              struct funcinfo **tmp;
01861 
01862              nested_funcs_size *= 2;
01863              tmp = bfd_realloc (nested_funcs,
01864                              (nested_funcs_size
01865                               * sizeof (struct funcinfo *)));
01866              if (tmp == NULL)
01867               {
01868                 free (nested_funcs);
01869                 return FALSE;
01870               }
01871              nested_funcs = tmp;
01872            }
01873          nested_funcs[nesting_level] = 0;
01874        }
01875     }
01876 
01877   free (nested_funcs);
01878   return TRUE;
01879 }
01880 
01881 /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
01882    includes the compilation unit header that proceeds the DIE's, but
01883    does not include the length field that precedes each compilation
01884    unit header.  END_PTR points one past the end of this comp unit.
01885    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
01886 
01887    This routine does not read the whole compilation unit; only enough
01888    to get to the line number information for the compilation unit.  */
01889 
01890 static struct comp_unit *
01891 parse_comp_unit (bfd *abfd,
01892                struct dwarf2_debug *stash,
01893                bfd_vma unit_length,
01894                bfd_byte *info_ptr_unit,
01895                unsigned int offset_size)
01896 {
01897   struct comp_unit* unit;
01898   unsigned int version;
01899   bfd_uint64_t abbrev_offset = 0;
01900   unsigned int addr_size;
01901   struct abbrev_info** abbrevs;
01902   unsigned int abbrev_number, bytes_read, i;
01903   struct abbrev_info *abbrev;
01904   struct attribute attr;
01905   bfd_byte *info_ptr = stash->info_ptr;
01906   bfd_byte *end_ptr = info_ptr + unit_length;
01907   bfd_size_type amt;
01908   bfd_vma low_pc = 0;
01909   bfd_vma high_pc = 0;
01910 
01911   version = read_2_bytes (abfd, info_ptr);
01912   info_ptr += 2;
01913   BFD_ASSERT (offset_size == 4 || offset_size == 8);
01914   if (offset_size == 4)
01915     abbrev_offset = read_4_bytes (abfd, info_ptr);
01916   else
01917     abbrev_offset = read_8_bytes (abfd, info_ptr);
01918   info_ptr += offset_size;
01919   addr_size = read_1_byte (abfd, info_ptr);
01920   info_ptr += 1;
01921 
01922   if (version != 2)
01923     {
01924       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
01925       bfd_set_error (bfd_error_bad_value);
01926       return 0;
01927     }
01928 
01929   if (addr_size > sizeof (bfd_vma))
01930     {
01931       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
01932                       addr_size,
01933                       (unsigned int) sizeof (bfd_vma));
01934       bfd_set_error (bfd_error_bad_value);
01935       return 0;
01936     }
01937 
01938   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
01939     {
01940       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
01941       bfd_set_error (bfd_error_bad_value);
01942       return 0;
01943     }
01944 
01945   /* Read the abbrevs for this compilation unit into a table.  */
01946   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
01947   if (! abbrevs)
01948       return 0;
01949 
01950   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
01951   info_ptr += bytes_read;
01952   if (! abbrev_number)
01953     {
01954       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
01955                       abbrev_number);
01956       bfd_set_error (bfd_error_bad_value);
01957       return 0;
01958     }
01959 
01960   abbrev = lookup_abbrev (abbrev_number, abbrevs);
01961   if (! abbrev)
01962     {
01963       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
01964                       abbrev_number);
01965       bfd_set_error (bfd_error_bad_value);
01966       return 0;
01967     }
01968 
01969   amt = sizeof (struct comp_unit);
01970   unit = bfd_zalloc (abfd, amt);
01971   unit->abfd = abfd;
01972   unit->addr_size = addr_size;
01973   unit->offset_size = offset_size;
01974   unit->abbrevs = abbrevs;
01975   unit->end_ptr = end_ptr;
01976   unit->stash = stash;
01977   unit->info_ptr_unit = info_ptr_unit;
01978 
01979   for (i = 0; i < abbrev->num_attrs; ++i)
01980     {
01981       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
01982 
01983       /* Store the data if it is of an attribute we want to keep in a
01984         partial symbol table.  */
01985       switch (attr.name)
01986        {
01987        case DW_AT_stmt_list:
01988          unit->stmtlist = 1;
01989          unit->line_offset = attr.u.val;
01990          break;
01991 
01992        case DW_AT_name:
01993          unit->name = attr.u.str;
01994          break;
01995 
01996        case DW_AT_low_pc:
01997          low_pc = attr.u.val;
01998          /* If the compilation unit DIE has a DW_AT_low_pc attribute,
01999             this is the base address to use when reading location
02000             lists or range lists. */
02001          unit->base_address = low_pc;
02002          break;
02003 
02004        case DW_AT_high_pc:
02005          high_pc = attr.u.val;
02006          break;
02007 
02008        case DW_AT_ranges:
02009          read_rangelist (unit, &unit->arange, attr.u.val);
02010          break;
02011 
02012        case DW_AT_comp_dir:
02013          {
02014            char *comp_dir = attr.u.str;
02015            if (comp_dir)
02016              {
02017               /* Irix 6.2 native cc prepends <machine>.: to the compilation
02018                  directory, get rid of it.  */
02019               char *cp = strchr (comp_dir, ':');
02020 
02021               if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
02022                 comp_dir = cp + 1;
02023              }
02024            unit->comp_dir = comp_dir;
02025            break;
02026          }
02027 
02028        default:
02029          break;
02030        }
02031     }
02032   if (high_pc != 0)
02033     {
02034       arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
02035     }
02036 
02037   unit->first_child_die_ptr = info_ptr;
02038   return unit;
02039 }
02040 
02041 /* Return TRUE if UNIT may contain the address given by ADDR.  When
02042    there are functions written entirely with inline asm statements, the
02043    range info in the compilation unit header may not be correct.  We
02044    need to consult the line info table to see if a compilation unit
02045    really contains the given address.  */
02046 
02047 static bfd_boolean
02048 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
02049 {
02050   struct arange *arange;
02051 
02052   if (unit->error)
02053     return FALSE;
02054 
02055   arange = &unit->arange;
02056   do
02057     {
02058       if (addr >= arange->low && addr < arange->high)
02059        return TRUE;
02060       arange = arange->next;
02061     }
02062   while (arange);
02063 
02064   return FALSE;
02065 }
02066 
02067 /* If UNIT contains ADDR, set the output parameters to the values for
02068    the line containing ADDR.  The output parameters, FILENAME_PTR,
02069    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
02070    to be filled in.
02071 
02072    Return TRUE if UNIT contains ADDR, and no errors were encountered;
02073    FALSE otherwise.  */
02074 
02075 static bfd_boolean
02076 comp_unit_find_nearest_line (struct comp_unit *unit,
02077                           bfd_vma addr,
02078                           const char **filename_ptr,
02079                           const char **functionname_ptr,
02080                           unsigned int *linenumber_ptr,
02081                           struct dwarf2_debug *stash)
02082 {
02083   bfd_boolean line_p;
02084   bfd_boolean func_p;
02085   struct funcinfo *function;
02086 
02087   if (unit->error)
02088     return FALSE;
02089 
02090   if (! unit->line_table)
02091     {
02092       if (! unit->stmtlist)
02093        {
02094          unit->error = 1;
02095          return FALSE;
02096        }
02097 
02098       unit->line_table = decode_line_info (unit, stash);
02099 
02100       if (! unit->line_table)
02101        {
02102          unit->error = 1;
02103          return FALSE;
02104        }
02105 
02106       if (unit->first_child_die_ptr < unit->end_ptr
02107          && ! scan_unit_for_symbols (unit))
02108        {
02109          unit->error = 1;
02110          return FALSE;
02111        }
02112     }
02113 
02114   function = NULL;
02115   func_p = lookup_address_in_function_table (unit, addr,
02116                                         &function, functionname_ptr);
02117   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
02118     stash->inliner_chain = function;
02119   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
02120                                          function, filename_ptr,
02121                                          linenumber_ptr);
02122   return line_p || func_p;
02123 }
02124 
02125 /* If UNIT contains SYM at ADDR, set the output parameters to the
02126    values for the line containing SYM.  The output parameters,
02127    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
02128    filled in.
02129 
02130    Return TRUE if UNIT contains SYM, and no errors were encountered;
02131    FALSE otherwise.  */
02132 
02133 static bfd_boolean
02134 comp_unit_find_line (struct comp_unit *unit,
02135                    asymbol *sym,
02136                    bfd_vma addr,
02137                    const char **filename_ptr,
02138                    unsigned int *linenumber_ptr,
02139                    struct dwarf2_debug *stash)
02140 {
02141   if (unit->error)
02142     return FALSE;
02143 
02144   if (! unit->line_table)
02145     {
02146       if (! unit->stmtlist)
02147        {
02148          unit->error = 1;
02149          return FALSE;
02150        }
02151 
02152       unit->line_table = decode_line_info (unit, stash);
02153 
02154       if (! unit->line_table)
02155        {
02156          unit->error = 1;
02157          return FALSE;
02158        }
02159 
02160       if (unit->first_child_die_ptr < unit->end_ptr
02161          && ! scan_unit_for_symbols (unit))
02162        {
02163          unit->error = 1;
02164          return FALSE;
02165        }
02166     }
02167 
02168   if (sym->flags & BSF_FUNCTION)
02169     return lookup_symbol_in_function_table (unit, sym, addr,
02170                                        filename_ptr,
02171                                        linenumber_ptr);
02172   else
02173     return lookup_symbol_in_variable_table (unit, sym, addr,
02174                                        filename_ptr,
02175                                        linenumber_ptr);
02176 }
02177 
02178 /* Locate a section in a BFD containing debugging info.  The search starts
02179    from the section after AFTER_SEC, or from the first section in the BFD if
02180    AFTER_SEC is NULL.  The search works by examining the names of the
02181    sections.  There are two permissiable names.  The first is .debug_info.
02182    This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
02183    This is a variation on the .debug_info section which has a checksum
02184    describing the contents appended onto the name.  This allows the linker to
02185    identify and discard duplicate debugging sections for different
02186    compilation units.  */
02187 #define DWARF2_DEBUG_INFO ".debug_info"
02188 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
02189 
02190 static asection *
02191 find_debug_info (bfd *abfd, asection *after_sec)
02192 {
02193   asection * msec;
02194 
02195   if (after_sec)
02196     msec = after_sec->next;
02197   else
02198     msec = abfd->sections;
02199 
02200   while (msec)
02201     {
02202       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
02203        return msec;
02204 
02205       if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
02206        return msec;
02207 
02208       msec = msec->next;
02209     }
02210 
02211   return NULL;
02212 }
02213 
02214 /* Unset vmas for loadable sections in STASH.  */
02215 
02216 static void
02217 unset_sections (struct dwarf2_debug *stash)
02218 {
02219   unsigned int i;
02220   struct loadable_section *p;
02221 
02222   i = stash->loadable_section_count;
02223   p = stash->loadable_sections;
02224   for (; i > 0; i--, p++)
02225     p->section->vma = 0;
02226 }
02227 
02228 /* Set unique vmas for loadable sections in ABFD and save vmas in
02229    STASH for unset_sections.  */
02230 
02231 static bfd_boolean
02232 place_sections (bfd *abfd, struct dwarf2_debug *stash)
02233 {
02234   struct loadable_section *p;
02235   unsigned int i;
02236 
02237   if (stash->loadable_section_count != 0)
02238     {
02239       i = stash->loadable_section_count;
02240       p = stash->loadable_sections;
02241       for (; i > 0; i--, p++)
02242        p->section->vma = p->adj_vma;
02243     }
02244   else
02245     {
02246       asection *sect;
02247       bfd_vma last_vma = 0;
02248       bfd_size_type amt;
02249       struct loadable_section *p;
02250 
02251       i = 0;
02252       for (sect = abfd->sections; sect != NULL; sect = sect->next)
02253        {
02254          bfd_size_type sz;
02255 
02256          if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
02257            continue;
02258 
02259          sz = sect->rawsize ? sect->rawsize : sect->size;
02260          if (sz == 0)
02261            continue;
02262 
02263          i++;
02264        }
02265 
02266       amt = i * sizeof (struct loadable_section);
02267       p = (struct loadable_section *) bfd_zalloc (abfd, amt);
02268       if (! p)
02269        return FALSE;
02270 
02271       stash->loadable_sections = p;
02272       stash->loadable_section_count = i;
02273 
02274       for (sect = abfd->sections; sect != NULL; sect = sect->next)
02275        {
02276          bfd_size_type sz;
02277 
02278          if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
02279            continue;
02280 
02281          sz = sect->rawsize ? sect->rawsize : sect->size;
02282          if (sz == 0)
02283            continue;
02284 
02285          p->section = sect;
02286          if (last_vma != 0)
02287            {
02288              /* Align the new address to the current section
02289                alignment.  */
02290              last_vma = ((last_vma
02291                         + ~((bfd_vma) -1 << sect->alignment_power))
02292                        & ((bfd_vma) -1 << sect->alignment_power));
02293              sect->vma = last_vma;
02294            }
02295          p->adj_vma = sect->vma;
02296          last_vma += sect->vma + sz;
02297 
02298          p++;
02299        }
02300     }
02301 
02302   return TRUE;
02303 }
02304 
02305 /* The DWARF2 version of find_nearest_line.  Return TRUE if the line
02306    is found without error.  ADDR_SIZE is the number of bytes in the
02307    initial .debug_info length field and in the abbreviation offset.
02308    You may use zero to indicate that the default value should be
02309    used.  */
02310 
02311 bfd_boolean
02312 _bfd_dwarf2_find_nearest_line (bfd *abfd,
02313                             asection *section,
02314                             asymbol **symbols,
02315                             bfd_vma offset,
02316                             const char **filename_ptr,
02317                             const char **functionname_ptr,
02318                             unsigned int *linenumber_ptr,
02319                             unsigned int addr_size,
02320                             void **pinfo)
02321 {
02322   /* Read each compilation unit from the section .debug_info, and check
02323      to see if it contains the address we are searching for.  If yes,
02324      lookup the address, and return the line number info.  If no, go
02325      on to the next compilation unit.
02326 
02327      We keep a list of all the previously read compilation units, and
02328      a pointer to the next un-read compilation unit.  Check the
02329      previously read units before reading more.  */
02330   struct dwarf2_debug *stash;
02331 
02332   /* What address are we looking for?  */
02333   bfd_vma addr;
02334 
02335   struct comp_unit* each;
02336 
02337   bfd_vma found = FALSE;
02338 
02339   stash = *pinfo;
02340 
02341   if (! stash)
02342     {
02343       bfd_size_type amt = sizeof (struct dwarf2_debug);
02344 
02345       stash = bfd_zalloc (abfd, amt);
02346       if (! stash)
02347        return FALSE;
02348     }
02349 
02350   /* In a relocatable file, 2 functions may have the same address.
02351      We change the section vma so that they won't overlap.  */
02352   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
02353     {
02354       if (! place_sections (abfd, stash))
02355        return FALSE;
02356     }
02357 
02358   addr = offset;
02359   if (section->output_section)
02360     addr += section->output_section->vma + section->output_offset;
02361   else
02362     addr += section->vma;
02363   *filename_ptr = NULL;
02364   *functionname_ptr = NULL;
02365   *linenumber_ptr = 0;
02366 
02367   /* The DWARF2 spec says that the initial length field, and the
02368      offset of the abbreviation table, should both be 4-byte values.
02369      However, some compilers do things differently.  */
02370   if (addr_size == 0)
02371     addr_size = 4;
02372   BFD_ASSERT (addr_size == 4 || addr_size == 8);
02373 
02374   if (! *pinfo)
02375     {
02376       bfd_size_type total_size;
02377       asection *msec;
02378 
02379       *pinfo = stash;
02380 
02381       msec = find_debug_info (abfd, NULL);
02382       if (! msec)
02383        /* No dwarf2 info.  Note that at this point the stash
02384           has been allocated, but contains zeros, this lets
02385           future calls to this function fail quicker.  */
02386        goto done;
02387 
02388       /* There can be more than one DWARF2 info section in a BFD these days.
02389         Read them all in and produce one large stash.  We do this in two
02390         passes - in the first pass we just accumulate the section sizes.
02391         In the second pass we read in the section's contents.  The allows
02392         us to avoid reallocing the data as we add sections to the stash.  */
02393       for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
02394        total_size += msec->size;
02395 
02396       stash->info_ptr = bfd_alloc (abfd, total_size);
02397       if (stash->info_ptr == NULL)
02398        goto done;
02399 
02400       stash->info_ptr_end = stash->info_ptr;
02401 
02402       for (msec = find_debug_info (abfd, NULL);
02403           msec;
02404           msec = find_debug_info (abfd, msec))
02405        {
02406          bfd_size_type size;
02407          bfd_size_type start;
02408 
02409          size = msec->size;
02410          if (size == 0)
02411            continue;
02412 
02413          start = stash->info_ptr_end - stash->info_ptr;
02414 
02415          if ((bfd_simple_get_relocated_section_contents
02416               (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
02417            continue;
02418 
02419          stash->info_ptr_end = stash->info_ptr + start + size;
02420        }
02421 
02422       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
02423 
02424       stash->sec = find_debug_info (abfd, NULL);
02425       stash->sec_info_ptr = stash->info_ptr;
02426       stash->syms = symbols;
02427     }
02428 
02429   /* A null info_ptr indicates that there is no dwarf2 info
02430      (or that an error occured while setting up the stash).  */
02431   if (! stash->info_ptr)
02432     goto done;
02433 
02434   stash->inliner_chain = NULL;
02435 
02436   /* Check the previously read comp. units first.  */
02437   for (each = stash->all_comp_units; each; each = each->next_unit)
02438     if (comp_unit_contains_address (each, addr)
02439        && comp_unit_find_nearest_line (each, addr, filename_ptr,
02440                                    functionname_ptr,
02441                                    linenumber_ptr, stash))
02442       {
02443        found = TRUE;
02444        goto done;
02445       }
02446 
02447   /* Read each remaining comp. units checking each as they are read.  */
02448   while (stash->info_ptr < stash->info_ptr_end)
02449     {
02450       bfd_vma length;
02451       unsigned int offset_size = addr_size;
02452       bfd_byte *info_ptr_unit = stash->info_ptr;
02453 
02454       length = read_4_bytes (abfd, stash->info_ptr);
02455       /* A 0xffffff length is the DWARF3 way of indicating we use
02456         64-bit offsets, instead of 32-bit offsets.  */
02457       if (length == 0xffffffff)
02458        {
02459          offset_size = 8;
02460          length = read_8_bytes (abfd, stash->info_ptr + 4);
02461          stash->info_ptr += 12;
02462        }
02463       /* A zero length is the IRIX way of indicating 64-bit offsets,
02464         mostly because the 64-bit length will generally fit in 32
02465         bits, and the endianness helps.  */
02466       else if (length == 0)
02467        {
02468          offset_size = 8;
02469          length = read_4_bytes (abfd, stash->info_ptr + 4);
02470          stash->info_ptr += 8;
02471        }
02472       /* In the absence of the hints above, we assume 32-bit DWARF2
02473         offsets even for targets with 64-bit addresses, because:
02474           a) most of the time these targets will not have generated
02475              more than 2Gb of debug info and so will not need 64-bit
02476              offsets,
02477         and
02478           b) if they do use 64-bit offsets but they are not using
02479              the size hints that are tested for above then they are
02480              not conforming to the DWARF3 standard anyway.  */
02481       else if (addr_size == 8)
02482        {
02483          offset_size = 4;
02484           stash->info_ptr += 4;
02485        }
02486       else
02487        stash->info_ptr += 4;
02488 
02489       if (length > 0)
02490        {
02491          each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
02492                               offset_size);
02493          stash->info_ptr += length;
02494 
02495          if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
02496              == stash->sec->size)
02497            {
02498              stash->sec = find_debug_info (abfd, stash->sec);
02499              stash->sec_info_ptr = stash->info_ptr;
02500            }
02501 
02502          if (each)
02503            {
02504              each->next_unit = stash->all_comp_units;
02505              stash->all_comp_units = each;
02506 
02507              /* DW_AT_low_pc and DW_AT_high_pc are optional for
02508                compilation units.  If we don't have them (i.e.,
02509                unit->high == 0), we need to consult the line info
02510                table to see if a compilation unit contains the given
02511                address.  */
02512              if ((each->arange.high == 0
02513                  || comp_unit_contains_address (each, addr))
02514                 && comp_unit_find_nearest_line (each, addr,
02515                                             filename_ptr,
02516                                             functionname_ptr,
02517                                             linenumber_ptr,
02518                                             stash))
02519               {
02520                 found = TRUE;
02521                 goto done;
02522               }
02523            }
02524        }
02525     }
02526 
02527 done:
02528   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
02529     unset_sections (stash);
02530 
02531   return found;
02532 }
02533 
02534 /* The DWARF2 version of find_line.  Return TRUE if the line is found
02535    without error.  */
02536 
02537 bfd_boolean
02538 _bfd_dwarf2_find_line (bfd *abfd,
02539                      asymbol **symbols,
02540                      asymbol *symbol,
02541                      const char **filename_ptr,
02542                      unsigned int *linenumber_ptr,
02543                      unsigned int addr_size,
02544                      void **pinfo)
02545 {
02546   /* Read each compilation unit from the section .debug_info, and check
02547      to see if it contains the address we are searching for.  If yes,
02548      lookup the address, and return the line number info.  If no, go
02549      on to the next compilation unit.
02550 
02551      We keep a list of all the previously read compilation units, and
02552      a pointer to the next un-read compilation unit.  Check the
02553      previously read units before reading more.  */
02554   struct dwarf2_debug *stash;
02555 
02556   /* What address are we looking for?  */
02557   bfd_vma addr;
02558 
02559   struct comp_unit* each;
02560 
02561   asection *section;
02562 
02563   bfd_boolean found = FALSE;
02564 
02565   section = bfd_get_section (symbol);
02566 
02567   stash = *pinfo;
02568 
02569   if (! stash)
02570     {
02571       bfd_size_type amt = sizeof (struct dwarf2_debug);
02572 
02573       stash = bfd_zalloc (abfd, amt);
02574       if (! stash)
02575        return FALSE;
02576     }
02577 
02578   /* In a relocatable file, 2 functions may have the same address.
02579      We change the section vma so that they won't overlap.  */
02580   if (!stash && (abfd->flags & (EXEC_P | DYNAMIC)) == 0)
02581     {
02582       if (! place_sections (abfd, stash))
02583        return FALSE;
02584     }
02585 
02586   addr = symbol->value;
02587   if (section->output_section)
02588     addr += section->output_section->vma + section->output_offset;
02589   else
02590     addr += section->vma;
02591 
02592   *filename_ptr = NULL;
02593   *filename_ptr = NULL;
02594   *linenumber_ptr = 0;
02595 
02596   if (! *pinfo)
02597     {
02598       bfd_size_type total_size;
02599       asection *msec;
02600 
02601       *pinfo = stash;
02602 
02603       msec = find_debug_info (abfd, NULL);
02604       if (! msec)
02605        /* No dwarf2 info.  Note that at this point the stash
02606           has been allocated, but contains zeros, this lets
02607           future calls to this function fail quicker.  */
02608        goto done;
02609 
02610       /* There can be more than one DWARF2 info section in a BFD these days.
02611         Read them all in and produce one large stash.  We do this in two
02612         passes - in the first pass we just accumulate the section sizes.
02613         In the second pass we read in the section's contents.  The allows
02614         us to avoid reallocing the data as we add sections to the stash.  */
02615       for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
02616        total_size += msec->size;
02617 
02618       stash->info_ptr = bfd_alloc (abfd, total_size);
02619       if (stash->info_ptr == NULL)
02620        goto done;
02621 
02622       stash->info_ptr_end = stash->info_ptr;
02623 
02624       for (msec = find_debug_info (abfd, NULL);
02625           msec;
02626           msec = find_debug_info (abfd, msec))
02627        {
02628          bfd_size_type size;
02629          bfd_size_type start;
02630 
02631          size = msec->size;
02632          if (size == 0)
02633            continue;
02634 
02635          start = stash->info_ptr_end - stash->info_ptr;
02636 
02637          if ((bfd_simple_get_relocated_section_contents
02638               (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
02639            continue;
02640 
02641          stash->info_ptr_end = stash->info_ptr + start + size;
02642        }
02643 
02644       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
02645 
02646       stash->sec = find_debug_info (abfd, NULL);
02647       stash->sec_info_ptr = stash->info_ptr;
02648       stash->syms = symbols;
02649     }
02650 
02651   /* A null info_ptr indicates that there is no dwarf2 info
02652      (or that an error occured while setting up the stash).  */
02653   if (! stash->info_ptr)
02654     goto done;
02655 
02656   stash->inliner_chain = NULL;
02657 
02658   /* Check the previously read comp. units first.  */
02659   for (each = stash->all_comp_units; each; each = each->next_unit)
02660     if ((symbol->flags & BSF_FUNCTION) == 0
02661        || comp_unit_contains_address (each, addr))
02662       {
02663        found = comp_unit_find_line (each, symbol, addr, filename_ptr,
02664                                  linenumber_ptr, stash);
02665        if (found)
02666          goto done;
02667       }
02668 
02669   /* The DWARF2 spec says that the initial length field, and the
02670      offset of the abbreviation table, should both be 4-byte values.
02671      However, some compilers do things differently.  */
02672   if (addr_size == 0)
02673     addr_size = 4;
02674   BFD_ASSERT (addr_size == 4 || addr_size == 8);
02675 
02676   /* Read each remaining comp. units checking each as they are read.  */
02677   while (stash->info_ptr < stash->info_ptr_end)
02678     {
02679       bfd_vma length;
02680       unsigned int offset_size = addr_size;
02681       bfd_byte *info_ptr_unit = stash->info_ptr;
02682 
02683       length = read_4_bytes (abfd, stash->info_ptr);
02684       /* A 0xffffff length is the DWARF3 way of indicating we use
02685         64-bit offsets, instead of 32-bit offsets.  */
02686       if (length == 0xffffffff)
02687        {
02688          offset_size = 8;
02689          length = read_8_bytes (abfd, stash->info_ptr + 4);
02690          stash->info_ptr += 12;
02691        }
02692       /* A zero length is the IRIX way of indicating 64-bit offsets,
02693         mostly because the 64-bit length will generally fit in 32
02694         bits, and the endianness helps.  */
02695       else if (length == 0)
02696        {
02697          offset_size = 8;
02698          length = read_4_bytes (abfd, stash->info_ptr + 4);
02699          stash->info_ptr += 8;
02700        }
02701       /* In the absence of the hints above, we assume 32-bit DWARF2
02702         offsets even for targets with 64-bit addresses, because:
02703           a) most of the time these targets will not have generated
02704              more than 2Gb of debug info and so will not need 64-bit
02705              offsets,
02706         and
02707           b) if they do use 64-bit offsets but they are not using
02708              the size hints that are tested for above then they are
02709              not conforming to the DWARF3 standard anyway.  */
02710       else if (addr_size == 8)
02711        {
02712          offset_size = 4;
02713           stash->info_ptr += 4;
02714        }
02715       else
02716        stash->info_ptr += 4;
02717 
02718       if (length > 0)
02719        {
02720          each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
02721                               offset_size);
02722          stash->info_ptr += length;
02723 
02724          if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
02725              == stash->sec->size)
02726            {
02727              stash->sec = find_debug_info (abfd, stash->sec);
02728              stash->sec_info_ptr = stash->info_ptr;
02729            }
02730 
02731          if (each)
02732            {
02733              each->next_unit = stash->all_comp_units;
02734              stash->all_comp_units = each;
02735 
02736              /* DW_AT_low_pc and DW_AT_high_pc are optional for
02737                compilation units.  If we don't have them (i.e.,
02738                unit->high == 0), we need to consult the line info
02739                table to see if a compilation unit contains the given
02740                address.  */
02741              found = (((symbol->flags & BSF_FUNCTION) == 0
02742                      || each->arange.high <= 0
02743                      || comp_unit_contains_address (each, addr))
02744                      && comp_unit_find_line (each, symbol, addr,
02745                                           filename_ptr,
02746                                           linenumber_ptr,
02747                                           stash));
02748              if (found)
02749               goto done;
02750            }
02751        }
02752     }
02753 
02754 done:
02755   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
02756     unset_sections (stash);
02757 
02758   return found;
02759 }
02760 
02761 bfd_boolean
02762 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
02763                             const char **filename_ptr,
02764                             const char **functionname_ptr,
02765                             unsigned int *linenumber_ptr,
02766                             void **pinfo)
02767 {
02768   struct dwarf2_debug *stash;
02769 
02770   stash = *pinfo;
02771   if (stash)
02772     {
02773       struct funcinfo *func = stash->inliner_chain;
02774       if (func && func->caller_func)
02775        {
02776          *filename_ptr = func->caller_file;
02777          *functionname_ptr = func->caller_func->name;
02778          *linenumber_ptr = func->caller_line;
02779          stash->inliner_chain = func->caller_func;
02780          return (TRUE);
02781        }
02782     }
02783 
02784   return (FALSE);
02785 }
02786 
02787 void
02788 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
02789 {
02790   struct comp_unit *each;
02791   struct dwarf2_debug *stash;
02792 
02793   if (abfd == NULL || elf_tdata (abfd) == NULL)
02794     return;
02795 
02796   stash = elf_tdata (abfd)->dwarf2_find_line_info;
02797 
02798   if (stash == NULL)
02799     return;
02800 
02801   for (each = stash->all_comp_units; each; each = each->next_unit)
02802     {
02803       struct abbrev_info **abbrevs = each->abbrevs;
02804       size_t i;
02805 
02806       for (i = 0; i < ABBREV_HASH_SIZE; i++)
02807        {
02808          struct abbrev_info *abbrev = abbrevs[i];
02809 
02810          while (abbrev)
02811            {
02812              free (abbrev->attrs);
02813              abbrev = abbrev->next;
02814            }
02815        }
02816 
02817       if (each->line_table)
02818        {
02819          free (each->line_table->dirs);
02820          free (each->line_table->files);
02821        }
02822     }
02823 
02824   free (stash->dwarf_abbrev_buffer);
02825   free (stash->dwarf_line_buffer);
02826   free (stash->dwarf_ranges_buffer);
02827 }