]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libbacktrace/elf.c
Update copyright years.
[thirdparty/gcc.git] / libbacktrace / elf.c
index 3ee1dbeaf790cea1efdfc71edff77f9828b0edc8..9a866eb5048c17848aaa77dd10e8c34560126835 100644 (file)
@@ -1,5 +1,5 @@
 /* elf.c -- Get debug data from an ELF file for backtraces.
-   Copyright (C) 2012-2018 Free Software Foundation, Inc.
+   Copyright (C) 2012-2020 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Google.
 
 Redistribution and use in source and binary forms, with or without
@@ -165,9 +165,12 @@ dl_iterate_phdr (int (*callback) (struct dl_phdr_info *,
 #undef ELFDATA2MSB
 #undef EV_CURRENT
 #undef ET_DYN
+#undef EM_PPC64
+#undef EF_PPC64_ABI
 #undef SHN_LORESERVE
 #undef SHN_XINDEX
 #undef SHN_UNDEF
+#undef SHT_PROGBITS
 #undef SHT_SYMTAB
 #undef SHT_STRTAB
 #undef SHT_DYNSYM
@@ -245,6 +248,9 @@ typedef struct {
 
 #define ET_DYN 3
 
+#define EM_PPC64 21
+#define EF_PPC64_ABI 3
+
 typedef struct {
   b_elf_word   sh_name;                /* Section name, index in string tbl */
   b_elf_word   sh_type;                /* Type of section */
@@ -262,6 +268,7 @@ typedef struct {
 #define SHN_LORESERVE  0xFF00          /* Begin range of reserved indices */
 #define SHN_XINDEX     0xFFFF          /* Section index is held elsewhere */
 
+#define SHT_PROGBITS 1
 #define SHT_SYMTAB 2
 #define SHT_STRTAB 3
 #define SHT_DYNSYM 11
@@ -330,41 +337,19 @@ typedef struct
 
 #define ELFCOMPRESS_ZLIB 1
 
-/* An index of ELF sections we care about.  */
-
-enum debug_section
-{
-  DEBUG_INFO,
-  DEBUG_LINE,
-  DEBUG_ABBREV,
-  DEBUG_RANGES,
-  DEBUG_STR,
-
-  /* The old style compressed sections.  This list must correspond to
-     the list of normal debug sections.  */
-  ZDEBUG_INFO,
-  ZDEBUG_LINE,
-  ZDEBUG_ABBREV,
-  ZDEBUG_RANGES,
-  ZDEBUG_STR,
-
-  DEBUG_MAX
-};
-
-/* Names of sections, indexed by enum elf_section.  */
+/* Names of sections, indexed by enum dwarf_section in internal.h.  */
 
-static const char * const debug_section_names[DEBUG_MAX] =
+static const char * const dwarf_section_names[DEBUG_MAX] =
 {
   ".debug_info",
   ".debug_line",
   ".debug_abbrev",
   ".debug_ranges",
   ".debug_str",
-  ".zdebug_info",
-  ".zdebug_line",
-  ".zdebug_abbrev",
-  ".zdebug_ranges",
-  ".zdebug_str"
+  ".debug_addr",
+  ".debug_str_offsets",
+  ".debug_line_str",
+  ".debug_rnglists"
 };
 
 /* Information we gather for the sections we care about.  */
@@ -405,6 +390,20 @@ struct elf_syminfo_data
   size_t count;
 };
 
+/* Information about PowerPC64 ELFv1 .opd section.  */
+
+struct elf_ppc64_opd_data
+{
+  /* Address of the .opd section.  */
+  b_elf_addr addr;
+  /* Section data.  */
+  const char *data;
+  /* Size of the .opd section.  */
+  size_t size;
+  /* Corresponding section view.  */
+  struct backtrace_view view;
+};
+
 /* Compute the CRC-32 of BUF/LEN.  This uses the CRC used for
    .gnu_debuglink files.  */
 
