daily update
[deliverable/binutils-gdb.git] / bfd / dwarf2.c
index 1d55e9ecb11e06758efbda8238bda186013cfc4d..bcaebc2b88d15d0575bab5deb1eefd4ff0837679 100644 (file)
@@ -1,6 +1,6 @@
 /* DWARF 2 support.
    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
-   2004, 2005 Free Software Foundation, Inc.
+   2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 
    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
    (gavin@cygnus.com).
@@ -17,7 +17,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or (at
+   the Free Software Foundation; either version 3 of the License, or (at
    your option) any later version.
 
    This program is distributed in the hope that it will be useful, but
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
-#include "bfd.h"
 #include "sysdep.h"
+#include "bfd.h"
 #include "libiberty.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
@@ -74,6 +75,12 @@ struct dwarf_block
   bfd_byte *data;
 };
 
+struct loadable_section
+{
+  asection *section;
+  bfd_vma adj_vma;
+};
+
 struct dwarf2_debug
 {
   /* A list of all previously read comp_units.  */
@@ -87,8 +94,10 @@ struct dwarf2_debug
   /* Pointer to the end of the .debug_info section memory buffer.  */
   bfd_byte *info_ptr_end;
 
-  /* Pointer to the section and address of the beginning of the
-     section.  */
+  /* Pointer to the bfd, section and address of the beginning of the
+     section.  The bfd might be different than expected because of
+     gnu_debuglink sections.  */
+  bfd * bfd;
   asection *sec;
   bfd_byte *sec_info_ptr;
 
@@ -124,6 +133,12 @@ struct dwarf2_debug
      calling chain for subsequent calls to bfd_find_inliner_info to
      use. */
   struct funcinfo *inliner_chain;
+
+  /* Number of loadable sections.  */
+  unsigned int loadable_section_count;
+
+  /* Array of loadable sections.  */
+  struct loadable_section *loadable_sections;
 };
 
 struct arange
@@ -481,21 +496,21 @@ read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
              amt *= sizeof (struct attr_abbrev);
              tmp = bfd_realloc (cur_abbrev->attrs, amt);
              if (tmp == NULL)
-               {
-                 size_t i;
-
-                 for (i = 0; i < ABBREV_HASH_SIZE; i++)
-                   {
-                   struct abbrev_info *abbrev = abbrevs[i];
-
-                   while (abbrev)
-                     {
-                       free (abbrev->attrs);
-                       abbrev = abbrev->next;
-                     }
-                   }
-                 return NULL;
-               }
+               {
+                 size_t i;
+
+                 for (i = 0; i < ABBREV_HASH_SIZE; i++)
+                   {
+                     struct abbrev_info *abbrev = abbrevs[i];
+
+                     while (abbrev)
+                       {
+                         free (abbrev->attrs);
+                         abbrev = abbrev->next;
+                       }
+                   }
+                 return NULL;
+               }
              cur_abbrev->attrs = tmp;
            }
 
@@ -521,7 +536,7 @@ read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
         for the next compile unit) or if the end of the abbreviation
         table is reached.  */
       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
-           >= stash->dwarf_abbrev_size)
+         >= stash->dwarf_abbrev_size)
        break;
       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
       abbrev_ptr += bytes_read;
@@ -744,6 +759,17 @@ struct varinfo
   unsigned int stack: 1;
 };
 
+/* Return TRUE if NEW_LINE should sort after LINE.  */
+
+static inline bfd_boolean
+new_line_sorts_after (struct line_info *new_line, struct line_info *line)
+{
+  return (new_line->address > line->address
+         || (new_line->address == line->address
+             && new_line->end_sequence < line->end_sequence));
+}
+
+
 /* Adds a new entry to the line_info list in the line_info_table, ensuring
    that the list is sorted.  Note that the line_info list is sorted from
    highest to lowest VMA (with possible duplicates); that is,
@@ -760,6 +786,21 @@ add_line_info (struct line_info_table *table,
   bfd_size_type amt = sizeof (struct line_info);
   struct line_info* info = bfd_alloc (table->abfd, amt);
 
+  /* Set member data of 'info'.  */
+  info->address = address;
+  info->line = line;
+  info->column = column;
+  info->end_sequence = end_sequence;
+
+  if (filename && filename[0])
+    {
+      info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
+      if (info->filename)
+       strcpy (info->filename, filename);
+    }
+  else
+    info->filename = NULL;
+
   /* Find the correct location for 'info'.  Normally we will receive
      new line_info data 1) in order and 2) with increasing VMAs.
      However some compilers break the rules (cf. decode_line_info) and
@@ -775,70 +816,45 @@ add_line_info (struct line_info_table *table,
 
      Note: we may receive duplicate entries from 'decode_line_info'.  */
 
