1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, 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. */
26 #include "coff/internal.h"
27 #include "coff/xcoff.h"
30 #include "libiberty.h"
31 #include "xcofflink.h"
33 /* This file holds the XCOFF linker code. */
35 #undef STRING_SIZE_SIZE
36 #define STRING_SIZE_SIZE 4
38 /* The list of import files. */
40 struct xcoff_import_file
42 /* The next entry in the list. */
43 struct xcoff_import_file
*next
;
48 /* The member name. */
52 /* Information we keep for each section in the output file during the
55 struct xcoff_link_section_info
57 /* The relocs to be output. */
58 struct internal_reloc
*relocs
;
59 /* For each reloc against a global symbol whose index was not known
60 when the reloc was handled, the global hash table entry. */
61 struct xcoff_link_hash_entry
**rel_hashes
;
62 /* If there is a TOC relative reloc against a global symbol, and the
63 index of the TOC symbol is not known when the reloc was handled,
64 an entry is added to this linked list. This is not an array,
65 like rel_hashes, because this case is quite uncommon. */
66 struct xcoff_toc_rel_hash
68 struct xcoff_toc_rel_hash
*next
;
69 struct xcoff_link_hash_entry
*h
;
70 struct internal_reloc
*rel
;
74 /* Information that the XCOFF linker collects about an archive. */
75 struct xcoff_archive_info
77 /* The archive described by this entry. */
80 /* The import path and import filename to use when referring to
81 this archive in the .loader section. */
85 /* True if the archive contains a dynamic object. */
86 unsigned int contains_shared_object_p
: 1;
88 /* True if the previous field is valid. */
89 unsigned int know_contains_shared_object_p
: 1;
92 struct xcoff_link_hash_table
94 struct bfd_link_hash_table root
;
96 /* The stub hash table. */
97 struct bfd_hash_table stub_hash_table
;
99 /* Info passed by the linker. */
100 struct bfd_xcoff_link_params
*params
;
102 /* The .debug string hash table. We need to compute this while
103 reading the input files, so that we know how large the .debug
104 section will be before we assign section positions. */
105 struct bfd_strtab_hash
*debug_strtab
;
107 /* The .debug section we will use for the final output. */
108 asection
*debug_section
;
110 /* The .loader section we will use for the final output. */
111 asection
*loader_section
;
113 /* The structure holding information about the .loader section. */
114 struct xcoff_loader_info ldinfo
;
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr
;
119 /* The .gl section we use to hold global linkage code. */
120 asection
*linkage_section
;
122 /* The .tc section we use to hold toc entries we build for global
124 asection
*toc_section
;
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection
*descriptor_section
;
130 /* The list of import files. */
131 struct xcoff_import_file
*imports
;
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align
;
136 /* Whether the .text section must be read-only. */
139 /* Whether -brtl was specified. */
142 /* Whether garbage collection was done. */
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
148 struct xcoff_link_size_list
*next
;
149 struct xcoff_link_hash_entry
*h
;
154 /* Information about archives. */
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection
*special_sections
[XCOFF_NUMBER_OF_SPECIAL_SECTIONS
];
161 /* Information that we pass around while doing the final link step. */
163 struct xcoff_final_link_info
165 /* General link information. */
166 struct bfd_link_info
*info
;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash
*strtab
;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info
*section_info
;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index
;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file
;
178 /* Symbol index of TOC symbol. */
180 /* Start of .loader symbols. */
182 /* Next .loader reloc to swap out. */
184 /* File position of start of line numbers. */
185 file_ptr line_filepos
;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment
*internal_syms
;
188 /* Buffer large enough to hold output indices of symbols of any
191 /* Buffer large enough to hold output symbols for any input file. */
193 /* Buffer large enough to hold external line numbers for any input
196 /* Buffer large enough to hold any input section. */
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte
*external_relocs
;
202 #define xcoff_stub_hash_entry(ent) \
203 ((struct xcoff_stub_hash_entry *)(ent))
205 #define xcoff_stub_hash_lookup(table, string, create, copy) \
206 ((struct xcoff_stub_hash_entry *) \
207 bfd_hash_lookup ((table), (string), (create), (copy)))
209 static bool xcoff_mark (struct bfd_link_info
*, asection
*);
213 /* Routines to read XCOFF dynamic information. This don't really
214 belong here, but we already have the ldsym manipulation routines
217 /* Read the contents of a section. */
220 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
222 if (coff_section_data (abfd
, sec
) == NULL
)
224 size_t amt
= sizeof (struct coff_section_tdata
);
226 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
227 if (sec
->used_by_bfd
== NULL
)
231 bfd_byte
*contents
= coff_section_data (abfd
, sec
)->contents
;
232 if (contents
== NULL
)
234 if (bfd_malloc_and_get_section (abfd
, sec
, &contents
))
235 coff_section_data (abfd
, sec
)->contents
= contents
;
246 /* Get the size required to hold the dynamic symbols. */
249 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
253 struct internal_ldhdr ldhdr
;
255 if ((abfd
->flags
& DYNAMIC
) == 0)
257 bfd_set_error (bfd_error_invalid_operation
);
261 lsec
= bfd_get_section_by_name (abfd
, ".loader");
262 if (lsec
== NULL
|| (lsec
->flags
& SEC_HAS_CONTENTS
) == 0)
264 bfd_set_error (bfd_error_no_symbols
);
268 contents
= xcoff_get_section_contents (abfd
, lsec
);
272 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
274 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
277 /* Get the dynamic symbols. */
280 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
284 struct internal_ldhdr ldhdr
;
286 bfd_byte
*elsym
, *elsymend
;
287 coff_symbol_type
*symbuf
;
289 if ((abfd
->flags
& DYNAMIC
) == 0)
291 bfd_set_error (bfd_error_invalid_operation
);
295 lsec
= bfd_get_section_by_name (abfd
, ".loader");
296 if (lsec
== NULL
|| (lsec
->flags
& SEC_HAS_CONTENTS
) == 0)
298 bfd_set_error (bfd_error_no_symbols
);
302 contents
= xcoff_get_section_contents (abfd
, lsec
);
306 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
308 strings
= (char *) contents
+ ldhdr
.l_stoff
;
310 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
314 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
316 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
317 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
319 struct internal_ldsym ldsym
;
321 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
323 symbuf
->symbol
.the_bfd
= abfd
;
325 if (ldsym
._l
._l_l
._l_zeroes
== 0)
326 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
331 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
334 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
336 symbuf
->symbol
.name
= c
;
339 if (ldsym
.l_smclas
== XMC_XO
)
340 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
342 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
344 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
346 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
347 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
349 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
350 symbuf
->symbol
.flags
|= BSF_WEAK
;
352 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
355 /* FIXME: We have no way to record the other information stored
356 with the loader symbol. */
357 *psyms
= (asymbol
*) symbuf
;
362 return ldhdr
.l_nsyms
;
365 /* Get the size required to hold the dynamic relocs. */
368 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
372 struct internal_ldhdr ldhdr
;
374 if ((abfd
->flags
& DYNAMIC
) == 0)
376 bfd_set_error (bfd_error_invalid_operation
);
380 lsec
= bfd_get_section_by_name (abfd
, ".loader");
381 if (lsec
== NULL
|| (lsec
->flags
& SEC_HAS_CONTENTS
) == 0)
383 bfd_set_error (bfd_error_no_symbols
);
387 contents
= xcoff_get_section_contents (abfd
, lsec
);
391 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
393 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
396 /* Get the dynamic relocs. */
399 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
405 struct internal_ldhdr ldhdr
;
407 bfd_byte
*elrel
, *elrelend
;
409 if ((abfd
->flags
& DYNAMIC
) == 0)
411 bfd_set_error (bfd_error_invalid_operation
);
415 lsec
= bfd_get_section_by_name (abfd
, ".loader");
416 if (lsec
== NULL
|| (lsec
->flags
& SEC_HAS_CONTENTS
) == 0)
418 bfd_set_error (bfd_error_no_symbols
);
422 contents
= xcoff_get_section_contents (abfd
, lsec
);
426 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
428 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
432 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
434 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
435 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
438 struct internal_ldrel ldrel
;
440 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
442 if (ldrel
.l_symndx
>= 3)
443 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
449 switch (ldrel
.l_symndx
)
465 sec
= bfd_get_section_by_name (abfd
, name
);
468 bfd_set_error (bfd_error_bad_value
);
472 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
475 relbuf
->address
= ldrel
.l_vaddr
;
478 /* Most dynamic relocs have the same type. FIXME: This is only
479 correct if ldrel.l_rtype == 0. In other cases, we should use
480 a different howto. */
481 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
483 /* FIXME: We have no way to record the l_rsecnm field. */
490 return ldhdr
.l_nreloc
;
493 /* Hash functions for xcoff_link_hash_table's archive_info. */
496 xcoff_archive_info_hash (const void *data
)
498 const struct xcoff_archive_info
*info
;
500 info
= (const struct xcoff_archive_info
*) data
;
501 return htab_hash_pointer (info
->archive
);
505 xcoff_archive_info_eq (const void *data1
, const void *data2
)
507 const struct xcoff_archive_info
*info1
;
508 const struct xcoff_archive_info
*info2
;
510 info1
= (const struct xcoff_archive_info
*) data1
;
511 info2
= (const struct xcoff_archive_info
*) data2
;
512 return info1
->archive
== info2
->archive
;
515 /* Return information about archive ARCHIVE. Return NULL on error. */
517 static struct xcoff_archive_info
*
518 xcoff_get_archive_info (struct bfd_link_info
*info
, bfd
*archive
)
520 struct xcoff_link_hash_table
*htab
;
521 struct xcoff_archive_info
*entryp
, entry
;
524 htab
= xcoff_hash_table (info
);
525 entry
.archive
= archive
;
526 slot
= htab_find_slot (htab
->archive_info
, &entry
, INSERT
);
533 entryp
= bfd_zalloc (info
->output_bfd
, sizeof (entry
));
537 entryp
->archive
= archive
;
544 /* Initialize an entry in the stub hash table. */
545 static struct bfd_hash_entry
*
546 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
547 struct bfd_hash_table
*table
,
550 /* Allocate the structure if it has not already been allocated by a
554 entry
= bfd_hash_allocate (table
,
555 sizeof (struct xcoff_stub_hash_entry
));
560 /* Call the allocation method of the superclass. */
561 entry
= bfd_hash_newfunc (entry
, table
, string
);
564 struct xcoff_stub_hash_entry
*hsh
;
566 /* Initialize the local fields. */
567 hsh
= (struct xcoff_stub_hash_entry
*) entry
;
568 hsh
->stub_type
= xcoff_stub_none
;
570 hsh
->stub_offset
= 0;
571 hsh
->target_section
= NULL
;
578 /* Routine to create an entry in an XCOFF link hash table. */
580 static struct bfd_hash_entry
*
581 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
582 struct bfd_hash_table
*table
,
585 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
587 /* Allocate the structure if it has not already been allocated by a
590 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
594 /* Call the allocation method of the superclass. */
595 ret
= ((struct xcoff_link_hash_entry
*)
596 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
600 /* Set local fields. */
602 ret
->toc_section
= NULL
;
603 ret
->u
.toc_indx
= -1;
604 ret
->descriptor
= NULL
;
608 ret
->smclas
= XMC_UA
;
611 return (struct bfd_hash_entry
*) ret
;
614 /* Destroy an XCOFF link hash table. */
617 _bfd_xcoff_bfd_link_hash_table_free (bfd
*obfd
)
619 struct xcoff_link_hash_table
*ret
;
621 ret
= (struct xcoff_link_hash_table
*) obfd
->link
.hash
;
622 if (ret
->archive_info
)
623 htab_delete (ret
->archive_info
);
624 if (ret
->debug_strtab
)
625 _bfd_stringtab_free (ret
->debug_strtab
);
627 bfd_hash_table_free (&ret
->stub_hash_table
);
628 _bfd_generic_link_hash_table_free (obfd
);
631 /* Create an XCOFF link hash table. */
633 struct bfd_link_hash_table
*
634 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
636 struct xcoff_link_hash_table
*ret
;
637 bool isxcoff64
= false;
638 size_t amt
= sizeof (* ret
);
640 ret
= bfd_zmalloc (amt
);
643 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
644 sizeof (struct xcoff_link_hash_entry
)))
650 /* Init the stub hash table too. */
651 if (!bfd_hash_table_init (&ret
->stub_hash_table
, stub_hash_newfunc
,
652 sizeof (struct xcoff_stub_hash_entry
)))
654 _bfd_xcoff_bfd_link_hash_table_free (abfd
);
658 isxcoff64
= bfd_coff_debug_string_prefix_length (abfd
) == 4;
660 ret
->debug_strtab
= _bfd_xcoff_stringtab_init (isxcoff64
);
661 ret
->archive_info
= htab_create (37, xcoff_archive_info_hash
,
662 xcoff_archive_info_eq
, NULL
);
663 if (!ret
->debug_strtab
|| !ret
->archive_info
)
665 _bfd_xcoff_bfd_link_hash_table_free (abfd
);
668 ret
->root
.hash_table_free
= _bfd_xcoff_bfd_link_hash_table_free
;
670 /* The linker will always generate a full a.out header. We need to
671 record that fact now, before the sizeof_headers routine could be
673 xcoff_data (abfd
)->full_aouthdr
= true;
678 /* Read internal relocs for an XCOFF csect. This is a wrapper around
679 _bfd_coff_read_internal_relocs which tries to take advantage of any
680 relocs which may have been cached for the enclosing section. */
682 static struct internal_reloc
*
683 xcoff_read_internal_relocs (bfd
*abfd
,
686 bfd_byte
*external_relocs
,
687 bool require_internal
,
688 struct internal_reloc
*internal_relocs
)
690 if (coff_section_data (abfd
, sec
) != NULL
691 && coff_section_data (abfd
, sec
)->relocs
== NULL
692 && xcoff_section_data (abfd
, sec
) != NULL
)
696 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
698 if (enclosing
!= NULL
699 && (coff_section_data (abfd
, enclosing
) == NULL
700 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
702 && enclosing
->reloc_count
> 0)
704 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
705 external_relocs
, false, NULL
)
710 if (enclosing
!= NULL
711 && coff_section_data (abfd
, enclosing
) != NULL
712 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
716 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
717 / bfd_coff_relsz (abfd
));
719 if (! require_internal
)
720 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
721 memcpy (internal_relocs
,
722 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
723 sec
->reloc_count
* sizeof (struct internal_reloc
));
724 return internal_relocs
;
728 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
729 require_internal
, internal_relocs
);
732 /* Split FILENAME into an import path and an import filename,
733 storing them in *IMPPATH and *IMPFILE respectively. */
736 bfd_xcoff_split_import_path (bfd
*abfd
, const char *filename
,
737 const char **imppath
, const char **impfile
)
743 base
= lbasename (filename
);
744 length
= base
- filename
;
746 /* The filename has no directory component, so use an empty path. */
748 else if (length
== 1)
749 /* The filename is in the root directory. */
753 /* Extract the (non-empty) directory part. Note that we don't
754 need to strip duplicate directory separators from any part
755 of the string; the native linker doesn't do that either. */
756 path
= bfd_alloc (abfd
, length
);
759 memcpy (path
, filename
, length
- 1);
760 path
[length
- 1] = 0;
767 /* Set ARCHIVE's import path as though its filename had been given
771 bfd_xcoff_set_archive_import_path (struct bfd_link_info
*info
,
772 bfd
*archive
, const char *filename
)
774 struct xcoff_archive_info
*archive_info
;
776 archive_info
= xcoff_get_archive_info (info
, archive
);
777 return (archive_info
!= NULL
778 && bfd_xcoff_split_import_path (archive
, filename
,
779 &archive_info
->imppath
,
780 &archive_info
->impfile
));
783 /* H is an imported symbol. Set the import module's path, file and member
784 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
785 no specific import module is specified. */
788 xcoff_set_import_path (struct bfd_link_info
*info
,
789 struct xcoff_link_hash_entry
*h
,
790 const char *imppath
, const char *impfile
,
791 const char *impmember
)
794 struct xcoff_import_file
**pp
;
796 /* We overload the ldindx field to hold the l_ifile value for this
798 BFD_ASSERT (h
->ldsym
== NULL
);
799 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
804 /* We start c at 1 because the first entry in the import list is
805 reserved for the library search path. */
806 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
808 pp
= &(*pp
)->next
, ++c
)
810 if (filename_cmp ((*pp
)->path
, imppath
) == 0
811 && filename_cmp ((*pp
)->file
, impfile
) == 0
812 && filename_cmp ((*pp
)->member
, impmember
) == 0)
818 struct xcoff_import_file
*n
;
819 size_t amt
= sizeof (*n
);
821 n
= bfd_alloc (info
->output_bfd
, amt
);
827 n
->member
= impmember
;
835 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
836 Return true if LDSYM defines H. */
839 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry
*h
,
840 struct internal_ldsym
*ldsym
)
842 /* If we didn't know about H before processing LDSYM, LDSYM
843 definitely defines H. */
844 if (h
->root
.type
== bfd_link_hash_new
)
847 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
848 dynamic symbol, LDSYM trumps the current definition of H. */
849 if ((ldsym
->l_smtype
& L_WEAK
) == 0
850 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
851 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
852 && (h
->root
.type
== bfd_link_hash_defweak
853 || h
->root
.type
== bfd_link_hash_undefweak
))
856 /* If H is currently undefined, LDSYM defines it.
857 However, if H has a hidden visibility, LDSYM must not
859 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
860 && (h
->root
.type
== bfd_link_hash_undefined
861 || h
->root
.type
== bfd_link_hash_undefweak
)
862 && (h
->visibility
!= SYM_V_HIDDEN
863 && h
->visibility
!= SYM_V_INTERNAL
))
869 /* This function is used to add symbols from a dynamic object to the
870 global symbol table. */
873 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
877 struct internal_ldhdr ldhdr
;
879 bfd_byte
*elsym
, *elsymend
;
880 struct xcoff_import_file
*n
;
882 struct xcoff_import_file
**pp
;
884 /* We can only handle a dynamic object if we are generating an XCOFF
886 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
889 (_("%pB: XCOFF shared object when not producing XCOFF output"),
891 bfd_set_error (bfd_error_invalid_operation
);
895 /* The symbols we use from a dynamic object are not the symbols in
896 the normal symbol table, but, rather, the symbols in the export
897 table. If there is a global symbol in a dynamic object which is
898 not in the export table, the loader will not be able to find it,
899 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
900 libc.a has symbols in the export table which are not in the
903 /* Read in the .loader section. FIXME: We should really use the
904 o_snloader field in the a.out header, rather than grabbing the
906 lsec
= bfd_get_section_by_name (abfd
, ".loader");
907 if (lsec
== NULL
|| (lsec
->flags
& SEC_HAS_CONTENTS
) == 0)
910 (_("%pB: dynamic object with no .loader section"),
912 bfd_set_error (bfd_error_no_symbols
);
916 contents
= xcoff_get_section_contents (abfd
, lsec
);
920 /* Remove the sections from this object, so that they do not get
921 included in the link. */
922 bfd_section_list_clear (abfd
);
924 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
926 strings
= (char *) contents
+ ldhdr
.l_stoff
;
928 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
930 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
932 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
934 struct internal_ldsym ldsym
;
935 char nambuf
[SYMNMLEN
+ 1];
937 struct xcoff_link_hash_entry
*h
;
939 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
941 /* We are only interested in exported symbols. */
942 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
945 if (ldsym
._l
._l_l
._l_zeroes
== 0)
946 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
949 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
950 nambuf
[SYMNMLEN
] = '\0';
954 /* Normally we could not call xcoff_link_hash_lookup in an add
955 symbols routine, since we might not be using an XCOFF hash
956 table. However, we verified above that we are using an XCOFF
959 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true,
964 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
967 h
->flags
|= XCOFF_DEF_DYNAMIC
;
968 h
->smclas
= ldsym
.l_smclas
;
969 if (h
->smclas
== XMC_XO
)
971 /* This symbol has an absolute value. */
972 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
973 h
->root
.type
= bfd_link_hash_defweak
;
975 h
->root
.type
= bfd_link_hash_defined
;
976 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
977 h
->root
.u
.def
.value
= ldsym
.l_value
;
981 /* Otherwise, we don't bother to actually define the symbol,
982 since we don't have a section to put it in anyhow.
983 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
984 should be imported from the symbol's undef.abfd. */
985 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
986 h
->root
.type
= bfd_link_hash_undefweak
;
988 h
->root
.type
= bfd_link_hash_undefined
;
989 h
->root
.u
.undef
.abfd
= abfd
;
992 /* If this symbol defines a function descriptor, then it
993 implicitly defines the function code as well. */
994 if (h
->smclas
== XMC_DS
995 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
996 h
->flags
|= XCOFF_DESCRIPTOR
;
997 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
999 struct xcoff_link_hash_entry
*hds
;
1001 hds
= h
->descriptor
;
1006 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
1010 strcpy (dsnm
+ 1, name
);
1011 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
1017 hds
->descriptor
= h
;
1018 h
->descriptor
= hds
;
1021 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
1023 hds
->root
.type
= h
->root
.type
;
1024 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
1025 if (h
->smclas
== XMC_XO
)
1027 /* An absolute symbol appears to actually define code, not a
1028 function descriptor. This is how some math functions are
1029 implemented on AIX 4.1. */
1030 hds
->smclas
= XMC_XO
;
1031 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
1032 hds
->root
.u
.def
.value
= ldsym
.l_value
;
1036 hds
->smclas
= XMC_PR
;
1037 hds
->root
.u
.undef
.abfd
= abfd
;
1038 /* We do not want to add this to the undefined
1046 coff_section_data (abfd
, lsec
)->contents
= NULL
;
1048 /* Record this file in the import files. */
1049 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
1054 if (abfd
->my_archive
== NULL
|| bfd_is_thin_archive (abfd
->my_archive
))
1056 if (!bfd_xcoff_split_import_path (abfd
, bfd_get_filename (abfd
),
1057 &n
->path
, &n
->file
))
1063 struct xcoff_archive_info
*archive_info
;
1065 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1066 if (!archive_info
->impfile
)
1068 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1069 bfd_get_filename (archive_info
1071 &archive_info
->imppath
,
1072 &archive_info
->impfile
))
1075 n
->path
= archive_info
->imppath
;
1076 n
->file
= archive_info
->impfile
;
1077 n
->member
= bfd_get_filename (abfd
);
1080 /* We start c at 1 because the first import file number is reserved
1082 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1084 pp
= &(*pp
)->next
, ++c
)
1088 xcoff_data (abfd
)->import_file_id
= c
;
1093 /* xcoff_link_create_extra_sections
1095 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1098 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1100 bool return_value
= false;
1102 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1104 /* We need to build a .loader section, so we do it here. This
1105 won't work if we're producing an XCOFF output file with no
1106 XCOFF input files. FIXME. */
1108 if (!bfd_link_relocatable (info
)
1109 && xcoff_hash_table (info
)->loader_section
== NULL
)
1112 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1114 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1118 xcoff_hash_table (info
)->loader_section
= lsec
;
1121 /* Likewise for the linkage section. */
1122 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1125 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1128 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1132 xcoff_hash_table (info
)->linkage_section
= lsec
;
1133 lsec
->alignment_power
= 2;
1136 /* Likewise for the TOC section. */
1137 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1140 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1143 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1147 xcoff_hash_table (info
)->toc_section
= tsec
;
1148 tsec
->alignment_power
= 2;
1151 /* Likewise for the descriptor section. */
1152 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1155 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1158 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1162 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1163 dsec
->alignment_power
= 2;
1166 /* Likewise for the .debug section. */
1167 if (xcoff_hash_table (info
)->debug_section
== NULL
1168 && info
->strip
!= strip_all
)
1171 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1173 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1177 xcoff_hash_table (info
)->debug_section
= dsec
;
1181 return_value
= true;
1185 return return_value
;
1188 /* Returns the index of reloc in RELOCS with the least address greater
1189 than or equal to ADDRESS. The relocs are sorted by address. */
1191 static bfd_size_type
1192 xcoff_find_reloc (struct internal_reloc
*relocs
,
1193 bfd_size_type count
,
1196 bfd_size_type min
, max
, this;
1200 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1209 /* Do a binary search over (min,max]. */
1210 while (min
+ 1 < max
)
1214 this = (max
+ min
) / 2;
1215 raddr
= relocs
[this].r_vaddr
;
1216 if (raddr
> address
)
1218 else if (raddr
< address
)
1227 if (relocs
[min
].r_vaddr
< address
)
1231 && relocs
[min
- 1].r_vaddr
== address
)
1237 /* Return true if the symbol has to be added to the linker hash
1240 xcoff_link_add_symbols_to_hash_table (struct internal_syment sym
,
1241 union internal_auxent aux
)
1243 /* External symbols must be added. */
1244 if (EXTERN_SYM_P (sym
.n_sclass
))
1247 /* Hidden TLS symbols must be added to verify TLS relocations
1248 in xcoff_reloc_type_tls. */
1249 if (sym
.n_sclass
== C_HIDEXT
1250 && ((aux
.x_csect
.x_smclas
== XMC_TL
1251 || aux
.x_csect
.x_smclas
== XMC_UL
)))
1257 /* Add all the symbols from an object file to the hash table.
1259 XCOFF is a weird format. A normal XCOFF .o files will have three
1260 COFF sections--.text, .data, and .bss--but each COFF section will
1261 contain many csects. These csects are described in the symbol
1262 table. From the linker's point of view, each csect must be
1263 considered a section in its own right. For example, a TOC entry is
1264 handled as a small XMC_TC csect. The linker must be able to merge
1265 different TOC entries together, which means that it must be able to
1266 extract the XMC_TC csects from the .data section of the input .o
1269 From the point of view of our linker, this is, of course, a hideous
1270 nightmare. We cope by actually creating sections for each csect,
1271 and discarding the original sections. We then have to handle the
1272 relocation entries carefully, since the only way to tell which
1273 csect they belong to is to examine the address. */
1276 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1278 unsigned int n_tmask
;
1279 unsigned int n_btshft
;
1281 bfd_size_type symcount
;
1282 struct xcoff_link_hash_entry
**sym_hash
;
1283 asection
**csect_cache
;
1284 unsigned int *lineno_counts
;
1285 bfd_size_type linesz
;
1287 asection
*last_real
;
1290 unsigned int csect_index
;
1291 asection
*first_csect
;
1292 bfd_size_type symesz
;
1295 struct reloc_info_struct
1297 struct internal_reloc
*relocs
;
1300 } *reloc_info
= NULL
;
1302 unsigned short visibility
;
1304 keep_syms
= obj_coff_keep_syms (abfd
);
1306 if ((abfd
->flags
& DYNAMIC
) != 0
1307 && ! info
->static_link
)
1309 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1313 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1314 if (! xcoff_link_create_extra_sections (abfd
, info
))
1317 if ((abfd
->flags
& DYNAMIC
) != 0
1318 && ! info
->static_link
)
1321 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1322 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1324 /* Define macros so that ISFCN, et. al., macros work correctly. */
1325 #define N_TMASK n_tmask
1326 #define N_BTSHFT n_btshft
1328 if (info
->keep_memory
)
1329 default_copy
= false;
1331 default_copy
= true;
1333 symcount
= obj_raw_syment_count (abfd
);
1335 /* We keep a list of the linker hash table entries that correspond
1336 to each external symbol. */
1337 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1338 sym_hash
= bfd_zalloc (abfd
, amt
);
1339 if (sym_hash
== NULL
&& symcount
!= 0)
1341 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1343 /* Because of the weird stuff we are doing with XCOFF csects, we can
1344 not easily determine which section a symbol is in, so we store
1345 the information in the tdata for the input file. */
1346 amt
= symcount
* sizeof (asection
*);
1347 csect_cache
= bfd_zalloc (abfd
, amt
);
1348 if (csect_cache
== NULL
&& symcount
!= 0)
1350 xcoff_data (abfd
)->csects
= csect_cache
;
1352 /* We garbage-collect line-number information on a symbol-by-symbol
1353 basis, so we need to have quick access to the number of entries
1355 amt
= symcount
* sizeof (unsigned int);
1356 lineno_counts
= bfd_zalloc (abfd
, amt
);
1357 if (lineno_counts
== NULL
&& symcount
!= 0)
1359 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1361 /* While splitting sections into csects, we need to assign the
1362 relocs correctly. The relocs and the csects must both be in
1363 order by VMA within a given section, so we handle this by
1364 scanning along the relocs as we process the csects. We index
1365 into reloc_info using the section target_index. */
1366 amt
= abfd
->section_count
+ 1;
1367 amt
*= sizeof (struct reloc_info_struct
);
1368 reloc_info
= bfd_zmalloc (amt
);
1369 if (reloc_info
== NULL
)
1372 /* Read in the relocs and line numbers for each section. */
1373 linesz
= bfd_coff_linesz (abfd
);
1375 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1379 if ((o
->flags
& SEC_RELOC
) != 0)
1381 reloc_info
[o
->target_index
].relocs
=
1382 xcoff_read_internal_relocs (abfd
, o
, true, NULL
, false, NULL
);
1383 amt
= o
->reloc_count
;
1384 amt
*= sizeof (asection
*);
1385 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1386 if (reloc_info
[o
->target_index
].csects
== NULL
)
1390 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1391 && o
->lineno_count
> 0)
1395 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0)
1397 if (_bfd_mul_overflow (linesz
, o
->lineno_count
, &amt
))
1399 bfd_set_error (bfd_error_file_too_big
);
1402 linenos
= _bfd_malloc_and_read (abfd
, amt
, amt
);
1403 if (linenos
== NULL
)
1405 reloc_info
[o
->target_index
].linenos
= linenos
;
1409 /* Don't let the linker relocation routines discard the symbols. */
1410 obj_coff_keep_syms (abfd
) = true;
1416 symesz
= bfd_coff_symesz (abfd
);
1417 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1418 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1419 esym_end
= esym
+ symcount
* symesz
;
1421 while (esym
< esym_end
)
1423 struct internal_syment sym
;
1424 union internal_auxent aux
;
1426 char buf
[SYMNMLEN
+ 1];
1430 struct xcoff_link_hash_entry
*set_toc
;
1432 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1434 /* In this pass we are only interested in symbols with csect
1436 if (!CSECT_SYM_P (sym
.n_sclass
))
1439 Normally csect is a .pr, .rw etc. created in the loop
1440 If C_FILE or first time, handle special
1442 Advance esym, sym_hash, csect_hash ptrs. */
1443 if (sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1446 *csect_cache
= csect
;
1447 else if (first_csect
== NULL
1448 || sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1449 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1451 *csect_cache
= NULL
;
1452 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1453 sym_hash
+= sym
.n_numaux
+ 1;
1454 csect_cache
+= sym
.n_numaux
+ 1;
1455 lineno_counts
+= sym
.n_numaux
+ 1;
1460 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1465 /* If this symbol has line number information attached to it,
1466 and we're not stripping it, count the number of entries and
1467 add them to the count for this csect. In the final link pass
1468 we are going to attach line number information by symbol,
1469 rather than by section, in order to more easily handle
1470 garbage collection. */
1471 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1474 && ISFCN (sym
.n_type
))
1476 union internal_auxent auxlin
;
1478 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1479 sym
.n_type
, sym
.n_sclass
,
1480 0, sym
.n_numaux
, (void *) &auxlin
);
1482 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1484 asection
*enclosing
;
1485 bfd_signed_vma linoff
;
1487 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1488 if (enclosing
== NULL
)
1491 /* xgettext:c-format */
1492 (_("%pB: `%s' has line numbers but no enclosing section"),
1494 bfd_set_error (bfd_error_bad_value
);
1497 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1498 - enclosing
->line_filepos
);
1499 /* Explicit cast to bfd_signed_vma for compiler. */
1500 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1502 struct internal_lineno lin
;
1503 bfd_byte
*linpstart
;
1505 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1507 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1509 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1511 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1514 bfd_byte
*linpend
, *linp
;
1516 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1517 + enclosing
->lineno_count
* linesz
);
1518 for (linp
= linpstart
+ linesz
;
1522 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1524 if (lin
.l_lnno
== 0)
1527 *lineno_counts
= (linp
- linpstart
) / linesz
;
1528 /* The setting of line_filepos will only be
1529 useful if all the line number entries for a
1530 csect are contiguous; this only matters for
1532 if (csect
->line_filepos
== 0)
1533 csect
->line_filepos
=
1534 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1540 /* Record visibility. */
1541 visibility
= sym
.n_type
& SYM_V_MASK
;
1543 /* Pick up the csect auxiliary information. */
1544 if (sym
.n_numaux
== 0)
1547 /* xgettext:c-format */
1548 (_("%pB: class %d symbol `%s' has no aux entries"),
1549 abfd
, sym
.n_sclass
, name
);
1550 bfd_set_error (bfd_error_bad_value
);
1554 bfd_coff_swap_aux_in (abfd
,
1555 (void *) (esym
+ symesz
* sym
.n_numaux
),
1556 sym
.n_type
, sym
.n_sclass
,
1557 sym
.n_numaux
- 1, sym
.n_numaux
,
1560 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1570 /* xgettext:c-format */
1571 (_("%pB: symbol `%s' has unrecognized csect type %d"),
1573 bfd_set_error (bfd_error_bad_value
);
1577 /* This is an external reference. */
1578 if (sym
.n_sclass
== C_HIDEXT
1579 || sym
.n_scnum
!= N_UNDEF
1580 || aux
.x_csect
.x_scnlen
.u64
!= 0)
1583 /* xgettext:c-format */
1584 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1586 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1587 aux
.x_csect
.x_scnlen
.u64
);
1588 bfd_set_error (bfd_error_bad_value
);
1592 /* An XMC_XO external reference is actually a reference to
1593 an absolute location. */
1594 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1595 section
= bfd_und_section_ptr
;
1598 section
= bfd_abs_section_ptr
;
1599 value
= sym
.n_value
;
1605 csect_index
= -(unsigned) 1;
1607 /* When we see a TOC anchor, we record the TOC value. */
1608 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1610 if (sym
.n_sclass
!= C_HIDEXT
1611 || aux
.x_csect
.x_scnlen
.u64
!= 0)
1614 /* xgettext:c-format */
1615 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRIu64
),
1616 abfd
, name
, sym
.n_sclass
, aux
.x_csect
.x_scnlen
.u64
);
1617 bfd_set_error (bfd_error_bad_value
);
1620 xcoff_data (abfd
)->toc
= sym
.n_value
;
1623 /* We must merge TOC entries for the same symbol. We can
1624 merge two TOC entries if they are both C_HIDEXT, they
1625 both have the same name, they are both 4 or 8 bytes long, and
1626 they both have a relocation table entry for an external
1627 symbol with the same name. Unfortunately, this means
1628 that we must look through the relocations. Ick.
1630 Logic for 32 bit vs 64 bit.
1631 32 bit has a csect length of 4 for TOC
1632 64 bit has a csect length of 8 for TOC
1634 An exception is made for TOC entries with a R_TLSML
1635 relocation. This relocation is made for the loader.
1636 We must check that the referenced symbol is the TOC entry
1639 The conditions to get past the if-check are not that bad.
1640 They are what is used to create the TOC csects in the first
1642 if (aux
.x_csect
.x_smclas
== XMC_TC
1643 && sym
.n_sclass
== C_HIDEXT
1644 && info
->output_bfd
->xvec
== abfd
->xvec
1645 && ((bfd_xcoff_is_xcoff32 (abfd
)
1646 && aux
.x_csect
.x_scnlen
.u64
== 4)
1647 || (bfd_xcoff_is_xcoff64 (abfd
)
1648 && aux
.x_csect
.x_scnlen
.u64
== 8)))
1650 asection
*enclosing
;
1651 struct internal_reloc
*relocs
;
1652 bfd_size_type relindx
;
1653 struct internal_reloc
*rel
;
1655 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1656 if (enclosing
== NULL
)
1659 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1660 amt
= enclosing
->reloc_count
;
1661 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1662 rel
= relocs
+ relindx
;
1664 /* 32 bit R_POS r_size is 31
1665 64 bit R_POS r_size is 63 */
1666 if (relindx
< enclosing
->reloc_count
1667 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1668 && (rel
->r_type
== R_POS
||
1669 rel
->r_type
== R_TLSML
)
1670 && ((bfd_xcoff_is_xcoff32 (abfd
)
1671 && rel
->r_size
== 31)
1672 || (bfd_xcoff_is_xcoff64 (abfd
)
1673 && rel
->r_size
== 63)))
1677 struct internal_syment relsym
;
1679 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1680 + rel
->r_symndx
* symesz
);
1681 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1682 if (EXTERN_SYM_P (relsym
.n_sclass
))
1684 const char *relname
;
1685 char relbuf
[SYMNMLEN
+ 1];
1687 struct xcoff_link_hash_entry
*h
;
1689 /* At this point we know that the TOC entry is
1690 for an externally visible symbol. */
1691 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1693 if (relname
== NULL
)
1696 /* We only merge TOC entries if the TC name is
1697 the same as the symbol name. This handles
1698 the normal case, but not common cases like
1699 SYM.P4 which gcc generates to store SYM + 4
1700 in the TOC. FIXME. */
1701 if (strcmp (name
, relname
) == 0)
1703 copy
= (! info
->keep_memory
1704 || relsym
._n
._n_n
._n_zeroes
!= 0
1705 || relsym
._n
._n_n
._n_offset
== 0);
1706 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1707 relname
, true, copy
,
1712 /* At this point h->root.type could be
1713 bfd_link_hash_new. That should be OK,
1714 since we know for sure that we will come
1715 across this symbol as we step through the
1718 /* We store h in *sym_hash for the
1719 convenience of the relocate_section
1723 if (h
->toc_section
!= NULL
)
1725 asection
**rel_csects
;
1727 /* We already have a TOC entry for this
1728 symbol, so we can just ignore this
1731 reloc_info
[enclosing
->target_index
].csects
;
1732 rel_csects
[relindx
] = bfd_und_section_ptr
;
1736 /* We are about to create a TOC entry for
1741 else if (rel
->r_type
== R_TLSML
)
1743 csect_index
= ((esym
1744 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1746 if (((unsigned long) rel
->r_symndx
) != csect_index
)
1749 /* xgettext:c-format */
1750 (_("%pB: TOC entry `%s' has a R_TLSML"
1751 "relocation not targeting itself"),
1753 bfd_set_error (bfd_error_bad_value
);
1761 asection
*enclosing
;
1763 /* We need to create a new section. We get the name from
1764 the csect storage mapping class, so that the linker can
1765 accumulate similar csects together. */
1767 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1771 /* The enclosing section is the main section : .data, .text
1772 or .bss that the csect is coming from. */
1773 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1774 if (enclosing
== NULL
)
1777 if (! bfd_is_abs_section (enclosing
)
1778 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1779 || (sym
.n_value
+ aux
.x_csect
.x_scnlen
.u64
1780 > enclosing
->vma
+ enclosing
->size
)))
1783 /* xgettext:c-format */
1784 (_("%pB: csect `%s' not in enclosing section"),
1786 bfd_set_error (bfd_error_bad_value
);
1789 csect
->vma
= sym
.n_value
;
1790 csect
->filepos
= (enclosing
->filepos
1793 csect
->size
= aux
.x_csect
.x_scnlen
.u64
;
1794 csect
->rawsize
= aux
.x_csect
.x_scnlen
.u64
;
1795 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1796 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1798 /* Record the enclosing section in the tdata for this new
1800 amt
= sizeof (struct coff_section_tdata
);
1801 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1802 if (csect
->used_by_bfd
== NULL
)
1804 amt
= sizeof (struct xcoff_section_tdata
);
1805 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1806 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1808 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1809 xcoff_section_data (abfd
, csect
)->lineno_count
=
1810 enclosing
->lineno_count
;
1812 if (enclosing
->owner
== abfd
)
1814 struct internal_reloc
*relocs
;
1815 bfd_size_type relindx
;
1816 struct internal_reloc
*rel
;
1817 asection
**rel_csect
;
1819 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1820 amt
= enclosing
->reloc_count
;
1821 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1823 rel
= relocs
+ relindx
;
1824 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1827 csect
->rel_filepos
= (enclosing
->rel_filepos
1828 + relindx
* bfd_coff_relsz (abfd
));
1829 while (relindx
< enclosing
->reloc_count
1830 && *rel_csect
== NULL
1831 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1835 csect
->flags
|= SEC_RELOC
;
1836 ++csect
->reloc_count
;
1843 /* There are a number of other fields and section flags
1844 which we do not bother to set. */
1846 csect_index
= ((esym
1847 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1850 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1852 if (first_csect
== NULL
)
1853 first_csect
= csect
;
1855 /* If this symbol must be added to the linker hash table,
1856 we treat it as starting at the beginning of the newly
1858 if (xcoff_link_add_symbols_to_hash_table (sym
, aux
))
1864 /* If this is a TOC section for a symbol, record it. */
1865 if (set_toc
!= NULL
)
1866 set_toc
->toc_section
= csect
;
1871 /* This is a label definition. The x_scnlen field is the
1872 symbol index of the csect. Usually the XTY_LD symbol will
1873 follow its appropriate XTY_SD symbol. The .set pseudo op can
1874 cause the XTY_LD to not follow the XTY_SD symbol. */
1879 if (aux
.x_csect
.x_scnlen
.u64
1880 >= (size_t) (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1884 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.u64
];
1886 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1892 /* xgettext:c-format */
1893 (_("%pB: misplaced XTY_LD `%s'"),
1895 bfd_set_error (bfd_error_bad_value
);
1899 value
= sym
.n_value
- csect
->vma
;
1904 /* This is an unitialized csect. We could base the name on
1905 the storage mapping class, but we don't bother except for
1906 an XMC_TD symbol. If this csect is externally visible,
1907 it is a common symbol. We put XMC_TD symbols in sections
1908 named .tocbss, and rely on the linker script to put that
1911 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1913 /* The linker script puts the .td section in the data
1914 section after the .tc section. */
1915 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1918 else if (aux
.x_csect
.x_smclas
== XMC_UL
)
1920 /* This is a thread-local unitialized csect. */
1921 csect
= bfd_make_section_anyway_with_flags (abfd
, ".tbss",
1922 SEC_ALLOC
| SEC_THREAD_LOCAL
);
1925 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1930 csect
->vma
= sym
.n_value
;
1931 csect
->size
= aux
.x_csect
.x_scnlen
.u64
;
1932 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1933 /* There are a number of other fields and section flags
1934 which we do not bother to set. */
1936 csect_index
= ((esym
1937 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1940 amt
= sizeof (struct coff_section_tdata
);
1941 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1942 if (csect
->used_by_bfd
== NULL
)
1944 amt
= sizeof (struct xcoff_section_tdata
);
1945 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1946 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1948 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1950 if (first_csect
== NULL
)
1951 first_csect
= csect
;
1953 if (xcoff_link_add_symbols_to_hash_table (sym
, aux
))
1955 csect
->flags
|= SEC_IS_COMMON
;
1958 value
= aux
.x_csect
.x_scnlen
.u64
;
1964 /* Check for magic symbol names. */
1965 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1966 && aux
.x_csect
.x_smclas
!= XMC_TC
1967 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1973 if (strcmp (name
, "_text") == 0)
1974 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1975 else if (strcmp (name
, "_etext") == 0)
1976 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1977 else if (strcmp (name
, "_data") == 0)
1978 i
= XCOFF_SPECIAL_SECTION_DATA
;
1979 else if (strcmp (name
, "_edata") == 0)
1980 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1981 else if (strcmp (name
, "_end") == 0)
1982 i
= XCOFF_SPECIAL_SECTION_END
;
1984 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1985 i
= XCOFF_SPECIAL_SECTION_END2
;
1988 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1991 /* Now we have enough information to add the symbol to the
1992 linker hash table. */
1994 if (xcoff_link_add_symbols_to_hash_table (sym
, aux
))
1999 BFD_ASSERT (section
!= NULL
);
2001 /* We must copy the name into memory if we got it from the
2002 syment itself, rather than the string table. */
2003 copy
= default_copy
;
2004 if (sym
._n
._n_n
._n_zeroes
!= 0
2005 || sym
._n
._n_n
._n_offset
== 0)
2008 /* Ignore global linkage code when linking statically. */
2009 if (info
->static_link
2010 && (smtyp
== XTY_SD
|| smtyp
== XTY_LD
)
2011 && aux
.x_csect
.x_smclas
== XMC_GL
)
2013 section
= bfd_und_section_ptr
;
2017 /* The AIX linker appears to only detect multiple symbol
2018 definitions when there is a reference to the symbol. If
2019 a symbol is defined multiple times, and the only
2020 references are from the same object file, the AIX linker
2021 appears to permit it. It does not merge the different
2022 definitions, but handles them independently. On the
2023 other hand, if there is a reference, the linker reports
2026 This matters because the AIX <net/net_globals.h> header
2027 file actually defines an initialized array, so we have to
2028 actually permit that to work.
2030 Just to make matters even more confusing, the AIX linker
2031 appears to permit multiple symbol definitions whenever
2032 the second definition is in an archive rather than an
2033 object file. This may be a consequence of the manner in
2034 which it handles archives: I think it may load the entire
2035 archive in as separate csects, and then let garbage
2036 collection discard symbols.
2038 We also have to handle the case of statically linking a
2039 shared object, which will cause symbol redefinitions,
2040 although this is an easier case to detect. */
2041 else if (info
->output_bfd
->xvec
== abfd
->xvec
)
2043 if (! bfd_is_und_section (section
))
2044 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2045 name
, true, copy
, false);
2047 /* Make a copy of the symbol name to prevent problems with
2049 *sym_hash
= ((struct xcoff_link_hash_entry
*)
2050 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
2051 true, true, false));
2053 if (*sym_hash
== NULL
)
2055 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
2056 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2057 && ! bfd_is_und_section (section
)
2058 && ! bfd_is_com_section (section
))
2060 /* This is a second definition of a defined symbol. */
2061 if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
2062 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
2064 /* The existing symbol is from a shared library.
2066 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
2067 (*sym_hash
)->root
.u
.undef
.abfd
=
2068 (*sym_hash
)->root
.u
.def
.section
->owner
;
2070 else if (abfd
->my_archive
!= NULL
)
2072 /* This is a redefinition in an object contained
2073 in an archive. Just ignore it. See the
2075 section
= bfd_und_section_ptr
;
2078 else if (sym
.n_sclass
== C_AIX_WEAKEXT
2079 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2081 /* At least one of the definitions is weak.
2082 Allow the normal rules to take effect. */
2084 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
2085 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
2087 /* This symbol has been referenced. In this
2088 case, we just continue and permit the
2089 multiple definition error. See the comment
2090 above about the behaviour of the AIX linker. */
2092 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
2094 /* The symbols are both csects of the same
2095 class. There is at least a chance that this
2096 is a semi-legitimate redefinition. */
2097 section
= bfd_und_section_ptr
;
2099 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
2102 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
2103 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
2104 && (bfd_is_und_section (section
)
2105 || bfd_is_com_section (section
)))
2107 /* This is a reference to a multiply defined symbol.
2108 Report the error now. See the comment above
2109 about the behaviour of the AIX linker. We could
2110 also do this with warning symbols, but I'm not
2111 sure the XCOFF linker is wholly prepared to
2112 handle them, and that would only be a warning,
2114 (*info
->callbacks
->multiple_definition
) (info
,
2118 /* Try not to give this error too many times. */
2119 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2123 /* If the symbol is hidden or internal, completely undo
2124 any dynamic link state. */
2125 if ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
2126 && (visibility
== SYM_V_HIDDEN
2127 || visibility
== SYM_V_INTERNAL
))
2128 (*sym_hash
)->flags
&= ~XCOFF_DEF_DYNAMIC
;
2131 /* Keep the most constraining visibility. */
2132 unsigned short hvis
= (*sym_hash
)->visibility
;
2133 if (visibility
&& ( !hvis
|| visibility
< hvis
))
2134 (*sym_hash
)->visibility
= visibility
;
2139 /* _bfd_generic_link_add_one_symbol may call the linker to
2140 generate an error message, and the linker may try to read
2141 the symbol table to give a good error. Right now, the
2142 line numbers are in an inconsistent state, since they are
2143 counted both in the real sections and in the new csects.
2144 We need to leave the count in the real sections so that
2145 the linker can report the line number of the error
2146 correctly, so temporarily clobber the link to the csects
2147 so that the linker will not try to read the line numbers
2148 a second time from the csects. */
2149 BFD_ASSERT (last_real
->next
== first_csect
);
2150 last_real
->next
= NULL
;
2151 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2152 ok
= (_bfd_generic_link_add_one_symbol
2153 (info
, abfd
, name
, flags
, section
, value
, NULL
, copy
, true,
2154 (struct bfd_link_hash_entry
**) sym_hash
));
2155 last_real
->next
= first_csect
;
2159 if (smtyp
== XTY_CM
)
2161 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2162 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2163 /* We don't need the common csect we just created. */
2166 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2167 = csect
->alignment_power
;
2170 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2176 || section
== bfd_und_section_ptr
)
2177 flag
= XCOFF_REF_REGULAR
;
2179 flag
= XCOFF_DEF_REGULAR
;
2180 (*sym_hash
)->flags
|= flag
;
2182 if ((*sym_hash
)->smclas
== XMC_UA
2183 || flag
== XCOFF_DEF_REGULAR
)
2184 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2188 if (smtyp
== XTY_ER
)
2189 *csect_cache
= section
;
2192 *csect_cache
= csect
;
2194 xcoff_section_data (abfd
, csect
)->last_symndx
2195 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2198 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2199 sym_hash
+= sym
.n_numaux
+ 1;
2200 csect_cache
+= sym
.n_numaux
+ 1;
2201 lineno_counts
+= sym
.n_numaux
+ 1;
2204 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2206 /* Make sure that we have seen all the relocs. */
2207 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2209 /* Debugging sections have no csects. */
2210 if (bfd_section_flags (o
) & SEC_DEBUGGING
)
2213 /* Reset the section size and the line number count, since the
2214 data is now attached to the csects. Don't reset the size of
2215 the .debug section, since we need to read it below in
2216 bfd_xcoff_size_dynamic_sections. */
2217 if (strcmp (bfd_section_name (o
), ".debug") != 0)
2219 o
->lineno_count
= 0;
2221 if ((o
->flags
& SEC_RELOC
) != 0)
2224 struct internal_reloc
*rel
;
2225 asection
**rel_csect
;
2227 rel
= reloc_info
[o
->target_index
].relocs
;
2228 rel_csect
= reloc_info
[o
->target_index
].csects
;
2230 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2232 if (*rel_csect
== NULL
)
2235 /* xgettext:c-format */
2236 (_("%pB: reloc %s:%" PRId64
" not in csect"),
2237 abfd
, o
->name
, (int64_t) i
);
2238 bfd_set_error (bfd_error_bad_value
);
2242 /* We identify all function symbols that are the target
2243 of a relocation, so that we can create glue code for
2244 functions imported from dynamic objects. */
2245 if (info
->output_bfd
->xvec
== abfd
->xvec
2246 && *rel_csect
!= bfd_und_section_ptr
2247 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2249 struct xcoff_link_hash_entry
*h
;
2251 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2252 /* If the symbol name starts with a period, it is
2253 the code of a function. If the symbol is
2254 currently undefined, then add an undefined symbol
2255 for the function descriptor. This should do no
2256 harm, because any regular object that defines the
2257 function should also define the function
2258 descriptor. It helps, because it means that we
2259 will identify the function descriptor with a
2260 dynamic object if a dynamic object defines it. */
2261 if (h
->root
.root
.string
[0] == '.'
2262 && h
->descriptor
== NULL
)
2264 struct xcoff_link_hash_entry
*hds
;
2265 struct bfd_link_hash_entry
*bh
;
2267 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2268 h
->root
.root
.string
+ 1,
2272 if (hds
->root
.type
== bfd_link_hash_new
)
2275 if (! (_bfd_generic_link_add_one_symbol
2276 (info
, abfd
, hds
->root
.root
.string
,
2277 (flagword
) 0, bfd_und_section_ptr
,
2278 (bfd_vma
) 0, NULL
, false,
2281 hds
= (struct xcoff_link_hash_entry
*) bh
;
2283 hds
->flags
|= XCOFF_DESCRIPTOR
;
2284 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2285 hds
->descriptor
= h
;
2286 h
->descriptor
= hds
;
2288 if (h
->root
.root
.string
[0] == '.')
2289 h
->flags
|= XCOFF_CALLED
;
2293 free (reloc_info
[o
->target_index
].csects
);
2294 reloc_info
[o
->target_index
].csects
= NULL
;
2296 /* Reset SEC_RELOC and the reloc_count, since the reloc
2297 information is now attached to the csects. */
2298 o
->flags
&=~ SEC_RELOC
;
2301 /* If we are not keeping memory, free the reloc information. */
2302 if (! info
->keep_memory
2303 && coff_section_data (abfd
, o
) != NULL
)
2305 free (coff_section_data (abfd
, o
)->relocs
);
2306 coff_section_data (abfd
, o
)->relocs
= NULL
;
2310 /* Free up the line numbers. FIXME: We could cache these
2311 somewhere for the final link, to avoid reading them again. */
2312 free (reloc_info
[o
->target_index
].linenos
);
2313 reloc_info
[o
->target_index
].linenos
= NULL
;
2318 obj_coff_keep_syms (abfd
) = keep_syms
;
2323 if (reloc_info
!= NULL
)
2325 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2327 free (reloc_info
[o
->target_index
].csects
);
2328 free (reloc_info
[o
->target_index
].linenos
);
2332 obj_coff_keep_syms (abfd
) = keep_syms
;
2339 /* Add symbols from an XCOFF object file. */
2342 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2344 if (! _bfd_coff_get_external_symbols (abfd
))
2346 if (! xcoff_link_add_symbols (abfd
, info
))
2348 if (! info
->keep_memory
)
2350 if (! _bfd_coff_free_symbols (abfd
))
2356 /* Look through the loader symbols to see if this dynamic object
2357 should be included in the link. The native linker uses the loader
2358 symbols, not the normal symbol table, so we do too. */
2361 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2362 struct bfd_link_info
*info
,
2368 struct internal_ldhdr ldhdr
;
2369 const char *strings
;
2370 bfd_byte
*elsym
, *elsymend
;
2374 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2375 if (lsec
== NULL
|| (lsec
->flags
& SEC_HAS_CONTENTS
) == 0)
2376 /* There are no symbols, so don't try to include it. */
2379 contents
= xcoff_get_section_contents (abfd
, lsec
);
2383 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2385 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2387 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2389 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2390 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2392 struct internal_ldsym ldsym
;
2393 char nambuf
[SYMNMLEN
+ 1];
2395 struct bfd_link_hash_entry
*h
;
2397 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2399 /* We are only interested in exported symbols. */
2400 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2403 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2404 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2407 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2408 nambuf
[SYMNMLEN
] = '\0';
2412 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2414 /* We are only interested in symbols that are currently
2415 undefined. At this point we know that we are using an XCOFF
2418 && h
->type
== bfd_link_hash_undefined
2419 && (((struct xcoff_link_hash_entry
*) h
)->flags
2420 & XCOFF_DEF_DYNAMIC
) == 0)
2422 if (!(*info
->callbacks
2423 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2430 /* We do not need this shared object's .loader section. */
2432 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2437 /* Look through the symbols to see if this object file should be
2438 included in the link. */
2441 xcoff_link_check_ar_symbols (bfd
*abfd
,
2442 struct bfd_link_info
*info
,
2446 bfd_size_type symesz
;
2452 if ((abfd
->flags
& DYNAMIC
) != 0
2453 && ! info
->static_link
2454 && info
->output_bfd
->xvec
== abfd
->xvec
)
2455 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
, subsbfd
);
2457 symesz
= bfd_coff_symesz (abfd
);
2458 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2459 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2460 while (esym
< esym_end
)
2462 struct internal_syment sym
;
2464 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2465 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2467 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2470 char buf
[SYMNMLEN
+ 1];
2471 struct bfd_link_hash_entry
*h
;
2473 /* This symbol is externally visible, and is defined by this
2475 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2479 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2481 /* We are only interested in symbols that are currently
2482 undefined. If a symbol is currently known to be common,
2483 XCOFF linkers do not bring in an object file which
2484 defines it. We also don't bring in symbols to satisfy
2485 undefined references in shared objects. */
2487 && h
->type
== bfd_link_hash_undefined
2488 && (info
->output_bfd
->xvec
!= abfd
->xvec
2489 || (((struct xcoff_link_hash_entry
*) h
)->flags
2490 & XCOFF_DEF_DYNAMIC
) == 0))
2492 if (!(*info
->callbacks
2493 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2501 /* We do not need this object file. */
2505 /* Check a single archive element to see if we need to include it in
2506 the link. *PNEEDED is set according to whether this element is
2507 needed in the link or not. This is called via
2508 _bfd_generic_link_add_archive_symbols. */
2511 xcoff_link_check_archive_element (bfd
*abfd
,
2512 struct bfd_link_info
*info
,
2513 struct bfd_link_hash_entry
*h ATTRIBUTE_UNUSED
,
2514 const char *name ATTRIBUTE_UNUSED
,
2520 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2521 if (!_bfd_coff_get_external_symbols (abfd
))
2525 if (!xcoff_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
2530 /* Potentially, the add_archive_element hook may have set a
2531 substitute BFD for us. */
2535 && !_bfd_coff_free_symbols (oldbfd
))
2537 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2538 if (!_bfd_coff_get_external_symbols (abfd
))
2541 if (!xcoff_link_add_symbols (abfd
, info
))
2543 if (info
->keep_memory
)
2549 if (!_bfd_coff_free_symbols (abfd
))
2556 /* Given an XCOFF BFD, add symbols to the global hash table as
2560 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2562 switch (bfd_get_format (abfd
))
2565 return xcoff_link_add_object_symbols (abfd
, info
);
2568 /* If the archive has a map, do the usual search. We then need
2569 to check the archive for dynamic objects, because they may not
2570 appear in the archive map even though they should, perhaps, be
2571 included. If the archive has no map, we just consider each object
2572 file in turn, since that apparently is what the AIX native linker
2574 if (bfd_has_map (abfd
))
2576 if (! (_bfd_generic_link_add_archive_symbols
2577 (abfd
, info
, xcoff_link_check_archive_element
)))
2584 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2585 while (member
!= NULL
)
2587 if (bfd_check_format (member
, bfd_object
)
2588 && (info
->output_bfd
->xvec
== member
->xvec
)
2589 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2593 if (! xcoff_link_check_archive_element (member
, info
,
2594 NULL
, NULL
, &needed
))
2597 member
->archive_pass
= -1;
2599 member
= bfd_openr_next_archived_file (abfd
, member
);
2606 bfd_set_error (bfd_error_wrong_format
);
2612 _bfd_xcoff_define_common_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2613 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2614 struct bfd_link_hash_entry
*harg
)
2616 struct xcoff_link_hash_entry
*h
;
2618 if (!bfd_generic_define_common_symbol (output_bfd
, info
, harg
))
2621 h
= (struct xcoff_link_hash_entry
*) harg
;
2622 h
->flags
|= XCOFF_DEF_REGULAR
;
2626 /* If symbol H has not been interpreted as a function descriptor,
2627 see whether it should be. Set up its descriptor information if so. */
2630 xcoff_find_function (struct bfd_link_info
*info
,
2631 struct xcoff_link_hash_entry
*h
)
2633 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2634 && h
->root
.root
.string
[0] != '.')
2637 struct xcoff_link_hash_entry
*hfn
;
2640 amt
= strlen (h
->root
.root
.string
) + 2;
2641 fnname
= bfd_malloc (amt
);
2645 strcpy (fnname
+ 1, h
->root
.root
.string
);
2646 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2647 fnname
, false, false, true);
2650 && hfn
->smclas
== XMC_PR
2651 && (hfn
->root
.type
== bfd_link_hash_defined
2652 || hfn
->root
.type
== bfd_link_hash_defweak
))
2654 h
->flags
|= XCOFF_DESCRIPTOR
;
2655 h
->descriptor
= hfn
;
2656 hfn
->descriptor
= h
;
2662 /* Return true if the given bfd contains at least one shared object. */
2665 xcoff_archive_contains_shared_object_p (struct bfd_link_info
*info
,
2668 struct xcoff_archive_info
*archive_info
;
2671 archive_info
= xcoff_get_archive_info (info
, archive
);
2672 if (!archive_info
->know_contains_shared_object_p
)
2674 member
= bfd_openr_next_archived_file (archive
, NULL
);
2675 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2676 member
= bfd_openr_next_archived_file (archive
, member
);
2678 archive_info
->contains_shared_object_p
= (member
!= NULL
);
2679 archive_info
->know_contains_shared_object_p
= 1;
2681 return archive_info
->contains_shared_object_p
;
2684 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2685 qualifies for export by -bexpall. */
2688 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2690 /* Exclude symbols beginning with '_'. */
2691 if (h
->root
.root
.string
[0] == '_')
2694 /* Exclude archive members that would otherwise be unreferenced. */
2695 if ((h
->flags
& XCOFF_MARK
) == 0
2696 && (h
->root
.type
== bfd_link_hash_defined
2697 || h
->root
.type
== bfd_link_hash_defweak
)
2698 && h
->root
.u
.def
.section
->owner
!= NULL
2699 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2705 /* Return true if symbol H qualifies for the forms of automatic export
2706 specified by AUTO_EXPORT_FLAGS. */
2709 xcoff_auto_export_p (struct bfd_link_info
*info
,
2710 struct xcoff_link_hash_entry
*h
,
2711 unsigned int auto_export_flags
)
2713 /* Don't automatically export things that were explicitly exported. */
2714 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2717 /* Don't export things that we don't define. */
2718 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2721 /* Don't export functions; export their descriptors instead. */
2722 if (h
->root
.root
.string
[0] == '.')
2725 /* Don't export hidden or internal symbols. */
2726 if (h
->visibility
== SYM_V_HIDDEN
2727 || h
->visibility
== SYM_V_INTERNAL
)
2730 /* We don't export a symbol which is being defined by an object
2731 included from an archive which contains a shared object. The
2732 rationale is that if an archive contains both an unshared and
2733 a shared object, then there must be some reason that the
2734 unshared object is unshared, and we don't want to start
2735 providing a shared version of it. In particular, this solves
2736 a bug involving the _savefNN set of functions. gcc will call
2737 those functions without providing a slot to restore the TOC,
2738 so it is essential that these functions be linked in directly
2739 and not from a shared object, which means that a shared
2740 object which also happens to link them in must not export
2741 them. This is confusing, but I haven't been able to think of
2742 a different approach. Note that the symbols can, of course,
2743 be exported explicitly. */
2744 if (h
->root
.type
== bfd_link_hash_defined
2745 || h
->root
.type
== bfd_link_hash_defweak
)
2749 owner
= h
->root
.u
.def
.section
->owner
;
2751 && owner
->my_archive
!= NULL
2752 && xcoff_archive_contains_shared_object_p (info
, owner
->my_archive
))
2756 /* Otherwise, all symbols are exported by -bexpfull. */
2757 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2760 /* Despite its name, -bexpall exports most but not all symbols. */
2761 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2762 && xcoff_covered_by_expall_p (h
))
2768 /* Return true if relocation REL needs to be copied to the .loader section.
2769 If REL is against a global symbol, H is that symbol, otherwise it
2773 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2774 struct xcoff_link_hash_entry
*h
, asection
*ssec
)
2776 if (!xcoff_hash_table (info
)->loader_section
)
2779 switch (rel
->r_type
)
2786 /* We should never need a .loader reloc for a TOC-relative reloc. */
2790 /* In this case, relocations against defined symbols can be resolved
2793 || h
->root
.type
== bfd_link_hash_defined
2794 || h
->root
.type
== bfd_link_hash_defweak
2795 || h
->root
.type
== bfd_link_hash_common
)
2798 /* We will always provide a local definition of function symbols,
2799 even if we don't have one yet. */
2800 if ((h
->flags
& XCOFF_CALLED
) != 0)
2809 /* Absolute relocations against absolute symbols can be
2810 resolved statically. */
2812 && (h
->root
.type
== bfd_link_hash_defined
2813 || h
->root
.type
== bfd_link_hash_defweak
)
2814 && !h
->root
.rel_from_abs
)
2816 asection
*sec
= h
->root
.u
.def
.section
;
2817 if (bfd_is_abs_section (sec
)
2819 && bfd_is_abs_section (sec
->output_section
)))
2823 /* Absolute relocations from read-only sections are forbidden
2824 by AIX loader. However, they can appear in their section's
2827 && (ssec
->output_section
->flags
& SEC_READONLY
) != 0)
2842 /* Mark a symbol as not being garbage, including the section in which
2846 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2848 if ((h
->flags
& XCOFF_MARK
) != 0)
2851 h
->flags
|= XCOFF_MARK
;
2853 /* If we're marking an undefined symbol, try find some way of
2855 if (!bfd_link_relocatable (info
)
2856 && (h
->flags
& XCOFF_IMPORT
) == 0
2857 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2858 && (h
->root
.type
== bfd_link_hash_undefined
2859 || h
->root
.type
== bfd_link_hash_undefweak
))
2861 /* First check whether this symbol can be interpreted as an
2862 undefined function descriptor for a defined function symbol. */
2863 if (!xcoff_find_function (info
, h
))
2866 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2867 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2868 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2870 /* This is a descriptor for a defined symbol, but the input
2871 objects have not defined the descriptor itself. Fill in
2872 the definition automatically.
2874 Note that we do this even if we found a dynamic definition
2875 of H. The local function definition logically overrides
2879 sec
= xcoff_hash_table (info
)->descriptor_section
;
2880 h
->root
.type
= bfd_link_hash_defined
;
2881 h
->root
.u
.def
.section
= sec
;
2882 h
->root
.u
.def
.value
= sec
->size
;
2884 h
->flags
|= XCOFF_DEF_REGULAR
;
2886 /* The size of the function descriptor depends on whether this
2887 is xcoff32 (12) or xcoff64 (24). */
2888 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2890 /* A function descriptor uses two relocs: one for the
2891 associated code, and one for the TOC address. */
2892 xcoff_hash_table (info
)->ldinfo
.ldrel_count
+= 2;
2893 sec
->reloc_count
+= 2;
2895 /* Mark the function itself. */
2896 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2899 /* Mark the TOC section, so that we get an anchor
2900 to relocate against. */
2901 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2904 /* We handle writing out the contents of the descriptor in
2905 xcoff_write_global_symbol. */
2907 else if (info
->static_link
)
2908 /* We can't get a symbol value dynamically, so just assume
2909 that it's undefined. */
2910 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2911 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2913 /* This is a function symbol for which we need to create
2916 struct xcoff_link_hash_entry
*hds
;
2918 /* Mark the descriptor (and its TOC section). */
2919 hds
= h
->descriptor
;
2920 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2921 || hds
->root
.type
== bfd_link_hash_undefweak
)
2922 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2923 if (!xcoff_mark_symbol (info
, hds
))
2926 /* Treat this symbol as undefined if the descriptor was. */
2927 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2928 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2930 /* Allocate room for the global linkage code itself. */
2931 sec
= xcoff_hash_table (info
)->linkage_section
;
2932 h
->root
.type
= bfd_link_hash_defined
;
2933 h
->root
.u
.def
.section
= sec
;
2934 h
->root
.u
.def
.value
= sec
->size
;
2936 h
->flags
|= XCOFF_DEF_REGULAR
;
2937 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2939 /* The global linkage code requires a TOC entry for the
2941 if (hds
->toc_section
== NULL
)
2946 xcoff32 uses 4 bytes in the toc.
2947 xcoff64 uses 8 bytes in the toc. */
2948 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2950 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2955 /* Allocate room in the fallback TOC section. */
2956 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2957 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2958 hds
->toc_section
->size
+= byte_size
;
2959 if (!xcoff_mark (info
, hds
->toc_section
))
2962 /* Allocate room for a static and dynamic R_TOC
2964 ++xcoff_hash_table (info
)->ldinfo
.ldrel_count
;
2965 ++hds
->toc_section
->reloc_count
;
2967 /* Set the index to -2 to force this symbol to
2970 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2973 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2975 /* Record that the symbol was undefined, then import it.
2976 -brtl links use a special fake import file. */
2977 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2978 if (xcoff_hash_table (info
)->rtld
)
2980 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2985 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2991 if (h
->root
.type
== bfd_link_hash_defined
2992 || h
->root
.type
== bfd_link_hash_defweak
)
2996 hsec
= h
->root
.u
.def
.section
;
2997 if (! bfd_is_abs_section (hsec
)
2998 && hsec
->gc_mark
== 0)
3000 if (! xcoff_mark (info
, hsec
))
3005 if (h
->toc_section
!= NULL
3006 && h
->toc_section
->gc_mark
== 0)
3008 if (! xcoff_mark (info
, h
->toc_section
))
3015 /* Look for a symbol called NAME. If the symbol is defined, mark it.
3016 If the symbol exists, set FLAGS. */
3019 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
3020 const char *name
, unsigned int flags
)
3022 struct xcoff_link_hash_entry
*h
;
3024 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
3025 false, false, true);
3029 if (h
->root
.type
== bfd_link_hash_defined
3030 || h
->root
.type
== bfd_link_hash_defweak
)
3032 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
3039 /* The mark phase of garbage collection. For a given section, mark
3040 it, and all the sections which define symbols to which it refers.
3041 Because this function needs to look at the relocs, we also count
3042 the number of relocs which need to be copied into the .loader
3046 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
3048 if (bfd_is_const_section (sec
)
3049 || sec
->gc_mark
!= 0)
3054 if (sec
->owner
->xvec
!= info
->output_bfd
->xvec
)
3057 if (coff_section_data (sec
->owner
, sec
) == NULL
)
3061 if (xcoff_section_data (sec
->owner
, sec
) != NULL
)
3063 struct xcoff_link_hash_entry
**syms
;
3065 unsigned long i
, first
, last
;
3067 /* Mark all the symbols in this section. */
3068 syms
= obj_xcoff_sym_hashes (sec
->owner
);
3069 csects
= xcoff_data (sec
->owner
)->csects
;
3070 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
3071 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
3072 for (i
= first
; i
<= last
; i
++)
3073 if (csects
[i
] == sec
3075 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
3077 if (!xcoff_mark_symbol (info
, syms
[i
]))
3082 /* Look through the section relocs. */
3083 if ((sec
->flags
& SEC_RELOC
) != 0
3084 && sec
->reloc_count
> 0)
3086 struct internal_reloc
*rel
, *relend
;
3088 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
3092 relend
= rel
+ sec
->reloc_count
;
3093 for (; rel
< relend
; rel
++)
3095 struct xcoff_link_hash_entry
*h
;
3097 if ((unsigned int) rel
->r_symndx
3098 > obj_raw_syment_count (sec
->owner
))
3101 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
3104 if ((h
->flags
& XCOFF_MARK
) == 0)
3106 if (!xcoff_mark_symbol (info
, h
))
3114 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
3116 && rsec
->gc_mark
== 0)
3118 if (!xcoff_mark (info
, rsec
))
3123 /* See if this reloc needs to be copied into the .loader
3125 if ((sec
->flags
& SEC_DEBUGGING
) == 0
3126 && xcoff_need_ldrel_p (info
, rel
, h
, sec
))
3128 ++xcoff_hash_table (info
)->ldinfo
.ldrel_count
;
3130 h
->flags
|= XCOFF_LDREL
;
3134 if (! info
->keep_memory
3135 && coff_section_data (sec
->owner
, sec
) != NULL
)
3137 free (coff_section_data (sec
->owner
, sec
)->relocs
);
3138 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
3145 /* Routines that are called after all the input files have been
3146 handled, but before the sections are laid out in memory. */
3148 /* The sweep phase of garbage collection. Remove all garbage
3152 xcoff_sweep (struct bfd_link_info
*info
)
3156 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3159 bool some_kept
= false;
3161 /* As says below keep all sections from non-XCOFF
3163 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3167 /* See whether any section is already marked. */
3168 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3173 /* If no section in this file will be kept, then we can
3174 toss out debug sections. */
3177 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3185 /* Keep all sections from non-XCOFF input files. Keep
3186 special sections. Keep .debug sections for the
3188 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3190 if (o
->gc_mark
== 1)
3193 if (sub
->xvec
!= info
->output_bfd
->xvec
3194 || o
== xcoff_hash_table (info
)->debug_section
3195 || o
== xcoff_hash_table (info
)->loader_section
3196 || o
== xcoff_hash_table (info
)->linkage_section
3197 || o
== xcoff_hash_table (info
)->descriptor_section
3198 || (bfd_section_flags (o
) & SEC_DEBUGGING
)
3199 || strcmp (o
->name
, ".debug") == 0)
3200 xcoff_mark (info
, o
);
3210 /* Initialize the back-end with linker infos. */
3213 bfd_xcoff_link_init (struct bfd_link_info
*info
,
3214 struct bfd_xcoff_link_params
*params
)
3216 xcoff_hash_table (info
)->params
= params
;
3221 /* Record the number of elements in a set. This is used to output the
3222 correct csect length. */
3225 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3226 struct bfd_link_info
*info
,
3227 struct bfd_link_hash_entry
*harg
,
3230 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3231 struct xcoff_link_size_list
*n
;
3234 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3237 /* This will hardly ever be called. I don't want to burn four bytes
3238 per global symbol, so instead the size is kept on a linked list
3239 attached to the hash table. */
3241 n
= bfd_alloc (output_bfd
, amt
);
3244 n
->next
= xcoff_hash_table (info
)->size_list
;
3247 xcoff_hash_table (info
)->size_list
= n
;
3249 h
->flags
|= XCOFF_HAS_SIZE
;
3254 /* Import a symbol. */
3257 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3258 struct bfd_link_info
*info
,
3259 struct bfd_link_hash_entry
*harg
,
3261 const char *imppath
,
3262 const char *impfile
,
3263 const char *impmember
,
3264 unsigned int syscall_flag
)
3266 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3268 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3271 /* A symbol name which starts with a period is the code for a
3272 function. If the symbol is undefined, then add an undefined
3273 symbol for the function descriptor, and import that instead. */
3274 if (h
->root
.root
.string
[0] == '.'
3275 && h
->root
.type
== bfd_link_hash_undefined
3276 && val
== (bfd_vma
) -1)
3278 struct xcoff_link_hash_entry
*hds
;
3280 hds
= h
->descriptor
;
3283 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3284 h
->root
.root
.string
+ 1,
3288 if (hds
->root
.type
== bfd_link_hash_new
)
3290 hds
->root
.type
= bfd_link_hash_undefined
;
3291 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3293 hds
->flags
|= XCOFF_DESCRIPTOR
;
3294 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3295 hds
->descriptor
= h
;
3296 h
->descriptor
= hds
;
3299 /* Now, if the descriptor is undefined, import the descriptor
3300 rather than the symbol we were told to import. FIXME: Is
3301 this correct in all cases? */
3302 if (hds
->root
.type
== bfd_link_hash_undefined
)
3306 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3308 if (val
!= (bfd_vma
) -1)
3310 if (h
->root
.type
== bfd_link_hash_defined
)
3311 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
, output_bfd
,
3312 bfd_abs_section_ptr
, val
);
3314 h
->root
.type
= bfd_link_hash_defined
;
3315 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3316 h
->root
.u
.def
.value
= val
;
3320 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3326 /* Export a symbol. */
3329 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3330 struct bfd_link_info
*info
,
3331 struct bfd_link_hash_entry
*harg
)
3333 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3335 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3338 /* As AIX linker, symbols exported with hidden visibility are
3339 silently ignored. */
3340 if (h
->visibility
== SYM_V_HIDDEN
)
3343 if (h
->visibility
== SYM_V_INTERNAL
)
3345 _bfd_error_handler (_("%pB: cannot export internal symbol `%s`."),
3346 output_bfd
, h
->root
.root
.string
);
3347 bfd_set_error (bfd_error_bad_value
);
3351 h
->flags
|= XCOFF_EXPORT
;
3353 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3354 I'm just going to ignore it until somebody explains it. */
3356 /* Make sure we don't garbage collect this symbol. */
3357 if (! xcoff_mark_symbol (info
, h
))
3360 /* If this is a function descriptor, make sure we don't garbage
3361 collect the associated function code. We normally don't have to
3362 worry about this, because the descriptor will be attached to a
3363 section with relocs, but if we are creating the descriptor
3364 ourselves those relocs will not be visible to the mark code. */
3365 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3367 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3374 /* Count a reloc against a symbol. This is called for relocs
3375 generated by the linker script, typically for global constructors
3379 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3380 struct bfd_link_info
*info
,
3383 struct xcoff_link_hash_entry
*h
;
3385 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3388 h
= ((struct xcoff_link_hash_entry
*)
3389 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, false, false,
3393 _bfd_error_handler (_("%s: no such symbol"), name
);
3394 bfd_set_error (bfd_error_no_symbols
);
3398 h
->flags
|= XCOFF_REF_REGULAR
;
3399 if (xcoff_hash_table (info
)->loader_section
)
3401 h
->flags
|= XCOFF_LDREL
;
3402 ++xcoff_hash_table (info
)->ldinfo
.ldrel_count
;
3405 /* Mark the symbol to avoid garbage collection. */
3406 if (! xcoff_mark_symbol (info
, h
))
3412 /* This function is called for each symbol to which the linker script
3414 FIXME: In cases like the linker test ld-scripts/defined5 where a
3415 symbol is defined both by an input object file and the script,
3416 the script definition doesn't override the object file definition
3417 as is usual for other targets. At least not when the symbol is
3418 output. Other uses of the symbol value by the linker do use the
3422 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3423 struct bfd_link_info
*info
,
3426 struct xcoff_link_hash_entry
*h
;
3428 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3431 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
3436 h
->flags
|= XCOFF_DEF_REGULAR
;
3441 /* An xcoff_link_hash_traverse callback for which DATA points to an
3442 xcoff_loader_info. Mark all symbols that should be automatically
3446 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3448 struct xcoff_loader_info
*ldinfo
;
3450 ldinfo
= (struct xcoff_loader_info
*) data
;
3451 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3453 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3454 ldinfo
->failed
= true;
3459 /* INPUT_BFD has an external symbol associated with hash table entry H
3460 and csect CSECT. Return true if INPUT_BFD defines H. */
3463 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3466 switch (h
->root
.type
)
3468 case bfd_link_hash_defined
:
3469 case bfd_link_hash_defweak
:
3470 /* No input bfd owns absolute symbols. They are written by
3471 xcoff_write_global_symbol instead. */
3472 return (!bfd_is_abs_section (csect
)
3473 && h
->root
.u
.def
.section
== csect
);
3475 case bfd_link_hash_common
:
3476 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3478 case bfd_link_hash_undefined
:
3479 case bfd_link_hash_undefweak
:
3480 /* We can't treat undef.abfd as the owner because that bfd
3481 might be a dynamic object. Allow any bfd to claim it. */
3489 /* See if H should have a loader symbol associated with it. */
3492 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3493 struct xcoff_link_hash_entry
*h
)
3497 /* Warn if this symbol is exported but not defined. */
3498 if ((h
->flags
& XCOFF_EXPORT
) != 0
3499 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3502 (_("warning: attempt to export undefined symbol `%s'"),
3503 h
->root
.root
.string
);
3507 /* We need to add a symbol to the .loader section if it is mentioned
3508 in a reloc which we are copying to the .loader section and it was
3509 not defined or common, or if it is the entry point, or if it is
3511 if (((h
->flags
& XCOFF_LDREL
) == 0
3512 || h
->root
.type
== bfd_link_hash_defined
3513 || h
->root
.type
== bfd_link_hash_defweak
3514 || h
->root
.type
== bfd_link_hash_common
)
3515 && (h
->flags
& XCOFF_ENTRY
) == 0
3516 && (h
->flags
& XCOFF_EXPORT
) == 0)
3519 /* We need to add this symbol to the .loader symbols. */
3521 BFD_ASSERT (h
->ldsym
== NULL
);
3522 amt
= sizeof (struct internal_ldsym
);
3523 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3524 if (h
->ldsym
== NULL
)
3526 ldinfo
->failed
= true;
3530 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3532 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3533 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3535 h
->ldsym
->l_ifile
= h
->ldindx
;
3538 /* The first 3 symbol table indices are reserved to indicate the
3539 data, text and bss sections. */
3540 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3542 ++ldinfo
->ldsym_count
;
3544 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3545 h
->ldsym
, h
->root
.root
.string
))
3548 h
->flags
|= XCOFF_BUILT_LDSYM
;
3552 /* An xcoff_htab_traverse callback that is called for each symbol
3553 once garbage collection is complete. */
3556 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3558 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3560 /* __rtinit, this symbol has special handling. */
3561 if (h
->flags
& XCOFF_RTINIT
)
3564 /* We don't want to garbage collect symbols which are not defined in
3565 XCOFF files. This is a convenient place to mark them. */
3566 if (xcoff_hash_table (ldinfo
->info
)->gc
3567 && (h
->flags
& XCOFF_MARK
) == 0
3568 && (h
->root
.type
== bfd_link_hash_defined
3569 || h
->root
.type
== bfd_link_hash_defweak
)
3570 && (h
->root
.u
.def
.section
->owner
== NULL
3571 || (h
->root
.u
.def
.section
->owner
->xvec
3572 != ldinfo
->info
->output_bfd
->xvec
)))
3573 h
->flags
|= XCOFF_MARK
;
3575 /* Skip discarded symbols. */
3576 if (xcoff_hash_table (ldinfo
->info
)->gc
3577 && (h
->flags
& XCOFF_MARK
) == 0)
3580 /* If this is still a common symbol, and it wasn't garbage
3581 collected, we need to actually allocate space for it in the .bss
3583 if (h
->root
.type
== bfd_link_hash_common
3584 && h
->root
.u
.c
.p
->section
->size
== 0)
3586 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3587 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3590 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3592 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3593 h
->flags
|= XCOFF_EXPORT
;
3595 if (!xcoff_build_ldsym (ldinfo
, h
))
3602 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3603 hash table entry H and csect CSECT. AUX contains ISYM's auxiliary
3604 csect information, if any. NAME is the function's name if the name
3605 is stored in the .debug section, otherwise it is null.
3607 Return 1 if we should include an appropriately-adjusted ISYM
3608 in the output file, 0 if we should discard ISYM, or -1 if an
3612 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3613 struct internal_syment
*isym
,
3614 union internal_auxent
*aux
,
3615 struct xcoff_link_hash_entry
*h
,
3616 asection
*csect
, const char *name
)
3620 /* If we are skipping this csect, we want to strip the symbol too. */
3624 /* Likewise if we garbage-collected the csect. */
3625 if (xcoff_hash_table (info
)->gc
3626 && !bfd_is_abs_section (csect
)
3627 && !bfd_is_und_section (csect
)
3628 && csect
->gc_mark
== 0)
3631 /* An XCOFF linker always removes C_STAT symbols. */
3632 if (isym
->n_sclass
== C_STAT
)
3635 /* We generate the TOC anchor separately. */
3636 if (isym
->n_sclass
== C_HIDEXT
3637 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3640 /* If we are stripping all symbols, we want to discard this one. */
3641 if (info
->strip
== strip_all
)
3644 /* Discard symbols that are defined elsewhere. */
3645 if (EXTERN_SYM_P (isym
->n_sclass
))
3647 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3649 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3653 /* If we're discarding local symbols, check whether ISYM is local. */
3654 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3655 if (info
->discard
== discard_all
3656 && !EXTERN_SYM_P (isym
->n_sclass
)
3657 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3660 /* If we're stripping debugging symbols, check whether ISYM is one. */
3661 if (info
->strip
== strip_debugger
3662 && isym
->n_scnum
== N_DEBUG
)
3665 /* If we are stripping symbols based on name, check how ISYM's
3666 name should be handled. */
3667 if (info
->strip
== strip_some
3668 || info
->discard
== discard_l
)
3670 char buf
[SYMNMLEN
+ 1];
3674 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3679 if (info
->strip
== strip_some
3680 && bfd_hash_lookup (info
->keep_hash
, name
, false, false) == NULL
)
3683 if (info
->discard
== discard_l
3684 && !EXTERN_SYM_P (isym
->n_sclass
)
3685 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3686 && bfd_is_local_label_name (input_bfd
, name
))
3693 /* Compute the current size of the .loader section. Start filling
3694 its header but it will be finalized in xcoff_build_loader_section. */
3697 xcoff_size_loader_section (struct xcoff_loader_info
*ldinfo
)
3700 struct xcoff_link_hash_table
*htab
;
3701 struct internal_ldhdr
*ldhdr
;
3702 struct xcoff_import_file
*fl
;
3703 bfd_size_type stoff
;
3704 size_t impsize
, impcount
;
3707 output_bfd
= ldinfo
->output_bfd
;
3708 htab
= xcoff_hash_table (ldinfo
->info
);
3709 ldhdr
= &htab
->ldhdr
;
3711 /* If this function has already been called (ie l_version is set)
3712 and the number of symbols or relocations haven't changed since
3713 last call, the size is already known. */
3714 if (ldhdr
->l_version
!= 0
3715 && ldhdr
->l_nsyms
== ldinfo
->ldsym_count
3716 && ldhdr
->l_nreloc
== ldinfo
->ldrel_count
)
3719 /* Work out the size of the import file names. Each import file ID
3720 consists of three null terminated strings: the path, the file
3721 name, and the archive member name. The first entry in the list
3722 of names is the path to use to find objects, which the linker has
3723 passed in as the libpath argument. For some reason, the path
3724 entry in the other import file names appears to always be empty. */
3725 if (ldhdr
->l_nimpid
== 0)
3727 impsize
= strlen (ldinfo
->libpath
) + 3;
3729 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3732 impsize
+= (strlen (fl
->path
)
3734 + strlen (fl
->member
)
3737 ldhdr
->l_istlen
= impsize
;
3738 ldhdr
->l_nimpid
= impcount
;
3741 /* Set up the .loader section header. */
3742 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3743 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3744 ldhdr
->l_nreloc
= ldinfo
->ldrel_count
;
3745 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3746 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3747 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3748 ldhdr
->l_stlen
= ldinfo
->string_size
;
3749 stoff
= ldhdr
->l_impoff
+ ldhdr
->l_istlen
;
3750 if (ldinfo
->string_size
== 0)
3753 ldhdr
->l_stoff
= stoff
;
3755 /* 64 bit elements to ldhdr
3756 The swap out routine for 32 bit will ignore them.
3757 Nothing fancy, symbols come after the header and relocs come
3759 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3760 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3761 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3763 /* Save the size of the .loader section. */
3764 lsec
= htab
->loader_section
;
3765 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3770 /* Prepare the .loader section. This is called by the XCOFF linker
3771 emulation before_allocation routine. We must set the size of the
3772 .loader section before the linker lays out the output file. However,
3773 some symbols or relocations might be append to the .loader section
3774 when processing the addresses, thus it's not layout right now and
3775 its size might change.
3776 LIBPATH is the library path to search for shared objects; this is
3777 normally built from the -L arguments passed to the linker. ENTRY
3778 is the name of the entry point symbol (the -e linker option).
3779 FILE_ALIGN is the alignment to use for sections within the file
3780 (the -H linker option). MAXSTACK is the maximum stack size (the
3781 -bmaxstack linker option). MAXDATA is the maximum data size (the
3782 -bmaxdata linker option). GC is whether to do garbage collection
3783 (the -bgc linker option). MODTYPE is the module type (the
3784 -bmodtype linker option). TEXTRO is whether the text section must
3785 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3786 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3787 is set by this routine to csects with magic names like _end. */
3790 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3791 struct bfd_link_info
*info
,
3792 const char *libpath
,
3794 unsigned long file_align
,
3795 unsigned long maxstack
,
3796 unsigned long maxdata
,
3800 unsigned int auto_export_flags
,
3801 asection
**special_sections
,
3804 struct xcoff_loader_info
*ldinfo
;
3810 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3812 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3813 special_sections
[i
] = NULL
;
3818 ldinfo
= &(xcoff_hash_table (info
)->ldinfo
);
3820 ldinfo
->failed
= false;
3821 ldinfo
->output_bfd
= output_bfd
;
3822 ldinfo
->info
= info
;
3823 ldinfo
->auto_export_flags
= auto_export_flags
;
3824 ldinfo
->ldsym_count
= 0;
3825 ldinfo
->string_size
= 0;
3826 ldinfo
->strings
= NULL
;
3827 ldinfo
->string_alc
= 0;
3828 ldinfo
->libpath
= libpath
;
3830 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3831 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3832 xcoff_data (output_bfd
)->modtype
= modtype
;
3834 xcoff_hash_table (info
)->file_align
= file_align
;
3835 xcoff_hash_table (info
)->textro
= textro
;
3836 xcoff_hash_table (info
)->rtld
= rtld
;
3839 if (xcoff_hash_table (info
)->loader_section
3840 && (info
->init_function
|| info
->fini_function
|| rtld
))
3842 struct xcoff_link_hash_entry
*hsym
;
3843 struct internal_ldsym
*ldsym
;
3845 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3846 "__rtinit", false, false, true);
3850 (_("error: undefined symbol __rtinit"));
3854 xcoff_mark_symbol (info
, hsym
);
3855 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3857 /* __rtinit initialized. */
3858 amt
= sizeof (* ldsym
);
3859 ldsym
= bfd_malloc (amt
);
3861 ldsym
->l_value
= 0; /* Will be filled in later. */
3862 ldsym
->l_scnum
= 2; /* Data section. */
3863 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3864 ldsym
->l_smclas
= 5; /* .rw. */
3865 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3866 ldsym
->l_parm
= 0; /* NA. */
3868 /* Force __rtinit to be the first symbol in the loader symbol table
3869 See xcoff_build_ldsyms
3871 The first 3 symbol table indices are reserved to indicate the data,
3872 text and bss sections. */
3873 BFD_ASSERT (0 == ldinfo
->ldsym_count
);
3876 ldinfo
->ldsym_count
= 1;
3877 hsym
->ldsym
= ldsym
;
3879 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3880 hsym
->ldsym
, hsym
->root
.root
.string
))
3883 /* This symbol is written out by xcoff_write_global_symbol
3884 Set stuff up so xcoff_write_global_symbol logic works. */
3885 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3886 hsym
->root
.type
= bfd_link_hash_defined
;
3887 hsym
->root
.u
.def
.value
= 0;
3890 /* Garbage collect unused sections. */
3891 if (bfd_link_relocatable (info
) || !gc
)
3894 xcoff_hash_table (info
)->gc
= false;
3896 /* We still need to call xcoff_mark, in order to set ldrel_count
3898 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3902 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3904 /* We shouldn't unconditionaly mark the TOC section.
3905 The output file should only have a TOC if either
3906 (a) one of the input files did or (b) we end up
3907 creating TOC references as part of the link process. */
3908 if (o
!= xcoff_hash_table (info
)->toc_section
3911 if (! xcoff_mark (info
, o
))
3920 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3922 if (info
->init_function
!= NULL
3923 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3925 if (info
->fini_function
!= NULL
3926 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3928 if (auto_export_flags
!= 0)
3930 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3931 xcoff_mark_auto_exports
, ldinfo
);
3936 xcoff_hash_table (info
)->gc
= true;
3939 /* Return special sections to the caller. */
3940 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3942 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3946 && sec
->gc_mark
== 0)
3949 special_sections
[i
] = sec
;
3952 if (info
->input_bfds
== NULL
)
3953 /* I'm not sure what to do in this bizarre case. */
3956 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3961 if (xcoff_hash_table (info
)->loader_section
3962 && !xcoff_size_loader_section (ldinfo
))
3968 free (ldinfo
->strings
);
3972 /* Lay out the .loader section, finalizing its header and
3973 filling the import paths */
3975 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
)
3979 struct xcoff_link_hash_table
*htab
;
3980 struct internal_ldhdr
*ldhdr
;
3981 struct xcoff_import_file
*fl
;
3984 output_bfd
= ldinfo
->output_bfd
;
3985 htab
= xcoff_hash_table (ldinfo
->info
);
3986 lsec
= htab
->loader_section
;
3987 ldhdr
= &htab
->ldhdr
;
3989 /* We could have called xcoff_size_loader_section one more time.
3990 However, this function is called once all the addresses have
3991 been layout thus the .loader section shouldn't be changed
3993 BFD_ASSERT (ldhdr
->l_nsyms
== ldinfo
->ldsym_count
);
3994 BFD_ASSERT (ldhdr
->l_nreloc
== ldinfo
->ldrel_count
);
3996 /* We now know the final size of the .loader section. Allocate
3998 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3999 if (lsec
->contents
== NULL
)
4002 /* Set up the header. */
4003 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
4005 /* Set up the import file names. */
4006 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
4007 strcpy (out
, ldinfo
->libpath
);
4008 out
+= strlen (ldinfo
->libpath
) + 1;
4011 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
4016 while ((*out
++ = *s
++) != '\0')
4019 while ((*out
++ = *s
++) != '\0')
4022 while ((*out
++ = *s
++) != '\0')
4026 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == ldhdr
->l_impoff
+ ldhdr
->l_istlen
);
4028 /* Set up the symbol string table. */
4029 if (ldinfo
->string_size
> 0)
4031 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
4032 free (ldinfo
->strings
);
4033 ldinfo
->strings
= NULL
;
4036 /* We can't set up the symbol table or the relocs yet, because we
4037 don't yet know the final position of the various sections. The
4038 .loader symbols are written out when the corresponding normal
4039 symbols are written out in xcoff_link_input_bfd or
4040 xcoff_write_global_symbol. The .loader relocs are written out
4041 when the corresponding normal relocs are handled in
4042 xcoff_link_input_bfd. */
4048 /* Lay out the .loader section and allocate the space for
4049 the other dynamic sections of XCOFF. */
4051 bfd_xcoff_build_dynamic_sections (bfd
*output_bfd
,
4052 struct bfd_link_info
*info
)
4054 struct xcoff_loader_info
*ldinfo
;
4055 struct bfd_strtab_hash
*debug_strtab
;
4056 bfd_byte
*debug_contents
= NULL
;
4060 ldinfo
= &(xcoff_hash_table (info
)->ldinfo
);
4062 if (xcoff_hash_table (info
)->loader_section
4063 && !xcoff_build_loader_section (ldinfo
))
4066 /* Allocate space for the magic sections. */
4067 sec
= xcoff_hash_table (info
)->linkage_section
;
4070 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
4071 if (sec
->contents
== NULL
)
4074 sec
= xcoff_hash_table (info
)->toc_section
;
4077 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
4078 if (sec
->contents
== NULL
)
4081 sec
= xcoff_hash_table (info
)->descriptor_section
;
4084 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
4085 if (sec
->contents
== NULL
)
4089 /* Now that we've done garbage collection, decide which symbols to keep,
4090 and figure out the contents of the .debug section. */
4091 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
4093 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
4096 bfd_size_type symcount
;
4099 unsigned int *lineno_counts
;
4100 struct xcoff_link_hash_entry
**sym_hash
;
4101 bfd_byte
*esym
, *esymend
;
4102 bfd_size_type symesz
;
4104 if (sub
->xvec
!= info
->output_bfd
->xvec
)
4107 if ((sub
->flags
& DYNAMIC
) != 0
4108 && !info
->static_link
)
4111 if (! _bfd_coff_get_external_symbols (sub
))
4114 symcount
= obj_raw_syment_count (sub
);
4115 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
4116 if (debug_index
== NULL
)
4118 xcoff_data (sub
)->debug_indices
= debug_index
;
4120 if (info
->strip
== strip_all
4121 || info
->strip
== strip_debugger
4122 || info
->discard
== discard_all
)
4123 /* We're stripping all debugging information, so there's no need
4124 to read SUB's .debug section. */
4128 /* Grab the contents of SUB's .debug section, if any. */
4129 subdeb
= bfd_get_section_by_name (sub
, ".debug");
4131 && subdeb
->size
!= 0
4132 && (subdeb
->flags
& SEC_HAS_CONTENTS
) != 0)
4134 /* We use malloc and copy the names into the debug
4135 stringtab, rather than bfd_alloc, because I expect
4136 that, when linking many files together, many of the
4137 strings will be the same. Storing the strings in the
4138 hash table should save space in this case. */
4139 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
4144 csectpp
= xcoff_data (sub
)->csects
;
4145 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
4146 sym_hash
= obj_xcoff_sym_hashes (sub
);
4147 symesz
= bfd_coff_symesz (sub
);
4148 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
4149 esymend
= esym
+ symcount
* symesz
;
4151 while (esym
< esymend
)
4153 struct internal_syment sym
;
4154 union internal_auxent aux
;
4159 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
4161 /* Read in the csect information, if any. */
4162 if (CSECT_SYM_P (sym
.n_sclass
))
4164 BFD_ASSERT (sym
.n_numaux
> 0);
4165 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
4166 sym
.n_type
, sym
.n_sclass
,
4167 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
4170 /* If this symbol's name is stored in the debug section,
4171 get a pointer to it. */
4172 if (debug_contents
!= NULL
4173 && sym
._n
._n_n
._n_zeroes
== 0
4174 && bfd_coff_symname_in_debug (sub
, &sym
))
4175 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
4179 /* Decide whether to copy this symbol to the output file. */
4181 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
4182 *sym_hash
, csect
, name
);
4187 /* Use a debug_index of -2 to record that a symbol should
4192 /* See whether we should store the symbol name in the
4193 output .debug section. */
4198 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
4199 if (indx
== (bfd_size_type
) -1)
4201 *debug_index
= indx
;
4206 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
4207 if (*lineno_counts
> 0)
4208 csect
->output_section
->lineno_count
+= *lineno_counts
;
4211 esym
+= (sym
.n_numaux
+ 1) * symesz
;
4212 csectpp
+= sym
.n_numaux
+ 1;
4213 sym_hash
+= sym
.n_numaux
+ 1;
4214 lineno_counts
+= sym
.n_numaux
+ 1;
4215 debug_index
+= sym
.n_numaux
+ 1;
4220 free (debug_contents
);
4221 debug_contents
= NULL
;
4223 /* Clear the size of subdeb, so that it is not included directly
4224 in the output file. */
4228 if (! info
->keep_memory
)
4230 if (! _bfd_coff_free_symbols (sub
))
4235 if (info
->strip
!= strip_all
4236 && xcoff_hash_table (info
)->debug_section
!= NULL
)
4237 xcoff_hash_table (info
)->debug_section
->size
=
4238 _bfd_stringtab_size (debug_strtab
);
4243 free (debug_contents
);
4248 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
4253 struct bfd_in_memory
*bim
;
4255 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
4262 abfd
->link
.next
= 0;
4263 abfd
->format
= bfd_object
;
4264 abfd
->iostream
= (void *) bim
;
4265 abfd
->flags
= BFD_IN_MEMORY
;
4266 abfd
->iovec
= &_bfd_memory_iovec
;
4267 abfd
->direction
= write_direction
;
4271 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
4274 /* need to reset to unknown or it will not be read back in correctly */
4275 abfd
->format
= bfd_unknown
;
4276 abfd
->direction
= read_direction
;
4284 The stubs will be gathered in stub csects named "@FIX'number'".
4285 A new csect will be created by xcoff_stub_get_csect_in_range,
4286 everytime a relocation cannot reach its target and its section
4287 is too far from the others stub csects.
4288 The stubs will simply be code generated inside these stub
4289 csects. In order to simplify the symbol table, only the symbols
4290 for the stub csects are written.
4292 As the code is dependent of the architecture, it's defined
4295 xcoff_stub_indirect_call:
4296 Used when a 24 bit branch cannot reach its destination and that
4297 this destination isn't a global linkage symbol.
4299 xcoff_stub_shared_call:
4300 As above but when it's a global linkage symbol.
4301 The main difference being that it doesn't branch to the global
4302 linkage symbol which will then call the shared library. It
4303 directly call it saving the TOC.
4305 TODO: -bbigtoc option should be able to be implemented using
4308 /* Get the name of a csect which will contain stubs.
4309 It has the same pattern as AIX linker: @FIX"number". */
4311 xcoff_stub_csect_name (unsigned int n
)
4317 /* For now, allow "only" 1000000 stub csects. */
4324 sprintf (buf
, "%d", n
);
4325 len
= 4 + strlen (buf
) + 1;
4327 csect_name
= bfd_malloc (len
);
4328 if (csect_name
== NULL
)
4330 sprintf (csect_name
, "@FIX%d", n
);
4335 /* Return a stub section which can be reach with a single branch
4336 from SECTION. CREATE means that creating a csect is allowed. */
4337 static struct xcoff_link_hash_entry
*
4338 xcoff_stub_get_csect_in_range (asection
*section
,
4339 struct bfd_link_info
*info
,
4342 struct xcoff_link_hash_table
*htab
= xcoff_hash_table (info
);
4343 struct xcoff_link_hash_entry
*csect_entry
;
4344 struct bfd_link_hash_entry
*bh
= NULL
;
4349 /* Search for a csect in range. */
4350 for (csect
= htab
->params
->stub_bfd
->sections
, it
= 0;
4352 csect
= csect
->next
, it
++)
4354 /* A csect is in range if everything instructions in SECTION
4355 can branch to every stubs in the stub csect. This can
4356 be simplify by saying that the first entry of each sections
4357 (ie the vma of this section) can reach the last entry of the
4358 stub csect (ie the vma of the csect + its size).
4359 However, as the stub csect might be growing its size isn't
4360 fixed. Thus, the last entry of SECTION might not be able
4361 to reach the first entry of the stub csect anymore.
4362 If this case happens, the following condition will be
4363 false during the next pass of bfd_xcoff_size_stubs and
4364 another csect will be used.
4365 This means we might create more stubs than needed. */
4366 bfd_vma csect_vma
, section_vma
;
4367 bfd_vma csect_last_vma
, section_last_vma
;
4369 csect_vma
= (csect
->output_section
->vma
4370 + csect
->output_offset
);
4371 csect_last_vma
= (csect
->output_section
->vma
4372 + csect
->output_offset
4374 section_vma
= (section
->output_section
->vma
4375 + section
->output_offset
);
4376 section_last_vma
= (section
->output_section
->vma
4377 + section
->output_offset
4380 if (csect_last_vma
- section_vma
+ (1 << 25) < 2 * (1 << 25)
4381 && section_last_vma
- csect_vma
+ (1 << 25) < 2 * (1 << 25))
4385 if (!create
&& csect
== NULL
)
4388 csect_name
= xcoff_stub_csect_name (it
);
4392 /* A stub csect already exists, get its entry. */
4395 csect_entry
= xcoff_link_hash_lookup (htab
, csect_name
, false, false, true);
4400 /* Create the csect and its symbol. */
4401 csect
= (*htab
->params
->add_stub_section
) (".pr", section
);
4408 csect
->alignment_power
= 2;
4410 csect
->reloc_count
= 0;
4412 /* We need to associate a VMA to this new csect. Otherwise,
4413 our "in range" algorithm won't find it for the next stub.
4414 And as we will be adding this stub section just after the
4415 SECTION, we know its address. */
4416 csect
->output_offset
= BFD_ALIGN (section
->output_offset
+ section
->size
,
4419 if (!_bfd_generic_link_add_one_symbol (info
, htab
->params
->stub_bfd
,
4420 csect_name
, BSF_GLOBAL
, csect
, 0,
4421 NULL
, true, true, &bh
))
4427 csect_entry
= (struct xcoff_link_hash_entry
*)bh
;
4428 csect_entry
->smclas
= XMC_PR
;
4429 csect_entry
->flags
= XCOFF_MARK
| XCOFF_DEF_REGULAR
;
4436 /* Build a name for an entry in the stub hash table. */
4438 xcoff_stub_name (const struct xcoff_link_hash_entry
*h
,
4439 const struct xcoff_link_hash_entry
*hcsect
)
4446 /* The name of a stub is based on its stub csect and the
4447 symbol it wants to reach. It looks like: ".@FIX0.tramp.f".
4448 When the stub targets a function, the last dot of ".tramp."
4449 is removed to avoid having two dot. */
4451 + strlen (hcsect
->root
.root
.string
)
4452 + strlen (h
->root
.root
.string
)
4454 if (h
->root
.root
.string
[0] != '.')
4457 stub_name
= bfd_malloc (len
);
4458 if (stub_name
== NULL
)
4461 if (h
->root
.root
.string
[0] == '.')
4462 sprintf (stub_name
, ".%s.tramp%s",
4463 hcsect
->root
.root
.string
,
4464 h
->root
.root
.string
);
4466 sprintf (stub_name
, ".%s.tramp.%s",
4467 hcsect
->root
.root
.string
,
4468 h
->root
.root
.string
);
4479 /* Look up an entry in the stub hash. */
4480 struct xcoff_stub_hash_entry
*
4481 bfd_xcoff_get_stub_entry (asection
*section
,
4482 struct xcoff_link_hash_entry
*h
,
4483 struct bfd_link_info
*info
)
4485 struct xcoff_link_hash_table
*htab
= xcoff_hash_table (info
);
4486 struct xcoff_link_hash_entry
*hcsect
;
4487 struct xcoff_stub_hash_entry
*hstub
;
4490 hcsect
= xcoff_stub_get_csect_in_range (section
, info
, false);
4494 stub_name
= xcoff_stub_name (h
, hcsect
);
4495 if (stub_name
== NULL
)
4498 hstub
= xcoff_stub_hash_lookup (&htab
->stub_hash_table
,
4499 stub_name
, false, false);
4505 /* Check if the symbol targeted by IREL is reachable.
4506 Return the type of stub needed otherwise. */
4507 enum xcoff_stub_type
4508 bfd_xcoff_type_of_stub (asection
*sec
,
4509 const struct internal_reloc
*irel
,
4510 bfd_vma destination
,
4511 struct xcoff_link_hash_entry
*h
)
4513 bfd_vma location
, offset
, max_offset
;
4515 switch (irel
->r_type
)
4518 return xcoff_stub_none
;
4522 location
= (sec
->output_section
->vma
4523 + sec
->output_offset
4527 max_offset
= 1 << 25 ;
4529 offset
= destination
- location
;
4531 if (offset
+ max_offset
< 2 * max_offset
)
4532 return xcoff_stub_none
;
4534 /* A stub is needed. Now, check that we can make one. */
4536 && h
->descriptor
!= NULL
)
4538 /* Not sure how to handle this case. For now, skip it. */
4539 if (bfd_is_abs_section (h
->root
.u
.def
.section
))
4540 return xcoff_stub_none
;
4542 if (h
->smclas
== XMC_GL
)
4543 return xcoff_stub_shared_call
;
4545 return xcoff_stub_indirect_call
;
4550 return xcoff_stub_none
;
4553 /* Add a new stub entry to the stub hash. Not all fields of the new
4554 stub entry are initialised. */
4555 static struct xcoff_stub_hash_entry
*
4556 xcoff_add_stub (const char *stub_name
,
4557 struct xcoff_link_hash_entry
*hstub_csect
,
4558 struct xcoff_link_hash_entry
*htarget
,
4559 struct bfd_link_info
*info
,
4560 enum xcoff_stub_type stub_type
)
4562 struct xcoff_link_hash_table
*htab
= xcoff_hash_table (info
);
4563 struct xcoff_stub_hash_entry
*hstub
;
4564 bfd_vma stub_offset
;
4565 asection
*stub_csect
;
4567 stub_csect
= hstub_csect
->root
.u
.def
.section
;
4568 stub_offset
= stub_csect
->size
;
4570 /* Update the relocation counter and the size of
4571 the containing csect. The size is needed for
4572 the algorithm in xcoff_stub_get_csect_in_range. */
4579 case xcoff_stub_indirect_call
:
4580 stub_csect
->reloc_count
++;
4581 stub_csect
->size
+= bfd_xcoff_stub_indirect_call_size (info
->output_bfd
);
4584 case xcoff_stub_shared_call
:
4585 stub_csect
->reloc_count
++;
4586 stub_csect
->size
+= bfd_xcoff_stub_shared_call_size (info
->output_bfd
);
4590 /* Create the stub entry. */
4591 hstub
= xcoff_stub_hash_lookup (&htab
->stub_hash_table
, stub_name
,
4596 hstub
->htarget
= htarget
;
4597 hstub
->stub_offset
= stub_offset
;
4599 /* For indirect call or shared call, the relocations are against
4600 the target descriptor. Its toc entry will be used. */
4601 if (stub_type
== xcoff_stub_indirect_call
4602 || stub_type
== xcoff_stub_shared_call
)
4604 struct xcoff_link_hash_entry
*hds
= htarget
->descriptor
;
4605 asection
*hds_section
= hds
->root
.u
.def
.section
;
4607 hstub
->htarget
= hds
;
4609 /* If the symbol haven't been marked, its section might have
4610 its size and its relocation count been deleted by xcoff_sweep.
4612 if ((hds
->flags
& XCOFF_MARK
) == 0)
4614 if (hds_section
->size
== 0
4615 && hds_section
->reloc_count
== 0
4616 && hds_section
->rawsize
!= 0)
4618 hds_section
->size
= hds_section
->rawsize
;
4619 /* Always two relocations for a XMC_DS symbol. */
4620 hds_section
->reloc_count
= 2;
4623 /* Mark the section and the symbol. */
4624 if (!xcoff_mark (info
, hds_section
))
4628 /* Add a TOC entry for the descriptor if non exists. */
4629 if (hds
->toc_section
== NULL
)
4633 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
4635 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
4640 /* Allocate room in the fallback TOC section. */
4641 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
4642 hds
->u
.toc_offset
= hds
->toc_section
->size
;
4643 hds
->toc_section
->size
+= byte_size
;
4644 if (!xcoff_mark (info
, hds
->toc_section
))
4647 /* Update relocation counters for a static and dynamic
4648 R_TOC relocation. */
4649 ++hds
->toc_section
->reloc_count
;
4650 ++htab
->ldinfo
.ldrel_count
;
4652 /* Set the index to -2 to force this symbol to
4655 hds
->flags
|= XCOFF_SET_TOC
;
4663 xcoff_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg
)
4665 struct xcoff_stub_hash_entry
*hstub
4666 = (struct xcoff_stub_hash_entry
*) gen_entry
;
4670 struct bfd_link_info
*info
;
4675 info
= (struct bfd_link_info
*) in_arg
;
4676 stub_bfd
= xcoff_hash_table (info
)->params
->stub_bfd
;
4677 output_bfd
= info
->output_bfd
;
4679 /* Fail if the target section could not be assigned to an output
4680 section. The user should fix his linker script. */
4681 if (hstub
->target_section
!= NULL
4682 && hstub
->target_section
->output_section
== NULL
4683 && info
->non_contiguous_regions
)
4684 info
->callbacks
->einfo (_("%F%P: Could not assign `%pA' to an output section. "
4685 "Retry without --enable-non-contiguous-regions.\n"),
4686 hstub
->target_section
);
4688 loc
= (hstub
->hcsect
->root
.u
.def
.section
->contents
4689 + hstub
->stub_offset
);
4692 switch (hstub
->stub_type
)
4694 case xcoff_stub_indirect_call
:
4695 BFD_ASSERT (hstub
->htarget
->toc_section
!= NULL
);
4696 /* The first instruction in the stub code needs to be
4697 cooked to hold the correct offset in the toc. It will
4698 be filled by xcoff_stub_create_relocations. */
4699 for (i
= 0; i
< bfd_xcoff_stub_indirect_call_size(output_bfd
) / 4; i
++)
4700 bfd_put_32 (stub_bfd
,
4701 (bfd_vma
) bfd_xcoff_stub_indirect_call_code(output_bfd
, i
),
4705 case xcoff_stub_shared_call
:
4706 BFD_ASSERT (hstub
->htarget
->toc_section
!= NULL
);
4707 /* The first instruction in the glink code needs to be
4708 cooked to hold the correct offset in the toc. It will
4709 be filled by xcoff_stub_create_relocations. */
4710 for (i
= 0; i
< bfd_xcoff_stub_shared_call_size(output_bfd
) / 4; i
++)
4711 bfd_put_32 (stub_bfd
,
4712 (bfd_vma
) bfd_xcoff_stub_shared_call_code(output_bfd
, i
),
4724 /* Check relocations and adds stubs if needed. */
4727 bfd_xcoff_size_stubs (struct bfd_link_info
*info
)
4729 struct xcoff_link_hash_table
*htab
= xcoff_hash_table (info
);
4730 struct xcoff_loader_info
*ldinfo
= &(htab
->ldinfo
);
4735 bool stub_changed
= false;
4737 for (input_bfd
= info
->input_bfds
;
4739 input_bfd
= input_bfd
->link
.next
)
4742 bfd_size_type symcount
;
4743 bfd_size_type symesz
;
4746 if (bfd_get_flavour (input_bfd
) != bfd_target_xcoff_flavour
)
4749 symcount
= obj_raw_syment_count (input_bfd
);
4752 symesz
= bfd_coff_symesz (input_bfd
);
4753 esyms
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4755 /* Walk over each section attached to the input bfd. */
4756 for (section
= input_bfd
->sections
;
4758 section
= section
->next
)
4760 struct internal_reloc
*internal_relocs
;
4761 struct internal_reloc
*irel
, *irelend
;
4763 /* If there aren't any relocs, then there's nothing more
4765 if ((section
->flags
& SEC_RELOC
) == 0
4766 || section
->reloc_count
== 0)
4769 /* If this section is a link-once section that will be
4770 discarded, then don't create any stubs. */
4771 if (section
->output_section
== NULL
4772 || section
->output_section
->owner
!= info
->output_bfd
)
4775 /* This section have been garbage-collected. */
4776 if (section
->gc_mark
== 0)
4779 /* Read in the relocs. */
4780 internal_relocs
= (xcoff_read_internal_relocs
4781 (input_bfd
, section
, true, NULL
,
4783 if (internal_relocs
== NULL
)
4786 irel
= internal_relocs
;
4787 irelend
= irel
+ section
->reloc_count
;
4788 for (; irel
< irelend
; irel
++)
4790 enum xcoff_stub_type stub_type
;
4791 struct xcoff_link_hash_entry
*hsym
= NULL
;
4792 struct xcoff_link_hash_entry
*hstub_csect
= NULL
;
4793 struct xcoff_stub_hash_entry
*hstub
= NULL
;
4796 bfd_vma destination
;
4799 if (irel
->r_symndx
== -1)
4802 switch (irel
->r_type
)
4812 /* Retrieve targeted symbol address */
4813 hsym
= obj_xcoff_sym_hashes (input_bfd
)[irel
->r_symndx
];
4816 struct internal_syment sym
;
4817 if ((long unsigned int)irel
->r_symndx
> symcount
)
4823 bfd_coff_swap_sym_in (input_bfd
,
4824 (void *) esyms
+ irel
->r_symndx
* symesz
,
4827 sym_sec
= xcoff_data (input_bfd
)->csects
[irel
->r_symndx
];
4828 sym_value
= sym
.n_value
- sym_sec
->vma
;
4830 destination
= (sym_value
4831 + sym_sec
->output_section
->vma
4832 + sym_sec
->output_offset
);
4834 else if (hsym
->root
.type
== bfd_link_hash_defined
4835 || hsym
->root
.type
== bfd_link_hash_defweak
)
4837 sym_sec
= hsym
->root
.u
.def
.section
;
4838 sym_value
= hsym
->root
.u
.def
.value
;
4839 destination
= (sym_value
4840 + sym_sec
->output_section
->vma
4841 + sym_sec
->output_offset
);
4845 bfd_set_error (bfd_error_bad_value
);
4849 /* I'm not sure how to handle this case. Skip it for now. */
4850 if (bfd_is_abs_section (sym_sec
))
4853 stub_type
= bfd_xcoff_type_of_stub (section
, irel
, destination
, hsym
);
4855 if (stub_type
== xcoff_stub_none
)
4858 /* Get a stub csect in ranch. */
4859 hstub_csect
= xcoff_stub_get_csect_in_range (section
, info
, true);
4862 /* xgettext:c-format */
4863 _bfd_error_handler (_("%pB: Unable to find a stub csect in range"
4864 "of relocation at %#" PRIx64
" targeting"
4866 section
->owner
, (uint64_t) irel
->r_vaddr
,
4867 hsym
->root
.root
.string
);
4871 /* Get the name of this stub. */
4872 stub_name
= xcoff_stub_name (hsym
, hstub_csect
);
4876 hstub
= xcoff_stub_hash_lookup (&(xcoff_hash_table (info
)->stub_hash_table
),
4877 stub_name
, false, false);
4879 /* A stub entry inside the in range csect already exists. */
4886 stub_changed
= true;
4888 hstub
= xcoff_add_stub (stub_name
, hstub_csect
, hsym
, info
, stub_type
);
4891 /* xgettext:c-format */
4892 _bfd_error_handler (_("%pB: Cannot create stub entry '%s'"),
4893 section
->owner
, stub_name
);
4898 hstub
->stub_type
= stub_type
;
4899 hstub
->hcsect
= hstub_csect
;
4900 hstub
->target_section
= sym_sec
;
4909 /* Update the size of the loader. */
4910 if (xcoff_hash_table (info
)->loader_section
4911 && !xcoff_size_loader_section (ldinfo
))
4914 /* Ask the linker to do its stuff. */
4915 (*htab
->params
->layout_sections_again
) ();
4921 bfd_set_error (bfd_error_bad_value
);
4926 bfd_xcoff_build_stubs (struct bfd_link_info
*info
)
4928 struct xcoff_link_hash_table
*htab
= xcoff_hash_table (info
);
4931 for (stub_sec
= htab
->params
->stub_bfd
->sections
;
4933 stub_sec
= stub_sec
->next
)
4937 /* Allocate memory to hold the linker stubs. */
4938 size
= stub_sec
->size
;
4939 stub_sec
->contents
= bfd_zalloc (htab
->params
->stub_bfd
, size
);
4940 if (stub_sec
->contents
== NULL
&& size
!= 0)
4945 /* Build the stubs as directed by the stub hash table. */
4946 bfd_hash_traverse (&htab
->stub_hash_table
, xcoff_build_one_stub
, info
);
4950 /* Create and apply relocations made by a stub entry. */
4952 xcoff_stub_create_relocations (struct bfd_hash_entry
*bh
, void * inf
)
4954 struct xcoff_stub_hash_entry
*hstub
4955 = (struct xcoff_stub_hash_entry
*) bh
;
4956 struct xcoff_final_link_info
*flinfo
4957 = (struct xcoff_final_link_info
*) inf
;
4960 struct internal_reloc
*irel
;
4961 struct xcoff_link_hash_entry
**rel_hash
;
4962 struct xcoff_link_hash_entry
*htarget
;
4963 asection
*sec
, *osec
;
4967 htarget
= hstub
->htarget
;
4968 sec
= hstub
->hcsect
->root
.u
.def
.section
;
4969 osec
= sec
->output_section
;
4971 irel
= (flinfo
->section_info
[osec
->target_index
].relocs
4972 + osec
->reloc_count
);
4973 rel_hash
= (flinfo
->section_info
[osec
->target_index
].rel_hashes
4974 + osec
->output_section
->reloc_count
);
4976 output_bfd
= flinfo
->output_bfd
;
4978 irel
->r_symndx
= htarget
->indx
;
4979 irel
->r_vaddr
= (osec
->vma
4980 + sec
->output_offset
4981 + hstub
->hcsect
->root
.u
.def
.value
4982 + hstub
->stub_offset
);
4985 + hstub
->stub_offset
);
4987 switch (hstub
->stub_type
)
4993 /* The first instruction of this stub code need
4994 a R_TOC relocation. */
4995 case xcoff_stub_indirect_call
:
4996 case xcoff_stub_shared_call
:
4998 irel
->r_type
= R_TOC
;
5000 /* Retrieve the toc offset of the target which is
5001 a function descriptor. */
5002 BFD_ASSERT (htarget
->toc_section
!= NULL
);
5003 if ((htarget
->flags
& XCOFF_SET_TOC
) != 0)
5004 off
= hstub
->htarget
->u
.toc_offset
;
5006 off
= (htarget
->toc_section
->output_section
->vma
5007 + htarget
->toc_section
->output_offset
5008 - xcoff_data (flinfo
->output_bfd
)->toc
);
5009 if ((off
& 0xffff) != off
)
5012 (_("TOC overflow during stub generation; try -mminimal-toc "
5014 bfd_set_error (bfd_error_file_too_big
);
5018 bfd_put_16 (output_bfd
, off
& 0xffff, p
+2);
5022 ++osec
->reloc_count
;
5027 /* Return the section that defines H. Return null if no section does. */
5030 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
5032 switch (h
->root
.type
)
5034 case bfd_link_hash_defined
:
5035 case bfd_link_hash_defweak
:
5036 return h
->root
.u
.def
.section
;
5038 case bfd_link_hash_common
:
5039 return h
->root
.u
.c
.p
->section
;
5046 /* Add a .loader relocation for input relocation IREL. If the loader
5047 relocation should be against an output section, HSEC points to the
5048 input section that IREL is against, otherwise HSEC is null. H is the
5049 symbol that IREL is against, or null if it isn't against a global symbol.
5050 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
5053 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
5054 asection
*output_section
, bfd
*reference_bfd
,
5055 struct internal_reloc
*irel
, asection
*hsec
,
5056 struct xcoff_link_hash_entry
*h
)
5058 struct internal_ldrel ldrel
;
5060 ldrel
.l_vaddr
= irel
->r_vaddr
;
5063 const char *secname
;
5065 secname
= hsec
->output_section
->name
;
5066 if (strcmp (secname
, ".text") == 0)
5068 else if (strcmp (secname
, ".data") == 0)
5070 else if (strcmp (secname
, ".bss") == 0)
5072 else if (strcmp (secname
, ".tdata") == 0)
5073 ldrel
.l_symndx
= -1;
5074 else if (strcmp (secname
, ".tbss") == 0)
5075 ldrel
.l_symndx
= -2;
5079 /* xgettext:c-format */
5080 (_("%pB: loader reloc in unrecognized section `%s'"),
5081 reference_bfd
, secname
);
5082 bfd_set_error (bfd_error_nonrepresentable_section
);
5091 /* xgettext:c-format */
5092 (_("%pB: `%s' in loader reloc but not loader sym"),
5093 reference_bfd
, h
->root
.root
.string
);
5094 bfd_set_error (bfd_error_bad_value
);
5097 ldrel
.l_symndx
= h
->ldindx
;
5100 ldrel
.l_symndx
= -(bfd_size_type
) 1;
5102 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
5103 ldrel
.l_rsecnm
= output_section
->target_index
;
5104 if (xcoff_hash_table (flinfo
->info
)->textro
5105 && strcmp (output_section
->name
, ".text") == 0)
5108 /* xgettext:c-format */
5109 (_("%pB: loader reloc in read-only section %pA"),
5110 reference_bfd
, output_section
);
5111 bfd_set_error (bfd_error_invalid_operation
);
5114 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
5115 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
5119 /* Link an input file into the linker output file. This function
5120 handles all the sections and relocations of the input file at once. */
5123 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
5127 const char *strings
;
5128 bfd_size_type syment_base
;
5129 unsigned int n_tmask
;
5130 unsigned int n_btshft
;
5132 bfd_size_type isymesz
;
5133 bfd_size_type osymesz
;
5134 bfd_size_type linesz
;
5137 struct xcoff_link_hash_entry
**sym_hash
;
5138 struct internal_syment
*isymp
;
5140 unsigned int *lineno_counts
;
5143 unsigned long output_index
;
5150 /* We can just skip DYNAMIC files, unless this is a static link. */
5151 if ((input_bfd
->flags
& DYNAMIC
) != 0
5152 && ! flinfo
->info
->static_link
)
5155 /* Move all the symbols to the output file. */
5156 output_bfd
= flinfo
->output_bfd
;
5158 syment_base
= obj_raw_syment_count (output_bfd
);
5159 isymesz
= bfd_coff_symesz (input_bfd
);
5160 osymesz
= bfd_coff_symesz (output_bfd
);
5161 linesz
= bfd_coff_linesz (input_bfd
);
5162 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
5164 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
5165 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
5167 /* Define macros so that ISFCN, et. al., macros work correctly. */
5168 #define N_TMASK n_tmask
5169 #define N_BTSHFT n_btshft
5172 if (! flinfo
->info
->keep_memory
)
5175 if (flinfo
->info
->traditional_format
)
5178 if (! _bfd_coff_get_external_symbols (input_bfd
))
5181 /* Make one pass over the symbols and assign indices to symbols that
5182 we have decided to keep. Also use create .loader symbol information
5183 and update information in hash table entries. */
5184 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
5185 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
5186 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
5187 csectpp
= xcoff_data (input_bfd
)->csects
;
5188 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
5189 isymp
= flinfo
->internal_syms
;
5190 indexp
= flinfo
->sym_indices
;
5191 output_index
= syment_base
;
5192 while (esym
< esym_end
)
5194 union internal_auxent aux
;
5198 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
5200 /* Read in the csect information, if any. */
5201 if (CSECT_SYM_P (isymp
->n_sclass
))
5203 BFD_ASSERT (isymp
->n_numaux
> 0);
5204 bfd_coff_swap_aux_in (input_bfd
,
5205 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
5206 isymp
->n_type
, isymp
->n_sclass
,
5207 isymp
->n_numaux
- 1, isymp
->n_numaux
,
5210 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
5213 /* If this symbol is in the .loader section, swap out the
5214 .loader symbol information. If this is an external symbol
5215 reference to a defined symbol, though, then wait until we get
5216 to the definition. */
5217 if (EXTERN_SYM_P (isymp
->n_sclass
)
5218 && *sym_hash
!= NULL
5219 && (*sym_hash
)->ldsym
!= NULL
5220 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
5222 struct xcoff_link_hash_entry
*h
;
5223 struct internal_ldsym
*ldsym
;
5227 if (isymp
->n_scnum
> 0)
5229 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
5230 ldsym
->l_value
= (isymp
->n_value
5231 + (*csectpp
)->output_section
->vma
5232 + (*csectpp
)->output_offset
5237 ldsym
->l_scnum
= isymp
->n_scnum
;
5238 ldsym
->l_value
= isymp
->n_value
;
5241 ldsym
->l_smtype
= smtyp
;
5242 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5243 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5244 || (h
->flags
& XCOFF_IMPORT
) != 0)
5245 ldsym
->l_smtype
|= L_IMPORT
;
5246 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5247 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5248 || (h
->flags
& XCOFF_EXPORT
) != 0)
5249 ldsym
->l_smtype
|= L_EXPORT
;
5250 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5251 ldsym
->l_smtype
|= L_ENTRY
;
5252 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
5253 ldsym
->l_smtype
|= L_WEAK
;
5255 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
5257 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
5259 else if (ldsym
->l_ifile
== 0)
5261 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5267 if (h
->root
.type
== bfd_link_hash_defined
5268 || h
->root
.type
== bfd_link_hash_defweak
)
5269 impbfd
= h
->root
.u
.def
.section
->owner
;
5270 else if (h
->root
.type
== bfd_link_hash_undefined
5271 || h
->root
.type
== bfd_link_hash_undefweak
)
5272 impbfd
= h
->root
.u
.undef
.abfd
;
5280 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
5281 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5288 BFD_ASSERT (h
->ldindx
>= 0);
5289 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
5292 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
5295 /* Fill in snentry now that we know the target_index. */
5296 if ((h
->flags
& XCOFF_ENTRY
) != 0
5297 && (h
->root
.type
== bfd_link_hash_defined
5298 || h
->root
.type
== bfd_link_hash_defweak
))
5300 xcoff_data (output_bfd
)->snentry
=
5301 h
->root
.u
.def
.section
->output_section
->target_index
;
5305 add
= 1 + isymp
->n_numaux
;
5307 if (*debug_index
== -2)
5308 /* We've decided to strip this symbol. */
5312 /* Assign the next unused index to this symbol. */
5313 *indexp
= output_index
;
5315 if (EXTERN_SYM_P (isymp
->n_sclass
))
5317 BFD_ASSERT (*sym_hash
!= NULL
);
5318 (*sym_hash
)->indx
= output_index
;
5321 /* If this is a symbol in the TOC which we may have merged
5322 (class XMC_TC), remember the symbol index of the TOC
5324 if (isymp
->n_sclass
== C_HIDEXT
5325 && aux
.x_csect
.x_smclas
== XMC_TC
5326 && *sym_hash
!= NULL
)
5328 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
5329 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
5330 (*sym_hash
)->u
.toc_indx
= output_index
;
5333 output_index
+= add
;
5336 esym
+= add
* isymesz
;
5342 for (--add
; add
> 0; --add
)
5346 /* Now write out the symbols that we decided to keep. */
5348 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
5349 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
5350 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
5351 isymp
= flinfo
->internal_syms
;
5352 indexp
= flinfo
->sym_indices
;
5353 csectpp
= xcoff_data (input_bfd
)->csects
;
5354 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
5355 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
5356 outsym
= flinfo
->outsyms
;
5359 while (esym
< esym_end
)
5363 add
= 1 + isymp
->n_numaux
;
5366 esym
+= add
* isymesz
;
5369 struct internal_syment isym
;
5372 /* Adjust the symbol in order to output it. */
5374 if (isym
._n
._n_n
._n_zeroes
== 0
5375 && isym
._n
._n_n
._n_offset
!= 0)
5377 /* This symbol has a long name. Enter it in the string
5378 table we are building. If *debug_index != -1, the
5379 name has already been entered in the .debug section. */
5380 if (*debug_index
>= 0)
5381 isym
._n
._n_n
._n_offset
= *debug_index
;
5387 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
5391 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
5392 if (indx
== (bfd_size_type
) -1)
5394 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
5398 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
5399 multiple definition problems when linking a shared object
5400 statically. (The native linker doesn't enter __rtinit into
5401 the normal table at all, but having a local symbol can make
5402 the objdump output easier to read.) */
5403 if (isym
.n_sclass
== C_EXT
5405 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
5406 isym
.n_sclass
= C_HIDEXT
;
5408 /* The value of a C_FILE symbol is the symbol index of the
5409 next C_FILE symbol. The value of the last C_FILE symbol
5410 is -1. We try to get this right, below, just before we
5411 write the symbols out, but in the general case we may
5412 have to write the symbol out twice. */
5413 if (isym
.n_sclass
== C_FILE
)
5415 if (flinfo
->last_file_index
!= -1
5416 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
5418 /* We must correct the value of the last C_FILE entry. */
5419 flinfo
->last_file
.n_value
= *indexp
;
5420 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
5422 /* The last C_FILE symbol is in this input file. */
5423 bfd_coff_swap_sym_out (output_bfd
,
5424 (void *) &flinfo
->last_file
,
5425 (void *) (flinfo
->outsyms
5426 + ((flinfo
->last_file_index
5432 /* We have already written out the last C_FILE
5433 symbol. We need to write it out again. We
5434 borrow *outsym temporarily. */
5437 bfd_coff_swap_sym_out (output_bfd
,
5438 (void *) &flinfo
->last_file
,
5441 pos
= obj_sym_filepos (output_bfd
);
5442 pos
+= flinfo
->last_file_index
* osymesz
;
5443 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5444 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
5450 flinfo
->last_file_index
= *indexp
;
5451 flinfo
->last_file
= isym
;
5454 /* The value of a C_BINCL or C_EINCL symbol is a file offset
5455 into the line numbers. We update the symbol values when
5456 we handle the line numbers. */
5457 if (isym
.n_sclass
== C_BINCL
5458 || isym
.n_sclass
== C_EINCL
)
5460 isym
.n_value
= flinfo
->line_filepos
;
5463 /* The value of a C_BSTAT symbol is the symbol table
5464 index of the containing csect. */
5465 else if (isym
.n_sclass
== C_BSTAT
)
5469 indx
= isym
.n_value
;
5470 if (indx
< obj_raw_syment_count (input_bfd
))
5474 symindx
= flinfo
->sym_indices
[indx
];
5478 isym
.n_value
= symindx
;
5481 else if (isym
.n_sclass
!= C_ESTAT
5482 && isym
.n_sclass
!= C_DECL
5483 && isym
.n_scnum
> 0)
5485 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
5486 isym
.n_value
+= ((*csectpp
)->output_section
->vma
5487 + (*csectpp
)->output_offset
5491 /* Update visibility. */
5494 isym
.n_type
&= ~SYM_V_MASK
;
5495 isym
.n_type
|= (*sym_hash
)->visibility
;
5498 /* Output the symbol. */
5499 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5504 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
5506 union internal_auxent aux
;
5508 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
5509 isymp
->n_sclass
, i
, isymp
->n_numaux
,
5512 if (isymp
->n_sclass
== C_FILE
)
5514 /* This is the file name (or some comment put in by
5515 the compiler). If it is long, we must put it in
5516 the string table. */
5517 if (aux
.x_file
.x_n
.x_n
.x_zeroes
== 0
5518 && aux
.x_file
.x_n
.x_n
.x_offset
!= 0)
5520 const char *filename
;
5523 BFD_ASSERT (aux
.x_file
.x_n
.x_n
.x_offset
5524 >= STRING_SIZE_SIZE
);
5525 if (strings
== NULL
)
5527 strings
= _bfd_coff_read_string_table (input_bfd
);
5528 if (strings
== NULL
)
5531 if ((bfd_size_type
) aux
.x_file
.x_n
.x_n
.x_offset
>= obj_coff_strings_len (input_bfd
))
5532 filename
= _("<corrupt>");
5534 filename
= strings
+ aux
.x_file
.x_n
.x_n
.x_offset
;
5535 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
5537 if (indx
== (bfd_size_type
) -1)
5539 aux
.x_file
.x_n
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
5542 else if (CSECT_SYM_P (isymp
->n_sclass
)
5543 && i
+ 1 == isymp
->n_numaux
)
5546 /* We don't support type checking. I don't know if
5548 aux
.x_csect
.x_parmhash
= 0;
5549 /* I don't think anybody uses these fields, but we'd
5550 better clobber them just in case. */
5551 aux
.x_csect
.x_stab
= 0;
5552 aux
.x_csect
.x_snstab
= 0;
5554 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
5558 indx
= aux
.x_csect
.x_scnlen
.u64
;
5559 if (indx
< obj_raw_syment_count (input_bfd
))
5563 symindx
= flinfo
->sym_indices
[indx
];
5566 aux
.x_csect
.x_scnlen
.u64
= 0;
5570 aux
.x_csect
.x_scnlen
.u64
= symindx
;
5575 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
5579 if (ISFCN (isymp
->n_type
)
5580 || ISTAG (isymp
->n_sclass
)
5581 || isymp
->n_sclass
== C_BLOCK
5582 || isymp
->n_sclass
== C_FCN
)
5584 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
;
5586 && indx
< obj_raw_syment_count (input_bfd
))
5588 /* We look forward through the symbol for
5589 the index of the next symbol we are going
5590 to include. I don't know if this is
5592 while (flinfo
->sym_indices
[indx
] < 0
5593 && indx
< obj_raw_syment_count (input_bfd
))
5595 if (indx
>= obj_raw_syment_count (input_bfd
))
5596 indx
= output_index
;
5598 indx
= flinfo
->sym_indices
[indx
];
5599 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
= indx
;
5604 indx
= aux
.x_sym
.x_tagndx
.u32
;
5605 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
5609 symindx
= flinfo
->sym_indices
[indx
];
5611 aux
.x_sym
.x_tagndx
.u32
= 0;
5613 aux
.x_sym
.x_tagndx
.u32
= symindx
;
5618 /* Copy over the line numbers, unless we are stripping
5619 them. We do this on a symbol by symbol basis in
5620 order to more easily handle garbage collection. */
5621 if (CSECT_SYM_P (isymp
->n_sclass
)
5623 && isymp
->n_numaux
> 1
5624 && ISFCN (isymp
->n_type
)
5625 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
5627 if (*lineno_counts
== 0)
5628 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
5631 asection
*enclosing
;
5632 unsigned int enc_count
;
5633 bfd_signed_vma linoff
;
5634 struct internal_lineno lin
;
5641 /* Read in the enclosing section's line-number
5642 information, if we haven't already. */
5644 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
5645 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
5646 if (oline
!= enclosing
)
5648 pos
= enclosing
->line_filepos
;
5649 amt
= linesz
* enc_count
;
5650 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
5651 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
5657 /* Copy across the first entry, adjusting its
5659 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
5660 - enclosing
->line_filepos
);
5661 linp
= flinfo
->linenos
+ linoff
;
5662 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
5663 lin
.l_addr
.l_symndx
= *indexp
;
5664 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
5666 /* Copy the other entries, adjusting their addresses. */
5667 linpend
= linp
+ *lineno_counts
* linesz
;
5668 offset
= (o
->output_section
->vma
5671 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
5673 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
5674 lin
.l_addr
.l_paddr
+= offset
;
5675 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
5678 /* Write out the entries we've just processed. */
5679 pos
= (o
->output_section
->line_filepos
5680 + o
->output_section
->lineno_count
* linesz
);
5681 amt
= linesz
* *lineno_counts
;
5682 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5683 || bfd_bwrite (flinfo
->linenos
+ linoff
,
5684 amt
, output_bfd
) != amt
)
5686 o
->output_section
->lineno_count
+= *lineno_counts
;
5688 /* Record the offset of the symbol's line numbers
5689 in the output file. */
5690 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
5694 struct internal_syment
*iisp
, *iispend
;
5697 bfd_vma range_start
, range_end
;
5700 /* Update any C_BINCL or C_EINCL symbols
5701 that refer to a line number in the
5702 range we just output. */
5703 iisp
= flinfo
->internal_syms
;
5704 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
5705 iindp
= flinfo
->sym_indices
;
5706 oos
= flinfo
->outsyms
;
5707 range_start
= enclosing
->line_filepos
+ linoff
;
5708 range_end
= range_start
+ *lineno_counts
* linesz
;
5709 while (iisp
< iispend
)
5712 && (iisp
->n_sclass
== C_BINCL
5713 || iisp
->n_sclass
== C_EINCL
)
5714 && iisp
->n_value
>= range_start
5715 && iisp
->n_value
< range_end
)
5717 struct internal_syment iis
;
5719 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
5720 iis
.n_value
= (iisp
->n_value
5723 bfd_coff_swap_sym_out (output_bfd
,
5728 iiadd
= 1 + iisp
->n_numaux
;
5730 oos
+= iiadd
* osymesz
;
5738 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
5739 isymp
->n_sclass
, i
, isymp
->n_numaux
,
5750 lineno_counts
+= add
;
5754 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5755 symbol will be the first symbol in the next input file. In the
5756 normal case, this will save us from writing out the C_FILE symbol
5758 if (flinfo
->last_file_index
!= -1
5759 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
5761 flinfo
->last_file
.n_value
= output_index
;
5762 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
5763 (void *) (flinfo
->outsyms
5764 + ((flinfo
->last_file_index
- syment_base
)
5768 /* Write the modified symbols to the output file. */
5769 if (outsym
> flinfo
->outsyms
)
5771 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
5772 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
5773 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5774 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5777 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
5778 + (outsym
- flinfo
->outsyms
) / osymesz
)
5781 obj_raw_syment_count (output_bfd
) = output_index
;
5784 /* Don't let the linker relocation routines discard the symbols. */
5785 keep_syms
= obj_coff_keep_syms (input_bfd
);
5786 obj_coff_keep_syms (input_bfd
) = true;
5788 /* Relocate the contents of each section. */
5789 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5793 if (! o
->linker_mark
)
5794 /* This section was omitted from the link. */
5797 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5799 || (o
->flags
& SEC_IN_MEMORY
) != 0)
5802 /* We have set filepos correctly for the sections we created to
5803 represent csects, so bfd_get_section_contents should work. */
5804 if (coff_section_data (input_bfd
, o
) != NULL
5805 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
5806 contents
= coff_section_data (input_bfd
, o
)->contents
;
5809 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
5810 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
5812 contents
= flinfo
->contents
;
5815 if ((o
->flags
& SEC_RELOC
) != 0)
5818 struct internal_reloc
*internal_relocs
;
5819 struct internal_reloc
*irel
;
5821 struct internal_reloc
*irelend
;
5822 struct xcoff_link_hash_entry
**rel_hash
;
5825 /* Read in the relocs. */
5826 target_index
= o
->output_section
->target_index
;
5827 internal_relocs
= (xcoff_read_internal_relocs
5828 (input_bfd
, o
, false, flinfo
->external_relocs
,
5830 (flinfo
->section_info
[target_index
].relocs
5831 + o
->output_section
->reloc_count
)));
5832 if (internal_relocs
== NULL
)
5835 /* Call processor specific code to relocate the section
5837 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
5841 flinfo
->internal_syms
,
5842 xcoff_data (input_bfd
)->csects
))
5845 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
5846 irel
= internal_relocs
;
5847 irelend
= irel
+ o
->reloc_count
;
5848 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
5849 + o
->output_section
->reloc_count
);
5850 for (; irel
< irelend
; irel
++, rel_hash
++)
5852 struct xcoff_link_hash_entry
*h
= NULL
;
5856 /* Adjust the reloc address and symbol index. */
5858 r_symndx
= irel
->r_symndx
;
5863 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
5865 /* In case of a R_BR or R_RBR, change the target if
5866 a stub is being called. */
5868 && (irel
->r_type
== R_BR
5869 || irel
->r_type
== R_RBR
))
5873 struct xcoff_stub_hash_entry
*hstub
= NULL
;
5874 enum xcoff_stub_type stub_type
;
5876 if (h
->root
.type
== bfd_link_hash_defined
5877 || h
->root
.type
== bfd_link_hash_defweak
)
5879 sym_sec
= h
->root
.u
.def
.section
;
5880 dest
= (h
->root
.u
.def
.value
5881 + sym_sec
->output_section
->vma
5882 + sym_sec
->output_offset
);
5890 stub_type
= bfd_xcoff_type_of_stub (o
, irel
, dest
, h
);
5891 if (stub_type
!= xcoff_stub_none
)
5893 hstub
= bfd_xcoff_get_stub_entry (o
, h
, flinfo
->info
);
5902 irel
->r_vaddr
+= offset
;
5904 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
5908 && h
->smclas
!= XMC_TD
5909 && (irel
->r_type
== R_TOC
5910 || irel
->r_type
== R_GL
5911 || irel
->r_type
== R_TCL
5912 || irel
->r_type
== R_TRL
5913 || irel
->r_type
== R_TRLA
))
5915 /* This is a TOC relative reloc with a symbol
5916 attached. The symbol should be the one which
5917 this reloc is for. We want to make this
5918 reloc against the TOC address of the symbol,
5919 not the symbol itself. */
5920 BFD_ASSERT (h
->toc_section
!= NULL
);
5921 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
5922 if (h
->u
.toc_indx
!= -1)
5923 irel
->r_symndx
= h
->u
.toc_indx
;
5926 struct xcoff_toc_rel_hash
*n
;
5927 struct xcoff_link_section_info
*si
;
5931 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
5934 si
= flinfo
->section_info
+ target_index
;
5935 n
->next
= si
->toc_rel_hashes
;
5938 si
->toc_rel_hashes
= n
;
5943 /* This is a global symbol. */
5945 irel
->r_symndx
= h
->indx
;
5948 /* This symbol is being written at the end
5949 of the file, and we do not yet know the
5950 symbol index. We save the pointer to the
5951 hash table entry in the rel_hash list.
5952 We set the indx field to -2 to indicate
5953 that this symbol must not be stripped. */
5962 indx
= flinfo
->sym_indices
[r_symndx
];
5966 struct internal_syment
*is
;
5968 /* Relocations against a TC0 TOC anchor are
5969 automatically transformed to be against
5970 the TOC anchor in the output file. */
5971 is
= flinfo
->internal_syms
+ r_symndx
;
5972 if (is
->n_sclass
== C_HIDEXT
5973 && is
->n_numaux
> 0)
5976 union internal_auxent aux
;
5980 obj_coff_external_syms (input_bfd
))
5981 + ((r_symndx
+ is
->n_numaux
)
5983 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
5984 is
->n_type
, is
->n_sclass
,
5988 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
5989 && aux
.x_csect
.x_smclas
== XMC_TC0
)
5990 indx
= flinfo
->toc_symindx
;
5995 irel
->r_symndx
= indx
;
5999 struct internal_syment
*is
;
6002 char buf
[SYMNMLEN
+ 1];
6004 /* This reloc is against a symbol we are
6005 stripping. It would be possible to handle
6006 this case, but I don't think it's worth it. */
6007 is
= flinfo
->internal_syms
+ r_symndx
;
6009 if (is
->n_sclass
!= C_DWARF
)
6011 name
= (_bfd_coff_internal_syment_name
6012 (input_bfd
, is
, buf
));
6017 (*flinfo
->info
->callbacks
->unattached_reloc
)
6018 (flinfo
->info
, name
,
6019 input_bfd
, o
, irel
->r_vaddr
);
6025 if ((o
->flags
& SEC_DEBUGGING
) == 0
6026 && xcoff_need_ldrel_p (flinfo
->info
, irel
, h
, o
))
6033 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
6035 sec
= xcoff_symbol_section (h
);
6036 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
6037 o
->output_section
, input_bfd
,
6043 o
->output_section
->reloc_count
+= o
->reloc_count
;
6046 /* Write out the modified section contents. */
6047 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
6048 contents
, (file_ptr
) o
->output_offset
,
6053 obj_coff_keep_syms (input_bfd
) = keep_syms
;
6055 if (! flinfo
->info
->keep_memory
)
6057 if (! _bfd_coff_free_symbols (input_bfd
))
6064 obj_coff_keep_syms (input_bfd
) = keep_syms
;
6071 /* Sort relocs by VMA. This is called via qsort. */
6074 xcoff_sort_relocs (const void * p1
, const void * p2
)
6076 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
6077 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
6079 if (r1
->r_vaddr
> r2
->r_vaddr
)
6081 else if (r1
->r_vaddr
< r2
->r_vaddr
)
6087 /* Return true if section SEC is a TOC section. */
6090 xcoff_toc_section_p (asection
*sec
)
6095 if (name
[0] == '.' && name
[1] == 't')
6099 if (name
[3] == '0' && name
[4] == 0)
6104 if (name
[2] == 'd' && name
[3] == 0)
6110 /* See if the link requires a TOC (it usually does!). If so, find a
6111 good place to put the TOC anchor csect, and write out the associated
6115 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
6117 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
6121 struct internal_syment irsym
;
6122 union internal_auxent iraux
;
6126 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
6127 index of a csect at the beginning of the TOC. */
6128 toc_start
= ~(bfd_vma
) 0;
6131 for (input_bfd
= flinfo
->info
->input_bfds
;
6133 input_bfd
= input_bfd
->link
.next
)
6134 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6135 if (sec
->gc_mark
!= 0 && xcoff_toc_section_p (sec
))
6137 start
= sec
->output_section
->vma
+ sec
->output_offset
;
6138 if (toc_start
> start
)
6141 section_index
= sec
->output_section
->target_index
;
6144 end
= start
+ sec
->size
;
6149 /* There's no need for a TC0 symbol if we don't have a TOC. */
6150 if (toc_end
< toc_start
)
6152 xcoff_data (output_bfd
)->toc
= toc_start
;
6156 if (toc_end
- toc_start
< 0x8000)
6157 /* Every TOC csect can be accessed from TOC_START. */
6158 best_address
= toc_start
;
6161 /* Find the lowest TOC csect that is still within range of TOC_END. */
6162 best_address
= toc_end
;
6163 for (input_bfd
= flinfo
->info
->input_bfds
;
6165 input_bfd
= input_bfd
->link
.next
)
6166 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6167 if (sec
->gc_mark
!= 0 && xcoff_toc_section_p (sec
))
6169 start
= sec
->output_section
->vma
+ sec
->output_offset
;
6170 if (start
< best_address
6171 && start
+ 0x8000 >= toc_end
)
6173 best_address
= start
;
6174 section_index
= sec
->output_section
->target_index
;
6178 /* Make sure that the start of the TOC is also within range. */
6179 if (best_address
> toc_start
+ 0x8000)
6182 (_("TOC overflow: %#" PRIx64
" > 0x10000; try -mminimal-toc "
6184 (uint64_t) (toc_end
- toc_start
));
6185 bfd_set_error (bfd_error_file_too_big
);
6190 /* Record the chosen TOC value. */
6191 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
6192 xcoff_data (output_bfd
)->toc
= best_address
;
6193 xcoff_data (output_bfd
)->sntoc
= section_index
;
6195 /* Fill out the TC0 symbol. */
6196 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
6199 irsym
.n_value
= best_address
;
6200 irsym
.n_scnum
= section_index
;
6201 irsym
.n_sclass
= C_HIDEXT
;
6202 irsym
.n_type
= T_NULL
;
6204 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
6206 /* Fill out the auxiliary csect information. */
6207 memset (&iraux
, 0, sizeof iraux
);
6208 iraux
.x_csect
.x_smtyp
= XTY_SD
;
6209 iraux
.x_csect
.x_smclas
= XMC_TC0
;
6210 iraux
.x_csect
.x_scnlen
.u64
= 0;
6211 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
6212 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
6214 /* Write the contents to the file. */
6215 pos
= obj_sym_filepos (output_bfd
);
6216 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
6217 size
= 2 * bfd_coff_symesz (output_bfd
);
6218 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
6219 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
6221 obj_raw_syment_count (output_bfd
) += 2;
6226 /* Write out a non-XCOFF global symbol. */
6229 xcoff_write_global_symbol (struct bfd_hash_entry
*bh
, void * inf
)
6231 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) bh
;
6232 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
6235 struct internal_syment isym
;
6236 union internal_auxent aux
;
6241 output_bfd
= flinfo
->output_bfd
;
6242 outsym
= flinfo
->outsyms
;
6244 if (h
->root
.type
== bfd_link_hash_warning
)
6246 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
6247 if (h
->root
.type
== bfd_link_hash_new
)
6251 /* If this symbol was garbage collected, just skip it. */
6252 if (xcoff_hash_table (flinfo
->info
)->gc
6253 && (h
->flags
& XCOFF_MARK
) == 0)
6256 /* If we need a .loader section entry, write it out. */
6257 if (h
->ldsym
!= NULL
)
6259 struct internal_ldsym
*ldsym
;
6264 if (h
->root
.type
== bfd_link_hash_undefined
6265 || h
->root
.type
== bfd_link_hash_undefweak
)
6269 ldsym
->l_scnum
= N_UNDEF
;
6270 ldsym
->l_smtype
= XTY_ER
;
6271 impbfd
= h
->root
.u
.undef
.abfd
;
6274 else if (h
->root
.type
== bfd_link_hash_defined
6275 || h
->root
.type
== bfd_link_hash_defweak
)
6279 sec
= h
->root
.u
.def
.section
;
6280 ldsym
->l_value
= (sec
->output_section
->vma
6281 + sec
->output_offset
6282 + h
->root
.u
.def
.value
);
6283 ldsym
->l_scnum
= sec
->output_section
->target_index
;
6284 ldsym
->l_smtype
= XTY_SD
;
6285 impbfd
= sec
->owner
;
6291 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
6292 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
6293 || (h
->flags
& XCOFF_IMPORT
) != 0)
6295 Import symbols are defined so the check above will make
6296 the l_smtype XTY_SD. But this is not correct, it should
6298 ldsym
->l_smtype
|= L_IMPORT
;
6300 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
6301 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
6302 || (h
->flags
& XCOFF_EXPORT
) != 0)
6303 ldsym
->l_smtype
|= L_EXPORT
;
6305 if ((h
->flags
& XCOFF_ENTRY
) != 0)
6306 ldsym
->l_smtype
|= L_ENTRY
;
6308 if ((h
->flags
& XCOFF_RTINIT
) != 0)
6309 ldsym
->l_smtype
= XTY_SD
;
6311 ldsym
->l_smclas
= h
->smclas
;
6313 if (ldsym
->l_smtype
& L_IMPORT
)
6315 if ((h
->root
.type
== bfd_link_hash_defined
6316 || h
->root
.type
== bfd_link_hash_defweak
)
6317 && (h
->root
.u
.def
.value
!= 0))
6318 ldsym
->l_smclas
= XMC_XO
;
6320 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
6321 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
6322 ldsym
->l_smclas
= XMC_SV3264
;
6324 else if (h
->flags
& XCOFF_SYSCALL32
)
6325 ldsym
->l_smclas
= XMC_SV
;
6327 else if (h
->flags
& XCOFF_SYSCALL64
)
6328 ldsym
->l_smclas
= XMC_SV64
;
6331 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
6335 else if (ldsym
->l_ifile
== 0)
6337 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
6339 else if (impbfd
== NULL
)
6343 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
6344 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
6350 BFD_ASSERT (h
->ldindx
>= 0);
6352 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
6355 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
6359 /* If this symbol needs global linkage code, write it out. */
6360 if (h
->root
.type
== bfd_link_hash_defined
6361 && (h
->root
.u
.def
.section
6362 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
6368 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
6370 /* The first instruction in the global linkage code loads a
6371 specific TOC element. */
6372 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
6373 + h
->descriptor
->toc_section
->output_offset
6374 - xcoff_data (output_bfd
)->toc
);
6376 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
6377 tocoff
+= h
->descriptor
->u
.toc_offset
;
6379 /* The first instruction in the glink code needs to be
6380 cooked to hold the correct offset in the toc. The
6381 rest are just output raw. */
6382 bfd_put_32 (output_bfd
,
6383 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
6385 /* Start with i == 1 to get past the first instruction done above
6386 The /4 is because the glink code is in bytes and we are going
6388 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
6389 bfd_put_32 (output_bfd
,
6390 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
6394 /* If we created a TOC entry for this symbol, write out the required
6396 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
6401 struct internal_reloc
*irel
;
6402 struct internal_syment irsym
;
6403 union internal_auxent iraux
;
6405 tocsec
= h
->toc_section
;
6406 osec
= tocsec
->output_section
;
6407 oindx
= osec
->target_index
;
6408 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
6409 irel
->r_vaddr
= (osec
->vma
6410 + tocsec
->output_offset
6414 irel
->r_symndx
= h
->indx
;
6418 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
6421 /* Initialize the aux union here instead of closer to when it is
6422 written out below because the length of the csect depends on
6423 whether the output is 32 or 64 bit. */
6424 memset (&iraux
, 0, sizeof iraux
);
6425 iraux
.x_csect
.x_smtyp
= XTY_SD
;
6426 /* iraux.x_csect.x_scnlen.u64 = 4 or 8, see below. */
6427 iraux
.x_csect
.x_smclas
= XMC_TC
;
6429 /* 32 bit uses a 32 bit R_POS to do the relocations
6430 64 bit uses a 64 bit R_POS to do the relocations
6432 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
6434 Which one is determined by the backend. */
6435 if (bfd_xcoff_is_xcoff64 (output_bfd
))
6438 iraux
.x_csect
.x_scnlen
.u64
= 8;
6440 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
6443 iraux
.x_csect
.x_scnlen
.u64
= 4;
6448 irel
->r_type
= R_POS
;
6449 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
6450 ++osec
->reloc_count
;
6452 /* There are two kind of linker-created TOC entry.
6453 The ones importing their symbols from outside, made for the
6454 global linkage. These symbols have XCOFF_LDREL set and only
6455 requires a loader relocation on their imported symbol.
6456 On the other hand, symbols without XCOFF_LDREL are TOC entries
6457 of internal symbols (like function descriptors made for stubs).
6458 These symbols needs a loader relocation over .data and this
6459 relocation must be applied. */
6461 if ((h
->flags
& XCOFF_LDREL
) != 0
6464 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
6465 output_bfd
, irel
, NULL
, h
))
6473 p
= tocsec
->contents
+ h
->u
.toc_offset
;
6474 val
= (h
->root
.u
.def
.value
6475 + h
->root
.u
.def
.section
->output_section
->vma
6476 + h
->root
.u
.def
.section
->output_offset
);
6478 if (bfd_xcoff_is_xcoff64 (output_bfd
))
6479 bfd_put_64 (output_bfd
, val
, p
);
6480 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
6481 bfd_put_32 (output_bfd
, val
, p
);
6485 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
6486 output_bfd
, irel
, h
->root
.u
.def
.section
, h
))
6490 /* We need to emit a symbol to define a csect which holds
6492 if (flinfo
->info
->strip
!= strip_all
)
6494 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
,
6496 &irsym
, h
->root
.root
.string
);
6500 irsym
.n_value
= irel
->r_vaddr
;
6501 irsym
.n_scnum
= osec
->target_index
;
6502 irsym
.n_sclass
= C_HIDEXT
;
6503 irsym
.n_type
= T_NULL
;
6506 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
6507 outsym
+= bfd_coff_symesz (output_bfd
);
6509 /* Note : iraux is initialized above. */
6510 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
6511 0, 1, (void *) outsym
);
6512 outsym
+= bfd_coff_auxesz (output_bfd
);
6516 /* We aren't going to write out the symbols below, so we
6517 need to write them out now. */
6518 pos
= obj_sym_filepos (output_bfd
);
6519 pos
+= (obj_raw_syment_count (output_bfd
)
6520 * bfd_coff_symesz (output_bfd
));
6521 amt
= outsym
- flinfo
->outsyms
;
6522 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
6523 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
6525 obj_raw_syment_count (output_bfd
) +=
6526 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
6528 outsym
= flinfo
->outsyms
;
6533 /* If this symbol is a specially defined function descriptor, write
6534 it out. The first word is the address of the function code
6535 itself, the second word is the address of the TOC, and the third
6539 The addresses for the 32 bit will take 4 bytes and the addresses
6540 for 64 bit will take 8 bytes. Similar for the relocs. This type
6541 of logic was also done above to create a TOC entry in
6542 xcoff_write_global_symbol. */
6543 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
6544 && h
->root
.type
== bfd_link_hash_defined
6545 && (h
->root
.u
.def
.section
6546 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
6552 struct xcoff_link_hash_entry
*hentry
;
6554 struct internal_reloc
*irel
;
6556 unsigned int reloc_size
, byte_size
;
6558 if (bfd_xcoff_is_xcoff64 (output_bfd
))
6563 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
6571 sec
= h
->root
.u
.def
.section
;
6572 osec
= sec
->output_section
;
6573 oindx
= osec
->target_index
;
6574 p
= sec
->contents
+ h
->root
.u
.def
.value
;
6576 hentry
= h
->descriptor
;
6577 BFD_ASSERT (hentry
!= NULL
6578 && (hentry
->root
.type
== bfd_link_hash_defined
6579 || hentry
->root
.type
== bfd_link_hash_defweak
));
6580 esec
= hentry
->root
.u
.def
.section
;
6582 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
6583 irel
->r_vaddr
= (osec
->vma
6584 + sec
->output_offset
6585 + h
->root
.u
.def
.value
);
6586 irel
->r_symndx
= esec
->output_section
->target_index
;
6587 irel
->r_type
= R_POS
;
6588 irel
->r_size
= reloc_size
;
6589 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
6590 ++osec
->reloc_count
;
6592 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
6593 output_bfd
, irel
, esec
, NULL
))
6596 /* There are three items to write out,
6597 the address of the code
6598 the address of the toc anchor
6599 the environment pointer.
6600 We are ignoring the environment pointer. So set it to zero. */
6601 if (bfd_xcoff_is_xcoff64 (output_bfd
))
6603 bfd_put_64 (output_bfd
,
6604 (esec
->output_section
->vma
+ esec
->output_offset
6605 + hentry
->root
.u
.def
.value
),
6607 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
6608 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
6613 This logic was already called above so the error case where
6614 the backend is neither has already been checked. */
6615 bfd_put_32 (output_bfd
,
6616 (esec
->output_section
->vma
+ esec
->output_offset
6617 + hentry
->root
.u
.def
.value
),
6619 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
6620 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
6623 tsec
= coff_section_from_bfd_index (output_bfd
,
6624 xcoff_data (output_bfd
)->sntoc
);
6627 irel
->r_vaddr
= (osec
->vma
6628 + sec
->output_offset
6629 + h
->root
.u
.def
.value
6631 irel
->r_symndx
= tsec
->output_section
->target_index
;
6632 irel
->r_type
= R_POS
;
6633 irel
->r_size
= reloc_size
;
6634 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
6635 ++osec
->reloc_count
;
6637 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
6638 output_bfd
, irel
, tsec
, NULL
))
6642 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
6644 BFD_ASSERT (outsym
== flinfo
->outsyms
);
6649 && (flinfo
->info
->strip
== strip_all
6650 || (flinfo
->info
->strip
== strip_some
6651 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
6652 false, false) == NULL
)))
6654 BFD_ASSERT (outsym
== flinfo
->outsyms
);
6659 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
6661 BFD_ASSERT (outsym
== flinfo
->outsyms
);
6665 memset (&aux
, 0, sizeof aux
);
6667 h
->indx
= obj_raw_syment_count (output_bfd
);
6669 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
6670 &isym
, h
->root
.root
.string
);
6674 if (h
->root
.type
== bfd_link_hash_undefined
6675 || h
->root
.type
== bfd_link_hash_undefweak
)
6678 isym
.n_scnum
= N_UNDEF
;
6679 if (h
->root
.type
== bfd_link_hash_undefweak
6680 && C_WEAKEXT
== C_AIX_WEAKEXT
)
6681 isym
.n_sclass
= C_WEAKEXT
;
6683 isym
.n_sclass
= C_EXT
;
6684 aux
.x_csect
.x_smtyp
= XTY_ER
;
6686 else if ((h
->root
.type
== bfd_link_hash_defined
6687 || h
->root
.type
== bfd_link_hash_defweak
)
6688 && h
->smclas
== XMC_XO
)
6690 BFD_ASSERT (bfd_is_abs_symbol (&h
->root
));
6691 isym
.n_value
= h
->root
.u
.def
.value
;
6692 isym
.n_scnum
= N_UNDEF
;
6693 if (h
->root
.type
== bfd_link_hash_defweak
6694 && C_WEAKEXT
== C_AIX_WEAKEXT
)
6695 isym
.n_sclass
= C_WEAKEXT
;
6697 isym
.n_sclass
= C_EXT
;
6698 aux
.x_csect
.x_smtyp
= XTY_ER
;
6700 else if (h
->root
.type
== bfd_link_hash_defined
6701 || h
->root
.type
== bfd_link_hash_defweak
)
6703 struct xcoff_link_size_list
*l
;
6705 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
6706 + h
->root
.u
.def
.section
->output_offset
6707 + h
->root
.u
.def
.value
);
6708 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
6709 isym
.n_scnum
= N_ABS
;
6711 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
6712 isym
.n_sclass
= C_HIDEXT
;
6713 aux
.x_csect
.x_smtyp
= XTY_SD
;
6715 /* For stub symbols, the section already has its correct size. */
6716 if (h
->root
.u
.def
.section
->owner
== xcoff_hash_table (flinfo
->info
)->params
->stub_bfd
)
6718 aux
.x_csect
.x_scnlen
.u64
= h
->root
.u
.def
.section
->size
;
6720 else if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
6722 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
6728 aux
.x_csect
.x_scnlen
.u64
= l
->size
;
6734 else if (h
->root
.type
== bfd_link_hash_common
)
6736 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
6737 + h
->root
.u
.c
.p
->section
->output_offset
);
6738 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
6739 isym
.n_sclass
= C_EXT
;
6740 aux
.x_csect
.x_smtyp
= XTY_CM
;
6741 aux
.x_csect
.x_scnlen
.u64
= h
->root
.u
.c
.size
;
6746 isym
.n_type
= T_NULL
;
6749 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
6750 outsym
+= bfd_coff_symesz (output_bfd
);
6752 aux
.x_csect
.x_smclas
= h
->smclas
;
6753 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
6755 outsym
+= bfd_coff_auxesz (output_bfd
);
6757 if ((h
->root
.type
== bfd_link_hash_defined
6758 || h
->root
.type
== bfd_link_hash_defweak
)
6759 && h
->smclas
!= XMC_XO
)
6761 /* We just output an SD symbol. Now output an LD symbol. */
6764 if (h
->root
.type
== bfd_link_hash_defweak
6765 && C_WEAKEXT
== C_AIX_WEAKEXT
)
6766 isym
.n_sclass
= C_WEAKEXT
;
6768 isym
.n_sclass
= C_EXT
;
6769 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
6770 outsym
+= bfd_coff_symesz (output_bfd
);
6772 aux
.x_csect
.x_smtyp
= XTY_LD
;
6773 aux
.x_csect
.x_scnlen
.u64
= obj_raw_syment_count (output_bfd
);
6774 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
6776 outsym
+= bfd_coff_auxesz (output_bfd
);
6779 pos
= obj_sym_filepos (output_bfd
);
6780 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
6781 amt
= outsym
- flinfo
->outsyms
;
6782 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
6783 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
6785 obj_raw_syment_count (output_bfd
) +=
6786 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
6791 /* Handle a link order which is supposed to generate a reloc. */
6794 xcoff_reloc_link_order (bfd
*output_bfd
,
6795 struct xcoff_final_link_info
*flinfo
,
6796 asection
*output_section
,
6797 struct bfd_link_order
*link_order
)
6799 reloc_howto_type
*howto
;
6800 struct xcoff_link_hash_entry
*h
;
6804 struct internal_reloc
*irel
;
6805 struct xcoff_link_hash_entry
**rel_hash_ptr
;
6807 if (link_order
->type
== bfd_section_reloc_link_order
)
6808 /* We need to somehow locate a symbol in the right section. The
6809 symbol must either have a value of zero, or we must adjust
6810 the addend by the value of the symbol. FIXME: Write this
6811 when we need it. The old linker couldn't handle this anyhow. */
6814 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6817 bfd_set_error (bfd_error_bad_value
);
6821 h
= ((struct xcoff_link_hash_entry
*)
6822 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
6823 link_order
->u
.reloc
.p
->u
.name
,
6824 false, false, true));
6827 (*flinfo
->info
->callbacks
->unattached_reloc
)
6828 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
6832 hsec
= xcoff_symbol_section (h
);
6833 if (h
->root
.type
== bfd_link_hash_defined
6834 || h
->root
.type
== bfd_link_hash_defweak
)
6835 hval
= h
->root
.u
.def
.value
;
6839 addend
= link_order
->u
.reloc
.p
->addend
;
6841 addend
+= (hsec
->output_section
->vma
6842 + hsec
->output_offset
6849 bfd_reloc_status_type rstat
;
6852 size
= bfd_get_reloc_size (howto
);
6853 buf
= bfd_zmalloc (size
);
6854 if (buf
== NULL
&& size
!= 0)
6857 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
6863 case bfd_reloc_outofrange
:
6865 case bfd_reloc_overflow
:
6866 (*flinfo
->info
->callbacks
->reloc_overflow
)
6867 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
6868 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0);
6871 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
6872 (file_ptr
) link_order
->offset
, size
);
6878 /* Store the reloc information in the right place. It will get
6879 swapped and written out at the end of the final_link routine. */
6880 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
6881 + output_section
->reloc_count
);
6882 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
6883 + output_section
->reloc_count
);
6885 memset (irel
, 0, sizeof (struct internal_reloc
));
6886 *rel_hash_ptr
= NULL
;
6888 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
6891 irel
->r_symndx
= h
->indx
;
6894 /* Set the index to -2 to force this symbol to get written out. */
6900 irel
->r_type
= howto
->type
;
6901 irel
->r_size
= howto
->bitsize
- 1;
6902 if (howto
->complain_on_overflow
== complain_overflow_signed
)
6903 irel
->r_size
|= 0x80;
6905 ++output_section
->reloc_count
;
6907 /* Now output the reloc to the .loader section. */
6908 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
6910 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
6911 output_bfd
, irel
, hsec
, h
))
6918 /* Do the final link step. */
6921 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
6923 bfd_size_type symesz
;
6924 struct xcoff_final_link_info flinfo
;
6926 struct bfd_link_order
*p
;
6927 bfd_size_type max_contents_size
;
6928 bfd_size_type max_sym_count
;
6929 bfd_size_type max_lineno_count
;
6930 bfd_size_type max_reloc_count
;
6931 bfd_size_type max_output_reloc_count
;
6932 file_ptr rel_filepos
;
6934 file_ptr line_filepos
;
6935 unsigned int linesz
;
6937 bfd_byte
*external_relocs
= NULL
;
6938 char strbuf
[STRING_SIZE_SIZE
];
6942 if (bfd_link_pic (info
))
6943 abfd
->flags
|= DYNAMIC
;
6945 symesz
= bfd_coff_symesz (abfd
);
6948 flinfo
.output_bfd
= abfd
;
6949 flinfo
.strtab
= NULL
;
6950 flinfo
.section_info
= NULL
;
6951 flinfo
.last_file_index
= -1;
6952 flinfo
.toc_symindx
= -1;
6953 flinfo
.internal_syms
= NULL
;
6954 flinfo
.sym_indices
= NULL
;
6955 flinfo
.outsyms
= NULL
;
6956 flinfo
.linenos
= NULL
;
6957 flinfo
.contents
= NULL
;
6958 flinfo
.external_relocs
= NULL
;
6960 if (xcoff_hash_table (info
)->loader_section
)
6962 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
6963 + bfd_xcoff_ldhdrsz (abfd
));
6964 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
6965 + bfd_xcoff_ldhdrsz (abfd
)
6966 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
6967 * bfd_xcoff_ldsymsz (abfd
)));
6971 flinfo
.ldsym
= NULL
;
6972 flinfo
.ldrel
= NULL
;
6975 xcoff_data (abfd
)->coff
.link_info
= info
;
6977 flinfo
.strtab
= _bfd_stringtab_init ();
6978 if (flinfo
.strtab
== NULL
)
6981 /* Count the relocation entries required for the output file.
6982 (We've already counted the line numbers.) Determine a few
6984 max_contents_size
= 0;
6985 max_lineno_count
= 0;
6986 max_reloc_count
= 0;
6987 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6990 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6992 if (p
->type
== bfd_indirect_link_order
)
6996 sec
= p
->u
.indirect
.section
;
6998 /* Mark all sections which are to be included in the
6999 link. This will normally be every section. We need
7000 to do this so that we can identify any sections which
7001 the linker has decided to not include. */
7002 sec
->linker_mark
= true;
7004 o
->reloc_count
+= sec
->reloc_count
;
7006 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
7008 if (sec
->rawsize
> max_contents_size
)
7009 max_contents_size
= sec
->rawsize
;
7010 if (sec
->size
> max_contents_size
)
7011 max_contents_size
= sec
->size
;
7013 if (coff_section_data (sec
->owner
, sec
) != NULL
7014 && xcoff_section_data (sec
->owner
, sec
) != NULL
7015 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
7016 > max_lineno_count
))
7018 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
7019 if (sec
->reloc_count
> max_reloc_count
)
7020 max_reloc_count
= sec
->reloc_count
;
7022 else if (p
->type
== bfd_section_reloc_link_order
7023 || p
->type
== bfd_symbol_reloc_link_order
)
7028 /* Compute the file positions for all the sections. */
7029 if (abfd
->output_has_begun
)
7031 if (xcoff_hash_table (info
)->file_align
!= 0)
7038 file_align
= xcoff_hash_table (info
)->file_align
;
7039 if (file_align
!= 0)
7045 /* Insert .pad sections before every section which has
7046 contents and is loaded, if it is preceded by some other
7047 section which has contents and is loaded. */
7048 saw_contents
= true;
7049 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7051 if (strcmp (o
->name
, ".pad") == 0)
7052 saw_contents
= false;
7053 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
7054 && (o
->flags
& SEC_LOAD
) != 0)
7057 saw_contents
= true;
7062 /* Create a pad section and place it before the section
7063 that needs padding. This requires unlinking and
7064 relinking the bfd's section list. */
7066 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
7068 n
->alignment_power
= 0;
7070 bfd_section_list_remove (abfd
, n
);
7071 bfd_section_list_insert_before (abfd
, o
, n
);
7072 saw_contents
= false;
7077 /* Reset the section indices after inserting the new
7079 if (xcoff_data (abfd
)->coff
.section_by_target_index
)
7080 htab_empty (xcoff_data (abfd
)->coff
.section_by_target_index
);
7082 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7085 o
->target_index
= indx
;
7087 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
7089 /* Work out appropriate sizes for the .pad sections to force
7090 each section to land on a page boundary. This bit of
7091 code knows what compute_section_file_positions is going
7093 sofar
= bfd_coff_filhsz (abfd
);
7094 sofar
+= bfd_coff_aoutsz (abfd
);
7095 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
7096 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7097 if ((bfd_xcoff_is_reloc_count_overflow
7098 (abfd
, (bfd_vma
) o
->reloc_count
))
7099 || (bfd_xcoff_is_lineno_count_overflow
7100 (abfd
, (bfd_vma
) o
->lineno_count
)))
7101 /* 64 does not overflow, need to check if 32 does */
7102 sofar
+= bfd_coff_scnhsz (abfd
);
7104 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7106 if (strcmp (o
->name
, ".pad") == 0)
7110 BFD_ASSERT (o
->size
== 0);
7111 pageoff
= sofar
& (file_align
- 1);
7114 o
->size
= file_align
- pageoff
;
7115 sofar
+= file_align
- pageoff
;
7116 o
->flags
|= SEC_HAS_CONTENTS
;
7121 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
7122 sofar
+= BFD_ALIGN (o
->size
,
7123 1 << o
->alignment_power
);
7128 if (! bfd_coff_compute_section_file_positions (abfd
))
7132 /* Allocate space for the pointers we need to keep for the relocs. */
7136 /* We use section_count + 1, rather than section_count, because
7137 the target_index fields are 1 based. */
7138 amt
= abfd
->section_count
+ 1;
7139 amt
*= sizeof (struct xcoff_link_section_info
);
7140 flinfo
.section_info
= bfd_malloc (amt
);
7141 if (flinfo
.section_info
== NULL
)
7143 for (i
= 0; i
<= abfd
->section_count
; i
++)
7145 flinfo
.section_info
[i
].relocs
= NULL
;
7146 flinfo
.section_info
[i
].rel_hashes
= NULL
;
7147 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
7151 /* Set the file positions for the relocs. */
7152 rel_filepos
= obj_relocbase (abfd
);
7153 relsz
= bfd_coff_relsz (abfd
);
7154 max_output_reloc_count
= 0;
7155 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7157 if (o
->reloc_count
== 0)
7161 /* A stripped file has no relocs. However, we still
7162 allocate the buffers, so that later code doesn't have to
7163 worry about whether we are stripping or not. */
7164 if (info
->strip
== strip_all
)
7168 o
->flags
|= SEC_RELOC
;
7169 o
->rel_filepos
= rel_filepos
;
7170 rel_filepos
+= o
->reloc_count
* relsz
;
7173 /* We don't know the indices of global symbols until we have
7174 written out all the local symbols. For each section in
7175 the output file, we keep an array of pointers to hash
7176 table entries. Each entry in the array corresponds to a
7177 reloc. When we find a reloc against a global symbol, we
7178 set the corresponding entry in this array so that we can
7179 fix up the symbol index after we have written out all the
7182 Because of this problem, we also keep the relocs in
7183 memory until the end of the link. This wastes memory.
7184 We could backpatch the file later, I suppose, although it
7186 amt
= o
->reloc_count
;
7187 amt
*= sizeof (struct internal_reloc
);
7188 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
7190 amt
= o
->reloc_count
;
7191 amt
*= sizeof (struct xcoff_link_hash_entry
*);
7192 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
7194 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
7195 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
7198 if (o
->reloc_count
> max_output_reloc_count
)
7199 max_output_reloc_count
= o
->reloc_count
;
7203 /* We now know the size of the relocs, so we can determine the file
7204 positions of the line numbers. */
7205 line_filepos
= rel_filepos
;
7206 flinfo
.line_filepos
= line_filepos
;
7207 linesz
= bfd_coff_linesz (abfd
);
7208 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7210 if (o
->lineno_count
== 0)
7211 o
->line_filepos
= 0;
7214 o
->line_filepos
= line_filepos
;
7215 line_filepos
+= o
->lineno_count
* linesz
;
7218 /* Reset the reloc and lineno counts, so that we can use them to
7219 count the number of entries we have output so far. */
7221 o
->lineno_count
= 0;
7224 obj_sym_filepos (abfd
) = line_filepos
;
7226 /* Figure out the largest number of symbols in an input BFD. Take
7227 the opportunity to clear the output_has_begun fields of all the
7228 input BFD's. We want at least 6 symbols, since that is the
7229 number which xcoff_write_global_symbol may need. */
7231 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7235 sub
->output_has_begun
= false;
7236 sz
= obj_raw_syment_count (sub
);
7237 if (sz
> max_sym_count
)
7241 /* Allocate some buffers used while linking. */
7242 amt
= max_sym_count
* sizeof (struct internal_syment
);
7243 flinfo
.internal_syms
= bfd_malloc (amt
);
7245 amt
= max_sym_count
* sizeof (long);
7246 flinfo
.sym_indices
= bfd_malloc (amt
);
7248 amt
= (max_sym_count
+ 1) * symesz
;
7249 flinfo
.outsyms
= bfd_malloc (amt
);
7251 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
7252 flinfo
.linenos
= bfd_malloc (amt
);
7254 amt
= max_contents_size
;
7255 flinfo
.contents
= bfd_malloc (amt
);
7257 amt
= max_reloc_count
* relsz
;
7258 flinfo
.external_relocs
= bfd_malloc (amt
);
7260 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
7261 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
7262 || flinfo
.outsyms
== NULL
7263 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
7264 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
7265 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
7268 obj_raw_syment_count (abfd
) = 0;
7270 /* Find a TOC symbol, if we need one. */
7271 if (!xcoff_find_tc0 (abfd
, &flinfo
))
7274 /* We now know the position of everything in the file, except that
7275 we don't know the size of the symbol table and therefore we don't
7276 know where the string table starts. We just build the string
7277 table in memory as we go along. We process all the relocations
7278 for a single input file at once. */
7279 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7281 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7283 if (p
->type
== bfd_indirect_link_order
7284 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
7286 sub
= p
->u
.indirect
.section
->owner
;
7287 if (! sub
->output_has_begun
)
7289 if (sub
== xcoff_hash_table (info
)->params
->stub_bfd
)
7295 if (! xcoff_link_input_bfd (&flinfo
, sub
))
7298 (_("Unable to link input file: %s"), sub
->filename
);
7299 bfd_set_error (bfd_error_sorry
);
7303 sub
->output_has_begun
= true;
7306 else if (p
->type
== bfd_section_reloc_link_order
7307 || p
->type
== bfd_symbol_reloc_link_order
)
7309 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
7314 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7320 /* Free up the buffers used by xcoff_link_input_bfd. */
7321 free (flinfo
.internal_syms
);
7322 flinfo
.internal_syms
= NULL
;
7323 free (flinfo
.sym_indices
);
7324 flinfo
.sym_indices
= NULL
;
7325 free (flinfo
.linenos
);
7326 flinfo
.linenos
= NULL
;
7327 free (flinfo
.contents
);
7328 flinfo
.contents
= NULL
;
7329 free (flinfo
.external_relocs
);
7330 flinfo
.external_relocs
= NULL
;
7332 /* The value of the last C_FILE symbol is supposed to be -1. Write
7334 if (flinfo
.last_file_index
!= -1)
7336 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
7337 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
7338 (void *) flinfo
.outsyms
);
7339 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
7340 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7341 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
7345 /* Write out all the global symbols which do not come from XCOFF
7347 bfd_hash_traverse (&info
->hash
->table
, xcoff_write_global_symbol
, &flinfo
);
7349 /* Write out the relocations created by stub entries. The symbols
7350 will have been already written by xcoff_write_global_symbol. */
7351 bfd_hash_traverse (&xcoff_hash_table(info
)->stub_hash_table
,
7352 xcoff_stub_create_relocations
,
7355 free (flinfo
.outsyms
);
7356 flinfo
.outsyms
= NULL
;
7358 /* Now that we have written out all the global symbols, we know the
7359 symbol indices to use for relocs against them, and we can finally
7360 write out the relocs. */
7361 amt
= max_output_reloc_count
* relsz
;
7362 external_relocs
= bfd_malloc (amt
);
7363 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
7366 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7368 struct internal_reloc
*irel
;
7369 struct internal_reloc
*irelend
;
7370 struct xcoff_link_hash_entry
**rel_hash
;
7371 struct xcoff_toc_rel_hash
*toc_rel_hash
;
7373 bfd_size_type rel_size
;
7375 /* A stripped file has no relocs. */
7376 if (info
->strip
== strip_all
)
7382 if (o
->reloc_count
== 0)
7385 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
7386 irelend
= irel
+ o
->reloc_count
;
7387 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
7388 for (; irel
< irelend
; irel
++, rel_hash
++)
7390 if (*rel_hash
!= NULL
)
7392 if ((*rel_hash
)->indx
< 0)
7394 (*info
->callbacks
->unattached_reloc
)
7395 (info
, (*rel_hash
)->root
.root
.string
,
7396 NULL
, o
, irel
->r_vaddr
);
7397 (*rel_hash
)->indx
= 0;
7399 irel
->r_symndx
= (*rel_hash
)->indx
;
7403 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
7404 toc_rel_hash
!= NULL
;
7405 toc_rel_hash
= toc_rel_hash
->next
)
7407 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
7409 (*info
->callbacks
->unattached_reloc
)
7410 (info
, toc_rel_hash
->h
->root
.root
.string
,
7411 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
);
7412 toc_rel_hash
->h
->u
.toc_indx
= 0;
7414 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
7417 /* XCOFF requires that the relocs be sorted by address. We tend
7418 to produce them in the order in which their containing csects
7419 appear in the symbol table, which is not necessarily by
7420 address. So we sort them here. There may be a better way to
7422 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
7423 o
->reloc_count
, sizeof (struct internal_reloc
),
7426 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
7427 irelend
= irel
+ o
->reloc_count
;
7428 erel
= external_relocs
;
7429 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
7430 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
7432 rel_size
= relsz
* o
->reloc_count
;
7433 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
7434 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
7438 free (external_relocs
);
7439 external_relocs
= NULL
;
7441 /* Free up the section information. */
7442 if (flinfo
.section_info
!= NULL
)
7446 for (i
= 0; i
< abfd
->section_count
; i
++)
7448 free (flinfo
.section_info
[i
].relocs
);
7449 free (flinfo
.section_info
[i
].rel_hashes
);
7451 free (flinfo
.section_info
);
7452 flinfo
.section_info
= NULL
;
7455 /* Write out the stub sections. */
7456 for (o
= xcoff_hash_table (info
)->params
->stub_bfd
->sections
;
7457 o
!= NULL
; o
= o
->next
)
7459 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
7463 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
7464 (file_ptr
) o
->output_offset
, o
->size
))
7468 /* Write out the loader section contents. */
7469 o
= xcoff_hash_table (info
)->loader_section
;
7472 && o
->output_section
!= bfd_abs_section_ptr
)
7474 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
7475 == (xcoff_hash_table (info
)->loader_section
->contents
7476 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
7477 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
7478 (file_ptr
) o
->output_offset
, o
->size
))
7482 /* Write out the magic sections. */
7483 o
= xcoff_hash_table (info
)->linkage_section
;
7486 && o
->output_section
!= bfd_abs_section_ptr
7487 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
7488 (file_ptr
) o
->output_offset
,
7491 o
= xcoff_hash_table (info
)->toc_section
;
7494 && o
->output_section
!= bfd_abs_section_ptr
7495 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
7496 (file_ptr
) o
->output_offset
,
7499 o
= xcoff_hash_table (info
)->descriptor_section
;
7502 && o
->output_section
!= bfd_abs_section_ptr
7503 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
7504 (file_ptr
) o
->output_offset
,
7508 /* Write out the string table. */
7509 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
7510 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
7513 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
7515 amt
= STRING_SIZE_SIZE
;
7516 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
7518 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
7521 _bfd_stringtab_free (flinfo
.strtab
);
7523 /* Write out the debugging string table. */
7524 o
= xcoff_hash_table (info
)->debug_section
;
7527 && o
->output_section
!= bfd_abs_section_ptr
)
7529 struct bfd_strtab_hash
*debug_strtab
;
7531 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
7532 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
7533 >= _bfd_stringtab_size (debug_strtab
));
7534 pos
= o
->output_section
->filepos
+ o
->output_offset
;
7535 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
7537 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
7541 /* Setting symcount to 0 will cause write_object_contents to
7542 not try to write out the symbols. */
7548 if (flinfo
.strtab
!= NULL
)
7549 _bfd_stringtab_free (flinfo
.strtab
);
7551 if (flinfo
.section_info
!= NULL
)
7555 for (i
= 0; i
< abfd
->section_count
; i
++)
7557 free (flinfo
.section_info
[i
].relocs
);
7558 free (flinfo
.section_info
[i
].rel_hashes
);
7560 free (flinfo
.section_info
);
7563 free (flinfo
.internal_syms
);
7564 free (flinfo
.sym_indices
);
7565 free (flinfo
.outsyms
);
7566 free (flinfo
.linenos
);
7567 free (flinfo
.contents
);
7568 free (flinfo
.external_relocs
);
7569 free (external_relocs
);