1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file contains the COFF backend linker code. */
28 #include "coff/internal.h"
31 static boolean coff_link_add_object_symbols
32 PARAMS ((bfd *, struct bfd_link_info *));
33 static boolean coff_link_check_archive_element
34 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
35 static boolean coff_link_check_ar_symbols
36 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
37 static boolean coff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
38 static char *dores_com PARAMS ((char *, bfd *, int));
39 static char *get_name PARAMS ((char *, char **));
40 static int process_embedded_commands
41 PARAMS ((bfd *, struct bfd_link_info *, bfd *));
42 static void mark_relocs PARAMS ((struct coff_final_link_info *, bfd *));
44 /* Return true if SYM is a weak, external symbol. */
45 #define IS_WEAK_EXTERNAL(abfd, sym) \
46 ((sym).n_sclass == C_WEAKEXT \
47 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
49 /* Return true if SYM is an external symbol. */
50 #define IS_EXTERNAL(abfd, sym) \
51 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
53 /* Define macros so that the ISFCN, et. al., macros work correctly.
54 These macros are defined in include/coff/internal.h in terms of
55 N_TMASK, etc. These definitions require a user to define local
56 variables with the appropriate names, and with values from the
57 coff_data (abfd) structure. */
59 #define N_TMASK n_tmask
60 #define N_BTSHFT n_btshft
61 #define N_BTMASK n_btmask
63 /* Create an entry in a COFF linker hash table. */
65 struct bfd_hash_entry *
66 _bfd_coff_link_hash_newfunc (entry, table, string)
67 struct bfd_hash_entry *entry;
68 struct bfd_hash_table *table;
71 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
73 /* Allocate the structure if it has not already been allocated by a
75 if (ret == (struct coff_link_hash_entry *) NULL)
76 ret = ((struct coff_link_hash_entry *)
77 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
78 if (ret == (struct coff_link_hash_entry *) NULL)
79 return (struct bfd_hash_entry *) ret;
81 /* Call the allocation method of the superclass. */
82 ret = ((struct coff_link_hash_entry *)
83 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
85 if (ret != (struct coff_link_hash_entry *) NULL)
87 /* Set local fields. */
96 return (struct bfd_hash_entry *) ret;
99 /* Initialize a COFF linker hash table. */
102 _bfd_coff_link_hash_table_init (table, abfd, newfunc)
103 struct coff_link_hash_table *table;
105 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
106 struct bfd_hash_table *,
109 table->stab_info = NULL;
110 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
113 /* Create a COFF linker hash table. */
115 struct bfd_link_hash_table *
116 _bfd_coff_link_hash_table_create (abfd)
119 struct coff_link_hash_table *ret;
121 ret = ((struct coff_link_hash_table *)
122 bfd_alloc (abfd, sizeof (struct coff_link_hash_table)));
125 if (! _bfd_coff_link_hash_table_init (ret, abfd,
126 _bfd_coff_link_hash_newfunc))
128 bfd_release (abfd, ret);
129 return (struct bfd_link_hash_table *) NULL;
134 /* Create an entry in a COFF debug merge hash table. */
136 struct bfd_hash_entry *
137 _bfd_coff_debug_merge_hash_newfunc (entry, table, string)
138 struct bfd_hash_entry *entry;
139 struct bfd_hash_table *table;
142 struct coff_debug_merge_hash_entry *ret =
143 (struct coff_debug_merge_hash_entry *) entry;
145 /* Allocate the structure if it has not already been allocated by a
147 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
148 ret = ((struct coff_debug_merge_hash_entry *)
149 bfd_hash_allocate (table,
150 sizeof (struct coff_debug_merge_hash_entry)));
151 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
152 return (struct bfd_hash_entry *) ret;
154 /* Call the allocation method of the superclass. */
155 ret = ((struct coff_debug_merge_hash_entry *)
156 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
157 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
159 /* Set local fields. */
163 return (struct bfd_hash_entry *) ret;
166 /* Given a COFF BFD, add symbols to the global hash table as
170 _bfd_coff_link_add_symbols (abfd, info)
172 struct bfd_link_info *info;
174 switch (bfd_get_format (abfd))
177 return coff_link_add_object_symbols (abfd, info);
179 return (_bfd_generic_link_add_archive_symbols
180 (abfd, info, coff_link_check_archive_element));
182 bfd_set_error (bfd_error_wrong_format);
187 /* Add symbols from a COFF object file. */
190 coff_link_add_object_symbols (abfd, info)
192 struct bfd_link_info *info;
194 if (! _bfd_coff_get_external_symbols (abfd))
196 if (! coff_link_add_symbols (abfd, info))
199 if (! info->keep_memory)
201 if (! _bfd_coff_free_symbols (abfd))
207 /* Check a single archive element to see if we need to include it in
208 the link. *PNEEDED is set according to whether this element is
209 needed in the link or not. This is called via
210 _bfd_generic_link_add_archive_symbols. */
213 coff_link_check_archive_element (abfd, info, pneeded)
215 struct bfd_link_info *info;
218 if (! _bfd_coff_get_external_symbols (abfd))
221 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
226 if (! coff_link_add_symbols (abfd, info))
230 if (! info->keep_memory || ! *pneeded)
232 if (! _bfd_coff_free_symbols (abfd))
239 /* Look through the symbols to see if this object file should be
240 included in the link. */
243 coff_link_check_ar_symbols (abfd, info, pneeded)
245 struct bfd_link_info *info;
248 bfd_size_type symesz;
254 symesz = bfd_coff_symesz (abfd);
255 esym = (bfd_byte *) obj_coff_external_syms (abfd);
256 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
257 while (esym < esym_end)
259 struct internal_syment sym;
260 enum coff_symbol_classification classification;
262 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
264 classification = bfd_coff_classify_symbol (abfd, &sym);
265 if (classification == COFF_SYMBOL_GLOBAL
266 || classification == COFF_SYMBOL_COMMON)
269 char buf[SYMNMLEN + 1];
270 struct bfd_link_hash_entry *h;
272 /* This symbol is externally visible, and is defined by this
275 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
278 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
280 /* We are only interested in symbols that are currently
281 undefined. If a symbol is currently known to be common,
282 COFF linkers do not bring in an object file which defines
284 if (h != (struct bfd_link_hash_entry *) NULL
285 && h->type == bfd_link_hash_undefined)
287 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
294 esym += (sym.n_numaux + 1) * symesz;
297 /* We do not need this object file. */
301 /* Add all the symbols from an object file to the hash table. */
304 coff_link_add_symbols (abfd, info)
306 struct bfd_link_info *info;
308 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
309 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
310 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
312 boolean default_copy;
313 bfd_size_type symcount;
314 struct coff_link_hash_entry **sym_hash;
315 bfd_size_type symesz;
319 /* Keep the symbols during this function, in case the linker needs
320 to read the generic symbols in order to report an error message. */
321 keep_syms = obj_coff_keep_syms (abfd);
322 obj_coff_keep_syms (abfd) = true;
324 if (info->keep_memory)
325 default_copy = false;
329 symcount = obj_raw_syment_count (abfd);
331 /* We keep a list of the linker hash table entries that correspond
332 to particular symbols. */
333 sym_hash = ((struct coff_link_hash_entry **)
336 * sizeof (struct coff_link_hash_entry *))));
337 if (sym_hash == NULL && symcount != 0)
339 obj_coff_sym_hashes (abfd) = sym_hash;
341 (size_t) symcount * sizeof (struct coff_link_hash_entry *));
343 symesz = bfd_coff_symesz (abfd);
344 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
345 esym = (bfd_byte *) obj_coff_external_syms (abfd);
346 esym_end = esym + symcount * symesz;
347 while (esym < esym_end)
349 struct internal_syment sym;
350 enum coff_symbol_classification classification;
353 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
355 classification = bfd_coff_classify_symbol (abfd, &sym);
356 if (classification != COFF_SYMBOL_LOCAL)
359 char buf[SYMNMLEN + 1];
365 /* This symbol is externally visible. */
367 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
371 /* We must copy the name into memory if we got it from the
372 syment itself, rather than the string table. */
374 if (sym._n._n_n._n_zeroes != 0
375 || sym._n._n_n._n_offset == 0)
380 switch (classification)
385 case COFF_SYMBOL_GLOBAL:
386 flags = BSF_EXPORT | BSF_GLOBAL;
387 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
389 value -= section->vma;
392 case COFF_SYMBOL_UNDEFINED:
394 section = bfd_und_section_ptr;
397 case COFF_SYMBOL_COMMON:
399 section = bfd_com_section_ptr;
402 case COFF_SYMBOL_PE_SECTION:
403 flags = BSF_SECTION_SYM | BSF_GLOBAL;
404 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
408 if (IS_WEAK_EXTERNAL (abfd, sym))
413 /* In the PE format, section symbols actually refer to the
414 start of the output section. We handle them specially
416 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
418 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
419 name, false, copy, false);
420 if (*sym_hash != NULL)
422 if (((*sym_hash)->coff_link_hash_flags
423 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
424 && (*sym_hash)->root.type != bfd_link_hash_undefined
425 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
426 (*_bfd_error_handler)
427 ("Warning: symbol `%s' is both section and non-section",
434 /* The Microsoft Visual C compiler does string pooling by
435 hashing the constants to an internal symbol name, and
436 relying on the the linker comdat support to discard
437 duplicate names. However, if one string is a literal and
438 one is a data initializer, one will end up in the .data
439 section and one will end up in the .rdata section. The
440 Microsoft linker will combine them into the .data
441 section, which seems to be wrong since it might cause the
444 As long as there are no external references to the
445 symbols, which there shouldn't be, we can treat the .data
446 and .rdata instances as separate symbols. The comdat
447 code in the linker will do the appropriate merging. Here
448 we avoid getting a multiple definition error for one of
449 these special symbols.
451 FIXME: I don't think this will work in the case where
452 there are two object files which use the constants as a
453 literal and two object files which use it as a data
454 initializer. One or the other of the second object files
455 is going to wind up with an inappropriate reference. */
457 && (classification == COFF_SYMBOL_GLOBAL
458 || classification == COFF_SYMBOL_PE_SECTION)
459 && section->comdat != NULL
460 && strncmp (name, "??_", 3) == 0
461 && strcmp (name, section->comdat->name) == 0)
463 if (*sym_hash == NULL)
464 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
465 name, false, copy, false);
466 if (*sym_hash != NULL
467 && (*sym_hash)->root.type == bfd_link_hash_defined
468 && (*sym_hash)->root.u.def.section->comdat != NULL
469 && strcmp ((*sym_hash)->root.u.def.section->comdat->name,
470 section->comdat->name) == 0)
476 if (! (bfd_coff_link_add_one_symbol
477 (info, abfd, name, flags, section, value,
478 (const char *) NULL, copy, false,
479 (struct bfd_link_hash_entry **) sym_hash)))
483 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
484 (*sym_hash)->coff_link_hash_flags |=
485 COFF_LINK_HASH_PE_SECTION_SYMBOL;
487 /* Limit the alignment of a common symbol to the possible
488 alignment of a section. There is no point to permitting
489 a higher alignment for a common symbol: we can not
490 guarantee it, and it may cause us to allocate extra space
491 in the common section. */
492 if (section == bfd_com_section_ptr
493 && (*sym_hash)->root.type == bfd_link_hash_common
494 && ((*sym_hash)->root.u.c.p->alignment_power
495 > bfd_coff_default_section_alignment_power (abfd)))
496 (*sym_hash)->root.u.c.p->alignment_power
497 = bfd_coff_default_section_alignment_power (abfd);
499 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
501 /* If we don't have any symbol information currently in
502 the hash table, or if we are looking at a symbol
503 definition, then update the symbol class and type in
505 if (((*sym_hash)->class == C_NULL
506 && (*sym_hash)->type == T_NULL)
509 && (*sym_hash)->root.type != bfd_link_hash_defined
510 && (*sym_hash)->root.type != bfd_link_hash_defweak))
512 (*sym_hash)->class = sym.n_sclass;
513 if (sym.n_type != T_NULL)
515 /* We want to warn if the type changed, but not
516 if it changed from an unspecified type.
517 Testing the whole type byte may work, but the
518 change from (e.g.) a function of unspecified
519 type to function of known type also wants to
521 if ((*sym_hash)->type != T_NULL
522 && (*sym_hash)->type != sym.n_type
523 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
524 && (BTYPE ((*sym_hash)->type) == T_NULL
525 || BTYPE (sym.n_type) == T_NULL)))
526 (*_bfd_error_handler)
527 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
528 name, (*sym_hash)->type, sym.n_type,
529 bfd_get_filename (abfd));
531 /* We don't want to change from a meaningful
532 base type to a null one, but if we know
533 nothing, take what little we might now know. */
534 if (BTYPE (sym.n_type) != T_NULL
535 || (*sym_hash)->type == T_NULL)
536 (*sym_hash)->type = sym.n_type;
538 (*sym_hash)->auxbfd = abfd;
539 if (sym.n_numaux != 0)
541 union internal_auxent *alloc;
544 union internal_auxent *iaux;
546 (*sym_hash)->numaux = sym.n_numaux;
547 alloc = ((union internal_auxent *)
548 bfd_hash_allocate (&info->hash->table,
550 * sizeof (*alloc))));
553 for (i = 0, eaux = esym + symesz, iaux = alloc;
555 i++, eaux += symesz, iaux++)
556 bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
557 sym.n_sclass, i, sym.n_numaux,
559 (*sym_hash)->aux = alloc;
564 if (classification == COFF_SYMBOL_PE_SECTION
565 && (*sym_hash)->numaux != 0)
567 /* Some PE sections (such as .bss) have a zero size in
568 the section header, but a non-zero size in the AUX
569 record. Correct that here.
571 FIXME: This is not at all the right place to do this.
572 For example, it won't help objdump. This needs to be
573 done when we swap in the section header. */
575 BFD_ASSERT ((*sym_hash)->numaux == 1);
576 if (section->_raw_size == 0)
577 section->_raw_size = (*sym_hash)->aux[0].x_scn.x_scnlen;
579 /* FIXME: We could test whether the section sizes
580 matches the size in the aux entry, but apparently
581 that sometimes fails unexpectedly. */
585 esym += (sym.n_numaux + 1) * symesz;
586 sym_hash += sym.n_numaux + 1;
589 /* If this is a non-traditional, non-relocateable link, try to
590 optimize the handling of any .stab/.stabstr sections. */
591 if (! info->relocateable
592 && ! info->traditional_format
593 && info->hash->creator->flavour == bfd_get_flavour (abfd)
594 && (info->strip != strip_all && info->strip != strip_debugger))
596 asection *stab, *stabstr;
598 stab = bfd_get_section_by_name (abfd, ".stab");
601 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
605 struct coff_link_hash_table *table;
606 struct coff_section_tdata *secdata;
608 secdata = coff_section_data (abfd, stab);
612 (PTR) bfd_zalloc (abfd,
613 sizeof (struct coff_section_tdata));
614 if (stab->used_by_bfd == NULL)
616 secdata = coff_section_data (abfd, stab);
619 table = coff_hash_table (info);
621 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
623 &secdata->stab_info))
629 obj_coff_keep_syms (abfd) = keep_syms;
634 obj_coff_keep_syms (abfd) = keep_syms;
638 /* Do the final link step. */
641 _bfd_coff_final_link (abfd, info)
643 struct bfd_link_info *info;
645 bfd_size_type symesz;
646 struct coff_final_link_info finfo;
647 boolean debug_merge_allocated;
648 boolean long_section_names;
650 struct bfd_link_order *p;
651 size_t max_sym_count;
652 size_t max_lineno_count;
653 size_t max_reloc_count;
654 size_t max_output_reloc_count;
655 size_t max_contents_size;
656 file_ptr rel_filepos;
658 file_ptr line_filepos;
661 bfd_byte *external_relocs = NULL;
662 char strbuf[STRING_SIZE_SIZE];
664 symesz = bfd_coff_symesz (abfd);
667 finfo.output_bfd = abfd;
669 finfo.section_info = NULL;
670 finfo.last_file_index = -1;
671 finfo.last_bf_index = -1;
672 finfo.internal_syms = NULL;
673 finfo.sec_ptrs = NULL;
674 finfo.sym_indices = NULL;
675 finfo.outsyms = NULL;
676 finfo.linenos = NULL;
677 finfo.contents = NULL;
678 finfo.external_relocs = NULL;
679 finfo.internal_relocs = NULL;
680 finfo.global_to_static = false;
681 debug_merge_allocated = false;
683 coff_data (abfd)->link_info = info;
685 finfo.strtab = _bfd_stringtab_init ();
686 if (finfo.strtab == NULL)
689 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
691 debug_merge_allocated = true;
693 /* Compute the file positions for all the sections. */
694 if (! abfd->output_has_begun)
696 if (! bfd_coff_compute_section_file_positions (abfd))
700 /* Count the line numbers and relocation entries required for the
701 output file. Set the file positions for the relocs. */
702 rel_filepos = obj_relocbase (abfd);
703 relsz = bfd_coff_relsz (abfd);
704 max_contents_size = 0;
705 max_lineno_count = 0;
708 long_section_names = false;
709 for (o = abfd->sections; o != NULL; o = o->next)
713 for (p = o->link_order_head; p != NULL; p = p->next)
715 if (p->type == bfd_indirect_link_order)
719 sec = p->u.indirect.section;
721 /* Mark all sections which are to be included in the
722 link. This will normally be every section. We need
723 to do this so that we can identify any sections which
724 the linker has decided to not include. */
725 sec->linker_mark = true;
727 if (info->strip == strip_none
728 || info->strip == strip_some)
729 o->lineno_count += sec->lineno_count;
731 if (info->relocateable)
732 o->reloc_count += sec->reloc_count;
734 if (sec->_raw_size > max_contents_size)
735 max_contents_size = sec->_raw_size;
736 if (sec->lineno_count > max_lineno_count)
737 max_lineno_count = sec->lineno_count;
738 if (sec->reloc_count > max_reloc_count)
739 max_reloc_count = sec->reloc_count;
741 else if (info->relocateable
742 && (p->type == bfd_section_reloc_link_order
743 || p->type == bfd_symbol_reloc_link_order))
746 if (o->reloc_count == 0)
750 o->flags |= SEC_RELOC;
751 o->rel_filepos = rel_filepos;
752 rel_filepos += o->reloc_count * relsz;
755 if (bfd_coff_long_section_names (abfd)
756 && strlen (o->name) > SCNNMLEN)
758 /* This section has a long name which must go in the string
759 table. This must correspond to the code in
760 coff_write_object_contents which puts the string index
761 into the s_name field of the section header. That is why
762 we pass hash as false. */
763 if (_bfd_stringtab_add (finfo.strtab, o->name, false, false)
764 == (bfd_size_type) -1)
766 long_section_names = true;
770 /* If doing a relocateable link, allocate space for the pointers we
772 if (info->relocateable)
776 /* We use section_count + 1, rather than section_count, because
777 the target_index fields are 1 based. */
779 ((struct coff_link_section_info *)
780 bfd_malloc ((abfd->section_count + 1)
781 * sizeof (struct coff_link_section_info)));
782 if (finfo.section_info == NULL)
784 for (i = 0; i <= abfd->section_count; i++)
786 finfo.section_info[i].relocs = NULL;
787 finfo.section_info[i].rel_hashes = NULL;
791 /* We now know the size of the relocs, so we can determine the file
792 positions of the line numbers. */
793 line_filepos = rel_filepos;
794 linesz = bfd_coff_linesz (abfd);
795 max_output_reloc_count = 0;
796 for (o = abfd->sections; o != NULL; o = o->next)
798 if (o->lineno_count == 0)
802 o->line_filepos = line_filepos;
803 line_filepos += o->lineno_count * linesz;
806 if (o->reloc_count != 0)
808 /* We don't know the indices of global symbols until we have
809 written out all the local symbols. For each section in
810 the output file, we keep an array of pointers to hash
811 table entries. Each entry in the array corresponds to a
812 reloc. When we find a reloc against a global symbol, we
813 set the corresponding entry in this array so that we can
814 fix up the symbol index after we have written out all the
817 Because of this problem, we also keep the relocs in
818 memory until the end of the link. This wastes memory,
819 but only when doing a relocateable link, which is not the
821 BFD_ASSERT (info->relocateable);
822 finfo.section_info[o->target_index].relocs =
823 ((struct internal_reloc *)
824 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
825 finfo.section_info[o->target_index].rel_hashes =
826 ((struct coff_link_hash_entry **)
827 bfd_malloc (o->reloc_count
828 * sizeof (struct coff_link_hash_entry *)));
829 if (finfo.section_info[o->target_index].relocs == NULL
830 || finfo.section_info[o->target_index].rel_hashes == NULL)
833 if (o->reloc_count > max_output_reloc_count)
834 max_output_reloc_count = o->reloc_count;
837 /* Reset the reloc and lineno counts, so that we can use them to
838 count the number of entries we have output so far. */
843 obj_sym_filepos (abfd) = line_filepos;
845 /* Figure out the largest number of symbols in an input BFD. Take
846 the opportunity to clear the output_has_begun fields of all the
849 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
853 sub->output_has_begun = false;
854 sz = obj_raw_syment_count (sub);
855 if (sz > max_sym_count)
859 /* Allocate some buffers used while linking. */
860 finfo.internal_syms = ((struct internal_syment *)
861 bfd_malloc (max_sym_count
862 * sizeof (struct internal_syment)));
863 finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count
864 * sizeof (asection *));
865 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
866 finfo.outsyms = ((bfd_byte *)
867 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
868 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
869 * bfd_coff_linesz (abfd));
870 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
871 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
872 if (! info->relocateable)
873 finfo.internal_relocs = ((struct internal_reloc *)
874 bfd_malloc (max_reloc_count
875 * sizeof (struct internal_reloc)));
876 if ((finfo.internal_syms == NULL && max_sym_count > 0)
877 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
878 || (finfo.sym_indices == NULL && max_sym_count > 0)
879 || finfo.outsyms == NULL
880 || (finfo.linenos == NULL && max_lineno_count > 0)
881 || (finfo.contents == NULL && max_contents_size > 0)
882 || (finfo.external_relocs == NULL && max_reloc_count > 0)
883 || (! info->relocateable
884 && finfo.internal_relocs == NULL
885 && max_reloc_count > 0))
888 /* We now know the position of everything in the file, except that
889 we don't know the size of the symbol table and therefore we don't
890 know where the string table starts. We just build the string
891 table in memory as we go along. We process all the relocations
892 for a single input file at once. */
893 obj_raw_syment_count (abfd) = 0;
895 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
897 if (! bfd_coff_start_final_link (abfd, info))
901 for (o = abfd->sections; o != NULL; o = o->next)
903 for (p = o->link_order_head; p != NULL; p = p->next)
905 if (p->type == bfd_indirect_link_order
906 && bfd_family_coff (p->u.indirect.section->owner))
908 sub = p->u.indirect.section->owner;
909 if (! bfd_coff_link_output_has_begun (sub, & finfo))
911 if (! _bfd_coff_link_input_bfd (&finfo, sub))
913 sub->output_has_begun = true;
916 else if (p->type == bfd_section_reloc_link_order
917 || p->type == bfd_symbol_reloc_link_order)
919 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
924 if (! _bfd_default_link_order (abfd, info, o, p))
930 if (! bfd_coff_final_link_postscript (abfd, & finfo))
933 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
935 coff_debug_merge_hash_table_free (&finfo.debug_merge);
936 debug_merge_allocated = false;
938 if (finfo.internal_syms != NULL)
940 free (finfo.internal_syms);
941 finfo.internal_syms = NULL;
943 if (finfo.sec_ptrs != NULL)
945 free (finfo.sec_ptrs);
946 finfo.sec_ptrs = NULL;
948 if (finfo.sym_indices != NULL)
950 free (finfo.sym_indices);
951 finfo.sym_indices = NULL;
953 if (finfo.linenos != NULL)
955 free (finfo.linenos);
956 finfo.linenos = NULL;
958 if (finfo.contents != NULL)
960 free (finfo.contents);
961 finfo.contents = NULL;
963 if (finfo.external_relocs != NULL)
965 free (finfo.external_relocs);
966 finfo.external_relocs = NULL;
968 if (finfo.internal_relocs != NULL)
970 free (finfo.internal_relocs);
971 finfo.internal_relocs = NULL;
974 /* The value of the last C_FILE symbol is supposed to be the symbol
975 index of the first external symbol. Write it out again if
977 if (finfo.last_file_index != -1
978 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
980 finfo.last_file.n_value = obj_raw_syment_count (abfd);
981 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
982 (PTR) finfo.outsyms);
984 (obj_sym_filepos (abfd)
985 + finfo.last_file_index * symesz),
987 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
991 /* If doing task linking (ld --task-link) then make a pass through the
992 global symbols, writing out any that are defined, and making them
996 finfo.failed = false;
997 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_task_globals,
1003 /* Write out the global symbols. */
1004 finfo.failed = false;
1005 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
1010 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1011 if (finfo.outsyms != NULL)
1013 free (finfo.outsyms);
1014 finfo.outsyms = NULL;
1017 if (info->relocateable && max_output_reloc_count > 0)
1019 /* Now that we have written out all the global symbols, we know
1020 the symbol indices to use for relocs against them, and we can
1021 finally write out the relocs. */
1022 external_relocs = ((bfd_byte *)
1023 bfd_malloc (max_output_reloc_count * relsz));
1024 if (external_relocs == NULL)
1027 for (o = abfd->sections; o != NULL; o = o->next)
1029 struct internal_reloc *irel;
1030 struct internal_reloc *irelend;
1031 struct coff_link_hash_entry **rel_hash;
1034 if (o->reloc_count == 0)
1037 irel = finfo.section_info[o->target_index].relocs;
1038 irelend = irel + o->reloc_count;
1039 rel_hash = finfo.section_info[o->target_index].rel_hashes;
1040 erel = external_relocs;
1041 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1043 if (*rel_hash != NULL)
1045 BFD_ASSERT ((*rel_hash)->indx >= 0);
1046 irel->r_symndx = (*rel_hash)->indx;
1048 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
1051 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
1052 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
1053 abfd) != relsz * o->reloc_count)
1057 free (external_relocs);
1058 external_relocs = NULL;
1061 /* Free up the section information. */
1062 if (finfo.section_info != NULL)
1066 for (i = 0; i < abfd->section_count; i++)
1068 if (finfo.section_info[i].relocs != NULL)
1069 free (finfo.section_info[i].relocs);
1070 if (finfo.section_info[i].rel_hashes != NULL)
1071 free (finfo.section_info[i].rel_hashes);
1073 free (finfo.section_info);
1074 finfo.section_info = NULL;
1077 /* If we have optimized stabs strings, output them. */
1078 if (coff_hash_table (info)->stab_info != NULL)
1080 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1084 /* Write out the string table. */
1085 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1088 (obj_sym_filepos (abfd)
1089 + obj_raw_syment_count (abfd) * symesz),
1093 #if STRING_SIZE_SIZE == 4
1095 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1096 (bfd_byte *) strbuf);
1098 #error Change bfd_h_put_32
1101 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
1104 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1107 obj_coff_strings_written (abfd) = true;
1110 _bfd_stringtab_free (finfo.strtab);
1112 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1113 not try to write out the symbols. */
1114 bfd_get_symcount (abfd) = 0;
1119 if (debug_merge_allocated)
1120 coff_debug_merge_hash_table_free (&finfo.debug_merge);
1121 if (finfo.strtab != NULL)
1122 _bfd_stringtab_free (finfo.strtab);
1123 if (finfo.section_info != NULL)
1127 for (i = 0; i < abfd->section_count; i++)
1129 if (finfo.section_info[i].relocs != NULL)
1130 free (finfo.section_info[i].relocs);
1131 if (finfo.section_info[i].rel_hashes != NULL)
1132 free (finfo.section_info[i].rel_hashes);
1134 free (finfo.section_info);
1136 if (finfo.internal_syms != NULL)
1137 free (finfo.internal_syms);
1138 if (finfo.sec_ptrs != NULL)
1139 free (finfo.sec_ptrs);
1140 if (finfo.sym_indices != NULL)
1141 free (finfo.sym_indices);
1142 if (finfo.outsyms != NULL)
1143 free (finfo.outsyms);
1144 if (finfo.linenos != NULL)
1145 free (finfo.linenos);
1146 if (finfo.contents != NULL)
1147 free (finfo.contents);
1148 if (finfo.external_relocs != NULL)
1149 free (finfo.external_relocs);
1150 if (finfo.internal_relocs != NULL)
1151 free (finfo.internal_relocs);
1152 if (external_relocs != NULL)
1153 free (external_relocs);
1157 /* parse out a -heap <reserved>,<commit> line */
1160 dores_com (ptr, output_bfd, heap)
1165 if (coff_data(output_bfd)->pe)
1167 int val = strtoul (ptr, &ptr, 0);
1169 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve =val;
1171 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve =val;
1175 int val = strtoul (ptr+1, &ptr, 0);
1177 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit =val;
1179 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit =val;
1185 static char *get_name(ptr, dst)
1192 while (*ptr && *ptr != ' ')
1198 /* Process any magic embedded commands in a section called .drectve */
1201 process_embedded_commands (output_bfd, info, abfd)
1203 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1206 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1213 copy = bfd_malloc ((size_t) sec->_raw_size);
1216 if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size))
1221 e = copy + sec->_raw_size;
1222 for (s = copy; s < e ; )
1228 if (strncmp (s,"-attr", 5) == 0)
1240 s = get_name(s, &name);
1241 s = get_name(s, &attribs);
1261 asec = bfd_get_section_by_name (abfd, name);
1264 asec->flags |= SEC_CODE;
1266 asec->flags |= SEC_READONLY;
1269 else if (strncmp (s,"-heap", 5) == 0)
1271 s = dores_com (s+5, output_bfd, 1);
1273 else if (strncmp (s,"-stack", 6) == 0)
1275 s = dores_com (s+6, output_bfd, 0);
1284 /* Place a marker against all symbols which are used by relocations.
1285 This marker can be picked up by the 'do we skip this symbol ?'
1286 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1291 mark_relocs (finfo, input_bfd)
1292 struct coff_final_link_info * finfo;
1297 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1300 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1302 struct internal_reloc * internal_relocs;
1303 struct internal_reloc * irel;
1304 struct internal_reloc * irelend;
1306 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1)
1309 /* Read in the relocs. */
1310 internal_relocs = _bfd_coff_read_internal_relocs
1311 (input_bfd, a, false,
1312 finfo->external_relocs,
1313 finfo->info->relocateable,
1314 (finfo->info->relocateable
1315 ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1316 : finfo->internal_relocs)
1319 if (internal_relocs == NULL)
1322 irel = internal_relocs;
1323 irelend = irel + a->reloc_count;
1325 /* Place a mark in the sym_indices array (whose entries have
1326 been initialised to 0) for all of the symbols that are used
1327 in the relocation table. This will then be picked up in the
1330 for (; irel < irelend; irel++)
1332 finfo->sym_indices[ irel->r_symndx ] = -1;
1337 /* Link an input file into the linker output file. This function
1338 handles all the sections and relocations of the input file at once. */
1341 _bfd_coff_link_input_bfd (finfo, input_bfd)
1342 struct coff_final_link_info *finfo;
1345 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1346 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1348 unsigned int n_btmask = coff_data (input_bfd)->local_n_btmask;
1350 boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *,
1351 asection *, struct internal_reloc *,
1354 const char *strings;
1355 bfd_size_type syment_base;
1357 bfd_size_type isymesz;
1358 bfd_size_type osymesz;
1359 bfd_size_type linesz;
1362 struct internal_syment *isymp;
1365 unsigned long output_index;
1367 struct coff_link_hash_entry **sym_hash;
1370 /* Move all the symbols to the output file. */
1372 output_bfd = finfo->output_bfd;
1374 syment_base = obj_raw_syment_count (output_bfd);
1375 isymesz = bfd_coff_symesz (input_bfd);
1376 osymesz = bfd_coff_symesz (output_bfd);
1377 linesz = bfd_coff_linesz (input_bfd);
1378 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1381 if (! finfo->info->keep_memory)
1384 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1387 if (! _bfd_coff_get_external_symbols (input_bfd))
1390 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1391 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1392 isymp = finfo->internal_syms;
1393 secpp = finfo->sec_ptrs;
1394 indexp = finfo->sym_indices;
1395 output_index = syment_base;
1396 outsym = finfo->outsyms;
1398 if (coff_data (output_bfd)->pe)
1400 if (! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1404 /* If we are going to perform relocations and also strip/discard some symbols
1405 then we must make sure that we do not strip/discard those symbols that are
1406 going to be involved in the relocations */
1407 if (( finfo->info->strip != strip_none
1408 || finfo->info->discard != discard_none)
1409 && finfo->info->relocateable)
1411 /* mark the symbol array as 'not-used' */
1412 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1414 mark_relocs (finfo, input_bfd);
1417 while (esym < esym_end)
1419 struct internal_syment isym;
1420 enum coff_symbol_classification classification;
1423 boolean dont_skip_symbol;
1426 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1428 /* Make a copy of *isymp so that the relocate_section function
1429 always sees the original values. This is more reliable than
1430 always recomputing the symbol value even if we are stripping
1434 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1435 switch (classification)
1439 case COFF_SYMBOL_GLOBAL:
1440 case COFF_SYMBOL_PE_SECTION:
1441 case COFF_SYMBOL_LOCAL:
1442 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1444 case COFF_SYMBOL_COMMON:
1445 *secpp = bfd_com_section_ptr;
1447 case COFF_SYMBOL_UNDEFINED:
1448 *secpp = bfd_und_section_ptr;
1452 /* Extract the flag indicating if this symbol is used by a
1454 if ((finfo->info->strip != strip_none
1455 || finfo->info->discard != discard_none)
1456 && finfo->info->relocateable)
1457 dont_skip_symbol = *indexp;
1459 dont_skip_symbol = false;
1465 add = 1 + isym.n_numaux;
1467 /* If we are stripping all symbols, we want to skip this one. */
1468 if (finfo->info->strip == strip_all && ! dont_skip_symbol)
1473 switch (classification)
1477 case COFF_SYMBOL_GLOBAL:
1478 case COFF_SYMBOL_COMMON:
1479 case COFF_SYMBOL_PE_SECTION:
1480 /* This is a global symbol. Global symbols come at the
1481 end of the symbol table, so skip them for now.
1482 Locally defined function symbols, however, are an
1483 exception, and are not moved to the end. */
1485 if (! ISFCN (isym.n_type))
1489 case COFF_SYMBOL_UNDEFINED:
1490 /* Undefined symbols are left for the end. */
1495 case COFF_SYMBOL_LOCAL:
1496 /* This is a local symbol. Skip it if we are discarding
1498 if (finfo->info->discard == discard_all && ! dont_skip_symbol)
1504 /* If we stripping debugging symbols, and this is a debugging
1505 symbol, then skip it. FIXME: gas sets the section to N_ABS
1506 for some types of debugging symbols; I don't know if this is
1507 a bug or not. In any case, we handle it here. */
1509 && finfo->info->strip == strip_debugger
1510 && ! dont_skip_symbol
1511 && (isym.n_scnum == N_DEBUG
1512 || (isym.n_scnum == N_ABS
1513 && (isym.n_sclass == C_AUTO
1514 || isym.n_sclass == C_REG
1515 || isym.n_sclass == C_MOS
1516 || isym.n_sclass == C_MOE
1517 || isym.n_sclass == C_MOU
1518 || isym.n_sclass == C_ARG
1519 || isym.n_sclass == C_REGPARM
1520 || isym.n_sclass == C_FIELD
1521 || isym.n_sclass == C_EOS))))
1524 /* If some symbols are stripped based on the name, work out the
1525 name and decide whether to skip this symbol. */
1527 && (finfo->info->strip == strip_some
1528 || finfo->info->discard == discard_l))
1531 char buf[SYMNMLEN + 1];
1533 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1537 if (! dont_skip_symbol
1538 && ((finfo->info->strip == strip_some
1539 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1542 && finfo->info->discard == discard_l
1543 && bfd_is_local_label_name (input_bfd, name))))
1547 /* If this is an enum, struct, or union tag, see if we have
1548 already output an identical type. */
1550 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1551 && (isym.n_sclass == C_ENTAG
1552 || isym.n_sclass == C_STRTAG
1553 || isym.n_sclass == C_UNTAG)
1554 && isym.n_numaux == 1)
1557 char buf[SYMNMLEN + 1];
1558 struct coff_debug_merge_hash_entry *mh;
1559 struct coff_debug_merge_type *mt;
1560 union internal_auxent aux;
1561 struct coff_debug_merge_element **epp;
1562 bfd_byte *esl, *eslend;
1563 struct internal_syment *islp;
1565 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1569 /* Ignore fake names invented by compiler; treat them all as
1571 if (*name == '~' || *name == '.' || *name == '$'
1572 || (*name == bfd_get_symbol_leading_char (input_bfd)
1573 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1576 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1581 /* Allocate memory to hold type information. If this turns
1582 out to be a duplicate, we pass this address to
1584 mt = ((struct coff_debug_merge_type *)
1585 bfd_alloc (input_bfd,
1586 sizeof (struct coff_debug_merge_type)));
1589 mt->class = isym.n_sclass;
1591 /* Pick up the aux entry, which points to the end of the tag
1593 bfd_coff_swap_aux_in (input_bfd, (PTR) (esym + isymesz),
1594 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1597 /* Gather the elements. */
1598 epp = &mt->elements;
1599 mt->elements = NULL;
1601 esl = esym + 2 * isymesz;
1602 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1603 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1604 while (esl < eslend)
1606 const char *elename;
1607 char elebuf[SYMNMLEN + 1];
1610 bfd_coff_swap_sym_in (input_bfd, (PTR) esl, (PTR) islp);
1612 *epp = ((struct coff_debug_merge_element *)
1613 bfd_alloc (input_bfd,
1614 sizeof (struct coff_debug_merge_element)));
1618 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1620 if (elename == NULL)
1623 name_copy = (char *) bfd_alloc (input_bfd,
1624 strlen (elename) + 1);
1625 if (name_copy == NULL)
1627 strcpy (name_copy, elename);
1629 (*epp)->name = name_copy;
1630 (*epp)->type = islp->n_type;
1632 if (islp->n_numaux >= 1
1633 && islp->n_type != T_NULL
1634 && islp->n_sclass != C_EOS)
1636 union internal_auxent eleaux;
1639 bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz),
1640 islp->n_type, islp->n_sclass, 0,
1641 islp->n_numaux, (PTR) &eleaux);
1642 indx = eleaux.x_sym.x_tagndx.l;
1644 /* FIXME: If this tagndx entry refers to a symbol
1645 defined later in this file, we just ignore it.
1646 Handling this correctly would be tedious, and may
1652 (bfd_byte *) obj_coff_external_syms (input_bfd))
1655 (*epp)->tagndx = finfo->sym_indices[indx];
1656 if ((*epp)->tagndx < 0)
1660 epp = &(*epp)->next;
1663 esl += (islp->n_numaux + 1) * isymesz;
1664 islp += islp->n_numaux + 1;
1667 /* See if we already have a definition which matches this
1668 type. We always output the type if it has no elements,
1670 if (mt->elements == NULL)
1671 bfd_release (input_bfd, (PTR) mt);
1674 struct coff_debug_merge_type *mtl;
1676 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1678 struct coff_debug_merge_element *me, *mel;
1680 if (mtl->class != mt->class)
1683 for (me = mt->elements, mel = mtl->elements;
1684 me != NULL && mel != NULL;
1685 me = me->next, mel = mel->next)
1687 if (strcmp (me->name, mel->name) != 0
1688 || me->type != mel->type
1689 || me->tagndx != mel->tagndx)
1693 if (me == NULL && mel == NULL)
1697 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1699 /* This is the first definition of this type. */
1700 mt->indx = output_index;
1701 mt->next = mh->types;
1706 /* This is a redefinition which can be merged. */
1707 bfd_release (input_bfd, (PTR) mt);
1708 *indexp = mtl->indx;
1709 add = (eslend - esym) / isymesz;
1715 /* We now know whether we are to skip this symbol or not. */
1718 /* Adjust the symbol in order to output it. */
1720 if (isym._n._n_n._n_zeroes == 0
1721 && isym._n._n_n._n_offset != 0)
1726 /* This symbol has a long name. Enter it in the string
1727 table we are building. Note that we do not check
1728 bfd_coff_symname_in_debug. That is only true for
1729 XCOFF, and XCOFF requires different linking code
1731 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1735 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1736 if (indx == (bfd_size_type) -1)
1738 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1741 switch (isym.n_sclass)
1757 /* The symbol value should not be modified. */
1761 if (obj_pe (input_bfd)
1762 && strcmp (isym.n_name, ".bf") != 0
1763 && isym.n_scnum > 0)
1765 /* For PE, .lf and .ef get their value left alone,
1766 while .bf gets relocated. However, they all have
1767 "real" section numbers, and need to be moved into
1769 isym.n_scnum = (*secpp)->output_section->target_index;
1774 case C_LABEL: /* Not completely sure about these 2 */
1783 /* Compute new symbol location. */
1784 if (isym.n_scnum > 0)
1786 isym.n_scnum = (*secpp)->output_section->target_index;
1787 isym.n_value += (*secpp)->output_offset;
1788 if (! obj_pe (input_bfd))
1789 isym.n_value -= (*secpp)->vma;
1790 if (! obj_pe (finfo->output_bfd))
1791 isym.n_value += (*secpp)->output_section->vma;
1796 /* The value of a C_FILE symbol is the symbol index of
1797 the next C_FILE symbol. The value of the last C_FILE
1798 symbol is the symbol index to the first external
1799 symbol (actually, coff_renumber_symbols does not get
1800 this right--it just sets the value of the last C_FILE
1801 symbol to zero--and nobody has ever complained about
1802 it). We try to get this right, below, just before we
1803 write the symbols out, but in the general case we may
1804 have to write the symbol out twice. */
1806 if (finfo->last_file_index != -1
1807 && finfo->last_file.n_value != (long) output_index)
1809 /* We must correct the value of the last C_FILE
1811 finfo->last_file.n_value = output_index;
1812 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1814 /* The last C_FILE symbol is in this input file. */
1815 bfd_coff_swap_sym_out (output_bfd,
1816 (PTR) &finfo->last_file,
1817 (PTR) (finfo->outsyms
1818 + ((finfo->last_file_index
1824 /* We have already written out the last C_FILE
1825 symbol. We need to write it out again. We
1826 borrow *outsym temporarily. */
1827 bfd_coff_swap_sym_out (output_bfd,
1828 (PTR) &finfo->last_file,
1830 if (bfd_seek (output_bfd,
1831 (obj_sym_filepos (output_bfd)
1832 + finfo->last_file_index * osymesz),
1834 || (bfd_write (outsym, osymesz, 1, output_bfd)
1840 finfo->last_file_index = output_index;
1841 finfo->last_file = isym;
1845 /* If doing task linking, convert normal global function symbols to
1846 static functions. */
1847 if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1848 isym.n_sclass = C_STAT;
1850 /* Output the symbol. */
1852 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1854 *indexp = output_index;
1859 struct coff_link_hash_entry *h;
1861 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1863 h = obj_coff_sym_hashes (input_bfd)[indx];
1866 /* This can happen if there were errors earlier in
1868 bfd_set_error (bfd_error_bad_value);
1871 h->indx = output_index;
1874 output_index += add;
1875 outsym += add * osymesz;
1878 esym += add * isymesz;
1882 for (--add; add > 0; --add)
1889 /* Fix up the aux entries. This must be done in a separate pass,
1890 because we don't know the correct symbol indices until we have
1891 already decided which symbols we are going to keep. */
1893 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1894 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1895 isymp = finfo->internal_syms;
1896 indexp = finfo->sym_indices;
1897 sym_hash = obj_coff_sym_hashes (input_bfd);
1898 outsym = finfo->outsyms;
1899 while (esym < esym_end)
1903 add = 1 + isymp->n_numaux;
1906 || (bfd_size_type) *indexp < syment_base)
1907 && (*sym_hash == NULL
1908 || (*sym_hash)->auxbfd != input_bfd))
1909 esym += add * isymesz;
1912 struct coff_link_hash_entry *h;
1920 /* The m68k-motorola-sysv assembler will sometimes
1921 generate two symbols with the same name, but only one
1922 will have aux entries. */
1923 BFD_ASSERT (isymp->n_numaux == 0
1924 || h->numaux == isymp->n_numaux);
1932 /* Handle the aux entries. This handling is based on
1933 coff_pointerize_aux. I don't know if it always correct. */
1934 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1936 union internal_auxent aux;
1937 union internal_auxent *auxp;
1943 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1944 isymp->n_sclass, i, isymp->n_numaux,
1949 if (isymp->n_sclass == C_FILE)
1951 /* If this is a long filename, we must put it in the
1953 if (auxp->x_file.x_n.x_zeroes == 0
1954 && auxp->x_file.x_n.x_offset != 0)
1956 const char *filename;
1959 BFD_ASSERT (auxp->x_file.x_n.x_offset
1960 >= STRING_SIZE_SIZE);
1961 if (strings == NULL)
1963 strings = _bfd_coff_read_string_table (input_bfd);
1964 if (strings == NULL)
1967 filename = strings + auxp->x_file.x_n.x_offset;
1968 indx = _bfd_stringtab_add (finfo->strtab, filename,
1970 if (indx == (bfd_size_type) -1)
1972 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1975 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1979 if (ISFCN (isymp->n_type)
1980 || ISTAG (isymp->n_sclass)
1981 || isymp->n_sclass == C_BLOCK
1982 || isymp->n_sclass == C_FCN)
1984 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1986 && indx < obj_raw_syment_count (input_bfd))
1988 /* We look forward through the symbol for
1989 the index of the next symbol we are going
1990 to include. I don't know if this is
1992 while ((finfo->sym_indices[indx] < 0
1993 || ((bfd_size_type) finfo->sym_indices[indx]
1995 && indx < obj_raw_syment_count (input_bfd))
1997 if (indx >= obj_raw_syment_count (input_bfd))
1998 indx = output_index;
2000 indx = finfo->sym_indices[indx];
2001 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2005 indx = auxp->x_sym.x_tagndx.l;
2006 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2010 symindx = finfo->sym_indices[indx];
2012 auxp->x_sym.x_tagndx.l = 0;
2014 auxp->x_sym.x_tagndx.l = symindx;
2017 /* The .bf symbols are supposed to be linked through
2018 the endndx field. We need to carry this list
2019 across object files. */
2022 && isymp->n_sclass == C_FCN
2023 && (isymp->_n._n_n._n_zeroes != 0
2024 || isymp->_n._n_n._n_offset == 0)
2025 && isymp->_n._n_name[0] == '.'
2026 && isymp->_n._n_name[1] == 'b'
2027 && isymp->_n._n_name[2] == 'f'
2028 && isymp->_n._n_name[3] == '\0')
2030 if (finfo->last_bf_index != -1)
2032 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2035 if ((bfd_size_type) finfo->last_bf_index
2040 /* The last .bf symbol is in this input
2041 file. This will only happen if the
2042 assembler did not set up the .bf
2043 endndx symbols correctly. */
2044 auxout = (PTR) (finfo->outsyms
2045 + ((finfo->last_bf_index
2048 bfd_coff_swap_aux_out (output_bfd,
2049 (PTR) &finfo->last_bf,
2057 /* We have already written out the last
2058 .bf aux entry. We need to write it
2059 out again. We borrow *outsym
2060 temporarily. FIXME: This case should
2062 bfd_coff_swap_aux_out (output_bfd,
2063 (PTR) &finfo->last_bf,
2068 if (bfd_seek (output_bfd,
2069 (obj_sym_filepos (output_bfd)
2070 + finfo->last_bf_index * osymesz),
2072 || bfd_write (outsym, osymesz, 1,
2073 output_bfd) != osymesz)
2078 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2079 finfo->last_bf_index = -1;
2082 /* The endndx field of this aux entry must
2083 be updated with the symbol number of the
2085 finfo->last_bf = *auxp;
2086 finfo->last_bf_index = (((outsym - finfo->outsyms)
2095 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
2096 isymp->n_sclass, i, isymp->n_numaux,
2110 /* Relocate the line numbers, unless we are stripping them. */
2111 if (finfo->info->strip == strip_none
2112 || finfo->info->strip == strip_some)
2114 for (o = input_bfd->sections; o != NULL; o = o->next)
2122 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2123 build_link_order in ldwrite.c will not have created a
2124 link order, which means that we will not have seen this
2125 input section in _bfd_coff_final_link, which means that
2126 we will not have allocated space for the line numbers of
2127 this section. I don't think line numbers can be
2128 meaningful for a section which does not have
2129 SEC_HAS_CONTENTS set, but, if they do, this must be
2131 if (o->lineno_count == 0
2132 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2135 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2136 || bfd_read (finfo->linenos, linesz, o->lineno_count,
2137 input_bfd) != linesz * o->lineno_count)
2140 offset = o->output_section->vma + o->output_offset - o->vma;
2141 eline = finfo->linenos;
2142 oeline = finfo->linenos;
2143 elineend = eline + linesz * o->lineno_count;
2145 for (; eline < elineend; eline += linesz)
2147 struct internal_lineno iline;
2149 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
2151 if (iline.l_lnno != 0)
2152 iline.l_addr.l_paddr += offset;
2153 else if (iline.l_addr.l_symndx >= 0
2154 && ((unsigned long) iline.l_addr.l_symndx
2155 < obj_raw_syment_count (input_bfd)))
2159 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2163 /* These line numbers are attached to a symbol
2164 which we are stripping. We must discard the
2165 line numbers because reading them back with
2166 no associated symbol (or associating them all
2167 with symbol #0) will fail. We can't regain
2168 the space in the output file, but at least
2174 struct internal_syment is;
2175 union internal_auxent ia;
2177 /* Fix up the lnnoptr field in the aux entry of
2178 the symbol. It turns out that we can't do
2179 this when we modify the symbol aux entries,
2180 because gas sometimes screws up the lnnoptr
2181 field and makes it an offset from the start
2182 of the line numbers rather than an absolute
2184 bfd_coff_swap_sym_in (output_bfd,
2185 (PTR) (finfo->outsyms
2186 + ((indx - syment_base)
2189 if ((ISFCN (is.n_type)
2190 || is.n_sclass == C_BLOCK)
2191 && is.n_numaux >= 1)
2195 auxptr = (PTR) (finfo->outsyms
2196 + ((indx - syment_base + 1)
2198 bfd_coff_swap_aux_in (output_bfd, auxptr,
2199 is.n_type, is.n_sclass,
2200 0, is.n_numaux, (PTR) &ia);
2201 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2202 (o->output_section->line_filepos
2203 + o->output_section->lineno_count * linesz
2204 + eline - finfo->linenos);
2205 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
2206 is.n_type, is.n_sclass, 0,
2207 is.n_numaux, auxptr);
2213 iline.l_addr.l_symndx = indx;
2218 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline,
2224 if (bfd_seek (output_bfd,
2225 (o->output_section->line_filepos
2226 + o->output_section->lineno_count * linesz),
2228 || (bfd_write (finfo->linenos, 1, oeline - finfo->linenos,
2230 != (bfd_size_type) (oeline - finfo->linenos)))
2233 o->output_section->lineno_count +=
2234 (oeline - finfo->linenos) / linesz;
2238 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2239 symbol will be the first symbol in the next input file. In the
2240 normal case, this will save us from writing out the C_FILE symbol
2242 if (finfo->last_file_index != -1
2243 && (bfd_size_type) finfo->last_file_index >= syment_base)
2245 finfo->last_file.n_value = output_index;
2246 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
2247 (PTR) (finfo->outsyms
2248 + ((finfo->last_file_index - syment_base)
2252 /* Write the modified symbols to the output file. */
2253 if (outsym > finfo->outsyms)
2255 if (bfd_seek (output_bfd,
2256 obj_sym_filepos (output_bfd) + syment_base * osymesz,
2258 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
2260 != (bfd_size_type) (outsym - finfo->outsyms)))
2263 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2264 + (outsym - finfo->outsyms) / osymesz)
2267 obj_raw_syment_count (output_bfd) = output_index;
2270 /* Relocate the contents of each section. */
2271 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2272 for (o = input_bfd->sections; o != NULL; o = o->next)
2275 struct coff_section_tdata *secdata;
2277 if (! o->linker_mark)
2279 /* This section was omitted from the link. */
2283 if ((o->flags & SEC_HAS_CONTENTS) == 0
2284 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
2286 if ((o->flags & SEC_RELOC) != 0
2287 && o->reloc_count != 0)
2289 ((*_bfd_error_handler)
2290 (_("%s: relocs in section `%s', but it has no contents"),
2291 bfd_get_filename (input_bfd),
2292 bfd_get_section_name (input_bfd, o)));
2293 bfd_set_error (bfd_error_no_contents);
2300 secdata = coff_section_data (input_bfd, o);
2301 if (secdata != NULL && secdata->contents != NULL)
2302 contents = secdata->contents;
2305 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2306 (file_ptr) 0, o->_raw_size))
2308 contents = finfo->contents;
2311 if ((o->flags & SEC_RELOC) != 0)
2314 struct internal_reloc *internal_relocs;
2315 struct internal_reloc *irel;
2317 /* Read in the relocs. */
2318 target_index = o->output_section->target_index;
2319 internal_relocs = (_bfd_coff_read_internal_relocs
2320 (input_bfd, o, false, finfo->external_relocs,
2321 finfo->info->relocateable,
2322 (finfo->info->relocateable
2323 ? (finfo->section_info[target_index].relocs
2324 + o->output_section->reloc_count)
2325 : finfo->internal_relocs)));
2326 if (internal_relocs == NULL)
2329 /* Call processor specific code to relocate the section
2331 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2335 finfo->internal_syms,
2339 if (finfo->info->relocateable)
2342 struct internal_reloc *irelend;
2343 struct coff_link_hash_entry **rel_hash;
2345 offset = o->output_section->vma + o->output_offset - o->vma;
2346 irel = internal_relocs;
2347 irelend = irel + o->reloc_count;
2348 rel_hash = (finfo->section_info[target_index].rel_hashes
2349 + o->output_section->reloc_count);
2350 for (; irel < irelend; irel++, rel_hash++)
2352 struct coff_link_hash_entry *h;
2357 /* Adjust the reloc address and symbol index. */
2359 irel->r_vaddr += offset;
2361 if (irel->r_symndx == -1)
2366 if (! (*adjust_symndx) (output_bfd, finfo->info,
2374 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2377 /* This is a global symbol. */
2379 irel->r_symndx = h->indx;
2382 /* This symbol is being written at the end
2383 of the file, and we do not yet know the
2384 symbol index. We save the pointer to the
2385 hash table entry in the rel_hash list.
2386 We set the indx field to -2 to indicate
2387 that this symbol must not be stripped. */
2396 indx = finfo->sym_indices[irel->r_symndx];
2398 irel->r_symndx = indx;
2401 struct internal_syment *is;
2403 char buf[SYMNMLEN + 1];
2405 /* This reloc is against a symbol we are
2406 stripping. This should have been handled
2407 by the 'dont_skip_symbol' code in the while
2408 loop at the top of this function. */
2410 is = finfo->internal_syms + irel->r_symndx;
2412 name = (_bfd_coff_internal_syment_name
2413 (input_bfd, is, buf));
2417 if (! ((*finfo->info->callbacks->unattached_reloc)
2418 (finfo->info, name, input_bfd, o,
2425 o->output_section->reloc_count += o->reloc_count;
2429 /* Write out the modified section contents. */
2430 if (secdata == NULL || secdata->stab_info == NULL)
2432 if (! bfd_set_section_contents (output_bfd, o->output_section,
2436 bfd_octets_per_byte (output_bfd)),
2437 (o->_cooked_size != 0
2444 if (! (_bfd_write_section_stabs
2445 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2446 o, &secdata->stab_info, contents)))
2451 if (! finfo->info->keep_memory)
2453 if (! _bfd_coff_free_symbols (input_bfd))
2460 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2463 _bfd_coff_write_global_sym (h, data)
2464 struct coff_link_hash_entry *h;
2467 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2469 struct internal_syment isym;
2470 bfd_size_type symesz;
2473 output_bfd = finfo->output_bfd;
2479 && (finfo->info->strip == strip_all
2480 || (finfo->info->strip == strip_some
2481 && (bfd_hash_lookup (finfo->info->keep_hash,
2482 h->root.root.string, false, false)
2486 switch (h->root.type)
2489 case bfd_link_hash_new:
2493 case bfd_link_hash_undefined:
2494 case bfd_link_hash_undefweak:
2495 isym.n_scnum = N_UNDEF;
2499 case bfd_link_hash_defined:
2500 case bfd_link_hash_defweak:
2504 sec = h->root.u.def.section->output_section;
2505 if (bfd_is_abs_section (sec))
2506 isym.n_scnum = N_ABS;
2508 isym.n_scnum = sec->target_index;
2509 isym.n_value = (h->root.u.def.value
2510 + h->root.u.def.section->output_offset);
2511 if (! obj_pe (finfo->output_bfd))
2512 isym.n_value += sec->vma;
2516 case bfd_link_hash_common:
2517 isym.n_scnum = N_UNDEF;
2518 isym.n_value = h->root.u.c.size;
2521 case bfd_link_hash_indirect:
2522 case bfd_link_hash_warning:
2523 /* Just ignore these. They can't be handled anyhow. */
2527 if (strlen (h->root.root.string) <= SYMNMLEN)
2528 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2535 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2537 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2539 if (indx == (bfd_size_type) -1)
2541 finfo->failed = true;
2544 isym._n._n_n._n_zeroes = 0;
2545 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2548 isym.n_sclass = h->class;
2549 isym.n_type = h->type;
2551 if (isym.n_sclass == C_NULL)
2552 isym.n_sclass = C_EXT;
2554 /* If doing task linking and this is the pass where we convert
2555 defined globals to statics, then do that conversion now. If the
2556 symbol is not being converted, just ignore it and it will be
2557 output during a later pass. */
2558 if (finfo->global_to_static)
2560 if (! IS_EXTERNAL (output_bfd, isym))
2563 isym.n_sclass = C_STAT;
2566 /* When a weak symbol is not overriden by a strong one,
2567 turn it into an external symbol when not building a
2568 shared or relocateable object. */
2569 if (! finfo->info->shared
2570 && ! finfo->info->relocateable
2571 && IS_WEAK_EXTERNAL (finfo->output_bfd, isym))
2572 isym.n_sclass = C_EXT;
2574 isym.n_numaux = h->numaux;
2576 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2578 symesz = bfd_coff_symesz (output_bfd);
2580 if (bfd_seek (output_bfd,
2581 (obj_sym_filepos (output_bfd)
2582 + obj_raw_syment_count (output_bfd) * symesz),
2584 || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2586 finfo->failed = true;
2590 h->indx = obj_raw_syment_count (output_bfd);
2592 ++obj_raw_syment_count (output_bfd);
2594 /* Write out any associated aux entries. Most of the aux entries
2595 will have been modified in _bfd_coff_link_input_bfd. We have to
2596 handle section aux entries here, now that we have the final
2597 relocation and line number counts. */
2598 for (i = 0; i < isym.n_numaux; i++)
2600 union internal_auxent *auxp;
2604 /* Look for a section aux entry here using the same tests that
2605 coff_swap_aux_out uses. */
2607 && (isym.n_sclass == C_STAT
2608 || isym.n_sclass == C_HIDDEN)
2609 && isym.n_type == T_NULL
2610 && (h->root.type == bfd_link_hash_defined
2611 || h->root.type == bfd_link_hash_defweak))
2615 sec = h->root.u.def.section->output_section;
2618 auxp->x_scn.x_scnlen = (sec->_cooked_size != 0
2622 /* For PE, an overflow on the final link reportedly does
2623 not matter. FIXME: Why not? */
2625 if (sec->reloc_count > 0xffff
2626 && (! obj_pe (output_bfd)
2627 || finfo->info->relocateable))
2628 (*_bfd_error_handler)
2629 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2630 bfd_get_filename (output_bfd),
2631 bfd_get_section_name (output_bfd, sec),
2634 if (sec->lineno_count > 0xffff
2635 && (! obj_pe (output_bfd)
2636 || finfo->info->relocateable))
2637 (*_bfd_error_handler)
2638 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2639 bfd_get_filename (output_bfd),
2640 bfd_get_section_name (output_bfd, sec),
2643 auxp->x_scn.x_nreloc = sec->reloc_count;
2644 auxp->x_scn.x_nlinno = sec->lineno_count;
2645 auxp->x_scn.x_checksum = 0;
2646 auxp->x_scn.x_associated = 0;
2647 auxp->x_scn.x_comdat = 0;
2651 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isym.n_type,
2652 isym.n_sclass, i, isym.n_numaux,
2653 (PTR) finfo->outsyms);
2654 if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2656 finfo->failed = true;
2659 ++obj_raw_syment_count (output_bfd);
2665 /* Write out task global symbols, converting them to statics. Called
2666 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2667 the dirty work, if the symbol we are processing needs conversion. */
2670 _bfd_coff_write_task_globals (h, data)
2671 struct coff_link_hash_entry *h;
2674 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2675 boolean rtnval = true;
2676 boolean save_global_to_static;
2680 switch (h->root.type)
2682 case bfd_link_hash_defined:
2683 case bfd_link_hash_defweak:
2684 save_global_to_static = finfo->global_to_static;
2685 finfo->global_to_static = true;
2686 rtnval = _bfd_coff_write_global_sym (h, data);
2687 finfo->global_to_static = save_global_to_static;
2696 /* Handle a link order which is supposed to generate a reloc. */
2699 _bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2701 struct coff_final_link_info *finfo;
2702 asection *output_section;
2703 struct bfd_link_order *link_order;
2705 reloc_howto_type *howto;
2706 struct internal_reloc *irel;
2707 struct coff_link_hash_entry **rel_hash_ptr;
2709 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2712 bfd_set_error (bfd_error_bad_value);
2716 if (link_order->u.reloc.p->addend != 0)
2720 bfd_reloc_status_type rstat;
2723 size = bfd_get_reloc_size (howto);
2724 buf = (bfd_byte *) bfd_zmalloc (size);
2728 rstat = _bfd_relocate_contents (howto, output_bfd,
2729 link_order->u.reloc.p->addend, buf);
2735 case bfd_reloc_outofrange:
2737 case bfd_reloc_overflow:
2738 if (! ((*finfo->info->callbacks->reloc_overflow)
2740 (link_order->type == bfd_section_reloc_link_order
2741 ? bfd_section_name (output_bfd,
2742 link_order->u.reloc.p->u.section)
2743 : link_order->u.reloc.p->u.name),
2744 howto->name, link_order->u.reloc.p->addend,
2745 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2752 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2754 (link_order->offset *
2755 bfd_octets_per_byte (output_bfd)), size);
2761 /* Store the reloc information in the right place. It will get
2762 swapped and written out at the end of the final_link routine. */
2764 irel = (finfo->section_info[output_section->target_index].relocs
2765 + output_section->reloc_count);
2766 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2767 + output_section->reloc_count);
2769 memset (irel, 0, sizeof (struct internal_reloc));
2770 *rel_hash_ptr = NULL;
2772 irel->r_vaddr = output_section->vma + link_order->offset;
2774 if (link_order->type == bfd_section_reloc_link_order)
2776 /* We need to somehow locate a symbol in the right section. The
2777 symbol must either have a value of zero, or we must adjust
2778 the addend by the value of the symbol. FIXME: Write this
2779 when we need it. The old linker couldn't handle this anyhow. */
2781 *rel_hash_ptr = NULL;
2786 struct coff_link_hash_entry *h;
2788 h = ((struct coff_link_hash_entry *)
2789 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2790 link_order->u.reloc.p->u.name,
2791 false, false, true));
2795 irel->r_symndx = h->indx;
2798 /* Set the index to -2 to force this symbol to get
2807 if (! ((*finfo->info->callbacks->unattached_reloc)
2808 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2809 (asection *) NULL, (bfd_vma) 0)))
2815 /* FIXME: Is this always right? */
2816 irel->r_type = howto->type;
2818 /* r_size is only used on the RS/6000, which needs its own linker
2819 routines anyhow. r_extern is only used for ECOFF. */
2821 /* FIXME: What is the right value for r_offset? Is zero OK? */
2823 ++output_section->reloc_count;
2828 /* A basic reloc handling routine which may be used by processors with
2832 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2833 input_section, contents, relocs, syms,
2836 struct bfd_link_info *info;
2838 asection *input_section;
2840 struct internal_reloc *relocs;
2841 struct internal_syment *syms;
2842 asection **sections;
2844 struct internal_reloc *rel;
2845 struct internal_reloc *relend;
2848 relend = rel + input_section->reloc_count;
2849 for (; rel < relend; rel++)
2852 struct coff_link_hash_entry *h;
2853 struct internal_syment *sym;
2856 reloc_howto_type *howto;
2857 bfd_reloc_status_type rstat;
2859 symndx = rel->r_symndx;
2867 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2869 (*_bfd_error_handler)
2870 ("%s: illegal symbol index %ld in relocs",
2871 bfd_get_filename (input_bfd), symndx);
2876 h = obj_coff_sym_hashes (input_bfd)[symndx];
2877 sym = syms + symndx;
2880 /* COFF treats common symbols in one of two ways. Either the
2881 size of the symbol is included in the section contents, or it
2882 is not. We assume that the size is not included, and force
2883 the rtype_to_howto function to adjust the addend as needed. */
2885 if (sym != NULL && sym->n_scnum != 0)
2886 addend = - sym->n_value;
2890 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2895 /* If we are doing a relocateable link, then we can just ignore
2896 a PC relative reloc that is pcrel_offset. It will already
2897 have the correct value. If this is not a relocateable link,
2898 then we should ignore the symbol value. */
2899 if (howto->pc_relative && howto->pcrel_offset)
2901 if (info->relocateable)
2903 if (sym != NULL && sym->n_scnum != 0)
2904 addend += sym->n_value;
2915 sec = bfd_abs_section_ptr;
2920 sec = sections[symndx];
2921 val = (sec->output_section->vma
2922 + sec->output_offset
2924 if (! obj_pe (input_bfd))
2930 if (h->root.type == bfd_link_hash_defined
2931 || h->root.type == bfd_link_hash_defweak)
2935 sec = h->root.u.def.section;
2936 val = (h->root.u.def.value
2937 + sec->output_section->vma
2938 + sec->output_offset);
2941 else if (h->root.type == bfd_link_hash_undefweak)
2944 else if (! info->relocateable)
2946 if (! ((*info->callbacks->undefined_symbol)
2947 (info, h->root.root.string, input_bfd, input_section,
2948 rel->r_vaddr - input_section->vma, true)))
2953 if (info->base_file)
2955 /* Emit a reloc if the backend thinks it needs it. */
2956 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
2958 /* Relocation to a symbol in a section which isn't
2959 absolute. We output the address here to a file.
2960 This file is then read by dlltool when generating the
2961 reloc section. Note that the base file is not
2962 portable between systems. We write out a long here,
2963 and dlltool reads in a long. */
2964 long addr = (rel->r_vaddr
2965 - input_section->vma
2966 + input_section->output_offset
2967 + input_section->output_section->vma);
2968 if (coff_data (output_bfd)->pe)
2969 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2970 if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
2973 bfd_set_error (bfd_error_system_call);
2979 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2981 rel->r_vaddr - input_section->vma,
2990 case bfd_reloc_outofrange:
2991 (*_bfd_error_handler)
2992 (_("%s: bad reloc address 0x%lx in section `%s'"),
2993 bfd_get_filename (input_bfd),
2994 (unsigned long) rel->r_vaddr,
2995 bfd_get_section_name (input_bfd, input_section));
2997 case bfd_reloc_overflow:
3000 char buf[SYMNMLEN + 1];
3005 name = h->root.root.string;
3008 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3013 if (! ((*info->callbacks->reloc_overflow)
3014 (info, name, howto->name, (bfd_vma) 0, input_bfd,
3015 input_section, rel->r_vaddr - input_section->vma)))