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
!= NULL
)
3997 xcoff_hash_table (info
)->debug_section
->size
=
3998 _bfd_stringtab_size (debug_strtab
);
4003 free (ldinfo
.strings
);
4004 free (debug_contents
);
4009 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
4014 struct bfd_in_memory
*bim
;
4016 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
4023 abfd
->link
.next
= 0;
4024 abfd
->format
= bfd_object
;
4025 abfd
->iostream
= (void *) bim
;
4026 abfd
->flags
= BFD_IN_MEMORY
;
4027 abfd
->iovec
= &_bfd_memory_iovec
;
4028 abfd
->direction
= write_direction
;
4032 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
4035 /* need to reset to unknown or it will not be read back in correctly */
4036 abfd
->format
= bfd_unknown
;
4037 abfd
->direction
= read_direction
;
4043 /* Return the section that defines H. Return null if no section does. */
4046 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
4048 switch (h
->root
.type
)
4050 case bfd_link_hash_defined
:
4051 case bfd_link_hash_defweak
:
4052 return h
->root
.u
.def
.section
;
4054 case bfd_link_hash_common
:
4055 return h
->root
.u
.c
.p
->section
;
4062 /* Add a .loader relocation for input relocation IREL. If the loader
4063 relocation should be against an output section, HSEC points to the
4064 input section that IREL is against, otherwise HSEC is null. H is the
4065 symbol that IREL is against, or null if it isn't against a global symbol.
4066 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4069 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
4070 asection
*output_section
, bfd
*reference_bfd
,
4071 struct internal_reloc
*irel
, asection
*hsec
,
4072 struct xcoff_link_hash_entry
*h
)
4074 struct internal_ldrel ldrel
;
4076 ldrel
.l_vaddr
= irel
->r_vaddr
;
4079 const char *secname
;
4081 secname
= hsec
->output_section
->name
;
4082 if (strcmp (secname
, ".text") == 0)
4084 else if (strcmp (secname
, ".data") == 0)
4086 else if (strcmp (secname
, ".bss") == 0)
4088 else if (strcmp (secname
, ".tdata") == 0)
4089 ldrel
.l_symndx
= -1;
4090 else if (strcmp (secname
, ".tbss") == 0)
4091 ldrel
.l_symndx
= -2;
4095 /* xgettext:c-format */
4096 (_("%pB: loader reloc in unrecognized section `%s'"),
4097 reference_bfd
, secname
);
4098 bfd_set_error (bfd_error_nonrepresentable_section
);
4107 /* xgettext:c-format */
4108 (_("%pB: `%s' in loader reloc but not loader sym"),
4109 reference_bfd
, h
->root
.root
.string
);
4110 bfd_set_error (bfd_error_bad_value
);
4113 ldrel
.l_symndx
= h
->ldindx
;
4116 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4118 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4119 ldrel
.l_rsecnm
= output_section
->target_index
;
4120 if (xcoff_hash_table (flinfo
->info
)->textro
4121 && strcmp (output_section
->name
, ".text") == 0)
4124 /* xgettext:c-format */
4125 (_("%pB: loader reloc in read-only section %pA"),
4126 reference_bfd
, output_section
);
4127 bfd_set_error (bfd_error_invalid_operation
);
4130 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
4131 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4135 /* Link an input file into the linker output file. This function
4136 handles all the sections and relocations of the input file at once. */
4139 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
4143 const char *strings
;
4144 bfd_size_type syment_base
;
4145 unsigned int n_tmask
;
4146 unsigned int n_btshft
;
4148 bfd_size_type isymesz
;
4149 bfd_size_type osymesz
;
4150 bfd_size_type linesz
;
4153 struct xcoff_link_hash_entry
**sym_hash
;
4154 struct internal_syment
*isymp
;
4156 unsigned int *lineno_counts
;
4159 unsigned long output_index
;
4166 /* We can just skip DYNAMIC files, unless this is a static link. */
4167 if ((input_bfd
->flags
& DYNAMIC
) != 0
4168 && ! flinfo
->info
->static_link
)
4171 /* Move all the symbols to the output file. */
4172 output_bfd
= flinfo
->output_bfd
;
4174 syment_base
= obj_raw_syment_count (output_bfd
);
4175 isymesz
= bfd_coff_symesz (input_bfd
);
4176 osymesz
= bfd_coff_symesz (output_bfd
);
4177 linesz
= bfd_coff_linesz (input_bfd
);
4178 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4180 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4181 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4183 /* Define macros so that ISFCN, et. al., macros work correctly. */
4184 #define N_TMASK n_tmask
4185 #define N_BTSHFT n_btshft
4188 if (! flinfo
->info
->keep_memory
)
4191 if (flinfo
->info
->traditional_format
)
4194 if (! _bfd_coff_get_external_symbols (input_bfd
))
4197 /* Make one pass over the symbols and assign indices to symbols that
4198 we have decided to keep. Also use create .loader symbol information
4199 and update information in hash table entries. */
4200 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4201 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4202 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4203 csectpp
= xcoff_data (input_bfd
)->csects
;
4204 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4205 isymp
= flinfo
->internal_syms
;
4206 indexp
= flinfo
->sym_indices
;
4207 output_index
= syment_base
;
4208 while (esym
< esym_end
)
4210 union internal_auxent aux
;
4214 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4216 /* Read in the csect information, if any. */
4217 if (CSECT_SYM_P (isymp
->n_sclass
))
4219 BFD_ASSERT (isymp
->n_numaux
> 0);
4220 bfd_coff_swap_aux_in (input_bfd
,
4221 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4222 isymp
->n_type
, isymp
->n_sclass
,
4223 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4226 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4229 /* If this symbol is in the .loader section, swap out the
4230 .loader symbol information. If this is an external symbol
4231 reference to a defined symbol, though, then wait until we get
4232 to the definition. */
4233 if (EXTERN_SYM_P (isymp
->n_sclass
)
4234 && *sym_hash
!= NULL
4235 && (*sym_hash
)->ldsym
!= NULL
4236 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4238 struct xcoff_link_hash_entry
*h
;
4239 struct internal_ldsym
*ldsym
;
4243 if (isymp
->n_scnum
> 0)
4245 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4246 ldsym
->l_value
= (isymp
->n_value
4247 + (*csectpp
)->output_section
->vma
4248 + (*csectpp
)->output_offset
4253 ldsym
->l_scnum
= isymp
->n_scnum
;
4254 ldsym
->l_value
= isymp
->n_value
;
4257 ldsym
->l_smtype
= smtyp
;
4258 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4259 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4260 || (h
->flags
& XCOFF_IMPORT
) != 0)
4261 ldsym
->l_smtype
|= L_IMPORT
;
4262 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4263 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4264 || (h
->flags
& XCOFF_EXPORT
) != 0)
4265 ldsym
->l_smtype
|= L_EXPORT
;
4266 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4267 ldsym
->l_smtype
|= L_ENTRY
;
4268 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4269 ldsym
->l_smtype
|= L_WEAK
;
4271 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4273 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4275 else if (ldsym
->l_ifile
== 0)
4277 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4283 if (h
->root
.type
== bfd_link_hash_defined
4284 || h
->root
.type
== bfd_link_hash_defweak
)
4285 impbfd
= h
->root
.u
.def
.section
->owner
;
4286 else if (h
->root
.type
== bfd_link_hash_undefined
4287 || h
->root
.type
== bfd_link_hash_undefweak
)
4288 impbfd
= h
->root
.u
.undef
.abfd
;
4296 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
4297 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4304 BFD_ASSERT (h
->ldindx
>= 0);
4305 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
4308 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
4311 /* Fill in snentry now that we know the target_index. */
4312 if ((h
->flags
& XCOFF_ENTRY
) != 0
4313 && (h
->root
.type
== bfd_link_hash_defined
4314 || h
->root
.type
== bfd_link_hash_defweak
))
4316 xcoff_data (output_bfd
)->snentry
=
4317 h
->root
.u
.def
.section
->output_section
->target_index
;
4321 add
= 1 + isymp
->n_numaux
;
4323 if (*debug_index
== -2)
4324 /* We've decided to strip this symbol. */
4328 /* Assign the next unused index to this symbol. */
4329 *indexp
= output_index
;
4331 if (EXTERN_SYM_P (isymp
->n_sclass
))
4333 BFD_ASSERT (*sym_hash
!= NULL
);
4334 (*sym_hash
)->indx
= output_index
;
4337 /* If this is a symbol in the TOC which we may have merged
4338 (class XMC_TC), remember the symbol index of the TOC
4340 if (isymp
->n_sclass
== C_HIDEXT
4341 && aux
.x_csect
.x_smclas
== XMC_TC
4342 && *sym_hash
!= NULL
)
4344 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4345 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4346 (*sym_hash
)->u
.toc_indx
= output_index
;
4349 output_index
+= add
;
4352 esym
+= add
* isymesz
;
4358 for (--add
; add
> 0; --add
)
4362 /* Now write out the symbols that we decided to keep. */
4364 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4365 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4366 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4367 isymp
= flinfo
->internal_syms
;
4368 indexp
= flinfo
->sym_indices
;
4369 csectpp
= xcoff_data (input_bfd
)->csects
;
4370 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4371 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4372 outsym
= flinfo
->outsyms
;
4375 while (esym
< esym_end
)
4379 add
= 1 + isymp
->n_numaux
;
4382 esym
+= add
* isymesz
;
4385 struct internal_syment isym
;
4388 /* Adjust the symbol in order to output it. */
4390 if (isym
._n
._n_n
._n_zeroes
== 0
4391 && isym
._n
._n_n
._n_offset
!= 0)
4393 /* This symbol has a long name. Enter it in the string
4394 table we are building. If *debug_index != -1, the
4395 name has already been entered in the .debug section. */
4396 if (*debug_index
>= 0)
4397 isym
._n
._n_n
._n_offset
= *debug_index
;
4403 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4407 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
4408 if (indx
== (bfd_size_type
) -1)
4410 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4414 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4415 multiple definition problems when linking a shared object
4416 statically. (The native linker doesn't enter __rtinit into
4417 the normal table at all, but having a local symbol can make
4418 the objdump output easier to read.) */
4419 if (isym
.n_sclass
== C_EXT
4421 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4422 isym
.n_sclass
= C_HIDEXT
;
4424 /* The value of a C_FILE symbol is the symbol index of the
4425 next C_FILE symbol. The value of the last C_FILE symbol
4426 is -1. We try to get this right, below, just before we
4427 write the symbols out, but in the general case we may
4428 have to write the symbol out twice. */
4429 if (isym
.n_sclass
== C_FILE
)
4431 if (flinfo
->last_file_index
!= -1
4432 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4434 /* We must correct the value of the last C_FILE entry. */
4435 flinfo
->last_file
.n_value
= *indexp
;
4436 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4438 /* The last C_FILE symbol is in this input file. */
4439 bfd_coff_swap_sym_out (output_bfd
,
4440 (void *) &flinfo
->last_file
,
4441 (void *) (flinfo
->outsyms
4442 + ((flinfo
->last_file_index
4448 /* We have already written out the last C_FILE
4449 symbol. We need to write it out again. We
4450 borrow *outsym temporarily. */
4453 bfd_coff_swap_sym_out (output_bfd
,
4454 (void *) &flinfo
->last_file
,
4457 pos
= obj_sym_filepos (output_bfd
);
4458 pos
+= flinfo
->last_file_index
* osymesz
;
4459 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4460 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4466 flinfo
->last_file_index
= *indexp
;
4467 flinfo
->last_file
= isym
;
4470 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4471 into the line numbers. We update the symbol values when
4472 we handle the line numbers. */
4473 if (isym
.n_sclass
== C_BINCL
4474 || isym
.n_sclass
== C_EINCL
)
4476 isym
.n_value
= flinfo
->line_filepos
;
4479 /* The value of a C_BSTAT symbol is the symbol table
4480 index of the containing csect. */
4481 else if (isym
.n_sclass
== C_BSTAT
)
4485 indx
= isym
.n_value
;
4486 if (indx
< obj_raw_syment_count (input_bfd
))
4490 symindx
= flinfo
->sym_indices
[indx
];
4494 isym
.n_value
= symindx
;
4497 else if (isym
.n_sclass
!= C_ESTAT
4498 && isym
.n_sclass
!= C_DECL
4499 && isym
.n_scnum
> 0)
4501 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4502 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4503 + (*csectpp
)->output_offset
4507 /* Output the symbol. */
4508 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4513 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4515 union internal_auxent aux
;
4517 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4518 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4521 if (isymp
->n_sclass
== C_FILE
)
4523 /* This is the file name (or some comment put in by
4524 the compiler). If it is long, we must put it in
4525 the string table. */
4526 if (aux
.x_file
.x_n
.x_zeroes
== 0
4527 && aux
.x_file
.x_n
.x_offset
!= 0)
4529 const char *filename
;
4532 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4533 >= STRING_SIZE_SIZE
);
4534 if (strings
== NULL
)
4536 strings
= _bfd_coff_read_string_table (input_bfd
);
4537 if (strings
== NULL
)
4540 if ((bfd_size_type
) aux
.x_file
.x_n
.x_offset
>= obj_coff_strings_len (input_bfd
))
4541 filename
= _("<corrupt>");
4543 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4544 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
4546 if (indx
== (bfd_size_type
) -1)
4548 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4551 else if (CSECT_SYM_P (isymp
->n_sclass
)
4552 && i
+ 1 == isymp
->n_numaux
)
4555 /* We don't support type checking. I don't know if
4557 aux
.x_csect
.x_parmhash
= 0;
4558 /* I don't think anybody uses these fields, but we'd
4559 better clobber them just in case. */
4560 aux
.x_csect
.x_stab
= 0;
4561 aux
.x_csect
.x_snstab
= 0;
4563 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4567 indx
= aux
.x_csect
.x_scnlen
.l
;
4568 if (indx
< obj_raw_syment_count (input_bfd
))
4572 symindx
= flinfo
->sym_indices
[indx
];
4575 aux
.x_csect
.x_scnlen
.l
= 0;
4579 aux
.x_csect
.x_scnlen
.l
= symindx
;
4584 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4588 if (ISFCN (isymp
->n_type
)
4589 || ISTAG (isymp
->n_sclass
)
4590 || isymp
->n_sclass
== C_BLOCK
4591 || isymp
->n_sclass
== C_FCN
)
4593 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4595 && indx
< obj_raw_syment_count (input_bfd
))
4597 /* We look forward through the symbol for
4598 the index of the next symbol we are going
4599 to include. I don't know if this is
4601 while (flinfo
->sym_indices
[indx
] < 0
4602 && indx
< obj_raw_syment_count (input_bfd
))
4604 if (indx
>= obj_raw_syment_count (input_bfd
))
4605 indx
= output_index
;
4607 indx
= flinfo
->sym_indices
[indx
];
4608 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4613 indx
= aux
.x_sym
.x_tagndx
.l
;
4614 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4618 symindx
= flinfo
->sym_indices
[indx
];
4620 aux
.x_sym
.x_tagndx
.l
= 0;
4622 aux
.x_sym
.x_tagndx
.l
= symindx
;
4627 /* Copy over the line numbers, unless we are stripping
4628 them. We do this on a symbol by symbol basis in
4629 order to more easily handle garbage collection. */
4630 if (CSECT_SYM_P (isymp
->n_sclass
)
4632 && isymp
->n_numaux
> 1
4633 && ISFCN (isymp
->n_type
)
4634 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4636 if (*lineno_counts
== 0)
4637 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4640 asection
*enclosing
;
4641 unsigned int enc_count
;
4642 bfd_signed_vma linoff
;
4643 struct internal_lineno lin
;
4650 /* Read in the enclosing section's line-number
4651 information, if we haven't already. */
4653 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4654 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4655 if (oline
!= enclosing
)
4657 pos
= enclosing
->line_filepos
;
4658 amt
= linesz
* enc_count
;
4659 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4660 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
4666 /* Copy across the first entry, adjusting its
4668 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4669 - enclosing
->line_filepos
);
4670 linp
= flinfo
->linenos
+ linoff
;
4671 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4672 lin
.l_addr
.l_symndx
= *indexp
;
4673 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4675 /* Copy the other entries, adjusting their addresses. */
4676 linpend
= linp
+ *lineno_counts
* linesz
;
4677 offset
= (o
->output_section
->vma
4680 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4682 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4683 lin
.l_addr
.l_paddr
+= offset
;
4684 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4687 /* Write out the entries we've just processed. */
4688 pos
= (o
->output_section
->line_filepos
4689 + o
->output_section
->lineno_count
* linesz
);
4690 amt
= linesz
* *lineno_counts
;
4691 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4692 || bfd_bwrite (flinfo
->linenos
+ linoff
,
4693 amt
, output_bfd
) != amt
)
4695 o
->output_section
->lineno_count
+= *lineno_counts
;
4697 /* Record the offset of the symbol's line numbers
4698 in the output file. */
4699 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4703 struct internal_syment
*iisp
, *iispend
;
4706 bfd_vma range_start
, range_end
;
4709 /* Update any C_BINCL or C_EINCL symbols
4710 that refer to a line number in the
4711 range we just output. */
4712 iisp
= flinfo
->internal_syms
;
4713 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4714 iindp
= flinfo
->sym_indices
;
4715 oos
= flinfo
->outsyms
;
4716 range_start
= enclosing
->line_filepos
+ linoff
;
4717 range_end
= range_start
+ *lineno_counts
* linesz
;
4718 while (iisp
< iispend
)
4721 && (iisp
->n_sclass
== C_BINCL
4722 || iisp
->n_sclass
== C_EINCL
)
4723 && iisp
->n_value
>= range_start
4724 && iisp
->n_value
< range_end
)
4726 struct internal_syment iis
;
4728 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4729 iis
.n_value
= (iisp
->n_value
4732 bfd_coff_swap_sym_out (output_bfd
,
4737 iiadd
= 1 + iisp
->n_numaux
;
4739 oos
+= iiadd
* osymesz
;
4747 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4748 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4759 lineno_counts
+= add
;
4763 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4764 symbol will be the first symbol in the next input file. In the
4765 normal case, this will save us from writing out the C_FILE symbol
4767 if (flinfo
->last_file_index
!= -1
4768 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4770 flinfo
->last_file
.n_value
= output_index
;
4771 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
4772 (void *) (flinfo
->outsyms
4773 + ((flinfo
->last_file_index
- syment_base
)
4777 /* Write the modified symbols to the output file. */
4778 if (outsym
> flinfo
->outsyms
)
4780 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4781 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
4782 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4783 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
4786 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4787 + (outsym
- flinfo
->outsyms
) / osymesz
)
4790 obj_raw_syment_count (output_bfd
) = output_index
;
4793 /* Don't let the linker relocation routines discard the symbols. */
4794 keep_syms
= obj_coff_keep_syms (input_bfd
);
4795 obj_coff_keep_syms (input_bfd
) = true;
4797 /* Relocate the contents of each section. */
4798 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4802 if (! o
->linker_mark
)
4803 /* This section was omitted from the link. */
4806 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4808 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4811 /* We have set filepos correctly for the sections we created to
4812 represent csects, so bfd_get_section_contents should work. */
4813 if (coff_section_data (input_bfd
, o
) != NULL
4814 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4815 contents
= coff_section_data (input_bfd
, o
)->contents
;
4818 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4819 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
4821 contents
= flinfo
->contents
;
4824 if ((o
->flags
& SEC_RELOC
) != 0)
4827 struct internal_reloc
*internal_relocs
;
4828 struct internal_reloc
*irel
;
4830 struct internal_reloc
*irelend
;
4831 struct xcoff_link_hash_entry
**rel_hash
;
4834 /* Read in the relocs. */
4835 target_index
= o
->output_section
->target_index
;
4836 internal_relocs
= (xcoff_read_internal_relocs
4837 (input_bfd
, o
, false, flinfo
->external_relocs
,
4839 (flinfo
->section_info
[target_index
].relocs
4840 + o
->output_section
->reloc_count
)));
4841 if (internal_relocs
== NULL
)
4844 /* Call processor specific code to relocate the section
4846 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
4850 flinfo
->internal_syms
,
4851 xcoff_data (input_bfd
)->csects
))
4854 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4855 irel
= internal_relocs
;
4856 irelend
= irel
+ o
->reloc_count
;
4857 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
4858 + o
->output_section
->reloc_count
);
4859 for (; irel
< irelend
; irel
++, rel_hash
++)
4861 struct xcoff_link_hash_entry
*h
= NULL
;
4865 /* Adjust the reloc address and symbol index. */
4867 irel
->r_vaddr
+= offset
;
4869 r_symndx
= irel
->r_symndx
;
4874 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4876 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
4879 && h
->smclas
!= XMC_TD
4880 && (irel
->r_type
== R_TOC
4881 || irel
->r_type
== R_GL
4882 || irel
->r_type
== R_TCL
4883 || irel
->r_type
== R_TRL
4884 || irel
->r_type
== R_TRLA
))
4886 /* This is a TOC relative reloc with a symbol
4887 attached. The symbol should be the one which
4888 this reloc is for. We want to make this
4889 reloc against the TOC address of the symbol,
4890 not the symbol itself. */
4891 BFD_ASSERT (h
->toc_section
!= NULL
);
4892 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4893 if (h
->u
.toc_indx
!= -1)
4894 irel
->r_symndx
= h
->u
.toc_indx
;
4897 struct xcoff_toc_rel_hash
*n
;
4898 struct xcoff_link_section_info
*si
;
4902 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
4905 si
= flinfo
->section_info
+ target_index
;
4906 n
->next
= si
->toc_rel_hashes
;
4909 si
->toc_rel_hashes
= n
;
4914 /* This is a global symbol. */
4916 irel
->r_symndx
= h
->indx
;
4919 /* This symbol is being written at the end
4920 of the file, and we do not yet know the
4921 symbol index. We save the pointer to the
4922 hash table entry in the rel_hash list.
4923 We set the indx field to -2 to indicate
4924 that this symbol must not be stripped. */
4933 indx
= flinfo
->sym_indices
[r_symndx
];
4937 struct internal_syment
*is
;
4939 /* Relocations against a TC0 TOC anchor are
4940 automatically transformed to be against
4941 the TOC anchor in the output file. */
4942 is
= flinfo
->internal_syms
+ r_symndx
;
4943 if (is
->n_sclass
== C_HIDEXT
4944 && is
->n_numaux
> 0)
4947 union internal_auxent aux
;
4951 obj_coff_external_syms (input_bfd
))
4952 + ((r_symndx
+ is
->n_numaux
)
4954 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4955 is
->n_type
, is
->n_sclass
,
4959 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4960 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4961 indx
= flinfo
->toc_symindx
;
4966 irel
->r_symndx
= indx
;
4970 struct internal_syment
*is
;
4973 char buf
[SYMNMLEN
+ 1];
4975 /* This reloc is against a symbol we are
4976 stripping. It would be possible to handle
4977 this case, but I don't think it's worth it. */
4978 is
= flinfo
->internal_syms
+ r_symndx
;
4980 if (is
->n_sclass
!= C_DWARF
)
4982 name
= (_bfd_coff_internal_syment_name
4983 (input_bfd
, is
, buf
));
4988 (*flinfo
->info
->callbacks
->unattached_reloc
)
4989 (flinfo
->info
, name
,
4990 input_bfd
, o
, irel
->r_vaddr
);
4996 if ((o
->flags
& SEC_DEBUGGING
) == 0
4997 && xcoff_need_ldrel_p (flinfo
->info
, irel
, h
, o
))
5004 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
5006 sec
= xcoff_symbol_section (h
);
5007 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
5008 o
->output_section
, input_bfd
,
5014 o
->output_section
->reloc_count
+= o
->reloc_count
;
5017 /* Write out the modified section contents. */
5018 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5019 contents
, (file_ptr
) o
->output_offset
,
5024 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5026 if (! flinfo
->info
->keep_memory
)
5028 if (! _bfd_coff_free_symbols (input_bfd
))
5035 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5042 /* Sort relocs by VMA. This is called via qsort. */
5045 xcoff_sort_relocs (const void * p1
, const void * p2
)
5047 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
5048 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
5050 if (r1
->r_vaddr
> r2
->r_vaddr
)
5052 else if (r1
->r_vaddr
< r2
->r_vaddr
)
5058 /* Return true if section SEC is a TOC section. */
5061 xcoff_toc_section_p (asection
*sec
)
5066 if (name
[0] == '.' && name
[1] == 't')
5070 if (name
[3] == '0' && name
[4] == 0)
5075 if (name
[2] == 'd' && name
[3] == 0)
5081 /* See if the link requires a TOC (it usually does!). If so, find a
5082 good place to put the TOC anchor csect, and write out the associated
5086 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
5088 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5092 struct internal_syment irsym
;
5093 union internal_auxent iraux
;
5097 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5098 index of a csect at the beginning of the TOC. */
5099 toc_start
= ~(bfd_vma
) 0;
5102 for (input_bfd
= flinfo
->info
->input_bfds
;
5104 input_bfd
= input_bfd
->link
.next
)
5105 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5106 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5108 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5109 if (toc_start
> start
)
5112 section_index
= sec
->output_section
->target_index
;
5115 end
= start
+ sec
->size
;
5120 /* There's no need for a TC0 symbol if we don't have a TOC. */
5121 if (toc_end
< toc_start
)
5123 xcoff_data (output_bfd
)->toc
= toc_start
;
5127 if (toc_end
- toc_start
< 0x8000)
5128 /* Every TOC csect can be accessed from TOC_START. */
5129 best_address
= toc_start
;
5132 /* Find the lowest TOC csect that is still within range of TOC_END. */
5133 best_address
= toc_end
;
5134 for (input_bfd
= flinfo
->info
->input_bfds
;
5136 input_bfd
= input_bfd
->link
.next
)
5137 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5138 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5140 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5141 if (start
< best_address
5142 && start
+ 0x8000 >= toc_end
)
5144 best_address
= start
;
5145 section_index
= sec
->output_section
->target_index
;
5149 /* Make sure that the start of the TOC is also within range. */
5150 if (best_address
> toc_start
+ 0x8000)
5153 (_("TOC overflow: %#" PRIx64
" > 0x10000; try -mminimal-toc "
5155 (uint64_t) (toc_end
- toc_start
));
5156 bfd_set_error (bfd_error_file_too_big
);
5161 /* Record the chosen TOC value. */
5162 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5163 xcoff_data (output_bfd
)->toc
= best_address
;
5164 xcoff_data (output_bfd
)->sntoc
= section_index
;
5166 /* Fill out the TC0 symbol. */
5167 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5170 irsym
.n_value
= best_address
;
5171 irsym
.n_scnum
= section_index
;
5172 irsym
.n_sclass
= C_HIDEXT
;
5173 irsym
.n_type
= T_NULL
;
5175 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
5177 /* Fill out the auxiliary csect information. */
5178 memset (&iraux
, 0, sizeof iraux
);
5179 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5180 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5181 iraux
.x_csect
.x_scnlen
.l
= 0;
5182 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5183 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5185 /* Write the contents to the file. */
5186 pos
= obj_sym_filepos (output_bfd
);
5187 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5188 size
= 2 * bfd_coff_symesz (output_bfd
);
5189 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5190 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
5192 obj_raw_syment_count (output_bfd
) += 2;
5197 /* Write out a non-XCOFF global symbol. */
5200 xcoff_write_global_symbol (struct bfd_hash_entry
*bh
, void * inf
)
5202 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) bh
;
5203 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
5206 struct internal_syment isym
;
5207 union internal_auxent aux
;
5212 output_bfd
= flinfo
->output_bfd
;
5213 outsym
= flinfo
->outsyms
;
5215 if (h
->root
.type
== bfd_link_hash_warning
)
5217 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5218 if (h
->root
.type
== bfd_link_hash_new
)
5222 /* If this symbol was garbage collected, just skip it. */
5223 if (xcoff_hash_table (flinfo
->info
)->gc
5224 && (h
->flags
& XCOFF_MARK
) == 0)
5227 /* If we need a .loader section entry, write it out. */
5228 if (h
->ldsym
!= NULL
)
5230 struct internal_ldsym
*ldsym
;
5235 if (h
->root
.type
== bfd_link_hash_undefined
5236 || h
->root
.type
== bfd_link_hash_undefweak
)
5240 ldsym
->l_scnum
= N_UNDEF
;
5241 ldsym
->l_smtype
= XTY_ER
;
5242 impbfd
= h
->root
.u
.undef
.abfd
;
5245 else if (h
->root
.type
== bfd_link_hash_defined
5246 || h
->root
.type
== bfd_link_hash_defweak
)
5250 sec
= h
->root
.u
.def
.section
;
5251 ldsym
->l_value
= (sec
->output_section
->vma
5252 + sec
->output_offset
5253 + h
->root
.u
.def
.value
);
5254 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5255 ldsym
->l_smtype
= XTY_SD
;
5256 impbfd
= sec
->owner
;
5262 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5263 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5264 || (h
->flags
& XCOFF_IMPORT
) != 0)
5266 Import symbols are defined so the check above will make
5267 the l_smtype XTY_SD. But this is not correct, it should
5269 ldsym
->l_smtype
|= L_IMPORT
;
5271 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5272 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5273 || (h
->flags
& XCOFF_EXPORT
) != 0)
5274 ldsym
->l_smtype
|= L_EXPORT
;
5276 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5277 ldsym
->l_smtype
|= L_ENTRY
;
5279 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5280 ldsym
->l_smtype
= XTY_SD
;
5282 ldsym
->l_smclas
= h
->smclas
;
5284 if (ldsym
->l_smtype
& L_IMPORT
)
5286 if ((h
->root
.type
== bfd_link_hash_defined
5287 || h
->root
.type
== bfd_link_hash_defweak
)
5288 && (h
->root
.u
.def
.value
!= 0))
5289 ldsym
->l_smclas
= XMC_XO
;
5291 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5292 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5293 ldsym
->l_smclas
= XMC_SV3264
;
5295 else if (h
->flags
& XCOFF_SYSCALL32
)
5296 ldsym
->l_smclas
= XMC_SV
;
5298 else if (h
->flags
& XCOFF_SYSCALL64
)
5299 ldsym
->l_smclas
= XMC_SV64
;
5302 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5306 else if (ldsym
->l_ifile
== 0)
5308 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5310 else if (impbfd
== NULL
)
5314 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5315 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5321 BFD_ASSERT (h
->ldindx
>= 0);
5323 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5326 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
5330 /* If this symbol needs global linkage code, write it out. */
5331 if (h
->root
.type
== bfd_link_hash_defined
5332 && (h
->root
.u
.def
.section
5333 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
5339 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5341 /* The first instruction in the global linkage code loads a
5342 specific TOC element. */
5343 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5344 + h
->descriptor
->toc_section
->output_offset
5345 - xcoff_data (output_bfd
)->toc
);
5347 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5348 tocoff
+= h
->descriptor
->u
.toc_offset
;
5350 /* The first instruction in the glink code needs to be
5351 cooked to hold the correct offset in the toc. The
5352 rest are just output raw. */
5353 bfd_put_32 (output_bfd
,
5354 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5356 /* Start with i == 1 to get past the first instruction done above
5357 The /4 is because the glink code is in bytes and we are going
5359 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5360 bfd_put_32 (output_bfd
,
5361 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5365 /* If we created a TOC entry for this symbol, write out the required
5367 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5372 struct internal_reloc
*irel
;
5373 struct internal_syment irsym
;
5374 union internal_auxent iraux
;
5376 tocsec
= h
->toc_section
;
5377 osec
= tocsec
->output_section
;
5378 oindx
= osec
->target_index
;
5379 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5380 irel
->r_vaddr
= (osec
->vma
5381 + tocsec
->output_offset
5385 irel
->r_symndx
= h
->indx
;
5389 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5392 BFD_ASSERT (h
->ldindx
>= 0);
5394 /* Initialize the aux union here instead of closer to when it is
5395 written out below because the length of the csect depends on
5396 whether the output is 32 or 64 bit. */
5397 memset (&iraux
, 0, sizeof iraux
);
5398 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5399 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5400 iraux
.x_csect
.x_smclas
= XMC_TC
;
5402 /* 32 bit uses a 32 bit R_POS to do the relocations
5403 64 bit uses a 64 bit R_POS to do the relocations
5405 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5407 Which one is determined by the backend. */
5408 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5411 iraux
.x_csect
.x_scnlen
.l
= 8;
5413 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5416 iraux
.x_csect
.x_scnlen
.l
= 4;
5421 irel
->r_type
= R_POS
;
5422 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5423 ++osec
->reloc_count
;
5425 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5426 output_bfd
, irel
, NULL
, h
))
5429 /* We need to emit a symbol to define a csect which holds
5431 if (flinfo
->info
->strip
!= strip_all
)
5433 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
,
5435 &irsym
, h
->root
.root
.string
);
5439 irsym
.n_value
= irel
->r_vaddr
;
5440 irsym
.n_scnum
= osec
->target_index
;
5441 irsym
.n_sclass
= C_HIDEXT
;
5442 irsym
.n_type
= T_NULL
;
5445 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5446 outsym
+= bfd_coff_symesz (output_bfd
);
5448 /* Note : iraux is initialized above. */
5449 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5450 0, 1, (void *) outsym
);
5451 outsym
+= bfd_coff_auxesz (output_bfd
);
5455 /* We aren't going to write out the symbols below, so we
5456 need to write them out now. */
5457 pos
= obj_sym_filepos (output_bfd
);
5458 pos
+= (obj_raw_syment_count (output_bfd
)
5459 * bfd_coff_symesz (output_bfd
));
5460 amt
= outsym
- flinfo
->outsyms
;
5461 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5462 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5464 obj_raw_syment_count (output_bfd
) +=
5465 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5467 outsym
= flinfo
->outsyms
;
5472 /* If this symbol is a specially defined function descriptor, write
5473 it out. The first word is the address of the function code
5474 itself, the second word is the address of the TOC, and the third
5478 The addresses for the 32 bit will take 4 bytes and the addresses
5479 for 64 bit will take 8 bytes. Similar for the relocs. This type
5480 of logic was also done above to create a TOC entry in
5481 xcoff_write_global_symbol. */
5482 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5483 && h
->root
.type
== bfd_link_hash_defined
5484 && (h
->root
.u
.def
.section
5485 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
5491 struct xcoff_link_hash_entry
*hentry
;
5493 struct internal_reloc
*irel
;
5495 unsigned int reloc_size
, byte_size
;
5497 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5502 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5510 sec
= h
->root
.u
.def
.section
;
5511 osec
= sec
->output_section
;
5512 oindx
= osec
->target_index
;
5513 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5515 hentry
= h
->descriptor
;
5516 BFD_ASSERT (hentry
!= NULL
5517 && (hentry
->root
.type
== bfd_link_hash_defined
5518 || hentry
->root
.type
== bfd_link_hash_defweak
));
5519 esec
= hentry
->root
.u
.def
.section
;
5521 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5522 irel
->r_vaddr
= (osec
->vma
5523 + sec
->output_offset
5524 + h
->root
.u
.def
.value
);
5525 irel
->r_symndx
= esec
->output_section
->target_index
;
5526 irel
->r_type
= R_POS
;
5527 irel
->r_size
= reloc_size
;
5528 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5529 ++osec
->reloc_count
;
5531 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5532 output_bfd
, irel
, esec
, NULL
))
5535 /* There are three items to write out,
5536 the address of the code
5537 the address of the toc anchor
5538 the environment pointer.
5539 We are ignoring the environment pointer. So set it to zero. */
5540 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5542 bfd_put_64 (output_bfd
,
5543 (esec
->output_section
->vma
+ esec
->output_offset
5544 + hentry
->root
.u
.def
.value
),
5546 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5547 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5552 This logic was already called above so the error case where
5553 the backend is neither has already been checked. */
5554 bfd_put_32 (output_bfd
,
5555 (esec
->output_section
->vma
+ esec
->output_offset
5556 + hentry
->root
.u
.def
.value
),
5558 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5559 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5562 tsec
= coff_section_from_bfd_index (output_bfd
,
5563 xcoff_data (output_bfd
)->sntoc
);
5566 irel
->r_vaddr
= (osec
->vma
5567 + sec
->output_offset
5568 + h
->root
.u
.def
.value
5570 irel
->r_symndx
= tsec
->output_section
->target_index
;
5571 irel
->r_type
= R_POS
;
5572 irel
->r_size
= reloc_size
;
5573 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5574 ++osec
->reloc_count
;
5576 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5577 output_bfd
, irel
, tsec
, NULL
))
5581 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
5583 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5588 && (flinfo
->info
->strip
== strip_all
5589 || (flinfo
->info
->strip
== strip_some
5590 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
5591 false, false) == NULL
)))
5593 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5598 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5600 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5604 memset (&aux
, 0, sizeof aux
);
5606 h
->indx
= obj_raw_syment_count (output_bfd
);
5608 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5609 &isym
, h
->root
.root
.string
);
5613 if (h
->root
.type
== bfd_link_hash_undefined
5614 || h
->root
.type
== bfd_link_hash_undefweak
)
5617 isym
.n_scnum
= N_UNDEF
;
5618 if (h
->root
.type
== bfd_link_hash_undefweak
5619 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5620 isym
.n_sclass
= C_WEAKEXT
;
5622 isym
.n_sclass
= C_EXT
;
5623 aux
.x_csect
.x_smtyp
= XTY_ER
;
5625 else if ((h
->root
.type
== bfd_link_hash_defined
5626 || h
->root
.type
== bfd_link_hash_defweak
)
5627 && h
->smclas
== XMC_XO
)
5629 BFD_ASSERT (bfd_is_abs_symbol (&h
->root
));
5630 isym
.n_value
= h
->root
.u
.def
.value
;
5631 isym
.n_scnum
= N_UNDEF
;
5632 if (h
->root
.type
== bfd_link_hash_defweak
5633 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5634 isym
.n_sclass
= C_WEAKEXT
;
5636 isym
.n_sclass
= C_EXT
;
5637 aux
.x_csect
.x_smtyp
= XTY_ER
;
5639 else if (h
->root
.type
== bfd_link_hash_defined
5640 || h
->root
.type
== bfd_link_hash_defweak
)
5642 struct xcoff_link_size_list
*l
;
5644 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5645 + h
->root
.u
.def
.section
->output_offset
5646 + h
->root
.u
.def
.value
);
5647 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5648 isym
.n_scnum
= N_ABS
;
5650 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5651 isym
.n_sclass
= C_HIDEXT
;
5652 aux
.x_csect
.x_smtyp
= XTY_SD
;
5654 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5656 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
5662 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5668 else if (h
->root
.type
== bfd_link_hash_common
)
5670 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5671 + h
->root
.u
.c
.p
->section
->output_offset
);
5672 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5673 isym
.n_sclass
= C_EXT
;
5674 aux
.x_csect
.x_smtyp
= XTY_CM
;
5675 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5680 isym
.n_type
= T_NULL
;
5683 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5684 outsym
+= bfd_coff_symesz (output_bfd
);
5686 aux
.x_csect
.x_smclas
= h
->smclas
;
5687 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5689 outsym
+= bfd_coff_auxesz (output_bfd
);
5691 if ((h
->root
.type
== bfd_link_hash_defined
5692 || h
->root
.type
== bfd_link_hash_defweak
)
5693 && h
->smclas
!= XMC_XO
)
5695 /* We just output an SD symbol. Now output an LD symbol. */
5698 if (h
->root
.type
== bfd_link_hash_defweak
5699 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5700 isym
.n_sclass
= C_WEAKEXT
;
5702 isym
.n_sclass
= C_EXT
;
5703 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5704 outsym
+= bfd_coff_symesz (output_bfd
);
5706 aux
.x_csect
.x_smtyp
= XTY_LD
;
5707 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5708 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5710 outsym
+= bfd_coff_auxesz (output_bfd
);
5713 pos
= obj_sym_filepos (output_bfd
);
5714 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5715 amt
= outsym
- flinfo
->outsyms
;
5716 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5717 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5719 obj_raw_syment_count (output_bfd
) +=
5720 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5725 /* Handle a link order which is supposed to generate a reloc. */
5728 xcoff_reloc_link_order (bfd
*output_bfd
,
5729 struct xcoff_final_link_info
*flinfo
,
5730 asection
*output_section
,
5731 struct bfd_link_order
*link_order
)
5733 reloc_howto_type
*howto
;
5734 struct xcoff_link_hash_entry
*h
;
5738 struct internal_reloc
*irel
;
5739 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5741 if (link_order
->type
== bfd_section_reloc_link_order
)
5742 /* We need to somehow locate a symbol in the right section. The
5743 symbol must either have a value of zero, or we must adjust
5744 the addend by the value of the symbol. FIXME: Write this
5745 when we need it. The old linker couldn't handle this anyhow. */
5748 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5751 bfd_set_error (bfd_error_bad_value
);
5755 h
= ((struct xcoff_link_hash_entry
*)
5756 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
5757 link_order
->u
.reloc
.p
->u
.name
,
5758 false, false, true));
5761 (*flinfo
->info
->callbacks
->unattached_reloc
)
5762 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
5766 hsec
= xcoff_symbol_section (h
);
5767 if (h
->root
.type
== bfd_link_hash_defined
5768 || h
->root
.type
== bfd_link_hash_defweak
)
5769 hval
= h
->root
.u
.def
.value
;
5773 addend
= link_order
->u
.reloc
.p
->addend
;
5775 addend
+= (hsec
->output_section
->vma
5776 + hsec
->output_offset
5783 bfd_reloc_status_type rstat
;
5786 size
= bfd_get_reloc_size (howto
);
5787 buf
= bfd_zmalloc (size
);
5788 if (buf
== NULL
&& size
!= 0)
5791 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5797 case bfd_reloc_outofrange
:
5799 case bfd_reloc_overflow
:
5800 (*flinfo
->info
->callbacks
->reloc_overflow
)
5801 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5802 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0);
5805 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5806 (file_ptr
) link_order
->offset
, size
);
5812 /* Store the reloc information in the right place. It will get
5813 swapped and written out at the end of the final_link routine. */
5814 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
5815 + output_section
->reloc_count
);
5816 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
5817 + output_section
->reloc_count
);
5819 memset (irel
, 0, sizeof (struct internal_reloc
));
5820 *rel_hash_ptr
= NULL
;
5822 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5825 irel
->r_symndx
= h
->indx
;
5828 /* Set the index to -2 to force this symbol to get written out. */
5834 irel
->r_type
= howto
->type
;
5835 irel
->r_size
= howto
->bitsize
- 1;
5836 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5837 irel
->r_size
|= 0x80;
5839 ++output_section
->reloc_count
;
5841 /* Now output the reloc to the .loader section. */
5842 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
5844 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
5845 output_bfd
, irel
, hsec
, h
))
5852 /* Do the final link step. */
5855 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5857 bfd_size_type symesz
;
5858 struct xcoff_final_link_info flinfo
;
5860 struct bfd_link_order
*p
;
5861 bfd_size_type max_contents_size
;
5862 bfd_size_type max_sym_count
;
5863 bfd_size_type max_lineno_count
;
5864 bfd_size_type max_reloc_count
;
5865 bfd_size_type max_output_reloc_count
;
5866 file_ptr rel_filepos
;
5868 file_ptr line_filepos
;
5869 unsigned int linesz
;
5871 bfd_byte
*external_relocs
= NULL
;
5872 char strbuf
[STRING_SIZE_SIZE
];
5876 if (bfd_link_pic (info
))
5877 abfd
->flags
|= DYNAMIC
;
5879 symesz
= bfd_coff_symesz (abfd
);
5882 flinfo
.output_bfd
= abfd
;
5883 flinfo
.strtab
= NULL
;
5884 flinfo
.section_info
= NULL
;
5885 flinfo
.last_file_index
= -1;
5886 flinfo
.toc_symindx
= -1;
5887 flinfo
.internal_syms
= NULL
;
5888 flinfo
.sym_indices
= NULL
;
5889 flinfo
.outsyms
= NULL
;
5890 flinfo
.linenos
= NULL
;
5891 flinfo
.contents
= NULL
;
5892 flinfo
.external_relocs
= NULL
;
5894 if (xcoff_hash_table (info
)->loader_section
)
5896 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5897 + bfd_xcoff_ldhdrsz (abfd
));
5898 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5899 + bfd_xcoff_ldhdrsz (abfd
)
5900 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5901 * bfd_xcoff_ldsymsz (abfd
)));
5905 flinfo
.ldsym
= NULL
;
5906 flinfo
.ldrel
= NULL
;
5909 xcoff_data (abfd
)->coff
.link_info
= info
;
5911 flinfo
.strtab
= _bfd_stringtab_init ();
5912 if (flinfo
.strtab
== NULL
)
5915 /* Count the relocation entries required for the output file.
5916 (We've already counted the line numbers.) Determine a few
5918 max_contents_size
= 0;
5919 max_lineno_count
= 0;
5920 max_reloc_count
= 0;
5921 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5924 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5926 if (p
->type
== bfd_indirect_link_order
)
5930 sec
= p
->u
.indirect
.section
;
5932 /* Mark all sections which are to be included in the
5933 link. This will normally be every section. We need
5934 to do this so that we can identify any sections which
5935 the linker has decided to not include. */
5936 sec
->linker_mark
= true;
5938 o
->reloc_count
+= sec
->reloc_count
;
5940 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
5942 if (sec
->rawsize
> max_contents_size
)
5943 max_contents_size
= sec
->rawsize
;
5944 if (sec
->size
> max_contents_size
)
5945 max_contents_size
= sec
->size
;
5947 if (coff_section_data (sec
->owner
, sec
) != NULL
5948 && xcoff_section_data (sec
->owner
, sec
) != NULL
5949 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5950 > max_lineno_count
))
5952 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5953 if (sec
->reloc_count
> max_reloc_count
)
5954 max_reloc_count
= sec
->reloc_count
;
5956 else if (p
->type
== bfd_section_reloc_link_order
5957 || p
->type
== bfd_symbol_reloc_link_order
)
5962 /* Compute the file positions for all the sections. */
5963 if (abfd
->output_has_begun
)
5965 if (xcoff_hash_table (info
)->file_align
!= 0)
5972 file_align
= xcoff_hash_table (info
)->file_align
;
5973 if (file_align
!= 0)
5979 /* Insert .pad sections before every section which has
5980 contents and is loaded, if it is preceded by some other
5981 section which has contents and is loaded. */
5982 saw_contents
= true;
5983 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5985 if (strcmp (o
->name
, ".pad") == 0)
5986 saw_contents
= false;
5987 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5988 && (o
->flags
& SEC_LOAD
) != 0)
5991 saw_contents
= true;
5996 /* Create a pad section and place it before the section
5997 that needs padding. This requires unlinking and
5998 relinking the bfd's section list. */
6000 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
6002 n
->alignment_power
= 0;
6004 bfd_section_list_remove (abfd
, n
);
6005 bfd_section_list_insert_before (abfd
, o
, n
);
6006 saw_contents
= false;
6011 /* Reset the section indices after inserting the new
6014 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6017 o
->target_index
= indx
;
6019 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
6021 /* Work out appropriate sizes for the .pad sections to force
6022 each section to land on a page boundary. This bit of
6023 code knows what compute_section_file_positions is going
6025 sofar
= bfd_coff_filhsz (abfd
);
6026 sofar
+= bfd_coff_aoutsz (abfd
);
6027 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
6028 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6029 if ((bfd_xcoff_is_reloc_count_overflow
6030 (abfd
, (bfd_vma
) o
->reloc_count
))
6031 || (bfd_xcoff_is_lineno_count_overflow
6032 (abfd
, (bfd_vma
) o
->lineno_count
)))
6033 /* 64 does not overflow, need to check if 32 does */
6034 sofar
+= bfd_coff_scnhsz (abfd
);
6036 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6038 if (strcmp (o
->name
, ".pad") == 0)
6042 BFD_ASSERT (o
->size
== 0);
6043 pageoff
= sofar
& (file_align
- 1);
6046 o
->size
= file_align
- pageoff
;
6047 sofar
+= file_align
- pageoff
;
6048 o
->flags
|= SEC_HAS_CONTENTS
;
6053 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
6054 sofar
+= BFD_ALIGN (o
->size
,
6055 1 << o
->alignment_power
);
6060 if (! bfd_coff_compute_section_file_positions (abfd
))
6064 /* Allocate space for the pointers we need to keep for the relocs. */
6068 /* We use section_count + 1, rather than section_count, because
6069 the target_index fields are 1 based. */
6070 amt
= abfd
->section_count
+ 1;
6071 amt
*= sizeof (struct xcoff_link_section_info
);
6072 flinfo
.section_info
= bfd_malloc (amt
);
6073 if (flinfo
.section_info
== NULL
)
6075 for (i
= 0; i
<= abfd
->section_count
; i
++)
6077 flinfo
.section_info
[i
].relocs
= NULL
;
6078 flinfo
.section_info
[i
].rel_hashes
= NULL
;
6079 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6083 /* Set the file positions for the relocs. */
6084 rel_filepos
= obj_relocbase (abfd
);
6085 relsz
= bfd_coff_relsz (abfd
);
6086 max_output_reloc_count
= 0;
6087 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6089 if (o
->reloc_count
== 0)
6093 /* A stripped file has no relocs. However, we still
6094 allocate the buffers, so that later code doesn't have to
6095 worry about whether we are stripping or not. */
6096 if (info
->strip
== strip_all
)
6100 o
->flags
|= SEC_RELOC
;
6101 o
->rel_filepos
= rel_filepos
;
6102 rel_filepos
+= o
->reloc_count
* relsz
;
6105 /* We don't know the indices of global symbols until we have
6106 written out all the local symbols. For each section in
6107 the output file, we keep an array of pointers to hash
6108 table entries. Each entry in the array corresponds to a
6109 reloc. When we find a reloc against a global symbol, we
6110 set the corresponding entry in this array so that we can
6111 fix up the symbol index after we have written out all the
6114 Because of this problem, we also keep the relocs in
6115 memory until the end of the link. This wastes memory.
6116 We could backpatch the file later, I suppose, although it
6118 amt
= o
->reloc_count
;
6119 amt
*= sizeof (struct internal_reloc
);
6120 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6122 amt
= o
->reloc_count
;
6123 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6124 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6126 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
6127 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6130 if (o
->reloc_count
> max_output_reloc_count
)
6131 max_output_reloc_count
= o
->reloc_count
;
6135 /* We now know the size of the relocs, so we can determine the file
6136 positions of the line numbers. */
6137 line_filepos
= rel_filepos
;
6138 flinfo
.line_filepos
= line_filepos
;
6139 linesz
= bfd_coff_linesz (abfd
);
6140 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6142 if (o
->lineno_count
== 0)
6143 o
->line_filepos
= 0;
6146 o
->line_filepos
= line_filepos
;
6147 line_filepos
+= o
->lineno_count
* linesz
;
6150 /* Reset the reloc and lineno counts, so that we can use them to
6151 count the number of entries we have output so far. */
6153 o
->lineno_count
= 0;
6156 obj_sym_filepos (abfd
) = line_filepos
;
6158 /* Figure out the largest number of symbols in an input BFD. Take
6159 the opportunity to clear the output_has_begun fields of all the
6160 input BFD's. We want at least 6 symbols, since that is the
6161 number which xcoff_write_global_symbol may need. */
6163 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6167 sub
->output_has_begun
= false;
6168 sz
= obj_raw_syment_count (sub
);
6169 if (sz
> max_sym_count
)
6173 /* Allocate some buffers used while linking. */
6174 amt
= max_sym_count
* sizeof (struct internal_syment
);
6175 flinfo
.internal_syms
= bfd_malloc (amt
);
6177 amt
= max_sym_count
* sizeof (long);
6178 flinfo
.sym_indices
= bfd_malloc (amt
);
6180 amt
= (max_sym_count
+ 1) * symesz
;
6181 flinfo
.outsyms
= bfd_malloc (amt
);
6183 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6184 flinfo
.linenos
= bfd_malloc (amt
);
6186 amt
= max_contents_size
;
6187 flinfo
.contents
= bfd_malloc (amt
);
6189 amt
= max_reloc_count
* relsz
;
6190 flinfo
.external_relocs
= bfd_malloc (amt
);
6192 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6193 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6194 || flinfo
.outsyms
== NULL
6195 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
6196 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
6197 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6200 obj_raw_syment_count (abfd
) = 0;
6202 /* Find a TOC symbol, if we need one. */
6203 if (!xcoff_find_tc0 (abfd
, &flinfo
))
6206 /* We now know the position of everything in the file, except that
6207 we don't know the size of the symbol table and therefore we don't
6208 know where the string table starts. We just build the string
6209 table in memory as we go along. We process all the relocations
6210 for a single input file at once. */
6211 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6213 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6215 if (p
->type
== bfd_indirect_link_order
6216 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6218 sub
= p
->u
.indirect
.section
->owner
;
6219 if (! sub
->output_has_begun
)
6221 if (! xcoff_link_input_bfd (&flinfo
, sub
))
6223 sub
->output_has_begun
= true;
6226 else if (p
->type
== bfd_section_reloc_link_order
6227 || p
->type
== bfd_symbol_reloc_link_order
)
6229 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
6234 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6240 /* Free up the buffers used by xcoff_link_input_bfd. */
6241 free (flinfo
.internal_syms
);
6242 flinfo
.internal_syms
= NULL
;
6243 free (flinfo
.sym_indices
);
6244 flinfo
.sym_indices
= NULL
;
6245 free (flinfo
.linenos
);
6246 flinfo
.linenos
= NULL
;
6247 free (flinfo
.contents
);
6248 flinfo
.contents
= NULL
;
6249 free (flinfo
.external_relocs
);
6250 flinfo
.external_relocs
= NULL
;
6252 /* The value of the last C_FILE symbol is supposed to be -1. Write
6254 if (flinfo
.last_file_index
!= -1)
6256 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
6257 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
6258 (void *) flinfo
.outsyms
);
6259 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
6260 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6261 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
6265 /* Write out all the global symbols which do not come from XCOFF
6267 bfd_hash_traverse (&info
->hash
->table
, xcoff_write_global_symbol
, &flinfo
);
6269 free (flinfo
.outsyms
);
6270 flinfo
.outsyms
= NULL
;
6272 /* Now that we have written out all the global symbols, we know the
6273 symbol indices to use for relocs against them, and we can finally
6274 write out the relocs. */
6275 amt
= max_output_reloc_count
* relsz
;
6276 external_relocs
= bfd_malloc (amt
);
6277 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6280 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6282 struct internal_reloc
*irel
;
6283 struct internal_reloc
*irelend
;
6284 struct xcoff_link_hash_entry
**rel_hash
;
6285 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6287 bfd_size_type rel_size
;
6289 /* A stripped file has no relocs. */
6290 if (info
->strip
== strip_all
)
6296 if (o
->reloc_count
== 0)
6299 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6300 irelend
= irel
+ o
->reloc_count
;
6301 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
6302 for (; irel
< irelend
; irel
++, rel_hash
++)
6304 if (*rel_hash
!= NULL
)
6306 if ((*rel_hash
)->indx
< 0)
6308 (*info
->callbacks
->unattached_reloc
)
6309 (info
, (*rel_hash
)->root
.root
.string
,
6310 NULL
, o
, irel
->r_vaddr
);
6311 (*rel_hash
)->indx
= 0;
6313 irel
->r_symndx
= (*rel_hash
)->indx
;
6317 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6318 toc_rel_hash
!= NULL
;
6319 toc_rel_hash
= toc_rel_hash
->next
)
6321 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6323 (*info
->callbacks
->unattached_reloc
)
6324 (info
, toc_rel_hash
->h
->root
.root
.string
,
6325 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
);
6326 toc_rel_hash
->h
->u
.toc_indx
= 0;
6328 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6331 /* XCOFF requires that the relocs be sorted by address. We tend
6332 to produce them in the order in which their containing csects
6333 appear in the symbol table, which is not necessarily by
6334 address. So we sort them here. There may be a better way to
6336 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
6337 o
->reloc_count
, sizeof (struct internal_reloc
),
6340 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6341 irelend
= irel
+ o
->reloc_count
;
6342 erel
= external_relocs
;
6343 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6344 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6346 rel_size
= relsz
* o
->reloc_count
;
6347 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6348 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6352 free (external_relocs
);
6353 external_relocs
= NULL
;
6355 /* Free up the section information. */
6356 if (flinfo
.section_info
!= NULL
)
6360 for (i
= 0; i
< abfd
->section_count
; i
++)
6362 free (flinfo
.section_info
[i
].relocs
);
6363 free (flinfo
.section_info
[i
].rel_hashes
);
6365 free (flinfo
.section_info
);
6366 flinfo
.section_info
= NULL
;
6369 /* Write out the loader section contents. */
6370 o
= xcoff_hash_table (info
)->loader_section
;
6373 && o
->output_section
!= bfd_abs_section_ptr
)
6375 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
6376 == (xcoff_hash_table (info
)->loader_section
->contents
6377 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6378 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6379 (file_ptr
) o
->output_offset
, o
->size
))
6383 /* Write out the magic sections. */
6384 o
= xcoff_hash_table (info
)->linkage_section
;
6387 && o
->output_section
!= bfd_abs_section_ptr
6388 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6389 (file_ptr
) o
->output_offset
,
6392 o
= xcoff_hash_table (info
)->toc_section
;
6395 && o
->output_section
!= bfd_abs_section_ptr
6396 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6397 (file_ptr
) o
->output_offset
,
6400 o
= xcoff_hash_table (info
)->descriptor_section
;
6403 && o
->output_section
!= bfd_abs_section_ptr
6404 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6405 (file_ptr
) o
->output_offset
,
6409 /* Write out the string table. */
6410 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6411 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6414 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
6416 amt
= STRING_SIZE_SIZE
;
6417 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6419 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
6422 _bfd_stringtab_free (flinfo
.strtab
);
6424 /* Write out the debugging string table. */
6425 o
= xcoff_hash_table (info
)->debug_section
;
6428 && o
->output_section
!= bfd_abs_section_ptr
)
6430 struct bfd_strtab_hash
*debug_strtab
;
6432 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6433 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6434 >= _bfd_stringtab_size (debug_strtab
));
6435 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6436 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6438 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6442 /* Setting symcount to 0 will cause write_object_contents to
6443 not try to write out the symbols. */
6449 if (flinfo
.strtab
!= NULL
)
6450 _bfd_stringtab_free (flinfo
.strtab
);
6452 if (flinfo
.section_info
!= NULL
)
6456 for (i
= 0; i
< abfd
->section_count
; i
++)
6458 free (flinfo
.section_info
[i
].relocs
);
6459 free (flinfo
.section_info
[i
].rel_hashes
);
6461 free (flinfo
.section_info
);
6464 free (flinfo
.internal_syms
);
6465 free (flinfo
.sym_indices
);
6466 free (flinfo
.outsyms
);
6467 free (flinfo
.linenos
);
6468 free (flinfo
.contents
);
6469 free (flinfo
.external_relocs
);
6470 free (external_relocs
);