@@ -569,7 +568,8 @@ elf_initialize_syminfo (struct backtrace_state *state,
                        const unsigned char *symtab_data, size_t symtab_size,
                        const unsigned char *strtab, size_t strtab_size,
                        backtrace_error_callback error_callback,
-                       void *data, struct elf_syminfo_data *sdata)
+                       void *data, struct elf_syminfo_data *sdata,
+                       struct elf_ppc64_opd_data *opd)
 {
   size_t sym_count;
   const b_elf_sym *sym;
@@ -620,7 +620,17 @@ elf_initialize_syminfo (struct backtrace_state *state,
          return 0;
        }
       elf_symbols[j].name = (const char *) strtab + sym->st_name;
-      elf_symbols[j].address = sym->st_value + base_address;
+      /* Special case PowerPC64 ELFv1 symbols in .opd section, if the symbol
+        is a function descriptor, read the actual code address from the
+        descriptor.  */
+      if (opd
+         && sym->st_value >= opd->addr
+         && sym->st_value < opd->addr + opd->size)
+       elf_symbols[j].address
+         = *(const b_elf_addr *) (opd->data + (sym->st_value - opd->addr));
+      else
+       elf_symbols[j].address = sym->st_value;
+      elf_symbols[j].address += base_address;
       elf_symbols[j].size = sym->st_size;
       ++j;
     }
@@ -777,6 +787,8 @@ elf_readlink (struct backtrace_state *state, const char *filename,
     }
 }
 
+#define SYSTEM_BUILD_ID_DIR "/usr/lib/debug/.build-id/"
+
 /* Open a separate debug info file, using the build ID to find it.
    Returns an open file descriptor, or -1.
 
@@ -789,7 +801,7 @@ elf_open_debugfile_by_buildid (struct backtrace_state *state,
                               backtrace_error_callback error_callback,
                               void *data)
 {
-  const char * const prefix = "/usr/lib/debug/.build-id/";
+  const char * const prefix = SYSTEM_BUILD_ID_DIR;
   const size_t prefix_len = strlen (prefix);
   const char * const suffix = ".debug";
   const size_t suffix_len = strlen (suffix);
@@ -1054,12 +1066,19 @@ elf_zlib_fetch (const unsigned char **ppin, const unsigned char *pinend,
       return 0;
     }
 
+#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \
+    && defined(__ORDER_BIG_ENDIAN__) \
+    && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ \
+        || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
   /* We've ensured that PIN is aligned.  */
   next = *(const uint32_t *)pin;
 
-#if __BYTE_ORDER == __ORDER_BIG_ENDIAN
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
   next = __builtin_bswap32 (next);
 #endif
+#else
+  next = pin[0] | (pin[1] << 8) | (pin[2] << 16) | (pin[3] << 24);
+#endif
 
   val |= (uint64_t)next << bits;
   bits += 32;
@@ -2599,7 +2618,8 @@ static int
 elf_add (struct backtrace_state *state, const char *filename, int descriptor,
         uintptr_t base_address, backtrace_error_callback error_callback,
         void *data, fileline *fileline_fn, int *found_sym, int *found_dwarf,
-        int exe, int debuginfo)
+        struct dwarf_data **fileline_entry, int exe, int debuginfo,
+        const char *with_buildid_data, uint32_t with_buildid_size)
 {
   struct backtrace_view ehdr_view;
   b_elf_ehdr ehdr;
@@ -2619,6 +2639,7 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
   unsigned int dynsym_shndx;
   unsigned int i;
   struct debug_section_info sections[DEBUG_MAX];
+  struct debug_section_info zsections[DEBUG_MAX];
   struct backtrace_view symtab_view;
   int symtab_view_valid;
   struct backtrace_view strtab_view;
@@ -2631,12 +2652,19 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
   int debuglink_view_valid;
   const char *debuglink_name;
   uint32_t debuglink_crc;
+  struct backtrace_view debugaltlink_view;
+  int debugaltlink_view_valid;
+  const char *debugaltlink_name;
+  const char *debugaltlink_buildid_data;
+  uint32_t debugaltlink_buildid_size;
   off_t min_offset;
   off_t max_offset;
   struct backtrace_view debug_view;
   int debug_view_valid;
   unsigned int using_debug_view;
   uint16_t *zdebug_table;
+  struct elf_ppc64_opd_data opd_data, *opd;
+  struct dwarf_sections dwarf_sections;
 
   if (!debuginfo)
     {
@@ -2654,7 +2682,12 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
   debuglink_view_valid = 0;
   debuglink_name = NULL;
   debuglink_crc = 0;
+  debugaltlink_view_valid = 0;
+  debugaltlink_name = NULL;
+  debugaltlink_buildid_data = NULL;
+  debugaltlink_buildid_size = 0;
   debug_view_valid = 0;
+  opd = NULL;
 
   if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback,
                           data, &ehdr_view))
@@ -2762,7 +2795,7 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
   shstr_size = shstrhdr->sh_size;
   shstr_off = shstrhdr->sh_offset;
 
