minidebug.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Read MiniDebugInfo data from an objfile.
  2. Copyright (C) 2012-2022 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. #include <algorithm>
  20. #ifdef HAVE_LIBLZMA
  21. /* We stash a reference to the .gnu_debugdata BFD on the enclosing
  22. BFD. */
  23. static const bfd_key<gdb_bfd_ref_ptr> gnu_debug_key;
  24. #include <lzma.h>
  25. /* Allocator function for LZMA. */
  26. static void *
  27. alloc_lzma (void *opaque, size_t nmemb, size_t size)
  28. {
  29. return xmalloc (nmemb * size);
  30. }
  31. /* Free function for LZMA. */
  32. static void
  33. free_lzma (void *opaque, void *ptr)
  34. {
  35. xfree (ptr);
  36. }
  37. /* The allocator object for LZMA. Note that 'gdb_lzma_allocator'
  38. cannot be const due to the lzma library function prototypes. */
  39. static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };
  40. /* Custom bfd_openr_iovec implementation to read compressed data from
  41. a section. This keeps only the last decompressed block in memory
  42. to allow larger data without using to much memory. */
  43. struct gdb_lzma_stream
  44. {
  45. /* Section of input BFD from which we are decoding data. */
  46. asection *section;
  47. /* lzma library decompression state. */
  48. lzma_index *index;
  49. /* Currently decoded block. */
  50. bfd_size_type data_start;
  51. bfd_size_type data_end;
  52. gdb_byte *data;
  53. };
  54. /* bfd_openr_iovec OPEN_P implementation for
  55. find_separate_debug_file_in_section. OPEN_CLOSURE is 'asection *'
  56. of the section to decompress.
  57. Return 'struct gdb_lzma_stream *' must be freed by caller by xfree,
  58. together with its INDEX lzma data. */
  59. static void *
  60. lzma_open (struct bfd *nbfd, void *open_closure)
  61. {
  62. asection *section = (asection *) open_closure;
  63. bfd_size_type size, offset;
  64. lzma_stream_flags options;
  65. gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
  66. gdb_byte *indexdata;
  67. lzma_index *index;
  68. uint64_t memlimit = UINT64_MAX;
  69. struct gdb_lzma_stream *lstream;
  70. size_t pos;
  71. size = bfd_section_size (section);
  72. offset = section->filepos + size - LZMA_STREAM_HEADER_SIZE;
  73. if (size < LZMA_STREAM_HEADER_SIZE
  74. || bfd_seek (section->owner, offset, SEEK_SET) != 0
  75. || bfd_bread (footer, LZMA_STREAM_HEADER_SIZE, section->owner)
  76. != LZMA_STREAM_HEADER_SIZE
  77. || lzma_stream_footer_decode (&options, footer) != LZMA_OK
  78. || offset < options.backward_size)
  79. {
  80. bfd_set_error (bfd_error_wrong_format);
  81. return NULL;
  82. }
  83. offset -= options.backward_size;
  84. indexdata = (gdb_byte *) xmalloc (options.backward_size);
  85. index = NULL;
  86. pos = 0;
  87. if (bfd_seek (section->owner, offset, SEEK_SET) != 0
  88. || bfd_bread (indexdata, options.backward_size, section->owner)
  89. != options.backward_size
  90. || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator,
  91. indexdata, &pos, options.backward_size)
  92. != LZMA_OK
  93. || lzma_index_size (index) != options.backward_size)
  94. {
  95. xfree (indexdata);
  96. bfd_set_error (bfd_error_wrong_format);
  97. return NULL;
  98. }
  99. xfree (indexdata);
  100. lstream = XCNEW (struct gdb_lzma_stream);
  101. lstream->section = section;
  102. lstream->index = index;
  103. return lstream;
  104. }
  105. /* bfd_openr_iovec PREAD_P implementation for
  106. find_separate_debug_file_in_section. Passed STREAM
  107. is 'struct gdb_lzma_stream *'. */
  108. static file_ptr
  109. lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
  110. file_ptr offset)
  111. {
  112. struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
  113. bfd_size_type chunk_size;
  114. lzma_index_iter iter;
  115. gdb_byte *compressed, *uncompressed;
  116. file_ptr block_offset;
  117. lzma_filter filters[LZMA_FILTERS_MAX + 1];
  118. lzma_block block;
  119. size_t compressed_pos, uncompressed_pos;
  120. file_ptr res;
  121. res = 0;
  122. while (nbytes > 0)
  123. {
  124. if (lstream->data == NULL
  125. || lstream->data_start > offset || offset >= lstream->data_end)
  126. {
  127. asection *section = lstream->section;
  128. lzma_index_iter_init (&iter, lstream->index);
  129. if (lzma_index_iter_locate (&iter, offset))
  130. break;
  131. compressed = (gdb_byte *) xmalloc (iter.block.total_size);
  132. block_offset = section->filepos + iter.block.compressed_file_offset;
  133. if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0
  134. || bfd_bread (compressed, iter.block.total_size, section->owner)
  135. != iter.block.total_size)
  136. {
  137. xfree (compressed);
  138. break;
  139. }
  140. uncompressed = (gdb_byte *) xmalloc (iter.block.uncompressed_size);
  141. memset (&block, 0, sizeof (block));
  142. block.filters = filters;
  143. block.header_size = lzma_block_header_size_decode (compressed[0]);
  144. if (lzma_block_header_decode (&block, &gdb_lzma_allocator, compressed)
  145. != LZMA_OK)
  146. {
  147. xfree (compressed);
  148. xfree (uncompressed);
  149. break;
  150. }
  151. compressed_pos = block.header_size;
  152. uncompressed_pos = 0;
  153. if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator,
  154. compressed, &compressed_pos,
  155. iter.block.total_size,
  156. uncompressed, &uncompressed_pos,
  157. iter.block.uncompressed_size)
  158. != LZMA_OK)
  159. {
  160. xfree (compressed);
  161. xfree (uncompressed);
  162. break;
  163. }
  164. xfree (compressed);
  165. xfree (lstream->data);
  166. lstream->data = uncompressed;
  167. lstream->data_start = iter.block.uncompressed_file_offset;
  168. lstream->data_end = (iter.block.uncompressed_file_offset
  169. + iter.block.uncompressed_size);
  170. }
  171. chunk_size = std::min (nbytes, (file_ptr) lstream->data_end - offset);
  172. memcpy (buf, lstream->data + offset - lstream->data_start, chunk_size);
  173. buf = (gdb_byte *) buf + chunk_size;
  174. offset += chunk_size;
  175. nbytes -= chunk_size;
  176. res += chunk_size;
  177. }
  178. return res;
  179. }
  180. /* bfd_openr_iovec CLOSE_P implementation for
  181. find_separate_debug_file_in_section. Passed STREAM
  182. is 'struct gdb_lzma_stream *'. */
  183. static int
  184. lzma_close (struct bfd *nbfd,
  185. void *stream)
  186. {
  187. struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
  188. lzma_index_end (lstream->index, &gdb_lzma_allocator);
  189. xfree (lstream->data);
  190. xfree (lstream);
  191. /* Zero means success. */
  192. return 0;
  193. }
  194. /* bfd_openr_iovec STAT_P implementation for
  195. find_separate_debug_file_in_section. Passed STREAM
  196. is 'struct gdb_lzma_stream *'. */
  197. static int
  198. lzma_stat (struct bfd *abfd,
  199. void *stream,
  200. struct stat *sb)
  201. {
  202. struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
  203. memset (sb, 0, sizeof (struct stat));
  204. sb->st_size = lzma_index_uncompressed_size (lstream->index);
  205. return 0;
  206. }
  207. #endif /* HAVE_LIBLZMA */
  208. /* This looks for a xz compressed separate debug info object file embedded
  209. in a section called .gnu_debugdata. See
  210. http://fedoraproject.org/wiki/Features/MiniDebugInfo
  211. or the "Separate Debug Sections" of the manual for details.
  212. If we find one we create a iovec based bfd that decompresses the
  213. object data on demand. If we don't find one, return NULL. */
  214. gdb_bfd_ref_ptr
  215. find_separate_debug_file_in_section (struct objfile *objfile)
  216. {
  217. asection *section;
  218. gdb_bfd_ref_ptr abfd;
  219. if (objfile->obfd == NULL)
  220. return NULL;
  221. section = bfd_get_section_by_name (objfile->obfd, ".gnu_debugdata");
  222. if (section == NULL)
  223. return NULL;
  224. #ifdef HAVE_LIBLZMA
  225. gdb_bfd_ref_ptr *shared = gnu_debug_key.get (objfile->obfd);
  226. if (shared != nullptr)
  227. return *shared;
  228. std::string filename = string_printf (_(".gnu_debugdata for %s"),
  229. objfile_name (objfile));
  230. abfd = gdb_bfd_openr_iovec (filename.c_str (), gnutarget, lzma_open,
  231. section, lzma_pread, lzma_close, lzma_stat);
  232. if (abfd == NULL)
  233. return NULL;
  234. if (!bfd_check_format (abfd.get (), bfd_object))
  235. {
  236. warning (_("Cannot parse .gnu_debugdata section; not a BFD object"));
  237. return NULL;
  238. }
  239. gnu_debug_key.emplace (objfile->obfd, abfd);
  240. #else
  241. warning (_("Cannot parse .gnu_debugdata section; LZMA support was "
  242. "disabled at compile time"));
  243. #endif /* !HAVE_LIBLZMA */
  244. return abfd;
  245. }