stabs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /* Generic stabs parsing for gas.
  2. Copyright (C) 1989-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 3,
  7. or (at your option) any later version.
  8. GAS is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  11. the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "as.h"
  17. #include "filenames.h"
  18. #include "obstack.h"
  19. #include "subsegs.h"
  20. #include "ecoff.h"
  21. /* We need this, despite the apparent object format dependency, since
  22. it defines stab types, which all object formats can use now. */
  23. #include "aout/stab_gnu.h"
  24. /* Holds whether the assembler is generating stabs line debugging
  25. information or not. Potentially used by md_cleanup function. */
  26. int outputting_stabs_line_debug = 0;
  27. static void generate_asm_file (int, const char *);
  28. /* Allow backends to override the names used for the stab sections. */
  29. #ifndef STAB_SECTION_NAME
  30. #define STAB_SECTION_NAME ".stab"
  31. #endif
  32. #ifndef STAB_STRING_SECTION_NAME
  33. #define STAB_STRING_SECTION_NAME ".stabstr"
  34. #endif
  35. /* True if we're in the middle of a .func function, in which case
  36. stabs_generate_asm_lineno emits function relative line number stabs.
  37. Otherwise it emits line number stabs with absolute addresses. Note that
  38. both cases only apply to assembler code assembled with -gstabs. */
  39. static bool in_dot_func_p = false;
  40. /* Label at start of current function if in_dot_func_p != FALSE. */
  41. static const char *current_function_label;
  42. /*
  43. * Handle .stabX directives, which used to be open-coded.
  44. * So much creeping featurism overloaded the semantics that we decided
  45. * to put all .stabX thinking in one place. Here.
  46. *
  47. * We try to make any .stabX directive legal. Other people's AS will often
  48. * do assembly-time consistency checks: eg assigning meaning to n_type bits
  49. * and "protecting" you from setting them to certain values. (They also zero
  50. * certain bits before emitting symbols. Tut tut.)
  51. *
  52. * If an expression is not absolute we either gripe or use the relocation
  53. * information. Other people's assemblers silently forget information they
  54. * don't need and invent information they need that you didn't supply.
  55. */
  56. /*
  57. * Build a string dictionary entry for a .stabX symbol.
  58. * The symbol is added to the .<secname>str section.
  59. */
  60. #ifndef SEPARATE_STAB_SECTIONS
  61. #define SEPARATE_STAB_SECTIONS 0
  62. #endif
  63. unsigned int
  64. get_stab_string_offset (const char *string, const char *stabstr_secname,
  65. bool free_stabstr_secname)
  66. {
  67. unsigned int length;
  68. unsigned int retval;
  69. segT save_seg;
  70. subsegT save_subseg;
  71. segT seg;
  72. char *p;
  73. if (! SEPARATE_STAB_SECTIONS)
  74. abort ();
  75. length = strlen (string);
  76. save_seg = now_seg;
  77. save_subseg = now_subseg;
  78. /* Create the stab string section, if it doesn't already exist. */
  79. seg = subseg_new (stabstr_secname, 0);
  80. if (free_stabstr_secname && seg->name != stabstr_secname)
  81. free ((char *) stabstr_secname);
  82. retval = seg_info (seg)->stabu.stab_string_size;
  83. if (retval <= 0)
  84. {
  85. /* Make sure the first string is empty. */
  86. p = frag_more (1);
  87. *p = 0;
  88. retval = seg_info (seg)->stabu.stab_string_size = 1;
  89. bfd_set_section_flags (seg, SEC_READONLY | SEC_DEBUGGING);
  90. }
  91. if (length > 0)
  92. { /* Ordinary case. */
  93. p = frag_more (length + 1);
  94. strcpy (p, string);
  95. seg_info (seg)->stabu.stab_string_size += length + 1;
  96. }
  97. else
  98. retval = 0;
  99. subseg_set (save_seg, save_subseg);
  100. return retval;
  101. }
  102. #ifdef AOUT_STABS
  103. #ifndef OBJ_PROCESS_STAB
  104. #define OBJ_PROCESS_STAB(SEG,W,S,T,O,D) aout_process_stab(W,S,T,O,D)
  105. #endif
  106. /* Here instead of obj-aout.c because other formats use it too. */
  107. void
  108. aout_process_stab (int what, const char *string, int type, int other, int desc)
  109. {
  110. /* Put the stab information in the symbol table. */
  111. symbolS *symbol;
  112. /* Create the symbol now, but only insert it into the symbol chain
  113. after any symbols mentioned in the value expression get into the
  114. symbol chain. This is to avoid "continuation symbols" (where one
  115. ends in "\" and the debug info is continued in the next .stabs
  116. directive) from being separated by other random symbols. */
  117. symbol = symbol_create (string, undefined_section, &zero_address_frag, 0);
  118. if (what == 's' || what == 'n')
  119. {
  120. /* Pick up the value from the input line. */
  121. pseudo_set (symbol);
  122. }
  123. else
  124. {
  125. /* .stabd sets the name to NULL. Why? */
  126. S_SET_NAME (symbol, NULL);
  127. symbol_set_frag (symbol, frag_now);
  128. S_SET_VALUE (symbol, (valueT) frag_now_fix ());
  129. }
  130. symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
  131. symbol_get_bfdsym (symbol)->flags |= BSF_DEBUGGING;
  132. S_SET_TYPE (symbol, type);
  133. S_SET_OTHER (symbol, other);
  134. S_SET_DESC (symbol, desc);
  135. }
  136. #endif
  137. /* This can handle different kinds of stabs (s,n,d) and different
  138. kinds of stab sections. If STAB_SECNAME_OBSTACK_END is non-NULL,
  139. then STAB_SECNAME and STABSTR_SECNAME will be freed if possible
  140. before this function returns (the former by obstack_free). */
  141. static void
  142. s_stab_generic (int what,
  143. const char *stab_secname,
  144. const char *stabstr_secname,
  145. const char *stab_secname_obstack_end)
  146. {
  147. long longint;
  148. const char *string;
  149. char *saved_string_obstack_end;
  150. int type;
  151. int other;
  152. int desc;
  153. /* The general format is:
  154. .stabs "STRING",TYPE,OTHER,DESC,VALUE
  155. .stabn TYPE,OTHER,DESC,VALUE
  156. .stabd TYPE,OTHER,DESC
  157. At this point input_line_pointer points after the pseudo-op and
  158. any trailing whitespace. The argument what is one of 's', 'n' or
  159. 'd' indicating which type of .stab this is. */
  160. if (what != 's')
  161. {
  162. string = "";
  163. saved_string_obstack_end = 0;
  164. }
  165. else
  166. {
  167. int length;
  168. string = demand_copy_C_string (&length);
  169. if (string == NULL)
  170. {
  171. as_warn (_(".stab%c: missing string"), what);
  172. ignore_rest_of_line ();
  173. return;
  174. }
  175. /* FIXME: We should probably find some other temporary storage
  176. for string, rather than leaking memory if someone else
  177. happens to use the notes obstack. */
  178. saved_string_obstack_end = obstack_next_free (&notes);
  179. SKIP_WHITESPACE ();
  180. if (*input_line_pointer == ',')
  181. input_line_pointer++;
  182. else
  183. {
  184. as_warn (_(".stab%c: missing comma"), what);
  185. ignore_rest_of_line ();
  186. return;
  187. }
  188. }
  189. if (get_absolute_expression_and_terminator (&longint) != ',')
  190. {
  191. as_warn (_(".stab%c: missing comma"), what);
  192. ignore_rest_of_line ();
  193. return;
  194. }
  195. type = longint;
  196. if (get_absolute_expression_and_terminator (&longint) != ',')
  197. {
  198. as_warn (_(".stab%c: missing comma"), what);
  199. ignore_rest_of_line ();
  200. return;
  201. }
  202. other = longint;
  203. desc = get_absolute_expression ();
  204. if ((desc > 0xffff) || (desc < -0x8000))
  205. /* This could happen for example with a source file with a huge
  206. number of lines. The only cure is to use a different debug
  207. format, probably DWARF. */
  208. as_warn (_(".stab%c: description field '%x' too big, try a different debug format"),
  209. what, desc);
  210. if (what == 's' || what == 'n')
  211. {
  212. if (*input_line_pointer != ',')
  213. {
  214. as_warn (_(".stab%c: missing comma"), what);
  215. ignore_rest_of_line ();
  216. return;
  217. }
  218. input_line_pointer++;
  219. SKIP_WHITESPACE ();
  220. }
  221. #ifdef TC_PPC
  222. #ifdef OBJ_ELF
  223. /* Solaris on PowerPC has decided that .stabd can take 4 arguments, so if we were
  224. given 4 arguments, make it a .stabn */
  225. else if (what == 'd')
  226. {
  227. char *save_location = input_line_pointer;
  228. SKIP_WHITESPACE ();
  229. if (*input_line_pointer == ',')
  230. {
  231. input_line_pointer++;
  232. what = 'n';
  233. }
  234. else
  235. input_line_pointer = save_location;
  236. }
  237. #endif /* OBJ_ELF */
  238. #endif /* TC_PPC */
  239. #ifndef NO_LISTING
  240. if (listing)
  241. {
  242. switch (type)
  243. {
  244. case N_SLINE:
  245. listing_source_line ((unsigned int) desc);
  246. break;
  247. case N_SO:
  248. case N_SOL:
  249. listing_source_file (string);
  250. break;
  251. }
  252. }
  253. #endif /* ! NO_LISTING */
  254. /* We have now gathered the type, other, and desc information. For
  255. .stabs or .stabn, input_line_pointer is now pointing at the
  256. value. */
  257. if (SEPARATE_STAB_SECTIONS)
  258. /* Output the stab information in a separate section. This is used
  259. at least for COFF and ELF. */
  260. {
  261. segT saved_seg = now_seg;
  262. subsegT saved_subseg = now_subseg;
  263. fragS *saved_frag = frag_now;
  264. valueT dot;
  265. segT seg;
  266. unsigned int stroff;
  267. char *p;
  268. static segT cached_sec;
  269. dot = frag_now_fix ();
  270. #ifdef md_flush_pending_output
  271. md_flush_pending_output ();
  272. #endif
  273. if (cached_sec && strcmp (cached_sec->name, stab_secname) == 0)
  274. {
  275. seg = cached_sec;
  276. subseg_set (seg, 0);
  277. }
  278. else
  279. {
  280. seg = subseg_new (stab_secname, 0);
  281. cached_sec = seg;
  282. }
  283. if (! seg_info (seg)->hadone)
  284. {
  285. bfd_set_section_flags (seg,
  286. SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
  287. #ifdef INIT_STAB_SECTION
  288. INIT_STAB_SECTION (seg);
  289. #endif
  290. seg_info (seg)->hadone = 1;
  291. }
  292. stroff = get_stab_string_offset (string, stabstr_secname,
  293. stab_secname_obstack_end != NULL);
  294. /* Release the string, if nobody else has used the obstack. */
  295. if (saved_string_obstack_end != NULL
  296. && saved_string_obstack_end == obstack_next_free (&notes))
  297. obstack_free (&notes, string);
  298. /* Similarly for the section name. This must be done before
  299. creating symbols below, which uses the notes obstack. */
  300. if (seg->name != stab_secname
  301. && stab_secname_obstack_end != NULL
  302. && stab_secname_obstack_end == obstack_next_free (&notes))
  303. obstack_free (&notes, stab_secname);
  304. /* At least for now, stabs in a special stab section are always
  305. output as 12 byte blocks of information. */
  306. p = frag_more (8);
  307. md_number_to_chars (p, (valueT) stroff, 4);
  308. md_number_to_chars (p + 4, (valueT) type, 1);
  309. md_number_to_chars (p + 5, (valueT) other, 1);
  310. md_number_to_chars (p + 6, (valueT) desc, 2);
  311. if (what == 's' || what == 'n')
  312. {
  313. /* Pick up the value from the input line. */
  314. cons (4);
  315. input_line_pointer--;
  316. }
  317. else
  318. {
  319. symbolS *symbol;
  320. expressionS exp;
  321. /* Arrange for a value representing the current location. */
  322. symbol = symbol_temp_new (saved_seg, saved_frag, dot);
  323. exp.X_op = O_symbol;
  324. exp.X_add_symbol = symbol;
  325. exp.X_add_number = 0;
  326. emit_expr (&exp, 4);
  327. }
  328. #ifdef OBJ_PROCESS_STAB
  329. OBJ_PROCESS_STAB (seg, what, string, type, other, desc);
  330. #endif
  331. subseg_set (saved_seg, saved_subseg);
  332. }
  333. else
  334. {
  335. if (stab_secname_obstack_end != NULL)
  336. {
  337. free ((char *) stabstr_secname);
  338. if (stab_secname_obstack_end == obstack_next_free (&notes))
  339. obstack_free (&notes, stab_secname);
  340. }
  341. #ifdef OBJ_PROCESS_STAB
  342. OBJ_PROCESS_STAB (0, what, string, type, other, desc);
  343. #else
  344. abort ();
  345. #endif
  346. }
  347. demand_empty_rest_of_line ();
  348. }
  349. /* Regular stab directive. */
  350. void
  351. s_stab (int what)
  352. {
  353. s_stab_generic (what, STAB_SECTION_NAME, STAB_STRING_SECTION_NAME, NULL);
  354. }
  355. /* "Extended stabs", used in Solaris only now. */
  356. void
  357. s_xstab (int what)
  358. {
  359. int length;
  360. char *stab_secname, *stabstr_secname, *stab_secname_obstack_end;
  361. stab_secname = demand_copy_C_string (&length);
  362. stab_secname_obstack_end = obstack_next_free (&notes);
  363. SKIP_WHITESPACE ();
  364. if (*input_line_pointer == ',')
  365. input_line_pointer++;
  366. else
  367. {
  368. as_bad (_("comma missing in .xstabs"));
  369. ignore_rest_of_line ();
  370. return;
  371. }
  372. /* To get the name of the stab string section, simply add "str" to
  373. the stab section name. */
  374. stabstr_secname = concat (stab_secname, "str", (char *) NULL);
  375. s_stab_generic (what, stab_secname, stabstr_secname,
  376. stab_secname_obstack_end);
  377. }
  378. #ifdef S_SET_DESC
  379. /* Frob invented at RMS' request. Set the n_desc of a symbol. */
  380. void
  381. s_desc (int ignore ATTRIBUTE_UNUSED)
  382. {
  383. char *name;
  384. char c;
  385. char *p;
  386. symbolS *symbolP;
  387. int temp;
  388. c = get_symbol_name (&name);
  389. p = input_line_pointer;
  390. *p = c;
  391. SKIP_WHITESPACE_AFTER_NAME ();
  392. if (*input_line_pointer != ',')
  393. {
  394. *p = 0;
  395. as_bad (_("expected comma after \"%s\""), name);
  396. *p = c;
  397. ignore_rest_of_line ();
  398. }
  399. else
  400. {
  401. input_line_pointer++;
  402. temp = get_absolute_expression ();
  403. *p = 0;
  404. symbolP = symbol_find_or_make (name);
  405. *p = c;
  406. S_SET_DESC (symbolP, temp);
  407. }
  408. demand_empty_rest_of_line ();
  409. } /* s_desc() */
  410. #endif /* defined (S_SET_DESC) */
  411. /* Generate stabs debugging information to denote the main source file. */
  412. void
  413. stabs_generate_asm_file (void)
  414. {
  415. const char *file;
  416. unsigned int lineno;
  417. file = as_where (&lineno);
  418. if (use_gnu_debug_info_extensions)
  419. {
  420. const char *dir;
  421. char *dir2;
  422. dir = remap_debug_filename (getpwd ());
  423. dir2 = concat (dir, "/", NULL);
  424. generate_asm_file (N_SO, dir2);
  425. free (dir2);
  426. xfree ((char *) dir);
  427. }
  428. generate_asm_file (N_SO, file);
  429. }
  430. /* Generate stabs debugging information to denote the source file.
  431. TYPE is one of N_SO, N_SOL. */
  432. static void
  433. generate_asm_file (int type, const char *file)
  434. {
  435. static char *last_file;
  436. static int label_count;
  437. char sym[30];
  438. char *buf;
  439. const char *tmp = file;
  440. const char *file_endp = file + strlen (file);
  441. char *bufp;
  442. if (last_file != NULL
  443. && filename_cmp (last_file, file) == 0)
  444. return;
  445. /* Rather than try to do this in some efficient fashion, we just
  446. generate a string and then parse it again. That lets us use the
  447. existing stabs hook, which expect to see a string, rather than
  448. inventing new ones. */
  449. sprintf (sym, "%sF%d", FAKE_LABEL_NAME, label_count);
  450. ++label_count;
  451. /* Allocate enough space for the file name (possibly extended with
  452. doubled up backslashes), the symbol name, and the other characters
  453. that make up a stabs file directive. */
  454. bufp = buf = XNEWVEC (char, 2 * strlen (file) + strlen (sym) + 12);
  455. *bufp++ = '"';
  456. while (tmp < file_endp)
  457. {
  458. const char *bslash = strchr (tmp, '\\');
  459. size_t len = bslash != NULL ? bslash - tmp + 1 : file_endp - tmp;
  460. /* Double all backslashes, since demand_copy_C_string (used by
  461. s_stab to extract the part in quotes) will try to replace them as
  462. escape sequences. backslash may appear in a filespec. */
  463. memcpy (bufp, tmp, len);
  464. tmp += len;
  465. bufp += len;
  466. if (bslash != NULL)
  467. *bufp++ = '\\';
  468. }
  469. sprintf (bufp, "\",%d,0,0,%s\n", type, sym);
  470. temp_ilp (buf);
  471. s_stab ('s');
  472. restore_ilp ();
  473. colon (sym);
  474. free (last_file);
  475. last_file = xstrdup (file);
  476. free (buf);
  477. }
  478. /* Generate stabs debugging information for the current line. This is
  479. used to produce debugging information for an assembler file. */
  480. void
  481. stabs_generate_asm_lineno (void)
  482. {
  483. static int label_count;
  484. const char *file;
  485. unsigned int lineno;
  486. char *buf;
  487. char sym[30];
  488. /* Remember the last file/line and avoid duplicates. */
  489. static unsigned int prev_lineno = -1;
  490. static char *prev_file = NULL;
  491. /* Rather than try to do this in some efficient fashion, we just
  492. generate a string and then parse it again. That lets us use the
  493. existing stabs hook, which expect to see a string, rather than
  494. inventing new ones. */
  495. file = as_where (&lineno);
  496. /* Don't emit sequences of stabs for the same line. */
  497. if (prev_file == NULL)
  498. {
  499. /* First time through. */
  500. prev_file = xstrdup (file);
  501. prev_lineno = lineno;
  502. }
  503. else if (lineno == prev_lineno
  504. && filename_cmp (file, prev_file) == 0)
  505. {
  506. /* Same file/line as last time. */
  507. return;
  508. }
  509. else
  510. {
  511. /* Remember file/line for next time. */
  512. prev_lineno = lineno;
  513. if (filename_cmp (file, prev_file) != 0)
  514. {
  515. free (prev_file);
  516. prev_file = xstrdup (file);
  517. }
  518. }
  519. /* Let the world know that we are in the middle of generating a
  520. piece of stabs line debugging information. */
  521. outputting_stabs_line_debug = 1;
  522. generate_asm_file (N_SOL, file);
  523. sprintf (sym, "%sL%d", FAKE_LABEL_NAME, label_count);
  524. ++label_count;
  525. if (in_dot_func_p)
  526. {
  527. buf = XNEWVEC (char, 100 + strlen (current_function_label));
  528. sprintf (buf, "%d,0,%d,%s-%s\n", N_SLINE, lineno,
  529. sym, current_function_label);
  530. }
  531. else
  532. {
  533. buf = XNEWVEC (char, 100);
  534. sprintf (buf, "%d,0,%d,%s\n", N_SLINE, lineno, sym);
  535. }
  536. temp_ilp (buf);
  537. s_stab ('n');
  538. restore_ilp ();
  539. colon (sym);
  540. outputting_stabs_line_debug = 0;
  541. free (buf);
  542. }
  543. /* Emit a function stab.
  544. All assembler functions are assumed to have return type `void'. */
  545. void
  546. stabs_generate_asm_func (const char *funcname, const char *startlabname)
  547. {
  548. static bool void_emitted_p = false;
  549. char *buf;
  550. unsigned int lineno;
  551. if (! void_emitted_p)
  552. {
  553. temp_ilp ((char *) "\"void:t1=1\",128,0,0,0");
  554. s_stab ('s');
  555. restore_ilp ();
  556. void_emitted_p = true;
  557. }
  558. as_where (&lineno);
  559. if (asprintf (&buf, "\"%s:F1\",%d,0,%d,%s",
  560. funcname, N_FUN, lineno + 1, startlabname) == -1)
  561. as_fatal ("%s", xstrerror (errno));
  562. temp_ilp (buf);
  563. s_stab ('s');
  564. restore_ilp ();
  565. free (buf);
  566. current_function_label = xstrdup (startlabname);
  567. in_dot_func_p = true;
  568. }
  569. /* Emit a stab to record the end of a function. */
  570. void
  571. stabs_generate_asm_endfunc (const char *funcname ATTRIBUTE_UNUSED,
  572. const char *startlabname)
  573. {
  574. static int label_count;
  575. char *buf;
  576. char sym[30];
  577. sprintf (sym, "%sendfunc%d", FAKE_LABEL_NAME, label_count);
  578. ++label_count;
  579. colon (sym);
  580. if (asprintf (&buf, "\"\",%d,0,0,%s-%s", N_FUN, sym, startlabname) == -1)
  581. as_fatal ("%s", xstrerror (errno));
  582. temp_ilp (buf);
  583. s_stab ('s');
  584. restore_ilp ();
  585. free (buf);
  586. in_dot_func_p = false;
  587. current_function_label = NULL;
  588. }