rddbg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* rddbg.c -- Read debugging information into a generic form.
  2. Copyright (C) 1995-2015 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 bfd_boolean read_section_stabs_debugging_info
  27. (bfd *, asymbol **, long, void *, bfd_boolean *);
  28. static bfd_boolean read_symbol_stabs_debugging_info
  29. (bfd *, asymbol **, long, void *, bfd_boolean *);
  30. static bfd_boolean read_ieee_debugging_info (bfd *, void *, bfd_boolean *);
  31. static void save_stab (int, int, bfd_vma, const char *);
  32. static void stab_context (void);
  33. static void free_saved_stabs (void);
  34. /* Read debugging information from a BFD. Returns a generic debugging
  35. pointer. */
  36. void *
  37. read_debugging_info (bfd *abfd, asymbol **syms, long symcount, bfd_boolean no_messages)
  38. {
  39. void *dhandle;
  40. bfd_boolean 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. return NULL;
  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. return NULL;
  52. }
  53. if (bfd_get_flavour (abfd) == bfd_target_ieee_flavour)
  54. {
  55. if (! read_ieee_debugging_info (abfd, dhandle, &found))
  56. return NULL;
  57. }
  58. /* Try reading the COFF symbols if we didn't find any stabs in COFF
  59. sections. */
  60. if (! found
  61. && bfd_get_flavour (abfd) == bfd_target_coff_flavour
  62. && symcount > 0)
  63. {
  64. if (! parse_coff (abfd, syms, symcount, dhandle))
  65. return NULL;
  66. found = TRUE;
  67. }
  68. if (! found)
  69. {
  70. if (! no_messages)
  71. non_fatal (_("%s: no recognized debugging information"),
  72. bfd_get_filename (abfd));
  73. return NULL;
  74. }
  75. return dhandle;
  76. }
  77. /* Read stabs in sections debugging information from a BFD. */
  78. static bfd_boolean
  79. read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
  80. void *dhandle, bfd_boolean *pfound)
  81. {
  82. static struct
  83. {
  84. const char *secname;
  85. const char *strsecname;
  86. }
  87. names[] =
  88. {
  89. { ".stab", ".stabstr" },
  90. { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" },
  91. { "$GDB_SYMBOLS$", "$GDB_STRINGS$" }
  92. };
  93. unsigned int i;
  94. void *shandle;
  95. *pfound = FALSE;
  96. shandle = NULL;
  97. for (i = 0; i < sizeof names / sizeof names[0]; i++)
  98. {
  99. asection *sec, *strsec;
  100. sec = bfd_get_section_by_name (abfd, names[i].secname);
  101. strsec = bfd_get_section_by_name (abfd, names[i].strsecname);
  102. if (sec != NULL && strsec != NULL)
  103. {
  104. bfd_size_type stabsize, strsize;
  105. bfd_byte *stabs, *strings;
  106. bfd_byte *stab;
  107. bfd_size_type stroff, next_stroff;
  108. stabsize = bfd_section_size (abfd, sec);
  109. stabs = (bfd_byte *) xmalloc (stabsize);
  110. if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
  111. {
  112. fprintf (stderr, "%s: %s: %s\n",
  113. bfd_get_filename (abfd), names[i].secname,
  114. bfd_errmsg (bfd_get_error ()));
  115. return FALSE;
  116. }
  117. strsize = bfd_section_size (abfd, strsec);
  118. strings = (bfd_byte *) xmalloc (strsize + 1);
  119. if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
  120. {
  121. fprintf (stderr, "%s: %s: %s\n",
  122. bfd_get_filename (abfd), names[i].strsecname,
  123. bfd_errmsg (bfd_get_error ()));
  124. return FALSE;
  125. }
  126. /* Zero terminate the strings table, just in case. */
  127. strings [strsize] = 0;
  128. if (shandle == NULL)
  129. {
  130. shandle = start_stab (dhandle, abfd, TRUE, syms, symcount);
  131. if (shandle == NULL)
  132. return FALSE;
  133. }
  134. *pfound = TRUE;
  135. stroff = 0;
  136. next_stroff = 0;
  137. /* PR 17512: file: 078-60391-0.001:0.1. */
  138. for (stab = stabs; stab <= (stabs + stabsize) - 12; stab += 12)
  139. {
  140. unsigned int strx;
  141. int type;
  142. int other ATTRIBUTE_UNUSED;
  143. int desc;
  144. bfd_vma value;
  145. /* This code presumes 32 bit values. */
  146. strx = bfd_get_32 (abfd, stab);
  147. type = bfd_get_8 (abfd, stab + 4);
  148. other = bfd_get_8 (abfd, stab + 5);
  149. desc = bfd_get_16 (abfd, stab + 6);
  150. value = bfd_get_32 (abfd, stab + 8);
  151. if (type == 0)
  152. {
  153. /* Special type 0 stabs indicate the offset to the
  154. next string table. */
  155. stroff = next_stroff;
  156. next_stroff += value;
  157. }
  158. else
  159. {
  160. size_t len;
  161. char *f, *s;
  162. if (stroff + strx >= strsize)
  163. {
  164. fprintf (stderr, _("%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n"),
  165. bfd_get_filename (abfd), names[i].secname,
  166. (long) (stab - stabs) / 12, strx, type);
  167. continue;
  168. }
  169. s = (char *) strings + stroff + strx;
  170. f = NULL;
  171. /* PR 17512: file: 002-87578-0.001:0.1.
  172. It is possible to craft a file where, without the 'strlen (s) > 0',
  173. an attempt to read the byte before 'strings' would occur. */
  174. while ((len = strlen (s)) > 0
  175. && s[len - 1] == '\\'
  176. && stab + 12 < stabs + stabsize)
  177. {
  178. char *p;
  179. stab += 12;
  180. p = s + len - 1;
  181. *p = '\0';
  182. strx = stroff + bfd_get_32 (abfd, stab);
  183. if (strx >= strsize)
  184. {
  185. fprintf (stderr, _("%s: %s: stab entry %ld is corrupt\n"),
  186. bfd_get_filename (abfd), names[i].secname,
  187. (long) (stab - stabs) / 12);
  188. break;
  189. }
  190. else
  191. s = concat (s, (char *) strings + strx,
  192. (const char *) NULL);
  193. /* We have to restore the backslash, because, if
  194. the linker is hashing stabs strings, we may
  195. see the same string more than once. */
  196. *p = '\\';
  197. if (f != NULL)
  198. free (f);
  199. f = s;
  200. }
  201. save_stab (type, desc, value, s);
  202. if (! parse_stab (dhandle, shandle, type, desc, value, s))
  203. {
  204. stab_context ();
  205. free_saved_stabs ();
  206. return FALSE;
  207. }
  208. /* Don't free f, since I think the stabs code
  209. expects strings to hang around. This should be
  210. straightened out. FIXME. */
  211. }
  212. }
  213. free_saved_stabs ();
  214. free (stabs);
  215. /* Don't free strings, since I think the stabs code expects
  216. the strings to hang around. This should be straightened
  217. out. FIXME. */
  218. }
  219. }
  220. if (shandle != NULL)
  221. {
  222. if (! finish_stab (dhandle, shandle))
  223. return FALSE;
  224. }
  225. return TRUE;
  226. }
  227. /* Read stabs in the symbol table. */
  228. static bfd_boolean
  229. read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
  230. void *dhandle, bfd_boolean *pfound)
  231. {
  232. void *shandle;
  233. asymbol **ps, **symend;
  234. shandle = NULL;
  235. symend = syms + symcount;
  236. for (ps = syms; ps < symend; ps++)
  237. {
  238. symbol_info i;
  239. bfd_get_symbol_info (abfd, *ps, &i);
  240. if (i.type == '-')
  241. {
  242. const char *s;
  243. char *f;
  244. if (shandle == NULL)
  245. {
  246. shandle = start_stab (dhandle, abfd, FALSE, syms, symcount);
  247. if (shandle == NULL)
  248. return FALSE;
  249. }
  250. *pfound = TRUE;
  251. s = i.name;
  252. f = NULL;
  253. while (s[strlen (s) - 1] == '\\'
  254. && ps + 1 < symend)
  255. {
  256. char *sc, *n;
  257. ++ps;
  258. sc = xstrdup (s);
  259. sc[strlen (sc) - 1] = '\0';
  260. n = concat (sc, bfd_asymbol_name (*ps), (const char *) NULL);
  261. free (sc);
  262. if (f != NULL)
  263. free (f);
  264. f = n;
  265. s = n;
  266. }
  267. save_stab (i.stab_type, i.stab_desc, i.value, s);
  268. if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc,
  269. i.value, s))
  270. {
  271. stab_context ();
  272. free_saved_stabs ();
  273. return FALSE;
  274. }
  275. /* Don't free f, since I think the stabs code expects
  276. strings to hang around. This should be straightened out.
  277. FIXME. */
  278. }
  279. }
  280. free_saved_stabs ();
  281. if (shandle != NULL)
  282. {
  283. if (! finish_stab (dhandle, shandle))
  284. return FALSE;
  285. }
  286. return TRUE;
  287. }
  288. /* Read IEEE debugging information. */
  289. static bfd_boolean
  290. read_ieee_debugging_info (bfd *abfd, void *dhandle, bfd_boolean *pfound)
  291. {
  292. asection *dsec;
  293. bfd_size_type size;
  294. bfd_byte *contents;
  295. /* The BFD backend puts the debugging information into a section
  296. named .debug. */
  297. dsec = bfd_get_section_by_name (abfd, ".debug");
  298. if (dsec == NULL)
  299. return TRUE;
  300. size = bfd_section_size (abfd, dsec);
  301. contents = (bfd_byte *) xmalloc (size);
  302. if (! bfd_get_section_contents (abfd, dsec, contents, 0, size))
  303. return FALSE;
  304. if (! parse_ieee (dhandle, abfd, contents, size))
  305. return FALSE;
  306. free (contents);
  307. *pfound = TRUE;
  308. return TRUE;
  309. }
  310. /* Record stabs strings, so that we can give some context for errors. */
  311. #define SAVE_STABS_COUNT (16)
  312. struct saved_stab
  313. {
  314. int type;
  315. int desc;
  316. bfd_vma value;
  317. char *string;
  318. };
  319. static struct saved_stab saved_stabs[SAVE_STABS_COUNT];
  320. static int saved_stabs_index;
  321. /* Save a stabs string. */
  322. static void
  323. save_stab (int type, int desc, bfd_vma value, const char *string)
  324. {
  325. if (saved_stabs[saved_stabs_index].string != NULL)
  326. free (saved_stabs[saved_stabs_index].string);
  327. saved_stabs[saved_stabs_index].type = type;
  328. saved_stabs[saved_stabs_index].desc = desc;
  329. saved_stabs[saved_stabs_index].value = value;
  330. saved_stabs[saved_stabs_index].string = xstrdup (string);
  331. saved_stabs_index = (saved_stabs_index + 1) % SAVE_STABS_COUNT;
  332. }
  333. /* Provide context for an error. */
  334. static void
  335. stab_context (void)
  336. {
  337. int i;
  338. fprintf (stderr, _("Last stabs entries before error:\n"));
  339. fprintf (stderr, "n_type n_desc n_value string\n");
  340. i = saved_stabs_index;
  341. do
  342. {
  343. struct saved_stab *stabp;
  344. stabp = saved_stabs + i;
  345. if (stabp->string != NULL)
  346. {
  347. const char *s;
  348. s = bfd_get_stab_name (stabp->type);
  349. if (s != NULL)
  350. fprintf (stderr, "%-6s", s);
  351. else if (stabp->type == 0)
  352. fprintf (stderr, "HdrSym");
  353. else
  354. fprintf (stderr, "%-6d", stabp->type);
  355. fprintf (stderr, " %-6d ", stabp->desc);
  356. fprintf_vma (stderr, stabp->value);
  357. if (stabp->type != 0)
  358. fprintf (stderr, " %s", stabp->string);
  359. fprintf (stderr, "\n");
  360. }
  361. i = (i + 1) % SAVE_STABS_COUNT;
  362. }
  363. while (i != saved_stabs_index);
  364. }
  365. /* Free the saved stab strings. */
  366. static void
  367. free_saved_stabs (void)
  368. {
  369. int i;
  370. for (i = 0; i < SAVE_STABS_COUNT; i++)
  371. {
  372. if (saved_stabs[i].string != NULL)
  373. {
  374. free (saved_stabs[i].string);
  375. saved_stabs[i].string = NULL;
  376. }
  377. }
  378. saved_stabs_index = 0;
  379. }