gdb/minidebug.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Read MiniDebugInfo data from an objfile.

  2.    Copyright (C) 2012-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdb_bfd.h"
  16. #include "symfile.h"
  17. #include "objfiles.h"
  18. #include "gdbcore.h"

  19. #ifdef HAVE_LIBLZMA

  20. #include <lzma.h>

  21. /* Allocator function for LZMA.  */

  22. static void *
  23. alloc_lzma (void *opaque, size_t nmemb, size_t size)
  24. {
  25.   return xmalloc (nmemb * size);
  26. }

  27. /* Free function for LZMA.  */

  28. static void
  29. free_lzma (void *opaque, void *ptr)
  30. {
  31.   xfree (ptr);
  32. }

  33. /* The allocator object for LZMA.  Note that 'gdb_lzma_allocator'
  34.    cannot be const due to the lzma library function prototypes.  */

  35. static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };

  36. /* Custom bfd_openr_iovec implementation to read compressed data from
  37.    a section.  This keeps only the last decompressed block in memory
  38.    to allow larger data without using to much memory.  */

  39. struct lzma_stream
  40. {
  41.   /* Section of input BFD from which we are decoding data.  */
  42.   asection *section;

  43.   /* lzma library decompression state.  */
  44.   lzma_index *index;

  45.   /* Currently decoded block.  */
  46.   bfd_size_type data_start;
  47.   bfd_size_type data_end;
  48.   gdb_byte *data;
  49. };

  50. /* bfd_openr_iovec OPEN_P implementation for
  51.    find_separate_debug_file_in_section.  OPEN_CLOSURE is 'asection *'
  52.    of the section to decompress.

  53.    Return 'struct lzma_stream *' must be freed by caller by xfree, together
  54.    with its INDEX lzma data.  */

  55. static void *
  56. lzma_open (struct bfd *nbfd, void *open_closure)
  57. {
  58.   asection *section = open_closure;
  59.   bfd_size_type size, offset;
  60.   lzma_stream_flags options;
  61.   gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
  62.   gdb_byte *indexdata;
  63.   lzma_index *index;
  64.   int ret;
  65.   uint64_t memlimit = UINT64_MAX;
  66.   struct lzma_stream *lstream;
  67.   size_t pos;

  68.   size = bfd_get_section_size (section);
  69.   offset = section->filepos + size - LZMA_STREAM_HEADER_SIZE;
  70.   if (size < LZMA_STREAM_HEADER_SIZE
  71.       || bfd_seek (section->owner, offset, SEEK_SET) != 0
  72.       || bfd_bread (footer, LZMA_STREAM_HEADER_SIZE, section->owner)
  73.          != LZMA_STREAM_HEADER_SIZE
  74.       || lzma_stream_footer_decode (&options, footer) != LZMA_OK
  75.       || offset < options.backward_size)
  76.     {
  77.       bfd_set_error (bfd_error_wrong_format);
  78.       return NULL;
  79.     }

  80.   offset -= options.backward_size;
  81.   indexdata = xmalloc (options.backward_size);
  82.   index = NULL;
  83.   pos = 0;
  84.   if (bfd_seek (section->owner, offset, SEEK_SET) != 0
  85.       || bfd_bread (indexdata, options.backward_size, section->owner)
  86.          != options.backward_size
  87.       || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator,
  88.                                    indexdata, &pos, options.backward_size)
  89.          != LZMA_OK
  90.       || lzma_index_size (index) != options.backward_size)
  91.     {
  92.       xfree (indexdata);
  93.       bfd_set_error (bfd_error_wrong_format);
  94.       return NULL;
  95.     }
  96.   xfree (indexdata);

  97.   lstream = xzalloc (sizeof (struct lzma_stream));
  98.   lstream->section = section;
  99.   lstream->index = index;

  100.   return lstream;
  101. }

  102. /* bfd_openr_iovec PREAD_P implementation for
  103.    find_separate_debug_file_in_section.  Passed STREAM
  104.    is 'struct lzma_stream *'.  */

  105. static file_ptr
  106. lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
  107.             file_ptr offset)
  108. {
  109.   struct lzma_stream *lstream = stream;
  110.   bfd_size_type chunk_size;
  111.   lzma_index_iter iter;
  112.   gdb_byte *compressed, *uncompressed;
  113.   file_ptr block_offset;
  114.   lzma_filter filters[LZMA_FILTERS_MAX + 1];
  115.   lzma_block block;
  116.   size_t compressed_pos, uncompressed_pos;
  117.   file_ptr res;

  118.   res = 0;
  119.   while (nbytes > 0)
  120.     {
  121.       if (lstream->data == NULL
  122.           || lstream->data_start > offset || offset >= lstream->data_end)
  123.         {
  124.           asection *section = lstream->section;

  125.           lzma_index_iter_init (&iter, lstream->index);
  126.           if (lzma_index_iter_locate (&iter, offset))
  127.             break;

  128.           compressed = xmalloc (iter.block.total_size);
  129.           block_offset = section->filepos + iter.block.compressed_file_offset;
  130.           if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0
  131.               || bfd_bread (compressed, iter.block.total_size, section->owner)
  132.                  != iter.block.total_size)
  133.             {
  134.               xfree (compressed);
  135.               break;
  136.             }

  137.           uncompressed = xmalloc (iter.block.uncompressed_size);

  138.           memset (&block, 0, sizeof (block));
  139.           block.filters = filters;
  140.           block.header_size = lzma_block_header_size_decode (compressed[0]);
  141.           if (lzma_block_header_decode (&block, &gdb_lzma_allocator, compressed)
  142.               != LZMA_OK)
  143.             {
  144.               xfree (compressed);
  145.               xfree (uncompressed);
  146.               break;
  147.             }

  148.           compressed_pos = block.header_size;
  149.           uncompressed_pos = 0;
  150.           if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator,
  151.                                         compressed, &compressed_pos,
  152.                                         iter.block.total_size,
  153.                                         uncompressed, &uncompressed_pos,
  154.                                         iter.block.uncompressed_size)
  155.               != LZMA_OK)
  156.             {
  157.               xfree (compressed);
  158.               xfree (uncompressed);
  159.               break;
  160.             }

  161.           xfree (compressed);

  162.           xfree (lstream->data);
  163.           lstream->data = uncompressed;
  164.           lstream->data_start = iter.block.uncompressed_file_offset;
  165.           lstream->data_end = (iter.block.uncompressed_file_offset
  166.                                + iter.block.uncompressed_size);
  167.         }

  168.       chunk_size = min (nbytes, lstream->data_end - offset);
  169.       memcpy (buf, lstream->data + offset - lstream->data_start, chunk_size);
  170.       buf = (gdb_byte *) buf + chunk_size;
  171.       offset += chunk_size;
  172.       nbytes -= chunk_size;
  173.       res += chunk_size;
  174.     }

  175.   return res;
  176. }

  177. /* bfd_openr_iovec CLOSE_P implementation for
  178.    find_separate_debug_file_in_section.  Passed STREAM
  179.    is 'struct lzma_stream *'.  */

  180. static int
  181. lzma_close (struct bfd *nbfd,
  182.             void *stream)
  183. {
  184.   struct lzma_stream *lstream = stream;

  185.   lzma_index_end (lstream->index, &gdb_lzma_allocator);
  186.   xfree (lstream->data);
  187.   xfree (lstream);

  188.   /* Zero means success.  */
  189.   return 0;
  190. }

  191. /* bfd_openr_iovec STAT_P implementation for
  192.    find_separate_debug_file_in_section.  Passed STREAM
  193.    is 'struct lzma_stream *'.  */

  194. static int
  195. lzma_stat (struct bfd *abfd,
  196.            void *stream,
  197.            struct stat *sb)
  198. {
  199.   struct lzma_stream *lstream = stream;

  200.   sb->st_size = lzma_index_uncompressed_size (lstream->index);
  201.   return 0;
  202. }

  203. #endif /* HAVE_LIBLZMA  */

  204. /* This looks for a xz compressed separate debug info object file embedded
  205.    in a section called .gnu_debugdata.  See
  206.    http://fedoraproject.org/wiki/Features/MiniDebugInfo
  207.    or the "Separate Debug Sections" of the manual for details.
  208.    If we find one we create a iovec based bfd that decompresses the
  209.    object data on demand.  If we don't find one, return NULL.  */

  210. bfd *
  211. find_separate_debug_file_in_section (struct objfile *objfile)
  212. {
  213.   asection *section;
  214.   bfd *abfd;

  215.   if (objfile->obfd == NULL)
  216.     return NULL;

  217.   section = bfd_get_section_by_name (objfile->obfd, ".gnu_debugdata");
  218.   if (section == NULL)
  219.     return NULL;

  220. #ifdef HAVE_LIBLZMA
  221.   abfd = gdb_bfd_openr_iovec (objfile_name (objfile), gnutarget, lzma_open,
  222.                               section, lzma_pread, lzma_close, lzma_stat);
  223.   if (abfd == NULL)
  224.     return NULL;

  225.   if (!bfd_check_format (abfd, bfd_object))
  226.     {
  227.       warning (_("Cannot parse .gnu_debugdata section; not a BFD object"));
  228.       gdb_bfd_unref (abfd);
  229.       return NULL;
  230.     }
  231. #else
  232.   warning (_("Cannot parse .gnu_debugdata section; LZMA support was "
  233.              "disabled at compile time"));
  234.   abfd = NULL;
  235. #endif /* !HAVE_LIBLZMA */

  236.   return abfd;
  237. }