-  while (1)
-    if (!table->last_line
-       || address >= table->last_line->address)
-      {
-       /* Normal case: add 'info' to the beginning of the list */
-       info->prev_line = table->last_line;
-       table->last_line = info;
-
-       /* lcl_head: initialize to head a *possible* sequence at the end.  */
-       if (!table->lcl_head)
-         table->lcl_head = info;
-       break;
-      }
-    else if (!table->lcl_head->prev_line
-            && table->lcl_head->address > address)
-      {
-       /* Abnormal but easy: lcl_head is 1) at the *end* of the line
-          list and 2) the head of 'info'.  */
-       info->prev_line = NULL;
-       table->lcl_head->prev_line = info;
-       break;
-      }
-    else if (table->lcl_head->prev_line
-            && table->lcl_head->address > address
-            && address >= table->lcl_head->prev_line->address)
-      {
-       /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
-          list and 2) the head of 'info'.  */
-       info->prev_line = table->lcl_head->prev_line;
-       table->lcl_head->prev_line = info;
-       break;
-      }
-    else
-      {
-       /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
-          heads for 'info'.  Reset 'lcl_head' and repeat.  */
-       struct line_info* li2 = table->last_line; /* always non-NULL */
-       struct line_info* li1 = li2->prev_line;
-
-       while (li1)
-         {
-           if (li2->address > address && address >= li1->address)
-             break;
-
-           li2 = li1; /* always non-NULL */
-           li1 = li1->prev_line;
-         }
-       table->lcl_head = li2;
-      }
-
-  /* Set member data of 'info'.  */
-  info->address = address;
-  info->line = line;
-  info->column = column;
-  info->end_sequence = end_sequence;
+  if (!table->last_line
+      || new_line_sorts_after (info, table->last_line))
+    {
+      /* Normal case: add 'info' to the beginning of the list */
+      info->prev_line = table->last_line;
+      table->last_line = info;
 
-  if (filename && filename[0])
+      /* lcl_head: initialize to head a *possible* sequence at the end.  */
+      if (!table->lcl_head)
+       table->lcl_head = info;
+    }
+  else if (!new_line_sorts_after (info, table->lcl_head)
+          && (!table->lcl_head->prev_line
+              || new_line_sorts_after (info, table->lcl_head->prev_line)))
     {
-      info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
-      if (info->filename)
-       strcpy (info->filename, filename);
+      /* Abnormal but easy: lcl_head is the head of 'info'.  */
+      info->prev_line = table->lcl_head->prev_line;
+      table->lcl_head->prev_line = info;
     }
   else
-    info->filename = NULL;
+    {
+      /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
+        heads for 'info'.  Reset 'lcl_head'.  */
+      struct line_info* li2 = table->last_line; /* always non-NULL */
+      struct line_info* li1 = li2->prev_line;
+
+      while (li1)
+       {
+         if (!new_line_sorts_after (info, li2)
+             && new_line_sorts_after (info, li1))
+           break;
+
+         li2 = li1; /* always non-NULL */
+         li1 = li1->prev_line;
+       }
+      table->lcl_head = li2;
+      info->prev_line = table->lcl_head->prev_line;
+      table->lcl_head->prev_line = info;
+    }
 }
 
 /* Extract a fully qualified filename from a line info table.
@@ -852,31 +868,54 @@ concat_filename (struct line_info_table *table, unsigned int file)
 
   if (file - 1 >= table->num_files)
     {
-      (*_bfd_error_handler)
-       (_("Dwarf Error: mangled line number section (bad file number)."));
+      /* FILE == 0 means unknown.  */
+      if (file)
+       (*_bfd_error_handler)
+         (_("Dwarf Error: mangled line number section (bad file number)."));
       return strdup ("<unknown>");
     }
 
   filename = table->files[file - 1].name;
 
