1 /* Support for the generic parts of COFF, for BFD.
2 Copyright (C) 1990-2014 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
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
42 #include "coff/internal.h"
45 /* Take a section header read from a coff file (in HOST byte order),
46 and make a BFD "section" out of it. This is used by ECOFF. */
49 make_a_section_from_file (bfd *abfd,
50 struct internal_scnhdr *hdr,
51 unsigned int target_index)
53 asection *return_section;
55 bfd_boolean result = TRUE;
60 /* Handle long section names as in PE. On reading, we want to
61 accept long names if the format permits them at all, regardless
62 of the current state of the flag that dictates if we would generate
63 them in outputs; this construct checks if that is the case by
64 attempting to set the flag, without changing its state; the call
65 will fail for formats that do not support long names at all. */
66 if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
67 && hdr->s_name[0] == '/')
74 /* Flag that this BFD uses long names, even though the format might
75 expect them to be off by default. This won't directly affect the
76 format of any output BFD created from this one, but the information
77 can be used to decide what to do. */
78 bfd_coff_set_long_section_names (abfd, TRUE);
79 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
80 buf[SCNNMLEN - 1] = '\0';
81 strindex = strtol (buf, &p, 10);
82 if (*p == '\0' && strindex >= 0)
84 strings = _bfd_coff_read_string_table (abfd);
87 /* FIXME: For extra safety, we should make sure that
88 strindex does not run us past the end, but right now we
89 don't know the length of the string table. */
91 name = (char *) bfd_alloc (abfd,
92 (bfd_size_type) strlen (strings) + 1 + 1);
95 strcpy (name, strings);
101 /* Assorted wastage to null-terminate the name, thanks AT&T! */
102 name = (char *) bfd_alloc (abfd,
103 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
106 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
107 name[sizeof (hdr->s_name)] = 0;
110 return_section = bfd_make_section_anyway (abfd, name);
111 if (return_section == NULL)
114 return_section->vma = hdr->s_vaddr;
115 return_section->lma = hdr->s_paddr;
116 return_section->size = hdr->s_size;
117 return_section->filepos = hdr->s_scnptr;
118 return_section->rel_filepos = hdr->s_relptr;
119 return_section->reloc_count = hdr->s_nreloc;
121 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
123 return_section->line_filepos = hdr->s_lnnoptr;
125 return_section->lineno_count = hdr->s_nlnno;
126 return_section->userdata = NULL;
127 return_section->next = NULL;
128 return_section->target_index = target_index;
130 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
134 return_section->flags = flags;
136 /* At least on i386-coff, the line number count for a shared library
137 section must be ignored. */
138 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
139 return_section->lineno_count = 0;
141 if (hdr->s_nreloc != 0)
142 return_section->flags |= SEC_RELOC;
143 /* FIXME: should this check 'hdr->s_size > 0'. */
144 if (hdr->s_scnptr != 0)
145 return_section->flags |= SEC_HAS_CONTENTS;
147 /* Compress/decompress DWARF debug sections with names: .debug_* and
148 .zdebug_*, after the section flags is set. */
149 if ((flags & SEC_DEBUGGING)
150 && ((name[1] == 'd' && name[6] == '_')
151 || (name[1] == 'z' && name[7] == '_')))
153 enum { nothing, compress, decompress } action = nothing;
154 char *new_name = NULL;
156 if (bfd_is_section_compressed (abfd, return_section))
158 /* Compressed section. Check if we should decompress. */
159 if ((abfd->flags & BFD_DECOMPRESS))
162 else if (!bfd_is_section_compressed (abfd, return_section))
164 /* Normal section. Check if we should compress. */
165 if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0)
174 if (!bfd_init_section_compress_status (abfd, return_section))
176 (*_bfd_error_handler)
177 (_("%B: unable to initialize compress status for section %s"),
183 unsigned int len = strlen (name);
185 new_name = bfd_alloc (abfd, len + 2);
186 if (new_name == NULL)
190 memcpy (new_name + 2, name + 1, len);
194 if (!bfd_init_section_decompress_status (abfd, return_section))
196 (*_bfd_error_handler)
197 (_("%B: unable to initialize decompress status for section %s"),
203 unsigned int len = strlen (name);
205 new_name = bfd_alloc (abfd, len);
206 if (new_name == NULL)
209 memcpy (new_name + 1, name + 2, len - 1);
213 if (new_name != NULL)
214 bfd_rename_section (abfd, return_section, new_name);
220 /* Read in a COFF object and make it into a BFD. This is used by
223 coff_real_object_p (bfd *,
225 struct internal_filehdr *,
226 struct internal_aouthdr *);
228 coff_real_object_p (bfd *abfd,
230 struct internal_filehdr *internal_f,
231 struct internal_aouthdr *internal_a)
233 flagword oflags = abfd->flags;
234 bfd_vma ostart = bfd_get_start_address (abfd);
237 bfd_size_type readsize; /* Length of file_info. */
239 char *external_sections;
241 if (!(internal_f->f_flags & F_RELFLG))
242 abfd->flags |= HAS_RELOC;
243 if ((internal_f->f_flags & F_EXEC))
244 abfd->flags |= EXEC_P;
245 if (!(internal_f->f_flags & F_LNNO))
246 abfd->flags |= HAS_LINENO;
247 if (!(internal_f->f_flags & F_LSYMS))
248 abfd->flags |= HAS_LOCALS;
250 /* FIXME: How can we set D_PAGED correctly? */
251 if ((internal_f->f_flags & F_EXEC) != 0)
252 abfd->flags |= D_PAGED;
254 bfd_get_symcount (abfd) = internal_f->f_nsyms;
255 if (internal_f->f_nsyms)
256 abfd->flags |= HAS_SYMS;
258 if (internal_a != (struct internal_aouthdr *) NULL)
259 bfd_get_start_address (abfd) = internal_a->entry;
261 bfd_get_start_address (abfd) = 0;
263 /* Set up the tdata area. ECOFF uses its own routine, and overrides
265 tdata_save = abfd->tdata.any;
266 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
270 scnhsz = bfd_coff_scnhsz (abfd);
271 readsize = (bfd_size_type) nscns * scnhsz;
272 external_sections = (char *) bfd_alloc (abfd, readsize);
273 if (!external_sections)
276 if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
279 /* Set the arch/mach *before* swapping in sections; section header swapping
280 may depend on arch/mach info. */
281 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
284 /* Now copy data as required; construct all asections etc. */
288 for (i = 0; i < nscns; i++)
290 struct internal_scnhdr tmp;
291 bfd_coff_swap_scnhdr_in (abfd,
292 (void *) (external_sections + i * scnhsz),
294 if (! make_a_section_from_file (abfd, &tmp, i + 1))
302 bfd_release (abfd, tdata);
304 abfd->tdata.any = tdata_save;
305 abfd->flags = oflags;
306 bfd_get_start_address (abfd) = ostart;
307 return (const bfd_target *) NULL;
310 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
311 not a COFF file. This is also used by ECOFF. */
314 coff_object_p (bfd *abfd)
316 bfd_size_type filhsz;
317 bfd_size_type aoutsz;
320 struct internal_filehdr internal_f;
321 struct internal_aouthdr internal_a;
323 /* Figure out how much to read. */
324 filhsz = bfd_coff_filhsz (abfd);
325 aoutsz = bfd_coff_aoutsz (abfd);
327 filehdr = bfd_alloc (abfd, filhsz);
330 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
332 if (bfd_get_error () != bfd_error_system_call)
333 bfd_set_error (bfd_error_wrong_format);
334 bfd_release (abfd, filehdr);
337 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
338 bfd_release (abfd, filehdr);
340 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
341 (less than aoutsz) used in object files and AOUTSZ (equal to
342 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
343 expects this header to be aoutsz bytes in length, so we use that
344 value in the call to bfd_alloc below. But we must be careful to
345 only read in f_opthdr bytes in the call to bfd_bread. We should
346 also attempt to catch corrupt or non-COFF binaries with a strange
347 value for f_opthdr. */
348 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
349 || internal_f.f_opthdr > aoutsz)
351 bfd_set_error (bfd_error_wrong_format);
354 nscns = internal_f.f_nscns;
356 if (internal_f.f_opthdr)
360 opthdr = bfd_alloc (abfd, aoutsz);
363 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
364 != internal_f.f_opthdr)
366 bfd_release (abfd, opthdr);
369 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
370 bfd_release (abfd, opthdr);
373 return coff_real_object_p (abfd, nscns, &internal_f,
374 (internal_f.f_opthdr != 0
376 : (struct internal_aouthdr *) NULL));
379 /* Get the BFD section from a COFF symbol section number. */
382 coff_section_from_bfd_index (bfd *abfd, int section_index)
384 struct bfd_section *answer = abfd->sections;
386 if (section_index == N_ABS)
387 return bfd_abs_section_ptr;
388 if (section_index == N_UNDEF)
389 return bfd_und_section_ptr;
390 if (section_index == N_DEBUG)
391 return bfd_abs_section_ptr;
395 if (answer->target_index == section_index)
397 answer = answer->next;
400 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
401 has a bad symbol table in biglitpow.o. */
402 return bfd_und_section_ptr;
405 /* Get the upper bound of a COFF symbol table. */
408 coff_get_symtab_upper_bound (bfd *abfd)
410 if (!bfd_coff_slurp_symbol_table (abfd))
413 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
416 /* Canonicalize a COFF symbol table. */
419 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
421 unsigned int counter;
422 coff_symbol_type *symbase;
423 coff_symbol_type **location = (coff_symbol_type **) alocation;
425 if (!bfd_coff_slurp_symbol_table (abfd))
428 symbase = obj_symbols (abfd);
429 counter = bfd_get_symcount (abfd);
430 while (counter-- > 0)
431 *location++ = symbase++;
435 return bfd_get_symcount (abfd);
438 /* Get the name of a symbol. The caller must pass in a buffer of size
442 _bfd_coff_internal_syment_name (bfd *abfd,
443 const struct internal_syment *sym,
446 /* FIXME: It's not clear this will work correctly if sizeof
448 if (sym->_n._n_n._n_zeroes != 0
449 || sym->_n._n_n._n_offset == 0)
451 memcpy (buf, sym->_n._n_name, SYMNMLEN);
452 buf[SYMNMLEN] = '\0';
459 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
460 strings = obj_coff_strings (abfd);
463 strings = _bfd_coff_read_string_table (abfd);
467 return strings + sym->_n._n_n._n_offset;
471 /* Read in and swap the relocs. This returns a buffer holding the
472 relocs for section SEC in file ABFD. If CACHE is TRUE and
473 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
474 the function is called again. If EXTERNAL_RELOCS is not NULL, it
475 is a buffer large enough to hold the unswapped relocs. If
476 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
477 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
478 value must be INTERNAL_RELOCS. The function returns NULL on error. */
480 struct internal_reloc *
481 _bfd_coff_read_internal_relocs (bfd *abfd,
484 bfd_byte *external_relocs,
485 bfd_boolean require_internal,
486 struct internal_reloc *internal_relocs)
489 bfd_byte *free_external = NULL;
490 struct internal_reloc *free_internal = NULL;
493 struct internal_reloc *irel;
496 if (sec->reloc_count == 0)
497 return internal_relocs; /* Nothing to do. */
499 if (coff_section_data (abfd, sec) != NULL
500 && coff_section_data (abfd, sec)->relocs != NULL)
502 if (! require_internal)
503 return coff_section_data (abfd, sec)->relocs;
504 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
505 sec->reloc_count * sizeof (struct internal_reloc));
506 return internal_relocs;
509 relsz = bfd_coff_relsz (abfd);
511 amt = sec->reloc_count * relsz;
512 if (external_relocs == NULL)
514 free_external = (bfd_byte *) bfd_malloc (amt);
515 if (free_external == NULL)
517 external_relocs = free_external;
520 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
521 || bfd_bread (external_relocs, amt, abfd) != amt)
524 if (internal_relocs == NULL)
526 amt = sec->reloc_count;
527 amt *= sizeof (struct internal_reloc);
528 free_internal = (struct internal_reloc *) bfd_malloc (amt);
529 if (free_internal == NULL)
531 internal_relocs = free_internal;
534 /* Swap in the relocs. */
535 erel = external_relocs;
536 erel_end = erel + relsz * sec->reloc_count;
537 irel = internal_relocs;
538 for (; erel < erel_end; erel += relsz, irel++)
539 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
541 if (free_external != NULL)
543 free (free_external);
544 free_external = NULL;
547 if (cache && free_internal != NULL)
549 if (coff_section_data (abfd, sec) == NULL)
551 amt = sizeof (struct coff_section_tdata);
552 sec->used_by_bfd = bfd_zalloc (abfd, amt);
553 if (sec->used_by_bfd == NULL)
555 coff_section_data (abfd, sec)->contents = NULL;
557 coff_section_data (abfd, sec)->relocs = free_internal;
560 return internal_relocs;
563 if (free_external != NULL)
564 free (free_external);
565 if (free_internal != NULL)
566 free (free_internal);
570 /* Set lineno_count for the output sections of a COFF file. */
573 coff_count_linenumbers (bfd *abfd)
575 unsigned int limit = bfd_get_symcount (abfd);
583 /* This may be from the backend linker, in which case the
584 lineno_count in the sections is correct. */
585 for (s = abfd->sections; s != NULL; s = s->next)
586 total += s->lineno_count;
590 for (s = abfd->sections; s != NULL; s = s->next)
591 BFD_ASSERT (s->lineno_count == 0);
593 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
595 asymbol *q_maybe = *p;
597 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
599 coff_symbol_type *q = coffsymbol (q_maybe);
601 /* The AIX 4.1 compiler can sometimes generate line numbers
602 attached to debugging symbols. We try to simply ignore
604 if (q->lineno != NULL
605 && q->symbol.section->owner != NULL)
607 /* This symbol has line numbers. Increment the owning
608 section's linenumber count. */
609 alent *l = q->lineno;
613 asection * sec = q->symbol.section->output_section;
615 /* Do not try to update fields in read-only sections. */
616 if (! bfd_is_const_section (sec))
617 sec->lineno_count ++;
622 while (l->line_number != 0);
630 /* Takes a bfd and a symbol, returns a pointer to the coff specific
631 area of the symbol if there is one. */
634 coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
637 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
638 return (coff_symbol_type *) NULL;
640 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
641 return (coff_symbol_type *) NULL;
643 return (coff_symbol_type *) symbol;
647 fixup_symbol_value (bfd *abfd,
648 coff_symbol_type *coff_symbol_ptr,
649 struct internal_syment *syment)
651 /* Normalize the symbol flags. */
652 if (coff_symbol_ptr->symbol.section
653 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
655 /* A common symbol is undefined with a value. */
656 syment->n_scnum = N_UNDEF;
657 syment->n_value = coff_symbol_ptr->symbol.value;
659 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
660 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
662 syment->n_value = coff_symbol_ptr->symbol.value;
664 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
666 syment->n_scnum = N_UNDEF;
669 /* FIXME: Do we need to handle the absolute section here? */
672 if (coff_symbol_ptr->symbol.section)
675 coff_symbol_ptr->symbol.section->output_section->target_index;
677 syment->n_value = (coff_symbol_ptr->symbol.value
678 + coff_symbol_ptr->symbol.section->output_offset);
681 syment->n_value += (syment->n_sclass == C_STATLAB)
682 ? coff_symbol_ptr->symbol.section->output_section->lma
683 : coff_symbol_ptr->symbol.section->output_section->vma;
689 /* This can happen, but I don't know why yet (steve@cygnus.com) */
690 syment->n_scnum = N_ABS;
691 syment->n_value = coff_symbol_ptr->symbol.value;
696 /* Run through all the symbols in the symbol table and work out what
697 their indexes into the symbol table will be when output.
699 Coff requires that each C_FILE symbol points to the next one in the
700 chain, and that the last one points to the first external symbol. We
704 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
706 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
707 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
708 unsigned int native_index = 0;
709 struct internal_syment *last_file = NULL;
710 unsigned int symbol_index;
712 /* COFF demands that undefined symbols come after all other symbols.
713 Since we don't need to impose this extra knowledge on all our
714 client programs, deal with that here. Sort the symbol table;
715 just move the undefined symbols to the end, leaving the rest
716 alone. The O'Reilly book says that defined global symbols come
717 at the end before the undefined symbols, so we do that here as
719 /* @@ Do we have some condition we could test for, so we don't always
720 have to do this? I don't think relocatability is quite right, but
721 I'm not certain. [raeburn:19920508.1711EST] */
727 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
728 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
731 bfd_ptr->outsymbols = newsyms;
732 for (i = 0; i < symbol_count; i++)
733 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
734 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
735 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
736 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
737 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
739 *newsyms++ = symbol_ptr_ptr[i];
741 for (i = 0; i < symbol_count; i++)
742 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
743 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
744 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
745 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
746 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
748 *newsyms++ = symbol_ptr_ptr[i];
750 *first_undef = newsyms - bfd_ptr->outsymbols;
752 for (i = 0; i < symbol_count; i++)
753 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
754 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
755 *newsyms++ = symbol_ptr_ptr[i];
756 *newsyms = (asymbol *) NULL;
757 symbol_ptr_ptr = bfd_ptr->outsymbols;
760 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
762 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
763 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
764 if (coff_symbol_ptr && coff_symbol_ptr->native)
766 combined_entry_type *s = coff_symbol_ptr->native;
769 if (s->u.syment.n_sclass == C_FILE)
771 if (last_file != NULL)
772 last_file->n_value = native_index;
773 last_file = &(s->u.syment);
776 /* Modify the symbol values according to their section and
778 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
780 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
781 s[i].offset = native_index++;
787 obj_conv_table_size (bfd_ptr) = native_index;
792 /* Run thorough the symbol table again, and fix it so that all
793 pointers to entries are changed to the entries' index in the output
797 coff_mangle_symbols (bfd *bfd_ptr)
799 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
800 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
801 unsigned int symbol_index;
803 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
805 coff_symbol_type *coff_symbol_ptr =
806 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
808 if (coff_symbol_ptr && coff_symbol_ptr->native)
811 combined_entry_type *s = coff_symbol_ptr->native;
815 /* FIXME: We should use a union here. */
816 s->u.syment.n_value =
817 (bfd_hostptr_t) ((combined_entry_type *)
818 ((bfd_hostptr_t) s->u.syment.n_value))->offset;
823 /* The value is the offset into the line number entries
824 for the symbol's section. On output, the symbol's
825 section should be N_DEBUG. */
826 s->u.syment.n_value =
827 (coff_symbol_ptr->symbol.section->output_section->line_filepos
828 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
829 coff_symbol_ptr->symbol.section =
830 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
831 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
833 for (i = 0; i < s->u.syment.n_numaux; i++)
835 combined_entry_type *a = s + i + 1;
838 a->u.auxent.x_sym.x_tagndx.l =
839 a->u.auxent.x_sym.x_tagndx.p->offset;
844 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
845 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
850 a->u.auxent.x_csect.x_scnlen.l =
851 a->u.auxent.x_csect.x_scnlen.p->offset;
860 coff_fix_symbol_name (bfd *abfd,
862 combined_entry_type *native,
863 bfd_size_type *string_size_p,
864 asection **debug_string_section_p,
865 bfd_size_type *debug_string_size_p)
867 unsigned int name_length;
868 union internal_auxent *auxent;
869 char *name = (char *) (symbol->name);
873 /* COFF symbols always have names, so we'll make one up. */
874 symbol->name = "strange";
875 name = (char *) symbol->name;
877 name_length = strlen (name);
879 if (native->u.syment.n_sclass == C_FILE
880 && native->u.syment.n_numaux > 0)
882 unsigned int filnmlen;
884 if (bfd_coff_force_symnames_in_strings (abfd))
886 native->u.syment._n._n_n._n_offset =
887 (*string_size_p + STRING_SIZE_SIZE);
888 native->u.syment._n._n_n._n_zeroes = 0;
889 *string_size_p += 6; /* strlen(".file") + 1 */
892 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
894 auxent = &(native + 1)->u.auxent;
896 filnmlen = bfd_coff_filnmlen (abfd);
898 if (bfd_coff_long_filenames (abfd))
900 if (name_length <= filnmlen)
901 strncpy (auxent->x_file.x_fname, name, filnmlen);
904 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
905 auxent->x_file.x_n.x_zeroes = 0;
906 *string_size_p += name_length + 1;
911 strncpy (auxent->x_file.x_fname, name, filnmlen);
912 if (name_length > filnmlen)
913 name[filnmlen] = '\0';
918 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
919 /* This name will fit into the symbol neatly. */
920 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
922 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
924 native->u.syment._n._n_n._n_offset = (*string_size_p
926 native->u.syment._n._n_n._n_zeroes = 0;
927 *string_size_p += name_length + 1;
933 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
935 /* This name should be written into the .debug section. For
936 some reason each name is preceded by a two byte length
937 and also followed by a null byte. FIXME: We assume that
938 the .debug section has already been created, and that it
940 if (*debug_string_section_p == (asection *) NULL)
941 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
942 filepos = bfd_tell (abfd);
944 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
946 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
948 if (!bfd_set_section_contents (abfd,
949 *debug_string_section_p,
951 (file_ptr) *debug_string_size_p,
952 (bfd_size_type) prefix_len)
953 || !bfd_set_section_contents (abfd,
954 *debug_string_section_p,
955 (void *) symbol->name,
956 (file_ptr) (*debug_string_size_p
958 (bfd_size_type) name_length + 1))
960 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
962 native->u.syment._n._n_n._n_offset =
963 *debug_string_size_p + prefix_len;
964 native->u.syment._n._n_n._n_zeroes = 0;
965 *debug_string_size_p += name_length + 1 + prefix_len;
970 /* We need to keep track of the symbol index so that when we write out
971 the relocs we can get the index for a symbol. This method is a
974 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
976 /* Write a symbol out to a COFF file. */
979 coff_write_symbol (bfd *abfd,
981 combined_entry_type *native,
983 bfd_size_type *string_size_p,
984 asection **debug_string_section_p,
985 bfd_size_type *debug_string_size_p)
987 unsigned int numaux = native->u.syment.n_numaux;
988 int type = native->u.syment.n_type;
989 int n_sclass = (int) native->u.syment.n_sclass;
990 asection *output_section = symbol->section->output_section
991 ? symbol->section->output_section
994 bfd_size_type symesz;
996 if (native->u.syment.n_sclass == C_FILE)
997 symbol->flags |= BSF_DEBUGGING;
999 if (symbol->flags & BSF_DEBUGGING
1000 && bfd_is_abs_section (symbol->section))
1001 native->u.syment.n_scnum = N_DEBUG;
1003 else if (bfd_is_abs_section (symbol->section))
1004 native->u.syment.n_scnum = N_ABS;
1006 else if (bfd_is_und_section (symbol->section))
1007 native->u.syment.n_scnum = N_UNDEF;
1010 native->u.syment.n_scnum =
1011 output_section->target_index;
1013 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
1014 debug_string_section_p, debug_string_size_p);
1016 symesz = bfd_coff_symesz (abfd);
1017 buf = bfd_alloc (abfd, symesz);
1020 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1021 if (bfd_bwrite (buf, symesz, abfd) != symesz)
1023 bfd_release (abfd, buf);
1025 if (native->u.syment.n_numaux > 0)
1027 bfd_size_type auxesz;
1030 auxesz = bfd_coff_auxesz (abfd);
1031 buf = bfd_alloc (abfd, auxesz);
1034 for (j = 0; j < native->u.syment.n_numaux; j++)
1036 bfd_coff_swap_aux_out (abfd,
1037 &((native + j + 1)->u.auxent),
1038 type, n_sclass, (int) j,
1039 native->u.syment.n_numaux,
1041 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1044 bfd_release (abfd, buf);
1047 /* Store the index for use when we write out the relocs. */
1048 set_index (symbol, *written);
1050 *written += numaux + 1;
1054 /* Write out a symbol to a COFF file that does not come from a COFF
1055 file originally. This symbol may have been created by the linker,
1056 or we may be linking a non COFF file to a COFF file. */
1059 coff_write_alien_symbol (bfd *abfd,
1061 struct internal_syment *isym,
1063 bfd_size_type *string_size_p,
1064 asection **debug_string_section_p,
1065 bfd_size_type *debug_string_size_p)
1067 combined_entry_type *native;
1068 combined_entry_type dummy[2];
1069 asection *output_section = symbol->section->output_section
1070 ? symbol->section->output_section
1072 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1075 if ((!link_info || link_info->strip_discarded)
1076 && !bfd_is_abs_section (symbol->section)
1077 && symbol->section->output_section == bfd_abs_section_ptr)
1081 memset (isym, 0, sizeof(*isym));
1085 native->u.syment.n_type = T_NULL;
1086 native->u.syment.n_flags = 0;
1087 native->u.syment.n_numaux = 0;
1088 if (bfd_is_und_section (symbol->section))
1090 native->u.syment.n_scnum = N_UNDEF;
1091 native->u.syment.n_value = symbol->value;
1093 else if (bfd_is_com_section (symbol->section))
1095 native->u.syment.n_scnum = N_UNDEF;
1096 native->u.syment.n_value = symbol->value;
1098 else if (symbol->flags & BSF_FILE)
1100 native->u.syment.n_scnum = N_DEBUG;
1101 native->u.syment.n_numaux = 1;
1103 else if (symbol->flags & BSF_DEBUGGING)
1105 /* There isn't much point to writing out a debugging symbol
1106 unless we are prepared to convert it into COFF debugging
1107 format. So, we just ignore them. We must clobber the symbol
1108 name to keep it from being put in the string table. */
1111 memset (isym, 0, sizeof(*isym));
1116 native->u.syment.n_scnum = output_section->target_index;
1117 native->u.syment.n_value = (symbol->value
1118 + symbol->section->output_offset);
1119 if (! obj_pe (abfd))
1120 native->u.syment.n_value += output_section->vma;
1122 /* Copy the any flags from the file header into the symbol.
1125 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1126 if (c != (coff_symbol_type *) NULL)
1127 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1131 native->u.syment.n_type = 0;
1132 if (symbol->flags & BSF_FILE)
1133 native->u.syment.n_sclass = C_FILE;
1134 else if (symbol->flags & BSF_LOCAL)
1135 native->u.syment.n_sclass = C_STAT;
1136 else if (symbol->flags & BSF_WEAK)
1137 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1139 native->u.syment.n_sclass = C_EXT;
1141 ret = coff_write_symbol (abfd, symbol, native, written, string_size_p,
1142 debug_string_section_p, debug_string_size_p);
1144 *isym = native->u.syment;
1148 /* Write a native symbol to a COFF file. */
1151 coff_write_native_symbol (bfd *abfd,
1152 coff_symbol_type *symbol,
1154 bfd_size_type *string_size_p,
1155 asection **debug_string_section_p,
1156 bfd_size_type *debug_string_size_p)
1158 combined_entry_type *native = symbol->native;
1159 alent *lineno = symbol->lineno;
1160 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1162 if ((!link_info || link_info->strip_discarded)
1163 && !bfd_is_abs_section (symbol->symbol.section)
1164 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1166 symbol->symbol.name = "";
1170 /* If this symbol has an associated line number, we must store the
1171 symbol index in the line number field. We also tag the auxent to
1172 point to the right place in the lineno table. */
1173 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1175 unsigned int count = 0;
1177 lineno[count].u.offset = *written;
1178 if (native->u.syment.n_numaux)
1180 union internal_auxent *a = &((native + 1)->u.auxent);
1182 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1183 symbol->symbol.section->output_section->moving_line_filepos;
1186 /* Count and relocate all other linenumbers. */
1188 while (lineno[count].line_number != 0)
1190 lineno[count].u.offset +=
1191 (symbol->symbol.section->output_section->vma
1192 + symbol->symbol.section->output_offset);
1195 symbol->done_lineno = TRUE;
1197 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1198 symbol->symbol.section->output_section->moving_line_filepos +=
1199 count * bfd_coff_linesz (abfd);
1202 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1203 string_size_p, debug_string_section_p,
1204 debug_string_size_p);
1208 null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
1212 /* Write out the COFF symbols. */
1215 coff_write_symbols (bfd *abfd)
1217 bfd_size_type string_size;
1218 asection *debug_string_section;
1219 bfd_size_type debug_string_size;
1221 unsigned int limit = bfd_get_symcount (abfd);
1222 bfd_vma written = 0;
1226 debug_string_section = NULL;
1227 debug_string_size = 0;
1229 /* If this target supports long section names, they must be put into
1230 the string table. This is supported by PE. This code must
1231 handle section names just as they are handled in
1232 coff_write_object_contents. */
1233 if (bfd_coff_long_section_names (abfd))
1237 for (o = abfd->sections; o != NULL; o = o->next)
1241 len = strlen (o->name);
1243 string_size += len + 1;
1247 /* Seek to the right place. */
1248 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1251 /* Output all the symbols we have. */
1253 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1255 asymbol *symbol = *p;
1256 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1258 if (c_symbol == (coff_symbol_type *) NULL
1259 || c_symbol->native == (combined_entry_type *) NULL)
1261 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1262 &string_size, &debug_string_section,
1263 &debug_string_size))
1268 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1270 bfd_error_handler_type current_error_handler;
1271 enum coff_symbol_classification sym_class;
1272 unsigned char *n_sclass;
1274 /* Suppress error reporting by bfd_coff_classify_symbol.
1275 Error messages can be generated when we are processing a local
1276 symbol which has no associated section and we do not have to
1277 worry about this, all we need to know is that it is local. */
1278 current_error_handler = bfd_set_error_handler (null_error_handler);
1279 sym_class = bfd_coff_classify_symbol (abfd,
1280 &c_symbol->native->u.syment);
1281 (void) bfd_set_error_handler (current_error_handler);
1283 n_sclass = &c_symbol->native->u.syment.n_sclass;
1285 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1286 we cannot retain the existing sclass from the original symbol.
1287 Weak symbols only have one valid sclass, so just set it always.
1288 If it is not local class and should be, set it C_STAT.
1289 If it is global and not classified as global, or if it is
1290 weak (which is also classified as global), set it C_EXT. */
1292 if (symbol->flags & BSF_WEAK)
1293 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1294 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1296 else if (symbol->flags & BSF_GLOBAL
1297 && (sym_class != COFF_SYMBOL_GLOBAL
1299 || *n_sclass == C_NT_WEAK
1301 || *n_sclass == C_WEAKEXT))
1302 c_symbol->native->u.syment.n_sclass = C_EXT;
1305 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1306 &string_size, &debug_string_section,
1307 &debug_string_size))
1312 obj_raw_syment_count (abfd) = written;
1314 /* Now write out strings. */
1315 if (string_size != 0)
1317 unsigned int size = string_size + STRING_SIZE_SIZE;
1318 bfd_byte buffer[STRING_SIZE_SIZE];
1320 #if STRING_SIZE_SIZE == 4
1321 H_PUT_32 (abfd, size, buffer);
1323 #error Change H_PUT_32
1325 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1329 /* Handle long section names. This code must handle section
1330 names just as they are handled in coff_write_object_contents. */
1331 if (bfd_coff_long_section_names (abfd))
1335 for (o = abfd->sections; o != NULL; o = o->next)
1339 len = strlen (o->name);
1342 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1349 for (p = abfd->outsymbols, i = 0;
1354 size_t name_length = strlen (q->name);
1355 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1358 /* Figure out whether the symbol name should go in the string
1359 table. Symbol names that are short enough are stored
1360 directly in the syment structure. File names permit a
1361 different, longer, length in the syment structure. On
1362 XCOFF, some symbol names are stored in the .debug section
1363 rather than in the string table. */
1365 if (c_symbol == NULL
1366 || c_symbol->native == NULL)
1367 /* This is not a COFF symbol, so it certainly is not a
1368 file name, nor does it go in the .debug section. */
1369 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1371 else if (bfd_coff_symname_in_debug (abfd,
1372 &c_symbol->native->u.syment))
1373 /* This symbol name is in the XCOFF .debug section.
1374 Don't write it into the string table. */
1375 maxlen = name_length;
1377 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1378 && c_symbol->native->u.syment.n_numaux > 0)
1380 if (bfd_coff_force_symnames_in_strings (abfd))
1382 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1385 maxlen = bfd_coff_filnmlen (abfd);
1388 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1390 if (name_length > maxlen)
1392 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1393 abfd) != name_length + 1)
1400 /* We would normally not write anything here, but we'll write
1401 out 4 so that any stupid coff reader which tries to read the
1402 string table even when there isn't one won't croak. */
1403 unsigned int size = STRING_SIZE_SIZE;
1404 bfd_byte buffer[STRING_SIZE_SIZE];
1406 #if STRING_SIZE_SIZE == 4
1407 H_PUT_32 (abfd, size, buffer);
1409 #error Change H_PUT_32
1411 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1412 != STRING_SIZE_SIZE)
1416 /* Make sure the .debug section was created to be the correct size.
1417 We should create it ourselves on the fly, but we don't because
1418 BFD won't let us write to any section until we know how large all
1419 the sections are. We could still do it by making another pass
1420 over the symbols. FIXME. */
1421 BFD_ASSERT (debug_string_size == 0
1422 || (debug_string_section != (asection *) NULL
1423 && (BFD_ALIGN (debug_string_size,
1424 1 << debug_string_section->alignment_power)
1425 == debug_string_section->size)));
1431 coff_write_linenumbers (bfd *abfd)
1434 bfd_size_type linesz;
1437 linesz = bfd_coff_linesz (abfd);
1438 buff = bfd_alloc (abfd, linesz);
1441 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1443 if (s->lineno_count)
1445 asymbol **q = abfd->outsymbols;
1446 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1448 /* Find all the linenumbers in this section. */
1452 if (p->section->output_section == s)
1455 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1456 (bfd_asymbol_bfd (p), p));
1459 /* Found a linenumber entry, output. */
1460 struct internal_lineno out;
1461 memset ((void *) & out, 0, sizeof (out));
1463 out.l_addr.l_symndx = l->u.offset;
1464 bfd_coff_swap_lineno_out (abfd, &out, buff);
1465 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1469 while (l->line_number)
1471 out.l_lnno = l->line_number;
1472 out.l_addr.l_symndx = l->u.offset;
1473 bfd_coff_swap_lineno_out (abfd, &out, buff);
1474 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1485 bfd_release (abfd, buff);
1490 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1492 return coffsymbol (symbol)->lineno;
1495 /* This function transforms the offsets into the symbol table into
1496 pointers to syments. */
1499 coff_pointerize_aux (bfd *abfd,
1500 combined_entry_type *table_base,
1501 combined_entry_type *symbol,
1502 unsigned int indaux,
1503 combined_entry_type *auxent)
1505 unsigned int type = symbol->u.syment.n_type;
1506 unsigned int n_sclass = symbol->u.syment.n_sclass;
1508 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1510 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1511 (abfd, table_base, symbol, indaux, auxent))
1515 /* Don't bother if this is a file or a section. */
1516 if (n_sclass == C_STAT && type == T_NULL)
1518 if (n_sclass == C_FILE)
1521 /* Otherwise patch up. */
1522 #define N_TMASK coff_data (abfd)->local_n_tmask
1523 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1525 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1526 || n_sclass == C_FCN)
1527 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1529 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1530 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1531 auxent->fix_end = 1;
1533 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1534 generate one, so we must be careful to ignore it. */
1535 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1537 auxent->u.auxent.x_sym.x_tagndx.p =
1538 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1539 auxent->fix_tag = 1;
1543 /* Allocate space for the ".debug" section, and read it.
1544 We did not read the debug section until now, because
1545 we didn't want to go to the trouble until someone needed it. */
1548 build_debug_section (bfd *abfd)
1550 char *debug_section;
1552 bfd_size_type sec_size;
1554 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1558 bfd_set_error (bfd_error_no_debug_section);
1562 sec_size = sect->size;
1563 debug_section = (char *) bfd_alloc (abfd, sec_size);
1564 if (debug_section == NULL)
1567 /* Seek to the beginning of the `.debug' section and read it.
1568 Save the current position first; it is needed by our caller.
1569 Then read debug section and reset the file pointer. */
1571 position = bfd_tell (abfd);
1572 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1573 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1574 || bfd_seek (abfd, position, SEEK_SET) != 0)
1576 return debug_section;
1579 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1580 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1581 be \0-terminated. */
1584 copy_name (bfd *abfd, char *name, size_t maxlen)
1589 for (len = 0; len < maxlen; ++len)
1590 if (name[len] == '\0')
1593 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1596 strncpy (newname, name, len);
1597 newname[len] = '\0';
1601 /* Read in the external symbols. */
1604 _bfd_coff_get_external_symbols (bfd *abfd)
1606 bfd_size_type symesz;
1610 if (obj_coff_external_syms (abfd) != NULL)
1613 symesz = bfd_coff_symesz (abfd);
1615 size = obj_raw_syment_count (abfd) * symesz;
1619 syms = bfd_malloc (size);
1623 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1624 || bfd_bread (syms, size, abfd) != size)
1631 obj_coff_external_syms (abfd) = syms;
1636 /* Read in the external strings. The strings are not loaded until
1637 they are needed. This is because we have no simple way of
1638 detecting a missing string table in an archive. */
1641 _bfd_coff_read_string_table (bfd *abfd)
1643 char extstrsize[STRING_SIZE_SIZE];
1644 bfd_size_type strsize;
1648 if (obj_coff_strings (abfd) != NULL)
1649 return obj_coff_strings (abfd);
1651 if (obj_sym_filepos (abfd) == 0)
1653 bfd_set_error (bfd_error_no_symbols);
1657 pos = obj_sym_filepos (abfd);
1658 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1659 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1662 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1663 != sizeof extstrsize)
1665 if (bfd_get_error () != bfd_error_file_truncated)
1668 /* There is no string table. */
1669 strsize = STRING_SIZE_SIZE;
1673 #if STRING_SIZE_SIZE == 4
1674 strsize = H_GET_32 (abfd, extstrsize);
1676 #error Change H_GET_32
1680 if (strsize < STRING_SIZE_SIZE)
1682 (*_bfd_error_handler)
1683 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1684 bfd_set_error (bfd_error_bad_value);
1688 strings = (char *) bfd_malloc (strsize);
1689 if (strings == NULL)
1692 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1693 != strsize - STRING_SIZE_SIZE)
1699 obj_coff_strings (abfd) = strings;
1704 /* Free up the external symbols and strings read from a COFF file. */
1707 _bfd_coff_free_symbols (bfd *abfd)
1709 if (obj_coff_external_syms (abfd) != NULL
1710 && ! obj_coff_keep_syms (abfd))
1712 free (obj_coff_external_syms (abfd));
1713 obj_coff_external_syms (abfd) = NULL;
1715 if (obj_coff_strings (abfd) != NULL
1716 && ! obj_coff_keep_strings (abfd))
1718 free (obj_coff_strings (abfd));
1719 obj_coff_strings (abfd) = NULL;
1724 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1725 knit the symbol names into a normalized form. By normalized here I
1726 mean that all symbols have an n_offset pointer that points to a null-
1727 terminated string. */
1729 combined_entry_type *
1730 coff_get_normalized_symtab (bfd *abfd)
1732 combined_entry_type *internal;
1733 combined_entry_type *internal_ptr;
1734 combined_entry_type *symbol_ptr;
1735 combined_entry_type *internal_end;
1739 const char *string_table = NULL;
1740 char *debug_section = NULL;
1743 if (obj_raw_syments (abfd) != NULL)
1744 return obj_raw_syments (abfd);
1746 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1747 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1748 if (internal == NULL && size != 0)
1750 internal_end = internal + obj_raw_syment_count (abfd);
1752 if (! _bfd_coff_get_external_symbols (abfd))
1755 raw_src = (char *) obj_coff_external_syms (abfd);
1757 /* Mark the end of the symbols. */
1758 symesz = bfd_coff_symesz (abfd);
1759 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1761 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1762 probably possible. If one shows up, it will probably kill us. */
1764 /* Swap all the raw entries. */
1765 for (internal_ptr = internal;
1767 raw_src += symesz, internal_ptr++)
1771 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1772 (void *) & internal_ptr->u.syment);
1773 symbol_ptr = internal_ptr;
1776 i < symbol_ptr->u.syment.n_numaux;
1780 /* PR 17512: Prevent buffer overrun. */
1781 if (internal_ptr >= internal_end)
1785 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1786 symbol_ptr->u.syment.n_type,
1787 symbol_ptr->u.syment.n_sclass,
1788 (int) i, symbol_ptr->u.syment.n_numaux,
1789 &(internal_ptr->u.auxent));
1790 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1795 /* Free the raw symbols, but not the strings (if we have them). */
1796 obj_coff_keep_strings (abfd) = TRUE;
1797 if (! _bfd_coff_free_symbols (abfd))
1800 for (internal_ptr = internal; internal_ptr < internal_end;
1803 if (internal_ptr->u.syment.n_sclass == C_FILE
1804 && internal_ptr->u.syment.n_numaux > 0)
1806 /* Make a file symbol point to the name in the auxent, since
1807 the text ".file" is redundant. */
1808 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1810 /* The filename is a long one, point into the string table. */
1811 if (string_table == NULL)
1813 string_table = _bfd_coff_read_string_table (abfd);
1814 if (string_table == NULL)
1818 internal_ptr->u.syment._n._n_n._n_offset =
1821 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1825 /* Ordinary short filename, put into memory anyway. The
1826 Microsoft PE tools sometimes store a filename in
1827 multiple AUX entries. */
1828 if (internal_ptr->u.syment.n_numaux > 1
1829 && coff_data (abfd)->pe)
1830 internal_ptr->u.syment._n._n_n._n_offset =
1833 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1834 internal_ptr->u.syment.n_numaux * symesz));
1836 internal_ptr->u.syment._n._n_n._n_offset =
1839 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1840 (size_t) bfd_coff_filnmlen (abfd)));
1845 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1847 /* This is a "short" name. Make it long. */
1851 /* Find the length of this string without walking into memory
1853 for (i = 0; i < 8; ++i)
1854 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1857 newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1858 if (newstring == NULL)
1860 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1861 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1862 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1864 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1865 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1866 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1868 /* Long name already. Point symbol at the string in the
1870 if (string_table == NULL)
1872 string_table = _bfd_coff_read_string_table (abfd);
1873 if (string_table == NULL)
1876 internal_ptr->u.syment._n._n_n._n_offset =
1879 + internal_ptr->u.syment._n._n_n._n_offset));
1883 /* Long name in debug section. Very similar. */
1884 if (debug_section == NULL)
1885 debug_section = build_debug_section (abfd);
1886 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1887 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1890 internal_ptr += internal_ptr->u.syment.n_numaux;
1893 obj_raw_syments (abfd) = internal;
1894 BFD_ASSERT (obj_raw_syment_count (abfd)
1895 == (unsigned int) (internal_ptr - internal));
1901 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1903 if (bfd_get_format (abfd) != bfd_object)
1905 bfd_set_error (bfd_error_invalid_operation);
1908 return (asect->reloc_count + 1) * sizeof (arelent *);
1912 coff_make_empty_symbol (bfd *abfd)
1914 bfd_size_type amt = sizeof (coff_symbol_type);
1915 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1917 if (new_symbol == NULL)
1919 new_symbol->symbol.section = 0;
1920 new_symbol->native = 0;
1921 new_symbol->lineno = NULL;
1922 new_symbol->done_lineno = FALSE;
1923 new_symbol->symbol.the_bfd = abfd;
1925 return & new_symbol->symbol;
1928 /* Make a debugging symbol. */
1931 coff_bfd_make_debug_symbol (bfd *abfd,
1932 void * ptr ATTRIBUTE_UNUSED,
1933 unsigned long sz ATTRIBUTE_UNUSED)
1935 bfd_size_type amt = sizeof (coff_symbol_type);
1936 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
1938 if (new_symbol == NULL)
1940 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1941 (but shouldn't be a constant). */
1942 amt = sizeof (combined_entry_type) * 10;
1943 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1944 if (!new_symbol->native)
1946 new_symbol->symbol.section = bfd_abs_section_ptr;
1947 new_symbol->symbol.flags = BSF_DEBUGGING;
1948 new_symbol->lineno = NULL;
1949 new_symbol->done_lineno = FALSE;
1950 new_symbol->symbol.the_bfd = abfd;
1952 return & new_symbol->symbol;
1956 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1958 bfd_symbol_info (symbol, ret);
1960 if (coffsymbol (symbol)->native != NULL
1961 && coffsymbol (symbol)->native->fix_value)
1962 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1963 (bfd_hostptr_t) obj_raw_syments (abfd);
1966 /* Return the COFF syment for a symbol. */
1969 bfd_coff_get_syment (bfd *abfd,
1971 struct internal_syment *psyment)
1973 coff_symbol_type *csym;
1975 csym = coff_symbol_from (abfd, symbol);
1976 if (csym == NULL || csym->native == NULL)
1978 bfd_set_error (bfd_error_invalid_operation);
1982 *psyment = csym->native->u.syment;
1984 if (csym->native->fix_value)
1985 psyment->n_value = psyment->n_value -
1986 (bfd_hostptr_t) obj_raw_syments (abfd);
1988 /* FIXME: We should handle fix_line here. */
1993 /* Return the COFF auxent for a symbol. */
1996 bfd_coff_get_auxent (bfd *abfd,
1999 union internal_auxent *pauxent)
2001 coff_symbol_type *csym;
2002 combined_entry_type *ent;
2004 csym = coff_symbol_from (abfd, symbol);
2007 || csym->native == NULL
2008 || indx >= csym->native->u.syment.n_numaux)
2010 bfd_set_error (bfd_error_invalid_operation);
2014 ent = csym->native + indx + 1;
2016 *pauxent = ent->u.auxent;
2019 pauxent->x_sym.x_tagndx.l =
2020 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
2021 - obj_raw_syments (abfd));
2024 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2025 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2026 - obj_raw_syments (abfd));
2028 if (ent->fix_scnlen)
2029 pauxent->x_csect.x_scnlen.l =
2030 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2031 - obj_raw_syments (abfd));
2036 /* Print out information about COFF symbol. */
2039 coff_print_symbol (bfd *abfd,
2042 bfd_print_symbol_type how)
2044 FILE * file = (FILE *) filep;
2048 case bfd_print_symbol_name:
2049 fprintf (file, "%s", symbol->name);
2052 case bfd_print_symbol_more:
2053 fprintf (file, "coff %s %s",
2054 coffsymbol (symbol)->native ? "n" : "g",
2055 coffsymbol (symbol)->lineno ? "l" : " ");
2058 case bfd_print_symbol_all:
2059 if (coffsymbol (symbol)->native)
2063 combined_entry_type *combined = coffsymbol (symbol)->native;
2064 combined_entry_type *root = obj_raw_syments (abfd);
2065 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2067 fprintf (file, "[%3ld]", (long) (combined - root));
2069 if (! combined->fix_value)
2070 val = (bfd_vma) combined->u.syment.n_value;
2072 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
2074 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
2075 combined->u.syment.n_scnum,
2076 combined->u.syment.n_flags,
2077 combined->u.syment.n_type,
2078 combined->u.syment.n_sclass,
2079 combined->u.syment.n_numaux);
2080 bfd_fprintf_vma (abfd, file, val);
2081 fprintf (file, " %s", symbol->name);
2083 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2085 combined_entry_type *auxp = combined + aux + 1;
2089 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2091 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2093 fprintf (file, "\n");
2095 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2098 switch (combined->u.syment.n_sclass)
2101 fprintf (file, "File ");
2105 if (combined->u.syment.n_type == T_NULL)
2106 /* Probably a section symbol ? */
2108 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2109 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2110 auxp->u.auxent.x_scn.x_nreloc,
2111 auxp->u.auxent.x_scn.x_nlinno);
2112 if (auxp->u.auxent.x_scn.x_checksum != 0
2113 || auxp->u.auxent.x_scn.x_associated != 0
2114 || auxp->u.auxent.x_scn.x_comdat != 0)
2115 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2116 auxp->u.auxent.x_scn.x_checksum,
2117 auxp->u.auxent.x_scn.x_associated,
2118 auxp->u.auxent.x_scn.x_comdat);
2121 /* Otherwise fall through. */
2124 if (ISFCN (combined->u.syment.n_type))
2129 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2132 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2133 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2135 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2137 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2141 /* Otherwise fall through. */
2143 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2144 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2145 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2148 fprintf (file, " endndx %ld",
2150 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2158 fprintf (file, "\n%s :", l->u.sym->name);
2160 while (l->line_number)
2162 fprintf (file, "\n%4d : ", l->line_number);
2163 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2170 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2171 fprintf (file, " %-5s %s %s %s",
2172 symbol->section->name,
2173 coffsymbol (symbol)->native ? "n" : "g",
2174 coffsymbol (symbol)->lineno ? "l" : " ",
2180 /* Return whether a symbol name implies a local symbol. In COFF,
2181 local symbols generally start with ``.L''. Most targets use this
2182 function for the is_local_label_name entry point, but some may
2186 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2189 return name[0] == '.' && name[1] == 'L';
2192 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2193 section, calculate and return the name of the source file and the line
2194 nearest to the wanted location. */
2197 coff_find_nearest_line_with_names (bfd *abfd,
2201 const char **filename_ptr,
2202 const char **functionname_ptr,
2203 unsigned int *line_ptr,
2204 const struct dwarf_debug_section *debug_sections)
2208 unsigned int line_base;
2209 coff_data_type *cof = coff_data (abfd);
2210 /* Run through the raw syments if available. */
2211 combined_entry_type *p;
2212 combined_entry_type *pend;
2214 struct coff_section_tdata *sec_data;
2217 /* Before looking through the symbol table, try to use a .stab
2218 section to find the information. */
2219 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2220 &found, filename_ptr,
2221 functionname_ptr, line_ptr,
2222 &coff_data(abfd)->line_info))
2228 /* Also try examining DWARF2 debugging information. */
2229 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2230 filename_ptr, functionname_ptr,
2231 line_ptr, NULL, debug_sections, 0,
2232 &coff_data(abfd)->dwarf2_find_line_info))
2236 *functionname_ptr = 0;
2239 /* Don't try and find line numbers in a non coff file. */
2240 if (!bfd_family_coff (abfd))
2246 /* Find the first C_FILE symbol. */
2247 p = cof->raw_syments;
2251 pend = p + cof->raw_syment_count;
2254 if (p->u.syment.n_sclass == C_FILE)
2256 p += 1 + p->u.syment.n_numaux;
2264 /* Look through the C_FILE symbols to find the best one. */
2265 sec_vma = bfd_get_section_vma (abfd, section);
2266 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2267 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2271 combined_entry_type *p2;
2273 for (p2 = p + 1 + p->u.syment.n_numaux;
2275 p2 += 1 + p2->u.syment.n_numaux)
2277 if (p2->u.syment.n_scnum > 0
2279 == coff_section_from_bfd_index (abfd,
2280 p2->u.syment.n_scnum)))
2282 if (p2->u.syment.n_sclass == C_FILE)
2289 file_addr = (bfd_vma) p2->u.syment.n_value;
2290 /* PR 11512: Include the section address of the function name symbol. */
2291 if (p2->u.syment.n_scnum > 0)
2292 file_addr += coff_section_from_bfd_index (abfd,
2293 p2->u.syment.n_scnum)->vma;
2294 /* We use <= MAXDIFF here so that if we get a zero length
2295 file, we actually use the next file entry. */
2297 && offset + sec_vma >= file_addr
2298 && offset + sec_vma - file_addr <= maxdiff)
2300 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2301 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2304 /* Avoid endless loops on erroneous files by ensuring that
2305 we always move forward in the file. */
2306 if (p >= cof->raw_syments + p->u.syment.n_value)
2309 p = cof->raw_syments + p->u.syment.n_value;
2310 if (p > pend || p->u.syment.n_sclass != C_FILE)
2315 /* Now wander though the raw linenumbers of the section. */
2316 /* If we have been called on this section before, and the offset we
2317 want is further down then we can prime the lookup loop. */
2318 sec_data = coff_section_data (abfd, section);
2319 if (sec_data != NULL
2321 && offset >= sec_data->offset)
2324 *functionname_ptr = sec_data->function;
2325 line_base = sec_data->line_base;
2333 if (section->lineno != NULL)
2335 bfd_vma last_value = 0;
2337 l = §ion->lineno[i];
2339 for (; i < section->lineno_count; i++)
2341 if (l->line_number == 0)
2343 /* Get the symbol this line number points at. */
2344 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2345 if (coff->symbol.value > offset)
2347 *functionname_ptr = coff->symbol.name;
2348 last_value = coff->symbol.value;
2351 combined_entry_type *s = coff->native;
2352 s = s + 1 + s->u.syment.n_numaux;
2354 /* In XCOFF a debugging symbol can follow the
2356 if (s->u.syment.n_scnum == N_DEBUG)
2357 s = s + 1 + s->u.syment.n_numaux;
2359 /* S should now point to the .bf of the function. */
2360 if (s->u.syment.n_numaux)
2362 /* The linenumber is stored in the auxent. */
2363 union internal_auxent *a = &((s + 1)->u.auxent);
2364 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2365 *line_ptr = line_base;
2371 if (l->u.offset > offset)
2373 *line_ptr = l->line_number + line_base - 1;
2378 /* If we fell off the end of the loop, then assume that this
2379 symbol has no line number info. Otherwise, symbols with no
2380 line number info get reported with the line number of the
2381 last line of the last symbol which does have line number
2382 info. We use 0x100 as a slop to account for cases where the
2383 last line has executable code. */
2384 if (i >= section->lineno_count
2386 && offset - last_value > 0x100)
2388 *functionname_ptr = NULL;
2393 /* Cache the results for the next call. */
2394 if (sec_data == NULL && section->owner == abfd)
2396 amt = sizeof (struct coff_section_tdata);
2397 section->used_by_bfd = bfd_zalloc (abfd, amt);
2398 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2400 if (sec_data != NULL)
2402 sec_data->offset = offset;
2403 sec_data->i = i - 1;
2404 sec_data->function = *functionname_ptr;
2405 sec_data->line_base = line_base;
2412 coff_find_nearest_line (bfd *abfd,
2416 const char **filename_ptr,
2417 const char **functionname_ptr,
2418 unsigned int *line_ptr,
2419 unsigned int *discriminator_ptr)
2421 if (discriminator_ptr)
2422 *discriminator_ptr = 0;
2423 return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2424 filename_ptr, functionname_ptr,
2425 line_ptr, dwarf_debug_sections);
2429 coff_find_inliner_info (bfd *abfd,
2430 const char **filename_ptr,
2431 const char **functionname_ptr,
2432 unsigned int *line_ptr)
2436 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2437 functionname_ptr, line_ptr,
2438 &coff_data(abfd)->dwarf2_find_line_info);
2443 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2447 if (!info->relocatable)
2448 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2450 size = bfd_coff_filhsz (abfd);
2452 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2456 /* Change the class of a coff symbol held by BFD. */
2459 bfd_coff_set_symbol_class (bfd * abfd,
2461 unsigned int symbol_class)
2463 coff_symbol_type * csym;
2465 csym = coff_symbol_from (abfd, symbol);
2468 bfd_set_error (bfd_error_invalid_operation);
2471 else if (csym->native == NULL)
2473 /* This is an alien symbol which no native coff backend data.
2474 We cheat here by creating a fake native entry for it and
2475 then filling in the class. This code is based on that in
2476 coff_write_alien_symbol(). */
2478 combined_entry_type * native;
2479 bfd_size_type amt = sizeof (* native);
2481 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2485 native->u.syment.n_type = T_NULL;
2486 native->u.syment.n_sclass = symbol_class;
2488 if (bfd_is_und_section (symbol->section))
2490 native->u.syment.n_scnum = N_UNDEF;
2491 native->u.syment.n_value = symbol->value;
2493 else if (bfd_is_com_section (symbol->section))
2495 native->u.syment.n_scnum = N_UNDEF;
2496 native->u.syment.n_value = symbol->value;
2500 native->u.syment.n_scnum =
2501 symbol->section->output_section->target_index;
2502 native->u.syment.n_value = (symbol->value
2503 + symbol->section->output_offset);
2504 if (! obj_pe (abfd))
2505 native->u.syment.n_value += symbol->section->output_section->vma;
2507 /* Copy the any flags from the file header into the symbol.
2509 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2512 csym->native = native;
2515 csym->native->u.syment.n_sclass = symbol_class;
2520 struct coff_comdat_info *
2521 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2523 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2524 && coff_section_data (abfd, sec) != NULL)
2525 return coff_section_data (abfd, sec)->comdat;
2531 _bfd_coff_section_already_linked (bfd *abfd,
2533 struct bfd_link_info *info)
2536 const char *name, *key;
2537 struct bfd_section_already_linked *l;
2538 struct bfd_section_already_linked_hash_entry *already_linked_list;
2539 struct coff_comdat_info *s_comdat;
2542 if ((flags & SEC_LINK_ONCE) == 0)
2545 /* The COFF backend linker doesn't support group sections. */
2546 if ((flags & SEC_GROUP) != 0)
2549 name = bfd_get_section_name (abfd, sec);
2550 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2552 if (s_comdat != NULL)
2553 key = s_comdat->name;
2556 if (CONST_STRNEQ (name, ".gnu.linkonce.")
2557 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2560 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2561 .xdata$<key> and .pdata$<key> only the first of which has a
2562 comdat key. Should these all match the LTO IR key? */
2566 already_linked_list = bfd_section_already_linked_table_lookup (key);
2568 for (l = already_linked_list->entry; l != NULL; l = l->next)
2570 struct coff_comdat_info *l_comdat;
2572 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2574 /* The section names must match, and both sections must be
2575 comdat and have the same comdat name, or both sections must
2576 be non-comdat. LTO IR plugin sections are an exception. They
2577 are always named .gnu.linkonce.t.<key> (<key> is some string)
2578 and match any comdat section with comdat name of <key>, and
2579 any linkonce section with the same suffix, ie.
2580 .gnu.linkonce.*.<key>. */
2581 if (((s_comdat != NULL) == (l_comdat != NULL)
2582 && strcmp (name, l->sec->name) == 0)
2583 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
2585 /* The section has already been linked. See if we should
2587 return _bfd_handle_already_linked (sec, l, info);
2591 /* This is the first section with this name. Record it. */
2592 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2593 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));