-  if (!backtrace_get_view (state, descriptor, shstr_off, shstr_size,
+  if (!backtrace_get_view (state, descriptor, shstr_off, shstrhdr->sh_size,
                           error_callback, data, &names_view))
     goto fail;
   names_view_valid = 1;
@@ -2772,6 +2805,7 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
   dynsym_shndx = 0;
 
   memset (sections, 0, sizeof sections);
+  memset (zsections, 0, sizeof zsections);
 
   /* Look for the symbol table.  */
   for (i = 1; i < shnum; ++i)
@@ -2799,7 +2833,7 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
 
       for (j = 0; j < (int) DEBUG_MAX; ++j)
        {
-         if (strcmp (name, debug_section_names[j]) == 0)
+         if (strcmp (name, dwarf_section_names[j]) == 0)
            {
              sections[j].offset = shdr->sh_offset;
              sections[j].size = shdr->sh_size;
@@ -2808,10 +2842,23 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
            }
        }
 
+      if (name[0] == '.' && name[1] == 'z')
+       {
+         for (j = 0; j < (int) DEBUG_MAX; ++j)
+           {
+             if (strcmp (name + 2, dwarf_section_names[j] + 1) == 0)
+               {
+                 zsections[j].offset = shdr->sh_offset;
+                 zsections[j].size = shdr->sh_size;
+                 break;
+               }
+           }
+       }
+
       /* Read the build ID if present.  This could check for any
         SHT_NOTE section with the right note name and type, but gdb
         looks for a specific section name.  */
-      if (!debuginfo
+      if ((!debuginfo || with_buildid_data != NULL)
          && !buildid_view_valid
          && strcmp (name, ".note.gnu.build-id") == 0)
        {
@@ -2827,11 +2874,20 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
          if (note->type == NT_GNU_BUILD_ID
              && note->namesz == 4
              && strncmp (note->name, "GNU", 4) == 0
-             && shdr->sh_size < 12 + ((note->namesz + 3) & ~ 3) + note->descsz)
+             && shdr->sh_size <= 12 + ((note->namesz + 3) & ~ 3) + note->descsz)
            {
              buildid_data = &note->name[0] + ((note->namesz + 3) & ~ 3);
              buildid_size = note->descsz;
            }
+
+         if (with_buildid_size != 0)
+           {
+             if (buildid_size != with_buildid_size)
+               goto fail;
+
+             if (memcmp (buildid_data, with_buildid_data, buildid_size) != 0)
+               goto fail;
+           }
        }
 
       /* Read the debuglink file if present.  */
@@ -2857,6 +2913,49 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
              debuglink_crc = *(const uint32_t*)(debuglink_data + crc_offset);
            }
        }
+
+      if (!debugaltlink_view_valid
+         && strcmp (name, ".gnu_debugaltlink") == 0)
+       {
+         const char *debugaltlink_data;
+         size_t debugaltlink_name_len;
+
+         if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
+                                  shdr->sh_size, error_callback, data,
+                                  &debugaltlink_view))
+           goto fail;
+
+         debugaltlink_view_valid = 1;
+         debugaltlink_data = (const char *) debugaltlink_view.data;
+         debugaltlink_name = debugaltlink_data;
+         debugaltlink_name_len = strnlen (debugaltlink_data, shdr->sh_size);
+         if (debugaltlink_name_len < shdr->sh_size)
+           {
+             /* Include terminating zero.  */
+             debugaltlink_name_len += 1;
+
+             debugaltlink_buildid_data
+               = debugaltlink_data + debugaltlink_name_len;
+             debugaltlink_buildid_size = shdr->sh_size - debugaltlink_name_len;
+           }
+       }
+
+      /* Read the .opd section on PowerPC64 ELFv1.  */
+      if (ehdr.e_machine == EM_PPC64
+         && (ehdr.e_flags & EF_PPC64_ABI) < 2
+         && shdr->sh_type == SHT_PROGBITS
+         && strcmp (name, ".opd") == 0)
+       {
+         if (!backtrace_get_view (state, descriptor, shdr->sh_offset,
+                                  shdr->sh_size, error_callback, data,
+                                  &opd_data.view))
+           goto fail;
+
+         opd = &opd_data;
+         opd->addr = shdr->sh_addr;
+         opd->data = (const char *) opd_data.view.data;
+         opd->size = shdr->sh_size;
+       }
     }
 
   if (symtab_shndx == 0)
