Correctly emit lo16 relocs for elf-dlx target, fixing bogus range checking bug.
[deliverable/binutils-gdb.git] / bfd / stabs.c
CommitLineData
252b5132 1/* Stabs in sections linking support.
3d456464 2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
dc810e39 3 Free Software Foundation, Inc.
252b5132
RH
4 Written by Ian Lance Taylor, Cygnus Support.
5
955a76eb 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
955a76eb
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
252b5132 12
955a76eb
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
955a76eb
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132
RH
21
22/* This file contains support for linking stabs in sections, as used
23 on COFF and ELF. */
24
25#include "bfd.h"
26#include "sysdep.h"
27#include "libbfd.h"
28#include "aout/stab_gnu.h"
3882b010 29#include "safe-ctype.h"
252b5132
RH
30
31/* Stabs entries use a 12 byte format:
32 4 byte string table index
33 1 byte stab type
34 1 byte stab other field
35 2 byte stab desc field
36 4 byte stab value
37 FIXME: This will have to change for a 64 bit object format.
38
39 The stabs symbols are divided into compilation units. For the
40 first entry in each unit, the type of 0, the value is the length of
41 the string table for this unit, and the desc field is the number of
42 stabs symbols for this unit. */
43
44#define STRDXOFF (0)
45#define TYPEOFF (4)
46#define OTHEROFF (5)
47#define DESCOFF (6)
48#define VALOFF (8)
49#define STABSIZE (12)
50
51/* A hash table used for header files with N_BINCL entries. */
52
53struct stab_link_includes_table
54{
55 struct bfd_hash_table root;
56};
57
58/* A linked list of totals that we have found for a particular header
a56b48eb
NC
59 file. A total is the sum of all the STABS characters for a particular
60 file and the number of these charactes. It is used to identify
61 duplicate files which can be excluded. XXX: A better method would be to
62 compute an MD5 checksum, but that is coding left for another day.
63 The bfd_vma type is used because it is a very large unsigned type. */
252b5132
RH
64
65struct stab_link_includes_totals
66{
67 struct stab_link_includes_totals *next;
a56b48eb
NC
68 bfd_vma sum_chars; /* Accumulated sum of STABS characters. */
69 bfd_vma num_chars; /* Number of STABS characters. */
252b5132
RH
70};
71
72/* An entry in the header file hash table. */
73
74struct stab_link_includes_entry
75{
76 struct bfd_hash_entry root;
77 /* List of totals we have found for this file. */
78 struct stab_link_includes_totals *totals;
79};
80
81/* Look up an entry in an the header file hash table. */
82
83#define stab_link_includes_lookup(table, string, create, copy) \
84 ((struct stab_link_includes_entry *) \
85 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
86
87/* This structure is used to hold a list of N_BINCL symbols, some of
88 which might be converted into N_EXCL symbols. */
89
90struct stab_excl_list
91{
92 /* The next symbol to convert. */
93 struct stab_excl_list *next;
94 /* The offset to this symbol in the section contents. */
95 bfd_size_type offset;
96 /* The value to use for the symbol. */
97 bfd_vma val;
98 /* The type of this symbol (N_BINCL or N_EXCL). */
99 int type;
100};
101
102/* This structure is stored with each .stab section. */
103
104struct stab_section_info
105{
106 /* This is a linked list of N_BINCL symbols which should be
107 converted into N_EXCL symbols. */
108 struct stab_excl_list *excls;
109
110 /* This is used to map input stab offsets within their sections
111 to output stab offsets, to take into account stabs that have
112 been deleted. If it is NULL, the output offsets are the same
113 as the input offsets, because no stabs have been deleted from
114 this section. Otherwise the i'th entry is the number of
115 bytes of stabs that have been deleted prior to the i'th
7b82c249 116 stab. */
252b5132
RH
117 bfd_size_type *cumulative_skips;
118
119 /* This is an array of string indices. For each stab symbol, we
120 store the string index here. If a stab symbol should not be
121 included in the final output, the string index is -1. */
122 bfd_size_type stridxs[1];
123};
124
125/* This structure is used to keep track of stabs in sections
126 information while linking. */
127
128struct stab_info
129{
130 /* A hash table used to hold stabs strings. */
131 struct bfd_strtab_hash *strings;
132 /* The header file hash table. */
133 struct stab_link_includes_table includes;
134 /* The first .stabstr section. */
135 asection *stabstr;
136};
137
138static struct bfd_hash_entry *stab_link_includes_newfunc
139 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
140\f
141/* The function to create a new entry in the header file hash table. */
142
143static struct bfd_hash_entry *
144stab_link_includes_newfunc (entry, table, string)
145 struct bfd_hash_entry *entry;
146 struct bfd_hash_table *table;
147 const char *string;
148{
149 struct stab_link_includes_entry *ret =
150 (struct stab_link_includes_entry *) entry;
151
152 /* Allocate the structure if it has not already been allocated by a
153 subclass. */
154 if (ret == (struct stab_link_includes_entry *) NULL)
155 ret = ((struct stab_link_includes_entry *)
156 bfd_hash_allocate (table,
157 sizeof (struct stab_link_includes_entry)));
158 if (ret == (struct stab_link_includes_entry *) NULL)
159 return (struct bfd_hash_entry *) ret;
160
161 /* Call the allocation method of the superclass. */
162 ret = ((struct stab_link_includes_entry *)
163 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
164 if (ret)
165 {
166 /* Set local fields. */
167 ret->totals = NULL;
168 }
169
170 return (struct bfd_hash_entry *) ret;
171}
172\f
173/* This function is called for each input file from the add_symbols
174 pass of the linker. */
175
b34976b6 176bfd_boolean
29ca8dc5 177_bfd_link_section_stabs (abfd, psinfo, stabsec, stabstrsec, psecinfo, pstring_offset)
252b5132
RH
178 bfd *abfd;
179 PTR *psinfo;
180 asection *stabsec;
181 asection *stabstrsec;
182 PTR *psecinfo;
29ca8dc5 183 bfd_size_type *pstring_offset;
252b5132 184{
b34976b6 185 bfd_boolean first;
252b5132 186 struct stab_info *sinfo;
dc810e39 187 bfd_size_type count, amt;
252b5132
RH
188 struct stab_section_info *secinfo;
189 bfd_byte *stabbuf = NULL;
190 bfd_byte *stabstrbuf = NULL;
191 bfd_byte *sym, *symend;
192 bfd_size_type stroff, next_stroff, skip;
193 bfd_size_type *pstridx;
194
195 if (stabsec->_raw_size == 0
196 || stabstrsec->_raw_size == 0)
197 {
198 /* This file does not contain stabs debugging information. */
b34976b6 199 return TRUE;
252b5132
RH
200 }
201
202 if (stabsec->_raw_size % STABSIZE != 0)
203 {
204 /* Something is wrong with the format of these stab symbols.
b34976b6
AM
205 Don't try to optimize them. */
206 return TRUE;
252b5132
RH
207 }
208
209 if ((stabstrsec->flags & SEC_RELOC) != 0)
210 {
211 /* We shouldn't see relocations in the strings, and we aren't
b34976b6
AM
212 prepared to handle them. */
213 return TRUE;
252b5132
RH
214 }
215
216 if ((stabsec->output_section != NULL
217 && bfd_is_abs_section (stabsec->output_section))
218 || (stabstrsec->output_section != NULL
219 && bfd_is_abs_section (stabstrsec->output_section)))
220 {
221 /* At least one of the sections is being discarded from the
b34976b6
AM
222 link, so we should just ignore them. */
223 return TRUE;
252b5132
RH
224 }
225
b34976b6 226 first = FALSE;
252b5132
RH
227
228 if (*psinfo == NULL)
229 {
230 /* Initialize the stabs information we need to keep track of. */
b34976b6 231 first = TRUE;
dc810e39
AM
232 amt = sizeof (struct stab_info);
233 *psinfo = (PTR) bfd_alloc (abfd, amt);
252b5132
RH
234 if (*psinfo == NULL)
235 goto error_return;
236 sinfo = (struct stab_info *) *psinfo;
237 sinfo->strings = _bfd_stringtab_init ();
238 if (sinfo->strings == NULL)
239 goto error_return;
8140b664 240 /* Make sure the first byte is zero. */
b34976b6 241 (void) _bfd_stringtab_add (sinfo->strings, "", TRUE, TRUE);
252b5132
RH
242 if (! bfd_hash_table_init_n (&sinfo->includes.root,
243 stab_link_includes_newfunc,
244 251))
245 goto error_return;
246 sinfo->stabstr = bfd_make_section_anyway (abfd, ".stabstr");
247 sinfo->stabstr->flags |= SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
248 }
249
250 sinfo = (struct stab_info *) *psinfo;
251
252 /* Initialize the information we are going to store for this .stab
253 section. */
254
255 count = stabsec->_raw_size / STABSIZE;
256
dc810e39
AM
257 amt = sizeof (struct stab_section_info);
258 amt += (count - 1) * sizeof (bfd_size_type);
259 *psecinfo = bfd_alloc (abfd, amt);
252b5132
RH
260 if (*psecinfo == NULL)
261 goto error_return;
262
263 secinfo = (struct stab_section_info *) *psecinfo;
264 secinfo->excls = NULL;
265 secinfo->cumulative_skips = NULL;
dc810e39 266 memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type));
252b5132
RH
267
268 /* Read the stabs information from abfd. */
269
270 stabbuf = (bfd_byte *) bfd_malloc (stabsec->_raw_size);
271 stabstrbuf = (bfd_byte *) bfd_malloc (stabstrsec->_raw_size);
272 if (stabbuf == NULL || stabstrbuf == NULL)
273 goto error_return;
274
dc810e39 275 if (! bfd_get_section_contents (abfd, stabsec, stabbuf, (bfd_vma) 0,
252b5132 276 stabsec->_raw_size)
dc810e39 277 || ! bfd_get_section_contents (abfd, stabstrsec, stabstrbuf, (bfd_vma) 0,
252b5132
RH
278 stabstrsec->_raw_size))
279 goto error_return;
280
281 /* Look through the stabs symbols, work out the new string indices,
282 and identify N_BINCL symbols which can be eliminated. */
283
284 stroff = 0;
29ca8dc5
NS
285 /* The stabs sections can be split when
286 -split-by-reloc/-split-by-file is used. We must keep track of
287 each stab section's place in the single concatenated string
288 table. */
289 next_stroff = pstring_offset ? *pstring_offset : 0;
252b5132
RH
290 skip = 0;
291
292 symend = stabbuf + stabsec->_raw_size;
293 for (sym = stabbuf, pstridx = secinfo->stridxs;
294 sym < symend;
295 sym += STABSIZE, ++pstridx)
296 {
644c4c80 297 bfd_size_type symstroff;
252b5132
RH
298 int type;
299 const char *string;
300
301 if (*pstridx != 0)
302 {
303 /* This symbol has already been handled by an N_BINCL pass. */
304 continue;
305 }
306
307 type = sym[TYPEOFF];
308
309 if (type == 0)
310 {
311 /* Special type 0 stabs indicate the offset to the next
b34976b6 312 string table. We only copy the very first one. */
252b5132
RH
313 stroff = next_stroff;
314 next_stroff += bfd_get_32 (abfd, sym + 8);
29ca8dc5
NS
315 if (pstring_offset)
316 *pstring_offset = next_stroff;
252b5132
RH
317 if (! first)
318 {
319 *pstridx = (bfd_size_type) -1;
320 ++skip;
321 continue;
322 }
b34976b6 323 first = FALSE;
252b5132
RH
324 }
325
326 /* Store the string in the hash table, and record the index. */
644c4c80
RS
327 symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF);
328 if (symstroff >= stabstrsec->_raw_size)
329 {
330 (*_bfd_error_handler)
331 (_("%s(%s+0x%lx): Stabs entry has invalid string index."),
332 bfd_archive_filename (abfd),
333 bfd_get_section_name (abfd, stabsec),
334 (long) (sym - stabbuf));
335 bfd_set_error (bfd_error_bad_value);
336 goto error_return;
337 }
338 string = (char *) stabstrbuf + symstroff;
b34976b6 339 *pstridx = _bfd_stringtab_add (sinfo->strings, string, TRUE, TRUE);
252b5132
RH
340
341 /* An N_BINCL symbol indicates the start of the stabs entries
342 for a header file. We need to scan ahead to the next N_EINCL
343 symbol, ignoring nesting, adding up all the characters in the
344 symbol names, not including the file numbers in types (the
345 first number after an open parenthesis). */
d45913a0 346 if (type == (int) N_BINCL)
252b5132 347 {
a56b48eb
NC
348 bfd_vma sum_chars;
349 bfd_vma num_chars;
252b5132
RH
350 int nest;
351 bfd_byte *incl_sym;
352 struct stab_link_includes_entry *incl_entry;
353 struct stab_link_includes_totals *t;
354 struct stab_excl_list *ne;
355
a56b48eb 356 sum_chars = num_chars = 0;
252b5132
RH
357 nest = 0;
358 for (incl_sym = sym + STABSIZE;
359 incl_sym < symend;
360 incl_sym += STABSIZE)
361 {
362 int incl_type;
363
364 incl_type = incl_sym[TYPEOFF];
365 if (incl_type == 0)
366 break;
955a76eb
NC
367 else if (incl_type == (int) N_EXCL)
368 continue;
d45913a0 369 else if (incl_type == (int) N_EINCL)
252b5132
RH
370 {
371 if (nest == 0)
372 break;
373 --nest;
374 }
d45913a0 375 else if (incl_type == (int) N_BINCL)
252b5132
RH
376 ++nest;
377 else if (nest == 0)
378 {
379 const char *str;
380
381 str = ((char *) stabstrbuf
382 + stroff
383 + bfd_get_32 (abfd, incl_sym + STRDXOFF));
384 for (; *str != '\0'; str++)
385 {
a56b48eb
NC
386 sum_chars += *str;
387 num_chars ++;
252b5132
RH
388 if (*str == '(')
389 {
390 /* Skip the file number. */
391 ++str;
3882b010 392 while (ISDIGIT (*str))
252b5132
RH
393 ++str;
394 --str;
395 }
396 }
397 }
398 }
399
400 /* If we have already included a header file with the same
401 value, then replaced this one with an N_EXCL symbol. */
402 incl_entry = stab_link_includes_lookup (&sinfo->includes, string,
b34976b6 403 TRUE, TRUE);
252b5132
RH
404 if (incl_entry == NULL)
405 goto error_return;
406
407 for (t = incl_entry->totals; t != NULL; t = t->next)
a56b48eb 408 if (t->sum_chars == sum_chars && t->num_chars == num_chars)
252b5132
RH
409 break;
410
411 /* Record this symbol, so that we can set the value
b34976b6 412 correctly. */
dc810e39
AM
413 amt = sizeof *ne;
414 ne = (struct stab_excl_list *) bfd_alloc (abfd, amt);
252b5132
RH
415 if (ne == NULL)
416 goto error_return;
417 ne->offset = sym - stabbuf;
a56b48eb 418 ne->val = sum_chars;
d45913a0 419 ne->type = (int) N_BINCL;
252b5132
RH
420 ne->next = secinfo->excls;
421 secinfo->excls = ne;
422
423 if (t == NULL)
424 {
425 /* This is the first time we have seen this header file
426 with this set of stabs strings. */
427 t = ((struct stab_link_includes_totals *)
428 bfd_hash_allocate (&sinfo->includes.root, sizeof *t));
429 if (t == NULL)
430 goto error_return;
a56b48eb
NC
431 t->sum_chars = sum_chars;
432 t->num_chars = num_chars;
252b5132
RH
433 t->next = incl_entry->totals;
434 incl_entry->totals = t;
435 }
436 else
437 {
438 bfd_size_type *incl_pstridx;
439
440 /* We have seen this header file before. Tell the final
441 pass to change the type to N_EXCL. */
d45913a0 442 ne->type = (int) N_EXCL;
252b5132
RH
443
444 /* Mark the skipped symbols. */
445
446 nest = 0;
447 for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1;
448 incl_sym < symend;
449 incl_sym += STABSIZE, ++incl_pstridx)
450 {
451 int incl_type;
452
453 incl_type = incl_sym[TYPEOFF];
454
d45913a0 455 if (incl_type == (int) N_EINCL)
252b5132
RH
456 {
457 if (nest == 0)
458 {
459 *incl_pstridx = (bfd_size_type) -1;
460 ++skip;
461 break;
462 }
463 --nest;
464 }
d45913a0 465 else if (incl_type == (int) N_BINCL)
252b5132 466 ++nest;
3d456464
NC
467 else if (incl_type == (int) N_EXCL)
468 /* Keep existing exclusion marks. */
469 continue;
252b5132
RH
470 else if (nest == 0)
471 {
472 *incl_pstridx = (bfd_size_type) -1;
473 ++skip;
474 }
475 }
476 }
477 }
478 }
479
480 free (stabbuf);
481 stabbuf = NULL;
482 free (stabstrbuf);
483 stabstrbuf = NULL;
484
485 /* We need to set the section sizes such that the linker will
486 compute the output section sizes correctly. We set the .stab
487 size to not include the entries we don't want. We set
488 SEC_EXCLUDE for the .stabstr section, so that it will be dropped
489 from the link. We record the size of the strtab in the first
490 .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
491 for that section. */
492 stabsec->_cooked_size = (count - skip) * STABSIZE;
493 if (stabsec->_cooked_size == 0)
494 stabsec->flags |= SEC_EXCLUDE;
495 stabstrsec->flags |= SEC_EXCLUDE;
496 sinfo->stabstr->_cooked_size = _bfd_stringtab_size (sinfo->strings);
497
498 /* Calculate the `cumulative_skips' array now that stabs have been
7b82c249 499 deleted for this section. */
252b5132
RH
500
501 if (skip != 0)
502 {
503 bfd_size_type i, offset;
504 bfd_size_type *pskips;
505
dc810e39
AM
506 amt = count * sizeof (bfd_size_type);
507 secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
252b5132
RH
508 if (secinfo->cumulative_skips == NULL)
509 goto error_return;
510
511 pskips = secinfo->cumulative_skips;
512 pstridx = secinfo->stridxs;
513 offset = 0;
514
515 for (i = 0; i < count; i++, pskips++, pstridx++)
516 {
517 *pskips = offset;
518 if (*pstridx == (bfd_size_type) -1)
519 offset += STABSIZE;
520 }
521
522 BFD_ASSERT (offset != 0);
523 }
524
b34976b6 525 return TRUE;
252b5132
RH
526
527 error_return:
528 if (stabbuf != NULL)
529 free (stabbuf);
530 if (stabstrbuf != NULL)
531 free (stabstrbuf);
b34976b6 532 return FALSE;
252b5132
RH
533}
534
73d074b4
DJ
535\f
536/* This function is called for each input file before the stab
537 section is relocated. It discards stab entries for discarded
b34976b6 538 functions and variables. The function returns TRUE iff
73d074b4
DJ
539 any entries have been deleted.
540*/
541
b34976b6 542bfd_boolean
73d074b4
DJ
543_bfd_discard_section_stabs (abfd, stabsec, psecinfo,
544 reloc_symbol_deleted_p, cookie)
545 bfd *abfd;
546 asection *stabsec;
547 PTR psecinfo;
b34976b6 548 bfd_boolean (*reloc_symbol_deleted_p) PARAMS ((bfd_vma, PTR));
73d074b4
DJ
549 PTR cookie;
550{
551 bfd_size_type count, amt;
552 struct stab_section_info *secinfo;
553 bfd_byte *stabbuf = NULL;
554 bfd_byte *sym, *symend;
555 bfd_size_type skip;
556 bfd_size_type *pstridx;
557 int deleting;
558
559 if (stabsec->_raw_size == 0)
560 {
561 /* This file does not contain stabs debugging information. */
b34976b6 562 return FALSE;
73d074b4
DJ
563 }
564
565 if (stabsec->_raw_size % STABSIZE != 0)
566 {
567 /* Something is wrong with the format of these stab symbols.
b34976b6
AM
568 Don't try to optimize them. */
569 return FALSE;
73d074b4
DJ
570 }
571
572 if ((stabsec->output_section != NULL
573 && bfd_is_abs_section (stabsec->output_section)))
574 {
575 /* At least one of the sections is being discarded from the
b34976b6
AM
576 link, so we should just ignore them. */
577 return FALSE;
73d074b4
DJ
578 }
579
580 /* We should have initialized our data in _bfd_link_stab_sections.
581 If there was some bizarre error reading the string sections, though,
582 we might not have. Bail rather than asserting. */
583 if (psecinfo == NULL)
b34976b6 584 return FALSE;
73d074b4
DJ
585
586 count = stabsec->_raw_size / STABSIZE;
587 secinfo = (struct stab_section_info *) psecinfo;
588
589 /* Read the stabs information from abfd. */
590
591 stabbuf = (bfd_byte *) bfd_malloc (stabsec->_raw_size);
592 if (stabbuf == NULL)
593 goto error_return;
594
595 if (! bfd_get_section_contents (abfd, stabsec, stabbuf, (bfd_vma) 0,
596 stabsec->_raw_size))
597 goto error_return;
598
599 /* Look through the stabs symbols and discard any information for
600 discarded functions. */
601
602 skip = 0;
603 deleting = -1;
604
605 symend = stabbuf + stabsec->_raw_size;
606 for (sym = stabbuf, pstridx = secinfo->stridxs;
607 sym < symend;
608 sym += STABSIZE, ++pstridx)
609 {
610 int type;
611
612 if (*pstridx == (bfd_size_type) -1)
613 {
614 /* This stab was deleted in a previous pass. */
615 continue;
616 }
617
618 type = sym[TYPEOFF];
619
d45913a0 620 if (type == (int) N_FUN)
73d074b4
DJ
621 {
622 int strx = bfd_get_32 (abfd, sym + STRDXOFF);
623
624 if (strx == 0)
625 {
626 if (deleting)
627 {
628 skip++;
629 *pstridx = -1;
630 }
631 deleting = -1;
632 continue;
633 }
634 deleting = 0;
635 if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
636 deleting = 1;
637 }
638
639 if (deleting == 1)
640 {
641 *pstridx = -1;
642 skip++;
643 }
644 else if (deleting == -1)
645 {
646 /* Outside of a function. Check for deleted variables. */
d45913a0 647 if (type == (int) N_STSYM || type == (int) N_LCSYM)
73d074b4
DJ
648 if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
649 {
650 *pstridx = -1;
651 skip ++;
652 }
653 /* We should also check for N_GSYM entries which reference a
654 deleted global, but those are less harmful to debuggers
655 and would require parsing the stab strings. */
656 }
657 }
658
659 free (stabbuf);
660 stabbuf = NULL;
661
662 /* Shrink the stabsec as needed. */
663 stabsec->_cooked_size -= skip * STABSIZE;
664 if (stabsec->_cooked_size == 0)
665 stabsec->flags |= SEC_EXCLUDE;
666
667 /* Recalculate the `cumulative_skips' array now that stabs have been
668 deleted for this section. */
669
670 if (skip != 0)
671 {
672 bfd_size_type i, offset;
673 bfd_size_type *pskips;
674
675 if (secinfo->cumulative_skips == NULL)
676 {
677 amt = count * sizeof (bfd_size_type);
678 secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
679 if (secinfo->cumulative_skips == NULL)
680 goto error_return;
681 }
682
683 pskips = secinfo->cumulative_skips;
684 pstridx = secinfo->stridxs;
685 offset = 0;
686
687 for (i = 0; i < count; i++, pskips++, pstridx++)
688 {
689 *pskips = offset;
690 if (*pstridx == (bfd_size_type) -1)
691 offset += STABSIZE;
692 }
693
694 BFD_ASSERT (offset != 0);
695 }
696
b34976b6 697 return skip > 0;
73d074b4
DJ
698
699 error_return:
700 if (stabbuf != NULL)
701 free (stabbuf);
b34976b6 702 return FALSE;
73d074b4
DJ
703}
704
252b5132
RH
705/* Write out the stab section. This is called with the relocated
706 contents. */
707
b34976b6 708bfd_boolean
252b5132
RH
709_bfd_write_section_stabs (output_bfd, psinfo, stabsec, psecinfo, contents)
710 bfd *output_bfd;
711 PTR *psinfo;
712 asection *stabsec;
713 PTR *psecinfo;
714 bfd_byte *contents;
715{
716 struct stab_info *sinfo;
717 struct stab_section_info *secinfo;
718 struct stab_excl_list *e;
719 bfd_byte *sym, *tosym, *symend;
720 bfd_size_type *pstridx;
721
722 sinfo = (struct stab_info *) *psinfo;
723 secinfo = (struct stab_section_info *) *psecinfo;
724
725 if (secinfo == NULL)
726 return bfd_set_section_contents (output_bfd, stabsec->output_section,
dc810e39
AM
727 contents,
728 (file_ptr) stabsec->output_offset,
252b5132
RH
729 stabsec->_raw_size);
730
731 /* Handle each N_BINCL entry. */
732 for (e = secinfo->excls; e != NULL; e = e->next)
733 {
734 bfd_byte *excl_sym;
735
736 BFD_ASSERT (e->offset < stabsec->_raw_size);
737 excl_sym = contents + e->offset;
738 bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF);
739 excl_sym[TYPEOFF] = e->type;
740 }
741
742 /* Copy over all the stabs symbols, omitting the ones we don't want,
743 and correcting the string indices for those we do want. */
744 tosym = contents;
745 symend = contents + stabsec->_raw_size;
746 for (sym = contents, pstridx = secinfo->stridxs;
747 sym < symend;
748 sym += STABSIZE, ++pstridx)
749 {
750 if (*pstridx != (bfd_size_type) -1)
751 {
752 if (tosym != sym)
753 memcpy (tosym, sym, STABSIZE);
754 bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF);
755
756 if (sym[TYPEOFF] == 0)
757 {
758 /* This is the header symbol for the stabs section. We
b34976b6
AM
759 don't really need one, since we have merged all the
760 input stabs sections into one, but we generate one
761 for the benefit of readers which expect to see one. */
252b5132
RH
762 BFD_ASSERT (sym == contents);
763 bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings),
764 tosym + VALOFF);
765 bfd_put_16 (output_bfd,
766 stabsec->output_section->_raw_size / STABSIZE - 1,
767 tosym + DESCOFF);
768 }
769
770 tosym += STABSIZE;
771 }
772 }
773
774 BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->_cooked_size);
775
776 return bfd_set_section_contents (output_bfd, stabsec->output_section,
dc810e39 777 contents, (file_ptr) stabsec->output_offset,
252b5132
RH
778 stabsec->_cooked_size);
779}
780
781/* Write out the .stabstr section. */
782
b34976b6 783bfd_boolean
252b5132
RH
784_bfd_write_stab_strings (output_bfd, psinfo)
785 bfd *output_bfd;
786 PTR *psinfo;
787{
788 struct stab_info *sinfo;
789
790 sinfo = (struct stab_info *) *psinfo;
791
792 if (sinfo == NULL)
b34976b6 793 return TRUE;
252b5132
RH
794
795 if (bfd_is_abs_section (sinfo->stabstr->output_section))
796 {
797 /* The section was discarded from the link. */
b34976b6 798 return TRUE;
252b5132
RH
799 }
800
801 BFD_ASSERT ((sinfo->stabstr->output_offset
802 + _bfd_stringtab_size (sinfo->strings))
803 <= sinfo->stabstr->output_section->_raw_size);
804
805 if (bfd_seek (output_bfd,
dc810e39
AM
806 (file_ptr) (sinfo->stabstr->output_section->filepos
807 + sinfo->stabstr->output_offset),
252b5132 808 SEEK_SET) != 0)
b34976b6 809 return FALSE;
252b5132
RH
810
811 if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
b34976b6 812 return FALSE;
252b5132
RH
813
814 /* We no longer need the stabs information. */
815 _bfd_stringtab_free (sinfo->strings);
816 bfd_hash_table_free (&sinfo->includes.root);
817
b34976b6 818 return TRUE;
252b5132
RH
819}
820
821/* Adjust an address in the .stab section. Given OFFSET within
822 STABSEC, this returns the new offset in the adjusted stab section,
823 or -1 if the address refers to a stab which has been removed. */
824
825bfd_vma
826_bfd_stab_section_offset (output_bfd, psinfo, stabsec, psecinfo, offset)
7442e600
ILT
827 bfd *output_bfd ATTRIBUTE_UNUSED;
828 PTR *psinfo ATTRIBUTE_UNUSED;
252b5132
RH
829 asection *stabsec;
830 PTR *psecinfo;
831 bfd_vma offset;
832{
833 struct stab_section_info *secinfo;
834
835 secinfo = (struct stab_section_info *) *psecinfo;
836
837 if (secinfo == NULL)
838 return offset;
839
840 if (offset >= stabsec->_raw_size)
841 return offset - (stabsec->_cooked_size - stabsec->_raw_size);
842
843 if (secinfo->cumulative_skips)
844 {
845 bfd_vma i;
846
847 i = offset / STABSIZE;
848
849 if (secinfo->stridxs [i] == (bfd_size_type) -1)
850 return (bfd_vma) -1;
851
852 return offset - secinfo->cumulative_skips [i];
853 }
854
855 return offset;
856}
This page took 0.299651 seconds and 4 git commands to generate.