1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2021 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 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
39 This flag will only be used on input sections. */
41 #define SEC_MARK (SEC_ROM)
43 /* The list of import files. */
45 struct xcoff_import_file
47 /* The next entry in the list. */
48 struct xcoff_import_file
*next
;
53 /* The member name. */
57 /* Information we keep for each section in the output file during the
60 struct xcoff_link_section_info
62 /* The relocs to be output. */
63 struct internal_reloc
*relocs
;
64 /* For each reloc against a global symbol whose index was not known
65 when the reloc was handled, the global hash table entry. */
66 struct xcoff_link_hash_entry
**rel_hashes
;
67 /* If there is a TOC relative reloc against a global symbol, and the
68 index of the TOC symbol is not known when the reloc was handled,
69 an entry is added to this linked list. This is not an array,
70 like rel_hashes, because this case is quite uncommon. */
71 struct xcoff_toc_rel_hash
73 struct xcoff_toc_rel_hash
*next
;
74 struct xcoff_link_hash_entry
*h
;
75 struct internal_reloc
*rel
;
79 /* Information that the XCOFF linker collects about an archive. */
80 struct xcoff_archive_info
82 /* The archive described by this entry. */
85 /* The import path and import filename to use when referring to
86 this archive in the .loader section. */
90 /* True if the archive contains a dynamic object. */
91 unsigned int contains_shared_object_p
: 1;
93 /* True if the previous field is valid. */
94 unsigned int know_contains_shared_object_p
: 1;
97 struct xcoff_link_hash_table
99 struct bfd_link_hash_table root
;
101 /* The .debug string hash table. We need to compute this while
102 reading the input files, so that we know how large the .debug
103 section will be before we assign section positions. */
104 struct bfd_strtab_hash
*debug_strtab
;
106 /* The .debug section we will use for the final output. */
107 asection
*debug_section
;
109 /* The .loader section we will use for the final output. */
110 asection
*loader_section
;
112 /* A count of non TOC relative relocs which will need to be
113 allocated in the .loader section. */
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 static bool xcoff_mark (struct bfd_link_info
*, asection
*);
206 /* Routines to read XCOFF dynamic information. This don't really
207 belong here, but we already have the ldsym manipulation routines
210 /* Read the contents of a section. */
213 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
215 if (coff_section_data (abfd
, sec
) == NULL
)
217 size_t amt
= sizeof (struct coff_section_tdata
);
219 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
220 if (sec
->used_by_bfd
== NULL
)
224 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
228 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
233 coff_section_data (abfd
, sec
)->contents
= contents
;
239 /* Get the size required to hold the dynamic symbols. */
242 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
246 struct internal_ldhdr ldhdr
;
248 if ((abfd
->flags
& DYNAMIC
) == 0)
250 bfd_set_error (bfd_error_invalid_operation
);
254 lsec
= bfd_get_section_by_name (abfd
, ".loader");
257 bfd_set_error (bfd_error_no_symbols
);
261 if (! xcoff_get_section_contents (abfd
, lsec
))
263 contents
= coff_section_data (abfd
, lsec
)->contents
;
265 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
267 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
270 /* Get the dynamic symbols. */
273 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
277 struct internal_ldhdr ldhdr
;
279 bfd_byte
*elsym
, *elsymend
;
280 coff_symbol_type
*symbuf
;
282 if ((abfd
->flags
& DYNAMIC
) == 0)
284 bfd_set_error (bfd_error_invalid_operation
);
288 lsec
= bfd_get_section_by_name (abfd
, ".loader");
291 bfd_set_error (bfd_error_no_symbols
);
295 if (! xcoff_get_section_contents (abfd
, lsec
))
297 contents
= coff_section_data (abfd
, lsec
)->contents
;
299 coff_section_data (abfd
, lsec
)->keep_contents
= true;
301 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
303 strings
= (char *) contents
+ ldhdr
.l_stoff
;
305 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
309 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
311 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
312 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
314 struct internal_ldsym ldsym
;
316 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
318 symbuf
->symbol
.the_bfd
= abfd
;
320 if (ldsym
._l
._l_l
._l_zeroes
== 0)
321 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
326 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
329 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
331 symbuf
->symbol
.name
= c
;
334 if (ldsym
.l_smclas
== XMC_XO
)
335 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
337 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
339 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
341 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
342 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
344 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
345 symbuf
->symbol
.flags
|= BSF_WEAK
;
347 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
350 /* FIXME: We have no way to record the other information stored
351 with the loader symbol. */
352 *psyms
= (asymbol
*) symbuf
;
357 return ldhdr
.l_nsyms
;
360 /* Get the size required to hold the dynamic relocs. */
363 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
367 struct internal_ldhdr ldhdr
;
369 if ((abfd
->flags
& DYNAMIC
) == 0)
371 bfd_set_error (bfd_error_invalid_operation
);
375 lsec
= bfd_get_section_by_name (abfd
, ".loader");
378 bfd_set_error (bfd_error_no_symbols
);
382 if (! xcoff_get_section_contents (abfd
, lsec
))
384 contents
= coff_section_data (abfd
, lsec
)->contents
;
386 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
388 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
391 /* Get the dynamic relocs. */
394 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
400 struct internal_ldhdr ldhdr
;
402 bfd_byte
*elrel
, *elrelend
;
404 if ((abfd
->flags
& DYNAMIC
) == 0)
406 bfd_set_error (bfd_error_invalid_operation
);
410 lsec
= bfd_get_section_by_name (abfd
, ".loader");
413 bfd_set_error (bfd_error_no_symbols
);
417 if (! xcoff_get_section_contents (abfd
, lsec
))
419 contents
= coff_section_data (abfd
, lsec
)->contents
;
421 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
423 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
427 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
429 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
430 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
433 struct internal_ldrel ldrel
;
435 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
437 if (ldrel
.l_symndx
>= 3)
438 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
444 switch (ldrel
.l_symndx
)
460 sec
= bfd_get_section_by_name (abfd
, name
);
463 bfd_set_error (bfd_error_bad_value
);
467 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
470 relbuf
->address
= ldrel
.l_vaddr
;
473 /* Most dynamic relocs have the same type. FIXME: This is only
474 correct if ldrel.l_rtype == 0. In other cases, we should use
475 a different howto. */
476 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
478 /* FIXME: We have no way to record the l_rsecnm field. */
485 return ldhdr
.l_nreloc
;
488 /* Hash functions for xcoff_link_hash_table's archive_info. */
491 xcoff_archive_info_hash (const void *data
)
493 const struct xcoff_archive_info
*info
;
495 info
= (const struct xcoff_archive_info
*) data
;
496 return htab_hash_pointer (info
->archive
);
500 xcoff_archive_info_eq (const void *data1
, const void *data2
)
502 const struct xcoff_archive_info
*info1
;
503 const struct xcoff_archive_info
*info2
;
505 info1
= (const struct xcoff_archive_info
*) data1
;
506 info2
= (const struct xcoff_archive_info
*) data2
;
507 return info1
->archive
== info2
->archive
;
510 /* Return information about archive ARCHIVE. Return NULL on error. */
512 static struct xcoff_archive_info
*
513 xcoff_get_archive_info (struct bfd_link_info
*info
, bfd
*archive
)
515 struct xcoff_link_hash_table
*htab
;
516 struct xcoff_archive_info
*entryp
, entry
;
519 htab
= xcoff_hash_table (info
);
520 entry
.archive
= archive
;
521 slot
= htab_find_slot (htab
->archive_info
, &entry
, INSERT
);
528 entryp
= bfd_zalloc (info
->output_bfd
, sizeof (entry
));
532 entryp
->archive
= archive
;
538 /* Routine to create an entry in an XCOFF link hash table. */
540 static struct bfd_hash_entry
*
541 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
542 struct bfd_hash_table
*table
,
545 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
547 /* Allocate the structure if it has not already been allocated by a
550 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
554 /* Call the allocation method of the superclass. */
555 ret
= ((struct xcoff_link_hash_entry
*)
556 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
560 /* Set local fields. */
562 ret
->toc_section
= NULL
;
563 ret
->u
.toc_indx
= -1;
564 ret
->descriptor
= NULL
;
568 ret
->smclas
= XMC_UA
;
571 return (struct bfd_hash_entry
*) ret
;
574 /* Destroy an XCOFF link hash table. */
577 _bfd_xcoff_bfd_link_hash_table_free (bfd
*obfd
)
579 struct xcoff_link_hash_table
*ret
;
581 ret
= (struct xcoff_link_hash_table
*) obfd
->link
.hash
;
582 if (ret
->archive_info
)
583 htab_delete (ret
->archive_info
);
584 if (ret
->debug_strtab
)
585 _bfd_stringtab_free (ret
->debug_strtab
);
586 _bfd_generic_link_hash_table_free (obfd
);
589 /* Create an XCOFF link hash table. */
591 struct bfd_link_hash_table
*
592 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
594 struct xcoff_link_hash_table
*ret
;
595 bool isxcoff64
= false;
596 size_t amt
= sizeof (* ret
);
598 ret
= bfd_zmalloc (amt
);
601 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
602 sizeof (struct xcoff_link_hash_entry
)))
608 isxcoff64
= bfd_coff_debug_string_prefix_length (abfd
) == 4;
610 ret
->debug_strtab
= _bfd_xcoff_stringtab_init (isxcoff64
);
611 ret
->archive_info
= htab_create (37, xcoff_archive_info_hash
,
612 xcoff_archive_info_eq
, NULL
);
613 if (!ret
->debug_strtab
|| !ret
->archive_info
)
615 _bfd_xcoff_bfd_link_hash_table_free (abfd
);
618 ret
->root
.hash_table_free
= _bfd_xcoff_bfd_link_hash_table_free
;
620 /* The linker will always generate a full a.out header. We need to
621 record that fact now, before the sizeof_headers routine could be
623 xcoff_data (abfd
)->full_aouthdr
= true;
628 /* Read internal relocs for an XCOFF csect. This is a wrapper around
629 _bfd_coff_read_internal_relocs which tries to take advantage of any
630 relocs which may have been cached for the enclosing section. */
632 static struct internal_reloc
*
633 xcoff_read_internal_relocs (bfd
*abfd
,
636 bfd_byte
*external_relocs
,
637 bool require_internal
,
638 struct internal_reloc
*internal_relocs
)
640 if (coff_section_data (abfd
, sec
) != NULL
641 && coff_section_data (abfd
, sec
)->relocs
== NULL
642 && xcoff_section_data (abfd
, sec
) != NULL
)
646 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
648 if (enclosing
!= NULL
649 && (coff_section_data (abfd
, enclosing
) == NULL
650 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
652 && enclosing
->reloc_count
> 0)
654 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
655 external_relocs
, false, NULL
)
660 if (enclosing
!= NULL
661 && coff_section_data (abfd
, enclosing
) != NULL
662 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
666 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
667 / bfd_coff_relsz (abfd
));
669 if (! require_internal
)
670 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
671 memcpy (internal_relocs
,
672 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
673 sec
->reloc_count
* sizeof (struct internal_reloc
));
674 return internal_relocs
;
678 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
679 require_internal
, internal_relocs
);
682 /* Split FILENAME into an import path and an import filename,
683 storing them in *IMPPATH and *IMPFILE respectively. */
686 bfd_xcoff_split_import_path (bfd
*abfd
, const char *filename
,
687 const char **imppath
, const char **impfile
)
693 base
= lbasename (filename
);
694 length
= base
- filename
;
696 /* The filename has no directory component, so use an empty path. */
698 else if (length
== 1)
699 /* The filename is in the root directory. */
703 /* Extract the (non-empty) directory part. Note that we don't
704 need to strip duplicate directory separators from any part
705 of the string; the native linker doesn't do that either. */
706 path
= bfd_alloc (abfd
, length
);
709 memcpy (path
, filename
, length
- 1);
710 path
[length
- 1] = 0;
717 /* Set ARCHIVE's import path as though its filename had been given
721 bfd_xcoff_set_archive_import_path (struct bfd_link_info
*info
,
722 bfd
*archive
, const char *filename
)
724 struct xcoff_archive_info
*archive_info
;
726 archive_info
= xcoff_get_archive_info (info
, archive
);
727 return (archive_info
!= NULL
728 && bfd_xcoff_split_import_path (archive
, filename
,
729 &archive_info
->imppath
,
730 &archive_info
->impfile
));
733 /* H is an imported symbol. Set the import module's path, file and member
734 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
735 no specific import module is specified. */
738 xcoff_set_import_path (struct bfd_link_info
*info
,
739 struct xcoff_link_hash_entry
*h
,
740 const char *imppath
, const char *impfile
,
741 const char *impmember
)
744 struct xcoff_import_file
**pp
;
746 /* We overload the ldindx field to hold the l_ifile value for this
748 BFD_ASSERT (h
->ldsym
== NULL
);
749 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
754 /* We start c at 1 because the first entry in the import list is
755 reserved for the library search path. */
756 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
758 pp
= &(*pp
)->next
, ++c
)
760 if (filename_cmp ((*pp
)->path
, imppath
) == 0
761 && filename_cmp ((*pp
)->file
, impfile
) == 0
762 && filename_cmp ((*pp
)->member
, impmember
) == 0)
768 struct xcoff_import_file
*n
;
769 size_t amt
= sizeof (*n
);
771 n
= bfd_alloc (info
->output_bfd
, amt
);
777 n
->member
= impmember
;
785 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
786 Return true if LDSYM defines H. */
789 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry
*h
,
790 struct internal_ldsym
*ldsym
)
792 /* If we didn't know about H before processing LDSYM, LDSYM
793 definitely defines H. */
794 if (h
->root
.type
== bfd_link_hash_new
)
797 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
798 dynamic symbol, LDSYM trumps the current definition of H. */
799 if ((ldsym
->l_smtype
& L_WEAK
) == 0
800 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
801 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
802 && (h
->root
.type
== bfd_link_hash_defweak
803 || h
->root
.type
== bfd_link_hash_undefweak
))
806 /* If H is currently undefined, LDSYM defines it. */
807 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
808 && (h
->root
.type
== bfd_link_hash_undefined
809 || h
->root
.type
== bfd_link_hash_undefweak
))
815 /* This function is used to add symbols from a dynamic object to the
816 global symbol table. */
819 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
823 struct internal_ldhdr ldhdr
;
825 bfd_byte
*elsym
, *elsymend
;
826 struct xcoff_import_file
*n
;
828 struct xcoff_import_file
**pp
;
830 /* We can only handle a dynamic object if we are generating an XCOFF
832 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
835 (_("%pB: XCOFF shared object when not producing XCOFF output"),
837 bfd_set_error (bfd_error_invalid_operation
);
841 /* The symbols we use from a dynamic object are not the symbols in
842 the normal symbol table, but, rather, the symbols in the export
843 table. If there is a global symbol in a dynamic object which is
844 not in the export table, the loader will not be able to find it,
845 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
846 libc.a has symbols in the export table which are not in the
849 /* Read in the .loader section. FIXME: We should really use the
850 o_snloader field in the a.out header, rather than grabbing the
852 lsec
= bfd_get_section_by_name (abfd
, ".loader");
856 (_("%pB: dynamic object with no .loader section"),
858 bfd_set_error (bfd_error_no_symbols
);
862 if (! xcoff_get_section_contents (abfd
, lsec
))
864 contents
= coff_section_data (abfd
, lsec
)->contents
;
866 /* Remove the sections from this object, so that they do not get
867 included in the link. */
868 bfd_section_list_clear (abfd
);
870 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
872 strings
= (char *) contents
+ ldhdr
.l_stoff
;
874 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
876 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
878 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
880 struct internal_ldsym ldsym
;
881 char nambuf
[SYMNMLEN
+ 1];
883 struct xcoff_link_hash_entry
*h
;
885 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
887 /* We are only interested in exported symbols. */
888 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
891 if (ldsym
._l
._l_l
._l_zeroes
== 0)
892 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
895 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
896 nambuf
[SYMNMLEN
] = '\0';
900 /* Normally we could not call xcoff_link_hash_lookup in an add
901 symbols routine, since we might not be using an XCOFF hash
902 table. However, we verified above that we are using an XCOFF
905 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true,
910 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
913 h
->flags
|= XCOFF_DEF_DYNAMIC
;
914 h
->smclas
= ldsym
.l_smclas
;
915 if (h
->smclas
== XMC_XO
)
917 /* This symbol has an absolute value. */
918 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
919 h
->root
.type
= bfd_link_hash_defweak
;
921 h
->root
.type
= bfd_link_hash_defined
;
922 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
923 h
->root
.u
.def
.value
= ldsym
.l_value
;
927 /* Otherwise, we don't bother to actually define the symbol,
928 since we don't have a section to put it in anyhow.
929 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
930 should be imported from the symbol's undef.abfd. */
931 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
932 h
->root
.type
= bfd_link_hash_undefweak
;
934 h
->root
.type
= bfd_link_hash_undefined
;
935 h
->root
.u
.undef
.abfd
= abfd
;
938 /* If this symbol defines a function descriptor, then it
939 implicitly defines the function code as well. */
940 if (h
->smclas
== XMC_DS
941 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
942 h
->flags
|= XCOFF_DESCRIPTOR
;
943 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
945 struct xcoff_link_hash_entry
*hds
;
952 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
956 strcpy (dsnm
+ 1, name
);
957 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
967 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
969 hds
->root
.type
= h
->root
.type
;
970 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
971 if (h
->smclas
== XMC_XO
)
973 /* An absolute symbol appears to actually define code, not a
974 function descriptor. This is how some math functions are
975 implemented on AIX 4.1. */
976 hds
->smclas
= XMC_XO
;
977 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
978 hds
->root
.u
.def
.value
= ldsym
.l_value
;
982 hds
->smclas
= XMC_PR
;
983 hds
->root
.u
.undef
.abfd
= abfd
;
984 /* We do not want to add this to the undefined
991 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
993 free (coff_section_data (abfd
, lsec
)->contents
);
994 coff_section_data (abfd
, lsec
)->contents
= NULL
;
997 /* Record this file in the import files. */
998 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
1003 if (abfd
->my_archive
== NULL
|| bfd_is_thin_archive (abfd
->my_archive
))
1005 if (!bfd_xcoff_split_import_path (abfd
, bfd_get_filename (abfd
),
1006 &n
->path
, &n
->file
))
1012 struct xcoff_archive_info
*archive_info
;
1014 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1015 if (!archive_info
->impfile
)
1017 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1018 bfd_get_filename (archive_info
1020 &archive_info
->imppath
,
1021 &archive_info
->impfile
))
1024 n
->path
= archive_info
->imppath
;
1025 n
->file
= archive_info
->impfile
;
1026 n
->member
= bfd_get_filename (abfd
);
1029 /* We start c at 1 because the first import file number is reserved
1031 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1033 pp
= &(*pp
)->next
, ++c
)
1037 xcoff_data (abfd
)->import_file_id
= c
;
1042 /* xcoff_link_create_extra_sections
1044 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1047 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1049 bool return_value
= false;
1051 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1053 /* We need to build a .loader section, so we do it here. This
1054 won't work if we're producing an XCOFF output file with no
1055 XCOFF input files. FIXME. */
1057 if (!bfd_link_relocatable (info
)
1058 && xcoff_hash_table (info
)->loader_section
== NULL
)
1061 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1063 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1067 xcoff_hash_table (info
)->loader_section
= lsec
;
1070 /* Likewise for the linkage section. */
1071 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1074 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1077 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1081 xcoff_hash_table (info
)->linkage_section
= lsec
;
1082 lsec
->alignment_power
= 2;
1085 /* Likewise for the TOC section. */
1086 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1089 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1092 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1096 xcoff_hash_table (info
)->toc_section
= tsec
;
1097 tsec
->alignment_power
= 2;
1100 /* Likewise for the descriptor section. */
1101 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1104 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1107 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1111 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1112 dsec
->alignment_power
= 2;
1115 /* Likewise for the .debug section. */
1116 if (xcoff_hash_table (info
)->debug_section
== NULL
1117 && info
->strip
!= strip_all
)
1120 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1122 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1126 xcoff_hash_table (info
)->debug_section
= dsec
;
1130 return_value
= true;
1134 return return_value
;
1137 /* Returns the index of reloc in RELOCS with the least address greater
1138 than or equal to ADDRESS. The relocs are sorted by address. */
1140 static bfd_size_type
1141 xcoff_find_reloc (struct internal_reloc
*relocs
,
1142 bfd_size_type count
,
1145 bfd_size_type min
, max
, this;
1149 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1158 /* Do a binary search over (min,max]. */
1159 while (min
+ 1 < max
)
1163 this = (max
+ min
) / 2;
1164 raddr
= relocs
[this].r_vaddr
;
1165 if (raddr
> address
)
1167 else if (raddr
< address
)
1176 if (relocs
[min
].r_vaddr
< address
)
1180 && relocs
[min
- 1].r_vaddr
== address
)
1186 /* Add all the symbols from an object file to the hash table.
1188 XCOFF is a weird format. A normal XCOFF .o files will have three
1189 COFF sections--.text, .data, and .bss--but each COFF section will
1190 contain many csects. These csects are described in the symbol
1191 table. From the linker's point of view, each csect must be
1192 considered a section in its own right. For example, a TOC entry is
1193 handled as a small XMC_TC csect. The linker must be able to merge
1194 different TOC entries together, which means that it must be able to
1195 extract the XMC_TC csects from the .data section of the input .o
1198 From the point of view of our linker, this is, of course, a hideous
1199 nightmare. We cope by actually creating sections for each csect,
1200 and discarding the original sections. We then have to handle the
1201 relocation entries carefully, since the only way to tell which
1202 csect they belong to is to examine the address. */
1205 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1207 unsigned int n_tmask
;
1208 unsigned int n_btshft
;
1210 bfd_size_type symcount
;
1211 struct xcoff_link_hash_entry
**sym_hash
;
1212 asection
**csect_cache
;
1213 unsigned int *lineno_counts
;
1214 bfd_size_type linesz
;
1216 asection
*last_real
;
1219 unsigned int csect_index
;
1220 asection
*first_csect
;
1221 bfd_size_type symesz
;
1224 struct reloc_info_struct
1226 struct internal_reloc
*relocs
;
1229 } *reloc_info
= NULL
;
1232 keep_syms
= obj_coff_keep_syms (abfd
);
1234 if ((abfd
->flags
& DYNAMIC
) != 0
1235 && ! info
->static_link
)
1237 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1241 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1242 if (! xcoff_link_create_extra_sections (abfd
, info
))
1245 if ((abfd
->flags
& DYNAMIC
) != 0
1246 && ! info
->static_link
)
1249 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1250 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1252 /* Define macros so that ISFCN, et. al., macros work correctly. */
1253 #define N_TMASK n_tmask
1254 #define N_BTSHFT n_btshft
1256 if (info
->keep_memory
)
1257 default_copy
= false;
1259 default_copy
= true;
1261 symcount
= obj_raw_syment_count (abfd
);
1263 /* We keep a list of the linker hash table entries that correspond
1264 to each external symbol. */
1265 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1266 sym_hash
= bfd_zalloc (abfd
, amt
);
1267 if (sym_hash
== NULL
&& symcount
!= 0)
1269 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1271 /* Because of the weird stuff we are doing with XCOFF csects, we can
1272 not easily determine which section a symbol is in, so we store
1273 the information in the tdata for the input file. */
1274 amt
= symcount
* sizeof (asection
*);
1275 csect_cache
= bfd_zalloc (abfd
, amt
);
1276 if (csect_cache
== NULL
&& symcount
!= 0)
1278 xcoff_data (abfd
)->csects
= csect_cache
;
1280 /* We garbage-collect line-number information on a symbol-by-symbol
1281 basis, so we need to have quick access to the number of entries
1283 amt
= symcount
* sizeof (unsigned int);
1284 lineno_counts
= bfd_zalloc (abfd
, amt
);
1285 if (lineno_counts
== NULL
&& symcount
!= 0)
1287 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1289 /* While splitting sections into csects, we need to assign the
1290 relocs correctly. The relocs and the csects must both be in
1291 order by VMA within a given section, so we handle this by
1292 scanning along the relocs as we process the csects. We index
1293 into reloc_info using the section target_index. */
1294 amt
= abfd
->section_count
+ 1;
1295 amt
*= sizeof (struct reloc_info_struct
);
1296 reloc_info
= bfd_zmalloc (amt
);
1297 if (reloc_info
== NULL
)
1300 /* Read in the relocs and line numbers for each section. */
1301 linesz
= bfd_coff_linesz (abfd
);
1303 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1307 if ((o
->flags
& SEC_RELOC
) != 0)
1309 reloc_info
[o
->target_index
].relocs
=
1310 xcoff_read_internal_relocs (abfd
, o
, true, NULL
, false, NULL
);
1311 amt
= o
->reloc_count
;
1312 amt
*= sizeof (asection
*);
1313 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1314 if (reloc_info
[o
->target_index
].csects
== NULL
)
1318 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1319 && o
->lineno_count
> 0)
1323 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0)
1325 if (_bfd_mul_overflow (linesz
, o
->lineno_count
, &amt
))
1327 bfd_set_error (bfd_error_file_too_big
);
1330 linenos
= _bfd_malloc_and_read (abfd
, amt
, amt
);
1331 if (linenos
== NULL
)
1333 reloc_info
[o
->target_index
].linenos
= linenos
;
1337 /* Don't let the linker relocation routines discard the symbols. */
1338 obj_coff_keep_syms (abfd
) = true;
1344 symesz
= bfd_coff_symesz (abfd
);
1345 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1346 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1347 esym_end
= esym
+ symcount
* symesz
;
1349 while (esym
< esym_end
)
1351 struct internal_syment sym
;
1352 union internal_auxent aux
;
1354 char buf
[SYMNMLEN
+ 1];
1358 struct xcoff_link_hash_entry
*set_toc
;
1360 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1362 /* In this pass we are only interested in symbols with csect
1364 if (!CSECT_SYM_P (sym
.n_sclass
))
1367 Normally csect is a .pr, .rw etc. created in the loop
1368 If C_FILE or first time, handle special
1370 Advance esym, sym_hash, csect_hash ptrs. */
1371 if (sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1374 *csect_cache
= csect
;
1375 else if (first_csect
== NULL
1376 || sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1377 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1379 *csect_cache
= NULL
;
1380 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1381 sym_hash
+= sym
.n_numaux
+ 1;
1382 csect_cache
+= sym
.n_numaux
+ 1;
1383 lineno_counts
+= sym
.n_numaux
+ 1;
1388 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1393 /* If this symbol has line number information attached to it,
1394 and we're not stripping it, count the number of entries and
1395 add them to the count for this csect. In the final link pass
1396 we are going to attach line number information by symbol,
1397 rather than by section, in order to more easily handle
1398 garbage collection. */
1399 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1402 && ISFCN (sym
.n_type
))
1404 union internal_auxent auxlin
;
1406 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1407 sym
.n_type
, sym
.n_sclass
,
1408 0, sym
.n_numaux
, (void *) &auxlin
);
1410 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1412 asection
*enclosing
;
1413 bfd_signed_vma linoff
;
1415 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1416 if (enclosing
== NULL
)
1419 /* xgettext:c-format */
1420 (_("%pB: `%s' has line numbers but no enclosing section"),
1422 bfd_set_error (bfd_error_bad_value
);
1425 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1426 - enclosing
->line_filepos
);
1427 /* Explicit cast to bfd_signed_vma for compiler. */
1428 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1430 struct internal_lineno lin
;
1431 bfd_byte
*linpstart
;
1433 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1435 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1437 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1439 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1442 bfd_byte
*linpend
, *linp
;
1444 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1445 + enclosing
->lineno_count
* linesz
);
1446 for (linp
= linpstart
+ linesz
;
1450 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1452 if (lin
.l_lnno
== 0)
1455 *lineno_counts
= (linp
- linpstart
) / linesz
;
1456 /* The setting of line_filepos will only be
1457 useful if all the line number entries for a
1458 csect are contiguous; this only matters for
1460 if (csect
->line_filepos
== 0)
1461 csect
->line_filepos
=
1462 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1468 /* Pick up the csect auxiliary information. */
1469 if (sym
.n_numaux
== 0)
1472 /* xgettext:c-format */
1473 (_("%pB: class %d symbol `%s' has no aux entries"),
1474 abfd
, sym
.n_sclass
, name
);
1475 bfd_set_error (bfd_error_bad_value
);
1479 bfd_coff_swap_aux_in (abfd
,
1480 (void *) (esym
+ symesz
* sym
.n_numaux
),
1481 sym
.n_type
, sym
.n_sclass
,
1482 sym
.n_numaux
- 1, sym
.n_numaux
,
1485 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1495 /* xgettext:c-format */
1496 (_("%pB: symbol `%s' has unrecognized csect type %d"),
1498 bfd_set_error (bfd_error_bad_value
);
1502 /* This is an external reference. */
1503 if (sym
.n_sclass
== C_HIDEXT
1504 || sym
.n_scnum
!= N_UNDEF
1505 || aux
.x_csect
.x_scnlen
.l
!= 0)
1508 /* xgettext:c-format */
1509 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1511 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1512 (int64_t) aux
.x_csect
.x_scnlen
.l
);
1513 bfd_set_error (bfd_error_bad_value
);
1517 /* An XMC_XO external reference is actually a reference to
1518 an absolute location. */
1519 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1520 section
= bfd_und_section_ptr
;
1523 section
= bfd_abs_section_ptr
;
1524 value
= sym
.n_value
;
1530 csect_index
= -(unsigned) 1;
1532 /* When we see a TOC anchor, we record the TOC value. */
1533 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1535 if (sym
.n_sclass
!= C_HIDEXT
1536 || aux
.x_csect
.x_scnlen
.l
!= 0)
1539 /* xgettext:c-format */
1540 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64
),
1541 abfd
, name
, sym
.n_sclass
, (int64_t) aux
.x_csect
.x_scnlen
.l
);
1542 bfd_set_error (bfd_error_bad_value
);
1545 xcoff_data (abfd
)->toc
= sym
.n_value
;
1548 /* We must merge TOC entries for the same symbol. We can
1549 merge two TOC entries if they are both C_HIDEXT, they
1550 both have the same name, they are both 4 or 8 bytes long, and
1551 they both have a relocation table entry for an external
1552 symbol with the same name. Unfortunately, this means
1553 that we must look through the relocations. Ick.
1555 Logic for 32 bit vs 64 bit.
1556 32 bit has a csect length of 4 for TOC
1557 64 bit has a csect length of 8 for TOC
1559 The conditions to get past the if-check are not that bad.
1560 They are what is used to create the TOC csects in the first
1562 if (aux
.x_csect
.x_smclas
== XMC_TC
1563 && sym
.n_sclass
== C_HIDEXT
1564 && info
->output_bfd
->xvec
== abfd
->xvec
1565 && ((bfd_xcoff_is_xcoff32 (abfd
)
1566 && aux
.x_csect
.x_scnlen
.l
== 4)
1567 || (bfd_xcoff_is_xcoff64 (abfd
)
1568 && aux
.x_csect
.x_scnlen
.l
== 8)))
1570 asection
*enclosing
;
1571 struct internal_reloc
*relocs
;
1572 bfd_size_type relindx
;
1573 struct internal_reloc
*rel
;
1575 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1576 if (enclosing
== NULL
)
1579 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1580 amt
= enclosing
->reloc_count
;
1581 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1582 rel
= relocs
+ relindx
;
1584 /* 32 bit R_POS r_size is 31
1585 64 bit R_POS r_size is 63 */
1586 if (relindx
< enclosing
->reloc_count
1587 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1588 && rel
->r_type
== R_POS
1589 && ((bfd_xcoff_is_xcoff32 (abfd
)
1590 && rel
->r_size
== 31)
1591 || (bfd_xcoff_is_xcoff64 (abfd
)
1592 && rel
->r_size
== 63)))
1596 struct internal_syment relsym
;
1598 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1599 + rel
->r_symndx
* symesz
);
1600 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1601 if (EXTERN_SYM_P (relsym
.n_sclass
))
1603 const char *relname
;
1604 char relbuf
[SYMNMLEN
+ 1];
1606 struct xcoff_link_hash_entry
*h
;
1608 /* At this point we know that the TOC entry is
1609 for an externally visible symbol. */
1610 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1612 if (relname
== NULL
)
1615 /* We only merge TOC entries if the TC name is
1616 the same as the symbol name. This handles
1617 the normal case, but not common cases like
1618 SYM.P4 which gcc generates to store SYM + 4
1619 in the TOC. FIXME. */
1620 if (strcmp (name
, relname
) == 0)
1622 copy
= (! info
->keep_memory
1623 || relsym
._n
._n_n
._n_zeroes
!= 0
1624 || relsym
._n
._n_n
._n_offset
== 0);
1625 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1626 relname
, true, copy
,
1631 /* At this point h->root.type could be
1632 bfd_link_hash_new. That should be OK,
1633 since we know for sure that we will come
1634 across this symbol as we step through the
1637 /* We store h in *sym_hash for the
1638 convenience of the relocate_section
1642 if (h
->toc_section
!= NULL
)
1644 asection
**rel_csects
;
1646 /* We already have a TOC entry for this
1647 symbol, so we can just ignore this
1650 reloc_info
[enclosing
->target_index
].csects
;
1651 rel_csects
[relindx
] = bfd_und_section_ptr
;
1655 /* We are about to create a TOC entry for
1664 asection
*enclosing
;
1666 /* We need to create a new section. We get the name from
1667 the csect storage mapping class, so that the linker can
1668 accumulate similar csects together. */
1670 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1674 /* The enclosing section is the main section : .data, .text
1675 or .bss that the csect is coming from. */
1676 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1677 if (enclosing
== NULL
)
1680 if (! bfd_is_abs_section (enclosing
)
1681 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1682 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1683 > enclosing
->vma
+ enclosing
->size
)))
1686 /* xgettext:c-format */
1687 (_("%pB: csect `%s' not in enclosing section"),
1689 bfd_set_error (bfd_error_bad_value
);
1692 csect
->vma
= sym
.n_value
;
1693 csect
->filepos
= (enclosing
->filepos
1696 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1697 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1698 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1700 /* Record the enclosing section in the tdata for this new
1702 amt
= sizeof (struct coff_section_tdata
);
1703 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1704 if (csect
->used_by_bfd
== NULL
)
1706 amt
= sizeof (struct xcoff_section_tdata
);
1707 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1708 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1710 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1711 xcoff_section_data (abfd
, csect
)->lineno_count
=
1712 enclosing
->lineno_count
;
1714 if (enclosing
->owner
== abfd
)
1716 struct internal_reloc
*relocs
;
1717 bfd_size_type relindx
;
1718 struct internal_reloc
*rel
;
1719 asection
**rel_csect
;
1721 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1722 amt
= enclosing
->reloc_count
;
1723 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1725 rel
= relocs
+ relindx
;
1726 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1729 csect
->rel_filepos
= (enclosing
->rel_filepos
1730 + relindx
* bfd_coff_relsz (abfd
));
1731 while (relindx
< enclosing
->reloc_count
1732 && *rel_csect
== NULL
1733 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1737 csect
->flags
|= SEC_RELOC
;
1738 ++csect
->reloc_count
;
1745 /* There are a number of other fields and section flags
1746 which we do not bother to set. */
1748 csect_index
= ((esym
1749 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1752 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1754 if (first_csect
== NULL
)
1755 first_csect
= csect
;
1757 /* If this symbol is external, we treat it as starting at the
1758 beginning of the newly created section. */
1759 if (EXTERN_SYM_P (sym
.n_sclass
))
1765 /* If this is a TOC section for a symbol, record it. */
1766 if (set_toc
!= NULL
)
1767 set_toc
->toc_section
= csect
;
1772 /* This is a label definition. The x_scnlen field is the
1773 symbol index of the csect. Usually the XTY_LD symbol will
1774 follow its appropriate XTY_SD symbol. The .set pseudo op can
1775 cause the XTY_LD to not follow the XTY_SD symbol. */
1780 if (aux
.x_csect
.x_scnlen
.l
< 0
1781 || (aux
.x_csect
.x_scnlen
.l
1782 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1786 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1788 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1794 /* xgettext:c-format */
1795 (_("%pB: misplaced XTY_LD `%s'"),
1797 bfd_set_error (bfd_error_bad_value
);
1801 value
= sym
.n_value
- csect
->vma
;
1806 /* This is an unitialized csect. We could base the name on
1807 the storage mapping class, but we don't bother except for
1808 an XMC_TD symbol. If this csect is externally visible,
1809 it is a common symbol. We put XMC_TD symbols in sections
1810 named .tocbss, and rely on the linker script to put that
1813 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1815 /* The linker script puts the .td section in the data
1816 section after the .tc section. */
1817 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1820 else if (aux
.x_csect
.x_smclas
== XMC_UL
)
1822 /* This is a thread-local unitialized csect. */
1823 csect
= bfd_make_section_anyway_with_flags (abfd
, ".tbss",
1824 SEC_ALLOC
| SEC_THREAD_LOCAL
);
1827 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1832 csect
->vma
= sym
.n_value
;
1833 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1834 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1835 /* There are a number of other fields and section flags
1836 which we do not bother to set. */
1838 csect_index
= ((esym
1839 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1842 amt
= sizeof (struct coff_section_tdata
);
1843 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1844 if (csect
->used_by_bfd
== NULL
)
1846 amt
= sizeof (struct xcoff_section_tdata
);
1847 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1848 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1850 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1852 if (first_csect
== NULL
)
1853 first_csect
= csect
;
1855 if (EXTERN_SYM_P (sym
.n_sclass
))
1857 csect
->flags
|= SEC_IS_COMMON
;
1860 value
= aux
.x_csect
.x_scnlen
.l
;
1866 /* Check for magic symbol names. */
1867 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1868 && aux
.x_csect
.x_smclas
!= XMC_TC
1869 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1875 if (strcmp (name
, "_text") == 0)
1876 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1877 else if (strcmp (name
, "_etext") == 0)
1878 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1879 else if (strcmp (name
, "_data") == 0)
1880 i
= XCOFF_SPECIAL_SECTION_DATA
;
1881 else if (strcmp (name
, "_edata") == 0)
1882 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1883 else if (strcmp (name
, "_end") == 0)
1884 i
= XCOFF_SPECIAL_SECTION_END
;
1886 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1887 i
= XCOFF_SPECIAL_SECTION_END2
;
1890 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1893 /* Now we have enough information to add the symbol to the
1894 linker hash table. */
1896 if (EXTERN_SYM_P (sym
.n_sclass
))
1901 BFD_ASSERT (section
!= NULL
);
1903 /* We must copy the name into memory if we got it from the
1904 syment itself, rather than the string table. */
1905 copy
= default_copy
;
1906 if (sym
._n
._n_n
._n_zeroes
!= 0
1907 || sym
._n
._n_n
._n_offset
== 0)
1910 /* Ignore global linkage code when linking statically. */
1911 if (info
->static_link
1912 && (smtyp
== XTY_SD
|| smtyp
== XTY_LD
)
1913 && aux
.x_csect
.x_smclas
== XMC_GL
)
1915 section
= bfd_und_section_ptr
;
1919 /* The AIX linker appears to only detect multiple symbol
1920 definitions when there is a reference to the symbol. If
1921 a symbol is defined multiple times, and the only
1922 references are from the same object file, the AIX linker
1923 appears to permit it. It does not merge the different
1924 definitions, but handles them independently. On the
1925 other hand, if there is a reference, the linker reports
1928 This matters because the AIX <net/net_globals.h> header
1929 file actually defines an initialized array, so we have to
1930 actually permit that to work.
1932 Just to make matters even more confusing, the AIX linker
1933 appears to permit multiple symbol definitions whenever
1934 the second definition is in an archive rather than an
1935 object file. This may be a consequence of the manner in
1936 which it handles archives: I think it may load the entire
1937 archive in as separate csects, and then let garbage
1938 collection discard symbols.
1940 We also have to handle the case of statically linking a
1941 shared object, which will cause symbol redefinitions,
1942 although this is an easier case to detect. */
1943 else if (info
->output_bfd
->xvec
== abfd
->xvec
)
1945 if (! bfd_is_und_section (section
))
1946 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1947 name
, true, copy
, false);
1949 /* Make a copy of the symbol name to prevent problems with
1951 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1952 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1953 true, true, false));
1955 if (*sym_hash
== NULL
)
1957 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1958 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1959 && ! bfd_is_und_section (section
)
1960 && ! bfd_is_com_section (section
))
1962 /* This is a second definition of a defined symbol. */
1963 if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
1964 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
1966 /* The existing symbol is from a shared library.
1968 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1969 (*sym_hash
)->root
.u
.undef
.abfd
=
1970 (*sym_hash
)->root
.u
.def
.section
->owner
;
1972 else if (abfd
->my_archive
!= NULL
)
1974 /* This is a redefinition in an object contained
1975 in an archive. Just ignore it. See the
1977 section
= bfd_und_section_ptr
;
1980 else if (sym
.n_sclass
== C_AIX_WEAKEXT
1981 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1983 /* At least one of the definitions is weak.
1984 Allow the normal rules to take effect. */
1986 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1987 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1989 /* This symbol has been referenced. In this
1990 case, we just continue and permit the
1991 multiple definition error. See the comment
1992 above about the behaviour of the AIX linker. */
1994 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1996 /* The symbols are both csects of the same
1997 class. There is at least a chance that this
1998 is a semi-legitimate redefinition. */
1999 section
= bfd_und_section_ptr
;
2001 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
2004 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
2005 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
2006 && (bfd_is_und_section (section
)
2007 || bfd_is_com_section (section
)))
2009 /* This is a reference to a multiply defined symbol.
2010 Report the error now. See the comment above
2011 about the behaviour of the AIX linker. We could
2012 also do this with warning symbols, but I'm not
2013 sure the XCOFF linker is wholly prepared to
2014 handle them, and that would only be a warning,
2016 (*info
->callbacks
->multiple_definition
) (info
,
2020 /* Try not to give this error too many times. */
2021 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2025 /* _bfd_generic_link_add_one_symbol may call the linker to
2026 generate an error message, and the linker may try to read
2027 the symbol table to give a good error. Right now, the
2028 line numbers are in an inconsistent state, since they are
2029 counted both in the real sections and in the new csects.
2030 We need to leave the count in the real sections so that
2031 the linker can report the line number of the error
2032 correctly, so temporarily clobber the link to the csects
2033 so that the linker will not try to read the line numbers
2034 a second time from the csects. */
2035 BFD_ASSERT (last_real
->next
== first_csect
);
2036 last_real
->next
= NULL
;
2037 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2038 ok
= (_bfd_generic_link_add_one_symbol
2039 (info
, abfd
, name
, flags
, section
, value
, NULL
, copy
, true,
2040 (struct bfd_link_hash_entry
**) sym_hash
));
2041 last_real
->next
= first_csect
;
2045 if (smtyp
== XTY_CM
)
2047 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2048 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2049 /* We don't need the common csect we just created. */
2052 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2053 = csect
->alignment_power
;
2056 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2062 || section
== bfd_und_section_ptr
)
2063 flag
= XCOFF_REF_REGULAR
;
2065 flag
= XCOFF_DEF_REGULAR
;
2066 (*sym_hash
)->flags
|= flag
;
2068 if ((*sym_hash
)->smclas
== XMC_UA
2069 || flag
== XCOFF_DEF_REGULAR
)
2070 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2074 if (smtyp
== XTY_ER
)
2075 *csect_cache
= section
;
2078 *csect_cache
= csect
;
2080 xcoff_section_data (abfd
, csect
)->last_symndx
2081 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2084 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2085 sym_hash
+= sym
.n_numaux
+ 1;
2086 csect_cache
+= sym
.n_numaux
+ 1;
2087 lineno_counts
+= sym
.n_numaux
+ 1;
2090 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2092 /* Make sure that we have seen all the relocs. */
2093 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2095 /* Debugging sections have no csects. */
2096 if (bfd_section_flags (o
) & SEC_DEBUGGING
)
2099 /* Reset the section size and the line number count, since the
2100 data is now attached to the csects. Don't reset the size of
2101 the .debug section, since we need to read it below in
2102 bfd_xcoff_size_dynamic_sections. */
2103 if (strcmp (bfd_section_name (o
), ".debug") != 0)
2105 o
->lineno_count
= 0;
2107 if ((o
->flags
& SEC_RELOC
) != 0)
2110 struct internal_reloc
*rel
;
2111 asection
**rel_csect
;
2113 rel
= reloc_info
[o
->target_index
].relocs
;
2114 rel_csect
= reloc_info
[o
->target_index
].csects
;
2116 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2118 if (*rel_csect
== NULL
)
2121 /* xgettext:c-format */
2122 (_("%pB: reloc %s:%" PRId64
" not in csect"),
2123 abfd
, o
->name
, (int64_t) i
);
2124 bfd_set_error (bfd_error_bad_value
);
2128 /* We identify all function symbols that are the target
2129 of a relocation, so that we can create glue code for
2130 functions imported from dynamic objects. */
2131 if (info
->output_bfd
->xvec
== abfd
->xvec
2132 && *rel_csect
!= bfd_und_section_ptr
2133 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2135 struct xcoff_link_hash_entry
*h
;
2137 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2138 /* If the symbol name starts with a period, it is
2139 the code of a function. If the symbol is
2140 currently undefined, then add an undefined symbol
2141 for the function descriptor. This should do no
2142 harm, because any regular object that defines the
2143 function should also define the function
2144 descriptor. It helps, because it means that we
2145 will identify the function descriptor with a
2146 dynamic object if a dynamic object defines it. */
2147 if (h
->root
.root
.string
[0] == '.'
2148 && h
->descriptor
== NULL
)
2150 struct xcoff_link_hash_entry
*hds
;
2151 struct bfd_link_hash_entry
*bh
;
2153 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2154 h
->root
.root
.string
+ 1,
2158 if (hds
->root
.type
== bfd_link_hash_new
)
2161 if (! (_bfd_generic_link_add_one_symbol
2162 (info
, abfd
, hds
->root
.root
.string
,
2163 (flagword
) 0, bfd_und_section_ptr
,
2164 (bfd_vma
) 0, NULL
, false,
2167 hds
= (struct xcoff_link_hash_entry
*) bh
;
2169 hds
->flags
|= XCOFF_DESCRIPTOR
;
2170 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2171 hds
->descriptor
= h
;
2172 h
->descriptor
= hds
;
2174 if (h
->root
.root
.string
[0] == '.')
2175 h
->flags
|= XCOFF_CALLED
;
2179 free (reloc_info
[o
->target_index
].csects
);
2180 reloc_info
[o
->target_index
].csects
= NULL
;
2182 /* Reset SEC_RELOC and the reloc_count, since the reloc
2183 information is now attached to the csects. */
2184 o
->flags
&=~ SEC_RELOC
;
2187 /* If we are not keeping memory, free the reloc information. */
2188 if (! info
->keep_memory
2189 && coff_section_data (abfd
, o
) != NULL
2190 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2192 free (coff_section_data (abfd
, o
)->relocs
);
2193 coff_section_data (abfd
, o
)->relocs
= NULL
;
2197 /* Free up the line numbers. FIXME: We could cache these
2198 somewhere for the final link, to avoid reading them again. */
2199 free (reloc_info
[o
->target_index
].linenos
);
2200 reloc_info
[o
->target_index
].linenos
= NULL
;
2205 obj_coff_keep_syms (abfd
) = keep_syms
;
2210 if (reloc_info
!= NULL
)
2212 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2214 free (reloc_info
[o
->target_index
].csects
);
2215 free (reloc_info
[o
->target_index
].linenos
);
2219 obj_coff_keep_syms (abfd
) = keep_syms
;
2226 /* Add symbols from an XCOFF object file. */
2229 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2231 if (! _bfd_coff_get_external_symbols (abfd
))
2233 if (! xcoff_link_add_symbols (abfd
, info
))
2235 if (! info
->keep_memory
)
2237 if (! _bfd_coff_free_symbols (abfd
))
2243 /* Look through the loader symbols to see if this dynamic object
2244 should be included in the link. The native linker uses the loader
2245 symbols, not the normal symbol table, so we do too. */
2248 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2249 struct bfd_link_info
*info
,
2255 struct internal_ldhdr ldhdr
;
2256 const char *strings
;
2257 bfd_byte
*elsym
, *elsymend
;
2261 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2263 /* There are no symbols, so don't try to include it. */
2266 if (! xcoff_get_section_contents (abfd
, lsec
))
2268 contents
= coff_section_data (abfd
, lsec
)->contents
;
2270 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2272 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2274 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2276 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2277 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2279 struct internal_ldsym ldsym
;
2280 char nambuf
[SYMNMLEN
+ 1];
2282 struct bfd_link_hash_entry
*h
;
2284 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2286 /* We are only interested in exported symbols. */
2287 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2290 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2291 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2294 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2295 nambuf
[SYMNMLEN
] = '\0';
2299 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2301 /* We are only interested in symbols that are currently
2302 undefined. At this point we know that we are using an XCOFF
2305 && h
->type
== bfd_link_hash_undefined
2306 && (((struct xcoff_link_hash_entry
*) h
)->flags
2307 & XCOFF_DEF_DYNAMIC
) == 0)
2309 if (!(*info
->callbacks
2310 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2317 /* We do not need this shared object. */
2318 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2320 free (coff_section_data (abfd
, lsec
)->contents
);
2321 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2327 /* Look through the symbols to see if this object file should be
2328 included in the link. */
2331 xcoff_link_check_ar_symbols (bfd
*abfd
,
2332 struct bfd_link_info
*info
,
2336 bfd_size_type symesz
;
2342 if ((abfd
->flags
& DYNAMIC
) != 0
2343 && ! info
->static_link
2344 && info
->output_bfd
->xvec
== abfd
->xvec
)
2345 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
, subsbfd
);
2347 symesz
= bfd_coff_symesz (abfd
);
2348 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2349 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2350 while (esym
< esym_end
)
2352 struct internal_syment sym
;
2354 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2355 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2357 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2360 char buf
[SYMNMLEN
+ 1];
2361 struct bfd_link_hash_entry
*h
;
2363 /* This symbol is externally visible, and is defined by this
2365 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2369 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2371 /* We are only interested in symbols that are currently
2372 undefined. If a symbol is currently known to be common,
2373 XCOFF linkers do not bring in an object file which
2374 defines it. We also don't bring in symbols to satisfy
2375 undefined references in shared objects. */
2377 && h
->type
== bfd_link_hash_undefined
2378 && (info
->output_bfd
->xvec
!= abfd
->xvec
2379 || (((struct xcoff_link_hash_entry
*) h
)->flags
2380 & XCOFF_DEF_DYNAMIC
) == 0))
2382 if (!(*info
->callbacks
2383 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2391 /* We do not need this object file. */
2395 /* Check a single archive element to see if we need to include it in
2396 the link. *PNEEDED is set according to whether this element is
2397 needed in the link or not. This is called via
2398 _bfd_generic_link_add_archive_symbols. */
2401 xcoff_link_check_archive_element (bfd
*abfd
,
2402 struct bfd_link_info
*info
,
2403 struct bfd_link_hash_entry
*h ATTRIBUTE_UNUSED
,
2404 const char *name ATTRIBUTE_UNUSED
,
2410 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2411 if (!_bfd_coff_get_external_symbols (abfd
))
2415 if (!xcoff_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
2420 /* Potentially, the add_archive_element hook may have set a
2421 substitute BFD for us. */
2425 && !_bfd_coff_free_symbols (oldbfd
))
2427 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2428 if (!_bfd_coff_get_external_symbols (abfd
))
2431 if (!xcoff_link_add_symbols (abfd
, info
))
2433 if (info
->keep_memory
)
2439 if (!_bfd_coff_free_symbols (abfd
))
2446 /* Given an XCOFF BFD, add symbols to the global hash table as
2450 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2452 switch (bfd_get_format (abfd
))
2455 return xcoff_link_add_object_symbols (abfd
, info
);
2458 /* If the archive has a map, do the usual search. We then need
2459 to check the archive for dynamic objects, because they may not
2460 appear in the archive map even though they should, perhaps, be
2461 included. If the archive has no map, we just consider each object
2462 file in turn, since that apparently is what the AIX native linker
2464 if (bfd_has_map (abfd
))
2466 if (! (_bfd_generic_link_add_archive_symbols
2467 (abfd
, info
, xcoff_link_check_archive_element
)))
2474 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2475 while (member
!= NULL
)
2477 if (bfd_check_format (member
, bfd_object
)
2478 && (info
->output_bfd
->xvec
== member
->xvec
)
2479 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2483 if (! xcoff_link_check_archive_element (member
, info
,
2484 NULL
, NULL
, &needed
))
2487 member
->archive_pass
= -1;
2489 member
= bfd_openr_next_archived_file (abfd
, member
);
2496 bfd_set_error (bfd_error_wrong_format
);
2502 _bfd_xcoff_define_common_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2503 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2504 struct bfd_link_hash_entry
*harg
)
2506 struct xcoff_link_hash_entry
*h
;
2508 if (!bfd_generic_define_common_symbol (output_bfd
, info
, harg
))
2511 h
= (struct xcoff_link_hash_entry
*) harg
;
2512 h
->flags
|= XCOFF_DEF_REGULAR
;
2516 /* If symbol H has not been interpreted as a function descriptor,
2517 see whether it should be. Set up its descriptor information if so. */
2520 xcoff_find_function (struct bfd_link_info
*info
,
2521 struct xcoff_link_hash_entry
*h
)
2523 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2524 && h
->root
.root
.string
[0] != '.')
2527 struct xcoff_link_hash_entry
*hfn
;
2530 amt
= strlen (h
->root
.root
.string
) + 2;
2531 fnname
= bfd_malloc (amt
);
2535 strcpy (fnname
+ 1, h
->root
.root
.string
);
2536 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2537 fnname
, false, false, true);
2540 && hfn
->smclas
== XMC_PR
2541 && (hfn
->root
.type
== bfd_link_hash_defined
2542 || hfn
->root
.type
== bfd_link_hash_defweak
))
2544 h
->flags
|= XCOFF_DESCRIPTOR
;
2545 h
->descriptor
= hfn
;
2546 hfn
->descriptor
= h
;
2552 /* Return true if the given bfd contains at least one shared object. */
2555 xcoff_archive_contains_shared_object_p (struct bfd_link_info
*info
,
2558 struct xcoff_archive_info
*archive_info
;
2561 archive_info
= xcoff_get_archive_info (info
, archive
);
2562 if (!archive_info
->know_contains_shared_object_p
)
2564 member
= bfd_openr_next_archived_file (archive
, NULL
);
2565 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2566 member
= bfd_openr_next_archived_file (archive
, member
);
2568 archive_info
->contains_shared_object_p
= (member
!= NULL
);
2569 archive_info
->know_contains_shared_object_p
= 1;
2571 return archive_info
->contains_shared_object_p
;
2574 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2575 qualifies for export by -bexpall. */
2578 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2580 /* Exclude symbols beginning with '_'. */
2581 if (h
->root
.root
.string
[0] == '_')
2584 /* Exclude archive members that would otherwise be unreferenced. */
2585 if ((h
->flags
& XCOFF_MARK
) == 0
2586 && (h
->root
.type
== bfd_link_hash_defined
2587 || h
->root
.type
== bfd_link_hash_defweak
)
2588 && h
->root
.u
.def
.section
->owner
!= NULL
2589 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2595 /* Return true if symbol H qualifies for the forms of automatic export
2596 specified by AUTO_EXPORT_FLAGS. */
2599 xcoff_auto_export_p (struct bfd_link_info
*info
,
2600 struct xcoff_link_hash_entry
*h
,
2601 unsigned int auto_export_flags
)
2603 /* Don't automatically export things that were explicitly exported. */
2604 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2607 /* Don't export things that we don't define. */
2608 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2611 /* Don't export functions; export their descriptors instead. */
2612 if (h
->root
.root
.string
[0] == '.')
2615 /* We don't export a symbol which is being defined by an object
2616 included from an archive which contains a shared object. The
2617 rationale is that if an archive contains both an unshared and
2618 a shared object, then there must be some reason that the
2619 unshared object is unshared, and we don't want to start
2620 providing a shared version of it. In particular, this solves
2621 a bug involving the _savefNN set of functions. gcc will call
2622 those functions without providing a slot to restore the TOC,
2623 so it is essential that these functions be linked in directly
2624 and not from a shared object, which means that a shared
2625 object which also happens to link them in must not export
2626 them. This is confusing, but I haven't been able to think of
2627 a different approach. Note that the symbols can, of course,
2628 be exported explicitly. */
2629 if (h
->root
.type
== bfd_link_hash_defined
2630 || h
->root
.type
== bfd_link_hash_defweak
)
2634 owner
= h
->root
.u
.def
.section
->owner
;
2636 && owner
->my_archive
!= NULL
2637 && xcoff_archive_contains_shared_object_p (info
, owner
->my_archive
))
2641 /* Otherwise, all symbols are exported by -bexpfull. */
2642 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2645 /* Despite its name, -bexpall exports most but not all symbols. */
2646 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2647 && xcoff_covered_by_expall_p (h
))
2653 /* Return true if relocation REL needs to be copied to the .loader section.
2654 If REL is against a global symbol, H is that symbol, otherwise it
2658 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2659 struct xcoff_link_hash_entry
*h
, asection
*ssec
)
2661 if (!xcoff_hash_table (info
)->loader_section
)
2664 switch (rel
->r_type
)
2671 /* We should never need a .loader reloc for a TOC-relative reloc. */
2675 /* In this case, relocations against defined symbols can be resolved
2678 || h
->root
.type
== bfd_link_hash_defined
2679 || h
->root
.type
== bfd_link_hash_defweak
2680 || h
->root
.type
== bfd_link_hash_common
)
2683 /* We will always provide a local definition of function symbols,
2684 even if we don't have one yet. */
2685 if ((h
->flags
& XCOFF_CALLED
) != 0)
2694 /* Absolute relocations against absolute symbols can be
2695 resolved statically. */
2697 && (h
->root
.type
== bfd_link_hash_defined
2698 || h
->root
.type
== bfd_link_hash_defweak
)
2699 && !h
->root
.rel_from_abs
)
2701 asection
*sec
= h
->root
.u
.def
.section
;
2702 if (bfd_is_abs_section (sec
)
2704 && bfd_is_abs_section (sec
->output_section
)))
2708 /* Absolute relocations from read-only sections are forbidden
2709 by AIX loader. However, they can appear in their section's
2712 && (ssec
->output_section
->flags
& SEC_READONLY
) != 0)
2727 /* Mark a symbol as not being garbage, including the section in which
2731 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2733 if ((h
->flags
& XCOFF_MARK
) != 0)
2736 h
->flags
|= XCOFF_MARK
;
2738 /* If we're marking an undefined symbol, try find some way of
2740 if (!bfd_link_relocatable (info
)
2741 && (h
->flags
& XCOFF_IMPORT
) == 0
2742 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2743 && (h
->root
.type
== bfd_link_hash_undefined
2744 || h
->root
.type
== bfd_link_hash_undefweak
))
2746 /* First check whether this symbol can be interpreted as an
2747 undefined function descriptor for a defined function symbol. */
2748 if (!xcoff_find_function (info
, h
))
2751 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2752 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2753 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2755 /* This is a descriptor for a defined symbol, but the input
2756 objects have not defined the descriptor itself. Fill in
2757 the definition automatically.
2759 Note that we do this even if we found a dynamic definition
2760 of H. The local function definition logically overrides
2764 sec
= xcoff_hash_table (info
)->descriptor_section
;
2765 h
->root
.type
= bfd_link_hash_defined
;
2766 h
->root
.u
.def
.section
= sec
;
2767 h
->root
.u
.def
.value
= sec
->size
;
2769 h
->flags
|= XCOFF_DEF_REGULAR
;
2771 /* The size of the function descriptor depends on whether this
2772 is xcoff32 (12) or xcoff64 (24). */
2773 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2775 /* A function descriptor uses two relocs: one for the
2776 associated code, and one for the TOC address. */
2777 xcoff_hash_table (info
)->ldrel_count
+= 2;
2778 sec
->reloc_count
+= 2;
2780 /* Mark the function itself. */
2781 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2784 /* Mark the TOC section, so that we get an anchor
2785 to relocate against. */
2786 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2789 /* We handle writing out the contents of the descriptor in
2790 xcoff_write_global_symbol. */
2792 else if (info
->static_link
)
2793 /* We can't get a symbol value dynamically, so just assume
2794 that it's undefined. */
2795 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2796 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2798 /* This is a function symbol for which we need to create
2801 struct xcoff_link_hash_entry
*hds
;
2803 /* Mark the descriptor (and its TOC section). */
2804 hds
= h
->descriptor
;
2805 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2806 || hds
->root
.type
== bfd_link_hash_undefweak
)
2807 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2808 if (!xcoff_mark_symbol (info
, hds
))
2811 /* Treat this symbol as undefined if the descriptor was. */
2812 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2813 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2815 /* Allocate room for the global linkage code itself. */
2816 sec
= xcoff_hash_table (info
)->linkage_section
;
2817 h
->root
.type
= bfd_link_hash_defined
;
2818 h
->root
.u
.def
.section
= sec
;
2819 h
->root
.u
.def
.value
= sec
->size
;
2821 h
->flags
|= XCOFF_DEF_REGULAR
;
2822 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2824 /* The global linkage code requires a TOC entry for the
2826 if (hds
->toc_section
== NULL
)
2831 xcoff32 uses 4 bytes in the toc.
2832 xcoff64 uses 8 bytes in the toc. */
2833 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2835 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2840 /* Allocate room in the fallback TOC section. */
2841 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2842 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2843 hds
->toc_section
->size
+= byte_size
;
2844 if (!xcoff_mark (info
, hds
->toc_section
))
2847 /* Allocate room for a static and dynamic R_TOC
2849 ++xcoff_hash_table (info
)->ldrel_count
;
2850 ++hds
->toc_section
->reloc_count
;
2852 /* Set the index to -2 to force this symbol to
2855 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2858 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2860 /* Record that the symbol was undefined, then import it.
2861 -brtl links use a special fake import file. */
2862 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2863 if (xcoff_hash_table (info
)->rtld
)
2865 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2870 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2876 if (h
->root
.type
== bfd_link_hash_defined
2877 || h
->root
.type
== bfd_link_hash_defweak
)
2881 hsec
= h
->root
.u
.def
.section
;
2882 if (! bfd_is_abs_section (hsec
)
2883 && (hsec
->flags
& SEC_MARK
) == 0)
2885 if (! xcoff_mark (info
, hsec
))
2890 if (h
->toc_section
!= NULL
2891 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2893 if (! xcoff_mark (info
, h
->toc_section
))
2900 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2901 If the symbol exists, set FLAGS. */
2904 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2905 const char *name
, unsigned int flags
)
2907 struct xcoff_link_hash_entry
*h
;
2909 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2910 false, false, true);
2914 if (h
->root
.type
== bfd_link_hash_defined
2915 || h
->root
.type
== bfd_link_hash_defweak
)
2917 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2924 /* The mark phase of garbage collection. For a given section, mark
2925 it, and all the sections which define symbols to which it refers.
2926 Because this function needs to look at the relocs, we also count
2927 the number of relocs which need to be copied into the .loader
2931 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2933 if (bfd_is_const_section (sec
)
2934 || (sec
->flags
& SEC_MARK
) != 0)
2937 sec
->flags
|= SEC_MARK
;
2939 if (sec
->owner
->xvec
== info
->output_bfd
->xvec
2940 && coff_section_data (sec
->owner
, sec
) != NULL
2941 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2943 struct xcoff_link_hash_entry
**syms
;
2944 struct internal_reloc
*rel
, *relend
;
2946 unsigned long i
, first
, last
;
2948 /* Mark all the symbols in this section. */
2949 syms
= obj_xcoff_sym_hashes (sec
->owner
);
2950 csects
= xcoff_data (sec
->owner
)->csects
;
2951 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
2952 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
2953 for (i
= first
; i
<= last
; i
++)
2954 if (csects
[i
] == sec
2956 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
2958 if (!xcoff_mark_symbol (info
, syms
[i
]))
2962 /* Look through the section relocs. */
2963 if ((sec
->flags
& SEC_RELOC
) != 0
2964 && sec
->reloc_count
> 0)
2966 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
2970 relend
= rel
+ sec
->reloc_count
;
2971 for (; rel
< relend
; rel
++)
2973 struct xcoff_link_hash_entry
*h
;
2975 if ((unsigned int) rel
->r_symndx
2976 > obj_raw_syment_count (sec
->owner
))
2979 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2982 if ((h
->flags
& XCOFF_MARK
) == 0)
2984 if (!xcoff_mark_symbol (info
, h
))
2992 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2994 && (rsec
->flags
& SEC_MARK
) == 0)
2996 if (!xcoff_mark (info
, rsec
))
3001 /* See if this reloc needs to be copied into the .loader
3003 if (xcoff_need_ldrel_p (info
, rel
, h
, sec
))
3005 ++xcoff_hash_table (info
)->ldrel_count
;
3007 h
->flags
|= XCOFF_LDREL
;
3011 if (! info
->keep_memory
3012 && coff_section_data (sec
->owner
, sec
) != NULL
3013 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
3015 free (coff_section_data (sec
->owner
, sec
)->relocs
);
3016 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
3024 /* Routines that are called after all the input files have been
3025 handled, but before the sections are laid out in memory. */
3027 /* The sweep phase of garbage collection. Remove all garbage
3031 xcoff_sweep (struct bfd_link_info
*info
)
3035 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3039 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3041 if ((o
->flags
& SEC_MARK
) == 0)
3043 /* Keep all sections from non-XCOFF input files. Keep
3044 special sections. Keep .debug sections for the
3046 if (sub
->xvec
!= info
->output_bfd
->xvec
3047 || o
== xcoff_hash_table (info
)->debug_section
3048 || o
== xcoff_hash_table (info
)->loader_section
3049 || o
== xcoff_hash_table (info
)->linkage_section
3050 || o
== xcoff_hash_table (info
)->descriptor_section
3051 || (bfd_section_flags (o
) & SEC_DEBUGGING
)
3052 || strcmp (o
->name
, ".debug") == 0)
3053 o
->flags
|= SEC_MARK
;
3064 /* Record the number of elements in a set. This is used to output the
3065 correct csect length. */
3068 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3069 struct bfd_link_info
*info
,
3070 struct bfd_link_hash_entry
*harg
,
3073 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3074 struct xcoff_link_size_list
*n
;
3077 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3080 /* This will hardly ever be called. I don't want to burn four bytes
3081 per global symbol, so instead the size is kept on a linked list
3082 attached to the hash table. */
3084 n
= bfd_alloc (output_bfd
, amt
);
3087 n
->next
= xcoff_hash_table (info
)->size_list
;
3090 xcoff_hash_table (info
)->size_list
= n
;
3092 h
->flags
|= XCOFF_HAS_SIZE
;
3097 /* Import a symbol. */
3100 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3101 struct bfd_link_info
*info
,
3102 struct bfd_link_hash_entry
*harg
,
3104 const char *imppath
,
3105 const char *impfile
,
3106 const char *impmember
,
3107 unsigned int syscall_flag
)
3109 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3111 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3114 /* A symbol name which starts with a period is the code for a
3115 function. If the symbol is undefined, then add an undefined
3116 symbol for the function descriptor, and import that instead. */
3117 if (h
->root
.root
.string
[0] == '.'
3118 && h
->root
.type
== bfd_link_hash_undefined
3119 && val
== (bfd_vma
) -1)
3121 struct xcoff_link_hash_entry
*hds
;
3123 hds
= h
->descriptor
;
3126 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3127 h
->root
.root
.string
+ 1,
3131 if (hds
->root
.type
== bfd_link_hash_new
)
3133 hds
->root
.type
= bfd_link_hash_undefined
;
3134 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3136 hds
->flags
|= XCOFF_DESCRIPTOR
;
3137 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3138 hds
->descriptor
= h
;
3139 h
->descriptor
= hds
;
3142 /* Now, if the descriptor is undefined, import the descriptor
3143 rather than the symbol we were told to import. FIXME: Is
3144 this correct in all cases? */
3145 if (hds
->root
.type
== bfd_link_hash_undefined
)
3149 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3151 if (val
!= (bfd_vma
) -1)
3153 if (h
->root
.type
== bfd_link_hash_defined
)
3154 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
, output_bfd
,
3155 bfd_abs_section_ptr
, val
);
3157 h
->root
.type
= bfd_link_hash_defined
;
3158 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3159 h
->root
.u
.def
.value
= val
;
3163 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3169 /* Export a symbol. */
3172 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3173 struct bfd_link_info
*info
,
3174 struct bfd_link_hash_entry
*harg
)
3176 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3178 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3181 h
->flags
|= XCOFF_EXPORT
;
3183 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3184 I'm just going to ignore it until somebody explains it. */
3186 /* Make sure we don't garbage collect this symbol. */
3187 if (! xcoff_mark_symbol (info
, h
))
3190 /* If this is a function descriptor, make sure we don't garbage
3191 collect the associated function code. We normally don't have to
3192 worry about this, because the descriptor will be attached to a
3193 section with relocs, but if we are creating the descriptor
3194 ourselves those relocs will not be visible to the mark code. */
3195 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3197 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3204 /* Count a reloc against a symbol. This is called for relocs
3205 generated by the linker script, typically for global constructors
3209 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3210 struct bfd_link_info
*info
,
3213 struct xcoff_link_hash_entry
*h
;
3215 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3218 h
= ((struct xcoff_link_hash_entry
*)
3219 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, false, false,
3223 _bfd_error_handler (_("%s: no such symbol"), name
);
3224 bfd_set_error (bfd_error_no_symbols
);
3228 h
->flags
|= XCOFF_REF_REGULAR
;
3229 if (xcoff_hash_table (info
)->loader_section
)
3231 h
->flags
|= XCOFF_LDREL
;
3232 ++xcoff_hash_table (info
)->ldrel_count
;
3235 /* Mark the symbol to avoid garbage collection. */
3236 if (! xcoff_mark_symbol (info
, h
))
3242 /* This function is called for each symbol to which the linker script
3244 FIXME: In cases like the linker test ld-scripts/defined5 where a
3245 symbol is defined both by an input object file and the script,
3246 the script definition doesn't override the object file definition
3247 as is usual for other targets. At least not when the symbol is
3248 output. Other uses of the symbol value by the linker do use the
3252 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3253 struct bfd_link_info
*info
,
3256 struct xcoff_link_hash_entry
*h
;
3258 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3261 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
3266 h
->flags
|= XCOFF_DEF_REGULAR
;
3271 /* An xcoff_link_hash_traverse callback for which DATA points to an
3272 xcoff_loader_info. Mark all symbols that should be automatically
3276 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3278 struct xcoff_loader_info
*ldinfo
;
3280 ldinfo
= (struct xcoff_loader_info
*) data
;
3281 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3283 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3284 ldinfo
->failed
= true;
3289 /* Add a symbol to the .loader symbols, if necessary. */
3291 /* INPUT_BFD has an external symbol associated with hash table entry H
3292 and csect CSECT. Return true if INPUT_BFD defines H. */
3295 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3298 switch (h
->root
.type
)
3300 case bfd_link_hash_defined
:
3301 case bfd_link_hash_defweak
:
3302 /* No input bfd owns absolute symbols. They are written by
3303 xcoff_write_global_symbol instead. */
3304 return (!bfd_is_abs_section (csect
)
3305 && h
->root
.u
.def
.section
== csect
);
3307 case bfd_link_hash_common
:
3308 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3310 case bfd_link_hash_undefined
:
3311 case bfd_link_hash_undefweak
:
3312 /* We can't treat undef.abfd as the owner because that bfd
3313 might be a dynamic object. Allow any bfd to claim it. */
3321 /* See if H should have a loader symbol associated with it. */
3324 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3325 struct xcoff_link_hash_entry
*h
)
3329 /* Warn if this symbol is exported but not defined. */
3330 if ((h
->flags
& XCOFF_EXPORT
) != 0
3331 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3334 (_("warning: attempt to export undefined symbol `%s'"),
3335 h
->root
.root
.string
);
3339 /* We need to add a symbol to the .loader section if it is mentioned
3340 in a reloc which we are copying to the .loader section and it was
3341 not defined or common, or if it is the entry point, or if it is
3343 if (((h
->flags
& XCOFF_LDREL
) == 0
3344 || h
->root
.type
== bfd_link_hash_defined
3345 || h
->root
.type
== bfd_link_hash_defweak
3346 || h
->root
.type
== bfd_link_hash_common
)
3347 && (h
->flags
& XCOFF_ENTRY
) == 0
3348 && (h
->flags
& XCOFF_EXPORT
) == 0)
3351 /* We need to add this symbol to the .loader symbols. */
3353 BFD_ASSERT (h
->ldsym
== NULL
);
3354 amt
= sizeof (struct internal_ldsym
);
3355 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3356 if (h
->ldsym
== NULL
)
3358 ldinfo
->failed
= true;
3362 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3364 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3365 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3367 h
->ldsym
->l_ifile
= h
->ldindx
;
3370 /* The first 3 symbol table indices are reserved to indicate the
3371 data, text and bss sections. */
3372 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3374 ++ldinfo
->ldsym_count
;
3376 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3377 h
->ldsym
, h
->root
.root
.string
))
3380 h
->flags
|= XCOFF_BUILT_LDSYM
;
3384 /* An xcoff_htab_traverse callback that is called for each symbol
3385 once garbage collection is complete. */
3388 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3390 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3392 /* __rtinit, this symbol has special handling. */
3393 if (h
->flags
& XCOFF_RTINIT
)
3396 /* We don't want to garbage collect symbols which are not defined in
3397 XCOFF files. This is a convenient place to mark them. */
3398 if (xcoff_hash_table (ldinfo
->info
)->gc
3399 && (h
->flags
& XCOFF_MARK
) == 0
3400 && (h
->root
.type
== bfd_link_hash_defined
3401 || h
->root
.type
== bfd_link_hash_defweak
)
3402 && (h
->root
.u
.def
.section
->owner
== NULL
3403 || (h
->root
.u
.def
.section
->owner
->xvec
3404 != ldinfo
->info
->output_bfd
->xvec
)))
3405 h
->flags
|= XCOFF_MARK
;
3407 /* Skip discarded symbols. */
3408 if (xcoff_hash_table (ldinfo
->info
)->gc
3409 && (h
->flags
& XCOFF_MARK
) == 0)
3412 /* If this is still a common symbol, and it wasn't garbage
3413 collected, we need to actually allocate space for it in the .bss
3415 if (h
->root
.type
== bfd_link_hash_common
3416 && h
->root
.u
.c
.p
->section
->size
== 0)
3418 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3419 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3422 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3424 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3425 h
->flags
|= XCOFF_EXPORT
;
3427 if (!xcoff_build_ldsym (ldinfo
, h
))
3434 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3435 hash table entry H and csect CSECT. AUX contains ISYM's auxiliary
3436 csect information, if any. NAME is the function's name if the name
3437 is stored in the .debug section, otherwise it is null.
3439 Return 1 if we should include an appropriately-adjusted ISYM
3440 in the output file, 0 if we should discard ISYM, or -1 if an
3444 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3445 struct internal_syment
*isym
,
3446 union internal_auxent
*aux
,
3447 struct xcoff_link_hash_entry
*h
,
3448 asection
*csect
, const char *name
)
3452 /* If we are skipping this csect, we want to strip the symbol too. */
3456 /* Likewise if we garbage-collected the csect. */
3457 if (xcoff_hash_table (info
)->gc
3458 && !bfd_is_abs_section (csect
)
3459 && !bfd_is_und_section (csect
)
3460 && (csect
->flags
& SEC_MARK
) == 0)
3463 /* An XCOFF linker always removes C_STAT symbols. */
3464 if (isym
->n_sclass
== C_STAT
)
3467 /* We generate the TOC anchor separately. */
3468 if (isym
->n_sclass
== C_HIDEXT
3469 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3472 /* If we are stripping all symbols, we want to discard this one. */
3473 if (info
->strip
== strip_all
)
3476 /* Discard symbols that are defined elsewhere. */
3477 if (EXTERN_SYM_P (isym
->n_sclass
))
3479 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3481 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3485 /* If we're discarding local symbols, check whether ISYM is local. */
3486 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3487 if (info
->discard
== discard_all
3488 && !EXTERN_SYM_P (isym
->n_sclass
)
3489 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3492 /* If we're stripping debugging symbols, check whether ISYM is one. */
3493 if (info
->strip
== strip_debugger
3494 && isym
->n_scnum
== N_DEBUG
)
3497 /* If we are stripping symbols based on name, check how ISYM's
3498 name should be handled. */
3499 if (info
->strip
== strip_some
3500 || info
->discard
== discard_l
)
3502 char buf
[SYMNMLEN
+ 1];
3506 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3511 if (info
->strip
== strip_some
3512 && bfd_hash_lookup (info
->keep_hash
, name
, false, false) == NULL
)
3515 if (info
->discard
== discard_l
3516 && !EXTERN_SYM_P (isym
->n_sclass
)
3517 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3518 && bfd_is_local_label_name (input_bfd
, name
))
3525 /* Lay out the .loader section, filling in the header and the import paths.
3526 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3529 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
,
3530 const char *libpath
)
3533 struct xcoff_link_hash_table
*htab
;
3534 struct internal_ldhdr
*ldhdr
;
3535 struct xcoff_import_file
*fl
;
3536 bfd_size_type stoff
;
3537 size_t impsize
, impcount
;
3541 /* Work out the size of the import file names. Each import file ID
3542 consists of three null terminated strings: the path, the file
3543 name, and the archive member name. The first entry in the list
3544 of names is the path to use to find objects, which the linker has
3545 passed in as the libpath argument. For some reason, the path
3546 entry in the other import file names appears to always be empty. */
3547 output_bfd
= ldinfo
->output_bfd
;
3548 htab
= xcoff_hash_table (ldinfo
->info
);
3549 impsize
= strlen (libpath
) + 3;
3551 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3554 impsize
+= (strlen (fl
->path
)
3556 + strlen (fl
->member
)
3560 /* Set up the .loader section header. */
3561 ldhdr
= &htab
->ldhdr
;
3562 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3563 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3564 ldhdr
->l_nreloc
= htab
->ldrel_count
;
3565 ldhdr
->l_istlen
= impsize
;
3566 ldhdr
->l_nimpid
= impcount
;
3567 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3568 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3569 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3570 ldhdr
->l_stlen
= ldinfo
->string_size
;
3571 stoff
= ldhdr
->l_impoff
+ impsize
;
3572 if (ldinfo
->string_size
== 0)
3575 ldhdr
->l_stoff
= stoff
;
3577 /* 64 bit elements to ldhdr
3578 The swap out routine for 32 bit will ignore them.
3579 Nothing fancy, symbols come after the header and relocs come
3581 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3582 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3583 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3585 /* We now know the final size of the .loader section. Allocate
3587 lsec
= htab
->loader_section
;
3588 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3589 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3590 if (lsec
->contents
== NULL
)
3593 /* Set up the header. */
3594 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3596 /* Set up the import file names. */
3597 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3598 strcpy (out
, libpath
);
3599 out
+= strlen (libpath
) + 1;
3602 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3607 while ((*out
++ = *s
++) != '\0')
3610 while ((*out
++ = *s
++) != '\0')
3613 while ((*out
++ = *s
++) != '\0')
3617 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3619 /* Set up the symbol string table. */
3620 if (ldinfo
->string_size
> 0)
3622 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
3623 free (ldinfo
->strings
);
3624 ldinfo
->strings
= NULL
;
3627 /* We can't set up the symbol table or the relocs yet, because we
3628 don't yet know the final position of the various sections. The
3629 .loader symbols are written out when the corresponding normal
3630 symbols are written out in xcoff_link_input_bfd or
3631 xcoff_write_global_symbol. The .loader relocs are written out
3632 when the corresponding normal relocs are handled in
3633 xcoff_link_input_bfd. */
3638 /* Build the .loader section. This is called by the XCOFF linker
3639 emulation before_allocation routine. We must set the size of the
3640 .loader section before the linker lays out the output file.
3641 LIBPATH is the library path to search for shared objects; this is
3642 normally built from the -L arguments passed to the linker. ENTRY
3643 is the name of the entry point symbol (the -e linker option).
3644 FILE_ALIGN is the alignment to use for sections within the file
3645 (the -H linker option). MAXSTACK is the maximum stack size (the
3646 -bmaxstack linker option). MAXDATA is the maximum data size (the
3647 -bmaxdata linker option). GC is whether to do garbage collection
3648 (the -bgc linker option). MODTYPE is the module type (the
3649 -bmodtype linker option). TEXTRO is whether the text section must
3650 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3651 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3652 is set by this routine to csects with magic names like _end. */
3655 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3656 struct bfd_link_info
*info
,
3657 const char *libpath
,
3659 unsigned long file_align
,
3660 unsigned long maxstack
,
3661 unsigned long maxdata
,
3665 unsigned int auto_export_flags
,
3666 asection
**special_sections
,
3669 struct xcoff_loader_info ldinfo
;
3673 struct bfd_strtab_hash
*debug_strtab
;
3674 bfd_byte
*debug_contents
= NULL
;
3677 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3679 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3680 special_sections
[i
] = NULL
;
3684 ldinfo
.failed
= false;
3685 ldinfo
.output_bfd
= output_bfd
;
3687 ldinfo
.auto_export_flags
= auto_export_flags
;
3688 ldinfo
.ldsym_count
= 0;
3689 ldinfo
.string_size
= 0;
3690 ldinfo
.strings
= NULL
;
3691 ldinfo
.string_alc
= 0;
3693 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3694 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3695 xcoff_data (output_bfd
)->modtype
= modtype
;
3697 xcoff_hash_table (info
)->file_align
= file_align
;
3698 xcoff_hash_table (info
)->textro
= textro
;
3699 xcoff_hash_table (info
)->rtld
= rtld
;
3702 if (xcoff_hash_table (info
)->loader_section
3703 && (info
->init_function
|| info
->fini_function
|| rtld
))
3705 struct xcoff_link_hash_entry
*hsym
;
3706 struct internal_ldsym
*ldsym
;
3708 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3709 "__rtinit", false, false, true);
3713 (_("error: undefined symbol __rtinit"));
3717 xcoff_mark_symbol (info
, hsym
);
3718 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3720 /* __rtinit initialized. */
3721 amt
= sizeof (* ldsym
);
3722 ldsym
= bfd_malloc (amt
);
3724 ldsym
->l_value
= 0; /* Will be filled in later. */
3725 ldsym
->l_scnum
= 2; /* Data section. */
3726 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3727 ldsym
->l_smclas
= 5; /* .rw. */
3728 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3729 ldsym
->l_parm
= 0; /* NA. */
3731 /* Force __rtinit to be the first symbol in the loader symbol table
3732 See xcoff_build_ldsyms
3734 The first 3 symbol table indices are reserved to indicate the data,
3735 text and bss sections. */
3736 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3739 ldinfo
.ldsym_count
= 1;
3740 hsym
->ldsym
= ldsym
;
3742 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3743 hsym
->ldsym
, hsym
->root
.root
.string
))
3746 /* This symbol is written out by xcoff_write_global_symbol
3747 Set stuff up so xcoff_write_global_symbol logic works. */
3748 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3749 hsym
->root
.type
= bfd_link_hash_defined
;
3750 hsym
->root
.u
.def
.value
= 0;
3753 /* Garbage collect unused sections. */
3754 if (bfd_link_relocatable (info
) || !gc
)
3757 xcoff_hash_table (info
)->gc
= false;
3759 /* We still need to call xcoff_mark, in order to set ldrel_count
3761 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3765 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3767 /* We shouldn't unconditionaly mark the TOC section.
3768 The output file should only have a TOC if either
3769 (a) one of the input files did or (b) we end up
3770 creating TOC references as part of the link process. */
3771 if (o
!= xcoff_hash_table (info
)->toc_section
3772 && (o
->flags
& SEC_MARK
) == 0)
3774 if (! xcoff_mark (info
, o
))
3783 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3785 if (info
->init_function
!= NULL
3786 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3788 if (info
->fini_function
!= NULL
3789 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3791 if (auto_export_flags
!= 0)
3793 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3794 xcoff_mark_auto_exports
, &ldinfo
);
3799 xcoff_hash_table (info
)->gc
= true;
3802 /* Return special sections to the caller. */
3803 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3805 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3809 && (sec
->flags
& SEC_MARK
) == 0)
3812 special_sections
[i
] = sec
;
3815 if (info
->input_bfds
== NULL
)
3816 /* I'm not sure what to do in this bizarre case. */
3819 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3824 if (xcoff_hash_table (info
)->loader_section
3825 && !xcoff_build_loader_section (&ldinfo
, libpath
))
3828 /* Allocate space for the magic sections. */
3829 sec
= xcoff_hash_table (info
)->linkage_section
;
3832 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3833 if (sec
->contents
== NULL
)
3836 sec
= xcoff_hash_table (info
)->toc_section
;
3839 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3840 if (sec
->contents
== NULL
)
3843 sec
= xcoff_hash_table (info
)->descriptor_section
;
3846 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3847 if (sec
->contents
== NULL
)
3851 /* Now that we've done garbage collection, decide which symbols to keep,
3852 and figure out the contents of the .debug section. */
3853 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3855 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3858 bfd_size_type symcount
;
3861 unsigned int *lineno_counts
;
3862 struct xcoff_link_hash_entry
**sym_hash
;
3863 bfd_byte
*esym
, *esymend
;
3864 bfd_size_type symesz
;
3866 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3869 if ((sub
->flags
& DYNAMIC
) != 0
3870 && !info
->static_link
)
3873 if (! _bfd_coff_get_external_symbols (sub
))
3876 symcount
= obj_raw_syment_count (sub
);
3877 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
3878 if (debug_index
== NULL
)
3880 xcoff_data (sub
)->debug_indices
= debug_index
;
3882 if (info
->strip
== strip_all
3883 || info
->strip
== strip_debugger
3884 || info
->discard
== discard_all
)
3885 /* We're stripping all debugging information, so there's no need
3886 to read SUB's .debug section. */
3890 /* Grab the contents of SUB's .debug section, if any. */
3891 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3892 if (subdeb
!= NULL
&& subdeb
->size
> 0)
3894 /* We use malloc and copy the names into the debug
3895 stringtab, rather than bfd_alloc, because I expect
3896 that, when linking many files together, many of the
3897 strings will be the same. Storing the strings in the
3898 hash table should save space in this case. */
3899 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
3904 csectpp
= xcoff_data (sub
)->csects
;
3905 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
3906 sym_hash
= obj_xcoff_sym_hashes (sub
);
3907 symesz
= bfd_coff_symesz (sub
);
3908 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3909 esymend
= esym
+ symcount
* symesz
;
3911 while (esym
< esymend
)
3913 struct internal_syment sym
;
3914 union internal_auxent aux
;
3919 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
3921 /* Read in the csect information, if any. */
3922 if (CSECT_SYM_P (sym
.n_sclass
))
3924 BFD_ASSERT (sym
.n_numaux
> 0);
3925 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
3926 sym
.n_type
, sym
.n_sclass
,
3927 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
3930 /* If this symbol's name is stored in the debug section,
3931 get a pointer to it. */
3932 if (debug_contents
!= NULL
3933 && sym
._n
._n_n
._n_zeroes
== 0
3934 && bfd_coff_symname_in_debug (sub
, &sym
))
3935 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3939 /* Decide whether to copy this symbol to the output file. */
3941 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
3942 *sym_hash
, csect
, name
);
3947 /* Use a debug_index of -2 to record that a symbol should
3952 /* See whether we should store the symbol name in the
3953 output .debug section. */
3958 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
3959 if (indx
== (bfd_size_type
) -1)
3961 *debug_index
= indx
;
3966 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
3967 if (*lineno_counts
> 0)
3968 csect
->output_section
->lineno_count
+= *lineno_counts
;
3971 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3972 csectpp
+= sym
.n_numaux
+ 1;
3973 sym_hash
+= sym
.n_numaux
+ 1;
3974 lineno_counts
+= sym
.n_numaux
+ 1;
3975 debug_index
+= sym
.n_numaux
+ 1;
3980 free (debug_contents
);
3981 debug_contents
= NULL
;
3983 /* Clear the size of subdeb, so that it is not included directly
3984 in the output file. */
3988 if (! info
->keep_memory
)
3990 if (! _bfd_coff_free_symbols (sub
))
3995 if (info
->strip
!= strip_all
)
3996 xcoff_hash_table (info
)->debug_section
->size
=
3997 _bfd_stringtab_size (debug_strtab
);
4002 free (ldinfo
.strings
);
4003 free (debug_contents
);
4008 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
4013 struct bfd_in_memory
*bim
;
4015 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
4022 abfd
->link
.next
= 0;
4023 abfd
->format
= bfd_object
;
4024 abfd
->iostream
= (void *) bim
;
4025 abfd
->flags
= BFD_IN_MEMORY
;
4026 abfd
->iovec
= &_bfd_memory_iovec
;
4027 abfd
->direction
= write_direction
;
4031 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
4034 /* need to reset to unknown or it will not be read back in correctly */
4035 abfd
->format
= bfd_unknown
;
4036 abfd
->direction
= read_direction
;
4042 /* Return the section that defines H. Return null if no section does. */
4045 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
4047 switch (h
->root
.type
)
4049 case bfd_link_hash_defined
:
4050 case bfd_link_hash_defweak
:
4051 return h
->root
.u
.def
.section
;
4053 case bfd_link_hash_common
:
4054 return h
->root
.u
.c
.p
->section
;
4061 /* Add a .loader relocation for input relocation IREL. If the loader
4062 relocation should be against an output section, HSEC points to the
4063 input section that IREL is against, otherwise HSEC is null. H is the
4064 symbol that IREL is against, or null if it isn't against a global symbol.
4065 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4068 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
4069 asection
*output_section
, bfd
*reference_bfd
,
4070 struct internal_reloc
*irel
, asection
*hsec
,
4071 struct xcoff_link_hash_entry
*h
)
4073 struct internal_ldrel ldrel
;
4075 ldrel
.l_vaddr
= irel
->r_vaddr
;
4078 const char *secname
;
4080 secname
= hsec
->output_section
->name
;
4081 if (strcmp (secname
, ".text") == 0)
4083 else if (strcmp (secname
, ".data") == 0)
4085 else if (strcmp (secname
, ".bss") == 0)
4087 else if (strcmp (secname
, ".tdata") == 0)
4088 ldrel
.l_symndx
= -1;
4089 else if (strcmp (secname
, ".tbss") == 0)
4090 ldrel
.l_symndx
= -2;
4094 /* xgettext:c-format */
4095 (_("%pB: loader reloc in unrecognized section `%s'"),
4096 reference_bfd
, secname
);
4097 bfd_set_error (bfd_error_nonrepresentable_section
);
4106 /* xgettext:c-format */
4107 (_("%pB: `%s' in loader reloc but not loader sym"),
4108 reference_bfd
, h
->root
.root
.string
);
4109 bfd_set_error (bfd_error_bad_value
);
4112 ldrel
.l_symndx
= h
->ldindx
;
4115 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4117 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4118 ldrel
.l_rsecnm
= output_section
->target_index
;
4119 if (xcoff_hash_table (flinfo
->info
)->textro
4120 && strcmp (output_section
->name
, ".text") == 0)
4123 /* xgettext:c-format */
4124 (_("%pB: loader reloc in read-only section %pA"),
4125 reference_bfd
, output_section
);
4126 bfd_set_error (bfd_error_invalid_operation
);
4129 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
4130 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4134 /* Link an input file into the linker output file. This function
4135 handles all the sections and relocations of the input file at once. */
4138 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
4142 const char *strings
;
4143 bfd_size_type syment_base
;
4144 unsigned int n_tmask
;
4145 unsigned int n_btshft
;
4147 bfd_size_type isymesz
;
4148 bfd_size_type osymesz
;
4149 bfd_size_type linesz
;
4152 struct xcoff_link_hash_entry
**sym_hash
;
4153 struct internal_syment
*isymp
;
4155 unsigned int *lineno_counts
;
4158 unsigned long output_index
;
4165 /* We can just skip DYNAMIC files, unless this is a static link. */
4166 if ((input_bfd
->flags
& DYNAMIC
) != 0
4167 && ! flinfo
->info
->static_link
)
4170 /* Move all the symbols to the output file. */
4171 output_bfd
= flinfo
->output_bfd
;
4173 syment_base
= obj_raw_syment_count (output_bfd
);
4174 isymesz
= bfd_coff_symesz (input_bfd
);
4175 osymesz
= bfd_coff_symesz (output_bfd
);
4176 linesz
= bfd_coff_linesz (input_bfd
);
4177 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4179 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4180 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4182 /* Define macros so that ISFCN, et. al., macros work correctly. */
4183 #define N_TMASK n_tmask
4184 #define N_BTSHFT n_btshft
4187 if (! flinfo
->info
->keep_memory
)
4190 if (flinfo
->info
->traditional_format
)
4193 if (! _bfd_coff_get_external_symbols (input_bfd
))
4196 /* Make one pass over the symbols and assign indices to symbols that
4197 we have decided to keep. Also use create .loader symbol information
4198 and update information in hash table entries. */
4199 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4200 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4201 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4202 csectpp
= xcoff_data (input_bfd
)->csects
;
4203 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4204 isymp
= flinfo
->internal_syms
;
4205 indexp
= flinfo
->sym_indices
;
4206 output_index
= syment_base
;
4207 while (esym
< esym_end
)
4209 union internal_auxent aux
;
4213 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4215 /* Read in the csect information, if any. */
4216 if (CSECT_SYM_P (isymp
->n_sclass
))
4218 BFD_ASSERT (isymp
->n_numaux
> 0);
4219 bfd_coff_swap_aux_in (input_bfd
,
4220 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4221 isymp
->n_type
, isymp
->n_sclass
,
4222 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4225 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4228 /* If this symbol is in the .loader section, swap out the
4229 .loader symbol information. If this is an external symbol
4230 reference to a defined symbol, though, then wait until we get
4231 to the definition. */
4232 if (EXTERN_SYM_P (isymp
->n_sclass
)
4233 && *sym_hash
!= NULL
4234 && (*sym_hash
)->ldsym
!= NULL
4235 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4237 struct xcoff_link_hash_entry
*h
;
4238 struct internal_ldsym
*ldsym
;
4242 if (isymp
->n_scnum
> 0)
4244 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4245 ldsym
->l_value
= (isymp
->n_value
4246 + (*csectpp
)->output_section
->vma
4247 + (*csectpp
)->output_offset
4252 ldsym
->l_scnum
= isymp
->n_scnum
;
4253 ldsym
->l_value
= isymp
->n_value
;
4256 ldsym
->l_smtype
= smtyp
;
4257 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4258 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4259 || (h
->flags
& XCOFF_IMPORT
) != 0)
4260 ldsym
->l_smtype
|= L_IMPORT
;
4261 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4262 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4263 || (h
->flags
& XCOFF_EXPORT
) != 0)
4264 ldsym
->l_smtype
|= L_EXPORT
;
4265 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4266 ldsym
->l_smtype
|= L_ENTRY
;
4267 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4268 ldsym
->l_smtype
|= L_WEAK
;
4270 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4272 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4274 else if (ldsym
->l_ifile
== 0)
4276 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4282 if (h
->root
.type
== bfd_link_hash_defined
4283 || h
->root
.type
== bfd_link_hash_defweak
)
4284 impbfd
= h
->root
.u
.def
.section
->owner
;
4285 else if (h
->root
.type
== bfd_link_hash_undefined
4286 || h
->root
.type
== bfd_link_hash_undefweak
)
4287 impbfd
= h
->root
.u
.undef
.abfd
;
4295 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
4296 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4303 BFD_ASSERT (h
->ldindx
>= 0);
4304 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
4307 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
4310 /* Fill in snentry now that we know the target_index. */
4311 if ((h
->flags
& XCOFF_ENTRY
) != 0
4312 && (h
->root
.type
== bfd_link_hash_defined
4313 || h
->root
.type
== bfd_link_hash_defweak
))
4315 xcoff_data (output_bfd
)->snentry
=
4316 h
->root
.u
.def
.section
->output_section
->target_index
;
4320 add
= 1 + isymp
->n_numaux
;
4322 if (*debug_index
== -2)
4323 /* We've decided to strip this symbol. */
4327 /* Assign the next unused index to this symbol. */
4328 *indexp
= output_index
;
4330 if (EXTERN_SYM_P (isymp
->n_sclass
))
4332 BFD_ASSERT (*sym_hash
!= NULL
);
4333 (*sym_hash
)->indx
= output_index
;
4336 /* If this is a symbol in the TOC which we may have merged
4337 (class XMC_TC), remember the symbol index of the TOC
4339 if (isymp
->n_sclass
== C_HIDEXT
4340 && aux
.x_csect
.x_smclas
== XMC_TC
4341 && *sym_hash
!= NULL
)
4343 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4344 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4345 (*sym_hash
)->u
.toc_indx
= output_index
;
4348 output_index
+= add
;
4351 esym
+= add
* isymesz
;
4357 for (--add
; add
> 0; --add
)
4361 /* Now write out the symbols that we decided to keep. */
4363 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4364 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4365 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4366 isymp
= flinfo
->internal_syms
;
4367 indexp
= flinfo
->sym_indices
;
4368 csectpp
= xcoff_data (input_bfd
)->csects
;
4369 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4370 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4371 outsym
= flinfo
->outsyms
;
4374 while (esym
< esym_end
)
4378 add
= 1 + isymp
->n_numaux
;
4381 esym
+= add
* isymesz
;
4384 struct internal_syment isym
;
4387 /* Adjust the symbol in order to output it. */
4389 if (isym
._n
._n_n
._n_zeroes
== 0
4390 && isym
._n
._n_n
._n_offset
!= 0)
4392 /* This symbol has a long name. Enter it in the string
4393 table we are building. If *debug_index != -1, the
4394 name has already been entered in the .debug section. */
4395 if (*debug_index
>= 0)
4396 isym
._n
._n_n
._n_offset
= *debug_index
;
4402 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4406 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
4407 if (indx
== (bfd_size_type
) -1)
4409 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4413 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4414 multiple definition problems when linking a shared object
4415 statically. (The native linker doesn't enter __rtinit into
4416 the normal table at all, but having a local symbol can make
4417 the objdump output easier to read.) */
4418 if (isym
.n_sclass
== C_EXT
4420 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4421 isym
.n_sclass
= C_HIDEXT
;
4423 /* The value of a C_FILE symbol is the symbol index of the
4424 next C_FILE symbol. The value of the last C_FILE symbol
4425 is -1. We try to get this right, below, just before we
4426 write the symbols out, but in the general case we may
4427 have to write the symbol out twice. */
4428 if (isym
.n_sclass
== C_FILE
)
4430 if (flinfo
->last_file_index
!= -1
4431 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4433 /* We must correct the value of the last C_FILE entry. */
4434 flinfo
->last_file
.n_value
= *indexp
;
4435 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4437 /* The last C_FILE symbol is in this input file. */
4438 bfd_coff_swap_sym_out (output_bfd
,
4439 (void *) &flinfo
->last_file
,
4440 (void *) (flinfo
->outsyms
4441 + ((flinfo
->last_file_index
4447 /* We have already written out the last C_FILE
4448 symbol. We need to write it out again. We
4449 borrow *outsym temporarily. */
4452 bfd_coff_swap_sym_out (output_bfd
,
4453 (void *) &flinfo
->last_file
,
4456 pos
= obj_sym_filepos (output_bfd
);
4457 pos
+= flinfo
->last_file_index
* osymesz
;
4458 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4459 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4465 flinfo
->last_file_index
= *indexp
;
4466 flinfo
->last_file
= isym
;
4469 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4470 into the line numbers. We update the symbol values when
4471 we handle the line numbers. */
4472 if (isym
.n_sclass
== C_BINCL
4473 || isym
.n_sclass
== C_EINCL
)
4475 isym
.n_value
= flinfo
->line_filepos
;
4478 /* The value of a C_BSTAT symbol is the symbol table
4479 index of the containing csect. */
4480 else if (isym
.n_sclass
== C_BSTAT
)
4484 indx
= isym
.n_value
;
4485 if (indx
< obj_raw_syment_count (input_bfd
))
4489 symindx
= flinfo
->sym_indices
[indx
];
4493 isym
.n_value
= symindx
;
4496 else if (isym
.n_sclass
!= C_ESTAT
4497 && isym
.n_sclass
!= C_DECL
4498 && isym
.n_scnum
> 0)
4500 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4501 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4502 + (*csectpp
)->output_offset
4506 /* Output the symbol. */
4507 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4512 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4514 union internal_auxent aux
;
4516 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4517 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4520 if (isymp
->n_sclass
== C_FILE
)
4522 /* This is the file name (or some comment put in by
4523 the compiler). If it is long, we must put it in
4524 the string table. */
4525 if (aux
.x_file
.x_n
.x_zeroes
== 0
4526 && aux
.x_file
.x_n
.x_offset
!= 0)
4528 const char *filename
;
4531 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4532 >= STRING_SIZE_SIZE
);
4533 if (strings
== NULL
)
4535 strings
= _bfd_coff_read_string_table (input_bfd
);
4536 if (strings
== NULL
)
4539 if ((bfd_size_type
) aux
.x_file
.x_n
.x_offset
>= obj_coff_strings_len (input_bfd
))
4540 filename
= _("<corrupt>");
4542 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4543 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
4545 if (indx
== (bfd_size_type
) -1)
4547 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4550 else if (CSECT_SYM_P (isymp
->n_sclass
)
4551 && i
+ 1 == isymp
->n_numaux
)
4554 /* We don't support type checking. I don't know if
4556 aux
.x_csect
.x_parmhash
= 0;
4557 /* I don't think anybody uses these fields, but we'd
4558 better clobber them just in case. */
4559 aux
.x_csect
.x_stab
= 0;
4560 aux
.x_csect
.x_snstab
= 0;
4562 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4566 indx
= aux
.x_csect
.x_scnlen
.l
;
4567 if (indx
< obj_raw_syment_count (input_bfd
))
4571 symindx
= flinfo
->sym_indices
[indx
];
4574 aux
.x_csect
.x_scnlen
.l
= 0;
4578 aux
.x_csect
.x_scnlen
.l
= symindx
;
4583 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4587 if (ISFCN (isymp
->n_type
)
4588 || ISTAG (isymp
->n_sclass
)
4589 || isymp
->n_sclass
== C_BLOCK
4590 || isymp
->n_sclass
== C_FCN
)
4592 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4594 && indx
< obj_raw_syment_count (input_bfd
))
4596 /* We look forward through the symbol for
4597 the index of the next symbol we are going
4598 to include. I don't know if this is
4600 while (flinfo
->sym_indices
[indx
] < 0
4601 && indx
< obj_raw_syment_count (input_bfd
))
4603 if (indx
>= obj_raw_syment_count (input_bfd
))
4604 indx
= output_index
;
4606 indx
= flinfo
->sym_indices
[indx
];
4607 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4612 indx
= aux
.x_sym
.x_tagndx
.l
;
4613 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4617 symindx
= flinfo
->sym_indices
[indx
];
4619 aux
.x_sym
.x_tagndx
.l
= 0;
4621 aux
.x_sym
.x_tagndx
.l
= symindx
;
4626 /* Copy over the line numbers, unless we are stripping
4627 them. We do this on a symbol by symbol basis in
4628 order to more easily handle garbage collection. */
4629 if (CSECT_SYM_P (isymp
->n_sclass
)
4631 && isymp
->n_numaux
> 1
4632 && ISFCN (isymp
->n_type
)
4633 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4635 if (*lineno_counts
== 0)
4636 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4639 asection
*enclosing
;
4640 unsigned int enc_count
;
4641 bfd_signed_vma linoff
;
4642 struct internal_lineno lin
;
4649 /* Read in the enclosing section's line-number
4650 information, if we haven't already. */
4652 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4653 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4654 if (oline
!= enclosing
)
4656 pos
= enclosing
->line_filepos
;
4657 amt
= linesz
* enc_count
;
4658 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4659 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
4665 /* Copy across the first entry, adjusting its
4667 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4668 - enclosing
->line_filepos
);
4669 linp
= flinfo
->linenos
+ linoff
;
4670 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4671 lin
.l_addr
.l_symndx
= *indexp
;
4672 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4674 /* Copy the other entries, adjusting their addresses. */
4675 linpend
= linp
+ *lineno_counts
* linesz
;
4676 offset
= (o
->output_section
->vma
4679 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4681 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4682 lin
.l_addr
.l_paddr
+= offset
;
4683 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4686 /* Write out the entries we've just processed. */
4687 pos
= (o
->output_section
->line_filepos
4688 + o
->output_section
->lineno_count
* linesz
);
4689 amt
= linesz
* *lineno_counts
;
4690 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4691 || bfd_bwrite (flinfo
->linenos
+ linoff
,
4692 amt
, output_bfd
) != amt
)
4694 o
->output_section
->lineno_count
+= *lineno_counts
;
4696 /* Record the offset of the symbol's line numbers
4697 in the output file. */
4698 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4702 struct internal_syment
*iisp
, *iispend
;
4705 bfd_vma range_start
, range_end
;
4708 /* Update any C_BINCL or C_EINCL symbols
4709 that refer to a line number in the
4710 range we just output. */
4711 iisp
= flinfo
->internal_syms
;
4712 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4713 iindp
= flinfo
->sym_indices
;
4714 oos
= flinfo
->outsyms
;
4715 range_start
= enclosing
->line_filepos
+ linoff
;
4716 range_end
= range_start
+ *lineno_counts
* linesz
;
4717 while (iisp
< iispend
)
4720 && (iisp
->n_sclass
== C_BINCL
4721 || iisp
->n_sclass
== C_EINCL
)
4722 && iisp
->n_value
>= range_start
4723 && iisp
->n_value
< range_end
)
4725 struct internal_syment iis
;
4727 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4728 iis
.n_value
= (iisp
->n_value
4731 bfd_coff_swap_sym_out (output_bfd
,
4736 iiadd
= 1 + iisp
->n_numaux
;
4738 oos
+= iiadd
* osymesz
;
4746 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4747 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4758 lineno_counts
+= add
;
4762 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4763 symbol will be the first symbol in the next input file. In the
4764 normal case, this will save us from writing out the C_FILE symbol
4766 if (flinfo
->last_file_index
!= -1
4767 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4769 flinfo
->last_file
.n_value
= output_index
;
4770 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
4771 (void *) (flinfo
->outsyms
4772 + ((flinfo
->last_file_index
- syment_base
)
4776 /* Write the modified symbols to the output file. */
4777 if (outsym
> flinfo
->outsyms
)
4779 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4780 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
4781 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4782 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
4785 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4786 + (outsym
- flinfo
->outsyms
) / osymesz
)
4789 obj_raw_syment_count (output_bfd
) = output_index
;
4792 /* Don't let the linker relocation routines discard the symbols. */
4793 keep_syms
= obj_coff_keep_syms (input_bfd
);
4794 obj_coff_keep_syms (input_bfd
) = true;
4796 /* Relocate the contents of each section. */
4797 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4801 if (! o
->linker_mark
)
4802 /* This section was omitted from the link. */
4805 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4807 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4810 /* We have set filepos correctly for the sections we created to
4811 represent csects, so bfd_get_section_contents should work. */
4812 if (coff_section_data (input_bfd
, o
) != NULL
4813 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4814 contents
= coff_section_data (input_bfd
, o
)->contents
;
4817 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4818 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
4820 contents
= flinfo
->contents
;
4823 if ((o
->flags
& SEC_RELOC
) != 0)
4826 struct internal_reloc
*internal_relocs
;
4827 struct internal_reloc
*irel
;
4829 struct internal_reloc
*irelend
;
4830 struct xcoff_link_hash_entry
**rel_hash
;
4833 /* Read in the relocs. */
4834 target_index
= o
->output_section
->target_index
;
4835 internal_relocs
= (xcoff_read_internal_relocs
4836 (input_bfd
, o
, false, flinfo
->external_relocs
,
4838 (flinfo
->section_info
[target_index
].relocs
4839 + o
->output_section
->reloc_count
)));
4840 if (internal_relocs
== NULL
)
4843 /* Call processor specific code to relocate the section
4845 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
4849 flinfo
->internal_syms
,
4850 xcoff_data (input_bfd
)->csects
))
4853 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4854 irel
= internal_relocs
;
4855 irelend
= irel
+ o
->reloc_count
;
4856 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
4857 + o
->output_section
->reloc_count
);
4858 for (; irel
< irelend
; irel
++, rel_hash
++)
4860 struct xcoff_link_hash_entry
*h
= NULL
;
4864 /* Adjust the reloc address and symbol index. */
4866 irel
->r_vaddr
+= offset
;
4868 r_symndx
= irel
->r_symndx
;
4873 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4875 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
4878 && h
->smclas
!= XMC_TD
4879 && (irel
->r_type
== R_TOC
4880 || irel
->r_type
== R_GL
4881 || irel
->r_type
== R_TCL
4882 || irel
->r_type
== R_TRL
4883 || irel
->r_type
== R_TRLA
))
4885 /* This is a TOC relative reloc with a symbol
4886 attached. The symbol should be the one which
4887 this reloc is for. We want to make this
4888 reloc against the TOC address of the symbol,
4889 not the symbol itself. */
4890 BFD_ASSERT (h
->toc_section
!= NULL
);
4891 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4892 if (h
->u
.toc_indx
!= -1)
4893 irel
->r_symndx
= h
->u
.toc_indx
;
4896 struct xcoff_toc_rel_hash
*n
;
4897 struct xcoff_link_section_info
*si
;
4901 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
4904 si
= flinfo
->section_info
+ target_index
;
4905 n
->next
= si
->toc_rel_hashes
;
4908 si
->toc_rel_hashes
= n
;
4913 /* This is a global symbol. */
4915 irel
->r_symndx
= h
->indx
;
4918 /* This symbol is being written at the end
4919 of the file, and we do not yet know the
4920 symbol index. We save the pointer to the
4921 hash table entry in the rel_hash list.
4922 We set the indx field to -2 to indicate
4923 that this symbol must not be stripped. */
4932 indx
= flinfo
->sym_indices
[r_symndx
];
4936 struct internal_syment
*is
;
4938 /* Relocations against a TC0 TOC anchor are
4939 automatically transformed to be against
4940 the TOC anchor in the output file. */
4941 is
= flinfo
->internal_syms
+ r_symndx
;
4942 if (is
->n_sclass
== C_HIDEXT
4943 && is
->n_numaux
> 0)
4946 union internal_auxent aux
;
4950 obj_coff_external_syms (input_bfd
))
4951 + ((r_symndx
+ is
->n_numaux
)
4953 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4954 is
->n_type
, is
->n_sclass
,
4958 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4959 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4960 indx
= flinfo
->toc_symindx
;
4965 irel
->r_symndx
= indx
;
4969 struct internal_syment
*is
;
4972 char buf
[SYMNMLEN
+ 1];
4974 /* This reloc is against a symbol we are
4975 stripping. It would be possible to handle
4976 this case, but I don't think it's worth it. */
4977 is
= flinfo
->internal_syms
+ r_symndx
;
4979 if (is
->n_sclass
!= C_DWARF
)
4981 name
= (_bfd_coff_internal_syment_name
4982 (input_bfd
, is
, buf
));
4987 (*flinfo
->info
->callbacks
->unattached_reloc
)
4988 (flinfo
->info
, name
,
4989 input_bfd
, o
, irel
->r_vaddr
);
4995 if ((o
->flags
& SEC_DEBUGGING
) == 0
4996 && xcoff_need_ldrel_p (flinfo
->info
, irel
, h
, o
))
5003 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
5005 sec
= xcoff_symbol_section (h
);
5006 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
5007 o
->output_section
, input_bfd
,
5013 o
->output_section
->reloc_count
+= o
->reloc_count
;
5016 /* Write out the modified section contents. */
5017 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5018 contents
, (file_ptr
) o
->output_offset
,
5023 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5025 if (! flinfo
->info
->keep_memory
)
5027 if (! _bfd_coff_free_symbols (input_bfd
))
5034 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5041 /* Sort relocs by VMA. This is called via qsort. */
5044 xcoff_sort_relocs (const void * p1
, const void * p2
)
5046 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
5047 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
5049 if (r1
->r_vaddr
> r2
->r_vaddr
)
5051 else if (r1
->r_vaddr
< r2
->r_vaddr
)
5057 /* Return true if section SEC is a TOC section. */
5060 xcoff_toc_section_p (asection
*sec
)
5065 if (name
[0] == '.' && name
[1] == 't')
5069 if (name
[3] == '0' && name
[4] == 0)
5074 if (name
[2] == 'd' && name
[3] == 0)
5080 /* See if the link requires a TOC (it usually does!). If so, find a
5081 good place to put the TOC anchor csect, and write out the associated
5085 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
5087 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5091 struct internal_syment irsym
;
5092 union internal_auxent iraux
;
5096 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5097 index of a csect at the beginning of the TOC. */
5098 toc_start
= ~(bfd_vma
) 0;
5101 for (input_bfd
= flinfo
->info
->input_bfds
;
5103 input_bfd
= input_bfd
->link
.next
)
5104 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5105 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5107 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5108 if (toc_start
> start
)
5111 section_index
= sec
->output_section
->target_index
;
5114 end
= start
+ sec
->size
;
5119 /* There's no need for a TC0 symbol if we don't have a TOC. */
5120 if (toc_end
< toc_start
)
5122 xcoff_data (output_bfd
)->toc
= toc_start
;
5126 if (toc_end
- toc_start
< 0x8000)
5127 /* Every TOC csect can be accessed from TOC_START. */
5128 best_address
= toc_start
;
5131 /* Find the lowest TOC csect that is still within range of TOC_END. */
5132 best_address
= toc_end
;
5133 for (input_bfd
= flinfo
->info
->input_bfds
;
5135 input_bfd
= input_bfd
->link
.next
)
5136 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5137 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5139 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5140 if (start
< best_address
5141 && start
+ 0x8000 >= toc_end
)
5143 best_address
= start
;
5144 section_index
= sec
->output_section
->target_index
;
5148 /* Make sure that the start of the TOC is also within range. */
5149 if (best_address
> toc_start
+ 0x8000)
5152 (_("TOC overflow: %#" PRIx64
" > 0x10000; try -mminimal-toc "
5154 (uint64_t) (toc_end
- toc_start
));
5155 bfd_set_error (bfd_error_file_too_big
);
5160 /* Record the chosen TOC value. */
5161 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5162 xcoff_data (output_bfd
)->toc
= best_address
;
5163 xcoff_data (output_bfd
)->sntoc
= section_index
;
5165 /* Fill out the TC0 symbol. */
5166 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5169 irsym
.n_value
= best_address
;
5170 irsym
.n_scnum
= section_index
;
5171 irsym
.n_sclass
= C_HIDEXT
;
5172 irsym
.n_type
= T_NULL
;
5174 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
5176 /* Fill out the auxiliary csect information. */
5177 memset (&iraux
, 0, sizeof iraux
);
5178 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5179 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5180 iraux
.x_csect
.x_scnlen
.l
= 0;
5181 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5182 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5184 /* Write the contents to the file. */
5185 pos
= obj_sym_filepos (output_bfd
);
5186 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5187 size
= 2 * bfd_coff_symesz (output_bfd
);
5188 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5189 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
5191 obj_raw_syment_count (output_bfd
) += 2;
5196 /* Write out a non-XCOFF global symbol. */
5199 xcoff_write_global_symbol (struct bfd_hash_entry
*bh
, void * inf
)
5201 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) bh
;
5202 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
5205 struct internal_syment isym
;
5206 union internal_auxent aux
;
5211 output_bfd
= flinfo
->output_bfd
;
5212 outsym
= flinfo
->outsyms
;
5214 if (h
->root
.type
== bfd_link_hash_warning
)
5216 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5217 if (h
->root
.type
== bfd_link_hash_new
)
5221 /* If this symbol was garbage collected, just skip it. */
5222 if (xcoff_hash_table (flinfo
->info
)->gc
5223 && (h
->flags
& XCOFF_MARK
) == 0)
5226 /* If we need a .loader section entry, write it out. */
5227 if (h
->ldsym
!= NULL
)
5229 struct internal_ldsym
*ldsym
;
5234 if (h
->root
.type
== bfd_link_hash_undefined
5235 || h
->root
.type
== bfd_link_hash_undefweak
)
5239 ldsym
->l_scnum
= N_UNDEF
;
5240 ldsym
->l_smtype
= XTY_ER
;
5241 impbfd
= h
->root
.u
.undef
.abfd
;
5244 else if (h
->root
.type
== bfd_link_hash_defined
5245 || h
->root
.type
== bfd_link_hash_defweak
)
5249 sec
= h
->root
.u
.def
.section
;
5250 ldsym
->l_value
= (sec
->output_section
->vma
5251 + sec
->output_offset
5252 + h
->root
.u
.def
.value
);
5253 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5254 ldsym
->l_smtype
= XTY_SD
;
5255 impbfd
= sec
->owner
;
5261 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5262 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5263 || (h
->flags
& XCOFF_IMPORT
) != 0)
5265 Import symbols are defined so the check above will make
5266 the l_smtype XTY_SD. But this is not correct, it should
5268 ldsym
->l_smtype
|= L_IMPORT
;
5270 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5271 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5272 || (h
->flags
& XCOFF_EXPORT
) != 0)
5273 ldsym
->l_smtype
|= L_EXPORT
;
5275 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5276 ldsym
->l_smtype
|= L_ENTRY
;
5278 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5279 ldsym
->l_smtype
= XTY_SD
;
5281 ldsym
->l_smclas
= h
->smclas
;
5283 if (ldsym
->l_smtype
& L_IMPORT
)
5285 if ((h
->root
.type
== bfd_link_hash_defined
5286 || h
->root
.type
== bfd_link_hash_defweak
)
5287 && (h
->root
.u
.def
.value
!= 0))
5288 ldsym
->l_smclas
= XMC_XO
;
5290 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5291 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5292 ldsym
->l_smclas
= XMC_SV3264
;
5294 else if (h
->flags
& XCOFF_SYSCALL32
)
5295 ldsym
->l_smclas
= XMC_SV
;
5297 else if (h
->flags
& XCOFF_SYSCALL64
)
5298 ldsym
->l_smclas
= XMC_SV64
;
5301 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5305 else if (ldsym
->l_ifile
== 0)
5307 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5309 else if (impbfd
== NULL
)
5313 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5314 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5320 BFD_ASSERT (h
->ldindx
>= 0);
5322 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5325 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
5329 /* If this symbol needs global linkage code, write it out. */
5330 if (h
->root
.type
== bfd_link_hash_defined
5331 && (h
->root
.u
.def
.section
5332 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
5338 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5340 /* The first instruction in the global linkage code loads a
5341 specific TOC element. */
5342 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5343 + h
->descriptor
->toc_section
->output_offset
5344 - xcoff_data (output_bfd
)->toc
);
5346 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5347 tocoff
+= h
->descriptor
->u
.toc_offset
;
5349 /* The first instruction in the glink code needs to be
5350 cooked to hold the correct offset in the toc. The
5351 rest are just output raw. */
5352 bfd_put_32 (output_bfd
,
5353 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5355 /* Start with i == 1 to get past the first instruction done above
5356 The /4 is because the glink code is in bytes and we are going
5358 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5359 bfd_put_32 (output_bfd
,
5360 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5364 /* If we created a TOC entry for this symbol, write out the required
5366 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5371 struct internal_reloc
*irel
;
5372 struct internal_syment irsym
;
5373 union internal_auxent iraux
;
5375 tocsec
= h
->toc_section
;
5376 osec
= tocsec
->output_section
;
5377 oindx
= osec
->target_index
;
5378 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5379 irel
->r_vaddr
= (osec
->vma
5380 + tocsec
->output_offset
5384 irel
->r_symndx
= h
->indx
;
5388 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5391 BFD_ASSERT (h
->ldindx
>= 0);
5393 /* Initialize the aux union here instead of closer to when it is
5394 written out below because the length of the csect depends on
5395 whether the output is 32 or 64 bit. */
5396 memset (&iraux
, 0, sizeof iraux
);
5397 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5398 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5399 iraux
.x_csect
.x_smclas
= XMC_TC
;
5401 /* 32 bit uses a 32 bit R_POS to do the relocations
5402 64 bit uses a 64 bit R_POS to do the relocations
5404 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5406 Which one is determined by the backend. */
5407 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5410 iraux
.x_csect
.x_scnlen
.l
= 8;
5412 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5415 iraux
.x_csect
.x_scnlen
.l
= 4;
5420 irel
->r_type
= R_POS
;
5421 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5422 ++osec
->reloc_count
;
5424 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5425 output_bfd
, irel
, NULL
, h
))
5428 /* We need to emit a symbol to define a csect which holds
5430 if (flinfo
->info
->strip
!= strip_all
)
5432 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
,
5434 &irsym
, h
->root
.root
.string
);
5438 irsym
.n_value
= irel
->r_vaddr
;
5439 irsym
.n_scnum
= osec
->target_index
;
5440 irsym
.n_sclass
= C_HIDEXT
;
5441 irsym
.n_type
= T_NULL
;
5444 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5445 outsym
+= bfd_coff_symesz (output_bfd
);
5447 /* Note : iraux is initialized above. */
5448 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5449 0, 1, (void *) outsym
);
5450 outsym
+= bfd_coff_auxesz (output_bfd
);
5454 /* We aren't going to write out the symbols below, so we
5455 need to write them out now. */
5456 pos
= obj_sym_filepos (output_bfd
);
5457 pos
+= (obj_raw_syment_count (output_bfd
)
5458 * bfd_coff_symesz (output_bfd
));
5459 amt
= outsym
- flinfo
->outsyms
;
5460 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5461 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5463 obj_raw_syment_count (output_bfd
) +=
5464 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5466 outsym
= flinfo
->outsyms
;
5471 /* If this symbol is a specially defined function descriptor, write
5472 it out. The first word is the address of the function code
5473 itself, the second word is the address of the TOC, and the third
5477 The addresses for the 32 bit will take 4 bytes and the addresses
5478 for 64 bit will take 8 bytes. Similar for the relocs. This type
5479 of logic was also done above to create a TOC entry in
5480 xcoff_write_global_symbol. */
5481 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5482 && h
->root
.type
== bfd_link_hash_defined
5483 && (h
->root
.u
.def
.section
5484 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
5490 struct xcoff_link_hash_entry
*hentry
;
5492 struct internal_reloc
*irel
;
5494 unsigned int reloc_size
, byte_size
;
5496 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5501 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5509 sec
= h
->root
.u
.def
.section
;
5510 osec
= sec
->output_section
;
5511 oindx
= osec
->target_index
;
5512 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5514 hentry
= h
->descriptor
;
5515 BFD_ASSERT (hentry
!= NULL
5516 && (hentry
->root
.type
== bfd_link_hash_defined
5517 || hentry
->root
.type
== bfd_link_hash_defweak
));
5518 esec
= hentry
->root
.u
.def
.section
;
5520 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5521 irel
->r_vaddr
= (osec
->vma
5522 + sec
->output_offset
5523 + h
->root
.u
.def
.value
);
5524 irel
->r_symndx
= esec
->output_section
->target_index
;
5525 irel
->r_type
= R_POS
;
5526 irel
->r_size
= reloc_size
;
5527 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5528 ++osec
->reloc_count
;
5530 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5531 output_bfd
, irel
, esec
, NULL
))
5534 /* There are three items to write out,
5535 the address of the code
5536 the address of the toc anchor
5537 the environment pointer.
5538 We are ignoring the environment pointer. So set it to zero. */
5539 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5541 bfd_put_64 (output_bfd
,
5542 (esec
->output_section
->vma
+ esec
->output_offset
5543 + hentry
->root
.u
.def
.value
),
5545 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5546 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5551 This logic was already called above so the error case where
5552 the backend is neither has already been checked. */
5553 bfd_put_32 (output_bfd
,
5554 (esec
->output_section
->vma
+ esec
->output_offset
5555 + hentry
->root
.u
.def
.value
),
5557 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5558 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5561 tsec
= coff_section_from_bfd_index (output_bfd
,
5562 xcoff_data (output_bfd
)->sntoc
);
5565 irel
->r_vaddr
= (osec
->vma
5566 + sec
->output_offset
5567 + h
->root
.u
.def
.value
5569 irel
->r_symndx
= tsec
->output_section
->target_index
;
5570 irel
->r_type
= R_POS
;
5571 irel
->r_size
= reloc_size
;
5572 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5573 ++osec
->reloc_count
;
5575 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5576 output_bfd
, irel
, tsec
, NULL
))
5580 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
5582 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5587 && (flinfo
->info
->strip
== strip_all
5588 || (flinfo
->info
->strip
== strip_some
5589 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
5590 false, false) == NULL
)))
5592 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5597 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5599 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5603 memset (&aux
, 0, sizeof aux
);
5605 h
->indx
= obj_raw_syment_count (output_bfd
);
5607 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5608 &isym
, h
->root
.root
.string
);
5612 if (h
->root
.type
== bfd_link_hash_undefined
5613 || h
->root
.type
== bfd_link_hash_undefweak
)
5616 isym
.n_scnum
= N_UNDEF
;
5617 if (h
->root
.type
== bfd_link_hash_undefweak
5618 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5619 isym
.n_sclass
= C_WEAKEXT
;
5621 isym
.n_sclass
= C_EXT
;
5622 aux
.x_csect
.x_smtyp
= XTY_ER
;
5624 else if ((h
->root
.type
== bfd_link_hash_defined
5625 || h
->root
.type
== bfd_link_hash_defweak
)
5626 && h
->smclas
== XMC_XO
)
5628 BFD_ASSERT (bfd_is_abs_symbol (&h
->root
));
5629 isym
.n_value
= h
->root
.u
.def
.value
;
5630 isym
.n_scnum
= N_UNDEF
;
5631 if (h
->root
.type
== bfd_link_hash_defweak
5632 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5633 isym
.n_sclass
= C_WEAKEXT
;
5635 isym
.n_sclass
= C_EXT
;
5636 aux
.x_csect
.x_smtyp
= XTY_ER
;
5638 else if (h
->root
.type
== bfd_link_hash_defined
5639 || h
->root
.type
== bfd_link_hash_defweak
)
5641 struct xcoff_link_size_list
*l
;
5643 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5644 + h
->root
.u
.def
.section
->output_offset
5645 + h
->root
.u
.def
.value
);
5646 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5647 isym
.n_scnum
= N_ABS
;
5649 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5650 isym
.n_sclass
= C_HIDEXT
;
5651 aux
.x_csect
.x_smtyp
= XTY_SD
;
5653 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5655 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
5661 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5667 else if (h
->root
.type
== bfd_link_hash_common
)
5669 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5670 + h
->root
.u
.c
.p
->section
->output_offset
);
5671 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5672 isym
.n_sclass
= C_EXT
;
5673 aux
.x_csect
.x_smtyp
= XTY_CM
;
5674 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5679 isym
.n_type
= T_NULL
;
5682 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5683 outsym
+= bfd_coff_symesz (output_bfd
);
5685 aux
.x_csect
.x_smclas
= h
->smclas
;
5686 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5688 outsym
+= bfd_coff_auxesz (output_bfd
);
5690 if ((h
->root
.type
== bfd_link_hash_defined
5691 || h
->root
.type
== bfd_link_hash_defweak
)
5692 && h
->smclas
!= XMC_XO
)
5694 /* We just output an SD symbol. Now output an LD symbol. */
5697 if (h
->root
.type
== bfd_link_hash_defweak
5698 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5699 isym
.n_sclass
= C_WEAKEXT
;
5701 isym
.n_sclass
= C_EXT
;
5702 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5703 outsym
+= bfd_coff_symesz (output_bfd
);
5705 aux
.x_csect
.x_smtyp
= XTY_LD
;
5706 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5707 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5709 outsym
+= bfd_coff_auxesz (output_bfd
);
5712 pos
= obj_sym_filepos (output_bfd
);
5713 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5714 amt
= outsym
- flinfo
->outsyms
;
5715 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5716 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5718 obj_raw_syment_count (output_bfd
) +=
5719 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5724 /* Handle a link order which is supposed to generate a reloc. */
5727 xcoff_reloc_link_order (bfd
*output_bfd
,
5728 struct xcoff_final_link_info
*flinfo
,
5729 asection
*output_section
,
5730 struct bfd_link_order
*link_order
)
5732 reloc_howto_type
*howto
;
5733 struct xcoff_link_hash_entry
*h
;
5737 struct internal_reloc
*irel
;
5738 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5740 if (link_order
->type
== bfd_section_reloc_link_order
)
5741 /* We need to somehow locate a symbol in the right section. The
5742 symbol must either have a value of zero, or we must adjust
5743 the addend by the value of the symbol. FIXME: Write this
5744 when we need it. The old linker couldn't handle this anyhow. */
5747 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5750 bfd_set_error (bfd_error_bad_value
);
5754 h
= ((struct xcoff_link_hash_entry
*)
5755 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
5756 link_order
->u
.reloc
.p
->u
.name
,
5757 false, false, true));
5760 (*flinfo
->info
->callbacks
->unattached_reloc
)
5761 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
5765 hsec
= xcoff_symbol_section (h
);
5766 if (h
->root
.type
== bfd_link_hash_defined
5767 || h
->root
.type
== bfd_link_hash_defweak
)
5768 hval
= h
->root
.u
.def
.value
;
5772 addend
= link_order
->u
.reloc
.p
->addend
;
5774 addend
+= (hsec
->output_section
->vma
5775 + hsec
->output_offset
5782 bfd_reloc_status_type rstat
;
5785 size
= bfd_get_reloc_size (howto
);
5786 buf
= bfd_zmalloc (size
);
5787 if (buf
== NULL
&& size
!= 0)
5790 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5796 case bfd_reloc_outofrange
:
5798 case bfd_reloc_overflow
:
5799 (*flinfo
->info
->callbacks
->reloc_overflow
)
5800 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5801 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0);
5804 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5805 (file_ptr
) link_order
->offset
, size
);
5811 /* Store the reloc information in the right place. It will get
5812 swapped and written out at the end of the final_link routine. */
5813 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
5814 + output_section
->reloc_count
);
5815 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
5816 + output_section
->reloc_count
);
5818 memset (irel
, 0, sizeof (struct internal_reloc
));
5819 *rel_hash_ptr
= NULL
;
5821 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5824 irel
->r_symndx
= h
->indx
;
5827 /* Set the index to -2 to force this symbol to get written out. */
5833 irel
->r_type
= howto
->type
;
5834 irel
->r_size
= howto
->bitsize
- 1;
5835 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5836 irel
->r_size
|= 0x80;
5838 ++output_section
->reloc_count
;
5840 /* Now output the reloc to the .loader section. */
5841 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
5843 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
5844 output_bfd
, irel
, hsec
, h
))
5851 /* Do the final link step. */
5854 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5856 bfd_size_type symesz
;
5857 struct xcoff_final_link_info flinfo
;
5859 struct bfd_link_order
*p
;
5860 bfd_size_type max_contents_size
;
5861 bfd_size_type max_sym_count
;
5862 bfd_size_type max_lineno_count
;
5863 bfd_size_type max_reloc_count
;
5864 bfd_size_type max_output_reloc_count
;
5865 file_ptr rel_filepos
;
5867 file_ptr line_filepos
;
5868 unsigned int linesz
;
5870 bfd_byte
*external_relocs
= NULL
;
5871 char strbuf
[STRING_SIZE_SIZE
];
5875 if (bfd_link_pic (info
))
5876 abfd
->flags
|= DYNAMIC
;
5878 symesz
= bfd_coff_symesz (abfd
);
5881 flinfo
.output_bfd
= abfd
;
5882 flinfo
.strtab
= NULL
;
5883 flinfo
.section_info
= NULL
;
5884 flinfo
.last_file_index
= -1;
5885 flinfo
.toc_symindx
= -1;
5886 flinfo
.internal_syms
= NULL
;
5887 flinfo
.sym_indices
= NULL
;
5888 flinfo
.outsyms
= NULL
;
5889 flinfo
.linenos
= NULL
;
5890 flinfo
.contents
= NULL
;
5891 flinfo
.external_relocs
= NULL
;
5893 if (xcoff_hash_table (info
)->loader_section
)
5895 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5896 + bfd_xcoff_ldhdrsz (abfd
));
5897 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5898 + bfd_xcoff_ldhdrsz (abfd
)
5899 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5900 * bfd_xcoff_ldsymsz (abfd
)));
5904 flinfo
.ldsym
= NULL
;
5905 flinfo
.ldrel
= NULL
;
5908 xcoff_data (abfd
)->coff
.link_info
= info
;
5910 flinfo
.strtab
= _bfd_stringtab_init ();
5911 if (flinfo
.strtab
== NULL
)
5914 /* Count the relocation entries required for the output file.
5915 (We've already counted the line numbers.) Determine a few
5917 max_contents_size
= 0;
5918 max_lineno_count
= 0;
5919 max_reloc_count
= 0;
5920 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5923 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5925 if (p
->type
== bfd_indirect_link_order
)
5929 sec
= p
->u
.indirect
.section
;
5931 /* Mark all sections which are to be included in the
5932 link. This will normally be every section. We need
5933 to do this so that we can identify any sections which
5934 the linker has decided to not include. */
5935 sec
->linker_mark
= true;
5937 o
->reloc_count
+= sec
->reloc_count
;
5939 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
5941 if (sec
->rawsize
> max_contents_size
)
5942 max_contents_size
= sec
->rawsize
;
5943 if (sec
->size
> max_contents_size
)
5944 max_contents_size
= sec
->size
;
5946 if (coff_section_data (sec
->owner
, sec
) != NULL
5947 && xcoff_section_data (sec
->owner
, sec
) != NULL
5948 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5949 > max_lineno_count
))
5951 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5952 if (sec
->reloc_count
> max_reloc_count
)
5953 max_reloc_count
= sec
->reloc_count
;
5955 else if (p
->type
== bfd_section_reloc_link_order
5956 || p
->type
== bfd_symbol_reloc_link_order
)
5961 /* Compute the file positions for all the sections. */
5962 if (abfd
->output_has_begun
)
5964 if (xcoff_hash_table (info
)->file_align
!= 0)
5971 file_align
= xcoff_hash_table (info
)->file_align
;
5972 if (file_align
!= 0)
5978 /* Insert .pad sections before every section which has
5979 contents and is loaded, if it is preceded by some other
5980 section which has contents and is loaded. */
5981 saw_contents
= true;
5982 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5984 if (strcmp (o
->name
, ".pad") == 0)
5985 saw_contents
= false;
5986 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5987 && (o
->flags
& SEC_LOAD
) != 0)
5990 saw_contents
= true;
5995 /* Create a pad section and place it before the section
5996 that needs padding. This requires unlinking and
5997 relinking the bfd's section list. */
5999 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
6001 n
->alignment_power
= 0;
6003 bfd_section_list_remove (abfd
, n
);
6004 bfd_section_list_insert_before (abfd
, o
, n
);
6005 saw_contents
= false;
6010 /* Reset the section indices after inserting the new
6013 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6016 o
->target_index
= indx
;
6018 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
6020 /* Work out appropriate sizes for the .pad sections to force
6021 each section to land on a page boundary. This bit of
6022 code knows what compute_section_file_positions is going
6024 sofar
= bfd_coff_filhsz (abfd
);
6025 sofar
+= bfd_coff_aoutsz (abfd
);
6026 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
6027 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6028 if ((bfd_xcoff_is_reloc_count_overflow
6029 (abfd
, (bfd_vma
) o
->reloc_count
))
6030 || (bfd_xcoff_is_lineno_count_overflow
6031 (abfd
, (bfd_vma
) o
->lineno_count
)))
6032 /* 64 does not overflow, need to check if 32 does */
6033 sofar
+= bfd_coff_scnhsz (abfd
);
6035 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6037 if (strcmp (o
->name
, ".pad") == 0)
6041 BFD_ASSERT (o
->size
== 0);
6042 pageoff
= sofar
& (file_align
- 1);
6045 o
->size
= file_align
- pageoff
;
6046 sofar
+= file_align
- pageoff
;
6047 o
->flags
|= SEC_HAS_CONTENTS
;
6052 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
6053 sofar
+= BFD_ALIGN (o
->size
,
6054 1 << o
->alignment_power
);
6059 if (! bfd_coff_compute_section_file_positions (abfd
))
6063 /* Allocate space for the pointers we need to keep for the relocs. */
6067 /* We use section_count + 1, rather than section_count, because
6068 the target_index fields are 1 based. */
6069 amt
= abfd
->section_count
+ 1;
6070 amt
*= sizeof (struct xcoff_link_section_info
);
6071 flinfo
.section_info
= bfd_malloc (amt
);
6072 if (flinfo
.section_info
== NULL
)
6074 for (i
= 0; i
<= abfd
->section_count
; i
++)
6076 flinfo
.section_info
[i
].relocs
= NULL
;
6077 flinfo
.section_info
[i
].rel_hashes
= NULL
;
6078 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6082 /* Set the file positions for the relocs. */
6083 rel_filepos
= obj_relocbase (abfd
);
6084 relsz
= bfd_coff_relsz (abfd
);
6085 max_output_reloc_count
= 0;
6086 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6088 if (o
->reloc_count
== 0)
6092 /* A stripped file has no relocs. However, we still
6093 allocate the buffers, so that later code doesn't have to
6094 worry about whether we are stripping or not. */
6095 if (info
->strip
== strip_all
)
6099 o
->flags
|= SEC_RELOC
;
6100 o
->rel_filepos
= rel_filepos
;
6101 rel_filepos
+= o
->reloc_count
* relsz
;
6104 /* We don't know the indices of global symbols until we have
6105 written out all the local symbols. For each section in
6106 the output file, we keep an array of pointers to hash
6107 table entries. Each entry in the array corresponds to a
6108 reloc. When we find a reloc against a global symbol, we
6109 set the corresponding entry in this array so that we can
6110 fix up the symbol index after we have written out all the
6113 Because of this problem, we also keep the relocs in
6114 memory until the end of the link. This wastes memory.
6115 We could backpatch the file later, I suppose, although it
6117 amt
= o
->reloc_count
;
6118 amt
*= sizeof (struct internal_reloc
);
6119 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6121 amt
= o
->reloc_count
;
6122 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6123 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6125 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
6126 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6129 if (o
->reloc_count
> max_output_reloc_count
)
6130 max_output_reloc_count
= o
->reloc_count
;
6134 /* We now know the size of the relocs, so we can determine the file
6135 positions of the line numbers. */
6136 line_filepos
= rel_filepos
;
6137 flinfo
.line_filepos
= line_filepos
;
6138 linesz
= bfd_coff_linesz (abfd
);
6139 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6141 if (o
->lineno_count
== 0)
6142 o
->line_filepos
= 0;
6145 o
->line_filepos
= line_filepos
;
6146 line_filepos
+= o
->lineno_count
* linesz
;
6149 /* Reset the reloc and lineno counts, so that we can use them to
6150 count the number of entries we have output so far. */
6152 o
->lineno_count
= 0;
6155 obj_sym_filepos (abfd
) = line_filepos
;
6157 /* Figure out the largest number of symbols in an input BFD. Take
6158 the opportunity to clear the output_has_begun fields of all the
6159 input BFD's. We want at least 6 symbols, since that is the
6160 number which xcoff_write_global_symbol may need. */
6162 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6166 sub
->output_has_begun
= false;
6167 sz
= obj_raw_syment_count (sub
);
6168 if (sz
> max_sym_count
)
6172 /* Allocate some buffers used while linking. */
6173 amt
= max_sym_count
* sizeof (struct internal_syment
);
6174 flinfo
.internal_syms
= bfd_malloc (amt
);
6176 amt
= max_sym_count
* sizeof (long);
6177 flinfo
.sym_indices
= bfd_malloc (amt
);
6179 amt
= (max_sym_count
+ 1) * symesz
;
6180 flinfo
.outsyms
= bfd_malloc (amt
);
6182 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6183 flinfo
.linenos
= bfd_malloc (amt
);
6185 amt
= max_contents_size
;
6186 flinfo
.contents
= bfd_malloc (amt
);
6188 amt
= max_reloc_count
* relsz
;
6189 flinfo
.external_relocs
= bfd_malloc (amt
);
6191 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6192 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6193 || flinfo
.outsyms
== NULL
6194 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
6195 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
6196 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6199 obj_raw_syment_count (abfd
) = 0;
6201 /* Find a TOC symbol, if we need one. */
6202 if (!xcoff_find_tc0 (abfd
, &flinfo
))
6205 /* We now know the position of everything in the file, except that
6206 we don't know the size of the symbol table and therefore we don't
6207 know where the string table starts. We just build the string
6208 table in memory as we go along. We process all the relocations
6209 for a single input file at once. */
6210 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6212 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6214 if (p
->type
== bfd_indirect_link_order
6215 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6217 sub
= p
->u
.indirect
.section
->owner
;
6218 if (! sub
->output_has_begun
)
6220 if (! xcoff_link_input_bfd (&flinfo
, sub
))
6222 sub
->output_has_begun
= true;
6225 else if (p
->type
== bfd_section_reloc_link_order
6226 || p
->type
== bfd_symbol_reloc_link_order
)
6228 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
6233 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6239 /* Free up the buffers used by xcoff_link_input_bfd. */
6240 free (flinfo
.internal_syms
);
6241 flinfo
.internal_syms
= NULL
;
6242 free (flinfo
.sym_indices
);
6243 flinfo
.sym_indices
= NULL
;
6244 free (flinfo
.linenos
);
6245 flinfo
.linenos
= NULL
;
6246 free (flinfo
.contents
);
6247 flinfo
.contents
= NULL
;
6248 free (flinfo
.external_relocs
);
6249 flinfo
.external_relocs
= NULL
;
6251 /* The value of the last C_FILE symbol is supposed to be -1. Write
6253 if (flinfo
.last_file_index
!= -1)
6255 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
6256 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
6257 (void *) flinfo
.outsyms
);
6258 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
6259 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6260 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
6264 /* Write out all the global symbols which do not come from XCOFF
6266 bfd_hash_traverse (&info
->hash
->table
, xcoff_write_global_symbol
, &flinfo
);
6268 free (flinfo
.outsyms
);
6269 flinfo
.outsyms
= NULL
;
6271 /* Now that we have written out all the global symbols, we know the
6272 symbol indices to use for relocs against them, and we can finally
6273 write out the relocs. */
6274 amt
= max_output_reloc_count
* relsz
;
6275 external_relocs
= bfd_malloc (amt
);
6276 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6279 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6281 struct internal_reloc
*irel
;
6282 struct internal_reloc
*irelend
;
6283 struct xcoff_link_hash_entry
**rel_hash
;
6284 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6286 bfd_size_type rel_size
;
6288 /* A stripped file has no relocs. */
6289 if (info
->strip
== strip_all
)
6295 if (o
->reloc_count
== 0)
6298 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6299 irelend
= irel
+ o
->reloc_count
;
6300 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
6301 for (; irel
< irelend
; irel
++, rel_hash
++)
6303 if (*rel_hash
!= NULL
)
6305 if ((*rel_hash
)->indx
< 0)
6307 (*info
->callbacks
->unattached_reloc
)
6308 (info
, (*rel_hash
)->root
.root
.string
,
6309 NULL
, o
, irel
->r_vaddr
);
6310 (*rel_hash
)->indx
= 0;
6312 irel
->r_symndx
= (*rel_hash
)->indx
;
6316 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6317 toc_rel_hash
!= NULL
;
6318 toc_rel_hash
= toc_rel_hash
->next
)
6320 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6322 (*info
->callbacks
->unattached_reloc
)
6323 (info
, toc_rel_hash
->h
->root
.root
.string
,
6324 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
);
6325 toc_rel_hash
->h
->u
.toc_indx
= 0;
6327 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6330 /* XCOFF requires that the relocs be sorted by address. We tend
6331 to produce them in the order in which their containing csects
6332 appear in the symbol table, which is not necessarily by
6333 address. So we sort them here. There may be a better way to
6335 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
6336 o
->reloc_count
, sizeof (struct internal_reloc
),
6339 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6340 irelend
= irel
+ o
->reloc_count
;
6341 erel
= external_relocs
;
6342 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6343 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6345 rel_size
= relsz
* o
->reloc_count
;
6346 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6347 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6351 free (external_relocs
);
6352 external_relocs
= NULL
;
6354 /* Free up the section information. */
6355 if (flinfo
.section_info
!= NULL
)
6359 for (i
= 0; i
< abfd
->section_count
; i
++)
6361 free (flinfo
.section_info
[i
].relocs
);
6362 free (flinfo
.section_info
[i
].rel_hashes
);
6364 free (flinfo
.section_info
);
6365 flinfo
.section_info
= NULL
;
6368 /* Write out the loader section contents. */
6369 o
= xcoff_hash_table (info
)->loader_section
;
6372 && o
->output_section
!= bfd_abs_section_ptr
)
6374 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
6375 == (xcoff_hash_table (info
)->loader_section
->contents
6376 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6377 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6378 (file_ptr
) o
->output_offset
, o
->size
))
6382 /* Write out the magic sections. */
6383 o
= xcoff_hash_table (info
)->linkage_section
;
6386 && o
->output_section
!= bfd_abs_section_ptr
6387 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6388 (file_ptr
) o
->output_offset
,
6391 o
= xcoff_hash_table (info
)->toc_section
;
6394 && o
->output_section
!= bfd_abs_section_ptr
6395 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6396 (file_ptr
) o
->output_offset
,
6399 o
= xcoff_hash_table (info
)->descriptor_section
;
6402 && o
->output_section
!= bfd_abs_section_ptr
6403 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6404 (file_ptr
) o
->output_offset
,
6408 /* Write out the string table. */
6409 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6410 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6413 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
6415 amt
= STRING_SIZE_SIZE
;
6416 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6418 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
6421 _bfd_stringtab_free (flinfo
.strtab
);
6423 /* Write out the debugging string table. */
6424 o
= xcoff_hash_table (info
)->debug_section
;
6427 && o
->output_section
!= bfd_abs_section_ptr
)
6429 struct bfd_strtab_hash
*debug_strtab
;
6431 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6432 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6433 >= _bfd_stringtab_size (debug_strtab
));
6434 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6435 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6437 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6441 /* Setting symcount to 0 will cause write_object_contents to
6442 not try to write out the symbols. */
6448 if (flinfo
.strtab
!= NULL
)
6449 _bfd_stringtab_free (flinfo
.strtab
);
6451 if (flinfo
.section_info
!= NULL
)
6455 for (i
= 0; i
< abfd
->section_count
; i
++)
6457 free (flinfo
.section_info
[i
].relocs
);
6458 free (flinfo
.section_info
[i
].rel_hashes
);
6460 free (flinfo
.section_info
);
6463 free (flinfo
.internal_syms
);
6464 free (flinfo
.sym_indices
);
6465 free (flinfo
.outsyms
);
6466 free (flinfo
.linenos
);
6467 free (flinfo
.contents
);
6468 free (flinfo
.external_relocs
);
6469 free (external_relocs
);