-  if (! IS_ABSOLUTE_PATH (filename))
+  if (!IS_ABSOLUTE_PATH (filename))
     {
-      char *dirname = (table->files[file - 1].dir
-                      ? table->dirs[table->files[file - 1].dir - 1]
-                      : table->comp_dir);
+      char *dirname = NULL;
+      char *subdirname = NULL;
+      char *name;
+      size_t len;
+
+      if (table->files[file - 1].dir)
+       subdirname = table->dirs[table->files[file - 1].dir - 1];
+
+      if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
+       dirname = table->comp_dir;
 
-      /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
-        The best we can do is return the filename part.  */
-      if (dirname != NULL)
+      if (!dirname)
        {
-         unsigned int len = strlen (dirname) + strlen (filename) + 2;
-         char * name;
+         dirname = subdirname;
+         subdirname = NULL;
+       }
+
+      if (!dirname)
+       return strdup (filename);
 
+      len = strlen (dirname) + strlen (filename) + 2;
+
+      if (subdirname)
+       {
+         len += strlen (subdirname) + 1;
+         name = bfd_malloc (len);
+         if (name)
+           sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
+       }
+      else
+       {
          name = bfd_malloc (len);
          if (name)
            sprintf (name, "%s/%s", dirname, filename);
-         return name;
        }
+
+      return name;
     }
 
   return strdup (filename);
@@ -1102,7 +1141,6 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
       unsigned int line = 1;
       unsigned int column = 0;
       int is_stmt = lh.default_is_stmt;
-      int basic_block = 0;
       int end_sequence = 0;
       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
         compilers generate address sequences that are wildly out of
@@ -1127,7 +1165,6 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
              line += lh.line_base + (adj_opcode % lh.line_range);
              /* Append row to matrix using current values.  */
              add_line_info (table, address, filename, line, column, 0);
-             basic_block = 1;
              if (address < low_pc)
                low_pc = address;
              if (address > high_pc)
@@ -1168,12 +1205,12 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
                      amt *= sizeof (struct fileinfo);
                      tmp = bfd_realloc (table->files, amt);
                      if (tmp == NULL)
-                       {
+                       {
                          free (table->files);
                          free (table->dirs);
                          free (filename);
                          return NULL;
-                       }
+                       }
                      table->files = tmp;
                    }
                  table->files[table->num_files].name = cur_file;
@@ -1199,7 +1236,6 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
              break;
            case DW_LNS_copy:
              add_line_info (table, address, filename, line, column, 0);
-             basic_block = 0;
              if (address < low_pc)
                low_pc = address;
              if (address > high_pc)
@@ -1235,7 +1271,6 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
              is_stmt = (!is_stmt);
              break;
            case DW_LNS_set_basic_block:
-             basic_block = 1;
              break;
            case DW_LNS_const_add_pc:
              address += lh.minimum_instruction_length
@@ -1585,7 +1620,7 @@ read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offs
        return;
     }
   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
-    
+
   for (;;)
     {
       bfd_vma low_pc;
@@ -1610,7 +1645,7 @@ read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offs
       if (low_pc == -1UL && high_pc != -1UL)
        base_address = high_pc;
       else
-         arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
+       arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
     }
 }
 
@@ -1802,7 +1837,7 @@ scan_unit_for_symbols (struct comp_unit *unit)
                                                 attr.u.blk->data + 1);
                        }
                      break;
-                   
+
                    default:
                      break;
                    }
@@ -1856,8 +1891,7 @@ scan_unit_for_symbols (struct comp_unit *unit)
    to get to the line number information for the compilation unit.  */
 
 static struct comp_unit *
