stabs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /* Stabs in sections linking support.
  2. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  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,
  16. MA 02110-1301, USA. */
  17. /* This file contains support for linking stabs in sections, as used
  18. on COFF and ELF. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "libbfd.h"
  22. #include "aout/stab_gnu.h"
  23. #include "safe-ctype.h"
  24. /* Stabs entries use a 12 byte format:
  25. 4 byte string table index
  26. 1 byte stab type
  27. 1 byte stab other field
  28. 2 byte stab desc field
  29. 4 byte stab value
  30. FIXME: This will have to change for a 64 bit object format.
  31. The stabs symbols are divided into compilation units. For the
  32. first entry in each unit, the type of 0, the value is the length of
  33. the string table for this unit, and the desc field is the number of
  34. stabs symbols for this unit. */
  35. #define STRDXOFF 0
  36. #define TYPEOFF 4
  37. #define OTHEROFF 5
  38. #define DESCOFF 6
  39. #define VALOFF 8
  40. #define STABSIZE 12
  41. /* A linked list of totals that we have found for a particular header
  42. file. A total is a unique identifier for a particular BINCL...EINCL
  43. sequence of STABs that can be used to identify duplicate sequences.
  44. It consists of three fields, 'sum_chars' which is the sum of all the
  45. STABS characters; 'num_chars' which is the number of these charactes
  46. and 'symb' which is a buffer of all the symbols in the sequence. This
  47. buffer is only checked as a last resort. */
  48. struct stab_link_includes_totals
  49. {
  50. struct stab_link_includes_totals *next;
  51. bfd_vma sum_chars; /* Accumulated sum of STABS characters. */
  52. bfd_vma num_chars; /* Number of STABS characters. */
  53. const char* symb; /* The STABS characters themselves. */
  54. };
  55. /* An entry in the header file hash table. */
  56. struct stab_link_includes_entry
  57. {
  58. struct bfd_hash_entry root;
  59. /* List of totals we have found for this file. */
  60. struct stab_link_includes_totals *totals;
  61. };
  62. /* This structure is used to hold a list of N_BINCL symbols, some of
  63. which might be converted into N_EXCL symbols. */
  64. struct stab_excl_list
  65. {
  66. /* The next symbol to convert. */
  67. struct stab_excl_list *next;
  68. /* The offset to this symbol in the section contents. */
  69. bfd_size_type offset;
  70. /* The value to use for the symbol. */
  71. bfd_vma val;
  72. /* The type of this symbol (N_BINCL or N_EXCL). */
  73. int type;
  74. };
  75. /* This structure is stored with each .stab section. */
  76. struct stab_section_info
  77. {
  78. /* This is a linked list of N_BINCL symbols which should be
  79. converted into N_EXCL symbols. */
  80. struct stab_excl_list *excls;
  81. /* This is used to map input stab offsets within their sections
  82. to output stab offsets, to take into account stabs that have
  83. been deleted. If it is NULL, the output offsets are the same
  84. as the input offsets, because no stabs have been deleted from
  85. this section. Otherwise the i'th entry is the number of
  86. bytes of stabs that have been deleted prior to the i'th
  87. stab. */
  88. bfd_size_type *cumulative_skips;
  89. /* This is an array of string indices. For each stab symbol, we
  90. store the string index here. If a stab symbol should not be
  91. included in the final output, the string index is -1. */
  92. bfd_size_type stridxs[1];
  93. };
  94. /* The function to create a new entry in the header file hash table. */
  95. static struct bfd_hash_entry *
  96. stab_link_includes_newfunc (struct bfd_hash_entry *entry,
  97. struct bfd_hash_table *table,
  98. const char *string)
  99. {
  100. struct stab_link_includes_entry *ret =
  101. (struct stab_link_includes_entry *) entry;
  102. /* Allocate the structure if it has not already been allocated by a
  103. subclass. */
  104. if (ret == NULL)
  105. ret = (struct stab_link_includes_entry *)
  106. bfd_hash_allocate (table, sizeof (struct stab_link_includes_entry));
  107. if (ret == NULL)
  108. return NULL;
  109. /* Call the allocation method of the superclass. */
  110. ret = ((struct stab_link_includes_entry *)
  111. bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  112. if (ret)
  113. /* Set local fields. */
  114. ret->totals = NULL;
  115. return (struct bfd_hash_entry *) ret;
  116. }
  117. /* This function is called for each input file from the add_symbols
  118. pass of the linker. */
  119. bool
  120. _bfd_link_section_stabs (bfd *abfd,
  121. struct stab_info *sinfo,
  122. asection *stabsec,
  123. asection *stabstrsec,
  124. void * *psecinfo,
  125. bfd_size_type *pstring_offset)
  126. {
  127. bool first;
  128. bfd_size_type count, amt;
  129. struct stab_section_info *secinfo;
  130. bfd_byte *stabbuf = NULL;
  131. bfd_byte *stabstrbuf = NULL;
  132. bfd_byte *sym, *symend;
  133. bfd_size_type stroff, next_stroff, skip;
  134. bfd_size_type *pstridx;
  135. if (stabsec->size == 0
  136. || stabstrsec->size == 0)
  137. /* This file does not contain stabs debugging information. */
  138. return true;
  139. if (stabsec->size % STABSIZE != 0)
  140. /* Something is wrong with the format of these stab symbols.
  141. Don't try to optimize them. */
  142. return true;
  143. if ((stabstrsec->flags & SEC_RELOC) != 0)
  144. /* We shouldn't see relocations in the strings, and we aren't
  145. prepared to handle them. */
  146. return true;
  147. if (bfd_is_abs_section (stabsec->output_section)
  148. || bfd_is_abs_section (stabstrsec->output_section))
  149. /* At least one of the sections is being discarded from the
  150. link, so we should just ignore them. */
  151. return true;
  152. first = false;
  153. if (sinfo->stabstr == NULL)
  154. {
  155. flagword flags;
  156. /* Initialize the stabs information we need to keep track of. */
  157. first = true;
  158. sinfo->strings = _bfd_stringtab_init ();
  159. if (sinfo->strings == NULL)
  160. goto error_return;
  161. /* Make sure the first byte is zero. */
  162. (void) _bfd_stringtab_add (sinfo->strings, "", true, true);
  163. if (! bfd_hash_table_init (&sinfo->includes,
  164. stab_link_includes_newfunc,
  165. sizeof (struct stab_link_includes_entry)))
  166. goto error_return;
  167. flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING
  168. | SEC_LINKER_CREATED);
  169. sinfo->stabstr = bfd_make_section_anyway_with_flags (abfd, ".stabstr",
  170. flags);
  171. if (sinfo->stabstr == NULL)
  172. goto error_return;
  173. }
  174. /* Initialize the information we are going to store for this .stab
  175. section. */
  176. count = stabsec->size / STABSIZE;
  177. amt = sizeof (struct stab_section_info);
  178. amt += (count - 1) * sizeof (bfd_size_type);
  179. *psecinfo = bfd_alloc (abfd, amt);
  180. if (*psecinfo == NULL)
  181. goto error_return;
  182. secinfo = (struct stab_section_info *) *psecinfo;
  183. secinfo->excls = NULL;
  184. stabsec->rawsize = stabsec->size;
  185. secinfo->cumulative_skips = NULL;
  186. memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type));
  187. /* Read the stabs information from abfd. */
  188. if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf)
  189. || !bfd_malloc_and_get_section (abfd, stabstrsec, &stabstrbuf))
  190. goto error_return;
  191. /* Look through the stabs symbols, work out the new string indices,
  192. and identify N_BINCL symbols which can be eliminated. */
  193. stroff = 0;
  194. /* The stabs sections can be split when
  195. -split-by-reloc/-split-by-file is used. We must keep track of
  196. each stab section's place in the single concatenated string
  197. table. */
  198. next_stroff = pstring_offset ? *pstring_offset : 0;
  199. skip = 0;
  200. symend = stabbuf + stabsec->size;
  201. for (sym = stabbuf, pstridx = secinfo->stridxs;
  202. sym < symend;
  203. sym += STABSIZE, ++pstridx)
  204. {
  205. bfd_size_type symstroff;
  206. int type;
  207. const char *string;
  208. if (*pstridx != 0)
  209. /* This symbol has already been handled by an N_BINCL pass. */
  210. continue;
  211. type = sym[TYPEOFF];
  212. if (type == 0)
  213. {
  214. /* Special type 0 stabs indicate the offset to the next
  215. string table. We only copy the very first one. */
  216. stroff = next_stroff;
  217. next_stroff += bfd_get_32 (abfd, sym + 8);
  218. if (pstring_offset)
  219. *pstring_offset = next_stroff;
  220. if (! first)
  221. {
  222. *pstridx = (bfd_size_type) -1;
  223. ++skip;
  224. continue;
  225. }
  226. first = false;
  227. }
  228. /* Store the string in the hash table, and record the index. */
  229. symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF);
  230. if (symstroff >= stabstrsec->size)
  231. {
  232. _bfd_error_handler
  233. /* xgettext:c-format */
  234. (_("%pB(%pA+%#lx): stabs entry has invalid string index"),
  235. abfd, stabsec, (long) (sym - stabbuf));
  236. bfd_set_error (bfd_error_bad_value);
  237. goto error_return;
  238. }
  239. string = (char *) stabstrbuf + symstroff;
  240. *pstridx = _bfd_stringtab_add (sinfo->strings, string, true, true);
  241. /* An N_BINCL symbol indicates the start of the stabs entries
  242. for a header file. We need to scan ahead to the next N_EINCL
  243. symbol, ignoring nesting, adding up all the characters in the
  244. symbol names, not including the file numbers in types (the
  245. first number after an open parenthesis). */
  246. if (type == (int) N_BINCL)
  247. {
  248. bfd_vma sum_chars;
  249. bfd_vma num_chars;
  250. bfd_vma buf_len = 0;
  251. char * symb;
  252. char * symb_rover;
  253. int nest;
  254. bfd_byte * incl_sym;
  255. struct stab_link_includes_entry * incl_entry;
  256. struct stab_link_includes_totals * t;
  257. struct stab_excl_list * ne;
  258. symb = symb_rover = NULL;
  259. sum_chars = num_chars = 0;
  260. nest = 0;
  261. for (incl_sym = sym + STABSIZE;
  262. incl_sym < symend;
  263. incl_sym += STABSIZE)
  264. {
  265. int incl_type;
  266. incl_type = incl_sym[TYPEOFF];
  267. if (incl_type == 0)
  268. break;
  269. else if (incl_type == (int) N_EXCL)
  270. continue;
  271. else if (incl_type == (int) N_EINCL)
  272. {
  273. if (nest == 0)
  274. break;
  275. --nest;
  276. }
  277. else if (incl_type == (int) N_BINCL)
  278. ++nest;
  279. else if (nest == 0)
  280. {
  281. const char *str;
  282. str = ((char *) stabstrbuf
  283. + stroff
  284. + bfd_get_32 (abfd, incl_sym + STRDXOFF));
  285. for (; *str != '\0'; str++)
  286. {
  287. if (num_chars >= buf_len)
  288. {
  289. buf_len += 32 * 1024;
  290. symb = (char *) bfd_realloc_or_free (symb, buf_len);
  291. if (symb == NULL)
  292. goto error_return;
  293. symb_rover = symb + num_chars;
  294. }
  295. * symb_rover ++ = * str;
  296. sum_chars += *str;
  297. num_chars ++;
  298. if (*str == '(')
  299. {
  300. /* Skip the file number. */
  301. ++str;
  302. while (ISDIGIT (*str))
  303. ++str;
  304. --str;
  305. }
  306. }
  307. }
  308. }
  309. BFD_ASSERT (num_chars == (bfd_vma) (symb_rover - symb));
  310. /* If we have already included a header file with the same
  311. value, then replaced this one with an N_EXCL symbol. */
  312. incl_entry = (struct stab_link_includes_entry * )
  313. bfd_hash_lookup (&sinfo->includes, string, true, true);
  314. if (incl_entry == NULL)
  315. goto error_return;
  316. for (t = incl_entry->totals; t != NULL; t = t->next)
  317. if (t->sum_chars == sum_chars
  318. && t->num_chars == num_chars
  319. && memcmp (t->symb, symb, num_chars) == 0)
  320. break;
  321. /* Record this symbol, so that we can set the value
  322. correctly. */
  323. amt = sizeof *ne;
  324. ne = (struct stab_excl_list *) bfd_alloc (abfd, amt);
  325. if (ne == NULL)
  326. goto error_return;
  327. ne->offset = sym - stabbuf;
  328. ne->val = sum_chars;
  329. ne->type = (int) N_BINCL;
  330. ne->next = secinfo->excls;
  331. secinfo->excls = ne;
  332. if (t == NULL)
  333. {
  334. /* This is the first time we have seen this header file
  335. with this set of stabs strings. */
  336. t = (struct stab_link_includes_totals *)
  337. bfd_hash_allocate (&sinfo->includes, sizeof *t);
  338. if (t == NULL)
  339. goto error_return;
  340. t->sum_chars = sum_chars;
  341. t->num_chars = num_chars;
  342. /* Trim data down. */
  343. t->symb = symb = (char *) bfd_realloc_or_free (symb, num_chars);
  344. t->next = incl_entry->totals;
  345. incl_entry->totals = t;
  346. }
  347. else
  348. {
  349. bfd_size_type *incl_pstridx;
  350. /* We have seen this header file before. Tell the final
  351. pass to change the type to N_EXCL. */
  352. ne->type = (int) N_EXCL;
  353. /* Free off superfluous symbols. */
  354. free (symb);
  355. /* Mark the skipped symbols. */
  356. nest = 0;
  357. for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1;
  358. incl_sym < symend;
  359. incl_sym += STABSIZE, ++incl_pstridx)
  360. {
  361. int incl_type;
  362. incl_type = incl_sym[TYPEOFF];
  363. if (incl_type == (int) N_EINCL)
  364. {
  365. if (nest == 0)
  366. {
  367. *incl_pstridx = (bfd_size_type) -1;
  368. ++skip;
  369. break;
  370. }
  371. --nest;
  372. }
  373. else if (incl_type == (int) N_BINCL)
  374. ++nest;
  375. else if (incl_type == (int) N_EXCL)
  376. /* Keep existing exclusion marks. */
  377. continue;
  378. else if (nest == 0)
  379. {
  380. *incl_pstridx = (bfd_size_type) -1;
  381. ++skip;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. free (stabbuf);
  388. stabbuf = NULL;
  389. free (stabstrbuf);
  390. stabstrbuf = NULL;
  391. /* We need to set the section sizes such that the linker will
  392. compute the output section sizes correctly. We set the .stab
  393. size to not include the entries we don't want. We set
  394. SEC_EXCLUDE for the .stabstr section, so that it will be dropped
  395. from the link. We record the size of the strtab in the first
  396. .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
  397. for that section. */
  398. stabsec->size = (count - skip) * STABSIZE;
  399. if (stabsec->size == 0)
  400. stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
  401. stabstrsec->flags |= SEC_EXCLUDE | SEC_KEEP;
  402. sinfo->stabstr->size = _bfd_stringtab_size (sinfo->strings);
  403. /* Calculate the `cumulative_skips' array now that stabs have been
  404. deleted for this section. */
  405. if (skip != 0)
  406. {
  407. bfd_size_type i, offset;
  408. bfd_size_type *pskips;
  409. amt = count * sizeof (bfd_size_type);
  410. secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
  411. if (secinfo->cumulative_skips == NULL)
  412. goto error_return;
  413. pskips = secinfo->cumulative_skips;
  414. pstridx = secinfo->stridxs;
  415. offset = 0;
  416. for (i = 0; i < count; i++, pskips++, pstridx++)
  417. {
  418. *pskips = offset;
  419. if (*pstridx == (bfd_size_type) -1)
  420. offset += STABSIZE;
  421. }
  422. BFD_ASSERT (offset != 0);
  423. }
  424. return true;
  425. error_return:
  426. free (stabbuf);
  427. free (stabstrbuf);
  428. return false;
  429. }
  430. /* This function is called for each input file before the stab
  431. section is relocated. It discards stab entries for discarded
  432. functions and variables. The function returns TRUE iff
  433. any entries have been deleted.
  434. */
  435. bool
  436. _bfd_discard_section_stabs (bfd *abfd,
  437. asection *stabsec,
  438. void * psecinfo,
  439. bool (*reloc_symbol_deleted_p) (bfd_vma, void *),
  440. void * cookie)
  441. {
  442. bfd_size_type count, amt;
  443. struct stab_section_info *secinfo;
  444. bfd_byte *stabbuf = NULL;
  445. bfd_byte *sym, *symend;
  446. bfd_size_type skip;
  447. bfd_size_type *pstridx;
  448. int deleting;
  449. if (stabsec->size == 0)
  450. /* This file does not contain stabs debugging information. */
  451. return false;
  452. if (stabsec->size % STABSIZE != 0)
  453. /* Something is wrong with the format of these stab symbols.
  454. Don't try to optimize them. */
  455. return false;
  456. if ((stabsec->output_section != NULL
  457. && bfd_is_abs_section (stabsec->output_section)))
  458. /* At least one of the sections is being discarded from the
  459. link, so we should just ignore them. */
  460. return false;
  461. /* We should have initialized our data in _bfd_link_section_stabs.
  462. If there was some bizarre error reading the string sections, though,
  463. we might not have. Bail rather than asserting. */
  464. if (psecinfo == NULL)
  465. return false;
  466. count = stabsec->rawsize / STABSIZE;
  467. secinfo = (struct stab_section_info *) psecinfo;
  468. /* Read the stabs information from abfd. */
  469. if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf))
  470. goto error_return;
  471. /* Look through the stabs symbols and discard any information for
  472. discarded functions. */
  473. skip = 0;
  474. deleting = -1;
  475. symend = stabbuf + stabsec->rawsize;
  476. for (sym = stabbuf, pstridx = secinfo->stridxs;
  477. sym < symend;
  478. sym += STABSIZE, ++pstridx)
  479. {
  480. int type;
  481. if (*pstridx == (bfd_size_type) -1)
  482. /* This stab was deleted in a previous pass. */
  483. continue;
  484. type = sym[TYPEOFF];
  485. if (type == (int) N_FUN)
  486. {
  487. int strx = bfd_get_32 (abfd, sym + STRDXOFF);
  488. if (strx == 0)
  489. {
  490. if (deleting)
  491. {
  492. skip++;
  493. *pstridx = -1;
  494. }
  495. deleting = -1;
  496. continue;
  497. }
  498. deleting = 0;
  499. if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
  500. deleting = 1;
  501. }
  502. if (deleting == 1)
  503. {
  504. *pstridx = -1;
  505. skip++;
  506. }
  507. else if (deleting == -1)
  508. {
  509. /* Outside of a function. Check for deleted variables. */
  510. if (type == (int) N_STSYM || type == (int) N_LCSYM)
  511. if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
  512. {
  513. *pstridx = -1;
  514. skip ++;
  515. }
  516. /* We should also check for N_GSYM entries which reference a
  517. deleted global, but those are less harmful to debuggers
  518. and would require parsing the stab strings. */
  519. }
  520. }
  521. free (stabbuf);
  522. stabbuf = NULL;
  523. /* Shrink the stabsec as needed. */
  524. stabsec->size -= skip * STABSIZE;
  525. if (stabsec->size == 0)
  526. stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
  527. /* Recalculate the `cumulative_skips' array now that stabs have been
  528. deleted for this section. */
  529. if (skip != 0)
  530. {
  531. bfd_size_type i, offset;
  532. bfd_size_type *pskips;
  533. if (secinfo->cumulative_skips == NULL)
  534. {
  535. amt = count * sizeof (bfd_size_type);
  536. secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
  537. if (secinfo->cumulative_skips == NULL)
  538. goto error_return;
  539. }
  540. pskips = secinfo->cumulative_skips;
  541. pstridx = secinfo->stridxs;
  542. offset = 0;
  543. for (i = 0; i < count; i++, pskips++, pstridx++)
  544. {
  545. *pskips = offset;
  546. if (*pstridx == (bfd_size_type) -1)
  547. offset += STABSIZE;
  548. }
  549. BFD_ASSERT (offset != 0);
  550. }
  551. return skip > 0;
  552. error_return:
  553. free (stabbuf);
  554. return false;
  555. }
  556. /* Write out the stab section. This is called with the relocated
  557. contents. */
  558. bool
  559. _bfd_write_section_stabs (bfd *output_bfd,
  560. struct stab_info *sinfo,
  561. asection *stabsec,
  562. void * *psecinfo,
  563. bfd_byte *contents)
  564. {
  565. struct stab_section_info *secinfo;
  566. struct stab_excl_list *e;
  567. bfd_byte *sym, *tosym, *symend;
  568. bfd_size_type *pstridx;
  569. secinfo = (struct stab_section_info *) *psecinfo;
  570. if (secinfo == NULL)
  571. return bfd_set_section_contents (output_bfd, stabsec->output_section,
  572. contents, stabsec->output_offset,
  573. stabsec->size);
  574. /* Handle each N_BINCL entry. */
  575. for (e = secinfo->excls; e != NULL; e = e->next)
  576. {
  577. bfd_byte *excl_sym;
  578. BFD_ASSERT (e->offset < stabsec->rawsize);
  579. excl_sym = contents + e->offset;
  580. bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF);
  581. excl_sym[TYPEOFF] = e->type;
  582. }
  583. /* Copy over all the stabs symbols, omitting the ones we don't want,
  584. and correcting the string indices for those we do want. */
  585. tosym = contents;
  586. symend = contents + stabsec->rawsize;
  587. for (sym = contents, pstridx = secinfo->stridxs;
  588. sym < symend;
  589. sym += STABSIZE, ++pstridx)
  590. {
  591. if (*pstridx != (bfd_size_type) -1)
  592. {
  593. if (tosym != sym)
  594. memcpy (tosym, sym, STABSIZE);
  595. bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF);
  596. if (sym[TYPEOFF] == 0)
  597. {
  598. /* This is the header symbol for the stabs section. We
  599. don't really need one, since we have merged all the
  600. input stabs sections into one, but we generate one
  601. for the benefit of readers which expect to see one. */
  602. BFD_ASSERT (sym == contents);
  603. bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings),
  604. tosym + VALOFF);
  605. bfd_put_16 (output_bfd,
  606. stabsec->output_section->size / STABSIZE - 1,
  607. tosym + DESCOFF);
  608. }
  609. tosym += STABSIZE;
  610. }
  611. }
  612. BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->size);
  613. return bfd_set_section_contents (output_bfd, stabsec->output_section,
  614. contents, (file_ptr) stabsec->output_offset,
  615. stabsec->size);
  616. }
  617. /* Write out the .stabstr section. */
  618. bool
  619. _bfd_write_stab_strings (bfd *output_bfd, struct stab_info *sinfo)
  620. {
  621. if (bfd_is_abs_section (sinfo->stabstr->output_section))
  622. /* The section was discarded from the link. */
  623. return true;
  624. BFD_ASSERT ((sinfo->stabstr->output_offset
  625. + _bfd_stringtab_size (sinfo->strings))
  626. <= sinfo->stabstr->output_section->size);
  627. if (bfd_seek (output_bfd,
  628. (file_ptr) (sinfo->stabstr->output_section->filepos
  629. + sinfo->stabstr->output_offset),
  630. SEEK_SET) != 0)
  631. return false;
  632. if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
  633. return false;
  634. /* We no longer need the stabs information. */
  635. _bfd_stringtab_free (sinfo->strings);
  636. bfd_hash_table_free (&sinfo->includes);
  637. return true;
  638. }
  639. /* Adjust an address in the .stab section. Given OFFSET within
  640. STABSEC, this returns the new offset in the adjusted stab section,
  641. or -1 if the address refers to a stab which has been removed. */
  642. bfd_vma
  643. _bfd_stab_section_offset (asection *stabsec,
  644. void * psecinfo,
  645. bfd_vma offset)
  646. {
  647. struct stab_section_info *secinfo;
  648. secinfo = (struct stab_section_info *) psecinfo;
  649. if (secinfo == NULL)
  650. return offset;
  651. if (offset >= stabsec->rawsize)
  652. return offset - stabsec->rawsize + stabsec->size;
  653. if (secinfo->cumulative_skips)
  654. {
  655. bfd_vma i;
  656. i = offset / STABSIZE;
  657. if (secinfo->stridxs [i] == (bfd_size_type) -1)
  658. return (bfd_vma) -1;
  659. return offset - secinfo->cumulative_skips [i];
  660. }
  661. return offset;
  662. }