rddbg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* rddbg.c -- Read debugging information into a generic form.
  2. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@cygnus.com>.
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. /* This file reads debugging information into a generic form. This
  18. file knows how to dig the debugging information out of an object
  19. file. */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "libiberty.h"
  23. #include "bucomm.h"
  24. #include "debug.h"
  25. #include "budbg.h"
  26. static bool read_section_stabs_debugging_info
  27. (bfd *, asymbol **, long, void *, bool *);
  28. static bool read_symbol_stabs_debugging_info
  29. (bfd *, asymbol **, long, void *, bool *);
  30. static void save_stab (int, int, bfd_vma, const char *);
  31. static void stab_context (void);
  32. static void free_saved_stabs (void);
  33. /* Read debugging information from a BFD. Returns a generic debugging
  34. pointer. */
  35. void *
  36. read_debugging_info (bfd *abfd, asymbol **syms, long symcount,
  37. bool no_messages)
  38. {
  39. void *dhandle;
  40. bool found;
  41. dhandle = debug_init ();
  42. if (dhandle == NULL)
  43. return NULL;
  44. if (! read_section_stabs_debugging_info (abfd, syms, symcount, dhandle,
  45. &found))
  46. goto err_exit;
  47. if (bfd_get_flavour (abfd) == bfd_target_aout_flavour)
  48. {
  49. if (! read_symbol_stabs_debugging_info (abfd, syms, symcount, dhandle,
  50. &found))
  51. goto err_exit;
  52. }
  53. /* Try reading the COFF symbols if we didn't find any stabs in COFF
  54. sections. */
  55. if (! found
  56. && bfd_get_flavour (abfd) == bfd_target_coff_flavour
  57. && symcount > 0)
  58. {
  59. if (! parse_coff (abfd, syms, symcount, dhandle))
  60. goto err_exit;
  61. found = true;
  62. }
  63. if (! found)
  64. {
  65. if (! no_messages)
  66. non_fatal (_("%s: no recognized debugging information"),
  67. bfd_get_filename (abfd));
  68. err_exit:
  69. free (dhandle);
  70. return NULL;
  71. }
  72. return dhandle;
  73. }
  74. /* Read stabs in sections debugging information from a BFD. */
  75. static bool
  76. read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
  77. void *dhandle, bool *pfound)
  78. {
  79. static struct
  80. {
  81. const char *secname;
  82. const char *strsecname;
  83. }
  84. names[] =
  85. {
  86. { ".stab", ".stabstr" },
  87. { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" },
  88. { "$GDB_SYMBOLS$", "$GDB_STRINGS$" }
  89. };
  90. unsigned int i;
  91. void *shandle;
  92. *pfound = false;
  93. shandle = NULL;
  94. for (i = 0; i < sizeof names / sizeof names[0]; i++)
  95. {
  96. asection *sec, *strsec;
  97. sec = bfd_get_section_by_name (abfd, names[i].secname);
  98. strsec = bfd_get_section_by_name (abfd, names[i].strsecname);
  99. if (sec != NULL && strsec != NULL)
  100. {
  101. bfd_size_type stabsize, strsize;
  102. bfd_byte *stabs, *strings;
  103. bfd_byte *stab;
  104. bfd_size_type stroff, next_stroff;
  105. stabsize = bfd_section_size (sec);
  106. stabs = (bfd_byte *) xmalloc (stabsize);
  107. if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
  108. {
  109. fprintf (stderr, "%s: %s: %s\n",
  110. bfd_get_filename (abfd), names[i].secname,
  111. bfd_errmsg (bfd_get_error ()));
  112. free (shandle);
  113. free (stabs);
  114. return false;
  115. }
  116. strsize = bfd_section_size (strsec);
  117. strings = (bfd_byte *) xmalloc (strsize + 1);
  118. if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
  119. {
  120. fprintf (stderr, "%s: %s: %s\n",
  121. bfd_get_filename (abfd), names[i].strsecname,
  122. bfd_errmsg (bfd_get_error ()));
  123. free (shandle);
  124. free (strings);
  125. free (stabs);
  126. return false;
  127. }
  128. /* Zero terminate the strings table, just in case. */
  129. strings [strsize] = 0;
  130. if (shandle == NULL)
  131. {
  132. shandle = start_stab (dhandle, abfd, true, syms, symcount);
  133. if (shandle == NULL)
  134. {
  135. free (strings);
  136. free (stabs);
  137. return false;
  138. }
  139. }
  140. *pfound = true;
  141. stroff = 0;
  142. next_stroff = 0;
  143. /* PR 17512: file: 078-60391-0.001:0.1. */
  144. for (stab = stabs; stab <= (stabs + stabsize) - 12; stab += 12)
  145. {
  146. unsigned int strx;
  147. int type;
  148. int other ATTRIBUTE_UNUSED;
  149. int desc;
  150. bfd_vma value;
  151. /* This code presumes 32 bit values. */
  152. strx = bfd_get_32 (abfd, stab);
  153. type = bfd_get_8 (abfd, stab + 4);
  154. other = bfd_get_8 (abfd, stab + 5);
  155. desc = bfd_get_16 (abfd, stab + 6);
  156. value = bfd_get_32 (abfd, stab + 8);
  157. if (type == 0)
  158. {
  159. /* Special type 0 stabs indicate the offset to the
  160. next string table. */
  161. stroff = next_stroff;
  162. next_stroff += value;
  163. }
  164. else
  165. {
  166. size_t len;
  167. char *f, *s;
  168. if (stroff + strx >= strsize)
  169. {
  170. fprintf (stderr, _("%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n"),
  171. bfd_get_filename (abfd), names[i].secname,
  172. (long) (stab - stabs) / 12, strx, type);
  173. continue;
  174. }
  175. s = (char *) strings + stroff + strx;
  176. f = NULL;
  177. /* PR 17512: file: 002-87578-0.001:0.1.
  178. It is possible to craft a file where, without the 'strlen (s) > 0',
  179. an attempt to read the byte before 'strings' would occur. */
  180. while ((len = strlen (s)) > 0
  181. && s[len - 1] == '\\'
  182. && stab + 16 <= stabs + stabsize)
  183. {
  184. char *p;
  185. stab += 12;
  186. p = s + len - 1;
  187. *p = '\0';
  188. strx = stroff + bfd_get_32 (abfd, stab);
  189. if (strx >= strsize)
  190. {
  191. fprintf (stderr, _("%s: %s: stab entry %ld is corrupt\n"),
  192. bfd_get_filename (abfd), names[i].secname,
  193. (long) (stab - stabs) / 12);
  194. break;
  195. }
  196. s = concat (s, (char *) strings + strx,
  197. (const char *) NULL);
  198. /* We have to restore the backslash, because, if
  199. the linker is hashing stabs strings, we may
  200. see the same string more than once. */
  201. *p = '\\';
  202. free (f);
  203. f = s;
  204. }
  205. save_stab (type, desc, value, s);
  206. if (! parse_stab (dhandle, shandle, type, desc, value, s))
  207. {
  208. stab_context ();
  209. free_saved_stabs ();
  210. free (f);
  211. free (shandle);
  212. free (stabs);
  213. free (strings);
  214. return false;
  215. }
  216. /* Don't free f, since I think the stabs code
  217. expects strings to hang around. This should be
  218. straightened out. FIXME. */
  219. }
  220. }
  221. free_saved_stabs ();
  222. free (stabs);
  223. /* Don't free strings, since I think the stabs code expects
  224. the strings to hang around. This should be straightened
  225. out. FIXME. */
  226. }
  227. }
  228. if (shandle != NULL)
  229. {
  230. if (! finish_stab (dhandle, shandle))
  231. return false;
  232. }
  233. return true;
  234. }
  235. /* Read stabs in the symbol table. */
  236. static bool
  237. read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
  238. void *dhandle, bool *pfound)
  239. {
  240. void *shandle;
  241. asymbol **ps, **symend;
  242. shandle = NULL;
  243. symend = syms + symcount;
  244. for (ps = syms; ps < symend; ps++)
  245. {
  246. symbol_info i;
  247. bfd_get_symbol_info (abfd, *ps, &i);
  248. if (i.type == '-')
  249. {
  250. const char *s;
  251. char *f;
  252. if (shandle == NULL)
  253. {
  254. shandle = start_stab (dhandle, abfd, false, syms, symcount);
  255. if (shandle == NULL)
  256. return false;
  257. }
  258. *pfound = true;
  259. s = i.name;
  260. if (s == NULL || strlen (s) < 1)
  261. return false;
  262. f = NULL;
  263. while (strlen (s) > 0
  264. && s[strlen (s) - 1] == '\\'
  265. && ps + 1 < symend)
  266. {
  267. char *sc, *n;
  268. ++ps;
  269. sc = xstrdup (s);
  270. sc[strlen (sc) - 1] = '\0';
  271. n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
  272. free (sc);
  273. free (f);
  274. f = n;
  275. s = n;
  276. }
  277. save_stab (i.stab_type, i.stab_desc, i.value, s);
  278. if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
  279. i.value, s))
  280. {
  281. stab_context ();
  282. free_saved_stabs ();
  283. return false;
  284. }
  285. /* Don't free f, since I think the stabs code expects
  286. strings to hang around. This should be straightened out.
  287. FIXME. */
  288. }
  289. }
  290. free_saved_stabs ();
  291. if (shandle != NULL)
  292. {
  293. if (! finish_stab (dhandle, shandle))
  294. return false;
  295. }
  296. return true;
  297. }
  298. /* Record stabs strings, so that we can give some context for errors. */
  299. #define SAVE_STABS_COUNT (16)
  300. struct saved_stab
  301. {
  302. int type;
  303. int desc;
  304. bfd_vma value;
  305. char *string;
  306. };
  307. static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
  308. static int saved_stabs_index;
  309. /* Save a stabs string. */
  310. static void
  311. save_stab (int type, int desc, bfd_vma value, const char *string)
  312. {
  313. free (saved_stabs[saved_stabs_index].string);
  314. saved_stabs[saved_stabs_index].type = type;
  315. saved_stabs[saved_stabs_index].desc = desc;
  316. saved_stabs[saved_stabs_index].value = value;
  317. saved_stabs[saved_stabs_index].string = xstrdup (string);
  318. saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
  319. }
  320. /* Provide context for an error. */
  321. static void
  322. stab_context (void)
  323. {
  324. int i;
  325. fprintf (stderr, _("Last stabs entries before error:\n"));
  326. fprintf (stderr, "n_type n_desc n_value string\n");
  327. i = saved_stabs_index;
  328. do
  329. {
  330. struct saved_stab *stabp;
  331. stabp = saved_stabs + i;
  332. if (stabp->string != NULL)
  333. {
  334. const char *s;
  335. s = bfd_get_stab_name (stabp->type);
  336. if (s != NULL)
  337. fprintf (stderr, "%-6s", s);
  338. else if (stabp->type == 0)
  339. fprintf (stderr, "HdrSym");
  340. else
  341. fprintf (stderr, "%-6d", stabp->type);
  342. fprintf (stderr, " %-6d ", stabp->desc);
  343. fprintf_vma (stderr, stabp->value);
  344. if (stabp->type != 0)
  345. fprintf (stderr, " %s", stabp->string);
  346. fprintf (stderr, "\n");
  347. }
  348. i = (i + 1) % SAVE_STABS_COUNT;
  349. }
  350. while (i != saved_stabs_index);
  351. }
  352. /* Free the saved stab strings. */
  353. static void
  354. free_saved_stabs (void)
  355. {
  356. int i;
  357. for (i = 0; i < SAVE_STABS_COUNT; i++)
  358. {
  359. free (saved_stabs[i].string);
  360. saved_stabs[i].string = NULL;
  361. }
  362. saved_stabs_index = 0;
  363. }