stabs.c 18 KB

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