1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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;
120 bfd_size_type amt = sizeof (struct coff_link_hash_table);
122 ret = (struct coff_link_hash_table *) bfd_malloc (amt);
125 if (! _bfd_coff_link_hash_table_init (ret, abfd,
126 _bfd_coff_link_hash_newfunc))
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);
281 if (!h && info->pei386_auto_import)
283 if (!strncmp (name,"__imp_", 6))
286 bfd_link_hash_lookup (info->hash, name + 6, false, false,
290 /* We are only interested in symbols that are currently
291 undefined. If a symbol is currently known to be common,
292 COFF linkers do not bring in an object file which defines
294 if (h != (struct bfd_link_hash_entry *) NULL
295 && h->type == bfd_link_hash_undefined)
297 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
304 esym += (sym.n_numaux + 1) * symesz;
307 /* We do not need this object file. */
311 /* Add all the symbols from an object file to the hash table. */
314 coff_link_add_symbols (abfd, info)
316 struct bfd_link_info *info;
318 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
319 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
320 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
322 boolean default_copy;
323 bfd_size_type symcount;
324 struct coff_link_hash_entry **sym_hash;
325 bfd_size_type symesz;
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 symcount = obj_raw_syment_count (abfd);
342 /* We keep a list of the linker hash table entries that correspond
343 to particular symbols. */
344 amt = symcount * sizeof (struct coff_link_hash_entry *);
345 sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
346 if (sym_hash == NULL && symcount != 0)
348 obj_coff_sym_hashes (abfd) = sym_hash;
350 symesz = bfd_coff_symesz (abfd);
351 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
352 esym = (bfd_byte *) obj_coff_external_syms (abfd);
353 esym_end = esym + symcount * symesz;
354 while (esym < esym_end)
356 struct internal_syment sym;
357 enum coff_symbol_classification classification;
360 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
362 classification = bfd_coff_classify_symbol (abfd, &sym);
363 if (classification != COFF_SYMBOL_LOCAL)
366 char buf[SYMNMLEN + 1];
372 /* This symbol is externally visible. */
374 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
378 /* We must copy the name into memory if we got it from the
379 syment itself, rather than the string table. */
381 if (sym._n._n_n._n_zeroes != 0
382 || sym._n._n_n._n_offset == 0)
387 switch (classification)
392 case COFF_SYMBOL_GLOBAL:
393 flags = BSF_EXPORT | BSF_GLOBAL;
394 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
396 value -= section->vma;
399 case COFF_SYMBOL_UNDEFINED:
401 section = bfd_und_section_ptr;
404 case COFF_SYMBOL_COMMON:
406 section = bfd_com_section_ptr;
409 case COFF_SYMBOL_PE_SECTION:
410 flags = BSF_SECTION_SYM | BSF_GLOBAL;
411 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
415 if (IS_WEAK_EXTERNAL (abfd, sym))
420 /* In the PE format, section symbols actually refer to the
421 start of the output section. We handle them specially
423 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
425 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
426 name, false, copy, false);
427 if (*sym_hash != NULL)
429 if (((*sym_hash)->coff_link_hash_flags
430 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
431 && (*sym_hash)->root.type != bfd_link_hash_undefined
432 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
433 (*_bfd_error_handler)
434 ("Warning: symbol `%s' is both section and non-section",
441 /* The Microsoft Visual C compiler does string pooling by
442 hashing the constants to an internal symbol name, and
443 relying on the linker comdat support to discard
444 duplicate names. However, if one string is a literal and
445 one is a data initializer, one will end up in the .data
446 section and one will end up in the .rdata section. The
447 Microsoft linker will combine them into the .data
448 section, which seems to be wrong since it might cause the
451 As long as there are no external references to the
452 symbols, which there shouldn't be, we can treat the .data
453 and .rdata instances as separate symbols. The comdat
454 code in the linker will do the appropriate merging. Here
455 we avoid getting a multiple definition error for one of
456 these special symbols.
458 FIXME: I don't think this will work in the case where
459 there are two object files which use the constants as a
460 literal and two object files which use it as a data
461 initializer. One or the other of the second object files
462 is going to wind up with an inappropriate reference. */
464 && (classification == COFF_SYMBOL_GLOBAL
465 || classification == COFF_SYMBOL_PE_SECTION)
466 && section->comdat != NULL
467 && strncmp (name, "??_", 3) == 0
468 && strcmp (name, section->comdat->name) == 0)
470 if (*sym_hash == NULL)
471 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
472 name, false, copy, false);
473 if (*sym_hash != NULL
474 && (*sym_hash)->root.type == bfd_link_hash_defined
475 && (*sym_hash)->root.u.def.section->comdat != NULL
476 && strcmp ((*sym_hash)->root.u.def.section->comdat->name,
477 section->comdat->name) == 0)
483 if (! (bfd_coff_link_add_one_symbol
484 (info, abfd, name, flags, section, value,
485 (const char *) NULL, copy, false,
486 (struct bfd_link_hash_entry **) sym_hash)))
490 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
491 (*sym_hash)->coff_link_hash_flags |=
492 COFF_LINK_HASH_PE_SECTION_SYMBOL;
494 /* Limit the alignment of a common symbol to the possible
495 alignment of a section. There is no point to permitting
496 a higher alignment for a common symbol: we can not
497 guarantee it, and it may cause us to allocate extra space
498 in the common section. */
499 if (section == bfd_com_section_ptr
500 && (*sym_hash)->root.type == bfd_link_hash_common
501 && ((*sym_hash)->root.u.c.p->alignment_power
502 > bfd_coff_default_section_alignment_power (abfd)))
503 (*sym_hash)->root.u.c.p->alignment_power
504 = bfd_coff_default_section_alignment_power (abfd);
506 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
508 /* If we don't have any symbol information currently in
509 the hash table, or if we are looking at a symbol
510 definition, then update the symbol class and type in
512 if (((*sym_hash)->class == C_NULL
513 && (*sym_hash)->type == T_NULL)
516 && (*sym_hash)->root.type != bfd_link_hash_defined
517 && (*sym_hash)->root.type != bfd_link_hash_defweak))
519 (*sym_hash)->class = sym.n_sclass;
520 if (sym.n_type != T_NULL)
522 /* We want to warn if the type changed, but not
523 if it changed from an unspecified type.
524 Testing the whole type byte may work, but the
525 change from (e.g.) a function of unspecified
526 type to function of known type also wants to
528 if ((*sym_hash)->type != T_NULL
529 && (*sym_hash)->type != sym.n_type
530 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
531 && (BTYPE ((*sym_hash)->type) == T_NULL
532 || BTYPE (sym.n_type) == T_NULL)))
533 (*_bfd_error_handler)
534 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
535 name, (*sym_hash)->type, sym.n_type,
536 bfd_archive_filename (abfd));
538 /* We don't want to change from a meaningful
539 base type to a null one, but if we know
540 nothing, take what little we might now know. */
541 if (BTYPE (sym.n_type) != T_NULL
542 || (*sym_hash)->type == T_NULL)
543 (*sym_hash)->type = sym.n_type;
545 (*sym_hash)->auxbfd = abfd;
546 if (sym.n_numaux != 0)
548 union internal_auxent *alloc;
551 union internal_auxent *iaux;
553 (*sym_hash)->numaux = sym.n_numaux;
554 alloc = ((union internal_auxent *)
555 bfd_hash_allocate (&info->hash->table,
557 * sizeof (*alloc))));
560 for (i = 0, eaux = esym + symesz, iaux = alloc;
562 i++, eaux += symesz, iaux++)
563 bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
564 sym.n_sclass, (int) i,
565 sym.n_numaux, (PTR) iaux);
566 (*sym_hash)->aux = alloc;
571 if (classification == COFF_SYMBOL_PE_SECTION
572 && (*sym_hash)->numaux != 0)
574 /* Some PE sections (such as .bss) have a zero size in
575 the section header, but a non-zero size in the AUX
576 record. Correct that here.
578 FIXME: This is not at all the right place to do this.
579 For example, it won't help objdump. This needs to be
580 done when we swap in the section header. */
582 BFD_ASSERT ((*sym_hash)->numaux == 1);
583 if (section->_raw_size == 0)
584 section->_raw_size = (*sym_hash)->aux[0].x_scn.x_scnlen;
586 /* FIXME: We could test whether the section sizes
587 matches the size in the aux entry, but apparently
588 that sometimes fails unexpectedly. */
592 esym += (sym.n_numaux + 1) * symesz;
593 sym_hash += sym.n_numaux + 1;
596 /* If this is a non-traditional, non-relocateable link, try to
597 optimize the handling of any .stab/.stabstr sections. */
598 if (! info->relocateable
599 && ! info->traditional_format
600 && info->hash->creator->flavour == bfd_get_flavour (abfd)
601 && (info->strip != strip_all && info->strip != strip_debugger))
603 asection *stab, *stabstr;
605 stab = bfd_get_section_by_name (abfd, ".stab");
608 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
612 struct coff_link_hash_table *table;
613 struct coff_section_tdata *secdata;
615 secdata = coff_section_data (abfd, stab);
618 amt = sizeof (struct coff_section_tdata);
619 stab->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
620 if (stab->used_by_bfd == NULL)
622 secdata = coff_section_data (abfd, stab);
625 table = coff_hash_table (info);
627 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
629 &secdata->stab_info))
635 obj_coff_keep_syms (abfd) = keep_syms;
640 obj_coff_keep_syms (abfd) = keep_syms;
644 /* Do the final link step. */
647 _bfd_coff_final_link (abfd, info)
649 struct bfd_link_info *info;
651 bfd_size_type symesz;
652 struct coff_final_link_info finfo;
653 boolean debug_merge_allocated;
654 boolean long_section_names;
656 struct bfd_link_order *p;
657 bfd_size_type max_sym_count;
658 bfd_size_type max_lineno_count;
659 bfd_size_type max_reloc_count;
660 bfd_size_type max_output_reloc_count;
661 bfd_size_type max_contents_size;
662 file_ptr rel_filepos;
664 file_ptr line_filepos;
667 bfd_byte *external_relocs = NULL;
668 char strbuf[STRING_SIZE_SIZE];
671 symesz = bfd_coff_symesz (abfd);
674 finfo.output_bfd = abfd;
676 finfo.section_info = NULL;
677 finfo.last_file_index = -1;
678 finfo.last_bf_index = -1;
679 finfo.internal_syms = NULL;
680 finfo.sec_ptrs = NULL;
681 finfo.sym_indices = NULL;
682 finfo.outsyms = NULL;
683 finfo.linenos = NULL;
684 finfo.contents = NULL;
685 finfo.external_relocs = NULL;
686 finfo.internal_relocs = NULL;
687 finfo.global_to_static = false;
688 debug_merge_allocated = false;
690 coff_data (abfd)->link_info = info;
692 finfo.strtab = _bfd_stringtab_init ();
693 if (finfo.strtab == NULL)
696 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
698 debug_merge_allocated = true;
700 /* Compute the file positions for all the sections. */
701 if (! abfd->output_has_begun)
703 if (! bfd_coff_compute_section_file_positions (abfd))
707 /* Count the line numbers and relocation entries required for the
708 output file. Set the file positions for the relocs. */
709 rel_filepos = obj_relocbase (abfd);
710 relsz = bfd_coff_relsz (abfd);
711 max_contents_size = 0;
712 max_lineno_count = 0;
715 long_section_names = false;
716 for (o = abfd->sections; o != NULL; o = o->next)
720 for (p = o->link_order_head; p != NULL; p = p->next)
722 if (p->type == bfd_indirect_link_order)
726 sec = p->u.indirect.section;
728 /* Mark all sections which are to be included in the
729 link. This will normally be every section. We need
730 to do this so that we can identify any sections which
731 the linker has decided to not include. */
732 sec->linker_mark = true;
734 if (info->strip == strip_none
735 || info->strip == strip_some)
736 o->lineno_count += sec->lineno_count;
738 if (info->relocateable)
739 o->reloc_count += sec->reloc_count;
741 if (sec->_raw_size > max_contents_size)
742 max_contents_size = sec->_raw_size;
743 if (sec->lineno_count > max_lineno_count)
744 max_lineno_count = sec->lineno_count;
745 if (sec->reloc_count > max_reloc_count)
746 max_reloc_count = sec->reloc_count;
748 else if (info->relocateable
749 && (p->type == bfd_section_reloc_link_order
750 || p->type == bfd_symbol_reloc_link_order))
753 if (o->reloc_count == 0)
757 o->flags |= SEC_RELOC;
758 o->rel_filepos = rel_filepos;
759 rel_filepos += o->reloc_count * relsz;
760 /* In PE COFF, if there are at least 0xffff relocations an
761 extra relocation will be written out to encode the count. */
762 if (obj_pe (abfd) && o->reloc_count >= 0xffff)
763 rel_filepos += relsz;
766 if (bfd_coff_long_section_names (abfd)
767 && strlen (o->name) > SCNNMLEN)
769 /* This section has a long name which must go in the string
770 table. This must correspond to the code in
771 coff_write_object_contents which puts the string index
772 into the s_name field of the section header. That is why
773 we pass hash as false. */
774 if (_bfd_stringtab_add (finfo.strtab, o->name, false, false)
775 == (bfd_size_type) -1)
777 long_section_names = true;
781 /* If doing a relocateable link, allocate space for the pointers we
783 if (info->relocateable)
787 /* We use section_count + 1, rather than section_count, because
788 the target_index fields are 1 based. */
789 amt = abfd->section_count + 1;
790 amt *= sizeof (struct coff_link_section_info);
791 finfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
792 if (finfo.section_info == NULL)
794 for (i = 0; i <= abfd->section_count; i++)
796 finfo.section_info[i].relocs = NULL;
797 finfo.section_info[i].rel_hashes = NULL;
801 /* We now know the size of the relocs, so we can determine the file
802 positions of the line numbers. */
803 line_filepos = rel_filepos;
804 linesz = bfd_coff_linesz (abfd);
805 max_output_reloc_count = 0;
806 for (o = abfd->sections; o != NULL; o = o->next)
808 if (o->lineno_count == 0)
812 o->line_filepos = line_filepos;
813 line_filepos += o->lineno_count * linesz;
816 if (o->reloc_count != 0)
818 /* We don't know the indices of global symbols until we have
819 written out all the local symbols. For each section in
820 the output file, we keep an array of pointers to hash
821 table entries. Each entry in the array corresponds to a
822 reloc. When we find a reloc against a global symbol, we
823 set the corresponding entry in this array so that we can
824 fix up the symbol index after we have written out all the
827 Because of this problem, we also keep the relocs in
828 memory until the end of the link. This wastes memory,
829 but only when doing a relocateable link, which is not the
831 BFD_ASSERT (info->relocateable);
832 amt = o->reloc_count;
833 amt *= sizeof (struct internal_reloc);
834 finfo.section_info[o->target_index].relocs =
835 (struct internal_reloc *) bfd_malloc (amt);
836 amt = o->reloc_count;
837 amt *= sizeof (struct coff_link_hash_entry *);
838 finfo.section_info[o->target_index].rel_hashes =
839 (struct coff_link_hash_entry **) bfd_malloc (amt);
840 if (finfo.section_info[o->target_index].relocs == NULL
841 || finfo.section_info[o->target_index].rel_hashes == NULL)
844 if (o->reloc_count > max_output_reloc_count)
845 max_output_reloc_count = o->reloc_count;
848 /* Reset the reloc and lineno counts, so that we can use them to
849 count the number of entries we have output so far. */
854 obj_sym_filepos (abfd) = line_filepos;
856 /* Figure out the largest number of symbols in an input BFD. Take
857 the opportunity to clear the output_has_begun fields of all the
860 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
864 sub->output_has_begun = false;
865 sz = obj_raw_syment_count (sub);
866 if (sz > max_sym_count)
870 /* Allocate some buffers used while linking. */
871 amt = max_sym_count * sizeof (struct internal_syment);
872 finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
873 amt = max_sym_count * sizeof (asection *);
874 finfo.sec_ptrs = (asection **) bfd_malloc (amt);
875 amt = max_sym_count * sizeof (long);
876 finfo.sym_indices = (long *) bfd_malloc (amt);
877 finfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
878 amt = max_lineno_count * bfd_coff_linesz (abfd);
879 finfo.linenos = (bfd_byte *) bfd_malloc (amt);
880 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
881 amt = max_reloc_count * relsz;
882 finfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
883 if (! info->relocateable)
885 amt = max_reloc_count * sizeof (struct internal_reloc);
886 finfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
888 if ((finfo.internal_syms == NULL && max_sym_count > 0)
889 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
890 || (finfo.sym_indices == NULL && max_sym_count > 0)
891 || finfo.outsyms == NULL
892 || (finfo.linenos == NULL && max_lineno_count > 0)
893 || (finfo.contents == NULL && max_contents_size > 0)
894 || (finfo.external_relocs == NULL && max_reloc_count > 0)
895 || (! info->relocateable
896 && finfo.internal_relocs == NULL
897 && max_reloc_count > 0))
900 /* We now know the position of everything in the file, except that
901 we don't know the size of the symbol table and therefore we don't
902 know where the string table starts. We just build the string
903 table in memory as we go along. We process all the relocations
904 for a single input file at once. */
905 obj_raw_syment_count (abfd) = 0;
907 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
909 if (! bfd_coff_start_final_link (abfd, info))
913 for (o = abfd->sections; o != NULL; o = o->next)
915 for (p = o->link_order_head; p != NULL; p = p->next)
917 if (p->type == bfd_indirect_link_order
918 && bfd_family_coff (p->u.indirect.section->owner))
920 sub = p->u.indirect.section->owner;
921 if (! bfd_coff_link_output_has_begun (sub, & finfo))
923 if (! _bfd_coff_link_input_bfd (&finfo, sub))
925 sub->output_has_begun = true;
928 else if (p->type == bfd_section_reloc_link_order
929 || p->type == bfd_symbol_reloc_link_order)
931 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
936 if (! _bfd_default_link_order (abfd, info, o, p))
942 if (! bfd_coff_final_link_postscript (abfd, & finfo))
945 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
947 coff_debug_merge_hash_table_free (&finfo.debug_merge);
948 debug_merge_allocated = false;
950 if (finfo.internal_syms != NULL)
952 free (finfo.internal_syms);
953 finfo.internal_syms = NULL;
955 if (finfo.sec_ptrs != NULL)
957 free (finfo.sec_ptrs);
958 finfo.sec_ptrs = NULL;
960 if (finfo.sym_indices != NULL)
962 free (finfo.sym_indices);
963 finfo.sym_indices = NULL;
965 if (finfo.linenos != NULL)
967 free (finfo.linenos);
968 finfo.linenos = NULL;
970 if (finfo.contents != NULL)
972 free (finfo.contents);
973 finfo.contents = NULL;
975 if (finfo.external_relocs != NULL)
977 free (finfo.external_relocs);
978 finfo.external_relocs = NULL;
980 if (finfo.internal_relocs != NULL)
982 free (finfo.internal_relocs);
983 finfo.internal_relocs = NULL;
986 /* The value of the last C_FILE symbol is supposed to be the symbol
987 index of the first external symbol. Write it out again if
989 if (finfo.last_file_index != -1
990 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
994 finfo.last_file.n_value = obj_raw_syment_count (abfd);
995 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
996 (PTR) finfo.outsyms);
998 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
999 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1000 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
1004 /* If doing task linking (ld --task-link) then make a pass through the
1005 global symbols, writing out any that are defined, and making them
1007 if (info->task_link)
1009 finfo.failed = false;
1010 coff_link_hash_traverse (coff_hash_table (info),
1011 _bfd_coff_write_task_globals,
1017 /* Write out the global symbols. */
1018 finfo.failed = false;
1019 coff_link_hash_traverse (coff_hash_table (info),
1020 _bfd_coff_write_global_sym,
1025 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1026 if (finfo.outsyms != NULL)
1028 free (finfo.outsyms);
1029 finfo.outsyms = NULL;
1032 if (info->relocateable && max_output_reloc_count > 0)
1034 /* Now that we have written out all the global symbols, we know
1035 the symbol indices to use for relocs against them, and we can
1036 finally write out the relocs. */
1037 amt = max_output_reloc_count * relsz;
1038 external_relocs = (bfd_byte *) bfd_malloc (amt);
1039 if (external_relocs == NULL)
1042 for (o = abfd->sections; o != NULL; o = o->next)
1044 struct internal_reloc *irel;
1045 struct internal_reloc *irelend;
1046 struct coff_link_hash_entry **rel_hash;
1049 if (o->reloc_count == 0)
1052 irel = finfo.section_info[o->target_index].relocs;
1053 irelend = irel + o->reloc_count;
1054 rel_hash = finfo.section_info[o->target_index].rel_hashes;
1055 erel = external_relocs;
1056 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1058 if (*rel_hash != NULL)
1060 BFD_ASSERT ((*rel_hash)->indx >= 0);
1061 irel->r_symndx = (*rel_hash)->indx;
1063 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
1066 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
1067 || (bfd_bwrite ((PTR) external_relocs,
1068 (bfd_size_type) relsz * o->reloc_count, abfd)
1069 != (bfd_size_type) relsz * o->reloc_count))
1073 free (external_relocs);
1074 external_relocs = NULL;
1077 /* Free up the section information. */
1078 if (finfo.section_info != NULL)
1082 for (i = 0; i < abfd->section_count; i++)
1084 if (finfo.section_info[i].relocs != NULL)
1085 free (finfo.section_info[i].relocs);
1086 if (finfo.section_info[i].rel_hashes != NULL)
1087 free (finfo.section_info[i].rel_hashes);
1089 free (finfo.section_info);
1090 finfo.section_info = NULL;
1093 /* If we have optimized stabs strings, output them. */
1094 if (coff_hash_table (info)->stab_info != NULL)
1096 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1100 /* Write out the string table. */
1101 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1105 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1106 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1109 #if STRING_SIZE_SIZE == 4
1111 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1114 #error Change H_PUT_32 above
1117 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1118 != STRING_SIZE_SIZE)
1121 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1124 obj_coff_strings_written (abfd) = true;
1127 _bfd_stringtab_free (finfo.strtab);
1129 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1130 not try to write out the symbols. */
1131 bfd_get_symcount (abfd) = 0;
1136 if (debug_merge_allocated)
1137 coff_debug_merge_hash_table_free (&finfo.debug_merge);
1138 if (finfo.strtab != NULL)
1139 _bfd_stringtab_free (finfo.strtab);
1140 if (finfo.section_info != NULL)
1144 for (i = 0; i < abfd->section_count; i++)
1146 if (finfo.section_info[i].relocs != NULL)
1147 free (finfo.section_info[i].relocs);
1148 if (finfo.section_info[i].rel_hashes != NULL)
1149 free (finfo.section_info[i].rel_hashes);
1151 free (finfo.section_info);
1153 if (finfo.internal_syms != NULL)
1154 free (finfo.internal_syms);
1155 if (finfo.sec_ptrs != NULL)
1156 free (finfo.sec_ptrs);
1157 if (finfo.sym_indices != NULL)
1158 free (finfo.sym_indices);
1159 if (finfo.outsyms != NULL)
1160 free (finfo.outsyms);
1161 if (finfo.linenos != NULL)
1162 free (finfo.linenos);
1163 if (finfo.contents != NULL)
1164 free (finfo.contents);
1165 if (finfo.external_relocs != NULL)
1166 free (finfo.external_relocs);
1167 if (finfo.internal_relocs != NULL)
1168 free (finfo.internal_relocs);
1169 if (external_relocs != NULL)
1170 free (external_relocs);
1174 /* parse out a -heap <reserved>,<commit> line */
1177 dores_com (ptr, output_bfd, heap)
1182 if (coff_data(output_bfd)->pe)
1184 int val = strtoul (ptr, &ptr, 0);
1186 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1188 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1192 val = strtoul (ptr+1, &ptr, 0);
1194 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1196 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1202 static char *get_name(ptr, dst)
1209 while (*ptr && *ptr != ' ')
1215 /* Process any magic embedded commands in a section called .drectve */
1218 process_embedded_commands (output_bfd, info, abfd)
1220 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1223 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1230 copy = bfd_malloc (sec->_raw_size);
1233 if (! bfd_get_section_contents(abfd, sec, copy, (bfd_vma) 0, sec->_raw_size))
1238 e = copy + sec->_raw_size;
1239 for (s = copy; s < e ; )
1245 if (strncmp (s,"-attr", 5) == 0)
1257 s = get_name(s, &name);
1258 s = get_name(s, &attribs);
1278 asec = bfd_get_section_by_name (abfd, name);
1281 asec->flags |= SEC_CODE;
1283 asec->flags |= SEC_READONLY;
1286 else if (strncmp (s,"-heap", 5) == 0)
1288 s = dores_com (s+5, output_bfd, 1);
1290 else if (strncmp (s,"-stack", 6) == 0)
1292 s = dores_com (s+6, output_bfd, 0);
1301 /* Place a marker against all symbols which are used by relocations.
1302 This marker can be picked up by the 'do we skip this symbol ?'
1303 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1308 mark_relocs (finfo, input_bfd)
1309 struct coff_final_link_info * finfo;
1314 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1317 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1319 struct internal_reloc * internal_relocs;
1320 struct internal_reloc * irel;
1321 struct internal_reloc * irelend;
1323 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1)
1326 /* Read in the relocs. */
1327 internal_relocs = _bfd_coff_read_internal_relocs
1328 (input_bfd, a, false,
1329 finfo->external_relocs,
1330 finfo->info->relocateable,
1331 (finfo->info->relocateable
1332 ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1333 : finfo->internal_relocs)
1336 if (internal_relocs == NULL)
1339 irel = internal_relocs;
1340 irelend = irel + a->reloc_count;
1342 /* Place a mark in the sym_indices array (whose entries have
1343 been initialised to 0) for all of the symbols that are used
1344 in the relocation table. This will then be picked up in the
1347 for (; irel < irelend; irel++)
1349 finfo->sym_indices[ irel->r_symndx ] = -1;
1354 /* Link an input file into the linker output file. This function
1355 handles all the sections and relocations of the input file at once. */
1358 _bfd_coff_link_input_bfd (finfo, input_bfd)
1359 struct coff_final_link_info *finfo;
1362 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1363 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1365 unsigned int n_btmask = coff_data (input_bfd)->local_n_btmask;
1367 boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *,
1368 asection *, struct internal_reloc *,
1371 const char *strings;
1372 bfd_size_type syment_base;
1374 bfd_size_type isymesz;
1375 bfd_size_type osymesz;
1376 bfd_size_type linesz;
1379 struct internal_syment *isymp;
1382 unsigned long output_index;
1384 struct coff_link_hash_entry **sym_hash;
1387 /* Move all the symbols to the output file. */
1389 output_bfd = finfo->output_bfd;
1391 syment_base = obj_raw_syment_count (output_bfd);
1392 isymesz = bfd_coff_symesz (input_bfd);
1393 osymesz = bfd_coff_symesz (output_bfd);
1394 linesz = bfd_coff_linesz (input_bfd);
1395 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1398 if (! finfo->info->keep_memory)
1401 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1404 if (! _bfd_coff_get_external_symbols (input_bfd))
1407 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1408 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1409 isymp = finfo->internal_syms;
1410 secpp = finfo->sec_ptrs;
1411 indexp = finfo->sym_indices;
1412 output_index = syment_base;
1413 outsym = finfo->outsyms;
1415 if (coff_data (output_bfd)->pe)
1417 if (! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1421 /* If we are going to perform relocations and also strip/discard some symbols
1422 then we must make sure that we do not strip/discard those symbols that are
1423 going to be involved in the relocations */
1424 if (( finfo->info->strip != strip_none
1425 || finfo->info->discard != discard_none)
1426 && finfo->info->relocateable)
1428 /* mark the symbol array as 'not-used' */
1429 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1431 mark_relocs (finfo, input_bfd);
1434 while (esym < esym_end)
1436 struct internal_syment isym;
1437 enum coff_symbol_classification classification;
1440 boolean dont_skip_symbol;
1443 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1445 /* Make a copy of *isymp so that the relocate_section function
1446 always sees the original values. This is more reliable than
1447 always recomputing the symbol value even if we are stripping
1451 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1452 switch (classification)
1456 case COFF_SYMBOL_GLOBAL:
1457 case COFF_SYMBOL_PE_SECTION:
1458 case COFF_SYMBOL_LOCAL:
1459 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1461 case COFF_SYMBOL_COMMON:
1462 *secpp = bfd_com_section_ptr;
1464 case COFF_SYMBOL_UNDEFINED:
1465 *secpp = bfd_und_section_ptr;
1469 /* Extract the flag indicating if this symbol is used by a
1471 if ((finfo->info->strip != strip_none
1472 || finfo->info->discard != discard_none)
1473 && finfo->info->relocateable)
1474 dont_skip_symbol = *indexp;
1476 dont_skip_symbol = false;
1482 add = 1 + isym.n_numaux;
1484 /* If we are stripping all symbols, we want to skip this one. */
1485 if (finfo->info->strip == strip_all && ! dont_skip_symbol)
1490 switch (classification)
1494 case COFF_SYMBOL_GLOBAL:
1495 case COFF_SYMBOL_COMMON:
1496 case COFF_SYMBOL_PE_SECTION:
1497 /* This is a global symbol. Global symbols come at the
1498 end of the symbol table, so skip them for now.
1499 Locally defined function symbols, however, are an
1500 exception, and are not moved to the end. */
1502 if (! ISFCN (isym.n_type))
1506 case COFF_SYMBOL_UNDEFINED:
1507 /* Undefined symbols are left for the end. */
1512 case COFF_SYMBOL_LOCAL:
1513 /* This is a local symbol. Skip it if we are discarding
1515 if (finfo->info->discard == discard_all && ! dont_skip_symbol)
1521 #ifndef COFF_WITH_PE
1522 /* Skip section symbols for sections which are not going to be
1525 && isym.n_sclass == C_STAT
1526 && isym.n_type == T_NULL
1527 && isym.n_numaux > 0)
1529 if ((*secpp)->output_section == bfd_abs_section_ptr)
1534 /* If we stripping debugging symbols, and this is a debugging
1535 symbol, then skip it. FIXME: gas sets the section to N_ABS
1536 for some types of debugging symbols; I don't know if this is
1537 a bug or not. In any case, we handle it here. */
1539 && finfo->info->strip == strip_debugger
1540 && ! dont_skip_symbol
1541 && (isym.n_scnum == N_DEBUG
1542 || (isym.n_scnum == N_ABS
1543 && (isym.n_sclass == C_AUTO
1544 || isym.n_sclass == C_REG
1545 || isym.n_sclass == C_MOS
1546 || isym.n_sclass == C_MOE
1547 || isym.n_sclass == C_MOU
1548 || isym.n_sclass == C_ARG
1549 || isym.n_sclass == C_REGPARM
1550 || isym.n_sclass == C_FIELD
1551 || isym.n_sclass == C_EOS))))
1554 /* If some symbols are stripped based on the name, work out the
1555 name and decide whether to skip this symbol. */
1557 && (finfo->info->strip == strip_some
1558 || finfo->info->discard == discard_l))
1561 char buf[SYMNMLEN + 1];
1563 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1567 if (! dont_skip_symbol
1568 && ((finfo->info->strip == strip_some
1569 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1572 && finfo->info->discard == discard_l
1573 && bfd_is_local_label_name (input_bfd, name))))
1577 /* If this is an enum, struct, or union tag, see if we have
1578 already output an identical type. */
1580 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1581 && (isym.n_sclass == C_ENTAG
1582 || isym.n_sclass == C_STRTAG
1583 || isym.n_sclass == C_UNTAG)
1584 && isym.n_numaux == 1)
1587 char buf[SYMNMLEN + 1];
1588 struct coff_debug_merge_hash_entry *mh;
1589 struct coff_debug_merge_type *mt;
1590 union internal_auxent aux;
1591 struct coff_debug_merge_element **epp;
1592 bfd_byte *esl, *eslend;
1593 struct internal_syment *islp;
1596 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1600 /* Ignore fake names invented by compiler; treat them all as
1602 if (*name == '~' || *name == '.' || *name == '$'
1603 || (*name == bfd_get_symbol_leading_char (input_bfd)
1604 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1607 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1612 /* Allocate memory to hold type information. If this turns
1613 out to be a duplicate, we pass this address to
1615 amt = sizeof (struct coff_debug_merge_type);
1616 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
1619 mt->class = isym.n_sclass;
1621 /* Pick up the aux entry, which points to the end of the tag
1623 bfd_coff_swap_aux_in (input_bfd, (PTR) (esym + isymesz),
1624 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1627 /* Gather the elements. */
1628 epp = &mt->elements;
1629 mt->elements = NULL;
1631 esl = esym + 2 * isymesz;
1632 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1633 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1634 while (esl < eslend)
1636 const char *elename;
1637 char elebuf[SYMNMLEN + 1];
1640 bfd_coff_swap_sym_in (input_bfd, (PTR) esl, (PTR) islp);
1642 amt = sizeof (struct coff_debug_merge_element);
1643 *epp = ((struct coff_debug_merge_element *)
1644 bfd_alloc (input_bfd, amt));
1648 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1650 if (elename == NULL)
1653 amt = strlen (elename) + 1;
1654 name_copy = (char *) bfd_alloc (input_bfd, amt);
1655 if (name_copy == NULL)
1657 strcpy (name_copy, elename);
1659 (*epp)->name = name_copy;
1660 (*epp)->type = islp->n_type;
1662 if (islp->n_numaux >= 1
1663 && islp->n_type != T_NULL
1664 && islp->n_sclass != C_EOS)
1666 union internal_auxent eleaux;
1669 bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz),
1670 islp->n_type, islp->n_sclass, 0,
1671 islp->n_numaux, (PTR) &eleaux);
1672 indx = eleaux.x_sym.x_tagndx.l;
1674 /* FIXME: If this tagndx entry refers to a symbol
1675 defined later in this file, we just ignore it.
1676 Handling this correctly would be tedious, and may
1682 (bfd_byte *) obj_coff_external_syms (input_bfd))
1685 (*epp)->tagndx = finfo->sym_indices[indx];
1686 if ((*epp)->tagndx < 0)
1690 epp = &(*epp)->next;
1693 esl += (islp->n_numaux + 1) * isymesz;
1694 islp += islp->n_numaux + 1;
1697 /* See if we already have a definition which matches this
1698 type. We always output the type if it has no elements,
1700 if (mt->elements == NULL)
1701 bfd_release (input_bfd, (PTR) mt);
1704 struct coff_debug_merge_type *mtl;
1706 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1708 struct coff_debug_merge_element *me, *mel;
1710 if (mtl->class != mt->class)
1713 for (me = mt->elements, mel = mtl->elements;
1714 me != NULL && mel != NULL;
1715 me = me->next, mel = mel->next)
1717 if (strcmp (me->name, mel->name) != 0
1718 || me->type != mel->type
1719 || me->tagndx != mel->tagndx)
1723 if (me == NULL && mel == NULL)
1727 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1729 /* This is the first definition of this type. */
1730 mt->indx = output_index;
1731 mt->next = mh->types;
1736 /* This is a redefinition which can be merged. */
1737 bfd_release (input_bfd, (PTR) mt);
1738 *indexp = mtl->indx;
1739 add = (eslend - esym) / isymesz;
1745 /* We now know whether we are to skip this symbol or not. */
1748 /* Adjust the symbol in order to output it. */
1750 if (isym._n._n_n._n_zeroes == 0
1751 && isym._n._n_n._n_offset != 0)
1756 /* This symbol has a long name. Enter it in the string
1757 table we are building. Note that we do not check
1758 bfd_coff_symname_in_debug. That is only true for
1759 XCOFF, and XCOFF requires different linking code
1761 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1765 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1766 if (indx == (bfd_size_type) -1)
1768 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1771 switch (isym.n_sclass)
1787 /* The symbol value should not be modified. */
1791 if (obj_pe (input_bfd)
1792 && strcmp (isym.n_name, ".bf") != 0
1793 && isym.n_scnum > 0)
1795 /* For PE, .lf and .ef get their value left alone,
1796 while .bf gets relocated. However, they all have
1797 "real" section numbers, and need to be moved into
1799 isym.n_scnum = (*secpp)->output_section->target_index;
1804 case C_LABEL: /* Not completely sure about these 2 */
1813 /* Compute new symbol location. */
1814 if (isym.n_scnum > 0)
1816 isym.n_scnum = (*secpp)->output_section->target_index;
1817 isym.n_value += (*secpp)->output_offset;
1818 if (! obj_pe (input_bfd))
1819 isym.n_value -= (*secpp)->vma;
1820 if (! obj_pe (finfo->output_bfd))
1821 isym.n_value += (*secpp)->output_section->vma;
1826 /* The value of a C_FILE symbol is the symbol index of
1827 the next C_FILE symbol. The value of the last C_FILE
1828 symbol is the symbol index to the first external
1829 symbol (actually, coff_renumber_symbols does not get
1830 this right--it just sets the value of the last C_FILE
1831 symbol to zero--and nobody has ever complained about
1832 it). We try to get this right, below, just before we
1833 write the symbols out, but in the general case we may
1834 have to write the symbol out twice. */
1836 if (finfo->last_file_index != -1
1837 && finfo->last_file.n_value != (bfd_vma) output_index)
1839 /* We must correct the value of the last C_FILE
1841 finfo->last_file.n_value = output_index;
1842 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1844 /* The last C_FILE symbol is in this input file. */
1845 bfd_coff_swap_sym_out (output_bfd,
1846 (PTR) &finfo->last_file,
1847 (PTR) (finfo->outsyms
1848 + ((finfo->last_file_index
1856 /* We have already written out the last C_FILE
1857 symbol. We need to write it out again. We
1858 borrow *outsym temporarily. */
1859 bfd_coff_swap_sym_out (output_bfd,
1860 (PTR) &finfo->last_file,
1862 pos = obj_sym_filepos (output_bfd);
1863 pos += finfo->last_file_index * osymesz;
1864 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1865 || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
1870 finfo->last_file_index = output_index;
1871 finfo->last_file = isym;
1875 /* If doing task linking, convert normal global function symbols to
1876 static functions. */
1877 if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1878 isym.n_sclass = C_STAT;
1880 /* Output the symbol. */
1882 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1884 *indexp = output_index;
1889 struct coff_link_hash_entry *h;
1891 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1893 h = obj_coff_sym_hashes (input_bfd)[indx];
1896 /* This can happen if there were errors earlier in
1898 bfd_set_error (bfd_error_bad_value);
1901 h->indx = output_index;
1904 output_index += add;
1905 outsym += add * osymesz;
1908 esym += add * isymesz;
1912 for (--add; add > 0; --add)
1919 /* Fix up the aux entries. This must be done in a separate pass,
1920 because we don't know the correct symbol indices until we have
1921 already decided which symbols we are going to keep. */
1923 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1924 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1925 isymp = finfo->internal_syms;
1926 indexp = finfo->sym_indices;
1927 sym_hash = obj_coff_sym_hashes (input_bfd);
1928 outsym = finfo->outsyms;
1929 while (esym < esym_end)
1933 add = 1 + isymp->n_numaux;
1936 || (bfd_size_type) *indexp < syment_base)
1937 && (*sym_hash == NULL
1938 || (*sym_hash)->auxbfd != input_bfd))
1939 esym += add * isymesz;
1942 struct coff_link_hash_entry *h;
1950 /* The m68k-motorola-sysv assembler will sometimes
1951 generate two symbols with the same name, but only one
1952 will have aux entries. */
1953 BFD_ASSERT (isymp->n_numaux == 0
1954 || h->numaux == isymp->n_numaux);
1962 /* Handle the aux entries. This handling is based on
1963 coff_pointerize_aux. I don't know if it always correct. */
1964 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1966 union internal_auxent aux;
1967 union internal_auxent *auxp;
1973 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1974 isymp->n_sclass, i, isymp->n_numaux,
1979 if (isymp->n_sclass == C_FILE)
1981 /* If this is a long filename, we must put it in the
1983 if (auxp->x_file.x_n.x_zeroes == 0
1984 && auxp->x_file.x_n.x_offset != 0)
1986 const char *filename;
1989 BFD_ASSERT (auxp->x_file.x_n.x_offset
1990 >= STRING_SIZE_SIZE);
1991 if (strings == NULL)
1993 strings = _bfd_coff_read_string_table (input_bfd);
1994 if (strings == NULL)
1997 filename = strings + auxp->x_file.x_n.x_offset;
1998 indx = _bfd_stringtab_add (finfo->strtab, filename,
2000 if (indx == (bfd_size_type) -1)
2002 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
2005 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2009 if (ISFCN (isymp->n_type)
2010 || ISTAG (isymp->n_sclass)
2011 || isymp->n_sclass == C_BLOCK
2012 || isymp->n_sclass == C_FCN)
2014 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
2016 && indx < obj_raw_syment_count (input_bfd))
2018 /* We look forward through the symbol for
2019 the index of the next symbol we are going
2020 to include. I don't know if this is
2022 while ((finfo->sym_indices[indx] < 0
2023 || ((bfd_size_type) finfo->sym_indices[indx]
2025 && indx < obj_raw_syment_count (input_bfd))
2027 if (indx >= obj_raw_syment_count (input_bfd))
2028 indx = output_index;
2030 indx = finfo->sym_indices[indx];
2031 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2035 indx = auxp->x_sym.x_tagndx.l;
2036 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2040 symindx = finfo->sym_indices[indx];
2042 auxp->x_sym.x_tagndx.l = 0;
2044 auxp->x_sym.x_tagndx.l = symindx;
2047 /* The .bf symbols are supposed to be linked through
2048 the endndx field. We need to carry this list
2049 across object files. */
2052 && isymp->n_sclass == C_FCN
2053 && (isymp->_n._n_n._n_zeroes != 0
2054 || isymp->_n._n_n._n_offset == 0)
2055 && isymp->_n._n_name[0] == '.'
2056 && isymp->_n._n_name[1] == 'b'
2057 && isymp->_n._n_name[2] == 'f'
2058 && isymp->_n._n_name[3] == '\0')
2060 if (finfo->last_bf_index != -1)
2062 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2065 if ((bfd_size_type) finfo->last_bf_index
2070 /* The last .bf symbol is in this input
2071 file. This will only happen if the
2072 assembler did not set up the .bf
2073 endndx symbols correctly. */
2074 auxout = (PTR) (finfo->outsyms
2075 + ((finfo->last_bf_index
2078 bfd_coff_swap_aux_out (output_bfd,
2079 (PTR) &finfo->last_bf,
2089 /* We have already written out the last
2090 .bf aux entry. We need to write it
2091 out again. We borrow *outsym
2092 temporarily. FIXME: This case should
2094 bfd_coff_swap_aux_out (output_bfd,
2095 (PTR) &finfo->last_bf,
2100 pos = obj_sym_filepos (output_bfd);
2101 pos += finfo->last_bf_index * osymesz;
2102 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2103 || (bfd_bwrite (outsym, osymesz, output_bfd)
2109 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2110 finfo->last_bf_index = -1;
2113 /* The endndx field of this aux entry must
2114 be updated with the symbol number of the
2116 finfo->last_bf = *auxp;
2117 finfo->last_bf_index = (((outsym - finfo->outsyms)
2126 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
2127 isymp->n_sclass, i, isymp->n_numaux,
2141 /* Relocate the line numbers, unless we are stripping them. */
2142 if (finfo->info->strip == strip_none
2143 || finfo->info->strip == strip_some)
2145 for (o = input_bfd->sections; o != NULL; o = o->next)
2155 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2156 build_link_order in ldwrite.c will not have created a
2157 link order, which means that we will not have seen this
2158 input section in _bfd_coff_final_link, which means that
2159 we will not have allocated space for the line numbers of
2160 this section. I don't think line numbers can be
2161 meaningful for a section which does not have
2162 SEC_HAS_CONTENTS set, but, if they do, this must be
2164 if (o->lineno_count == 0
2165 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2168 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2169 || bfd_bread (finfo->linenos, linesz * o->lineno_count,
2170 input_bfd) != linesz * o->lineno_count)
2173 offset = o->output_section->vma + o->output_offset - o->vma;
2174 eline = finfo->linenos;
2175 oeline = finfo->linenos;
2176 elineend = eline + linesz * o->lineno_count;
2178 for (; eline < elineend; eline += linesz)
2180 struct internal_lineno iline;
2182 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
2184 if (iline.l_lnno != 0)
2185 iline.l_addr.l_paddr += offset;
2186 else if (iline.l_addr.l_symndx >= 0
2187 && ((unsigned long) iline.l_addr.l_symndx
2188 < obj_raw_syment_count (input_bfd)))
2192 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2196 /* These line numbers are attached to a symbol
2197 which we are stripping. We must discard the
2198 line numbers because reading them back with
2199 no associated symbol (or associating them all
2200 with symbol #0) will fail. We can't regain
2201 the space in the output file, but at least
2207 struct internal_syment is;
2208 union internal_auxent ia;
2210 /* Fix up the lnnoptr field in the aux entry of
2211 the symbol. It turns out that we can't do
2212 this when we modify the symbol aux entries,
2213 because gas sometimes screws up the lnnoptr
2214 field and makes it an offset from the start
2215 of the line numbers rather than an absolute
2217 bfd_coff_swap_sym_in (output_bfd,
2218 (PTR) (finfo->outsyms
2219 + ((indx - syment_base)
2222 if ((ISFCN (is.n_type)
2223 || is.n_sclass == C_BLOCK)
2224 && is.n_numaux >= 1)
2228 auxptr = (PTR) (finfo->outsyms
2229 + ((indx - syment_base + 1)
2231 bfd_coff_swap_aux_in (output_bfd, auxptr,
2232 is.n_type, is.n_sclass,
2233 0, is.n_numaux, (PTR) &ia);
2234 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2235 (o->output_section->line_filepos
2236 + o->output_section->lineno_count * linesz
2237 + eline - finfo->linenos);
2238 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
2239 is.n_type, is.n_sclass, 0,
2240 is.n_numaux, auxptr);
2246 iline.l_addr.l_symndx = indx;
2251 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline,
2257 pos = o->output_section->line_filepos;
2258 pos += o->output_section->lineno_count * linesz;
2259 amt = oeline - finfo->linenos;
2260 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2261 || bfd_bwrite (finfo->linenos, amt, output_bfd) != amt)
2264 o->output_section->lineno_count += amt / linesz;
2268 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2269 symbol will be the first symbol in the next input file. In the
2270 normal case, this will save us from writing out the C_FILE symbol
2272 if (finfo->last_file_index != -1
2273 && (bfd_size_type) finfo->last_file_index >= syment_base)
2275 finfo->last_file.n_value = output_index;
2276 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
2277 (PTR) (finfo->outsyms
2278 + ((finfo->last_file_index - syment_base)
2282 /* Write the modified symbols to the output file. */
2283 if (outsym > finfo->outsyms)
2288 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2289 amt = outsym - finfo->outsyms;
2290 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2291 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
2294 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2295 + (outsym - finfo->outsyms) / osymesz)
2298 obj_raw_syment_count (output_bfd) = output_index;
2301 /* Relocate the contents of each section. */
2302 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2303 for (o = input_bfd->sections; o != NULL; o = o->next)
2306 struct coff_section_tdata *secdata;
2308 if (! o->linker_mark)
2310 /* This section was omitted from the link. */
2314 if ((o->flags & SEC_HAS_CONTENTS) == 0
2315 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
2317 if ((o->flags & SEC_RELOC) != 0
2318 && o->reloc_count != 0)
2320 ((*_bfd_error_handler)
2321 (_("%s: relocs in section `%s', but it has no contents"),
2322 bfd_archive_filename (input_bfd),
2323 bfd_get_section_name (input_bfd, o)));
2324 bfd_set_error (bfd_error_no_contents);
2331 secdata = coff_section_data (input_bfd, o);
2332 if (secdata != NULL && secdata->contents != NULL)
2333 contents = secdata->contents;
2336 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2337 (file_ptr) 0, o->_raw_size))
2339 contents = finfo->contents;
2342 if ((o->flags & SEC_RELOC) != 0)
2345 struct internal_reloc *internal_relocs;
2346 struct internal_reloc *irel;
2348 /* Read in the relocs. */
2349 target_index = o->output_section->target_index;
2350 internal_relocs = (_bfd_coff_read_internal_relocs
2351 (input_bfd, o, false, finfo->external_relocs,
2352 finfo->info->relocateable,
2353 (finfo->info->relocateable
2354 ? (finfo->section_info[target_index].relocs
2355 + o->output_section->reloc_count)
2356 : finfo->internal_relocs)));
2357 if (internal_relocs == NULL)
2360 /* Call processor specific code to relocate the section
2362 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2366 finfo->internal_syms,
2370 if (finfo->info->relocateable)
2373 struct internal_reloc *irelend;
2374 struct coff_link_hash_entry **rel_hash;
2376 offset = o->output_section->vma + o->output_offset - o->vma;
2377 irel = internal_relocs;
2378 irelend = irel + o->reloc_count;
2379 rel_hash = (finfo->section_info[target_index].rel_hashes
2380 + o->output_section->reloc_count);
2381 for (; irel < irelend; irel++, rel_hash++)
2383 struct coff_link_hash_entry *h;
2388 /* Adjust the reloc address and symbol index. */
2390 irel->r_vaddr += offset;
2392 if (irel->r_symndx == -1)
2397 if (! (*adjust_symndx) (output_bfd, finfo->info,
2405 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2408 /* This is a global symbol. */
2410 irel->r_symndx = h->indx;
2413 /* This symbol is being written at the end
2414 of the file, and we do not yet know the
2415 symbol index. We save the pointer to the
2416 hash table entry in the rel_hash list.
2417 We set the indx field to -2 to indicate
2418 that this symbol must not be stripped. */
2427 indx = finfo->sym_indices[irel->r_symndx];
2429 irel->r_symndx = indx;
2432 struct internal_syment *is;
2434 char buf[SYMNMLEN + 1];
2436 /* This reloc is against a symbol we are
2437 stripping. This should have been handled
2438 by the 'dont_skip_symbol' code in the while
2439 loop at the top of this function. */
2441 is = finfo->internal_syms + irel->r_symndx;
2443 name = (_bfd_coff_internal_syment_name
2444 (input_bfd, is, buf));
2448 if (! ((*finfo->info->callbacks->unattached_reloc)
2449 (finfo->info, name, input_bfd, o,
2456 o->output_section->reloc_count += o->reloc_count;
2460 /* Write out the modified section contents. */
2461 if (secdata == NULL || secdata->stab_info == NULL)
2463 file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
2464 bfd_size_type amt = (o->_cooked_size != 0
2465 ? o->_cooked_size : o->_raw_size);
2466 if (! bfd_set_section_contents (output_bfd, o->output_section,
2467 contents, loc, amt))
2472 if (! (_bfd_write_section_stabs
2473 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2474 o, &secdata->stab_info, contents)))
2479 if (! finfo->info->keep_memory)
2481 if (! _bfd_coff_free_symbols (input_bfd))
2488 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2491 _bfd_coff_write_global_sym (h, data)
2492 struct coff_link_hash_entry *h;
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->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 overriden by a strong one,
2603 turn it into an external symbol when not building a
2604 shared or relocateable object. */
2605 if (! finfo->info->shared
2606 && ! finfo->info->relocateable
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, (PTR) &isym, (PTR) 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->_cooked_size != 0
2657 /* For PE, an overflow on the final link reportedly does
2658 not matter. FIXME: Why not? */
2660 if (sec->reloc_count > 0xffff
2661 && (! obj_pe (output_bfd)
2662 || finfo->info->relocateable))
2663 (*_bfd_error_handler)
2664 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2665 bfd_get_filename (output_bfd),
2666 bfd_get_section_name (output_bfd, sec),
2669 if (sec->lineno_count > 0xffff
2670 && (! obj_pe (output_bfd)
2671 || finfo->info->relocateable))
2672 (*_bfd_error_handler)
2673 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2674 bfd_get_filename (output_bfd),
2675 bfd_get_section_name (output_bfd, sec),
2678 auxp->x_scn.x_nreloc = sec->reloc_count;
2679 auxp->x_scn.x_nlinno = sec->lineno_count;
2680 auxp->x_scn.x_checksum = 0;
2681 auxp->x_scn.x_associated = 0;
2682 auxp->x_scn.x_comdat = 0;
2686 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isym.n_type,
2687 isym.n_sclass, (int) i, isym.n_numaux,
2688 (PTR) finfo->outsyms);
2689 if (bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
2691 finfo->failed = true;
2694 ++obj_raw_syment_count (output_bfd);
2700 /* Write out task global symbols, converting them to statics. Called
2701 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2702 the dirty work, if the symbol we are processing needs conversion. */
2705 _bfd_coff_write_task_globals (h, data)
2706 struct coff_link_hash_entry *h;
2709 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2710 boolean rtnval = true;
2711 boolean save_global_to_static;
2713 if (h->root.type == bfd_link_hash_warning)
2714 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2718 switch (h->root.type)
2720 case bfd_link_hash_defined:
2721 case bfd_link_hash_defweak:
2722 save_global_to_static = finfo->global_to_static;
2723 finfo->global_to_static = true;
2724 rtnval = _bfd_coff_write_global_sym (h, data);
2725 finfo->global_to_static = save_global_to_static;
2734 /* Handle a link order which is supposed to generate a reloc. */
2737 _bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2739 struct coff_final_link_info *finfo;
2740 asection *output_section;
2741 struct bfd_link_order *link_order;
2743 reloc_howto_type *howto;
2744 struct internal_reloc *irel;
2745 struct coff_link_hash_entry **rel_hash_ptr;
2747 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2750 bfd_set_error (bfd_error_bad_value);
2754 if (link_order->u.reloc.p->addend != 0)
2758 bfd_reloc_status_type rstat;
2762 size = bfd_get_reloc_size (howto);
2763 buf = (bfd_byte *) bfd_zmalloc (size);
2767 rstat = _bfd_relocate_contents (howto, output_bfd,
2768 (bfd_vma) link_order->u.reloc.p->addend,\
2775 case bfd_reloc_outofrange:
2777 case bfd_reloc_overflow:
2778 if (! ((*finfo->info->callbacks->reloc_overflow)
2780 (link_order->type == bfd_section_reloc_link_order
2781 ? bfd_section_name (output_bfd,
2782 link_order->u.reloc.p->u.section)
2783 : link_order->u.reloc.p->u.name),
2784 howto->name, link_order->u.reloc.p->addend,
2785 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2792 loc = link_order->offset * bfd_octets_per_byte (output_bfd);
2793 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2800 /* Store the reloc information in the right place. It will get
2801 swapped and written out at the end of the final_link routine. */
2803 irel = (finfo->section_info[output_section->target_index].relocs
2804 + output_section->reloc_count);
2805 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2806 + output_section->reloc_count);
2808 memset (irel, 0, sizeof (struct internal_reloc));
2809 *rel_hash_ptr = NULL;
2811 irel->r_vaddr = output_section->vma + link_order->offset;
2813 if (link_order->type == bfd_section_reloc_link_order)
2815 /* We need to somehow locate a symbol in the right section. The
2816 symbol must either have a value of zero, or we must adjust
2817 the addend by the value of the symbol. FIXME: Write this
2818 when we need it. The old linker couldn't handle this anyhow. */
2820 *rel_hash_ptr = NULL;
2825 struct coff_link_hash_entry *h;
2827 h = ((struct coff_link_hash_entry *)
2828 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2829 link_order->u.reloc.p->u.name,
2830 false, false, true));
2834 irel->r_symndx = h->indx;
2837 /* Set the index to -2 to force this symbol to get
2846 if (! ((*finfo->info->callbacks->unattached_reloc)
2847 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2848 (asection *) NULL, (bfd_vma) 0)))
2854 /* FIXME: Is this always right? */
2855 irel->r_type = howto->type;
2857 /* r_size is only used on the RS/6000, which needs its own linker
2858 routines anyhow. r_extern is only used for ECOFF. */
2860 /* FIXME: What is the right value for r_offset? Is zero OK? */
2862 ++output_section->reloc_count;
2867 /* A basic reloc handling routine which may be used by processors with
2871 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2872 input_section, contents, relocs, syms,
2875 struct bfd_link_info *info;
2877 asection *input_section;
2879 struct internal_reloc *relocs;
2880 struct internal_syment *syms;
2881 asection **sections;
2883 struct internal_reloc *rel;
2884 struct internal_reloc *relend;
2887 relend = rel + input_section->reloc_count;
2888 for (; rel < relend; rel++)
2891 struct coff_link_hash_entry *h;
2892 struct internal_syment *sym;
2895 reloc_howto_type *howto;
2896 bfd_reloc_status_type rstat;
2898 symndx = rel->r_symndx;
2906 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2908 (*_bfd_error_handler)
2909 ("%s: illegal symbol index %ld in relocs",
2910 bfd_archive_filename (input_bfd), symndx);
2915 h = obj_coff_sym_hashes (input_bfd)[symndx];
2916 sym = syms + symndx;
2919 /* COFF treats common symbols in one of two ways. Either the
2920 size of the symbol is included in the section contents, or it
2921 is not. We assume that the size is not included, and force
2922 the rtype_to_howto function to adjust the addend as needed. */
2924 if (sym != NULL && sym->n_scnum != 0)
2925 addend = - sym->n_value;
2929 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2934 /* If we are doing a relocateable link, then we can just ignore
2935 a PC relative reloc that is pcrel_offset. It will already
2936 have the correct value. If this is not a relocateable link,
2937 then we should ignore the symbol value. */
2938 if (howto->pc_relative && howto->pcrel_offset)
2940 if (info->relocateable)
2942 if (sym != NULL && sym->n_scnum != 0)
2943 addend += sym->n_value;
2954 sec = bfd_abs_section_ptr;
2959 sec = sections[symndx];
2960 val = (sec->output_section->vma
2961 + sec->output_offset
2963 if (! obj_pe (input_bfd))
2969 if (h->root.type == bfd_link_hash_defined
2970 || h->root.type == bfd_link_hash_defweak)
2974 sec = h->root.u.def.section;
2975 val = (h->root.u.def.value
2976 + sec->output_section->vma
2977 + sec->output_offset);
2980 else if (h->root.type == bfd_link_hash_undefweak)
2983 else if (! info->relocateable)
2985 if (! ((*info->callbacks->undefined_symbol)
2986 (info, h->root.root.string, input_bfd, input_section,
2987 rel->r_vaddr - input_section->vma, true)))
2992 if (info->base_file)
2994 /* Emit a reloc if the backend thinks it needs it. */
2995 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
2997 /* Relocation to a symbol in a section which isn't
2998 absolute. We output the address here to a file.
2999 This file is then read by dlltool when generating the
3000 reloc section. Note that the base file is not
3001 portable between systems. We write out a long here,
3002 and dlltool reads in a long. */
3003 long addr = (rel->r_vaddr
3004 - input_section->vma
3005 + input_section->output_offset
3006 + input_section->output_section->vma);
3007 if (coff_data (output_bfd)->pe)
3008 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3009 if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
3012 bfd_set_error (bfd_error_system_call);
3018 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3020 rel->r_vaddr - input_section->vma,
3029 case bfd_reloc_outofrange:
3030 (*_bfd_error_handler)
3031 (_("%s: bad reloc address 0x%lx in section `%s'"),
3032 bfd_archive_filename (input_bfd),
3033 (unsigned long) rel->r_vaddr,
3034 bfd_get_section_name (input_bfd, input_section));
3036 case bfd_reloc_overflow:
3039 char buf[SYMNMLEN + 1];
3044 name = h->root.root.string;
3047 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3052 if (! ((*info->callbacks->reloc_overflow)
3053 (info, name, howto->name, (bfd_vma) 0, input_bfd,
3054 input_section, rel->r_vaddr - input_section->vma)))