-parse_comp_unit (bfd *abfd,
-                struct dwarf2_debug *stash,
+parse_comp_unit (struct dwarf2_debug *stash,
                 bfd_vma unit_length,
                 bfd_byte *info_ptr_unit,
                 unsigned int offset_size)
@@ -1875,6 +1909,7 @@ parse_comp_unit (bfd *abfd,
   bfd_size_type amt;
   bfd_vma low_pc = 0;
   bfd_vma high_pc = 0;
+  bfd *abfd = stash->bfd;
 
   version = read_2_bytes (abfd, info_ptr);
   info_ptr += 2;
@@ -2160,17 +2195,14 @@ find_debug_info (bfd *abfd, asection *after_sec)
 {
   asection * msec;
 
-  if (after_sec)
-    msec = after_sec->next;
-  else
-    msec = abfd->sections;
+  msec = after_sec != NULL ? after_sec->next : abfd->sections;
 
   while (msec)
     {
       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
        return msec;
 
-      if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
+      if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
        return msec;
 
       msec = msec->next;
@@ -2179,215 +2211,119 @@ find_debug_info (bfd *abfd, asection *after_sec)
   return NULL;
 }
 
-/* The DWARF2 version of find_nearest_line.  Return TRUE if the line
-   is found without error.  ADDR_SIZE is the number of bytes in the
-   initial .debug_info length field and in the abbreviation offset.
-   You may use zero to indicate that the default value should be
-   used.  */
+/* Unset vmas for loadable sections in STASH.  */
 
-bfd_boolean
-_bfd_dwarf2_find_nearest_line (bfd *abfd,
-                              asection *section,
-                              asymbol **symbols,
-                              bfd_vma offset,
-                              const char **filename_ptr,
-                              const char **functionname_ptr,
-                              unsigned int *linenumber_ptr,
-                              unsigned int addr_size,
-                              void **pinfo)
+static void
+unset_sections (struct dwarf2_debug *stash)
 {
-  /* Read each compilation unit from the section .debug_info, and check
-     to see if it contains the address we are searching for.  If yes,
-     lookup the address, and return the line number info.  If no, go
-     on to the next compilation unit.
+  unsigned int i;
+  struct loadable_section *p;
 
-     We keep a list of all the previously read compilation units, and
-     a pointer to the next un-read compilation unit.  Check the
-     previously read units before reading more.  */
-  struct dwarf2_debug *stash;
+  i = stash->loadable_section_count;
+  p = stash->loadable_sections;
+  for (; i > 0; i--, p++)
+    p->section->vma = 0;
+}
 
-  /* What address are we looking for?  */
-  bfd_vma addr;
+/* Set unique vmas for loadable sections in ABFD and save vmas in
+   STASH for unset_sections.  */
 
-  struct comp_unit* each;
+static bfd_boolean
+place_sections (bfd *abfd, struct dwarf2_debug *stash)
+{
+  struct loadable_section *p;
+  unsigned int i;
 
-  stash = *pinfo;
-  addr = offset;
-  if (section->output_section)
-    addr += section->output_section->vma + section->output_offset;
+  if (stash->loadable_section_count != 0)
+    {
+      i = stash->loadable_section_count;
+      p = stash->loadable_sections;
+      for (; i > 0; i--, p++)
+       p->section->vma = p->adj_vma;
+    }
   else
-    addr += section->vma;
-  *filename_ptr = NULL;
-  *functionname_ptr = NULL;
-  *linenumber_ptr = 0;
-
-  /* The DWARF2 spec says that the initial length field, and the
-     offset of the abbreviation table, should both be 4-byte values.
-     However, some compilers do things differently.  */
-  if (addr_size == 0)
-    addr_size = 4;
-  BFD_ASSERT (addr_size == 4 || addr_size == 8);
-
-  if (! stash)
     {
-      bfd_size_type total_size;
-      asection *msec;
-      bfd_size_type amt = sizeof (struct dwarf2_debug);
-
-      stash = bfd_zalloc (abfd, amt);
-      if (! stash)
-       return FALSE;
-
-      *pinfo = stash;
-
-      msec = find_debug_info (abfd, NULL);
-      if (! msec)
-       /* No dwarf2 info.  Note that at this point the stash
-          has been allocated, but contains zeros, this lets
-          future calls to this function fail quicker.  */
-        return FALSE;
-
-      /* There can be more than one DWARF2 info section in a BFD these days.
-        Read them all in and produce one large stash.  We do this in two
-        passes - in the first pass we just accumulate the section sizes.
-        In the second pass we read in the section's contents.  The allows
-        us to avoid reallocing the data as we add sections to the stash.  */
-      for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
-       total_size += msec->size;
+      asection *sect;
+      bfd_vma last_vma = 0;
+      bfd_size_type amt;
+      struct loadable_section *p;
 
-      stash->info_ptr = bfd_alloc (abfd, total_size);
-      if (stash->info_ptr == NULL)
-       return FALSE;
-
-      stash->info_ptr_end = stash->info_ptr;
-
-      for (msec = find_debug_info (abfd, NULL);
-          msec;
-          msec = find_debug_info (abfd, msec))
+      i = 0;
+      for (sect = abfd->sections; sect != NULL; sect = sect->next)
        {
-         bfd_size_type size;
-         bfd_size_type start;
+         bfd_size_type sz;
 
-         size = msec->size;
-         if (size == 0)
+         if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
            continue;
 
-         start = stash->info_ptr_end - stash->info_ptr;
-
-         if ((bfd_simple_get_relocated_section_contents
-              (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
+         sz = sect->rawsize ? sect->rawsize : sect->size;
+         if (sz == 0)
            continue;
 
-         stash->info_ptr_end = stash->info_ptr + start + size;
+         i++;
        }
 
-      BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
-
-      stash->sec = find_debug_info (abfd, NULL);
-      stash->sec_info_ptr = stash->info_ptr;
-      stash->syms = symbols;
-    }
-
-  /* A null info_ptr indicates that there is no dwarf2 info
-     (or that an error occured while setting up the stash).  */
-  if (! stash->info_ptr)
-    return FALSE;
-
-  stash->inliner_chain = NULL;
+      amt = i * sizeof (struct loadable_section);
+      p = (struct loadable_section *) bfd_zalloc (abfd, amt);
+      if (! p)
+       return FALSE;
 
-  /* Check the previously read comp. units first.  */
-  for (each = stash->all_comp_units; each; each = each->next_unit)
-    if (comp_unit_contains_address (each, addr)
-       && comp_unit_find_nearest_line (each, addr, filename_ptr,
-                                       functionname_ptr,
-                                       linenumber_ptr, stash))
-      return TRUE;
+      stash->loadable_sections = p;
+      stash->loadable_section_count = i;
 
-  /* Read each remaining comp. units checking each as they are read.  */
-  while (stash->info_ptr < stash->info_ptr_end)
-    {
-      bfd_vma length;
-      unsigned int offset_size = addr_size;
-      bfd_byte *info_ptr_unit = stash->info_ptr;
-
-      length = read_4_bytes (abfd, stash->info_ptr);
-      /* A 0xffffff length is the DWARF3 way of indicating we use
-        64-bit offsets, instead of 32-bit offsets.  */
-      if (length == 0xffffffff)
+      for (sect = abfd->sections; sect != NULL; sect = sect->next)
        {
-         offset_size = 8;
-         length = read_8_bytes (abfd, stash->info_ptr + 4);
-         stash->info_ptr += 12;
-       }
-      /* A zero length is the IRIX way of indicating 64-bit offsets,
-        mostly because the 64-bit length will generally fit in 32
-        bits, and the endianness helps.  */
-      else if (length == 0)
-       {
-         offset_size = 8;
-         length = read_4_bytes (abfd, stash->info_ptr + 4);
-         stash->info_ptr += 8;
-       }
-      /* In the absence of the hints above, we assume addr_size-sized
-        offsets, for backward-compatibility with pre-DWARF3 64-bit
-        platforms.  */
-      else if (addr_size == 8)
-       {
-         length = read_8_bytes (abfd, stash->info_ptr);
-         stash->info_ptr += 8;
-       }
-      else
-       stash->info_ptr += 4;
+         bfd_size_type sz;
 
-      if (length > 0)
-       {
-         each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
-                                 offset_size);
-         stash->info_ptr += length;
+         if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
+           continue;
 
-         if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
-             == stash->sec->size)
-           {
-             stash->sec = find_debug_info (abfd, stash->sec);
-             stash->sec_info_ptr = stash->info_ptr;
-           }
+         sz = sect->rawsize ? sect->rawsize : sect->size;
+         if (sz == 0)
+           continue;
 
-         if (each)
+         p->section = sect;
+         if (last_vma != 0)
            {
-             each->next_unit = stash->all_comp_units;
-             stash->all_comp_units = each;
-
-             /* DW_AT_low_pc and DW_AT_high_pc are optional for
-                compilation units.  If we don't have them (i.e.,
-                unit->high == 0), we need to consult the line info
-                table to see if a compilation unit contains the given
-                address.  */
-             if ((each->arange.high == 0
-                  || comp_unit_contains_address (each, addr))
-                 && comp_unit_find_nearest_line (each, addr,
-                                                 filename_ptr,
-                                                 functionname_ptr,
-                                                 linenumber_ptr,
-                                                 stash))
-               return TRUE;
+             /* Align the new address to the current section
+                alignment.  */
+             last_vma = ((last_vma
+                          + ~((bfd_vma) -1 << sect->alignment_power))
+                         & ((bfd_vma) -1 << sect->alignment_power));
+             sect->vma = last_vma;
            }
+         p->adj_vma = sect->vma;
+         last_vma += sect->vma + sz;
+
+         p++;
        }
     }
 
-  return FALSE;
+  return TRUE;
 }
 
-/* The DWARF2 version of find_line.  Return TRUE if the line is found
-   without error.  */
+/* Find the source code location of SYMBOL.  If SYMBOL is NULL
+   then find the nearest source code location corresponding to
+   the address SECTION + OFFSET.
+   Returns TRUE if the line is found without error and fills in
+   FILENAME_PTR and LINENUMBER_PTR.  In the case where SYMBOL was
+   NULL the FUNCTIONNAME_PTR is also filled in.
+   SYMBOLS contains the symbol table for ABFD.
+   ADDR_SIZE is the number of bytes in the initial .debug_info length
+   field and in the abbreviation offset, or zero to indicate that the
+   default value should be used.  */
 
-bfd_boolean
-_bfd_dwarf2_find_line (bfd *abfd,
-                      asymbol **symbols,
-                      asymbol *symbol,
-                      const char **filename_ptr,
-                      unsigned int *linenumber_ptr,
-                      unsigned int addr_size,
-                      void **pinfo)
+static bfd_boolean
+find_line (bfd *abfd,
+          asection *section,
+          bfd_vma offset,
+          asymbol *symbol,
+          asymbol **symbols,
+          const char **filename_ptr,
+          const char **functionname_ptr,
+          unsigned int *linenumber_ptr,
+          unsigned int addr_size,
+          void **pinfo)
 {
   /* Read each compilation unit from the section .debug_info, and check
      to see if it contains the address we are searching for.  If yes,
@@ -2398,65 +2334,107 @@ _bfd_dwarf2_find_line (bfd *abfd,
      a pointer to the next un-read compilation unit.  Check the
      previously read units before reading more.  */
   struct dwarf2_debug *stash;
-
   /* What address are we looking for?  */
   bfd_vma addr;
-
   struct comp_unit* each;
+  bfd_vma found = FALSE;
+  bfd_boolean do_line;
 
-  asection *section;
+  stash = *pinfo;
 
-  bfd_boolean found;
+  if (! stash)
+    {
+      bfd_size_type amt = sizeof (struct dwarf2_debug);
 
-  section = bfd_get_section (symbol);
+      stash = bfd_zalloc (abfd, amt);
+      if (! stash)
+       return FALSE;
+    }
+
+  /* In a relocatable file, 2 functions may have the same address.
+     We change the section vma so that they won't overlap.  */
+  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
+    {
+      if (! place_sections (abfd, stash))
+       return FALSE;
+    }
+
+  do_line = (section == NULL
+            && offset == 0
+            && functionname_ptr == NULL
+            && symbol != NULL);
+  if (do_line)
+    {
+      addr = symbol->value;
+      section = bfd_get_section (symbol);
+    }
+  else if (section != NULL
+          && functionname_ptr != NULL
+          && symbol == NULL)
+    addr = offset;
+  else
+    abort ();
 
-  addr = symbol->value;
   if (section->output_section)
     addr += section->output_section->vma + section->output_offset;
   else
     addr += section->vma;
-
-  *filename_ptr = NULL;
-  stash = *pinfo;
   *filename_ptr = NULL;
+  if (! do_line)
+    *functionname_ptr = NULL;
   *linenumber_ptr = 0;
 
-  if (! stash)
+  if (! *pinfo)
     {
+      bfd *debug_bfd;
       bfd_size_type total_size;
       asection *msec;
-      bfd_size_type amt = sizeof (struct dwarf2_debug);
-
-      stash = bfd_zalloc (abfd, amt);
-      if (! stash)
-       return FALSE;
 
       *pinfo = stash;
 
       msec = find_debug_info (abfd, NULL);
-      if (! msec)
-       /* No dwarf2 info.  Note that at this point the stash
-          has been allocated, but contains zeros, this lets
-          future calls to this function fail quicker.  */
-        return FALSE;
+      if (msec == NULL)
+       {
+         char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
+
+         if (debug_filename == NULL)
+           /* No dwarf2 info, and no gnu_debuglink to follow.
+              Note that at this point the stash has been allocated, but
+              contains zeros.  This lets future calls to this function
+              fail more quickly.  */
+           goto done;
+
+         if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
+             || ! bfd_check_format (debug_bfd, bfd_object)
+             || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
+           {
+             if (debug_bfd)
+               bfd_close (debug_bfd);
+             /* FIXME: Should we report our failure to follow the debuglink ?  */
+             free (debug_filename);
+             goto done;
+           }
+       }
+      else
+       debug_bfd = abfd;
 
       /* There can be more than one DWARF2 info section in a BFD these days.
         Read them all in and produce one large stash.  We do this in two
         passes - in the first pass we just accumulate the section sizes.
         In the second pass we read in the section's contents.  The allows
         us to avoid reallocing the data as we add sections to the stash.  */
-      for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
+      for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
        total_size += msec->size;
 
-      stash->info_ptr = bfd_alloc (abfd, total_size);
+      stash->info_ptr = bfd_alloc (debug_bfd, total_size);
       if (stash->info_ptr == NULL)
-       return FALSE;
+       goto done;
 
       stash->info_ptr_end = stash->info_ptr;
 
-      for (msec = find_debug_info (abfd, NULL);
+      for (msec = find_debug_info (debug_bfd, NULL);
           msec;
-          msec = find_debug_info (abfd, msec))
+          msec = find_debug_info (debug_bfd, msec))
        {
          bfd_size_type size;
          bfd_size_type start;
@@ -2468,7 +2446,7 @@ _bfd_dwarf2_find_line (bfd *abfd,
          start = stash->info_ptr_end - stash->info_ptr;
 
          if ((bfd_simple_get_relocated_section_contents
-              (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
+              (debug_bfd, msec, stash->info_ptr + start, symbols)) == NULL)
            continue;
 
          stash->info_ptr_end = stash->info_ptr + start + size;
@@ -2476,28 +2454,38 @@ _bfd_dwarf2_find_line (bfd *abfd,
 
       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
 
-      stash->sec = find_debug_info (abfd, NULL);
+      stash->sec = find_debug_info (debug_bfd, NULL);
       stash->sec_info_ptr = stash->info_ptr;
       stash->syms = symbols;
+      stash->bfd = debug_bfd;
     }
 
   /* A null info_ptr indicates that there is no dwarf2 info
      (or that an error occured while setting up the stash).  */
   if (! stash->info_ptr)
-    return FALSE;
+    goto done;
 
   stash->inliner_chain = NULL;
 
   /* Check the previously read comp. units first.  */
   for (each = stash->all_comp_units; each; each = each->next_unit)
-    if ((symbol->flags & BSF_FUNCTION) == 0
-       || comp_unit_contains_address (each, addr))
-      {
-       found = comp_unit_find_line (each, symbol, addr, filename_ptr,
-                                    linenumber_ptr, stash);
-       if (found)
-         return found;
-      }
+    {
+      if (do_line)
+       found = (((symbol->flags & BSF_FUNCTION) == 0
+                 || comp_unit_contains_address (each, addr))
+                && comp_unit_find_line (each, symbol, addr,
+                                        filename_ptr, linenumber_ptr,
+                                        stash));
+      else
+       found = (comp_unit_contains_address (each, addr)
+                && comp_unit_find_nearest_line (each, addr,
+                                                filename_ptr,
+                                                functionname_ptr,
+                                                linenumber_ptr,
+                                                stash));
+      if (found)
+       goto done;
+    }
 
   /* The DWARF2 spec says that the initial length field, and the
      offset of the abbreviation table, should both be 4-byte values.
@@ -2513,13 +2501,13 @@ _bfd_dwarf2_find_line (bfd *abfd,
       unsigned int offset_size = addr_size;
       bfd_byte *info_ptr_unit = stash->info_ptr;
 
-      length = read_4_bytes (abfd, stash->info_ptr);
-      /* A 0xffffff length is the DWARF3 way of indicating we use
-        64-bit offsets, instead of 32-bit offsets.  */
+      length = read_4_bytes (stash->bfd, stash->info_ptr);
+      /* A 0xffffff length is the DWARF3 way of indicating
+        we use 64-bit offsets, instead of 32-bit offsets.  */
       if (length == 0xffffffff)
        {
          offset_size = 8;
-         length = read_8_bytes (abfd, stash->info_ptr + 4);
+         length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
          stash->info_ptr += 12;
        }
       /* A zero length is the IRIX way of indicating 64-bit offsets,
@@ -2528,30 +2516,36 @@ _bfd_dwarf2_find_line (bfd *abfd,
       else if (length == 0)
        {
          offset_size = 8;
-         length = read_4_bytes (abfd, stash->info_ptr + 4);
+         length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
          stash->info_ptr += 8;
        }
-      /* In the absence of the hints above, we assume addr_size-sized
-        offsets, for backward-compatibility with pre-DWARF3 64-bit
-        platforms.  */
+      /* In the absence of the hints above, we assume 32-bit DWARF2
+        offsets even for targets with 64-bit addresses, because:
+          a) most of the time these targets will not have generated
+             more than 2Gb of debug info and so will not need 64-bit
+             offsets,
+        and
+          b) if they do use 64-bit offsets but they are not using
+             the size hints that are tested for above then they are
+             not conforming to the DWARF3 standard anyway.  */
       else if (addr_size == 8)
        {
-         length = read_8_bytes (abfd, stash->info_ptr);
-         stash->info_ptr += 8;
+         offset_size = 4;
+          stash->info_ptr += 4;
        }
       else
        stash->info_ptr += 4;
 
       if (length > 0)
        {
-         each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
+         each = parse_comp_unit (stash, length, info_ptr_unit,
                                  offset_size);
          stash->info_ptr += length;
 
          if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
              == stash->sec->size)
            {
-             stash->sec = find_debug_info (abfd, stash->sec);
+             stash->sec = find_debug_info (stash->bfd, stash->sec);
              stash->sec_info_ptr = stash->info_ptr;
            }
 
@@ -2565,20 +2559,69 @@ _bfd_dwarf2_find_line (bfd *abfd,
                 unit->high == 0), we need to consult the line info
                 table to see if a compilation unit contains the given
                 address.  */
-             found = (((symbol->flags & BSF_FUNCTION) == 0
-                       || each->arange.high <= 0
-                       || comp_unit_contains_address (each, addr))
-                      && comp_unit_find_line (each, symbol, addr,
-                                              filename_ptr,
-                                              linenumber_ptr,
-                                              stash));
+             if (do_line)
+               found = (((symbol->flags & BSF_FUNCTION) == 0
+                         || each->arange.high == 0
+                         || comp_unit_contains_address (each, addr))
+                        && comp_unit_find_line (each, symbol, addr,
+                                                filename_ptr,
+                                                linenumber_ptr,
+                                                stash));
+             else
+               found = ((each->arange.high == 0
+                         || comp_unit_contains_address (each, addr))
+                        && comp_unit_find_nearest_line (each, addr,
+                                                        filename_ptr,
+                                                        functionname_ptr,
+                                                        linenumber_ptr,
+                                                        stash));
              if (found)
-               return TRUE;
+               goto done;
            }
        }
     }
 
-  return FALSE;
+done:
+  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
+    unset_sections (stash);
+
+  return found;
+}
+
+/* The DWARF2 version of find_nearest_line.
+   Return TRUE if the line is found without error.  */
+
+bfd_boolean
+_bfd_dwarf2_find_nearest_line (bfd *abfd,
+                              asection *section,
+                              asymbol **symbols,
+                              bfd_vma offset,
+                              const char **filename_ptr,
+                              const char **functionname_ptr,
+                              unsigned int *linenumber_ptr,
+                              unsigned int addr_size,
+                              void **pinfo)
+{
+  return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
+                   functionname_ptr, linenumber_ptr, addr_size,
+                   pinfo);
+}
+
+/* The DWARF2 version of find_line.
+   Return TRUE if the line is found without error.  */
+
+bfd_boolean
+_bfd_dwarf2_find_line (bfd *abfd,
+                      asymbol **symbols,
+                      asymbol *symbol,
+                      const char **filename_ptr,
+                      unsigned int *linenumber_ptr,
+                      unsigned int addr_size,
+                      void **pinfo)
+{
+  return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
+                   NULL, linenumber_ptr, addr_size,
+                   pinfo);
 }
 
 bfd_boolean
@@ -2594,17 +2637,18 @@ _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
   if (stash)
     {
       struct funcinfo *func = stash->inliner_chain;
+
       if (func && func->caller_func)
        {
          *filename_ptr = func->caller_file;
          *functionname_ptr = func->caller_func->name;
          *linenumber_ptr = func->caller_line;
          stash->inliner_chain = func->caller_func;
-         return (TRUE);
+         return TRUE;
        }
     }
 
-  return (FALSE);
+  return FALSE;
 }
 
 void
@@ -2627,21 +2671,21 @@ _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
       size_t i;
 
       for (i = 0; i < ABBREV_HASH_SIZE; i++)
-        {
-          struct abbrev_info *abbrev = abbrevs[i];
+       {
+         struct abbrev_info *abbrev = abbrevs[i];
 
-          while (abbrev)
-            {
-              free (abbrev->attrs);
-              abbrev = abbrev->next;
-            }
-        }
+         while (abbrev)
+           {
+             free (abbrev->attrs);
+             abbrev = abbrev->next;
+           }
+       }
 
       if (each->line_table)
-        {
-          free (each->line_table->dirs);
-          free (each->line_table->files);
-        }
+       {
+         free (each->line_table->dirs);
+         free (each->line_table->files);
+       }
     }
 
   free (stash->dwarf_abbrev_buffer);
This page took 0.037298 seconds and 4 git commands to generate.