1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 /* This file contains the COFF backend linker code. */
29 #include "coff/internal.h"
31 #include "safe-ctype.h"
33 static bfd_boolean coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info);
34 static bfd_boolean coff_link_check_archive_element (bfd *abfd, struct bfd_link_info *info, bfd_boolean *pneeded);
35 static bfd_boolean coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info);
37 /* Return TRUE if SYM is a weak, external symbol. */
38 #define IS_WEAK_EXTERNAL(abfd, sym) \
39 ((sym).n_sclass == C_WEAKEXT \
40 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
42 /* Return TRUE if SYM is an external symbol. */
43 #define IS_EXTERNAL(abfd, sym) \
44 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
46 /* Define macros so that the ISFCN, et. al., macros work correctly.
47 These macros are defined in include/coff/internal.h in terms of
48 N_TMASK, etc. These definitions require a user to define local
49 variables with the appropriate names, and with values from the
50 coff_data (abfd) structure. */
52 #define N_TMASK n_tmask
53 #define N_BTSHFT n_btshft
54 #define N_BTMASK n_btmask
56 /* Create an entry in a COFF linker hash table. */
58 struct bfd_hash_entry *
59 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
60 struct bfd_hash_table *table,
63 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
65 /* Allocate the structure if it has not already been allocated by a
67 if (ret == (struct coff_link_hash_entry *) NULL)
68 ret = ((struct coff_link_hash_entry *)
69 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
70 if (ret == (struct coff_link_hash_entry *) NULL)
71 return (struct bfd_hash_entry *) ret;
73 /* Call the allocation method of the superclass. */
74 ret = ((struct coff_link_hash_entry *)
75 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
77 if (ret != (struct coff_link_hash_entry *) NULL)
79 /* Set local fields. */
82 ret->symbol_class = C_NULL;
88 return (struct bfd_hash_entry *) ret;
91 /* Initialize a COFF linker hash table. */
94 _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
96 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
97 struct bfd_hash_table *,
101 memset (&table->stab_info, 0, sizeof (table->stab_info));
102 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
105 /* Create a COFF linker hash table. */
107 struct bfd_link_hash_table *
108 _bfd_coff_link_hash_table_create (bfd *abfd)
110 struct coff_link_hash_table *ret;
111 bfd_size_type amt = sizeof (struct coff_link_hash_table);
113 ret = (struct coff_link_hash_table *) bfd_malloc (amt);
117 if (! _bfd_coff_link_hash_table_init (ret, abfd,
118 _bfd_coff_link_hash_newfunc,
119 sizeof (struct coff_link_hash_entry)))
122 return (struct bfd_link_hash_table *) NULL;
127 /* Create an entry in a COFF debug merge hash table. */
129 struct bfd_hash_entry *
130 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
131 struct bfd_hash_table *table,
134 struct coff_debug_merge_hash_entry *ret =
135 (struct coff_debug_merge_hash_entry *) entry;
137 /* Allocate the structure if it has not already been allocated by a
139 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
140 ret = ((struct coff_debug_merge_hash_entry *)
141 bfd_hash_allocate (table,
142 sizeof (struct coff_debug_merge_hash_entry)));
143 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
144 return (struct bfd_hash_entry *) ret;
146 /* Call the allocation method of the superclass. */
147 ret = ((struct coff_debug_merge_hash_entry *)
148 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
149 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
151 /* Set local fields. */
155 return (struct bfd_hash_entry *) ret;
158 /* Given a COFF BFD, add symbols to the global hash table as
162 _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
164 switch (bfd_get_format (abfd))
167 return coff_link_add_object_symbols (abfd, info);
169 return _bfd_generic_link_add_archive_symbols
170 (abfd, info, coff_link_check_archive_element);
172 bfd_set_error (bfd_error_wrong_format);
177 /* Add symbols from a COFF object file. */
180 coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
182 if (! _bfd_coff_get_external_symbols (abfd))
184 if (! coff_link_add_symbols (abfd, info))
187 if (! info->keep_memory
188 && ! _bfd_coff_free_symbols (abfd))
194 /* Look through the symbols to see if this object file should be
195 included in the link. */
198 coff_link_check_ar_symbols (bfd *abfd,
199 struct bfd_link_info *info,
200 bfd_boolean *pneeded,
203 bfd_size_type symesz;
209 symesz = bfd_coff_symesz (abfd);
210 esym = (bfd_byte *) obj_coff_external_syms (abfd);
211 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
212 while (esym < esym_end)
214 struct internal_syment sym;
215 enum coff_symbol_classification classification;
217 bfd_coff_swap_sym_in (abfd, esym, &sym);
219 classification = bfd_coff_classify_symbol (abfd, &sym);
220 if (classification == COFF_SYMBOL_GLOBAL
221 || classification == COFF_SYMBOL_COMMON)
224 char buf[SYMNMLEN + 1];
225 struct bfd_link_hash_entry *h;
227 /* This symbol is externally visible, and is defined by this
229 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
232 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
236 && info->pei386_auto_import
237 && CONST_STRNEQ (name, "__imp_"))
238 h = bfd_link_hash_lookup (info->hash, name + 6, FALSE, FALSE, TRUE);
240 /* We are only interested in symbols that are currently
241 undefined. If a symbol is currently known to be common,
242 COFF linkers do not bring in an object file which defines
244 if (h != (struct bfd_link_hash_entry *) NULL
245 && h->type == bfd_link_hash_undefined)
247 if (!(*info->callbacks
248 ->add_archive_element) (info, abfd, name, subsbfd))
255 esym += (sym.n_numaux + 1) * symesz;
258 /* We do not need this object file. */
262 /* Check a single archive element to see if we need to include it in
263 the link. *PNEEDED is set according to whether this element is
264 needed in the link or not. This is called via
265 _bfd_generic_link_add_archive_symbols. */
268 coff_link_check_archive_element (bfd *abfd,
269 struct bfd_link_info *info,
270 bfd_boolean *pneeded)
275 if (!_bfd_coff_get_external_symbols (abfd))
279 if (!coff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
285 /* Potentially, the add_archive_element hook may have set a
286 substitute BFD for us. */
289 if (!info->keep_memory
290 && !_bfd_coff_free_symbols (oldbfd))
292 if (!_bfd_coff_get_external_symbols (abfd))
295 if (!coff_link_add_symbols (abfd, info))
299 if (!info->keep_memory || !needed)
301 if (!_bfd_coff_free_symbols (abfd))
307 /* Add all the symbols from an object file to the hash table. */
310 coff_link_add_symbols (bfd *abfd,
311 struct bfd_link_info *info)
313 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
314 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
315 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
316 bfd_boolean keep_syms;
317 bfd_boolean default_copy;
318 bfd_size_type symcount;
319 struct coff_link_hash_entry **sym_hash;
320 bfd_size_type symesz;
325 symcount = obj_raw_syment_count (abfd);
328 return TRUE; /* Nothing to do. */
330 /* Keep the symbols during this function, in case the linker needs
331 to read the generic symbols in order to report an error message. */
332 keep_syms = obj_coff_keep_syms (abfd);
333 obj_coff_keep_syms (abfd) = TRUE;
335 if (info->keep_memory)
336 default_copy = FALSE;
340 /* We keep a list of the linker hash table entries that correspond
341 to particular symbols. */
342 amt = symcount * sizeof (struct coff_link_hash_entry *);
343 sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
344 if (sym_hash == NULL)
346 obj_coff_sym_hashes (abfd) = sym_hash;
348 symesz = bfd_coff_symesz (abfd);
349 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
350 esym = (bfd_byte *) obj_coff_external_syms (abfd);
351 esym_end = esym + symcount * symesz;
352 while (esym < esym_end)
354 struct internal_syment sym;
355 enum coff_symbol_classification classification;
358 bfd_coff_swap_sym_in (abfd, esym, &sym);
360 classification = bfd_coff_classify_symbol (abfd, &sym);
361 if (classification != COFF_SYMBOL_LOCAL)
364 char buf[SYMNMLEN + 1];
370 /* This symbol is externally visible. */
372 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
376 /* We must copy the name into memory if we got it from the
377 syment itself, rather than the string table. */
379 if (sym._n._n_n._n_zeroes != 0
380 || sym._n._n_n._n_offset == 0)
385 switch (classification)
390 case COFF_SYMBOL_GLOBAL:
391 flags = BSF_EXPORT | BSF_GLOBAL;
392 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
394 value -= section->vma;
397 case COFF_SYMBOL_UNDEFINED:
399 section = bfd_und_section_ptr;
402 case COFF_SYMBOL_COMMON:
404 section = bfd_com_section_ptr;
407 case COFF_SYMBOL_PE_SECTION:
408 flags = BSF_SECTION_SYM | BSF_GLOBAL;
409 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
413 if (IS_WEAK_EXTERNAL (abfd, sym))
418 /* In the PE format, section symbols actually refer to the
419 start of the output section. We handle them specially
421 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
423 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
424 name, FALSE, copy, FALSE);
425 if (*sym_hash != NULL)
427 if (((*sym_hash)->coff_link_hash_flags
428 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
429 && (*sym_hash)->root.type != bfd_link_hash_undefined
430 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
431 (*_bfd_error_handler)
432 ("Warning: symbol `%s' is both section and non-section",
439 /* The Microsoft Visual C compiler does string pooling by
440 hashing the constants to an internal symbol name, and
441 relying on the linker comdat support to discard
442 duplicate names. However, if one string is a literal and
443 one is a data initializer, one will end up in the .data
444 section and one will end up in the .rdata section. The
445 Microsoft linker will combine them into the .data
446 section, which seems to be wrong since it might cause the
449 As long as there are no external references to the
450 symbols, which there shouldn't be, we can treat the .data
451 and .rdata instances as separate symbols. The comdat
452 code in the linker will do the appropriate merging. Here
453 we avoid getting a multiple definition error for one of
454 these special symbols.
456 FIXME: I don't think this will work in the case where
457 there are two object files which use the constants as a
458 literal and two object files which use it as a data
459 initializer. One or the other of the second object files
460 is going to wind up with an inappropriate reference. */
462 && (classification == COFF_SYMBOL_GLOBAL
463 || classification == COFF_SYMBOL_PE_SECTION)
464 && coff_section_data (abfd, section) != NULL
465 && coff_section_data (abfd, section)->comdat != NULL
466 && CONST_STRNEQ (name, "??_")
467 && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
469 if (*sym_hash == NULL)
470 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
471 name, FALSE, copy, FALSE);
472 if (*sym_hash != NULL
473 && (*sym_hash)->root.type == bfd_link_hash_defined
474 && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
475 && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
476 coff_section_data (abfd, section)->comdat->name) == 0)
482 if (! (bfd_coff_link_add_one_symbol
483 (info, abfd, name, flags, section, value,
484 (const char *) NULL, copy, FALSE,
485 (struct bfd_link_hash_entry **) sym_hash)))
489 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
490 (*sym_hash)->coff_link_hash_flags |=
491 COFF_LINK_HASH_PE_SECTION_SYMBOL;
493 /* Limit the alignment of a common symbol to the possible
494 alignment of a section. There is no point to permitting
495 a higher alignment for a common symbol: we can not
496 guarantee it, and it may cause us to allocate extra space
497 in the common section. */
498 if (section == bfd_com_section_ptr
499 && (*sym_hash)->root.type == bfd_link_hash_common
500 && ((*sym_hash)->root.u.c.p->alignment_power
501 > bfd_coff_default_section_alignment_power (abfd)))
502 (*sym_hash)->root.u.c.p->alignment_power
503 = bfd_coff_default_section_alignment_power (abfd);
505 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
507 /* If we don't have any symbol information currently in
508 the hash table, or if we are looking at a symbol
509 definition, then update the symbol class and type in
511 if (((*sym_hash)->symbol_class == C_NULL
512 && (*sym_hash)->type == T_NULL)
515 && (*sym_hash)->root.type != bfd_link_hash_defined
516 && (*sym_hash)->root.type != bfd_link_hash_defweak))
518 (*sym_hash)->symbol_class = sym.n_sclass;
519 if (sym.n_type != T_NULL)
521 /* We want to warn if the type changed, but not
522 if it changed from an unspecified type.
523 Testing the whole type byte may work, but the
524 change from (e.g.) a function of unspecified
525 type to function of known type also wants to
527 if ((*sym_hash)->type != T_NULL
528 && (*sym_hash)->type != sym.n_type
529 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
530 && (BTYPE ((*sym_hash)->type) == T_NULL
531 || BTYPE (sym.n_type) == T_NULL)))
532 (*_bfd_error_handler)
533 (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
534 abfd, name, (*sym_hash)->type, sym.n_type);
536 /* We don't want to change from a meaningful
537 base type to a null one, but if we know
538 nothing, take what little we might now know. */
539 if (BTYPE (sym.n_type) != T_NULL
540 || (*sym_hash)->type == T_NULL)
541 (*sym_hash)->type = sym.n_type;
543 (*sym_hash)->auxbfd = abfd;
544 if (sym.n_numaux != 0)
546 union internal_auxent *alloc;
549 union internal_auxent *iaux;
551 (*sym_hash)->numaux = sym.n_numaux;
552 alloc = ((union internal_auxent *)
553 bfd_hash_allocate (&info->hash->table,
555 * sizeof (*alloc))));
558 for (i = 0, eaux = esym + symesz, iaux = alloc;
560 i++, eaux += symesz, iaux++)
561 bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
562 sym.n_sclass, (int) i,
564 (*sym_hash)->aux = alloc;
569 if (classification == COFF_SYMBOL_PE_SECTION
570 && (*sym_hash)->numaux != 0)
572 /* Some PE sections (such as .bss) have a zero size in
573 the section header, but a non-zero size in the AUX
574 record. Correct that here.
576 FIXME: This is not at all the right place to do this.
577 For example, it won't help objdump. This needs to be
578 done when we swap in the section header. */
579 BFD_ASSERT ((*sym_hash)->numaux == 1);
580 if (section->size == 0)
581 section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
583 /* FIXME: We could test whether the section sizes
584 matches the size in the aux entry, but apparently
585 that sometimes fails unexpectedly. */
589 esym += (sym.n_numaux + 1) * symesz;
590 sym_hash += sym.n_numaux + 1;
593 /* If this is a non-traditional, non-relocatable link, try to
594 optimize the handling of any .stab/.stabstr sections. */
595 if (! info->relocatable
596 && ! info->traditional_format
597 && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
598 && (info->strip != strip_all && info->strip != strip_debugger))
602 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
606 bfd_size_type string_offset = 0;
609 for (stab = abfd->sections; stab; stab = stab->next)
610 if (CONST_STRNEQ (stab->name, ".stab")
612 || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
614 struct coff_link_hash_table *table;
615 struct coff_section_tdata *secdata
616 = coff_section_data (abfd, stab);
620 amt = sizeof (struct coff_section_tdata);
621 stab->used_by_bfd = bfd_zalloc (abfd, amt);
622 if (stab->used_by_bfd == NULL)
624 secdata = coff_section_data (abfd, stab);
627 table = coff_hash_table (info);
629 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
638 obj_coff_keep_syms (abfd) = keep_syms;
643 obj_coff_keep_syms (abfd) = keep_syms;
647 /* Do the final link step. */
650 _bfd_coff_final_link (bfd *abfd,
651 struct bfd_link_info *info)
653 bfd_size_type symesz;
654 struct coff_final_link_info finfo;
655 bfd_boolean debug_merge_allocated;
656 bfd_boolean long_section_names;
658 struct bfd_link_order *p;
659 bfd_size_type max_sym_count;
660 bfd_size_type max_lineno_count;
661 bfd_size_type max_reloc_count;
662 bfd_size_type max_output_reloc_count;
663 bfd_size_type max_contents_size;
664 file_ptr rel_filepos;
666 file_ptr line_filepos;
669 bfd_byte *external_relocs = NULL;
670 char strbuf[STRING_SIZE_SIZE];
673 symesz = bfd_coff_symesz (abfd);
676 finfo.output_bfd = abfd;
678 finfo.section_info = NULL;
679 finfo.last_file_index = -1;
680 finfo.last_bf_index = -1;
681 finfo.internal_syms = NULL;
682 finfo.sec_ptrs = NULL;
683 finfo.sym_indices = NULL;
684 finfo.outsyms = NULL;
685 finfo.linenos = NULL;
686 finfo.contents = NULL;
687 finfo.external_relocs = NULL;
688 finfo.internal_relocs = NULL;
689 finfo.global_to_static = FALSE;
690 debug_merge_allocated = FALSE;
692 coff_data (abfd)->link_info = info;
694 finfo.strtab = _bfd_stringtab_init ();
695 if (finfo.strtab == NULL)
698 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
700 debug_merge_allocated = TRUE;
702 /* Compute the file positions for all the sections. */
703 if (! abfd->output_has_begun)
705 if (! bfd_coff_compute_section_file_positions (abfd))
709 /* Count the line numbers and relocation entries required for the
710 output file. Set the file positions for the relocs. */
711 rel_filepos = obj_relocbase (abfd);
712 relsz = bfd_coff_relsz (abfd);
713 max_contents_size = 0;
714 max_lineno_count = 0;
717 long_section_names = FALSE;
718 for (o = abfd->sections; o != NULL; o = o->next)
722 for (p = o->map_head.link_order; p != NULL; p = p->next)
724 if (p->type == bfd_indirect_link_order)
728 sec = p->u.indirect.section;
730 /* Mark all sections which are to be included in the
731 link. This will normally be every section. We need
732 to do this so that we can identify any sections which
733 the linker has decided to not include. */
734 sec->linker_mark = TRUE;
736 if (info->strip == strip_none
737 || info->strip == strip_some)
738 o->lineno_count += sec->lineno_count;
740 if (info->relocatable)
741 o->reloc_count += sec->reloc_count;
743 if (sec->rawsize > max_contents_size)
744 max_contents_size = sec->rawsize;
745 if (sec->size > max_contents_size)
746 max_contents_size = sec->size;
747 if (sec->lineno_count > max_lineno_count)
748 max_lineno_count = sec->lineno_count;
749 if (sec->reloc_count > max_reloc_count)
750 max_reloc_count = sec->reloc_count;
752 else if (info->relocatable
753 && (p->type == bfd_section_reloc_link_order
754 || p->type == bfd_symbol_reloc_link_order))
757 if (o->reloc_count == 0)
761 o->flags |= SEC_RELOC;
762 o->rel_filepos = rel_filepos;
763 rel_filepos += o->reloc_count * relsz;
764 /* In PE COFF, if there are at least 0xffff relocations an
765 extra relocation will be written out to encode the count. */
766 if (obj_pe (abfd) && o->reloc_count >= 0xffff)
767 rel_filepos += relsz;
770 if (bfd_coff_long_section_names (abfd)
771 && strlen (o->name) > SCNNMLEN)
773 /* This section has a long name which must go in the string
774 table. This must correspond to the code in
775 coff_write_object_contents which puts the string index
776 into the s_name field of the section header. That is why
777 we pass hash as FALSE. */
778 if (_bfd_stringtab_add (finfo.strtab, o->name, FALSE, FALSE)
779 == (bfd_size_type) -1)
781 long_section_names = TRUE;
785 /* If doing a relocatable link, allocate space for the pointers we
787 if (info->relocatable)
791 /* We use section_count + 1, rather than section_count, because
792 the target_index fields are 1 based. */
793 amt = abfd->section_count + 1;
794 amt *= sizeof (struct coff_link_section_info);
795 finfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
796 if (finfo.section_info == NULL)
798 for (i = 0; i <= abfd->section_count; i++)
800 finfo.section_info[i].relocs = NULL;
801 finfo.section_info[i].rel_hashes = NULL;
805 /* We now know the size of the relocs, so we can determine the file
806 positions of the line numbers. */
807 line_filepos = rel_filepos;
808 linesz = bfd_coff_linesz (abfd);
809 max_output_reloc_count = 0;
810 for (o = abfd->sections; o != NULL; o = o->next)
812 if (o->lineno_count == 0)
816 o->line_filepos = line_filepos;
817 line_filepos += o->lineno_count * linesz;
820 if (o->reloc_count != 0)
822 /* We don't know the indices of global symbols until we have
823 written out all the local symbols. For each section in
824 the output file, we keep an array of pointers to hash
825 table entries. Each entry in the array corresponds to a
826 reloc. When we find a reloc against a global symbol, we
827 set the corresponding entry in this array so that we can
828 fix up the symbol index after we have written out all the
831 Because of this problem, we also keep the relocs in
832 memory until the end of the link. This wastes memory,
833 but only when doing a relocatable link, which is not the
835 BFD_ASSERT (info->relocatable);
836 amt = o->reloc_count;
837 amt *= sizeof (struct internal_reloc);
838 finfo.section_info[o->target_index].relocs =
839 (struct internal_reloc *) bfd_malloc (amt);
840 amt = o->reloc_count;
841 amt *= sizeof (struct coff_link_hash_entry *);
842 finfo.section_info[o->target_index].rel_hashes =
843 (struct coff_link_hash_entry **) bfd_malloc (amt);
844 if (finfo.section_info[o->target_index].relocs == NULL
845 || finfo.section_info[o->target_index].rel_hashes == NULL)
848 if (o->reloc_count > max_output_reloc_count)
849 max_output_reloc_count = o->reloc_count;
852 /* Reset the reloc and lineno counts, so that we can use them to
853 count the number of entries we have output so far. */
858 obj_sym_filepos (abfd) = line_filepos;
860 /* Figure out the largest number of symbols in an input BFD. Take
861 the opportunity to clear the output_has_begun fields of all the
864 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
868 sub->output_has_begun = FALSE;
869 sz = obj_raw_syment_count (sub);
870 if (sz > max_sym_count)
874 /* Allocate some buffers used while linking. */
875 amt = max_sym_count * sizeof (struct internal_syment);
876 finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
877 amt = max_sym_count * sizeof (asection *);
878 finfo.sec_ptrs = (asection **) bfd_malloc (amt);
879 amt = max_sym_count * sizeof (long);
880 finfo.sym_indices = (long int *) bfd_malloc (amt);
881 finfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
882 amt = max_lineno_count * bfd_coff_linesz (abfd);
883 finfo.linenos = (bfd_byte *) bfd_malloc (amt);
884 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
885 amt = max_reloc_count * relsz;
886 finfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
887 if (! info->relocatable)
889 amt = max_reloc_count * sizeof (struct internal_reloc);
890 finfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
892 if ((finfo.internal_syms == NULL && max_sym_count > 0)
893 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
894 || (finfo.sym_indices == NULL && max_sym_count > 0)
895 || finfo.outsyms == NULL
896 || (finfo.linenos == NULL && max_lineno_count > 0)
897 || (finfo.contents == NULL && max_contents_size > 0)
898 || (finfo.external_relocs == NULL && max_reloc_count > 0)
899 || (! info->relocatable
900 && finfo.internal_relocs == NULL
901 && max_reloc_count > 0))
904 /* We now know the position of everything in the file, except that
905 we don't know the size of the symbol table and therefore we don't
906 know where the string table starts. We just build the string
907 table in memory as we go along. We process all the relocations
908 for a single input file at once. */
909 obj_raw_syment_count (abfd) = 0;
911 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
913 if (! bfd_coff_start_final_link (abfd, info))
917 for (o = abfd->sections; o != NULL; o = o->next)
919 for (p = o->map_head.link_order; p != NULL; p = p->next)
921 if (p->type == bfd_indirect_link_order
922 && bfd_family_coff (p->u.indirect.section->owner))
924 sub = p->u.indirect.section->owner;
925 if (! bfd_coff_link_output_has_begun (sub, & finfo))
927 if (! _bfd_coff_link_input_bfd (&finfo, sub))
929 sub->output_has_begun = TRUE;
932 else if (p->type == bfd_section_reloc_link_order
933 || p->type == bfd_symbol_reloc_link_order)
935 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
940 if (! _bfd_default_link_order (abfd, info, o, p))
946 if (! bfd_coff_final_link_postscript (abfd, & finfo))
949 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
951 coff_debug_merge_hash_table_free (&finfo.debug_merge);
952 debug_merge_allocated = FALSE;
954 if (finfo.internal_syms != NULL)
956 free (finfo.internal_syms);
957 finfo.internal_syms = NULL;
959 if (finfo.sec_ptrs != NULL)
961 free (finfo.sec_ptrs);
962 finfo.sec_ptrs = NULL;
964 if (finfo.sym_indices != NULL)
966 free (finfo.sym_indices);
967 finfo.sym_indices = NULL;
969 if (finfo.linenos != NULL)
971 free (finfo.linenos);
972 finfo.linenos = NULL;
974 if (finfo.contents != NULL)
976 free (finfo.contents);
977 finfo.contents = NULL;
979 if (finfo.external_relocs != NULL)
981 free (finfo.external_relocs);
982 finfo.external_relocs = NULL;
984 if (finfo.internal_relocs != NULL)
986 free (finfo.internal_relocs);
987 finfo.internal_relocs = NULL;
990 /* The value of the last C_FILE symbol is supposed to be the symbol
991 index of the first external symbol. Write it out again if
993 if (finfo.last_file_index != -1
994 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
998 finfo.last_file.n_value = obj_raw_syment_count (abfd);
999 bfd_coff_swap_sym_out (abfd, &finfo.last_file,
1002 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
1003 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1004 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
1008 /* If doing task linking (ld --task-link) then make a pass through the
1009 global symbols, writing out any that are defined, and making them
1011 if (info->task_link)
1013 finfo.failed = FALSE;
1014 coff_link_hash_traverse (coff_hash_table (info),
1015 _bfd_coff_write_task_globals, &finfo);
1020 /* Write out the global symbols. */
1021 finfo.failed = FALSE;
1022 coff_link_hash_traverse (coff_hash_table (info),
1023 _bfd_coff_write_global_sym, &finfo);
1027 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1028 if (finfo.outsyms != NULL)
1030 free (finfo.outsyms);
1031 finfo.outsyms = NULL;
1034 if (info->relocatable && max_output_reloc_count > 0)
1036 /* Now that we have written out all the global symbols, we know
1037 the symbol indices to use for relocs against them, and we can
1038 finally write out the relocs. */
1039 amt = max_output_reloc_count * relsz;
1040 external_relocs = (bfd_byte *) bfd_malloc (amt);
1041 if (external_relocs == NULL)
1044 for (o = abfd->sections; o != NULL; o = o->next)
1046 struct internal_reloc *irel;
1047 struct internal_reloc *irelend;
1048 struct coff_link_hash_entry **rel_hash;
1051 if (o->reloc_count == 0)
1054 irel = finfo.section_info[o->target_index].relocs;
1055 irelend = irel + o->reloc_count;
1056 rel_hash = finfo.section_info[o->target_index].rel_hashes;
1057 erel = external_relocs;
1058 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1060 if (*rel_hash != NULL)
1062 BFD_ASSERT ((*rel_hash)->indx >= 0);
1063 irel->r_symndx = (*rel_hash)->indx;
1065 bfd_coff_swap_reloc_out (abfd, irel, erel);
1068 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1070 if (obj_pe (abfd) && o->reloc_count >= 0xffff)
1072 /* In PE COFF, write the count of relocs as the first
1073 reloc. The header overflow bit will be set
1075 struct internal_reloc incount;
1076 bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
1078 memset (&incount, 0, sizeof (incount));
1079 incount.r_vaddr = o->reloc_count + 1;
1080 bfd_coff_swap_reloc_out (abfd, (PTR) &incount, (PTR) excount);
1081 if (bfd_bwrite (excount, relsz, abfd) != relsz)
1082 /* We'll leak, but it's an error anyway. */
1086 if (bfd_bwrite (external_relocs,
1087 (bfd_size_type) relsz * o->reloc_count, abfd)
1088 != (bfd_size_type) relsz * o->reloc_count)
1092 free (external_relocs);
1093 external_relocs = NULL;
1096 /* Free up the section information. */
1097 if (finfo.section_info != NULL)
1101 for (i = 0; i < abfd->section_count; i++)
1103 if (finfo.section_info[i].relocs != NULL)
1104 free (finfo.section_info[i].relocs);
1105 if (finfo.section_info[i].rel_hashes != NULL)
1106 free (finfo.section_info[i].rel_hashes);
1108 free (finfo.section_info);
1109 finfo.section_info = NULL;
1112 /* If we have optimized stabs strings, output them. */
1113 if (coff_hash_table (info)->stab_info.stabstr != NULL)
1115 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1119 /* Write out the string table. */
1120 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1124 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1125 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1128 #if STRING_SIZE_SIZE == 4
1130 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1133 #error Change H_PUT_32 above
1136 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1137 != STRING_SIZE_SIZE)
1140 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1143 obj_coff_strings_written (abfd) = TRUE;
1146 _bfd_stringtab_free (finfo.strtab);
1148 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1149 not try to write out the symbols. */
1150 bfd_get_symcount (abfd) = 0;
1155 if (debug_merge_allocated)
1156 coff_debug_merge_hash_table_free (&finfo.debug_merge);
1157 if (finfo.strtab != NULL)
1158 _bfd_stringtab_free (finfo.strtab);
1159 if (finfo.section_info != NULL)
1163 for (i = 0; i < abfd->section_count; i++)
1165 if (finfo.section_info[i].relocs != NULL)
1166 free (finfo.section_info[i].relocs);
1167 if (finfo.section_info[i].rel_hashes != NULL)
1168 free (finfo.section_info[i].rel_hashes);
1170 free (finfo.section_info);
1172 if (finfo.internal_syms != NULL)
1173 free (finfo.internal_syms);
1174 if (finfo.sec_ptrs != NULL)
1175 free (finfo.sec_ptrs);
1176 if (finfo.sym_indices != NULL)
1177 free (finfo.sym_indices);
1178 if (finfo.outsyms != NULL)
1179 free (finfo.outsyms);
1180 if (finfo.linenos != NULL)
1181 free (finfo.linenos);
1182 if (finfo.contents != NULL)
1183 free (finfo.contents);
1184 if (finfo.external_relocs != NULL)
1185 free (finfo.external_relocs);
1186 if (finfo.internal_relocs != NULL)
1187 free (finfo.internal_relocs);
1188 if (external_relocs != NULL)
1189 free (external_relocs);
1193 /* Parse out a -heap <reserved>,<commit> line. */
1196 dores_com (char *ptr, bfd *output_bfd, int heap)
1198 if (coff_data(output_bfd)->pe)
1200 int val = strtoul (ptr, &ptr, 0);
1203 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1205 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1209 val = strtoul (ptr+1, &ptr, 0);
1211 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1213 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1220 get_name (char *ptr, char **dst)
1225 while (*ptr && *ptr != ' ')
1231 /* Process any magic embedded commands in a section called .drectve. */
1234 process_embedded_commands (bfd *output_bfd,
1235 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1238 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1246 if (!bfd_malloc_and_get_section (abfd, sec, ©))
1252 e = (char *) copy + sec->size;
1254 for (s = (char *) copy; s < e ; )
1261 if (CONST_STRNEQ (s, "-attr"))
1271 s = get_name (s, &name);
1272 s = get_name (s, &attribs);
1292 asec = bfd_get_section_by_name (abfd, name);
1296 asec->flags |= SEC_CODE;
1298 asec->flags |= SEC_READONLY;
1301 else if (CONST_STRNEQ (s, "-heap"))
1302 s = dores_com (s + 5, output_bfd, 1);
1304 else if (CONST_STRNEQ (s, "-stack"))
1305 s = dores_com (s + 6, output_bfd, 0);
1307 /* GNU extension for aligned commons. */
1308 else if (CONST_STRNEQ (s, "-aligncomm:"))
1310 /* Common symbols must be aligned on reading, as it
1311 is too late to do anything here, after they have
1312 already been allocated, so just skip the directive. */
1323 /* Place a marker against all symbols which are used by relocations.
1324 This marker can be picked up by the 'do we skip this symbol ?'
1325 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1329 mark_relocs (struct coff_final_link_info *finfo, bfd *input_bfd)
1333 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1336 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1338 struct internal_reloc * internal_relocs;
1339 struct internal_reloc * irel;
1340 struct internal_reloc * irelend;
1342 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1)
1344 /* Don't mark relocs in excluded sections. */
1345 if (a->output_section == bfd_abs_section_ptr)
1348 /* Read in the relocs. */
1349 internal_relocs = _bfd_coff_read_internal_relocs
1350 (input_bfd, a, FALSE,
1351 finfo->external_relocs,
1352 finfo->info->relocatable,
1353 (finfo->info->relocatable
1354 ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1355 : finfo->internal_relocs)
1358 if (internal_relocs == NULL)
1361 irel = internal_relocs;
1362 irelend = irel + a->reloc_count;
1364 /* Place a mark in the sym_indices array (whose entries have
1365 been initialised to 0) for all of the symbols that are used
1366 in the relocation table. This will then be picked up in the
1367 skip/don't-skip pass. */
1368 for (; irel < irelend; irel++)
1369 finfo->sym_indices[ irel->r_symndx ] = -1;
1373 /* Link an input file into the linker output file. This function
1374 handles all the sections and relocations of the input file at once. */
1377 _bfd_coff_link_input_bfd (struct coff_final_link_info *finfo, bfd *input_bfd)
1379 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1380 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1381 bfd_boolean (*adjust_symndx)
1382 (bfd *, struct bfd_link_info *, bfd *, asection *,
1383 struct internal_reloc *, bfd_boolean *);
1385 const char *strings;
1386 bfd_size_type syment_base;
1387 bfd_boolean copy, hash;
1388 bfd_size_type isymesz;
1389 bfd_size_type osymesz;
1390 bfd_size_type linesz;
1393 struct internal_syment *isymp;
1396 unsigned long output_index;
1398 struct coff_link_hash_entry **sym_hash;
1401 /* Move all the symbols to the output file. */
1403 output_bfd = finfo->output_bfd;
1405 syment_base = obj_raw_syment_count (output_bfd);
1406 isymesz = bfd_coff_symesz (input_bfd);
1407 osymesz = bfd_coff_symesz (output_bfd);
1408 linesz = bfd_coff_linesz (input_bfd);
1409 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1412 if (! finfo->info->keep_memory)
1415 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1418 if (! _bfd_coff_get_external_symbols (input_bfd))
1421 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1422 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1423 isymp = finfo->internal_syms;
1424 secpp = finfo->sec_ptrs;
1425 indexp = finfo->sym_indices;
1426 output_index = syment_base;
1427 outsym = finfo->outsyms;
1429 if (coff_data (output_bfd)->pe
1430 && ! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1433 /* If we are going to perform relocations and also strip/discard some
1434 symbols then we must make sure that we do not strip/discard those
1435 symbols that are going to be involved in the relocations. */
1436 if (( finfo->info->strip != strip_none
1437 || finfo->info->discard != discard_none)
1438 && finfo->info->relocatable)
1440 /* Mark the symbol array as 'not-used'. */
1441 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1443 mark_relocs (finfo, input_bfd);
1446 while (esym < esym_end)
1448 struct internal_syment isym;
1449 enum coff_symbol_classification classification;
1452 bfd_boolean dont_skip_symbol;
1455 bfd_coff_swap_sym_in (input_bfd, esym, isymp);
1457 /* Make a copy of *isymp so that the relocate_section function
1458 always sees the original values. This is more reliable than
1459 always recomputing the symbol value even if we are stripping
1463 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1464 switch (classification)
1468 case COFF_SYMBOL_GLOBAL:
1469 case COFF_SYMBOL_PE_SECTION:
1470 case COFF_SYMBOL_LOCAL:
1471 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1473 case COFF_SYMBOL_COMMON:
1474 *secpp = bfd_com_section_ptr;
1476 case COFF_SYMBOL_UNDEFINED:
1477 *secpp = bfd_und_section_ptr;
1481 /* Extract the flag indicating if this symbol is used by a
1483 if ((finfo->info->strip != strip_none
1484 || finfo->info->discard != discard_none)
1485 && finfo->info->relocatable)
1486 dont_skip_symbol = *indexp;
1488 dont_skip_symbol = FALSE;
1494 add = 1 + isym.n_numaux;
1496 /* If we are stripping all symbols, we want to skip this one. */
1497 if (finfo->info->strip == strip_all && ! dont_skip_symbol)
1502 switch (classification)
1506 case COFF_SYMBOL_GLOBAL:
1507 case COFF_SYMBOL_COMMON:
1508 case COFF_SYMBOL_PE_SECTION:
1509 /* This is a global symbol. Global symbols come at the
1510 end of the symbol table, so skip them for now.
1511 Locally defined function symbols, however, are an
1512 exception, and are not moved to the end. */
1514 if (! ISFCN (isym.n_type))
1518 case COFF_SYMBOL_UNDEFINED:
1519 /* Undefined symbols are left for the end. */
1524 case COFF_SYMBOL_LOCAL:
1525 /* This is a local symbol. Skip it if we are discarding
1527 if (finfo->info->discard == discard_all && ! dont_skip_symbol)
1533 #ifndef COFF_WITH_PE
1534 /* Skip section symbols for sections which are not going to be
1537 && !dont_skip_symbol
1538 && isym.n_sclass == C_STAT
1539 && isym.n_type == T_NULL
1540 && isym.n_numaux > 0
1541 && ((*secpp)->output_section == bfd_abs_section_ptr
1542 || bfd_section_removed_from_list (output_bfd,
1543 (*secpp)->output_section)))
1547 /* If we stripping debugging symbols, and this is a debugging
1548 symbol, then skip it. FIXME: gas sets the section to N_ABS
1549 for some types of debugging symbols; I don't know if this is
1550 a bug or not. In any case, we handle it here. */
1552 && finfo->info->strip == strip_debugger
1553 && ! dont_skip_symbol
1554 && (isym.n_scnum == N_DEBUG
1555 || (isym.n_scnum == N_ABS
1556 && (isym.n_sclass == C_AUTO
1557 || isym.n_sclass == C_REG
1558 || isym.n_sclass == C_MOS
1559 || isym.n_sclass == C_MOE
1560 || isym.n_sclass == C_MOU
1561 || isym.n_sclass == C_ARG
1562 || isym.n_sclass == C_REGPARM
1563 || isym.n_sclass == C_FIELD
1564 || isym.n_sclass == C_EOS))))
1567 /* If some symbols are stripped based on the name, work out the
1568 name and decide whether to skip this symbol. */
1570 && (finfo->info->strip == strip_some
1571 || finfo->info->discard == discard_l))
1574 char buf[SYMNMLEN + 1];
1576 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1580 if (! dont_skip_symbol
1581 && ((finfo->info->strip == strip_some
1582 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
1585 && finfo->info->discard == discard_l
1586 && bfd_is_local_label_name (input_bfd, name))))
1590 /* If this is an enum, struct, or union tag, see if we have
1591 already output an identical type. */
1593 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1594 && (isym.n_sclass == C_ENTAG
1595 || isym.n_sclass == C_STRTAG
1596 || isym.n_sclass == C_UNTAG)
1597 && isym.n_numaux == 1)
1600 char buf[SYMNMLEN + 1];
1601 struct coff_debug_merge_hash_entry *mh;
1602 struct coff_debug_merge_type *mt;
1603 union internal_auxent aux;
1604 struct coff_debug_merge_element **epp;
1605 bfd_byte *esl, *eslend;
1606 struct internal_syment *islp;
1609 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1613 /* Ignore fake names invented by compiler; treat them all as
1615 if (*name == '~' || *name == '.' || *name == '$'
1616 || (*name == bfd_get_symbol_leading_char (input_bfd)
1617 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1620 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1625 /* Allocate memory to hold type information. If this turns
1626 out to be a duplicate, we pass this address to
1628 amt = sizeof (struct coff_debug_merge_type);
1629 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
1632 mt->type_class = isym.n_sclass;
1634 /* Pick up the aux entry, which points to the end of the tag
1636 bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
1637 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1640 /* Gather the elements. */
1641 epp = &mt->elements;
1642 mt->elements = NULL;
1644 esl = esym + 2 * isymesz;
1645 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1646 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1647 while (esl < eslend)
1649 const char *elename;
1650 char elebuf[SYMNMLEN + 1];
1653 bfd_coff_swap_sym_in (input_bfd, esl, islp);
1655 amt = sizeof (struct coff_debug_merge_element);
1656 *epp = (struct coff_debug_merge_element *)
1657 bfd_alloc (input_bfd, amt);
1661 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1663 if (elename == NULL)
1666 amt = strlen (elename) + 1;
1667 name_copy = (char *) bfd_alloc (input_bfd, amt);
1668 if (name_copy == NULL)
1670 strcpy (name_copy, elename);
1672 (*epp)->name = name_copy;
1673 (*epp)->type = islp->n_type;
1675 if (islp->n_numaux >= 1
1676 && islp->n_type != T_NULL
1677 && islp->n_sclass != C_EOS)
1679 union internal_auxent eleaux;
1682 bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
1683 islp->n_type, islp->n_sclass, 0,
1684 islp->n_numaux, &eleaux);
1685 indx = eleaux.x_sym.x_tagndx.l;
1687 /* FIXME: If this tagndx entry refers to a symbol
1688 defined later in this file, we just ignore it.
1689 Handling this correctly would be tedious, and may
1694 (bfd_byte *) obj_coff_external_syms (input_bfd))
1697 (*epp)->tagndx = finfo->sym_indices[indx];
1698 if ((*epp)->tagndx < 0)
1702 epp = &(*epp)->next;
1705 esl += (islp->n_numaux + 1) * isymesz;
1706 islp += islp->n_numaux + 1;
1709 /* See if we already have a definition which matches this
1710 type. We always output the type if it has no elements,
1712 if (mt->elements == NULL)
1713 bfd_release (input_bfd, mt);
1716 struct coff_debug_merge_type *mtl;
1718 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1720 struct coff_debug_merge_element *me, *mel;
1722 if (mtl->type_class != mt->type_class)
1725 for (me = mt->elements, mel = mtl->elements;
1726 me != NULL && mel != NULL;
1727 me = me->next, mel = mel->next)
1729 if (strcmp (me->name, mel->name) != 0
1730 || me->type != mel->type
1731 || me->tagndx != mel->tagndx)
1735 if (me == NULL && mel == NULL)
1739 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1741 /* This is the first definition of this type. */
1742 mt->indx = output_index;
1743 mt->next = mh->types;
1748 /* This is a redefinition which can be merged. */
1749 bfd_release (input_bfd, mt);
1750 *indexp = mtl->indx;
1751 add = (eslend - esym) / isymesz;
1757 /* We now know whether we are to skip this symbol or not. */
1760 /* Adjust the symbol in order to output it. */
1762 if (isym._n._n_n._n_zeroes == 0
1763 && isym._n._n_n._n_offset != 0)
1768 /* This symbol has a long name. Enter it in the string
1769 table we are building. Note that we do not check
1770 bfd_coff_symname_in_debug. That is only true for
1771 XCOFF, and XCOFF requires different linking code
1773 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
1776 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1777 if (indx == (bfd_size_type) -1)
1779 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1782 switch (isym.n_sclass)
1798 /* The symbol value should not be modified. */
1802 if (obj_pe (input_bfd)
1803 && strcmp (isym.n_name, ".bf") != 0
1804 && isym.n_scnum > 0)
1806 /* For PE, .lf and .ef get their value left alone,
1807 while .bf gets relocated. However, they all have
1808 "real" section numbers, and need to be moved into
1810 isym.n_scnum = (*secpp)->output_section->target_index;
1815 case C_LABEL: /* Not completely sure about these 2 */
1824 /* Compute new symbol location. */
1825 if (isym.n_scnum > 0)
1827 isym.n_scnum = (*secpp)->output_section->target_index;
1828 isym.n_value += (*secpp)->output_offset;
1829 if (! obj_pe (input_bfd))
1830 isym.n_value -= (*secpp)->vma;
1831 if (! obj_pe (finfo->output_bfd))
1832 isym.n_value += (*secpp)->output_section->vma;
1837 /* The value of a C_FILE symbol is the symbol index of
1838 the next C_FILE symbol. The value of the last C_FILE
1839 symbol is the symbol index to the first external
1840 symbol (actually, coff_renumber_symbols does not get
1841 this right--it just sets the value of the last C_FILE
1842 symbol to zero--and nobody has ever complained about
1843 it). We try to get this right, below, just before we
1844 write the symbols out, but in the general case we may
1845 have to write the symbol out twice. */
1846 if (finfo->last_file_index != -1
1847 && finfo->last_file.n_value != (bfd_vma) output_index)
1849 /* We must correct the value of the last C_FILE
1851 finfo->last_file.n_value = output_index;
1852 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1854 /* The last C_FILE symbol is in this input file. */
1855 bfd_coff_swap_sym_out (output_bfd,
1858 + ((finfo->last_file_index
1866 /* We have already written out the last C_FILE
1867 symbol. We need to write it out again. We
1868 borrow *outsym temporarily. */
1869 bfd_coff_swap_sym_out (output_bfd,
1870 &finfo->last_file, outsym);
1871 pos = obj_sym_filepos (output_bfd);
1872 pos += finfo->last_file_index * osymesz;
1873 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1874 || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
1879 finfo->last_file_index = output_index;
1880 finfo->last_file = isym;
1884 /* If doing task linking, convert normal global function symbols to
1885 static functions. */
1886 if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1887 isym.n_sclass = C_STAT;
1889 /* Output the symbol. */
1890 bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
1892 *indexp = output_index;
1897 struct coff_link_hash_entry *h;
1899 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1901 h = obj_coff_sym_hashes (input_bfd)[indx];
1904 /* This can happen if there were errors earlier in
1906 bfd_set_error (bfd_error_bad_value);
1909 h->indx = output_index;
1912 output_index += add;
1913 outsym += add * osymesz;
1916 esym += add * isymesz;
1920 for (--add; add > 0; --add)
1927 /* Fix up the aux entries. This must be done in a separate pass,
1928 because we don't know the correct symbol indices until we have
1929 already decided which symbols we are going to keep. */
1930 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1931 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1932 isymp = finfo->internal_syms;
1933 indexp = finfo->sym_indices;
1934 sym_hash = obj_coff_sym_hashes (input_bfd);
1935 outsym = finfo->outsyms;
1937 while (esym < esym_end)
1941 add = 1 + isymp->n_numaux;
1944 || (bfd_size_type) *indexp < syment_base)
1945 && (*sym_hash == NULL
1946 || (*sym_hash)->auxbfd != input_bfd))
1947 esym += add * isymesz;
1950 struct coff_link_hash_entry *h;
1958 /* The m68k-motorola-sysv assembler will sometimes
1959 generate two symbols with the same name, but only one
1960 will have aux entries. */
1961 BFD_ASSERT (isymp->n_numaux == 0
1963 || h->numaux == isymp->n_numaux);
1971 /* Handle the aux entries. This handling is based on
1972 coff_pointerize_aux. I don't know if it always correct. */
1973 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1975 union internal_auxent aux;
1976 union internal_auxent *auxp;
1978 if (h != NULL && h->aux != NULL && (h->numaux > i))
1982 bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
1983 isymp->n_sclass, i, isymp->n_numaux, &aux);
1987 if (isymp->n_sclass == C_FILE)
1989 /* If this is a long filename, we must put it in the
1991 if (auxp->x_file.x_n.x_zeroes == 0
1992 && auxp->x_file.x_n.x_offset != 0)
1994 const char *filename;
1997 BFD_ASSERT (auxp->x_file.x_n.x_offset
1998 >= STRING_SIZE_SIZE);
1999 if (strings == NULL)
2001 strings = _bfd_coff_read_string_table (input_bfd);
2002 if (strings == NULL)
2005 filename = strings + auxp->x_file.x_n.x_offset;
2006 indx = _bfd_stringtab_add (finfo->strtab, filename,
2008 if (indx == (bfd_size_type) -1)
2010 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
2013 else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2014 && isymp->n_sclass != C_NT_WEAK)
2018 if (ISFCN (isymp->n_type)
2019 || ISTAG (isymp->n_sclass)
2020 || isymp->n_sclass == C_BLOCK
2021 || isymp->n_sclass == C_FCN)
2023 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
2025 && indx < obj_raw_syment_count (input_bfd))
2027 /* We look forward through the symbol for
2028 the index of the next symbol we are going
2029 to include. I don't know if this is
2031 while ((finfo->sym_indices[indx] < 0
2032 || ((bfd_size_type) finfo->sym_indices[indx]
2034 && indx < obj_raw_syment_count (input_bfd))
2036 if (indx >= obj_raw_syment_count (input_bfd))
2037 indx = output_index;
2039 indx = finfo->sym_indices[indx];
2040 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2044 indx = auxp->x_sym.x_tagndx.l;
2045 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2049 symindx = finfo->sym_indices[indx];
2051 auxp->x_sym.x_tagndx.l = 0;
2053 auxp->x_sym.x_tagndx.l = symindx;
2056 /* The .bf symbols are supposed to be linked through
2057 the endndx field. We need to carry this list
2058 across object files. */
2061 && isymp->n_sclass == C_FCN
2062 && (isymp->_n._n_n._n_zeroes != 0
2063 || isymp->_n._n_n._n_offset == 0)
2064 && isymp->_n._n_name[0] == '.'
2065 && isymp->_n._n_name[1] == 'b'
2066 && isymp->_n._n_name[2] == 'f'
2067 && isymp->_n._n_name[3] == '\0')
2069 if (finfo->last_bf_index != -1)
2071 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2074 if ((bfd_size_type) finfo->last_bf_index
2079 /* The last .bf symbol is in this input
2080 file. This will only happen if the
2081 assembler did not set up the .bf
2082 endndx symbols correctly. */
2083 auxout = (finfo->outsyms
2084 + ((finfo->last_bf_index
2088 bfd_coff_swap_aux_out (output_bfd,
2099 /* We have already written out the last
2100 .bf aux entry. We need to write it
2101 out again. We borrow *outsym
2102 temporarily. FIXME: This case should
2104 bfd_coff_swap_aux_out (output_bfd,
2110 pos = obj_sym_filepos (output_bfd);
2111 pos += finfo->last_bf_index * osymesz;
2112 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2113 || (bfd_bwrite (outsym, osymesz, output_bfd)
2119 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2120 finfo->last_bf_index = -1;
2123 /* The endndx field of this aux entry must
2124 be updated with the symbol number of the
2126 finfo->last_bf = *auxp;
2127 finfo->last_bf_index = (((outsym - finfo->outsyms)
2136 bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2137 isymp->n_sclass, i, isymp->n_numaux,
2151 /* Relocate the line numbers, unless we are stripping them. */
2152 if (finfo->info->strip == strip_none
2153 || finfo->info->strip == strip_some)
2155 for (o = input_bfd->sections; o != NULL; o = o->next)
2161 bfd_boolean skipping;
2165 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2166 build_link_order in ldwrite.c will not have created a
2167 link order, which means that we will not have seen this
2168 input section in _bfd_coff_final_link, which means that
2169 we will not have allocated space for the line numbers of
2170 this section. I don't think line numbers can be
2171 meaningful for a section which does not have
2172 SEC_HAS_CONTENTS set, but, if they do, this must be
2174 if (o->lineno_count == 0
2175 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2178 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2179 || bfd_bread (finfo->linenos, linesz * o->lineno_count,
2180 input_bfd) != linesz * o->lineno_count)
2183 offset = o->output_section->vma + o->output_offset - o->vma;
2184 eline = finfo->linenos;
2185 oeline = finfo->linenos;
2186 elineend = eline + linesz * o->lineno_count;
2188 for (; eline < elineend; eline += linesz)
2190 struct internal_lineno iline;
2192 bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2194 if (iline.l_lnno != 0)
2195 iline.l_addr.l_paddr += offset;
2196 else if (iline.l_addr.l_symndx >= 0
2197 && ((unsigned long) iline.l_addr.l_symndx
2198 < obj_raw_syment_count (input_bfd)))
2202 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2206 /* These line numbers are attached to a symbol
2207 which we are stripping. We must discard the
2208 line numbers because reading them back with
2209 no associated symbol (or associating them all
2210 with symbol #0) will fail. We can't regain
2211 the space in the output file, but at least
2217 struct internal_syment is;
2218 union internal_auxent ia;
2220 /* Fix up the lnnoptr field in the aux entry of
2221 the symbol. It turns out that we can't do
2222 this when we modify the symbol aux entries,
2223 because gas sometimes screws up the lnnoptr
2224 field and makes it an offset from the start
2225 of the line numbers rather than an absolute
2227 bfd_coff_swap_sym_in (output_bfd,
2229 + ((indx - syment_base)
2231 if ((ISFCN (is.n_type)
2232 || is.n_sclass == C_BLOCK)
2233 && is.n_numaux >= 1)
2237 auxptr = (finfo->outsyms
2238 + ((indx - syment_base + 1)
2240 bfd_coff_swap_aux_in (output_bfd, auxptr,
2241 is.n_type, is.n_sclass,
2242 0, is.n_numaux, &ia);
2243 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2244 (o->output_section->line_filepos
2245 + o->output_section->lineno_count * linesz
2246 + eline - finfo->linenos);
2247 bfd_coff_swap_aux_out (output_bfd, &ia,
2248 is.n_type, is.n_sclass, 0,
2249 is.n_numaux, auxptr);
2255 iline.l_addr.l_symndx = indx;
2260 bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2265 pos = o->output_section->line_filepos;
2266 pos += o->output_section->lineno_count * linesz;
2267 amt = oeline - finfo->linenos;
2268 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2269 || bfd_bwrite (finfo->linenos, amt, output_bfd) != amt)
2272 o->output_section->lineno_count += amt / linesz;
2276 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2277 symbol will be the first symbol in the next input file. In the
2278 normal case, this will save us from writing out the C_FILE symbol
2280 if (finfo->last_file_index != -1
2281 && (bfd_size_type) finfo->last_file_index >= syment_base)
2283 finfo->last_file.n_value = output_index;
2284 bfd_coff_swap_sym_out (output_bfd, &finfo->last_file,
2286 + ((finfo->last_file_index - syment_base)
2290 /* Write the modified symbols to the output file. */
2291 if (outsym > finfo->outsyms)
2296 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2297 amt = outsym - finfo->outsyms;
2298 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2299 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
2302 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2303 + (outsym - finfo->outsyms) / osymesz)
2306 obj_raw_syment_count (output_bfd) = output_index;
2309 /* Relocate the contents of each section. */
2310 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2311 for (o = input_bfd->sections; o != NULL; o = o->next)
2314 struct coff_section_tdata *secdata;
2316 if (! o->linker_mark)
2317 /* This section was omitted from the link. */
2320 if ((o->flags & SEC_LINKER_CREATED) != 0)
2323 if ((o->flags & SEC_HAS_CONTENTS) == 0
2324 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2326 if ((o->flags & SEC_RELOC) != 0
2327 && o->reloc_count != 0)
2329 (*_bfd_error_handler)
2330 (_("%B: relocs in section `%A', but it has no contents"),
2332 bfd_set_error (bfd_error_no_contents);
2339 secdata = coff_section_data (input_bfd, o);
2340 if (secdata != NULL && secdata->contents != NULL)
2341 contents = secdata->contents;
2344 bfd_size_type x = o->rawsize ? o->rawsize : o->size;
2345 if (! bfd_get_section_contents (input_bfd, o, finfo->contents, 0, x))
2347 contents = finfo->contents;
2350 if ((o->flags & SEC_RELOC) != 0)
2353 struct internal_reloc *internal_relocs;
2354 struct internal_reloc *irel;
2356 /* Read in the relocs. */
2357 target_index = o->output_section->target_index;
2358 internal_relocs = (_bfd_coff_read_internal_relocs
2359 (input_bfd, o, FALSE, finfo->external_relocs,
2360 finfo->info->relocatable,
2361 (finfo->info->relocatable
2362 ? (finfo->section_info[target_index].relocs
2363 + o->output_section->reloc_count)
2364 : finfo->internal_relocs)));
2365 if (internal_relocs == NULL)
2368 /* Call processor specific code to relocate the section
2370 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2374 finfo->internal_syms,
2378 if (finfo->info->relocatable)
2381 struct internal_reloc *irelend;
2382 struct coff_link_hash_entry **rel_hash;
2384 offset = o->output_section->vma + o->output_offset - o->vma;
2385 irel = internal_relocs;
2386 irelend = irel + o->reloc_count;
2387 rel_hash = (finfo->section_info[target_index].rel_hashes
2388 + o->output_section->reloc_count);
2389 for (; irel < irelend; irel++, rel_hash++)
2391 struct coff_link_hash_entry *h;
2392 bfd_boolean adjusted;
2396 /* Adjust the reloc address and symbol index. */
2397 irel->r_vaddr += offset;
2399 if (irel->r_symndx == -1)
2404 if (! (*adjust_symndx) (output_bfd, finfo->info,
2412 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2415 /* This is a global symbol. */
2417 irel->r_symndx = h->indx;
2420 /* This symbol is being written at the end
2421 of the file, and we do not yet know the
2422 symbol index. We save the pointer to the
2423 hash table entry in the rel_hash list.
2424 We set the indx field to -2 to indicate
2425 that this symbol must not be stripped. */
2434 indx = finfo->sym_indices[irel->r_symndx];
2436 irel->r_symndx = indx;
2439 struct internal_syment *is;
2441 char buf[SYMNMLEN + 1];
2443 /* This reloc is against a symbol we are
2444 stripping. This should have been handled
2445 by the 'dont_skip_symbol' code in the while
2446 loop at the top of this function. */
2447 is = finfo->internal_syms + irel->r_symndx;
2449 name = (_bfd_coff_internal_syment_name
2450 (input_bfd, is, buf));
2454 if (! ((*finfo->info->callbacks->unattached_reloc)
2455 (finfo->info, name, input_bfd, o,
2462 o->output_section->reloc_count += o->reloc_count;
2466 /* Write out the modified section contents. */
2467 if (secdata == NULL || secdata->stab_info == NULL)
2469 file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
2470 if (! bfd_set_section_contents (output_bfd, o->output_section,
2471 contents, loc, o->size))
2476 if (! (_bfd_write_section_stabs
2477 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2478 o, &secdata->stab_info, contents)))
2483 if (! finfo->info->keep_memory
2484 && ! _bfd_coff_free_symbols (input_bfd))
2490 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2493 _bfd_coff_write_global_sym (struct coff_link_hash_entry *h, void *data)
2495 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2497 struct internal_syment isym;
2498 bfd_size_type symesz;
2502 output_bfd = finfo->output_bfd;
2504 if (h->root.type == bfd_link_hash_warning)
2506 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2507 if (h->root.type == bfd_link_hash_new)
2515 && (finfo->info->strip == strip_all
2516 || (finfo->info->strip == strip_some
2517 && (bfd_hash_lookup (finfo->info->keep_hash,
2518 h->root.root.string, FALSE, FALSE)
2522 switch (h->root.type)
2525 case bfd_link_hash_new:
2526 case bfd_link_hash_warning:
2530 case bfd_link_hash_undefined:
2531 case bfd_link_hash_undefweak:
2532 isym.n_scnum = N_UNDEF;
2536 case bfd_link_hash_defined:
2537 case bfd_link_hash_defweak:
2541 sec = h->root.u.def.section->output_section;
2542 if (bfd_is_abs_section (sec))
2543 isym.n_scnum = N_ABS;
2545 isym.n_scnum = sec->target_index;
2546 isym.n_value = (h->root.u.def.value
2547 + h->root.u.def.section->output_offset);
2548 if (! obj_pe (finfo->output_bfd))
2549 isym.n_value += sec->vma;
2553 case bfd_link_hash_common:
2554 isym.n_scnum = N_UNDEF;
2555 isym.n_value = h->root.u.c.size;
2558 case bfd_link_hash_indirect:
2559 /* Just ignore these. They can't be handled anyhow. */
2563 if (strlen (h->root.root.string) <= SYMNMLEN)
2564 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2571 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2573 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2575 if (indx == (bfd_size_type) -1)
2577 finfo->failed = TRUE;
2580 isym._n._n_n._n_zeroes = 0;
2581 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2584 isym.n_sclass = h->symbol_class;
2585 isym.n_type = h->type;
2587 if (isym.n_sclass == C_NULL)
2588 isym.n_sclass = C_EXT;
2590 /* If doing task linking and this is the pass where we convert
2591 defined globals to statics, then do that conversion now. If the
2592 symbol is not being converted, just ignore it and it will be
2593 output during a later pass. */
2594 if (finfo->global_to_static)
2596 if (! IS_EXTERNAL (output_bfd, isym))
2599 isym.n_sclass = C_STAT;
2602 /* When a weak symbol is not overridden by a strong one,
2603 turn it into an external symbol when not building a
2604 shared or relocatable object. */
2605 if (! finfo->info->shared
2606 && ! finfo->info->relocatable
2607 && IS_WEAK_EXTERNAL (finfo->output_bfd, isym))
2608 isym.n_sclass = C_EXT;
2610 isym.n_numaux = h->numaux;
2612 bfd_coff_swap_sym_out (output_bfd, &isym, finfo->outsyms);
2614 symesz = bfd_coff_symesz (output_bfd);
2616 pos = obj_sym_filepos (output_bfd);
2617 pos += obj_raw_syment_count (output_bfd) * symesz;
2618 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2619 || bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2621 finfo->failed = TRUE;
2625 h->indx = obj_raw_syment_count (output_bfd);
2627 ++obj_raw_syment_count (output_bfd);
2629 /* Write out any associated aux entries. Most of the aux entries
2630 will have been modified in _bfd_coff_link_input_bfd. We have to
2631 handle section aux entries here, now that we have the final
2632 relocation and line number counts. */
2633 for (i = 0; i < isym.n_numaux; i++)
2635 union internal_auxent *auxp;
2639 /* Look for a section aux entry here using the same tests that
2640 coff_swap_aux_out uses. */
2642 && (isym.n_sclass == C_STAT
2643 || isym.n_sclass == C_HIDDEN)
2644 && isym.n_type == T_NULL
2645 && (h->root.type == bfd_link_hash_defined
2646 || h->root.type == bfd_link_hash_defweak))
2650 sec = h->root.u.def.section->output_section;
2653 auxp->x_scn.x_scnlen = sec->size;
2655 /* For PE, an overflow on the final link reportedly does
2656 not matter. FIXME: Why not? */
2657 if (sec->reloc_count > 0xffff
2658 && (! obj_pe (output_bfd)
2659 || finfo->info->relocatable))
2660 (*_bfd_error_handler)
2661 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2662 bfd_get_filename (output_bfd),
2663 bfd_get_section_name (output_bfd, sec),
2666 if (sec->lineno_count > 0xffff
2667 && (! obj_pe (output_bfd)
2668 || finfo->info->relocatable))
2669 (*_bfd_error_handler)
2670 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2671 bfd_get_filename (output_bfd),
2672 bfd_get_section_name (output_bfd, sec),
2675 auxp->x_scn.x_nreloc = sec->reloc_count;
2676 auxp->x_scn.x_nlinno = sec->lineno_count;
2677 auxp->x_scn.x_checksum = 0;
2678 auxp->x_scn.x_associated = 0;
2679 auxp->x_scn.x_comdat = 0;
2683 bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2684 isym.n_sclass, (int) i, isym.n_numaux,
2686 if (bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2688 finfo->failed = TRUE;
2691 ++obj_raw_syment_count (output_bfd);
2697 /* Write out task global symbols, converting them to statics. Called
2698 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2699 the dirty work, if the symbol we are processing needs conversion. */
2702 _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2704 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2705 bfd_boolean rtnval = TRUE;
2706 bfd_boolean save_global_to_static;
2708 if (h->root.type == bfd_link_hash_warning)
2709 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2713 switch (h->root.type)
2715 case bfd_link_hash_defined:
2716 case bfd_link_hash_defweak:
2717 save_global_to_static = finfo->global_to_static;
2718 finfo->global_to_static = TRUE;
2719 rtnval = _bfd_coff_write_global_sym (h, data);
2720 finfo->global_to_static = save_global_to_static;
2729 /* Handle a link order which is supposed to generate a reloc. */
2732 _bfd_coff_reloc_link_order (bfd *output_bfd,
2733 struct coff_final_link_info *finfo,
2734 asection *output_section,
2735 struct bfd_link_order *link_order)
2737 reloc_howto_type *howto;
2738 struct internal_reloc *irel;
2739 struct coff_link_hash_entry **rel_hash_ptr;
2741 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2744 bfd_set_error (bfd_error_bad_value);
2748 if (link_order->u.reloc.p->addend != 0)
2752 bfd_reloc_status_type rstat;
2756 size = bfd_get_reloc_size (howto);
2757 buf = (bfd_byte *) bfd_zmalloc (size);
2761 rstat = _bfd_relocate_contents (howto, output_bfd,
2762 (bfd_vma) link_order->u.reloc.p->addend,\
2769 case bfd_reloc_outofrange:
2771 case bfd_reloc_overflow:
2772 if (! ((*finfo->info->callbacks->reloc_overflow)
2774 (link_order->type == bfd_section_reloc_link_order
2775 ? bfd_section_name (output_bfd,
2776 link_order->u.reloc.p->u.section)
2777 : link_order->u.reloc.p->u.name),
2778 howto->name, link_order->u.reloc.p->addend,
2779 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2786 loc = link_order->offset * bfd_octets_per_byte (output_bfd);
2787 ok = bfd_set_section_contents (output_bfd, output_section, buf,
2794 /* Store the reloc information in the right place. It will get
2795 swapped and written out at the end of the final_link routine. */
2796 irel = (finfo->section_info[output_section->target_index].relocs
2797 + output_section->reloc_count);
2798 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2799 + output_section->reloc_count);
2801 memset (irel, 0, sizeof (struct internal_reloc));
2802 *rel_hash_ptr = NULL;
2804 irel->r_vaddr = output_section->vma + link_order->offset;
2806 if (link_order->type == bfd_section_reloc_link_order)
2808 /* We need to somehow locate a symbol in the right section. The
2809 symbol must either have a value of zero, or we must adjust
2810 the addend by the value of the symbol. FIXME: Write this
2811 when we need it. The old linker couldn't handle this anyhow. */
2813 *rel_hash_ptr = NULL;
2818 struct coff_link_hash_entry *h;
2820 h = ((struct coff_link_hash_entry *)
2821 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2822 link_order->u.reloc.p->u.name,
2823 FALSE, FALSE, TRUE));
2827 irel->r_symndx = h->indx;
2830 /* Set the index to -2 to force this symbol to get
2839 if (! ((*finfo->info->callbacks->unattached_reloc)
2840 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2841 (asection *) NULL, (bfd_vma) 0)))
2847 /* FIXME: Is this always right? */
2848 irel->r_type = howto->type;
2850 /* r_size is only used on the RS/6000, which needs its own linker
2851 routines anyhow. r_extern is only used for ECOFF. */
2853 /* FIXME: What is the right value for r_offset? Is zero OK? */
2854 ++output_section->reloc_count;
2859 /* A basic reloc handling routine which may be used by processors with
2863 _bfd_coff_generic_relocate_section (bfd *output_bfd,
2864 struct bfd_link_info *info,
2866 asection *input_section,
2868 struct internal_reloc *relocs,
2869 struct internal_syment *syms,
2870 asection **sections)
2872 struct internal_reloc *rel;
2873 struct internal_reloc *relend;
2876 relend = rel + input_section->reloc_count;
2877 for (; rel < relend; rel++)
2880 struct coff_link_hash_entry *h;
2881 struct internal_syment *sym;
2884 reloc_howto_type *howto;
2885 bfd_reloc_status_type rstat;
2887 symndx = rel->r_symndx;
2895 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2897 (*_bfd_error_handler)
2898 ("%B: illegal symbol index %ld in relocs", input_bfd, symndx);
2903 h = obj_coff_sym_hashes (input_bfd)[symndx];
2904 sym = syms + symndx;
2907 /* COFF treats common symbols in one of two ways. Either the
2908 size of the symbol is included in the section contents, or it
2909 is not. We assume that the size is not included, and force
2910 the rtype_to_howto function to adjust the addend as needed. */
2911 if (sym != NULL && sym->n_scnum != 0)
2912 addend = - sym->n_value;
2916 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2921 /* If we are doing a relocatable link, then we can just ignore
2922 a PC relative reloc that is pcrel_offset. It will already
2923 have the correct value. If this is not a relocatable link,
2924 then we should ignore the symbol value. */
2925 if (howto->pc_relative && howto->pcrel_offset)
2927 if (info->relocatable)
2929 if (sym != NULL && sym->n_scnum != 0)
2930 addend += sym->n_value;
2941 sec = bfd_abs_section_ptr;
2946 sec = sections[symndx];
2947 val = (sec->output_section->vma
2948 + sec->output_offset
2950 if (! obj_pe (input_bfd))
2956 if (h->root.type == bfd_link_hash_defined
2957 || h->root.type == bfd_link_hash_defweak)
2959 /* Defined weak symbols are a GNU extension. */
2962 sec = h->root.u.def.section;
2963 val = (h->root.u.def.value
2964 + sec->output_section->vma
2965 + sec->output_offset);
2968 else if (h->root.type == bfd_link_hash_undefweak)
2970 if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
2972 /* See _Microsoft Portable Executable and Common Object
2973 File Format Specification_, section 5.5.3.
2974 Note that weak symbols without aux records are a GNU
2976 FIXME: All weak externals are treated as having
2977 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
2978 These behave as per SVR4 ABI: A library member
2979 will resolve a weak external only if a normal
2980 external causes the library member to be linked.
2981 See also linker.c: generic_link_check_archive_element. */
2983 struct coff_link_hash_entry *h2 =
2984 h->auxbfd->tdata.coff_obj_data->sym_hashes[
2985 h->aux->x_sym.x_tagndx.l];
2987 if (!h2 || h2->root.type == bfd_link_hash_undefined)
2989 sec = bfd_abs_section_ptr;
2994 sec = h2->root.u.def.section;
2995 val = h2->root.u.def.value
2996 + sec->output_section->vma + sec->output_offset;
3000 /* This is a GNU extension. */
3004 else if (! info->relocatable)
3006 if (! ((*info->callbacks->undefined_symbol)
3007 (info, h->root.root.string, input_bfd, input_section,
3008 rel->r_vaddr - input_section->vma, TRUE)))
3013 if (info->base_file)
3015 /* Emit a reloc if the backend thinks it needs it. */
3016 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3018 /* Relocation to a symbol in a section which isn't
3019 absolute. We output the address here to a file.
3020 This file is then read by dlltool when generating the
3021 reloc section. Note that the base file is not
3022 portable between systems. We write out a bfd_vma here,
3023 and dlltool reads in a bfd_vma. */
3024 bfd_vma addr = (rel->r_vaddr
3025 - input_section->vma
3026 + input_section->output_offset
3027 + input_section->output_section->vma);
3028 if (coff_data (output_bfd)->pe)
3029 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3030 if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
3031 != sizeof (bfd_vma))
3033 bfd_set_error (bfd_error_system_call);
3039 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3041 rel->r_vaddr - input_section->vma,
3050 case bfd_reloc_outofrange:
3051 (*_bfd_error_handler)
3052 (_("%B: bad reloc address 0x%lx in section `%A'"),
3053 input_bfd, input_section, (unsigned long) rel->r_vaddr);
3055 case bfd_reloc_overflow:
3058 char buf[SYMNMLEN + 1];
3066 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3071 if (! ((*info->callbacks->reloc_overflow)
3072 (info, (h ? &h->root : NULL), name, howto->name,
3073 (bfd_vma) 0, input_bfd, input_section,
3074 rel->r_vaddr - input_section->vma)))