Add translations for various sub-directories
[binutils-gdb.git] / bfd / coffgen.c
blobc734f058892c0b065f71404b1c963aeacb568f38
1 /* Support for the generic parts of COFF, for BFD.
2 Copyright (C) 1990-2025 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"
45 #include "hashtab.h"
47 /* Extract a long section name at STRINDEX and copy it to the bfd objstack.
48 Return NULL in case of error. */
50 static char *
51 extract_long_section_name(bfd *abfd, unsigned long strindex)
53 const char *strings;
54 char *name;
56 strings = _bfd_coff_read_string_table (abfd);
57 if (strings == NULL)
58 return NULL;
59 if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
60 return NULL;
61 strings += strindex;
62 name = (char *) bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
63 if (name == NULL)
64 return NULL;
65 strcpy (name, strings);
67 return name;
70 /* Decode a base 64 coded string at STR of length LEN, and write the result
71 to RES. Return true on success.
72 Return false in case of invalid character or overflow. */
74 static bool
75 decode_base64 (const char *str, unsigned len, uint32_t *res)
77 unsigned i;
78 uint32_t val;
80 val = 0;
81 for (i = 0; i < len; i++)
83 char c = str[i];
84 unsigned d;
86 if (c >= 'A' && c <= 'Z')
87 d = c - 'A';
88 else if (c >= 'a' && c <= 'z')
89 d = c - 'a' + 26;
90 else if (c >= '0' && c <= '9')
91 d = c - '0' + 52;
92 else if (c == '+')
93 d = 62;
94 else if (c == '/')
95 d = 63;
96 else
97 return false;
99 /* Check for overflow. */
100 if ((val >> 26) != 0)
101 return false;
103 val = (val << 6) + d;
106 *res = val;
107 return true;
110 /* Take a section header read from a coff file (in HOST byte order),
111 and make a BFD "section" out of it. This is used by ECOFF. */
113 static bool
114 make_a_section_from_file (bfd *abfd,
115 struct internal_scnhdr *hdr,
116 unsigned int target_index)
118 asection *newsect;
119 char *name;
120 bool result = true;
121 flagword flags;
123 name = NULL;
125 /* Handle long section names as in PE. On reading, we want to
126 accept long names if the format permits them at all, regardless
127 of the current state of the flag that dictates if we would generate
128 them in outputs; this construct checks if that is the case by
129 attempting to set the flag, without changing its state; the call
130 will fail for formats that do not support long names at all. */
131 if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
132 && hdr->s_name[0] == '/')
134 /* Flag that this BFD uses long names, even though the format might
135 expect them to be off by default. This won't directly affect the
136 format of any output BFD created from this one, but the information
137 can be used to decide what to do. */
138 bfd_coff_set_long_section_names (abfd, true);
140 if (hdr->s_name[1] == '/')
142 /* LLVM extension: the '/' is followed by another '/' and then by
143 the index in the strtab encoded in base64 without NUL at the
144 end. */
145 uint32_t strindex;
147 /* Decode the index. No overflow is expected as the string table
148 length is at most 2^32 - 1 (the length is written on the first
149 four bytes).
150 Also, contrary to RFC 4648, all the characters must be decoded,
151 there is no padding. */
152 if (!decode_base64 (hdr->s_name + 2, SCNNMLEN - 2, &strindex))
153 return false;
155 name = extract_long_section_name (abfd, strindex);
156 if (name == NULL)
157 return false;
159 else
161 /* PE classic long section name. The '/' is followed by the index
162 in the strtab. The index is formatted as a decimal string. */
163 char buf[SCNNMLEN];
164 long strindex;
165 char *p;
167 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
168 buf[SCNNMLEN - 1] = '\0';
169 strindex = strtol (buf, &p, 10);
170 if (*p == '\0' && strindex >= 0)
172 name = extract_long_section_name (abfd, strindex);
173 if (name == NULL)
174 return false;
179 if (name == NULL)
181 /* Assorted wastage to null-terminate the name, thanks AT&T! */
182 name = (char *) bfd_alloc (abfd,
183 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
184 if (name == NULL)
185 return false;
186 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
187 name[sizeof (hdr->s_name)] = 0;
190 newsect = bfd_make_section_anyway (abfd, name);
191 if (newsect == NULL)
192 return false;
194 newsect->vma = hdr->s_vaddr;
195 newsect->lma = hdr->s_paddr;
196 newsect->size = hdr->s_size;
197 newsect->filepos = hdr->s_scnptr;
198 newsect->rel_filepos = hdr->s_relptr;
199 newsect->reloc_count = hdr->s_nreloc;
201 bfd_coff_set_alignment_hook (abfd, newsect, hdr);
203 newsect->line_filepos = hdr->s_lnnoptr;
205 newsect->lineno_count = hdr->s_nlnno;
206 newsect->userdata = NULL;
207 newsect->next = NULL;
208 newsect->target_index = target_index;
210 if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags))
211 result = false;
213 /* At least on i386-coff, the line number count for a shared library
214 section must be ignored. */
215 if ((flags & SEC_COFF_SHARED_LIBRARY) != 0)
216 newsect->lineno_count = 0;
218 if (hdr->s_nreloc != 0)
219 flags |= SEC_RELOC;
220 /* FIXME: should this check 'hdr->s_size > 0'. */
221 if (hdr->s_scnptr != 0)
222 flags |= SEC_HAS_CONTENTS;
224 newsect->flags = flags;
226 /* Compress/decompress DWARF debug sections. */
227 if ((flags & SEC_DEBUGGING) != 0
228 && (flags & SEC_HAS_CONTENTS) != 0
229 && (startswith (name, ".debug_")
230 || startswith (name, ".zdebug_")
231 || startswith (name, ".gnu.debuglto_.debug_")
232 || startswith (name, ".gnu.linkonce.wi.")))
234 enum { nothing, compress, decompress } action = nothing;
236 if (bfd_is_section_compressed (abfd, newsect))
238 /* Compressed section. Check if we should decompress. */
239 if ((abfd->flags & BFD_DECOMPRESS))
240 action = decompress;
242 else
244 /* Normal section. Check if we should compress. */
245 if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
246 action = compress;
249 if (action == compress)
251 if (!bfd_init_section_compress_status (abfd, newsect))
253 _bfd_error_handler
254 /* xgettext:c-format */
255 (_("%pB: unable to compress section %s"), abfd, name);
256 return false;
259 else if (action == decompress)
261 if (!bfd_init_section_decompress_status (abfd, newsect))
263 _bfd_error_handler
264 /* xgettext:c-format */
265 (_("%pB: unable to decompress section %s"), abfd, name);
266 return false;
268 if (abfd->is_linker_input
269 && name[1] == 'z')
271 /* Rename section from .zdebug_* to .debug_* so that ld
272 scripts will see this section as a debug section. */
273 char *new_name = bfd_zdebug_name_to_debug (abfd, name);
274 if (new_name == NULL)
275 return false;
276 bfd_rename_section (newsect, new_name);
281 return result;
284 void
285 coff_object_cleanup (bfd *abfd)
287 struct coff_tdata *td = coff_data (abfd);
288 if (td != NULL)
290 if (td->section_by_index)
291 htab_delete (td->section_by_index);
292 if (td->section_by_target_index)
293 htab_delete (td->section_by_target_index);
294 if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
295 htab_delete (pe_data (abfd)->comdat_hash);
299 /* Read in a COFF object and make it into a BFD. This is used by
300 ECOFF as well. */
301 bfd_cleanup
302 coff_real_object_p (bfd *abfd,
303 unsigned nscns,
304 struct internal_filehdr *internal_f,
305 struct internal_aouthdr *internal_a)
307 flagword oflags = abfd->flags;
308 bfd_vma ostart = bfd_get_start_address (abfd);
309 void * tdata;
310 bfd_size_type readsize; /* Length of file_info. */
311 unsigned int scnhsz;
312 char *external_sections;
314 if (!(internal_f->f_flags & F_RELFLG))
315 abfd->flags |= HAS_RELOC;
316 if ((internal_f->f_flags & F_EXEC))
317 abfd->flags |= EXEC_P;
318 if (!(internal_f->f_flags & F_LNNO))
319 abfd->flags |= HAS_LINENO;
320 if (!(internal_f->f_flags & F_LSYMS))
321 abfd->flags |= HAS_LOCALS;
323 /* FIXME: How can we set D_PAGED correctly? */
324 if ((internal_f->f_flags & F_EXEC) != 0)
325 abfd->flags |= D_PAGED;
327 abfd->symcount = internal_f->f_nsyms;
328 if (internal_f->f_nsyms)
329 abfd->flags |= HAS_SYMS;
331 if (internal_a != (struct internal_aouthdr *) NULL)
332 abfd->start_address = internal_a->entry;
333 else
334 abfd->start_address = 0;
336 /* Set up the tdata area. ECOFF uses its own routine, and overrides
337 abfd->flags. */
338 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
339 if (tdata == NULL)
340 goto fail2;
342 scnhsz = bfd_coff_scnhsz (abfd);
343 readsize = (bfd_size_type) nscns * scnhsz;
344 external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
345 if (!external_sections)
346 goto fail;
348 /* Set the arch/mach *before* swapping in sections; section header swapping
349 may depend on arch/mach info. */
350 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
351 goto fail;
353 /* Now copy data as required; construct all asections etc. */
354 if (nscns != 0)
356 unsigned int i;
357 for (i = 0; i < nscns; i++)
359 struct internal_scnhdr tmp;
360 bfd_coff_swap_scnhdr_in (abfd,
361 (void *) (external_sections + i * scnhsz),
362 (void *) & tmp);
363 if (! make_a_section_from_file (abfd, &tmp, i + 1))
364 goto fail;
368 _bfd_coff_free_symbols (abfd);
369 return coff_object_cleanup;
371 fail:
372 coff_object_cleanup (abfd);
373 _bfd_coff_free_symbols (abfd);
374 bfd_release (abfd, tdata);
375 fail2:
376 abfd->flags = oflags;
377 abfd->start_address = ostart;
378 return NULL;
381 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
382 not a COFF file. This is also used by ECOFF. */
384 bfd_cleanup
385 coff_object_p (bfd *abfd)
387 bfd_size_type filhsz;
388 bfd_size_type aoutsz;
389 unsigned int nscns;
390 void * filehdr;
391 struct internal_filehdr internal_f;
392 struct internal_aouthdr internal_a;
394 /* Figure out how much to read. */
395 filhsz = bfd_coff_filhsz (abfd);
396 aoutsz = bfd_coff_aoutsz (abfd);
398 filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
399 if (filehdr == NULL)
401 if (bfd_get_error () != bfd_error_system_call)
402 bfd_set_error (bfd_error_wrong_format);
403 return NULL;
405 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
406 bfd_release (abfd, filehdr);
408 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
409 (less than aoutsz) used in object files and AOUTSZ (equal to
410 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
411 expects this header to be aoutsz bytes in length, so we use that
412 value in the call to bfd_alloc below. But we must be careful to
413 only read in f_opthdr bytes in the call to bfd_read. We should
414 also attempt to catch corrupt or non-COFF binaries with a strange
415 value for f_opthdr. */
416 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
417 || internal_f.f_opthdr > aoutsz)
419 bfd_set_error (bfd_error_wrong_format);
420 return NULL;
422 nscns = internal_f.f_nscns;
424 if (internal_f.f_opthdr)
426 void * opthdr;
428 opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
429 if (opthdr == NULL)
430 return NULL;
431 /* PR 17512: file: 11056-1136-0.004. */
432 if (internal_f.f_opthdr < aoutsz)
433 memset (((char *) opthdr) + internal_f.f_opthdr, 0,
434 aoutsz - internal_f.f_opthdr);
436 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
437 bfd_release (abfd, opthdr);
440 return coff_real_object_p (abfd, nscns, &internal_f,
441 (internal_f.f_opthdr != 0
442 ? &internal_a
443 : (struct internal_aouthdr *) NULL));
446 static hashval_t
447 htab_hash_section_target_index (const void * entry)
449 const struct bfd_section * sec = entry;
450 return sec->target_index;
453 static int
454 htab_eq_section_target_index (const void * e1, const void * e2)
456 const struct bfd_section * sec1 = e1;
457 const struct bfd_section * sec2 = e2;
458 return sec1->target_index == sec2->target_index;
461 /* Get the BFD section from a COFF symbol section number. */
463 asection *
464 coff_section_from_bfd_index (bfd *abfd, int section_index)
466 if (section_index == N_ABS)
467 return bfd_abs_section_ptr;
468 if (section_index == N_UNDEF)
469 return bfd_und_section_ptr;
470 if (section_index == N_DEBUG)
471 return bfd_abs_section_ptr;
473 struct bfd_section *answer;
474 htab_t table = coff_data (abfd)->section_by_target_index;
476 if (!table)
478 table = htab_create (10, htab_hash_section_target_index,
479 htab_eq_section_target_index, NULL);
480 if (table == NULL)
481 return bfd_und_section_ptr;
482 coff_data (abfd)->section_by_target_index = table;
485 if (htab_elements (table) == 0)
487 for (answer = abfd->sections; answer; answer = answer->next)
489 void **slot = htab_find_slot (table, answer, INSERT);
490 if (slot == NULL)
491 return bfd_und_section_ptr;
492 *slot = answer;
496 struct bfd_section needle;
497 needle.target_index = section_index;
499 answer = htab_find (table, &needle);
500 if (answer != NULL)
501 return answer;
503 /* Cover the unlikely case of sections added after the first call to
504 this function. */
505 for (answer = abfd->sections; answer; answer = answer->next)
506 if (answer->target_index == section_index)
508 void **slot = htab_find_slot (table, answer, INSERT);
509 if (slot != NULL)
510 *slot = answer;
511 return answer;
514 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
515 has a bad symbol table in biglitpow.o. */
516 return bfd_und_section_ptr;
519 /* Get the upper bound of a COFF symbol table. */
521 long
522 coff_get_symtab_upper_bound (bfd *abfd)
524 if (!bfd_coff_slurp_symbol_table (abfd))
525 return -1;
527 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
530 /* Canonicalize a COFF symbol table. */
532 long
533 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
535 unsigned int counter;
536 coff_symbol_type *symbase;
537 coff_symbol_type **location = (coff_symbol_type **) alocation;
539 if (!bfd_coff_slurp_symbol_table (abfd))
540 return -1;
542 symbase = obj_symbols (abfd);
543 counter = bfd_get_symcount (abfd);
544 while (counter-- > 0)
545 *location++ = symbase++;
547 *location = NULL;
549 return bfd_get_symcount (abfd);
552 /* Get the name of a symbol. The caller must pass in a buffer of size
553 >= SYMNMLEN + 1. */
555 const char *
556 _bfd_coff_internal_syment_name (bfd *abfd,
557 const struct internal_syment *sym,
558 char *buf)
560 /* FIXME: It's not clear this will work correctly if sizeof
561 (_n_zeroes) != 4. */
562 if (sym->_n._n_n._n_zeroes != 0
563 || sym->_n._n_n._n_offset == 0)
565 memcpy (buf, sym->_n._n_name, SYMNMLEN);
566 buf[SYMNMLEN] = '\0';
567 return buf;
569 else
571 const char *strings;
573 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
574 strings = obj_coff_strings (abfd);
575 if (strings == NULL)
577 strings = _bfd_coff_read_string_table (abfd);
578 if (strings == NULL)
579 return NULL;
581 if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
582 return NULL;
583 return strings + sym->_n._n_n._n_offset;
587 /* Read in and swap the relocs. This returns a buffer holding the
588 relocs for section SEC in file ABFD. If CACHE is TRUE and
589 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
590 the function is called again. If EXTERNAL_RELOCS is not NULL, it
591 is a buffer large enough to hold the unswapped relocs. If
592 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
593 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
594 value must be INTERNAL_RELOCS. The function returns NULL on error. */
596 struct internal_reloc *
597 _bfd_coff_read_internal_relocs (bfd *abfd,
598 asection *sec,
599 bool cache,
600 bfd_byte *external_relocs,
601 bool require_internal,
602 struct internal_reloc *internal_relocs)
604 bfd_size_type relsz;
605 bfd_byte *free_external = NULL;
606 struct internal_reloc *free_internal = NULL;
607 bfd_byte *erel;
608 bfd_byte *erel_end;
609 struct internal_reloc *irel;
610 bfd_size_type amt;
612 if (sec->reloc_count == 0)
613 return internal_relocs; /* Nothing to do. */
615 if (coff_section_data (abfd, sec) != NULL
616 && coff_section_data (abfd, sec)->relocs != NULL)
618 if (! require_internal)
619 return coff_section_data (abfd, sec)->relocs;
620 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
621 sec->reloc_count * sizeof (struct internal_reloc));
622 return internal_relocs;
625 relsz = bfd_coff_relsz (abfd);
627 amt = sec->reloc_count * relsz;
628 if (external_relocs == NULL)
630 free_external = (bfd_byte *) bfd_malloc (amt);
631 if (free_external == NULL)
632 goto error_return;
633 external_relocs = free_external;
636 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
637 || bfd_read (external_relocs, amt, abfd) != amt)
638 goto error_return;
640 if (internal_relocs == NULL)
642 amt = sec->reloc_count;
643 amt *= sizeof (struct internal_reloc);
644 free_internal = (struct internal_reloc *) bfd_malloc (amt);
645 if (free_internal == NULL)
646 goto error_return;
647 internal_relocs = free_internal;
650 /* Swap in the relocs. */
651 erel = external_relocs;
652 erel_end = erel + relsz * sec->reloc_count;
653 irel = internal_relocs;
654 for (; erel < erel_end; erel += relsz, irel++)
655 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
657 free (free_external);
658 free_external = NULL;
660 if (cache && free_internal != NULL)
662 if (coff_section_data (abfd, sec) == NULL)
664 amt = sizeof (struct coff_section_tdata);
665 sec->used_by_bfd = bfd_zalloc (abfd, amt);
666 if (sec->used_by_bfd == NULL)
667 goto error_return;
668 coff_section_data (abfd, sec)->contents = NULL;
670 coff_section_data (abfd, sec)->relocs = free_internal;
673 return internal_relocs;
675 error_return:
676 free (free_external);
677 free (free_internal);
678 return NULL;
681 /* Set lineno_count for the output sections of a COFF file. */
684 coff_count_linenumbers (bfd *abfd)
686 unsigned int limit = bfd_get_symcount (abfd);
687 unsigned int i;
688 int total = 0;
689 asymbol **p;
690 asection *s;
692 if (limit == 0)
694 /* This may be from the backend linker, in which case the
695 lineno_count in the sections is correct. */
696 for (s = abfd->sections; s != NULL; s = s->next)
697 total += s->lineno_count;
698 return total;
701 for (s = abfd->sections; s != NULL; s = s->next)
702 BFD_ASSERT (s->lineno_count == 0);
704 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
706 asymbol *q_maybe = *p;
708 if (bfd_asymbol_bfd (q_maybe) != NULL
709 && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
711 coff_symbol_type *q = coffsymbol (q_maybe);
713 /* The AIX 4.1 compiler can sometimes generate line numbers
714 attached to debugging symbols. We try to simply ignore
715 those here. */
716 if (q->lineno != NULL
717 && q->symbol.section->owner != NULL)
719 /* This symbol has line numbers. Increment the owning
720 section's linenumber count. */
721 alent *l = q->lineno;
725 asection * sec = q->symbol.section->output_section;
727 /* Do not try to update fields in read-only sections. */
728 if (! bfd_is_const_section (sec))
729 sec->lineno_count ++;
731 ++total;
732 ++l;
734 while (l->line_number != 0);
739 return total;
742 static void
743 fixup_symbol_value (bfd *abfd,
744 coff_symbol_type *coff_symbol_ptr,
745 struct internal_syment *syment)
747 /* Normalize the symbol flags. */
748 if (coff_symbol_ptr->symbol.section
749 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
751 /* A common symbol is undefined with a value. */
752 syment->n_scnum = N_UNDEF;
753 syment->n_value = coff_symbol_ptr->symbol.value;
755 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
756 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
758 syment->n_value = coff_symbol_ptr->symbol.value;
760 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
762 syment->n_scnum = N_UNDEF;
763 syment->n_value = 0;
765 /* FIXME: Do we need to handle the absolute section here? */
766 else
768 if (coff_symbol_ptr->symbol.section)
770 syment->n_scnum =
771 coff_symbol_ptr->symbol.section->output_section->target_index;
773 syment->n_value = (coff_symbol_ptr->symbol.value
774 + coff_symbol_ptr->symbol.section->output_offset);
775 if (! obj_pe (abfd))
777 syment->n_value += (syment->n_sclass == C_STATLAB)
778 ? coff_symbol_ptr->symbol.section->output_section->lma
779 : coff_symbol_ptr->symbol.section->output_section->vma;
782 else
784 BFD_ASSERT (0);
785 /* This can happen, but I don't know why yet (steve@cygnus.com) */
786 syment->n_scnum = N_ABS;
787 syment->n_value = coff_symbol_ptr->symbol.value;
792 /* Run through all the symbols in the symbol table and work out what
793 their indexes into the symbol table will be when output.
795 Coff requires that each C_FILE symbol points to the next one in the
796 chain, and that the last one points to the first external symbol. We
797 do that here too. */
799 bool
800 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
802 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
803 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
804 unsigned int native_index = 0;
805 struct internal_syment *last_file = NULL;
806 unsigned int symbol_index;
808 /* COFF demands that undefined symbols come after all other symbols.
809 Since we don't need to impose this extra knowledge on all our
810 client programs, deal with that here. Sort the symbol table;
811 just move the undefined symbols to the end, leaving the rest
812 alone. The O'Reilly book says that defined global symbols come
813 at the end before the undefined symbols, so we do that here as
814 well. */
815 /* @@ Do we have some condition we could test for, so we don't always
816 have to do this? I don't think relocatability is quite right, but
817 I'm not certain. [raeburn:19920508.1711EST] */
819 asymbol **newsyms;
820 unsigned int i;
821 bfd_size_type amt;
823 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
824 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
825 if (!newsyms)
826 return false;
827 bfd_ptr->outsymbols = newsyms;
828 for (i = 0; i < symbol_count; i++)
829 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
830 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
831 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
832 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
833 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
834 == 0))))
835 *newsyms++ = symbol_ptr_ptr[i];
837 for (i = 0; i < symbol_count; i++)
838 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
839 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
840 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
841 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
842 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
843 != 0))))
844 *newsyms++ = symbol_ptr_ptr[i];
846 *first_undef = newsyms - bfd_ptr->outsymbols;
848 for (i = 0; i < symbol_count; i++)
849 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
850 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
851 *newsyms++ = symbol_ptr_ptr[i];
852 *newsyms = (asymbol *) NULL;
853 symbol_ptr_ptr = bfd_ptr->outsymbols;
856 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
858 coff_symbol_type *coff_symbol_ptr;
860 coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
861 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
862 if (coff_symbol_ptr && coff_symbol_ptr->native)
864 combined_entry_type *s = coff_symbol_ptr->native;
865 int i;
867 BFD_ASSERT (s->is_sym);
868 if (s->u.syment.n_sclass == C_FILE)
870 if (last_file != NULL)
871 last_file->n_value = native_index;
872 last_file = &(s->u.syment);
874 else
875 /* Modify the symbol values according to their section and
876 type. */
877 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
879 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
880 s[i].offset = native_index++;
882 else
883 native_index++;
886 obj_conv_table_size (bfd_ptr) = native_index;
888 return true;
891 /* Run thorough the symbol table again, and fix it so that all
892 pointers to entries are changed to the entries' index in the output
893 symbol table. */
895 void
896 coff_mangle_symbols (bfd *bfd_ptr)
898 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
899 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
900 unsigned int symbol_index;
902 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
904 coff_symbol_type *coff_symbol_ptr;
906 coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
907 if (coff_symbol_ptr && coff_symbol_ptr->native)
909 int i;
910 combined_entry_type *s = coff_symbol_ptr->native;
912 BFD_ASSERT (s->is_sym);
913 if (s->fix_value)
915 /* FIXME: We should use a union here. */
916 s->u.syment.n_value =
917 (uintptr_t) ((combined_entry_type *)
918 (uintptr_t) s->u.syment.n_value)->offset;
919 s->fix_value = 0;
921 if (s->fix_line)
923 /* The value is the offset into the line number entries
924 for the symbol's section. On output, the symbol's
925 section should be N_DEBUG. */
926 s->u.syment.n_value =
927 (coff_symbol_ptr->symbol.section->output_section->line_filepos
928 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
929 coff_symbol_ptr->symbol.section =
930 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
931 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
933 for (i = 0; i < s->u.syment.n_numaux; i++)
935 combined_entry_type *a = s + i + 1;
937 BFD_ASSERT (! a->is_sym);
938 if (a->fix_tag)
940 a->u.auxent.x_sym.x_tagndx.u32 =
941 a->u.auxent.x_sym.x_tagndx.p->offset;
942 a->fix_tag = 0;
944 if (a->fix_end)
946 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
947 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
948 a->fix_end = 0;
950 if (a->fix_scnlen)
952 a->u.auxent.x_csect.x_scnlen.u64 =
953 a->u.auxent.x_csect.x_scnlen.p->offset;
954 a->fix_scnlen = 0;
961 static bool
962 coff_write_auxent_fname (bfd *abfd,
963 char *str,
964 union internal_auxent *auxent,
965 struct bfd_strtab_hash *strtab,
966 bool hash)
968 unsigned int str_length = strlen (str);
969 unsigned int filnmlen = bfd_coff_filnmlen (abfd);
971 if (bfd_coff_long_filenames (abfd))
973 if (str_length <= filnmlen)
974 strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
975 else
977 bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
979 if (indx == (bfd_size_type) -1)
980 return false;
982 auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
983 auxent->x_file.x_n.x_n.x_zeroes = 0;
986 else
988 strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
989 if (str_length > filnmlen)
990 str[filnmlen] = '\0';
993 return true;
996 static bool
997 coff_fix_symbol_name (bfd *abfd,
998 asymbol *symbol,
999 combined_entry_type *native,
1000 struct bfd_strtab_hash *strtab,
1001 bool hash,
1002 asection **debug_string_section_p,
1003 bfd_size_type *debug_string_size_p)
1005 unsigned int name_length;
1006 char *name = (char *) (symbol->name);
1007 bfd_size_type indx;
1009 if (name == NULL)
1011 /* COFF symbols always have names, so we'll make one up. */
1012 symbol->name = "strange";
1013 name = (char *) symbol->name;
1015 name_length = strlen (name);
1017 BFD_ASSERT (native->is_sym);
1018 if (native->u.syment.n_sclass == C_FILE
1019 && native->u.syment.n_numaux > 0)
1021 if (bfd_coff_force_symnames_in_strings (abfd))
1023 indx = _bfd_stringtab_add (strtab, ".file", hash, false);
1024 if (indx == (bfd_size_type) -1)
1025 return false;
1027 native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1028 native->u.syment._n._n_n._n_zeroes = 0;
1030 else
1031 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
1033 BFD_ASSERT (! (native + 1)->is_sym);
1034 if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
1035 strtab, hash))
1036 return false;
1038 else
1040 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
1041 /* This name will fit into the symbol neatly. */
1042 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
1044 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
1046 indx = _bfd_stringtab_add (strtab, name, hash, false);
1047 if (indx == (bfd_size_type) -1)
1048 return false;
1050 native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1051 native->u.syment._n._n_n._n_zeroes = 0;
1053 else
1055 file_ptr filepos;
1056 bfd_byte buf[4];
1057 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
1059 /* This name should be written into the .debug section. For
1060 some reason each name is preceded by a two byte length
1061 and also followed by a null byte. FIXME: We assume that
1062 the .debug section has already been created, and that it
1063 is large enough. */
1064 if (*debug_string_section_p == (asection *) NULL)
1065 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
1066 filepos = bfd_tell (abfd);
1067 if (prefix_len == 4)
1068 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
1069 else
1070 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
1072 if (!bfd_set_section_contents (abfd,
1073 *debug_string_section_p,
1074 (void *) buf,
1075 (file_ptr) *debug_string_size_p,
1076 (bfd_size_type) prefix_len)
1077 || !bfd_set_section_contents (abfd,
1078 *debug_string_section_p,
1079 (void *) symbol->name,
1080 (file_ptr) (*debug_string_size_p
1081 + prefix_len),
1082 (bfd_size_type) name_length + 1))
1083 abort ();
1084 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1085 abort ();
1086 native->u.syment._n._n_n._n_offset =
1087 *debug_string_size_p + prefix_len;
1088 native->u.syment._n._n_n._n_zeroes = 0;
1089 *debug_string_size_p += name_length + 1 + prefix_len;
1093 return true;
1096 /* We need to keep track of the symbol index so that when we write out
1097 the relocs we can get the index for a symbol. This method is a
1098 hack. FIXME. */
1100 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
1102 /* Write a symbol out to a COFF file. */
1104 static bool
1105 coff_write_symbol (bfd *abfd,
1106 asymbol *symbol,
1107 combined_entry_type *native,
1108 bfd_vma *written,
1109 struct bfd_strtab_hash *strtab,
1110 bool hash,
1111 asection **debug_string_section_p,
1112 bfd_size_type *debug_string_size_p)
1114 unsigned int numaux = native->u.syment.n_numaux;
1115 int type = native->u.syment.n_type;
1116 int n_sclass = (int) native->u.syment.n_sclass;
1117 asection *output_section = symbol->section->output_section
1118 ? symbol->section->output_section
1119 : symbol->section;
1120 void * buf;
1121 bfd_size_type symesz;
1123 BFD_ASSERT (native->is_sym);
1125 if (native->u.syment.n_sclass == C_FILE)
1126 symbol->flags |= BSF_DEBUGGING;
1128 if (symbol->flags & BSF_DEBUGGING
1129 && bfd_is_abs_section (symbol->section))
1130 native->u.syment.n_scnum = N_DEBUG;
1132 else if (bfd_is_abs_section (symbol->section))
1133 native->u.syment.n_scnum = N_ABS;
1135 else if (bfd_is_und_section (symbol->section))
1136 native->u.syment.n_scnum = N_UNDEF;
1138 else
1139 native->u.syment.n_scnum =
1140 output_section->target_index;
1142 if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
1143 debug_string_section_p, debug_string_size_p))
1144 return false;
1146 symesz = bfd_coff_symesz (abfd);
1147 buf = bfd_alloc (abfd, symesz);
1148 if (!buf)
1149 return false;
1150 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1151 if (bfd_write (buf, symesz, abfd) != symesz)
1152 return false;
1153 bfd_release (abfd, buf);
1155 if (native->u.syment.n_numaux > 0)
1157 bfd_size_type auxesz;
1158 unsigned int j;
1160 auxesz = bfd_coff_auxesz (abfd);
1161 buf = bfd_alloc (abfd, auxesz);
1162 if (!buf)
1163 return false;
1164 for (j = 0; j < native->u.syment.n_numaux; j++)
1166 BFD_ASSERT (! (native + j + 1)->is_sym);
1168 /* Adjust auxent only if this isn't the filename
1169 auxiliary entry. */
1170 if (native->u.syment.n_sclass == C_FILE
1171 && (native + j + 1)->u.auxent.x_file.x_ftype
1172 && (native + j + 1)->extrap)
1173 coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap,
1174 &(native + j + 1)->u.auxent, strtab, hash);
1176 bfd_coff_swap_aux_out (abfd,
1177 &((native + j + 1)->u.auxent),
1178 type, n_sclass, (int) j,
1179 native->u.syment.n_numaux,
1180 buf);
1181 if (bfd_write (buf, auxesz, abfd) != auxesz)
1182 return false;
1184 bfd_release (abfd, buf);
1187 /* Store the index for use when we write out the relocs. */
1188 set_index (symbol, *written);
1190 *written += numaux + 1;
1191 return true;
1194 /* Write out a symbol to a COFF file that does not come from a COFF
1195 file originally. This symbol may have been created by the linker,
1196 or we may be linking a non COFF file to a COFF file. */
1198 bool
1199 coff_write_alien_symbol (bfd *abfd,
1200 asymbol *symbol,
1201 struct internal_syment *isym,
1202 bfd_vma *written,
1203 struct bfd_strtab_hash *strtab,
1204 bool hash,
1205 asection **debug_string_section_p,
1206 bfd_size_type *debug_string_size_p)
1208 combined_entry_type *native;
1209 combined_entry_type dummy[2];
1210 asection *output_section = symbol->section->output_section
1211 ? symbol->section->output_section
1212 : symbol->section;
1213 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1214 bool ret;
1216 if ((!link_info || link_info->strip_discarded)
1217 && !bfd_is_abs_section (symbol->section)
1218 && symbol->section->output_section == bfd_abs_section_ptr)
1220 symbol->name = "";
1221 if (isym != NULL)
1222 memset (isym, 0, sizeof (*isym));
1223 return true;
1225 memset (dummy, 0, sizeof dummy);
1226 native = dummy;
1227 native->is_sym = true;
1228 native[1].is_sym = false;
1229 native->u.syment.n_type = T_NULL;
1230 native->u.syment.n_flags = 0;
1231 native->u.syment.n_numaux = 0;
1232 if (bfd_is_und_section (symbol->section))
1234 native->u.syment.n_scnum = N_UNDEF;
1235 native->u.syment.n_value = symbol->value;
1237 else if (bfd_is_com_section (symbol->section))
1239 native->u.syment.n_scnum = N_UNDEF;
1240 native->u.syment.n_value = symbol->value;
1242 else if (symbol->flags & BSF_FILE)
1244 native->u.syment.n_scnum = N_DEBUG;
1245 native->u.syment.n_numaux = 1;
1247 else if (symbol->flags & BSF_DEBUGGING)
1249 /* There isn't much point to writing out a debugging symbol
1250 unless we are prepared to convert it into COFF debugging
1251 format. So, we just ignore them. We must clobber the symbol
1252 name to keep it from being put in the string table. */
1253 symbol->name = "";
1254 if (isym != NULL)
1255 memset (isym, 0, sizeof (*isym));
1256 return true;
1258 else
1260 native->u.syment.n_scnum = output_section->target_index;
1261 native->u.syment.n_value = (symbol->value
1262 + symbol->section->output_offset);
1263 if (! obj_pe (abfd))
1264 native->u.syment.n_value += output_section->vma;
1266 /* Copy the any flags from the file header into the symbol.
1267 FIXME: Why? */
1269 coff_symbol_type *c = coff_symbol_from (symbol);
1270 if (c != (coff_symbol_type *) NULL)
1271 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1275 native->u.syment.n_type = 0;
1276 if (symbol->flags & BSF_FILE)
1277 native->u.syment.n_sclass = C_FILE;
1278 else if (symbol->flags & BSF_LOCAL)
1279 native->u.syment.n_sclass = C_STAT;
1280 else if (symbol->flags & BSF_WEAK)
1281 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1282 else
1283 native->u.syment.n_sclass = C_EXT;
1285 ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash,
1286 debug_string_section_p, debug_string_size_p);
1287 if (isym != NULL)
1288 *isym = native->u.syment;
1289 return ret;
1292 /* Write a native symbol to a COFF file. */
1294 static bool
1295 coff_write_native_symbol (bfd *abfd,
1296 coff_symbol_type *symbol,
1297 bfd_vma *written,
1298 struct bfd_strtab_hash *strtab,
1299 asection **debug_string_section_p,
1300 bfd_size_type *debug_string_size_p)
1302 combined_entry_type *native = symbol->native;
1303 alent *lineno = symbol->lineno;
1304 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1306 if ((!link_info || link_info->strip_discarded)
1307 && !bfd_is_abs_section (symbol->symbol.section)
1308 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1310 symbol->symbol.name = "";
1311 return true;
1314 BFD_ASSERT (native->is_sym);
1315 /* If this symbol has an associated line number, we must store the
1316 symbol index in the line number field. We also tag the auxent to
1317 point to the right place in the lineno table. */
1318 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1320 unsigned int count = 0;
1322 lineno[count].u.offset = *written;
1323 if (native->u.syment.n_numaux)
1325 union internal_auxent *a = &((native + 1)->u.auxent);
1327 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1328 symbol->symbol.section->output_section->moving_line_filepos;
1331 /* Count and relocate all other linenumbers. */
1332 count++;
1333 while (lineno[count].line_number != 0)
1335 lineno[count].u.offset +=
1336 (symbol->symbol.section->output_section->vma
1337 + symbol->symbol.section->output_offset);
1338 count++;
1340 symbol->done_lineno = true;
1342 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1343 symbol->symbol.section->output_section->moving_line_filepos +=
1344 count * bfd_coff_linesz (abfd);
1347 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1348 strtab, true, debug_string_section_p,
1349 debug_string_size_p);
1352 static void
1353 null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
1354 va_list ap ATTRIBUTE_UNUSED)
1358 /* Write out the COFF symbols. */
1360 bool
1361 coff_write_symbols (bfd *abfd)
1363 struct bfd_strtab_hash *strtab;
1364 asection *debug_string_section;
1365 bfd_size_type debug_string_size;
1366 unsigned int i;
1367 unsigned int limit = bfd_get_symcount (abfd);
1368 bfd_vma written = 0;
1369 asymbol **p;
1371 debug_string_section = NULL;
1372 debug_string_size = 0;
1374 strtab = _bfd_stringtab_init ();
1375 if (strtab == NULL)
1376 return false;
1378 /* If this target supports long section names, they must be put into
1379 the string table. This is supported by PE. This code must
1380 handle section names just as they are handled in
1381 coff_write_object_contents. This is why we pass hash as FALSE below. */
1382 if (bfd_coff_long_section_names (abfd))
1384 asection *o;
1386 for (o = abfd->sections; o != NULL; o = o->next)
1387 if (strlen (o->name) > SCNNMLEN
1388 && _bfd_stringtab_add (strtab, o->name, false, false)
1389 == (bfd_size_type) -1)
1390 return false;
1393 /* Seek to the right place. */
1394 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1395 return false;
1397 /* Output all the symbols we have. */
1398 written = 0;
1399 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1401 asymbol *symbol = *p;
1402 coff_symbol_type *c_symbol = coff_symbol_from (symbol);
1404 if (c_symbol == (coff_symbol_type *) NULL
1405 || c_symbol->native == (combined_entry_type *) NULL)
1407 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1408 strtab, true, &debug_string_section,
1409 &debug_string_size))
1410 return false;
1412 else
1414 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1416 bfd_error_handler_type current_error_handler;
1417 enum coff_symbol_classification sym_class;
1418 unsigned char *n_sclass;
1420 /* Suppress error reporting by bfd_coff_classify_symbol.
1421 Error messages can be generated when we are processing a local
1422 symbol which has no associated section and we do not have to
1423 worry about this, all we need to know is that it is local. */
1424 current_error_handler = bfd_set_error_handler (null_error_handler);
1425 BFD_ASSERT (c_symbol->native->is_sym);
1426 sym_class = bfd_coff_classify_symbol (abfd,
1427 &c_symbol->native->u.syment);
1428 (void) bfd_set_error_handler (current_error_handler);
1430 n_sclass = &c_symbol->native->u.syment.n_sclass;
1432 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1433 we cannot retain the existing sclass from the original symbol.
1434 Weak symbols only have one valid sclass, so just set it always.
1435 If it is not local class and should be, set it C_STAT.
1436 If it is global and not classified as global, or if it is
1437 weak (which is also classified as global), set it C_EXT. */
1439 if (symbol->flags & BSF_WEAK)
1440 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1441 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1442 *n_sclass = C_STAT;
1443 else if (symbol->flags & BSF_GLOBAL
1444 && (sym_class != COFF_SYMBOL_GLOBAL
1445 #ifdef COFF_WITH_PE
1446 || *n_sclass == C_NT_WEAK
1447 #endif
1448 || *n_sclass == C_WEAKEXT))
1449 c_symbol->native->u.syment.n_sclass = C_EXT;
1452 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1453 strtab, &debug_string_section,
1454 &debug_string_size))
1455 return false;
1459 obj_raw_syment_count (abfd) = written;
1461 /* Now write out strings.
1463 We would normally not write anything here if there are no strings, but
1464 we'll write out 4 so that any stupid coff reader which tries to read the
1465 string table even when there isn't one won't croak. */
1467 bfd_byte buffer[STRING_SIZE_SIZE];
1469 #if STRING_SIZE_SIZE == 4
1470 H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
1471 #else
1472 #error Change H_PUT_32
1473 #endif
1474 if (bfd_write (buffer, sizeof (buffer), abfd) != sizeof (buffer))
1475 return false;
1477 if (! _bfd_stringtab_emit (abfd, strtab))
1478 return false;
1481 _bfd_stringtab_free (strtab);
1483 /* Make sure the .debug section was created to be the correct size.
1484 We should create it ourselves on the fly, but we don't because
1485 BFD won't let us write to any section until we know how large all
1486 the sections are. We could still do it by making another pass
1487 over the symbols. FIXME. */
1488 BFD_ASSERT (debug_string_size == 0
1489 || (debug_string_section != (asection *) NULL
1490 && (BFD_ALIGN (debug_string_size,
1491 1 << debug_string_section->alignment_power)
1492 == debug_string_section->size)));
1494 return true;
1497 bool
1498 coff_write_linenumbers (bfd *abfd)
1500 asection *s;
1501 bfd_size_type linesz;
1502 void * buff;
1504 linesz = bfd_coff_linesz (abfd);
1505 buff = bfd_alloc (abfd, linesz);
1506 if (!buff)
1507 return false;
1508 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1510 if (s->lineno_count)
1512 asymbol **q = abfd->outsymbols;
1513 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1514 return false;
1515 /* Find all the linenumbers in this section. */
1516 while (*q)
1518 asymbol *p = *q;
1519 if (p->section->output_section == s)
1521 alent *l =
1522 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1523 (bfd_asymbol_bfd (p), p));
1524 if (l)
1526 /* Found a linenumber entry, output. */
1527 struct internal_lineno out;
1529 memset ((void *) & out, 0, sizeof (out));
1530 out.l_lnno = 0;
1531 out.l_addr.l_symndx = l->u.offset;
1532 bfd_coff_swap_lineno_out (abfd, &out, buff);
1533 if (bfd_write (buff, linesz, abfd) != linesz)
1534 return false;
1535 l++;
1536 while (l->line_number)
1538 out.l_lnno = l->line_number;
1539 out.l_addr.l_symndx = l->u.offset;
1540 bfd_coff_swap_lineno_out (abfd, &out, buff);
1541 if (bfd_write (buff, linesz, abfd) != linesz)
1542 return false;
1543 l++;
1547 q++;
1551 bfd_release (abfd, buff);
1552 return true;
1555 alent *
1556 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1558 return coffsymbol (symbol)->lineno;
1561 /* This function transforms the offsets into the symbol table into
1562 pointers to syments. */
1564 static void
1565 coff_pointerize_aux (bfd *abfd,
1566 combined_entry_type *table_base,
1567 combined_entry_type *symbol,
1568 unsigned int indaux,
1569 combined_entry_type *auxent)
1571 unsigned int type = symbol->u.syment.n_type;
1572 unsigned int n_sclass = symbol->u.syment.n_sclass;
1574 BFD_ASSERT (symbol->is_sym);
1575 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1577 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1578 (abfd, table_base, symbol, indaux, auxent))
1579 return;
1582 /* Don't bother if this is a file or a section. */
1583 if (n_sclass == C_STAT && type == T_NULL)
1584 return;
1585 if (n_sclass == C_FILE)
1586 return;
1587 if (n_sclass == C_DWARF)
1588 return;
1590 BFD_ASSERT (! auxent->is_sym);
1591 /* Otherwise patch up. */
1592 #define N_TMASK coff_data (abfd)->local_n_tmask
1593 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1595 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1596 || n_sclass == C_FCN)
1597 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 > 0
1598 && (auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32
1599 < obj_raw_syment_count (abfd)))
1601 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1602 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
1603 auxent->fix_end = 1;
1606 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1607 generate one, so we must be careful to ignore it. */
1608 if (auxent->u.auxent.x_sym.x_tagndx.u32 < obj_raw_syment_count (abfd))
1610 auxent->u.auxent.x_sym.x_tagndx.p =
1611 table_base + auxent->u.auxent.x_sym.x_tagndx.u32;
1612 auxent->fix_tag = 1;
1616 /* Allocate space for the ".debug" section, and read it.
1617 We did not read the debug section until now, because
1618 we didn't want to go to the trouble until someone needed it. */
1620 static char *
1621 build_debug_section (bfd *abfd, asection ** sect_return)
1623 char *debug_section;
1624 file_ptr position;
1625 bfd_size_type sec_size;
1627 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1629 if (!sect)
1631 bfd_set_error (bfd_error_no_debug_section);
1632 return NULL;
1635 /* Seek to the beginning of the `.debug' section and read it.
1636 Save the current position first; it is needed by our caller.
1637 Then read debug section and reset the file pointer. */
1639 position = bfd_tell (abfd);
1640 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
1641 return NULL;
1643 sec_size = sect->size;
1644 debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size + 1, sec_size);
1645 if (debug_section == NULL)
1646 return NULL;
1647 debug_section[sec_size] = 0;
1649 if (bfd_seek (abfd, position, SEEK_SET) != 0)
1650 return NULL;
1652 * sect_return = sect;
1653 return debug_section;
1656 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1657 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1658 be \0-terminated. */
1660 static char *
1661 copy_name (bfd *abfd, char *name, size_t maxlen)
1663 size_t len;
1664 char *newname;
1666 for (len = 0; len < maxlen; ++len)
1667 if (name[len] == '\0')
1668 break;
1670 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1671 return NULL;
1673 strncpy (newname, name, len);
1674 newname[len] = '\0';
1675 return newname;
1678 /* Read in the external symbols. */
1680 bool
1681 _bfd_coff_get_external_symbols (bfd *abfd)
1683 size_t symesz;
1684 size_t size;
1685 void * syms;
1686 ufile_ptr filesize;
1688 if (obj_coff_external_syms (abfd) != NULL)
1689 return true;
1691 symesz = bfd_coff_symesz (abfd);
1692 if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
1694 bfd_set_error (bfd_error_file_truncated);
1695 return false;
1698 if (size == 0)
1699 return true;
1701 filesize = bfd_get_file_size (abfd);
1702 if (filesize != 0
1703 && ((ufile_ptr) obj_sym_filepos (abfd) > filesize
1704 || size > filesize - obj_sym_filepos (abfd)))
1706 bfd_set_error (bfd_error_file_truncated);
1707 return false;
1710 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1711 return false;
1712 syms = _bfd_malloc_and_read (abfd, size, size);
1713 obj_coff_external_syms (abfd) = syms;
1714 return syms != NULL;
1717 /* Read in the external strings. The strings are not loaded until
1718 they are needed. This is because we have no simple way of
1719 detecting a missing string table in an archive. If the strings
1720 are loaded then the STRINGS and STRINGS_LEN fields in the
1721 coff_tdata structure will be set. */
1723 const char *
1724 _bfd_coff_read_string_table (bfd *abfd)
1726 char extstrsize[STRING_SIZE_SIZE];
1727 bfd_size_type strsize;
1728 char *strings;
1729 ufile_ptr pos;
1730 ufile_ptr filesize;
1731 size_t symesz;
1732 size_t size;
1734 if (obj_coff_strings (abfd) != NULL)
1735 return obj_coff_strings (abfd);
1737 if (obj_sym_filepos (abfd) == 0)
1739 bfd_set_error (bfd_error_no_symbols);
1740 return NULL;
1743 symesz = bfd_coff_symesz (abfd);
1744 pos = obj_sym_filepos (abfd);
1745 if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
1746 || pos + size < pos)
1748 bfd_set_error (bfd_error_file_truncated);
1749 return NULL;
1752 if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
1753 return NULL;
1755 if (bfd_read (extstrsize, sizeof extstrsize, abfd) != sizeof extstrsize)
1757 if (bfd_get_error () != bfd_error_file_truncated)
1758 return NULL;
1760 /* There is no string table. */
1761 strsize = STRING_SIZE_SIZE;
1763 else
1765 #if STRING_SIZE_SIZE == 4
1766 strsize = H_GET_32 (abfd, extstrsize);
1767 #else
1768 #error Change H_GET_32
1769 #endif
1772 filesize = bfd_get_file_size (abfd);
1773 if (strsize < STRING_SIZE_SIZE
1774 || (filesize != 0 && strsize > filesize))
1776 _bfd_error_handler
1777 /* xgettext: c-format */
1778 (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
1779 bfd_set_error (bfd_error_bad_value);
1780 return NULL;
1783 strings = (char *) bfd_malloc (strsize + 1);
1784 if (strings == NULL)
1785 return NULL;
1787 /* PR 17521 file: 079-54929-0.004.
1788 A corrupt file could contain an index that points into the first
1789 STRING_SIZE_SIZE bytes of the string table, so make sure that
1790 they are zero. */
1791 memset (strings, 0, STRING_SIZE_SIZE);
1793 if (bfd_read (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1794 != strsize - STRING_SIZE_SIZE)
1796 free (strings);
1797 return NULL;
1800 obj_coff_strings (abfd) = strings;
1801 obj_coff_strings_len (abfd) = strsize;
1802 /* Terminate the string table, just in case. */
1803 strings[strsize] = 0;
1804 return strings;
1807 /* Free up the external symbols and strings read from a COFF file. */
1809 bool
1810 _bfd_coff_free_symbols (bfd *abfd)
1812 if (! bfd_family_coff (abfd))
1813 return false;
1815 if (obj_coff_external_syms (abfd) != NULL
1816 && ! obj_coff_keep_syms (abfd))
1818 free (obj_coff_external_syms (abfd));
1819 obj_coff_external_syms (abfd) = NULL;
1822 if (obj_coff_strings (abfd) != NULL
1823 && ! obj_coff_keep_strings (abfd))
1825 free (obj_coff_strings (abfd));
1826 obj_coff_strings (abfd) = NULL;
1827 obj_coff_strings_len (abfd) = 0;
1830 return true;
1833 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1834 knit the symbol names into a normalized form. By normalized here I
1835 mean that all symbols have an n_offset pointer that points to a null-
1836 terminated string. */
1838 combined_entry_type *
1839 coff_get_normalized_symtab (bfd *abfd)
1841 combined_entry_type *internal;
1842 combined_entry_type *internal_ptr;
1843 size_t symesz;
1844 char *raw_src;
1845 char *raw_end;
1846 const char *string_table = NULL;
1847 asection * debug_sec = NULL;
1848 char *debug_sec_data = NULL;
1849 bfd_size_type size;
1851 if (obj_raw_syments (abfd) != NULL)
1852 return obj_raw_syments (abfd);
1854 if (! _bfd_coff_get_external_symbols (abfd))
1855 return NULL;
1857 size = obj_raw_syment_count (abfd);
1858 /* Check for integer overflow. */
1859 if (size > (bfd_size_type) -1 / sizeof (combined_entry_type))
1860 return NULL;
1861 size *= sizeof (combined_entry_type);
1862 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1863 if (internal == NULL && size != 0)
1864 return NULL;
1866 raw_src = (char *) obj_coff_external_syms (abfd);
1868 /* Mark the end of the symbols. */
1869 symesz = bfd_coff_symesz (abfd);
1870 raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz);
1872 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1873 probably possible. If one shows up, it will probably kill us. */
1875 /* Swap all the raw entries. */
1876 for (internal_ptr = internal;
1877 raw_src < raw_end;
1878 raw_src += symesz, internal_ptr++)
1880 unsigned int i;
1882 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1883 (void *) & internal_ptr->u.syment);
1884 internal_ptr->is_sym = true;
1885 combined_entry_type *sym = internal_ptr;
1887 /* PR 17512: Prevent buffer overrun. */
1888 if (sym->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
1889 return NULL;
1891 for (i = 0; i < sym->u.syment.n_numaux; i++)
1893 internal_ptr++;
1894 raw_src += symesz;
1896 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1897 sym->u.syment.n_type,
1898 sym->u.syment.n_sclass,
1899 (int) i, sym->u.syment.n_numaux,
1900 &(internal_ptr->u.auxent));
1902 internal_ptr->is_sym = false;
1903 coff_pointerize_aux (abfd, internal, sym, i, internal_ptr);
1906 if (sym->u.syment.n_sclass == C_FILE
1907 && sym->u.syment.n_numaux > 0)
1909 combined_entry_type * aux = sym + 1;
1911 /* Make a file symbol point to the name in the auxent, since
1912 the text ".file" is redundant. */
1913 BFD_ASSERT (! aux->is_sym);
1915 if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1917 /* The filename is a long one, point into the string table. */
1918 if (string_table == NULL)
1920 string_table = _bfd_coff_read_string_table (abfd);
1921 if (string_table == NULL)
1922 return NULL;
1925 if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1926 >= obj_coff_strings_len (abfd))
1927 sym->u.syment._n._n_n._n_offset =
1928 (uintptr_t) bfd_symbol_error_name;
1929 else
1930 sym->u.syment._n._n_n._n_offset =
1931 (uintptr_t) (string_table
1932 + aux->u.auxent.x_file.x_n.x_n.x_offset);
1934 else
1936 /* Ordinary short filename, put into memory anyway. The
1937 Microsoft PE tools sometimes store a filename in
1938 multiple AUX entries. */
1939 size_t len;
1940 char *src;
1941 if (sym->u.syment.n_numaux > 1 && obj_pe (abfd))
1943 len = sym->u.syment.n_numaux * symesz;
1944 src = raw_src - (len - symesz);
1946 else
1948 len = bfd_coff_filnmlen (abfd);
1949 src = aux->u.auxent.x_file.x_n.x_fname;
1951 sym->u.syment._n._n_n._n_offset =
1952 (uintptr_t) copy_name (abfd, src, len);
1955 /* Normalize other strings available in C_FILE aux entries. */
1956 if (!obj_pe (abfd))
1957 for (int numaux = 1;
1958 numaux < sym->u.syment.n_numaux;
1959 numaux++)
1961 aux = sym + numaux + 1;
1962 BFD_ASSERT (! aux->is_sym);
1964 if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1966 /* The string information is a long one, point
1967 into the string table. */
1968 if (string_table == NULL)
1970 string_table = _bfd_coff_read_string_table (abfd);
1971 if (string_table == NULL)
1972 return NULL;
1975 if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1976 >= obj_coff_strings_len (abfd))
1977 aux->u.auxent.x_file.x_n.x_n.x_offset =
1978 (uintptr_t) bfd_symbol_error_name;
1979 else
1980 aux->u.auxent.x_file.x_n.x_n.x_offset =
1981 (uintptr_t) (string_table
1982 + aux->u.auxent.x_file.x_n.x_n.x_offset);
1984 else
1985 aux->u.auxent.x_file.x_n.x_n.x_offset =
1986 ((uintptr_t)
1987 copy_name (abfd,
1988 aux->u.auxent.x_file.x_n.x_fname,
1989 bfd_coff_filnmlen (abfd)));
1993 else
1995 if (sym->u.syment._n._n_n._n_zeroes != 0)
1997 /* This is a "short" name. Make it long. */
1998 char *newstring;
2000 /* Find the length of this string without walking into memory
2001 that isn't ours. */
2002 for (i = 0; i < SYMNMLEN; ++i)
2003 if (sym->u.syment._n._n_name[i] == '\0')
2004 break;
2006 newstring = bfd_alloc (abfd, i + 1);
2007 if (newstring == NULL)
2008 return NULL;
2009 memcpy (newstring, sym->u.syment._n._n_name, i);
2010 newstring[i] = 0;
2011 sym->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
2012 sym->u.syment._n._n_n._n_zeroes = 0;
2014 else if (sym->u.syment._n._n_n._n_offset == 0)
2015 sym->u.syment._n._n_n._n_offset = (uintptr_t) "";
2016 else if (!bfd_coff_symname_in_debug (abfd, &sym->u.syment))
2018 /* Long name already. Point symbol at the string in the
2019 table. */
2020 if (string_table == NULL)
2022 string_table = _bfd_coff_read_string_table (abfd);
2023 if (string_table == NULL)
2024 return NULL;
2026 if (sym->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd))
2027 sym->u.syment._n._n_n._n_offset =
2028 (uintptr_t) bfd_symbol_error_name;
2029 else
2030 sym->u.syment._n._n_n._n_offset =
2031 (uintptr_t) (string_table
2032 + sym->u.syment._n._n_n._n_offset);
2034 else
2036 /* Long name in debug section. Very similar. */
2037 if (debug_sec_data == NULL)
2039 debug_sec_data = build_debug_section (abfd, &debug_sec);
2040 if (debug_sec_data == NULL)
2041 return NULL;
2043 /* PR binutils/17512: Catch out of range offsets into
2044 the debug data. */
2045 if (sym->u.syment._n._n_n._n_offset >= debug_sec->size)
2046 sym->u.syment._n._n_n._n_offset =
2047 (uintptr_t) bfd_symbol_error_name;
2048 else
2049 sym->u.syment._n._n_n._n_offset =
2050 (uintptr_t) (debug_sec_data
2051 + sym->u.syment._n._n_n._n_offset);
2056 /* Free the raw symbols. */
2057 if (obj_coff_external_syms (abfd) != NULL
2058 && ! obj_coff_keep_syms (abfd))
2060 free (obj_coff_external_syms (abfd));
2061 obj_coff_external_syms (abfd) = NULL;
2064 obj_raw_syments (abfd) = internal;
2065 BFD_ASSERT (obj_raw_syment_count (abfd)
2066 == (size_t) (internal_ptr - internal));
2068 return internal;
2071 long
2072 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
2074 size_t count, raw;
2076 count = asect->reloc_count;
2077 if (count >= LONG_MAX / sizeof (arelent *)
2078 || _bfd_mul_overflow (count, bfd_coff_relsz (abfd), &raw))
2080 bfd_set_error (bfd_error_file_too_big);
2081 return -1;
2083 if (!bfd_write_p (abfd))
2085 ufile_ptr filesize = bfd_get_file_size (abfd);
2086 if (filesize != 0 && raw > filesize)
2088 bfd_set_error (bfd_error_file_truncated);
2089 return -1;
2092 return (count + 1) * sizeof (arelent *);
2095 asymbol *
2096 coff_make_empty_symbol (bfd *abfd)
2098 size_t amt = sizeof (coff_symbol_type);
2099 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
2101 if (new_symbol == NULL)
2102 return NULL;
2103 new_symbol->symbol.section = 0;
2104 new_symbol->native = NULL;
2105 new_symbol->lineno = NULL;
2106 new_symbol->done_lineno = false;
2107 new_symbol->symbol.the_bfd = abfd;
2109 return & new_symbol->symbol;
2112 /* Make a debugging symbol. */
2114 asymbol *
2115 coff_bfd_make_debug_symbol (bfd *abfd)
2117 size_t amt = sizeof (coff_symbol_type);
2118 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
2120 if (new_symbol == NULL)
2121 return NULL;
2122 /* @@ The 10 is a guess at a plausible maximum number of aux entries
2123 (but shouldn't be a constant). */
2124 amt = sizeof (combined_entry_type) * 10;
2125 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2126 if (!new_symbol->native)
2127 return NULL;
2128 new_symbol->native->is_sym = true;
2129 new_symbol->symbol.section = bfd_abs_section_ptr;
2130 new_symbol->symbol.flags = BSF_DEBUGGING;
2131 new_symbol->lineno = NULL;
2132 new_symbol->done_lineno = false;
2133 new_symbol->symbol.the_bfd = abfd;
2135 return & new_symbol->symbol;
2138 void
2139 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
2141 bfd_symbol_info (symbol, ret);
2143 if (coffsymbol (symbol)->native != NULL
2144 && coffsymbol (symbol)->native->fix_value
2145 && coffsymbol (symbol)->native->is_sym)
2146 ret->value
2147 = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value
2148 - (uintptr_t) obj_raw_syments (abfd))
2149 / sizeof (combined_entry_type));
2152 /* Print out information about COFF symbol. */
2154 void
2155 coff_print_symbol (bfd *abfd,
2156 void * filep,
2157 asymbol *symbol,
2158 bfd_print_symbol_type how)
2160 FILE * file = (FILE *) filep;
2161 const char *symname = (symbol->name != bfd_symbol_error_name
2162 ? symbol->name : _("<corrupt>"));
2164 switch (how)
2166 case bfd_print_symbol_name:
2167 fprintf (file, "%s", symname);
2168 break;
2170 case bfd_print_symbol_more:
2171 fprintf (file, "coff %s %s",
2172 coffsymbol (symbol)->native ? "n" : "g",
2173 coffsymbol (symbol)->lineno ? "l" : " ");
2174 break;
2176 case bfd_print_symbol_all:
2177 if (coffsymbol (symbol)->native)
2179 bfd_vma val;
2180 unsigned int aux;
2181 combined_entry_type *combined = coffsymbol (symbol)->native;
2182 combined_entry_type *root = obj_raw_syments (abfd);
2183 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2185 fprintf (file, "[%3ld]", (long) (combined - root));
2187 /* PR 17512: file: 079-33786-0.001:0.1. */
2188 if (combined < obj_raw_syments (abfd)
2189 || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
2191 fprintf (file, _("<corrupt info> %s"), symname);
2192 break;
2195 BFD_ASSERT (combined->is_sym);
2196 if (! combined->fix_value)
2197 val = (bfd_vma) combined->u.syment.n_value;
2198 else
2199 val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root)
2200 / sizeof (combined_entry_type));
2202 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
2203 combined->u.syment.n_scnum,
2204 combined->u.syment.n_flags,
2205 combined->u.syment.n_type,
2206 combined->u.syment.n_sclass,
2207 combined->u.syment.n_numaux);
2208 bfd_fprintf_vma (abfd, file, val);
2209 fprintf (file, " %s", symname);
2211 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2213 combined_entry_type *auxp = combined + aux + 1;
2214 long tagndx;
2216 BFD_ASSERT (! auxp->is_sym);
2217 if (auxp->fix_tag)
2218 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2219 else
2220 tagndx = auxp->u.auxent.x_sym.x_tagndx.u32;
2222 fprintf (file, "\n");
2224 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2225 continue;
2227 switch (combined->u.syment.n_sclass)
2229 case C_FILE:
2230 fprintf (file, "File ");
2231 /* Add additional information if this isn't the filename
2232 auxiliary entry. */
2233 if (auxp->u.auxent.x_file.x_ftype)
2234 fprintf (file, "ftype %d fname \"%s\"",
2235 auxp->u.auxent.x_file.x_ftype,
2236 (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset);
2237 break;
2239 case C_DWARF:
2240 fprintf (file, "AUX scnlen %#" PRIx64 " nreloc %" PRId64,
2241 auxp->u.auxent.x_sect.x_scnlen,
2242 auxp->u.auxent.x_sect.x_nreloc);
2243 break;
2245 case C_STAT:
2246 if (combined->u.syment.n_type == T_NULL)
2247 /* Probably a section symbol ? */
2249 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2250 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2251 auxp->u.auxent.x_scn.x_nreloc,
2252 auxp->u.auxent.x_scn.x_nlinno);
2253 if (auxp->u.auxent.x_scn.x_checksum != 0
2254 || auxp->u.auxent.x_scn.x_associated != 0
2255 || auxp->u.auxent.x_scn.x_comdat != 0)
2256 fprintf (file, " checksum 0x%x assoc %d comdat %d",
2257 auxp->u.auxent.x_scn.x_checksum,
2258 auxp->u.auxent.x_scn.x_associated,
2259 auxp->u.auxent.x_scn.x_comdat);
2260 break;
2262 /* Fall through. */
2263 case C_EXT:
2264 case C_AIX_WEAKEXT:
2265 if (ISFCN (combined->u.syment.n_type))
2267 long next, llnos;
2269 if (auxp->fix_end)
2270 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2271 - root);
2272 else
2273 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
2274 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2275 fprintf (file,
2276 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2277 tagndx,
2278 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2279 llnos, next);
2280 break;
2282 /* Fall through. */
2283 default:
2284 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2285 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2286 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2287 tagndx);
2288 if (auxp->fix_end)
2289 fprintf (file, " endndx %ld",
2290 ((long)
2291 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2292 - root)));
2293 break;
2297 if (l)
2299 fprintf (file, "\n%s :",
2300 l->u.sym->name != bfd_symbol_error_name
2301 ? l->u.sym->name : _("<corrupt>"));
2302 l++;
2303 while (l->line_number)
2305 if (l->line_number > 0)
2307 fprintf (file, "\n%4d : ", l->line_number);
2308 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2310 l++;
2314 else
2316 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2317 fprintf (file, " %-5s %s %s %s",
2318 symbol->section->name,
2319 coffsymbol (symbol)->native ? "n" : "g",
2320 coffsymbol (symbol)->lineno ? "l" : " ",
2321 symname);
2326 /* Return whether a symbol name implies a local symbol. In COFF,
2327 local symbols generally start with ``.L''. Most targets use this
2328 function for the is_local_label_name entry point, but some may
2329 override it. */
2331 bool
2332 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2333 const char *name)
2335 return name[0] == '.' && name[1] == 'L';
2338 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2339 section, calculate and return the name of the source file and the line
2340 nearest to the wanted location. */
2342 bool
2343 coff_find_nearest_line_with_names (bfd *abfd,
2344 asymbol **symbols,
2345 asection *section,
2346 bfd_vma offset,
2347 const char **filename_ptr,
2348 const char **functionname_ptr,
2349 unsigned int *line_ptr,
2350 const struct dwarf_debug_section *debug_sections)
2352 bool found;
2353 unsigned int i;
2354 unsigned int line_base;
2355 coff_data_type *cof = coff_data (abfd);
2356 /* Run through the raw syments if available. */
2357 combined_entry_type *p;
2358 combined_entry_type *pend;
2359 alent *l;
2360 struct coff_section_tdata *sec_data;
2361 size_t amt;
2363 /* Before looking through the symbol table, try to use a .stab
2364 section to find the information. */
2365 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2366 &found, filename_ptr,
2367 functionname_ptr, line_ptr,
2368 &coff_data(abfd)->line_info))
2369 return false;
2371 if (found)
2372 return true;
2374 /* Also try examining DWARF2 debugging information. */
2375 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2376 filename_ptr, functionname_ptr,
2377 line_ptr, NULL, debug_sections,
2378 &coff_data(abfd)->dwarf2_find_line_info))
2379 return true;
2381 sec_data = coff_section_data (abfd, section);
2383 /* If the DWARF lookup failed, but there is DWARF information available
2384 then the problem might be that the file has been rebased. This tool
2385 changes the VMAs of all the sections, but it does not update the DWARF
2386 information. So try again, using a bias against the address sought. */
2387 if (coff_data (abfd)->dwarf2_find_line_info != NULL)
2389 bfd_signed_vma bias = 0;
2391 /* Create a cache of the result for the next call. */
2392 if (sec_data == NULL && section->owner == abfd)
2394 amt = sizeof (struct coff_section_tdata);
2395 section->used_by_bfd = bfd_zalloc (abfd, amt);
2396 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2399 if (sec_data != NULL && sec_data->saved_bias)
2400 bias = sec_data->bias;
2401 else if (symbols)
2403 bias = _bfd_dwarf2_find_symbol_bias (symbols,
2404 & coff_data (abfd)->dwarf2_find_line_info);
2406 if (sec_data)
2408 sec_data->saved_bias = true;
2409 sec_data->bias = bias;
2413 if (bias
2414 && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
2415 offset + bias,
2416 filename_ptr, functionname_ptr,
2417 line_ptr, NULL, debug_sections,
2418 &coff_data(abfd)->dwarf2_find_line_info))
2419 return true;
2422 *filename_ptr = 0;
2423 *functionname_ptr = 0;
2424 *line_ptr = 0;
2426 /* Don't try and find line numbers in a non coff file. */
2427 if (!bfd_family_coff (abfd))
2428 return false;
2430 if (cof == NULL)
2431 return false;
2433 /* Find the first C_FILE symbol. */
2434 p = cof->raw_syments;
2435 if (!p)
2436 return false;
2438 pend = p + cof->raw_syment_count;
2439 while (p < pend)
2441 BFD_ASSERT (p->is_sym);
2442 if (p->u.syment.n_sclass == C_FILE)
2443 break;
2444 p += 1 + p->u.syment.n_numaux;
2447 if (p < pend)
2449 bfd_vma sec_vma;
2450 bfd_vma maxdiff;
2452 /* Look through the C_FILE symbols to find the best one. */
2453 sec_vma = bfd_section_vma (section);
2454 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2455 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2456 while (1)
2458 bfd_vma file_addr;
2459 combined_entry_type *p2;
2461 for (p2 = p + 1 + p->u.syment.n_numaux;
2462 p2 < pend;
2463 p2 += 1 + p2->u.syment.n_numaux)
2465 BFD_ASSERT (p2->is_sym);
2466 if (p2->u.syment.n_scnum > 0
2467 && (section
2468 == coff_section_from_bfd_index (abfd,
2469 p2->u.syment.n_scnum)))
2470 break;
2471 if (p2->u.syment.n_sclass == C_FILE)
2473 p2 = pend;
2474 break;
2477 if (p2 >= pend)
2478 break;
2480 file_addr = (bfd_vma) p2->u.syment.n_value;
2481 /* PR 11512: Include the section address of the function name symbol. */
2482 if (p2->u.syment.n_scnum > 0)
2483 file_addr += coff_section_from_bfd_index (abfd,
2484 p2->u.syment.n_scnum)->vma;
2485 /* We use <= MAXDIFF here so that if we get a zero length
2486 file, we actually use the next file entry. */
2487 if (p2 < pend
2488 && offset + sec_vma >= file_addr
2489 && offset + sec_vma - file_addr <= maxdiff)
2491 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2492 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2495 if (p->u.syment.n_value >= cof->raw_syment_count)
2496 break;
2498 /* Avoid endless loops on erroneous files by ensuring that
2499 we always move forward in the file. */
2500 if (p >= cof->raw_syments + p->u.syment.n_value)
2501 break;
2503 p = cof->raw_syments + p->u.syment.n_value;
2504 if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
2505 break;
2509 if (section->lineno_count == 0)
2511 *functionname_ptr = NULL;
2512 *line_ptr = 0;
2513 return true;
2516 /* Now wander though the raw linenumbers of the section.
2517 If we have been called on this section before, and the offset
2518 we want is further down then we can prime the lookup loop. */
2519 if (sec_data != NULL
2520 && sec_data->i > 0
2521 && offset >= sec_data->offset)
2523 i = sec_data->i;
2524 *functionname_ptr = sec_data->function;
2525 line_base = sec_data->line_base;
2527 else
2529 i = 0;
2530 line_base = 0;
2533 if (section->lineno != NULL)
2535 bfd_vma last_value = 0;
2537 l = &section->lineno[i];
2539 for (; i < section->lineno_count; i++)
2541 if (l->line_number == 0)
2543 /* Get the symbol this line number points at. */
2544 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2545 if (coff->symbol.value > offset)
2546 break;
2548 *functionname_ptr = coff->symbol.name;
2549 last_value = coff->symbol.value;
2550 if (coff->native)
2552 combined_entry_type *s = coff->native;
2554 BFD_ASSERT (s->is_sym);
2555 s = s + 1 + s->u.syment.n_numaux;
2557 /* In XCOFF a debugging symbol can follow the
2558 function symbol. */
2559 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2560 < obj_raw_syment_count (abfd) * sizeof (*s))
2561 && s->u.syment.n_scnum == N_DEBUG)
2562 s = s + 1 + s->u.syment.n_numaux;
2564 /* S should now point to the .bf of the function. */
2565 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2566 < obj_raw_syment_count (abfd) * sizeof (*s))
2567 && s->u.syment.n_numaux)
2569 /* The linenumber is stored in the auxent. */
2570 union internal_auxent *a = &((s + 1)->u.auxent);
2572 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2573 *line_ptr = line_base;
2577 else
2579 if (l->u.offset > offset)
2580 break;
2581 *line_ptr = l->line_number + line_base - 1;
2583 l++;
2586 /* If we fell off the end of the loop, then assume that this
2587 symbol has no line number info. Otherwise, symbols with no
2588 line number info get reported with the line number of the
2589 last line of the last symbol which does have line number
2590 info. We use 0x100 as a slop to account for cases where the
2591 last line has executable code. */
2592 if (i >= section->lineno_count
2593 && last_value != 0
2594 && offset - last_value > 0x100)
2596 *functionname_ptr = NULL;
2597 *line_ptr = 0;
2601 /* Cache the results for the next call. */
2602 if (sec_data == NULL && section->owner == abfd)
2604 amt = sizeof (struct coff_section_tdata);
2605 section->used_by_bfd = bfd_zalloc (abfd, amt);
2606 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2609 if (sec_data != NULL)
2611 sec_data->offset = offset;
2612 sec_data->i = i - 1;
2613 sec_data->function = *functionname_ptr;
2614 sec_data->line_base = line_base;
2617 return true;
2620 bool
2621 coff_find_nearest_line (bfd *abfd,
2622 asymbol **symbols,
2623 asection *section,
2624 bfd_vma offset,
2625 const char **filename_ptr,
2626 const char **functionname_ptr,
2627 unsigned int *line_ptr,
2628 unsigned int *discriminator_ptr)
2630 if (discriminator_ptr)
2631 *discriminator_ptr = 0;
2632 return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2633 filename_ptr, functionname_ptr,
2634 line_ptr, dwarf_debug_sections);
2637 bool
2638 coff_find_inliner_info (bfd *abfd,
2639 const char **filename_ptr,
2640 const char **functionname_ptr,
2641 unsigned int *line_ptr)
2643 bool found;
2645 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2646 functionname_ptr, line_ptr,
2647 &coff_data(abfd)->dwarf2_find_line_info);
2648 return (found);
2652 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2654 size_t size;
2656 if (!bfd_link_relocatable (info))
2657 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2658 else
2659 size = bfd_coff_filhsz (abfd);
2661 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2662 return size;
2665 /* Change the class of a coff symbol held by BFD. */
2667 bool
2668 bfd_coff_set_symbol_class (bfd * abfd,
2669 asymbol * symbol,
2670 unsigned int symbol_class)
2672 coff_symbol_type * csym;
2674 csym = coff_symbol_from (symbol);
2675 if (csym == NULL)
2677 bfd_set_error (bfd_error_invalid_operation);
2678 return false;
2680 else if (csym->native == NULL)
2682 /* This is an alien symbol which no native coff backend data.
2683 We cheat here by creating a fake native entry for it and
2684 then filling in the class. This code is based on that in
2685 coff_write_alien_symbol(). */
2687 combined_entry_type * native;
2688 size_t amt = sizeof (* native);
2690 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2691 if (native == NULL)
2692 return false;
2694 native->is_sym = true;
2695 native->u.syment.n_type = T_NULL;
2696 native->u.syment.n_sclass = symbol_class;
2698 if (bfd_is_und_section (symbol->section))
2700 native->u.syment.n_scnum = N_UNDEF;
2701 native->u.syment.n_value = symbol->value;
2703 else if (bfd_is_com_section (symbol->section))
2705 native->u.syment.n_scnum = N_UNDEF;
2706 native->u.syment.n_value = symbol->value;
2708 else
2710 native->u.syment.n_scnum =
2711 symbol->section->output_section->target_index;
2712 native->u.syment.n_value = (symbol->value
2713 + symbol->section->output_offset);
2714 if (! obj_pe (abfd))
2715 native->u.syment.n_value += symbol->section->output_section->vma;
2717 /* Copy the any flags from the file header into the symbol.
2718 FIXME: Why? */
2719 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2722 csym->native = native;
2724 else
2725 csym->native->u.syment.n_sclass = symbol_class;
2727 return true;
2730 bool
2731 _bfd_coff_section_already_linked (bfd *abfd,
2732 asection *sec,
2733 struct bfd_link_info *info)
2735 flagword flags;
2736 const char *name, *key;
2737 struct bfd_section_already_linked *l;
2738 struct bfd_section_already_linked_hash_entry *already_linked_list;
2739 struct coff_comdat_info *s_comdat;
2741 if (sec->output_section == bfd_abs_section_ptr)
2742 return false;
2744 flags = sec->flags;
2745 if ((flags & SEC_LINK_ONCE) == 0)
2746 return false;
2748 /* The COFF backend linker doesn't support group sections. */
2749 if ((flags & SEC_GROUP) != 0)
2750 return false;
2752 name = bfd_section_name (sec);
2753 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2755 if (s_comdat != NULL)
2756 key = s_comdat->name;
2757 else
2759 if (startswith (name, ".gnu.linkonce.")
2760 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2761 key++;
2762 else
2763 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2764 .xdata$<key> and .pdata$<key> only the first of which has a
2765 comdat key. Should these all match the LTO IR key? */
2766 key = name;
2769 already_linked_list = bfd_section_already_linked_table_lookup (key);
2771 for (l = already_linked_list->entry; l != NULL; l = l->next)
2773 struct coff_comdat_info *l_comdat;
2775 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2777 /* The section names must match, and both sections must be
2778 comdat and have the same comdat name, or both sections must
2779 be non-comdat. LTO IR plugin sections are an exception. They
2780 are always named .gnu.linkonce.t.<key> (<key> is some string)
2781 and match any comdat section with comdat name of <key>, and
2782 any linkonce section with the same suffix, ie.
2783 .gnu.linkonce.*.<key>. */
2784 if (((s_comdat != NULL) == (l_comdat != NULL)
2785 && strcmp (name, l->sec->name) == 0)
2786 || (l->sec->owner->flags & BFD_PLUGIN) != 0
2787 || (sec->owner->flags & BFD_PLUGIN) != 0)
2789 /* The section has already been linked. See if we should
2790 issue a warning. */
2791 return _bfd_handle_already_linked (sec, l, info);
2795 /* This is the first section with this name. Record it. */
2796 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2797 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2798 return false;
2801 /* Initialize COOKIE for input bfd ABFD. */
2803 static bool
2804 init_reloc_cookie (struct coff_reloc_cookie *cookie,
2805 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2806 bfd *abfd)
2808 /* Sometimes the symbol table does not yet have been loaded here. */
2809 bfd_coff_slurp_symbol_table (abfd);
2811 cookie->abfd = abfd;
2812 cookie->sym_hashes = obj_coff_sym_hashes (abfd);
2814 cookie->symbols = obj_symbols (abfd);
2816 return true;
2819 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
2821 static void
2822 fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED,
2823 bfd *abfd ATTRIBUTE_UNUSED)
2825 /* Nothing to do. */
2828 /* Initialize the relocation information in COOKIE for input section SEC
2829 of input bfd ABFD. */
2831 static bool
2832 init_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2833 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2834 bfd *abfd,
2835 asection *sec)
2837 if (sec->reloc_count == 0)
2839 cookie->rels = NULL;
2840 cookie->relend = NULL;
2841 cookie->rel = NULL;
2842 return true;
2845 cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL,
2846 0, NULL);
2848 if (cookie->rels == NULL)
2849 return false;
2851 cookie->rel = cookie->rels;
2852 cookie->relend = (cookie->rels + sec->reloc_count);
2853 return true;
2856 /* Free the memory allocated by init_reloc_cookie_rels,
2857 if appropriate. */
2859 static void
2860 fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2861 asection *sec)
2863 if (cookie->rels
2864 /* PR 20401. The relocs may not have been cached, so check first.
2865 If the relocs were loaded by init_reloc_cookie_rels() then this
2866 will be the case. FIXME: Would performance be improved if the
2867 relocs *were* cached ? */
2868 && coff_section_data (NULL, sec)
2869 && coff_section_data (NULL, sec)->relocs != cookie->rels)
2870 free (cookie->rels);
2873 /* Initialize the whole of COOKIE for input section SEC. */
2875 static bool
2876 init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2877 struct bfd_link_info *info,
2878 asection *sec)
2880 if (!init_reloc_cookie (cookie, info, sec->owner))
2881 return false;
2883 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
2885 fini_reloc_cookie (cookie, sec->owner);
2886 return false;
2888 return true;
2891 /* Free the memory allocated by init_reloc_cookie_for_section,
2892 if appropriate. */
2894 static void
2895 fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2896 asection *sec)
2898 fini_reloc_cookie_rels (cookie, sec);
2899 fini_reloc_cookie (cookie, sec->owner);
2902 static asection *
2903 _bfd_coff_gc_mark_hook (asection *sec,
2904 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2905 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2906 struct coff_link_hash_entry *h,
2907 struct internal_syment *sym)
2909 if (h != NULL)
2911 switch (h->root.type)
2913 case bfd_link_hash_defined:
2914 case bfd_link_hash_defweak:
2915 return h->root.u.def.section;
2917 case bfd_link_hash_common:
2918 return h->root.u.c.p->section;
2920 case bfd_link_hash_undefweak:
2921 if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
2923 /* PE weak externals. A weak symbol may include an auxiliary
2924 record indicating that if the weak symbol is not resolved,
2925 another external symbol is used instead. */
2926 struct coff_link_hash_entry *h2 =
2927 h->auxbfd->tdata.coff_obj_data->sym_hashes
2928 [h->aux->x_sym.x_tagndx.u32];
2930 if (h2 && h2->root.type != bfd_link_hash_undefined)
2931 return h2->root.u.def.section;
2933 break;
2935 case bfd_link_hash_undefined:
2936 default:
2937 break;
2939 return NULL;
2942 return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
2945 /* COOKIE->rel describes a relocation against section SEC, which is
2946 a section we've decided to keep. Return the section that contains
2947 the relocation symbol, or NULL if no section contains it. */
2949 static asection *
2950 _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
2951 coff_gc_mark_hook_fn gc_mark_hook,
2952 struct coff_reloc_cookie *cookie)
2954 struct coff_link_hash_entry *h;
2956 h = cookie->sym_hashes[cookie->rel->r_symndx];
2957 if (h != NULL)
2959 while (h->root.type == bfd_link_hash_indirect
2960 || h->root.type == bfd_link_hash_warning)
2961 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2963 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
2966 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
2967 &(cookie->symbols
2968 + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment);
2971 static bool _bfd_coff_gc_mark
2972 (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
2974 /* COOKIE->rel describes a relocation against section SEC, which is
2975 a section we've decided to keep. Mark the section that contains
2976 the relocation symbol. */
2978 static bool
2979 _bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
2980 asection *sec,
2981 coff_gc_mark_hook_fn gc_mark_hook,
2982 struct coff_reloc_cookie *cookie)
2984 asection *rsec;
2986 rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
2987 if (rsec && !rsec->gc_mark)
2989 if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
2990 rsec->gc_mark = 1;
2991 else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
2992 return false;
2994 return true;
2997 /* The mark phase of garbage collection. For a given section, mark
2998 it and any sections in this section's group, and all the sections
2999 which define symbols to which it refers. */
3001 static bool
3002 _bfd_coff_gc_mark (struct bfd_link_info *info,
3003 asection *sec,
3004 coff_gc_mark_hook_fn gc_mark_hook)
3006 bool ret = true;
3008 sec->gc_mark = 1;
3010 /* Look through the section relocs. */
3011 if ((sec->flags & SEC_RELOC) != 0
3012 && sec->reloc_count > 0)
3014 struct coff_reloc_cookie cookie;
3016 if (!init_reloc_cookie_for_section (&cookie, info, sec))
3017 ret = false;
3018 else
3020 for (; cookie.rel < cookie.relend; cookie.rel++)
3022 if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
3024 ret = false;
3025 break;
3028 fini_reloc_cookie_for_section (&cookie, sec);
3032 return ret;
3035 static bool
3036 _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
3037 coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
3039 bfd *ibfd;
3041 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3043 asection *isec;
3044 bool some_kept;
3046 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour)
3047 continue;
3049 /* Ensure all linker created sections are kept, and see whether
3050 any other section is already marked. */
3051 some_kept = false;
3052 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3054 if ((isec->flags & SEC_LINKER_CREATED) != 0)
3055 isec->gc_mark = 1;
3056 else if (isec->gc_mark)
3057 some_kept = true;
3060 /* If no section in this file will be kept, then we can
3061 toss out debug sections. */
3062 if (!some_kept)
3063 continue;
3065 /* Keep debug and special sections like .comment when they are
3066 not part of a group, or when we have single-member groups. */
3067 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3068 if ((isec->flags & SEC_DEBUGGING) != 0
3069 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3070 isec->gc_mark = 1;
3072 return true;
3075 /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */
3077 static bool
3078 coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
3079 void *data ATTRIBUTE_UNUSED)
3081 if (h->root.type == bfd_link_hash_warning)
3082 h = (struct coff_link_hash_entry *) h->root.u.i.link;
3084 if ((h->root.type == bfd_link_hash_defined
3085 || h->root.type == bfd_link_hash_defweak)
3086 && !h->root.u.def.section->gc_mark
3087 && !(h->root.u.def.section->owner->flags & DYNAMIC))
3089 /* Do our best to hide the symbol. */
3090 h->root.u.def.section = bfd_und_section_ptr;
3091 h->symbol_class = C_HIDDEN;
3094 return true;
3097 /* The sweep phase of garbage collection. Remove all garbage sections. */
3099 typedef bool (*gc_sweep_hook_fn)
3100 (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
3102 static bool
3103 coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3105 bfd *sub;
3107 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3109 asection *o;
3111 if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3112 continue;
3114 for (o = sub->sections; o != NULL; o = o->next)
3116 /* Keep debug and special sections. */
3117 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
3118 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3119 o->gc_mark = 1;
3120 else if (startswith (o->name, ".idata")
3121 || startswith (o->name, ".pdata")
3122 || startswith (o->name, ".xdata")
3123 || startswith (o->name, ".rsrc"))
3124 o->gc_mark = 1;
3126 if (o->gc_mark)
3127 continue;
3129 /* Skip sweeping sections already excluded. */
3130 if (o->flags & SEC_EXCLUDE)
3131 continue;
3133 /* Since this is early in the link process, it is simple
3134 to remove a section from the output. */
3135 o->flags |= SEC_EXCLUDE;
3137 if (info->print_gc_sections && o->size != 0)
3138 /* xgettext: c-format */
3139 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3140 o, sub);
3142 #if 0
3143 /* But we also have to update some of the relocation
3144 info we collected before. */
3145 if (gc_sweep_hook
3146 && (o->flags & SEC_RELOC) != 0
3147 && o->reloc_count > 0
3148 && !bfd_is_abs_section (o->output_section))
3150 struct internal_reloc *internal_relocs;
3151 bool r;
3153 internal_relocs
3154 = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
3155 info->keep_memory);
3156 if (internal_relocs == NULL)
3157 return false;
3159 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
3161 if (coff_section_data (o)->relocs != internal_relocs)
3162 free (internal_relocs);
3164 if (!r)
3165 return false;
3167 #endif
3171 /* Remove the symbols that were in the swept sections from the dynamic
3172 symbol table. */
3173 coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
3174 NULL);
3176 return true;
3179 /* Keep all sections containing symbols undefined on the command-line,
3180 and the section containing the entry symbol. */
3182 static void
3183 _bfd_coff_gc_keep (struct bfd_link_info *info)
3185 struct bfd_sym_chain *sym;
3187 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
3189 struct coff_link_hash_entry *h;
3191 h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
3192 false, false, false);
3194 if (h != NULL
3195 && (h->root.type == bfd_link_hash_defined
3196 || h->root.type == bfd_link_hash_defweak)
3197 && !bfd_is_abs_section (h->root.u.def.section))
3198 h->root.u.def.section->flags |= SEC_KEEP;
3202 /* Do mark and sweep of unused sections. */
3204 bool
3205 bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3207 bfd *sub;
3209 /* FIXME: Should we implement this? */
3210 #if 0
3211 const bfd_coff_backend_data *bed = coff_backend_info (abfd);
3213 if (!bed->can_gc_sections
3214 || !is_coff_hash_table (info->hash))
3216 _bfd_error_handler(_("warning: gc-sections option ignored"));
3217 return true;
3219 #endif
3221 _bfd_coff_gc_keep (info);
3223 /* Grovel through relocs to find out who stays ... */
3224 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3226 asection *o;
3228 if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3229 continue;
3231 for (o = sub->sections; o != NULL; o = o->next)
3233 if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
3234 || startswith (o->name, ".vectors")
3235 || startswith (o->name, ".ctors")
3236 || startswith (o->name, ".dtors"))
3237 && !o->gc_mark)
3239 if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
3240 return false;
3245 /* Allow the backend to mark additional target specific sections. */
3246 _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
3248 /* ... and mark SEC_EXCLUDE for those that go. */
3249 return coff_gc_sweep (abfd, info);
3252 /* Return name used to identify a comdat group. */
3254 const char *
3255 bfd_coff_group_name (bfd *abfd, const asection *sec)
3257 struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec);
3258 if (ci != NULL)
3259 return ci->name;
3260 return NULL;
3263 bool
3264 _bfd_coff_free_cached_info (bfd *abfd)
3266 struct coff_tdata *tdata;
3268 if (bfd_family_coff (abfd)
3269 && (bfd_get_format (abfd) == bfd_object
3270 || bfd_get_format (abfd) == bfd_core)
3271 && (tdata = coff_data (abfd)) != NULL)
3273 if (tdata->section_by_index)
3275 htab_delete (tdata->section_by_index);
3276 tdata->section_by_index = NULL;
3279 if (tdata->section_by_target_index)
3281 htab_delete (tdata->section_by_target_index);
3282 tdata->section_by_target_index = NULL;
3285 if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
3287 htab_delete (pe_data (abfd)->comdat_hash);
3288 pe_data (abfd)->comdat_hash = NULL;
3291 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
3292 _bfd_stab_cleanup (abfd, &tdata->line_info);
3294 /* PR 25447:
3295 Do not clear the keep_syms and keep_strings flags.
3296 These may have been set by pe_ILF_build_a_bfd() indicating
3297 that the syms and strings pointers are not to be freed. */
3298 _bfd_coff_free_symbols (abfd);
3300 /* Free raw syms, and any other data bfd_alloc'd after raw syms
3301 are read. */
3302 if (!obj_coff_keep_raw_syms (abfd) && obj_raw_syments (abfd))
3304 bfd_release (abfd, obj_raw_syments (abfd));
3305 obj_raw_syments (abfd) = NULL;
3306 obj_symbols (abfd) = NULL;
3307 obj_convert (abfd) = NULL;
3311 return _bfd_generic_bfd_free_cached_info (abfd);