1 /* COFF specific linker code.
2 Copyright (C) 1994-2018 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, 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 /* This file contains the COFF backend linker code. */
28 #include "coff/internal.h"
30 #include "safe-ctype.h"
32 static bfd_boolean coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
33 static bfd_boolean coff_link_check_archive_element
34 (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
36 static bfd_boolean coff_link_add_symbols (bfd *, struct bfd_link_info *);
38 /* Return TRUE if SYM is a weak, external symbol. */
39 #define IS_WEAK_EXTERNAL(abfd, sym) \
40 ((sym).n_sclass == C_WEAKEXT \
41 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
43 /* Return TRUE if SYM is an external symbol. */
44 #define IS_EXTERNAL(abfd, sym) \
45 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
47 /* Define macros so that the ISFCN, et. al., macros work correctly.
48 These macros are defined in include/coff/internal.h in terms of
49 N_TMASK, etc. These definitions require a user to define local
50 variables with the appropriate names, and with values from the
51 coff_data (abfd) structure. */
53 #define N_TMASK n_tmask
54 #define N_BTSHFT n_btshft
55 #define N_BTMASK n_btmask
57 /* Create an entry in a COFF linker hash table. */
59 struct bfd_hash_entry *
60 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
61 struct bfd_hash_table *table,
64 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
66 /* Allocate the structure if it has not already been allocated by a
68 if (ret == (struct coff_link_hash_entry *) NULL)
69 ret = ((struct coff_link_hash_entry *)
70 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
71 if (ret == (struct coff_link_hash_entry *) NULL)
72 return (struct bfd_hash_entry *) ret;
74 /* Call the allocation method of the superclass. */
75 ret = ((struct coff_link_hash_entry *)
76 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
78 if (ret != (struct coff_link_hash_entry *) NULL)
80 /* Set local fields. */
83 ret->symbol_class = C_NULL;
89 return (struct bfd_hash_entry *) ret;
92 /* Initialize a COFF linker hash table. */
95 _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
97 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
98 struct bfd_hash_table *,
100 unsigned int entsize)
102 memset (&table->stab_info, 0, sizeof (table->stab_info));
103 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
106 /* Create a COFF linker hash table. */
108 struct bfd_link_hash_table *
109 _bfd_coff_link_hash_table_create (bfd *abfd)
111 struct coff_link_hash_table *ret;
112 bfd_size_type amt = sizeof (struct coff_link_hash_table);
114 ret = (struct coff_link_hash_table *) bfd_malloc (amt);
118 if (! _bfd_coff_link_hash_table_init (ret, abfd,
119 _bfd_coff_link_hash_newfunc,
120 sizeof (struct coff_link_hash_entry)))
123 return (struct bfd_link_hash_table *) NULL;
128 /* Create an entry in a COFF debug merge hash table. */
130 struct bfd_hash_entry *
131 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
132 struct bfd_hash_table *table,
135 struct coff_debug_merge_hash_entry *ret =
136 (struct coff_debug_merge_hash_entry *) entry;
138 /* Allocate the structure if it has not already been allocated by a
140 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
141 ret = ((struct coff_debug_merge_hash_entry *)
142 bfd_hash_allocate (table,
143 sizeof (struct coff_debug_merge_hash_entry)));
144 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
145 return (struct bfd_hash_entry *) ret;
147 /* Call the allocation method of the superclass. */
148 ret = ((struct coff_debug_merge_hash_entry *)
149 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
150 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
152 /* Set local fields. */
156 return (struct bfd_hash_entry *) ret;
159 /* Given a COFF BFD, add symbols to the global hash table as
163 _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
165 switch (bfd_get_format (abfd))
168 return coff_link_add_object_symbols (abfd, info);
170 return _bfd_generic_link_add_archive_symbols
171 (abfd, info, coff_link_check_archive_element);
173 bfd_set_error (bfd_error_wrong_format);
178 /* Add symbols from a COFF object file. */
181 coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
183 if (! _bfd_coff_get_external_symbols (abfd))
185 if (! coff_link_add_symbols (abfd, info))
188 if (! info->keep_memory
189 && ! _bfd_coff_free_symbols (abfd))
195 /* Check a single archive element to see if we need to include it in
196 the link. *PNEEDED is set according to whether this element is
197 needed in the link or not. This is called via
198 _bfd_generic_link_add_archive_symbols. */
201 coff_link_check_archive_element (bfd *abfd,
202 struct bfd_link_info *info,
203 struct bfd_link_hash_entry *h,
205 bfd_boolean *pneeded)
209 /* We are only interested in symbols that are currently undefined.
210 If a symbol is currently known to be common, COFF linkers do not
211 bring in an object file which defines it. */
212 if (h->type != bfd_link_hash_undefined)
215 /* PR 22369 - Skip non COFF objects in the archive. */
216 if (! bfd_family_coff (abfd))
219 /* Include this element? */
220 if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
224 return coff_link_add_object_symbols (abfd, info);
227 /* Add all the symbols from an object file to the hash table. */
230 coff_link_add_symbols (bfd *abfd,
231 struct bfd_link_info *info)
233 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
234 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
235 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
236 bfd_boolean keep_syms;
237 bfd_boolean default_copy;
238 bfd_size_type symcount;
239 struct coff_link_hash_entry **sym_hash;
240 bfd_size_type symesz;
245 symcount = obj_raw_syment_count (abfd);
248 return TRUE; /* Nothing to do. */
250 /* Keep the symbols during this function, in case the linker needs
251 to read the generic symbols in order to report an error message. */
252 keep_syms = obj_coff_keep_syms (abfd);
253 obj_coff_keep_syms (abfd) = TRUE;
255 if (info->keep_memory)
256 default_copy = FALSE;
260 /* We keep a list of the linker hash table entries that correspond
261 to particular symbols. */
262 amt = symcount * sizeof (struct coff_link_hash_entry *);
263 sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
264 if (sym_hash == NULL)
266 obj_coff_sym_hashes (abfd) = sym_hash;
268 symesz = bfd_coff_symesz (abfd);
269 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
270 esym = (bfd_byte *) obj_coff_external_syms (abfd);
271 esym_end = esym + symcount * symesz;
272 while (esym < esym_end)
274 struct internal_syment sym;
275 enum coff_symbol_classification classification;
278 bfd_coff_swap_sym_in (abfd, esym, &sym);
280 classification = bfd_coff_classify_symbol (abfd, &sym);
281 if (classification != COFF_SYMBOL_LOCAL)
284 char buf[SYMNMLEN + 1];
290 /* This symbol is externally visible. */
292 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
296 /* We must copy the name into memory if we got it from the
297 syment itself, rather than the string table. */
299 if (sym._n._n_n._n_zeroes != 0
300 || sym._n._n_n._n_offset == 0)
305 switch (classification)
310 case COFF_SYMBOL_GLOBAL:
311 flags = BSF_EXPORT | BSF_GLOBAL;
312 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
314 value -= section->vma;
317 case COFF_SYMBOL_UNDEFINED:
319 section = bfd_und_section_ptr;
322 case COFF_SYMBOL_COMMON:
324 section = bfd_com_section_ptr;
327 case COFF_SYMBOL_PE_SECTION:
328 flags = BSF_SECTION_SYM | BSF_GLOBAL;
329 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
333 if (IS_WEAK_EXTERNAL (abfd, sym))
338 /* In the PE format, section symbols actually refer to the
339 start of the output section. We handle them specially
341 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
343 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
344 name, FALSE, copy, FALSE);
345 if (*sym_hash != NULL)
347 if (((*sym_hash)->coff_link_hash_flags
348 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
349 && (*sym_hash)->root.type != bfd_link_hash_undefined
350 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
352 (_("warning: symbol `%s' is both section and non-section"),
359 /* The Microsoft Visual C compiler does string pooling by
360 hashing the constants to an internal symbol name, and
361 relying on the linker comdat support to discard
362 duplicate names. However, if one string is a literal and
363 one is a data initializer, one will end up in the .data
364 section and one will end up in the .rdata section. The
365 Microsoft linker will combine them into the .data
366 section, which seems to be wrong since it might cause the
369 As long as there are no external references to the
370 symbols, which there shouldn't be, we can treat the .data
371 and .rdata instances as separate symbols. The comdat
372 code in the linker will do the appropriate merging. Here
373 we avoid getting a multiple definition error for one of
374 these special symbols.
376 FIXME: I don't think this will work in the case where
377 there are two object files which use the constants as a
378 literal and two object files which use it as a data
379 initializer. One or the other of the second object files
380 is going to wind up with an inappropriate reference. */
382 && (classification == COFF_SYMBOL_GLOBAL
383 || classification == COFF_SYMBOL_PE_SECTION)
384 && coff_section_data (abfd, section) != NULL
385 && coff_section_data (abfd, section)->comdat != NULL
386 && CONST_STRNEQ (name, "??_")
387 && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
389 if (*sym_hash == NULL)
390 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
391 name, FALSE, copy, FALSE);
392 if (*sym_hash != NULL
393 && (*sym_hash)->root.type == bfd_link_hash_defined
394 && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
395 && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
396 coff_section_data (abfd, section)->comdat->name) == 0)
402 if (! (bfd_coff_link_add_one_symbol
403 (info, abfd, name, flags, section, value,
404 (const char *) NULL, copy, FALSE,
405 (struct bfd_link_hash_entry **) sym_hash)))
409 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
410 (*sym_hash)->coff_link_hash_flags |=
411 COFF_LINK_HASH_PE_SECTION_SYMBOL;
413 /* Limit the alignment of a common symbol to the possible
414 alignment of a section. There is no point to permitting
415 a higher alignment for a common symbol: we can not
416 guarantee it, and it may cause us to allocate extra space
417 in the common section. */
418 if (section == bfd_com_section_ptr
419 && (*sym_hash)->root.type == bfd_link_hash_common
420 && ((*sym_hash)->root.u.c.p->alignment_power
421 > bfd_coff_default_section_alignment_power (abfd)))
422 (*sym_hash)->root.u.c.p->alignment_power
423 = bfd_coff_default_section_alignment_power (abfd);
425 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
427 /* If we don't have any symbol information currently in
428 the hash table, or if we are looking at a symbol
429 definition, then update the symbol class and type in
431 if (((*sym_hash)->symbol_class == C_NULL
432 && (*sym_hash)->type == T_NULL)
435 && (*sym_hash)->root.type != bfd_link_hash_defined
436 && (*sym_hash)->root.type != bfd_link_hash_defweak))
438 (*sym_hash)->symbol_class = sym.n_sclass;
439 if (sym.n_type != T_NULL)
441 /* We want to warn if the type changed, but not
442 if it changed from an unspecified type.
443 Testing the whole type byte may work, but the
444 change from (e.g.) a function of unspecified
445 type to function of known type also wants to
447 if ((*sym_hash)->type != T_NULL
448 && (*sym_hash)->type != sym.n_type
449 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
450 && (BTYPE ((*sym_hash)->type) == T_NULL
451 || BTYPE (sym.n_type) == T_NULL)))
453 /* xgettext: c-format */
454 (_("warning: type of symbol `%s' changed"
455 " from %d to %d in %pB"),
456 name, (*sym_hash)->type, sym.n_type, abfd);
458 /* We don't want to change from a meaningful
459 base type to a null one, but if we know
460 nothing, take what little we might now know. */
461 if (BTYPE (sym.n_type) != T_NULL
462 || (*sym_hash)->type == T_NULL)
463 (*sym_hash)->type = sym.n_type;
465 (*sym_hash)->auxbfd = abfd;
466 if (sym.n_numaux != 0)
468 union internal_auxent *alloc;
471 union internal_auxent *iaux;
473 (*sym_hash)->numaux = sym.n_numaux;
474 alloc = ((union internal_auxent *)
475 bfd_hash_allocate (&info->hash->table,
477 * sizeof (*alloc))));
480 for (i = 0, eaux = esym + symesz, iaux = alloc;
482 i++, eaux += symesz, iaux++)
483 bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
484 sym.n_sclass, (int) i,
486 (*sym_hash)->aux = alloc;
491 if (classification == COFF_SYMBOL_PE_SECTION
492 && (*sym_hash)->numaux != 0)
494 /* Some PE sections (such as .bss) have a zero size in
495 the section header, but a non-zero size in the AUX
496 record. Correct that here.
498 FIXME: This is not at all the right place to do this.
499 For example, it won't help objdump. This needs to be
500 done when we swap in the section header. */
501 BFD_ASSERT ((*sym_hash)->numaux == 1);
502 if (section->size == 0)
503 section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
505 /* FIXME: We could test whether the section sizes
506 matches the size in the aux entry, but apparently
507 that sometimes fails unexpectedly. */
511 esym += (sym.n_numaux + 1) * symesz;
512 sym_hash += sym.n_numaux + 1;
515 /* If this is a non-traditional, non-relocatable link, try to
516 optimize the handling of any .stab/.stabstr sections. */
517 if (! bfd_link_relocatable (info)
518 && ! info->traditional_format
519 && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
520 && (info->strip != strip_all && info->strip != strip_debugger))
524 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
528 bfd_size_type string_offset = 0;
531 for (stab = abfd->sections; stab; stab = stab->next)
532 if (CONST_STRNEQ (stab->name, ".stab")
534 || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
536 struct coff_link_hash_table *table;
537 struct coff_section_tdata *secdata
538 = coff_section_data (abfd, stab);
542 amt = sizeof (struct coff_section_tdata);
543 stab->used_by_bfd = bfd_zalloc (abfd, amt);
544 if (stab->used_by_bfd == NULL)
546 secdata = coff_section_data (abfd, stab);
549 table = coff_hash_table (info);
551 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
560 obj_coff_keep_syms (abfd) = keep_syms;
565 obj_coff_keep_syms (abfd) = keep_syms;
569 /* Do the final link step. */
572 _bfd_coff_final_link (bfd *abfd,
573 struct bfd_link_info *info)
575 bfd_size_type symesz;
576 struct coff_final_link_info flaginfo;
577 bfd_boolean debug_merge_allocated;
578 bfd_boolean long_section_names;
580 struct bfd_link_order *p;
581 bfd_size_type max_sym_count;
582 bfd_size_type max_lineno_count;
583 bfd_size_type max_reloc_count;
584 bfd_size_type max_output_reloc_count;
585 bfd_size_type max_contents_size;
586 file_ptr rel_filepos;
588 file_ptr line_filepos;
591 bfd_byte *external_relocs = NULL;
592 char strbuf[STRING_SIZE_SIZE];
595 symesz = bfd_coff_symesz (abfd);
597 flaginfo.info = info;
598 flaginfo.output_bfd = abfd;
599 flaginfo.strtab = NULL;
600 flaginfo.section_info = NULL;
601 flaginfo.last_file_index = -1;
602 flaginfo.last_bf_index = -1;
603 flaginfo.internal_syms = NULL;
604 flaginfo.sec_ptrs = NULL;
605 flaginfo.sym_indices = NULL;
606 flaginfo.outsyms = NULL;
607 flaginfo.linenos = NULL;
608 flaginfo.contents = NULL;
609 flaginfo.external_relocs = NULL;
610 flaginfo.internal_relocs = NULL;
611 flaginfo.global_to_static = FALSE;
612 debug_merge_allocated = FALSE;
614 coff_data (abfd)->link_info = info;
616 flaginfo.strtab = _bfd_stringtab_init ();
617 if (flaginfo.strtab == NULL)
620 if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
622 debug_merge_allocated = TRUE;
624 /* Compute the file positions for all the sections. */
625 if (! abfd->output_has_begun)
627 if (! bfd_coff_compute_section_file_positions (abfd))
631 /* Count the line numbers and relocation entries required for the
632 output file. Set the file positions for the relocs. */
633 rel_filepos = obj_relocbase (abfd);
634 relsz = bfd_coff_relsz (abfd);
635 max_contents_size = 0;
636 max_lineno_count = 0;
639 long_section_names = FALSE;
640 for (o = abfd->sections; o != NULL; o = o->next)
644 for (p = o->map_head.link_order; p != NULL; p = p->next)
646 if (p->type == bfd_indirect_link_order)
650 sec = p->u.indirect.section;
652 /* Mark all sections which are to be included in the
653 link. This will normally be every section. We need
654 to do this so that we can identify any sections which
655 the linker has decided to not include. */
656 sec->linker_mark = TRUE;
658 if (info->strip == strip_none
659 || info->strip == strip_some)
660 o->lineno_count += sec->lineno_count;
662 if (bfd_link_relocatable (info))
663 o->reloc_count += sec->reloc_count;
665 if (sec->rawsize > max_contents_size)
666 max_contents_size = sec->rawsize;
667 if (sec->size > max_contents_size)
668 max_contents_size = sec->size;
669 if (sec->lineno_count > max_lineno_count)
670 max_lineno_count = sec->lineno_count;
671 if (sec->reloc_count > max_reloc_count)
672 max_reloc_count = sec->reloc_count;
674 else if (bfd_link_relocatable (info)
675 && (p->type == bfd_section_reloc_link_order
676 || p->type == bfd_symbol_reloc_link_order))
679 if (o->reloc_count == 0)
683 o->flags |= SEC_RELOC;
684 o->rel_filepos = rel_filepos;
685 rel_filepos += o->reloc_count * relsz;
686 /* In PE COFF, if there are at least 0xffff relocations an
687 extra relocation will be written out to encode the count. */
688 if (obj_pe (abfd) && o->reloc_count >= 0xffff)
689 rel_filepos += relsz;
692 if (bfd_coff_long_section_names (abfd)
693 && strlen (o->name) > SCNNMLEN)
695 /* This section has a long name which must go in the string
696 table. This must correspond to the code in
697 coff_write_object_contents which puts the string index
698 into the s_name field of the section header. That is why
699 we pass hash as FALSE. */
700 if (_bfd_stringtab_add (flaginfo.strtab, o->name, FALSE, FALSE)
701 == (bfd_size_type) -1)
703 long_section_names = TRUE;
707 /* If doing a relocatable link, allocate space for the pointers we
709 if (bfd_link_relocatable (info))
713 /* We use section_count + 1, rather than section_count, because
714 the target_index fields are 1 based. */
715 amt = abfd->section_count + 1;
716 amt *= sizeof (struct coff_link_section_info);
717 flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
718 if (flaginfo.section_info == NULL)
720 for (i = 0; i <= abfd->section_count; i++)
722 flaginfo.section_info[i].relocs = NULL;
723 flaginfo.section_info[i].rel_hashes = NULL;
727 /* We now know the size of the relocs, so we can determine the file
728 positions of the line numbers. */
729 line_filepos = rel_filepos;
730 linesz = bfd_coff_linesz (abfd);
731 max_output_reloc_count = 0;
732 for (o = abfd->sections; o != NULL; o = o->next)
734 if (o->lineno_count == 0)
738 o->line_filepos = line_filepos;
739 line_filepos += o->lineno_count * linesz;
742 if (o->reloc_count != 0)
744 /* We don't know the indices of global symbols until we have
745 written out all the local symbols. For each section in
746 the output file, we keep an array of pointers to hash
747 table entries. Each entry in the array corresponds to a
748 reloc. When we find a reloc against a global symbol, we
749 set the corresponding entry in this array so that we can
750 fix up the symbol index after we have written out all the
753 Because of this problem, we also keep the relocs in
754 memory until the end of the link. This wastes memory,
755 but only when doing a relocatable link, which is not the
757 BFD_ASSERT (bfd_link_relocatable (info));
758 amt = o->reloc_count;
759 amt *= sizeof (struct internal_reloc);
760 flaginfo.section_info[o->target_index].relocs =
761 (struct internal_reloc *) bfd_malloc (amt);
762 amt = o->reloc_count;
763 amt *= sizeof (struct coff_link_hash_entry *);
764 flaginfo.section_info[o->target_index].rel_hashes =
765 (struct coff_link_hash_entry **) bfd_malloc (amt);
766 if (flaginfo.section_info[o->target_index].relocs == NULL
767 || flaginfo.section_info[o->target_index].rel_hashes == NULL)
770 if (o->reloc_count > max_output_reloc_count)
771 max_output_reloc_count = o->reloc_count;
774 /* Reset the reloc and lineno counts, so that we can use them to
775 count the number of entries we have output so far. */
780 obj_sym_filepos (abfd) = line_filepos;
782 /* Figure out the largest number of symbols in an input BFD. Take
783 the opportunity to clear the output_has_begun fields of all the
786 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
790 sub->output_has_begun = FALSE;
791 sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
792 if (sz > max_sym_count)
796 /* Allocate some buffers used while linking. */
797 amt = max_sym_count * sizeof (struct internal_syment);
798 flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
799 amt = max_sym_count * sizeof (asection *);
800 flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
801 amt = max_sym_count * sizeof (long);
802 flaginfo.sym_indices = (long int *) bfd_malloc (amt);
803 flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
804 amt = max_lineno_count * bfd_coff_linesz (abfd);
805 flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
806 flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
807 amt = max_reloc_count * relsz;
808 flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
809 if (! bfd_link_relocatable (info))
811 amt = max_reloc_count * sizeof (struct internal_reloc);
812 flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
814 if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
815 || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
816 || (flaginfo.sym_indices == NULL && max_sym_count > 0)
817 || flaginfo.outsyms == NULL
818 || (flaginfo.linenos == NULL && max_lineno_count > 0)
819 || (flaginfo.contents == NULL && max_contents_size > 0)
820 || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
821 || (! bfd_link_relocatable (info)
822 && flaginfo.internal_relocs == NULL
823 && max_reloc_count > 0))
826 /* We now know the position of everything in the file, except that
827 we don't know the size of the symbol table and therefore we don't
828 know where the string table starts. We just build the string
829 table in memory as we go along. We process all the relocations
830 for a single input file at once. */
831 obj_raw_syment_count (abfd) = 0;
833 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
835 if (! bfd_coff_start_final_link (abfd, info))
839 for (o = abfd->sections; o != NULL; o = o->next)
841 for (p = o->map_head.link_order; p != NULL; p = p->next)
843 if (p->type == bfd_indirect_link_order
844 && bfd_family_coff (p->u.indirect.section->owner))
846 sub = p->u.indirect.section->owner;
847 if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
849 if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
851 sub->output_has_begun = TRUE;
854 else if (p->type == bfd_section_reloc_link_order
855 || p->type == bfd_symbol_reloc_link_order)
857 if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
862 if (! _bfd_default_link_order (abfd, info, o, p))
868 if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
870 /* Add local symbols from foreign inputs. */
871 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
875 if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
877 for (i = 0; i < bfd_get_symcount (sub); ++i)
879 asymbol *sym = bfd_get_outsymbols (sub) [i];
881 struct internal_syment isym;
882 union internal_auxent iaux;
883 bfd_size_type string_size = 0, indx;
885 bfd_boolean rewrite = FALSE, hash;
887 if (! (sym->flags & BSF_LOCAL)
888 || (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
889 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
891 || ((sym->flags & BSF_DEBUGGING)
892 && ! (sym->flags & BSF_FILE)))
895 /* See if we are discarding symbols with this name. */
896 if ((flaginfo.info->strip == strip_some
897 && (bfd_hash_lookup (flaginfo.info->keep_hash,
898 bfd_asymbol_name(sym), FALSE, FALSE)
900 || (((flaginfo.info->discard == discard_sec_merge
901 && (bfd_get_section (sym)->flags & SEC_MERGE)
902 && ! bfd_link_relocatable (flaginfo.info))
903 || flaginfo.info->discard == discard_l)
904 && bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
907 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
909 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
911 if (! coff_write_alien_symbol(abfd, sym, &isym, &iaux, &written,
912 &string_size, NULL, NULL))
915 hash = !flaginfo.info->traditional_format;
917 if (string_size >= 6 && isym.n_sclass == C_FILE
918 && ! isym._n._n_n._n_zeroes && isym.n_numaux)
920 indx = _bfd_stringtab_add (flaginfo.strtab, ".file", hash,
922 if (indx == (bfd_size_type) -1)
924 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
925 bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
926 if (bfd_seek (abfd, pos, SEEK_SET) != 0
927 || bfd_bwrite (flaginfo.outsyms, symesz,
935 indx = _bfd_stringtab_add (flaginfo.strtab,
936 bfd_asymbol_name (sym), hash,
938 if (indx == (bfd_size_type) -1)
940 if (isym.n_sclass != C_FILE)
942 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
943 bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
948 BFD_ASSERT (isym.n_numaux == 1);
949 iaux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
950 bfd_coff_swap_aux_out (abfd, &iaux, isym.n_type, C_FILE,
951 0, 1, flaginfo.outsyms + symesz);
952 if (bfd_seek (abfd, pos + symesz, SEEK_SET) != 0
953 || bfd_bwrite (flaginfo.outsyms + symesz, symesz,
959 if (isym.n_sclass == C_FILE)
961 if (flaginfo.last_file_index != -1)
963 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
964 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
966 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
970 flaginfo.last_file_index = obj_raw_syment_count (abfd);
971 flaginfo.last_file = isym;
975 && (bfd_seek (abfd, pos, SEEK_SET) != 0
976 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz))
979 obj_raw_syment_count (abfd) += written;
984 if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
987 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
989 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
990 debug_merge_allocated = FALSE;
992 if (flaginfo.internal_syms != NULL)
994 free (flaginfo.internal_syms);
995 flaginfo.internal_syms = NULL;
997 if (flaginfo.sec_ptrs != NULL)
999 free (flaginfo.sec_ptrs);
1000 flaginfo.sec_ptrs = NULL;
1002 if (flaginfo.sym_indices != NULL)
1004 free (flaginfo.sym_indices);
1005 flaginfo.sym_indices = NULL;
1007 if (flaginfo.linenos != NULL)
1009 free (flaginfo.linenos);
1010 flaginfo.linenos = NULL;
1012 if (flaginfo.contents != NULL)
1014 free (flaginfo.contents);
1015 flaginfo.contents = NULL;
1017 if (flaginfo.external_relocs != NULL)
1019 free (flaginfo.external_relocs);
1020 flaginfo.external_relocs = NULL;
1022 if (flaginfo.internal_relocs != NULL)
1024 free (flaginfo.internal_relocs);
1025 flaginfo.internal_relocs = NULL;
1028 /* The value of the last C_FILE symbol is supposed to be the symbol
1029 index of the first external symbol. Write it out again if
1031 if (flaginfo.last_file_index != -1
1032 && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
1036 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
1037 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
1040 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
1041 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1042 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
1046 /* If doing task linking (ld --task-link) then make a pass through the
1047 global symbols, writing out any that are defined, and making them
1049 if (info->task_link)
1051 flaginfo.failed = FALSE;
1052 coff_link_hash_traverse (coff_hash_table (info),
1053 _bfd_coff_write_task_globals, &flaginfo);
1054 if (flaginfo.failed)
1058 /* Write out the global symbols. */
1059 flaginfo.failed = FALSE;
1060 bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
1061 if (flaginfo.failed)
1064 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1065 if (flaginfo.outsyms != NULL)
1067 free (flaginfo.outsyms);
1068 flaginfo.outsyms = NULL;
1071 if (bfd_link_relocatable (info) && max_output_reloc_count > 0)
1073 /* Now that we have written out all the global symbols, we know
1074 the symbol indices to use for relocs against them, and we can
1075 finally write out the relocs. */
1076 amt = max_output_reloc_count * relsz;
1077 external_relocs = (bfd_byte *) bfd_malloc (amt);
1078 if (external_relocs == NULL)
1081 for (o = abfd->sections; o != NULL; o = o->next)
1083 struct internal_reloc *irel;
1084 struct internal_reloc *irelend;
1085 struct coff_link_hash_entry **rel_hash;
1088 if (o->reloc_count == 0)
1091 irel = flaginfo.section_info[o->target_index].relocs;
1092 irelend = irel + o->reloc_count;
1093 rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
1094 erel = external_relocs;
1095 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1097 if (*rel_hash != NULL)
1099 BFD_ASSERT ((*rel_hash)->indx >= 0);
1100 irel->r_symndx = (*rel_hash)->indx;
1102 bfd_coff_swap_reloc_out (abfd, irel, erel);
1105 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1107 if (obj_pe (abfd) && o->reloc_count >= 0xffff)
1109 /* In PE COFF, write the count of relocs as the first
1110 reloc. The header overflow bit will be set
1112 struct internal_reloc incount;
1113 bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
1115 memset (&incount, 0, sizeof (incount));
1116 incount.r_vaddr = o->reloc_count + 1;
1117 bfd_coff_swap_reloc_out (abfd, &incount, excount);
1118 if (bfd_bwrite (excount, relsz, abfd) != relsz)
1119 /* We'll leak, but it's an error anyway. */
1123 if (bfd_bwrite (external_relocs,
1124 (bfd_size_type) relsz * o->reloc_count, abfd)
1125 != (bfd_size_type) relsz * o->reloc_count)
1129 free (external_relocs);
1130 external_relocs = NULL;
1133 /* Free up the section information. */
1134 if (flaginfo.section_info != NULL)
1138 for (i = 0; i < abfd->section_count; i++)
1140 if (flaginfo.section_info[i].relocs != NULL)
1141 free (flaginfo.section_info[i].relocs);
1142 if (flaginfo.section_info[i].rel_hashes != NULL)
1143 free (flaginfo.section_info[i].rel_hashes);
1145 free (flaginfo.section_info);
1146 flaginfo.section_info = NULL;
1149 /* If we have optimized stabs strings, output them. */
1150 if (coff_hash_table (info)->stab_info.stabstr != NULL)
1152 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1156 /* Write out the string table. */
1157 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1161 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1162 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1165 #if STRING_SIZE_SIZE == 4
1167 _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
1170 #error Change H_PUT_32 above
1173 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1174 != STRING_SIZE_SIZE)
1177 if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
1180 obj_coff_strings_written (abfd) = TRUE;
1183 _bfd_stringtab_free (flaginfo.strtab);
1185 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1186 not try to write out the symbols. */
1187 bfd_get_symcount (abfd) = 0;
1192 if (debug_merge_allocated)
1193 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
1194 if (flaginfo.strtab != NULL)
1195 _bfd_stringtab_free (flaginfo.strtab);
1196 if (flaginfo.section_info != NULL)
1200 for (i = 0; i < abfd->section_count; i++)
1202 if (flaginfo.section_info[i].relocs != NULL)
1203 free (flaginfo.section_info[i].relocs);
1204 if (flaginfo.section_info[i].rel_hashes != NULL)
1205 free (flaginfo.section_info[i].rel_hashes);
1207 free (flaginfo.section_info);
1209 if (flaginfo.internal_syms != NULL)
1210 free (flaginfo.internal_syms);
1211 if (flaginfo.sec_ptrs != NULL)
1212 free (flaginfo.sec_ptrs);
1213 if (flaginfo.sym_indices != NULL)
1214 free (flaginfo.sym_indices);
1215 if (flaginfo.outsyms != NULL)
1216 free (flaginfo.outsyms);
1217 if (flaginfo.linenos != NULL)
1218 free (flaginfo.linenos);
1219 if (flaginfo.contents != NULL)
1220 free (flaginfo.contents);
1221 if (flaginfo.external_relocs != NULL)
1222 free (flaginfo.external_relocs);
1223 if (flaginfo.internal_relocs != NULL)
1224 free (flaginfo.internal_relocs);
1225 if (external_relocs != NULL)
1226 free (external_relocs);
1230 /* Parse out a -heap <reserved>,<commit> line. */
1233 dores_com (char *ptr, bfd *output_bfd, int heap)
1235 if (coff_data(output_bfd)->pe)
1237 int val = strtoul (ptr, &ptr, 0);
1240 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1242 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1246 val = strtoul (ptr+1, &ptr, 0);
1248 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1250 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1257 get_name (char *ptr, char **dst)
1262 while (*ptr && *ptr != ' ')
1268 /* Process any magic embedded commands in a section called .drectve. */
1271 process_embedded_commands (bfd *output_bfd,
1272 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1275 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1283 if (!bfd_malloc_and_get_section (abfd, sec, ©))
1289 e = (char *) copy + sec->size;
1291 for (s = (char *) copy; s < e ; )
1298 if (CONST_STRNEQ (s, "-attr"))
1308 s = get_name (s, &name);
1309 s = get_name (s, &attribs);
1329 asec = bfd_get_section_by_name (abfd, name);
1333 asec->flags |= SEC_CODE;
1335 asec->flags |= SEC_READONLY;
1338 else if (CONST_STRNEQ (s, "-heap"))
1339 s = dores_com (s + 5, output_bfd, 1);
1341 else if (CONST_STRNEQ (s, "-stack"))
1342 s = dores_com (s + 6, output_bfd, 0);
1344 /* GNU extension for aligned commons. */
1345 else if (CONST_STRNEQ (s, "-aligncomm:"))
1347 /* Common symbols must be aligned on reading, as it
1348 is too late to do anything here, after they have
1349 already been allocated, so just skip the directive. */
1360 /* Place a marker against all symbols which are used by relocations.
1361 This marker can be picked up by the 'do we skip this symbol ?'
1362 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1366 mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1370 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1373 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1375 struct internal_reloc * internal_relocs;
1376 struct internal_reloc * irel;
1377 struct internal_reloc * irelend;
1379 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1
1380 || a->linker_mark == 0)
1382 /* Don't mark relocs in excluded sections. */
1383 if (a->output_section == bfd_abs_section_ptr)
1386 /* Read in the relocs. */
1387 internal_relocs = _bfd_coff_read_internal_relocs
1388 (input_bfd, a, FALSE,
1389 flaginfo->external_relocs,
1390 bfd_link_relocatable (flaginfo->info),
1391 (bfd_link_relocatable (flaginfo->info)
1392 ? (flaginfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1393 : flaginfo->internal_relocs)
1396 if (internal_relocs == NULL)
1399 irel = internal_relocs;
1400 irelend = irel + a->reloc_count;
1402 /* Place a mark in the sym_indices array (whose entries have
1403 been initialised to 0) for all of the symbols that are used
1404 in the relocation table. This will then be picked up in the
1405 skip/don't-skip pass. */
1406 for (; irel < irelend; irel++)
1407 if ((unsigned long) irel->r_symndx < obj_raw_syment_count (input_bfd))
1408 flaginfo->sym_indices[irel->r_symndx] = -1;
1412 /* Link an input file into the linker output file. This function
1413 handles all the sections and relocations of the input file at once. */
1416 _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1418 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1419 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1420 bfd_boolean (*adjust_symndx)
1421 (bfd *, struct bfd_link_info *, bfd *, asection *,
1422 struct internal_reloc *, bfd_boolean *);
1424 const char *strings;
1425 bfd_size_type syment_base;
1426 bfd_boolean copy, hash;
1427 bfd_size_type isymesz;
1428 bfd_size_type osymesz;
1429 bfd_size_type linesz;
1432 struct internal_syment *isymp;
1435 unsigned long output_index;
1437 struct coff_link_hash_entry **sym_hash;
1440 /* Move all the symbols to the output file. */
1442 output_bfd = flaginfo->output_bfd;
1444 syment_base = obj_raw_syment_count (output_bfd);
1445 isymesz = bfd_coff_symesz (input_bfd);
1446 osymesz = bfd_coff_symesz (output_bfd);
1447 linesz = bfd_coff_linesz (input_bfd);
1448 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1451 if (! flaginfo->info->keep_memory)
1454 if (flaginfo->info->traditional_format)
1457 if (! _bfd_coff_get_external_symbols (input_bfd))
1460 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1461 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1462 isymp = flaginfo->internal_syms;
1463 secpp = flaginfo->sec_ptrs;
1464 indexp = flaginfo->sym_indices;
1465 output_index = syment_base;
1466 outsym = flaginfo->outsyms;
1468 if (coff_data (output_bfd)->pe
1469 && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd))
1472 /* If we are going to perform relocations and also strip/discard some
1473 symbols then we must make sure that we do not strip/discard those
1474 symbols that are going to be involved in the relocations. */
1475 if (( flaginfo->info->strip != strip_none
1476 || flaginfo->info->discard != discard_none)
1477 && bfd_link_relocatable (flaginfo->info))
1479 /* Mark the symbol array as 'not-used'. */
1480 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1482 mark_relocs (flaginfo, input_bfd);
1485 while (esym < esym_end)
1487 struct internal_syment isym;
1488 enum coff_symbol_classification classification;
1491 bfd_boolean dont_skip_symbol;
1494 bfd_coff_swap_sym_in (input_bfd, esym, isymp);
1496 /* Make a copy of *isymp so that the relocate_section function
1497 always sees the original values. This is more reliable than
1498 always recomputing the symbol value even if we are stripping
1502 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1503 switch (classification)
1507 case COFF_SYMBOL_GLOBAL:
1508 case COFF_SYMBOL_PE_SECTION:
1509 case COFF_SYMBOL_LOCAL:
1510 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1512 case COFF_SYMBOL_COMMON:
1513 *secpp = bfd_com_section_ptr;
1515 case COFF_SYMBOL_UNDEFINED:
1516 *secpp = bfd_und_section_ptr;
1520 /* Extract the flag indicating if this symbol is used by a
1522 if ((flaginfo->info->strip != strip_none
1523 || flaginfo->info->discard != discard_none)
1524 && bfd_link_relocatable (flaginfo->info))
1525 dont_skip_symbol = *indexp;
1527 dont_skip_symbol = FALSE;
1533 add = 1 + isym.n_numaux;
1535 /* If we are stripping all symbols, we want to skip this one. */
1536 if (flaginfo->info->strip == strip_all && ! dont_skip_symbol)
1541 switch (classification)
1545 case COFF_SYMBOL_GLOBAL:
1546 case COFF_SYMBOL_COMMON:
1547 case COFF_SYMBOL_PE_SECTION:
1548 /* This is a global symbol. Global symbols come at the
1549 end of the symbol table, so skip them for now.
1550 Locally defined function symbols, however, are an
1551 exception, and are not moved to the end. */
1553 if (! ISFCN (isym.n_type))
1557 case COFF_SYMBOL_UNDEFINED:
1558 /* Undefined symbols are left for the end. */
1563 case COFF_SYMBOL_LOCAL:
1564 /* This is a local symbol. Skip it if we are discarding
1566 if (flaginfo->info->discard == discard_all && ! dont_skip_symbol)
1572 #ifndef COFF_WITH_PE
1573 /* Skip section symbols for sections which are not going to be
1576 && !dont_skip_symbol
1577 && isym.n_sclass == C_STAT
1578 && isym.n_type == T_NULL
1579 && isym.n_numaux > 0
1580 && ((*secpp)->output_section == bfd_abs_section_ptr
1581 || bfd_section_removed_from_list (output_bfd,
1582 (*secpp)->output_section)))
1586 /* If we stripping debugging symbols, and this is a debugging
1587 symbol, then skip it. FIXME: gas sets the section to N_ABS
1588 for some types of debugging symbols; I don't know if this is
1589 a bug or not. In any case, we handle it here. */
1591 && flaginfo->info->strip == strip_debugger
1592 && ! dont_skip_symbol
1593 && (isym.n_scnum == N_DEBUG
1594 || (isym.n_scnum == N_ABS
1595 && (isym.n_sclass == C_AUTO
1596 || isym.n_sclass == C_REG
1597 || isym.n_sclass == C_MOS
1598 || isym.n_sclass == C_MOE
1599 || isym.n_sclass == C_MOU
1600 || isym.n_sclass == C_ARG
1601 || isym.n_sclass == C_REGPARM
1602 || isym.n_sclass == C_FIELD
1603 || isym.n_sclass == C_EOS))))
1606 /* If some symbols are stripped based on the name, work out the
1607 name and decide whether to skip this symbol. */
1609 && (flaginfo->info->strip == strip_some
1610 || flaginfo->info->discard == discard_l))
1613 char buf[SYMNMLEN + 1];
1615 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1619 if (! dont_skip_symbol
1620 && ((flaginfo->info->strip == strip_some
1621 && (bfd_hash_lookup (flaginfo->info->keep_hash, name, FALSE,
1624 && flaginfo->info->discard == discard_l
1625 && bfd_is_local_label_name (input_bfd, name))))
1629 /* If this is an enum, struct, or union tag, see if we have
1630 already output an identical type. */
1632 && !flaginfo->info->traditional_format
1633 && (isym.n_sclass == C_ENTAG
1634 || isym.n_sclass == C_STRTAG
1635 || isym.n_sclass == C_UNTAG)
1636 && isym.n_numaux == 1)
1639 char buf[SYMNMLEN + 1];
1640 struct coff_debug_merge_hash_entry *mh;
1641 struct coff_debug_merge_type *mt;
1642 union internal_auxent aux;
1643 struct coff_debug_merge_element **epp;
1644 bfd_byte *esl, *eslend;
1645 struct internal_syment *islp;
1648 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1652 /* Ignore fake names invented by compiler; treat them all as
1654 if (*name == '~' || *name == '.' || *name == '$'
1655 || (*name == bfd_get_symbol_leading_char (input_bfd)
1656 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1659 mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name,
1664 /* Allocate memory to hold type information. If this turns
1665 out to be a duplicate, we pass this address to
1667 amt = sizeof (struct coff_debug_merge_type);
1668 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
1671 mt->type_class = isym.n_sclass;
1673 /* Pick up the aux entry, which points to the end of the tag
1675 bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
1676 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1679 /* Gather the elements. */
1680 epp = &mt->elements;
1681 mt->elements = NULL;
1683 esl = esym + 2 * isymesz;
1684 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1685 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1686 while (esl < eslend)
1688 const char *elename;
1689 char elebuf[SYMNMLEN + 1];
1692 bfd_coff_swap_sym_in (input_bfd, esl, islp);
1694 amt = sizeof (struct coff_debug_merge_element);
1695 *epp = (struct coff_debug_merge_element *)
1696 bfd_alloc (input_bfd, amt);
1700 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1702 if (elename == NULL)
1705 amt = strlen (elename) + 1;
1706 name_copy = (char *) bfd_alloc (input_bfd, amt);
1707 if (name_copy == NULL)
1709 strcpy (name_copy, elename);
1711 (*epp)->name = name_copy;
1712 (*epp)->type = islp->n_type;
1714 if (islp->n_numaux >= 1
1715 && islp->n_type != T_NULL
1716 && islp->n_sclass != C_EOS)
1718 union internal_auxent eleaux;
1721 bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
1722 islp->n_type, islp->n_sclass, 0,
1723 islp->n_numaux, &eleaux);
1724 indx = eleaux.x_sym.x_tagndx.l;
1726 /* FIXME: If this tagndx entry refers to a symbol
1727 defined later in this file, we just ignore it.
1728 Handling this correctly would be tedious, and may
1733 (bfd_byte *) obj_coff_external_syms (input_bfd))
1736 (*epp)->tagndx = flaginfo->sym_indices[indx];
1737 if ((*epp)->tagndx < 0)
1741 epp = &(*epp)->next;
1744 esl += (islp->n_numaux + 1) * isymesz;
1745 islp += islp->n_numaux + 1;
1748 /* See if we already have a definition which matches this
1749 type. We always output the type if it has no elements,
1751 if (mt->elements == NULL)
1752 bfd_release (input_bfd, mt);
1755 struct coff_debug_merge_type *mtl;
1757 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1759 struct coff_debug_merge_element *me, *mel;
1761 if (mtl->type_class != mt->type_class)
1764 for (me = mt->elements, mel = mtl->elements;
1765 me != NULL && mel != NULL;
1766 me = me->next, mel = mel->next)
1768 if (strcmp (me->name, mel->name) != 0
1769 || me->type != mel->type
1770 || me->tagndx != mel->tagndx)
1774 if (me == NULL && mel == NULL)
1778 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1780 /* This is the first definition of this type. */
1781 mt->indx = output_index;
1782 mt->next = mh->types;
1787 /* This is a redefinition which can be merged. */
1788 bfd_release (input_bfd, mt);
1789 *indexp = mtl->indx;
1790 add = (eslend - esym) / isymesz;
1796 /* We now know whether we are to skip this symbol or not. */
1799 /* Adjust the symbol in order to output it. */
1801 if (isym._n._n_n._n_zeroes == 0
1802 && isym._n._n_n._n_offset != 0)
1807 /* This symbol has a long name. Enter it in the string
1808 table we are building. Note that we do not check
1809 bfd_coff_symname_in_debug. That is only true for
1810 XCOFF, and XCOFF requires different linking code
1812 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
1815 indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy);
1816 if (indx == (bfd_size_type) -1)
1818 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1821 switch (isym.n_sclass)
1837 /* The symbol value should not be modified. */
1841 if (obj_pe (input_bfd)
1842 && strncmp (isym.n_name, ".bf", sizeof isym.n_name) != 0
1843 && isym.n_scnum > 0)
1845 /* For PE, .lf and .ef get their value left alone,
1846 while .bf gets relocated. However, they all have
1847 "real" section numbers, and need to be moved into
1849 isym.n_scnum = (*secpp)->output_section->target_index;
1854 case C_LABEL: /* Not completely sure about these 2 */
1863 /* Compute new symbol location. */
1864 if (isym.n_scnum > 0)
1866 isym.n_scnum = (*secpp)->output_section->target_index;
1867 isym.n_value += (*secpp)->output_offset;
1868 if (! obj_pe (input_bfd))
1869 isym.n_value -= (*secpp)->vma;
1870 if (! obj_pe (flaginfo->output_bfd))
1871 isym.n_value += (*secpp)->output_section->vma;
1876 /* The value of a C_FILE symbol is the symbol index of
1877 the next C_FILE symbol. The value of the last C_FILE
1878 symbol is the symbol index to the first external
1879 symbol (actually, coff_renumber_symbols does not get
1880 this right--it just sets the value of the last C_FILE
1881 symbol to zero--and nobody has ever complained about
1882 it). We try to get this right, below, just before we
1883 write the symbols out, but in the general case we may
1884 have to write the symbol out twice. */
1885 if (flaginfo->last_file_index != -1
1886 && flaginfo->last_file.n_value != (bfd_vma) output_index)
1888 /* We must correct the value of the last C_FILE
1890 flaginfo->last_file.n_value = output_index;
1891 if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
1893 /* The last C_FILE symbol is in this input file. */
1894 bfd_coff_swap_sym_out (output_bfd,
1895 &flaginfo->last_file,
1897 + ((flaginfo->last_file_index
1905 /* We have already written out the last C_FILE
1906 symbol. We need to write it out again. We
1907 borrow *outsym temporarily. */
1908 bfd_coff_swap_sym_out (output_bfd,
1909 &flaginfo->last_file, outsym);
1910 pos = obj_sym_filepos (output_bfd);
1911 pos += flaginfo->last_file_index * osymesz;
1912 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1913 || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
1918 flaginfo->last_file_index = output_index;
1919 flaginfo->last_file = isym;
1923 /* If doing task linking, convert normal global function symbols to
1924 static functions. */
1925 if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1926 isym.n_sclass = C_STAT;
1928 /* Output the symbol. */
1929 bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
1931 *indexp = output_index;
1936 struct coff_link_hash_entry *h;
1938 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1940 h = obj_coff_sym_hashes (input_bfd)[indx];
1943 /* This can happen if there were errors earlier in
1945 bfd_set_error (bfd_error_bad_value);
1948 h->indx = output_index;
1951 output_index += add;
1952 outsym += add * osymesz;
1955 esym += add * isymesz;
1959 for (--add; add > 0; --add)
1966 /* Fix up the aux entries. This must be done in a separate pass,
1967 because we don't know the correct symbol indices until we have
1968 already decided which symbols we are going to keep. */
1969 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1970 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1971 isymp = flaginfo->internal_syms;
1972 indexp = flaginfo->sym_indices;
1973 sym_hash = obj_coff_sym_hashes (input_bfd);
1974 outsym = flaginfo->outsyms;
1976 while (esym < esym_end)
1980 add = 1 + isymp->n_numaux;
1983 || (bfd_size_type) *indexp < syment_base)
1984 && (*sym_hash == NULL
1985 || (*sym_hash)->auxbfd != input_bfd))
1986 esym += add * isymesz;
1989 struct coff_link_hash_entry *h;
1997 /* The m68k-motorola-sysv assembler will sometimes
1998 generate two symbols with the same name, but only one
1999 will have aux entries. */
2000 BFD_ASSERT (isymp->n_numaux == 0
2002 || h->numaux == isymp->n_numaux);
2010 /* Handle the aux entries. This handling is based on
2011 coff_pointerize_aux. I don't know if it always correct. */
2012 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
2014 union internal_auxent aux;
2015 union internal_auxent *auxp;
2017 if (h != NULL && h->aux != NULL && (h->numaux > i))
2021 bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
2022 isymp->n_sclass, i, isymp->n_numaux, &aux);
2026 if (isymp->n_sclass == C_FILE)
2028 /* If this is a long filename, we must put it in the
2030 if (auxp->x_file.x_n.x_zeroes == 0
2031 && auxp->x_file.x_n.x_offset != 0)
2033 const char *filename;
2036 BFD_ASSERT (auxp->x_file.x_n.x_offset
2037 >= STRING_SIZE_SIZE);
2038 if (strings == NULL)
2040 strings = _bfd_coff_read_string_table (input_bfd);
2041 if (strings == NULL)
2044 if ((bfd_size_type) auxp->x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd))
2045 filename = _("<corrupt>");
2047 filename = strings + auxp->x_file.x_n.x_offset;
2048 indx = _bfd_stringtab_add (flaginfo->strtab, filename,
2050 if (indx == (bfd_size_type) -1)
2052 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
2055 else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2056 && isymp->n_sclass != C_NT_WEAK)
2060 if (ISFCN (isymp->n_type)
2061 || ISTAG (isymp->n_sclass)
2062 || isymp->n_sclass == C_BLOCK
2063 || isymp->n_sclass == C_FCN)
2065 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
2067 && indx < obj_raw_syment_count (input_bfd))
2069 /* We look forward through the symbol for
2070 the index of the next symbol we are going
2071 to include. I don't know if this is
2073 while ((flaginfo->sym_indices[indx] < 0
2074 || ((bfd_size_type) flaginfo->sym_indices[indx]
2076 && indx < obj_raw_syment_count (input_bfd))
2078 if (indx >= obj_raw_syment_count (input_bfd))
2079 indx = output_index;
2081 indx = flaginfo->sym_indices[indx];
2082 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2086 indx = auxp->x_sym.x_tagndx.l;
2087 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2091 symindx = flaginfo->sym_indices[indx];
2093 auxp->x_sym.x_tagndx.l = 0;
2095 auxp->x_sym.x_tagndx.l = symindx;
2098 /* The .bf symbols are supposed to be linked through
2099 the endndx field. We need to carry this list
2100 across object files. */
2103 && isymp->n_sclass == C_FCN
2104 && (isymp->_n._n_n._n_zeroes != 0
2105 || isymp->_n._n_n._n_offset == 0)
2106 && isymp->_n._n_name[0] == '.'
2107 && isymp->_n._n_name[1] == 'b'
2108 && isymp->_n._n_name[2] == 'f'
2109 && isymp->_n._n_name[3] == '\0')
2111 if (flaginfo->last_bf_index != -1)
2113 flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2116 if ((bfd_size_type) flaginfo->last_bf_index
2121 /* The last .bf symbol is in this input
2122 file. This will only happen if the
2123 assembler did not set up the .bf
2124 endndx symbols correctly. */
2125 auxout = (flaginfo->outsyms
2126 + ((flaginfo->last_bf_index
2130 bfd_coff_swap_aux_out (output_bfd,
2141 /* We have already written out the last
2142 .bf aux entry. We need to write it
2143 out again. We borrow *outsym
2144 temporarily. FIXME: This case should
2146 bfd_coff_swap_aux_out (output_bfd,
2152 pos = obj_sym_filepos (output_bfd);
2153 pos += flaginfo->last_bf_index * osymesz;
2154 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2155 || (bfd_bwrite (outsym, osymesz, output_bfd)
2161 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2162 flaginfo->last_bf_index = -1;
2165 /* The endndx field of this aux entry must
2166 be updated with the symbol number of the
2168 flaginfo->last_bf = *auxp;
2169 flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
2178 bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2179 isymp->n_sclass, i, isymp->n_numaux,
2193 /* Relocate the line numbers, unless we are stripping them. */
2194 if (flaginfo->info->strip == strip_none
2195 || flaginfo->info->strip == strip_some)
2197 for (o = input_bfd->sections; o != NULL; o = o->next)
2203 bfd_boolean skipping;
2207 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2208 build_link_order in ldwrite.c will not have created a
2209 link order, which means that we will not have seen this
2210 input section in _bfd_coff_final_link, which means that
2211 we will not have allocated space for the line numbers of
2212 this section. I don't think line numbers can be
2213 meaningful for a section which does not have
2214 SEC_HAS_CONTENTS set, but, if they do, this must be
2216 if (o->lineno_count == 0
2217 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2220 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2221 || bfd_bread (flaginfo->linenos, linesz * o->lineno_count,
2222 input_bfd) != linesz * o->lineno_count)
2225 offset = o->output_section->vma + o->output_offset - o->vma;
2226 eline = flaginfo->linenos;
2227 oeline = flaginfo->linenos;
2228 elineend = eline + linesz * o->lineno_count;
2230 for (; eline < elineend; eline += linesz)
2232 struct internal_lineno iline;
2234 bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2236 if (iline.l_lnno != 0)
2237 iline.l_addr.l_paddr += offset;
2238 else if (iline.l_addr.l_symndx >= 0
2239 && ((unsigned long) iline.l_addr.l_symndx
2240 < obj_raw_syment_count (input_bfd)))
2244 indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
2248 /* These line numbers are attached to a symbol
2249 which we are stripping. We must discard the
2250 line numbers because reading them back with
2251 no associated symbol (or associating them all
2252 with symbol #0) will fail. We can't regain
2253 the space in the output file, but at least
2259 struct internal_syment is;
2260 union internal_auxent ia;
2262 /* Fix up the lnnoptr field in the aux entry of
2263 the symbol. It turns out that we can't do
2264 this when we modify the symbol aux entries,
2265 because gas sometimes screws up the lnnoptr
2266 field and makes it an offset from the start
2267 of the line numbers rather than an absolute
2269 bfd_coff_swap_sym_in (output_bfd,
2271 + ((indx - syment_base)
2273 if ((ISFCN (is.n_type)
2274 || is.n_sclass == C_BLOCK)
2275 && is.n_numaux >= 1)
2279 auxptr = (flaginfo->outsyms
2280 + ((indx - syment_base + 1)
2282 bfd_coff_swap_aux_in (output_bfd, auxptr,
2283 is.n_type, is.n_sclass,
2284 0, is.n_numaux, &ia);
2285 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2286 (o->output_section->line_filepos
2287 + o->output_section->lineno_count * linesz
2288 + eline - flaginfo->linenos);
2289 bfd_coff_swap_aux_out (output_bfd, &ia,
2290 is.n_type, is.n_sclass, 0,
2291 is.n_numaux, auxptr);
2297 iline.l_addr.l_symndx = indx;
2302 bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2307 pos = o->output_section->line_filepos;
2308 pos += o->output_section->lineno_count * linesz;
2309 amt = oeline - flaginfo->linenos;
2310 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2311 || bfd_bwrite (flaginfo->linenos, amt, output_bfd) != amt)
2314 o->output_section->lineno_count += amt / linesz;
2318 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2319 symbol will be the first symbol in the next input file. In the
2320 normal case, this will save us from writing out the C_FILE symbol
2322 if (flaginfo->last_file_index != -1
2323 && (bfd_size_type) flaginfo->last_file_index >= syment_base)
2325 flaginfo->last_file.n_value = output_index;
2326 bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
2328 + ((flaginfo->last_file_index - syment_base)
2332 /* Write the modified symbols to the output file. */
2333 if (outsym > flaginfo->outsyms)
2338 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2339 amt = outsym - flaginfo->outsyms;
2340 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2341 || bfd_bwrite (flaginfo->outsyms, amt, output_bfd) != amt)
2344 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2345 + (outsym - flaginfo->outsyms) / osymesz)
2348 obj_raw_syment_count (output_bfd) = output_index;
2351 /* Relocate the contents of each section. */
2352 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2353 for (o = input_bfd->sections; o != NULL; o = o->next)
2356 struct coff_section_tdata *secdata;
2358 if (! o->linker_mark)
2359 /* This section was omitted from the link. */
2362 if ((o->flags & SEC_LINKER_CREATED) != 0)
2365 if ((o->flags & SEC_HAS_CONTENTS) == 0
2366 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2368 if ((o->flags & SEC_RELOC) != 0
2369 && o->reloc_count != 0)
2372 /* xgettext: c-format */
2373 (_("%pB: relocs in section `%pA', but it has no contents"),
2375 bfd_set_error (bfd_error_no_contents);
2382 secdata = coff_section_data (input_bfd, o);
2383 if (secdata != NULL && secdata->contents != NULL)
2384 contents = secdata->contents;
2387 contents = flaginfo->contents;
2388 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
2392 if ((o->flags & SEC_RELOC) != 0)
2395 struct internal_reloc *internal_relocs;
2396 struct internal_reloc *irel;
2398 /* Read in the relocs. */
2399 target_index = o->output_section->target_index;
2400 internal_relocs = (_bfd_coff_read_internal_relocs
2401 (input_bfd, o, FALSE, flaginfo->external_relocs,
2402 bfd_link_relocatable (flaginfo->info),
2403 (bfd_link_relocatable (flaginfo->info)
2404 ? (flaginfo->section_info[target_index].relocs
2405 + o->output_section->reloc_count)
2406 : flaginfo->internal_relocs)));
2407 if (internal_relocs == NULL
2408 && o->reloc_count > 0)
2411 /* Run through the relocs looking for relocs against symbols
2412 coming from discarded sections and complain about them. */
2413 irel = internal_relocs;
2414 for (; irel < &internal_relocs[o->reloc_count]; irel++)
2416 struct coff_link_hash_entry *h;
2417 asection *ps = NULL;
2418 long symndx = irel->r_symndx;
2421 h = obj_coff_sym_hashes (input_bfd)[symndx];
2424 while (h->root.type == bfd_link_hash_indirect
2425 || h->root.type == bfd_link_hash_warning)
2426 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2427 if (h->root.type == bfd_link_hash_defined
2428 || h->root.type == bfd_link_hash_defweak)
2429 ps = h->root.u.def.section;
2432 /* Complain if definition comes from an excluded section. */
2433 if (ps->flags & SEC_EXCLUDE)
2434 (*flaginfo->info->callbacks->einfo)
2435 /* xgettext: c-format */
2436 (_("%X`%s' referenced in section `%pA' of %pB: "
2437 "defined in discarded section `%pA' of %pB\n"),
2438 h->root.root.string, o, input_bfd, ps, ps->owner);
2441 /* Call processor specific code to relocate the section
2443 if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
2447 flaginfo->internal_syms,
2448 flaginfo->sec_ptrs))
2451 if (bfd_link_relocatable (flaginfo->info))
2454 struct internal_reloc *irelend;
2455 struct coff_link_hash_entry **rel_hash;
2457 offset = o->output_section->vma + o->output_offset - o->vma;
2458 irel = internal_relocs;
2459 irelend = irel + o->reloc_count;
2460 rel_hash = (flaginfo->section_info[target_index].rel_hashes
2461 + o->output_section->reloc_count);
2462 for (; irel < irelend; irel++, rel_hash++)
2464 struct coff_link_hash_entry *h;
2465 bfd_boolean adjusted;
2469 /* Adjust the reloc address and symbol index. */
2470 irel->r_vaddr += offset;
2472 if (irel->r_symndx == -1)
2477 if (! (*adjust_symndx) (output_bfd, flaginfo->info,
2485 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2488 /* This is a global symbol. */
2490 irel->r_symndx = h->indx;
2493 /* This symbol is being written at the end
2494 of the file, and we do not yet know the
2495 symbol index. We save the pointer to the
2496 hash table entry in the rel_hash list.
2497 We set the indx field to -2 to indicate
2498 that this symbol must not be stripped. */
2507 indx = flaginfo->sym_indices[irel->r_symndx];
2509 irel->r_symndx = indx;
2512 struct internal_syment *is;
2514 char buf[SYMNMLEN + 1];
2516 /* This reloc is against a symbol we are
2517 stripping. This should have been handled
2518 by the 'dont_skip_symbol' code in the while
2519 loop at the top of this function. */
2520 is = flaginfo->internal_syms + irel->r_symndx;
2522 name = (_bfd_coff_internal_syment_name
2523 (input_bfd, is, buf));
2527 (*flaginfo->info->callbacks->unattached_reloc)
2528 (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
2533 o->output_section->reloc_count += o->reloc_count;
2537 /* Write out the modified section contents. */
2538 if (secdata == NULL || secdata->stab_info == NULL)
2540 file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
2541 if (! bfd_set_section_contents (output_bfd, o->output_section,
2542 contents, loc, o->size))
2547 if (! (_bfd_write_section_stabs
2548 (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
2549 o, &secdata->stab_info, contents)))
2554 if (! flaginfo->info->keep_memory
2555 && ! _bfd_coff_free_symbols (input_bfd))
2561 /* Write out a global symbol. Called via bfd_hash_traverse. */
2564 _bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
2566 struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
2567 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2569 struct internal_syment isym;
2570 bfd_size_type symesz;
2574 output_bfd = flaginfo->output_bfd;
2576 if (h->root.type == bfd_link_hash_warning)
2578 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2579 if (h->root.type == bfd_link_hash_new)
2587 && (flaginfo->info->strip == strip_all
2588 || (flaginfo->info->strip == strip_some
2589 && (bfd_hash_lookup (flaginfo->info->keep_hash,
2590 h->root.root.string, FALSE, FALSE)
2594 switch (h->root.type)
2597 case bfd_link_hash_new:
2598 case bfd_link_hash_warning:
2602 case bfd_link_hash_undefined:
2603 case bfd_link_hash_undefweak:
2604 isym.n_scnum = N_UNDEF;
2608 case bfd_link_hash_defined:
2609 case bfd_link_hash_defweak:
2613 sec = h->root.u.def.section->output_section;
2614 if (bfd_is_abs_section (sec))
2615 isym.n_scnum = N_ABS;
2617 isym.n_scnum = sec->target_index;
2618 isym.n_value = (h->root.u.def.value
2619 + h->root.u.def.section->output_offset);
2620 if (! obj_pe (flaginfo->output_bfd))
2621 isym.n_value += sec->vma;
2625 case bfd_link_hash_common:
2626 isym.n_scnum = N_UNDEF;
2627 isym.n_value = h->root.u.c.size;
2630 case bfd_link_hash_indirect:
2631 /* Just ignore these. They can't be handled anyhow. */
2635 if (strlen (h->root.root.string) <= SYMNMLEN)
2636 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2643 if (flaginfo->info->traditional_format)
2645 indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
2647 if (indx == (bfd_size_type) -1)
2649 flaginfo->failed = TRUE;
2652 isym._n._n_n._n_zeroes = 0;
2653 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2656 isym.n_sclass = h->symbol_class;
2657 isym.n_type = h->type;
2659 if (isym.n_sclass == C_NULL)
2660 isym.n_sclass = C_EXT;
2662 /* If doing task linking and this is the pass where we convert
2663 defined globals to statics, then do that conversion now. If the
2664 symbol is not being converted, just ignore it and it will be
2665 output during a later pass. */
2666 if (flaginfo->global_to_static)
2668 if (! IS_EXTERNAL (output_bfd, isym))
2671 isym.n_sclass = C_STAT;
2674 /* When a weak symbol is not overridden by a strong one,
2675 turn it into an external symbol when not building a
2676 shared or relocatable object. */
2677 if (! bfd_link_pic (flaginfo->info)
2678 && ! bfd_link_relocatable (flaginfo->info)
2679 && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
2680 isym.n_sclass = C_EXT;
2682 isym.n_numaux = h->numaux;
2684 bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
2686 symesz = bfd_coff_symesz (output_bfd);
2688 pos = obj_sym_filepos (output_bfd);
2689 pos += obj_raw_syment_count (output_bfd) * symesz;
2690 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2691 || bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
2693 flaginfo->failed = TRUE;
2697 h->indx = obj_raw_syment_count (output_bfd);
2699 ++obj_raw_syment_count (output_bfd);
2701 /* Write out any associated aux entries. Most of the aux entries
2702 will have been modified in _bfd_coff_link_input_bfd. We have to
2703 handle section aux entries here, now that we have the final
2704 relocation and line number counts. */
2705 for (i = 0; i < isym.n_numaux; i++)
2707 union internal_auxent *auxp;
2711 /* Look for a section aux entry here using the same tests that
2712 coff_swap_aux_out uses. */
2714 && (isym.n_sclass == C_STAT
2715 || isym.n_sclass == C_HIDDEN)
2716 && isym.n_type == T_NULL
2717 && (h->root.type == bfd_link_hash_defined
2718 || h->root.type == bfd_link_hash_defweak))
2722 sec = h->root.u.def.section->output_section;
2725 auxp->x_scn.x_scnlen = sec->size;
2727 /* For PE, an overflow on the final link reportedly does
2728 not matter. FIXME: Why not? */
2729 if (sec->reloc_count > 0xffff
2730 && (! obj_pe (output_bfd)
2731 || bfd_link_relocatable (flaginfo->info)))
2733 /* xgettext: c-format */
2734 (_("%pB: %pA: reloc overflow: %#x > 0xffff"),
2735 output_bfd, sec, sec->reloc_count);
2737 if (sec->lineno_count > 0xffff
2738 && (! obj_pe (output_bfd)
2739 || bfd_link_relocatable (flaginfo->info)))
2741 /* xgettext: c-format */
2742 (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"),
2743 output_bfd, sec, sec->lineno_count);
2745 auxp->x_scn.x_nreloc = sec->reloc_count;
2746 auxp->x_scn.x_nlinno = sec->lineno_count;
2747 auxp->x_scn.x_checksum = 0;
2748 auxp->x_scn.x_associated = 0;
2749 auxp->x_scn.x_comdat = 0;
2753 bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2754 isym.n_sclass, (int) i, isym.n_numaux,
2756 if (bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
2758 flaginfo->failed = TRUE;
2761 ++obj_raw_syment_count (output_bfd);
2767 /* Write out task global symbols, converting them to statics. Called
2768 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2769 the dirty work, if the symbol we are processing needs conversion. */
2772 _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2774 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2775 bfd_boolean rtnval = TRUE;
2776 bfd_boolean save_global_to_static;
2778 if (h->root.type == bfd_link_hash_warning)
2779 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2783 switch (h->root.type)
2785 case bfd_link_hash_defined:
2786 case bfd_link_hash_defweak:
2787 save_global_to_static = flaginfo->global_to_static;
2788 flaginfo->global_to_static = TRUE;
2789 rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
2790 flaginfo->global_to_static = save_global_to_static;
2799 /* Handle a link order which is supposed to generate a reloc. */
2802 _bfd_coff_reloc_link_order (bfd *output_bfd,
2803 struct coff_final_link_info *flaginfo,
2804 asection *output_section,
2805 struct bfd_link_order *link_order)
2807 reloc_howto_type *howto;
2808 struct internal_reloc *irel;
2809 struct coff_link_hash_entry **rel_hash_ptr;
2811 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2814 bfd_set_error (bfd_error_bad_value);
2818 if (link_order->u.reloc.p->addend != 0)
2822 bfd_reloc_status_type rstat;
2826 size = bfd_get_reloc_size (howto);
2827 buf = (bfd_byte *) bfd_zmalloc (size);
2828 if (buf == NULL && size != 0)
2831 rstat = _bfd_relocate_contents (howto, output_bfd,
2832 (bfd_vma) link_order->u.reloc.p->addend,\
2839 case bfd_reloc_outofrange:
2841 case bfd_reloc_overflow:
2842 (*flaginfo->info->callbacks->reloc_overflow)
2843 (flaginfo->info, NULL,
2844 (link_order->type == bfd_section_reloc_link_order
2845 ? bfd_section_name (output_bfd,
2846 link_order->u.reloc.p->u.section)
2847 : link_order->u.reloc.p->u.name),
2848 howto->name, link_order->u.reloc.p->addend,
2849 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2852 loc = link_order->offset * bfd_octets_per_byte (output_bfd);
2853 ok = bfd_set_section_contents (output_bfd, output_section, buf,
2860 /* Store the reloc information in the right place. It will get
2861 swapped and written out at the end of the final_link routine. */
2862 irel = (flaginfo->section_info[output_section->target_index].relocs
2863 + output_section->reloc_count);
2864 rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
2865 + output_section->reloc_count);
2867 memset (irel, 0, sizeof (struct internal_reloc));
2868 *rel_hash_ptr = NULL;
2870 irel->r_vaddr = output_section->vma + link_order->offset;
2872 if (link_order->type == bfd_section_reloc_link_order)
2874 /* We need to somehow locate a symbol in the right section. The
2875 symbol must either have a value of zero, or we must adjust
2876 the addend by the value of the symbol. FIXME: Write this
2877 when we need it. The old linker couldn't handle this anyhow. */
2879 *rel_hash_ptr = NULL;
2884 struct coff_link_hash_entry *h;
2886 h = ((struct coff_link_hash_entry *)
2887 bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
2888 link_order->u.reloc.p->u.name,
2889 FALSE, FALSE, TRUE));
2893 irel->r_symndx = h->indx;
2896 /* Set the index to -2 to force this symbol to get
2905 (*flaginfo->info->callbacks->unattached_reloc)
2906 (flaginfo->info, link_order->u.reloc.p->u.name,
2907 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2912 /* FIXME: Is this always right? */
2913 irel->r_type = howto->type;
2915 /* r_size is only used on the RS/6000, which needs its own linker
2916 routines anyhow. r_extern is only used for ECOFF. */
2918 /* FIXME: What is the right value for r_offset? Is zero OK? */
2919 ++output_section->reloc_count;
2924 /* A basic reloc handling routine which may be used by processors with
2928 _bfd_coff_generic_relocate_section (bfd *output_bfd,
2929 struct bfd_link_info *info,
2931 asection *input_section,
2933 struct internal_reloc *relocs,
2934 struct internal_syment *syms,
2935 asection **sections)
2937 struct internal_reloc *rel;
2938 struct internal_reloc *relend;
2941 relend = rel + input_section->reloc_count;
2942 for (; rel < relend; rel++)
2945 struct coff_link_hash_entry *h;
2946 struct internal_syment *sym;
2950 reloc_howto_type *howto;
2951 bfd_reloc_status_type rstat;
2953 symndx = rel->r_symndx;
2961 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2964 /* xgettext: c-format */
2965 (_("%pB: illegal symbol index %ld in relocs"), input_bfd, symndx);
2970 h = obj_coff_sym_hashes (input_bfd)[symndx];
2971 sym = syms + symndx;
2974 /* COFF treats common symbols in one of two ways. Either the
2975 size of the symbol is included in the section contents, or it
2976 is not. We assume that the size is not included, and force
2977 the rtype_to_howto function to adjust the addend as needed. */
2978 if (sym != NULL && sym->n_scnum != 0)
2979 addend = - sym->n_value;
2983 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2988 /* If we are doing a relocatable link, then we can just ignore
2989 a PC relative reloc that is pcrel_offset. It will already
2990 have the correct value. If this is not a relocatable link,
2991 then we should ignore the symbol value. */
2992 if (howto->pc_relative && howto->pcrel_offset)
2994 if (bfd_link_relocatable (info))
2996 if (sym != NULL && sym->n_scnum != 0)
2997 addend += sym->n_value;
3006 sec = bfd_abs_section_ptr;
3011 sec = sections[symndx];
3013 /* PR 19623: Relocations against symbols in
3014 the absolute sections should ignored. */
3015 if (bfd_is_abs_section (sec))
3018 val = (sec->output_section->vma
3019 + sec->output_offset
3021 if (! obj_pe (input_bfd))
3027 if (h->root.type == bfd_link_hash_defined
3028 || h->root.type == bfd_link_hash_defweak)
3030 /* Defined weak symbols are a GNU extension. */
3031 sec = h->root.u.def.section;
3032 val = (h->root.u.def.value
3033 + sec->output_section->vma
3034 + sec->output_offset);
3037 else if (h->root.type == bfd_link_hash_undefweak)
3039 if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
3041 /* See _Microsoft Portable Executable and Common Object
3042 File Format Specification_, section 5.5.3.
3043 Note that weak symbols without aux records are a GNU
3045 FIXME: All weak externals are treated as having
3046 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
3047 These behave as per SVR4 ABI: A library member
3048 will resolve a weak external only if a normal
3049 external causes the library member to be linked.
3050 See also linker.c: generic_link_check_archive_element. */
3051 struct coff_link_hash_entry *h2 =
3052 h->auxbfd->tdata.coff_obj_data->sym_hashes[
3053 h->aux->x_sym.x_tagndx.l];
3055 if (!h2 || h2->root.type == bfd_link_hash_undefined)
3057 sec = bfd_abs_section_ptr;
3062 sec = h2->root.u.def.section;
3063 val = h2->root.u.def.value
3064 + sec->output_section->vma + sec->output_offset;
3068 /* This is a GNU extension. */
3072 else if (! bfd_link_relocatable (info))
3073 (*info->callbacks->undefined_symbol)
3074 (info, h->root.root.string, input_bfd, input_section,
3075 rel->r_vaddr - input_section->vma, TRUE);
3078 /* If the input section defining the symbol has been discarded
3079 then zero this reloc field. */
3080 if (sec != NULL && discarded_section (sec))
3082 _bfd_clear_contents (howto, input_bfd, input_section,
3083 contents + (rel->r_vaddr - input_section->vma));
3087 if (info->base_file)
3089 /* Emit a reloc if the backend thinks it needs it. */
3090 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3092 /* Relocation to a symbol in a section which isn't
3093 absolute. We output the address here to a file.
3094 This file is then read by dlltool when generating the
3095 reloc section. Note that the base file is not
3096 portable between systems. We write out a bfd_vma here,
3097 and dlltool reads in a bfd_vma. */
3098 bfd_vma addr = (rel->r_vaddr
3099 - input_section->vma
3100 + input_section->output_offset
3101 + input_section->output_section->vma);
3102 if (coff_data (output_bfd)->pe)
3103 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3104 if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
3105 != sizeof (bfd_vma))
3107 bfd_set_error (bfd_error_system_call);
3113 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3115 rel->r_vaddr - input_section->vma,
3124 case bfd_reloc_outofrange:
3126 /* xgettext: c-format */
3127 (_("%pB: bad reloc address %#" PRIx64 " in section `%pA'"),
3128 input_bfd, (uint64_t) rel->r_vaddr, input_section);
3130 case bfd_reloc_overflow:
3133 char buf[SYMNMLEN + 1];
3141 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3146 (*info->callbacks->reloc_overflow)
3147 (info, (h ? &h->root : NULL), name, howto->name,
3148 (bfd_vma) 0, input_bfd, input_section,
3149 rel->r_vaddr - input_section->vma);