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 if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
90 name = (char *) bfd_alloc (abfd,
91 (bfd_size_type) strlen (strings) + 1 + 1);
94 strcpy (name, strings);
100 /* Assorted wastage to null-terminate the name, thanks AT&T! */
101 name = (char *) bfd_alloc (abfd,
102 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
105 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
106 name[sizeof (hdr->s_name)] = 0;
109 return_section = bfd_make_section_anyway (abfd, name);
110 if (return_section == NULL)
113 return_section->vma = hdr->s_vaddr;
114 return_section->lma = hdr->s_paddr;
115 return_section->size = hdr->s_size;
116 return_section->filepos = hdr->s_scnptr;
117 return_section->rel_filepos = hdr->s_relptr;
118 return_section->reloc_count = hdr->s_nreloc;
120 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
122 return_section->line_filepos = hdr->s_lnnoptr;
124 return_section->lineno_count = hdr->s_nlnno;
125 return_section->userdata = NULL;
126 return_section->next = NULL;
127 return_section->target_index = target_index;
129 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
133 return_section->flags = flags;
135 /* At least on i386-coff, the line number count for a shared library
136 section must be ignored. */
137 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
138 return_section->lineno_count = 0;
140 if (hdr->s_nreloc != 0)
141 return_section->flags |= SEC_RELOC;
142 /* FIXME: should this check 'hdr->s_size > 0'. */
143 if (hdr->s_scnptr != 0)
144 return_section->flags |= SEC_HAS_CONTENTS;
146 /* Compress/decompress DWARF debug sections with names: .debug_* and
147 .zdebug_*, after the section flags is set. */
148 if ((flags & SEC_DEBUGGING)
149 && ((name[1] == 'd' && name[6] == '_')
150 || (name[1] == 'z' && name[7] == '_')))
152 enum { nothing, compress, decompress } action = nothing;
153 char *new_name = NULL;
155 if (bfd_is_section_compressed (abfd, return_section))
157 /* Compressed section. Check if we should decompress. */
158 if ((abfd->flags & BFD_DECOMPRESS))
161 else if (!bfd_is_section_compressed (abfd, return_section))
163 /* Normal section. Check if we should compress. */
164 if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0)
173 if (!bfd_init_section_compress_status (abfd, return_section))
175 (*_bfd_error_handler)
176 (_("%B: unable to initialize compress status for section %s"),
182 unsigned int len = strlen (name);
184 new_name = bfd_alloc (abfd, len + 2);
185 if (new_name == NULL)
189 memcpy (new_name + 2, name + 1, len);
193 if (!bfd_init_section_decompress_status (abfd, return_section))
195 (*_bfd_error_handler)
196 (_("%B: unable to initialize decompress status for section %s"),
202 unsigned int len = strlen (name);
204 new_name = bfd_alloc (abfd, len);
205 if (new_name == NULL)
208 memcpy (new_name + 1, name + 2, len - 1);
212 if (new_name != NULL)
213 bfd_rename_section (abfd, return_section, new_name);
219 /* Read in a COFF object and make it into a BFD. This is used by
222 coff_real_object_p (bfd *,
224 struct internal_filehdr *,
225 struct internal_aouthdr *);
227 coff_real_object_p (bfd *abfd,
229 struct internal_filehdr *internal_f,
230 struct internal_aouthdr *internal_a)
232 flagword oflags = abfd->flags;
233 bfd_vma ostart = bfd_get_start_address (abfd);
236 bfd_size_type readsize; /* Length of file_info. */
238 char *external_sections;
240 if (!(internal_f->f_flags & F_RELFLG))
241 abfd->flags |= HAS_RELOC;
242 if ((internal_f->f_flags & F_EXEC))
243 abfd->flags |= EXEC_P;
244 if (!(internal_f->f_flags & F_LNNO))
245 abfd->flags |= HAS_LINENO;
246 if (!(internal_f->f_flags & F_LSYMS))
247 abfd->flags |= HAS_LOCALS;
249 /* FIXME: How can we set D_PAGED correctly? */
250 if ((internal_f->f_flags & F_EXEC) != 0)
251 abfd->flags |= D_PAGED;
253 bfd_get_symcount (abfd) = internal_f->f_nsyms;
254 if (internal_f->f_nsyms)
255 abfd->flags |= HAS_SYMS;
257 if (internal_a != (struct internal_aouthdr *) NULL)
258 bfd_get_start_address (abfd) = internal_a->entry;
260 bfd_get_start_address (abfd) = 0;
262 /* Set up the tdata area. ECOFF uses its own routine, and overrides
264 tdata_save = abfd->tdata.any;
265 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
269 scnhsz = bfd_coff_scnhsz (abfd);
270 readsize = (bfd_size_type) nscns * scnhsz;
271 external_sections = (char *) bfd_alloc (abfd, readsize);
272 if (!external_sections)
275 if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
278 /* Set the arch/mach *before* swapping in sections; section header swapping
279 may depend on arch/mach info. */
280 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
283 /* Now copy data as required; construct all asections etc. */
287 for (i = 0; i < nscns; i++)
289 struct internal_scnhdr tmp;
290 bfd_coff_swap_scnhdr_in (abfd,
291 (void *) (external_sections + i * scnhsz),
293 if (! make_a_section_from_file (abfd, &tmp, i + 1))
301 bfd_release (abfd, tdata);
303 abfd->tdata.any = tdata_save;
304 abfd->flags = oflags;
305 bfd_get_start_address (abfd) = ostart;
306 return (const bfd_target *) NULL;
309 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
310 not a COFF file. This is also used by ECOFF. */
313 coff_object_p (bfd *abfd)
315 bfd_size_type filhsz;
316 bfd_size_type aoutsz;
319 struct internal_filehdr internal_f;
320 struct internal_aouthdr internal_a;
322 /* Figure out how much to read. */
323 filhsz = bfd_coff_filhsz (abfd);
324 aoutsz = bfd_coff_aoutsz (abfd);
326 filehdr = bfd_alloc (abfd, filhsz);
329 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
331 if (bfd_get_error () != bfd_error_system_call)
332 bfd_set_error (bfd_error_wrong_format);
333 bfd_release (abfd, filehdr);
336 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
337 bfd_release (abfd, filehdr);
339 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
340 (less than aoutsz) used in object files and AOUTSZ (equal to
341 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
342 expects this header to be aoutsz bytes in length, so we use that
343 value in the call to bfd_alloc below. But we must be careful to
344 only read in f_opthdr bytes in the call to bfd_bread. We should
345 also attempt to catch corrupt or non-COFF binaries with a strange
346 value for f_opthdr. */
347 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
348 || internal_f.f_opthdr > aoutsz)
350 bfd_set_error (bfd_error_wrong_format);
353 nscns = internal_f.f_nscns;
355 if (internal_f.f_opthdr)
359 opthdr = bfd_alloc (abfd, aoutsz);
362 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
363 != internal_f.f_opthdr)
365 bfd_release (abfd, opthdr);
368 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
369 bfd_release (abfd, opthdr);
372 return coff_real_object_p (abfd, nscns, &internal_f,
373 (internal_f.f_opthdr != 0
375 : (struct internal_aouthdr *) NULL));
378 /* Get the BFD section from a COFF symbol section number. */
381 coff_section_from_bfd_index (bfd *abfd, int section_index)
383 struct bfd_section *answer = abfd->sections;
385 if (section_index == N_ABS)
386 return bfd_abs_section_ptr;
387 if (section_index == N_UNDEF)
388 return bfd_und_section_ptr;
389 if (section_index == N_DEBUG)
390 return bfd_abs_section_ptr;
394 if (answer->target_index == section_index)
396 answer = answer->next;
399 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
400 has a bad symbol table in biglitpow.o. */
401 return bfd_und_section_ptr;
404 /* Get the upper bound of a COFF symbol table. */
407 coff_get_symtab_upper_bound (bfd *abfd)
409 if (!bfd_coff_slurp_symbol_table (abfd))
412 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
415 /* Canonicalize a COFF symbol table. */
418 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
420 unsigned int counter;
421 coff_symbol_type *symbase;
422 coff_symbol_type **location = (coff_symbol_type **) alocation;
424 if (!bfd_coff_slurp_symbol_table (abfd))
427 symbase = obj_symbols (abfd);
428 counter = bfd_get_symcount (abfd);
429 while (counter-- > 0)
430 *location++ = symbase++;
434 return bfd_get_symcount (abfd);
437 /* Get the name of a symbol. The caller must pass in a buffer of size
441 _bfd_coff_internal_syment_name (bfd *abfd,
442 const struct internal_syment *sym,
445 /* FIXME: It's not clear this will work correctly if sizeof
447 if (sym->_n._n_n._n_zeroes != 0
448 || sym->_n._n_n._n_offset == 0)
450 memcpy (buf, sym->_n._n_name, SYMNMLEN);
451 buf[SYMNMLEN] = '\0';
458 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
459 strings = obj_coff_strings (abfd);
462 strings = _bfd_coff_read_string_table (abfd);
466 if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
468 return strings + sym->_n._n_n._n_offset;
472 /* Read in and swap the relocs. This returns a buffer holding the
473 relocs for section SEC in file ABFD. If CACHE is TRUE and
474 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
475 the function is called again. If EXTERNAL_RELOCS is not NULL, it
476 is a buffer large enough to hold the unswapped relocs. If
477 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
478 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
479 value must be INTERNAL_RELOCS. The function returns NULL on error. */
481 struct internal_reloc *
482 _bfd_coff_read_internal_relocs (bfd *abfd,
485 bfd_byte *external_relocs,
486 bfd_boolean require_internal,
487 struct internal_reloc *internal_relocs)
490 bfd_byte *free_external = NULL;
491 struct internal_reloc *free_internal = NULL;
494 struct internal_reloc *irel;
497 if (sec->reloc_count == 0)
498 return internal_relocs; /* Nothing to do. */
500 if (coff_section_data (abfd, sec) != NULL
501 && coff_section_data (abfd, sec)->relocs != NULL)
503 if (! require_internal)
504 return coff_section_data (abfd, sec)->relocs;
505 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
506 sec->reloc_count * sizeof (struct internal_reloc));
507 return internal_relocs;
510 relsz = bfd_coff_relsz (abfd);
512 amt = sec->reloc_count * relsz;
513 if (external_relocs == NULL)
515 free_external = (bfd_byte *) bfd_malloc (amt);
516 if (free_external == NULL)
518 external_relocs = free_external;
521 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
522 || bfd_bread (external_relocs, amt, abfd) != amt)
525 if (internal_relocs == NULL)
527 amt = sec->reloc_count;
528 amt *= sizeof (struct internal_reloc);
529 free_internal = (struct internal_reloc *) bfd_malloc (amt);
530 if (free_internal == NULL)
532 internal_relocs = free_internal;
535 /* Swap in the relocs. */
536 erel = external_relocs;
537 erel_end = erel + relsz * sec->reloc_count;
538 irel = internal_relocs;
539 for (; erel < erel_end; erel += relsz, irel++)
540 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
542 if (free_external != NULL)
544 free (free_external);
545 free_external = NULL;
548 if (cache && free_internal != NULL)
550 if (coff_section_data (abfd, sec) == NULL)
552 amt = sizeof (struct coff_section_tdata);
553 sec->used_by_bfd = bfd_zalloc (abfd, amt);
554 if (sec->used_by_bfd == NULL)
556 coff_section_data (abfd, sec)->contents = NULL;
558 coff_section_data (abfd, sec)->relocs = free_internal;
561 return internal_relocs;
564 if (free_external != NULL)
565 free (free_external);
566 if (free_internal != NULL)
567 free (free_internal);
571 /* Set lineno_count for the output sections of a COFF file. */
574 coff_count_linenumbers (bfd *abfd)
576 unsigned int limit = bfd_get_symcount (abfd);
584 /* This may be from the backend linker, in which case the
585 lineno_count in the sections is correct. */
586 for (s = abfd->sections; s != NULL; s = s->next)
587 total += s->lineno_count;
591 for (s = abfd->sections; s != NULL; s = s->next)
592 BFD_ASSERT (s->lineno_count == 0);
594 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
596 asymbol *q_maybe = *p;
598 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
600 coff_symbol_type *q = coffsymbol (q_maybe);
602 /* The AIX 4.1 compiler can sometimes generate line numbers
603 attached to debugging symbols. We try to simply ignore
605 if (q->lineno != NULL
606 && q->symbol.section->owner != NULL)
608 /* This symbol has line numbers. Increment the owning
609 section's linenumber count. */
610 alent *l = q->lineno;
614 asection * sec = q->symbol.section->output_section;
616 /* Do not try to update fields in read-only sections. */
617 if (! bfd_is_const_section (sec))
618 sec->lineno_count ++;
623 while (l->line_number != 0);
631 /* Takes a bfd and a symbol, returns a pointer to the coff specific
632 area of the symbol if there is one. */
635 coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
638 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
639 return (coff_symbol_type *) NULL;
641 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
642 return (coff_symbol_type *) NULL;
644 return (coff_symbol_type *) symbol;
648 fixup_symbol_value (bfd *abfd,
649 coff_symbol_type *coff_symbol_ptr,
650 struct internal_syment *syment)
652 /* Normalize the symbol flags. */
653 if (coff_symbol_ptr->symbol.section
654 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
656 /* A common symbol is undefined with a value. */
657 syment->n_scnum = N_UNDEF;
658 syment->n_value = coff_symbol_ptr->symbol.value;
660 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
661 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
663 syment->n_value = coff_symbol_ptr->symbol.value;
665 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
667 syment->n_scnum = N_UNDEF;
670 /* FIXME: Do we need to handle the absolute section here? */
673 if (coff_symbol_ptr->symbol.section)
676 coff_symbol_ptr->symbol.section->output_section->target_index;
678 syment->n_value = (coff_symbol_ptr->symbol.value
679 + coff_symbol_ptr->symbol.section->output_offset);
682 syment->n_value += (syment->n_sclass == C_STATLAB)
683 ? coff_symbol_ptr->symbol.section->output_section->lma
684 : coff_symbol_ptr->symbol.section->output_section->vma;
690 /* This can happen, but I don't know why yet (steve@cygnus.com) */
691 syment->n_scnum = N_ABS;
692 syment->n_value = coff_symbol_ptr->symbol.value;
697 /* Run through all the symbols in the symbol table and work out what
698 their indexes into the symbol table will be when output.
700 Coff requires that each C_FILE symbol points to the next one in the
701 chain, and that the last one points to the first external symbol. We
705 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
707 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
708 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
709 unsigned int native_index = 0;
710 struct internal_syment *last_file = NULL;
711 unsigned int symbol_index;
713 /* COFF demands that undefined symbols come after all other symbols.
714 Since we don't need to impose this extra knowledge on all our
715 client programs, deal with that here. Sort the symbol table;
716 just move the undefined symbols to the end, leaving the rest
717 alone. The O'Reilly book says that defined global symbols come
718 at the end before the undefined symbols, so we do that here as
720 /* @@ Do we have some condition we could test for, so we don't always
721 have to do this? I don't think relocatability is quite right, but
722 I'm not certain. [raeburn:19920508.1711EST] */
728 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
729 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
732 bfd_ptr->outsymbols = newsyms;
733 for (i = 0; i < symbol_count; i++)
734 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
735 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
736 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
737 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
738 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
740 *newsyms++ = symbol_ptr_ptr[i];
742 for (i = 0; i < symbol_count; i++)
743 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
744 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
745 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
746 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
747 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
749 *newsyms++ = symbol_ptr_ptr[i];
751 *first_undef = newsyms - bfd_ptr->outsymbols;
753 for (i = 0; i < symbol_count; i++)
754 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
755 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
756 *newsyms++ = symbol_ptr_ptr[i];
757 *newsyms = (asymbol *) NULL;
758 symbol_ptr_ptr = bfd_ptr->outsymbols;
761 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
763 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
764 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
765 if (coff_symbol_ptr && coff_symbol_ptr->native)
767 combined_entry_type *s = coff_symbol_ptr->native;
770 if (s->u.syment.n_sclass == C_FILE)
772 if (last_file != NULL)
773 last_file->n_value = native_index;
774 last_file = &(s->u.syment);
777 /* Modify the symbol values according to their section and
779 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
781 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
782 s[i].offset = native_index++;
788 obj_conv_table_size (bfd_ptr) = native_index;
793 /* Run thorough the symbol table again, and fix it so that all
794 pointers to entries are changed to the entries' index in the output
798 coff_mangle_symbols (bfd *bfd_ptr)
800 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
801 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
802 unsigned int symbol_index;
804 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
806 coff_symbol_type *coff_symbol_ptr =
807 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
809 if (coff_symbol_ptr && coff_symbol_ptr->native)
812 combined_entry_type *s = coff_symbol_ptr->native;
816 /* FIXME: We should use a union here. */
817 s->u.syment.n_value =
818 (bfd_hostptr_t) ((combined_entry_type *)
819 ((bfd_hostptr_t) s->u.syment.n_value))->offset;
824 /* The value is the offset into the line number entries
825 for the symbol's section. On output, the symbol's
826 section should be N_DEBUG. */
827 s->u.syment.n_value =
828 (coff_symbol_ptr->symbol.section->output_section->line_filepos
829 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
830 coff_symbol_ptr->symbol.section =
831 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
832 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
834 for (i = 0; i < s->u.syment.n_numaux; i++)
836 combined_entry_type *a = s + i + 1;
839 a->u.auxent.x_sym.x_tagndx.l =
840 a->u.auxent.x_sym.x_tagndx.p->offset;
845 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
846 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
851 a->u.auxent.x_csect.x_scnlen.l =
852 a->u.auxent.x_csect.x_scnlen.p->offset;
861 coff_fix_symbol_name (bfd *abfd,
863 combined_entry_type *native,
864 bfd_size_type *string_size_p,
865 asection **debug_string_section_p,
866 bfd_size_type *debug_string_size_p)
868 unsigned int name_length;
869 union internal_auxent *auxent;
870 char *name = (char *) (symbol->name);
874 /* COFF symbols always have names, so we'll make one up. */
875 symbol->name = "strange";
876 name = (char *) symbol->name;
878 name_length = strlen (name);
880 if (native->u.syment.n_sclass == C_FILE
881 && native->u.syment.n_numaux > 0)
883 unsigned int filnmlen;
885 if (bfd_coff_force_symnames_in_strings (abfd))
887 native->u.syment._n._n_n._n_offset =
888 (*string_size_p + STRING_SIZE_SIZE);
889 native->u.syment._n._n_n._n_zeroes = 0;
890 *string_size_p += 6; /* strlen(".file") + 1 */
893 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
895 auxent = &(native + 1)->u.auxent;
897 filnmlen = bfd_coff_filnmlen (abfd);
899 if (bfd_coff_long_filenames (abfd))
901 if (name_length <= filnmlen)
902 strncpy (auxent->x_file.x_fname, name, filnmlen);
905 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
906 auxent->x_file.x_n.x_zeroes = 0;
907 *string_size_p += name_length + 1;
912 strncpy (auxent->x_file.x_fname, name, filnmlen);
913 if (name_length > filnmlen)
914 name[filnmlen] = '\0';
919 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
920 /* This name will fit into the symbol neatly. */
921 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
923 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
925 native->u.syment._n._n_n._n_offset = (*string_size_p
927 native->u.syment._n._n_n._n_zeroes = 0;
928 *string_size_p += name_length + 1;
934 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
936 /* This name should be written into the .debug section. For
937 some reason each name is preceded by a two byte length
938 and also followed by a null byte. FIXME: We assume that
939 the .debug section has already been created, and that it
941 if (*debug_string_section_p == (asection *) NULL)
942 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
943 filepos = bfd_tell (abfd);
945 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
947 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
949 if (!bfd_set_section_contents (abfd,
950 *debug_string_section_p,
952 (file_ptr) *debug_string_size_p,
953 (bfd_size_type) prefix_len)
954 || !bfd_set_section_contents (abfd,
955 *debug_string_section_p,
956 (void *) symbol->name,
957 (file_ptr) (*debug_string_size_p
959 (bfd_size_type) name_length + 1))
961 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
963 native->u.syment._n._n_n._n_offset =
964 *debug_string_size_p + prefix_len;
965 native->u.syment._n._n_n._n_zeroes = 0;
966 *debug_string_size_p += name_length + 1 + prefix_len;
971 /* We need to keep track of the symbol index so that when we write out
972 the relocs we can get the index for a symbol. This method is a
975 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
977 /* Write a symbol out to a COFF file. */
980 coff_write_symbol (bfd *abfd,
982 combined_entry_type *native,
984 bfd_size_type *string_size_p,
985 asection **debug_string_section_p,
986 bfd_size_type *debug_string_size_p)
988 unsigned int numaux = native->u.syment.n_numaux;
989 int type = native->u.syment.n_type;
990 int n_sclass = (int) native->u.syment.n_sclass;
991 asection *output_section = symbol->section->output_section
992 ? symbol->section->output_section
995 bfd_size_type symesz;
997 if (native->u.syment.n_sclass == C_FILE)
998 symbol->flags |= BSF_DEBUGGING;
1000 if (symbol->flags & BSF_DEBUGGING
1001 && bfd_is_abs_section (symbol->section))
1002 native->u.syment.n_scnum = N_DEBUG;
1004 else if (bfd_is_abs_section (symbol->section))
1005 native->u.syment.n_scnum = N_ABS;
1007 else if (bfd_is_und_section (symbol->section))
1008 native->u.syment.n_scnum = N_UNDEF;
1011 native->u.syment.n_scnum =
1012 output_section->target_index;
1014 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
1015 debug_string_section_p, debug_string_size_p);
1017 symesz = bfd_coff_symesz (abfd);
1018 buf = bfd_alloc (abfd, symesz);
1021 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1022 if (bfd_bwrite (buf, symesz, abfd) != symesz)
1024 bfd_release (abfd, buf);
1026 if (native->u.syment.n_numaux > 0)
1028 bfd_size_type auxesz;
1031 auxesz = bfd_coff_auxesz (abfd);
1032 buf = bfd_alloc (abfd, auxesz);
1035 for (j = 0; j < native->u.syment.n_numaux; j++)
1037 bfd_coff_swap_aux_out (abfd,
1038 &((native + j + 1)->u.auxent),
1039 type, n_sclass, (int) j,
1040 native->u.syment.n_numaux,
1042 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1045 bfd_release (abfd, buf);
1048 /* Store the index for use when we write out the relocs. */
1049 set_index (symbol, *written);
1051 *written += numaux + 1;
1055 /* Write out a symbol to a COFF file that does not come from a COFF
1056 file originally. This symbol may have been created by the linker,
1057 or we may be linking a non COFF file to a COFF file. */
1060 coff_write_alien_symbol (bfd *abfd,
1062 struct internal_syment *isym,
1064 bfd_size_type *string_size_p,
1065 asection **debug_string_section_p,
1066 bfd_size_type *debug_string_size_p)
1068 combined_entry_type *native;
1069 combined_entry_type dummy[2];
1070 asection *output_section = symbol->section->output_section
1071 ? symbol->section->output_section
1073 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1076 if ((!link_info || link_info->strip_discarded)
1077 && !bfd_is_abs_section (symbol->section)
1078 && symbol->section->output_section == bfd_abs_section_ptr)
1082 memset (isym, 0, sizeof(*isym));
1086 native->u.syment.n_type = T_NULL;
1087 native->u.syment.n_flags = 0;
1088 native->u.syment.n_numaux = 0;
1089 if (bfd_is_und_section (symbol->section))
1091 native->u.syment.n_scnum = N_UNDEF;
1092 native->u.syment.n_value = symbol->value;
1094 else if (bfd_is_com_section (symbol->section))
1096 native->u.syment.n_scnum = N_UNDEF;
1097 native->u.syment.n_value = symbol->value;
1099 else if (symbol->flags & BSF_FILE)
1101 native->u.syment.n_scnum = N_DEBUG;
1102 native->u.syment.n_numaux = 1;
1104 else if (symbol->flags & BSF_DEBUGGING)
1106 /* There isn't much point to writing out a debugging symbol
1107 unless we are prepared to convert it into COFF debugging
1108 format. So, we just ignore them. We must clobber the symbol
1109 name to keep it from being put in the string table. */
1112 memset (isym, 0, sizeof(*isym));
1117 native->u.syment.n_scnum = output_section->target_index;
1118 native->u.syment.n_value = (symbol->value
1119 + symbol->section->output_offset);
1120 if (! obj_pe (abfd))
1121 native->u.syment.n_value += output_section->vma;
1123 /* Copy the any flags from the file header into the symbol.
1126 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1127 if (c != (coff_symbol_type *) NULL)
1128 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1132 native->u.syment.n_type = 0;
1133 if (symbol->flags & BSF_FILE)
1134 native->u.syment.n_sclass = C_FILE;
1135 else if (symbol->flags & BSF_LOCAL)
1136 native->u.syment.n_sclass = C_STAT;
1137 else if (symbol->flags & BSF_WEAK)
1138 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1140 native->u.syment.n_sclass = C_EXT;
1142 ret = coff_write_symbol (abfd, symbol, native, written, string_size_p,
1143 debug_string_section_p, debug_string_size_p);
1145 *isym = native->u.syment;
1149 /* Write a native symbol to a COFF file. */
1152 coff_write_native_symbol (bfd *abfd,
1153 coff_symbol_type *symbol,
1155 bfd_size_type *string_size_p,
1156 asection **debug_string_section_p,
1157 bfd_size_type *debug_string_size_p)
1159 combined_entry_type *native = symbol->native;
1160 alent *lineno = symbol->lineno;
1161 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1163 if ((!link_info || link_info->strip_discarded)
1164 && !bfd_is_abs_section (symbol->symbol.section)
1165 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1167 symbol->symbol.name = "";
1171 /* If this symbol has an associated line number, we must store the
1172 symbol index in the line number field. We also tag the auxent to
1173 point to the right place in the lineno table. */
1174 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1176 unsigned int count = 0;
1178 lineno[count].u.offset = *written;
1179 if (native->u.syment.n_numaux)
1181 union internal_auxent *a = &((native + 1)->u.auxent);
1183 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1184 symbol->symbol.section->output_section->moving_line_filepos;
1187 /* Count and relocate all other linenumbers. */
1189 while (lineno[count].line_number != 0)
1191 lineno[count].u.offset +=
1192 (symbol->symbol.section->output_section->vma
1193 + symbol->symbol.section->output_offset);
1196 symbol->done_lineno = TRUE;
1198 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1199 symbol->symbol.section->output_section->moving_line_filepos +=
1200 count * bfd_coff_linesz (abfd);
1203 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1204 string_size_p, debug_string_section_p,
1205 debug_string_size_p);
1209 null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
1213 /* Write out the COFF symbols. */
1216 coff_write_symbols (bfd *abfd)
1218 bfd_size_type string_size;
1219 asection *debug_string_section;
1220 bfd_size_type debug_string_size;
1222 unsigned int limit = bfd_get_symcount (abfd);
1223 bfd_vma written = 0;
1227 debug_string_section = NULL;
1228 debug_string_size = 0;
1230 /* If this target supports long section names, they must be put into
1231 the string table. This is supported by PE. This code must
1232 handle section names just as they are handled in
1233 coff_write_object_contents. */
1234 if (bfd_coff_long_section_names (abfd))
1238 for (o = abfd->sections; o != NULL; o = o->next)
1242 len = strlen (o->name);
1244 string_size += len + 1;
1248 /* Seek to the right place. */
1249 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1252 /* Output all the symbols we have. */
1254 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1256 asymbol *symbol = *p;
1257 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1259 if (c_symbol == (coff_symbol_type *) NULL
1260 || c_symbol->native == (combined_entry_type *) NULL)
1262 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1263 &string_size, &debug_string_section,
1264 &debug_string_size))
1269 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1271 bfd_error_handler_type current_error_handler;
1272 enum coff_symbol_classification sym_class;
1273 unsigned char *n_sclass;
1275 /* Suppress error reporting by bfd_coff_classify_symbol.
1276 Error messages can be generated when we are processing a local
1277 symbol which has no associated section and we do not have to
1278 worry about this, all we need to know is that it is local. */
1279 current_error_handler = bfd_set_error_handler (null_error_handler);
1280 sym_class = bfd_coff_classify_symbol (abfd,
1281 &c_symbol->native->u.syment);
1282 (void) bfd_set_error_handler (current_error_handler);
1284 n_sclass = &c_symbol->native->u.syment.n_sclass;
1286 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1287 we cannot retain the existing sclass from the original symbol.
1288 Weak symbols only have one valid sclass, so just set it always.
1289 If it is not local class and should be, set it C_STAT.
1290 If it is global and not classified as global, or if it is
1291 weak (which is also classified as global), set it C_EXT. */
1293 if (symbol->flags & BSF_WEAK)
1294 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1295 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1297 else if (symbol->flags & BSF_GLOBAL
1298 && (sym_class != COFF_SYMBOL_GLOBAL
1300 || *n_sclass == C_NT_WEAK
1302 || *n_sclass == C_WEAKEXT))
1303 c_symbol->native->u.syment.n_sclass = C_EXT;
1306 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1307 &string_size, &debug_string_section,
1308 &debug_string_size))
1313 obj_raw_syment_count (abfd) = written;
1315 /* Now write out strings. */
1316 if (string_size != 0)
1318 unsigned int size = string_size + STRING_SIZE_SIZE;
1319 bfd_byte buffer[STRING_SIZE_SIZE];
1321 #if STRING_SIZE_SIZE == 4
1322 H_PUT_32 (abfd, size, buffer);
1324 #error Change H_PUT_32
1326 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1330 /* Handle long section names. This code must handle section
1331 names just as they are handled in coff_write_object_contents. */
1332 if (bfd_coff_long_section_names (abfd))
1336 for (o = abfd->sections; o != NULL; o = o->next)
1340 len = strlen (o->name);
1343 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1350 for (p = abfd->outsymbols, i = 0;
1355 size_t name_length = strlen (q->name);
1356 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1359 /* Figure out whether the symbol name should go in the string
1360 table. Symbol names that are short enough are stored
1361 directly in the syment structure. File names permit a
1362 different, longer, length in the syment structure. On
1363 XCOFF, some symbol names are stored in the .debug section
1364 rather than in the string table. */
1366 if (c_symbol == NULL
1367 || c_symbol->native == NULL)
1368 /* This is not a COFF symbol, so it certainly is not a
1369 file name, nor does it go in the .debug section. */
1370 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1372 else if (bfd_coff_symname_in_debug (abfd,
1373 &c_symbol->native->u.syment))
1374 /* This symbol name is in the XCOFF .debug section.
1375 Don't write it into the string table. */
1376 maxlen = name_length;
1378 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1379 && c_symbol->native->u.syment.n_numaux > 0)
1381 if (bfd_coff_force_symnames_in_strings (abfd))
1383 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1386 maxlen = bfd_coff_filnmlen (abfd);
1389 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1391 if (name_length > maxlen)
1393 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1394 abfd) != name_length + 1)
1401 /* We would normally not write anything here, but we'll write
1402 out 4 so that any stupid coff reader which tries to read the
1403 string table even when there isn't one won't croak. */
1404 unsigned int size = STRING_SIZE_SIZE;
1405 bfd_byte buffer[STRING_SIZE_SIZE];
1407 #if STRING_SIZE_SIZE == 4
1408 H_PUT_32 (abfd, size, buffer);
1410 #error Change H_PUT_32
1412 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1413 != STRING_SIZE_SIZE)
1417 /* Make sure the .debug section was created to be the correct size.
1418 We should create it ourselves on the fly, but we don't because
1419 BFD won't let us write to any section until we know how large all
1420 the sections are. We could still do it by making another pass
1421 over the symbols. FIXME. */
1422 BFD_ASSERT (debug_string_size == 0
1423 || (debug_string_section != (asection *) NULL
1424 && (BFD_ALIGN (debug_string_size,
1425 1 << debug_string_section->alignment_power)
1426 == debug_string_section->size)));
1432 coff_write_linenumbers (bfd *abfd)
1435 bfd_size_type linesz;
1438 linesz = bfd_coff_linesz (abfd);
1439 buff = bfd_alloc (abfd, linesz);
1442 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1444 if (s->lineno_count)
1446 asymbol **q = abfd->outsymbols;
1447 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1449 /* Find all the linenumbers in this section. */
1453 if (p->section->output_section == s)
1456 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1457 (bfd_asymbol_bfd (p), p));
1460 /* Found a linenumber entry, output. */
1461 struct internal_lineno out;
1462 memset ((void *) & out, 0, sizeof (out));
1464 out.l_addr.l_symndx = l->u.offset;
1465 bfd_coff_swap_lineno_out (abfd, &out, buff);
1466 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1470 while (l->line_number)
1472 out.l_lnno = l->line_number;
1473 out.l_addr.l_symndx = l->u.offset;
1474 bfd_coff_swap_lineno_out (abfd, &out, buff);
1475 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1486 bfd_release (abfd, buff);
1491 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1493 return coffsymbol (symbol)->lineno;
1496 /* This function transforms the offsets into the symbol table into
1497 pointers to syments. */
1500 coff_pointerize_aux (bfd *abfd,
1501 combined_entry_type *table_base,
1502 combined_entry_type *symbol,
1503 unsigned int indaux,
1504 combined_entry_type *auxent)
1506 unsigned int type = symbol->u.syment.n_type;
1507 unsigned int n_sclass = symbol->u.syment.n_sclass;
1509 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1511 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1512 (abfd, table_base, symbol, indaux, auxent))
1516 /* Don't bother if this is a file or a section. */
1517 if (n_sclass == C_STAT && type == T_NULL)
1519 if (n_sclass == C_FILE)
1522 /* Otherwise patch up. */
1523 #define N_TMASK coff_data (abfd)->local_n_tmask
1524 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1526 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1527 || n_sclass == C_FCN)
1528 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1530 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1531 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1532 auxent->fix_end = 1;
1534 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1535 generate one, so we must be careful to ignore it. */
1536 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1538 auxent->u.auxent.x_sym.x_tagndx.p =
1539 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1540 auxent->fix_tag = 1;
1544 /* Allocate space for the ".debug" section, and read it.
1545 We did not read the debug section until now, because
1546 we didn't want to go to the trouble until someone needed it. */
1549 build_debug_section (bfd *abfd, asection ** sect_return)
1551 char *debug_section;
1553 bfd_size_type sec_size;
1555 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1559 bfd_set_error (bfd_error_no_debug_section);
1563 sec_size = sect->size;
1564 debug_section = (char *) bfd_alloc (abfd, sec_size);
1565 if (debug_section == NULL)
1568 /* Seek to the beginning of the `.debug' section and read it.
1569 Save the current position first; it is needed by our caller.
1570 Then read debug section and reset the file pointer. */
1572 position = bfd_tell (abfd);
1573 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1574 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1575 || bfd_seek (abfd, position, SEEK_SET) != 0)
1578 * sect_return = sect;
1579 return debug_section;
1582 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1583 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1584 be \0-terminated. */
1587 copy_name (bfd *abfd, char *name, size_t maxlen)
1592 for (len = 0; len < maxlen; ++len)
1593 if (name[len] == '\0')
1596 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1599 strncpy (newname, name, len);
1600 newname[len] = '\0';
1604 /* Read in the external symbols. */
1607 _bfd_coff_get_external_symbols (bfd *abfd)
1609 bfd_size_type symesz;
1613 if (obj_coff_external_syms (abfd) != NULL)
1616 symesz = bfd_coff_symesz (abfd);
1618 size = obj_raw_syment_count (abfd) * symesz;
1622 /* PR binutils/17512: Do not even try to load
1623 a symbol table bigger than the entire file... */
1624 if (size >= (bfd_size_type) bfd_get_size (abfd))
1627 syms = bfd_malloc (size);
1631 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1632 || bfd_bread (syms, size, abfd) != size)
1639 obj_coff_external_syms (abfd) = syms;
1644 /* Read in the external strings. The strings are not loaded until
1645 they are needed. This is because we have no simple way of
1646 detecting a missing string table in an archive. If the strings
1647 are loaded then the STRINGS and STRINGS_LEN fields in the
1648 coff_tdata structure will be set. */
1651 _bfd_coff_read_string_table (bfd *abfd)
1653 char extstrsize[STRING_SIZE_SIZE];
1654 bfd_size_type strsize;
1658 if (obj_coff_strings (abfd) != NULL)
1659 return obj_coff_strings (abfd);
1661 if (obj_sym_filepos (abfd) == 0)
1663 bfd_set_error (bfd_error_no_symbols);
1667 pos = obj_sym_filepos (abfd);
1668 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1669 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1672 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1673 != sizeof extstrsize)
1675 if (bfd_get_error () != bfd_error_file_truncated)
1678 /* There is no string table. */
1679 strsize = STRING_SIZE_SIZE;
1683 #if STRING_SIZE_SIZE == 4
1684 strsize = H_GET_32 (abfd, extstrsize);
1686 #error Change H_GET_32
1690 if (strsize < STRING_SIZE_SIZE)
1692 (*_bfd_error_handler)
1693 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1694 bfd_set_error (bfd_error_bad_value);
1698 strings = (char *) bfd_malloc (strsize);
1699 if (strings == NULL)
1702 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1703 != strsize - STRING_SIZE_SIZE)
1709 obj_coff_strings (abfd) = strings;
1710 obj_coff_strings_len (abfd) = strsize;
1715 /* Free up the external symbols and strings read from a COFF file. */
1718 _bfd_coff_free_symbols (bfd *abfd)
1720 if (obj_coff_external_syms (abfd) != NULL
1721 && ! obj_coff_keep_syms (abfd))
1723 free (obj_coff_external_syms (abfd));
1724 obj_coff_external_syms (abfd) = NULL;
1726 if (obj_coff_strings (abfd) != NULL
1727 && ! obj_coff_keep_strings (abfd))
1729 free (obj_coff_strings (abfd));
1730 obj_coff_strings (abfd) = NULL;
1731 obj_coff_strings_len (abfd) = 0;
1736 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1737 knit the symbol names into a normalized form. By normalized here I
1738 mean that all symbols have an n_offset pointer that points to a null-
1739 terminated string. */
1741 combined_entry_type *
1742 coff_get_normalized_symtab (bfd *abfd)
1744 combined_entry_type *internal;
1745 combined_entry_type *internal_ptr;
1746 combined_entry_type *symbol_ptr;
1747 combined_entry_type *internal_end;
1751 const char *string_table = NULL;
1752 asection * debug_sec = NULL;
1753 char *debug_sec_data = NULL;
1756 if (obj_raw_syments (abfd) != NULL)
1757 return obj_raw_syments (abfd);
1759 size = obj_raw_syment_count (abfd);
1762 /* PR binutils/17512: Do not even try to load
1763 a symbol table bigger than the entire file... */
1764 if (size >= (bfd_size_type) bfd_get_size (abfd))
1767 size *= sizeof (combined_entry_type);
1768 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1769 if (internal == NULL && size != 0)
1771 internal_end = internal + obj_raw_syment_count (abfd);
1773 if (! _bfd_coff_get_external_symbols (abfd))
1776 raw_src = (char *) obj_coff_external_syms (abfd);
1778 /* Mark the end of the symbols. */
1779 symesz = bfd_coff_symesz (abfd);
1780 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1782 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1783 probably possible. If one shows up, it will probably kill us. */
1785 /* Swap all the raw entries. */
1786 for (internal_ptr = internal;
1788 raw_src += symesz, internal_ptr++)
1792 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1793 (void *) & internal_ptr->u.syment);
1794 symbol_ptr = internal_ptr;
1797 i < symbol_ptr->u.syment.n_numaux;
1801 /* PR 17512: Prevent buffer overrun. */
1802 if (internal_ptr >= internal_end)
1806 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1807 symbol_ptr->u.syment.n_type,
1808 symbol_ptr->u.syment.n_sclass,
1809 (int) i, symbol_ptr->u.syment.n_numaux,
1810 &(internal_ptr->u.auxent));
1811 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1816 /* Free the raw symbols, but not the strings (if we have them). */
1817 obj_coff_keep_strings (abfd) = TRUE;
1818 if (! _bfd_coff_free_symbols (abfd))
1821 for (internal_ptr = internal; internal_ptr < internal_end;
1824 if (internal_ptr->u.syment.n_sclass == C_FILE
1825 && internal_ptr->u.syment.n_numaux > 0)
1827 /* Make a file symbol point to the name in the auxent, since
1828 the text ".file" is redundant. */
1829 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1831 /* The filename is a long one, point into the string table. */
1832 if (string_table == NULL)
1834 string_table = _bfd_coff_read_string_table (abfd);
1835 if (string_table == NULL)
1838 if ((bfd_size_type)((internal_ptr + 1)->u.auxent.x_file.x_n.x_offset)
1839 >= obj_coff_strings_len (abfd))
1840 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) _("<corrupt>");
1842 internal_ptr->u.syment._n._n_n._n_offset =
1845 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1849 /* Ordinary short filename, put into memory anyway. The
1850 Microsoft PE tools sometimes store a filename in
1851 multiple AUX entries. */
1852 if (internal_ptr->u.syment.n_numaux > 1
1853 && coff_data (abfd)->pe)
1854 internal_ptr->u.syment._n._n_n._n_offset =
1857 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1858 internal_ptr->u.syment.n_numaux * symesz));
1860 internal_ptr->u.syment._n._n_n._n_offset =
1863 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1864 (size_t) bfd_coff_filnmlen (abfd)));
1869 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1871 /* This is a "short" name. Make it long. */
1875 /* Find the length of this string without walking into memory
1877 for (i = 0; i < 8; ++i)
1878 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1881 newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1882 if (newstring == NULL)
1884 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1885 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1886 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1888 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1889 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1890 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1892 /* Long name already. Point symbol at the string in the
1894 if (string_table == NULL)
1896 string_table = _bfd_coff_read_string_table (abfd);
1897 if (string_table == NULL)
1900 if (internal_ptr->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd))
1901 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) _("<corrupt>");
1903 internal_ptr->u.syment._n._n_n._n_offset =
1906 + internal_ptr->u.syment._n._n_n._n_offset));
1910 /* Long name in debug section. Very similar. */
1911 if (debug_sec_data == NULL)
1912 debug_sec_data = build_debug_section (abfd, & debug_sec);
1913 if (debug_sec_data != NULL)
1915 BFD_ASSERT (debug_sec != NULL);
1916 /* PR binutils/17512: Catch out of range offsets into the debug data. */
1917 if (internal_ptr->u.syment._n._n_n._n_offset > debug_sec->size)
1918 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) _("<corrupt>");
1920 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1921 (debug_sec_data + internal_ptr->u.syment._n._n_n._n_offset);
1924 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1927 internal_ptr += internal_ptr->u.syment.n_numaux;
1930 obj_raw_syments (abfd) = internal;
1931 BFD_ASSERT (obj_raw_syment_count (abfd)
1932 == (unsigned int) (internal_ptr - internal));
1938 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1940 if (bfd_get_format (abfd) != bfd_object)
1942 bfd_set_error (bfd_error_invalid_operation);
1945 return (asect->reloc_count + 1) * sizeof (arelent *);
1949 coff_make_empty_symbol (bfd *abfd)
1951 bfd_size_type amt = sizeof (coff_symbol_type);
1952 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1954 if (new_symbol == NULL)
1956 new_symbol->symbol.section = 0;
1957 new_symbol->native = 0;
1958 new_symbol->lineno = NULL;
1959 new_symbol->done_lineno = FALSE;
1960 new_symbol->symbol.the_bfd = abfd;
1962 return & new_symbol->symbol;
1965 /* Make a debugging symbol. */
1968 coff_bfd_make_debug_symbol (bfd *abfd,
1969 void * ptr ATTRIBUTE_UNUSED,
1970 unsigned long sz ATTRIBUTE_UNUSED)
1972 bfd_size_type amt = sizeof (coff_symbol_type);
1973 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
1975 if (new_symbol == NULL)
1977 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1978 (but shouldn't be a constant). */
1979 amt = sizeof (combined_entry_type) * 10;
1980 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1981 if (!new_symbol->native)
1983 new_symbol->symbol.section = bfd_abs_section_ptr;
1984 new_symbol->symbol.flags = BSF_DEBUGGING;
1985 new_symbol->lineno = NULL;
1986 new_symbol->done_lineno = FALSE;
1987 new_symbol->symbol.the_bfd = abfd;
1989 return & new_symbol->symbol;
1993 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1995 bfd_symbol_info (symbol, ret);
1997 if (coffsymbol (symbol)->native != NULL
1998 && coffsymbol (symbol)->native->fix_value)
1999 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
2000 (bfd_hostptr_t) obj_raw_syments (abfd);
2003 /* Return the COFF syment for a symbol. */
2006 bfd_coff_get_syment (bfd *abfd,
2008 struct internal_syment *psyment)
2010 coff_symbol_type *csym;
2012 csym = coff_symbol_from (abfd, symbol);
2013 if (csym == NULL || csym->native == NULL)
2015 bfd_set_error (bfd_error_invalid_operation);
2019 *psyment = csym->native->u.syment;
2021 if (csym->native->fix_value)
2022 psyment->n_value = psyment->n_value -
2023 (bfd_hostptr_t) obj_raw_syments (abfd);
2025 /* FIXME: We should handle fix_line here. */
2030 /* Return the COFF auxent for a symbol. */
2033 bfd_coff_get_auxent (bfd *abfd,
2036 union internal_auxent *pauxent)
2038 coff_symbol_type *csym;
2039 combined_entry_type *ent;
2041 csym = coff_symbol_from (abfd, symbol);
2044 || csym->native == NULL
2045 || indx >= csym->native->u.syment.n_numaux)
2047 bfd_set_error (bfd_error_invalid_operation);
2051 ent = csym->native + indx + 1;
2053 *pauxent = ent->u.auxent;
2056 pauxent->x_sym.x_tagndx.l =
2057 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
2058 - obj_raw_syments (abfd));
2061 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2062 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2063 - obj_raw_syments (abfd));
2065 if (ent->fix_scnlen)
2066 pauxent->x_csect.x_scnlen.l =
2067 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2068 - obj_raw_syments (abfd));
2073 /* Print out information about COFF symbol. */
2076 coff_print_symbol (bfd *abfd,
2079 bfd_print_symbol_type how)
2081 FILE * file = (FILE *) filep;
2085 case bfd_print_symbol_name:
2086 fprintf (file, "%s", symbol->name);
2089 case bfd_print_symbol_more:
2090 fprintf (file, "coff %s %s",
2091 coffsymbol (symbol)->native ? "n" : "g",
2092 coffsymbol (symbol)->lineno ? "l" : " ");
2095 case bfd_print_symbol_all:
2096 if (coffsymbol (symbol)->native)
2100 combined_entry_type *combined = coffsymbol (symbol)->native;
2101 combined_entry_type *root = obj_raw_syments (abfd);
2102 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2104 fprintf (file, "[%3ld]", (long) (combined - root));
2106 if (! combined->fix_value)
2107 val = (bfd_vma) combined->u.syment.n_value;
2109 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
2111 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
2112 combined->u.syment.n_scnum,
2113 combined->u.syment.n_flags,
2114 combined->u.syment.n_type,
2115 combined->u.syment.n_sclass,
2116 combined->u.syment.n_numaux);
2117 bfd_fprintf_vma (abfd, file, val);
2118 fprintf (file, " %s", symbol->name);
2120 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2122 combined_entry_type *auxp = combined + aux + 1;
2126 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2128 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2130 fprintf (file, "\n");
2132 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2135 switch (combined->u.syment.n_sclass)
2138 fprintf (file, "File ");
2142 if (combined->u.syment.n_type == T_NULL)
2143 /* Probably a section symbol ? */
2145 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2146 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2147 auxp->u.auxent.x_scn.x_nreloc,
2148 auxp->u.auxent.x_scn.x_nlinno);
2149 if (auxp->u.auxent.x_scn.x_checksum != 0
2150 || auxp->u.auxent.x_scn.x_associated != 0
2151 || auxp->u.auxent.x_scn.x_comdat != 0)
2152 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2153 auxp->u.auxent.x_scn.x_checksum,
2154 auxp->u.auxent.x_scn.x_associated,
2155 auxp->u.auxent.x_scn.x_comdat);
2158 /* Otherwise fall through. */
2161 if (ISFCN (combined->u.syment.n_type))
2166 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2169 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2170 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2172 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2174 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2178 /* Otherwise fall through. */
2180 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2181 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2182 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2185 fprintf (file, " endndx %ld",
2187 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2195 fprintf (file, "\n%s :", l->u.sym->name);
2197 while (l->line_number)
2199 fprintf (file, "\n%4d : ", l->line_number);
2200 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2207 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2208 fprintf (file, " %-5s %s %s %s",
2209 symbol->section->name,
2210 coffsymbol (symbol)->native ? "n" : "g",
2211 coffsymbol (symbol)->lineno ? "l" : " ",
2217 /* Return whether a symbol name implies a local symbol. In COFF,
2218 local symbols generally start with ``.L''. Most targets use this
2219 function for the is_local_label_name entry point, but some may
2223 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2226 return name[0] == '.' && name[1] == 'L';
2229 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2230 section, calculate and return the name of the source file and the line
2231 nearest to the wanted location. */
2234 coff_find_nearest_line_with_names (bfd *abfd,
2238 const char **filename_ptr,
2239 const char **functionname_ptr,
2240 unsigned int *line_ptr,
2241 const struct dwarf_debug_section *debug_sections)
2245 unsigned int line_base;
2246 coff_data_type *cof = coff_data (abfd);
2247 /* Run through the raw syments if available. */
2248 combined_entry_type *p;
2249 combined_entry_type *pend;
2251 struct coff_section_tdata *sec_data;
2254 /* Before looking through the symbol table, try to use a .stab
2255 section to find the information. */
2256 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2257 &found, filename_ptr,
2258 functionname_ptr, line_ptr,
2259 &coff_data(abfd)->line_info))
2265 /* Also try examining DWARF2 debugging information. */
2266 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2267 filename_ptr, functionname_ptr,
2268 line_ptr, NULL, debug_sections, 0,
2269 &coff_data(abfd)->dwarf2_find_line_info))
2273 *functionname_ptr = 0;
2276 /* Don't try and find line numbers in a non coff file. */
2277 if (!bfd_family_coff (abfd))
2283 /* Find the first C_FILE symbol. */
2284 p = cof->raw_syments;
2288 pend = p + cof->raw_syment_count;
2291 if (p->u.syment.n_sclass == C_FILE)
2293 p += 1 + p->u.syment.n_numaux;
2301 /* Look through the C_FILE symbols to find the best one. */
2302 sec_vma = bfd_get_section_vma (abfd, section);
2303 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2304 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2308 combined_entry_type *p2;
2310 for (p2 = p + 1 + p->u.syment.n_numaux;
2312 p2 += 1 + p2->u.syment.n_numaux)
2314 if (p2->u.syment.n_scnum > 0
2316 == coff_section_from_bfd_index (abfd,
2317 p2->u.syment.n_scnum)))
2319 if (p2->u.syment.n_sclass == C_FILE)
2326 file_addr = (bfd_vma) p2->u.syment.n_value;
2327 /* PR 11512: Include the section address of the function name symbol. */
2328 if (p2->u.syment.n_scnum > 0)
2329 file_addr += coff_section_from_bfd_index (abfd,
2330 p2->u.syment.n_scnum)->vma;
2331 /* We use <= MAXDIFF here so that if we get a zero length
2332 file, we actually use the next file entry. */
2334 && offset + sec_vma >= file_addr
2335 && offset + sec_vma - file_addr <= maxdiff)
2337 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2338 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2341 /* Avoid endless loops on erroneous files by ensuring that
2342 we always move forward in the file. */
2343 if (p >= cof->raw_syments + p->u.syment.n_value)
2346 p = cof->raw_syments + p->u.syment.n_value;
2347 if (p > pend || p->u.syment.n_sclass != C_FILE)
2352 /* Now wander though the raw linenumbers of the section. */
2353 /* If we have been called on this section before, and the offset we
2354 want is further down then we can prime the lookup loop. */
2355 sec_data = coff_section_data (abfd, section);
2356 if (sec_data != NULL
2358 && offset >= sec_data->offset)
2361 *functionname_ptr = sec_data->function;
2362 line_base = sec_data->line_base;
2370 if (section->lineno != NULL)
2372 bfd_vma last_value = 0;
2374 l = §ion->lineno[i];
2376 for (; i < section->lineno_count; i++)
2378 if (l->line_number == 0)
2380 /* Get the symbol this line number points at. */
2381 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2382 if (coff->symbol.value > offset)
2384 *functionname_ptr = coff->symbol.name;
2385 last_value = coff->symbol.value;
2388 combined_entry_type *s = coff->native;
2389 s = s + 1 + s->u.syment.n_numaux;
2391 /* In XCOFF a debugging symbol can follow the
2393 if (s->u.syment.n_scnum == N_DEBUG)
2394 s = s + 1 + s->u.syment.n_numaux;
2396 /* S should now point to the .bf of the function. */
2397 if (s->u.syment.n_numaux)
2399 /* The linenumber is stored in the auxent. */
2400 union internal_auxent *a = &((s + 1)->u.auxent);
2401 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2402 *line_ptr = line_base;
2408 if (l->u.offset > offset)
2410 *line_ptr = l->line_number + line_base - 1;
2415 /* If we fell off the end of the loop, then assume that this
2416 symbol has no line number info. Otherwise, symbols with no
2417 line number info get reported with the line number of the
2418 last line of the last symbol which does have line number
2419 info. We use 0x100 as a slop to account for cases where the
2420 last line has executable code. */
2421 if (i >= section->lineno_count
2423 && offset - last_value > 0x100)
2425 *functionname_ptr = NULL;
2430 /* Cache the results for the next call. */
2431 if (sec_data == NULL && section->owner == abfd)
2433 amt = sizeof (struct coff_section_tdata);
2434 section->used_by_bfd = bfd_zalloc (abfd, amt);
2435 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2437 if (sec_data != NULL)
2439 sec_data->offset = offset;
2440 sec_data->i = i - 1;
2441 sec_data->function = *functionname_ptr;
2442 sec_data->line_base = line_base;
2449 coff_find_nearest_line (bfd *abfd,
2453 const char **filename_ptr,
2454 const char **functionname_ptr,
2455 unsigned int *line_ptr,
2456 unsigned int *discriminator_ptr)
2458 if (discriminator_ptr)
2459 *discriminator_ptr = 0;
2460 return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2461 filename_ptr, functionname_ptr,
2462 line_ptr, dwarf_debug_sections);
2466 coff_find_inliner_info (bfd *abfd,
2467 const char **filename_ptr,
2468 const char **functionname_ptr,
2469 unsigned int *line_ptr)
2473 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2474 functionname_ptr, line_ptr,
2475 &coff_data(abfd)->dwarf2_find_line_info);
2480 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2484 if (!info->relocatable)
2485 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2487 size = bfd_coff_filhsz (abfd);
2489 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2493 /* Change the class of a coff symbol held by BFD. */
2496 bfd_coff_set_symbol_class (bfd * abfd,
2498 unsigned int symbol_class)
2500 coff_symbol_type * csym;
2502 csym = coff_symbol_from (abfd, symbol);
2505 bfd_set_error (bfd_error_invalid_operation);
2508 else if (csym->native == NULL)
2510 /* This is an alien symbol which no native coff backend data.
2511 We cheat here by creating a fake native entry for it and
2512 then filling in the class. This code is based on that in
2513 coff_write_alien_symbol(). */
2515 combined_entry_type * native;
2516 bfd_size_type amt = sizeof (* native);
2518 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2522 native->u.syment.n_type = T_NULL;
2523 native->u.syment.n_sclass = symbol_class;
2525 if (bfd_is_und_section (symbol->section))
2527 native->u.syment.n_scnum = N_UNDEF;
2528 native->u.syment.n_value = symbol->value;
2530 else if (bfd_is_com_section (symbol->section))
2532 native->u.syment.n_scnum = N_UNDEF;
2533 native->u.syment.n_value = symbol->value;
2537 native->u.syment.n_scnum =
2538 symbol->section->output_section->target_index;
2539 native->u.syment.n_value = (symbol->value
2540 + symbol->section->output_offset);
2541 if (! obj_pe (abfd))
2542 native->u.syment.n_value += symbol->section->output_section->vma;
2544 /* Copy the any flags from the file header into the symbol.
2546 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2549 csym->native = native;
2552 csym->native->u.syment.n_sclass = symbol_class;
2557 struct coff_comdat_info *
2558 bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2560 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2561 && coff_section_data (abfd, sec) != NULL)
2562 return coff_section_data (abfd, sec)->comdat;
2568 _bfd_coff_section_already_linked (bfd *abfd,
2570 struct bfd_link_info *info)
2573 const char *name, *key;
2574 struct bfd_section_already_linked *l;
2575 struct bfd_section_already_linked_hash_entry *already_linked_list;
2576 struct coff_comdat_info *s_comdat;
2579 if ((flags & SEC_LINK_ONCE) == 0)
2582 /* The COFF backend linker doesn't support group sections. */
2583 if ((flags & SEC_GROUP) != 0)
2586 name = bfd_get_section_name (abfd, sec);
2587 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2589 if (s_comdat != NULL)
2590 key = s_comdat->name;
2593 if (CONST_STRNEQ (name, ".gnu.linkonce.")
2594 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2597 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2598 .xdata$<key> and .pdata$<key> only the first of which has a
2599 comdat key. Should these all match the LTO IR key? */
2603 already_linked_list = bfd_section_already_linked_table_lookup (key);
2605 for (l = already_linked_list->entry; l != NULL; l = l->next)
2607 struct coff_comdat_info *l_comdat;
2609 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2611 /* The section names must match, and both sections must be
2612 comdat and have the same comdat name, or both sections must
2613 be non-comdat. LTO IR plugin sections are an exception. They
2614 are always named .gnu.linkonce.t.<key> (<key> is some string)
2615 and match any comdat section with comdat name of <key>, and
2616 any linkonce section with the same suffix, ie.
2617 .gnu.linkonce.*.<key>. */
2618 if (((s_comdat != NULL) == (l_comdat != NULL)
2619 && strcmp (name, l->sec->name) == 0)
2620 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
2622 /* The section has already been linked. See if we should
2624 return _bfd_handle_already_linked (sec, l, info);
2628 /* This is the first section with this name. Record it. */
2629 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2630 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));