1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
25 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
27 /* This file contains COFF code that is not dependent on any
28 particular COFF target. There is only one version of this file in
29 libbfd.a, so no target specific code may be put in here. Or, to
32 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
34 If you need to add some target specific behaviour, add a new hook
35 function to bfd_coff_backend_data.
37 Some of these functions are also called by the ECOFF routines.
38 Those functions may not use any COFF specific information, such as
44 #include "coff/internal.h"
47 /* Take a section header read from a coff file (in HOST byte order),
48 and make a BFD "section" out of it. This is used by ECOFF. */
51 make_a_section_from_file (bfd *abfd,
52 struct internal_scnhdr *hdr,
53 unsigned int target_index)
55 asection *return_section;
57 bfd_boolean result = TRUE;
62 /* Handle long section names as in PE. On reading, we want to
63 accept long names if the format permits them at all, regardless
64 of the current state of the flag that dictates if we would generate
65 them in outputs; this construct checks if that is the case by
66 attempting to set the flag, without changing its state; the call
67 will fail for formats that do not support long names at all. */
68 if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
69 && hdr->s_name[0] == '/')
76 /* Flag that this BFD uses long names, even though the format might
77 expect them to be off by default. This won't directly affect the
78 format of any output BFD created from this one, but the information
79 can be used to decide what to do. */
80 bfd_coff_set_long_section_names (abfd, TRUE);
81 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
82 buf[SCNNMLEN - 1] = '\0';
83 strindex = strtol (buf, &p, 10);
84 if (*p == '\0' && strindex >= 0)
86 strings = _bfd_coff_read_string_table (abfd);
89 /* FIXME: For extra safety, we should make sure that
90 strindex does not run us past the end, but right now we
91 don't know the length of the string table. */
93 name = (char *) bfd_alloc (abfd,
94 (bfd_size_type) strlen (strings) + 1 + 1);
97 strcpy (name, strings);
103 /* Assorted wastage to null-terminate the name, thanks AT&T! */
104 name = (char *) bfd_alloc (abfd,
105 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
108 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
109 name[sizeof (hdr->s_name)] = 0;
112 return_section = bfd_make_section_anyway (abfd, name);
113 if (return_section == NULL)
116 return_section->vma = hdr->s_vaddr;
117 return_section->lma = hdr->s_paddr;
118 return_section->size = hdr->s_size;
119 return_section->filepos = hdr->s_scnptr;
120 return_section->rel_filepos = hdr->s_relptr;
121 return_section->reloc_count = hdr->s_nreloc;
123 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
125 return_section->line_filepos = hdr->s_lnnoptr;
127 return_section->lineno_count = hdr->s_nlnno;
128 return_section->userdata = NULL;
129 return_section->next = NULL;
130 return_section->target_index = target_index;
132 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
136 return_section->flags = flags;
138 /* At least on i386-coff, the line number count for a shared library
139 section must be ignored. */
140 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
141 return_section->lineno_count = 0;
143 if (hdr->s_nreloc != 0)
144 return_section->flags |= SEC_RELOC;
145 /* FIXME: should this check 'hdr->s_size > 0'. */
146 if (hdr->s_scnptr != 0)
147 return_section->flags |= SEC_HAS_CONTENTS;
149 /* Compress/decompress DWARF debug sections with names: .debug_* and
150 .zdebug_*, after the section flags is set. */
151 if ((flags & SEC_DEBUGGING)
152 && ((name[1] == 'd' && name[6] == '_')
153 || (name[1] == 'z' && name[7] == '_')))
155 enum { nothing, compress, decompress } action = nothing;
156 char *new_name = NULL;
158 if (bfd_is_section_compressed (abfd, return_section))
160 /* Compressed section. Check if we should decompress. */
161 if ((abfd->flags & BFD_DECOMPRESS))
164 else if (!bfd_is_section_compressed (abfd, return_section))
166 /* Normal section. Check if we should compress. */
167 if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0)
176 if (!bfd_init_section_compress_status (abfd, return_section))
178 (*_bfd_error_handler)
179 (_("%B: unable to initialize compress status for section %s"),
185 unsigned int len = strlen (name);
187 new_name = bfd_alloc (abfd, len + 2);
188 if (new_name == NULL)
192 memcpy (new_name + 2, name + 1, len);
196 if (!bfd_init_section_decompress_status (abfd, return_section))
198 (*_bfd_error_handler)
199 (_("%B: unable to initialize decompress status for section %s"),
205 unsigned int len = strlen (name);
207 new_name = bfd_alloc (abfd, len);
208 if (new_name == NULL)
211 memcpy (new_name + 1, name + 2, len - 1);
215 if (new_name != NULL)
216 bfd_rename_section (abfd, return_section, new_name);
222 /* Read in a COFF object and make it into a BFD. This is used by
225 static const bfd_target *
226 coff_real_object_p (bfd *abfd,
228 struct internal_filehdr *internal_f,
229 struct internal_aouthdr *internal_a)
231 flagword oflags = abfd->flags;
232 bfd_vma ostart = bfd_get_start_address (abfd);
235 bfd_size_type readsize; /* Length of file_info. */
237 char *external_sections;
239 if (!(internal_f->f_flags & F_RELFLG))
240 abfd->flags |= HAS_RELOC;
241 if ((internal_f->f_flags & F_EXEC))
242 abfd->flags |= EXEC_P;
243 if (!(internal_f->f_flags & F_LNNO))
244 abfd->flags |= HAS_LINENO;
245 if (!(internal_f->f_flags & F_LSYMS))
246 abfd->flags |= HAS_LOCALS;
248 /* FIXME: How can we set D_PAGED correctly? */
249 if ((internal_f->f_flags & F_EXEC) != 0)
250 abfd->flags |= D_PAGED;
252 bfd_get_symcount (abfd) = internal_f->f_nsyms;
253 if (internal_f->f_nsyms)
254 abfd->flags |= HAS_SYMS;
256 if (internal_a != (struct internal_aouthdr *) NULL)
257 bfd_get_start_address (abfd) = internal_a->entry;
259 bfd_get_start_address (abfd) = 0;
261 /* Set up the tdata area. ECOFF uses its own routine, and overrides
263 tdata_save = abfd->tdata.any;
264 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
268 scnhsz = bfd_coff_scnhsz (abfd);
269 readsize = (bfd_size_type) nscns * scnhsz;
270 external_sections = (char *) bfd_alloc (abfd, readsize);
271 if (!external_sections)
274 if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
277 /* Set the arch/mach *before* swapping in sections; section header swapping
278 may depend on arch/mach info. */
279 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
282 /* Now copy data as required; construct all asections etc. */
286 for (i = 0; i < nscns; i++)
288 struct internal_scnhdr tmp;
289 bfd_coff_swap_scnhdr_in (abfd,
290 (void *) (external_sections + i * scnhsz),
292 if (! make_a_section_from_file (abfd, &tmp, i + 1))
300 bfd_release (abfd, tdata);
302 abfd->tdata.any = tdata_save;
303 abfd->flags = oflags;
304 bfd_get_start_address (abfd) = ostart;
305 return (const bfd_target *) NULL;
308 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
309 not a COFF file. This is also used by ECOFF. */
312 coff_object_p (bfd *abfd)
314 bfd_size_type filhsz;
315 bfd_size_type aoutsz;
318 struct internal_filehdr internal_f;
319 struct internal_aouthdr internal_a;
321 /* Figure out how much to read. */
322 filhsz = bfd_coff_filhsz (abfd);
323 aoutsz = bfd_coff_aoutsz (abfd);
325 filehdr = bfd_alloc (abfd, filhsz);
328 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
330 if (bfd_get_error () != bfd_error_system_call)
331 bfd_set_error (bfd_error_wrong_format);
332 bfd_release (abfd, filehdr);
335 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
336 bfd_release (abfd, filehdr);
338 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
339 (less than aoutsz) used in object files and AOUTSZ (equal to
340 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
341 expects this header to be aoutsz bytes in length, so we use that
342 value in the call to bfd_alloc below. But we must be careful to
343 only read in f_opthdr bytes in the call to bfd_bread. We should
344 also attempt to catch corrupt or non-COFF binaries with a strange
345 value for f_opthdr. */
346 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
347 || internal_f.f_opthdr > aoutsz)
349 bfd_set_error (bfd_error_wrong_format);
352 nscns = internal_f.f_nscns;
354 if (internal_f.f_opthdr)
358 opthdr = bfd_alloc (abfd, aoutsz);
361 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
362 != internal_f.f_opthdr)
364 bfd_release (abfd, opthdr);
367 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
368 bfd_release (abfd, opthdr);
371 return coff_real_object_p (abfd, nscns, &internal_f,
372 (internal_f.f_opthdr != 0
374 : (struct internal_aouthdr *) NULL));
377 /* Get the BFD section from a COFF symbol section number. */
380 coff_section_from_bfd_index (bfd *abfd, int section_index)
382 struct bfd_section *answer = abfd->sections;
384 if (section_index == N_ABS)
385 return bfd_abs_section_ptr;
386 if (section_index == N_UNDEF)
387 return bfd_und_section_ptr;
388 if (section_index == N_DEBUG)
389 return bfd_abs_section_ptr;
393 if (answer->target_index == section_index)
395 answer = answer->next;
398 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
399 has a bad symbol table in biglitpow.o. */
400 return bfd_und_section_ptr;
403 /* Get the upper bound of a COFF symbol table. */
406 coff_get_symtab_upper_bound (bfd *abfd)
408 if (!bfd_coff_slurp_symbol_table (abfd))
411 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
414 /* Canonicalize a COFF symbol table. */
417 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
419 unsigned int counter;
420 coff_symbol_type *symbase;
421 coff_symbol_type **location = (coff_symbol_type **) alocation;
423 if (!bfd_coff_slurp_symbol_table (abfd))
426 symbase = obj_symbols (abfd);
427 counter = bfd_get_symcount (abfd);
428 while (counter-- > 0)
429 *location++ = symbase++;
433 return bfd_get_symcount (abfd);
436 /* Get the name of a symbol. The caller must pass in a buffer of size
440 _bfd_coff_internal_syment_name (bfd *abfd,
441 const struct internal_syment *sym,
444 /* FIXME: It's not clear this will work correctly if sizeof
446 if (sym->_n._n_n._n_zeroes != 0
447 || sym->_n._n_n._n_offset == 0)
449 memcpy (buf, sym->_n._n_name, SYMNMLEN);
450 buf[SYMNMLEN] = '\0';
457 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
458 strings = obj_coff_strings (abfd);
461 strings = _bfd_coff_read_string_table (abfd);
465 return strings + sym->_n._n_n._n_offset;
469 /* Read in and swap the relocs. This returns a buffer holding the
470 relocs for section SEC in file ABFD. If CACHE is TRUE and
471 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
472 the function is called again. If EXTERNAL_RELOCS is not NULL, it
473 is a buffer large enough to hold the unswapped relocs. If
474 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
475 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
476 value must be INTERNAL_RELOCS. The function returns NULL on error. */
478 struct internal_reloc *
479 _bfd_coff_read_internal_relocs (bfd *abfd,
482 bfd_byte *external_relocs,
483 bfd_boolean require_internal,
484 struct internal_reloc *internal_relocs)
487 bfd_byte *free_external = NULL;
488 struct internal_reloc *free_internal = NULL;
491 struct internal_reloc *irel;
494 if (sec->reloc_count == 0)
495 return internal_relocs; /* Nothing to do. */
497 if (coff_section_data (abfd, sec) != NULL
498 && coff_section_data (abfd, sec)->relocs != NULL)
500 if (! require_internal)
501 return coff_section_data (abfd, sec)->relocs;
502 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
503 sec->reloc_count * sizeof (struct internal_reloc));
504 return internal_relocs;
507 relsz = bfd_coff_relsz (abfd);
509 amt = sec->reloc_count * relsz;
510 if (external_relocs == NULL)
512 free_external = (bfd_byte *) bfd_malloc (amt);
513 if (free_external == NULL)
515 external_relocs = free_external;
518 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
519 || bfd_bread (external_relocs, amt, abfd) != amt)
522 if (internal_relocs == NULL)
524 amt = sec->reloc_count;
525 amt *= sizeof (struct internal_reloc);
526 free_internal = (struct internal_reloc *) bfd_malloc (amt);
527 if (free_internal == NULL)
529 internal_relocs = free_internal;
532 /* Swap in the relocs. */
533 erel = external_relocs;
534 erel_end = erel + relsz * sec->reloc_count;
535 irel = internal_relocs;
536 for (; erel < erel_end; erel += relsz, irel++)
537 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
539 if (free_external != NULL)
541 free (free_external);
542 free_external = NULL;
545 if (cache && free_internal != NULL)
547 if (coff_section_data (abfd, sec) == NULL)
549 amt = sizeof (struct coff_section_tdata);
550 sec->used_by_bfd = bfd_zalloc (abfd, amt);
551 if (sec->used_by_bfd == NULL)
553 coff_section_data (abfd, sec)->contents = NULL;
555 coff_section_data (abfd, sec)->relocs = free_internal;
558 return internal_relocs;
561 if (free_external != NULL)
562 free (free_external);
563 if (free_internal != NULL)
564 free (free_internal);
568 /* Set lineno_count for the output sections of a COFF file. */
571 coff_count_linenumbers (bfd *abfd)
573 unsigned int limit = bfd_get_symcount (abfd);
581 /* This may be from the backend linker, in which case the
582 lineno_count in the sections is correct. */
583 for (s = abfd->sections; s != NULL; s = s->next)
584 total += s->lineno_count;
588 for (s = abfd->sections; s != NULL; s = s->next)
589 BFD_ASSERT (s->lineno_count == 0);
591 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
593 asymbol *q_maybe = *p;
595 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
597 coff_symbol_type *q = coffsymbol (q_maybe);
599 /* The AIX 4.1 compiler can sometimes generate line numbers
600 attached to debugging symbols. We try to simply ignore
602 if (q->lineno != NULL
603 && q->symbol.section->owner != NULL)
605 /* This symbol has line numbers. Increment the owning
606 section's linenumber count. */
607 alent *l = q->lineno;
611 asection * sec = q->symbol.section->output_section;
613 /* Do not try to update fields in read-only sections. */
614 if (! bfd_is_const_section (sec))
615 sec->lineno_count ++;
620 while (l->line_number != 0);
628 /* Takes a bfd and a symbol, returns a pointer to the coff specific
629 area of the symbol if there is one. */
632 coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
635 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
636 return (coff_symbol_type *) NULL;
638 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
639 return (coff_symbol_type *) NULL;
641 return (coff_symbol_type *) symbol;
645 fixup_symbol_value (bfd *abfd,
646 coff_symbol_type *coff_symbol_ptr,
647 struct internal_syment *syment)
649 /* Normalize the symbol flags. */
650 if (coff_symbol_ptr->symbol.section
651 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
653 /* A common symbol is undefined with a value. */
654 syment->n_scnum = N_UNDEF;
655 syment->n_value = coff_symbol_ptr->symbol.value;
657 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
658 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
660 syment->n_value = coff_symbol_ptr->symbol.value;
662 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
664 syment->n_scnum = N_UNDEF;
667 /* FIXME: Do we need to handle the absolute section here? */
670 if (coff_symbol_ptr->symbol.section)
673 coff_symbol_ptr->symbol.section->output_section->target_index;
675 syment->n_value = (coff_symbol_ptr->symbol.value
676 + coff_symbol_ptr->symbol.section->output_offset);
679 syment->n_value += (syment->n_sclass == C_STATLAB)
680 ? coff_symbol_ptr->symbol.section->output_section->lma
681 : coff_symbol_ptr->symbol.section->output_section->vma;
687 /* This can happen, but I don't know why yet (steve@cygnus.com) */
688 syment->n_scnum = N_ABS;
689 syment->n_value = coff_symbol_ptr->symbol.value;
694 /* Run through all the symbols in the symbol table and work out what
695 their indexes into the symbol table will be when output.
697 Coff requires that each C_FILE symbol points to the next one in the
698 chain, and that the last one points to the first external symbol. We
702 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
704 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
705 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
706 unsigned int native_index = 0;
707 struct internal_syment *last_file = NULL;
708 unsigned int symbol_index;
710 /* COFF demands that undefined symbols come after all other symbols.
711 Since we don't need to impose this extra knowledge on all our
712 client programs, deal with that here. Sort the symbol table;
713 just move the undefined symbols to the end, leaving the rest
714 alone. The O'Reilly book says that defined global symbols come
715 at the end before the undefined symbols, so we do that here as
717 /* @@ Do we have some condition we could test for, so we don't always
718 have to do this? I don't think relocatability is quite right, but
719 I'm not certain. [raeburn:19920508.1711EST] */
725 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
726 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
729 bfd_ptr->outsymbols = newsyms;
730 for (i = 0; i < symbol_count; i++)
731 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
732 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
733 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
734 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
735 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
737 *newsyms++ = symbol_ptr_ptr[i];
739 for (i = 0; i < symbol_count; i++)
740 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
741 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
742 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
743 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
744 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
746 *newsyms++ = symbol_ptr_ptr[i];
748 *first_undef = newsyms - bfd_ptr->outsymbols;
750 for (i = 0; i < symbol_count; i++)
751 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
752 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
753 *newsyms++ = symbol_ptr_ptr[i];
754 *newsyms = (asymbol *) NULL;
755 symbol_ptr_ptr = bfd_ptr->outsymbols;
758 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
760 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
761 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
762 if (coff_symbol_ptr && coff_symbol_ptr->native)
764 combined_entry_type *s = coff_symbol_ptr->native;
767 if (s->u.syment.n_sclass == C_FILE)
769 if (last_file != NULL)
770 last_file->n_value = native_index;
771 last_file = &(s->u.syment);
774 /* Modify the symbol values according to their section and
776 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
778 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
779 s[i].offset = native_index++;
785 obj_conv_table_size (bfd_ptr) = native_index;
790 /* Run thorough the symbol table again, and fix it so that all
791 pointers to entries are changed to the entries' index in the output
795 coff_mangle_symbols (bfd *bfd_ptr)
797 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
798 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
799 unsigned int symbol_index;
801 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
803 coff_symbol_type *coff_symbol_ptr =
804 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
806 if (coff_symbol_ptr && coff_symbol_ptr->native)
809 combined_entry_type *s = coff_symbol_ptr->native;
813 /* FIXME: We should use a union here. */
814 s->u.syment.n_value =
815 (bfd_hostptr_t) ((combined_entry_type *)
816 ((bfd_hostptr_t) s->u.syment.n_value))->offset;
821 /* The value is the offset into the line number entries
822 for the symbol's section. On output, the symbol's
823 section should be N_DEBUG. */
824 s->u.syment.n_value =
825 (coff_symbol_ptr->symbol.section->output_section->line_filepos
826 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
827 coff_symbol_ptr->symbol.section =
828 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
829 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
831 for (i = 0; i < s->u.syment.n_numaux; i++)
833 combined_entry_type *a = s + i + 1;
836 a->u.auxent.x_sym.x_tagndx.l =
837 a->u.auxent.x_sym.x_tagndx.p->offset;
842 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
843 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
848 a->u.auxent.x_csect.x_scnlen.l =
849 a->u.auxent.x_csect.x_scnlen.p->offset;
858 coff_fix_symbol_name (bfd *abfd,
860 combined_entry_type *native,
861 bfd_size_type *string_size_p,
862 asection **debug_string_section_p,
863 bfd_size_type *debug_string_size_p)
865 unsigned int name_length;
866 union internal_auxent *auxent;
867 char *name = (char *) (symbol->name);
871 /* COFF symbols always have names, so we'll make one up. */
872 symbol->name = "strange";
873 name = (char *) symbol->name;
875 name_length = strlen (name);
877 if (native->u.syment.n_sclass == C_FILE
878 && native->u.syment.n_numaux > 0)
880 unsigned int filnmlen;
882 if (bfd_coff_force_symnames_in_strings (abfd))
884 native->u.syment._n._n_n._n_offset =
885 (*string_size_p + STRING_SIZE_SIZE);
886 native->u.syment._n._n_n._n_zeroes = 0;
887 *string_size_p += 6; /* strlen(".file") + 1 */
890 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
892 auxent = &(native + 1)->u.auxent;
894 filnmlen = bfd_coff_filnmlen (abfd);
896 if (bfd_coff_long_filenames (abfd))
898 if (name_length <= filnmlen)
899 strncpy (auxent->x_file.x_fname, name, filnmlen);
902 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
903 auxent->x_file.x_n.x_zeroes = 0;
904 *string_size_p += name_length + 1;
909 strncpy (auxent->x_file.x_fname, name, filnmlen);
910 if (name_length > filnmlen)
911 name[filnmlen] = '\0';
916 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
917 /* This name will fit into the symbol neatly. */
918 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
920 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
922 native->u.syment._n._n_n._n_offset = (*string_size_p
924 native->u.syment._n._n_n._n_zeroes = 0;
925 *string_size_p += name_length + 1;
931 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
933 /* This name should be written into the .debug section. For
934 some reason each name is preceded by a two byte length
935 and also followed by a null byte. FIXME: We assume that
936 the .debug section has already been created, and that it
938 if (*debug_string_section_p == (asection *) NULL)
939 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
940 filepos = bfd_tell (abfd);
942 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
944 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
946 if (!bfd_set_section_contents (abfd,
947 *debug_string_section_p,
949 (file_ptr) *debug_string_size_p,
950 (bfd_size_type) prefix_len)
951 || !bfd_set_section_contents (abfd,
952 *debug_string_section_p,
953 (void *) symbol->name,
954 (file_ptr) (*debug_string_size_p
956 (bfd_size_type) name_length + 1))
958 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
960 native->u.syment._n._n_n._n_offset =
961 *debug_string_size_p + prefix_len;
962 native->u.syment._n._n_n._n_zeroes = 0;
963 *debug_string_size_p += name_length + 1 + prefix_len;
968 /* We need to keep track of the symbol index so that when we write out
969 the relocs we can get the index for a symbol. This method is a
972 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
974 /* Write a symbol out to a COFF file. */
977 coff_write_symbol (bfd *abfd,
979 combined_entry_type *native,
981 bfd_size_type *string_size_p,
982 asection **debug_string_section_p,
983 bfd_size_type *debug_string_size_p)
985 unsigned int numaux = native->u.syment.n_numaux;
986 int type = native->u.syment.n_type;
987 int n_sclass = (int) native->u.syment.n_sclass;
988 asection *output_section = symbol->section->output_section
989 ? symbol->section->output_section
992 bfd_size_type symesz;
994 if (native->u.syment.n_sclass == C_FILE)
995 symbol->flags |= BSF_DEBUGGING;
997 if (symbol->flags & BSF_DEBUGGING
998 && bfd_is_abs_section (symbol->section))
999 native->u.syment.n_scnum = N_DEBUG;
1001 else if (bfd_is_abs_section (symbol->section))
1002 native->u.syment.n_scnum = N_ABS;
1004 else if (bfd_is_und_section (symbol->section))
1005 native->u.syment.n_scnum = N_UNDEF;
1008 native->u.syment.n_scnum =
1009 output_section->target_index;
1011 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
1012 debug_string_section_p, debug_string_size_p);
1014 symesz = bfd_coff_symesz (abfd);
1015 buf = bfd_alloc (abfd, symesz);
1018 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1019 if (bfd_bwrite (buf, symesz, abfd) != symesz)
1021 bfd_release (abfd, buf);
1023 if (native->u.syment.n_numaux > 0)
1025 bfd_size_type auxesz;
1028 auxesz = bfd_coff_auxesz (abfd);
1029 buf = bfd_alloc (abfd, auxesz);
1032 for (j = 0; j < native->u.syment.n_numaux; j++)
1034 bfd_coff_swap_aux_out (abfd,
1035 &((native + j + 1)->u.auxent),
1036 type, n_sclass, (int) j,
1037 native->u.syment.n_numaux,
1039 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1042 bfd_release (abfd, buf);
1045 /* Store the index for use when we write out the relocs. */
1046 set_index (symbol, *written);
1048 *written += numaux + 1;
1052 /* Write out a symbol to a COFF file that does not come from a COFF
1053 file originally. This symbol may have been created by the linker,
1054 or we may be linking a non COFF file to a COFF file. */
1057 coff_write_alien_symbol (bfd *abfd,
1059 struct internal_syment *isym,
1061 bfd_size_type *string_size_p,
1062 asection **debug_string_section_p,
1063 bfd_size_type *debug_string_size_p)
1065 combined_entry_type *native;
1066 combined_entry_type dummy[2];
1067 asection *output_section = symbol->section->output_section
1068 ? symbol->section->output_section
1070 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1073 if ((!link_info || link_info->strip_discarded)
1074 && !bfd_is_abs_section (symbol->section)
1075 && symbol->section->output_section == bfd_abs_section_ptr)
1079 memset (isym, 0, sizeof(*isym));
1083 native->u.syment.n_type = T_NULL;
1084 native->u.syment.n_flags = 0;
1085 native->u.syment.n_numaux = 0;
1086 if (bfd_is_und_section (symbol->section))
1088 native->u.syment.n_scnum = N_UNDEF;
1089 native->u.syment.n_value = symbol->value;
1091 else if (bfd_is_com_section (symbol->section))
1093 native->u.syment.n_scnum = N_UNDEF;
1094 native->u.syment.n_value = symbol->value;
1096 else if (symbol->flags & BSF_FILE)
1098 native->u.syment.n_scnum = N_DEBUG;
1099 native->u.syment.n_numaux = 1;
1101 else if (symbol->flags & BSF_DEBUGGING)
1103 /* There isn't much point to writing out a debugging symbol
1104 unless we are prepared to convert it into COFF debugging
1105 format. So, we just ignore them. We must clobber the symbol
1106 name to keep it from being put in the string table. */
1109 memset (isym, 0, sizeof(*isym));
1114 native->u.syment.n_scnum = output_section->target_index;
1115 native->u.syment.n_value = (symbol->value
1116 + symbol->section->output_offset);
1117 if (! obj_pe (abfd))
1118 native->u.syment.n_value += output_section->vma;
1120 /* Copy the any flags from the file header into the symbol.
1123 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1124 if (c != (coff_symbol_type *) NULL)
1125 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1129 native->u.syment.n_type = 0;
1130 if (symbol->flags & BSF_FILE)
1131 native->u.syment.n_sclass = C_FILE;
1132 else if (symbol->flags & BSF_LOCAL)
1133 native->u.syment.n_sclass = C_STAT;
1134 else if (symbol->flags & BSF_WEAK)
1135 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1137 native->u.syment.n_sclass = C_EXT;
1139 ret = coff_write_symbol (abfd, symbol, native, written, string_size_p,
1140 debug_string_section_p, debug_string_size_p);
1142 *isym = native->u.syment;
1146 /* Write a native symbol to a COFF file. */
1149 coff_write_native_symbol (bfd *abfd,
1150 coff_symbol_type *symbol,
1152 bfd_size_type *string_size_p,
1153 asection **debug_string_section_p,
1154 bfd_size_type *debug_string_size_p)
1156 combined_entry_type *native = symbol->native;
1157 alent *lineno = symbol->lineno;
1158 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1160 if ((!link_info || link_info->strip_discarded)
1161 && !bfd_is_abs_section (symbol->symbol.section)
1162 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1164 symbol->symbol.name = "";
1168 /* If this symbol has an associated line number, we must store the
1169 symbol index in the line number field. We also tag the auxent to
1170 point to the right place in the lineno table. */
1171 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1173 unsigned int count = 0;
1175 lineno[count].u.offset = *written;
1176 if (native->u.syment.n_numaux)
1178 union internal_auxent *a = &((native + 1)->u.auxent);
1180 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1181 symbol->symbol.section->output_section->moving_line_filepos;
1184 /* Count and relocate all other linenumbers. */
1186 while (lineno[count].line_number != 0)
1188 lineno[count].u.offset +=
1189 (symbol->symbol.section->output_section->vma
1190 + symbol->symbol.section->output_offset);
1193 symbol->done_lineno = TRUE;
1195 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1196 symbol->symbol.section->output_section->moving_line_filepos +=
1197 count * bfd_coff_linesz (abfd);
1200 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1201 string_size_p, debug_string_section_p,
1202 debug_string_size_p);
1206 null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
1210 /* Write out the COFF symbols. */
1213 coff_write_symbols (bfd *abfd)
1215 bfd_size_type string_size;
1216 asection *debug_string_section;
1217 bfd_size_type debug_string_size;
1219 unsigned int limit = bfd_get_symcount (abfd);
1220 bfd_vma written = 0;
1224 debug_string_section = NULL;
1225 debug_string_size = 0;
1227 /* If this target supports long section names, they must be put into
1228 the string table. This is supported by PE. This code must
1229 handle section names just as they are handled in
1230 coff_write_object_contents. */
1231 if (bfd_coff_long_section_names (abfd))
1235 for (o = abfd->sections; o != NULL; o = o->next)
1239 len = strlen (o->name);
1241 string_size += len + 1;
1245 /* Seek to the right place. */
1246 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1249 /* Output all the symbols we have. */
1251 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1253 asymbol *symbol = *p;
1254 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1256 if (c_symbol == (coff_symbol_type *) NULL
1257 || c_symbol->native == (combined_entry_type *) NULL)
1259 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1260 &string_size, &debug_string_section,
1261 &debug_string_size))
1266 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1268 bfd_error_handler_type current_error_handler;
1269 enum coff_symbol_classification sym_class;
1270 unsigned char *n_sclass;
1272 /* Suppress error reporting by bfd_coff_classify_symbol.
1273 Error messages can be generated when we are processing a local
1274 symbol which has no associated section and we do not have to
1275 worry about this, all we need to know is that it is local. */
1276 current_error_handler = bfd_set_error_handler (null_error_handler);
1277 sym_class = bfd_coff_classify_symbol (abfd,
1278 &c_symbol->native->u.syment);
1279 (void) bfd_set_error_handler (current_error_handler);
1281 n_sclass = &c_symbol->native->u.syment.n_sclass;
1283 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1284 we cannot retain the existing sclass from the original symbol.
1285 Weak symbols only have one valid sclass, so just set it always.
1286 If it is not local class and should be, set it C_STAT.
1287 If it is global and not classified as global, or if it is
1288 weak (which is also classified as global), set it C_EXT. */
1290 if (symbol->flags & BSF_WEAK)
1291 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1292 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1294 else if (symbol->flags & BSF_GLOBAL
1295 && (sym_class != COFF_SYMBOL_GLOBAL
1297 || *n_sclass == C_NT_WEAK
1299 || *n_sclass == C_WEAKEXT))
1300 c_symbol->native->u.syment.n_sclass = C_EXT;
1303 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1304 &string_size, &debug_string_section,
1305 &debug_string_size))
1310 obj_raw_syment_count (abfd) = written;
1312 /* Now write out strings. */
1313 if (string_size != 0)
1315 unsigned int size = string_size + STRING_SIZE_SIZE;
1316 bfd_byte buffer[STRING_SIZE_SIZE];
1318 #if STRING_SIZE_SIZE == 4
1319 H_PUT_32 (abfd, size, buffer);
1321 #error Change H_PUT_32
1323 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1327 /* Handle long section names. This code must handle section
1328 names just as they are handled in coff_write_object_contents. */
1329 if (bfd_coff_long_section_names (abfd))
1333 for (o = abfd->sections; o != NULL; o = o->next)
1337 len = strlen (o->name);
1340 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1347 for (p = abfd->outsymbols, i = 0;
1352 size_t name_length = strlen (q->name);
1353 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1356 /* Figure out whether the symbol name should go in the string
1357 table. Symbol names that are short enough are stored
1358 directly in the syment structure. File names permit a
1359 different, longer, length in the syment structure. On
1360 XCOFF, some symbol names are stored in the .debug section
1361 rather than in the string table. */
1363 if (c_symbol == NULL
1364 || c_symbol->native == NULL)
1365 /* This is not a COFF symbol, so it certainly is not a
1366 file name, nor does it go in the .debug section. */
1367 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1369 else if (bfd_coff_symname_in_debug (abfd,
1370 &c_symbol->native->u.syment))
1371 /* This symbol name is in the XCOFF .debug section.
1372 Don't write it into the string table. */
1373 maxlen = name_length;
1375 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1376 && c_symbol->native->u.syment.n_numaux > 0)
1378 if (bfd_coff_force_symnames_in_strings (abfd))
1380 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1383 maxlen = bfd_coff_filnmlen (abfd);
1386 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1388 if (name_length > maxlen)
1390 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1391 abfd) != name_length + 1)
1398 /* We would normally not write anything here, but we'll write
1399 out 4 so that any stupid coff reader which tries to read the
1400 string table even when there isn't one won't croak. */
1401 unsigned int size = STRING_SIZE_SIZE;
1402 bfd_byte buffer[STRING_SIZE_SIZE];
1404 #if STRING_SIZE_SIZE == 4
1405 H_PUT_32 (abfd, size, buffer);
1407 #error Change H_PUT_32
1409 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1410 != STRING_SIZE_SIZE)
1414 /* Make sure the .debug section was created to be the correct size.
1415 We should create it ourselves on the fly, but we don't because
1416 BFD won't let us write to any section until we know how large all
1417 the sections are. We could still do it by making another pass
1418 over the symbols. FIXME. */
1419 BFD_ASSERT (debug_string_size == 0
1420 || (debug_string_section != (asection *) NULL
1421 && (BFD_ALIGN (debug_string_size,
1422 1 << debug_string_section->alignment_power)
1423 == debug_string_section->size)));
1429 coff_write_linenumbers (bfd *abfd)
1432 bfd_size_type linesz;
1435 linesz = bfd_coff_linesz (abfd);
1436 buff = bfd_alloc (abfd, linesz);
1439 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1441 if (s->lineno_count)
1443 asymbol **q = abfd->outsymbols;
1444 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1446 /* Find all the linenumbers in this section. */
1450 if (p->section->output_section == s)
1453 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1454 (bfd_asymbol_bfd (p), p));
1457 /* Found a linenumber entry, output. */
1458 struct internal_lineno out;
1459 memset ((void *) & out, 0, sizeof (out));
1461 out.l_addr.l_symndx = l->u.offset;
1462 bfd_coff_swap_lineno_out (abfd, &out, buff);
1463 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1467 while (l->line_number)
1469 out.l_lnno = l->line_number;
1470 out.l_addr.l_symndx = l->u.offset;
1471 bfd_coff_swap_lineno_out (abfd, &out, buff);
1472 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1483 bfd_release (abfd, buff);
1488 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1490 return coffsymbol (symbol)->lineno;
1493 /* This function transforms the offsets into the symbol table into
1494 pointers to syments. */
1497 coff_pointerize_aux (bfd *abfd,
1498 combined_entry_type *table_base,
1499 combined_entry_type *symbol,
1500 unsigned int indaux,
1501 combined_entry_type *auxent)
1503 unsigned int type = symbol->u.syment.n_type;
1504 unsigned int n_sclass = symbol->u.syment.n_sclass;
1506 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1508 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1509 (abfd, table_base, symbol, indaux, auxent))
1513 /* Don't bother if this is a file or a section. */
1514 if (n_sclass == C_STAT && type == T_NULL)
1516 if (n_sclass == C_FILE)
1519 /* Otherwise patch up. */
1520 #define N_TMASK coff_data (abfd)->local_n_tmask
1521 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1523 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1524 || n_sclass == C_FCN)
1525 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1527 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1528 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1529 auxent->fix_end = 1;
1531 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1532 generate one, so we must be careful to ignore it. */
1533 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1535 auxent->u.auxent.x_sym.x_tagndx.p =
1536 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1537 auxent->fix_tag = 1;
1541 /* Allocate space for the ".debug" section, and read it.
1542 We did not read the debug section until now, because
1543 we didn't want to go to the trouble until someone needed it. */
1546 build_debug_section (bfd *abfd)
1548 char *debug_section;
1550 bfd_size_type sec_size;
1552 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1556 bfd_set_error (bfd_error_no_debug_section);
1560 sec_size = sect->size;
1561 debug_section = (char *) bfd_alloc (abfd, sec_size);
1562 if (debug_section == NULL)
1565 /* Seek to the beginning of the `.debug' section and read it.
1566 Save the current position first; it is needed by our caller.
1567 Then read debug section and reset the file pointer. */
1569 position = bfd_tell (abfd);
1570 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1571 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1572 || bfd_seek (abfd, position, SEEK_SET) != 0)
1574 return debug_section;
1577 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1578 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1579 be \0-terminated. */
1582 copy_name (bfd *abfd, char *name, size_t maxlen)
1587 for (len = 0; len < maxlen; ++len)
1588 if (name[len] == '\0')
1591 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1594 strncpy (newname, name, len);
1595 newname[len] = '\0';
1599 /* Read in the external symbols. */
1602 _bfd_coff_get_external_symbols (bfd *abfd)
1604 bfd_size_type symesz;
1608 if (obj_coff_external_syms (abfd) != NULL)
1611 symesz = bfd_coff_symesz (abfd);
1613 size = obj_raw_syment_count (abfd) * symesz;
1617 syms = bfd_malloc (size);
1621 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1622 || bfd_bread (syms, size, abfd) != size)
1629 obj_coff_external_syms (abfd) = syms;
1634 /* Read in the external strings. The strings are not loaded until
1635 they are needed. This is because we have no simple way of
1636 detecting a missing string table in an archive. */
1639 _bfd_coff_read_string_table (bfd *abfd)
1641 char extstrsize[STRING_SIZE_SIZE];
1642 bfd_size_type strsize;
1646 if (obj_coff_strings (abfd) != NULL)
1647 return obj_coff_strings (abfd);
1649 if (obj_sym_filepos (abfd) == 0)
1651 bfd_set_error (bfd_error_no_symbols);
1655 pos = obj_sym_filepos (abfd);
1656 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1657 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1660 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1661 != sizeof extstrsize)
1663 if (bfd_get_error () != bfd_error_file_truncated)
1666 /* There is no string table. */
1667 strsize = STRING_SIZE_SIZE;
1671 #if STRING_SIZE_SIZE == 4
1672 strsize = H_GET_32 (abfd, extstrsize);
1674 #error Change H_GET_32
1678 if (strsize < STRING_SIZE_SIZE)
1680 (*_bfd_error_handler)
1681 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1682 bfd_set_error (bfd_error_bad_value);
1686 strings = (char *) bfd_malloc (strsize);
1687 if (strings == NULL)
1690 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1691 != strsize - STRING_SIZE_SIZE)
1697 obj_coff_strings (abfd) = strings;
1702 /* Free up the external symbols and strings read from a COFF file. */
1705 _bfd_coff_free_symbols (bfd *abfd)
1707 if (obj_coff_external_syms (abfd) != NULL
1708 && ! obj_coff_keep_syms (abfd))
1710 free (obj_coff_external_syms (abfd));
1711 obj_coff_external_syms (abfd) = NULL;
1713 if (obj_coff_strings (abfd) != NULL
1714 && ! obj_coff_keep_strings (abfd))
1716 free (obj_coff_strings (abfd));
1717 obj_coff_strings (abfd) = NULL;
1722 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1723 knit the symbol names into a normalized form. By normalized here I
1724 mean that all symbols have an n_offset pointer that points to a null-
1725 terminated string. */
1727 combined_entry_type *
1728 coff_get_normalized_symtab (bfd *abfd)
1730 combined_entry_type *internal;
1731 combined_entry_type *internal_ptr;
1732 combined_entry_type *symbol_ptr;
1733 combined_entry_type *internal_end;
1737 const char *string_table = NULL;
1738 char *debug_section = NULL;
1741 if (obj_raw_syments (abfd) != NULL)
1742 return obj_raw_syments (abfd);
1744 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1745 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1746 if (internal == NULL && size != 0)
1748 internal_end = internal + obj_raw_syment_count (abfd);
1750 if (! _bfd_coff_get_external_symbols (abfd))
1753 raw_src = (char *) obj_coff_external_syms (abfd);
1755 /* Mark the end of the symbols. */
1756 symesz = bfd_coff_symesz (abfd);
1757 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1759 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1760 probably possible. If one shows up, it will probably kill us. */
1762 /* Swap all the raw entries. */
1763 for (internal_ptr = internal;
1765 raw_src += symesz, internal_ptr++)
1769 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1770 (void *) & internal_ptr->u.syment);
1771 symbol_ptr = internal_ptr;
1774 i < symbol_ptr->u.syment.n_numaux;
1779 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1780 symbol_ptr->u.syment.n_type,
1781 symbol_ptr->u.syment.n_sclass,
1782 (int) i, symbol_ptr->u.syment.n_numaux,
1783 &(internal_ptr->u.auxent));
1784 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1789 /* Free the raw symbols, but not the strings (if we have them). */
1790 obj_coff_keep_strings (abfd) = TRUE;
1791 if (! _bfd_coff_free_symbols (abfd))
1794 for (internal_ptr = internal; internal_ptr < internal_end;
1797 if (internal_ptr->u.syment.n_sclass == C_FILE
1798 && internal_ptr->u.syment.n_numaux > 0)
1800 /* Make a file symbol point to the name in the auxent, since
1801 the text ".file" is redundant. */
1802 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1804 /* The filename is a long one, point into the string table. */
1805 if (string_table == NULL)
1807 string_table = _bfd_coff_read_string_table (abfd);
1808 if (string_table == NULL)
1812 internal_ptr->u.syment._n._n_n._n_offset =
1815 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1819 /* Ordinary short filename, put into memory anyway. The
1820 Microsoft PE tools sometimes store a filename in
1821 multiple AUX entries. */
1822 if (internal_ptr->u.syment.n_numaux > 1
1823 && coff_data (abfd)->pe)
1824 internal_ptr->u.syment._n._n_n._n_offset =
1827 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1828 internal_ptr->u.syment.n_numaux * symesz));
1830 internal_ptr->u.syment._n._n_n._n_offset =
1833 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1834 (size_t) bfd_coff_filnmlen (abfd)));
1839 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1841 /* This is a "short" name. Make it long. */
1845 /* Find the length of this string without walking into memory
1847 for (i = 0; i < 8; ++i)
1848 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1851 newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1852 if (newstring == NULL)
1854 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1855 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1856 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1858 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1859 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1860 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1862 /* Long name already. Point symbol at the string in the
1864 if (string_table == NULL)
1866 string_table = _bfd_coff_read_string_table (abfd);
1867 if (string_table == NULL)
1870 internal_ptr->u.syment._n._n_n._n_offset =
1873 + internal_ptr->u.syment._n._n_n._n_offset));
1877 /* Long name in debug section. Very similar. */
1878 if (debug_section == NULL)
1879 debug_section = build_debug_section (abfd);
1880 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1881 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1884 internal_ptr += internal_ptr->u.syment.n_numaux;
1887 obj_raw_syments (abfd) = internal;
1888 BFD_ASSERT (obj_raw_syment_count (abfd)
1889 == (unsigned int) (internal_ptr - internal));
1895 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1897 if (bfd_get_format (abfd) != bfd_object)
1899 bfd_set_error (bfd_error_invalid_operation);
1902 return (asect->reloc_count + 1) * sizeof (arelent *);
1906 coff_make_empty_symbol (bfd *abfd)
1908 bfd_size_type amt = sizeof (coff_symbol_type);
1909 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1911 if (new_symbol == NULL)
1913 new_symbol->symbol.section = 0;
1914 new_symbol->native = 0;
1915 new_symbol->lineno = NULL;
1916 new_symbol->done_lineno = FALSE;
1917 new_symbol->symbol.the_bfd = abfd;
1919 return & new_symbol->symbol;
1922 /* Make a debugging symbol. */
1925 coff_bfd_make_debug_symbol (bfd *abfd,
1926 void * ptr ATTRIBUTE_UNUSED,
1927 unsigned long sz ATTRIBUTE_UNUSED)
1929 bfd_size_type amt = sizeof (coff_symbol_type);
1930 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
1932 if (new_symbol == NULL)
1934 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1935 (but shouldn't be a constant). */
1936 amt = sizeof (combined_entry_type) * 10;
1937 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1938 if (!new_symbol->native)
1940 new_symbol->symbol.section = bfd_abs_section_ptr;
1941 new_symbol->symbol.flags = BSF_DEBUGGING;
1942 new_symbol->lineno = NULL;
1943 new_symbol->done_lineno = FALSE;
1944 new_symbol->symbol.the_bfd = abfd;
1946 return & new_symbol->symbol;
1950 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1952 bfd_symbol_info (symbol, ret);
1954 if (coffsymbol (symbol)->native != NULL
1955 && coffsymbol (symbol)->native->fix_value)
1956 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1957 (bfd_hostptr_t) obj_raw_syments (abfd);
1960 /* Return the COFF syment for a symbol. */
1963 bfd_coff_get_syment (bfd *abfd,
1965 struct internal_syment *psyment)
1967 coff_symbol_type *csym;
1969 csym = coff_symbol_from (abfd, symbol);
1970 if (csym == NULL || csym->native == NULL)
1972 bfd_set_error (bfd_error_invalid_operation);
1976 *psyment = csym->native->u.syment;
1978 if (csym->native->fix_value)
1979 psyment->n_value = psyment->n_value -
1980 (bfd_hostptr_t) obj_raw_syments (abfd);
1982 /* FIXME: We should handle fix_line here. */
1987 /* Return the COFF auxent for a symbol. */
1990 bfd_coff_get_auxent (bfd *abfd,
1993 union internal_auxent *pauxent)
1995 coff_symbol_type *csym;
1996 combined_entry_type *ent;
1998 csym = coff_symbol_from (abfd, symbol);
2001 || csym->native == NULL
2002 || indx >= csym->native->u.syment.n_numaux)
2004 bfd_set_error (bfd_error_invalid_operation);
2008 ent = csym->native + indx + 1;
2010 *pauxent = ent->u.auxent;
2013 pauxent->x_sym.x_tagndx.l =
2014 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
2015 - obj_raw_syments (abfd));
2018 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2019 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2020 - obj_raw_syments (abfd));
2022 if (ent->fix_scnlen)
2023 pauxent->x_csect.x_scnlen.l =
2024 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2025 - obj_raw_syments (abfd));
2030 /* Print out information about COFF symbol. */
2033 coff_print_symbol (bfd *abfd,
2036 bfd_print_symbol_type how)
2038 FILE * file = (FILE *) filep;
2042 case bfd_print_symbol_name:
2043 fprintf (file, "%s", symbol->name);
2046 case bfd_print_symbol_more:
2047 fprintf (file, "coff %s %s",
2048 coffsymbol (symbol)->native ? "n" : "g",
2049 coffsymbol (symbol)->lineno ? "l" : " ");
2052 case bfd_print_symbol_all:
2053 if (coffsymbol (symbol)->native)
2057 combined_entry_type *combined = coffsymbol (symbol)->native;
2058 combined_entry_type *root = obj_raw_syments (abfd);
2059 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2061 fprintf (file, "[%3ld]", (long) (combined - root));
2063 if (! combined->fix_value)
2064 val = (bfd_vma) combined->u.syment.n_value;
2066 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
2068 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
2069 combined->u.syment.n_scnum,
2070 combined->u.syment.n_flags,
2071 combined->u.syment.n_type,
2072 combined->u.syment.n_sclass,
2073 combined->u.syment.n_numaux);
2074 bfd_fprintf_vma (abfd, file, val);
2075 fprintf (file, " %s", symbol->name);
2077 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2079 combined_entry_type *auxp = combined + aux + 1;
2083 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2085 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2087 fprintf (file, "\n");
2089 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2092 switch (combined->u.syment.n_sclass)
2095 fprintf (file, "File ");
2099 if (combined->u.syment.n_type == T_NULL)
2100 /* Probably a section symbol ? */
2102 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2103 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2104 auxp->u.auxent.x_scn.x_nreloc,
2105 auxp->u.auxent.x_scn.x_nlinno);
2106 if (auxp->u.auxent.x_scn.x_checksum != 0
2107 || auxp->u.auxent.x_scn.x_associated != 0
2108 || auxp->u.auxent.x_scn.x_comdat != 0)
2109 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2110 auxp->u.auxent.x_scn.x_checksum,
2111 auxp->u.auxent.x_scn.x_associated,
2112 auxp->u.auxent.x_scn.x_comdat);
2115 /* Otherwise fall through. */
2118 if (ISFCN (combined->u.syment.n_type))
2123 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2126 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2127 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2129 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2131 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2135 /* Otherwise fall through. */
2137 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2138 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2139 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2142 fprintf (file, " endndx %ld",
2144 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2152 fprintf (file, "\n%s :", l->u.sym->name);
2154 while (l->line_number)
2156 fprintf (file, "\n%4d : ", l->line_number);
2157 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2164 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2165 fprintf (file, " %-5s %s %s %s",
2166 symbol->section->name,
2167 coffsymbol (symbol)->native ? "n" : "g",
2168 coffsymbol (symbol)->lineno ? "l" : " ",
2174 /* Return whether a symbol name implies a local symbol. In COFF,
2175 local symbols generally start with ``.L''. Most targets use this
2176 function for the is_local_label_name entry point, but some may
2180 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2183 return name[0] == '.' && name[1] == 'L';
2186 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2187 section, calculate and return the name of the source file and the line
2188 nearest to the wanted location. */
2191 coff_find_nearest_line_with_names (bfd *abfd,
2192 const struct dwarf_debug_section *debug_sections,
2196 const char **filename_ptr,
2197 const char **functionname_ptr,
2198 unsigned int *line_ptr)
2202 unsigned int line_base;
2203 coff_data_type *cof = coff_data (abfd);
2204 /* Run through the raw syments if available. */
2205 combined_entry_type *p;
2206 combined_entry_type *pend;
2208 struct coff_section_tdata *sec_data;
2211 /* Before looking through the symbol table, try to use a .stab
2212 section to find the information. */
2213 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2214 &found, filename_ptr,
2215 functionname_ptr, line_ptr,
2216 &coff_data(abfd)->line_info))
2222 /* Also try examining DWARF2 debugging information. */
2223 if (_bfd_dwarf2_find_nearest_line (abfd, debug_sections,
2224 section, symbols, offset,
2225 filename_ptr, functionname_ptr,
2227 &coff_data(abfd)->dwarf2_find_line_info))
2231 *functionname_ptr = 0;
2234 /* Don't try and find line numbers in a non coff file. */
2235 if (!bfd_family_coff (abfd))
2241 /* Find the first C_FILE symbol. */
2242 p = cof->raw_syments;
2246 pend = p + cof->raw_syment_count;
2249 if (p->u.syment.n_sclass == C_FILE)
2251 p += 1 + p->u.syment.n_numaux;
2259 /* Look through the C_FILE symbols to find the best one. */
2260 sec_vma = bfd_get_section_vma (abfd, section);
2261 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2262 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2266 combined_entry_type *p2;
2268 for (p2 = p + 1 + p->u.syment.n_numaux;
2270 p2 += 1 + p2->u.syment.n_numaux)
2272 if (p2->u.syment.n_scnum > 0
2274 == coff_section_from_bfd_index (abfd,
2275 p2->u.syment.n_scnum)))
2277 if (p2->u.syment.n_sclass == C_FILE)
2284 file_addr = (bfd_vma) p2->u.syment.n_value;
2285 /* PR 11512: Include the section address of the function name symbol. */
2286 if (p2->u.syment.n_scnum > 0)
2287 file_addr += coff_section_from_bfd_index (abfd,
2288 p2->u.syment.n_scnum)->vma;
2289 /* We use <= MAXDIFF here so that if we get a zero length
2290 file, we actually use the next file entry. */
2292 && offset + sec_vma >= file_addr
2293 && offset + sec_vma - file_addr <= maxdiff)
2295 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2296 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2299 /* Avoid endless loops on erroneous files by ensuring that
2300 we always move forward in the file. */
2301 if (p >= cof->raw_syments + p->u.syment.n_value)
2304 p = cof->raw_syments + p->u.syment.n_value;
2305 if (p > pend || p->u.syment.n_sclass != C_FILE)
2310 /* Now wander though the raw linenumbers of the section. */
2311 /* If we have been called on this section before, and the offset we
2312 want is further down then we can prime the lookup loop. */
2313 sec_data = coff_section_data (abfd, section);
2314 if (sec_data != NULL
2316 && offset >= sec_data->offset)
2319 *functionname_ptr = sec_data->function;
2320 line_base = sec_data->line_base;
2328 if (section->lineno != NULL)
2330 bfd_vma last_value = 0;
2332 l = §ion->lineno[i];
2334 for (; i < section->lineno_count; i++)
2336 if (l->line_number == 0)
2338 /* Get the symbol this line number points at. */
2339 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2340 if (coff->symbol.value > offset)
2342 *functionname_ptr = coff->symbol.name;
2343 last_value = coff->symbol.value;
2346 combined_entry_type *s = coff->native;
2347 s = s + 1 + s->u.syment.n_numaux;
2349 /* In XCOFF a debugging symbol can follow the
2351 if (s->u.syment.n_scnum == N_DEBUG)
2352 s = s + 1 + s->u.syment.n_numaux;
2354 /* S should now point to the .bf of the function. */
2355 if (s->u.syment.n_numaux)
2357 /* The linenumber is stored in the auxent. */
2358 union internal_auxent *a = &((s + 1)->u.auxent);
2359 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2360 *line_ptr = line_base;
2366 if (l->u.offset > offset)
2368 *line_ptr = l->line_number + line_base - 1;
2373 /* If we fell off the end of the loop, then assume that this
2374 symbol has no line number info. Otherwise, symbols with no
2375 line number info get reported with the line number of the
2376 last line of the last symbol which does have line number
2377 info. We use 0x100 as a slop to account for cases where the
2378 last line has executable code. */
2379 if (i >= section->lineno_count
2381 && offset - last_value > 0x100)
2383 *functionname_ptr = NULL;
2388 /* Cache the results for the next call. */
2389 if (sec_data == NULL && section->owner == abfd)
2391 amt = sizeof (struct coff_section_tdata);
2392 section->used_by_bfd = bfd_zalloc (abfd, amt);
2393 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2395 if (sec_data != NULL)
2397 sec_data->offset = offset;
2398 sec_data->i = i - 1;
2399 sec_data->function = *functionname_ptr;
2400 sec_data->line_base = line_base;
2407 coff_find_nearest_line (bfd *abfd,
2411 const char **filename_ptr,
2412 const char **functionname_ptr,
2413 unsigned int *line_ptr)
2415 return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections,
2416 section, symbols, offset,
2417 filename_ptr, functionname_ptr,
2422 coff_find_nearest_line_discriminator (bfd *abfd,
2426 const char **filename_ptr,
2427 const char **functionname_ptr,
2428 unsigned int *line_ptr,
2429 unsigned int *discriminator)
2432 return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections,
2433 section, symbols, offset,
2434 filename_ptr, functionname_ptr,
2440 coff_find_inliner_info (bfd *abfd,
2441 const char **filename_ptr,
2442 const char **functionname_ptr,
2443 unsigned int *line_ptr)
2447 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2448 functionname_ptr, line_ptr,
2449 &coff_data(abfd)->dwarf2_find_line_info);
2454 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2458 if (!info->relocatable)
2459 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2461 size = bfd_coff_filhsz (abfd);
2463 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2467 /* Change the class of a coff symbol held by BFD. */
2470 bfd_coff_set_symbol_class (bfd * abfd,
2472 unsigned int symbol_class)
2474 coff_symbol_type * csym;
2476 csym = coff_symbol_from (abfd, symbol);
2479 bfd_set_error (bfd_error_invalid_operation);
2482 else if (csym->native == NULL)
2484 /* This is an alien symbol which no native coff backend data.
2485 We cheat here by creating a fake native entry for it and
2486 then filling in the class. This code is based on that in
2487 coff_write_alien_symbol(). */
2489 combined_entry_type * native;
2490 bfd_size_type amt = sizeof (* native);
2492 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2496 native->u.syment.n_type = T_NULL;
2497 native->u.syment.n_sclass = symbol_class;
2499 if (bfd_is_und_section (symbol->section))
2501 native->u.syment.n_scnum = N_UNDEF;
2502 native->u.syment.n_value = symbol->value;
2504 else if (bfd_is_com_section (symbol->section))
2506 native->u.syment.n_scnum = N_UNDEF;
2507 native->u.syment.n_value = symbol->value;
2511 native->u.syment.n_scnum =
2512 symbol->section->output_section->target_index;
2513 native->u.syment.n_value = (symbol->value
2514 + symbol->section->output_offset);
2515 if (! obj_pe (abfd))
2516 native->u.syment.n_value += symbol->section->output_section->vma;
2518 /* Copy the any flags from the file header into the symbol.
2520 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2523 csym->native = native;
2526 csym->native->u.syment.n_sclass = symbol_class;
2531 struct coff_comdat_info *
2532 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2534 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2535 && coff_section_data (abfd, sec) != NULL)
2536 return coff_section_data (abfd, sec)->comdat;
2542 _bfd_coff_section_already_linked (bfd *abfd,
2544 struct bfd_link_info *info)
2547 const char *name, *key;
2548 struct bfd_section_already_linked *l;
2549 struct bfd_section_already_linked_hash_entry *already_linked_list;
2550 struct coff_comdat_info *s_comdat;
2553 if ((flags & SEC_LINK_ONCE) == 0)
2556 /* The COFF backend linker doesn't support group sections. */
2557 if ((flags & SEC_GROUP) != 0)
2560 name = bfd_get_section_name (abfd, sec);
2561 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2563 if (s_comdat != NULL)
2564 key = s_comdat->name;
2567 if (CONST_STRNEQ (name, ".gnu.linkonce.")
2568 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2571 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2572 .xdata$<key> and .pdata$<key> only the first of which has a
2573 comdat key. Should these all match the LTO IR key? */
2577 already_linked_list = bfd_section_already_linked_table_lookup (key);
2579 for (l = already_linked_list->entry; l != NULL; l = l->next)
2581 struct coff_comdat_info *l_comdat;
2583 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2585 /* The section names must match, and both sections must be
2586 comdat and have the same comdat name, or both sections must
2587 be non-comdat. LTO IR plugin sections are an exception. They
2588 are always named .gnu.linkonce.t.<key> (<key> is some string)
2589 and match any comdat section with comdat name of <key>, and
2590 any linkonce section with the same suffix, ie.
2591 .gnu.linkonce.*.<key>. */
2592 if (((s_comdat != NULL) == (l_comdat != NULL)
2593 && strcmp (name, l->sec->name) == 0)
2594 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
2596 /* The section has already been linked. See if we should
2598 return _bfd_handle_already_linked (sec, l, info);
2602 /* This is the first section with this name. Record it. */
2603 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2604 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));