Automatic date update in version.in
[binutils-gdb.git] / bfd / coffgen.c
blob07b3fb446aa28d9f4e1719df05d3e2b9d22e8959
1 /* Support for the generic parts of COFF, for BFD.
2 Copyright (C) 1990-2022 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
23 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
25 /* This file contains COFF code that is not dependent on any
26 particular COFF target. There is only one version of this file in
27 libbfd.a, so no target specific code may be put in here. Or, to
28 put it another way,
30 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
32 If you need to add some target specific behaviour, add a new hook
33 function to bfd_coff_backend_data.
35 Some of these functions are also called by the ECOFF routines.
36 Those functions may not use any COFF specific information, such as
37 coff_data (abfd). */
39 #include "sysdep.h"
40 #include <limits.h>
41 #include "bfd.h"
42 #include "libbfd.h"
43 #include "coff/internal.h"
44 #include "libcoff.h"
46 /* Take a section header read from a coff file (in HOST byte order),
47 and make a BFD "section" out of it. This is used by ECOFF. */
49 static bool
50 make_a_section_from_file (bfd *abfd,
51 struct internal_scnhdr *hdr,
52 unsigned int target_index)
54 asection *return_section;
55 char *name;
56 bool result = true;
57 flagword flags;
59 name = NULL;
61 /* Handle long section names as in PE. On reading, we want to
62 accept long names if the format permits them at all, regardless
63 of the current state of the flag that dictates if we would generate
64 them in outputs; this construct checks if that is the case by
65 attempting to set the flag, without changing its state; the call
66 will fail for formats that do not support long names at all. */
67 if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
68 && hdr->s_name[0] == '/')
70 char buf[SCNNMLEN];
71 long strindex;
72 char *p;
73 const char *strings;
75 /* Flag that this BFD uses long names, even though the format might
76 expect them to be off by default. This won't directly affect the
77 format of any output BFD created from this one, but the information
78 can be used to decide what to do. */
79 bfd_coff_set_long_section_names (abfd, true);
80 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
81 buf[SCNNMLEN - 1] = '\0';
82 strindex = strtol (buf, &p, 10);
83 if (*p == '\0' && strindex >= 0)
85 strings = _bfd_coff_read_string_table (abfd);
86 if (strings == NULL)
87 return false;
88 if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
89 return false;
90 strings += strindex;
91 name = (char *) bfd_alloc (abfd,
92 (bfd_size_type) strlen (strings) + 1 + 1);
93 if (name == NULL)
94 return false;
95 strcpy (name, strings);
99 if (name == NULL)
101 /* Assorted wastage to null-terminate the name, thanks AT&T! */
102 name = (char *) bfd_alloc (abfd,
103 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
104 if (name == NULL)
105 return false;
106 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
107 name[sizeof (hdr->s_name)] = 0;
110 return_section = bfd_make_section_anyway (abfd, name);
111 if (return_section == NULL)
112 return false;
114 return_section->vma = hdr->s_vaddr;
115 return_section->lma = hdr->s_paddr;
116 return_section->size = hdr->s_size;
117 return_section->filepos = hdr->s_scnptr;
118 return_section->rel_filepos = hdr->s_relptr;
119 return_section->reloc_count = hdr->s_nreloc;
121 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
123 return_section->line_filepos = hdr->s_lnnoptr;
125 return_section->lineno_count = hdr->s_nlnno;
126 return_section->userdata = NULL;
127 return_section->next = NULL;
128 return_section->target_index = target_index;
130 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
131 & flags))
132 result = false;
134 return_section->flags = flags;
136 /* At least on i386-coff, the line number count for a shared library
137 section must be ignored. */
138 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
139 return_section->lineno_count = 0;
141 if (hdr->s_nreloc != 0)
142 return_section->flags |= SEC_RELOC;
143 /* FIXME: should this check 'hdr->s_size > 0'. */
144 if (hdr->s_scnptr != 0)
145 return_section->flags |= SEC_HAS_CONTENTS;
147 /* Compress/decompress DWARF debug sections with names: .debug_* and
148 .zdebug_*, after the section flags is set. */
149 if ((flags & SEC_DEBUGGING)
150 && strlen (name) > 7
151 && ((name[1] == 'd' && name[6] == '_')
152 || (strlen (name) > 8 && name[1] == 'z' && name[7] == '_')))
154 enum { nothing, compress, decompress } action = nothing;
155 char *new_name = NULL;
157 if (bfd_is_section_compressed (abfd, return_section))
159 /* Compressed section. Check if we should decompress. */
160 if ((abfd->flags & BFD_DECOMPRESS))
161 action = decompress;
163 else if (!bfd_is_section_compressed (abfd, return_section))
165 /* Normal section. Check if we should compress. */
166 if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0)
167 action = compress;
170 switch (action)
172 case nothing:
173 break;
174 case compress:
175 if (!bfd_init_section_compress_status (abfd, return_section))
177 _bfd_error_handler
178 /* xgettext: c-format */
179 (_("%pB: unable to initialize compress status for section %s"),
180 abfd, name);
181 return false;
183 if (return_section->compress_status == COMPRESS_SECTION_DONE)
185 if (name[1] != 'z')
187 unsigned int len = strlen (name);
189 new_name = bfd_alloc (abfd, len + 2);
190 if (new_name == NULL)
191 return false;
192 new_name[0] = '.';
193 new_name[1] = 'z';
194 memcpy (new_name + 2, name + 1, len);
197 break;
198 case decompress:
199 if (!bfd_init_section_decompress_status (abfd, return_section))
201 _bfd_error_handler
202 /* xgettext: c-format */
203 (_("%pB: unable to initialize decompress status for section %s"),
204 abfd, name);
205 return false;
207 if (name[1] == 'z')
209 unsigned int len = strlen (name);
211 new_name = bfd_alloc (abfd, len);
212 if (new_name == NULL)
213 return false;
214 new_name[0] = '.';
215 memcpy (new_name + 1, name + 2, len - 1);
217 break;
219 if (new_name != NULL)
220 bfd_rename_section (return_section, new_name);
223 return result;
226 /* Read in a COFF object and make it into a BFD. This is used by
227 ECOFF as well. */
228 bfd_cleanup
229 coff_real_object_p (bfd *,
230 unsigned,
231 struct internal_filehdr *,
232 struct internal_aouthdr *);
233 bfd_cleanup
234 coff_real_object_p (bfd *abfd,
235 unsigned nscns,
236 struct internal_filehdr *internal_f,
237 struct internal_aouthdr *internal_a)
239 flagword oflags = abfd->flags;
240 bfd_vma ostart = bfd_get_start_address (abfd);
241 void * tdata;
242 void * tdata_save;
243 bfd_size_type readsize; /* Length of file_info. */
244 unsigned int scnhsz;
245 char *external_sections;
247 if (!(internal_f->f_flags & F_RELFLG))
248 abfd->flags |= HAS_RELOC;
249 if ((internal_f->f_flags & F_EXEC))
250 abfd->flags |= EXEC_P;
251 if (!(internal_f->f_flags & F_LNNO))
252 abfd->flags |= HAS_LINENO;
253 if (!(internal_f->f_flags & F_LSYMS))
254 abfd->flags |= HAS_LOCALS;
256 /* FIXME: How can we set D_PAGED correctly? */
257 if ((internal_f->f_flags & F_EXEC) != 0)
258 abfd->flags |= D_PAGED;
260 abfd->symcount = internal_f->f_nsyms;
261 if (internal_f->f_nsyms)
262 abfd->flags |= HAS_SYMS;
264 if (internal_a != (struct internal_aouthdr *) NULL)
265 abfd->start_address = internal_a->entry;
266 else
267 abfd->start_address = 0;
269 /* Set up the tdata area. ECOFF uses its own routine, and overrides
270 abfd->flags. */
271 tdata_save = abfd->tdata.any;
272 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
273 if (tdata == NULL)
274 goto fail2;
276 scnhsz = bfd_coff_scnhsz (abfd);
277 readsize = (bfd_size_type) nscns * scnhsz;
278 external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
279 if (!external_sections)
280 goto fail;
282 /* Set the arch/mach *before* swapping in sections; section header swapping
283 may depend on arch/mach info. */
284 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
285 goto fail;
287 /* Now copy data as required; construct all asections etc. */
288 if (nscns != 0)
290 unsigned int i;
291 for (i = 0; i < nscns; i++)
293 struct internal_scnhdr tmp;
294 bfd_coff_swap_scnhdr_in (abfd,
295 (void *) (external_sections + i * scnhsz),
296 (void *) & tmp);
297 if (! make_a_section_from_file (abfd, &tmp, i + 1))
298 goto fail;
302 _bfd_coff_free_symbols (abfd);
303 return _bfd_no_cleanup;
305 fail:
306 _bfd_coff_free_symbols (abfd);
307 bfd_release (abfd, tdata);
308 fail2:
309 abfd->tdata.any = tdata_save;
310 abfd->flags = oflags;
311 abfd->start_address = ostart;
312 return NULL;
315 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
316 not a COFF file. This is also used by ECOFF. */
318 bfd_cleanup
319 coff_object_p (bfd *abfd)
321 bfd_size_type filhsz;
322 bfd_size_type aoutsz;
323 unsigned int nscns;
324 void * filehdr;
325 struct internal_filehdr internal_f;
326 struct internal_aouthdr internal_a;
328 /* Figure out how much to read. */
329 filhsz = bfd_coff_filhsz (abfd);
330 aoutsz = bfd_coff_aoutsz (abfd);
332 filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
333 if (filehdr == NULL)
335 if (bfd_get_error () != bfd_error_system_call)
336 bfd_set_error (bfd_error_wrong_format);
337 return NULL;
339 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
340 bfd_release (abfd, filehdr);
342 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
343 (less than aoutsz) used in object files and AOUTSZ (equal to
344 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
345 expects this header to be aoutsz bytes in length, so we use that
346 value in the call to bfd_alloc below. But we must be careful to
347 only read in f_opthdr bytes in the call to bfd_bread. We should
348 also attempt to catch corrupt or non-COFF binaries with a strange
349 value for f_opthdr. */
350 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
351 || internal_f.f_opthdr > aoutsz)
353 bfd_set_error (bfd_error_wrong_format);
354 return NULL;
356 nscns = internal_f.f_nscns;
358 if (internal_f.f_opthdr)
360 void * opthdr;
362 opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
363 if (opthdr == NULL)
364 return NULL;
365 /* PR 17512: file: 11056-1136-0.004. */
366 if (internal_f.f_opthdr < aoutsz)
367 memset (((char *) opthdr) + internal_f.f_opthdr, 0,
368 aoutsz - internal_f.f_opthdr);
370 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
371 bfd_release (abfd, opthdr);
374 return coff_real_object_p (abfd, nscns, &internal_f,
375 (internal_f.f_opthdr != 0
376 ? &internal_a
377 : (struct internal_aouthdr *) NULL));
380 /* Get the BFD section from a COFF symbol section number. */
382 asection *
383 coff_section_from_bfd_index (bfd *abfd, int section_index)
385 struct bfd_section *answer = abfd->sections;
387 if (section_index == N_ABS)
388 return bfd_abs_section_ptr;
389 if (section_index == N_UNDEF)
390 return bfd_und_section_ptr;
391 if (section_index == N_DEBUG)
392 return bfd_abs_section_ptr;
394 while (answer)
396 if (answer->target_index == section_index)
397 return answer;
398 answer = answer->next;
401 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
402 has a bad symbol table in biglitpow.o. */
403 return bfd_und_section_ptr;
406 /* Get the upper bound of a COFF symbol table. */
408 long
409 coff_get_symtab_upper_bound (bfd *abfd)
411 if (!bfd_coff_slurp_symbol_table (abfd))
412 return -1;
414 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
417 /* Canonicalize a COFF symbol table. */
419 long
420 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
422 unsigned int counter;
423 coff_symbol_type *symbase;
424 coff_symbol_type **location = (coff_symbol_type **) alocation;
426 if (!bfd_coff_slurp_symbol_table (abfd))
427 return -1;
429 symbase = obj_symbols (abfd);
430 counter = bfd_get_symcount (abfd);
431 while (counter-- > 0)
432 *location++ = symbase++;
434 *location = NULL;
436 return bfd_get_symcount (abfd);
439 /* Get the name of a symbol. The caller must pass in a buffer of size
440 >= SYMNMLEN + 1. */
442 const char *
443 _bfd_coff_internal_syment_name (bfd *abfd,
444 const struct internal_syment *sym,
445 char *buf)
447 /* FIXME: It's not clear this will work correctly if sizeof
448 (_n_zeroes) != 4. */
449 if (sym->_n._n_n._n_zeroes != 0
450 || sym->_n._n_n._n_offset == 0)
452 memcpy (buf, sym->_n._n_name, SYMNMLEN);
453 buf[SYMNMLEN] = '\0';
454 return buf;
456 else
458 const char *strings;
460 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
461 strings = obj_coff_strings (abfd);
462 if (strings == NULL)
464 strings = _bfd_coff_read_string_table (abfd);
465 if (strings == NULL)
466 return NULL;
468 /* PR 17910: Only check for string overflow if the length has been set.
469 Some DLLs, eg those produced by Visual Studio, may not set the length field. */
470 if (obj_coff_strings_len (abfd) > 0
471 && sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
472 return NULL;
473 return strings + sym->_n._n_n._n_offset;
477 /* Read in and swap the relocs. This returns a buffer holding the
478 relocs for section SEC in file ABFD. If CACHE is TRUE and
479 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
480 the function is called again. If EXTERNAL_RELOCS is not NULL, it
481 is a buffer large enough to hold the unswapped relocs. If
482 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
483 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
484 value must be INTERNAL_RELOCS. The function returns NULL on error. */
486 struct internal_reloc *
487 _bfd_coff_read_internal_relocs (bfd *abfd,
488 asection *sec,
489 bool cache,
490 bfd_byte *external_relocs,
491 bool require_internal,
492 struct internal_reloc *internal_relocs)
494 bfd_size_type relsz;
495 bfd_byte *free_external = NULL;
496 struct internal_reloc *free_internal = NULL;
497 bfd_byte *erel;
498 bfd_byte *erel_end;
499 struct internal_reloc *irel;
500 bfd_size_type amt;
502 if (sec->reloc_count == 0)
503 return internal_relocs; /* Nothing to do. */
505 if (coff_section_data (abfd, sec) != NULL
506 && coff_section_data (abfd, sec)->relocs != NULL)
508 if (! require_internal)
509 return coff_section_data (abfd, sec)->relocs;
510 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
511 sec->reloc_count * sizeof (struct internal_reloc));
512 return internal_relocs;
515 relsz = bfd_coff_relsz (abfd);
517 amt = sec->reloc_count * relsz;
518 if (external_relocs == NULL)
520 free_external = (bfd_byte *) bfd_malloc (amt);
521 if (free_external == NULL)
522 goto error_return;
523 external_relocs = free_external;
526 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
527 || bfd_bread (external_relocs, amt, abfd) != amt)
528 goto error_return;
530 if (internal_relocs == NULL)
532 amt = sec->reloc_count;
533 amt *= sizeof (struct internal_reloc);
534 free_internal = (struct internal_reloc *) bfd_malloc (amt);
535 if (free_internal == NULL)
536 goto error_return;
537 internal_relocs = free_internal;
540 /* Swap in the relocs. */
541 erel = external_relocs;
542 erel_end = erel + relsz * sec->reloc_count;
543 irel = internal_relocs;
544 for (; erel < erel_end; erel += relsz, irel++)
545 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
547 free (free_external);
548 free_external = NULL;
550 if (cache && free_internal != NULL)
552 if (coff_section_data (abfd, sec) == NULL)
554 amt = sizeof (struct coff_section_tdata);
555 sec->used_by_bfd = bfd_zalloc (abfd, amt);
556 if (sec->used_by_bfd == NULL)
557 goto error_return;
558 coff_section_data (abfd, sec)->contents = NULL;
560 coff_section_data (abfd, sec)->relocs = free_internal;
563 return internal_relocs;
565 error_return:
566 free (free_external);
567 free (free_internal);
568 return NULL;
571 /* Set lineno_count for the output sections of a COFF file. */
574 coff_count_linenumbers (bfd *abfd)
576 unsigned int limit = bfd_get_symcount (abfd);
577 unsigned int i;
578 int total = 0;
579 asymbol **p;
580 asection *s;
582 if (limit == 0)
584 /* This may be from the backend linker, in which case the
585 lineno_count in the sections is correct. */
586 for (s = abfd->sections; s != NULL; s = s->next)
587 total += s->lineno_count;
588 return total;
591 for (s = abfd->sections; s != NULL; s = s->next)
592 BFD_ASSERT (s->lineno_count == 0);
594 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
596 asymbol *q_maybe = *p;
598 if (bfd_asymbol_bfd (q_maybe) != NULL
599 && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
601 coff_symbol_type *q = coffsymbol (q_maybe);
603 /* The AIX 4.1 compiler can sometimes generate line numbers
604 attached to debugging symbols. We try to simply ignore
605 those here. */
606 if (q->lineno != NULL
607 && q->symbol.section->owner != NULL)
609 /* This symbol has line numbers. Increment the owning
610 section's linenumber count. */
611 alent *l = q->lineno;
615 asection * sec = q->symbol.section->output_section;
617 /* Do not try to update fields in read-only sections. */
618 if (! bfd_is_const_section (sec))
619 sec->lineno_count ++;
621 ++total;
622 ++l;
624 while (l->line_number != 0);
629 return total;
632 static void
633 fixup_symbol_value (bfd *abfd,
634 coff_symbol_type *coff_symbol_ptr,
635 struct internal_syment *syment)
637 /* Normalize the symbol flags. */
638 if (coff_symbol_ptr->symbol.section
639 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
641 /* A common symbol is undefined with a value. */
642 syment->n_scnum = N_UNDEF;
643 syment->n_value = coff_symbol_ptr->symbol.value;
645 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
646 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
648 syment->n_value = coff_symbol_ptr->symbol.value;
650 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
652 syment->n_scnum = N_UNDEF;
653 syment->n_value = 0;
655 /* FIXME: Do we need to handle the absolute section here? */
656 else
658 if (coff_symbol_ptr->symbol.section)
660 syment->n_scnum =
661 coff_symbol_ptr->symbol.section->output_section->target_index;
663 syment->n_value = (coff_symbol_ptr->symbol.value
664 + coff_symbol_ptr->symbol.section->output_offset);
665 if (! obj_pe (abfd))
667 syment->n_value += (syment->n_sclass == C_STATLAB)
668 ? coff_symbol_ptr->symbol.section->output_section->lma
669 : coff_symbol_ptr->symbol.section->output_section->vma;
672 else
674 BFD_ASSERT (0);
675 /* This can happen, but I don't know why yet (steve@cygnus.com) */
676 syment->n_scnum = N_ABS;
677 syment->n_value = coff_symbol_ptr->symbol.value;
682 /* Run through all the symbols in the symbol table and work out what
683 their indexes into the symbol table will be when output.
685 Coff requires that each C_FILE symbol points to the next one in the
686 chain, and that the last one points to the first external symbol. We
687 do that here too. */
689 bool
690 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
692 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
693 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
694 unsigned int native_index = 0;
695 struct internal_syment *last_file = NULL;
696 unsigned int symbol_index;
698 /* COFF demands that undefined symbols come after all other symbols.
699 Since we don't need to impose this extra knowledge on all our
700 client programs, deal with that here. Sort the symbol table;
701 just move the undefined symbols to the end, leaving the rest
702 alone. The O'Reilly book says that defined global symbols come
703 at the end before the undefined symbols, so we do that here as
704 well. */
705 /* @@ Do we have some condition we could test for, so we don't always
706 have to do this? I don't think relocatability is quite right, but
707 I'm not certain. [raeburn:19920508.1711EST] */
709 asymbol **newsyms;
710 unsigned int i;
711 bfd_size_type amt;
713 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
714 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
715 if (!newsyms)
716 return false;
717 bfd_ptr->outsymbols = newsyms;
718 for (i = 0; i < symbol_count; i++)
719 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
720 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
721 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
722 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
723 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
724 == 0))))
725 *newsyms++ = symbol_ptr_ptr[i];
727 for (i = 0; i < symbol_count; i++)
728 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
729 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
730 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
731 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
732 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
733 != 0))))
734 *newsyms++ = symbol_ptr_ptr[i];
736 *first_undef = newsyms - bfd_ptr->outsymbols;
738 for (i = 0; i < symbol_count; i++)
739 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
740 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
741 *newsyms++ = symbol_ptr_ptr[i];
742 *newsyms = (asymbol *) NULL;
743 symbol_ptr_ptr = bfd_ptr->outsymbols;
746 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
748 coff_symbol_type *coff_symbol_ptr;
750 coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
751 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
752 if (coff_symbol_ptr && coff_symbol_ptr->native)
754 combined_entry_type *s = coff_symbol_ptr->native;
755 int i;
757 BFD_ASSERT (s->is_sym);
758 if (s->u.syment.n_sclass == C_FILE)
760 if (last_file != NULL)
761 last_file->n_value = native_index;
762 last_file = &(s->u.syment);
764 else
765 /* Modify the symbol values according to their section and
766 type. */
767 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
769 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
770 s[i].offset = native_index++;
772 else
773 native_index++;
776 obj_conv_table_size (bfd_ptr) = native_index;
778 return true;
781 /* Run thorough the symbol table again, and fix it so that all
782 pointers to entries are changed to the entries' index in the output
783 symbol table. */
785 void
786 coff_mangle_symbols (bfd *bfd_ptr)
788 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
789 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
790 unsigned int symbol_index;
792 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
794 coff_symbol_type *coff_symbol_ptr;
796 coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
797 if (coff_symbol_ptr && coff_symbol_ptr->native)
799 int i;
800 combined_entry_type *s = coff_symbol_ptr->native;
802 BFD_ASSERT (s->is_sym);
803 if (s->fix_value)
805 /* FIXME: We should use a union here. */
806 s->u.syment.n_value =
807 (uintptr_t) ((combined_entry_type *)
808 (uintptr_t) s->u.syment.n_value)->offset;
809 s->fix_value = 0;
811 if (s->fix_line)
813 /* The value is the offset into the line number entries
814 for the symbol's section. On output, the symbol's
815 section should be N_DEBUG. */
816 s->u.syment.n_value =
817 (coff_symbol_ptr->symbol.section->output_section->line_filepos
818 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
819 coff_symbol_ptr->symbol.section =
820 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
821 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
823 for (i = 0; i < s->u.syment.n_numaux; i++)
825 combined_entry_type *a = s + i + 1;
827 BFD_ASSERT (! a->is_sym);
828 if (a->fix_tag)
830 a->u.auxent.x_sym.x_tagndx.l =
831 a->u.auxent.x_sym.x_tagndx.p->offset;
832 a->fix_tag = 0;
834 if (a->fix_end)
836 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
837 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
838 a->fix_end = 0;
840 if (a->fix_scnlen)
842 a->u.auxent.x_csect.x_scnlen.l =
843 a->u.auxent.x_csect.x_scnlen.p->offset;
844 a->fix_scnlen = 0;
851 static bool
852 coff_write_auxent_fname (bfd *abfd,
853 char *str,
854 union internal_auxent *auxent,
855 struct bfd_strtab_hash *strtab,
856 bool hash)
858 unsigned int str_length = strlen (str);
859 unsigned int filnmlen = bfd_coff_filnmlen (abfd);
861 if (bfd_coff_long_filenames (abfd))
863 if (str_length <= filnmlen)
864 strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
865 else
867 bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
869 if (indx == (bfd_size_type) -1)
870 return false;
872 auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
873 auxent->x_file.x_n.x_n.x_zeroes = 0;
876 else
878 strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
879 if (str_length > filnmlen)
880 str[filnmlen] = '\0';
883 return true;
886 static bool
887 coff_fix_symbol_name (bfd *abfd,
888 asymbol *symbol,
889 combined_entry_type *native,
890 struct bfd_strtab_hash *strtab,
891 bool hash,
892 asection **debug_string_section_p,
893 bfd_size_type *debug_string_size_p)
895 unsigned int name_length;
896 char *name = (char *) (symbol->name);
897 bfd_size_type indx;
899 if (name == NULL)
901 /* COFF symbols always have names, so we'll make one up. */
902 symbol->name = "strange";
903 name = (char *) symbol->name;
905 name_length = strlen (name);
907 BFD_ASSERT (native->is_sym);
908 if (native->u.syment.n_sclass == C_FILE
909 && native->u.syment.n_numaux > 0)
911 if (bfd_coff_force_symnames_in_strings (abfd))
913 indx = _bfd_stringtab_add (strtab, ".file", hash, false);
914 if (indx == (bfd_size_type) -1)
915 return false;
917 native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
918 native->u.syment._n._n_n._n_zeroes = 0;
920 else
921 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
923 BFD_ASSERT (! (native + 1)->is_sym);
924 if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
925 strtab, hash))
926 return false;
928 else
930 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
931 /* This name will fit into the symbol neatly. */
932 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
934 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
936 indx = _bfd_stringtab_add (strtab, name, hash, false);
937 if (indx == (bfd_size_type) -1)
938 return false;
940 native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
941 native->u.syment._n._n_n._n_zeroes = 0;
943 else
945 file_ptr filepos;
946 bfd_byte buf[4];
947 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
949 /* This name should be written into the .debug section. For
950 some reason each name is preceded by a two byte length
951 and also followed by a null byte. FIXME: We assume that
952 the .debug section has already been created, and that it
953 is large enough. */
954 if (*debug_string_section_p == (asection *) NULL)
955 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
956 filepos = bfd_tell (abfd);
957 if (prefix_len == 4)
958 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
959 else
960 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
962 if (!bfd_set_section_contents (abfd,
963 *debug_string_section_p,
964 (void *) buf,
965 (file_ptr) *debug_string_size_p,
966 (bfd_size_type) prefix_len)
967 || !bfd_set_section_contents (abfd,
968 *debug_string_section_p,
969 (void *) symbol->name,
970 (file_ptr) (*debug_string_size_p
971 + prefix_len),
972 (bfd_size_type) name_length + 1))
973 abort ();
974 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
975 abort ();
976 native->u.syment._n._n_n._n_offset =
977 *debug_string_size_p + prefix_len;
978 native->u.syment._n._n_n._n_zeroes = 0;
979 *debug_string_size_p += name_length + 1 + prefix_len;
983 return true;
986 /* We need to keep track of the symbol index so that when we write out
987 the relocs we can get the index for a symbol. This method is a
988 hack. FIXME. */
990 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
992 /* Write a symbol out to a COFF file. */
994 static bool
995 coff_write_symbol (bfd *abfd,
996 asymbol *symbol,
997 combined_entry_type *native,
998 bfd_vma *written,
999 struct bfd_strtab_hash *strtab,
1000 bool hash,
1001 asection **debug_string_section_p,
1002 bfd_size_type *debug_string_size_p)
1004 unsigned int numaux = native->u.syment.n_numaux;
1005 int type = native->u.syment.n_type;
1006 int n_sclass = (int) native->u.syment.n_sclass;
1007 asection *output_section = symbol->section->output_section
1008 ? symbol->section->output_section
1009 : symbol->section;
1010 void * buf;
1011 bfd_size_type symesz;
1013 BFD_ASSERT (native->is_sym);
1015 if (native->u.syment.n_sclass == C_FILE)
1016 symbol->flags |= BSF_DEBUGGING;
1018 if (symbol->flags & BSF_DEBUGGING
1019 && bfd_is_abs_section (symbol->section))
1020 native->u.syment.n_scnum = N_DEBUG;
1022 else if (bfd_is_abs_section (symbol->section))
1023 native->u.syment.n_scnum = N_ABS;
1025 else if (bfd_is_und_section (symbol->section))
1026 native->u.syment.n_scnum = N_UNDEF;
1028 else
1029 native->u.syment.n_scnum =
1030 output_section->target_index;
1032 if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
1033 debug_string_section_p, debug_string_size_p))
1034 return false;
1036 symesz = bfd_coff_symesz (abfd);
1037 buf = bfd_alloc (abfd, symesz);
1038 if (!buf)
1039 return false;
1040 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1041 if (bfd_bwrite (buf, symesz, abfd) != symesz)
1042 return false;
1043 bfd_release (abfd, buf);
1045 if (native->u.syment.n_numaux > 0)
1047 bfd_size_type auxesz;
1048 unsigned int j;
1050 auxesz = bfd_coff_auxesz (abfd);
1051 buf = bfd_alloc (abfd, auxesz);
1052 if (!buf)
1053 return false;
1054 for (j = 0; j < native->u.syment.n_numaux; j++)
1056 BFD_ASSERT (! (native + j + 1)->is_sym);
1058 /* Adjust auxent only if this isn't the filename
1059 auxiliary entry. */
1060 if (native->u.syment.n_sclass == C_FILE
1061 && (native + j + 1)->u.auxent.x_file.x_ftype)
1062 coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap,
1063 &(native + j + 1)->u.auxent, strtab, hash);
1065 bfd_coff_swap_aux_out (abfd,
1066 &((native + j + 1)->u.auxent),
1067 type, n_sclass, (int) j,
1068 native->u.syment.n_numaux,
1069 buf);
1070 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1071 return false;
1073 bfd_release (abfd, buf);
1076 /* Store the index for use when we write out the relocs. */
1077 set_index (symbol, *written);
1079 *written += numaux + 1;
1080 return true;
1083 /* Write out a symbol to a COFF file that does not come from a COFF
1084 file originally. This symbol may have been created by the linker,
1085 or we may be linking a non COFF file to a COFF file. */
1087 bool
1088 coff_write_alien_symbol (bfd *abfd,
1089 asymbol *symbol,
1090 struct internal_syment *isym,
1091 bfd_vma *written,
1092 struct bfd_strtab_hash *strtab,
1093 bool hash,
1094 asection **debug_string_section_p,
1095 bfd_size_type *debug_string_size_p)
1097 combined_entry_type *native;
1098 combined_entry_type dummy[2];
1099 asection *output_section = symbol->section->output_section
1100 ? symbol->section->output_section
1101 : symbol->section;
1102 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1103 bool ret;
1105 if ((!link_info || link_info->strip_discarded)
1106 && !bfd_is_abs_section (symbol->section)
1107 && symbol->section->output_section == bfd_abs_section_ptr)
1109 symbol->name = "";
1110 if (isym != NULL)
1111 memset (isym, 0, sizeof (*isym));
1112 return true;
1114 memset (dummy, 0, sizeof dummy);
1115 native = dummy;
1116 native->is_sym = true;
1117 native[1].is_sym = false;
1118 native->u.syment.n_type = T_NULL;
1119 native->u.syment.n_flags = 0;
1120 native->u.syment.n_numaux = 0;
1121 if (bfd_is_und_section (symbol->section))
1123 native->u.syment.n_scnum = N_UNDEF;
1124 native->u.syment.n_value = symbol->value;
1126 else if (bfd_is_com_section (symbol->section))
1128 native->u.syment.n_scnum = N_UNDEF;
1129 native->u.syment.n_value = symbol->value;
1131 else if (symbol->flags & BSF_FILE)
1133 native->u.syment.n_scnum = N_DEBUG;
1134 native->u.syment.n_numaux = 1;
1136 else if (symbol->flags & BSF_DEBUGGING)
1138 /* There isn't much point to writing out a debugging symbol
1139 unless we are prepared to convert it into COFF debugging
1140 format. So, we just ignore them. We must clobber the symbol
1141 name to keep it from being put in the string table. */
1142 symbol->name = "";
1143 if (isym != NULL)
1144 memset (isym, 0, sizeof (*isym));
1145 return true;
1147 else
1149 native->u.syment.n_scnum = output_section->target_index;
1150 native->u.syment.n_value = (symbol->value
1151 + symbol->section->output_offset);
1152 if (! obj_pe (abfd))
1153 native->u.syment.n_value += output_section->vma;
1155 /* Copy the any flags from the file header into the symbol.
1156 FIXME: Why? */
1158 coff_symbol_type *c = coff_symbol_from (symbol);
1159 if (c != (coff_symbol_type *) NULL)
1160 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1164 native->u.syment.n_type = 0;
1165 if (symbol->flags & BSF_FILE)
1166 native->u.syment.n_sclass = C_FILE;
1167 else if (symbol->flags & BSF_LOCAL)
1168 native->u.syment.n_sclass = C_STAT;
1169 else if (symbol->flags & BSF_WEAK)
1170 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1171 else
1172 native->u.syment.n_sclass = C_EXT;
1174 ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash,
1175 debug_string_section_p, debug_string_size_p);
1176 if (isym != NULL)
1177 *isym = native->u.syment;
1178 return ret;
1181 /* Write a native symbol to a COFF file. */
1183 static bool
1184 coff_write_native_symbol (bfd *abfd,
1185 coff_symbol_type *symbol,
1186 bfd_vma *written,
1187 struct bfd_strtab_hash *strtab,
1188 asection **debug_string_section_p,
1189 bfd_size_type *debug_string_size_p)
1191 combined_entry_type *native = symbol->native;
1192 alent *lineno = symbol->lineno;
1193 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1195 if ((!link_info || link_info->strip_discarded)
1196 && !bfd_is_abs_section (symbol->symbol.section)
1197 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1199 symbol->symbol.name = "";
1200 return true;
1203 BFD_ASSERT (native->is_sym);
1204 /* If this symbol has an associated line number, we must store the
1205 symbol index in the line number field. We also tag the auxent to
1206 point to the right place in the lineno table. */
1207 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1209 unsigned int count = 0;
1211 lineno[count].u.offset = *written;
1212 if (native->u.syment.n_numaux)
1214 union internal_auxent *a = &((native + 1)->u.auxent);
1216 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1217 symbol->symbol.section->output_section->moving_line_filepos;
1220 /* Count and relocate all other linenumbers. */
1221 count++;
1222 while (lineno[count].line_number != 0)
1224 lineno[count].u.offset +=
1225 (symbol->symbol.section->output_section->vma
1226 + symbol->symbol.section->output_offset);
1227 count++;
1229 symbol->done_lineno = true;
1231 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1232 symbol->symbol.section->output_section->moving_line_filepos +=
1233 count * bfd_coff_linesz (abfd);
1236 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1237 strtab, true, debug_string_section_p,
1238 debug_string_size_p);
1241 static void
1242 null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
1243 va_list ap ATTRIBUTE_UNUSED)
1247 /* Write out the COFF symbols. */
1249 bool
1250 coff_write_symbols (bfd *abfd)
1252 struct bfd_strtab_hash *strtab;
1253 asection *debug_string_section;
1254 bfd_size_type debug_string_size;
1255 unsigned int i;
1256 unsigned int limit = bfd_get_symcount (abfd);
1257 bfd_vma written = 0;
1258 asymbol **p;
1260 debug_string_section = NULL;
1261 debug_string_size = 0;
1263 strtab = _bfd_stringtab_init ();
1264 if (strtab == NULL)
1265 return false;
1267 /* If this target supports long section names, they must be put into
1268 the string table. This is supported by PE. This code must
1269 handle section names just as they are handled in
1270 coff_write_object_contents. This is why we pass hash as FALSE below. */
1271 if (bfd_coff_long_section_names (abfd))
1273 asection *o;
1275 for (o = abfd->sections; o != NULL; o = o->next)
1276 if (strlen (o->name) > SCNNMLEN
1277 && _bfd_stringtab_add (strtab, o->name, false, false)
1278 == (bfd_size_type) -1)
1279 return false;
1282 /* Seek to the right place. */
1283 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1284 return false;
1286 /* Output all the symbols we have. */
1287 written = 0;
1288 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1290 asymbol *symbol = *p;
1291 coff_symbol_type *c_symbol = coff_symbol_from (symbol);
1293 if (c_symbol == (coff_symbol_type *) NULL
1294 || c_symbol->native == (combined_entry_type *) NULL)
1296 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1297 strtab, true, &debug_string_section,
1298 &debug_string_size))
1299 return false;
1301 else
1303 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1305 bfd_error_handler_type current_error_handler;
1306 enum coff_symbol_classification sym_class;
1307 unsigned char *n_sclass;
1309 /* Suppress error reporting by bfd_coff_classify_symbol.
1310 Error messages can be generated when we are processing a local
1311 symbol which has no associated section and we do not have to
1312 worry about this, all we need to know is that it is local. */
1313 current_error_handler = bfd_set_error_handler (null_error_handler);
1314 BFD_ASSERT (c_symbol->native->is_sym);
1315 sym_class = bfd_coff_classify_symbol (abfd,
1316 &c_symbol->native->u.syment);
1317 (void) bfd_set_error_handler (current_error_handler);
1319 n_sclass = &c_symbol->native->u.syment.n_sclass;
1321 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1322 we cannot retain the existing sclass from the original symbol.
1323 Weak symbols only have one valid sclass, so just set it always.
1324 If it is not local class and should be, set it C_STAT.
1325 If it is global and not classified as global, or if it is
1326 weak (which is also classified as global), set it C_EXT. */
1328 if (symbol->flags & BSF_WEAK)
1329 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1330 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1331 *n_sclass = C_STAT;
1332 else if (symbol->flags & BSF_GLOBAL
1333 && (sym_class != COFF_SYMBOL_GLOBAL
1334 #ifdef COFF_WITH_PE
1335 || *n_sclass == C_NT_WEAK
1336 #endif
1337 || *n_sclass == C_WEAKEXT))
1338 c_symbol->native->u.syment.n_sclass = C_EXT;
1341 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1342 strtab, &debug_string_section,
1343 &debug_string_size))
1344 return false;
1348 obj_raw_syment_count (abfd) = written;
1350 /* Now write out strings.
1352 We would normally not write anything here if there are no strings, but
1353 we'll write out 4 so that any stupid coff reader which tries to read the
1354 string table even when there isn't one won't croak. */
1356 bfd_byte buffer[STRING_SIZE_SIZE];
1358 #if STRING_SIZE_SIZE == 4
1359 H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
1360 #else
1361 #error Change H_PUT_32
1362 #endif
1363 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1364 != sizeof (buffer))
1365 return false;
1367 if (! _bfd_stringtab_emit (abfd, strtab))
1368 return false;
1371 _bfd_stringtab_free (strtab);
1373 /* Make sure the .debug section was created to be the correct size.
1374 We should create it ourselves on the fly, but we don't because
1375 BFD won't let us write to any section until we know how large all
1376 the sections are. We could still do it by making another pass
1377 over the symbols. FIXME. */
1378 BFD_ASSERT (debug_string_size == 0
1379 || (debug_string_section != (asection *) NULL
1380 && (BFD_ALIGN (debug_string_size,
1381 1 << debug_string_section->alignment_power)
1382 == debug_string_section->size)));
1384 return true;
1387 bool
1388 coff_write_linenumbers (bfd *abfd)
1390 asection *s;
1391 bfd_size_type linesz;
1392 void * buff;
1394 linesz = bfd_coff_linesz (abfd);
1395 buff = bfd_alloc (abfd, linesz);
1396 if (!buff)
1397 return false;
1398 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1400 if (s->lineno_count)
1402 asymbol **q = abfd->outsymbols;
1403 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1404 return false;
1405 /* Find all the linenumbers in this section. */
1406 while (*q)
1408 asymbol *p = *q;
1409 if (p->section->output_section == s)
1411 alent *l =
1412 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1413 (bfd_asymbol_bfd (p), p));
1414 if (l)
1416 /* Found a linenumber entry, output. */
1417 struct internal_lineno out;
1419 memset ((void *) & out, 0, sizeof (out));
1420 out.l_lnno = 0;
1421 out.l_addr.l_symndx = l->u.offset;
1422 bfd_coff_swap_lineno_out (abfd, &out, buff);
1423 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1424 != linesz)
1425 return false;
1426 l++;
1427 while (l->line_number)
1429 out.l_lnno = l->line_number;
1430 out.l_addr.l_symndx = l->u.offset;
1431 bfd_coff_swap_lineno_out (abfd, &out, buff);
1432 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1433 != linesz)
1434 return false;
1435 l++;
1439 q++;
1443 bfd_release (abfd, buff);
1444 return true;
1447 alent *
1448 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1450 return coffsymbol (symbol)->lineno;
1453 /* This function transforms the offsets into the symbol table into
1454 pointers to syments. */
1456 static void
1457 coff_pointerize_aux (bfd *abfd,
1458 combined_entry_type *table_base,
1459 combined_entry_type *symbol,
1460 unsigned int indaux,
1461 combined_entry_type *auxent,
1462 combined_entry_type *table_end)
1464 unsigned int type = symbol->u.syment.n_type;
1465 unsigned int n_sclass = symbol->u.syment.n_sclass;
1467 BFD_ASSERT (symbol->is_sym);
1468 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1470 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1471 (abfd, table_base, symbol, indaux, auxent))
1472 return;
1475 /* Don't bother if this is a file or a section. */
1476 if (n_sclass == C_STAT && type == T_NULL)
1477 return;
1478 if (n_sclass == C_FILE)
1479 return;
1480 if (n_sclass == C_DWARF)
1481 return;
1483 BFD_ASSERT (! auxent->is_sym);
1484 /* Otherwise patch up. */
1485 #define N_TMASK coff_data (abfd)->local_n_tmask
1486 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1488 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1489 || n_sclass == C_FCN)
1490 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0
1491 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l
1492 < (long) obj_raw_syment_count (abfd)
1493 && table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l
1494 < table_end)
1496 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1497 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1498 auxent->fix_end = 1;
1501 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1502 generate one, so we must be careful to ignore it. */
1503 if ((unsigned long) auxent->u.auxent.x_sym.x_tagndx.l
1504 < obj_raw_syment_count (abfd)
1505 && table_base + auxent->u.auxent.x_sym.x_tagndx.l < table_end)
1507 auxent->u.auxent.x_sym.x_tagndx.p =
1508 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1509 auxent->fix_tag = 1;
1513 /* Allocate space for the ".debug" section, and read it.
1514 We did not read the debug section until now, because
1515 we didn't want to go to the trouble until someone needed it. */
1517 static char *
1518 build_debug_section (bfd *abfd, asection ** sect_return)
1520 char *debug_section;
1521 file_ptr position;
1522 bfd_size_type sec_size;
1524 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1526 if (!sect)
1528 bfd_set_error (bfd_error_no_debug_section);
1529 return NULL;
1532 /* Seek to the beginning of the `.debug' section and read it.
1533 Save the current position first; it is needed by our caller.
1534 Then read debug section and reset the file pointer. */
1536 position = bfd_tell (abfd);
1537 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
1538 return NULL;
1540 sec_size = sect->size;
1541 debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size, sec_size);
1542 if (debug_section == NULL)
1543 return NULL;
1545 if (bfd_seek (abfd, position, SEEK_SET) != 0)
1546 return NULL;
1548 * sect_return = sect;
1549 return debug_section;
1552 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1553 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1554 be \0-terminated. */
1556 static char *
1557 copy_name (bfd *abfd, char *name, size_t maxlen)
1559 size_t len;
1560 char *newname;
1562 for (len = 0; len < maxlen; ++len)
1563 if (name[len] == '\0')
1564 break;
1566 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1567 return NULL;
1569 strncpy (newname, name, len);
1570 newname[len] = '\0';
1571 return newname;
1574 /* Read in the external symbols. */
1576 bool
1577 _bfd_coff_get_external_symbols (bfd *abfd)
1579 size_t symesz;
1580 size_t size;
1581 void * syms;
1583 if (obj_coff_external_syms (abfd) != NULL)
1584 return true;
1586 symesz = bfd_coff_symesz (abfd);
1587 if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
1589 bfd_set_error (bfd_error_file_truncated);
1590 return false;
1593 if (size == 0)
1594 return true;
1596 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1597 return false;
1598 syms = _bfd_malloc_and_read (abfd, size, size);
1599 obj_coff_external_syms (abfd) = syms;
1600 return syms != NULL;
1603 /* Read in the external strings. The strings are not loaded until
1604 they are needed. This is because we have no simple way of
1605 detecting a missing string table in an archive. If the strings
1606 are loaded then the STRINGS and STRINGS_LEN fields in the
1607 coff_tdata structure will be set. */
1609 const char *
1610 _bfd_coff_read_string_table (bfd *abfd)
1612 char extstrsize[STRING_SIZE_SIZE];
1613 bfd_size_type strsize;
1614 char *strings;
1615 ufile_ptr pos;
1616 ufile_ptr filesize;
1617 size_t symesz;
1618 size_t size;
1620 if (obj_coff_strings (abfd) != NULL)
1621 return obj_coff_strings (abfd);
1623 if (obj_sym_filepos (abfd) == 0)
1625 bfd_set_error (bfd_error_no_symbols);
1626 return NULL;
1629 symesz = bfd_coff_symesz (abfd);
1630 pos = obj_sym_filepos (abfd);
1631 if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
1632 || pos + size < pos)
1634 bfd_set_error (bfd_error_file_truncated);
1635 return NULL;
1638 if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
1639 return NULL;
1641 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1642 != sizeof extstrsize)
1644 if (bfd_get_error () != bfd_error_file_truncated)
1645 return NULL;
1647 /* There is no string table. */
1648 strsize = STRING_SIZE_SIZE;
1650 else
1652 #if STRING_SIZE_SIZE == 4
1653 strsize = H_GET_32 (abfd, extstrsize);
1654 #else
1655 #error Change H_GET_32
1656 #endif
1659 filesize = bfd_get_file_size (abfd);
1660 if (strsize < STRING_SIZE_SIZE
1661 || (filesize != 0 && strsize > filesize))
1663 _bfd_error_handler
1664 /* xgettext: c-format */
1665 (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
1666 bfd_set_error (bfd_error_bad_value);
1667 return NULL;
1670 strings = (char *) bfd_malloc (strsize + 1);
1671 if (strings == NULL)
1672 return NULL;
1674 /* PR 17521 file: 079-54929-0.004.
1675 A corrupt file could contain an index that points into the first
1676 STRING_SIZE_SIZE bytes of the string table, so make sure that
1677 they are zero. */
1678 memset (strings, 0, STRING_SIZE_SIZE);
1680 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1681 != strsize - STRING_SIZE_SIZE)
1683 free (strings);
1684 return NULL;
1687 obj_coff_strings (abfd) = strings;
1688 obj_coff_strings_len (abfd) = strsize;
1689 /* Terminate the string table, just in case. */
1690 strings[strsize] = 0;
1691 return strings;
1694 /* Free up the external symbols and strings read from a COFF file. */
1696 bool
1697 _bfd_coff_free_symbols (bfd *abfd)
1699 if (! bfd_family_coff (abfd))
1700 return false;
1702 if (obj_coff_external_syms (abfd) != NULL
1703 && ! obj_coff_keep_syms (abfd))
1705 free (obj_coff_external_syms (abfd));
1706 obj_coff_external_syms (abfd) = NULL;
1709 if (obj_coff_strings (abfd) != NULL
1710 && ! obj_coff_keep_strings (abfd))
1712 free (obj_coff_strings (abfd));
1713 obj_coff_strings (abfd) = NULL;
1714 obj_coff_strings_len (abfd) = 0;
1717 return true;
1720 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1721 knit the symbol names into a normalized form. By normalized here I
1722 mean that all symbols have an n_offset pointer that points to a null-
1723 terminated string. */
1725 combined_entry_type *
1726 coff_get_normalized_symtab (bfd *abfd)
1728 combined_entry_type *internal;
1729 combined_entry_type *internal_ptr;
1730 combined_entry_type *symbol_ptr;
1731 combined_entry_type *internal_end;
1732 size_t symesz;
1733 char *raw_src;
1734 char *raw_end;
1735 const char *string_table = NULL;
1736 asection * debug_sec = NULL;
1737 char *debug_sec_data = NULL;
1738 bfd_size_type size;
1740 if (obj_raw_syments (abfd) != NULL)
1741 return obj_raw_syments (abfd);
1743 if (! _bfd_coff_get_external_symbols (abfd))
1744 return NULL;
1746 size = obj_raw_syment_count (abfd);
1747 /* Check for integer overflow. */
1748 if (size > (bfd_size_type) -1 / sizeof (combined_entry_type))
1749 return NULL;
1750 size *= sizeof (combined_entry_type);
1751 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1752 if (internal == NULL && size != 0)
1753 return NULL;
1754 internal_end = internal + obj_raw_syment_count (abfd);
1756 raw_src = (char *) obj_coff_external_syms (abfd);
1758 /* Mark the end of the symbols. */
1759 symesz = bfd_coff_symesz (abfd);
1760 raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz);
1762 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1763 probably possible. If one shows up, it will probably kill us. */
1765 /* Swap all the raw entries. */
1766 for (internal_ptr = internal;
1767 raw_src < raw_end;
1768 raw_src += symesz, internal_ptr++)
1770 unsigned int i;
1772 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1773 (void *) & internal_ptr->u.syment);
1774 symbol_ptr = internal_ptr;
1775 internal_ptr->is_sym = true;
1777 /* PR 17512: Prevent buffer overrun. */
1778 if (symbol_ptr->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
1780 bfd_release (abfd, internal);
1781 return NULL;
1784 for (i = 0;
1785 i < symbol_ptr->u.syment.n_numaux;
1786 i++)
1788 internal_ptr++;
1789 raw_src += symesz;
1791 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1792 symbol_ptr->u.syment.n_type,
1793 symbol_ptr->u.syment.n_sclass,
1794 (int) i, symbol_ptr->u.syment.n_numaux,
1795 &(internal_ptr->u.auxent));
1797 internal_ptr->is_sym = false;
1798 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1799 internal_ptr, internal_end);
1803 /* Free the raw symbols. */
1804 if (obj_coff_external_syms (abfd) != NULL
1805 && ! obj_coff_keep_syms (abfd))
1807 free (obj_coff_external_syms (abfd));
1808 obj_coff_external_syms (abfd) = NULL;
1811 for (internal_ptr = internal; internal_ptr < internal_end;
1812 internal_ptr++)
1814 BFD_ASSERT (internal_ptr->is_sym);
1816 if (internal_ptr->u.syment.n_sclass == C_FILE
1817 && internal_ptr->u.syment.n_numaux > 0)
1819 combined_entry_type * aux = internal_ptr + 1;
1821 /* Make a file symbol point to the name in the auxent, since
1822 the text ".file" is redundant. */
1823 BFD_ASSERT (! aux->is_sym);
1825 if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1827 /* The filename is a long one, point into the string table. */
1828 if (string_table == NULL)
1830 string_table = _bfd_coff_read_string_table (abfd);
1831 if (string_table == NULL)
1832 return NULL;
1835 if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_n.x_offset)
1836 >= obj_coff_strings_len (abfd))
1837 internal_ptr->u.syment._n._n_n._n_offset =
1838 (uintptr_t) _("<corrupt>");
1839 else
1840 internal_ptr->u.syment._n._n_n._n_offset =
1841 (uintptr_t) (string_table
1842 + aux->u.auxent.x_file.x_n.x_n.x_offset);
1844 else
1846 /* Ordinary short filename, put into memory anyway. The
1847 Microsoft PE tools sometimes store a filename in
1848 multiple AUX entries. */
1849 if (internal_ptr->u.syment.n_numaux > 1
1850 && coff_data (abfd)->pe)
1851 internal_ptr->u.syment._n._n_n._n_offset =
1852 ((uintptr_t)
1853 copy_name (abfd,
1854 aux->u.auxent.x_file.x_n.x_fname,
1855 internal_ptr->u.syment.n_numaux * symesz));
1856 else
1857 internal_ptr->u.syment._n._n_n._n_offset =
1858 ((uintptr_t)
1859 copy_name (abfd,
1860 aux->u.auxent.x_file.x_n.x_fname,
1861 (size_t) bfd_coff_filnmlen (abfd)));
1864 /* Normalize other strings available in C_FILE aux entries. */
1865 if (!coff_data (abfd)->pe)
1866 for (int numaux = 1; numaux < internal_ptr->u.syment.n_numaux; numaux++)
1868 aux = internal_ptr + numaux + 1;
1869 BFD_ASSERT (! aux->is_sym);
1871 if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1873 /* The string information is a long one, point into the string table. */
1874 if (string_table == NULL)
1876 string_table = _bfd_coff_read_string_table (abfd);
1877 if (string_table == NULL)
1878 return NULL;
1881 if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_n.x_offset)
1882 >= obj_coff_strings_len (abfd))
1883 aux->u.auxent.x_file.x_n.x_n.x_offset =
1884 (uintptr_t) _("<corrupt>");
1885 else
1886 aux->u.auxent.x_file.x_n.x_n.x_offset =
1887 (uintptr_t) (string_table
1888 + (aux->u.auxent.x_file.x_n.x_n.x_offset));
1890 else
1891 aux->u.auxent.x_file.x_n.x_n.x_offset =
1892 ((uintptr_t)
1893 copy_name (abfd,
1894 aux->u.auxent.x_file.x_n.x_fname,
1895 (size_t) bfd_coff_filnmlen (abfd)));
1899 else
1901 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1903 /* This is a "short" name. Make it long. */
1904 size_t i;
1905 char *newstring;
1907 /* Find the length of this string without walking into memory
1908 that isn't ours. */
1909 for (i = 0; i < 8; ++i)
1910 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1911 break;
1913 newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1914 if (newstring == NULL)
1915 return NULL;
1916 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1917 internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
1918 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1920 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1921 internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) "";
1922 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1924 /* Long name already. Point symbol at the string in the
1925 table. */
1926 if (string_table == NULL)
1928 string_table = _bfd_coff_read_string_table (abfd);
1929 if (string_table == NULL)
1930 return NULL;
1932 if (internal_ptr->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd)
1933 || string_table + internal_ptr->u.syment._n._n_n._n_offset < string_table)
1934 internal_ptr->u.syment._n._n_n._n_offset =
1935 (uintptr_t) _("<corrupt>");
1936 else
1937 internal_ptr->u.syment._n._n_n._n_offset =
1938 ((uintptr_t) (string_table
1939 + internal_ptr->u.syment._n._n_n._n_offset));
1941 else
1943 /* Long name in debug section. Very similar. */
1944 if (debug_sec_data == NULL)
1945 debug_sec_data = build_debug_section (abfd, & debug_sec);
1946 if (debug_sec_data != NULL)
1948 BFD_ASSERT (debug_sec != NULL);
1949 /* PR binutils/17512: Catch out of range offsets into the debug data. */
1950 if (internal_ptr->u.syment._n._n_n._n_offset > debug_sec->size
1951 || debug_sec_data + internal_ptr->u.syment._n._n_n._n_offset < debug_sec_data)
1952 internal_ptr->u.syment._n._n_n._n_offset =
1953 (uintptr_t) _("<corrupt>");
1954 else
1955 internal_ptr->u.syment._n._n_n._n_offset =
1956 (uintptr_t) (debug_sec_data
1957 + internal_ptr->u.syment._n._n_n._n_offset);
1959 else
1960 internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) "";
1963 internal_ptr += internal_ptr->u.syment.n_numaux;
1966 obj_raw_syments (abfd) = internal;
1967 BFD_ASSERT (obj_raw_syment_count (abfd)
1968 == (unsigned int) (internal_ptr - internal));
1970 return internal;
1973 long
1974 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1976 if (bfd_get_format (abfd) != bfd_object)
1978 bfd_set_error (bfd_error_invalid_operation);
1979 return -1;
1981 #if SIZEOF_LONG == SIZEOF_INT
1982 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
1984 bfd_set_error (bfd_error_file_too_big);
1985 return -1;
1987 #endif
1988 return (asect->reloc_count + 1L) * sizeof (arelent *);
1991 asymbol *
1992 coff_make_empty_symbol (bfd *abfd)
1994 size_t amt = sizeof (coff_symbol_type);
1995 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1997 if (new_symbol == NULL)
1998 return NULL;
1999 new_symbol->symbol.section = 0;
2000 new_symbol->native = NULL;
2001 new_symbol->lineno = NULL;
2002 new_symbol->done_lineno = false;
2003 new_symbol->symbol.the_bfd = abfd;
2005 return & new_symbol->symbol;
2008 /* Make a debugging symbol. */
2010 asymbol *
2011 coff_bfd_make_debug_symbol (bfd *abfd,
2012 void * ptr ATTRIBUTE_UNUSED,
2013 unsigned long sz ATTRIBUTE_UNUSED)
2015 size_t amt = sizeof (coff_symbol_type);
2016 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
2018 if (new_symbol == NULL)
2019 return NULL;
2020 /* @@ The 10 is a guess at a plausible maximum number of aux entries
2021 (but shouldn't be a constant). */
2022 amt = sizeof (combined_entry_type) * 10;
2023 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2024 if (!new_symbol->native)
2025 return NULL;
2026 new_symbol->native->is_sym = true;
2027 new_symbol->symbol.section = bfd_abs_section_ptr;
2028 new_symbol->symbol.flags = BSF_DEBUGGING;
2029 new_symbol->lineno = NULL;
2030 new_symbol->done_lineno = false;
2031 new_symbol->symbol.the_bfd = abfd;
2033 return & new_symbol->symbol;
2036 void
2037 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
2039 bfd_symbol_info (symbol, ret);
2041 if (coffsymbol (symbol)->native != NULL
2042 && coffsymbol (symbol)->native->fix_value
2043 && coffsymbol (symbol)->native->is_sym)
2044 ret->value
2045 = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value
2046 - (uintptr_t) obj_raw_syments (abfd))
2047 / sizeof (combined_entry_type));
2050 /* Print out information about COFF symbol. */
2052 void
2053 coff_print_symbol (bfd *abfd,
2054 void * filep,
2055 asymbol *symbol,
2056 bfd_print_symbol_type how)
2058 FILE * file = (FILE *) filep;
2060 switch (how)
2062 case bfd_print_symbol_name:
2063 fprintf (file, "%s", symbol->name);
2064 break;
2066 case bfd_print_symbol_more:
2067 fprintf (file, "coff %s %s",
2068 coffsymbol (symbol)->native ? "n" : "g",
2069 coffsymbol (symbol)->lineno ? "l" : " ");
2070 break;
2072 case bfd_print_symbol_all:
2073 if (coffsymbol (symbol)->native)
2075 bfd_vma val;
2076 unsigned int aux;
2077 combined_entry_type *combined = coffsymbol (symbol)->native;
2078 combined_entry_type *root = obj_raw_syments (abfd);
2079 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2081 fprintf (file, "[%3ld]", (long) (combined - root));
2083 /* PR 17512: file: 079-33786-0.001:0.1. */
2084 if (combined < obj_raw_syments (abfd)
2085 || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
2087 fprintf (file, _("<corrupt info> %s"), symbol->name);
2088 break;
2091 BFD_ASSERT (combined->is_sym);
2092 if (! combined->fix_value)
2093 val = (bfd_vma) combined->u.syment.n_value;
2094 else
2095 val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root)
2096 / sizeof (combined_entry_type));
2098 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
2099 combined->u.syment.n_scnum,
2100 combined->u.syment.n_flags,
2101 combined->u.syment.n_type,
2102 combined->u.syment.n_sclass,
2103 combined->u.syment.n_numaux);
2104 bfd_fprintf_vma (abfd, file, val);
2105 fprintf (file, " %s", symbol->name);
2107 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2109 combined_entry_type *auxp = combined + aux + 1;
2110 long tagndx;
2112 BFD_ASSERT (! auxp->is_sym);
2113 if (auxp->fix_tag)
2114 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2115 else
2116 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2118 fprintf (file, "\n");
2120 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2121 continue;
2123 switch (combined->u.syment.n_sclass)
2125 case C_FILE:
2126 fprintf (file, "File ");
2127 /* Add additional information if this isn't the filename
2128 auxiliary entry. */
2129 if (auxp->u.auxent.x_file.x_ftype)
2130 fprintf (file, "ftype %d fname \"%s\"",
2131 auxp->u.auxent.x_file.x_ftype,
2132 (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset);
2133 break;
2135 case C_DWARF:
2136 fprintf (file, "AUX scnlen 0x%lx nreloc %ld",
2137 (unsigned long) auxp->u.auxent.x_sect.x_scnlen,
2138 auxp->u.auxent.x_sect.x_nreloc);
2139 break;
2141 case C_STAT:
2142 if (combined->u.syment.n_type == T_NULL)
2143 /* Probably a section symbol ? */
2145 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2146 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2147 auxp->u.auxent.x_scn.x_nreloc,
2148 auxp->u.auxent.x_scn.x_nlinno);
2149 if (auxp->u.auxent.x_scn.x_checksum != 0
2150 || auxp->u.auxent.x_scn.x_associated != 0
2151 || auxp->u.auxent.x_scn.x_comdat != 0)
2152 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2153 auxp->u.auxent.x_scn.x_checksum,
2154 auxp->u.auxent.x_scn.x_associated,
2155 auxp->u.auxent.x_scn.x_comdat);
2156 break;
2158 /* Fall through. */
2159 case C_EXT:
2160 case C_AIX_WEAKEXT:
2161 if (ISFCN (combined->u.syment.n_type))
2163 long next, llnos;
2165 if (auxp->fix_end)
2166 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2167 - root);
2168 else
2169 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2170 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2171 fprintf (file,
2172 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2173 tagndx,
2174 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2175 llnos, next);
2176 break;
2178 /* Fall through. */
2179 default:
2180 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2181 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2182 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2183 tagndx);
2184 if (auxp->fix_end)
2185 fprintf (file, " endndx %ld",
2186 ((long)
2187 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2188 - root)));
2189 break;
2193 if (l)
2195 fprintf (file, "\n%s :", l->u.sym->name);
2196 l++;
2197 while (l->line_number)
2199 if (l->line_number > 0)
2201 fprintf (file, "\n%4d : ", l->line_number);
2202 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2204 l++;
2208 else
2210 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2211 fprintf (file, " %-5s %s %s %s",
2212 symbol->section->name,
2213 coffsymbol (symbol)->native ? "n" : "g",
2214 coffsymbol (symbol)->lineno ? "l" : " ",
2215 symbol->name);
2220 /* Return whether a symbol name implies a local symbol. In COFF,
2221 local symbols generally start with ``.L''. Most targets use this
2222 function for the is_local_label_name entry point, but some may
2223 override it. */
2225 bool
2226 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2227 const char *name)
2229 return name[0] == '.' && name[1] == 'L';
2232 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2233 section, calculate and return the name of the source file and the line
2234 nearest to the wanted location. */
2236 bool
2237 coff_find_nearest_line_with_names (bfd *abfd,
2238 asymbol **symbols,
2239 asection *section,
2240 bfd_vma offset,
2241 const char **filename_ptr,
2242 const char **functionname_ptr,
2243 unsigned int *line_ptr,
2244 const struct dwarf_debug_section *debug_sections)
2246 bool found;
2247 unsigned int i;
2248 unsigned int line_base;
2249 coff_data_type *cof = coff_data (abfd);
2250 /* Run through the raw syments if available. */
2251 combined_entry_type *p;
2252 combined_entry_type *pend;
2253 alent *l;
2254 struct coff_section_tdata *sec_data;
2255 size_t amt;
2257 /* Before looking through the symbol table, try to use a .stab
2258 section to find the information. */
2259 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2260 &found, filename_ptr,
2261 functionname_ptr, line_ptr,
2262 &coff_data(abfd)->line_info))
2263 return false;
2265 if (found)
2266 return true;
2268 /* Also try examining DWARF2 debugging information. */
2269 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2270 filename_ptr, functionname_ptr,
2271 line_ptr, NULL, debug_sections,
2272 &coff_data(abfd)->dwarf2_find_line_info))
2273 return true;
2275 sec_data = coff_section_data (abfd, section);
2277 /* If the DWARF lookup failed, but there is DWARF information available
2278 then the problem might be that the file has been rebased. This tool
2279 changes the VMAs of all the sections, but it does not update the DWARF
2280 information. So try again, using a bias against the address sought. */
2281 if (coff_data (abfd)->dwarf2_find_line_info != NULL)
2283 bfd_signed_vma bias = 0;
2285 /* Create a cache of the result for the next call. */
2286 if (sec_data == NULL && section->owner == abfd)
2288 amt = sizeof (struct coff_section_tdata);
2289 section->used_by_bfd = bfd_zalloc (abfd, amt);
2290 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2293 if (sec_data != NULL && sec_data->saved_bias)
2294 bias = sec_data->bias;
2295 else if (symbols)
2297 bias = _bfd_dwarf2_find_symbol_bias (symbols,
2298 & coff_data (abfd)->dwarf2_find_line_info);
2300 if (sec_data)
2302 sec_data->saved_bias = true;
2303 sec_data->bias = bias;
2307 if (bias
2308 && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
2309 offset + bias,
2310 filename_ptr, functionname_ptr,
2311 line_ptr, NULL, debug_sections,
2312 &coff_data(abfd)->dwarf2_find_line_info))
2313 return true;
2316 *filename_ptr = 0;
2317 *functionname_ptr = 0;
2318 *line_ptr = 0;
2320 /* Don't try and find line numbers in a non coff file. */
2321 if (!bfd_family_coff (abfd))
2322 return false;
2324 if (cof == NULL)
2325 return false;
2327 /* Find the first C_FILE symbol. */
2328 p = cof->raw_syments;
2329 if (!p)
2330 return false;
2332 pend = p + cof->raw_syment_count;
2333 while (p < pend)
2335 BFD_ASSERT (p->is_sym);
2336 if (p->u.syment.n_sclass == C_FILE)
2337 break;
2338 p += 1 + p->u.syment.n_numaux;
2341 if (p < pend)
2343 bfd_vma sec_vma;
2344 bfd_vma maxdiff;
2346 /* Look through the C_FILE symbols to find the best one. */
2347 sec_vma = bfd_section_vma (section);
2348 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2349 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2350 while (1)
2352 bfd_vma file_addr;
2353 combined_entry_type *p2;
2355 for (p2 = p + 1 + p->u.syment.n_numaux;
2356 p2 < pend;
2357 p2 += 1 + p2->u.syment.n_numaux)
2359 BFD_ASSERT (p2->is_sym);
2360 if (p2->u.syment.n_scnum > 0
2361 && (section
2362 == coff_section_from_bfd_index (abfd,
2363 p2->u.syment.n_scnum)))
2364 break;
2365 if (p2->u.syment.n_sclass == C_FILE)
2367 p2 = pend;
2368 break;
2371 if (p2 >= pend)
2372 break;
2374 file_addr = (bfd_vma) p2->u.syment.n_value;
2375 /* PR 11512: Include the section address of the function name symbol. */
2376 if (p2->u.syment.n_scnum > 0)
2377 file_addr += coff_section_from_bfd_index (abfd,
2378 p2->u.syment.n_scnum)->vma;
2379 /* We use <= MAXDIFF here so that if we get a zero length
2380 file, we actually use the next file entry. */
2381 if (p2 < pend
2382 && offset + sec_vma >= file_addr
2383 && offset + sec_vma - file_addr <= maxdiff)
2385 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2386 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2389 if (p->u.syment.n_value >= cof->raw_syment_count)
2390 break;
2392 /* Avoid endless loops on erroneous files by ensuring that
2393 we always move forward in the file. */
2394 if (p >= cof->raw_syments + p->u.syment.n_value)
2395 break;
2397 p = cof->raw_syments + p->u.syment.n_value;
2398 if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
2399 break;
2403 if (section->lineno_count == 0)
2405 *functionname_ptr = NULL;
2406 *line_ptr = 0;
2407 return true;
2410 /* Now wander though the raw linenumbers of the section.
2411 If we have been called on this section before, and the offset
2412 we want is further down then we can prime the lookup loop. */
2413 if (sec_data != NULL
2414 && sec_data->i > 0
2415 && offset >= sec_data->offset)
2417 i = sec_data->i;
2418 *functionname_ptr = sec_data->function;
2419 line_base = sec_data->line_base;
2421 else
2423 i = 0;
2424 line_base = 0;
2427 if (section->lineno != NULL)
2429 bfd_vma last_value = 0;
2431 l = &section->lineno[i];
2433 for (; i < section->lineno_count; i++)
2435 if (l->line_number == 0)
2437 /* Get the symbol this line number points at. */
2438 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2439 if (coff->symbol.value > offset)
2440 break;
2442 *functionname_ptr = coff->symbol.name;
2443 last_value = coff->symbol.value;
2444 if (coff->native)
2446 combined_entry_type *s = coff->native;
2448 BFD_ASSERT (s->is_sym);
2449 s = s + 1 + s->u.syment.n_numaux;
2451 /* In XCOFF a debugging symbol can follow the
2452 function symbol. */
2453 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2454 < obj_raw_syment_count (abfd) * sizeof (*s))
2455 && s->u.syment.n_scnum == N_DEBUG)
2456 s = s + 1 + s->u.syment.n_numaux;
2458 /* S should now point to the .bf of the function. */
2459 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2460 < obj_raw_syment_count (abfd) * sizeof (*s))
2461 && s->u.syment.n_numaux)
2463 /* The linenumber is stored in the auxent. */
2464 union internal_auxent *a = &((s + 1)->u.auxent);
2466 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2467 *line_ptr = line_base;
2471 else
2473 if (l->u.offset > offset)
2474 break;
2475 *line_ptr = l->line_number + line_base - 1;
2477 l++;
2480 /* If we fell off the end of the loop, then assume that this
2481 symbol has no line number info. Otherwise, symbols with no
2482 line number info get reported with the line number of the
2483 last line of the last symbol which does have line number
2484 info. We use 0x100 as a slop to account for cases where the
2485 last line has executable code. */
2486 if (i >= section->lineno_count
2487 && last_value != 0
2488 && offset - last_value > 0x100)
2490 *functionname_ptr = NULL;
2491 *line_ptr = 0;
2495 /* Cache the results for the next call. */
2496 if (sec_data == NULL && section->owner == abfd)
2498 amt = sizeof (struct coff_section_tdata);
2499 section->used_by_bfd = bfd_zalloc (abfd, amt);
2500 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2503 if (sec_data != NULL)
2505 sec_data->offset = offset;
2506 sec_data->i = i - 1;
2507 sec_data->function = *functionname_ptr;
2508 sec_data->line_base = line_base;
2511 return true;
2514 bool
2515 coff_find_nearest_line (bfd *abfd,
2516 asymbol **symbols,
2517 asection *section,
2518 bfd_vma offset,
2519 const char **filename_ptr,
2520 const char **functionname_ptr,
2521 unsigned int *line_ptr,
2522 unsigned int *discriminator_ptr)
2524 if (discriminator_ptr)
2525 *discriminator_ptr = 0;
2526 return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2527 filename_ptr, functionname_ptr,
2528 line_ptr, dwarf_debug_sections);
2531 bool
2532 coff_find_inliner_info (bfd *abfd,
2533 const char **filename_ptr,
2534 const char **functionname_ptr,
2535 unsigned int *line_ptr)
2537 bool found;
2539 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2540 functionname_ptr, line_ptr,
2541 &coff_data(abfd)->dwarf2_find_line_info);
2542 return (found);
2546 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2548 size_t size;
2550 if (!bfd_link_relocatable (info))
2551 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2552 else
2553 size = bfd_coff_filhsz (abfd);
2555 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2556 return size;
2559 /* Change the class of a coff symbol held by BFD. */
2561 bool
2562 bfd_coff_set_symbol_class (bfd * abfd,
2563 asymbol * symbol,
2564 unsigned int symbol_class)
2566 coff_symbol_type * csym;
2568 csym = coff_symbol_from (symbol);
2569 if (csym == NULL)
2571 bfd_set_error (bfd_error_invalid_operation);
2572 return false;
2574 else if (csym->native == NULL)
2576 /* This is an alien symbol which no native coff backend data.
2577 We cheat here by creating a fake native entry for it and
2578 then filling in the class. This code is based on that in
2579 coff_write_alien_symbol(). */
2581 combined_entry_type * native;
2582 size_t amt = sizeof (* native);
2584 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2585 if (native == NULL)
2586 return false;
2588 native->is_sym = true;
2589 native->u.syment.n_type = T_NULL;
2590 native->u.syment.n_sclass = symbol_class;
2592 if (bfd_is_und_section (symbol->section))
2594 native->u.syment.n_scnum = N_UNDEF;
2595 native->u.syment.n_value = symbol->value;
2597 else if (bfd_is_com_section (symbol->section))
2599 native->u.syment.n_scnum = N_UNDEF;
2600 native->u.syment.n_value = symbol->value;
2602 else
2604 native->u.syment.n_scnum =
2605 symbol->section->output_section->target_index;
2606 native->u.syment.n_value = (symbol->value
2607 + symbol->section->output_offset);
2608 if (! obj_pe (abfd))
2609 native->u.syment.n_value += symbol->section->output_section->vma;
2611 /* Copy the any flags from the file header into the symbol.
2612 FIXME: Why? */
2613 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2616 csym->native = native;
2618 else
2619 csym->native->u.syment.n_sclass = symbol_class;
2621 return true;
2624 bool
2625 _bfd_coff_section_already_linked (bfd *abfd,
2626 asection *sec,
2627 struct bfd_link_info *info)
2629 flagword flags;
2630 const char *name, *key;
2631 struct bfd_section_already_linked *l;
2632 struct bfd_section_already_linked_hash_entry *already_linked_list;
2633 struct coff_comdat_info *s_comdat;
2635 if (sec->output_section == bfd_abs_section_ptr)
2636 return false;
2638 flags = sec->flags;
2639 if ((flags & SEC_LINK_ONCE) == 0)
2640 return false;
2642 /* The COFF backend linker doesn't support group sections. */
2643 if ((flags & SEC_GROUP) != 0)
2644 return false;
2646 name = bfd_section_name (sec);
2647 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2649 if (s_comdat != NULL)
2650 key = s_comdat->name;
2651 else
2653 if (startswith (name, ".gnu.linkonce.")
2654 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2655 key++;
2656 else
2657 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2658 .xdata$<key> and .pdata$<key> only the first of which has a
2659 comdat key. Should these all match the LTO IR key? */
2660 key = name;
2663 already_linked_list = bfd_section_already_linked_table_lookup (key);
2665 for (l = already_linked_list->entry; l != NULL; l = l->next)
2667 struct coff_comdat_info *l_comdat;
2669 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2671 /* The section names must match, and both sections must be
2672 comdat and have the same comdat name, or both sections must
2673 be non-comdat. LTO IR plugin sections are an exception. They
2674 are always named .gnu.linkonce.t.<key> (<key> is some string)
2675 and match any comdat section with comdat name of <key>, and
2676 any linkonce section with the same suffix, ie.
2677 .gnu.linkonce.*.<key>. */
2678 if (((s_comdat != NULL) == (l_comdat != NULL)
2679 && strcmp (name, l->sec->name) == 0)
2680 || (l->sec->owner->flags & BFD_PLUGIN) != 0
2681 || (sec->owner->flags & BFD_PLUGIN) != 0)
2683 /* The section has already been linked. See if we should
2684 issue a warning. */
2685 return _bfd_handle_already_linked (sec, l, info);
2689 /* This is the first section with this name. Record it. */
2690 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2691 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2692 return false;
2695 /* Initialize COOKIE for input bfd ABFD. */
2697 static bool
2698 init_reloc_cookie (struct coff_reloc_cookie *cookie,
2699 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2700 bfd *abfd)
2702 /* Sometimes the symbol table does not yet have been loaded here. */
2703 bfd_coff_slurp_symbol_table (abfd);
2705 cookie->abfd = abfd;
2706 cookie->sym_hashes = obj_coff_sym_hashes (abfd);
2708 cookie->symbols = obj_symbols (abfd);
2710 return true;
2713 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
2715 static void
2716 fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED,
2717 bfd *abfd ATTRIBUTE_UNUSED)
2719 /* Nothing to do. */
2722 /* Initialize the relocation information in COOKIE for input section SEC
2723 of input bfd ABFD. */
2725 static bool
2726 init_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2727 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2728 bfd *abfd,
2729 asection *sec)
2731 if (sec->reloc_count == 0)
2733 cookie->rels = NULL;
2734 cookie->relend = NULL;
2735 cookie->rel = NULL;
2736 return true;
2739 cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL,
2740 0, NULL);
2742 if (cookie->rels == NULL)
2743 return false;
2745 cookie->rel = cookie->rels;
2746 cookie->relend = (cookie->rels + sec->reloc_count);
2747 return true;
2750 /* Free the memory allocated by init_reloc_cookie_rels,
2751 if appropriate. */
2753 static void
2754 fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2755 asection *sec)
2757 if (cookie->rels
2758 /* PR 20401. The relocs may not have been cached, so check first.
2759 If the relocs were loaded by init_reloc_cookie_rels() then this
2760 will be the case. FIXME: Would performance be improved if the
2761 relocs *were* cached ? */
2762 && coff_section_data (NULL, sec)
2763 && coff_section_data (NULL, sec)->relocs != cookie->rels)
2764 free (cookie->rels);
2767 /* Initialize the whole of COOKIE for input section SEC. */
2769 static bool
2770 init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2771 struct bfd_link_info *info,
2772 asection *sec)
2774 if (!init_reloc_cookie (cookie, info, sec->owner))
2775 return false;
2777 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
2779 fini_reloc_cookie (cookie, sec->owner);
2780 return false;
2782 return true;
2785 /* Free the memory allocated by init_reloc_cookie_for_section,
2786 if appropriate. */
2788 static void
2789 fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2790 asection *sec)
2792 fini_reloc_cookie_rels (cookie, sec);
2793 fini_reloc_cookie (cookie, sec->owner);
2796 static asection *
2797 _bfd_coff_gc_mark_hook (asection *sec,
2798 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2799 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2800 struct coff_link_hash_entry *h,
2801 struct internal_syment *sym)
2803 if (h != NULL)
2805 switch (h->root.type)
2807 case bfd_link_hash_defined:
2808 case bfd_link_hash_defweak:
2809 return h->root.u.def.section;
2811 case bfd_link_hash_common:
2812 return h->root.u.c.p->section;
2814 case bfd_link_hash_undefweak:
2815 if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
2817 /* PE weak externals. A weak symbol may include an auxiliary
2818 record indicating that if the weak symbol is not resolved,
2819 another external symbol is used instead. */
2820 struct coff_link_hash_entry *h2 =
2821 h->auxbfd->tdata.coff_obj_data->sym_hashes[
2822 h->aux->x_sym.x_tagndx.l];
2824 if (h2 && h2->root.type != bfd_link_hash_undefined)
2825 return h2->root.u.def.section;
2827 break;
2829 case bfd_link_hash_undefined:
2830 default:
2831 break;
2833 return NULL;
2836 return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
2839 /* COOKIE->rel describes a relocation against section SEC, which is
2840 a section we've decided to keep. Return the section that contains
2841 the relocation symbol, or NULL if no section contains it. */
2843 static asection *
2844 _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
2845 coff_gc_mark_hook_fn gc_mark_hook,
2846 struct coff_reloc_cookie *cookie)
2848 struct coff_link_hash_entry *h;
2850 h = cookie->sym_hashes[cookie->rel->r_symndx];
2851 if (h != NULL)
2853 while (h->root.type == bfd_link_hash_indirect
2854 || h->root.type == bfd_link_hash_warning)
2855 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2857 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
2860 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
2861 &(cookie->symbols
2862 + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment);
2865 static bool _bfd_coff_gc_mark
2866 (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
2868 /* COOKIE->rel describes a relocation against section SEC, which is
2869 a section we've decided to keep. Mark the section that contains
2870 the relocation symbol. */
2872 static bool
2873 _bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
2874 asection *sec,
2875 coff_gc_mark_hook_fn gc_mark_hook,
2876 struct coff_reloc_cookie *cookie)
2878 asection *rsec;
2880 rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
2881 if (rsec && !rsec->gc_mark)
2883 if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
2884 rsec->gc_mark = 1;
2885 else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
2886 return false;
2888 return true;
2891 /* The mark phase of garbage collection. For a given section, mark
2892 it and any sections in this section's group, and all the sections
2893 which define symbols to which it refers. */
2895 static bool
2896 _bfd_coff_gc_mark (struct bfd_link_info *info,
2897 asection *sec,
2898 coff_gc_mark_hook_fn gc_mark_hook)
2900 bool ret = true;
2902 sec->gc_mark = 1;
2904 /* Look through the section relocs. */
2905 if ((sec->flags & SEC_RELOC) != 0
2906 && sec->reloc_count > 0)
2908 struct coff_reloc_cookie cookie;
2910 if (!init_reloc_cookie_for_section (&cookie, info, sec))
2911 ret = false;
2912 else
2914 for (; cookie.rel < cookie.relend; cookie.rel++)
2916 if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
2918 ret = false;
2919 break;
2922 fini_reloc_cookie_for_section (&cookie, sec);
2926 return ret;
2929 static bool
2930 _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
2931 coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
2933 bfd *ibfd;
2935 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2937 asection *isec;
2938 bool some_kept;
2940 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour)
2941 continue;
2943 /* Ensure all linker created sections are kept, and see whether
2944 any other section is already marked. */
2945 some_kept = false;
2946 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
2948 if ((isec->flags & SEC_LINKER_CREATED) != 0)
2949 isec->gc_mark = 1;
2950 else if (isec->gc_mark)
2951 some_kept = true;
2954 /* If no section in this file will be kept, then we can
2955 toss out debug sections. */
2956 if (!some_kept)
2957 continue;
2959 /* Keep debug and special sections like .comment when they are
2960 not part of a group, or when we have single-member groups. */
2961 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
2962 if ((isec->flags & SEC_DEBUGGING) != 0
2963 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
2964 isec->gc_mark = 1;
2966 return true;
2969 /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */
2971 static bool
2972 coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
2973 void *data ATTRIBUTE_UNUSED)
2975 if (h->root.type == bfd_link_hash_warning)
2976 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2978 if ((h->root.type == bfd_link_hash_defined
2979 || h->root.type == bfd_link_hash_defweak)
2980 && !h->root.u.def.section->gc_mark
2981 && !(h->root.u.def.section->owner->flags & DYNAMIC))
2983 /* Do our best to hide the symbol. */
2984 h->root.u.def.section = bfd_und_section_ptr;
2985 h->symbol_class = C_HIDDEN;
2988 return true;
2991 /* The sweep phase of garbage collection. Remove all garbage sections. */
2993 typedef bool (*gc_sweep_hook_fn)
2994 (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
2996 static bool
2997 coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
2999 bfd *sub;
3001 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3003 asection *o;
3005 if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3006 continue;
3008 for (o = sub->sections; o != NULL; o = o->next)
3010 /* Keep debug and special sections. */
3011 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
3012 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3013 o->gc_mark = 1;
3014 else if (startswith (o->name, ".idata")
3015 || startswith (o->name, ".pdata")
3016 || startswith (o->name, ".xdata")
3017 || startswith (o->name, ".rsrc"))
3018 o->gc_mark = 1;
3020 if (o->gc_mark)
3021 continue;
3023 /* Skip sweeping sections already excluded. */
3024 if (o->flags & SEC_EXCLUDE)
3025 continue;
3027 /* Since this is early in the link process, it is simple
3028 to remove a section from the output. */
3029 o->flags |= SEC_EXCLUDE;
3031 if (info->print_gc_sections && o->size != 0)
3032 /* xgettext: c-format */
3033 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3034 o, sub);
3036 #if 0
3037 /* But we also have to update some of the relocation
3038 info we collected before. */
3039 if (gc_sweep_hook
3040 && (o->flags & SEC_RELOC) != 0
3041 && o->reloc_count > 0
3042 && !bfd_is_abs_section (o->output_section))
3044 struct internal_reloc *internal_relocs;
3045 bool r;
3047 internal_relocs
3048 = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
3049 info->keep_memory);
3050 if (internal_relocs == NULL)
3051 return false;
3053 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
3055 if (coff_section_data (o)->relocs != internal_relocs)
3056 free (internal_relocs);
3058 if (!r)
3059 return false;
3061 #endif
3065 /* Remove the symbols that were in the swept sections from the dynamic
3066 symbol table. */
3067 coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
3068 NULL);
3070 return true;
3073 /* Keep all sections containing symbols undefined on the command-line,
3074 and the section containing the entry symbol. */
3076 static void
3077 _bfd_coff_gc_keep (struct bfd_link_info *info)
3079 struct bfd_sym_chain *sym;
3081 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
3083 struct coff_link_hash_entry *h;
3085 h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
3086 false, false, false);
3088 if (h != NULL
3089 && (h->root.type == bfd_link_hash_defined
3090 || h->root.type == bfd_link_hash_defweak)
3091 && !bfd_is_abs_section (h->root.u.def.section))
3092 h->root.u.def.section->flags |= SEC_KEEP;
3096 /* Do mark and sweep of unused sections. */
3098 bool
3099 bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3101 bfd *sub;
3103 /* FIXME: Should we implement this? */
3104 #if 0
3105 const bfd_coff_backend_data *bed = coff_backend_info (abfd);
3107 if (!bed->can_gc_sections
3108 || !is_coff_hash_table (info->hash))
3110 _bfd_error_handler(_("warning: gc-sections option ignored"));
3111 return true;
3113 #endif
3115 _bfd_coff_gc_keep (info);
3117 /* Grovel through relocs to find out who stays ... */
3118 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3120 asection *o;
3122 if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3123 continue;
3125 for (o = sub->sections; o != NULL; o = o->next)
3127 if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
3128 || startswith (o->name, ".vectors")
3129 || startswith (o->name, ".ctors")
3130 || startswith (o->name, ".dtors"))
3131 && !o->gc_mark)
3133 if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
3134 return false;
3139 /* Allow the backend to mark additional target specific sections. */
3140 _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
3142 /* ... and mark SEC_EXCLUDE for those that go. */
3143 return coff_gc_sweep (abfd, info);
3146 /* Return name used to identify a comdat group. */
3148 const char *
3149 bfd_coff_group_name (bfd *abfd, const asection *sec)
3151 struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec);
3152 if (ci != NULL)
3153 return ci->name;
3154 return NULL;
3157 bool
3158 _bfd_coff_close_and_cleanup (bfd *abfd)
3160 struct coff_tdata *tdata = coff_data (abfd);
3162 if (tdata != NULL)
3164 /* PR 25447:
3165 Do not clear the keep_syms and keep_strings flags.
3166 These may have been set by pe_ILF_build_a_bfd() indicating
3167 that the syms and strings pointers are not to be freed. */
3168 if (bfd_get_format (abfd) == bfd_object
3169 && bfd_family_coff (abfd)
3170 && !_bfd_coff_free_symbols (abfd))
3171 return false;
3173 if (bfd_get_format (abfd) == bfd_object
3174 || bfd_get_format (abfd) == bfd_core)
3175 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
3177 return _bfd_generic_close_and_cleanup (abfd);