@@ -2898,7 +2997,7 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
       if (!elf_initialize_syminfo (state, base_address,
                                   symtab_view.data, symtab_shdr->sh_size,
                                   strtab_view.data, strtab_shdr->sh_size,
-                                  error_callback, data, sdata))
+                                  error_callback, data, sdata, opd))
        {
          backtrace_free (state, sdata, sizeof *sdata, error_callback, data);
          goto fail;
@@ -2935,8 +3034,12 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
          if (debuglink_view_valid)
            backtrace_release_view (state, &debuglink_view, error_callback,
                                    data);
-         ret = elf_add (state, NULL, d, base_address, error_callback, data,
-                        fileline_fn, found_sym, found_dwarf, 0, 1);
+         if (debugaltlink_view_valid)
+           backtrace_release_view (state, &debugaltlink_view, error_callback,
+                                   data);
+         ret = elf_add (state, "", d, base_address, error_callback, data,
+                        fileline_fn, found_sym, found_dwarf, NULL, 0, 1, NULL,
+                        0);
          if (ret < 0)
            backtrace_close (d, error_callback, data);
          else
@@ -2951,6 +3054,12 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
       buildid_view_valid = 0;
     }
 
+  if (opd)
+    {
+      backtrace_release_view (state, &opd->view, error_callback, data);
+      opd = NULL;
+    }
+
   if (debuglink_name != NULL)
     {
       int d;
@@ -2964,8 +3073,12 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
 
          backtrace_release_view (state, &debuglink_view, error_callback,
                                  data);
-         ret = elf_add (state, NULL, d, base_address, error_callback, data,
-                        fileline_fn, found_sym, found_dwarf, 0, 1);
+         if (debugaltlink_view_valid)
+           backtrace_release_view (state, &debugaltlink_view, error_callback,
+                                   data);
+         ret = elf_add (state, "", d, base_address, error_callback, data,
+                        fileline_fn, found_sym, found_dwarf, NULL, 0, 1, NULL,
+                        0);
          if (ret < 0)
            backtrace_close (d, error_callback, data);
          else
@@ -2980,8 +3093,41 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
       debuglink_view_valid = 0;
     }
 
+  struct dwarf_data *fileline_altlink = NULL;
+  if (debugaltlink_name != NULL)
+    {
+      int d;
+
+      d = elf_open_debugfile_by_debuglink (state, filename, debugaltlink_name,
+                                          0, error_callback, data);
+      if (d >= 0)
+       {
+         int ret;
+
+         ret = elf_add (state, filename, d, base_address, error_callback, data,
+                        fileline_fn, found_sym, found_dwarf, &fileline_altlink,
+                        0, 1, debugaltlink_buildid_data,
+                        debugaltlink_buildid_size);
+         backtrace_release_view (state, &debugaltlink_view, error_callback,
+                                 data);
+         debugaltlink_view_valid = 0;
+         if (ret < 0)
+           {
+             backtrace_close (d, error_callback, data);
+             return ret;
+           }
+       }
+    }
+
+  if (debugaltlink_view_valid)
+    {
+      backtrace_release_view (state, &debugaltlink_view, error_callback, data);
+      debugaltlink_view_valid = 0;
+    }
+
   /* Read all the debug sections in a single view, since they are
-     probably adjacent in the file.  We never release this view.  */
+     probably adjacent in the file.  If any of sections are
+     uncompressed, we never release this view.  */
 
   min_offset = 0;
   max_offset = 0;
@@ -2989,13 +3135,22 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
     {
       off_t end;
 
-      if (sections[i].size == 0)
-       continue;
-      if (min_offset == 0 || sections[i].offset < min_offset)
-       min_offset = sections[i].offset;
-      end = sections[i].offset + sections[i].size;
-      if (end > max_offset)
-       max_offset = end;
+      if (sections[i].size != 0)
+       {
+         if (min_offset == 0 || sections[i].offset < min_offset)
+           min_offset = sections[i].offset;
+         end = sections[i].offset + sections[i].size;
+         if (end > max_offset)
+           max_offset = end;
+       }
+      if (zsections[i].size != 0)
+       {
+         if (min_offset == 0 || zsections[i].offset < min_offset)
+           min_offset = zsections[i].offset;
+         end = zsections[i].offset + zsections[i].size;
+         if (end > max_offset)
+           max_offset = end;
+       }
     }
   if (min_offset == 0 || max_offset == 0)
     {
@@ -3024,20 +3179,22 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
        {
          sections[i].data = ((const unsigned char *) debug_view.data
                              + (sections[i].offset - min_offset));
-         if (i < ZDEBUG_INFO)
-           ++using_debug_view;
+         ++using_debug_view;
        }
+
+      if (zsections[i].size == 0)
+       zsections[i].data = NULL;
+      else
+       zsections[i].data = ((const unsigned char *) debug_view.data
+                            + (zsections[i].offset - min_offset));
     }
 
   /* Uncompress the old format (--compress-debug-sections=zlib-gnu).  */
 
   zdebug_table = NULL;
