* as.c (show_usage): Document --execstack and --noexecstack.
[binutils-gdb.git] / bfd / dwarf2.c
index 0bc94b64135dd155e4371b27708c084049f69a46..7a9e5b00e31e1c63ccb4cb5e1737296f826356c1 100644 (file)
@@ -650,7 +650,7 @@ read_attribute_value (attr, form, unit, info_ptr)
   switch (form)
     {
     case DW_FORM_addr:
-      /* FIXME: DWARF3 draft sais DW_FORM_ref_addr is offset_size.  */
+      /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size.  */
     case DW_FORM_ref_addr:
       DW_ADDR (attr) = read_address (unit, info_ptr);
       info_ptr += unit->addr_size;
@@ -817,11 +817,11 @@ struct funcinfo
   bfd_vma high;
 };
 
-/* add_line_info: 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, line_info->prev_line always accesses an equal
-   or smaller VMA.  */
+/* 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,
+   line_info->prev_line always accesses an equal or smaller VMA.  */
+
 static void
 add_line_info (table, address, filename, line, column, end_sequence)
      struct line_info_table* table;
@@ -901,14 +901,24 @@ add_line_info (table, address, filename, line, column, end_sequence)
 
   /* Set member data of 'info'.  */
   info->address = address;
-  info->filename = filename;
   info->line = line;
   info->column = column;
   info->end_sequence = end_sequence;
+
+  amt = strlen (filename);
+  if (amt)
+    {
+      info->filename = bfd_alloc (table->abfd, amt + 1);
+      if (info->filename)
+       strcpy (info->filename, filename);
+    }
+  else
+    info->filename = NULL;
 }
 
 /* Extract a fully qualified filename from a line info table.
-   The returned string has been xmalloc'ed.  */
+   The returned string has been malloc'ed and it is the caller's
+   responsibility to free it.  */
 
 static char *
 concat_filename (table, file)
@@ -921,26 +931,32 @@ concat_filename (table, file)
     {
       (*_bfd_error_handler)
        (_("Dwarf Error: mangled line number section (bad file number)."));
-      return concat ("<unknown>");
+      return strdup ("<unknown>");
     }
 
   filename = table->files[file - 1].name;
 
-  if (IS_ABSOLUTE_PATH (filename))
-    return concat (filename);
-  else
+  if (! IS_ABSOLUTE_PATH (filename))
     {
       char* dirname = (table->files[file - 1].dir
                       ? table->dirs[table->files[file - 1].dir - 1]
                       : 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)
-       return concat (filename);
-      else
-       return concat (dirname, "/", filename, NULL);
+      /* 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)
+       {
+         unsigned int len = strlen (dirname) + strlen (filename) + 2;
+         char * name;
+
+         name = bfd_malloc (len);
+         if (name)
+           sprintf (name, "%s/%s", dirname, filename);
+         return name;
+       }
     }
+
+  return strdup (filename);
 }
 
 static void
@@ -1266,7 +1282,8 @@ decode_line_info (unit, stash)
                   based, the references are 1 based.  */
                file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
                line_ptr += bytes_read;
-               free (filename);
+               if (filename)
+                 free (filename);
                filename = concat_filename (table, file);
                break;
              }
@@ -1302,7 +1319,8 @@ decode_line_info (unit, stash)
            }
        }
 
-      free (filename);
+      if (filename)
+       free (filename);
     }
 
   return table;
@@ -1909,26 +1927,34 @@ _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
       bfd_boolean found;
       unsigned int offset_size = addr_size;
 
-      if (addr_size == 4)
+      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)
        {
-         length = read_4_bytes (abfd, stash->info_ptr);
-         if (length == 0xffffffff)
-           {
-             offset_size = 8;
-             length = read_8_bytes (abfd, stash->info_ptr + 4);
-             stash->info_ptr += 8;
-           }
-         else if (length == 0)
-           {
-             /* Handle (non-standard) 64-bit DWARF2 formats.  */
-             offset_size = 8;
-             length = read_4_bytes (abfd, stash->info_ptr + 4);
-             stash->info_ptr += 4;
-           }
+         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
-       length = read_8_bytes (abfd, stash->info_ptr);
-      stash->info_ptr += addr_size;
+       stash->info_ptr += 4;
 
       if (length > 0)
        {