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 /* The list of import files. */
40 struct xcoff_import_file
42 /* The next entry in the list. */
43 struct xcoff_import_file
*next
;
48 /* The member name. */
52 /* Information we keep for each section in the output file during the
55 struct xcoff_link_section_info
57 /* The relocs to be output. */
58 struct internal_reloc
*relocs
;
59 /* For each reloc against a global symbol whose index was not known
60 when the reloc was handled, the global hash table entry. */
61 struct xcoff_link_hash_entry
**rel_hashes
;
62 /* If there is a TOC relative reloc against a global symbol, and the
63 index of the TOC symbol is not known when the reloc was handled,
64 an entry is added to this linked list. This is not an array,
65 like rel_hashes, because this case is quite uncommon. */
66 struct xcoff_toc_rel_hash
68 struct xcoff_toc_rel_hash
*next
;
69 struct xcoff_link_hash_entry
*h
;
70 struct internal_reloc
*rel
;
74 /* Information that the XCOFF linker collects about an archive. */
75 struct xcoff_archive_info
77 /* The archive described by this entry. */
80 /* The import path and import filename to use when referring to
81 this archive in the .loader section. */
85 /* True if the archive contains a dynamic object. */
86 unsigned int contains_shared_object_p
: 1;
88 /* True if the previous field is valid. */
89 unsigned int know_contains_shared_object_p
: 1;
92 struct xcoff_link_hash_table
94 struct bfd_link_hash_table root
;
96 /* The .debug string hash table. We need to compute this while
97 reading the input files, so that we know how large the .debug
98 section will be before we assign section positions. */
99 struct bfd_strtab_hash
*debug_strtab
;
101 /* The .debug section we will use for the final output. */
102 asection
*debug_section
;
104 /* The .loader section we will use for the final output. */
105 asection
*loader_section
;
107 /* A count of non TOC relative relocs which will need to be
108 allocated in the .loader section. */
111 /* The .loader section header. */
112 struct internal_ldhdr ldhdr
;
114 /* The .gl section we use to hold global linkage code. */
115 asection
*linkage_section
;
117 /* The .tc section we use to hold toc entries we build for global
119 asection
*toc_section
;
121 /* The .ds section we use to hold function descriptors which we
122 create for exported symbols. */
123 asection
*descriptor_section
;
125 /* The list of import files. */
126 struct xcoff_import_file
*imports
;
128 /* Required alignment of sections within the output file. */
129 unsigned long file_align
;
131 /* Whether the .text section must be read-only. */
134 /* Whether -brtl was specified. */
137 /* Whether garbage collection was done. */
140 /* A linked list of symbols for which we have size information. */
141 struct xcoff_link_size_list
143 struct xcoff_link_size_list
*next
;
144 struct xcoff_link_hash_entry
*h
;
149 /* Information about archives. */
152 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
153 asection
*special_sections
[XCOFF_NUMBER_OF_SPECIAL_SECTIONS
];
156 /* Information that we pass around while doing the final link step. */
158 struct xcoff_final_link_info
160 /* General link information. */
161 struct bfd_link_info
*info
;
164 /* Hash table for long symbol names. */
165 struct bfd_strtab_hash
*strtab
;
166 /* Array of information kept for each output section, indexed by the
167 target_index field. */
168 struct xcoff_link_section_info
*section_info
;
169 /* Symbol index of last C_FILE symbol (-1 if none). */
170 long last_file_index
;
171 /* Contents of last C_FILE symbol. */
172 struct internal_syment last_file
;
173 /* Symbol index of TOC symbol. */
175 /* Start of .loader symbols. */
177 /* Next .loader reloc to swap out. */
179 /* File position of start of line numbers. */
180 file_ptr line_filepos
;
181 /* Buffer large enough to hold swapped symbols of any input file. */
182 struct internal_syment
*internal_syms
;
183 /* Buffer large enough to hold output indices of symbols of any
186 /* Buffer large enough to hold output symbols for any input file. */
188 /* Buffer large enough to hold external line numbers for any input
191 /* Buffer large enough to hold any input section. */
193 /* Buffer large enough to hold external relocs of any input section. */
194 bfd_byte
*external_relocs
;
197 static bool xcoff_mark (struct bfd_link_info
*, asection
*);
201 /* Routines to read XCOFF dynamic information. This don't really
202 belong here, but we already have the ldsym manipulation routines
205 /* Read the contents of a section. */
208 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
210 if (coff_section_data (abfd
, sec
) == NULL
)
212 size_t amt
= sizeof (struct coff_section_tdata
);
214 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
215 if (sec
->used_by_bfd
== NULL
)
219 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
223 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
228 coff_section_data (abfd
, sec
)->contents
= contents
;
234 /* Get the size required to hold the dynamic symbols. */
237 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
241 struct internal_ldhdr ldhdr
;
243 if ((abfd
->flags
& DYNAMIC
) == 0)
245 bfd_set_error (bfd_error_invalid_operation
);
249 lsec
= bfd_get_section_by_name (abfd
, ".loader");
252 bfd_set_error (bfd_error_no_symbols
);
256 if (! xcoff_get_section_contents (abfd
, lsec
))
258 contents
= coff_section_data (abfd
, lsec
)->contents
;
260 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
262 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
265 /* Get the dynamic symbols. */
268 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
272 struct internal_ldhdr ldhdr
;
274 bfd_byte
*elsym
, *elsymend
;
275 coff_symbol_type
*symbuf
;
277 if ((abfd
->flags
& DYNAMIC
) == 0)
279 bfd_set_error (bfd_error_invalid_operation
);
283 lsec
= bfd_get_section_by_name (abfd
, ".loader");
286 bfd_set_error (bfd_error_no_symbols
);
290 if (! xcoff_get_section_contents (abfd
, lsec
))
292 contents
= coff_section_data (abfd
, lsec
)->contents
;
294 coff_section_data (abfd
, lsec
)->keep_contents
= true;
296 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
298 strings
= (char *) contents
+ ldhdr
.l_stoff
;
300 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
304 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
306 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
307 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
309 struct internal_ldsym ldsym
;
311 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
313 symbuf
->symbol
.the_bfd
= abfd
;
315 if (ldsym
._l
._l_l
._l_zeroes
== 0)
316 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
321 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
324 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
326 symbuf
->symbol
.name
= c
;
329 if (ldsym
.l_smclas
== XMC_XO
)
330 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
332 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
334 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
336 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
337 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
339 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
340 symbuf
->symbol
.flags
|= BSF_WEAK
;
342 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
345 /* FIXME: We have no way to record the other information stored
346 with the loader symbol. */
347 *psyms
= (asymbol
*) symbuf
;
352 return ldhdr
.l_nsyms
;
355 /* Get the size required to hold the dynamic relocs. */
358 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
362 struct internal_ldhdr ldhdr
;
364 if ((abfd
->flags
& DYNAMIC
) == 0)
366 bfd_set_error (bfd_error_invalid_operation
);
370 lsec
= bfd_get_section_by_name (abfd
, ".loader");
373 bfd_set_error (bfd_error_no_symbols
);
377 if (! xcoff_get_section_contents (abfd
, lsec
))
379 contents
= coff_section_data (abfd
, lsec
)->contents
;
381 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
383 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
386 /* Get the dynamic relocs. */
389 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
395 struct internal_ldhdr ldhdr
;
397 bfd_byte
*elrel
, *elrelend
;
399 if ((abfd
->flags
& DYNAMIC
) == 0)
401 bfd_set_error (bfd_error_invalid_operation
);
405 lsec
= bfd_get_section_by_name (abfd
, ".loader");
408 bfd_set_error (bfd_error_no_symbols
);
412 if (! xcoff_get_section_contents (abfd
, lsec
))
414 contents
= coff_section_data (abfd
, lsec
)->contents
;
416 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
418 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
422 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
424 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
425 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
428 struct internal_ldrel ldrel
;
430 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
432 if (ldrel
.l_symndx
>= 3)
433 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
439 switch (ldrel
.l_symndx
)
455 sec
= bfd_get_section_by_name (abfd
, name
);
458 bfd_set_error (bfd_error_bad_value
);
462 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
465 relbuf
->address
= ldrel
.l_vaddr
;
468 /* Most dynamic relocs have the same type. FIXME: This is only
469 correct if ldrel.l_rtype == 0. In other cases, we should use
470 a different howto. */
471 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
473 /* FIXME: We have no way to record the l_rsecnm field. */
480 return ldhdr
.l_nreloc
;
483 /* Hash functions for xcoff_link_hash_table's archive_info. */
486 xcoff_archive_info_hash (const void *data
)
488 const struct xcoff_archive_info
*info
;
490 info
= (const struct xcoff_archive_info
*) data
;
491 return htab_hash_pointer (info
->archive
);
495 xcoff_archive_info_eq (const void *data1
, const void *data2
)
497 const struct xcoff_archive_info
*info1
;
498 const struct xcoff_archive_info
*info2
;
500 info1
= (const struct xcoff_archive_info
*) data1
;
501 info2
= (const struct xcoff_archive_info
*) data2
;
502 return info1
->archive
== info2
->archive
;
505 /* Return information about archive ARCHIVE. Return NULL on error. */
507 static struct xcoff_archive_info
*
508 xcoff_get_archive_info (struct bfd_link_info
*info
, bfd
*archive
)
510 struct xcoff_link_hash_table
*htab
;
511 struct xcoff_archive_info
*entryp
, entry
;
514 htab
= xcoff_hash_table (info
);
515 entry
.archive
= archive
;
516 slot
= htab_find_slot (htab
->archive_info
, &entry
, INSERT
);
523 entryp
= bfd_zalloc (info
->output_bfd
, sizeof (entry
));
527 entryp
->archive
= archive
;
533 /* Routine to create an entry in an XCOFF link hash table. */
535 static struct bfd_hash_entry
*
536 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
537 struct bfd_hash_table
*table
,
540 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
542 /* Allocate the structure if it has not already been allocated by a
545 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
549 /* Call the allocation method of the superclass. */
550 ret
= ((struct xcoff_link_hash_entry
*)
551 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
555 /* Set local fields. */
557 ret
->toc_section
= NULL
;
558 ret
->u
.toc_indx
= -1;
559 ret
->descriptor
= NULL
;
563 ret
->smclas
= XMC_UA
;
566 return (struct bfd_hash_entry
*) ret
;
569 /* Destroy an XCOFF link hash table. */
572 _bfd_xcoff_bfd_link_hash_table_free (bfd
*obfd
)
574 struct xcoff_link_hash_table
*ret
;
576 ret
= (struct xcoff_link_hash_table
*) obfd
->link
.hash
;
577 if (ret
->archive_info
)
578 htab_delete (ret
->archive_info
);
579 if (ret
->debug_strtab
)
580 _bfd_stringtab_free (ret
->debug_strtab
);
581 _bfd_generic_link_hash_table_free (obfd
);
584 /* Create an XCOFF link hash table. */
586 struct bfd_link_hash_table
*
587 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
589 struct xcoff_link_hash_table
*ret
;
590 bool isxcoff64
= false;
591 size_t amt
= sizeof (* ret
);
593 ret
= bfd_zmalloc (amt
);
596 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
597 sizeof (struct xcoff_link_hash_entry
)))
603 isxcoff64
= bfd_coff_debug_string_prefix_length (abfd
) == 4;
605 ret
->debug_strtab
= _bfd_xcoff_stringtab_init (isxcoff64
);
606 ret
->archive_info
= htab_create (37, xcoff_archive_info_hash
,
607 xcoff_archive_info_eq
, NULL
);
608 if (!ret
->debug_strtab
|| !ret
->archive_info
)
610 _bfd_xcoff_bfd_link_hash_table_free (abfd
);
613 ret
->root
.hash_table_free
= _bfd_xcoff_bfd_link_hash_table_free
;
615 /* The linker will always generate a full a.out header. We need to
616 record that fact now, before the sizeof_headers routine could be
618 xcoff_data (abfd
)->full_aouthdr
= true;
623 /* Read internal relocs for an XCOFF csect. This is a wrapper around
624 _bfd_coff_read_internal_relocs which tries to take advantage of any
625 relocs which may have been cached for the enclosing section. */
627 static struct internal_reloc
*
628 xcoff_read_internal_relocs (bfd
*abfd
,
631 bfd_byte
*external_relocs
,
632 bool require_internal
,
633 struct internal_reloc
*internal_relocs
)
635 if (coff_section_data (abfd
, sec
) != NULL
636 && coff_section_data (abfd
, sec
)->relocs
== NULL
637 && xcoff_section_data (abfd
, sec
) != NULL
)
641 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
643 if (enclosing
!= NULL
644 && (coff_section_data (abfd
, enclosing
) == NULL
645 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
647 && enclosing
->reloc_count
> 0)
649 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
650 external_relocs
, false, NULL
)
655 if (enclosing
!= NULL
656 && coff_section_data (abfd
, enclosing
) != NULL
657 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
661 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
662 / bfd_coff_relsz (abfd
));
664 if (! require_internal
)
665 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
666 memcpy (internal_relocs
,
667 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
668 sec
->reloc_count
* sizeof (struct internal_reloc
));
669 return internal_relocs
;
673 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
674 require_internal
, internal_relocs
);
677 /* Split FILENAME into an import path and an import filename,
678 storing them in *IMPPATH and *IMPFILE respectively. */
681 bfd_xcoff_split_import_path (bfd
*abfd
, const char *filename
,
682 const char **imppath
, const char **impfile
)
688 base
= lbasename (filename
);
689 length
= base
- filename
;
691 /* The filename has no directory component, so use an empty path. */
693 else if (length
== 1)
694 /* The filename is in the root directory. */
698 /* Extract the (non-empty) directory part. Note that we don't
699 need to strip duplicate directory separators from any part
700 of the string; the native linker doesn't do that either. */
701 path
= bfd_alloc (abfd
, length
);
704 memcpy (path
, filename
, length
- 1);
705 path
[length
- 1] = 0;
712 /* Set ARCHIVE's import path as though its filename had been given
716 bfd_xcoff_set_archive_import_path (struct bfd_link_info
*info
,
717 bfd
*archive
, const char *filename
)
719 struct xcoff_archive_info
*archive_info
;
721 archive_info
= xcoff_get_archive_info (info
, archive
);
722 return (archive_info
!= NULL
723 && bfd_xcoff_split_import_path (archive
, filename
,
724 &archive_info
->imppath
,
725 &archive_info
->impfile
));
728 /* H is an imported symbol. Set the import module's path, file and member
729 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
730 no specific import module is specified. */
733 xcoff_set_import_path (struct bfd_link_info
*info
,
734 struct xcoff_link_hash_entry
*h
,
735 const char *imppath
, const char *impfile
,
736 const char *impmember
)
739 struct xcoff_import_file
**pp
;
741 /* We overload the ldindx field to hold the l_ifile value for this
743 BFD_ASSERT (h
->ldsym
== NULL
);
744 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
749 /* We start c at 1 because the first entry in the import list is
750 reserved for the library search path. */
751 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
753 pp
= &(*pp
)->next
, ++c
)
755 if (filename_cmp ((*pp
)->path
, imppath
) == 0
756 && filename_cmp ((*pp
)->file
, impfile
) == 0
757 && filename_cmp ((*pp
)->member
, impmember
) == 0)
763 struct xcoff_import_file
*n
;
764 size_t amt
= sizeof (*n
);
766 n
= bfd_alloc (info
->output_bfd
, amt
);
772 n
->member
= impmember
;
780 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
781 Return true if LDSYM defines H. */
784 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry
*h
,
785 struct internal_ldsym
*ldsym
)
787 /* If we didn't know about H before processing LDSYM, LDSYM
788 definitely defines H. */
789 if (h
->root
.type
== bfd_link_hash_new
)
792 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
793 dynamic symbol, LDSYM trumps the current definition of H. */
794 if ((ldsym
->l_smtype
& L_WEAK
) == 0
795 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
796 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
797 && (h
->root
.type
== bfd_link_hash_defweak
798 || h
->root
.type
== bfd_link_hash_undefweak
))
801 /* If H is currently undefined, LDSYM defines it. */
802 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
803 && (h
->root
.type
== bfd_link_hash_undefined
804 || h
->root
.type
== bfd_link_hash_undefweak
))
810 /* This function is used to add symbols from a dynamic object to the
811 global symbol table. */
814 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
818 struct internal_ldhdr ldhdr
;
820 bfd_byte
*elsym
, *elsymend
;
821 struct xcoff_import_file
*n
;
823 struct xcoff_import_file
**pp
;
825 /* We can only handle a dynamic object if we are generating an XCOFF
827 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
830 (_("%pB: XCOFF shared object when not producing XCOFF output"),
832 bfd_set_error (bfd_error_invalid_operation
);
836 /* The symbols we use from a dynamic object are not the symbols in
837 the normal symbol table, but, rather, the symbols in the export
838 table. If there is a global symbol in a dynamic object which is
839 not in the export table, the loader will not be able to find it,
840 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
841 libc.a has symbols in the export table which are not in the
844 /* Read in the .loader section. FIXME: We should really use the
845 o_snloader field in the a.out header, rather than grabbing the
847 lsec
= bfd_get_section_by_name (abfd
, ".loader");
851 (_("%pB: dynamic object with no .loader section"),
853 bfd_set_error (bfd_error_no_symbols
);
857 if (! xcoff_get_section_contents (abfd
, lsec
))
859 contents
= coff_section_data (abfd
, lsec
)->contents
;
861 /* Remove the sections from this object, so that they do not get
862 included in the link. */
863 bfd_section_list_clear (abfd
);
865 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
867 strings
= (char *) contents
+ ldhdr
.l_stoff
;
869 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
871 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
873 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
875 struct internal_ldsym ldsym
;
876 char nambuf
[SYMNMLEN
+ 1];
878 struct xcoff_link_hash_entry
*h
;
880 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
882 /* We are only interested in exported symbols. */
883 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
886 if (ldsym
._l
._l_l
._l_zeroes
== 0)
887 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
890 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
891 nambuf
[SYMNMLEN
] = '\0';
895 /* Normally we could not call xcoff_link_hash_lookup in an add
896 symbols routine, since we might not be using an XCOFF hash
897 table. However, we verified above that we are using an XCOFF
900 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true,
905 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
908 h
->flags
|= XCOFF_DEF_DYNAMIC
;
909 h
->smclas
= ldsym
.l_smclas
;
910 if (h
->smclas
== XMC_XO
)
912 /* This symbol has an absolute value. */
913 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
914 h
->root
.type
= bfd_link_hash_defweak
;
916 h
->root
.type
= bfd_link_hash_defined
;
917 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
918 h
->root
.u
.def
.value
= ldsym
.l_value
;
922 /* Otherwise, we don't bother to actually define the symbol,
923 since we don't have a section to put it in anyhow.
924 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
925 should be imported from the symbol's undef.abfd. */
926 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
927 h
->root
.type
= bfd_link_hash_undefweak
;
929 h
->root
.type
= bfd_link_hash_undefined
;
930 h
->root
.u
.undef
.abfd
= abfd
;
933 /* If this symbol defines a function descriptor, then it
934 implicitly defines the function code as well. */
935 if (h
->smclas
== XMC_DS
936 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
937 h
->flags
|= XCOFF_DESCRIPTOR
;
938 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
940 struct xcoff_link_hash_entry
*hds
;
947 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
951 strcpy (dsnm
+ 1, name
);
952 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
962 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
964 hds
->root
.type
= h
->root
.type
;
965 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
966 if (h
->smclas
== XMC_XO
)
968 /* An absolute symbol appears to actually define code, not a
969 function descriptor. This is how some math functions are
970 implemented on AIX 4.1. */
971 hds
->smclas
= XMC_XO
;
972 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
973 hds
->root
.u
.def
.value
= ldsym
.l_value
;
977 hds
->smclas
= XMC_PR
;
978 hds
->root
.u
.undef
.abfd
= abfd
;
979 /* We do not want to add this to the undefined
986 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
988 free (coff_section_data (abfd
, lsec
)->contents
);
989 coff_section_data (abfd
, lsec
)->contents
= NULL
;
992 /* Record this file in the import files. */
993 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
998 if (abfd
->my_archive
== NULL
|| bfd_is_thin_archive (abfd
->my_archive
))
1000 if (!bfd_xcoff_split_import_path (abfd
, bfd_get_filename (abfd
),
1001 &n
->path
, &n
->file
))
1007 struct xcoff_archive_info
*archive_info
;
1009 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1010 if (!archive_info
->impfile
)
1012 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1013 bfd_get_filename (archive_info
1015 &archive_info
->imppath
,
1016 &archive_info
->impfile
))
1019 n
->path
= archive_info
->imppath
;
1020 n
->file
= archive_info
->impfile
;
1021 n
->member
= bfd_get_filename (abfd
);
1024 /* We start c at 1 because the first import file number is reserved
1026 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1028 pp
= &(*pp
)->next
, ++c
)
1032 xcoff_data (abfd
)->import_file_id
= c
;
1037 /* xcoff_link_create_extra_sections
1039 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1042 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1044 bool return_value
= false;
1046 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1048 /* We need to build a .loader section, so we do it here. This
1049 won't work if we're producing an XCOFF output file with no
1050 XCOFF input files. FIXME. */
1052 if (!bfd_link_relocatable (info
)
1053 && xcoff_hash_table (info
)->loader_section
== NULL
)
1056 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1058 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1062 xcoff_hash_table (info
)->loader_section
= lsec
;
1065 /* Likewise for the linkage section. */
1066 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1069 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1072 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1076 xcoff_hash_table (info
)->linkage_section
= lsec
;
1077 lsec
->alignment_power
= 2;
1080 /* Likewise for the TOC section. */
1081 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1084 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1087 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1091 xcoff_hash_table (info
)->toc_section
= tsec
;
1092 tsec
->alignment_power
= 2;
1095 /* Likewise for the descriptor section. */
1096 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1099 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1102 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1106 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1107 dsec
->alignment_power
= 2;
1110 /* Likewise for the .debug section. */
1111 if (xcoff_hash_table (info
)->debug_section
== NULL
1112 && info
->strip
!= strip_all
)
1115 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1117 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1121 xcoff_hash_table (info
)->debug_section
= dsec
;
1125 return_value
= true;
1129 return return_value
;
1132 /* Returns the index of reloc in RELOCS with the least address greater
1133 than or equal to ADDRESS. The relocs are sorted by address. */
1135 static bfd_size_type
1136 xcoff_find_reloc (struct internal_reloc
*relocs
,
1137 bfd_size_type count
,
1140 bfd_size_type min
, max
, this;
1144 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1153 /* Do a binary search over (min,max]. */
1154 while (min
+ 1 < max
)
1158 this = (max
+ min
) / 2;
1159 raddr
= relocs
[this].r_vaddr
;
1160 if (raddr
> address
)
1162 else if (raddr
< address
)
1171 if (relocs
[min
].r_vaddr
< address
)
1175 && relocs
[min
- 1].r_vaddr
== address
)
1181 /* Add all the symbols from an object file to the hash table.
1183 XCOFF is a weird format. A normal XCOFF .o files will have three
1184 COFF sections--.text, .data, and .bss--but each COFF section will
1185 contain many csects. These csects are described in the symbol
1186 table. From the linker's point of view, each csect must be
1187 considered a section in its own right. For example, a TOC entry is
1188 handled as a small XMC_TC csect. The linker must be able to merge
1189 different TOC entries together, which means that it must be able to
1190 extract the XMC_TC csects from the .data section of the input .o
1193 From the point of view of our linker, this is, of course, a hideous
1194 nightmare. We cope by actually creating sections for each csect,
1195 and discarding the original sections. We then have to handle the
1196 relocation entries carefully, since the only way to tell which
1197 csect they belong to is to examine the address. */
1200 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1202 unsigned int n_tmask
;
1203 unsigned int n_btshft
;
1205 bfd_size_type symcount
;
1206 struct xcoff_link_hash_entry
**sym_hash
;
1207 asection
**csect_cache
;
1208 unsigned int *lineno_counts
;
1209 bfd_size_type linesz
;
1211 asection
*last_real
;
1214 unsigned int csect_index
;
1215 asection
*first_csect
;
1216 bfd_size_type symesz
;
1219 struct reloc_info_struct
1221 struct internal_reloc
*relocs
;
1224 } *reloc_info
= NULL
;
1227 keep_syms
= obj_coff_keep_syms (abfd
);
1229 if ((abfd
->flags
& DYNAMIC
) != 0
1230 && ! info
->static_link
)
1232 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1236 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1237 if (! xcoff_link_create_extra_sections (abfd
, info
))
1240 if ((abfd
->flags
& DYNAMIC
) != 0
1241 && ! info
->static_link
)
1244 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1245 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1247 /* Define macros so that ISFCN, et. al., macros work correctly. */
1248 #define N_TMASK n_tmask
1249 #define N_BTSHFT n_btshft
1251 if (info
->keep_memory
)
1252 default_copy
= false;
1254 default_copy
= true;
1256 symcount
= obj_raw_syment_count (abfd
);
1258 /* We keep a list of the linker hash table entries that correspond
1259 to each external symbol. */
1260 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1261 sym_hash
= bfd_zalloc (abfd
, amt
);
1262 if (sym_hash
== NULL
&& symcount
!= 0)
1264 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1266 /* Because of the weird stuff we are doing with XCOFF csects, we can
1267 not easily determine which section a symbol is in, so we store
1268 the information in the tdata for the input file. */
1269 amt
= symcount
* sizeof (asection
*);
1270 csect_cache
= bfd_zalloc (abfd
, amt
);
1271 if (csect_cache
== NULL
&& symcount
!= 0)
1273 xcoff_data (abfd
)->csects
= csect_cache
;
1275 /* We garbage-collect line-number information on a symbol-by-symbol
1276 basis, so we need to have quick access to the number of entries
1278 amt
= symcount
* sizeof (unsigned int);
1279 lineno_counts
= bfd_zalloc (abfd
, amt
);
1280 if (lineno_counts
== NULL
&& symcount
!= 0)
1282 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1284 /* While splitting sections into csects, we need to assign the
1285 relocs correctly. The relocs and the csects must both be in
1286 order by VMA within a given section, so we handle this by
1287 scanning along the relocs as we process the csects. We index
1288 into reloc_info using the section target_index. */
1289 amt
= abfd
->section_count
+ 1;
1290 amt
*= sizeof (struct reloc_info_struct
);
1291 reloc_info
= bfd_zmalloc (amt
);
1292 if (reloc_info
== NULL
)
1295 /* Read in the relocs and line numbers for each section. */
1296 linesz
= bfd_coff_linesz (abfd
);
1298 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1302 if ((o
->flags
& SEC_RELOC
) != 0)
1304 reloc_info
[o
->target_index
].relocs
=
1305 xcoff_read_internal_relocs (abfd
, o
, true, NULL
, false, NULL
);
1306 amt
= o
->reloc_count
;
1307 amt
*= sizeof (asection
*);
1308 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1309 if (reloc_info
[o
->target_index
].csects
== NULL
)
1313 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1314 && o
->lineno_count
> 0)
1318 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0)
1320 if (_bfd_mul_overflow (linesz
, o
->lineno_count
, &amt
))
1322 bfd_set_error (bfd_error_file_too_big
);
1325 linenos
= _bfd_malloc_and_read (abfd
, amt
, amt
);
1326 if (linenos
== NULL
)
1328 reloc_info
[o
->target_index
].linenos
= linenos
;
1332 /* Don't let the linker relocation routines discard the symbols. */
1333 obj_coff_keep_syms (abfd
) = true;
1339 symesz
= bfd_coff_symesz (abfd
);
1340 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1341 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1342 esym_end
= esym
+ symcount
* symesz
;
1344 while (esym
< esym_end
)
1346 struct internal_syment sym
;
1347 union internal_auxent aux
;
1349 char buf
[SYMNMLEN
+ 1];
1353 struct xcoff_link_hash_entry
*set_toc
;
1355 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1357 /* In this pass we are only interested in symbols with csect
1359 if (!CSECT_SYM_P (sym
.n_sclass
))
1362 Normally csect is a .pr, .rw etc. created in the loop
1363 If C_FILE or first time, handle special
1365 Advance esym, sym_hash, csect_hash ptrs. */
1366 if (sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1369 *csect_cache
= csect
;
1370 else if (first_csect
== NULL
1371 || sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1372 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1374 *csect_cache
= NULL
;
1375 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1376 sym_hash
+= sym
.n_numaux
+ 1;
1377 csect_cache
+= sym
.n_numaux
+ 1;
1378 lineno_counts
+= sym
.n_numaux
+ 1;
1383 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1388 /* If this symbol has line number information attached to it,
1389 and we're not stripping it, count the number of entries and
1390 add them to the count for this csect. In the final link pass
1391 we are going to attach line number information by symbol,
1392 rather than by section, in order to more easily handle
1393 garbage collection. */
1394 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1397 && ISFCN (sym
.n_type
))
1399 union internal_auxent auxlin
;
1401 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1402 sym
.n_type
, sym
.n_sclass
,
1403 0, sym
.n_numaux
, (void *) &auxlin
);
1405 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1407 asection
*enclosing
;
1408 bfd_signed_vma linoff
;
1410 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1411 if (enclosing
== NULL
)
1414 /* xgettext:c-format */
1415 (_("%pB: `%s' has line numbers but no enclosing section"),
1417 bfd_set_error (bfd_error_bad_value
);
1420 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1421 - enclosing
->line_filepos
);
1422 /* Explicit cast to bfd_signed_vma for compiler. */
1423 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1425 struct internal_lineno lin
;
1426 bfd_byte
*linpstart
;
1428 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1430 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1432 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1434 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1437 bfd_byte
*linpend
, *linp
;
1439 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1440 + enclosing
->lineno_count
* linesz
);
1441 for (linp
= linpstart
+ linesz
;
1445 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1447 if (lin
.l_lnno
== 0)
1450 *lineno_counts
= (linp
- linpstart
) / linesz
;
1451 /* The setting of line_filepos will only be
1452 useful if all the line number entries for a
1453 csect are contiguous; this only matters for
1455 if (csect
->line_filepos
== 0)
1456 csect
->line_filepos
=
1457 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1463 /* Pick up the csect auxiliary information. */
1464 if (sym
.n_numaux
== 0)
1467 /* xgettext:c-format */
1468 (_("%pB: class %d symbol `%s' has no aux entries"),
1469 abfd
, sym
.n_sclass
, name
);
1470 bfd_set_error (bfd_error_bad_value
);
1474 bfd_coff_swap_aux_in (abfd
,
1475 (void *) (esym
+ symesz
* sym
.n_numaux
),
1476 sym
.n_type
, sym
.n_sclass
,
1477 sym
.n_numaux
- 1, sym
.n_numaux
,
1480 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1490 /* xgettext:c-format */
1491 (_("%pB: symbol `%s' has unrecognized csect type %d"),
1493 bfd_set_error (bfd_error_bad_value
);
1497 /* This is an external reference. */
1498 if (sym
.n_sclass
== C_HIDEXT
1499 || sym
.n_scnum
!= N_UNDEF
1500 || aux
.x_csect
.x_scnlen
.l
!= 0)
1503 /* xgettext:c-format */
1504 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1506 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1507 (int64_t) aux
.x_csect
.x_scnlen
.l
);
1508 bfd_set_error (bfd_error_bad_value
);
1512 /* An XMC_XO external reference is actually a reference to
1513 an absolute location. */
1514 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1515 section
= bfd_und_section_ptr
;
1518 section
= bfd_abs_section_ptr
;
1519 value
= sym
.n_value
;
1525 csect_index
= -(unsigned) 1;
1527 /* When we see a TOC anchor, we record the TOC value. */
1528 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1530 if (sym
.n_sclass
!= C_HIDEXT
1531 || aux
.x_csect
.x_scnlen
.l
!= 0)
1534 /* xgettext:c-format */
1535 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64
),
1536 abfd
, name
, sym
.n_sclass
, (int64_t) aux
.x_csect
.x_scnlen
.l
);
1537 bfd_set_error (bfd_error_bad_value
);
1540 xcoff_data (abfd
)->toc
= sym
.n_value
;
1543 /* We must merge TOC entries for the same symbol. We can
1544 merge two TOC entries if they are both C_HIDEXT, they
1545 both have the same name, they are both 4 or 8 bytes long, and
1546 they both have a relocation table entry for an external
1547 symbol with the same name. Unfortunately, this means
1548 that we must look through the relocations. Ick.
1550 Logic for 32 bit vs 64 bit.
1551 32 bit has a csect length of 4 for TOC
1552 64 bit has a csect length of 8 for TOC
1554 The conditions to get past the if-check are not that bad.
1555 They are what is used to create the TOC csects in the first
1557 if (aux
.x_csect
.x_smclas
== XMC_TC
1558 && sym
.n_sclass
== C_HIDEXT
1559 && info
->output_bfd
->xvec
== abfd
->xvec
1560 && ((bfd_xcoff_is_xcoff32 (abfd
)
1561 && aux
.x_csect
.x_scnlen
.l
== 4)
1562 || (bfd_xcoff_is_xcoff64 (abfd
)
1563 && aux
.x_csect
.x_scnlen
.l
== 8)))
1565 asection
*enclosing
;
1566 struct internal_reloc
*relocs
;
1567 bfd_size_type relindx
;
1568 struct internal_reloc
*rel
;
1570 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1571 if (enclosing
== NULL
)
1574 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1575 amt
= enclosing
->reloc_count
;
1576 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1577 rel
= relocs
+ relindx
;
1579 /* 32 bit R_POS r_size is 31
1580 64 bit R_POS r_size is 63 */
1581 if (relindx
< enclosing
->reloc_count
1582 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1583 && rel
->r_type
== R_POS
1584 && ((bfd_xcoff_is_xcoff32 (abfd
)
1585 && rel
->r_size
== 31)
1586 || (bfd_xcoff_is_xcoff64 (abfd
)
1587 && rel
->r_size
== 63)))
1591 struct internal_syment relsym
;
1593 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1594 + rel
->r_symndx
* symesz
);
1595 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1596 if (EXTERN_SYM_P (relsym
.n_sclass
))
1598 const char *relname
;
1599 char relbuf
[SYMNMLEN
+ 1];
1601 struct xcoff_link_hash_entry
*h
;
1603 /* At this point we know that the TOC entry is
1604 for an externally visible symbol. */
1605 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1607 if (relname
== NULL
)
1610 /* We only merge TOC entries if the TC name is
1611 the same as the symbol name. This handles
1612 the normal case, but not common cases like
1613 SYM.P4 which gcc generates to store SYM + 4
1614 in the TOC. FIXME. */
1615 if (strcmp (name
, relname
) == 0)
1617 copy
= (! info
->keep_memory
1618 || relsym
._n
._n_n
._n_zeroes
!= 0
1619 || relsym
._n
._n_n
._n_offset
== 0);
1620 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1621 relname
, true, copy
,
1626 /* At this point h->root.type could be
1627 bfd_link_hash_new. That should be OK,
1628 since we know for sure that we will come
1629 across this symbol as we step through the
1632 /* We store h in *sym_hash for the
1633 convenience of the relocate_section
1637 if (h
->toc_section
!= NULL
)
1639 asection
**rel_csects
;
1641 /* We already have a TOC entry for this
1642 symbol, so we can just ignore this
1645 reloc_info
[enclosing
->target_index
].csects
;
1646 rel_csects
[relindx
] = bfd_und_section_ptr
;
1650 /* We are about to create a TOC entry for
1659 asection
*enclosing
;
1661 /* We need to create a new section. We get the name from
1662 the csect storage mapping class, so that the linker can
1663 accumulate similar csects together. */
1665 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1669 /* The enclosing section is the main section : .data, .text
1670 or .bss that the csect is coming from. */
1671 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1672 if (enclosing
== NULL
)
1675 if (! bfd_is_abs_section (enclosing
)
1676 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1677 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1678 > enclosing
->vma
+ enclosing
->size
)))
1681 /* xgettext:c-format */
1682 (_("%pB: csect `%s' not in enclosing section"),
1684 bfd_set_error (bfd_error_bad_value
);
1687 csect
->vma
= sym
.n_value
;
1688 csect
->filepos
= (enclosing
->filepos
1691 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1692 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1693 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1695 /* Record the enclosing section in the tdata for this new
1697 amt
= sizeof (struct coff_section_tdata
);
1698 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1699 if (csect
->used_by_bfd
== NULL
)
1701 amt
= sizeof (struct xcoff_section_tdata
);
1702 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1703 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1705 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1706 xcoff_section_data (abfd
, csect
)->lineno_count
=
1707 enclosing
->lineno_count
;
1709 if (enclosing
->owner
== abfd
)
1711 struct internal_reloc
*relocs
;
1712 bfd_size_type relindx
;
1713 struct internal_reloc
*rel
;
1714 asection
**rel_csect
;
1716 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1717 amt
= enclosing
->reloc_count
;
1718 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1720 rel
= relocs
+ relindx
;
1721 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1724 csect
->rel_filepos
= (enclosing
->rel_filepos
1725 + relindx
* bfd_coff_relsz (abfd
));
1726 while (relindx
< enclosing
->reloc_count
1727 && *rel_csect
== NULL
1728 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1732 csect
->flags
|= SEC_RELOC
;
1733 ++csect
->reloc_count
;
1740 /* There are a number of other fields and section flags
1741 which we do not bother to set. */
1743 csect_index
= ((esym
1744 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1747 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1749 if (first_csect
== NULL
)
1750 first_csect
= csect
;
1752 /* If this symbol is external, we treat it as starting at the
1753 beginning of the newly created section. */
1754 if (EXTERN_SYM_P (sym
.n_sclass
))
1760 /* If this is a TOC section for a symbol, record it. */
1761 if (set_toc
!= NULL
)
1762 set_toc
->toc_section
= csect
;
1767 /* This is a label definition. The x_scnlen field is the
1768 symbol index of the csect. Usually the XTY_LD symbol will
1769 follow its appropriate XTY_SD symbol. The .set pseudo op can
1770 cause the XTY_LD to not follow the XTY_SD symbol. */
1775 if (aux
.x_csect
.x_scnlen
.l
< 0
1776 || (aux
.x_csect
.x_scnlen
.l
1777 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1781 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1783 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1789 /* xgettext:c-format */
1790 (_("%pB: misplaced XTY_LD `%s'"),
1792 bfd_set_error (bfd_error_bad_value
);
1796 value
= sym
.n_value
- csect
->vma
;
1801 /* This is an unitialized csect. We could base the name on
1802 the storage mapping class, but we don't bother except for
1803 an XMC_TD symbol. If this csect is externally visible,
1804 it is a common symbol. We put XMC_TD symbols in sections
1805 named .tocbss, and rely on the linker script to put that
1808 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1810 /* The linker script puts the .td section in the data
1811 section after the .tc section. */
1812 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1815 else if (aux
.x_csect
.x_smclas
== XMC_UL
)
1817 /* This is a thread-local unitialized csect. */
1818 csect
= bfd_make_section_anyway_with_flags (abfd
, ".tbss",
1819 SEC_ALLOC
| SEC_THREAD_LOCAL
);
1822 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1827 csect
->vma
= sym
.n_value
;
1828 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1829 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1830 /* There are a number of other fields and section flags
1831 which we do not bother to set. */
1833 csect_index
= ((esym
1834 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1837 amt
= sizeof (struct coff_section_tdata
);
1838 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1839 if (csect
->used_by_bfd
== NULL
)
1841 amt
= sizeof (struct xcoff_section_tdata
);
1842 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1843 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1845 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1847 if (first_csect
== NULL
)
1848 first_csect
= csect
;
1850 if (EXTERN_SYM_P (sym
.n_sclass
))
1852 csect
->flags
|= SEC_IS_COMMON
;
1855 value
= aux
.x_csect
.x_scnlen
.l
;
1861 /* Check for magic symbol names. */
1862 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1863 && aux
.x_csect
.x_smclas
!= XMC_TC
1864 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1870 if (strcmp (name
, "_text") == 0)
1871 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1872 else if (strcmp (name
, "_etext") == 0)
1873 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1874 else if (strcmp (name
, "_data") == 0)
1875 i
= XCOFF_SPECIAL_SECTION_DATA
;
1876 else if (strcmp (name
, "_edata") == 0)
1877 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1878 else if (strcmp (name
, "_end") == 0)
1879 i
= XCOFF_SPECIAL_SECTION_END
;
1881 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1882 i
= XCOFF_SPECIAL_SECTION_END2
;
1885 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1888 /* Now we have enough information to add the symbol to the
1889 linker hash table. */
1891 if (EXTERN_SYM_P (sym
.n_sclass
))
1896 BFD_ASSERT (section
!= NULL
);
1898 /* We must copy the name into memory if we got it from the
1899 syment itself, rather than the string table. */
1900 copy
= default_copy
;
1901 if (sym
._n
._n_n
._n_zeroes
!= 0
1902 || sym
._n
._n_n
._n_offset
== 0)
1905 /* Ignore global linkage code when linking statically. */
1906 if (info
->static_link
1907 && (smtyp
== XTY_SD
|| smtyp
== XTY_LD
)
1908 && aux
.x_csect
.x_smclas
== XMC_GL
)
1910 section
= bfd_und_section_ptr
;
1914 /* The AIX linker appears to only detect multiple symbol
1915 definitions when there is a reference to the symbol. If
1916 a symbol is defined multiple times, and the only
1917 references are from the same object file, the AIX linker
1918 appears to permit it. It does not merge the different
1919 definitions, but handles them independently. On the
1920 other hand, if there is a reference, the linker reports
1923 This matters because the AIX <net/net_globals.h> header
1924 file actually defines an initialized array, so we have to
1925 actually permit that to work.
1927 Just to make matters even more confusing, the AIX linker
1928 appears to permit multiple symbol definitions whenever
1929 the second definition is in an archive rather than an
1930 object file. This may be a consequence of the manner in
1931 which it handles archives: I think it may load the entire
1932 archive in as separate csects, and then let garbage
1933 collection discard symbols.
1935 We also have to handle the case of statically linking a
1936 shared object, which will cause symbol redefinitions,
1937 although this is an easier case to detect. */
1938 else if (info
->output_bfd
->xvec
== abfd
->xvec
)
1940 if (! bfd_is_und_section (section
))
1941 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1942 name
, true, copy
, false);
1944 /* Make a copy of the symbol name to prevent problems with
1946 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1947 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1948 true, true, false));
1950 if (*sym_hash
== NULL
)
1952 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1953 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1954 && ! bfd_is_und_section (section
)
1955 && ! bfd_is_com_section (section
))
1957 /* This is a second definition of a defined symbol. */
1958 if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
1959 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
1961 /* The existing symbol is from a shared library.
1963 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1964 (*sym_hash
)->root
.u
.undef
.abfd
=
1965 (*sym_hash
)->root
.u
.def
.section
->owner
;
1967 else if (abfd
->my_archive
!= NULL
)
1969 /* This is a redefinition in an object contained
1970 in an archive. Just ignore it. See the
1972 section
= bfd_und_section_ptr
;
1975 else if (sym
.n_sclass
== C_AIX_WEAKEXT
1976 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1978 /* At least one of the definitions is weak.
1979 Allow the normal rules to take effect. */
1981 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1982 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1984 /* This symbol has been referenced. In this
1985 case, we just continue and permit the
1986 multiple definition error. See the comment
1987 above about the behaviour of the AIX linker. */
1989 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1991 /* The symbols are both csects of the same
1992 class. There is at least a chance that this
1993 is a semi-legitimate redefinition. */
1994 section
= bfd_und_section_ptr
;
1996 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
1999 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
2000 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
2001 && (bfd_is_und_section (section
)
2002 || bfd_is_com_section (section
)))
2004 /* This is a reference to a multiply defined symbol.
2005 Report the error now. See the comment above
2006 about the behaviour of the AIX linker. We could
2007 also do this with warning symbols, but I'm not
2008 sure the XCOFF linker is wholly prepared to
2009 handle them, and that would only be a warning,
2011 (*info
->callbacks
->multiple_definition
) (info
,
2015 /* Try not to give this error too many times. */
2016 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2020 /* _bfd_generic_link_add_one_symbol may call the linker to
2021 generate an error message, and the linker may try to read
2022 the symbol table to give a good error. Right now, the
2023 line numbers are in an inconsistent state, since they are
2024 counted both in the real sections and in the new csects.
2025 We need to leave the count in the real sections so that
2026 the linker can report the line number of the error
2027 correctly, so temporarily clobber the link to the csects
2028 so that the linker will not try to read the line numbers
2029 a second time from the csects. */
2030 BFD_ASSERT (last_real
->next
== first_csect
);
2031 last_real
->next
= NULL
;
2032 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2033 ok
= (_bfd_generic_link_add_one_symbol
2034 (info
, abfd
, name
, flags
, section
, value
, NULL
, copy
, true,
2035 (struct bfd_link_hash_entry
**) sym_hash
));
2036 last_real
->next
= first_csect
;
2040 if (smtyp
== XTY_CM
)
2042 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2043 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2044 /* We don't need the common csect we just created. */
2047 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2048 = csect
->alignment_power
;
2051 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2057 || section
== bfd_und_section_ptr
)
2058 flag
= XCOFF_REF_REGULAR
;
2060 flag
= XCOFF_DEF_REGULAR
;
2061 (*sym_hash
)->flags
|= flag
;
2063 if ((*sym_hash
)->smclas
== XMC_UA
2064 || flag
== XCOFF_DEF_REGULAR
)
2065 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2069 if (smtyp
== XTY_ER
)
2070 *csect_cache
= section
;
2073 *csect_cache
= csect
;
2075 xcoff_section_data (abfd
, csect
)->last_symndx
2076 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2079 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2080 sym_hash
+= sym
.n_numaux
+ 1;
2081 csect_cache
+= sym
.n_numaux
+ 1;
2082 lineno_counts
+= sym
.n_numaux
+ 1;
2085 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2087 /* Make sure that we have seen all the relocs. */
2088 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2090 /* Debugging sections have no csects. */
2091 if (bfd_section_flags (o
) & SEC_DEBUGGING
)
2094 /* Reset the section size and the line number count, since the
2095 data is now attached to the csects. Don't reset the size of
2096 the .debug section, since we need to read it below in
2097 bfd_xcoff_size_dynamic_sections. */
2098 if (strcmp (bfd_section_name (o
), ".debug") != 0)
2100 o
->lineno_count
= 0;
2102 if ((o
->flags
& SEC_RELOC
) != 0)
2105 struct internal_reloc
*rel
;
2106 asection
**rel_csect
;
2108 rel
= reloc_info
[o
->target_index
].relocs
;
2109 rel_csect
= reloc_info
[o
->target_index
].csects
;
2111 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2113 if (*rel_csect
== NULL
)
2116 /* xgettext:c-format */
2117 (_("%pB: reloc %s:%" PRId64
" not in csect"),
2118 abfd
, o
->name
, (int64_t) i
);
2119 bfd_set_error (bfd_error_bad_value
);
2123 /* We identify all function symbols that are the target
2124 of a relocation, so that we can create glue code for
2125 functions imported from dynamic objects. */
2126 if (info
->output_bfd
->xvec
== abfd
->xvec
2127 && *rel_csect
!= bfd_und_section_ptr
2128 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2130 struct xcoff_link_hash_entry
*h
;
2132 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2133 /* If the symbol name starts with a period, it is
2134 the code of a function. If the symbol is
2135 currently undefined, then add an undefined symbol
2136 for the function descriptor. This should do no
2137 harm, because any regular object that defines the
2138 function should also define the function
2139 descriptor. It helps, because it means that we
2140 will identify the function descriptor with a
2141 dynamic object if a dynamic object defines it. */
2142 if (h
->root
.root
.string
[0] == '.'
2143 && h
->descriptor
== NULL
)
2145 struct xcoff_link_hash_entry
*hds
;
2146 struct bfd_link_hash_entry
*bh
;
2148 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2149 h
->root
.root
.string
+ 1,
2153 if (hds
->root
.type
== bfd_link_hash_new
)
2156 if (! (_bfd_generic_link_add_one_symbol
2157 (info
, abfd
, hds
->root
.root
.string
,
2158 (flagword
) 0, bfd_und_section_ptr
,
2159 (bfd_vma
) 0, NULL
, false,
2162 hds
= (struct xcoff_link_hash_entry
*) bh
;
2164 hds
->flags
|= XCOFF_DESCRIPTOR
;
2165 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2166 hds
->descriptor
= h
;
2167 h
->descriptor
= hds
;
2169 if (h
->root
.root
.string
[0] == '.')
2170 h
->flags
|= XCOFF_CALLED
;
2174 free (reloc_info
[o
->target_index
].csects
);
2175 reloc_info
[o
->target_index
].csects
= NULL
;
2177 /* Reset SEC_RELOC and the reloc_count, since the reloc
2178 information is now attached to the csects. */
2179 o
->flags
&=~ SEC_RELOC
;
2182 /* If we are not keeping memory, free the reloc information. */
2183 if (! info
->keep_memory
2184 && coff_section_data (abfd
, o
) != NULL
2185 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2187 free (coff_section_data (abfd
, o
)->relocs
);
2188 coff_section_data (abfd
, o
)->relocs
= NULL
;
2192 /* Free up the line numbers. FIXME: We could cache these
2193 somewhere for the final link, to avoid reading them again. */
2194 free (reloc_info
[o
->target_index
].linenos
);
2195 reloc_info
[o
->target_index
].linenos
= NULL
;
2200 obj_coff_keep_syms (abfd
) = keep_syms
;
2205 if (reloc_info
!= NULL
)
2207 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2209 free (reloc_info
[o
->target_index
].csects
);
2210 free (reloc_info
[o
->target_index
].linenos
);
2214 obj_coff_keep_syms (abfd
) = keep_syms
;
2221 /* Add symbols from an XCOFF object file. */
2224 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2226 if (! _bfd_coff_get_external_symbols (abfd
))
2228 if (! xcoff_link_add_symbols (abfd
, info
))
2230 if (! info
->keep_memory
)
2232 if (! _bfd_coff_free_symbols (abfd
))
2238 /* Look through the loader symbols to see if this dynamic object
2239 should be included in the link. The native linker uses the loader
2240 symbols, not the normal symbol table, so we do too. */
2243 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2244 struct bfd_link_info
*info
,
2250 struct internal_ldhdr ldhdr
;
2251 const char *strings
;
2252 bfd_byte
*elsym
, *elsymend
;
2256 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2258 /* There are no symbols, so don't try to include it. */
2261 if (! xcoff_get_section_contents (abfd
, lsec
))
2263 contents
= coff_section_data (abfd
, lsec
)->contents
;
2265 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2267 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2269 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2271 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2272 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2274 struct internal_ldsym ldsym
;
2275 char nambuf
[SYMNMLEN
+ 1];
2277 struct bfd_link_hash_entry
*h
;
2279 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2281 /* We are only interested in exported symbols. */
2282 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2285 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2286 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2289 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2290 nambuf
[SYMNMLEN
] = '\0';
2294 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2296 /* We are only interested in symbols that are currently
2297 undefined. At this point we know that we are using an XCOFF
2300 && h
->type
== bfd_link_hash_undefined
2301 && (((struct xcoff_link_hash_entry
*) h
)->flags
2302 & XCOFF_DEF_DYNAMIC
) == 0)
2304 if (!(*info
->callbacks
2305 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2312 /* We do not need this shared object. */
2313 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2315 free (coff_section_data (abfd
, lsec
)->contents
);
2316 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2322 /* Look through the symbols to see if this object file should be
2323 included in the link. */
2326 xcoff_link_check_ar_symbols (bfd
*abfd
,
2327 struct bfd_link_info
*info
,
2331 bfd_size_type symesz
;
2337 if ((abfd
->flags
& DYNAMIC
) != 0
2338 && ! info
->static_link
2339 && info
->output_bfd
->xvec
== abfd
->xvec
)
2340 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
, subsbfd
);
2342 symesz
= bfd_coff_symesz (abfd
);
2343 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2344 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2345 while (esym
< esym_end
)
2347 struct internal_syment sym
;
2349 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2350 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2352 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2355 char buf
[SYMNMLEN
+ 1];
2356 struct bfd_link_hash_entry
*h
;
2358 /* This symbol is externally visible, and is defined by this
2360 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2364 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2366 /* We are only interested in symbols that are currently
2367 undefined. If a symbol is currently known to be common,
2368 XCOFF linkers do not bring in an object file which
2369 defines it. We also don't bring in symbols to satisfy
2370 undefined references in shared objects. */
2372 && h
->type
== bfd_link_hash_undefined
2373 && (info
->output_bfd
->xvec
!= abfd
->xvec
2374 || (((struct xcoff_link_hash_entry
*) h
)->flags
2375 & XCOFF_DEF_DYNAMIC
) == 0))
2377 if (!(*info
->callbacks
2378 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2386 /* We do not need this object file. */
2390 /* Check a single archive element to see if we need to include it in
2391 the link. *PNEEDED is set according to whether this element is
2392 needed in the link or not. This is called via
2393 _bfd_generic_link_add_archive_symbols. */
2396 xcoff_link_check_archive_element (bfd
*abfd
,
2397 struct bfd_link_info
*info
,
2398 struct bfd_link_hash_entry
*h ATTRIBUTE_UNUSED
,
2399 const char *name ATTRIBUTE_UNUSED
,
2405 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2406 if (!_bfd_coff_get_external_symbols (abfd
))
2410 if (!xcoff_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
2415 /* Potentially, the add_archive_element hook may have set a
2416 substitute BFD for us. */
2420 && !_bfd_coff_free_symbols (oldbfd
))
2422 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2423 if (!_bfd_coff_get_external_symbols (abfd
))
2426 if (!xcoff_link_add_symbols (abfd
, info
))
2428 if (info
->keep_memory
)
2434 if (!_bfd_coff_free_symbols (abfd
))
2441 /* Given an XCOFF BFD, add symbols to the global hash table as
2445 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2447 switch (bfd_get_format (abfd
))
2450 return xcoff_link_add_object_symbols (abfd
, info
);
2453 /* If the archive has a map, do the usual search. We then need
2454 to check the archive for dynamic objects, because they may not
2455 appear in the archive map even though they should, perhaps, be
2456 included. If the archive has no map, we just consider each object
2457 file in turn, since that apparently is what the AIX native linker
2459 if (bfd_has_map (abfd
))
2461 if (! (_bfd_generic_link_add_archive_symbols
2462 (abfd
, info
, xcoff_link_check_archive_element
)))
2469 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2470 while (member
!= NULL
)
2472 if (bfd_check_format (member
, bfd_object
)
2473 && (info
->output_bfd
->xvec
== member
->xvec
)
2474 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2478 if (! xcoff_link_check_archive_element (member
, info
,
2479 NULL
, NULL
, &needed
))
2482 member
->archive_pass
= -1;
2484 member
= bfd_openr_next_archived_file (abfd
, member
);
2491 bfd_set_error (bfd_error_wrong_format
);
2497 _bfd_xcoff_define_common_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2498 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2499 struct bfd_link_hash_entry
*harg
)
2501 struct xcoff_link_hash_entry
*h
;
2503 if (!bfd_generic_define_common_symbol (output_bfd
, info
, harg
))
2506 h
= (struct xcoff_link_hash_entry
*) harg
;
2507 h
->flags
|= XCOFF_DEF_REGULAR
;
2511 /* If symbol H has not been interpreted as a function descriptor,
2512 see whether it should be. Set up its descriptor information if so. */
2515 xcoff_find_function (struct bfd_link_info
*info
,
2516 struct xcoff_link_hash_entry
*h
)
2518 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2519 && h
->root
.root
.string
[0] != '.')
2522 struct xcoff_link_hash_entry
*hfn
;
2525 amt
= strlen (h
->root
.root
.string
) + 2;
2526 fnname
= bfd_malloc (amt
);
2530 strcpy (fnname
+ 1, h
->root
.root
.string
);
2531 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2532 fnname
, false, false, true);
2535 && hfn
->smclas
== XMC_PR
2536 && (hfn
->root
.type
== bfd_link_hash_defined
2537 || hfn
->root
.type
== bfd_link_hash_defweak
))
2539 h
->flags
|= XCOFF_DESCRIPTOR
;
2540 h
->descriptor
= hfn
;
2541 hfn
->descriptor
= h
;
2547 /* Return true if the given bfd contains at least one shared object. */
2550 xcoff_archive_contains_shared_object_p (struct bfd_link_info
*info
,
2553 struct xcoff_archive_info
*archive_info
;
2556 archive_info
= xcoff_get_archive_info (info
, archive
);
2557 if (!archive_info
->know_contains_shared_object_p
)
2559 member
= bfd_openr_next_archived_file (archive
, NULL
);
2560 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2561 member
= bfd_openr_next_archived_file (archive
, member
);
2563 archive_info
->contains_shared_object_p
= (member
!= NULL
);
2564 archive_info
->know_contains_shared_object_p
= 1;
2566 return archive_info
->contains_shared_object_p
;
2569 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2570 qualifies for export by -bexpall. */
2573 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2575 /* Exclude symbols beginning with '_'. */
2576 if (h
->root
.root
.string
[0] == '_')
2579 /* Exclude archive members that would otherwise be unreferenced. */
2580 if ((h
->flags
& XCOFF_MARK
) == 0
2581 && (h
->root
.type
== bfd_link_hash_defined
2582 || h
->root
.type
== bfd_link_hash_defweak
)
2583 && h
->root
.u
.def
.section
->owner
!= NULL
2584 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2590 /* Return true if symbol H qualifies for the forms of automatic export
2591 specified by AUTO_EXPORT_FLAGS. */
2594 xcoff_auto_export_p (struct bfd_link_info
*info
,
2595 struct xcoff_link_hash_entry
*h
,
2596 unsigned int auto_export_flags
)
2598 /* Don't automatically export things that were explicitly exported. */
2599 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2602 /* Don't export things that we don't define. */
2603 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2606 /* Don't export functions; export their descriptors instead. */
2607 if (h
->root
.root
.string
[0] == '.')
2610 /* We don't export a symbol which is being defined by an object
2611 included from an archive which contains a shared object. The
2612 rationale is that if an archive contains both an unshared and
2613 a shared object, then there must be some reason that the
2614 unshared object is unshared, and we don't want to start
2615 providing a shared version of it. In particular, this solves
2616 a bug involving the _savefNN set of functions. gcc will call
2617 those functions without providing a slot to restore the TOC,
2618 so it is essential that these functions be linked in directly
2619 and not from a shared object, which means that a shared
2620 object which also happens to link them in must not export
2621 them. This is confusing, but I haven't been able to think of
2622 a different approach. Note that the symbols can, of course,
2623 be exported explicitly. */
2624 if (h
->root
.type
== bfd_link_hash_defined
2625 || h
->root
.type
== bfd_link_hash_defweak
)
2629 owner
= h
->root
.u
.def
.section
->owner
;
2631 && owner
->my_archive
!= NULL
2632 && xcoff_archive_contains_shared_object_p (info
, owner
->my_archive
))
2636 /* Otherwise, all symbols are exported by -bexpfull. */
2637 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2640 /* Despite its name, -bexpall exports most but not all symbols. */
2641 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2642 && xcoff_covered_by_expall_p (h
))
2648 /* Return true if relocation REL needs to be copied to the .loader section.
2649 If REL is against a global symbol, H is that symbol, otherwise it
2653 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2654 struct xcoff_link_hash_entry
*h
, asection
*ssec
)
2656 if (!xcoff_hash_table (info
)->loader_section
)
2659 switch (rel
->r_type
)
2666 /* We should never need a .loader reloc for a TOC-relative reloc. */
2670 /* In this case, relocations against defined symbols can be resolved
2673 || h
->root
.type
== bfd_link_hash_defined
2674 || h
->root
.type
== bfd_link_hash_defweak
2675 || h
->root
.type
== bfd_link_hash_common
)
2678 /* We will always provide a local definition of function symbols,
2679 even if we don't have one yet. */
2680 if ((h
->flags
& XCOFF_CALLED
) != 0)
2689 /* Absolute relocations against absolute symbols can be
2690 resolved statically. */
2692 && (h
->root
.type
== bfd_link_hash_defined
2693 || h
->root
.type
== bfd_link_hash_defweak
)
2694 && !h
->root
.rel_from_abs
)
2696 asection
*sec
= h
->root
.u
.def
.section
;
2697 if (bfd_is_abs_section (sec
)
2699 && bfd_is_abs_section (sec
->output_section
)))
2703 /* Absolute relocations from read-only sections are forbidden
2704 by AIX loader. However, they can appear in their section's
2707 && (ssec
->output_section
->flags
& SEC_READONLY
) != 0)
2722 /* Mark a symbol as not being garbage, including the section in which
2726 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2728 if ((h
->flags
& XCOFF_MARK
) != 0)
2731 h
->flags
|= XCOFF_MARK
;
2733 /* If we're marking an undefined symbol, try find some way of
2735 if (!bfd_link_relocatable (info
)
2736 && (h
->flags
& XCOFF_IMPORT
) == 0
2737 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2738 && (h
->root
.type
== bfd_link_hash_undefined
2739 || h
->root
.type
== bfd_link_hash_undefweak
))
2741 /* First check whether this symbol can be interpreted as an
2742 undefined function descriptor for a defined function symbol. */
2743 if (!xcoff_find_function (info
, h
))
2746 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2747 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2748 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2750 /* This is a descriptor for a defined symbol, but the input
2751 objects have not defined the descriptor itself. Fill in
2752 the definition automatically.
2754 Note that we do this even if we found a dynamic definition
2755 of H. The local function definition logically overrides
2759 sec
= xcoff_hash_table (info
)->descriptor_section
;
2760 h
->root
.type
= bfd_link_hash_defined
;
2761 h
->root
.u
.def
.section
= sec
;
2762 h
->root
.u
.def
.value
= sec
->size
;
2764 h
->flags
|= XCOFF_DEF_REGULAR
;
2766 /* The size of the function descriptor depends on whether this
2767 is xcoff32 (12) or xcoff64 (24). */
2768 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2770 /* A function descriptor uses two relocs: one for the
2771 associated code, and one for the TOC address. */
2772 xcoff_hash_table (info
)->ldrel_count
+= 2;
2773 sec
->reloc_count
+= 2;
2775 /* Mark the function itself. */
2776 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2779 /* Mark the TOC section, so that we get an anchor
2780 to relocate against. */
2781 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2784 /* We handle writing out the contents of the descriptor in
2785 xcoff_write_global_symbol. */
2787 else if (info
->static_link
)
2788 /* We can't get a symbol value dynamically, so just assume
2789 that it's undefined. */
2790 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2791 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2793 /* This is a function symbol for which we need to create
2796 struct xcoff_link_hash_entry
*hds
;
2798 /* Mark the descriptor (and its TOC section). */
2799 hds
= h
->descriptor
;
2800 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2801 || hds
->root
.type
== bfd_link_hash_undefweak
)
2802 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2803 if (!xcoff_mark_symbol (info
, hds
))
2806 /* Treat this symbol as undefined if the descriptor was. */
2807 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2808 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2810 /* Allocate room for the global linkage code itself. */
2811 sec
= xcoff_hash_table (info
)->linkage_section
;
2812 h
->root
.type
= bfd_link_hash_defined
;
2813 h
->root
.u
.def
.section
= sec
;
2814 h
->root
.u
.def
.value
= sec
->size
;
2816 h
->flags
|= XCOFF_DEF_REGULAR
;
2817 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2819 /* The global linkage code requires a TOC entry for the
2821 if (hds
->toc_section
== NULL
)
2826 xcoff32 uses 4 bytes in the toc.
2827 xcoff64 uses 8 bytes in the toc. */
2828 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2830 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2835 /* Allocate room in the fallback TOC section. */
2836 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2837 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2838 hds
->toc_section
->size
+= byte_size
;
2839 if (!xcoff_mark (info
, hds
->toc_section
))
2842 /* Allocate room for a static and dynamic R_TOC
2844 ++xcoff_hash_table (info
)->ldrel_count
;
2845 ++hds
->toc_section
->reloc_count
;
2847 /* Set the index to -2 to force this symbol to
2850 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2853 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2855 /* Record that the symbol was undefined, then import it.
2856 -brtl links use a special fake import file. */
2857 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2858 if (xcoff_hash_table (info
)->rtld
)
2860 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2865 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2871 if (h
->root
.type
== bfd_link_hash_defined
2872 || h
->root
.type
== bfd_link_hash_defweak
)
2876 hsec
= h
->root
.u
.def
.section
;
2877 if (! bfd_is_abs_section (hsec
)
2878 && hsec
->gc_mark
== 0)
2880 if (! xcoff_mark (info
, hsec
))
2885 if (h
->toc_section
!= NULL
2886 && h
->toc_section
->gc_mark
== 0)
2888 if (! xcoff_mark (info
, h
->toc_section
))
2895 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2896 If the symbol exists, set FLAGS. */
2899 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2900 const char *name
, unsigned int flags
)
2902 struct xcoff_link_hash_entry
*h
;
2904 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2905 false, false, true);
2909 if (h
->root
.type
== bfd_link_hash_defined
2910 || h
->root
.type
== bfd_link_hash_defweak
)
2912 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2919 /* The mark phase of garbage collection. For a given section, mark
2920 it, and all the sections which define symbols to which it refers.
2921 Because this function needs to look at the relocs, we also count
2922 the number of relocs which need to be copied into the .loader
2926 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2928 if (bfd_is_const_section (sec
)
2929 || sec
->gc_mark
!= 0)
2934 if (sec
->owner
->xvec
!= info
->output_bfd
->xvec
)
2937 if (coff_section_data (sec
->owner
, sec
) == NULL
)
2941 if (xcoff_section_data (sec
->owner
, sec
) != NULL
)
2943 struct xcoff_link_hash_entry
**syms
;
2945 unsigned long i
, first
, last
;
2947 /* Mark all the symbols in this section. */
2948 syms
= obj_xcoff_sym_hashes (sec
->owner
);
2949 csects
= xcoff_data (sec
->owner
)->csects
;
2950 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
2951 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
2952 for (i
= first
; i
<= last
; i
++)
2953 if (csects
[i
] == sec
2955 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
2957 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 struct internal_reloc
*rel
, *relend
;
2968 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
2972 relend
= rel
+ sec
->reloc_count
;
2973 for (; rel
< relend
; rel
++)
2975 struct xcoff_link_hash_entry
*h
;
2977 if ((unsigned int) rel
->r_symndx
2978 > obj_raw_syment_count (sec
->owner
))
2981 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2984 if ((h
->flags
& XCOFF_MARK
) == 0)
2986 if (!xcoff_mark_symbol (info
, h
))
2994 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2996 && rsec
->gc_mark
== 0)
2998 if (!xcoff_mark (info
, rsec
))
3003 /* See if this reloc needs to be copied into the .loader
3005 if ((sec
->flags
& SEC_DEBUGGING
) == 0
3006 && xcoff_need_ldrel_p (info
, rel
, h
, sec
))
3008 ++xcoff_hash_table (info
)->ldrel_count
;
3010 h
->flags
|= XCOFF_LDREL
;
3014 if (! info
->keep_memory
3015 && coff_section_data (sec
->owner
, sec
) != NULL
3016 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
3018 free (coff_section_data (sec
->owner
, sec
)->relocs
);
3019 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
3026 /* Routines that are called after all the input files have been
3027 handled, but before the sections are laid out in memory. */
3029 /* The sweep phase of garbage collection. Remove all garbage
3033 xcoff_sweep (struct bfd_link_info
*info
)
3037 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3040 bool some_kept
= false;
3042 /* As says below keep all sections from non-XCOFF
3044 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3048 /* See whether any section is already marked. */
3049 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3054 /* If no section in this file will be kept, then we can
3055 toss out debug sections. */
3058 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3066 /* Keep all sections from non-XCOFF input files. Keep
3067 special sections. Keep .debug sections for the
3069 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3071 if (o
->gc_mark
== 1)
3074 if (sub
->xvec
!= info
->output_bfd
->xvec
3075 || o
== xcoff_hash_table (info
)->debug_section
3076 || o
== xcoff_hash_table (info
)->loader_section
3077 || o
== xcoff_hash_table (info
)->linkage_section
3078 || o
== xcoff_hash_table (info
)->descriptor_section
3079 || (bfd_section_flags (o
) & SEC_DEBUGGING
)
3080 || strcmp (o
->name
, ".debug") == 0)
3081 xcoff_mark (info
, o
);
3091 /* Record the number of elements in a set. This is used to output the
3092 correct csect length. */
3095 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3096 struct bfd_link_info
*info
,
3097 struct bfd_link_hash_entry
*harg
,
3100 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3101 struct xcoff_link_size_list
*n
;
3104 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3107 /* This will hardly ever be called. I don't want to burn four bytes
3108 per global symbol, so instead the size is kept on a linked list
3109 attached to the hash table. */
3111 n
= bfd_alloc (output_bfd
, amt
);
3114 n
->next
= xcoff_hash_table (info
)->size_list
;
3117 xcoff_hash_table (info
)->size_list
= n
;
3119 h
->flags
|= XCOFF_HAS_SIZE
;
3124 /* Import a symbol. */
3127 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3128 struct bfd_link_info
*info
,
3129 struct bfd_link_hash_entry
*harg
,
3131 const char *imppath
,
3132 const char *impfile
,
3133 const char *impmember
,
3134 unsigned int syscall_flag
)
3136 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3138 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3141 /* A symbol name which starts with a period is the code for a
3142 function. If the symbol is undefined, then add an undefined
3143 symbol for the function descriptor, and import that instead. */
3144 if (h
->root
.root
.string
[0] == '.'
3145 && h
->root
.type
== bfd_link_hash_undefined
3146 && val
== (bfd_vma
) -1)
3148 struct xcoff_link_hash_entry
*hds
;
3150 hds
= h
->descriptor
;
3153 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3154 h
->root
.root
.string
+ 1,
3158 if (hds
->root
.type
== bfd_link_hash_new
)
3160 hds
->root
.type
= bfd_link_hash_undefined
;
3161 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3163 hds
->flags
|= XCOFF_DESCRIPTOR
;
3164 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3165 hds
->descriptor
= h
;
3166 h
->descriptor
= hds
;
3169 /* Now, if the descriptor is undefined, import the descriptor
3170 rather than the symbol we were told to import. FIXME: Is
3171 this correct in all cases? */
3172 if (hds
->root
.type
== bfd_link_hash_undefined
)
3176 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3178 if (val
!= (bfd_vma
) -1)
3180 if (h
->root
.type
== bfd_link_hash_defined
)
3181 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
, output_bfd
,
3182 bfd_abs_section_ptr
, val
);
3184 h
->root
.type
= bfd_link_hash_defined
;
3185 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3186 h
->root
.u
.def
.value
= val
;
3190 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3196 /* Export a symbol. */
3199 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3200 struct bfd_link_info
*info
,
3201 struct bfd_link_hash_entry
*harg
)
3203 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3205 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3208 h
->flags
|= XCOFF_EXPORT
;
3210 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3211 I'm just going to ignore it until somebody explains it. */
3213 /* Make sure we don't garbage collect this symbol. */
3214 if (! xcoff_mark_symbol (info
, h
))
3217 /* If this is a function descriptor, make sure we don't garbage
3218 collect the associated function code. We normally don't have to
3219 worry about this, because the descriptor will be attached to a
3220 section with relocs, but if we are creating the descriptor
3221 ourselves those relocs will not be visible to the mark code. */
3222 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3224 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3231 /* Count a reloc against a symbol. This is called for relocs
3232 generated by the linker script, typically for global constructors
3236 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3237 struct bfd_link_info
*info
,
3240 struct xcoff_link_hash_entry
*h
;
3242 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3245 h
= ((struct xcoff_link_hash_entry
*)
3246 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, false, false,
3250 _bfd_error_handler (_("%s: no such symbol"), name
);
3251 bfd_set_error (bfd_error_no_symbols
);
3255 h
->flags
|= XCOFF_REF_REGULAR
;
3256 if (xcoff_hash_table (info
)->loader_section
)
3258 h
->flags
|= XCOFF_LDREL
;
3259 ++xcoff_hash_table (info
)->ldrel_count
;
3262 /* Mark the symbol to avoid garbage collection. */
3263 if (! xcoff_mark_symbol (info
, h
))
3269 /* This function is called for each symbol to which the linker script
3271 FIXME: In cases like the linker test ld-scripts/defined5 where a
3272 symbol is defined both by an input object file and the script,
3273 the script definition doesn't override the object file definition
3274 as is usual for other targets. At least not when the symbol is
3275 output. Other uses of the symbol value by the linker do use the
3279 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3280 struct bfd_link_info
*info
,
3283 struct xcoff_link_hash_entry
*h
;
3285 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3288 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
3293 h
->flags
|= XCOFF_DEF_REGULAR
;
3298 /* An xcoff_link_hash_traverse callback for which DATA points to an
3299 xcoff_loader_info. Mark all symbols that should be automatically
3303 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3305 struct xcoff_loader_info
*ldinfo
;
3307 ldinfo
= (struct xcoff_loader_info
*) data
;
3308 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3310 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3311 ldinfo
->failed
= true;
3316 /* INPUT_BFD has an external symbol associated with hash table entry H
3317 and csect CSECT. Return true if INPUT_BFD defines H. */
3320 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3323 switch (h
->root
.type
)
3325 case bfd_link_hash_defined
:
3326 case bfd_link_hash_defweak
:
3327 /* No input bfd owns absolute symbols. They are written by
3328 xcoff_write_global_symbol instead. */
3329 return (!bfd_is_abs_section (csect
)
3330 && h
->root
.u
.def
.section
== csect
);
3332 case bfd_link_hash_common
:
3333 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3335 case bfd_link_hash_undefined
:
3336 case bfd_link_hash_undefweak
:
3337 /* We can't treat undef.abfd as the owner because that bfd
3338 might be a dynamic object. Allow any bfd to claim it. */
3346 /* See if H should have a loader symbol associated with it. */
3349 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3350 struct xcoff_link_hash_entry
*h
)
3354 /* Warn if this symbol is exported but not defined. */
3355 if ((h
->flags
& XCOFF_EXPORT
) != 0
3356 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3359 (_("warning: attempt to export undefined symbol `%s'"),
3360 h
->root
.root
.string
);
3364 /* We need to add a symbol to the .loader section if it is mentioned
3365 in a reloc which we are copying to the .loader section and it was
3366 not defined or common, or if it is the entry point, or if it is
3368 if (((h
->flags
& XCOFF_LDREL
) == 0
3369 || h
->root
.type
== bfd_link_hash_defined
3370 || h
->root
.type
== bfd_link_hash_defweak
3371 || h
->root
.type
== bfd_link_hash_common
)
3372 && (h
->flags
& XCOFF_ENTRY
) == 0
3373 && (h
->flags
& XCOFF_EXPORT
) == 0)
3376 /* We need to add this symbol to the .loader symbols. */
3378 BFD_ASSERT (h
->ldsym
== NULL
);
3379 amt
= sizeof (struct internal_ldsym
);
3380 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3381 if (h
->ldsym
== NULL
)
3383 ldinfo
->failed
= true;
3387 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3389 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3390 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3392 h
->ldsym
->l_ifile
= h
->ldindx
;
3395 /* The first 3 symbol table indices are reserved to indicate the
3396 data, text and bss sections. */
3397 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3399 ++ldinfo
->ldsym_count
;
3401 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3402 h
->ldsym
, h
->root
.root
.string
))
3405 h
->flags
|= XCOFF_BUILT_LDSYM
;
3409 /* An xcoff_htab_traverse callback that is called for each symbol
3410 once garbage collection is complete. */
3413 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3415 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3417 /* __rtinit, this symbol has special handling. */
3418 if (h
->flags
& XCOFF_RTINIT
)
3421 /* We don't want to garbage collect symbols which are not defined in
3422 XCOFF files. This is a convenient place to mark them. */
3423 if (xcoff_hash_table (ldinfo
->info
)->gc
3424 && (h
->flags
& XCOFF_MARK
) == 0
3425 && (h
->root
.type
== bfd_link_hash_defined
3426 || h
->root
.type
== bfd_link_hash_defweak
)
3427 && (h
->root
.u
.def
.section
->owner
== NULL
3428 || (h
->root
.u
.def
.section
->owner
->xvec
3429 != ldinfo
->info
->output_bfd
->xvec
)))
3430 h
->flags
|= XCOFF_MARK
;
3432 /* Skip discarded symbols. */
3433 if (xcoff_hash_table (ldinfo
->info
)->gc
3434 && (h
->flags
& XCOFF_MARK
) == 0)
3437 /* If this is still a common symbol, and it wasn't garbage
3438 collected, we need to actually allocate space for it in the .bss
3440 if (h
->root
.type
== bfd_link_hash_common
3441 && h
->root
.u
.c
.p
->section
->size
== 0)
3443 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3444 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3447 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3449 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3450 h
->flags
|= XCOFF_EXPORT
;
3452 if (!xcoff_build_ldsym (ldinfo
, h
))
3459 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3460 hash table entry H and csect CSECT. AUX contains ISYM's auxiliary
3461 csect information, if any. NAME is the function's name if the name
3462 is stored in the .debug section, otherwise it is null.
3464 Return 1 if we should include an appropriately-adjusted ISYM
3465 in the output file, 0 if we should discard ISYM, or -1 if an
3469 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3470 struct internal_syment
*isym
,
3471 union internal_auxent
*aux
,
3472 struct xcoff_link_hash_entry
*h
,
3473 asection
*csect
, const char *name
)
3477 /* If we are skipping this csect, we want to strip the symbol too. */
3481 /* Likewise if we garbage-collected the csect. */
3482 if (xcoff_hash_table (info
)->gc
3483 && !bfd_is_abs_section (csect
)
3484 && !bfd_is_und_section (csect
)
3485 && csect
->gc_mark
== 0)
3488 /* An XCOFF linker always removes C_STAT symbols. */
3489 if (isym
->n_sclass
== C_STAT
)
3492 /* We generate the TOC anchor separately. */
3493 if (isym
->n_sclass
== C_HIDEXT
3494 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3497 /* If we are stripping all symbols, we want to discard this one. */
3498 if (info
->strip
== strip_all
)
3501 /* Discard symbols that are defined elsewhere. */
3502 if (EXTERN_SYM_P (isym
->n_sclass
))
3504 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3506 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3510 /* If we're discarding local symbols, check whether ISYM is local. */
3511 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3512 if (info
->discard
== discard_all
3513 && !EXTERN_SYM_P (isym
->n_sclass
)
3514 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3517 /* If we're stripping debugging symbols, check whether ISYM is one. */
3518 if (info
->strip
== strip_debugger
3519 && isym
->n_scnum
== N_DEBUG
)
3522 /* If we are stripping symbols based on name, check how ISYM's
3523 name should be handled. */
3524 if (info
->strip
== strip_some
3525 || info
->discard
== discard_l
)
3527 char buf
[SYMNMLEN
+ 1];
3531 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3536 if (info
->strip
== strip_some
3537 && bfd_hash_lookup (info
->keep_hash
, name
, false, false) == NULL
)
3540 if (info
->discard
== discard_l
3541 && !EXTERN_SYM_P (isym
->n_sclass
)
3542 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3543 && bfd_is_local_label_name (input_bfd
, name
))
3550 /* Lay out the .loader section, filling in the header and the import paths.
3551 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3554 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
,
3555 const char *libpath
)
3558 struct xcoff_link_hash_table
*htab
;
3559 struct internal_ldhdr
*ldhdr
;
3560 struct xcoff_import_file
*fl
;
3561 bfd_size_type stoff
;
3562 size_t impsize
, impcount
;
3566 /* Work out the size of the import file names. Each import file ID
3567 consists of three null terminated strings: the path, the file
3568 name, and the archive member name. The first entry in the list
3569 of names is the path to use to find objects, which the linker has
3570 passed in as the libpath argument. For some reason, the path
3571 entry in the other import file names appears to always be empty. */
3572 output_bfd
= ldinfo
->output_bfd
;
3573 htab
= xcoff_hash_table (ldinfo
->info
);
3574 impsize
= strlen (libpath
) + 3;
3576 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3579 impsize
+= (strlen (fl
->path
)
3581 + strlen (fl
->member
)
3585 /* Set up the .loader section header. */
3586 ldhdr
= &htab
->ldhdr
;
3587 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3588 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3589 ldhdr
->l_nreloc
= htab
->ldrel_count
;
3590 ldhdr
->l_istlen
= impsize
;
3591 ldhdr
->l_nimpid
= impcount
;
3592 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3593 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3594 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3595 ldhdr
->l_stlen
= ldinfo
->string_size
;
3596 stoff
= ldhdr
->l_impoff
+ impsize
;
3597 if (ldinfo
->string_size
== 0)
3600 ldhdr
->l_stoff
= stoff
;
3602 /* 64 bit elements to ldhdr
3603 The swap out routine for 32 bit will ignore them.
3604 Nothing fancy, symbols come after the header and relocs come
3606 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3607 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3608 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3610 /* We now know the final size of the .loader section. Allocate
3612 lsec
= htab
->loader_section
;
3613 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3614 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3615 if (lsec
->contents
== NULL
)
3618 /* Set up the header. */
3619 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3621 /* Set up the import file names. */
3622 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3623 strcpy (out
, libpath
);
3624 out
+= strlen (libpath
) + 1;
3627 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3632 while ((*out
++ = *s
++) != '\0')
3635 while ((*out
++ = *s
++) != '\0')
3638 while ((*out
++ = *s
++) != '\0')
3642 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3644 /* Set up the symbol string table. */
3645 if (ldinfo
->string_size
> 0)
3647 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
3648 free (ldinfo
->strings
);
3649 ldinfo
->strings
= NULL
;
3652 /* We can't set up the symbol table or the relocs yet, because we
3653 don't yet know the final position of the various sections. The
3654 .loader symbols are written out when the corresponding normal
3655 symbols are written out in xcoff_link_input_bfd or
3656 xcoff_write_global_symbol. The .loader relocs are written out
3657 when the corresponding normal relocs are handled in
3658 xcoff_link_input_bfd. */
3663 /* Build the .loader section. This is called by the XCOFF linker
3664 emulation before_allocation routine. We must set the size of the
3665 .loader section before the linker lays out the output file.
3666 LIBPATH is the library path to search for shared objects; this is
3667 normally built from the -L arguments passed to the linker. ENTRY
3668 is the name of the entry point symbol (the -e linker option).
3669 FILE_ALIGN is the alignment to use for sections within the file
3670 (the -H linker option). MAXSTACK is the maximum stack size (the
3671 -bmaxstack linker option). MAXDATA is the maximum data size (the
3672 -bmaxdata linker option). GC is whether to do garbage collection
3673 (the -bgc linker option). MODTYPE is the module type (the
3674 -bmodtype linker option). TEXTRO is whether the text section must
3675 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3676 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3677 is set by this routine to csects with magic names like _end. */
3680 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3681 struct bfd_link_info
*info
,
3682 const char *libpath
,
3684 unsigned long file_align
,
3685 unsigned long maxstack
,
3686 unsigned long maxdata
,
3690 unsigned int auto_export_flags
,
3691 asection
**special_sections
,
3694 struct xcoff_loader_info ldinfo
;
3698 struct bfd_strtab_hash
*debug_strtab
;
3699 bfd_byte
*debug_contents
= NULL
;
3702 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3704 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3705 special_sections
[i
] = NULL
;
3709 ldinfo
.failed
= false;
3710 ldinfo
.output_bfd
= output_bfd
;
3712 ldinfo
.auto_export_flags
= auto_export_flags
;
3713 ldinfo
.ldsym_count
= 0;
3714 ldinfo
.string_size
= 0;
3715 ldinfo
.strings
= NULL
;
3716 ldinfo
.string_alc
= 0;
3718 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3719 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3720 xcoff_data (output_bfd
)->modtype
= modtype
;
3722 xcoff_hash_table (info
)->file_align
= file_align
;
3723 xcoff_hash_table (info
)->textro
= textro
;
3724 xcoff_hash_table (info
)->rtld
= rtld
;
3727 if (xcoff_hash_table (info
)->loader_section
3728 && (info
->init_function
|| info
->fini_function
|| rtld
))
3730 struct xcoff_link_hash_entry
*hsym
;
3731 struct internal_ldsym
*ldsym
;
3733 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3734 "__rtinit", false, false, true);
3738 (_("error: undefined symbol __rtinit"));
3742 xcoff_mark_symbol (info
, hsym
);
3743 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3745 /* __rtinit initialized. */
3746 amt
= sizeof (* ldsym
);
3747 ldsym
= bfd_malloc (amt
);
3749 ldsym
->l_value
= 0; /* Will be filled in later. */
3750 ldsym
->l_scnum
= 2; /* Data section. */
3751 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3752 ldsym
->l_smclas
= 5; /* .rw. */
3753 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3754 ldsym
->l_parm
= 0; /* NA. */
3756 /* Force __rtinit to be the first symbol in the loader symbol table
3757 See xcoff_build_ldsyms
3759 The first 3 symbol table indices are reserved to indicate the data,
3760 text and bss sections. */
3761 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3764 ldinfo
.ldsym_count
= 1;
3765 hsym
->ldsym
= ldsym
;
3767 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3768 hsym
->ldsym
, hsym
->root
.root
.string
))
3771 /* This symbol is written out by xcoff_write_global_symbol
3772 Set stuff up so xcoff_write_global_symbol logic works. */
3773 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3774 hsym
->root
.type
= bfd_link_hash_defined
;
3775 hsym
->root
.u
.def
.value
= 0;
3778 /* Garbage collect unused sections. */
3779 if (bfd_link_relocatable (info
) || !gc
)
3782 xcoff_hash_table (info
)->gc
= false;
3784 /* We still need to call xcoff_mark, in order to set ldrel_count
3786 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3790 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3792 /* We shouldn't unconditionaly mark the TOC section.
3793 The output file should only have a TOC if either
3794 (a) one of the input files did or (b) we end up
3795 creating TOC references as part of the link process. */
3796 if (o
!= xcoff_hash_table (info
)->toc_section
3799 if (! xcoff_mark (info
, o
))
3808 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3810 if (info
->init_function
!= NULL
3811 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3813 if (info
->fini_function
!= NULL
3814 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3816 if (auto_export_flags
!= 0)
3818 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3819 xcoff_mark_auto_exports
, &ldinfo
);
3824 xcoff_hash_table (info
)->gc
= true;
3827 /* Return special sections to the caller. */
3828 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3830 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3834 && sec
->gc_mark
== 0)
3837 special_sections
[i
] = sec
;
3840 if (info
->input_bfds
== NULL
)
3841 /* I'm not sure what to do in this bizarre case. */
3844 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3849 if (xcoff_hash_table (info
)->loader_section
3850 && !xcoff_build_loader_section (&ldinfo
, libpath
))
3853 /* Allocate space for the magic sections. */
3854 sec
= xcoff_hash_table (info
)->linkage_section
;
3857 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3858 if (sec
->contents
== NULL
)
3861 sec
= xcoff_hash_table (info
)->toc_section
;
3864 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3865 if (sec
->contents
== NULL
)
3868 sec
= xcoff_hash_table (info
)->descriptor_section
;
3871 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3872 if (sec
->contents
== NULL
)
3876 /* Now that we've done garbage collection, decide which symbols to keep,
3877 and figure out the contents of the .debug section. */
3878 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3880 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3883 bfd_size_type symcount
;
3886 unsigned int *lineno_counts
;
3887 struct xcoff_link_hash_entry
**sym_hash
;
3888 bfd_byte
*esym
, *esymend
;
3889 bfd_size_type symesz
;
3891 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3894 if ((sub
->flags
& DYNAMIC
) != 0
3895 && !info
->static_link
)
3898 if (! _bfd_coff_get_external_symbols (sub
))
3901 symcount
= obj_raw_syment_count (sub
);
3902 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
3903 if (debug_index
== NULL
)
3905 xcoff_data (sub
)->debug_indices
= debug_index
;
3907 if (info
->strip
== strip_all
3908 || info
->strip
== strip_debugger
3909 || info
->discard
== discard_all
)
3910 /* We're stripping all debugging information, so there's no need
3911 to read SUB's .debug section. */
3915 /* Grab the contents of SUB's .debug section, if any. */
3916 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3917 if (subdeb
!= NULL
&& subdeb
->size
> 0)
3919 /* We use malloc and copy the names into the debug
3920 stringtab, rather than bfd_alloc, because I expect
3921 that, when linking many files together, many of the
3922 strings will be the same. Storing the strings in the
3923 hash table should save space in this case. */
3924 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
3929 csectpp
= xcoff_data (sub
)->csects
;
3930 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
3931 sym_hash
= obj_xcoff_sym_hashes (sub
);
3932 symesz
= bfd_coff_symesz (sub
);
3933 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3934 esymend
= esym
+ symcount
* symesz
;
3936 while (esym
< esymend
)
3938 struct internal_syment sym
;
3939 union internal_auxent aux
;
3944 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
3946 /* Read in the csect information, if any. */
3947 if (CSECT_SYM_P (sym
.n_sclass
))
3949 BFD_ASSERT (sym
.n_numaux
> 0);
3950 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
3951 sym
.n_type
, sym
.n_sclass
,
3952 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
3955 /* If this symbol's name is stored in the debug section,
3956 get a pointer to it. */
3957 if (debug_contents
!= NULL
3958 && sym
._n
._n_n
._n_zeroes
== 0
3959 && bfd_coff_symname_in_debug (sub
, &sym
))
3960 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3964 /* Decide whether to copy this symbol to the output file. */
3966 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
3967 *sym_hash
, csect
, name
);
3972 /* Use a debug_index of -2 to record that a symbol should
3977 /* See whether we should store the symbol name in the
3978 output .debug section. */
3983 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
3984 if (indx
== (bfd_size_type
) -1)
3986 *debug_index
= indx
;
3991 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
3992 if (*lineno_counts
> 0)
3993 csect
->output_section
->lineno_count
+= *lineno_counts
;
3996 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3997 csectpp
+= sym
.n_numaux
+ 1;
3998 sym_hash
+= sym
.n_numaux
+ 1;
3999 lineno_counts
+= sym
.n_numaux
+ 1;
4000 debug_index
+= sym
.n_numaux
+ 1;
4005 free (debug_contents
);
4006 debug_contents
= NULL
;
4008 /* Clear the size of subdeb, so that it is not included directly
4009 in the output file. */
4013 if (! info
->keep_memory
)
4015 if (! _bfd_coff_free_symbols (sub
))
4020 if (info
->strip
!= strip_all
4021 && xcoff_hash_table (info
)->debug_section
!= NULL
)
4022 xcoff_hash_table (info
)->debug_section
->size
=
4023 _bfd_stringtab_size (debug_strtab
);
4028 free (ldinfo
.strings
);
4029 free (debug_contents
);
4034 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
4039 struct bfd_in_memory
*bim
;
4041 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
4048 abfd
->link
.next
= 0;
4049 abfd
->format
= bfd_object
;
4050 abfd
->iostream
= (void *) bim
;
4051 abfd
->flags
= BFD_IN_MEMORY
;
4052 abfd
->iovec
= &_bfd_memory_iovec
;
4053 abfd
->direction
= write_direction
;
4057 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
4060 /* need to reset to unknown or it will not be read back in correctly */
4061 abfd
->format
= bfd_unknown
;
4062 abfd
->direction
= read_direction
;
4068 /* Return the section that defines H. Return null if no section does. */
4071 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
4073 switch (h
->root
.type
)
4075 case bfd_link_hash_defined
:
4076 case bfd_link_hash_defweak
:
4077 return h
->root
.u
.def
.section
;
4079 case bfd_link_hash_common
:
4080 return h
->root
.u
.c
.p
->section
;
4087 /* Add a .loader relocation for input relocation IREL. If the loader
4088 relocation should be against an output section, HSEC points to the
4089 input section that IREL is against, otherwise HSEC is null. H is the
4090 symbol that IREL is against, or null if it isn't against a global symbol.
4091 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4094 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
4095 asection
*output_section
, bfd
*reference_bfd
,
4096 struct internal_reloc
*irel
, asection
*hsec
,
4097 struct xcoff_link_hash_entry
*h
)
4099 struct internal_ldrel ldrel
;
4101 ldrel
.l_vaddr
= irel
->r_vaddr
;
4104 const char *secname
;
4106 secname
= hsec
->output_section
->name
;
4107 if (strcmp (secname
, ".text") == 0)
4109 else if (strcmp (secname
, ".data") == 0)
4111 else if (strcmp (secname
, ".bss") == 0)
4113 else if (strcmp (secname
, ".tdata") == 0)
4114 ldrel
.l_symndx
= -1;
4115 else if (strcmp (secname
, ".tbss") == 0)
4116 ldrel
.l_symndx
= -2;
4120 /* xgettext:c-format */
4121 (_("%pB: loader reloc in unrecognized section `%s'"),
4122 reference_bfd
, secname
);
4123 bfd_set_error (bfd_error_nonrepresentable_section
);
4132 /* xgettext:c-format */
4133 (_("%pB: `%s' in loader reloc but not loader sym"),
4134 reference_bfd
, h
->root
.root
.string
);
4135 bfd_set_error (bfd_error_bad_value
);
4138 ldrel
.l_symndx
= h
->ldindx
;
4141 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4143 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4144 ldrel
.l_rsecnm
= output_section
->target_index
;
4145 if (xcoff_hash_table (flinfo
->info
)->textro
4146 && strcmp (output_section
->name
, ".text") == 0)
4149 /* xgettext:c-format */
4150 (_("%pB: loader reloc in read-only section %pA"),
4151 reference_bfd
, output_section
);
4152 bfd_set_error (bfd_error_invalid_operation
);
4155 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
4156 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4160 /* Link an input file into the linker output file. This function
4161 handles all the sections and relocations of the input file at once. */
4164 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
4168 const char *strings
;
4169 bfd_size_type syment_base
;
4170 unsigned int n_tmask
;
4171 unsigned int n_btshft
;
4173 bfd_size_type isymesz
;
4174 bfd_size_type osymesz
;
4175 bfd_size_type linesz
;
4178 struct xcoff_link_hash_entry
**sym_hash
;
4179 struct internal_syment
*isymp
;
4181 unsigned int *lineno_counts
;
4184 unsigned long output_index
;
4191 /* We can just skip DYNAMIC files, unless this is a static link. */
4192 if ((input_bfd
->flags
& DYNAMIC
) != 0
4193 && ! flinfo
->info
->static_link
)
4196 /* Move all the symbols to the output file. */
4197 output_bfd
= flinfo
->output_bfd
;
4199 syment_base
= obj_raw_syment_count (output_bfd
);
4200 isymesz
= bfd_coff_symesz (input_bfd
);
4201 osymesz
= bfd_coff_symesz (output_bfd
);
4202 linesz
= bfd_coff_linesz (input_bfd
);
4203 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4205 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4206 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4208 /* Define macros so that ISFCN, et. al., macros work correctly. */
4209 #define N_TMASK n_tmask
4210 #define N_BTSHFT n_btshft
4213 if (! flinfo
->info
->keep_memory
)
4216 if (flinfo
->info
->traditional_format
)
4219 if (! _bfd_coff_get_external_symbols (input_bfd
))
4222 /* Make one pass over the symbols and assign indices to symbols that
4223 we have decided to keep. Also use create .loader symbol information
4224 and update information in hash table entries. */
4225 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4226 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4227 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4228 csectpp
= xcoff_data (input_bfd
)->csects
;
4229 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4230 isymp
= flinfo
->internal_syms
;
4231 indexp
= flinfo
->sym_indices
;
4232 output_index
= syment_base
;
4233 while (esym
< esym_end
)
4235 union internal_auxent aux
;
4239 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4241 /* Read in the csect information, if any. */
4242 if (CSECT_SYM_P (isymp
->n_sclass
))
4244 BFD_ASSERT (isymp
->n_numaux
> 0);
4245 bfd_coff_swap_aux_in (input_bfd
,
4246 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4247 isymp
->n_type
, isymp
->n_sclass
,
4248 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4251 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4254 /* If this symbol is in the .loader section, swap out the
4255 .loader symbol information. If this is an external symbol
4256 reference to a defined symbol, though, then wait until we get
4257 to the definition. */
4258 if (EXTERN_SYM_P (isymp
->n_sclass
)
4259 && *sym_hash
!= NULL
4260 && (*sym_hash
)->ldsym
!= NULL
4261 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4263 struct xcoff_link_hash_entry
*h
;
4264 struct internal_ldsym
*ldsym
;
4268 if (isymp
->n_scnum
> 0)
4270 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4271 ldsym
->l_value
= (isymp
->n_value
4272 + (*csectpp
)->output_section
->vma
4273 + (*csectpp
)->output_offset
4278 ldsym
->l_scnum
= isymp
->n_scnum
;
4279 ldsym
->l_value
= isymp
->n_value
;
4282 ldsym
->l_smtype
= smtyp
;
4283 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4284 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4285 || (h
->flags
& XCOFF_IMPORT
) != 0)
4286 ldsym
->l_smtype
|= L_IMPORT
;
4287 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4288 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4289 || (h
->flags
& XCOFF_EXPORT
) != 0)
4290 ldsym
->l_smtype
|= L_EXPORT
;
4291 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4292 ldsym
->l_smtype
|= L_ENTRY
;
4293 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4294 ldsym
->l_smtype
|= L_WEAK
;
4296 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4298 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4300 else if (ldsym
->l_ifile
== 0)
4302 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4308 if (h
->root
.type
== bfd_link_hash_defined
4309 || h
->root
.type
== bfd_link_hash_defweak
)
4310 impbfd
= h
->root
.u
.def
.section
->owner
;
4311 else if (h
->root
.type
== bfd_link_hash_undefined
4312 || h
->root
.type
== bfd_link_hash_undefweak
)
4313 impbfd
= h
->root
.u
.undef
.abfd
;
4321 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
4322 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4329 BFD_ASSERT (h
->ldindx
>= 0);
4330 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
4333 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
4336 /* Fill in snentry now that we know the target_index. */
4337 if ((h
->flags
& XCOFF_ENTRY
) != 0
4338 && (h
->root
.type
== bfd_link_hash_defined
4339 || h
->root
.type
== bfd_link_hash_defweak
))
4341 xcoff_data (output_bfd
)->snentry
=
4342 h
->root
.u
.def
.section
->output_section
->target_index
;
4346 add
= 1 + isymp
->n_numaux
;
4348 if (*debug_index
== -2)
4349 /* We've decided to strip this symbol. */
4353 /* Assign the next unused index to this symbol. */
4354 *indexp
= output_index
;
4356 if (EXTERN_SYM_P (isymp
->n_sclass
))
4358 BFD_ASSERT (*sym_hash
!= NULL
);
4359 (*sym_hash
)->indx
= output_index
;
4362 /* If this is a symbol in the TOC which we may have merged
4363 (class XMC_TC), remember the symbol index of the TOC
4365 if (isymp
->n_sclass
== C_HIDEXT
4366 && aux
.x_csect
.x_smclas
== XMC_TC
4367 && *sym_hash
!= NULL
)
4369 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4370 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4371 (*sym_hash
)->u
.toc_indx
= output_index
;
4374 output_index
+= add
;
4377 esym
+= add
* isymesz
;
4383 for (--add
; add
> 0; --add
)
4387 /* Now write out the symbols that we decided to keep. */
4389 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4390 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4391 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4392 isymp
= flinfo
->internal_syms
;
4393 indexp
= flinfo
->sym_indices
;
4394 csectpp
= xcoff_data (input_bfd
)->csects
;
4395 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4396 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4397 outsym
= flinfo
->outsyms
;
4400 while (esym
< esym_end
)
4404 add
= 1 + isymp
->n_numaux
;
4407 esym
+= add
* isymesz
;
4410 struct internal_syment isym
;
4413 /* Adjust the symbol in order to output it. */
4415 if (isym
._n
._n_n
._n_zeroes
== 0
4416 && isym
._n
._n_n
._n_offset
!= 0)
4418 /* This symbol has a long name. Enter it in the string
4419 table we are building. If *debug_index != -1, the
4420 name has already been entered in the .debug section. */
4421 if (*debug_index
>= 0)
4422 isym
._n
._n_n
._n_offset
= *debug_index
;
4428 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4432 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
4433 if (indx
== (bfd_size_type
) -1)
4435 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4439 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4440 multiple definition problems when linking a shared object
4441 statically. (The native linker doesn't enter __rtinit into
4442 the normal table at all, but having a local symbol can make
4443 the objdump output easier to read.) */
4444 if (isym
.n_sclass
== C_EXT
4446 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4447 isym
.n_sclass
= C_HIDEXT
;
4449 /* The value of a C_FILE symbol is the symbol index of the
4450 next C_FILE symbol. The value of the last C_FILE symbol
4451 is -1. We try to get this right, below, just before we
4452 write the symbols out, but in the general case we may
4453 have to write the symbol out twice. */
4454 if (isym
.n_sclass
== C_FILE
)
4456 if (flinfo
->last_file_index
!= -1
4457 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4459 /* We must correct the value of the last C_FILE entry. */
4460 flinfo
->last_file
.n_value
= *indexp
;
4461 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4463 /* The last C_FILE symbol is in this input file. */
4464 bfd_coff_swap_sym_out (output_bfd
,
4465 (void *) &flinfo
->last_file
,
4466 (void *) (flinfo
->outsyms
4467 + ((flinfo
->last_file_index
4473 /* We have already written out the last C_FILE
4474 symbol. We need to write it out again. We
4475 borrow *outsym temporarily. */
4478 bfd_coff_swap_sym_out (output_bfd
,
4479 (void *) &flinfo
->last_file
,
4482 pos
= obj_sym_filepos (output_bfd
);
4483 pos
+= flinfo
->last_file_index
* osymesz
;
4484 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4485 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4491 flinfo
->last_file_index
= *indexp
;
4492 flinfo
->last_file
= isym
;
4495 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4496 into the line numbers. We update the symbol values when
4497 we handle the line numbers. */
4498 if (isym
.n_sclass
== C_BINCL
4499 || isym
.n_sclass
== C_EINCL
)
4501 isym
.n_value
= flinfo
->line_filepos
;
4504 /* The value of a C_BSTAT symbol is the symbol table
4505 index of the containing csect. */
4506 else if (isym
.n_sclass
== C_BSTAT
)
4510 indx
= isym
.n_value
;
4511 if (indx
< obj_raw_syment_count (input_bfd
))
4515 symindx
= flinfo
->sym_indices
[indx
];
4519 isym
.n_value
= symindx
;
4522 else if (isym
.n_sclass
!= C_ESTAT
4523 && isym
.n_sclass
!= C_DECL
4524 && isym
.n_scnum
> 0)
4526 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4527 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4528 + (*csectpp
)->output_offset
4532 /* Output the symbol. */
4533 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4538 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4540 union internal_auxent aux
;
4542 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4543 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4546 if (isymp
->n_sclass
== C_FILE
)
4548 /* This is the file name (or some comment put in by
4549 the compiler). If it is long, we must put it in
4550 the string table. */
4551 if (aux
.x_file
.x_n
.x_n
.x_zeroes
== 0
4552 && aux
.x_file
.x_n
.x_n
.x_offset
!= 0)
4554 const char *filename
;
4557 BFD_ASSERT (aux
.x_file
.x_n
.x_n
.x_offset
4558 >= STRING_SIZE_SIZE
);
4559 if (strings
== NULL
)
4561 strings
= _bfd_coff_read_string_table (input_bfd
);
4562 if (strings
== NULL
)
4565 if ((bfd_size_type
) aux
.x_file
.x_n
.x_n
.x_offset
>= obj_coff_strings_len (input_bfd
))
4566 filename
= _("<corrupt>");
4568 filename
= strings
+ aux
.x_file
.x_n
.x_n
.x_offset
;
4569 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
4571 if (indx
== (bfd_size_type
) -1)
4573 aux
.x_file
.x_n
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4576 else if (CSECT_SYM_P (isymp
->n_sclass
)
4577 && i
+ 1 == isymp
->n_numaux
)
4580 /* We don't support type checking. I don't know if
4582 aux
.x_csect
.x_parmhash
= 0;
4583 /* I don't think anybody uses these fields, but we'd
4584 better clobber them just in case. */
4585 aux
.x_csect
.x_stab
= 0;
4586 aux
.x_csect
.x_snstab
= 0;
4588 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4592 indx
= aux
.x_csect
.x_scnlen
.l
;
4593 if (indx
< obj_raw_syment_count (input_bfd
))
4597 symindx
= flinfo
->sym_indices
[indx
];
4600 aux
.x_csect
.x_scnlen
.l
= 0;
4604 aux
.x_csect
.x_scnlen
.l
= symindx
;
4609 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4613 if (ISFCN (isymp
->n_type
)
4614 || ISTAG (isymp
->n_sclass
)
4615 || isymp
->n_sclass
== C_BLOCK
4616 || isymp
->n_sclass
== C_FCN
)
4618 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4620 && indx
< obj_raw_syment_count (input_bfd
))
4622 /* We look forward through the symbol for
4623 the index of the next symbol we are going
4624 to include. I don't know if this is
4626 while (flinfo
->sym_indices
[indx
] < 0
4627 && indx
< obj_raw_syment_count (input_bfd
))
4629 if (indx
>= obj_raw_syment_count (input_bfd
))
4630 indx
= output_index
;
4632 indx
= flinfo
->sym_indices
[indx
];
4633 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4638 indx
= aux
.x_sym
.x_tagndx
.l
;
4639 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4643 symindx
= flinfo
->sym_indices
[indx
];
4645 aux
.x_sym
.x_tagndx
.l
= 0;
4647 aux
.x_sym
.x_tagndx
.l
= symindx
;
4652 /* Copy over the line numbers, unless we are stripping
4653 them. We do this on a symbol by symbol basis in
4654 order to more easily handle garbage collection. */
4655 if (CSECT_SYM_P (isymp
->n_sclass
)
4657 && isymp
->n_numaux
> 1
4658 && ISFCN (isymp
->n_type
)
4659 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4661 if (*lineno_counts
== 0)
4662 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4665 asection
*enclosing
;
4666 unsigned int enc_count
;
4667 bfd_signed_vma linoff
;
4668 struct internal_lineno lin
;
4675 /* Read in the enclosing section's line-number
4676 information, if we haven't already. */
4678 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4679 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4680 if (oline
!= enclosing
)
4682 pos
= enclosing
->line_filepos
;
4683 amt
= linesz
* enc_count
;
4684 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4685 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
4691 /* Copy across the first entry, adjusting its
4693 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4694 - enclosing
->line_filepos
);
4695 linp
= flinfo
->linenos
+ linoff
;
4696 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4697 lin
.l_addr
.l_symndx
= *indexp
;
4698 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4700 /* Copy the other entries, adjusting their addresses. */
4701 linpend
= linp
+ *lineno_counts
* linesz
;
4702 offset
= (o
->output_section
->vma
4705 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4707 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4708 lin
.l_addr
.l_paddr
+= offset
;
4709 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4712 /* Write out the entries we've just processed. */
4713 pos
= (o
->output_section
->line_filepos
4714 + o
->output_section
->lineno_count
* linesz
);
4715 amt
= linesz
* *lineno_counts
;
4716 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4717 || bfd_bwrite (flinfo
->linenos
+ linoff
,
4718 amt
, output_bfd
) != amt
)
4720 o
->output_section
->lineno_count
+= *lineno_counts
;
4722 /* Record the offset of the symbol's line numbers
4723 in the output file. */
4724 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4728 struct internal_syment
*iisp
, *iispend
;
4731 bfd_vma range_start
, range_end
;
4734 /* Update any C_BINCL or C_EINCL symbols
4735 that refer to a line number in the
4736 range we just output. */
4737 iisp
= flinfo
->internal_syms
;
4738 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4739 iindp
= flinfo
->sym_indices
;
4740 oos
= flinfo
->outsyms
;
4741 range_start
= enclosing
->line_filepos
+ linoff
;
4742 range_end
= range_start
+ *lineno_counts
* linesz
;
4743 while (iisp
< iispend
)
4746 && (iisp
->n_sclass
== C_BINCL
4747 || iisp
->n_sclass
== C_EINCL
)
4748 && iisp
->n_value
>= range_start
4749 && iisp
->n_value
< range_end
)
4751 struct internal_syment iis
;
4753 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4754 iis
.n_value
= (iisp
->n_value
4757 bfd_coff_swap_sym_out (output_bfd
,
4762 iiadd
= 1 + iisp
->n_numaux
;
4764 oos
+= iiadd
* osymesz
;
4772 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4773 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4784 lineno_counts
+= add
;
4788 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4789 symbol will be the first symbol in the next input file. In the
4790 normal case, this will save us from writing out the C_FILE symbol
4792 if (flinfo
->last_file_index
!= -1
4793 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4795 flinfo
->last_file
.n_value
= output_index
;
4796 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
4797 (void *) (flinfo
->outsyms
4798 + ((flinfo
->last_file_index
- syment_base
)
4802 /* Write the modified symbols to the output file. */
4803 if (outsym
> flinfo
->outsyms
)
4805 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4806 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
4807 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4808 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
4811 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4812 + (outsym
- flinfo
->outsyms
) / osymesz
)
4815 obj_raw_syment_count (output_bfd
) = output_index
;
4818 /* Don't let the linker relocation routines discard the symbols. */
4819 keep_syms
= obj_coff_keep_syms (input_bfd
);
4820 obj_coff_keep_syms (input_bfd
) = true;
4822 /* Relocate the contents of each section. */
4823 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4827 if (! o
->linker_mark
)
4828 /* This section was omitted from the link. */
4831 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4833 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4836 /* We have set filepos correctly for the sections we created to
4837 represent csects, so bfd_get_section_contents should work. */
4838 if (coff_section_data (input_bfd
, o
) != NULL
4839 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4840 contents
= coff_section_data (input_bfd
, o
)->contents
;
4843 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4844 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
4846 contents
= flinfo
->contents
;
4849 if ((o
->flags
& SEC_RELOC
) != 0)
4852 struct internal_reloc
*internal_relocs
;
4853 struct internal_reloc
*irel
;
4855 struct internal_reloc
*irelend
;
4856 struct xcoff_link_hash_entry
**rel_hash
;
4859 /* Read in the relocs. */
4860 target_index
= o
->output_section
->target_index
;
4861 internal_relocs
= (xcoff_read_internal_relocs
4862 (input_bfd
, o
, false, flinfo
->external_relocs
,
4864 (flinfo
->section_info
[target_index
].relocs
4865 + o
->output_section
->reloc_count
)));
4866 if (internal_relocs
== NULL
)
4869 /* Call processor specific code to relocate the section
4871 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
4875 flinfo
->internal_syms
,
4876 xcoff_data (input_bfd
)->csects
))
4879 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4880 irel
= internal_relocs
;
4881 irelend
= irel
+ o
->reloc_count
;
4882 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
4883 + o
->output_section
->reloc_count
);
4884 for (; irel
< irelend
; irel
++, rel_hash
++)
4886 struct xcoff_link_hash_entry
*h
= NULL
;
4890 /* Adjust the reloc address and symbol index. */
4892 irel
->r_vaddr
+= offset
;
4894 r_symndx
= irel
->r_symndx
;
4899 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4901 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
4904 && h
->smclas
!= XMC_TD
4905 && (irel
->r_type
== R_TOC
4906 || irel
->r_type
== R_GL
4907 || irel
->r_type
== R_TCL
4908 || irel
->r_type
== R_TRL
4909 || irel
->r_type
== R_TRLA
))
4911 /* This is a TOC relative reloc with a symbol
4912 attached. The symbol should be the one which
4913 this reloc is for. We want to make this
4914 reloc against the TOC address of the symbol,
4915 not the symbol itself. */
4916 BFD_ASSERT (h
->toc_section
!= NULL
);
4917 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4918 if (h
->u
.toc_indx
!= -1)
4919 irel
->r_symndx
= h
->u
.toc_indx
;
4922 struct xcoff_toc_rel_hash
*n
;
4923 struct xcoff_link_section_info
*si
;
4927 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
4930 si
= flinfo
->section_info
+ target_index
;
4931 n
->next
= si
->toc_rel_hashes
;
4934 si
->toc_rel_hashes
= n
;
4939 /* This is a global symbol. */
4941 irel
->r_symndx
= h
->indx
;
4944 /* This symbol is being written at the end
4945 of the file, and we do not yet know the
4946 symbol index. We save the pointer to the
4947 hash table entry in the rel_hash list.
4948 We set the indx field to -2 to indicate
4949 that this symbol must not be stripped. */
4958 indx
= flinfo
->sym_indices
[r_symndx
];
4962 struct internal_syment
*is
;
4964 /* Relocations against a TC0 TOC anchor are
4965 automatically transformed to be against
4966 the TOC anchor in the output file. */
4967 is
= flinfo
->internal_syms
+ r_symndx
;
4968 if (is
->n_sclass
== C_HIDEXT
4969 && is
->n_numaux
> 0)
4972 union internal_auxent aux
;
4976 obj_coff_external_syms (input_bfd
))
4977 + ((r_symndx
+ is
->n_numaux
)
4979 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4980 is
->n_type
, is
->n_sclass
,
4984 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4985 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4986 indx
= flinfo
->toc_symindx
;
4991 irel
->r_symndx
= indx
;
4995 struct internal_syment
*is
;
4998 char buf
[SYMNMLEN
+ 1];
5000 /* This reloc is against a symbol we are
5001 stripping. It would be possible to handle
5002 this case, but I don't think it's worth it. */
5003 is
= flinfo
->internal_syms
+ r_symndx
;
5005 if (is
->n_sclass
!= C_DWARF
)
5007 name
= (_bfd_coff_internal_syment_name
5008 (input_bfd
, is
, buf
));
5013 (*flinfo
->info
->callbacks
->unattached_reloc
)
5014 (flinfo
->info
, name
,
5015 input_bfd
, o
, irel
->r_vaddr
);
5021 if ((o
->flags
& SEC_DEBUGGING
) == 0
5022 && xcoff_need_ldrel_p (flinfo
->info
, irel
, h
, o
))
5029 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
5031 sec
= xcoff_symbol_section (h
);
5032 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
5033 o
->output_section
, input_bfd
,
5039 o
->output_section
->reloc_count
+= o
->reloc_count
;
5042 /* Write out the modified section contents. */
5043 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5044 contents
, (file_ptr
) o
->output_offset
,
5049 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5051 if (! flinfo
->info
->keep_memory
)
5053 if (! _bfd_coff_free_symbols (input_bfd
))
5060 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5067 /* Sort relocs by VMA. This is called via qsort. */
5070 xcoff_sort_relocs (const void * p1
, const void * p2
)
5072 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
5073 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
5075 if (r1
->r_vaddr
> r2
->r_vaddr
)
5077 else if (r1
->r_vaddr
< r2
->r_vaddr
)
5083 /* Return true if section SEC is a TOC section. */
5086 xcoff_toc_section_p (asection
*sec
)
5091 if (name
[0] == '.' && name
[1] == 't')
5095 if (name
[3] == '0' && name
[4] == 0)
5100 if (name
[2] == 'd' && name
[3] == 0)
5106 /* See if the link requires a TOC (it usually does!). If so, find a
5107 good place to put the TOC anchor csect, and write out the associated
5111 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
5113 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5117 struct internal_syment irsym
;
5118 union internal_auxent iraux
;
5122 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5123 index of a csect at the beginning of the TOC. */
5124 toc_start
= ~(bfd_vma
) 0;
5127 for (input_bfd
= flinfo
->info
->input_bfds
;
5129 input_bfd
= input_bfd
->link
.next
)
5130 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5131 if (sec
->gc_mark
!= 0 && xcoff_toc_section_p (sec
))
5133 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5134 if (toc_start
> start
)
5137 section_index
= sec
->output_section
->target_index
;
5140 end
= start
+ sec
->size
;
5145 /* There's no need for a TC0 symbol if we don't have a TOC. */
5146 if (toc_end
< toc_start
)
5148 xcoff_data (output_bfd
)->toc
= toc_start
;
5152 if (toc_end
- toc_start
< 0x8000)
5153 /* Every TOC csect can be accessed from TOC_START. */
5154 best_address
= toc_start
;
5157 /* Find the lowest TOC csect that is still within range of TOC_END. */
5158 best_address
= toc_end
;
5159 for (input_bfd
= flinfo
->info
->input_bfds
;
5161 input_bfd
= input_bfd
->link
.next
)
5162 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5163 if (sec
->gc_mark
!= 0 && xcoff_toc_section_p (sec
))
5165 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5166 if (start
< best_address
5167 && start
+ 0x8000 >= toc_end
)
5169 best_address
= start
;
5170 section_index
= sec
->output_section
->target_index
;
5174 /* Make sure that the start of the TOC is also within range. */
5175 if (best_address
> toc_start
+ 0x8000)
5178 (_("TOC overflow: %#" PRIx64
" > 0x10000; try -mminimal-toc "
5180 (uint64_t) (toc_end
- toc_start
));
5181 bfd_set_error (bfd_error_file_too_big
);
5186 /* Record the chosen TOC value. */
5187 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5188 xcoff_data (output_bfd
)->toc
= best_address
;
5189 xcoff_data (output_bfd
)->sntoc
= section_index
;
5191 /* Fill out the TC0 symbol. */
5192 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5195 irsym
.n_value
= best_address
;
5196 irsym
.n_scnum
= section_index
;
5197 irsym
.n_sclass
= C_HIDEXT
;
5198 irsym
.n_type
= T_NULL
;
5200 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
5202 /* Fill out the auxiliary csect information. */
5203 memset (&iraux
, 0, sizeof iraux
);
5204 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5205 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5206 iraux
.x_csect
.x_scnlen
.l
= 0;
5207 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5208 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5210 /* Write the contents to the file. */
5211 pos
= obj_sym_filepos (output_bfd
);
5212 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5213 size
= 2 * bfd_coff_symesz (output_bfd
);
5214 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5215 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
5217 obj_raw_syment_count (output_bfd
) += 2;
5222 /* Write out a non-XCOFF global symbol. */
5225 xcoff_write_global_symbol (struct bfd_hash_entry
*bh
, void * inf
)
5227 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) bh
;
5228 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
5231 struct internal_syment isym
;
5232 union internal_auxent aux
;
5237 output_bfd
= flinfo
->output_bfd
;
5238 outsym
= flinfo
->outsyms
;
5240 if (h
->root
.type
== bfd_link_hash_warning
)
5242 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5243 if (h
->root
.type
== bfd_link_hash_new
)
5247 /* If this symbol was garbage collected, just skip it. */
5248 if (xcoff_hash_table (flinfo
->info
)->gc
5249 && (h
->flags
& XCOFF_MARK
) == 0)
5252 /* If we need a .loader section entry, write it out. */
5253 if (h
->ldsym
!= NULL
)
5255 struct internal_ldsym
*ldsym
;
5260 if (h
->root
.type
== bfd_link_hash_undefined
5261 || h
->root
.type
== bfd_link_hash_undefweak
)
5265 ldsym
->l_scnum
= N_UNDEF
;
5266 ldsym
->l_smtype
= XTY_ER
;
5267 impbfd
= h
->root
.u
.undef
.abfd
;
5270 else if (h
->root
.type
== bfd_link_hash_defined
5271 || h
->root
.type
== bfd_link_hash_defweak
)
5275 sec
= h
->root
.u
.def
.section
;
5276 ldsym
->l_value
= (sec
->output_section
->vma
5277 + sec
->output_offset
5278 + h
->root
.u
.def
.value
);
5279 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5280 ldsym
->l_smtype
= XTY_SD
;
5281 impbfd
= sec
->owner
;
5287 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5288 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5289 || (h
->flags
& XCOFF_IMPORT
) != 0)
5291 Import symbols are defined so the check above will make
5292 the l_smtype XTY_SD. But this is not correct, it should
5294 ldsym
->l_smtype
|= L_IMPORT
;
5296 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5297 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5298 || (h
->flags
& XCOFF_EXPORT
) != 0)
5299 ldsym
->l_smtype
|= L_EXPORT
;
5301 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5302 ldsym
->l_smtype
|= L_ENTRY
;
5304 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5305 ldsym
->l_smtype
= XTY_SD
;
5307 ldsym
->l_smclas
= h
->smclas
;
5309 if (ldsym
->l_smtype
& L_IMPORT
)
5311 if ((h
->root
.type
== bfd_link_hash_defined
5312 || h
->root
.type
== bfd_link_hash_defweak
)
5313 && (h
->root
.u
.def
.value
!= 0))
5314 ldsym
->l_smclas
= XMC_XO
;
5316 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5317 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5318 ldsym
->l_smclas
= XMC_SV3264
;
5320 else if (h
->flags
& XCOFF_SYSCALL32
)
5321 ldsym
->l_smclas
= XMC_SV
;
5323 else if (h
->flags
& XCOFF_SYSCALL64
)
5324 ldsym
->l_smclas
= XMC_SV64
;
5327 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5331 else if (ldsym
->l_ifile
== 0)
5333 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5335 else if (impbfd
== NULL
)
5339 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5340 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5346 BFD_ASSERT (h
->ldindx
>= 0);
5348 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5351 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
5355 /* If this symbol needs global linkage code, write it out. */
5356 if (h
->root
.type
== bfd_link_hash_defined
5357 && (h
->root
.u
.def
.section
5358 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
5364 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5366 /* The first instruction in the global linkage code loads a
5367 specific TOC element. */
5368 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5369 + h
->descriptor
->toc_section
->output_offset
5370 - xcoff_data (output_bfd
)->toc
);
5372 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5373 tocoff
+= h
->descriptor
->u
.toc_offset
;
5375 /* The first instruction in the glink code needs to be
5376 cooked to hold the correct offset in the toc. The
5377 rest are just output raw. */
5378 bfd_put_32 (output_bfd
,
5379 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5381 /* Start with i == 1 to get past the first instruction done above
5382 The /4 is because the glink code is in bytes and we are going
5384 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5385 bfd_put_32 (output_bfd
,
5386 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5390 /* If we created a TOC entry for this symbol, write out the required
5392 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5397 struct internal_reloc
*irel
;
5398 struct internal_syment irsym
;
5399 union internal_auxent iraux
;
5401 tocsec
= h
->toc_section
;
5402 osec
= tocsec
->output_section
;
5403 oindx
= osec
->target_index
;
5404 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5405 irel
->r_vaddr
= (osec
->vma
5406 + tocsec
->output_offset
5410 irel
->r_symndx
= h
->indx
;
5414 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5417 BFD_ASSERT (h
->ldindx
>= 0);
5419 /* Initialize the aux union here instead of closer to when it is
5420 written out below because the length of the csect depends on
5421 whether the output is 32 or 64 bit. */
5422 memset (&iraux
, 0, sizeof iraux
);
5423 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5424 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5425 iraux
.x_csect
.x_smclas
= XMC_TC
;
5427 /* 32 bit uses a 32 bit R_POS to do the relocations
5428 64 bit uses a 64 bit R_POS to do the relocations
5430 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5432 Which one is determined by the backend. */
5433 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5436 iraux
.x_csect
.x_scnlen
.l
= 8;
5438 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5441 iraux
.x_csect
.x_scnlen
.l
= 4;
5446 irel
->r_type
= R_POS
;
5447 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5448 ++osec
->reloc_count
;
5450 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5451 output_bfd
, irel
, NULL
, h
))
5454 /* We need to emit a symbol to define a csect which holds
5456 if (flinfo
->info
->strip
!= strip_all
)
5458 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
,
5460 &irsym
, h
->root
.root
.string
);
5464 irsym
.n_value
= irel
->r_vaddr
;
5465 irsym
.n_scnum
= osec
->target_index
;
5466 irsym
.n_sclass
= C_HIDEXT
;
5467 irsym
.n_type
= T_NULL
;
5470 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5471 outsym
+= bfd_coff_symesz (output_bfd
);
5473 /* Note : iraux is initialized above. */
5474 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5475 0, 1, (void *) outsym
);
5476 outsym
+= bfd_coff_auxesz (output_bfd
);
5480 /* We aren't going to write out the symbols below, so we
5481 need to write them out now. */
5482 pos
= obj_sym_filepos (output_bfd
);
5483 pos
+= (obj_raw_syment_count (output_bfd
)
5484 * bfd_coff_symesz (output_bfd
));
5485 amt
= outsym
- flinfo
->outsyms
;
5486 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5487 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5489 obj_raw_syment_count (output_bfd
) +=
5490 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5492 outsym
= flinfo
->outsyms
;
5497 /* If this symbol is a specially defined function descriptor, write
5498 it out. The first word is the address of the function code
5499 itself, the second word is the address of the TOC, and the third
5503 The addresses for the 32 bit will take 4 bytes and the addresses
5504 for 64 bit will take 8 bytes. Similar for the relocs. This type
5505 of logic was also done above to create a TOC entry in
5506 xcoff_write_global_symbol. */
5507 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5508 && h
->root
.type
== bfd_link_hash_defined
5509 && (h
->root
.u
.def
.section
5510 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
5516 struct xcoff_link_hash_entry
*hentry
;
5518 struct internal_reloc
*irel
;
5520 unsigned int reloc_size
, byte_size
;
5522 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5527 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5535 sec
= h
->root
.u
.def
.section
;
5536 osec
= sec
->output_section
;
5537 oindx
= osec
->target_index
;
5538 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5540 hentry
= h
->descriptor
;
5541 BFD_ASSERT (hentry
!= NULL
5542 && (hentry
->root
.type
== bfd_link_hash_defined
5543 || hentry
->root
.type
== bfd_link_hash_defweak
));
5544 esec
= hentry
->root
.u
.def
.section
;
5546 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5547 irel
->r_vaddr
= (osec
->vma
5548 + sec
->output_offset
5549 + h
->root
.u
.def
.value
);
5550 irel
->r_symndx
= esec
->output_section
->target_index
;
5551 irel
->r_type
= R_POS
;
5552 irel
->r_size
= reloc_size
;
5553 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5554 ++osec
->reloc_count
;
5556 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5557 output_bfd
, irel
, esec
, NULL
))
5560 /* There are three items to write out,
5561 the address of the code
5562 the address of the toc anchor
5563 the environment pointer.
5564 We are ignoring the environment pointer. So set it to zero. */
5565 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5567 bfd_put_64 (output_bfd
,
5568 (esec
->output_section
->vma
+ esec
->output_offset
5569 + hentry
->root
.u
.def
.value
),
5571 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5572 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5577 This logic was already called above so the error case where
5578 the backend is neither has already been checked. */
5579 bfd_put_32 (output_bfd
,
5580 (esec
->output_section
->vma
+ esec
->output_offset
5581 + hentry
->root
.u
.def
.value
),
5583 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5584 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5587 tsec
= coff_section_from_bfd_index (output_bfd
,
5588 xcoff_data (output_bfd
)->sntoc
);
5591 irel
->r_vaddr
= (osec
->vma
5592 + sec
->output_offset
5593 + h
->root
.u
.def
.value
5595 irel
->r_symndx
= tsec
->output_section
->target_index
;
5596 irel
->r_type
= R_POS
;
5597 irel
->r_size
= reloc_size
;
5598 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5599 ++osec
->reloc_count
;
5601 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5602 output_bfd
, irel
, tsec
, NULL
))
5606 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
5608 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5613 && (flinfo
->info
->strip
== strip_all
5614 || (flinfo
->info
->strip
== strip_some
5615 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
5616 false, false) == NULL
)))
5618 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5623 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5625 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5629 memset (&aux
, 0, sizeof aux
);
5631 h
->indx
= obj_raw_syment_count (output_bfd
);
5633 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5634 &isym
, h
->root
.root
.string
);
5638 if (h
->root
.type
== bfd_link_hash_undefined
5639 || h
->root
.type
== bfd_link_hash_undefweak
)
5642 isym
.n_scnum
= N_UNDEF
;
5643 if (h
->root
.type
== bfd_link_hash_undefweak
5644 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5645 isym
.n_sclass
= C_WEAKEXT
;
5647 isym
.n_sclass
= C_EXT
;
5648 aux
.x_csect
.x_smtyp
= XTY_ER
;
5650 else if ((h
->root
.type
== bfd_link_hash_defined
5651 || h
->root
.type
== bfd_link_hash_defweak
)
5652 && h
->smclas
== XMC_XO
)
5654 BFD_ASSERT (bfd_is_abs_symbol (&h
->root
));
5655 isym
.n_value
= h
->root
.u
.def
.value
;
5656 isym
.n_scnum
= N_UNDEF
;
5657 if (h
->root
.type
== bfd_link_hash_defweak
5658 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5659 isym
.n_sclass
= C_WEAKEXT
;
5661 isym
.n_sclass
= C_EXT
;
5662 aux
.x_csect
.x_smtyp
= XTY_ER
;
5664 else if (h
->root
.type
== bfd_link_hash_defined
5665 || h
->root
.type
== bfd_link_hash_defweak
)
5667 struct xcoff_link_size_list
*l
;
5669 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5670 + h
->root
.u
.def
.section
->output_offset
5671 + h
->root
.u
.def
.value
);
5672 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5673 isym
.n_scnum
= N_ABS
;
5675 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5676 isym
.n_sclass
= C_HIDEXT
;
5677 aux
.x_csect
.x_smtyp
= XTY_SD
;
5679 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5681 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
5687 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5693 else if (h
->root
.type
== bfd_link_hash_common
)
5695 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5696 + h
->root
.u
.c
.p
->section
->output_offset
);
5697 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5698 isym
.n_sclass
= C_EXT
;
5699 aux
.x_csect
.x_smtyp
= XTY_CM
;
5700 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5705 isym
.n_type
= T_NULL
;
5708 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5709 outsym
+= bfd_coff_symesz (output_bfd
);
5711 aux
.x_csect
.x_smclas
= h
->smclas
;
5712 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5714 outsym
+= bfd_coff_auxesz (output_bfd
);
5716 if ((h
->root
.type
== bfd_link_hash_defined
5717 || h
->root
.type
== bfd_link_hash_defweak
)
5718 && h
->smclas
!= XMC_XO
)
5720 /* We just output an SD symbol. Now output an LD symbol. */
5723 if (h
->root
.type
== bfd_link_hash_defweak
5724 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5725 isym
.n_sclass
= C_WEAKEXT
;
5727 isym
.n_sclass
= C_EXT
;
5728 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5729 outsym
+= bfd_coff_symesz (output_bfd
);
5731 aux
.x_csect
.x_smtyp
= XTY_LD
;
5732 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5733 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5735 outsym
+= bfd_coff_auxesz (output_bfd
);
5738 pos
= obj_sym_filepos (output_bfd
);
5739 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5740 amt
= outsym
- flinfo
->outsyms
;
5741 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5742 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5744 obj_raw_syment_count (output_bfd
) +=
5745 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5750 /* Handle a link order which is supposed to generate a reloc. */
5753 xcoff_reloc_link_order (bfd
*output_bfd
,
5754 struct xcoff_final_link_info
*flinfo
,
5755 asection
*output_section
,
5756 struct bfd_link_order
*link_order
)
5758 reloc_howto_type
*howto
;
5759 struct xcoff_link_hash_entry
*h
;
5763 struct internal_reloc
*irel
;
5764 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5766 if (link_order
->type
== bfd_section_reloc_link_order
)
5767 /* We need to somehow locate a symbol in the right section. The
5768 symbol must either have a value of zero, or we must adjust
5769 the addend by the value of the symbol. FIXME: Write this
5770 when we need it. The old linker couldn't handle this anyhow. */
5773 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5776 bfd_set_error (bfd_error_bad_value
);
5780 h
= ((struct xcoff_link_hash_entry
*)
5781 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
5782 link_order
->u
.reloc
.p
->u
.name
,
5783 false, false, true));
5786 (*flinfo
->info
->callbacks
->unattached_reloc
)
5787 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
5791 hsec
= xcoff_symbol_section (h
);
5792 if (h
->root
.type
== bfd_link_hash_defined
5793 || h
->root
.type
== bfd_link_hash_defweak
)
5794 hval
= h
->root
.u
.def
.value
;
5798 addend
= link_order
->u
.reloc
.p
->addend
;
5800 addend
+= (hsec
->output_section
->vma
5801 + hsec
->output_offset
5808 bfd_reloc_status_type rstat
;
5811 size
= bfd_get_reloc_size (howto
);
5812 buf
= bfd_zmalloc (size
);
5813 if (buf
== NULL
&& size
!= 0)
5816 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5822 case bfd_reloc_outofrange
:
5824 case bfd_reloc_overflow
:
5825 (*flinfo
->info
->callbacks
->reloc_overflow
)
5826 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5827 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0);
5830 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5831 (file_ptr
) link_order
->offset
, size
);
5837 /* Store the reloc information in the right place. It will get
5838 swapped and written out at the end of the final_link routine. */
5839 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
5840 + output_section
->reloc_count
);
5841 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
5842 + output_section
->reloc_count
);
5844 memset (irel
, 0, sizeof (struct internal_reloc
));
5845 *rel_hash_ptr
= NULL
;
5847 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5850 irel
->r_symndx
= h
->indx
;
5853 /* Set the index to -2 to force this symbol to get written out. */
5859 irel
->r_type
= howto
->type
;
5860 irel
->r_size
= howto
->bitsize
- 1;
5861 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5862 irel
->r_size
|= 0x80;
5864 ++output_section
->reloc_count
;
5866 /* Now output the reloc to the .loader section. */
5867 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
5869 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
5870 output_bfd
, irel
, hsec
, h
))
5877 /* Do the final link step. */
5880 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5882 bfd_size_type symesz
;
5883 struct xcoff_final_link_info flinfo
;
5885 struct bfd_link_order
*p
;
5886 bfd_size_type max_contents_size
;
5887 bfd_size_type max_sym_count
;
5888 bfd_size_type max_lineno_count
;
5889 bfd_size_type max_reloc_count
;
5890 bfd_size_type max_output_reloc_count
;
5891 file_ptr rel_filepos
;
5893 file_ptr line_filepos
;
5894 unsigned int linesz
;
5896 bfd_byte
*external_relocs
= NULL
;
5897 char strbuf
[STRING_SIZE_SIZE
];
5901 if (bfd_link_pic (info
))
5902 abfd
->flags
|= DYNAMIC
;
5904 symesz
= bfd_coff_symesz (abfd
);
5907 flinfo
.output_bfd
= abfd
;
5908 flinfo
.strtab
= NULL
;
5909 flinfo
.section_info
= NULL
;
5910 flinfo
.last_file_index
= -1;
5911 flinfo
.toc_symindx
= -1;
5912 flinfo
.internal_syms
= NULL
;
5913 flinfo
.sym_indices
= NULL
;
5914 flinfo
.outsyms
= NULL
;
5915 flinfo
.linenos
= NULL
;
5916 flinfo
.contents
= NULL
;
5917 flinfo
.external_relocs
= NULL
;
5919 if (xcoff_hash_table (info
)->loader_section
)
5921 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5922 + bfd_xcoff_ldhdrsz (abfd
));
5923 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5924 + bfd_xcoff_ldhdrsz (abfd
)
5925 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5926 * bfd_xcoff_ldsymsz (abfd
)));
5930 flinfo
.ldsym
= NULL
;
5931 flinfo
.ldrel
= NULL
;
5934 xcoff_data (abfd
)->coff
.link_info
= info
;
5936 flinfo
.strtab
= _bfd_stringtab_init ();
5937 if (flinfo
.strtab
== NULL
)
5940 /* Count the relocation entries required for the output file.
5941 (We've already counted the line numbers.) Determine a few
5943 max_contents_size
= 0;
5944 max_lineno_count
= 0;
5945 max_reloc_count
= 0;
5946 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5949 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5951 if (p
->type
== bfd_indirect_link_order
)
5955 sec
= p
->u
.indirect
.section
;
5957 /* Mark all sections which are to be included in the
5958 link. This will normally be every section. We need
5959 to do this so that we can identify any sections which
5960 the linker has decided to not include. */
5961 sec
->linker_mark
= true;
5963 o
->reloc_count
+= sec
->reloc_count
;
5965 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
5967 if (sec
->rawsize
> max_contents_size
)
5968 max_contents_size
= sec
->rawsize
;
5969 if (sec
->size
> max_contents_size
)
5970 max_contents_size
= sec
->size
;
5972 if (coff_section_data (sec
->owner
, sec
) != NULL
5973 && xcoff_section_data (sec
->owner
, sec
) != NULL
5974 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5975 > max_lineno_count
))
5977 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5978 if (sec
->reloc_count
> max_reloc_count
)
5979 max_reloc_count
= sec
->reloc_count
;
5981 else if (p
->type
== bfd_section_reloc_link_order
5982 || p
->type
== bfd_symbol_reloc_link_order
)
5987 /* Compute the file positions for all the sections. */
5988 if (abfd
->output_has_begun
)
5990 if (xcoff_hash_table (info
)->file_align
!= 0)
5997 file_align
= xcoff_hash_table (info
)->file_align
;
5998 if (file_align
!= 0)
6004 /* Insert .pad sections before every section which has
6005 contents and is loaded, if it is preceded by some other
6006 section which has contents and is loaded. */
6007 saw_contents
= true;
6008 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6010 if (strcmp (o
->name
, ".pad") == 0)
6011 saw_contents
= false;
6012 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
6013 && (o
->flags
& SEC_LOAD
) != 0)
6016 saw_contents
= true;
6021 /* Create a pad section and place it before the section
6022 that needs padding. This requires unlinking and
6023 relinking the bfd's section list. */
6025 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
6027 n
->alignment_power
= 0;
6029 bfd_section_list_remove (abfd
, n
);
6030 bfd_section_list_insert_before (abfd
, o
, n
);
6031 saw_contents
= false;
6036 /* Reset the section indices after inserting the new
6039 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6042 o
->target_index
= indx
;
6044 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
6046 /* Work out appropriate sizes for the .pad sections to force
6047 each section to land on a page boundary. This bit of
6048 code knows what compute_section_file_positions is going
6050 sofar
= bfd_coff_filhsz (abfd
);
6051 sofar
+= bfd_coff_aoutsz (abfd
);
6052 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
6053 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6054 if ((bfd_xcoff_is_reloc_count_overflow
6055 (abfd
, (bfd_vma
) o
->reloc_count
))
6056 || (bfd_xcoff_is_lineno_count_overflow
6057 (abfd
, (bfd_vma
) o
->lineno_count
)))
6058 /* 64 does not overflow, need to check if 32 does */
6059 sofar
+= bfd_coff_scnhsz (abfd
);
6061 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6063 if (strcmp (o
->name
, ".pad") == 0)
6067 BFD_ASSERT (o
->size
== 0);
6068 pageoff
= sofar
& (file_align
- 1);
6071 o
->size
= file_align
- pageoff
;
6072 sofar
+= file_align
- pageoff
;
6073 o
->flags
|= SEC_HAS_CONTENTS
;
6078 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
6079 sofar
+= BFD_ALIGN (o
->size
,
6080 1 << o
->alignment_power
);
6085 if (! bfd_coff_compute_section_file_positions (abfd
))
6089 /* Allocate space for the pointers we need to keep for the relocs. */
6093 /* We use section_count + 1, rather than section_count, because
6094 the target_index fields are 1 based. */
6095 amt
= abfd
->section_count
+ 1;
6096 amt
*= sizeof (struct xcoff_link_section_info
);
6097 flinfo
.section_info
= bfd_malloc (amt
);
6098 if (flinfo
.section_info
== NULL
)
6100 for (i
= 0; i
<= abfd
->section_count
; i
++)
6102 flinfo
.section_info
[i
].relocs
= NULL
;
6103 flinfo
.section_info
[i
].rel_hashes
= NULL
;
6104 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6108 /* Set the file positions for the relocs. */
6109 rel_filepos
= obj_relocbase (abfd
);
6110 relsz
= bfd_coff_relsz (abfd
);
6111 max_output_reloc_count
= 0;
6112 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6114 if (o
->reloc_count
== 0)
6118 /* A stripped file has no relocs. However, we still
6119 allocate the buffers, so that later code doesn't have to
6120 worry about whether we are stripping or not. */
6121 if (info
->strip
== strip_all
)
6125 o
->flags
|= SEC_RELOC
;
6126 o
->rel_filepos
= rel_filepos
;
6127 rel_filepos
+= o
->reloc_count
* relsz
;
6130 /* We don't know the indices of global symbols until we have
6131 written out all the local symbols. For each section in
6132 the output file, we keep an array of pointers to hash
6133 table entries. Each entry in the array corresponds to a
6134 reloc. When we find a reloc against a global symbol, we
6135 set the corresponding entry in this array so that we can
6136 fix up the symbol index after we have written out all the
6139 Because of this problem, we also keep the relocs in
6140 memory until the end of the link. This wastes memory.
6141 We could backpatch the file later, I suppose, although it
6143 amt
= o
->reloc_count
;
6144 amt
*= sizeof (struct internal_reloc
);
6145 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6147 amt
= o
->reloc_count
;
6148 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6149 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6151 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
6152 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6155 if (o
->reloc_count
> max_output_reloc_count
)
6156 max_output_reloc_count
= o
->reloc_count
;
6160 /* We now know the size of the relocs, so we can determine the file
6161 positions of the line numbers. */
6162 line_filepos
= rel_filepos
;
6163 flinfo
.line_filepos
= line_filepos
;
6164 linesz
= bfd_coff_linesz (abfd
);
6165 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6167 if (o
->lineno_count
== 0)
6168 o
->line_filepos
= 0;
6171 o
->line_filepos
= line_filepos
;
6172 line_filepos
+= o
->lineno_count
* linesz
;
6175 /* Reset the reloc and lineno counts, so that we can use them to
6176 count the number of entries we have output so far. */
6178 o
->lineno_count
= 0;
6181 obj_sym_filepos (abfd
) = line_filepos
;
6183 /* Figure out the largest number of symbols in an input BFD. Take
6184 the opportunity to clear the output_has_begun fields of all the
6185 input BFD's. We want at least 6 symbols, since that is the
6186 number which xcoff_write_global_symbol may need. */
6188 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6192 sub
->output_has_begun
= false;
6193 sz
= obj_raw_syment_count (sub
);
6194 if (sz
> max_sym_count
)
6198 /* Allocate some buffers used while linking. */
6199 amt
= max_sym_count
* sizeof (struct internal_syment
);
6200 flinfo
.internal_syms
= bfd_malloc (amt
);
6202 amt
= max_sym_count
* sizeof (long);
6203 flinfo
.sym_indices
= bfd_malloc (amt
);
6205 amt
= (max_sym_count
+ 1) * symesz
;
6206 flinfo
.outsyms
= bfd_malloc (amt
);
6208 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6209 flinfo
.linenos
= bfd_malloc (amt
);
6211 amt
= max_contents_size
;
6212 flinfo
.contents
= bfd_malloc (amt
);
6214 amt
= max_reloc_count
* relsz
;
6215 flinfo
.external_relocs
= bfd_malloc (amt
);
6217 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6218 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6219 || flinfo
.outsyms
== NULL
6220 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
6221 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
6222 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6225 obj_raw_syment_count (abfd
) = 0;
6227 /* Find a TOC symbol, if we need one. */
6228 if (!xcoff_find_tc0 (abfd
, &flinfo
))
6231 /* We now know the position of everything in the file, except that
6232 we don't know the size of the symbol table and therefore we don't
6233 know where the string table starts. We just build the string
6234 table in memory as we go along. We process all the relocations
6235 for a single input file at once. */
6236 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6238 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6240 if (p
->type
== bfd_indirect_link_order
6241 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6243 sub
= p
->u
.indirect
.section
->owner
;
6244 if (! sub
->output_has_begun
)
6246 if (! xcoff_link_input_bfd (&flinfo
, sub
))
6248 sub
->output_has_begun
= true;
6251 else if (p
->type
== bfd_section_reloc_link_order
6252 || p
->type
== bfd_symbol_reloc_link_order
)
6254 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
6259 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6265 /* Free up the buffers used by xcoff_link_input_bfd. */
6266 free (flinfo
.internal_syms
);
6267 flinfo
.internal_syms
= NULL
;
6268 free (flinfo
.sym_indices
);
6269 flinfo
.sym_indices
= NULL
;
6270 free (flinfo
.linenos
);
6271 flinfo
.linenos
= NULL
;
6272 free (flinfo
.contents
);
6273 flinfo
.contents
= NULL
;
6274 free (flinfo
.external_relocs
);
6275 flinfo
.external_relocs
= NULL
;
6277 /* The value of the last C_FILE symbol is supposed to be -1. Write
6279 if (flinfo
.last_file_index
!= -1)
6281 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
6282 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
6283 (void *) flinfo
.outsyms
);
6284 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
6285 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6286 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
6290 /* Write out all the global symbols which do not come from XCOFF
6292 bfd_hash_traverse (&info
->hash
->table
, xcoff_write_global_symbol
, &flinfo
);
6294 free (flinfo
.outsyms
);
6295 flinfo
.outsyms
= NULL
;
6297 /* Now that we have written out all the global symbols, we know the
6298 symbol indices to use for relocs against them, and we can finally
6299 write out the relocs. */
6300 amt
= max_output_reloc_count
* relsz
;
6301 external_relocs
= bfd_malloc (amt
);
6302 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6305 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6307 struct internal_reloc
*irel
;
6308 struct internal_reloc
*irelend
;
6309 struct xcoff_link_hash_entry
**rel_hash
;
6310 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6312 bfd_size_type rel_size
;
6314 /* A stripped file has no relocs. */
6315 if (info
->strip
== strip_all
)
6321 if (o
->reloc_count
== 0)
6324 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6325 irelend
= irel
+ o
->reloc_count
;
6326 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
6327 for (; irel
< irelend
; irel
++, rel_hash
++)
6329 if (*rel_hash
!= NULL
)
6331 if ((*rel_hash
)->indx
< 0)
6333 (*info
->callbacks
->unattached_reloc
)
6334 (info
, (*rel_hash
)->root
.root
.string
,
6335 NULL
, o
, irel
->r_vaddr
);
6336 (*rel_hash
)->indx
= 0;
6338 irel
->r_symndx
= (*rel_hash
)->indx
;
6342 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6343 toc_rel_hash
!= NULL
;
6344 toc_rel_hash
= toc_rel_hash
->next
)
6346 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6348 (*info
->callbacks
->unattached_reloc
)
6349 (info
, toc_rel_hash
->h
->root
.root
.string
,
6350 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
);
6351 toc_rel_hash
->h
->u
.toc_indx
= 0;
6353 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6356 /* XCOFF requires that the relocs be sorted by address. We tend
6357 to produce them in the order in which their containing csects
6358 appear in the symbol table, which is not necessarily by
6359 address. So we sort them here. There may be a better way to
6361 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
6362 o
->reloc_count
, sizeof (struct internal_reloc
),
6365 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6366 irelend
= irel
+ o
->reloc_count
;
6367 erel
= external_relocs
;
6368 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6369 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6371 rel_size
= relsz
* o
->reloc_count
;
6372 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6373 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6377 free (external_relocs
);
6378 external_relocs
= NULL
;
6380 /* Free up the section information. */
6381 if (flinfo
.section_info
!= NULL
)
6385 for (i
= 0; i
< abfd
->section_count
; i
++)
6387 free (flinfo
.section_info
[i
].relocs
);
6388 free (flinfo
.section_info
[i
].rel_hashes
);
6390 free (flinfo
.section_info
);
6391 flinfo
.section_info
= NULL
;
6394 /* Write out the loader section contents. */
6395 o
= xcoff_hash_table (info
)->loader_section
;
6398 && o
->output_section
!= bfd_abs_section_ptr
)
6400 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
6401 == (xcoff_hash_table (info
)->loader_section
->contents
6402 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6403 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6404 (file_ptr
) o
->output_offset
, o
->size
))
6408 /* Write out the magic sections. */
6409 o
= xcoff_hash_table (info
)->linkage_section
;
6412 && o
->output_section
!= bfd_abs_section_ptr
6413 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6414 (file_ptr
) o
->output_offset
,
6417 o
= xcoff_hash_table (info
)->toc_section
;
6420 && o
->output_section
!= bfd_abs_section_ptr
6421 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6422 (file_ptr
) o
->output_offset
,
6425 o
= xcoff_hash_table (info
)->descriptor_section
;
6428 && o
->output_section
!= bfd_abs_section_ptr
6429 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6430 (file_ptr
) o
->output_offset
,
6434 /* Write out the string table. */
6435 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6436 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6439 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
6441 amt
= STRING_SIZE_SIZE
;
6442 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6444 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
6447 _bfd_stringtab_free (flinfo
.strtab
);
6449 /* Write out the debugging string table. */
6450 o
= xcoff_hash_table (info
)->debug_section
;
6453 && o
->output_section
!= bfd_abs_section_ptr
)
6455 struct bfd_strtab_hash
*debug_strtab
;
6457 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6458 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6459 >= _bfd_stringtab_size (debug_strtab
));
6460 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6461 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6463 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6467 /* Setting symcount to 0 will cause write_object_contents to
6468 not try to write out the symbols. */
6474 if (flinfo
.strtab
!= NULL
)
6475 _bfd_stringtab_free (flinfo
.strtab
);
6477 if (flinfo
.section_info
!= NULL
)
6481 for (i
= 0; i
< abfd
->section_count
; i
++)
6483 free (flinfo
.section_info
[i
].relocs
);
6484 free (flinfo
.section_info
[i
].rel_hashes
);
6486 free (flinfo
.section_info
);
6489 free (flinfo
.internal_syms
);
6490 free (flinfo
.sym_indices
);
6491 free (flinfo
.outsyms
);
6492 free (flinfo
.linenos
);
6493 free (flinfo
.contents
);
6494 free (flinfo
.external_relocs
);
6495 free (external_relocs
);