-  for (i = 0; i < ZDEBUG_INFO; ++i)
+  for (i = 0; i < (int) DEBUG_MAX; ++i)
     {
-      struct debug_section_info *pz;
-
-      pz = &sections[i + ZDEBUG_INFO - DEBUG_INFO];
-      if (sections[i].size == 0 && pz->size > 0)
+      if (sections[i].size == 0 && zsections[i].size > 0)
        {
          unsigned char *uncompressed_data;
          size_t uncompressed_size;
@@ -3053,7 +3210,8 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
 
          uncompressed_data = NULL;
          uncompressed_size = 0;
-         if (!elf_uncompress_zdebug (state, pz->data, pz->size, zdebug_table,
+         if (!elf_uncompress_zdebug (state, zsections[i].data,
+                                     zsections[i].size, zdebug_table,
                                      error_callback, data,
                                      &uncompressed_data, &uncompressed_size))
            goto fail;
@@ -3065,7 +3223,7 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
 
   /* Uncompress the official ELF format
      (--compress-debug-sections=zlib-gabi).  */
-  for (i = 0; i < ZDEBUG_INFO; ++i)
+  for (i = 0; i < (int) DEBUG_MAX; ++i)
     {
       unsigned char *uncompressed_data;
       size_t uncompressed_size;
@@ -3105,19 +3263,17 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
       debug_view_valid = 0;
     }
 
-  if (!backtrace_dwarf_add (state, base_address,
-                           sections[DEBUG_INFO].data,
-                           sections[DEBUG_INFO].size,
-                           sections[DEBUG_LINE].data,
-                           sections[DEBUG_LINE].size,
-                           sections[DEBUG_ABBREV].data,
-                           sections[DEBUG_ABBREV].size,
-                           sections[DEBUG_RANGES].data,
-                           sections[DEBUG_RANGES].size,
-                           sections[DEBUG_STR].data,
-                           sections[DEBUG_STR].size,
+  for (i = 0; i < (int) DEBUG_MAX; ++i)
+    {
+      dwarf_sections.data[i] = sections[i].data;
+      dwarf_sections.size[i] = sections[i].size;
+    }
+
+  if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
                            ehdr.e_ident[EI_DATA] == ELFDATA2MSB,
-                           error_callback, data, fileline_fn))
+                           fileline_altlink,
+                           error_callback, data, fileline_fn,
+                           fileline_entry))
     goto fail;
 
   *found_dwarf = 1;
@@ -3135,10 +3291,14 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor,
     backtrace_release_view (state, &strtab_view, error_callback, data);
   if (debuglink_view_valid)
     backtrace_release_view (state, &debuglink_view, error_callback, data);
+  if (debugaltlink_view_valid)
+    backtrace_release_view (state, &debugaltlink_view, error_callback, data);
   if (buildid_view_valid)
     backtrace_release_view (state, &buildid_view, error_callback, data);
   if (debug_view_valid)
     backtrace_release_view (state, &debug_view, error_callback, data);
+  if (opd)
+    backtrace_release_view (state, &opd->view, error_callback, data);
   if (descriptor != -1)
     backtrace_close (descriptor, error_callback, data);
   return 0;
@@ -3203,7 +3363,7 @@ phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED,
 
   if (elf_add (pd->state, filename, descriptor, info->dlpi_addr,
               pd->error_callback, pd->data, &elf_fileline_fn, pd->found_sym,
-              &found_dwarf, 0, 0))
+              &found_dwarf, NULL, 0, 0, NULL, 0))
     {
       if (found_dwarf)
        {
@@ -3231,7 +3391,8 @@ backtrace_initialize (struct backtrace_state *state, const char *filename,
   struct phdr_data pd;
 
   ret = elf_add (state, filename, descriptor, 0, error_callback, data,
-                &elf_fileline_fn, &found_sym, &found_dwarf, 1, 0);
+                &elf_fileline_fn, &found_sym, &found_dwarf, NULL, 1, 0, NULL,
+                0);
   if (!ret)
     return 0;