Support x86 Intel MSR_IMM
[binutils-gdb.git] / bfd / coffgen.c
blob5754dbbae15474cc53a80954e5f39541da5df0c3
1 /* Support for the generic parts of COFF, for BFD.
2 Copyright (C) 1990-2024 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 void * tdata_save;
311 bfd_size_type readsize; /* Length of file_info. */
312 unsigned int scnhsz;
313 char *external_sections;
315 if (!(internal_f->f_flags & F_RELFLG))
316 abfd->flags |= HAS_RELOC;
317 if ((internal_f->f_flags & F_EXEC))
318 abfd->flags |= EXEC_P;
319 if (!(internal_f->f_flags & F_LNNO))
320 abfd->flags |= HAS_LINENO;
321 if (!(internal_f->f_flags & F_LSYMS))
322 abfd->flags |= HAS_LOCALS;
324 /* FIXME: How can we set D_PAGED correctly? */
325 if ((internal_f->f_flags & F_EXEC) != 0)
326 abfd->flags |= D_PAGED;
328 abfd->symcount = internal_f->f_nsyms;
329 if (internal_f->f_nsyms)
330 abfd->flags |= HAS_SYMS;
332 if (internal_a != (struct internal_aouthdr *) NULL)
333 abfd->start_address = internal_a->entry;
334 else
335 abfd->start_address = 0;
337 /* Set up the tdata area. ECOFF uses its own routine, and overrides
338 abfd->flags. */
339 tdata_save = abfd->tdata.any;
340 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
341 if (tdata == NULL)
342 goto fail2;
344 scnhsz = bfd_coff_scnhsz (abfd);
345 readsize = (bfd_size_type) nscns * scnhsz;
346 external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
347 if (!external_sections)
348 goto fail;
350 /* Set the arch/mach *before* swapping in sections; section header swapping
351 may depend on arch/mach info. */
352 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
353 goto fail;
355 /* Now copy data as required; construct all asections etc. */
356 if (nscns != 0)
358 unsigned int i;
359 for (i = 0; i < nscns; i++)
361 struct internal_scnhdr tmp;
362 bfd_coff_swap_scnhdr_in (abfd,
363 (void *) (external_sections + i * scnhsz),
364 (void *) & tmp);
365 if (! make_a_section_from_file (abfd, &tmp, i + 1))
366 goto fail;
370 _bfd_coff_free_symbols (abfd);
371 return coff_object_cleanup;
373 fail:
374 coff_object_cleanup (abfd);
375 _bfd_coff_free_symbols (abfd);
376 bfd_release (abfd, tdata);
377 fail2:
378 abfd->tdata.any = tdata_save;
379 abfd->flags = oflags;
380 abfd->start_address = ostart;
381 return NULL;
384 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
385 not a COFF file. This is also used by ECOFF. */
387 bfd_cleanup
388 coff_object_p (bfd *abfd)
390 bfd_size_type filhsz;
391 bfd_size_type aoutsz;
392 unsigned int nscns;
393 void * filehdr;
394 struct internal_filehdr internal_f;
395 struct internal_aouthdr internal_a;
397 /* Figure out how much to read. */
398 filhsz = bfd_coff_filhsz (abfd);
399 aoutsz = bfd_coff_aoutsz (abfd);
401 filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
402 if (filehdr == NULL)
404 if (bfd_get_error () != bfd_error_system_call)
405 bfd_set_error (bfd_error_wrong_format);
406 return NULL;
408 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
409 bfd_release (abfd, filehdr);
411 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
412 (less than aoutsz) used in object files and AOUTSZ (equal to
413 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
414 expects this header to be aoutsz bytes in length, so we use that
415 value in the call to bfd_alloc below. But we must be careful to
416 only read in f_opthdr bytes in the call to bfd_read. We should
417 also attempt to catch corrupt or non-COFF binaries with a strange
418 value for f_opthdr. */
419 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
420 || internal_f.f_opthdr > aoutsz)
422 bfd_set_error (bfd_error_wrong_format);
423 return NULL;
425 nscns = internal_f.f_nscns;
427 if (internal_f.f_opthdr)
429 void * opthdr;
431 opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
432 if (opthdr == NULL)
433 return NULL;
434 /* PR 17512: file: 11056-1136-0.004. */
435 if (internal_f.f_opthdr < aoutsz)
436 memset (((char *) opthdr) + internal_f.f_opthdr, 0,
437 aoutsz - internal_f.f_opthdr);
439 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
440 bfd_release (abfd, opthdr);
443 return coff_real_object_p (abfd, nscns, &internal_f,
444 (internal_f.f_opthdr != 0
445 ? &internal_a
446 : (struct internal_aouthdr *) NULL));
449 static hashval_t
450 htab_hash_section_target_index (const void * entry)
452 const struct bfd_section * sec = entry;
453 return sec->target_index;
456 static int
457 htab_eq_section_target_index (const void * e1, const void * e2)
459 const struct bfd_section * sec1 = e1;
460 const struct bfd_section * sec2 = e2;
461 return sec1->target_index == sec2->target_index;
464 /* Get the BFD section from a COFF symbol section number. */
466 asection *
467 coff_section_from_bfd_index (bfd *abfd, int section_index)
469 if (section_index == N_ABS)
470 return bfd_abs_section_ptr;
471 if (section_index == N_UNDEF)
472 return bfd_und_section_ptr;
473 if (section_index == N_DEBUG)
474 return bfd_abs_section_ptr;
476 struct bfd_section *answer;
477 htab_t table = coff_data (abfd)->section_by_target_index;
479 if (!table)
481 table = htab_create (10, htab_hash_section_target_index,
482 htab_eq_section_target_index, NULL);
483 if (table == NULL)
484 return bfd_und_section_ptr;
485 coff_data (abfd)->section_by_target_index = table;
488 if (htab_elements (table) == 0)
490 for (answer = abfd->sections; answer; answer = answer->next)
492 void **slot = htab_find_slot (table, answer, INSERT);
493 if (slot == NULL)
494 return bfd_und_section_ptr;
495 *slot = answer;
499 struct bfd_section needle;
500 needle.target_index = section_index;
502 answer = htab_find (table, &needle);
503 if (answer != NULL)
504 return answer;
506 /* Cover the unlikely case of sections added after the first call to
507 this function. */
508 for (answer = abfd->sections; answer; answer = answer->next)
509 if (answer->target_index == section_index)
511 void **slot = htab_find_slot (table, answer, INSERT);
512 if (slot != NULL)
513 *slot = answer;
514 return answer;
517 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
518 has a bad symbol table in biglitpow.o. */
519 return bfd_und_section_ptr;
522 /* Get the upper bound of a COFF symbol table. */
524 long
525 coff_get_symtab_upper_bound (bfd *abfd)
527 if (!bfd_coff_slurp_symbol_table (abfd))
528 return -1;
530 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
533 /* Canonicalize a COFF symbol table. */
535 long
536 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
538 unsigned int counter;
539 coff_symbol_type *symbase;
540 coff_symbol_type **location = (coff_symbol_type **) alocation;
542 if (!bfd_coff_slurp_symbol_table (abfd))
543 return -1;
545 symbase = obj_symbols (abfd);
546 counter = bfd_get_symcount (abfd);
547 while (counter-- > 0)
548 *location++ = symbase++;
550 *location = NULL;
552 return bfd_get_symcount (abfd);
555 /* Get the name of a symbol. The caller must pass in a buffer of size
556 >= SYMNMLEN + 1. */
558 const char *
559 _bfd_coff_internal_syment_name (bfd *abfd,
560 const struct internal_syment *sym,
561 char *buf)
563 /* FIXME: It's not clear this will work correctly if sizeof
564 (_n_zeroes) != 4. */
565 if (sym->_n._n_n._n_zeroes != 0
566 || sym->_n._n_n._n_offset == 0)
568 memcpy (buf, sym->_n._n_name, SYMNMLEN);
569 buf[SYMNMLEN] = '\0';
570 return buf;
572 else
574 const char *strings;
576 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
577 strings = obj_coff_strings (abfd);
578 if (strings == NULL)
580 strings = _bfd_coff_read_string_table (abfd);
581 if (strings == NULL)
582 return NULL;
584 if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
585 return NULL;
586 return strings + sym->_n._n_n._n_offset;
590 /* Read in and swap the relocs. This returns a buffer holding the
591 relocs for section SEC in file ABFD. If CACHE is TRUE and
592 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
593 the function is called again. If EXTERNAL_RELOCS is not NULL, it
594 is a buffer large enough to hold the unswapped relocs. If
595 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
596 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
597 value must be INTERNAL_RELOCS. The function returns NULL on error. */
599 struct internal_reloc *
600 _bfd_coff_read_internal_relocs (bfd *abfd,
601 asection *sec,
602 bool cache,
603 bfd_byte *external_relocs,
604 bool require_internal,
605 struct internal_reloc *internal_relocs)
607 bfd_size_type relsz;
608 bfd_byte *free_external = NULL;
609 struct internal_reloc *free_internal = NULL;
610 bfd_byte *erel;
611 bfd_byte *erel_end;
612 struct internal_reloc *irel;
613 bfd_size_type amt;
615 if (sec->reloc_count == 0)
616 return internal_relocs; /* Nothing to do. */
618 if (coff_section_data (abfd, sec) != NULL
619 && coff_section_data (abfd, sec)->relocs != NULL)
621 if (! require_internal)
622 return coff_section_data (abfd, sec)->relocs;
623 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
624 sec->reloc_count * sizeof (struct internal_reloc));
625 return internal_relocs;
628 relsz = bfd_coff_relsz (abfd);
630 amt = sec->reloc_count * relsz;
631 if (external_relocs == NULL)
633 free_external = (bfd_byte *) bfd_malloc (amt);
634 if (free_external == NULL)
635 goto error_return;
636 external_relocs = free_external;
639 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
640 || bfd_read (external_relocs, amt, abfd) != amt)
641 goto error_return;
643 if (internal_relocs == NULL)
645 amt = sec->reloc_count;
646 amt *= sizeof (struct internal_reloc);
647 free_internal = (struct internal_reloc *) bfd_malloc (amt);
648 if (free_internal == NULL)
649 goto error_return;
650 internal_relocs = free_internal;
653 /* Swap in the relocs. */
654 erel = external_relocs;
655 erel_end = erel + relsz * sec->reloc_count;
656 irel = internal_relocs;
657 for (; erel < erel_end; erel += relsz, irel++)
658 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
660 free (free_external);
661 free_external = NULL;
663 if (cache && free_internal != NULL)
665 if (coff_section_data (abfd, sec) == NULL)
667 amt = sizeof (struct coff_section_tdata);
668 sec->used_by_bfd = bfd_zalloc (abfd, amt);
669 if (sec->used_by_bfd == NULL)
670 goto error_return;
671 coff_section_data (abfd, sec)->contents = NULL;
673 coff_section_data (abfd, sec)->relocs = free_internal;
676 return internal_relocs;
678 error_return:
679 free (free_external);
680 free (free_internal);
681 return NULL;
684 /* Set lineno_count for the output sections of a COFF file. */
687 coff_count_linenumbers (bfd *abfd)
689 unsigned int limit = bfd_get_symcount (abfd);
690 unsigned int i;
691 int total = 0;
692 asymbol **p;
693 asection *s;
695 if (limit == 0)
697 /* This may be from the backend linker, in which case the
698 lineno_count in the sections is correct. */
699 for (s = abfd->sections; s != NULL; s = s->next)
700 total += s->lineno_count;
701 return total;
704 for (s = abfd->sections; s != NULL; s = s->next)
705 BFD_ASSERT (s->lineno_count == 0);
707 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
709 asymbol *q_maybe = *p;
711 if (bfd_asymbol_bfd (q_maybe) != NULL
712 && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
714 coff_symbol_type *q = coffsymbol (q_maybe);
716 /* The AIX 4.1 compiler can sometimes generate line numbers
717 attached to debugging symbols. We try to simply ignore
718 those here. */
719 if (q->lineno != NULL
720 && q->symbol.section->owner != NULL)
722 /* This symbol has line numbers. Increment the owning
723 section's linenumber count. */
724 alent *l = q->lineno;
728 asection * sec = q->symbol.section->output_section;
730 /* Do not try to update fields in read-only sections. */
731 if (! bfd_is_const_section (sec))
732 sec->lineno_count ++;
734 ++total;
735 ++l;
737 while (l->line_number != 0);
742 return total;
745 static void
746 fixup_symbol_value (bfd *abfd,
747 coff_symbol_type *coff_symbol_ptr,
748 struct internal_syment *syment)
750 /* Normalize the symbol flags. */
751 if (coff_symbol_ptr->symbol.section
752 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
754 /* A common symbol is undefined with a value. */
755 syment->n_scnum = N_UNDEF;
756 syment->n_value = coff_symbol_ptr->symbol.value;
758 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
759 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
761 syment->n_value = coff_symbol_ptr->symbol.value;
763 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
765 syment->n_scnum = N_UNDEF;
766 syment->n_value = 0;
768 /* FIXME: Do we need to handle the absolute section here? */
769 else
771 if (coff_symbol_ptr->symbol.section)
773 syment->n_scnum =
774 coff_symbol_ptr->symbol.section->output_section->target_index;
776 syment->n_value = (coff_symbol_ptr->symbol.value
777 + coff_symbol_ptr->symbol.section->output_offset);
778 if (! obj_pe (abfd))
780 syment->n_value += (syment->n_sclass == C_STATLAB)
781 ? coff_symbol_ptr->symbol.section->output_section->lma
782 : coff_symbol_ptr->symbol.section->output_section->vma;
785 else
787 BFD_ASSERT (0);
788 /* This can happen, but I don't know why yet (steve@cygnus.com) */
789 syment->n_scnum = N_ABS;
790 syment->n_value = coff_symbol_ptr->symbol.value;
795 /* Run through all the symbols in the symbol table and work out what
796 their indexes into the symbol table will be when output.
798 Coff requires that each C_FILE symbol points to the next one in the
799 chain, and that the last one points to the first external symbol. We
800 do that here too. */
802 bool
803 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
805 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
806 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
807 unsigned int native_index = 0;
808 struct internal_syment *last_file = NULL;
809 unsigned int symbol_index;
811 /* COFF demands that undefined symbols come after all other symbols.
812 Since we don't need to impose this extra knowledge on all our
813 client programs, deal with that here. Sort the symbol table;
814 just move the undefined symbols to the end, leaving the rest
815 alone. The O'Reilly book says that defined global symbols come
816 at the end before the undefined symbols, so we do that here as
817 well. */
818 /* @@ Do we have some condition we could test for, so we don't always
819 have to do this? I don't think relocatability is quite right, but
820 I'm not certain. [raeburn:19920508.1711EST] */
822 asymbol **newsyms;
823 unsigned int i;
824 bfd_size_type amt;
826 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
827 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
828 if (!newsyms)
829 return false;
830 bfd_ptr->outsymbols = newsyms;
831 for (i = 0; i < symbol_count; i++)
832 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
833 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
834 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
835 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
836 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
837 == 0))))
838 *newsyms++ = symbol_ptr_ptr[i];
840 for (i = 0; i < symbol_count; i++)
841 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
842 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
843 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
844 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
845 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
846 != 0))))
847 *newsyms++ = symbol_ptr_ptr[i];
849 *first_undef = newsyms - bfd_ptr->outsymbols;
851 for (i = 0; i < symbol_count; i++)
852 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
853 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
854 *newsyms++ = symbol_ptr_ptr[i];
855 *newsyms = (asymbol *) NULL;
856 symbol_ptr_ptr = bfd_ptr->outsymbols;
859 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
861 coff_symbol_type *coff_symbol_ptr;
863 coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
864 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
865 if (coff_symbol_ptr && coff_symbol_ptr->native)
867 combined_entry_type *s = coff_symbol_ptr->native;
868 int i;
870 BFD_ASSERT (s->is_sym);
871 if (s->u.syment.n_sclass == C_FILE)
873 if (last_file != NULL)
874 last_file->n_value = native_index;
875 last_file = &(s->u.syment);
877 else
878 /* Modify the symbol values according to their section and
879 type. */
880 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
882 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
883 s[i].offset = native_index++;
885 else
886 native_index++;
889 obj_conv_table_size (bfd_ptr) = native_index;
891 return true;
894 /* Run thorough the symbol table again, and fix it so that all
895 pointers to entries are changed to the entries' index in the output
896 symbol table. */
898 void
899 coff_mangle_symbols (bfd *bfd_ptr)
901 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
902 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
903 unsigned int symbol_index;
905 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
907 coff_symbol_type *coff_symbol_ptr;
909 coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
910 if (coff_symbol_ptr && coff_symbol_ptr->native)
912 int i;
913 combined_entry_type *s = coff_symbol_ptr->native;
915 BFD_ASSERT (s->is_sym);
916 if (s->fix_value)
918 /* FIXME: We should use a union here. */
919 s->u.syment.n_value =
920 (uintptr_t) ((combined_entry_type *)
921 (uintptr_t) s->u.syment.n_value)->offset;
922 s->fix_value = 0;
924 if (s->fix_line)
926 /* The value is the offset into the line number entries
927 for the symbol's section. On output, the symbol's
928 section should be N_DEBUG. */
929 s->u.syment.n_value =
930 (coff_symbol_ptr->symbol.section->output_section->line_filepos
931 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
932 coff_symbol_ptr->symbol.section =
933 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
934 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
936 for (i = 0; i < s->u.syment.n_numaux; i++)
938 combined_entry_type *a = s + i + 1;
940 BFD_ASSERT (! a->is_sym);
941 if (a->fix_tag)
943 a->u.auxent.x_sym.x_tagndx.u32 =
944 a->u.auxent.x_sym.x_tagndx.p->offset;
945 a->fix_tag = 0;
947 if (a->fix_end)
949 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
950 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
951 a->fix_end = 0;
953 if (a->fix_scnlen)
955 a->u.auxent.x_csect.x_scnlen.u64 =
956 a->u.auxent.x_csect.x_scnlen.p->offset;
957 a->fix_scnlen = 0;
964 static bool
965 coff_write_auxent_fname (bfd *abfd,
966 char *str,
967 union internal_auxent *auxent,
968 struct bfd_strtab_hash *strtab,
969 bool hash)
971 unsigned int str_length = strlen (str);
972 unsigned int filnmlen = bfd_coff_filnmlen (abfd);
974 if (bfd_coff_long_filenames (abfd))
976 if (str_length <= filnmlen)
977 strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
978 else
980 bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
982 if (indx == (bfd_size_type) -1)
983 return false;
985 auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
986 auxent->x_file.x_n.x_n.x_zeroes = 0;
989 else
991 strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
992 if (str_length > filnmlen)
993 str[filnmlen] = '\0';
996 return true;
999 static bool
1000 coff_fix_symbol_name (bfd *abfd,
1001 asymbol *symbol,
1002 combined_entry_type *native,
1003 struct bfd_strtab_hash *strtab,
1004 bool hash,
1005 asection **debug_string_section_p,
1006 bfd_size_type *debug_string_size_p)
1008 unsigned int name_length;
1009 char *name = (char *) (symbol->name);
1010 bfd_size_type indx;
1012 if (name == NULL)
1014 /* COFF symbols always have names, so we'll make one up. */
1015 symbol->name = "strange";
1016 name = (char *) symbol->name;
1018 name_length = strlen (name);
1020 BFD_ASSERT (native->is_sym);
1021 if (native->u.syment.n_sclass == C_FILE
1022 && native->u.syment.n_numaux > 0)
1024 if (bfd_coff_force_symnames_in_strings (abfd))
1026 indx = _bfd_stringtab_add (strtab, ".file", hash, false);
1027 if (indx == (bfd_size_type) -1)
1028 return false;
1030 native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1031 native->u.syment._n._n_n._n_zeroes = 0;
1033 else
1034 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
1036 BFD_ASSERT (! (native + 1)->is_sym);
1037 if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
1038 strtab, hash))
1039 return false;
1041 else
1043 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
1044 /* This name will fit into the symbol neatly. */
1045 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
1047 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
1049 indx = _bfd_stringtab_add (strtab, name, hash, false);
1050 if (indx == (bfd_size_type) -1)
1051 return false;
1053 native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1054 native->u.syment._n._n_n._n_zeroes = 0;
1056 else
1058 file_ptr filepos;
1059 bfd_byte buf[4];
1060 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
1062 /* This name should be written into the .debug section. For
1063 some reason each name is preceded by a two byte length
1064 and also followed by a null byte. FIXME: We assume that
1065 the .debug section has already been created, and that it
1066 is large enough. */
1067 if (*debug_string_section_p == (asection *) NULL)
1068 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
1069 filepos = bfd_tell (abfd);
1070 if (prefix_len == 4)
1071 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
1072 else
1073 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
1075 if (!bfd_set_section_contents (abfd,
1076 *debug_string_section_p,
1077 (void *) buf,
1078 (file_ptr) *debug_string_size_p,
1079 (bfd_size_type) prefix_len)
1080 || !bfd_set_section_contents (abfd,
1081 *debug_string_section_p,
1082 (void *) symbol->name,
1083 (file_ptr) (*debug_string_size_p
1084 + prefix_len),
1085 (bfd_size_type) name_length + 1))
1086 abort ();
1087 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1088 abort ();
1089 native->u.syment._n._n_n._n_offset =
1090 *debug_string_size_p + prefix_len;
1091 native->u.syment._n._n_n._n_zeroes = 0;
1092 *debug_string_size_p += name_length + 1 + prefix_len;
1096 return true;
1099 /* We need to keep track of the symbol index so that when we write out
1100 the relocs we can get the index for a symbol. This method is a
1101 hack. FIXME. */
1103 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
1105 /* Write a symbol out to a COFF file. */
1107 static bool
1108 coff_write_symbol (bfd *abfd,
1109 asymbol *symbol,
1110 combined_entry_type *native,
1111 bfd_vma *written,
1112 struct bfd_strtab_hash *strtab,
1113 bool hash,
1114 asection **debug_string_section_p,
1115 bfd_size_type *debug_string_size_p)
1117 unsigned int numaux = native->u.syment.n_numaux;
1118 int type = native->u.syment.n_type;
1119 int n_sclass = (int) native->u.syment.n_sclass;
1120 asection *output_section = symbol->section->output_section
1121 ? symbol->section->output_section
1122 : symbol->section;
1123 void * buf;
1124 bfd_size_type symesz;
1126 BFD_ASSERT (native->is_sym);
1128 if (native->u.syment.n_sclass == C_FILE)
1129 symbol->flags |= BSF_DEBUGGING;
1131 if (symbol->flags & BSF_DEBUGGING
1132 && bfd_is_abs_section (symbol->section))
1133 native->u.syment.n_scnum = N_DEBUG;
1135 else if (bfd_is_abs_section (symbol->section))
1136 native->u.syment.n_scnum = N_ABS;
1138 else if (bfd_is_und_section (symbol->section))
1139 native->u.syment.n_scnum = N_UNDEF;
1141 else
1142 native->u.syment.n_scnum =
1143 output_section->target_index;
1145 if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
1146 debug_string_section_p, debug_string_size_p))
1147 return false;
1149 symesz = bfd_coff_symesz (abfd);
1150 buf = bfd_alloc (abfd, symesz);
1151 if (!buf)
1152 return false;
1153 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1154 if (bfd_write (buf, symesz, abfd) != symesz)
1155 return false;
1156 bfd_release (abfd, buf);
1158 if (native->u.syment.n_numaux > 0)
1160 bfd_size_type auxesz;
1161 unsigned int j;
1163 auxesz = bfd_coff_auxesz (abfd);
1164 buf = bfd_alloc (abfd, auxesz);
1165 if (!buf)
1166 return false;
1167 for (j = 0; j < native->u.syment.n_numaux; j++)
1169 BFD_ASSERT (! (native + j + 1)->is_sym);
1171 /* Adjust auxent only if this isn't the filename
1172 auxiliary entry. */
1173 if (native->u.syment.n_sclass == C_FILE
1174 && (native + j + 1)->u.auxent.x_file.x_ftype
1175 && (native + j + 1)->extrap)
1176 coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap,
1177 &(native + j + 1)->u.auxent, strtab, hash);
1179 bfd_coff_swap_aux_out (abfd,
1180 &((native + j + 1)->u.auxent),
1181 type, n_sclass, (int) j,
1182 native->u.syment.n_numaux,
1183 buf);
1184 if (bfd_write (buf, auxesz, abfd) != auxesz)
1185 return false;
1187 bfd_release (abfd, buf);
1190 /* Store the index for use when we write out the relocs. */
1191 set_index (symbol, *written);
1193 *written += numaux + 1;
1194 return true;
1197 /* Write out a symbol to a COFF file that does not come from a COFF
1198 file originally. This symbol may have been created by the linker,
1199 or we may be linking a non COFF file to a COFF file. */
1201 bool
1202 coff_write_alien_symbol (bfd *abfd,
1203 asymbol *symbol,
1204 struct internal_syment *isym,
1205 bfd_vma *written,
1206 struct bfd_strtab_hash *strtab,
1207 bool hash,
1208 asection **debug_string_section_p,
1209 bfd_size_type *debug_string_size_p)
1211 combined_entry_type *native;
1212 combined_entry_type dummy[2];
1213 asection *output_section = symbol->section->output_section
1214 ? symbol->section->output_section
1215 : symbol->section;
1216 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1217 bool ret;
1219 if ((!link_info || link_info->strip_discarded)
1220 && !bfd_is_abs_section (symbol->section)
1221 && symbol->section->output_section == bfd_abs_section_ptr)
1223 symbol->name = "";
1224 if (isym != NULL)
1225 memset (isym, 0, sizeof (*isym));
1226 return true;
1228 memset (dummy, 0, sizeof dummy);
1229 native = dummy;
1230 native->is_sym = true;
1231 native[1].is_sym = false;
1232 native->u.syment.n_type = T_NULL;
1233 native->u.syment.n_flags = 0;
1234 native->u.syment.n_numaux = 0;
1235 if (bfd_is_und_section (symbol->section))
1237 native->u.syment.n_scnum = N_UNDEF;
1238 native->u.syment.n_value = symbol->value;
1240 else if (bfd_is_com_section (symbol->section))
1242 native->u.syment.n_scnum = N_UNDEF;
1243 native->u.syment.n_value = symbol->value;
1245 else if (symbol->flags & BSF_FILE)
1247 native->u.syment.n_scnum = N_DEBUG;
1248 native->u.syment.n_numaux = 1;
1250 else if (symbol->flags & BSF_DEBUGGING)
1252 /* There isn't much point to writing out a debugging symbol
1253 unless we are prepared to convert it into COFF debugging
1254 format. So, we just ignore them. We must clobber the symbol
1255 name to keep it from being put in the string table. */
1256 symbol->name = "";
1257 if (isym != NULL)
1258 memset (isym, 0, sizeof (*isym));
1259 return true;
1261 else
1263 native->u.syment.n_scnum = output_section->target_index;
1264 native->u.syment.n_value = (symbol->value
1265 + symbol->section->output_offset);
1266 if (! obj_pe (abfd))
1267 native->u.syment.n_value += output_section->vma;
1269 /* Copy the any flags from the file header into the symbol.
1270 FIXME: Why? */
1272 coff_symbol_type *c = coff_symbol_from (symbol);
1273 if (c != (coff_symbol_type *) NULL)
1274 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1278 native->u.syment.n_type = 0;
1279 if (symbol->flags & BSF_FILE)
1280 native->u.syment.n_sclass = C_FILE;
1281 else if (symbol->flags & BSF_LOCAL)
1282 native->u.syment.n_sclass = C_STAT;
1283 else if (symbol->flags & BSF_WEAK)
1284 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1285 else
1286 native->u.syment.n_sclass = C_EXT;
1288 ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash,
1289 debug_string_section_p, debug_string_size_p);
1290 if (isym != NULL)
1291 *isym = native->u.syment;
1292 return ret;
1295 /* Write a native symbol to a COFF file. */
1297 static bool
1298 coff_write_native_symbol (bfd *abfd,
1299 coff_symbol_type *symbol,
1300 bfd_vma *written,
1301 struct bfd_strtab_hash *strtab,
1302 asection **debug_string_section_p,
1303 bfd_size_type *debug_string_size_p)
1305 combined_entry_type *native = symbol->native;
1306 alent *lineno = symbol->lineno;
1307 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1309 if ((!link_info || link_info->strip_discarded)
1310 && !bfd_is_abs_section (symbol->symbol.section)
1311 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1313 symbol->symbol.name = "";
1314 return true;
1317 BFD_ASSERT (native->is_sym);
1318 /* If this symbol has an associated line number, we must store the
1319 symbol index in the line number field. We also tag the auxent to
1320 point to the right place in the lineno table. */
1321 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1323 unsigned int count = 0;
1325 lineno[count].u.offset = *written;
1326 if (native->u.syment.n_numaux)
1328 union internal_auxent *a = &((native + 1)->u.auxent);
1330 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1331 symbol->symbol.section->output_section->moving_line_filepos;
1334 /* Count and relocate all other linenumbers. */
1335 count++;
1336 while (lineno[count].line_number != 0)
1338 lineno[count].u.offset +=
1339 (symbol->symbol.section->output_section->vma
1340 + symbol->symbol.section->output_offset);
1341 count++;
1343 symbol->done_lineno = true;
1345 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1346 symbol->symbol.section->output_section->moving_line_filepos +=
1347 count * bfd_coff_linesz (abfd);
1350 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1351 strtab, true, debug_string_section_p,
1352 debug_string_size_p);
1355 static void
1356 null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
1357 va_list ap ATTRIBUTE_UNUSED)
1361 /* Write out the COFF symbols. */
1363 bool
1364 coff_write_symbols (bfd *abfd)
1366 struct bfd_strtab_hash *strtab;
1367 asection *debug_string_section;
1368 bfd_size_type debug_string_size;
1369 unsigned int i;
1370 unsigned int limit = bfd_get_symcount (abfd);
1371 bfd_vma written = 0;
1372 asymbol **p;
1374 debug_string_section = NULL;
1375 debug_string_size = 0;
1377 strtab = _bfd_stringtab_init ();
1378 if (strtab == NULL)
1379 return false;
1381 /* If this target supports long section names, they must be put into
1382 the string table. This is supported by PE. This code must
1383 handle section names just as they are handled in
1384 coff_write_object_contents. This is why we pass hash as FALSE below. */
1385 if (bfd_coff_long_section_names (abfd))
1387 asection *o;
1389 for (o = abfd->sections; o != NULL; o = o->next)
1390 if (strlen (o->name) > SCNNMLEN
1391 && _bfd_stringtab_add (strtab, o->name, false, false)
1392 == (bfd_size_type) -1)
1393 return false;
1396 /* Seek to the right place. */
1397 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1398 return false;
1400 /* Output all the symbols we have. */
1401 written = 0;
1402 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1404 asymbol *symbol = *p;
1405 coff_symbol_type *c_symbol = coff_symbol_from (symbol);
1407 if (c_symbol == (coff_symbol_type *) NULL
1408 || c_symbol->native == (combined_entry_type *) NULL)
1410 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1411 strtab, true, &debug_string_section,
1412 &debug_string_size))
1413 return false;
1415 else
1417 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1419 bfd_error_handler_type current_error_handler;
1420 enum coff_symbol_classification sym_class;
1421 unsigned char *n_sclass;
1423 /* Suppress error reporting by bfd_coff_classify_symbol.
1424 Error messages can be generated when we are processing a local
1425 symbol which has no associated section and we do not have to
1426 worry about this, all we need to know is that it is local. */
1427 current_error_handler = bfd_set_error_handler (null_error_handler);
1428 BFD_ASSERT (c_symbol->native->is_sym);
1429 sym_class = bfd_coff_classify_symbol (abfd,
1430 &c_symbol->native->u.syment);
1431 (void) bfd_set_error_handler (current_error_handler);
1433 n_sclass = &c_symbol->native->u.syment.n_sclass;
1435 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1436 we cannot retain the existing sclass from the original symbol.
1437 Weak symbols only have one valid sclass, so just set it always.
1438 If it is not local class and should be, set it C_STAT.
1439 If it is global and not classified as global, or if it is
1440 weak (which is also classified as global), set it C_EXT. */
1442 if (symbol->flags & BSF_WEAK)
1443 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1444 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1445 *n_sclass = C_STAT;
1446 else if (symbol->flags & BSF_GLOBAL
1447 && (sym_class != COFF_SYMBOL_GLOBAL
1448 #ifdef COFF_WITH_PE
1449 || *n_sclass == C_NT_WEAK
1450 #endif
1451 || *n_sclass == C_WEAKEXT))
1452 c_symbol->native->u.syment.n_sclass = C_EXT;
1455 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1456 strtab, &debug_string_section,
1457 &debug_string_size))
1458 return false;
1462 obj_raw_syment_count (abfd) = written;
1464 /* Now write out strings.
1466 We would normally not write anything here if there are no strings, but
1467 we'll write out 4 so that any stupid coff reader which tries to read the
1468 string table even when there isn't one won't croak. */
1470 bfd_byte buffer[STRING_SIZE_SIZE];
1472 #if STRING_SIZE_SIZE == 4
1473 H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
1474 #else
1475 #error Change H_PUT_32
1476 #endif
1477 if (bfd_write (buffer, sizeof (buffer), abfd) != sizeof (buffer))
1478 return false;
1480 if (! _bfd_stringtab_emit (abfd, strtab))
1481 return false;
1484 _bfd_stringtab_free (strtab);
1486 /* Make sure the .debug section was created to be the correct size.
1487 We should create it ourselves on the fly, but we don't because
1488 BFD won't let us write to any section until we know how large all
1489 the sections are. We could still do it by making another pass
1490 over the symbols. FIXME. */
1491 BFD_ASSERT (debug_string_size == 0
1492 || (debug_string_section != (asection *) NULL
1493 && (BFD_ALIGN (debug_string_size,
1494 1 << debug_string_section->alignment_power)
1495 == debug_string_section->size)));
1497 return true;
1500 bool
1501 coff_write_linenumbers (bfd *abfd)
1503 asection *s;
1504 bfd_size_type linesz;
1505 void * buff;
1507 linesz = bfd_coff_linesz (abfd);
1508 buff = bfd_alloc (abfd, linesz);
1509 if (!buff)
1510 return false;
1511 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1513 if (s->lineno_count)
1515 asymbol **q = abfd->outsymbols;
1516 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1517 return false;
1518 /* Find all the linenumbers in this section. */
1519 while (*q)
1521 asymbol *p = *q;
1522 if (p->section->output_section == s)
1524 alent *l =
1525 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1526 (bfd_asymbol_bfd (p), p));
1527 if (l)
1529 /* Found a linenumber entry, output. */
1530 struct internal_lineno out;
1532 memset ((void *) & out, 0, sizeof (out));
1533 out.l_lnno = 0;
1534 out.l_addr.l_symndx = l->u.offset;
1535 bfd_coff_swap_lineno_out (abfd, &out, buff);
1536 if (bfd_write (buff, linesz, abfd) != linesz)
1537 return false;
1538 l++;
1539 while (l->line_number)
1541 out.l_lnno = l->line_number;
1542 out.l_addr.l_symndx = l->u.offset;
1543 bfd_coff_swap_lineno_out (abfd, &out, buff);
1544 if (bfd_write (buff, linesz, abfd) != linesz)
1545 return false;
1546 l++;
1550 q++;
1554 bfd_release (abfd, buff);
1555 return true;
1558 alent *
1559 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1561 return coffsymbol (symbol)->lineno;
1564 /* This function transforms the offsets into the symbol table into
1565 pointers to syments. */
1567 static void
1568 coff_pointerize_aux (bfd *abfd,
1569 combined_entry_type *table_base,
1570 combined_entry_type *symbol,
1571 unsigned int indaux,
1572 combined_entry_type *auxent)
1574 unsigned int type = symbol->u.syment.n_type;
1575 unsigned int n_sclass = symbol->u.syment.n_sclass;
1577 BFD_ASSERT (symbol->is_sym);
1578 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1580 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1581 (abfd, table_base, symbol, indaux, auxent))
1582 return;
1585 /* Don't bother if this is a file or a section. */
1586 if (n_sclass == C_STAT && type == T_NULL)
1587 return;
1588 if (n_sclass == C_FILE)
1589 return;
1590 if (n_sclass == C_DWARF)
1591 return;
1593 BFD_ASSERT (! auxent->is_sym);
1594 /* Otherwise patch up. */
1595 #define N_TMASK coff_data (abfd)->local_n_tmask
1596 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1598 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1599 || n_sclass == C_FCN)
1600 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 > 0
1601 && (auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32
1602 < obj_raw_syment_count (abfd)))
1604 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1605 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
1606 auxent->fix_end = 1;
1609 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1610 generate one, so we must be careful to ignore it. */
1611 if (auxent->u.auxent.x_sym.x_tagndx.u32 < obj_raw_syment_count (abfd))
1613 auxent->u.auxent.x_sym.x_tagndx.p =
1614 table_base + auxent->u.auxent.x_sym.x_tagndx.u32;
1615 auxent->fix_tag = 1;
1619 /* Allocate space for the ".debug" section, and read it.
1620 We did not read the debug section until now, because
1621 we didn't want to go to the trouble until someone needed it. */
1623 static char *
1624 build_debug_section (bfd *abfd, asection ** sect_return)
1626 char *debug_section;
1627 file_ptr position;
1628 bfd_size_type sec_size;
1630 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1632 if (!sect)
1634 bfd_set_error (bfd_error_no_debug_section);
1635 return NULL;
1638 /* Seek to the beginning of the `.debug' section and read it.
1639 Save the current position first; it is needed by our caller.
1640 Then read debug section and reset the file pointer. */
1642 position = bfd_tell (abfd);
1643 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
1644 return NULL;
1646 sec_size = sect->size;
1647 debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size + 1, sec_size);
1648 if (debug_section == NULL)
1649 return NULL;
1650 debug_section[sec_size] = 0;
1652 if (bfd_seek (abfd, position, SEEK_SET) != 0)
1653 return NULL;
1655 * sect_return = sect;
1656 return debug_section;
1659 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1660 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1661 be \0-terminated. */
1663 static char *
1664 copy_name (bfd *abfd, char *name, size_t maxlen)
1666 size_t len;
1667 char *newname;
1669 for (len = 0; len < maxlen; ++len)
1670 if (name[len] == '\0')
1671 break;
1673 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1674 return NULL;
1676 strncpy (newname, name, len);
1677 newname[len] = '\0';
1678 return newname;
1681 /* Read in the external symbols. */
1683 bool
1684 _bfd_coff_get_external_symbols (bfd *abfd)
1686 size_t symesz;
1687 size_t size;
1688 void * syms;
1689 ufile_ptr filesize;
1691 if (obj_coff_external_syms (abfd) != NULL)
1692 return true;
1694 symesz = bfd_coff_symesz (abfd);
1695 if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
1697 bfd_set_error (bfd_error_file_truncated);
1698 return false;
1701 if (size == 0)
1702 return true;
1704 filesize = bfd_get_file_size (abfd);
1705 if (filesize != 0
1706 && ((ufile_ptr) obj_sym_filepos (abfd) > filesize
1707 || size > filesize - obj_sym_filepos (abfd)))
1709 bfd_set_error (bfd_error_file_truncated);
1710 return false;
1713 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1714 return false;
1715 syms = _bfd_malloc_and_read (abfd, size, size);
1716 obj_coff_external_syms (abfd) = syms;
1717 return syms != NULL;
1720 /* Read in the external strings. The strings are not loaded until
1721 they are needed. This is because we have no simple way of
1722 detecting a missing string table in an archive. If the strings
1723 are loaded then the STRINGS and STRINGS_LEN fields in the
1724 coff_tdata structure will be set. */
1726 const char *
1727 _bfd_coff_read_string_table (bfd *abfd)
1729 char extstrsize[STRING_SIZE_SIZE];
1730 bfd_size_type strsize;
1731 char *strings;
1732 ufile_ptr pos;
1733 ufile_ptr filesize;
1734 size_t symesz;
1735 size_t size;
1737 if (obj_coff_strings (abfd) != NULL)
1738 return obj_coff_strings (abfd);
1740 if (obj_sym_filepos (abfd) == 0)
1742 bfd_set_error (bfd_error_no_symbols);
1743 return NULL;
1746 symesz = bfd_coff_symesz (abfd);
1747 pos = obj_sym_filepos (abfd);
1748 if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
1749 || pos + size < pos)
1751 bfd_set_error (bfd_error_file_truncated);
1752 return NULL;
1755 if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
1756 return NULL;
1758 if (bfd_read (extstrsize, sizeof extstrsize, abfd) != sizeof extstrsize)
1760 if (bfd_get_error () != bfd_error_file_truncated)
1761 return NULL;
1763 /* There is no string table. */
1764 strsize = STRING_SIZE_SIZE;
1766 else
1768 #if STRING_SIZE_SIZE == 4
1769 strsize = H_GET_32 (abfd, extstrsize);
1770 #else
1771 #error Change H_GET_32
1772 #endif
1775 filesize = bfd_get_file_size (abfd);
1776 if (strsize < STRING_SIZE_SIZE
1777 || (filesize != 0 && strsize > filesize))
1779 _bfd_error_handler
1780 /* xgettext: c-format */
1781 (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
1782 bfd_set_error (bfd_error_bad_value);
1783 return NULL;
1786 strings = (char *) bfd_malloc (strsize + 1);
1787 if (strings == NULL)
1788 return NULL;
1790 /* PR 17521 file: 079-54929-0.004.
1791 A corrupt file could contain an index that points into the first
1792 STRING_SIZE_SIZE bytes of the string table, so make sure that
1793 they are zero. */
1794 memset (strings, 0, STRING_SIZE_SIZE);
1796 if (bfd_read (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1797 != strsize - STRING_SIZE_SIZE)
1799 free (strings);
1800 return NULL;
1803 obj_coff_strings (abfd) = strings;
1804 obj_coff_strings_len (abfd) = strsize;
1805 /* Terminate the string table, just in case. */
1806 strings[strsize] = 0;
1807 return strings;
1810 /* Free up the external symbols and strings read from a COFF file. */
1812 bool
1813 _bfd_coff_free_symbols (bfd *abfd)
1815 if (! bfd_family_coff (abfd))
1816 return false;
1818 if (obj_coff_external_syms (abfd) != NULL
1819 && ! obj_coff_keep_syms (abfd))
1821 free (obj_coff_external_syms (abfd));
1822 obj_coff_external_syms (abfd) = NULL;
1825 if (obj_coff_strings (abfd) != NULL
1826 && ! obj_coff_keep_strings (abfd))
1828 free (obj_coff_strings (abfd));
1829 obj_coff_strings (abfd) = NULL;
1830 obj_coff_strings_len (abfd) = 0;
1833 return true;
1836 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1837 knit the symbol names into a normalized form. By normalized here I
1838 mean that all symbols have an n_offset pointer that points to a null-
1839 terminated string. */
1841 combined_entry_type *
1842 coff_get_normalized_symtab (bfd *abfd)
1844 combined_entry_type *internal;
1845 combined_entry_type *internal_ptr;
1846 size_t symesz;
1847 char *raw_src;
1848 char *raw_end;
1849 const char *string_table = NULL;
1850 asection * debug_sec = NULL;
1851 char *debug_sec_data = NULL;
1852 bfd_size_type size;
1854 if (obj_raw_syments (abfd) != NULL)
1855 return obj_raw_syments (abfd);
1857 if (! _bfd_coff_get_external_symbols (abfd))
1858 return NULL;
1860 size = obj_raw_syment_count (abfd);
1861 /* Check for integer overflow. */
1862 if (size > (bfd_size_type) -1 / sizeof (combined_entry_type))
1863 return NULL;
1864 size *= sizeof (combined_entry_type);
1865 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1866 if (internal == NULL && size != 0)
1867 return NULL;
1869 raw_src = (char *) obj_coff_external_syms (abfd);
1871 /* Mark the end of the symbols. */
1872 symesz = bfd_coff_symesz (abfd);
1873 raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz);
1875 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1876 probably possible. If one shows up, it will probably kill us. */
1878 /* Swap all the raw entries. */
1879 for (internal_ptr = internal;
1880 raw_src < raw_end;
1881 raw_src += symesz, internal_ptr++)
1883 unsigned int i;
1885 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1886 (void *) & internal_ptr->u.syment);
1887 internal_ptr->is_sym = true;
1888 combined_entry_type *sym = internal_ptr;
1890 /* PR 17512: Prevent buffer overrun. */
1891 if (sym->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
1892 return NULL;
1894 for (i = 0; i < sym->u.syment.n_numaux; i++)
1896 internal_ptr++;
1897 raw_src += symesz;
1899 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1900 sym->u.syment.n_type,
1901 sym->u.syment.n_sclass,
1902 (int) i, sym->u.syment.n_numaux,
1903 &(internal_ptr->u.auxent));
1905 internal_ptr->is_sym = false;
1906 coff_pointerize_aux (abfd, internal, sym, i, internal_ptr);
1909 if (sym->u.syment.n_sclass == C_FILE
1910 && sym->u.syment.n_numaux > 0)
1912 combined_entry_type * aux = sym + 1;
1914 /* Make a file symbol point to the name in the auxent, since
1915 the text ".file" is redundant. */
1916 BFD_ASSERT (! aux->is_sym);
1918 if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1920 /* The filename is a long one, point into the string table. */
1921 if (string_table == NULL)
1923 string_table = _bfd_coff_read_string_table (abfd);
1924 if (string_table == NULL)
1925 return NULL;
1928 if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1929 >= obj_coff_strings_len (abfd))
1930 sym->u.syment._n._n_n._n_offset =
1931 (uintptr_t) bfd_symbol_error_name;
1932 else
1933 sym->u.syment._n._n_n._n_offset =
1934 (uintptr_t) (string_table
1935 + aux->u.auxent.x_file.x_n.x_n.x_offset);
1937 else
1939 /* Ordinary short filename, put into memory anyway. The
1940 Microsoft PE tools sometimes store a filename in
1941 multiple AUX entries. */
1942 size_t len;
1943 char *src;
1944 if (sym->u.syment.n_numaux > 1 && obj_pe (abfd))
1946 len = sym->u.syment.n_numaux * symesz;
1947 src = raw_src - (len - symesz);
1949 else
1951 len = bfd_coff_filnmlen (abfd);
1952 src = aux->u.auxent.x_file.x_n.x_fname;
1954 sym->u.syment._n._n_n._n_offset =
1955 (uintptr_t) copy_name (abfd, src, len);
1958 /* Normalize other strings available in C_FILE aux entries. */
1959 if (!obj_pe (abfd))
1960 for (int numaux = 1;
1961 numaux < sym->u.syment.n_numaux;
1962 numaux++)
1964 aux = sym + numaux + 1;
1965 BFD_ASSERT (! aux->is_sym);
1967 if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1969 /* The string information is a long one, point
1970 into the string table. */
1971 if (string_table == NULL)
1973 string_table = _bfd_coff_read_string_table (abfd);
1974 if (string_table == NULL)
1975 return NULL;
1978 if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1979 >= obj_coff_strings_len (abfd))
1980 aux->u.auxent.x_file.x_n.x_n.x_offset =
1981 (uintptr_t) bfd_symbol_error_name;
1982 else
1983 aux->u.auxent.x_file.x_n.x_n.x_offset =
1984 (uintptr_t) (string_table
1985 + aux->u.auxent.x_file.x_n.x_n.x_offset);
1987 else
1988 aux->u.auxent.x_file.x_n.x_n.x_offset =
1989 ((uintptr_t)
1990 copy_name (abfd,
1991 aux->u.auxent.x_file.x_n.x_fname,
1992 bfd_coff_filnmlen (abfd)));
1996 else
1998 if (sym->u.syment._n._n_n._n_zeroes != 0)
2000 /* This is a "short" name. Make it long. */
2001 char *newstring;
2003 /* Find the length of this string without walking into memory
2004 that isn't ours. */
2005 for (i = 0; i < SYMNMLEN; ++i)
2006 if (sym->u.syment._n._n_name[i] == '\0')
2007 break;
2009 newstring = bfd_alloc (abfd, i + 1);
2010 if (newstring == NULL)
2011 return NULL;
2012 memcpy (newstring, sym->u.syment._n._n_name, i);
2013 newstring[i] = 0;
2014 sym->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
2015 sym->u.syment._n._n_n._n_zeroes = 0;
2017 else if (sym->u.syment._n._n_n._n_offset == 0)
2018 sym->u.syment._n._n_n._n_offset = (uintptr_t) "";
2019 else if (!bfd_coff_symname_in_debug (abfd, &sym->u.syment))
2021 /* Long name already. Point symbol at the string in the
2022 table. */
2023 if (string_table == NULL)
2025 string_table = _bfd_coff_read_string_table (abfd);
2026 if (string_table == NULL)
2027 return NULL;
2029 if (sym->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd))
2030 sym->u.syment._n._n_n._n_offset =
2031 (uintptr_t) bfd_symbol_error_name;
2032 else
2033 sym->u.syment._n._n_n._n_offset =
2034 (uintptr_t) (string_table
2035 + sym->u.syment._n._n_n._n_offset);
2037 else
2039 /* Long name in debug section. Very similar. */
2040 if (debug_sec_data == NULL)
2042 debug_sec_data = build_debug_section (abfd, &debug_sec);
2043 if (debug_sec_data == NULL)
2044 return NULL;
2046 /* PR binutils/17512: Catch out of range offsets into
2047 the debug data. */
2048 if (sym->u.syment._n._n_n._n_offset >= debug_sec->size)
2049 sym->u.syment._n._n_n._n_offset =
2050 (uintptr_t) bfd_symbol_error_name;
2051 else
2052 sym->u.syment._n._n_n._n_offset =
2053 (uintptr_t) (debug_sec_data
2054 + sym->u.syment._n._n_n._n_offset);
2059 /* Free the raw symbols. */
2060 if (obj_coff_external_syms (abfd) != NULL
2061 && ! obj_coff_keep_syms (abfd))
2063 free (obj_coff_external_syms (abfd));
2064 obj_coff_external_syms (abfd) = NULL;
2067 obj_raw_syments (abfd) = internal;
2068 BFD_ASSERT (obj_raw_syment_count (abfd)
2069 == (size_t) (internal_ptr - internal));
2071 return internal;
2074 long
2075 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
2077 size_t count, raw;
2079 count = asect->reloc_count;
2080 if (count >= LONG_MAX / sizeof (arelent *)
2081 || _bfd_mul_overflow (count, bfd_coff_relsz (abfd), &raw))
2083 bfd_set_error (bfd_error_file_too_big);
2084 return -1;
2086 if (!bfd_write_p (abfd))
2088 ufile_ptr filesize = bfd_get_file_size (abfd);
2089 if (filesize != 0 && raw > filesize)
2091 bfd_set_error (bfd_error_file_truncated);
2092 return -1;
2095 return (count + 1) * sizeof (arelent *);
2098 asymbol *
2099 coff_make_empty_symbol (bfd *abfd)
2101 size_t amt = sizeof (coff_symbol_type);
2102 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
2104 if (new_symbol == NULL)
2105 return NULL;
2106 new_symbol->symbol.section = 0;
2107 new_symbol->native = NULL;
2108 new_symbol->lineno = NULL;
2109 new_symbol->done_lineno = false;
2110 new_symbol->symbol.the_bfd = abfd;
2112 return & new_symbol->symbol;
2115 /* Make a debugging symbol. */
2117 asymbol *
2118 coff_bfd_make_debug_symbol (bfd *abfd)
2120 size_t amt = sizeof (coff_symbol_type);
2121 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
2123 if (new_symbol == NULL)
2124 return NULL;
2125 /* @@ The 10 is a guess at a plausible maximum number of aux entries
2126 (but shouldn't be a constant). */
2127 amt = sizeof (combined_entry_type) * 10;
2128 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2129 if (!new_symbol->native)
2130 return NULL;
2131 new_symbol->native->is_sym = true;
2132 new_symbol->symbol.section = bfd_abs_section_ptr;
2133 new_symbol->symbol.flags = BSF_DEBUGGING;
2134 new_symbol->lineno = NULL;
2135 new_symbol->done_lineno = false;
2136 new_symbol->symbol.the_bfd = abfd;
2138 return & new_symbol->symbol;
2141 void
2142 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
2144 bfd_symbol_info (symbol, ret);
2146 if (coffsymbol (symbol)->native != NULL
2147 && coffsymbol (symbol)->native->fix_value
2148 && coffsymbol (symbol)->native->is_sym)
2149 ret->value
2150 = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value
2151 - (uintptr_t) obj_raw_syments (abfd))
2152 / sizeof (combined_entry_type));
2155 /* Print out information about COFF symbol. */
2157 void
2158 coff_print_symbol (bfd *abfd,
2159 void * filep,
2160 asymbol *symbol,
2161 bfd_print_symbol_type how)
2163 FILE * file = (FILE *) filep;
2164 const char *symname = (symbol->name != bfd_symbol_error_name
2165 ? symbol->name : _("<corrupt>"));
2167 switch (how)
2169 case bfd_print_symbol_name:
2170 fprintf (file, "%s", symname);
2171 break;
2173 case bfd_print_symbol_more:
2174 fprintf (file, "coff %s %s",
2175 coffsymbol (symbol)->native ? "n" : "g",
2176 coffsymbol (symbol)->lineno ? "l" : " ");
2177 break;
2179 case bfd_print_symbol_all:
2180 if (coffsymbol (symbol)->native)
2182 bfd_vma val;
2183 unsigned int aux;
2184 combined_entry_type *combined = coffsymbol (symbol)->native;
2185 combined_entry_type *root = obj_raw_syments (abfd);
2186 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2188 fprintf (file, "[%3ld]", (long) (combined - root));
2190 /* PR 17512: file: 079-33786-0.001:0.1. */
2191 if (combined < obj_raw_syments (abfd)
2192 || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
2194 fprintf (file, _("<corrupt info> %s"), symname);
2195 break;
2198 BFD_ASSERT (combined->is_sym);
2199 if (! combined->fix_value)
2200 val = (bfd_vma) combined->u.syment.n_value;
2201 else
2202 val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root)
2203 / sizeof (combined_entry_type));
2205 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
2206 combined->u.syment.n_scnum,
2207 combined->u.syment.n_flags,
2208 combined->u.syment.n_type,
2209 combined->u.syment.n_sclass,
2210 combined->u.syment.n_numaux);
2211 bfd_fprintf_vma (abfd, file, val);
2212 fprintf (file, " %s", symname);
2214 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2216 combined_entry_type *auxp = combined + aux + 1;
2217 long tagndx;
2219 BFD_ASSERT (! auxp->is_sym);
2220 if (auxp->fix_tag)
2221 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2222 else
2223 tagndx = auxp->u.auxent.x_sym.x_tagndx.u32;
2225 fprintf (file, "\n");
2227 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2228 continue;
2230 switch (combined->u.syment.n_sclass)
2232 case C_FILE:
2233 fprintf (file, "File ");
2234 /* Add additional information if this isn't the filename
2235 auxiliary entry. */
2236 if (auxp->u.auxent.x_file.x_ftype)
2237 fprintf (file, "ftype %d fname \"%s\"",
2238 auxp->u.auxent.x_file.x_ftype,
2239 (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset);
2240 break;
2242 case C_DWARF:
2243 fprintf (file, "AUX scnlen %#" PRIx64 " nreloc %" PRId64,
2244 auxp->u.auxent.x_sect.x_scnlen,
2245 auxp->u.auxent.x_sect.x_nreloc);
2246 break;
2248 case C_STAT:
2249 if (combined->u.syment.n_type == T_NULL)
2250 /* Probably a section symbol ? */
2252 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2253 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2254 auxp->u.auxent.x_scn.x_nreloc,
2255 auxp->u.auxent.x_scn.x_nlinno);
2256 if (auxp->u.auxent.x_scn.x_checksum != 0
2257 || auxp->u.auxent.x_scn.x_associated != 0
2258 || auxp->u.auxent.x_scn.x_comdat != 0)
2259 fprintf (file, " checksum 0x%x assoc %d comdat %d",
2260 auxp->u.auxent.x_scn.x_checksum,
2261 auxp->u.auxent.x_scn.x_associated,
2262 auxp->u.auxent.x_scn.x_comdat);
2263 break;
2265 /* Fall through. */
2266 case C_EXT:
2267 case C_AIX_WEAKEXT:
2268 if (ISFCN (combined->u.syment.n_type))
2270 long next, llnos;
2272 if (auxp->fix_end)
2273 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2274 - root);
2275 else
2276 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
2277 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2278 fprintf (file,
2279 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2280 tagndx,
2281 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2282 llnos, next);
2283 break;
2285 /* Fall through. */
2286 default:
2287 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2288 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2289 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2290 tagndx);
2291 if (auxp->fix_end)
2292 fprintf (file, " endndx %ld",
2293 ((long)
2294 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2295 - root)));
2296 break;
2300 if (l)
2302 fprintf (file, "\n%s :",
2303 l->u.sym->name != bfd_symbol_error_name
2304 ? l->u.sym->name : _("<corrupt>"));
2305 l++;
2306 while (l->line_number)
2308 if (l->line_number > 0)
2310 fprintf (file, "\n%4d : ", l->line_number);
2311 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2313 l++;
2317 else
2319 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2320 fprintf (file, " %-5s %s %s %s",
2321 symbol->section->name,
2322 coffsymbol (symbol)->native ? "n" : "g",
2323 coffsymbol (symbol)->lineno ? "l" : " ",
2324 symname);
2329 /* Return whether a symbol name implies a local symbol. In COFF,
2330 local symbols generally start with ``.L''. Most targets use this
2331 function for the is_local_label_name entry point, but some may
2332 override it. */
2334 bool
2335 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2336 const char *name)
2338 return name[0] == '.' && name[1] == 'L';
2341 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2342 section, calculate and return the name of the source file and the line
2343 nearest to the wanted location. */
2345 bool
2346 coff_find_nearest_line_with_names (bfd *abfd,
2347 asymbol **symbols,
2348 asection *section,
2349 bfd_vma offset,
2350 const char **filename_ptr,
2351 const char **functionname_ptr,
2352 unsigned int *line_ptr,
2353 const struct dwarf_debug_section *debug_sections)
2355 bool found;
2356 unsigned int i;
2357 unsigned int line_base;
2358 coff_data_type *cof = coff_data (abfd);
2359 /* Run through the raw syments if available. */
2360 combined_entry_type *p;
2361 combined_entry_type *pend;
2362 alent *l;
2363 struct coff_section_tdata *sec_data;
2364 size_t amt;
2366 /* Before looking through the symbol table, try to use a .stab
2367 section to find the information. */
2368 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2369 &found, filename_ptr,
2370 functionname_ptr, line_ptr,
2371 &coff_data(abfd)->line_info))
2372 return false;
2374 if (found)
2375 return true;
2377 /* Also try examining DWARF2 debugging information. */
2378 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2379 filename_ptr, functionname_ptr,
2380 line_ptr, NULL, debug_sections,
2381 &coff_data(abfd)->dwarf2_find_line_info))
2382 return true;
2384 sec_data = coff_section_data (abfd, section);
2386 /* If the DWARF lookup failed, but there is DWARF information available
2387 then the problem might be that the file has been rebased. This tool
2388 changes the VMAs of all the sections, but it does not update the DWARF
2389 information. So try again, using a bias against the address sought. */
2390 if (coff_data (abfd)->dwarf2_find_line_info != NULL)
2392 bfd_signed_vma bias = 0;
2394 /* Create a cache of the result for the next call. */
2395 if (sec_data == NULL && section->owner == abfd)
2397 amt = sizeof (struct coff_section_tdata);
2398 section->used_by_bfd = bfd_zalloc (abfd, amt);
2399 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2402 if (sec_data != NULL && sec_data->saved_bias)
2403 bias = sec_data->bias;
2404 else if (symbols)
2406 bias = _bfd_dwarf2_find_symbol_bias (symbols,
2407 & coff_data (abfd)->dwarf2_find_line_info);
2409 if (sec_data)
2411 sec_data->saved_bias = true;
2412 sec_data->bias = bias;
2416 if (bias
2417 && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
2418 offset + bias,
2419 filename_ptr, functionname_ptr,
2420 line_ptr, NULL, debug_sections,
2421 &coff_data(abfd)->dwarf2_find_line_info))
2422 return true;
2425 *filename_ptr = 0;
2426 *functionname_ptr = 0;
2427 *line_ptr = 0;
2429 /* Don't try and find line numbers in a non coff file. */
2430 if (!bfd_family_coff (abfd))
2431 return false;
2433 if (cof == NULL)
2434 return false;
2436 /* Find the first C_FILE symbol. */
2437 p = cof->raw_syments;
2438 if (!p)
2439 return false;
2441 pend = p + cof->raw_syment_count;
2442 while (p < pend)
2444 BFD_ASSERT (p->is_sym);
2445 if (p->u.syment.n_sclass == C_FILE)
2446 break;
2447 p += 1 + p->u.syment.n_numaux;
2450 if (p < pend)
2452 bfd_vma sec_vma;
2453 bfd_vma maxdiff;
2455 /* Look through the C_FILE symbols to find the best one. */
2456 sec_vma = bfd_section_vma (section);
2457 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2458 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2459 while (1)
2461 bfd_vma file_addr;
2462 combined_entry_type *p2;
2464 for (p2 = p + 1 + p->u.syment.n_numaux;
2465 p2 < pend;
2466 p2 += 1 + p2->u.syment.n_numaux)
2468 BFD_ASSERT (p2->is_sym);
2469 if (p2->u.syment.n_scnum > 0
2470 && (section
2471 == coff_section_from_bfd_index (abfd,
2472 p2->u.syment.n_scnum)))
2473 break;
2474 if (p2->u.syment.n_sclass == C_FILE)
2476 p2 = pend;
2477 break;
2480 if (p2 >= pend)
2481 break;
2483 file_addr = (bfd_vma) p2->u.syment.n_value;
2484 /* PR 11512: Include the section address of the function name symbol. */
2485 if (p2->u.syment.n_scnum > 0)
2486 file_addr += coff_section_from_bfd_index (abfd,
2487 p2->u.syment.n_scnum)->vma;
2488 /* We use <= MAXDIFF here so that if we get a zero length
2489 file, we actually use the next file entry. */
2490 if (p2 < pend
2491 && offset + sec_vma >= file_addr
2492 && offset + sec_vma - file_addr <= maxdiff)
2494 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2495 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2498 if (p->u.syment.n_value >= cof->raw_syment_count)
2499 break;
2501 /* Avoid endless loops on erroneous files by ensuring that
2502 we always move forward in the file. */
2503 if (p >= cof->raw_syments + p->u.syment.n_value)
2504 break;
2506 p = cof->raw_syments + p->u.syment.n_value;
2507 if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
2508 break;
2512 if (section->lineno_count == 0)
2514 *functionname_ptr = NULL;
2515 *line_ptr = 0;
2516 return true;
2519 /* Now wander though the raw linenumbers of the section.
2520 If we have been called on this section before, and the offset
2521 we want is further down then we can prime the lookup loop. */
2522 if (sec_data != NULL
2523 && sec_data->i > 0
2524 && offset >= sec_data->offset)
2526 i = sec_data->i;
2527 *functionname_ptr = sec_data->function;
2528 line_base = sec_data->line_base;
2530 else
2532 i = 0;
2533 line_base = 0;
2536 if (section->lineno != NULL)
2538 bfd_vma last_value = 0;
2540 l = &section->lineno[i];
2542 for (; i < section->lineno_count; i++)
2544 if (l->line_number == 0)
2546 /* Get the symbol this line number points at. */
2547 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2548 if (coff->symbol.value > offset)
2549 break;
2551 *functionname_ptr = coff->symbol.name;
2552 last_value = coff->symbol.value;
2553 if (coff->native)
2555 combined_entry_type *s = coff->native;
2557 BFD_ASSERT (s->is_sym);
2558 s = s + 1 + s->u.syment.n_numaux;
2560 /* In XCOFF a debugging symbol can follow the
2561 function symbol. */
2562 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2563 < obj_raw_syment_count (abfd) * sizeof (*s))
2564 && s->u.syment.n_scnum == N_DEBUG)
2565 s = s + 1 + s->u.syment.n_numaux;
2567 /* S should now point to the .bf of the function. */
2568 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2569 < obj_raw_syment_count (abfd) * sizeof (*s))
2570 && s->u.syment.n_numaux)
2572 /* The linenumber is stored in the auxent. */
2573 union internal_auxent *a = &((s + 1)->u.auxent);
2575 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2576 *line_ptr = line_base;
2580 else
2582 if (l->u.offset > offset)
2583 break;
2584 *line_ptr = l->line_number + line_base - 1;
2586 l++;
2589 /* If we fell off the end of the loop, then assume that this
2590 symbol has no line number info. Otherwise, symbols with no
2591 line number info get reported with the line number of the
2592 last line of the last symbol which does have line number
2593 info. We use 0x100 as a slop to account for cases where the
2594 last line has executable code. */
2595 if (i >= section->lineno_count
2596 && last_value != 0
2597 && offset - last_value > 0x100)
2599 *functionname_ptr = NULL;
2600 *line_ptr = 0;
2604 /* Cache the results for the next call. */
2605 if (sec_data == NULL && section->owner == abfd)
2607 amt = sizeof (struct coff_section_tdata);
2608 section->used_by_bfd = bfd_zalloc (abfd, amt);
2609 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2612 if (sec_data != NULL)
2614 sec_data->offset = offset;
2615 sec_data->i = i - 1;
2616 sec_data->function = *functionname_ptr;
2617 sec_data->line_base = line_base;
2620 return true;
2623 bool
2624 coff_find_nearest_line (bfd *abfd,
2625 asymbol **symbols,
2626 asection *section,
2627 bfd_vma offset,
2628 const char **filename_ptr,
2629 const char **functionname_ptr,
2630 unsigned int *line_ptr,
2631 unsigned int *discriminator_ptr)
2633 if (discriminator_ptr)
2634 *discriminator_ptr = 0;
2635 return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2636 filename_ptr, functionname_ptr,
2637 line_ptr, dwarf_debug_sections);
2640 bool
2641 coff_find_inliner_info (bfd *abfd,
2642 const char **filename_ptr,
2643 const char **functionname_ptr,
2644 unsigned int *line_ptr)
2646 bool found;
2648 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2649 functionname_ptr, line_ptr,
2650 &coff_data(abfd)->dwarf2_find_line_info);
2651 return (found);
2655 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2657 size_t size;
2659 if (!bfd_link_relocatable (info))
2660 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2661 else
2662 size = bfd_coff_filhsz (abfd);
2664 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2665 return size;
2668 /* Change the class of a coff symbol held by BFD. */
2670 bool
2671 bfd_coff_set_symbol_class (bfd * abfd,
2672 asymbol * symbol,
2673 unsigned int symbol_class)
2675 coff_symbol_type * csym;
2677 csym = coff_symbol_from (symbol);
2678 if (csym == NULL)
2680 bfd_set_error (bfd_error_invalid_operation);
2681 return false;
2683 else if (csym->native == NULL)
2685 /* This is an alien symbol which no native coff backend data.
2686 We cheat here by creating a fake native entry for it and
2687 then filling in the class. This code is based on that in
2688 coff_write_alien_symbol(). */
2690 combined_entry_type * native;
2691 size_t amt = sizeof (* native);
2693 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2694 if (native == NULL)
2695 return false;
2697 native->is_sym = true;
2698 native->u.syment.n_type = T_NULL;
2699 native->u.syment.n_sclass = symbol_class;
2701 if (bfd_is_und_section (symbol->section))
2703 native->u.syment.n_scnum = N_UNDEF;
2704 native->u.syment.n_value = symbol->value;
2706 else if (bfd_is_com_section (symbol->section))
2708 native->u.syment.n_scnum = N_UNDEF;
2709 native->u.syment.n_value = symbol->value;
2711 else
2713 native->u.syment.n_scnum =
2714 symbol->section->output_section->target_index;
2715 native->u.syment.n_value = (symbol->value
2716 + symbol->section->output_offset);
2717 if (! obj_pe (abfd))
2718 native->u.syment.n_value += symbol->section->output_section->vma;
2720 /* Copy the any flags from the file header into the symbol.
2721 FIXME: Why? */
2722 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2725 csym->native = native;
2727 else
2728 csym->native->u.syment.n_sclass = symbol_class;
2730 return true;
2733 bool
2734 _bfd_coff_section_already_linked (bfd *abfd,
2735 asection *sec,
2736 struct bfd_link_info *info)
2738 flagword flags;
2739 const char *name, *key;
2740 struct bfd_section_already_linked *l;
2741 struct bfd_section_already_linked_hash_entry *already_linked_list;
2742 struct coff_comdat_info *s_comdat;
2744 if (sec->output_section == bfd_abs_section_ptr)
2745 return false;
2747 flags = sec->flags;
2748 if ((flags & SEC_LINK_ONCE) == 0)
2749 return false;
2751 /* The COFF backend linker doesn't support group sections. */
2752 if ((flags & SEC_GROUP) != 0)
2753 return false;
2755 name = bfd_section_name (sec);
2756 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2758 if (s_comdat != NULL)
2759 key = s_comdat->name;
2760 else
2762 if (startswith (name, ".gnu.linkonce.")
2763 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2764 key++;
2765 else
2766 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2767 .xdata$<key> and .pdata$<key> only the first of which has a
2768 comdat key. Should these all match the LTO IR key? */
2769 key = name;
2772 already_linked_list = bfd_section_already_linked_table_lookup (key);
2774 for (l = already_linked_list->entry; l != NULL; l = l->next)
2776 struct coff_comdat_info *l_comdat;
2778 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2780 /* The section names must match, and both sections must be
2781 comdat and have the same comdat name, or both sections must
2782 be non-comdat. LTO IR plugin sections are an exception. They
2783 are always named .gnu.linkonce.t.<key> (<key> is some string)
2784 and match any comdat section with comdat name of <key>, and
2785 any linkonce section with the same suffix, ie.
2786 .gnu.linkonce.*.<key>. */
2787 if (((s_comdat != NULL) == (l_comdat != NULL)
2788 && strcmp (name, l->sec->name) == 0)
2789 || (l->sec->owner->flags & BFD_PLUGIN) != 0
2790 || (sec->owner->flags & BFD_PLUGIN) != 0)
2792 /* The section has already been linked. See if we should
2793 issue a warning. */
2794 return _bfd_handle_already_linked (sec, l, info);
2798 /* This is the first section with this name. Record it. */
2799 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2800 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2801 return false;
2804 /* Initialize COOKIE for input bfd ABFD. */
2806 static bool
2807 init_reloc_cookie (struct coff_reloc_cookie *cookie,
2808 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2809 bfd *abfd)
2811 /* Sometimes the symbol table does not yet have been loaded here. */
2812 bfd_coff_slurp_symbol_table (abfd);
2814 cookie->abfd = abfd;
2815 cookie->sym_hashes = obj_coff_sym_hashes (abfd);
2817 cookie->symbols = obj_symbols (abfd);
2819 return true;
2822 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
2824 static void
2825 fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED,
2826 bfd *abfd ATTRIBUTE_UNUSED)
2828 /* Nothing to do. */
2831 /* Initialize the relocation information in COOKIE for input section SEC
2832 of input bfd ABFD. */
2834 static bool
2835 init_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2836 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2837 bfd *abfd,
2838 asection *sec)
2840 if (sec->reloc_count == 0)
2842 cookie->rels = NULL;
2843 cookie->relend = NULL;
2844 cookie->rel = NULL;
2845 return true;
2848 cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL,
2849 0, NULL);
2851 if (cookie->rels == NULL)
2852 return false;
2854 cookie->rel = cookie->rels;
2855 cookie->relend = (cookie->rels + sec->reloc_count);
2856 return true;
2859 /* Free the memory allocated by init_reloc_cookie_rels,
2860 if appropriate. */
2862 static void
2863 fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2864 asection *sec)
2866 if (cookie->rels
2867 /* PR 20401. The relocs may not have been cached, so check first.
2868 If the relocs were loaded by init_reloc_cookie_rels() then this
2869 will be the case. FIXME: Would performance be improved if the
2870 relocs *were* cached ? */
2871 && coff_section_data (NULL, sec)
2872 && coff_section_data (NULL, sec)->relocs != cookie->rels)
2873 free (cookie->rels);
2876 /* Initialize the whole of COOKIE for input section SEC. */
2878 static bool
2879 init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2880 struct bfd_link_info *info,
2881 asection *sec)
2883 if (!init_reloc_cookie (cookie, info, sec->owner))
2884 return false;
2886 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
2888 fini_reloc_cookie (cookie, sec->owner);
2889 return false;
2891 return true;
2894 /* Free the memory allocated by init_reloc_cookie_for_section,
2895 if appropriate. */
2897 static void
2898 fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2899 asection *sec)
2901 fini_reloc_cookie_rels (cookie, sec);
2902 fini_reloc_cookie (cookie, sec->owner);
2905 static asection *
2906 _bfd_coff_gc_mark_hook (asection *sec,
2907 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2908 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2909 struct coff_link_hash_entry *h,
2910 struct internal_syment *sym)
2912 if (h != NULL)
2914 switch (h->root.type)
2916 case bfd_link_hash_defined:
2917 case bfd_link_hash_defweak:
2918 return h->root.u.def.section;
2920 case bfd_link_hash_common:
2921 return h->root.u.c.p->section;
2923 case bfd_link_hash_undefweak:
2924 if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
2926 /* PE weak externals. A weak symbol may include an auxiliary
2927 record indicating that if the weak symbol is not resolved,
2928 another external symbol is used instead. */
2929 struct coff_link_hash_entry *h2 =
2930 h->auxbfd->tdata.coff_obj_data->sym_hashes
2931 [h->aux->x_sym.x_tagndx.u32];
2933 if (h2 && h2->root.type != bfd_link_hash_undefined)
2934 return h2->root.u.def.section;
2936 break;
2938 case bfd_link_hash_undefined:
2939 default:
2940 break;
2942 return NULL;
2945 return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
2948 /* COOKIE->rel describes a relocation against section SEC, which is
2949 a section we've decided to keep. Return the section that contains
2950 the relocation symbol, or NULL if no section contains it. */
2952 static asection *
2953 _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
2954 coff_gc_mark_hook_fn gc_mark_hook,
2955 struct coff_reloc_cookie *cookie)
2957 struct coff_link_hash_entry *h;
2959 h = cookie->sym_hashes[cookie->rel->r_symndx];
2960 if (h != NULL)
2962 while (h->root.type == bfd_link_hash_indirect
2963 || h->root.type == bfd_link_hash_warning)
2964 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2966 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
2969 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
2970 &(cookie->symbols
2971 + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment);
2974 static bool _bfd_coff_gc_mark
2975 (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
2977 /* COOKIE->rel describes a relocation against section SEC, which is
2978 a section we've decided to keep. Mark the section that contains
2979 the relocation symbol. */
2981 static bool
2982 _bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
2983 asection *sec,
2984 coff_gc_mark_hook_fn gc_mark_hook,
2985 struct coff_reloc_cookie *cookie)
2987 asection *rsec;
2989 rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
2990 if (rsec && !rsec->gc_mark)
2992 if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
2993 rsec->gc_mark = 1;
2994 else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
2995 return false;
2997 return true;
3000 /* The mark phase of garbage collection. For a given section, mark
3001 it and any sections in this section's group, and all the sections
3002 which define symbols to which it refers. */
3004 static bool
3005 _bfd_coff_gc_mark (struct bfd_link_info *info,
3006 asection *sec,
3007 coff_gc_mark_hook_fn gc_mark_hook)
3009 bool ret = true;
3011 sec->gc_mark = 1;
3013 /* Look through the section relocs. */
3014 if ((sec->flags & SEC_RELOC) != 0
3015 && sec->reloc_count > 0)
3017 struct coff_reloc_cookie cookie;
3019 if (!init_reloc_cookie_for_section (&cookie, info, sec))
3020 ret = false;
3021 else
3023 for (; cookie.rel < cookie.relend; cookie.rel++)
3025 if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
3027 ret = false;
3028 break;
3031 fini_reloc_cookie_for_section (&cookie, sec);
3035 return ret;
3038 static bool
3039 _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
3040 coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
3042 bfd *ibfd;
3044 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3046 asection *isec;
3047 bool some_kept;
3049 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour)
3050 continue;
3052 /* Ensure all linker created sections are kept, and see whether
3053 any other section is already marked. */
3054 some_kept = false;
3055 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3057 if ((isec->flags & SEC_LINKER_CREATED) != 0)
3058 isec->gc_mark = 1;
3059 else if (isec->gc_mark)
3060 some_kept = true;
3063 /* If no section in this file will be kept, then we can
3064 toss out debug sections. */
3065 if (!some_kept)
3066 continue;
3068 /* Keep debug and special sections like .comment when they are
3069 not part of a group, or when we have single-member groups. */
3070 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3071 if ((isec->flags & SEC_DEBUGGING) != 0
3072 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3073 isec->gc_mark = 1;
3075 return true;
3078 /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */
3080 static bool
3081 coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
3082 void *data ATTRIBUTE_UNUSED)
3084 if (h->root.type == bfd_link_hash_warning)
3085 h = (struct coff_link_hash_entry *) h->root.u.i.link;
3087 if ((h->root.type == bfd_link_hash_defined
3088 || h->root.type == bfd_link_hash_defweak)
3089 && !h->root.u.def.section->gc_mark
3090 && !(h->root.u.def.section->owner->flags & DYNAMIC))
3092 /* Do our best to hide the symbol. */
3093 h->root.u.def.section = bfd_und_section_ptr;
3094 h->symbol_class = C_HIDDEN;
3097 return true;
3100 /* The sweep phase of garbage collection. Remove all garbage sections. */
3102 typedef bool (*gc_sweep_hook_fn)
3103 (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
3105 static bool
3106 coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3108 bfd *sub;
3110 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3112 asection *o;
3114 if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3115 continue;
3117 for (o = sub->sections; o != NULL; o = o->next)
3119 /* Keep debug and special sections. */
3120 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
3121 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3122 o->gc_mark = 1;
3123 else if (startswith (o->name, ".idata")
3124 || startswith (o->name, ".pdata")
3125 || startswith (o->name, ".xdata")
3126 || startswith (o->name, ".rsrc"))
3127 o->gc_mark = 1;
3129 if (o->gc_mark)
3130 continue;
3132 /* Skip sweeping sections already excluded. */
3133 if (o->flags & SEC_EXCLUDE)
3134 continue;
3136 /* Since this is early in the link process, it is simple
3137 to remove a section from the output. */
3138 o->flags |= SEC_EXCLUDE;
3140 if (info->print_gc_sections && o->size != 0)
3141 /* xgettext: c-format */
3142 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3143 o, sub);
3145 #if 0
3146 /* But we also have to update some of the relocation
3147 info we collected before. */
3148 if (gc_sweep_hook
3149 && (o->flags & SEC_RELOC) != 0
3150 && o->reloc_count > 0
3151 && !bfd_is_abs_section (o->output_section))
3153 struct internal_reloc *internal_relocs;
3154 bool r;
3156 internal_relocs
3157 = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
3158 info->keep_memory);
3159 if (internal_relocs == NULL)
3160 return false;
3162 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
3164 if (coff_section_data (o)->relocs != internal_relocs)
3165 free (internal_relocs);
3167 if (!r)
3168 return false;
3170 #endif
3174 /* Remove the symbols that were in the swept sections from the dynamic
3175 symbol table. */
3176 coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
3177 NULL);
3179 return true;
3182 /* Keep all sections containing symbols undefined on the command-line,
3183 and the section containing the entry symbol. */
3185 static void
3186 _bfd_coff_gc_keep (struct bfd_link_info *info)
3188 struct bfd_sym_chain *sym;
3190 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
3192 struct coff_link_hash_entry *h;
3194 h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
3195 false, false, false);
3197 if (h != NULL
3198 && (h->root.type == bfd_link_hash_defined
3199 || h->root.type == bfd_link_hash_defweak)
3200 && !bfd_is_abs_section (h->root.u.def.section))
3201 h->root.u.def.section->flags |= SEC_KEEP;
3205 /* Do mark and sweep of unused sections. */
3207 bool
3208 bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3210 bfd *sub;
3212 /* FIXME: Should we implement this? */
3213 #if 0
3214 const bfd_coff_backend_data *bed = coff_backend_info (abfd);
3216 if (!bed->can_gc_sections
3217 || !is_coff_hash_table (info->hash))
3219 _bfd_error_handler(_("warning: gc-sections option ignored"));
3220 return true;
3222 #endif
3224 _bfd_coff_gc_keep (info);
3226 /* Grovel through relocs to find out who stays ... */
3227 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3229 asection *o;
3231 if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3232 continue;
3234 for (o = sub->sections; o != NULL; o = o->next)
3236 if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
3237 || startswith (o->name, ".vectors")
3238 || startswith (o->name, ".ctors")
3239 || startswith (o->name, ".dtors"))
3240 && !o->gc_mark)
3242 if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
3243 return false;
3248 /* Allow the backend to mark additional target specific sections. */
3249 _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
3251 /* ... and mark SEC_EXCLUDE for those that go. */
3252 return coff_gc_sweep (abfd, info);
3255 /* Return name used to identify a comdat group. */
3257 const char *
3258 bfd_coff_group_name (bfd *abfd, const asection *sec)
3260 struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec);
3261 if (ci != NULL)
3262 return ci->name;
3263 return NULL;
3266 bool
3267 _bfd_coff_free_cached_info (bfd *abfd)
3269 struct coff_tdata *tdata;
3271 if (bfd_family_coff (abfd)
3272 && (bfd_get_format (abfd) == bfd_object
3273 || bfd_get_format (abfd) == bfd_core)
3274 && (tdata = coff_data (abfd)) != NULL)
3276 if (tdata->section_by_index)
3278 htab_delete (tdata->section_by_index);
3279 tdata->section_by_index = NULL;
3282 if (tdata->section_by_target_index)
3284 htab_delete (tdata->section_by_target_index);
3285 tdata->section_by_target_index = NULL;
3288 if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
3290 htab_delete (pe_data (abfd)->comdat_hash);
3291 pe_data (abfd)->comdat_hash = NULL;
3294 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
3295 _bfd_stab_cleanup (abfd, &tdata->line_info);
3297 /* PR 25447:
3298 Do not clear the keep_syms and keep_strings flags.
3299 These may have been set by pe_ILF_build_a_bfd() indicating
3300 that the syms and strings pointers are not to be freed. */
3301 if (!_bfd_coff_free_symbols (abfd))
3302 return false;
3305 return _bfd_generic_bfd_free_cached_info (abfd);