1 /* COFF specific linker code.
2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 /* This file contains the COFF backend linker code. */
28 #include "coff/internal.h"
30 #include "safe-ctype.h"
32 static bool coff_link_add_object_symbols (bfd
*, struct bfd_link_info
*);
33 static bool coff_link_check_archive_element
34 (bfd
*, struct bfd_link_info
*, struct bfd_link_hash_entry
*, const char *,
36 static bool coff_link_add_symbols (bfd
*, struct bfd_link_info
*);
38 /* Return TRUE if SYM is a weak, external symbol. */
39 #define IS_WEAK_EXTERNAL(abfd, sym) \
40 ((sym).n_sclass == C_WEAKEXT \
41 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
43 /* Return TRUE if SYM is an external symbol. */
44 #define IS_EXTERNAL(abfd, sym) \
45 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
47 /* Define macros so that the ISFCN, et. al., macros work correctly.
48 These macros are defined in include/coff/internal.h in terms of
49 N_TMASK, etc. These definitions require a user to define local
50 variables with the appropriate names, and with values from the
51 coff_data (abfd) structure. */
53 #define N_TMASK n_tmask
54 #define N_BTSHFT n_btshft
55 #define N_BTMASK n_btmask
57 /* Create an entry in a COFF linker hash table. */
59 struct bfd_hash_entry
*
60 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
61 struct bfd_hash_table
*table
,
64 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
66 /* Allocate the structure if it has not already been allocated by a
68 if (ret
== (struct coff_link_hash_entry
*) NULL
)
69 ret
= ((struct coff_link_hash_entry
*)
70 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
71 if (ret
== (struct coff_link_hash_entry
*) NULL
)
72 return (struct bfd_hash_entry
*) ret
;
74 /* Call the allocation method of the superclass. */
75 ret
= ((struct coff_link_hash_entry
*)
76 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
78 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
80 /* Set local fields. */
83 ret
->symbol_class
= C_NULL
;
89 return (struct bfd_hash_entry
*) ret
;
92 /* Initialize a COFF linker hash table. */
95 _bfd_coff_link_hash_table_init (struct coff_link_hash_table
*table
,
97 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
98 struct bfd_hash_table
*,
100 unsigned int entsize
)
102 memset (&table
->stab_info
, 0, sizeof (table
->stab_info
));
103 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
106 /* Create a COFF linker hash table. */
108 struct bfd_link_hash_table
*
109 _bfd_coff_link_hash_table_create (bfd
*abfd
)
111 struct coff_link_hash_table
*ret
;
112 size_t amt
= sizeof (struct coff_link_hash_table
);
114 ret
= (struct coff_link_hash_table
*) bfd_malloc (amt
);
118 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
119 _bfd_coff_link_hash_newfunc
,
120 sizeof (struct coff_link_hash_entry
)))
123 return (struct bfd_link_hash_table
*) NULL
;
128 /* Create an entry in a COFF debug merge hash table. */
130 struct bfd_hash_entry
*
131 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry
*entry
,
132 struct bfd_hash_table
*table
,
135 struct coff_debug_merge_hash_entry
*ret
=
136 (struct coff_debug_merge_hash_entry
*) entry
;
138 /* Allocate the structure if it has not already been allocated by a
140 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
141 ret
= ((struct coff_debug_merge_hash_entry
*)
142 bfd_hash_allocate (table
,
143 sizeof (struct coff_debug_merge_hash_entry
)));
144 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
145 return (struct bfd_hash_entry
*) ret
;
147 /* Call the allocation method of the superclass. */
148 ret
= ((struct coff_debug_merge_hash_entry
*)
149 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
150 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
152 /* Set local fields. */
156 return (struct bfd_hash_entry
*) ret
;
159 /* Given a COFF BFD, add symbols to the global hash table as
163 _bfd_coff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
165 switch (bfd_get_format (abfd
))
168 return coff_link_add_object_symbols (abfd
, info
);
170 return _bfd_generic_link_add_archive_symbols
171 (abfd
, info
, coff_link_check_archive_element
);
173 bfd_set_error (bfd_error_wrong_format
);
178 /* Add symbols from a COFF object file. */
181 coff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
183 if (! _bfd_coff_get_external_symbols (abfd
))
185 if (! coff_link_add_symbols (abfd
, info
))
188 if (! info
->keep_memory
189 && ! _bfd_coff_free_symbols (abfd
))
195 /* Check a single archive element to see if we need to include it in
196 the link. *PNEEDED is set according to whether this element is
197 needed in the link or not. This is called via
198 _bfd_generic_link_add_archive_symbols. */
201 coff_link_check_archive_element (bfd
*abfd
,
202 struct bfd_link_info
*info
,
203 struct bfd_link_hash_entry
*h
,
209 /* PR 22369 - Skip non COFF objects in the archive. */
210 if (! bfd_family_coff (abfd
))
213 /* We are only interested in symbols that are currently undefined.
214 If a symbol is currently known to be common, COFF linkers do not
215 bring in an object file which defines it. */
216 if (h
->type
!= bfd_link_hash_undefined
)
219 /* If the archive element has already been loaded then one
220 of the symbols defined by that element might have been
221 made undefined due to being in a discarded section. */
222 if (((struct coff_link_hash_entry
*) h
)->indx
== -3)
225 /* Include this element? */
226 if (!(*info
->callbacks
->add_archive_element
) (info
, abfd
, name
, &abfd
))
230 return bfd_link_add_symbols (abfd
, info
);
233 /* Add all the symbols from an object file to the hash table. */
236 coff_link_add_symbols (bfd
*abfd
,
237 struct bfd_link_info
*info
)
239 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
240 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
241 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
244 bfd_size_type symcount
;
245 struct coff_link_hash_entry
**sym_hash
;
246 bfd_size_type symesz
;
251 symcount
= obj_raw_syment_count (abfd
);
254 return true; /* Nothing to do. */
256 /* Keep the symbols during this function, in case the linker needs
257 to read the generic symbols in order to report an error message. */
258 keep_syms
= obj_coff_keep_syms (abfd
);
259 obj_coff_keep_syms (abfd
) = true;
261 if (info
->keep_memory
)
262 default_copy
= false;
266 /* We keep a list of the linker hash table entries that correspond
267 to particular symbols. */
268 amt
= symcount
* sizeof (struct coff_link_hash_entry
*);
269 sym_hash
= (struct coff_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
270 if (sym_hash
== NULL
)
272 obj_coff_sym_hashes (abfd
) = sym_hash
;
274 symesz
= bfd_coff_symesz (abfd
);
275 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
276 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
277 esym_end
= esym
+ symcount
* symesz
;
278 while (esym
< esym_end
)
280 struct internal_syment sym
;
281 enum coff_symbol_classification classification
;
284 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
286 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
287 if (classification
!= COFF_SYMBOL_LOCAL
)
290 char buf
[SYMNMLEN
+ 1];
295 bool discarded
= false;
297 /* This symbol is externally visible. */
299 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
303 /* We must copy the name into memory if we got it from the
304 syment itself, rather than the string table. */
306 if (sym
._n
._n_n
._n_zeroes
!= 0
307 || sym
._n
._n_n
._n_offset
== 0)
312 switch (classification
)
317 case COFF_SYMBOL_GLOBAL
:
318 flags
= BSF_EXPORT
| BSF_GLOBAL
;
319 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
320 if (discarded_section (section
))
323 section
= bfd_und_section_ptr
;
325 else if (! obj_pe (abfd
))
326 value
-= section
->vma
;
329 case COFF_SYMBOL_UNDEFINED
:
331 section
= bfd_und_section_ptr
;
334 case COFF_SYMBOL_COMMON
:
336 section
= bfd_com_section_ptr
;
339 case COFF_SYMBOL_PE_SECTION
:
340 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
341 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
342 if (discarded_section (section
))
343 section
= bfd_und_section_ptr
;
347 if (IS_WEAK_EXTERNAL (abfd
, sym
))
352 /* In the PE format, section symbols actually refer to the
353 start of the output section. We handle them specially
355 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
357 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
358 name
, false, copy
, false);
359 if (*sym_hash
!= NULL
)
361 if (((*sym_hash
)->coff_link_hash_flags
362 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
363 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
364 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
366 (_("warning: symbol `%s' is both section and non-section"),
373 /* The Microsoft Visual C compiler does string pooling by
374 hashing the constants to an internal symbol name, and
375 relying on the linker comdat support to discard
376 duplicate names. However, if one string is a literal and
377 one is a data initializer, one will end up in the .data
378 section and one will end up in the .rdata section. The
379 Microsoft linker will combine them into the .data
380 section, which seems to be wrong since it might cause the
383 As long as there are no external references to the
384 symbols, which there shouldn't be, we can treat the .data
385 and .rdata instances as separate symbols. The comdat
386 code in the linker will do the appropriate merging. Here
387 we avoid getting a multiple definition error for one of
388 these special symbols.
390 FIXME: I don't think this will work in the case where
391 there are two object files which use the constants as a
392 literal and two object files which use it as a data
393 initializer. One or the other of the second object files
394 is going to wind up with an inappropriate reference. */
396 && (classification
== COFF_SYMBOL_GLOBAL
397 || classification
== COFF_SYMBOL_PE_SECTION
)
398 && coff_section_data (abfd
, section
) != NULL
399 && coff_section_data (abfd
, section
)->comdat
!= NULL
400 && startswith (name
, "??_")
401 && strcmp (name
, coff_section_data (abfd
, section
)->comdat
->name
) == 0)
403 if (*sym_hash
== NULL
)
404 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
405 name
, false, copy
, false);
406 if (*sym_hash
!= NULL
407 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
408 && coff_section_data (abfd
, (*sym_hash
)->root
.u
.def
.section
)->comdat
!= NULL
409 && strcmp (coff_section_data (abfd
, (*sym_hash
)->root
.u
.def
.section
)->comdat
->name
,
410 coff_section_data (abfd
, section
)->comdat
->name
) == 0)
416 if (! (bfd_coff_link_add_one_symbol
417 (info
, abfd
, name
, flags
, section
, value
,
418 (const char *) NULL
, copy
, false,
419 (struct bfd_link_hash_entry
**) sym_hash
)))
423 (*sym_hash
)->indx
= -3;
426 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
427 (*sym_hash
)->coff_link_hash_flags
|=
428 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
430 /* Limit the alignment of a common symbol to the possible
431 alignment of a section. There is no point to permitting
432 a higher alignment for a common symbol: we can not
433 guarantee it, and it may cause us to allocate extra space
434 in the common section. */
435 if (section
== bfd_com_section_ptr
436 && (*sym_hash
)->root
.type
== bfd_link_hash_common
437 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
438 > bfd_coff_default_section_alignment_power (abfd
)))
439 (*sym_hash
)->root
.u
.c
.p
->alignment_power
440 = bfd_coff_default_section_alignment_power (abfd
);
442 if (bfd_get_flavour (info
->output_bfd
) == bfd_get_flavour (abfd
))
444 /* If we don't have any symbol information currently in
445 the hash table, or if we are looking at a symbol
446 definition, then update the symbol class and type in
448 if (((*sym_hash
)->symbol_class
== C_NULL
449 && (*sym_hash
)->type
== T_NULL
)
452 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
453 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
455 (*sym_hash
)->symbol_class
= sym
.n_sclass
;
456 if (sym
.n_type
!= T_NULL
)
458 /* We want to warn if the type changed, but not
459 if it changed from an unspecified type.
460 Testing the whole type byte may work, but the
461 change from (e.g.) a function of unspecified
462 type to function of known type also wants to
464 if ((*sym_hash
)->type
!= T_NULL
465 && (*sym_hash
)->type
!= sym
.n_type
466 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
467 && (BTYPE ((*sym_hash
)->type
) == T_NULL
468 || BTYPE (sym
.n_type
) == T_NULL
)))
470 /* xgettext: c-format */
471 (_("warning: type of symbol `%s' changed"
472 " from %d to %d in %pB"),
473 name
, (*sym_hash
)->type
, sym
.n_type
, abfd
);
475 /* We don't want to change from a meaningful
476 base type to a null one, but if we know
477 nothing, take what little we might now know. */
478 if (BTYPE (sym
.n_type
) != T_NULL
479 || (*sym_hash
)->type
== T_NULL
)
480 (*sym_hash
)->type
= sym
.n_type
;
482 (*sym_hash
)->auxbfd
= abfd
;
483 if (sym
.n_numaux
!= 0)
485 union internal_auxent
*alloc
;
488 union internal_auxent
*iaux
;
490 (*sym_hash
)->numaux
= sym
.n_numaux
;
491 alloc
= ((union internal_auxent
*)
492 bfd_hash_allocate (&info
->hash
->table
,
494 * sizeof (*alloc
))));
497 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
499 i
++, eaux
+= symesz
, iaux
++)
500 bfd_coff_swap_aux_in (abfd
, eaux
, sym
.n_type
,
501 sym
.n_sclass
, (int) i
,
503 (*sym_hash
)->aux
= alloc
;
508 if (classification
== COFF_SYMBOL_PE_SECTION
509 && (*sym_hash
)->numaux
!= 0)
511 /* Some PE sections (such as .bss) have a zero size in
512 the section header, but a non-zero size in the AUX
513 record. Correct that here.
515 FIXME: This is not at all the right place to do this.
516 For example, it won't help objdump. This needs to be
517 done when we swap in the section header. */
518 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
519 if (section
->size
== 0)
520 section
->size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
522 /* FIXME: We could test whether the section sizes
523 matches the size in the aux entry, but apparently
524 that sometimes fails unexpectedly. */
528 esym
+= (sym
.n_numaux
+ 1) * symesz
;
529 sym_hash
+= sym
.n_numaux
+ 1;
532 /* If this is a non-traditional, non-relocatable link, try to
533 optimize the handling of any .stab/.stabstr sections. */
534 if (! bfd_link_relocatable (info
)
535 && ! info
->traditional_format
536 && bfd_get_flavour (info
->output_bfd
) == bfd_get_flavour (abfd
)
537 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
541 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
545 bfd_size_type string_offset
= 0;
548 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
549 if (startswith (stab
->name
, ".stab")
551 || (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6]))))
553 struct coff_link_hash_table
*table
;
554 struct coff_section_tdata
*secdata
555 = coff_section_data (abfd
, stab
);
559 amt
= sizeof (struct coff_section_tdata
);
560 stab
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
561 if (stab
->used_by_bfd
== NULL
)
563 secdata
= coff_section_data (abfd
, stab
);
566 table
= coff_hash_table (info
);
568 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
577 obj_coff_keep_syms (abfd
) = keep_syms
;
582 obj_coff_keep_syms (abfd
) = keep_syms
;
586 /* Do the final link step. */
589 _bfd_coff_final_link (bfd
*abfd
,
590 struct bfd_link_info
*info
)
592 bfd_size_type symesz
;
593 struct coff_final_link_info flaginfo
;
594 bool debug_merge_allocated
;
595 bool long_section_names
;
597 struct bfd_link_order
*p
;
598 bfd_size_type max_sym_count
;
599 bfd_size_type max_lineno_count
;
600 bfd_size_type max_reloc_count
;
601 bfd_size_type max_output_reloc_count
;
602 bfd_size_type max_contents_size
;
603 file_ptr rel_filepos
;
605 file_ptr line_filepos
;
608 bfd_byte
*external_relocs
= NULL
;
609 char strbuf
[STRING_SIZE_SIZE
];
612 symesz
= bfd_coff_symesz (abfd
);
614 flaginfo
.info
= info
;
615 flaginfo
.output_bfd
= abfd
;
616 flaginfo
.strtab
= NULL
;
617 flaginfo
.section_info
= NULL
;
618 flaginfo
.last_file_index
= -1;
619 flaginfo
.last_bf_index
= -1;
620 flaginfo
.internal_syms
= NULL
;
621 flaginfo
.sec_ptrs
= NULL
;
622 flaginfo
.sym_indices
= NULL
;
623 flaginfo
.outsyms
= NULL
;
624 flaginfo
.linenos
= NULL
;
625 flaginfo
.contents
= NULL
;
626 flaginfo
.external_relocs
= NULL
;
627 flaginfo
.internal_relocs
= NULL
;
628 flaginfo
.global_to_static
= false;
629 debug_merge_allocated
= false;
631 coff_data (abfd
)->link_info
= info
;
633 flaginfo
.strtab
= _bfd_stringtab_init ();
634 if (flaginfo
.strtab
== NULL
)
637 if (! coff_debug_merge_hash_table_init (&flaginfo
.debug_merge
))
639 debug_merge_allocated
= true;
641 /* Compute the file positions for all the sections. */
642 if (! abfd
->output_has_begun
)
644 if (! bfd_coff_compute_section_file_positions (abfd
))
648 /* Count the line numbers and relocation entries required for the
649 output file. Set the file positions for the relocs. */
650 rel_filepos
= obj_relocbase (abfd
);
651 relsz
= bfd_coff_relsz (abfd
);
652 max_contents_size
= 0;
653 max_lineno_count
= 0;
656 long_section_names
= false;
657 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
661 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
663 if (p
->type
== bfd_indirect_link_order
)
667 sec
= p
->u
.indirect
.section
;
669 /* Mark all sections which are to be included in the
670 link. This will normally be every section. We need
671 to do this so that we can identify any sections which
672 the linker has decided to not include. */
673 sec
->linker_mark
= true;
675 if (info
->strip
== strip_none
676 || info
->strip
== strip_some
)
677 o
->lineno_count
+= sec
->lineno_count
;
679 if (bfd_link_relocatable (info
))
680 o
->reloc_count
+= sec
->reloc_count
;
682 if (sec
->rawsize
> max_contents_size
)
683 max_contents_size
= sec
->rawsize
;
684 if (sec
->size
> max_contents_size
)
685 max_contents_size
= sec
->size
;
686 if (sec
->lineno_count
> max_lineno_count
)
687 max_lineno_count
= sec
->lineno_count
;
688 if (sec
->reloc_count
> max_reloc_count
)
689 max_reloc_count
= sec
->reloc_count
;
691 else if (bfd_link_relocatable (info
)
692 && (p
->type
== bfd_section_reloc_link_order
693 || p
->type
== bfd_symbol_reloc_link_order
))
696 if (o
->reloc_count
== 0)
700 o
->flags
|= SEC_RELOC
;
701 o
->rel_filepos
= rel_filepos
;
702 rel_filepos
+= o
->reloc_count
* relsz
;
703 /* In PE COFF, if there are at least 0xffff relocations an
704 extra relocation will be written out to encode the count. */
705 if ((obj_pe (abfd
) || obj_go32 (abfd
)) && o
->reloc_count
>= 0xffff)
706 rel_filepos
+= relsz
;
709 if (bfd_coff_long_section_names (abfd
)
710 && strlen (o
->name
) > SCNNMLEN
)
712 /* This section has a long name which must go in the string
713 table. This must correspond to the code in
714 coff_write_object_contents which puts the string index
715 into the s_name field of the section header. That is why
716 we pass hash as FALSE. */
717 if (_bfd_stringtab_add (flaginfo
.strtab
, o
->name
, false, false)
718 == (bfd_size_type
) -1)
720 long_section_names
= true;
724 /* If doing a relocatable link, allocate space for the pointers we
726 if (bfd_link_relocatable (info
))
730 /* We use section_count + 1, rather than section_count, because
731 the target_index fields are 1 based. */
732 amt
= abfd
->section_count
+ 1;
733 amt
*= sizeof (struct coff_link_section_info
);
734 flaginfo
.section_info
= (struct coff_link_section_info
*) bfd_malloc (amt
);
735 if (flaginfo
.section_info
== NULL
)
737 for (i
= 0; i
<= abfd
->section_count
; i
++)
739 flaginfo
.section_info
[i
].relocs
= NULL
;
740 flaginfo
.section_info
[i
].rel_hashes
= NULL
;
744 /* We now know the size of the relocs, so we can determine the file
745 positions of the line numbers. */
746 line_filepos
= rel_filepos
;
747 linesz
= bfd_coff_linesz (abfd
);
748 max_output_reloc_count
= 0;
749 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
751 if (o
->lineno_count
== 0)
755 o
->line_filepos
= line_filepos
;
756 line_filepos
+= o
->lineno_count
* linesz
;
759 if (o
->reloc_count
!= 0)
761 /* We don't know the indices of global symbols until we have
762 written out all the local symbols. For each section in
763 the output file, we keep an array of pointers to hash
764 table entries. Each entry in the array corresponds to a
765 reloc. When we find a reloc against a global symbol, we
766 set the corresponding entry in this array so that we can
767 fix up the symbol index after we have written out all the
770 Because of this problem, we also keep the relocs in
771 memory until the end of the link. This wastes memory,
772 but only when doing a relocatable link, which is not the
774 BFD_ASSERT (bfd_link_relocatable (info
));
775 amt
= o
->reloc_count
;
776 amt
*= sizeof (struct internal_reloc
);
777 flaginfo
.section_info
[o
->target_index
].relocs
=
778 (struct internal_reloc
*) bfd_malloc (amt
);
779 amt
= o
->reloc_count
;
780 amt
*= sizeof (struct coff_link_hash_entry
*);
781 flaginfo
.section_info
[o
->target_index
].rel_hashes
=
782 (struct coff_link_hash_entry
**) bfd_malloc (amt
);
783 if (flaginfo
.section_info
[o
->target_index
].relocs
== NULL
784 || flaginfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
787 if (o
->reloc_count
> max_output_reloc_count
)
788 max_output_reloc_count
= o
->reloc_count
;
791 /* Reset the reloc and lineno counts, so that we can use them to
792 count the number of entries we have output so far. */
797 obj_sym_filepos (abfd
) = line_filepos
;
799 /* Figure out the largest number of symbols in an input BFD. Take
800 the opportunity to clear the output_has_begun fields of all the
803 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
807 sub
->output_has_begun
= false;
808 sz
= bfd_family_coff (sub
) ? obj_raw_syment_count (sub
) : 2;
809 if (sz
> max_sym_count
)
813 /* Allocate some buffers used while linking. */
814 amt
= max_sym_count
* sizeof (struct internal_syment
);
815 flaginfo
.internal_syms
= (struct internal_syment
*) bfd_malloc (amt
);
816 amt
= max_sym_count
* sizeof (asection
*);
817 flaginfo
.sec_ptrs
= (asection
**) bfd_malloc (amt
);
818 amt
= max_sym_count
* sizeof (long);
819 flaginfo
.sym_indices
= (long int *) bfd_malloc (amt
);
820 flaginfo
.outsyms
= (bfd_byte
*) bfd_malloc ((max_sym_count
+ 1) * symesz
);
821 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
822 flaginfo
.linenos
= (bfd_byte
*) bfd_malloc (amt
);
823 flaginfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
824 amt
= max_reloc_count
* relsz
;
825 flaginfo
.external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
826 if (! bfd_link_relocatable (info
))
828 amt
= max_reloc_count
* sizeof (struct internal_reloc
);
829 flaginfo
.internal_relocs
= (struct internal_reloc
*) bfd_malloc (amt
);
831 if ((flaginfo
.internal_syms
== NULL
&& max_sym_count
> 0)
832 || (flaginfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
833 || (flaginfo
.sym_indices
== NULL
&& max_sym_count
> 0)
834 || flaginfo
.outsyms
== NULL
835 || (flaginfo
.linenos
== NULL
&& max_lineno_count
> 0)
836 || (flaginfo
.contents
== NULL
&& max_contents_size
> 0)
837 || (flaginfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
838 || (! bfd_link_relocatable (info
)
839 && flaginfo
.internal_relocs
== NULL
840 && max_reloc_count
> 0))
843 /* We now know the position of everything in the file, except that
844 we don't know the size of the symbol table and therefore we don't
845 know where the string table starts. We just build the string
846 table in memory as we go along. We process all the relocations
847 for a single input file at once. */
848 obj_raw_syment_count (abfd
) = 0;
850 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
852 if (! bfd_coff_start_final_link (abfd
, info
))
856 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
858 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
860 if (p
->type
== bfd_indirect_link_order
861 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
863 sub
= p
->u
.indirect
.section
->owner
;
864 if (! bfd_coff_link_output_has_begun (sub
, & flaginfo
))
866 if (! _bfd_coff_link_input_bfd (&flaginfo
, sub
))
868 sub
->output_has_begun
= true;
871 else if (p
->type
== bfd_section_reloc_link_order
872 || p
->type
== bfd_symbol_reloc_link_order
)
874 if (! _bfd_coff_reloc_link_order (abfd
, &flaginfo
, o
, p
))
879 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
885 if (flaginfo
.info
->strip
!= strip_all
&& flaginfo
.info
->discard
!= discard_all
)
887 /* Add local symbols from foreign inputs. */
888 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
892 if (bfd_family_coff (sub
) || ! bfd_get_outsymbols (sub
))
894 for (i
= 0; i
< bfd_get_symcount (sub
); ++i
)
896 asymbol
*sym
= bfd_get_outsymbols (sub
) [i
];
898 struct internal_syment isym
;
900 bool rewrite
= false;
902 if (! (sym
->flags
& BSF_LOCAL
)
903 || (sym
->flags
& (BSF_SECTION_SYM
| BSF_DEBUGGING_RELOC
904 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
906 || ((sym
->flags
& BSF_DEBUGGING
)
907 && ! (sym
->flags
& BSF_FILE
)))
910 /* See if we are discarding symbols with this name. */
911 if ((flaginfo
.info
->strip
== strip_some
912 && (bfd_hash_lookup (flaginfo
.info
->keep_hash
,
913 bfd_asymbol_name(sym
), false, false)
915 || (((flaginfo
.info
->discard
== discard_sec_merge
916 && (bfd_asymbol_section (sym
)->flags
& SEC_MERGE
)
917 && ! bfd_link_relocatable (flaginfo
.info
))
918 || flaginfo
.info
->discard
== discard_l
)
919 && bfd_is_local_label_name (sub
, bfd_asymbol_name(sym
))))
922 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
)
924 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
926 if (! coff_write_alien_symbol(abfd
, sym
, &isym
, &written
,
928 !flaginfo
.info
->traditional_format
,
932 if (isym
.n_sclass
== C_FILE
)
934 if (flaginfo
.last_file_index
!= -1)
936 flaginfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
937 bfd_coff_swap_sym_out (abfd
, &flaginfo
.last_file
,
939 pos
= obj_sym_filepos (abfd
) + flaginfo
.last_file_index
943 flaginfo
.last_file_index
= obj_raw_syment_count (abfd
);
944 flaginfo
.last_file
= isym
;
948 && (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
949 || bfd_bwrite (flaginfo
.outsyms
, symesz
, abfd
) != symesz
))
952 obj_raw_syment_count (abfd
) += written
;
957 if (! bfd_coff_final_link_postscript (abfd
, & flaginfo
))
960 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
962 coff_debug_merge_hash_table_free (&flaginfo
.debug_merge
);
963 debug_merge_allocated
= false;
965 free (flaginfo
.internal_syms
);
966 flaginfo
.internal_syms
= NULL
;
967 free (flaginfo
.sec_ptrs
);
968 flaginfo
.sec_ptrs
= NULL
;
969 free (flaginfo
.sym_indices
);
970 flaginfo
.sym_indices
= NULL
;
971 free (flaginfo
.linenos
);
972 flaginfo
.linenos
= NULL
;
973 free (flaginfo
.contents
);
974 flaginfo
.contents
= NULL
;
975 free (flaginfo
.external_relocs
);
976 flaginfo
.external_relocs
= NULL
;
977 free (flaginfo
.internal_relocs
);
978 flaginfo
.internal_relocs
= NULL
;
980 /* The value of the last C_FILE symbol is supposed to be the symbol
981 index of the first external symbol. Write it out again if
983 if (flaginfo
.last_file_index
!= -1
984 && (unsigned int) flaginfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
988 flaginfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
989 bfd_coff_swap_sym_out (abfd
, &flaginfo
.last_file
,
992 pos
= obj_sym_filepos (abfd
) + flaginfo
.last_file_index
* symesz
;
993 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
994 || bfd_bwrite (flaginfo
.outsyms
, symesz
, abfd
) != symesz
)
998 /* If doing task linking (ld --task-link) then make a pass through the
999 global symbols, writing out any that are defined, and making them
1001 if (info
->task_link
)
1003 flaginfo
.failed
= false;
1004 coff_link_hash_traverse (coff_hash_table (info
),
1005 _bfd_coff_write_task_globals
, &flaginfo
);
1006 if (flaginfo
.failed
)
1010 /* Write out the global symbols. */
1011 flaginfo
.failed
= false;
1012 bfd_hash_traverse (&info
->hash
->table
, _bfd_coff_write_global_sym
, &flaginfo
);
1013 if (flaginfo
.failed
)
1016 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1017 free (flaginfo
.outsyms
);
1018 flaginfo
.outsyms
= NULL
;
1020 if (bfd_link_relocatable (info
) && max_output_reloc_count
> 0)
1022 /* Now that we have written out all the global symbols, we know
1023 the symbol indices to use for relocs against them, and we can
1024 finally write out the relocs. */
1025 amt
= max_output_reloc_count
* relsz
;
1026 external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
1027 if (external_relocs
== NULL
)
1030 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1032 struct internal_reloc
*irel
;
1033 struct internal_reloc
*irelend
;
1034 struct coff_link_hash_entry
**rel_hash
;
1037 if (o
->reloc_count
== 0)
1040 irel
= flaginfo
.section_info
[o
->target_index
].relocs
;
1041 irelend
= irel
+ o
->reloc_count
;
1042 rel_hash
= flaginfo
.section_info
[o
->target_index
].rel_hashes
;
1043 erel
= external_relocs
;
1044 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1046 if (*rel_hash
!= NULL
)
1048 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1049 irel
->r_symndx
= (*rel_hash
)->indx
;
1051 bfd_coff_swap_reloc_out (abfd
, irel
, erel
);
1054 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0)
1056 if ((obj_pe (abfd
) || obj_go32 (abfd
)) && o
->reloc_count
>= 0xffff)
1058 /* In PE COFF, write the count of relocs as the first
1059 reloc. The header overflow bit will be set
1061 struct internal_reloc incount
;
1062 bfd_byte
*excount
= (bfd_byte
*)bfd_malloc (relsz
);
1064 memset (&incount
, 0, sizeof (incount
));
1065 incount
.r_vaddr
= o
->reloc_count
+ 1;
1066 bfd_coff_swap_reloc_out (abfd
, &incount
, excount
);
1067 if (bfd_bwrite (excount
, relsz
, abfd
) != relsz
)
1068 /* We'll leak, but it's an error anyway. */
1072 if (bfd_bwrite (external_relocs
,
1073 (bfd_size_type
) relsz
* o
->reloc_count
, abfd
)
1074 != (bfd_size_type
) relsz
* o
->reloc_count
)
1078 free (external_relocs
);
1079 external_relocs
= NULL
;
1082 /* Free up the section information. */
1083 if (flaginfo
.section_info
!= NULL
)
1087 for (i
= 0; i
< abfd
->section_count
; i
++)
1089 free (flaginfo
.section_info
[i
].relocs
);
1090 free (flaginfo
.section_info
[i
].rel_hashes
);
1092 free (flaginfo
.section_info
);
1093 flaginfo
.section_info
= NULL
;
1096 /* If we have optimized stabs strings, output them. */
1097 if (coff_hash_table (info
)->stab_info
.stabstr
!= NULL
)
1099 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1103 /* Write out the string table. */
1104 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1108 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
1109 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1112 #if STRING_SIZE_SIZE == 4
1114 _bfd_stringtab_size (flaginfo
.strtab
) + STRING_SIZE_SIZE
,
1117 #error Change H_PUT_32 above
1120 if (bfd_bwrite (strbuf
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1121 != STRING_SIZE_SIZE
)
1124 if (! _bfd_stringtab_emit (abfd
, flaginfo
.strtab
))
1127 obj_coff_strings_written (abfd
) = true;
1130 _bfd_stringtab_free (flaginfo
.strtab
);
1132 /* Setting symcount to 0 will cause write_object_contents to
1133 not try to write out the symbols. */
1139 if (debug_merge_allocated
)
1140 coff_debug_merge_hash_table_free (&flaginfo
.debug_merge
);
1141 if (flaginfo
.strtab
!= NULL
)
1142 _bfd_stringtab_free (flaginfo
.strtab
);
1143 if (flaginfo
.section_info
!= NULL
)
1147 for (i
= 0; i
< abfd
->section_count
; i
++)
1149 free (flaginfo
.section_info
[i
].relocs
);
1150 free (flaginfo
.section_info
[i
].rel_hashes
);
1152 free (flaginfo
.section_info
);
1154 free (flaginfo
.internal_syms
);
1155 free (flaginfo
.sec_ptrs
);
1156 free (flaginfo
.sym_indices
);
1157 free (flaginfo
.outsyms
);
1158 free (flaginfo
.linenos
);
1159 free (flaginfo
.contents
);
1160 free (flaginfo
.external_relocs
);
1161 free (flaginfo
.internal_relocs
);
1162 free (external_relocs
);
1166 /* Parse out a -heap <reserved>,<commit> line. */
1169 dores_com (char *ptr
, bfd
*output_bfd
, int heap
)
1171 if (coff_data(output_bfd
)->pe
)
1173 int val
= strtoul (ptr
, &ptr
, 0);
1176 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
= val
;
1178 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
= val
;
1182 val
= strtoul (ptr
+1, &ptr
, 0);
1184 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
= val
;
1186 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
= val
;
1193 get_name (char *ptr
, char **dst
)
1198 while (*ptr
&& *ptr
!= ' ')
1204 /* Process any magic embedded commands in a section called .drectve. */
1207 process_embedded_commands (bfd
*output_bfd
,
1208 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1211 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1219 if (!bfd_malloc_and_get_section (abfd
, sec
, ©
))
1224 e
= (char *) copy
+ sec
->size
;
1226 for (s
= (char *) copy
; s
< e
; )
1233 if (startswith (s
, "-attr"))
1243 s
= get_name (s
, &name
);
1244 s
= get_name (s
, &attribs
);
1264 asec
= bfd_get_section_by_name (abfd
, name
);
1268 asec
->flags
|= SEC_CODE
;
1270 asec
->flags
|= SEC_READONLY
;
1273 else if (startswith (s
, "-heap"))
1274 s
= dores_com (s
+ 5, output_bfd
, 1);
1276 else if (startswith (s
, "-stack"))
1277 s
= dores_com (s
+ 6, output_bfd
, 0);
1279 /* GNU extension for aligned commons. */
1280 else if (startswith (s
, "-aligncomm:"))
1282 /* Common symbols must be aligned on reading, as it
1283 is too late to do anything here, after they have
1284 already been allocated, so just skip the directive. */
1295 /* Place a marker against all symbols which are used by relocations.
1296 This marker can be picked up by the 'do we skip this symbol ?'
1297 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1301 mark_relocs (struct coff_final_link_info
*flaginfo
, bfd
*input_bfd
)
1305 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1308 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1310 struct internal_reloc
* internal_relocs
;
1311 struct internal_reloc
* irel
;
1312 struct internal_reloc
* irelend
;
1314 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1
1315 || a
->linker_mark
== 0)
1317 /* Don't mark relocs in excluded sections. */
1318 if (a
->output_section
== bfd_abs_section_ptr
)
1321 /* Read in the relocs. */
1322 internal_relocs
= _bfd_coff_read_internal_relocs
1323 (input_bfd
, a
, false,
1324 flaginfo
->external_relocs
,
1325 bfd_link_relocatable (flaginfo
->info
),
1326 (bfd_link_relocatable (flaginfo
->info
)
1327 ? (flaginfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1328 : flaginfo
->internal_relocs
)
1331 if (internal_relocs
== NULL
)
1334 irel
= internal_relocs
;
1335 irelend
= irel
+ a
->reloc_count
;
1337 /* Place a mark in the sym_indices array (whose entries have
1338 been initialised to 0) for all of the symbols that are used
1339 in the relocation table. This will then be picked up in the
1340 skip/don't-skip pass. */
1341 for (; irel
< irelend
; irel
++)
1342 if ((unsigned long) irel
->r_symndx
< obj_raw_syment_count (input_bfd
))
1343 flaginfo
->sym_indices
[irel
->r_symndx
] = -1;
1347 /* Link an input file into the linker output file. This function
1348 handles all the sections and relocations of the input file at once. */
1351 _bfd_coff_link_input_bfd (struct coff_final_link_info
*flaginfo
, bfd
*input_bfd
)
1353 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1354 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1355 bool (*adjust_symndx
)
1356 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
1357 struct internal_reloc
*, bool *);
1359 const char *strings
;
1360 bfd_size_type syment_base
;
1362 bfd_size_type isymesz
;
1363 bfd_size_type osymesz
;
1364 bfd_size_type linesz
;
1367 struct internal_syment
*isymp
;
1370 unsigned long output_index
;
1372 struct coff_link_hash_entry
**sym_hash
;
1375 /* Move all the symbols to the output file. */
1377 output_bfd
= flaginfo
->output_bfd
;
1379 syment_base
= obj_raw_syment_count (output_bfd
);
1380 isymesz
= bfd_coff_symesz (input_bfd
);
1381 osymesz
= bfd_coff_symesz (output_bfd
);
1382 linesz
= bfd_coff_linesz (input_bfd
);
1383 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1386 if (! flaginfo
->info
->keep_memory
)
1389 if (flaginfo
->info
->traditional_format
)
1392 if (! _bfd_coff_get_external_symbols (input_bfd
))
1395 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1396 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1397 isymp
= flaginfo
->internal_syms
;
1398 secpp
= flaginfo
->sec_ptrs
;
1399 indexp
= flaginfo
->sym_indices
;
1400 output_index
= syment_base
;
1401 outsym
= flaginfo
->outsyms
;
1403 if (coff_data (output_bfd
)->pe
1404 && ! process_embedded_commands (output_bfd
, flaginfo
->info
, input_bfd
))
1407 /* If we are going to perform relocations and also strip/discard some
1408 symbols then we must make sure that we do not strip/discard those
1409 symbols that are going to be involved in the relocations. */
1410 if (( flaginfo
->info
->strip
!= strip_none
1411 || flaginfo
->info
->discard
!= discard_none
)
1412 && bfd_link_relocatable (flaginfo
->info
))
1414 /* Mark the symbol array as 'not-used'. */
1415 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1417 mark_relocs (flaginfo
, input_bfd
);
1420 while (esym
< esym_end
)
1422 struct internal_syment isym
;
1423 enum coff_symbol_classification classification
;
1426 bool dont_skip_symbol
;
1429 bfd_coff_swap_sym_in (input_bfd
, esym
, isymp
);
1431 /* Make a copy of *isymp so that the relocate_section function
1432 always sees the original values. This is more reliable than
1433 always recomputing the symbol value even if we are stripping
1437 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1438 switch (classification
)
1442 case COFF_SYMBOL_GLOBAL
:
1443 case COFF_SYMBOL_PE_SECTION
:
1444 case COFF_SYMBOL_LOCAL
:
1445 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1447 case COFF_SYMBOL_COMMON
:
1448 *secpp
= bfd_com_section_ptr
;
1450 case COFF_SYMBOL_UNDEFINED
:
1451 *secpp
= bfd_und_section_ptr
;
1455 /* Extract the flag indicating if this symbol is used by a
1457 if ((flaginfo
->info
->strip
!= strip_none
1458 || flaginfo
->info
->discard
!= discard_none
)
1459 && bfd_link_relocatable (flaginfo
->info
))
1460 dont_skip_symbol
= *indexp
;
1462 dont_skip_symbol
= false;
1468 add
= 1 + isym
.n_numaux
;
1470 /* If we are stripping all symbols, we want to skip this one. */
1471 if (flaginfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1476 switch (classification
)
1480 case COFF_SYMBOL_GLOBAL
:
1481 case COFF_SYMBOL_COMMON
:
1482 case COFF_SYMBOL_PE_SECTION
:
1483 /* This is a global symbol. Global symbols come at the
1484 end of the symbol table, so skip them for now.
1485 Locally defined function symbols, however, are an
1486 exception, and are not moved to the end. */
1488 if (! ISFCN (isym
.n_type
))
1492 case COFF_SYMBOL_UNDEFINED
:
1493 /* Undefined symbols are left for the end. */
1498 case COFF_SYMBOL_LOCAL
:
1499 /* This is a local symbol. Skip it if we are discarding
1501 if (flaginfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1507 #ifndef COFF_WITH_PE
1508 /* Skip section symbols for sections which are not going to be
1511 && !dont_skip_symbol
1512 && isym
.n_sclass
== C_STAT
1513 && isym
.n_type
== T_NULL
1514 && isym
.n_numaux
> 0
1515 && ((*secpp
)->output_section
== bfd_abs_section_ptr
1516 || bfd_section_removed_from_list (output_bfd
,
1517 (*secpp
)->output_section
)))
1521 /* If we stripping debugging symbols, and this is a debugging
1522 symbol, then skip it. FIXME: gas sets the section to N_ABS
1523 for some types of debugging symbols; I don't know if this is
1524 a bug or not. In any case, we handle it here. */
1526 && flaginfo
->info
->strip
== strip_debugger
1527 && ! dont_skip_symbol
1528 && (isym
.n_scnum
== N_DEBUG
1529 || (isym
.n_scnum
== N_ABS
1530 && (isym
.n_sclass
== C_AUTO
1531 || isym
.n_sclass
== C_REG
1532 || isym
.n_sclass
== C_MOS
1533 || isym
.n_sclass
== C_MOE
1534 || isym
.n_sclass
== C_MOU
1535 || isym
.n_sclass
== C_ARG
1536 || isym
.n_sclass
== C_REGPARM
1537 || isym
.n_sclass
== C_FIELD
1538 || isym
.n_sclass
== C_EOS
))))
1541 /* If some symbols are stripped based on the name, work out the
1542 name and decide whether to skip this symbol. */
1544 && (flaginfo
->info
->strip
== strip_some
1545 || flaginfo
->info
->discard
== discard_l
))
1548 char buf
[SYMNMLEN
+ 1];
1550 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1554 if (! dont_skip_symbol
1555 && ((flaginfo
->info
->strip
== strip_some
1556 && (bfd_hash_lookup (flaginfo
->info
->keep_hash
, name
, false,
1559 && flaginfo
->info
->discard
== discard_l
1560 && bfd_is_local_label_name (input_bfd
, name
))))
1564 /* If this is an enum, struct, or union tag, see if we have
1565 already output an identical type. */
1567 && !flaginfo
->info
->traditional_format
1568 && (isym
.n_sclass
== C_ENTAG
1569 || isym
.n_sclass
== C_STRTAG
1570 || isym
.n_sclass
== C_UNTAG
)
1571 && isym
.n_numaux
== 1)
1574 char buf
[SYMNMLEN
+ 1];
1575 struct coff_debug_merge_hash_entry
*mh
;
1576 struct coff_debug_merge_type
*mt
;
1577 union internal_auxent aux
;
1578 struct coff_debug_merge_element
**epp
;
1579 bfd_byte
*esl
, *eslend
;
1580 struct internal_syment
*islp
;
1583 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1587 /* Ignore fake names invented by compiler; treat them all as
1589 if (*name
== '~' || *name
== '.' || *name
== '$'
1590 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1591 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1594 mh
= coff_debug_merge_hash_lookup (&flaginfo
->debug_merge
, name
,
1599 /* Allocate memory to hold type information. If this turns
1600 out to be a duplicate, we pass this address to
1602 amt
= sizeof (struct coff_debug_merge_type
);
1603 mt
= (struct coff_debug_merge_type
*) bfd_alloc (input_bfd
, amt
);
1606 mt
->type_class
= isym
.n_sclass
;
1608 /* Pick up the aux entry, which points to the end of the tag
1610 bfd_coff_swap_aux_in (input_bfd
, (esym
+ isymesz
),
1611 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1614 /* Gather the elements. */
1615 epp
= &mt
->elements
;
1616 mt
->elements
= NULL
;
1618 esl
= esym
+ 2 * isymesz
;
1619 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1620 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1621 while (esl
< eslend
)
1623 const char *elename
;
1624 char elebuf
[SYMNMLEN
+ 1];
1627 bfd_coff_swap_sym_in (input_bfd
, esl
, islp
);
1629 amt
= sizeof (struct coff_debug_merge_element
);
1630 *epp
= (struct coff_debug_merge_element
*)
1631 bfd_alloc (input_bfd
, amt
);
1635 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1637 if (elename
== NULL
)
1640 amt
= strlen (elename
) + 1;
1641 name_copy
= (char *) bfd_alloc (input_bfd
, amt
);
1642 if (name_copy
== NULL
)
1644 strcpy (name_copy
, elename
);
1646 (*epp
)->name
= name_copy
;
1647 (*epp
)->type
= islp
->n_type
;
1649 if (islp
->n_numaux
>= 1
1650 && islp
->n_type
!= T_NULL
1651 && islp
->n_sclass
!= C_EOS
)
1653 union internal_auxent eleaux
;
1656 bfd_coff_swap_aux_in (input_bfd
, (esl
+ isymesz
),
1657 islp
->n_type
, islp
->n_sclass
, 0,
1658 islp
->n_numaux
, &eleaux
);
1659 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1661 /* FIXME: If this tagndx entry refers to a symbol
1662 defined later in this file, we just ignore it.
1663 Handling this correctly would be tedious, and may
1668 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1671 (*epp
)->tagndx
= flaginfo
->sym_indices
[indx
];
1672 if ((*epp
)->tagndx
< 0)
1676 epp
= &(*epp
)->next
;
1679 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1680 islp
+= islp
->n_numaux
+ 1;
1683 /* See if we already have a definition which matches this
1684 type. We always output the type if it has no elements,
1686 if (mt
->elements
== NULL
)
1687 bfd_release (input_bfd
, mt
);
1690 struct coff_debug_merge_type
*mtl
;
1692 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1694 struct coff_debug_merge_element
*me
, *mel
;
1696 if (mtl
->type_class
!= mt
->type_class
)
1699 for (me
= mt
->elements
, mel
= mtl
->elements
;
1700 me
!= NULL
&& mel
!= NULL
;
1701 me
= me
->next
, mel
= mel
->next
)
1703 if (strcmp (me
->name
, mel
->name
) != 0
1704 || me
->type
!= mel
->type
1705 || me
->tagndx
!= mel
->tagndx
)
1709 if (me
== NULL
&& mel
== NULL
)
1713 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1715 /* This is the first definition of this type. */
1716 mt
->indx
= output_index
;
1717 mt
->next
= mh
->types
;
1722 /* This is a redefinition which can be merged. */
1723 bfd_release (input_bfd
, mt
);
1724 *indexp
= mtl
->indx
;
1725 add
= (eslend
- esym
) / isymesz
;
1731 /* We now know whether we are to skip this symbol or not. */
1734 /* Adjust the symbol in order to output it. */
1736 if (isym
._n
._n_n
._n_zeroes
== 0
1737 && isym
._n
._n_n
._n_offset
!= 0)
1742 /* This symbol has a long name. Enter it in the string
1743 table we are building. Note that we do not check
1744 bfd_coff_symname_in_debug. That is only true for
1745 XCOFF, and XCOFF requires different linking code
1747 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
1750 indx
= _bfd_stringtab_add (flaginfo
->strtab
, name
, hash
, copy
);
1751 if (indx
== (bfd_size_type
) -1)
1753 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1756 switch (isym
.n_sclass
)
1772 /* The symbol value should not be modified. */
1776 if (obj_pe (input_bfd
)
1777 && memcmp (isym
.n_name
, ".bf", sizeof ".bf") != 0
1778 && isym
.n_scnum
> 0)
1780 /* For PE, .lf and .ef get their value left alone,
1781 while .bf gets relocated. However, they all have
1782 "real" section numbers, and need to be moved into
1784 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1789 case C_LABEL
: /* Not completely sure about these 2 */
1798 /* Compute new symbol location. */
1799 if (isym
.n_scnum
> 0)
1801 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1802 isym
.n_value
+= (*secpp
)->output_offset
;
1803 if (! obj_pe (input_bfd
))
1804 isym
.n_value
-= (*secpp
)->vma
;
1805 if (! obj_pe (flaginfo
->output_bfd
))
1806 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1811 /* The value of a C_FILE symbol is the symbol index of
1812 the next C_FILE symbol. The value of the last C_FILE
1813 symbol is the symbol index to the first external
1814 symbol (actually, coff_renumber_symbols does not get
1815 this right--it just sets the value of the last C_FILE
1816 symbol to zero--and nobody has ever complained about
1817 it). We try to get this right, below, just before we
1818 write the symbols out, but in the general case we may
1819 have to write the symbol out twice. */
1820 if (flaginfo
->last_file_index
!= -1
1821 && flaginfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
1823 /* We must correct the value of the last C_FILE
1825 flaginfo
->last_file
.n_value
= output_index
;
1826 if ((bfd_size_type
) flaginfo
->last_file_index
>= syment_base
)
1828 /* The last C_FILE symbol is in this input file. */
1829 bfd_coff_swap_sym_out (output_bfd
,
1830 &flaginfo
->last_file
,
1832 + ((flaginfo
->last_file_index
1840 /* We have already written out the last C_FILE
1841 symbol. We need to write it out again. We
1842 borrow *outsym temporarily. */
1843 bfd_coff_swap_sym_out (output_bfd
,
1844 &flaginfo
->last_file
, outsym
);
1845 pos
= obj_sym_filepos (output_bfd
);
1846 pos
+= flaginfo
->last_file_index
* osymesz
;
1847 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
1848 || bfd_bwrite (outsym
, osymesz
, output_bfd
) != osymesz
)
1853 flaginfo
->last_file_index
= output_index
;
1854 flaginfo
->last_file
= isym
;
1858 /* If doing task linking, convert normal global function symbols to
1859 static functions. */
1860 if (flaginfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1861 isym
.n_sclass
= C_STAT
;
1863 /* Output the symbol. */
1864 bfd_coff_swap_sym_out (output_bfd
, &isym
, outsym
);
1866 *indexp
= output_index
;
1871 struct coff_link_hash_entry
*h
;
1873 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1875 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1878 /* This can happen if there were errors earlier in
1880 bfd_set_error (bfd_error_bad_value
);
1883 h
->indx
= output_index
;
1886 output_index
+= add
;
1887 outsym
+= add
* osymesz
;
1890 esym
+= add
* isymesz
;
1894 for (--add
; add
> 0; --add
)
1901 /* Fix up the aux entries. This must be done in a separate pass,
1902 because we don't know the correct symbol indices until we have
1903 already decided which symbols we are going to keep. */
1904 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1905 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1906 isymp
= flaginfo
->internal_syms
;
1907 indexp
= flaginfo
->sym_indices
;
1908 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1909 outsym
= flaginfo
->outsyms
;
1911 while (esym
< esym_end
)
1915 add
= 1 + isymp
->n_numaux
;
1918 || (bfd_size_type
) *indexp
< syment_base
)
1919 && (*sym_hash
== NULL
1920 || (*sym_hash
)->auxbfd
!= input_bfd
))
1921 esym
+= add
* isymesz
;
1924 struct coff_link_hash_entry
*h
;
1932 /* The m68k-motorola-sysv assembler will sometimes
1933 generate two symbols with the same name, but only one
1934 will have aux entries. */
1935 BFD_ASSERT (isymp
->n_numaux
== 0
1937 || h
->numaux
== isymp
->n_numaux
);
1945 /* Handle the aux entries. This handling is based on
1946 coff_pointerize_aux. I don't know if it always correct. */
1947 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1949 union internal_auxent aux
;
1950 union internal_auxent
*auxp
;
1952 if (h
!= NULL
&& h
->aux
!= NULL
&& (h
->numaux
> i
))
1956 bfd_coff_swap_aux_in (input_bfd
, esym
, isymp
->n_type
,
1957 isymp
->n_sclass
, i
, isymp
->n_numaux
, &aux
);
1961 if (isymp
->n_sclass
== C_FILE
)
1963 /* If this is a long filename, we must put it in the
1965 if (auxp
->x_file
.x_n
.x_n
.x_zeroes
== 0
1966 && auxp
->x_file
.x_n
.x_n
.x_offset
!= 0)
1968 const char *filename
;
1971 BFD_ASSERT (auxp
->x_file
.x_n
.x_n
.x_offset
1972 >= STRING_SIZE_SIZE
);
1973 if (strings
== NULL
)
1975 strings
= _bfd_coff_read_string_table (input_bfd
);
1976 if (strings
== NULL
)
1979 if ((bfd_size_type
) auxp
->x_file
.x_n
.x_n
.x_offset
>= obj_coff_strings_len (input_bfd
))
1980 filename
= _("<corrupt>");
1982 filename
= strings
+ auxp
->x_file
.x_n
.x_n
.x_offset
;
1983 indx
= _bfd_stringtab_add (flaginfo
->strtab
, filename
,
1985 if (indx
== (bfd_size_type
) -1)
1987 auxp
->x_file
.x_n
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1990 else if ((isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1991 && isymp
->n_sclass
!= C_NT_WEAK
)
1995 if (ISFCN (isymp
->n_type
)
1996 || ISTAG (isymp
->n_sclass
)
1997 || isymp
->n_sclass
== C_BLOCK
1998 || isymp
->n_sclass
== C_FCN
)
2000 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2002 && indx
< obj_raw_syment_count (input_bfd
))
2004 /* We look forward through the symbol for
2005 the index of the next symbol we are going
2006 to include. I don't know if this is
2008 while ((flaginfo
->sym_indices
[indx
] < 0
2009 || ((bfd_size_type
) flaginfo
->sym_indices
[indx
]
2011 && indx
< obj_raw_syment_count (input_bfd
))
2013 if (indx
>= obj_raw_syment_count (input_bfd
))
2014 indx
= output_index
;
2016 indx
= flaginfo
->sym_indices
[indx
];
2017 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
2021 indx
= auxp
->x_sym
.x_tagndx
.l
;
2022 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2026 symindx
= flaginfo
->sym_indices
[indx
];
2028 auxp
->x_sym
.x_tagndx
.l
= 0;
2030 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2033 /* The .bf symbols are supposed to be linked through
2034 the endndx field. We need to carry this list
2035 across object files. */
2038 && isymp
->n_sclass
== C_FCN
2039 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2040 || isymp
->_n
._n_n
._n_offset
== 0)
2041 && isymp
->_n
._n_name
[0] == '.'
2042 && isymp
->_n
._n_name
[1] == 'b'
2043 && isymp
->_n
._n_name
[2] == 'f'
2044 && isymp
->_n
._n_name
[3] == '\0')
2046 if (flaginfo
->last_bf_index
!= -1)
2048 flaginfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2051 if ((bfd_size_type
) flaginfo
->last_bf_index
2056 /* The last .bf symbol is in this input
2057 file. This will only happen if the
2058 assembler did not set up the .bf
2059 endndx symbols correctly. */
2060 auxout
= (flaginfo
->outsyms
2061 + ((flaginfo
->last_bf_index
2065 bfd_coff_swap_aux_out (output_bfd
,
2076 /* We have already written out the last
2077 .bf aux entry. We need to write it
2078 out again. We borrow *outsym
2079 temporarily. FIXME: This case should
2081 bfd_coff_swap_aux_out (output_bfd
,
2087 pos
= obj_sym_filepos (output_bfd
);
2088 pos
+= flaginfo
->last_bf_index
* osymesz
;
2089 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2090 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
2096 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2097 flaginfo
->last_bf_index
= -1;
2100 /* The endndx field of this aux entry must
2101 be updated with the symbol number of the
2103 flaginfo
->last_bf
= *auxp
;
2104 flaginfo
->last_bf_index
= (((outsym
- flaginfo
->outsyms
)
2113 bfd_coff_swap_aux_out (output_bfd
, auxp
, isymp
->n_type
,
2114 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2128 /* Relocate the line numbers, unless we are stripping them. */
2129 if (flaginfo
->info
->strip
== strip_none
2130 || flaginfo
->info
->strip
== strip_some
)
2132 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2142 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2143 build_link_order in ldwrite.c will not have created a
2144 link order, which means that we will not have seen this
2145 input section in _bfd_coff_final_link, which means that
2146 we will not have allocated space for the line numbers of
2147 this section. I don't think line numbers can be
2148 meaningful for a section which does not have
2149 SEC_HAS_CONTENTS set, but, if they do, this must be
2151 if (o
->lineno_count
== 0
2152 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2155 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2156 || bfd_bread (flaginfo
->linenos
, linesz
* o
->lineno_count
,
2157 input_bfd
) != linesz
* o
->lineno_count
)
2160 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2161 eline
= flaginfo
->linenos
;
2162 oeline
= flaginfo
->linenos
;
2163 elineend
= eline
+ linesz
* o
->lineno_count
;
2165 for (; eline
< elineend
; eline
+= linesz
)
2167 struct internal_lineno iline
;
2169 bfd_coff_swap_lineno_in (input_bfd
, eline
, &iline
);
2171 if (iline
.l_lnno
!= 0)
2172 iline
.l_addr
.l_paddr
+= offset
;
2173 else if (iline
.l_addr
.l_symndx
>= 0
2174 && ((unsigned long) iline
.l_addr
.l_symndx
2175 < obj_raw_syment_count (input_bfd
)))
2179 indx
= flaginfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2183 /* These line numbers are attached to a symbol
2184 which we are stripping. We must discard the
2185 line numbers because reading them back with
2186 no associated symbol (or associating them all
2187 with symbol #0) will fail. We can't regain
2188 the space in the output file, but at least
2194 struct internal_syment is
;
2195 union internal_auxent ia
;
2197 /* Fix up the lnnoptr field in the aux entry of
2198 the symbol. It turns out that we can't do
2199 this when we modify the symbol aux entries,
2200 because gas sometimes screws up the lnnoptr
2201 field and makes it an offset from the start
2202 of the line numbers rather than an absolute
2204 bfd_coff_swap_sym_in (output_bfd
,
2206 + ((indx
- syment_base
)
2208 if ((ISFCN (is
.n_type
)
2209 || is
.n_sclass
== C_BLOCK
)
2210 && is
.n_numaux
>= 1)
2214 auxptr
= (flaginfo
->outsyms
2215 + ((indx
- syment_base
+ 1)
2217 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2218 is
.n_type
, is
.n_sclass
,
2219 0, is
.n_numaux
, &ia
);
2220 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2221 (o
->output_section
->line_filepos
2222 + o
->output_section
->lineno_count
* linesz
2223 + eline
- flaginfo
->linenos
);
2224 bfd_coff_swap_aux_out (output_bfd
, &ia
,
2225 is
.n_type
, is
.n_sclass
, 0,
2226 is
.n_numaux
, auxptr
);
2232 iline
.l_addr
.l_symndx
= indx
;
2237 bfd_coff_swap_lineno_out (output_bfd
, &iline
, oeline
);
2242 pos
= o
->output_section
->line_filepos
;
2243 pos
+= o
->output_section
->lineno_count
* linesz
;
2244 amt
= oeline
- flaginfo
->linenos
;
2245 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2246 || bfd_bwrite (flaginfo
->linenos
, amt
, output_bfd
) != amt
)
2249 o
->output_section
->lineno_count
+= amt
/ linesz
;
2253 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2254 symbol will be the first symbol in the next input file. In the
2255 normal case, this will save us from writing out the C_FILE symbol
2257 if (flaginfo
->last_file_index
!= -1
2258 && (bfd_size_type
) flaginfo
->last_file_index
>= syment_base
)
2260 flaginfo
->last_file
.n_value
= output_index
;
2261 bfd_coff_swap_sym_out (output_bfd
, &flaginfo
->last_file
,
2263 + ((flaginfo
->last_file_index
- syment_base
)
2267 /* Write the modified symbols to the output file. */
2268 if (outsym
> flaginfo
->outsyms
)
2273 pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
2274 amt
= outsym
- flaginfo
->outsyms
;
2275 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2276 || bfd_bwrite (flaginfo
->outsyms
, amt
, output_bfd
) != amt
)
2279 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2280 + (outsym
- flaginfo
->outsyms
) / osymesz
)
2283 obj_raw_syment_count (output_bfd
) = output_index
;
2286 /* Relocate the contents of each section. */
2287 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2288 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2291 struct coff_section_tdata
*secdata
;
2293 if (! o
->linker_mark
)
2294 /* This section was omitted from the link. */
2297 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
2300 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2301 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2303 if ((o
->flags
& SEC_RELOC
) != 0
2304 && o
->reloc_count
!= 0)
2307 /* xgettext: c-format */
2308 (_("%pB: relocs in section `%pA', but it has no contents"),
2310 bfd_set_error (bfd_error_no_contents
);
2317 secdata
= coff_section_data (input_bfd
, o
);
2318 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2319 contents
= secdata
->contents
;
2322 contents
= flaginfo
->contents
;
2323 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
2327 if ((o
->flags
& SEC_RELOC
) != 0)
2330 struct internal_reloc
*internal_relocs
;
2331 struct internal_reloc
*irel
;
2333 /* Read in the relocs. */
2334 target_index
= o
->output_section
->target_index
;
2335 internal_relocs
= (_bfd_coff_read_internal_relocs
2336 (input_bfd
, o
, false, flaginfo
->external_relocs
,
2337 bfd_link_relocatable (flaginfo
->info
),
2338 (bfd_link_relocatable (flaginfo
->info
)
2339 ? (flaginfo
->section_info
[target_index
].relocs
2340 + o
->output_section
->reloc_count
)
2341 : flaginfo
->internal_relocs
)));
2342 if (internal_relocs
== NULL
2343 && o
->reloc_count
> 0)
2346 /* Run through the relocs looking for relocs against symbols
2347 coming from discarded sections and complain about them. */
2348 irel
= internal_relocs
;
2349 for (; irel
< &internal_relocs
[o
->reloc_count
]; irel
++)
2351 struct coff_link_hash_entry
*h
;
2352 asection
*ps
= NULL
;
2353 long symndx
= irel
->r_symndx
;
2356 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2359 while (h
->root
.type
== bfd_link_hash_indirect
2360 || h
->root
.type
== bfd_link_hash_warning
)
2361 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2362 if (h
->root
.type
== bfd_link_hash_defined
2363 || h
->root
.type
== bfd_link_hash_defweak
)
2364 ps
= h
->root
.u
.def
.section
;
2367 /* Complain if definition comes from an excluded section. */
2368 if (ps
->flags
& SEC_EXCLUDE
)
2369 (*flaginfo
->info
->callbacks
->einfo
)
2370 /* xgettext: c-format */
2371 (_("%X`%s' referenced in section `%pA' of %pB: "
2372 "defined in discarded section `%pA' of %pB\n"),
2373 h
->root
.root
.string
, o
, input_bfd
, ps
, ps
->owner
);
2376 /* Call processor specific code to relocate the section
2378 if (! bfd_coff_relocate_section (output_bfd
, flaginfo
->info
,
2382 flaginfo
->internal_syms
,
2383 flaginfo
->sec_ptrs
))
2386 if (bfd_link_relocatable (flaginfo
->info
))
2389 struct internal_reloc
*irelend
;
2390 struct coff_link_hash_entry
**rel_hash
;
2392 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2393 irel
= internal_relocs
;
2394 irelend
= irel
+ o
->reloc_count
;
2395 rel_hash
= (flaginfo
->section_info
[target_index
].rel_hashes
2396 + o
->output_section
->reloc_count
);
2397 for (; irel
< irelend
; irel
++, rel_hash
++)
2399 struct coff_link_hash_entry
*h
;
2404 /* Adjust the reloc address and symbol index. */
2405 irel
->r_vaddr
+= offset
;
2407 if (irel
->r_symndx
== -1)
2412 if (! (*adjust_symndx
) (output_bfd
, flaginfo
->info
,
2420 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2423 /* This is a global symbol. */
2425 irel
->r_symndx
= h
->indx
;
2428 /* This symbol is being written at the end
2429 of the file, and we do not yet know the
2430 symbol index. We save the pointer to the
2431 hash table entry in the rel_hash list.
2432 We set the indx field to -2 to indicate
2433 that this symbol must not be stripped. */
2442 indx
= flaginfo
->sym_indices
[irel
->r_symndx
];
2444 irel
->r_symndx
= indx
;
2447 struct internal_syment
*is
;
2449 char buf
[SYMNMLEN
+ 1];
2451 /* This reloc is against a symbol we are
2452 stripping. This should have been handled
2453 by the 'dont_skip_symbol' code in the while
2454 loop at the top of this function. */
2455 is
= flaginfo
->internal_syms
+ irel
->r_symndx
;
2457 name
= (_bfd_coff_internal_syment_name
2458 (input_bfd
, is
, buf
));
2462 (*flaginfo
->info
->callbacks
->unattached_reloc
)
2463 (flaginfo
->info
, name
, input_bfd
, o
, irel
->r_vaddr
);
2468 o
->output_section
->reloc_count
+= o
->reloc_count
;
2472 /* Write out the modified section contents. */
2473 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2475 file_ptr loc
= (o
->output_offset
2476 * bfd_octets_per_byte (output_bfd
, o
));
2477 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2478 contents
, loc
, o
->size
))
2483 if (! (_bfd_write_section_stabs
2484 (output_bfd
, &coff_hash_table (flaginfo
->info
)->stab_info
,
2485 o
, &secdata
->stab_info
, contents
)))
2490 if (! flaginfo
->info
->keep_memory
2491 && ! _bfd_coff_free_symbols (input_bfd
))
2497 /* Write out a global symbol. Called via bfd_hash_traverse. */
2500 _bfd_coff_write_global_sym (struct bfd_hash_entry
*bh
, void *data
)
2502 struct coff_link_hash_entry
*h
= (struct coff_link_hash_entry
*) bh
;
2503 struct coff_final_link_info
*flaginfo
= (struct coff_final_link_info
*) data
;
2505 struct internal_syment isym
;
2506 bfd_size_type symesz
;
2510 output_bfd
= flaginfo
->output_bfd
;
2512 if (h
->root
.type
== bfd_link_hash_warning
)
2514 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2515 if (h
->root
.type
== bfd_link_hash_new
)
2523 && (flaginfo
->info
->strip
== strip_all
2524 || (flaginfo
->info
->strip
== strip_some
2525 && (bfd_hash_lookup (flaginfo
->info
->keep_hash
,
2526 h
->root
.root
.string
, false, false)
2530 switch (h
->root
.type
)
2533 case bfd_link_hash_new
:
2534 case bfd_link_hash_warning
:
2538 case bfd_link_hash_undefined
:
2542 case bfd_link_hash_undefweak
:
2543 isym
.n_scnum
= N_UNDEF
;
2547 case bfd_link_hash_defined
:
2548 case bfd_link_hash_defweak
:
2552 sec
= h
->root
.u
.def
.section
->output_section
;
2553 if (bfd_is_abs_section (sec
))
2554 isym
.n_scnum
= N_ABS
;
2556 isym
.n_scnum
= sec
->target_index
;
2557 isym
.n_value
= (h
->root
.u
.def
.value
2558 + h
->root
.u
.def
.section
->output_offset
);
2559 if (! obj_pe (flaginfo
->output_bfd
))
2560 isym
.n_value
+= sec
->vma
;
2562 if (isym
.n_value
> (bfd_vma
) 0xffffffff)
2564 if (! h
->root
.linker_def
)
2566 (_("%pB: stripping non-representable symbol '%s' "
2567 "(value 0x%" PRIx64
")"),
2568 output_bfd
, h
->root
.root
.string
, isym
.n_value
);
2575 case bfd_link_hash_common
:
2576 isym
.n_scnum
= N_UNDEF
;
2577 isym
.n_value
= h
->root
.u
.c
.size
;
2580 case bfd_link_hash_indirect
:
2581 /* Just ignore these. They can't be handled anyhow. */
2585 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2586 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2593 if (flaginfo
->info
->traditional_format
)
2595 indx
= _bfd_stringtab_add (flaginfo
->strtab
, h
->root
.root
.string
, hash
,
2597 if (indx
== (bfd_size_type
) -1)
2599 flaginfo
->failed
= true;
2602 isym
._n
._n_n
._n_zeroes
= 0;
2603 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2606 isym
.n_sclass
= h
->symbol_class
;
2607 isym
.n_type
= h
->type
;
2609 if (isym
.n_sclass
== C_NULL
)
2610 isym
.n_sclass
= C_EXT
;
2612 /* If doing task linking and this is the pass where we convert
2613 defined globals to statics, then do that conversion now. If the
2614 symbol is not being converted, just ignore it and it will be
2615 output during a later pass. */
2616 if (flaginfo
->global_to_static
)
2618 if (! IS_EXTERNAL (output_bfd
, isym
))
2621 isym
.n_sclass
= C_STAT
;
2624 /* When a weak symbol is not overridden by a strong one,
2625 turn it into an external symbol when not building a
2626 shared or relocatable object. */
2627 if (! bfd_link_pic (flaginfo
->info
)
2628 && ! bfd_link_relocatable (flaginfo
->info
)
2629 && IS_WEAK_EXTERNAL (flaginfo
->output_bfd
, isym
))
2630 isym
.n_sclass
= C_EXT
;
2632 isym
.n_numaux
= h
->numaux
;
2634 bfd_coff_swap_sym_out (output_bfd
, &isym
, flaginfo
->outsyms
);
2636 symesz
= bfd_coff_symesz (output_bfd
);
2638 pos
= obj_sym_filepos (output_bfd
);
2639 pos
+= obj_raw_syment_count (output_bfd
) * symesz
;
2640 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2641 || bfd_bwrite (flaginfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2643 flaginfo
->failed
= true;
2647 h
->indx
= obj_raw_syment_count (output_bfd
);
2649 ++obj_raw_syment_count (output_bfd
);
2651 /* Write out any associated aux entries. Most of the aux entries
2652 will have been modified in _bfd_coff_link_input_bfd. We have to
2653 handle section aux entries here, now that we have the final
2654 relocation and line number counts. */
2655 for (i
= 0; i
< isym
.n_numaux
; i
++)
2657 union internal_auxent
*auxp
;
2661 /* Look for a section aux entry here using the same tests that
2662 coff_swap_aux_out uses. */
2664 && (isym
.n_sclass
== C_STAT
2665 || isym
.n_sclass
== C_HIDDEN
)
2666 && isym
.n_type
== T_NULL
2667 && (h
->root
.type
== bfd_link_hash_defined
2668 || h
->root
.type
== bfd_link_hash_defweak
))
2672 sec
= h
->root
.u
.def
.section
->output_section
;
2675 auxp
->x_scn
.x_scnlen
= sec
->size
;
2677 /* For PE, an overflow on the final link reportedly does
2678 not matter. FIXME: Why not? */
2679 if (sec
->reloc_count
> 0xffff
2680 && (! obj_pe (output_bfd
)
2681 || bfd_link_relocatable (flaginfo
->info
)))
2683 /* xgettext: c-format */
2684 (_("%pB: %pA: reloc overflow: %#x > 0xffff"),
2685 output_bfd
, sec
, sec
->reloc_count
);
2687 if (sec
->lineno_count
> 0xffff
2688 && (! obj_pe (output_bfd
)
2689 || bfd_link_relocatable (flaginfo
->info
)))
2691 /* xgettext: c-format */
2692 (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"),
2693 output_bfd
, sec
, sec
->lineno_count
);
2695 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2696 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2697 auxp
->x_scn
.x_checksum
= 0;
2698 auxp
->x_scn
.x_associated
= 0;
2699 auxp
->x_scn
.x_comdat
= 0;
2703 bfd_coff_swap_aux_out (output_bfd
, auxp
, isym
.n_type
,
2704 isym
.n_sclass
, (int) i
, isym
.n_numaux
,
2706 if (bfd_bwrite (flaginfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2708 flaginfo
->failed
= true;
2711 ++obj_raw_syment_count (output_bfd
);
2717 /* Write out task global symbols, converting them to statics. Called
2718 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2719 the dirty work, if the symbol we are processing needs conversion. */
2722 _bfd_coff_write_task_globals (struct coff_link_hash_entry
*h
, void *data
)
2724 struct coff_final_link_info
*flaginfo
= (struct coff_final_link_info
*) data
;
2726 bool save_global_to_static
;
2728 if (h
->root
.type
== bfd_link_hash_warning
)
2729 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2733 switch (h
->root
.type
)
2735 case bfd_link_hash_defined
:
2736 case bfd_link_hash_defweak
:
2737 save_global_to_static
= flaginfo
->global_to_static
;
2738 flaginfo
->global_to_static
= true;
2739 rtnval
= _bfd_coff_write_global_sym (&h
->root
.root
, data
);
2740 flaginfo
->global_to_static
= save_global_to_static
;
2749 /* Handle a link order which is supposed to generate a reloc. */
2752 _bfd_coff_reloc_link_order (bfd
*output_bfd
,
2753 struct coff_final_link_info
*flaginfo
,
2754 asection
*output_section
,
2755 struct bfd_link_order
*link_order
)
2757 reloc_howto_type
*howto
;
2758 struct internal_reloc
*irel
;
2759 struct coff_link_hash_entry
**rel_hash_ptr
;
2761 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2764 bfd_set_error (bfd_error_bad_value
);
2768 if (link_order
->u
.reloc
.p
->addend
!= 0)
2772 bfd_reloc_status_type rstat
;
2776 size
= bfd_get_reloc_size (howto
);
2777 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2778 if (buf
== NULL
&& size
!= 0)
2781 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2782 (bfd_vma
) link_order
->u
.reloc
.p
->addend
,
2789 case bfd_reloc_outofrange
:
2791 case bfd_reloc_overflow
:
2792 (*flaginfo
->info
->callbacks
->reloc_overflow
)
2793 (flaginfo
->info
, NULL
,
2794 (link_order
->type
== bfd_section_reloc_link_order
2795 ? bfd_section_name (link_order
->u
.reloc
.p
->u
.section
)
2796 : link_order
->u
.reloc
.p
->u
.name
),
2797 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2798 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0);
2801 loc
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
2803 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
2810 /* Store the reloc information in the right place. It will get
2811 swapped and written out at the end of the final_link routine. */
2812 irel
= (flaginfo
->section_info
[output_section
->target_index
].relocs
2813 + output_section
->reloc_count
);
2814 rel_hash_ptr
= (flaginfo
->section_info
[output_section
->target_index
].rel_hashes
2815 + output_section
->reloc_count
);
2817 memset (irel
, 0, sizeof (struct internal_reloc
));
2818 *rel_hash_ptr
= NULL
;
2820 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2822 if (link_order
->type
== bfd_section_reloc_link_order
)
2824 /* We need to somehow locate a symbol in the right section. The
2825 symbol must either have a value of zero, or we must adjust
2826 the addend by the value of the symbol. FIXME: Write this
2827 when we need it. The old linker couldn't handle this anyhow. */
2829 *rel_hash_ptr
= NULL
;
2834 struct coff_link_hash_entry
*h
;
2836 h
= ((struct coff_link_hash_entry
*)
2837 bfd_wrapped_link_hash_lookup (output_bfd
, flaginfo
->info
,
2838 link_order
->u
.reloc
.p
->u
.name
,
2839 false, false, true));
2843 irel
->r_symndx
= h
->indx
;
2846 /* Set the index to -2 to force this symbol to get
2855 (*flaginfo
->info
->callbacks
->unattached_reloc
)
2856 (flaginfo
->info
, link_order
->u
.reloc
.p
->u
.name
,
2857 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0);
2862 /* FIXME: Is this always right? */
2863 irel
->r_type
= howto
->type
;
2865 /* r_size is only used on the RS/6000, which needs its own linker
2866 routines anyhow. r_extern is only used for ECOFF. */
2868 /* FIXME: What is the right value for r_offset? Is zero OK? */
2869 ++output_section
->reloc_count
;
2874 /* A basic reloc handling routine which may be used by processors with
2878 _bfd_coff_generic_relocate_section (bfd
*output_bfd
,
2879 struct bfd_link_info
*info
,
2881 asection
*input_section
,
2883 struct internal_reloc
*relocs
,
2884 struct internal_syment
*syms
,
2885 asection
**sections
)
2887 struct internal_reloc
*rel
;
2888 struct internal_reloc
*relend
;
2891 relend
= rel
+ input_section
->reloc_count
;
2892 for (; rel
< relend
; rel
++)
2895 struct coff_link_hash_entry
*h
;
2896 struct internal_syment
*sym
;
2900 reloc_howto_type
*howto
;
2901 bfd_reloc_status_type rstat
;
2903 symndx
= rel
->r_symndx
;
2911 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2914 /* xgettext: c-format */
2915 (_("%pB: illegal symbol index %ld in relocs"), input_bfd
, symndx
);
2920 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2921 sym
= syms
+ symndx
;
2924 /* COFF treats common symbols in one of two ways. Either the
2925 size of the symbol is included in the section contents, or it
2926 is not. We assume that the size is not included, and force
2927 the rtype_to_howto function to adjust the addend as needed. */
2928 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2929 addend
= - sym
->n_value
;
2933 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2938 /* If we are doing a relocatable link, then we can just ignore
2939 a PC relative reloc that is pcrel_offset. It will already
2940 have the correct value. If this is not a relocatable link,
2941 then we should ignore the symbol value. */
2942 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2944 if (bfd_link_relocatable (info
))
2946 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2947 addend
+= sym
->n_value
;
2956 sec
= bfd_abs_section_ptr
;
2961 sec
= sections
[symndx
];
2963 /* PR 19623: Relocations against symbols in
2964 the absolute sections should ignored. */
2965 if (bfd_is_abs_section (sec
))
2968 val
= (sec
->output_section
->vma
2969 + sec
->output_offset
2971 if (! obj_pe (input_bfd
))
2977 if (h
->root
.type
== bfd_link_hash_defined
2978 || h
->root
.type
== bfd_link_hash_defweak
)
2980 /* Defined weak symbols are a GNU extension. */
2981 sec
= h
->root
.u
.def
.section
;
2982 val
= (h
->root
.u
.def
.value
2983 + sec
->output_section
->vma
2984 + sec
->output_offset
);
2987 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2989 if (h
->symbol_class
== C_NT_WEAK
&& h
->numaux
== 1)
2991 /* See _Microsoft Portable Executable and Common Object
2992 File Format Specification_, section 5.5.3.
2993 Note that weak symbols without aux records are a GNU
2995 FIXME: All weak externals are treated as having
2996 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
2997 These behave as per SVR4 ABI: A library member
2998 will resolve a weak external only if a normal
2999 external causes the library member to be linked.
3000 See also linker.c: generic_link_check_archive_element. */
3001 struct coff_link_hash_entry
*h2
=
3002 h
->auxbfd
->tdata
.coff_obj_data
->sym_hashes
[
3003 h
->aux
->x_sym
.x_tagndx
.l
];
3005 if (!h2
|| h2
->root
.type
== bfd_link_hash_undefined
)
3007 sec
= bfd_abs_section_ptr
;
3012 sec
= h2
->root
.u
.def
.section
;
3013 val
= h2
->root
.u
.def
.value
3014 + sec
->output_section
->vma
+ sec
->output_offset
;
3018 /* This is a GNU extension. */
3022 else if (! bfd_link_relocatable (info
))
3024 (*info
->callbacks
->undefined_symbol
)
3025 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
3026 rel
->r_vaddr
- input_section
->vma
, true);
3027 /* Stop the linker from issueing errors about truncated relocs
3028 referencing this undefined symbol by giving it an address
3029 that should be in range. */
3030 val
= input_section
->output_section
->vma
;
3034 /* If the input section defining the symbol has been discarded
3035 then zero this reloc field. */
3036 if (sec
!= NULL
&& discarded_section (sec
))
3038 _bfd_clear_contents (howto
, input_bfd
, input_section
,
3039 contents
, rel
->r_vaddr
- input_section
->vma
);
3043 if (info
->base_file
)
3045 /* Emit a reloc if the backend thinks it needs it. */
3046 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
3048 /* Relocation to a symbol in a section which isn't
3049 absolute. We output the address here to a file.
3050 This file is then read by dlltool when generating the
3051 reloc section. Note that the base file is not
3052 portable between systems. We write out a bfd_vma here,
3053 and dlltool reads in a bfd_vma. */
3054 bfd_vma addr
= (rel
->r_vaddr
3055 - input_section
->vma
3056 + input_section
->output_offset
3057 + input_section
->output_section
->vma
);
3058 if (coff_data (output_bfd
)->pe
)
3059 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
3060 if (fwrite (&addr
, 1, sizeof (bfd_vma
), (FILE *) info
->base_file
)
3061 != sizeof (bfd_vma
))
3063 bfd_set_error (bfd_error_system_call
);
3069 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3071 rel
->r_vaddr
- input_section
->vma
,
3080 case bfd_reloc_outofrange
:
3082 /* xgettext: c-format */
3083 (_("%pB: bad reloc address %#" PRIx64
" in section `%pA'"),
3084 input_bfd
, (uint64_t) rel
->r_vaddr
, input_section
);
3086 case bfd_reloc_overflow
:
3089 /* Ignore any weak undef symbols that may have overflowed. Due to
3090 PR ld/19011 the base address is now in the upper 64-bit address
3091 range. This means that when _bfd_final_link_relocate calculates
3092 the overlow it takes the distance between the symbol and the VMA
3093 which will now always overflow as 0 - 64-bit addr > 32-bit range
3094 of the relocation. This ends up creating PR ld/26659. */
3096 /* Reverse the hack where 4 is subtracted from the addend. */
3097 && (addend
+ 4) == 0
3098 && sym
->n_sclass
== C_NT_WEAK
3099 && bfd_coff_classify_symbol (output_bfd
, sym
)
3100 == COFF_SYMBOL_UNDEFINED
)
3104 char buf
[SYMNMLEN
+ 1];
3112 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3117 (*info
->callbacks
->reloc_overflow
)
3118 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3119 (bfd_vma
) 0, input_bfd
, input_section
,
3120 rel
->r_vaddr
- input_section
->vma
);