GDB (API)
/home/stan/gdb/src/gdb/minidebug.c
Go to the documentation of this file.
00001 /* Read MiniDebugInfo data from an objfile.
00002 
00003    Copyright (C) 2012-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdb_bfd.h"
00022 #include "gdb_string.h"
00023 #include "symfile.h"
00024 #include "objfiles.h"
00025 #include "gdbcore.h"
00026 
00027 #ifdef HAVE_LIBLZMA
00028 
00029 #include <lzma.h>
00030 
00031 /* Allocator function for LZMA.  */
00032 
00033 static void *
00034 alloc_lzma (void *opaque, size_t nmemb, size_t size)
00035 {
00036   return xmalloc (nmemb * size);
00037 }
00038 
00039 /* Free function for LZMA.  */
00040 
00041 static void
00042 free_lzma (void *opaque, void *ptr)
00043 {
00044   xfree (ptr);
00045 }
00046 
00047 /* The allocator object for LZMA.  Note that 'gdb_lzma_allocator'
00048    cannot be const due to the lzma library function prototypes.  */
00049 
00050 static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };
00051 
00052 /* Custom bfd_openr_iovec implementation to read compressed data from
00053    a section.  This keeps only the last decompressed block in memory
00054    to allow larger data without using to much memory.  */
00055 
00056 struct lzma_stream
00057 {
00058   /* Section of input BFD from which we are decoding data.  */
00059   asection *section;
00060 
00061   /* lzma library decompression state.  */
00062   lzma_index *index;
00063 
00064   /* Currently decoded block.  */
00065   bfd_size_type data_start;
00066   bfd_size_type data_end;
00067   gdb_byte *data;
00068 };
00069 
00070 /* bfd_openr_iovec OPEN_P implementation for
00071    find_separate_debug_file_in_section.  OPEN_CLOSURE is 'asection *'
00072    of the section to decompress.
00073 
00074    Return 'struct lzma_stream *' must be freed by caller by xfree, together
00075    with its INDEX lzma data.  */
00076 
00077 static void *
00078 lzma_open (struct bfd *nbfd, void *open_closure)
00079 {
00080   asection *section = open_closure;
00081   bfd_size_type size, offset;
00082   lzma_stream_flags options;
00083   gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
00084   gdb_byte *indexdata;
00085   lzma_index *index;
00086   int ret;
00087   uint64_t memlimit = UINT64_MAX;
00088   struct lzma_stream *lstream;
00089   size_t pos;
00090 
00091   size = bfd_get_section_size (section);
00092   offset = section->filepos + size - LZMA_STREAM_HEADER_SIZE;
00093   if (size < LZMA_STREAM_HEADER_SIZE
00094       || bfd_seek (section->owner, offset, SEEK_SET) != 0
00095       || bfd_bread (footer, LZMA_STREAM_HEADER_SIZE, section->owner)
00096          != LZMA_STREAM_HEADER_SIZE
00097       || lzma_stream_footer_decode (&options, footer) != LZMA_OK
00098       || offset < options.backward_size)
00099     {
00100       bfd_set_error (bfd_error_wrong_format);
00101       return NULL;
00102     }
00103 
00104   offset -= options.backward_size;
00105   indexdata = xmalloc (options.backward_size);
00106   index = NULL;
00107   pos = 0;
00108   if (bfd_seek (section->owner, offset, SEEK_SET) != 0
00109       || bfd_bread (indexdata, options.backward_size, section->owner)
00110          != options.backward_size
00111       || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator,
00112                                    indexdata, &pos, options.backward_size)
00113          != LZMA_OK
00114       || lzma_index_size (index) != options.backward_size)
00115     {
00116       xfree (indexdata);
00117       bfd_set_error (bfd_error_wrong_format);
00118       return NULL;
00119     }
00120   xfree (indexdata);
00121 
00122   lstream = xzalloc (sizeof (struct lzma_stream));
00123   lstream->section = section;
00124   lstream->index = index;
00125 
00126   return lstream;
00127 }
00128 
00129 /* bfd_openr_iovec PREAD_P implementation for
00130    find_separate_debug_file_in_section.  Passed STREAM
00131    is 'struct lzma_stream *'.  */
00132 
00133 static file_ptr
00134 lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
00135             file_ptr offset)
00136 {
00137   struct lzma_stream *lstream = stream;
00138   bfd_size_type chunk_size;
00139   lzma_index_iter iter;
00140   gdb_byte *compressed, *uncompressed;
00141   file_ptr block_offset;
00142   lzma_filter filters[LZMA_FILTERS_MAX + 1];
00143   lzma_block block;
00144   size_t compressed_pos, uncompressed_pos;
00145   file_ptr res;
00146 
00147   res = 0;
00148   while (nbytes > 0)
00149     {
00150       if (lstream->data == NULL
00151           || lstream->data_start > offset || offset >= lstream->data_end)
00152         {
00153           asection *section = lstream->section;
00154 
00155           lzma_index_iter_init (&iter, lstream->index);
00156           if (lzma_index_iter_locate (&iter, offset))
00157             break;
00158 
00159           compressed = xmalloc (iter.block.total_size);
00160           block_offset = section->filepos + iter.block.compressed_file_offset;
00161           if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0
00162               || bfd_bread (compressed, iter.block.total_size, section->owner)
00163                  != iter.block.total_size)
00164             {
00165               xfree (compressed);
00166               break;
00167             }
00168 
00169           uncompressed = xmalloc (iter.block.uncompressed_size);
00170 
00171           memset (&block, 0, sizeof (block));
00172           block.filters = filters;
00173           block.header_size = lzma_block_header_size_decode (compressed[0]);
00174           if (lzma_block_header_decode (&block, &gdb_lzma_allocator, compressed)
00175               != LZMA_OK)
00176             {
00177               xfree (compressed);
00178               xfree (uncompressed);
00179               break;
00180             }
00181 
00182           compressed_pos = block.header_size;
00183           uncompressed_pos = 0;
00184           if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator,
00185                                         compressed, &compressed_pos,
00186                                         iter.block.total_size,
00187                                         uncompressed, &uncompressed_pos,
00188                                         iter.block.uncompressed_size)
00189               != LZMA_OK)
00190             {
00191               xfree (compressed);
00192               xfree (uncompressed);
00193               break;
00194             }
00195 
00196           xfree (compressed);
00197 
00198           xfree (lstream->data);
00199           lstream->data = uncompressed;
00200           lstream->data_start = iter.block.uncompressed_file_offset;
00201           lstream->data_end = (iter.block.uncompressed_file_offset
00202                                + iter.block.uncompressed_size);
00203         }
00204 
00205       chunk_size = min (nbytes, lstream->data_end - offset);
00206       memcpy (buf, lstream->data + offset - lstream->data_start, chunk_size);
00207       buf = (gdb_byte *) buf + chunk_size;
00208       offset += chunk_size;
00209       nbytes -= chunk_size;
00210       res += chunk_size;
00211     }
00212 
00213   return res;
00214 }
00215 
00216 /* bfd_openr_iovec CLOSE_P implementation for
00217    find_separate_debug_file_in_section.  Passed STREAM
00218    is 'struct lzma_stream *'.  */
00219 
00220 static int
00221 lzma_close (struct bfd *nbfd,
00222             void *stream)
00223 {
00224   struct lzma_stream *lstream = stream;
00225 
00226   lzma_index_end (lstream->index, &gdb_lzma_allocator);
00227   xfree (lstream->data);
00228   xfree (lstream);
00229 
00230   /* Zero means success.  */
00231   return 0;
00232 }
00233 
00234 /* bfd_openr_iovec STAT_P implementation for
00235    find_separate_debug_file_in_section.  Passed STREAM
00236    is 'struct lzma_stream *'.  */
00237 
00238 static int
00239 lzma_stat (struct bfd *abfd,
00240            void *stream,
00241            struct stat *sb)
00242 {
00243   struct lzma_stream *lstream = stream;
00244 
00245   sb->st_size = lzma_index_uncompressed_size (lstream->index);
00246   return 0;
00247 }
00248 
00249 #endif /* HAVE_LIBLZMA  */
00250 
00251 /* This looks for a xz compressed separate debug info object file embedded
00252    in a section called .gnu_debugdata.  See
00253    http://fedoraproject.org/wiki/Features/MiniDebugInfo
00254    or the "Separate Debug Sections" of the manual for details.
00255    If we find one we create a iovec based bfd that decompresses the
00256    object data on demand.  If we don't find one, return NULL.  */
00257 
00258 bfd *
00259 find_separate_debug_file_in_section (struct objfile *objfile)
00260 {
00261   asection *section;
00262   bfd *abfd;
00263 
00264   if (objfile->obfd == NULL)
00265     return NULL;
00266 
00267   section = bfd_get_section_by_name (objfile->obfd, ".gnu_debugdata");
00268   if (section == NULL)
00269     return NULL;
00270 
00271 #ifdef HAVE_LIBLZMA
00272   abfd = gdb_bfd_openr_iovec (objfile_name (objfile), gnutarget, lzma_open,
00273                               section, lzma_pread, lzma_close, lzma_stat);
00274   if (abfd == NULL)
00275     return NULL;
00276 
00277   if (!bfd_check_format (abfd, bfd_object))
00278     {
00279       warning (_("Cannot parse .gnu_debugdata section; not a BFD object"));
00280       gdb_bfd_unref (abfd);
00281       return NULL;
00282     }
00283 #else
00284   warning (_("Cannot parse .gnu_debugdata section; LZMA support was "
00285              "disabled at compile time"));
00286   abfd = NULL;
00287 #endif /* !HAVE_LIBLZMA */
00288 
00289   return abfd;
00290 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines