1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2022 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 However, if H has a hidden visibility, LDSYM must not
804 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
805 && (h
->root
.type
== bfd_link_hash_undefined
806 || h
->root
.type
== bfd_link_hash_undefweak
)
807 && (h
->visibility
!= SYM_V_HIDDEN
808 && h
->visibility
!= SYM_V_INTERNAL
))
814 /* This function is used to add symbols from a dynamic object to the
815 global symbol table. */
818 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
822 struct internal_ldhdr ldhdr
;
824 bfd_byte
*elsym
, *elsymend
;
825 struct xcoff_import_file
*n
;
827 struct xcoff_import_file
**pp
;
829 /* We can only handle a dynamic object if we are generating an XCOFF
831 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
834 (_("%pB: XCOFF shared object when not producing XCOFF output"),
836 bfd_set_error (bfd_error_invalid_operation
);
840 /* The symbols we use from a dynamic object are not the symbols in
841 the normal symbol table, but, rather, the symbols in the export
842 table. If there is a global symbol in a dynamic object which is
843 not in the export table, the loader will not be able to find it,
844 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
845 libc.a has symbols in the export table which are not in the
848 /* Read in the .loader section. FIXME: We should really use the
849 o_snloader field in the a.out header, rather than grabbing the
851 lsec
= bfd_get_section_by_name (abfd
, ".loader");
855 (_("%pB: dynamic object with no .loader section"),
857 bfd_set_error (bfd_error_no_symbols
);
861 if (! xcoff_get_section_contents (abfd
, lsec
))
863 contents
= coff_section_data (abfd
, lsec
)->contents
;
865 /* Remove the sections from this object, so that they do not get
866 included in the link. */
867 bfd_section_list_clear (abfd
);
869 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
871 strings
= (char *) contents
+ ldhdr
.l_stoff
;
873 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
875 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
877 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
879 struct internal_ldsym ldsym
;
880 char nambuf
[SYMNMLEN
+ 1];
882 struct xcoff_link_hash_entry
*h
;
884 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
886 /* We are only interested in exported symbols. */
887 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
890 if (ldsym
._l
._l_l
._l_zeroes
== 0)
891 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
894 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
895 nambuf
[SYMNMLEN
] = '\0';
899 /* Normally we could not call xcoff_link_hash_lookup in an add
900 symbols routine, since we might not be using an XCOFF hash
901 table. However, we verified above that we are using an XCOFF
904 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true,
909 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
912 h
->flags
|= XCOFF_DEF_DYNAMIC
;
913 h
->smclas
= ldsym
.l_smclas
;
914 if (h
->smclas
== XMC_XO
)
916 /* This symbol has an absolute value. */
917 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
918 h
->root
.type
= bfd_link_hash_defweak
;
920 h
->root
.type
= bfd_link_hash_defined
;
921 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
922 h
->root
.u
.def
.value
= ldsym
.l_value
;
926 /* Otherwise, we don't bother to actually define the symbol,
927 since we don't have a section to put it in anyhow.
928 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
929 should be imported from the symbol's undef.abfd. */
930 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
931 h
->root
.type
= bfd_link_hash_undefweak
;
933 h
->root
.type
= bfd_link_hash_undefined
;
934 h
->root
.u
.undef
.abfd
= abfd
;
937 /* If this symbol defines a function descriptor, then it
938 implicitly defines the function code as well. */
939 if (h
->smclas
== XMC_DS
940 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
941 h
->flags
|= XCOFF_DESCRIPTOR
;
942 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
944 struct xcoff_link_hash_entry
*hds
;
951 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
955 strcpy (dsnm
+ 1, name
);
956 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
966 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
968 hds
->root
.type
= h
->root
.type
;
969 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
970 if (h
->smclas
== XMC_XO
)
972 /* An absolute symbol appears to actually define code, not a
973 function descriptor. This is how some math functions are
974 implemented on AIX 4.1. */
975 hds
->smclas
= XMC_XO
;
976 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
977 hds
->root
.u
.def
.value
= ldsym
.l_value
;
981 hds
->smclas
= XMC_PR
;
982 hds
->root
.u
.undef
.abfd
= abfd
;
983 /* We do not want to add this to the undefined
990 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
992 free (coff_section_data (abfd
, lsec
)->contents
);
993 coff_section_data (abfd
, lsec
)->contents
= NULL
;
996 /* Record this file in the import files. */
997 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
1002 if (abfd
->my_archive
== NULL
|| bfd_is_thin_archive (abfd
->my_archive
))
1004 if (!bfd_xcoff_split_import_path (abfd
, bfd_get_filename (abfd
),
1005 &n
->path
, &n
->file
))
1011 struct xcoff_archive_info
*archive_info
;
1013 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1014 if (!archive_info
->impfile
)
1016 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1017 bfd_get_filename (archive_info
1019 &archive_info
->imppath
,
1020 &archive_info
->impfile
))
1023 n
->path
= archive_info
->imppath
;
1024 n
->file
= archive_info
->impfile
;
1025 n
->member
= bfd_get_filename (abfd
);
1028 /* We start c at 1 because the first import file number is reserved
1030 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1032 pp
= &(*pp
)->next
, ++c
)
1036 xcoff_data (abfd
)->import_file_id
= c
;
1041 /* xcoff_link_create_extra_sections
1043 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1046 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1048 bool return_value
= false;
1050 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1052 /* We need to build a .loader section, so we do it here. This
1053 won't work if we're producing an XCOFF output file with no
1054 XCOFF input files. FIXME. */
1056 if (!bfd_link_relocatable (info
)
1057 && xcoff_hash_table (info
)->loader_section
== NULL
)
1060 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1062 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1066 xcoff_hash_table (info
)->loader_section
= lsec
;
1069 /* Likewise for the linkage section. */
1070 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1073 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1076 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1080 xcoff_hash_table (info
)->linkage_section
= lsec
;
1081 lsec
->alignment_power
= 2;
1084 /* Likewise for the TOC section. */
1085 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1088 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1091 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1095 xcoff_hash_table (info
)->toc_section
= tsec
;
1096 tsec
->alignment_power
= 2;
1099 /* Likewise for the descriptor section. */
1100 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1103 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1106 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1110 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1111 dsec
->alignment_power
= 2;
1114 /* Likewise for the .debug section. */
1115 if (xcoff_hash_table (info
)->debug_section
== NULL
1116 && info
->strip
!= strip_all
)
1119 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1121 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1125 xcoff_hash_table (info
)->debug_section
= dsec
;
1129 return_value
= true;
1133 return return_value
;
1136 /* Returns the index of reloc in RELOCS with the least address greater
1137 than or equal to ADDRESS. The relocs are sorted by address. */
1139 static bfd_size_type
1140 xcoff_find_reloc (struct internal_reloc
*relocs
,
1141 bfd_size_type count
,
1144 bfd_size_type min
, max
, this;
1148 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1157 /* Do a binary search over (min,max]. */
1158 while (min
+ 1 < max
)
1162 this = (max
+ min
) / 2;
1163 raddr
= relocs
[this].r_vaddr
;
1164 if (raddr
> address
)
1166 else if (raddr
< address
)
1175 if (relocs
[min
].r_vaddr
< address
)
1179 && relocs
[min
- 1].r_vaddr
== address
)
1185 /* Return true if the symbol has to be added to the linker hash
1188 xcoff_link_add_symbols_to_hash_table (struct internal_syment sym
,
1189 union internal_auxent aux
)
1191 /* External symbols must be added. */
1192 if (EXTERN_SYM_P (sym
.n_sclass
))
1195 /* Hidden TLS symbols must be added to verify TLS relocations
1196 in xcoff_reloc_type_tls. */
1197 if (sym
.n_sclass
== C_HIDEXT
1198 && ((aux
.x_csect
.x_smclas
== XMC_TL
1199 || aux
.x_csect
.x_smclas
== XMC_UL
)))
1205 /* Add all the symbols from an object file to the hash table.
1207 XCOFF is a weird format. A normal XCOFF .o files will have three
1208 COFF sections--.text, .data, and .bss--but each COFF section will
1209 contain many csects. These csects are described in the symbol
1210 table. From the linker's point of view, each csect must be
1211 considered a section in its own right. For example, a TOC entry is
1212 handled as a small XMC_TC csect. The linker must be able to merge
1213 different TOC entries together, which means that it must be able to
1214 extract the XMC_TC csects from the .data section of the input .o
1217 From the point of view of our linker, this is, of course, a hideous
1218 nightmare. We cope by actually creating sections for each csect,
1219 and discarding the original sections. We then have to handle the
1220 relocation entries carefully, since the only way to tell which
1221 csect they belong to is to examine the address. */
1224 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1226 unsigned int n_tmask
;
1227 unsigned int n_btshft
;
1229 bfd_size_type symcount
;
1230 struct xcoff_link_hash_entry
**sym_hash
;
1231 asection
**csect_cache
;
1232 unsigned int *lineno_counts
;
1233 bfd_size_type linesz
;
1235 asection
*last_real
;
1238 unsigned int csect_index
;
1239 asection
*first_csect
;
1240 bfd_size_type symesz
;
1243 struct reloc_info_struct
1245 struct internal_reloc
*relocs
;
1248 } *reloc_info
= NULL
;
1250 unsigned short visibility
;
1252 keep_syms
= obj_coff_keep_syms (abfd
);
1254 if ((abfd
->flags
& DYNAMIC
) != 0
1255 && ! info
->static_link
)
1257 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1261 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1262 if (! xcoff_link_create_extra_sections (abfd
, info
))
1265 if ((abfd
->flags
& DYNAMIC
) != 0
1266 && ! info
->static_link
)
1269 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1270 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1272 /* Define macros so that ISFCN, et. al., macros work correctly. */
1273 #define N_TMASK n_tmask
1274 #define N_BTSHFT n_btshft
1276 if (info
->keep_memory
)
1277 default_copy
= false;
1279 default_copy
= true;
1281 symcount
= obj_raw_syment_count (abfd
);
1283 /* We keep a list of the linker hash table entries that correspond
1284 to each external symbol. */
1285 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1286 sym_hash
= bfd_zalloc (abfd
, amt
);
1287 if (sym_hash
== NULL
&& symcount
!= 0)
1289 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1291 /* Because of the weird stuff we are doing with XCOFF csects, we can
1292 not easily determine which section a symbol is in, so we store
1293 the information in the tdata for the input file. */
1294 amt
= symcount
* sizeof (asection
*);
1295 csect_cache
= bfd_zalloc (abfd
, amt
);
1296 if (csect_cache
== NULL
&& symcount
!= 0)
1298 xcoff_data (abfd
)->csects
= csect_cache
;
1300 /* We garbage-collect line-number information on a symbol-by-symbol
1301 basis, so we need to have quick access to the number of entries
1303 amt
= symcount
* sizeof (unsigned int);
1304 lineno_counts
= bfd_zalloc (abfd
, amt
);
1305 if (lineno_counts
== NULL
&& symcount
!= 0)
1307 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1309 /* While splitting sections into csects, we need to assign the
1310 relocs correctly. The relocs and the csects must both be in
1311 order by VMA within a given section, so we handle this by
1312 scanning along the relocs as we process the csects. We index
1313 into reloc_info using the section target_index. */
1314 amt
= abfd
->section_count
+ 1;
1315 amt
*= sizeof (struct reloc_info_struct
);
1316 reloc_info
= bfd_zmalloc (amt
);
1317 if (reloc_info
== NULL
)
1320 /* Read in the relocs and line numbers for each section. */
1321 linesz
= bfd_coff_linesz (abfd
);
1323 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1327 if ((o
->flags
& SEC_RELOC
) != 0)
1329 reloc_info
[o
->target_index
].relocs
=
1330 xcoff_read_internal_relocs (abfd
, o
, true, NULL
, false, NULL
);
1331 amt
= o
->reloc_count
;
1332 amt
*= sizeof (asection
*);
1333 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1334 if (reloc_info
[o
->target_index
].csects
== NULL
)
1338 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1339 && o
->lineno_count
> 0)
1343 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0)
1345 if (_bfd_mul_overflow (linesz
, o
->lineno_count
, &amt
))
1347 bfd_set_error (bfd_error_file_too_big
);
1350 linenos
= _bfd_malloc_and_read (abfd
, amt
, amt
);
1351 if (linenos
== NULL
)
1353 reloc_info
[o
->target_index
].linenos
= linenos
;
1357 /* Don't let the linker relocation routines discard the symbols. */
1358 obj_coff_keep_syms (abfd
) = true;
1364 symesz
= bfd_coff_symesz (abfd
);
1365 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1366 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1367 esym_end
= esym
+ symcount
* symesz
;
1369 while (esym
< esym_end
)
1371 struct internal_syment sym
;
1372 union internal_auxent aux
;
1374 char buf
[SYMNMLEN
+ 1];
1378 struct xcoff_link_hash_entry
*set_toc
;
1380 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1382 /* In this pass we are only interested in symbols with csect
1384 if (!CSECT_SYM_P (sym
.n_sclass
))
1387 Normally csect is a .pr, .rw etc. created in the loop
1388 If C_FILE or first time, handle special
1390 Advance esym, sym_hash, csect_hash ptrs. */
1391 if (sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1394 *csect_cache
= csect
;
1395 else if (first_csect
== NULL
1396 || sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1397 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1399 *csect_cache
= NULL
;
1400 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1401 sym_hash
+= sym
.n_numaux
+ 1;
1402 csect_cache
+= sym
.n_numaux
+ 1;
1403 lineno_counts
+= sym
.n_numaux
+ 1;
1408 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1413 /* If this symbol has line number information attached to it,
1414 and we're not stripping it, count the number of entries and
1415 add them to the count for this csect. In the final link pass
1416 we are going to attach line number information by symbol,
1417 rather than by section, in order to more easily handle
1418 garbage collection. */
1419 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1422 && ISFCN (sym
.n_type
))
1424 union internal_auxent auxlin
;
1426 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1427 sym
.n_type
, sym
.n_sclass
,
1428 0, sym
.n_numaux
, (void *) &auxlin
);
1430 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1432 asection
*enclosing
;
1433 bfd_signed_vma linoff
;
1435 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1436 if (enclosing
== NULL
)
1439 /* xgettext:c-format */
1440 (_("%pB: `%s' has line numbers but no enclosing section"),
1442 bfd_set_error (bfd_error_bad_value
);
1445 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1446 - enclosing
->line_filepos
);
1447 /* Explicit cast to bfd_signed_vma for compiler. */
1448 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1450 struct internal_lineno lin
;
1451 bfd_byte
*linpstart
;
1453 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1455 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1457 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1459 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1462 bfd_byte
*linpend
, *linp
;
1464 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1465 + enclosing
->lineno_count
* linesz
);
1466 for (linp
= linpstart
+ linesz
;
1470 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1472 if (lin
.l_lnno
== 0)
1475 *lineno_counts
= (linp
- linpstart
) / linesz
;
1476 /* The setting of line_filepos will only be
1477 useful if all the line number entries for a
1478 csect are contiguous; this only matters for
1480 if (csect
->line_filepos
== 0)
1481 csect
->line_filepos
=
1482 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1488 /* Record visibility. */
1489 visibility
= sym
.n_type
& SYM_V_MASK
;
1491 /* Pick up the csect auxiliary information. */
1492 if (sym
.n_numaux
== 0)
1495 /* xgettext:c-format */
1496 (_("%pB: class %d symbol `%s' has no aux entries"),
1497 abfd
, sym
.n_sclass
, name
);
1498 bfd_set_error (bfd_error_bad_value
);
1502 bfd_coff_swap_aux_in (abfd
,
1503 (void *) (esym
+ symesz
* sym
.n_numaux
),
1504 sym
.n_type
, sym
.n_sclass
,
1505 sym
.n_numaux
- 1, sym
.n_numaux
,
1508 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1518 /* xgettext:c-format */
1519 (_("%pB: symbol `%s' has unrecognized csect type %d"),
1521 bfd_set_error (bfd_error_bad_value
);
1525 /* This is an external reference. */
1526 if (sym
.n_sclass
== C_HIDEXT
1527 || sym
.n_scnum
!= N_UNDEF
1528 || aux
.x_csect
.x_scnlen
.l
!= 0)
1531 /* xgettext:c-format */
1532 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1534 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1535 (int64_t) aux
.x_csect
.x_scnlen
.l
);
1536 bfd_set_error (bfd_error_bad_value
);
1540 /* An XMC_XO external reference is actually a reference to
1541 an absolute location. */
1542 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1543 section
= bfd_und_section_ptr
;
1546 section
= bfd_abs_section_ptr
;
1547 value
= sym
.n_value
;
1553 csect_index
= -(unsigned) 1;
1555 /* When we see a TOC anchor, we record the TOC value. */
1556 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1558 if (sym
.n_sclass
!= C_HIDEXT
1559 || aux
.x_csect
.x_scnlen
.l
!= 0)
1562 /* xgettext:c-format */
1563 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64
),
1564 abfd
, name
, sym
.n_sclass
, (int64_t) aux
.x_csect
.x_scnlen
.l
);
1565 bfd_set_error (bfd_error_bad_value
);
1568 xcoff_data (abfd
)->toc
= sym
.n_value
;
1571 /* We must merge TOC entries for the same symbol. We can
1572 merge two TOC entries if they are both C_HIDEXT, they
1573 both have the same name, they are both 4 or 8 bytes long, and
1574 they both have a relocation table entry for an external
1575 symbol with the same name. Unfortunately, this means
1576 that we must look through the relocations. Ick.
1578 Logic for 32 bit vs 64 bit.
1579 32 bit has a csect length of 4 for TOC
1580 64 bit has a csect length of 8 for TOC
1582 An exception is made for TOC entries with a R_TLSML
1583 relocation. This relocation is made for the loader.
1584 We must check that the referenced symbol is the TOC entry
1587 The conditions to get past the if-check are not that bad.
1588 They are what is used to create the TOC csects in the first
1590 if (aux
.x_csect
.x_smclas
== XMC_TC
1591 && sym
.n_sclass
== C_HIDEXT
1592 && info
->output_bfd
->xvec
== abfd
->xvec
1593 && ((bfd_xcoff_is_xcoff32 (abfd
)
1594 && aux
.x_csect
.x_scnlen
.l
== 4)
1595 || (bfd_xcoff_is_xcoff64 (abfd
)
1596 && aux
.x_csect
.x_scnlen
.l
== 8)))
1598 asection
*enclosing
;
1599 struct internal_reloc
*relocs
;
1600 bfd_size_type relindx
;
1601 struct internal_reloc
*rel
;
1603 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1604 if (enclosing
== NULL
)
1607 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1608 amt
= enclosing
->reloc_count
;
1609 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1610 rel
= relocs
+ relindx
;
1612 /* 32 bit R_POS r_size is 31
1613 64 bit R_POS r_size is 63 */
1614 if (relindx
< enclosing
->reloc_count
1615 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1616 && (rel
->r_type
== R_POS
||
1617 rel
->r_type
== R_TLSML
)
1618 && ((bfd_xcoff_is_xcoff32 (abfd
)
1619 && rel
->r_size
== 31)
1620 || (bfd_xcoff_is_xcoff64 (abfd
)
1621 && rel
->r_size
== 63)))
1625 struct internal_syment relsym
;
1627 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1628 + rel
->r_symndx
* symesz
);
1629 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1630 if (EXTERN_SYM_P (relsym
.n_sclass
))
1632 const char *relname
;
1633 char relbuf
[SYMNMLEN
+ 1];
1635 struct xcoff_link_hash_entry
*h
;
1637 /* At this point we know that the TOC entry is
1638 for an externally visible symbol. */
1639 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1641 if (relname
== NULL
)
1644 /* We only merge TOC entries if the TC name is
1645 the same as the symbol name. This handles
1646 the normal case, but not common cases like
1647 SYM.P4 which gcc generates to store SYM + 4
1648 in the TOC. FIXME. */
1649 if (strcmp (name
, relname
) == 0)
1651 copy
= (! info
->keep_memory
1652 || relsym
._n
._n_n
._n_zeroes
!= 0
1653 || relsym
._n
._n_n
._n_offset
== 0);
1654 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1655 relname
, true, copy
,
1660 /* At this point h->root.type could be
1661 bfd_link_hash_new. That should be OK,
1662 since we know for sure that we will come
1663 across this symbol as we step through the
1666 /* We store h in *sym_hash for the
1667 convenience of the relocate_section
1671 if (h
->toc_section
!= NULL
)
1673 asection
**rel_csects
;
1675 /* We already have a TOC entry for this
1676 symbol, so we can just ignore this
1679 reloc_info
[enclosing
->target_index
].csects
;
1680 rel_csects
[relindx
] = bfd_und_section_ptr
;
1684 /* We are about to create a TOC entry for
1689 else if (rel
->r_type
== R_TLSML
)
1691 csect_index
= ((esym
1692 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1694 if (((unsigned long) rel
->r_symndx
) != csect_index
)
1697 /* xgettext:c-format */
1698 (_("%pB: TOC entry `%s' has a R_TLSML"
1699 "relocation not targeting itself"),
1701 bfd_set_error (bfd_error_bad_value
);
1709 asection
*enclosing
;
1711 /* We need to create a new section. We get the name from
1712 the csect storage mapping class, so that the linker can
1713 accumulate similar csects together. */
1715 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1719 /* The enclosing section is the main section : .data, .text
1720 or .bss that the csect is coming from. */
1721 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1722 if (enclosing
== NULL
)
1725 if (! bfd_is_abs_section (enclosing
)
1726 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1727 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1728 > enclosing
->vma
+ enclosing
->size
)))
1731 /* xgettext:c-format */
1732 (_("%pB: csect `%s' not in enclosing section"),
1734 bfd_set_error (bfd_error_bad_value
);
1737 csect
->vma
= sym
.n_value
;
1738 csect
->filepos
= (enclosing
->filepos
1741 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1742 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1743 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1745 /* Record the enclosing section in the tdata for this new
1747 amt
= sizeof (struct coff_section_tdata
);
1748 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1749 if (csect
->used_by_bfd
== NULL
)
1751 amt
= sizeof (struct xcoff_section_tdata
);
1752 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1753 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1755 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1756 xcoff_section_data (abfd
, csect
)->lineno_count
=
1757 enclosing
->lineno_count
;
1759 if (enclosing
->owner
== abfd
)
1761 struct internal_reloc
*relocs
;
1762 bfd_size_type relindx
;
1763 struct internal_reloc
*rel
;
1764 asection
**rel_csect
;
1766 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1767 amt
= enclosing
->reloc_count
;
1768 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1770 rel
= relocs
+ relindx
;
1771 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1774 csect
->rel_filepos
= (enclosing
->rel_filepos
1775 + relindx
* bfd_coff_relsz (abfd
));
1776 while (relindx
< enclosing
->reloc_count
1777 && *rel_csect
== NULL
1778 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1782 csect
->flags
|= SEC_RELOC
;
1783 ++csect
->reloc_count
;
1790 /* There are a number of other fields and section flags
1791 which we do not bother to set. */
1793 csect_index
= ((esym
1794 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1797 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1799 if (first_csect
== NULL
)
1800 first_csect
= csect
;
1802 /* If this symbol must be added to the linker hash table,
1803 we treat it as starting at the beginning of the newly
1805 if (xcoff_link_add_symbols_to_hash_table (sym
, aux
))
1811 /* If this is a TOC section for a symbol, record it. */
1812 if (set_toc
!= NULL
)
1813 set_toc
->toc_section
= csect
;
1818 /* This is a label definition. The x_scnlen field is the
1819 symbol index of the csect. Usually the XTY_LD symbol will
1820 follow its appropriate XTY_SD symbol. The .set pseudo op can
1821 cause the XTY_LD to not follow the XTY_SD symbol. */
1826 if (aux
.x_csect
.x_scnlen
.l
< 0
1827 || (aux
.x_csect
.x_scnlen
.l
1828 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1832 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1834 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1840 /* xgettext:c-format */
1841 (_("%pB: misplaced XTY_LD `%s'"),
1843 bfd_set_error (bfd_error_bad_value
);
1847 value
= sym
.n_value
- csect
->vma
;
1852 /* This is an unitialized csect. We could base the name on
1853 the storage mapping class, but we don't bother except for
1854 an XMC_TD symbol. If this csect is externally visible,
1855 it is a common symbol. We put XMC_TD symbols in sections
1856 named .tocbss, and rely on the linker script to put that
1859 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1861 /* The linker script puts the .td section in the data
1862 section after the .tc section. */
1863 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1866 else if (aux
.x_csect
.x_smclas
== XMC_UL
)
1868 /* This is a thread-local unitialized csect. */
1869 csect
= bfd_make_section_anyway_with_flags (abfd
, ".tbss",
1870 SEC_ALLOC
| SEC_THREAD_LOCAL
);
1873 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1878 csect
->vma
= sym
.n_value
;
1879 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1880 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1881 /* There are a number of other fields and section flags
1882 which we do not bother to set. */
1884 csect_index
= ((esym
1885 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1888 amt
= sizeof (struct coff_section_tdata
);
1889 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1890 if (csect
->used_by_bfd
== NULL
)
1892 amt
= sizeof (struct xcoff_section_tdata
);
1893 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1894 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1896 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1898 if (first_csect
== NULL
)
1899 first_csect
= csect
;
1901 if (xcoff_link_add_symbols_to_hash_table (sym
, aux
))
1903 csect
->flags
|= SEC_IS_COMMON
;
1906 value
= aux
.x_csect
.x_scnlen
.l
;
1912 /* Check for magic symbol names. */
1913 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1914 && aux
.x_csect
.x_smclas
!= XMC_TC
1915 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1921 if (strcmp (name
, "_text") == 0)
1922 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1923 else if (strcmp (name
, "_etext") == 0)
1924 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1925 else if (strcmp (name
, "_data") == 0)
1926 i
= XCOFF_SPECIAL_SECTION_DATA
;
1927 else if (strcmp (name
, "_edata") == 0)
1928 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1929 else if (strcmp (name
, "_end") == 0)
1930 i
= XCOFF_SPECIAL_SECTION_END
;
1932 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1933 i
= XCOFF_SPECIAL_SECTION_END2
;
1936 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1939 /* Now we have enough information to add the symbol to the
1940 linker hash table. */
1942 if (xcoff_link_add_symbols_to_hash_table (sym
, aux
))
1947 BFD_ASSERT (section
!= NULL
);
1949 /* We must copy the name into memory if we got it from the
1950 syment itself, rather than the string table. */
1951 copy
= default_copy
;
1952 if (sym
._n
._n_n
._n_zeroes
!= 0
1953 || sym
._n
._n_n
._n_offset
== 0)
1956 /* Ignore global linkage code when linking statically. */
1957 if (info
->static_link
1958 && (smtyp
== XTY_SD
|| smtyp
== XTY_LD
)
1959 && aux
.x_csect
.x_smclas
== XMC_GL
)
1961 section
= bfd_und_section_ptr
;
1965 /* The AIX linker appears to only detect multiple symbol
1966 definitions when there is a reference to the symbol. If
1967 a symbol is defined multiple times, and the only
1968 references are from the same object file, the AIX linker
1969 appears to permit it. It does not merge the different
1970 definitions, but handles them independently. On the
1971 other hand, if there is a reference, the linker reports
1974 This matters because the AIX <net/net_globals.h> header
1975 file actually defines an initialized array, so we have to
1976 actually permit that to work.
1978 Just to make matters even more confusing, the AIX linker
1979 appears to permit multiple symbol definitions whenever
1980 the second definition is in an archive rather than an
1981 object file. This may be a consequence of the manner in
1982 which it handles archives: I think it may load the entire
1983 archive in as separate csects, and then let garbage
1984 collection discard symbols.
1986 We also have to handle the case of statically linking a
1987 shared object, which will cause symbol redefinitions,
1988 although this is an easier case to detect. */
1989 else if (info
->output_bfd
->xvec
== abfd
->xvec
)
1991 if (! bfd_is_und_section (section
))
1992 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1993 name
, true, copy
, false);
1995 /* Make a copy of the symbol name to prevent problems with
1997 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1998 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1999 true, true, false));
2001 if (*sym_hash
== NULL
)
2003 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
2004 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2005 && ! bfd_is_und_section (section
)
2006 && ! bfd_is_com_section (section
))
2008 /* This is a second definition of a defined symbol. */
2009 if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
2010 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
2012 /* The existing symbol is from a shared library.
2014 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
2015 (*sym_hash
)->root
.u
.undef
.abfd
=
2016 (*sym_hash
)->root
.u
.def
.section
->owner
;
2018 else if (abfd
->my_archive
!= NULL
)
2020 /* This is a redefinition in an object contained
2021 in an archive. Just ignore it. See the
2023 section
= bfd_und_section_ptr
;
2026 else if (sym
.n_sclass
== C_AIX_WEAKEXT
2027 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2029 /* At least one of the definitions is weak.
2030 Allow the normal rules to take effect. */
2032 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
2033 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
2035 /* This symbol has been referenced. In this
2036 case, we just continue and permit the
2037 multiple definition error. See the comment
2038 above about the behaviour of the AIX linker. */
2040 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
2042 /* The symbols are both csects of the same
2043 class. There is at least a chance that this
2044 is a semi-legitimate redefinition. */
2045 section
= bfd_und_section_ptr
;
2047 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
2050 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
2051 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
2052 && (bfd_is_und_section (section
)
2053 || bfd_is_com_section (section
)))
2055 /* This is a reference to a multiply defined symbol.
2056 Report the error now. See the comment above
2057 about the behaviour of the AIX linker. We could
2058 also do this with warning symbols, but I'm not
2059 sure the XCOFF linker is wholly prepared to
2060 handle them, and that would only be a warning,
2062 (*info
->callbacks
->multiple_definition
) (info
,
2066 /* Try not to give this error too many times. */
2067 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2071 /* If the symbol is hidden or internal, completely undo
2072 any dynamic link state. */
2073 if ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
2074 && (visibility
== SYM_V_HIDDEN
2075 || visibility
== SYM_V_INTERNAL
))
2076 (*sym_hash
)->flags
&= ~XCOFF_DEF_DYNAMIC
;
2079 /* Keep the most constraining visibility. */
2080 unsigned short hvis
= (*sym_hash
)->visibility
;
2081 if (visibility
&& ( !hvis
|| visibility
< hvis
))
2082 (*sym_hash
)->visibility
= visibility
;
2087 /* _bfd_generic_link_add_one_symbol may call the linker to
2088 generate an error message, and the linker may try to read
2089 the symbol table to give a good error. Right now, the
2090 line numbers are in an inconsistent state, since they are
2091 counted both in the real sections and in the new csects.
2092 We need to leave the count in the real sections so that
2093 the linker can report the line number of the error
2094 correctly, so temporarily clobber the link to the csects
2095 so that the linker will not try to read the line numbers
2096 a second time from the csects. */
2097 BFD_ASSERT (last_real
->next
== first_csect
);
2098 last_real
->next
= NULL
;
2099 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2100 ok
= (_bfd_generic_link_add_one_symbol
2101 (info
, abfd
, name
, flags
, section
, value
, NULL
, copy
, true,
2102 (struct bfd_link_hash_entry
**) sym_hash
));
2103 last_real
->next
= first_csect
;
2107 if (smtyp
== XTY_CM
)
2109 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2110 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2111 /* We don't need the common csect we just created. */
2114 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2115 = csect
->alignment_power
;
2118 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2124 || section
== bfd_und_section_ptr
)
2125 flag
= XCOFF_REF_REGULAR
;
2127 flag
= XCOFF_DEF_REGULAR
;
2128 (*sym_hash
)->flags
|= flag
;
2130 if ((*sym_hash
)->smclas
== XMC_UA
2131 || flag
== XCOFF_DEF_REGULAR
)
2132 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2136 if (smtyp
== XTY_ER
)
2137 *csect_cache
= section
;
2140 *csect_cache
= csect
;
2142 xcoff_section_data (abfd
, csect
)->last_symndx
2143 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2146 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2147 sym_hash
+= sym
.n_numaux
+ 1;
2148 csect_cache
+= sym
.n_numaux
+ 1;
2149 lineno_counts
+= sym
.n_numaux
+ 1;
2152 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2154 /* Make sure that we have seen all the relocs. */
2155 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2157 /* Debugging sections have no csects. */
2158 if (bfd_section_flags (o
) & SEC_DEBUGGING
)
2161 /* Reset the section size and the line number count, since the
2162 data is now attached to the csects. Don't reset the size of
2163 the .debug section, since we need to read it below in
2164 bfd_xcoff_size_dynamic_sections. */
2165 if (strcmp (bfd_section_name (o
), ".debug") != 0)
2167 o
->lineno_count
= 0;
2169 if ((o
->flags
& SEC_RELOC
) != 0)
2172 struct internal_reloc
*rel
;
2173 asection
**rel_csect
;
2175 rel
= reloc_info
[o
->target_index
].relocs
;
2176 rel_csect
= reloc_info
[o
->target_index
].csects
;
2178 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2180 if (*rel_csect
== NULL
)
2183 /* xgettext:c-format */
2184 (_("%pB: reloc %s:%" PRId64
" not in csect"),
2185 abfd
, o
->name
, (int64_t) i
);
2186 bfd_set_error (bfd_error_bad_value
);
2190 /* We identify all function symbols that are the target
2191 of a relocation, so that we can create glue code for
2192 functions imported from dynamic objects. */
2193 if (info
->output_bfd
->xvec
== abfd
->xvec
2194 && *rel_csect
!= bfd_und_section_ptr
2195 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2197 struct xcoff_link_hash_entry
*h
;
2199 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2200 /* If the symbol name starts with a period, it is
2201 the code of a function. If the symbol is
2202 currently undefined, then add an undefined symbol
2203 for the function descriptor. This should do no
2204 harm, because any regular object that defines the
2205 function should also define the function
2206 descriptor. It helps, because it means that we
2207 will identify the function descriptor with a
2208 dynamic object if a dynamic object defines it. */
2209 if (h
->root
.root
.string
[0] == '.'
2210 && h
->descriptor
== NULL
)
2212 struct xcoff_link_hash_entry
*hds
;
2213 struct bfd_link_hash_entry
*bh
;
2215 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2216 h
->root
.root
.string
+ 1,
2220 if (hds
->root
.type
== bfd_link_hash_new
)
2223 if (! (_bfd_generic_link_add_one_symbol
2224 (info
, abfd
, hds
->root
.root
.string
,
2225 (flagword
) 0, bfd_und_section_ptr
,
2226 (bfd_vma
) 0, NULL
, false,
2229 hds
= (struct xcoff_link_hash_entry
*) bh
;
2231 hds
->flags
|= XCOFF_DESCRIPTOR
;
2232 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2233 hds
->descriptor
= h
;
2234 h
->descriptor
= hds
;
2236 if (h
->root
.root
.string
[0] == '.')
2237 h
->flags
|= XCOFF_CALLED
;
2241 free (reloc_info
[o
->target_index
].csects
);
2242 reloc_info
[o
->target_index
].csects
= NULL
;
2244 /* Reset SEC_RELOC and the reloc_count, since the reloc
2245 information is now attached to the csects. */
2246 o
->flags
&=~ SEC_RELOC
;
2249 /* If we are not keeping memory, free the reloc information. */
2250 if (! info
->keep_memory
2251 && coff_section_data (abfd
, o
) != NULL
2252 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2254 free (coff_section_data (abfd
, o
)->relocs
);
2255 coff_section_data (abfd
, o
)->relocs
= NULL
;
2259 /* Free up the line numbers. FIXME: We could cache these
2260 somewhere for the final link, to avoid reading them again. */
2261 free (reloc_info
[o
->target_index
].linenos
);
2262 reloc_info
[o
->target_index
].linenos
= NULL
;
2267 obj_coff_keep_syms (abfd
) = keep_syms
;
2272 if (reloc_info
!= NULL
)
2274 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2276 free (reloc_info
[o
->target_index
].csects
);
2277 free (reloc_info
[o
->target_index
].linenos
);
2281 obj_coff_keep_syms (abfd
) = keep_syms
;
2288 /* Add symbols from an XCOFF object file. */
2291 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2293 if (! _bfd_coff_get_external_symbols (abfd
))
2295 if (! xcoff_link_add_symbols (abfd
, info
))
2297 if (! info
->keep_memory
)
2299 if (! _bfd_coff_free_symbols (abfd
))
2305 /* Look through the loader symbols to see if this dynamic object
2306 should be included in the link. The native linker uses the loader
2307 symbols, not the normal symbol table, so we do too. */
2310 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2311 struct bfd_link_info
*info
,
2317 struct internal_ldhdr ldhdr
;
2318 const char *strings
;
2319 bfd_byte
*elsym
, *elsymend
;
2323 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2325 /* There are no symbols, so don't try to include it. */
2328 if (! xcoff_get_section_contents (abfd
, lsec
))
2330 contents
= coff_section_data (abfd
, lsec
)->contents
;
2332 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2334 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2336 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2338 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2339 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2341 struct internal_ldsym ldsym
;
2342 char nambuf
[SYMNMLEN
+ 1];
2344 struct bfd_link_hash_entry
*h
;
2346 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2348 /* We are only interested in exported symbols. */
2349 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2352 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2353 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2356 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2357 nambuf
[SYMNMLEN
] = '\0';
2361 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2363 /* We are only interested in symbols that are currently
2364 undefined. At this point we know that we are using an XCOFF
2367 && h
->type
== bfd_link_hash_undefined
2368 && (((struct xcoff_link_hash_entry
*) h
)->flags
2369 & XCOFF_DEF_DYNAMIC
) == 0)
2371 if (!(*info
->callbacks
2372 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2379 /* We do not need this shared object. */
2380 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2382 free (coff_section_data (abfd
, lsec
)->contents
);
2383 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2389 /* Look through the symbols to see if this object file should be
2390 included in the link. */
2393 xcoff_link_check_ar_symbols (bfd
*abfd
,
2394 struct bfd_link_info
*info
,
2398 bfd_size_type symesz
;
2404 if ((abfd
->flags
& DYNAMIC
) != 0
2405 && ! info
->static_link
2406 && info
->output_bfd
->xvec
== abfd
->xvec
)
2407 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
, subsbfd
);
2409 symesz
= bfd_coff_symesz (abfd
);
2410 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2411 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2412 while (esym
< esym_end
)
2414 struct internal_syment sym
;
2416 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2417 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2419 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2422 char buf
[SYMNMLEN
+ 1];
2423 struct bfd_link_hash_entry
*h
;
2425 /* This symbol is externally visible, and is defined by this
2427 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2431 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2433 /* We are only interested in symbols that are currently
2434 undefined. If a symbol is currently known to be common,
2435 XCOFF linkers do not bring in an object file which
2436 defines it. We also don't bring in symbols to satisfy
2437 undefined references in shared objects. */
2439 && h
->type
== bfd_link_hash_undefined
2440 && (info
->output_bfd
->xvec
!= abfd
->xvec
2441 || (((struct xcoff_link_hash_entry
*) h
)->flags
2442 & XCOFF_DEF_DYNAMIC
) == 0))
2444 if (!(*info
->callbacks
2445 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2453 /* We do not need this object file. */
2457 /* Check a single archive element to see if we need to include it in
2458 the link. *PNEEDED is set according to whether this element is
2459 needed in the link or not. This is called via
2460 _bfd_generic_link_add_archive_symbols. */
2463 xcoff_link_check_archive_element (bfd
*abfd
,
2464 struct bfd_link_info
*info
,
2465 struct bfd_link_hash_entry
*h ATTRIBUTE_UNUSED
,
2466 const char *name ATTRIBUTE_UNUSED
,
2472 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2473 if (!_bfd_coff_get_external_symbols (abfd
))
2477 if (!xcoff_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
2482 /* Potentially, the add_archive_element hook may have set a
2483 substitute BFD for us. */
2487 && !_bfd_coff_free_symbols (oldbfd
))
2489 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2490 if (!_bfd_coff_get_external_symbols (abfd
))
2493 if (!xcoff_link_add_symbols (abfd
, info
))
2495 if (info
->keep_memory
)
2501 if (!_bfd_coff_free_symbols (abfd
))
2508 /* Given an XCOFF BFD, add symbols to the global hash table as
2512 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2514 switch (bfd_get_format (abfd
))
2517 return xcoff_link_add_object_symbols (abfd
, info
);
2520 /* If the archive has a map, do the usual search. We then need
2521 to check the archive for dynamic objects, because they may not
2522 appear in the archive map even though they should, perhaps, be
2523 included. If the archive has no map, we just consider each object
2524 file in turn, since that apparently is what the AIX native linker
2526 if (bfd_has_map (abfd
))
2528 if (! (_bfd_generic_link_add_archive_symbols
2529 (abfd
, info
, xcoff_link_check_archive_element
)))
2536 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2537 while (member
!= NULL
)
2539 if (bfd_check_format (member
, bfd_object
)
2540 && (info
->output_bfd
->xvec
== member
->xvec
)
2541 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2545 if (! xcoff_link_check_archive_element (member
, info
,
2546 NULL
, NULL
, &needed
))
2549 member
->archive_pass
= -1;
2551 member
= bfd_openr_next_archived_file (abfd
, member
);
2558 bfd_set_error (bfd_error_wrong_format
);
2564 _bfd_xcoff_define_common_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2565 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2566 struct bfd_link_hash_entry
*harg
)
2568 struct xcoff_link_hash_entry
*h
;
2570 if (!bfd_generic_define_common_symbol (output_bfd
, info
, harg
))
2573 h
= (struct xcoff_link_hash_entry
*) harg
;
2574 h
->flags
|= XCOFF_DEF_REGULAR
;
2578 /* If symbol H has not been interpreted as a function descriptor,
2579 see whether it should be. Set up its descriptor information if so. */
2582 xcoff_find_function (struct bfd_link_info
*info
,
2583 struct xcoff_link_hash_entry
*h
)
2585 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2586 && h
->root
.root
.string
[0] != '.')
2589 struct xcoff_link_hash_entry
*hfn
;
2592 amt
= strlen (h
->root
.root
.string
) + 2;
2593 fnname
= bfd_malloc (amt
);
2597 strcpy (fnname
+ 1, h
->root
.root
.string
);
2598 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2599 fnname
, false, false, true);
2602 && hfn
->smclas
== XMC_PR
2603 && (hfn
->root
.type
== bfd_link_hash_defined
2604 || hfn
->root
.type
== bfd_link_hash_defweak
))
2606 h
->flags
|= XCOFF_DESCRIPTOR
;
2607 h
->descriptor
= hfn
;
2608 hfn
->descriptor
= h
;
2614 /* Return true if the given bfd contains at least one shared object. */
2617 xcoff_archive_contains_shared_object_p (struct bfd_link_info
*info
,
2620 struct xcoff_archive_info
*archive_info
;
2623 archive_info
= xcoff_get_archive_info (info
, archive
);
2624 if (!archive_info
->know_contains_shared_object_p
)
2626 member
= bfd_openr_next_archived_file (archive
, NULL
);
2627 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2628 member
= bfd_openr_next_archived_file (archive
, member
);
2630 archive_info
->contains_shared_object_p
= (member
!= NULL
);
2631 archive_info
->know_contains_shared_object_p
= 1;
2633 return archive_info
->contains_shared_object_p
;
2636 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2637 qualifies for export by -bexpall. */
2640 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2642 /* Exclude symbols beginning with '_'. */
2643 if (h
->root
.root
.string
[0] == '_')
2646 /* Exclude archive members that would otherwise be unreferenced. */
2647 if ((h
->flags
& XCOFF_MARK
) == 0
2648 && (h
->root
.type
== bfd_link_hash_defined
2649 || h
->root
.type
== bfd_link_hash_defweak
)
2650 && h
->root
.u
.def
.section
->owner
!= NULL
2651 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2657 /* Return true if symbol H qualifies for the forms of automatic export
2658 specified by AUTO_EXPORT_FLAGS. */
2661 xcoff_auto_export_p (struct bfd_link_info
*info
,
2662 struct xcoff_link_hash_entry
*h
,
2663 unsigned int auto_export_flags
)
2665 /* Don't automatically export things that were explicitly exported. */
2666 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2669 /* Don't export things that we don't define. */
2670 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2673 /* Don't export functions; export their descriptors instead. */
2674 if (h
->root
.root
.string
[0] == '.')
2677 /* Don't export hidden or internal symbols. */
2678 if (h
->visibility
== SYM_V_HIDDEN
2679 || h
->visibility
== SYM_V_INTERNAL
)
2682 /* We don't export a symbol which is being defined by an object
2683 included from an archive which contains a shared object. The
2684 rationale is that if an archive contains both an unshared and
2685 a shared object, then there must be some reason that the
2686 unshared object is unshared, and we don't want to start
2687 providing a shared version of it. In particular, this solves
2688 a bug involving the _savefNN set of functions. gcc will call
2689 those functions without providing a slot to restore the TOC,
2690 so it is essential that these functions be linked in directly
2691 and not from a shared object, which means that a shared
2692 object which also happens to link them in must not export
2693 them. This is confusing, but I haven't been able to think of
2694 a different approach. Note that the symbols can, of course,
2695 be exported explicitly. */
2696 if (h
->root
.type
== bfd_link_hash_defined
2697 || h
->root
.type
== bfd_link_hash_defweak
)
2701 owner
= h
->root
.u
.def
.section
->owner
;
2703 && owner
->my_archive
!= NULL
2704 && xcoff_archive_contains_shared_object_p (info
, owner
->my_archive
))
2708 /* Otherwise, all symbols are exported by -bexpfull. */
2709 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2712 /* Despite its name, -bexpall exports most but not all symbols. */
2713 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2714 && xcoff_covered_by_expall_p (h
))
2720 /* Return true if relocation REL needs to be copied to the .loader section.
2721 If REL is against a global symbol, H is that symbol, otherwise it
2725 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2726 struct xcoff_link_hash_entry
*h
, asection
*ssec
)
2728 if (!xcoff_hash_table (info
)->loader_section
)
2731 switch (rel
->r_type
)
2738 /* We should never need a .loader reloc for a TOC-relative reloc. */
2742 /* In this case, relocations against defined symbols can be resolved
2745 || h
->root
.type
== bfd_link_hash_defined
2746 || h
->root
.type
== bfd_link_hash_defweak
2747 || h
->root
.type
== bfd_link_hash_common
)
2750 /* We will always provide a local definition of function symbols,
2751 even if we don't have one yet. */
2752 if ((h
->flags
& XCOFF_CALLED
) != 0)
2761 /* Absolute relocations against absolute symbols can be
2762 resolved statically. */
2764 && (h
->root
.type
== bfd_link_hash_defined
2765 || h
->root
.type
== bfd_link_hash_defweak
)
2766 && !h
->root
.rel_from_abs
)
2768 asection
*sec
= h
->root
.u
.def
.section
;
2769 if (bfd_is_abs_section (sec
)
2771 && bfd_is_abs_section (sec
->output_section
)))
2775 /* Absolute relocations from read-only sections are forbidden
2776 by AIX loader. However, they can appear in their section's
2779 && (ssec
->output_section
->flags
& SEC_READONLY
) != 0)
2794 /* Mark a symbol as not being garbage, including the section in which
2798 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2800 if ((h
->flags
& XCOFF_MARK
) != 0)
2803 h
->flags
|= XCOFF_MARK
;
2805 /* If we're marking an undefined symbol, try find some way of
2807 if (!bfd_link_relocatable (info
)
2808 && (h
->flags
& XCOFF_IMPORT
) == 0
2809 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2810 && (h
->root
.type
== bfd_link_hash_undefined
2811 || h
->root
.type
== bfd_link_hash_undefweak
))
2813 /* First check whether this symbol can be interpreted as an
2814 undefined function descriptor for a defined function symbol. */
2815 if (!xcoff_find_function (info
, h
))
2818 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2819 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2820 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2822 /* This is a descriptor for a defined symbol, but the input
2823 objects have not defined the descriptor itself. Fill in
2824 the definition automatically.
2826 Note that we do this even if we found a dynamic definition
2827 of H. The local function definition logically overrides
2831 sec
= xcoff_hash_table (info
)->descriptor_section
;
2832 h
->root
.type
= bfd_link_hash_defined
;
2833 h
->root
.u
.def
.section
= sec
;
2834 h
->root
.u
.def
.value
= sec
->size
;
2836 h
->flags
|= XCOFF_DEF_REGULAR
;
2838 /* The size of the function descriptor depends on whether this
2839 is xcoff32 (12) or xcoff64 (24). */
2840 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2842 /* A function descriptor uses two relocs: one for the
2843 associated code, and one for the TOC address. */
2844 xcoff_hash_table (info
)->ldrel_count
+= 2;
2845 sec
->reloc_count
+= 2;
2847 /* Mark the function itself. */
2848 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2851 /* Mark the TOC section, so that we get an anchor
2852 to relocate against. */
2853 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2856 /* We handle writing out the contents of the descriptor in
2857 xcoff_write_global_symbol. */
2859 else if (info
->static_link
)
2860 /* We can't get a symbol value dynamically, so just assume
2861 that it's undefined. */
2862 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2863 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2865 /* This is a function symbol for which we need to create
2868 struct xcoff_link_hash_entry
*hds
;
2870 /* Mark the descriptor (and its TOC section). */
2871 hds
= h
->descriptor
;
2872 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2873 || hds
->root
.type
== bfd_link_hash_undefweak
)
2874 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2875 if (!xcoff_mark_symbol (info
, hds
))
2878 /* Treat this symbol as undefined if the descriptor was. */
2879 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2880 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2882 /* Allocate room for the global linkage code itself. */
2883 sec
= xcoff_hash_table (info
)->linkage_section
;
2884 h
->root
.type
= bfd_link_hash_defined
;
2885 h
->root
.u
.def
.section
= sec
;
2886 h
->root
.u
.def
.value
= sec
->size
;
2888 h
->flags
|= XCOFF_DEF_REGULAR
;
2889 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2891 /* The global linkage code requires a TOC entry for the
2893 if (hds
->toc_section
== NULL
)
2898 xcoff32 uses 4 bytes in the toc.
2899 xcoff64 uses 8 bytes in the toc. */
2900 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2902 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2907 /* Allocate room in the fallback TOC section. */
2908 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2909 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2910 hds
->toc_section
->size
+= byte_size
;
2911 if (!xcoff_mark (info
, hds
->toc_section
))
2914 /* Allocate room for a static and dynamic R_TOC
2916 ++xcoff_hash_table (info
)->ldrel_count
;
2917 ++hds
->toc_section
->reloc_count
;
2919 /* Set the index to -2 to force this symbol to
2922 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2925 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2927 /* Record that the symbol was undefined, then import it.
2928 -brtl links use a special fake import file. */
2929 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2930 if (xcoff_hash_table (info
)->rtld
)
2932 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2937 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2943 if (h
->root
.type
== bfd_link_hash_defined
2944 || h
->root
.type
== bfd_link_hash_defweak
)
2948 hsec
= h
->root
.u
.def
.section
;
2949 if (! bfd_is_abs_section (hsec
)
2950 && hsec
->gc_mark
== 0)
2952 if (! xcoff_mark (info
, hsec
))
2957 if (h
->toc_section
!= NULL
2958 && h
->toc_section
->gc_mark
== 0)
2960 if (! xcoff_mark (info
, h
->toc_section
))
2967 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2968 If the symbol exists, set FLAGS. */
2971 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2972 const char *name
, unsigned int flags
)
2974 struct xcoff_link_hash_entry
*h
;
2976 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2977 false, false, true);
2981 if (h
->root
.type
== bfd_link_hash_defined
2982 || h
->root
.type
== bfd_link_hash_defweak
)
2984 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2991 /* The mark phase of garbage collection. For a given section, mark
2992 it, and all the sections which define symbols to which it refers.
2993 Because this function needs to look at the relocs, we also count
2994 the number of relocs which need to be copied into the .loader
2998 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
3000 if (bfd_is_const_section (sec
)
3001 || sec
->gc_mark
!= 0)
3006 if (sec
->owner
->xvec
!= info
->output_bfd
->xvec
)
3009 if (coff_section_data (sec
->owner
, sec
) == NULL
)
3013 if (xcoff_section_data (sec
->owner
, sec
) != NULL
)
3015 struct xcoff_link_hash_entry
**syms
;
3017 unsigned long i
, first
, last
;
3019 /* Mark all the symbols in this section. */
3020 syms
= obj_xcoff_sym_hashes (sec
->owner
);
3021 csects
= xcoff_data (sec
->owner
)->csects
;
3022 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
3023 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
3024 for (i
= first
; i
<= last
; i
++)
3025 if (csects
[i
] == sec
3027 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
3029 if (!xcoff_mark_symbol (info
, syms
[i
]))
3034 /* Look through the section relocs. */
3035 if ((sec
->flags
& SEC_RELOC
) != 0
3036 && sec
->reloc_count
> 0)
3038 struct internal_reloc
*rel
, *relend
;
3040 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
3044 relend
= rel
+ sec
->reloc_count
;
3045 for (; rel
< relend
; rel
++)
3047 struct xcoff_link_hash_entry
*h
;
3049 if ((unsigned int) rel
->r_symndx
3050 > obj_raw_syment_count (sec
->owner
))
3053 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
3056 if ((h
->flags
& XCOFF_MARK
) == 0)
3058 if (!xcoff_mark_symbol (info
, h
))
3066 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
3068 && rsec
->gc_mark
== 0)
3070 if (!xcoff_mark (info
, rsec
))
3075 /* See if this reloc needs to be copied into the .loader
3077 if ((sec
->flags
& SEC_DEBUGGING
) == 0
3078 && xcoff_need_ldrel_p (info
, rel
, h
, sec
))
3080 ++xcoff_hash_table (info
)->ldrel_count
;
3082 h
->flags
|= XCOFF_LDREL
;
3086 if (! info
->keep_memory
3087 && coff_section_data (sec
->owner
, sec
) != NULL
3088 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
3090 free (coff_section_data (sec
->owner
, sec
)->relocs
);
3091 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
3098 /* Routines that are called after all the input files have been
3099 handled, but before the sections are laid out in memory. */
3101 /* The sweep phase of garbage collection. Remove all garbage
3105 xcoff_sweep (struct bfd_link_info
*info
)
3109 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3112 bool some_kept
= false;
3114 /* As says below keep all sections from non-XCOFF
3116 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3120 /* See whether any section is already marked. */
3121 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3126 /* If no section in this file will be kept, then we can
3127 toss out debug sections. */
3130 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3138 /* Keep all sections from non-XCOFF input files. Keep
3139 special sections. Keep .debug sections for the
3141 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3143 if (o
->gc_mark
== 1)
3146 if (sub
->xvec
!= info
->output_bfd
->xvec
3147 || o
== xcoff_hash_table (info
)->debug_section
3148 || o
== xcoff_hash_table (info
)->loader_section
3149 || o
== xcoff_hash_table (info
)->linkage_section
3150 || o
== xcoff_hash_table (info
)->descriptor_section
3151 || (bfd_section_flags (o
) & SEC_DEBUGGING
)
3152 || strcmp (o
->name
, ".debug") == 0)
3153 xcoff_mark (info
, o
);
3163 /* Record the number of elements in a set. This is used to output the
3164 correct csect length. */
3167 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3168 struct bfd_link_info
*info
,
3169 struct bfd_link_hash_entry
*harg
,
3172 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3173 struct xcoff_link_size_list
*n
;
3176 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3179 /* This will hardly ever be called. I don't want to burn four bytes
3180 per global symbol, so instead the size is kept on a linked list
3181 attached to the hash table. */
3183 n
= bfd_alloc (output_bfd
, amt
);
3186 n
->next
= xcoff_hash_table (info
)->size_list
;
3189 xcoff_hash_table (info
)->size_list
= n
;
3191 h
->flags
|= XCOFF_HAS_SIZE
;
3196 /* Import a symbol. */
3199 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3200 struct bfd_link_info
*info
,
3201 struct bfd_link_hash_entry
*harg
,
3203 const char *imppath
,
3204 const char *impfile
,
3205 const char *impmember
,
3206 unsigned int syscall_flag
)
3208 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3210 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3213 /* A symbol name which starts with a period is the code for a
3214 function. If the symbol is undefined, then add an undefined
3215 symbol for the function descriptor, and import that instead. */
3216 if (h
->root
.root
.string
[0] == '.'
3217 && h
->root
.type
== bfd_link_hash_undefined
3218 && val
== (bfd_vma
) -1)
3220 struct xcoff_link_hash_entry
*hds
;
3222 hds
= h
->descriptor
;
3225 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3226 h
->root
.root
.string
+ 1,
3230 if (hds
->root
.type
== bfd_link_hash_new
)
3232 hds
->root
.type
= bfd_link_hash_undefined
;
3233 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3235 hds
->flags
|= XCOFF_DESCRIPTOR
;
3236 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3237 hds
->descriptor
= h
;
3238 h
->descriptor
= hds
;
3241 /* Now, if the descriptor is undefined, import the descriptor
3242 rather than the symbol we were told to import. FIXME: Is
3243 this correct in all cases? */
3244 if (hds
->root
.type
== bfd_link_hash_undefined
)
3248 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3250 if (val
!= (bfd_vma
) -1)
3252 if (h
->root
.type
== bfd_link_hash_defined
)
3253 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
, output_bfd
,
3254 bfd_abs_section_ptr
, val
);
3256 h
->root
.type
= bfd_link_hash_defined
;
3257 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3258 h
->root
.u
.def
.value
= val
;
3262 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3268 /* Export a symbol. */
3271 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3272 struct bfd_link_info
*info
,
3273 struct bfd_link_hash_entry
*harg
)
3275 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3277 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3280 /* As AIX linker, symbols exported with hidden visibility are
3281 silently ignored. */
3282 if (h
->visibility
== SYM_V_HIDDEN
)
3285 if (h
->visibility
== SYM_V_INTERNAL
)
3287 _bfd_error_handler (_("%pB: cannot export internal symbol `%s`."),
3288 output_bfd
, h
->root
.root
.string
);
3289 bfd_set_error (bfd_error_bad_value
);
3293 h
->flags
|= XCOFF_EXPORT
;
3295 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3296 I'm just going to ignore it until somebody explains it. */
3298 /* Make sure we don't garbage collect this symbol. */
3299 if (! xcoff_mark_symbol (info
, h
))
3302 /* If this is a function descriptor, make sure we don't garbage
3303 collect the associated function code. We normally don't have to
3304 worry about this, because the descriptor will be attached to a
3305 section with relocs, but if we are creating the descriptor
3306 ourselves those relocs will not be visible to the mark code. */
3307 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3309 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3316 /* Count a reloc against a symbol. This is called for relocs
3317 generated by the linker script, typically for global constructors
3321 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3322 struct bfd_link_info
*info
,
3325 struct xcoff_link_hash_entry
*h
;
3327 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3330 h
= ((struct xcoff_link_hash_entry
*)
3331 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, false, false,
3335 _bfd_error_handler (_("%s: no such symbol"), name
);
3336 bfd_set_error (bfd_error_no_symbols
);
3340 h
->flags
|= XCOFF_REF_REGULAR
;
3341 if (xcoff_hash_table (info
)->loader_section
)
3343 h
->flags
|= XCOFF_LDREL
;
3344 ++xcoff_hash_table (info
)->ldrel_count
;
3347 /* Mark the symbol to avoid garbage collection. */
3348 if (! xcoff_mark_symbol (info
, h
))
3354 /* This function is called for each symbol to which the linker script
3356 FIXME: In cases like the linker test ld-scripts/defined5 where a
3357 symbol is defined both by an input object file and the script,
3358 the script definition doesn't override the object file definition
3359 as is usual for other targets. At least not when the symbol is
3360 output. Other uses of the symbol value by the linker do use the
3364 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3365 struct bfd_link_info
*info
,
3368 struct xcoff_link_hash_entry
*h
;
3370 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3373 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
3378 h
->flags
|= XCOFF_DEF_REGULAR
;
3383 /* An xcoff_link_hash_traverse callback for which DATA points to an
3384 xcoff_loader_info. Mark all symbols that should be automatically
3388 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3390 struct xcoff_loader_info
*ldinfo
;
3392 ldinfo
= (struct xcoff_loader_info
*) data
;
3393 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3395 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3396 ldinfo
->failed
= true;
3401 /* INPUT_BFD has an external symbol associated with hash table entry H
3402 and csect CSECT. Return true if INPUT_BFD defines H. */
3405 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3408 switch (h
->root
.type
)
3410 case bfd_link_hash_defined
:
3411 case bfd_link_hash_defweak
:
3412 /* No input bfd owns absolute symbols. They are written by
3413 xcoff_write_global_symbol instead. */
3414 return (!bfd_is_abs_section (csect
)
3415 && h
->root
.u
.def
.section
== csect
);
3417 case bfd_link_hash_common
:
3418 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3420 case bfd_link_hash_undefined
:
3421 case bfd_link_hash_undefweak
:
3422 /* We can't treat undef.abfd as the owner because that bfd
3423 might be a dynamic object. Allow any bfd to claim it. */
3431 /* See if H should have a loader symbol associated with it. */
3434 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3435 struct xcoff_link_hash_entry
*h
)
3439 /* Warn if this symbol is exported but not defined. */
3440 if ((h
->flags
& XCOFF_EXPORT
) != 0
3441 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3444 (_("warning: attempt to export undefined symbol `%s'"),
3445 h
->root
.root
.string
);
3449 /* We need to add a symbol to the .loader section if it is mentioned
3450 in a reloc which we are copying to the .loader section and it was
3451 not defined or common, or if it is the entry point, or if it is
3453 if (((h
->flags
& XCOFF_LDREL
) == 0
3454 || h
->root
.type
== bfd_link_hash_defined
3455 || h
->root
.type
== bfd_link_hash_defweak
3456 || h
->root
.type
== bfd_link_hash_common
)
3457 && (h
->flags
& XCOFF_ENTRY
) == 0
3458 && (h
->flags
& XCOFF_EXPORT
) == 0)
3461 /* We need to add this symbol to the .loader symbols. */
3463 BFD_ASSERT (h
->ldsym
== NULL
);
3464 amt
= sizeof (struct internal_ldsym
);
3465 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3466 if (h
->ldsym
== NULL
)
3468 ldinfo
->failed
= true;
3472 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3474 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3475 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3477 h
->ldsym
->l_ifile
= h
->ldindx
;
3480 /* The first 3 symbol table indices are reserved to indicate the
3481 data, text and bss sections. */
3482 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3484 ++ldinfo
->ldsym_count
;
3486 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3487 h
->ldsym
, h
->root
.root
.string
))
3490 h
->flags
|= XCOFF_BUILT_LDSYM
;
3494 /* An xcoff_htab_traverse callback that is called for each symbol
3495 once garbage collection is complete. */
3498 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3500 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3502 /* __rtinit, this symbol has special handling. */
3503 if (h
->flags
& XCOFF_RTINIT
)
3506 /* We don't want to garbage collect symbols which are not defined in
3507 XCOFF files. This is a convenient place to mark them. */
3508 if (xcoff_hash_table (ldinfo
->info
)->gc
3509 && (h
->flags
& XCOFF_MARK
) == 0
3510 && (h
->root
.type
== bfd_link_hash_defined
3511 || h
->root
.type
== bfd_link_hash_defweak
)
3512 && (h
->root
.u
.def
.section
->owner
== NULL
3513 || (h
->root
.u
.def
.section
->owner
->xvec
3514 != ldinfo
->info
->output_bfd
->xvec
)))
3515 h
->flags
|= XCOFF_MARK
;
3517 /* Skip discarded symbols. */
3518 if (xcoff_hash_table (ldinfo
->info
)->gc
3519 && (h
->flags
& XCOFF_MARK
) == 0)
3522 /* If this is still a common symbol, and it wasn't garbage
3523 collected, we need to actually allocate space for it in the .bss
3525 if (h
->root
.type
== bfd_link_hash_common
3526 && h
->root
.u
.c
.p
->section
->size
== 0)
3528 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3529 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3532 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3534 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3535 h
->flags
|= XCOFF_EXPORT
;
3537 if (!xcoff_build_ldsym (ldinfo
, h
))
3544 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3545 hash table entry H and csect CSECT. AUX contains ISYM's auxiliary
3546 csect information, if any. NAME is the function's name if the name
3547 is stored in the .debug section, otherwise it is null.
3549 Return 1 if we should include an appropriately-adjusted ISYM
3550 in the output file, 0 if we should discard ISYM, or -1 if an
3554 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3555 struct internal_syment
*isym
,
3556 union internal_auxent
*aux
,
3557 struct xcoff_link_hash_entry
*h
,
3558 asection
*csect
, const char *name
)
3562 /* If we are skipping this csect, we want to strip the symbol too. */
3566 /* Likewise if we garbage-collected the csect. */
3567 if (xcoff_hash_table (info
)->gc
3568 && !bfd_is_abs_section (csect
)
3569 && !bfd_is_und_section (csect
)
3570 && csect
->gc_mark
== 0)
3573 /* An XCOFF linker always removes C_STAT symbols. */
3574 if (isym
->n_sclass
== C_STAT
)
3577 /* We generate the TOC anchor separately. */
3578 if (isym
->n_sclass
== C_HIDEXT
3579 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3582 /* If we are stripping all symbols, we want to discard this one. */
3583 if (info
->strip
== strip_all
)
3586 /* Discard symbols that are defined elsewhere. */
3587 if (EXTERN_SYM_P (isym
->n_sclass
))
3589 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3591 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3595 /* If we're discarding local symbols, check whether ISYM is local. */
3596 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3597 if (info
->discard
== discard_all
3598 && !EXTERN_SYM_P (isym
->n_sclass
)
3599 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3602 /* If we're stripping debugging symbols, check whether ISYM is one. */
3603 if (info
->strip
== strip_debugger
3604 && isym
->n_scnum
== N_DEBUG
)
3607 /* If we are stripping symbols based on name, check how ISYM's
3608 name should be handled. */
3609 if (info
->strip
== strip_some
3610 || info
->discard
== discard_l
)
3612 char buf
[SYMNMLEN
+ 1];
3616 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3621 if (info
->strip
== strip_some
3622 && bfd_hash_lookup (info
->keep_hash
, name
, false, false) == NULL
)
3625 if (info
->discard
== discard_l
3626 && !EXTERN_SYM_P (isym
->n_sclass
)
3627 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3628 && bfd_is_local_label_name (input_bfd
, name
))
3635 /* Lay out the .loader section, filling in the header and the import paths.
3636 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3639 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
,
3640 const char *libpath
)
3643 struct xcoff_link_hash_table
*htab
;
3644 struct internal_ldhdr
*ldhdr
;
3645 struct xcoff_import_file
*fl
;
3646 bfd_size_type stoff
;
3647 size_t impsize
, impcount
;
3651 /* Work out the size of the import file names. Each import file ID
3652 consists of three null terminated strings: the path, the file
3653 name, and the archive member name. The first entry in the list
3654 of names is the path to use to find objects, which the linker has
3655 passed in as the libpath argument. For some reason, the path
3656 entry in the other import file names appears to always be empty. */
3657 output_bfd
= ldinfo
->output_bfd
;
3658 htab
= xcoff_hash_table (ldinfo
->info
);
3659 impsize
= strlen (libpath
) + 3;
3661 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3664 impsize
+= (strlen (fl
->path
)
3666 + strlen (fl
->member
)
3670 /* Set up the .loader section header. */
3671 ldhdr
= &htab
->ldhdr
;
3672 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3673 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3674 ldhdr
->l_nreloc
= htab
->ldrel_count
;
3675 ldhdr
->l_istlen
= impsize
;
3676 ldhdr
->l_nimpid
= impcount
;
3677 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3678 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3679 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3680 ldhdr
->l_stlen
= ldinfo
->string_size
;
3681 stoff
= ldhdr
->l_impoff
+ impsize
;
3682 if (ldinfo
->string_size
== 0)
3685 ldhdr
->l_stoff
= stoff
;
3687 /* 64 bit elements to ldhdr
3688 The swap out routine for 32 bit will ignore them.
3689 Nothing fancy, symbols come after the header and relocs come
3691 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3692 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3693 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3695 /* We now know the final size of the .loader section. Allocate
3697 lsec
= htab
->loader_section
;
3698 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3699 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3700 if (lsec
->contents
== NULL
)
3703 /* Set up the header. */
3704 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3706 /* Set up the import file names. */
3707 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3708 strcpy (out
, libpath
);
3709 out
+= strlen (libpath
) + 1;
3712 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3717 while ((*out
++ = *s
++) != '\0')
3720 while ((*out
++ = *s
++) != '\0')
3723 while ((*out
++ = *s
++) != '\0')
3727 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3729 /* Set up the symbol string table. */
3730 if (ldinfo
->string_size
> 0)
3732 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
3733 free (ldinfo
->strings
);
3734 ldinfo
->strings
= NULL
;
3737 /* We can't set up the symbol table or the relocs yet, because we
3738 don't yet know the final position of the various sections. The
3739 .loader symbols are written out when the corresponding normal
3740 symbols are written out in xcoff_link_input_bfd or
3741 xcoff_write_global_symbol. The .loader relocs are written out
3742 when the corresponding normal relocs are handled in
3743 xcoff_link_input_bfd. */
3748 /* Build the .loader section. This is called by the XCOFF linker
3749 emulation before_allocation routine. We must set the size of the
3750 .loader section before the linker lays out the output file.
3751 LIBPATH is the library path to search for shared objects; this is
3752 normally built from the -L arguments passed to the linker. ENTRY
3753 is the name of the entry point symbol (the -e linker option).
3754 FILE_ALIGN is the alignment to use for sections within the file
3755 (the -H linker option). MAXSTACK is the maximum stack size (the
3756 -bmaxstack linker option). MAXDATA is the maximum data size (the
3757 -bmaxdata linker option). GC is whether to do garbage collection
3758 (the -bgc linker option). MODTYPE is the module type (the
3759 -bmodtype linker option). TEXTRO is whether the text section must
3760 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3761 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3762 is set by this routine to csects with magic names like _end. */
3765 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3766 struct bfd_link_info
*info
,
3767 const char *libpath
,
3769 unsigned long file_align
,
3770 unsigned long maxstack
,
3771 unsigned long maxdata
,
3775 unsigned int auto_export_flags
,
3776 asection
**special_sections
,
3779 struct xcoff_loader_info ldinfo
;
3783 struct bfd_strtab_hash
*debug_strtab
;
3784 bfd_byte
*debug_contents
= NULL
;
3787 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3789 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3790 special_sections
[i
] = NULL
;
3794 ldinfo
.failed
= false;
3795 ldinfo
.output_bfd
= output_bfd
;
3797 ldinfo
.auto_export_flags
= auto_export_flags
;
3798 ldinfo
.ldsym_count
= 0;
3799 ldinfo
.string_size
= 0;
3800 ldinfo
.strings
= NULL
;
3801 ldinfo
.string_alc
= 0;
3803 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3804 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3805 xcoff_data (output_bfd
)->modtype
= modtype
;
3807 xcoff_hash_table (info
)->file_align
= file_align
;
3808 xcoff_hash_table (info
)->textro
= textro
;
3809 xcoff_hash_table (info
)->rtld
= rtld
;
3812 if (xcoff_hash_table (info
)->loader_section
3813 && (info
->init_function
|| info
->fini_function
|| rtld
))
3815 struct xcoff_link_hash_entry
*hsym
;
3816 struct internal_ldsym
*ldsym
;
3818 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3819 "__rtinit", false, false, true);
3823 (_("error: undefined symbol __rtinit"));
3827 xcoff_mark_symbol (info
, hsym
);
3828 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3830 /* __rtinit initialized. */
3831 amt
= sizeof (* ldsym
);
3832 ldsym
= bfd_malloc (amt
);
3834 ldsym
->l_value
= 0; /* Will be filled in later. */
3835 ldsym
->l_scnum
= 2; /* Data section. */
3836 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3837 ldsym
->l_smclas
= 5; /* .rw. */
3838 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3839 ldsym
->l_parm
= 0; /* NA. */
3841 /* Force __rtinit to be the first symbol in the loader symbol table
3842 See xcoff_build_ldsyms
3844 The first 3 symbol table indices are reserved to indicate the data,
3845 text and bss sections. */
3846 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3849 ldinfo
.ldsym_count
= 1;
3850 hsym
->ldsym
= ldsym
;
3852 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3853 hsym
->ldsym
, hsym
->root
.root
.string
))
3856 /* This symbol is written out by xcoff_write_global_symbol
3857 Set stuff up so xcoff_write_global_symbol logic works. */
3858 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3859 hsym
->root
.type
= bfd_link_hash_defined
;
3860 hsym
->root
.u
.def
.value
= 0;
3863 /* Garbage collect unused sections. */
3864 if (bfd_link_relocatable (info
) || !gc
)
3867 xcoff_hash_table (info
)->gc
= false;
3869 /* We still need to call xcoff_mark, in order to set ldrel_count
3871 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3875 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3877 /* We shouldn't unconditionaly mark the TOC section.
3878 The output file should only have a TOC if either
3879 (a) one of the input files did or (b) we end up
3880 creating TOC references as part of the link process. */
3881 if (o
!= xcoff_hash_table (info
)->toc_section
3884 if (! xcoff_mark (info
, o
))
3893 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3895 if (info
->init_function
!= NULL
3896 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3898 if (info
->fini_function
!= NULL
3899 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3901 if (auto_export_flags
!= 0)
3903 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3904 xcoff_mark_auto_exports
, &ldinfo
);
3909 xcoff_hash_table (info
)->gc
= true;
3912 /* Return special sections to the caller. */
3913 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3915 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3919 && sec
->gc_mark
== 0)
3922 special_sections
[i
] = sec
;
3925 if (info
->input_bfds
== NULL
)
3926 /* I'm not sure what to do in this bizarre case. */
3929 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3934 if (xcoff_hash_table (info
)->loader_section
3935 && !xcoff_build_loader_section (&ldinfo
, libpath
))
3938 /* Allocate space for the magic sections. */
3939 sec
= xcoff_hash_table (info
)->linkage_section
;
3942 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3943 if (sec
->contents
== NULL
)
3946 sec
= xcoff_hash_table (info
)->toc_section
;
3949 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3950 if (sec
->contents
== NULL
)
3953 sec
= xcoff_hash_table (info
)->descriptor_section
;
3956 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3957 if (sec
->contents
== NULL
)
3961 /* Now that we've done garbage collection, decide which symbols to keep,
3962 and figure out the contents of the .debug section. */
3963 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3965 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3968 bfd_size_type symcount
;
3971 unsigned int *lineno_counts
;
3972 struct xcoff_link_hash_entry
**sym_hash
;
3973 bfd_byte
*esym
, *esymend
;
3974 bfd_size_type symesz
;
3976 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3979 if ((sub
->flags
& DYNAMIC
) != 0
3980 && !info
->static_link
)
3983 if (! _bfd_coff_get_external_symbols (sub
))
3986 symcount
= obj_raw_syment_count (sub
);
3987 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
3988 if (debug_index
== NULL
)
3990 xcoff_data (sub
)->debug_indices
= debug_index
;
3992 if (info
->strip
== strip_all
3993 || info
->strip
== strip_debugger
3994 || info
->discard
== discard_all
)
3995 /* We're stripping all debugging information, so there's no need
3996 to read SUB's .debug section. */
4000 /* Grab the contents of SUB's .debug section, if any. */
4001 subdeb
= bfd_get_section_by_name (sub
, ".debug");
4002 if (subdeb
!= NULL
&& subdeb
->size
> 0)
4004 /* We use malloc and copy the names into the debug
4005 stringtab, rather than bfd_alloc, because I expect
4006 that, when linking many files together, many of the
4007 strings will be the same. Storing the strings in the
4008 hash table should save space in this case. */
4009 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
4014 csectpp
= xcoff_data (sub
)->csects
;
4015 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
4016 sym_hash
= obj_xcoff_sym_hashes (sub
);
4017 symesz
= bfd_coff_symesz (sub
);
4018 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
4019 esymend
= esym
+ symcount
* symesz
;
4021 while (esym
< esymend
)
4023 struct internal_syment sym
;
4024 union internal_auxent aux
;
4029 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
4031 /* Read in the csect information, if any. */
4032 if (CSECT_SYM_P (sym
.n_sclass
))
4034 BFD_ASSERT (sym
.n_numaux
> 0);
4035 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
4036 sym
.n_type
, sym
.n_sclass
,
4037 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
4040 /* If this symbol's name is stored in the debug section,
4041 get a pointer to it. */
4042 if (debug_contents
!= NULL
4043 && sym
._n
._n_n
._n_zeroes
== 0
4044 && bfd_coff_symname_in_debug (sub
, &sym
))
4045 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
4049 /* Decide whether to copy this symbol to the output file. */
4051 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
4052 *sym_hash
, csect
, name
);
4057 /* Use a debug_index of -2 to record that a symbol should
4062 /* See whether we should store the symbol name in the
4063 output .debug section. */
4068 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
4069 if (indx
== (bfd_size_type
) -1)
4071 *debug_index
= indx
;
4076 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
4077 if (*lineno_counts
> 0)
4078 csect
->output_section
->lineno_count
+= *lineno_counts
;
4081 esym
+= (sym
.n_numaux
+ 1) * symesz
;
4082 csectpp
+= sym
.n_numaux
+ 1;
4083 sym_hash
+= sym
.n_numaux
+ 1;
4084 lineno_counts
+= sym
.n_numaux
+ 1;
4085 debug_index
+= sym
.n_numaux
+ 1;
4090 free (debug_contents
);
4091 debug_contents
= NULL
;
4093 /* Clear the size of subdeb, so that it is not included directly
4094 in the output file. */
4098 if (! info
->keep_memory
)
4100 if (! _bfd_coff_free_symbols (sub
))
4105 if (info
->strip
!= strip_all
4106 && xcoff_hash_table (info
)->debug_section
!= NULL
)
4107 xcoff_hash_table (info
)->debug_section
->size
=
4108 _bfd_stringtab_size (debug_strtab
);
4113 free (ldinfo
.strings
);
4114 free (debug_contents
);
4119 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
4124 struct bfd_in_memory
*bim
;
4126 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
4133 abfd
->link
.next
= 0;
4134 abfd
->format
= bfd_object
;
4135 abfd
->iostream
= (void *) bim
;
4136 abfd
->flags
= BFD_IN_MEMORY
;
4137 abfd
->iovec
= &_bfd_memory_iovec
;
4138 abfd
->direction
= write_direction
;
4142 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
4145 /* need to reset to unknown or it will not be read back in correctly */
4146 abfd
->format
= bfd_unknown
;
4147 abfd
->direction
= read_direction
;
4153 /* Return the section that defines H. Return null if no section does. */
4156 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
4158 switch (h
->root
.type
)
4160 case bfd_link_hash_defined
:
4161 case bfd_link_hash_defweak
:
4162 return h
->root
.u
.def
.section
;
4164 case bfd_link_hash_common
:
4165 return h
->root
.u
.c
.p
->section
;
4172 /* Add a .loader relocation for input relocation IREL. If the loader
4173 relocation should be against an output section, HSEC points to the
4174 input section that IREL is against, otherwise HSEC is null. H is the
4175 symbol that IREL is against, or null if it isn't against a global symbol.
4176 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4179 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
4180 asection
*output_section
, bfd
*reference_bfd
,
4181 struct internal_reloc
*irel
, asection
*hsec
,
4182 struct xcoff_link_hash_entry
*h
)
4184 struct internal_ldrel ldrel
;
4186 ldrel
.l_vaddr
= irel
->r_vaddr
;
4189 const char *secname
;
4191 secname
= hsec
->output_section
->name
;
4192 if (strcmp (secname
, ".text") == 0)
4194 else if (strcmp (secname
, ".data") == 0)
4196 else if (strcmp (secname
, ".bss") == 0)
4198 else if (strcmp (secname
, ".tdata") == 0)
4199 ldrel
.l_symndx
= -1;
4200 else if (strcmp (secname
, ".tbss") == 0)
4201 ldrel
.l_symndx
= -2;
4205 /* xgettext:c-format */
4206 (_("%pB: loader reloc in unrecognized section `%s'"),
4207 reference_bfd
, secname
);
4208 bfd_set_error (bfd_error_nonrepresentable_section
);
4217 /* xgettext:c-format */
4218 (_("%pB: `%s' in loader reloc but not loader sym"),
4219 reference_bfd
, h
->root
.root
.string
);
4220 bfd_set_error (bfd_error_bad_value
);
4223 ldrel
.l_symndx
= h
->ldindx
;
4226 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4228 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4229 ldrel
.l_rsecnm
= output_section
->target_index
;
4230 if (xcoff_hash_table (flinfo
->info
)->textro
4231 && strcmp (output_section
->name
, ".text") == 0)
4234 /* xgettext:c-format */
4235 (_("%pB: loader reloc in read-only section %pA"),
4236 reference_bfd
, output_section
);
4237 bfd_set_error (bfd_error_invalid_operation
);
4240 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
4241 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4245 /* Link an input file into the linker output file. This function
4246 handles all the sections and relocations of the input file at once. */
4249 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
4253 const char *strings
;
4254 bfd_size_type syment_base
;
4255 unsigned int n_tmask
;
4256 unsigned int n_btshft
;
4258 bfd_size_type isymesz
;
4259 bfd_size_type osymesz
;
4260 bfd_size_type linesz
;
4263 struct xcoff_link_hash_entry
**sym_hash
;
4264 struct internal_syment
*isymp
;
4266 unsigned int *lineno_counts
;
4269 unsigned long output_index
;
4276 /* We can just skip DYNAMIC files, unless this is a static link. */
4277 if ((input_bfd
->flags
& DYNAMIC
) != 0
4278 && ! flinfo
->info
->static_link
)
4281 /* Move all the symbols to the output file. */
4282 output_bfd
= flinfo
->output_bfd
;
4284 syment_base
= obj_raw_syment_count (output_bfd
);
4285 isymesz
= bfd_coff_symesz (input_bfd
);
4286 osymesz
= bfd_coff_symesz (output_bfd
);
4287 linesz
= bfd_coff_linesz (input_bfd
);
4288 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4290 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4291 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4293 /* Define macros so that ISFCN, et. al., macros work correctly. */
4294 #define N_TMASK n_tmask
4295 #define N_BTSHFT n_btshft
4298 if (! flinfo
->info
->keep_memory
)
4301 if (flinfo
->info
->traditional_format
)
4304 if (! _bfd_coff_get_external_symbols (input_bfd
))
4307 /* Make one pass over the symbols and assign indices to symbols that
4308 we have decided to keep. Also use create .loader symbol information
4309 and update information in hash table entries. */
4310 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4311 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4312 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4313 csectpp
= xcoff_data (input_bfd
)->csects
;
4314 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4315 isymp
= flinfo
->internal_syms
;
4316 indexp
= flinfo
->sym_indices
;
4317 output_index
= syment_base
;
4318 while (esym
< esym_end
)
4320 union internal_auxent aux
;
4324 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4326 /* Read in the csect information, if any. */
4327 if (CSECT_SYM_P (isymp
->n_sclass
))
4329 BFD_ASSERT (isymp
->n_numaux
> 0);
4330 bfd_coff_swap_aux_in (input_bfd
,
4331 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4332 isymp
->n_type
, isymp
->n_sclass
,
4333 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4336 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4339 /* If this symbol is in the .loader section, swap out the
4340 .loader symbol information. If this is an external symbol
4341 reference to a defined symbol, though, then wait until we get
4342 to the definition. */
4343 if (EXTERN_SYM_P (isymp
->n_sclass
)
4344 && *sym_hash
!= NULL
4345 && (*sym_hash
)->ldsym
!= NULL
4346 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4348 struct xcoff_link_hash_entry
*h
;
4349 struct internal_ldsym
*ldsym
;
4353 if (isymp
->n_scnum
> 0)
4355 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4356 ldsym
->l_value
= (isymp
->n_value
4357 + (*csectpp
)->output_section
->vma
4358 + (*csectpp
)->output_offset
4363 ldsym
->l_scnum
= isymp
->n_scnum
;
4364 ldsym
->l_value
= isymp
->n_value
;
4367 ldsym
->l_smtype
= smtyp
;
4368 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4369 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4370 || (h
->flags
& XCOFF_IMPORT
) != 0)
4371 ldsym
->l_smtype
|= L_IMPORT
;
4372 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4373 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4374 || (h
->flags
& XCOFF_EXPORT
) != 0)
4375 ldsym
->l_smtype
|= L_EXPORT
;
4376 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4377 ldsym
->l_smtype
|= L_ENTRY
;
4378 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4379 ldsym
->l_smtype
|= L_WEAK
;
4381 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4383 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4385 else if (ldsym
->l_ifile
== 0)
4387 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4393 if (h
->root
.type
== bfd_link_hash_defined
4394 || h
->root
.type
== bfd_link_hash_defweak
)
4395 impbfd
= h
->root
.u
.def
.section
->owner
;
4396 else if (h
->root
.type
== bfd_link_hash_undefined
4397 || h
->root
.type
== bfd_link_hash_undefweak
)
4398 impbfd
= h
->root
.u
.undef
.abfd
;
4406 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
4407 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4414 BFD_ASSERT (h
->ldindx
>= 0);
4415 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
4418 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
4421 /* Fill in snentry now that we know the target_index. */
4422 if ((h
->flags
& XCOFF_ENTRY
) != 0
4423 && (h
->root
.type
== bfd_link_hash_defined
4424 || h
->root
.type
== bfd_link_hash_defweak
))
4426 xcoff_data (output_bfd
)->snentry
=
4427 h
->root
.u
.def
.section
->output_section
->target_index
;
4431 add
= 1 + isymp
->n_numaux
;
4433 if (*debug_index
== -2)
4434 /* We've decided to strip this symbol. */
4438 /* Assign the next unused index to this symbol. */
4439 *indexp
= output_index
;
4441 if (EXTERN_SYM_P (isymp
->n_sclass
))
4443 BFD_ASSERT (*sym_hash
!= NULL
);
4444 (*sym_hash
)->indx
= output_index
;
4447 /* If this is a symbol in the TOC which we may have merged
4448 (class XMC_TC), remember the symbol index of the TOC
4450 if (isymp
->n_sclass
== C_HIDEXT
4451 && aux
.x_csect
.x_smclas
== XMC_TC
4452 && *sym_hash
!= NULL
)
4454 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4455 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4456 (*sym_hash
)->u
.toc_indx
= output_index
;
4459 output_index
+= add
;
4462 esym
+= add
* isymesz
;
4468 for (--add
; add
> 0; --add
)
4472 /* Now write out the symbols that we decided to keep. */
4474 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4475 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4476 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4477 isymp
= flinfo
->internal_syms
;
4478 indexp
= flinfo
->sym_indices
;
4479 csectpp
= xcoff_data (input_bfd
)->csects
;
4480 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4481 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4482 outsym
= flinfo
->outsyms
;
4485 while (esym
< esym_end
)
4489 add
= 1 + isymp
->n_numaux
;
4492 esym
+= add
* isymesz
;
4495 struct internal_syment isym
;
4498 /* Adjust the symbol in order to output it. */
4500 if (isym
._n
._n_n
._n_zeroes
== 0
4501 && isym
._n
._n_n
._n_offset
!= 0)
4503 /* This symbol has a long name. Enter it in the string
4504 table we are building. If *debug_index != -1, the
4505 name has already been entered in the .debug section. */
4506 if (*debug_index
>= 0)
4507 isym
._n
._n_n
._n_offset
= *debug_index
;
4513 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4517 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
4518 if (indx
== (bfd_size_type
) -1)
4520 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4524 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4525 multiple definition problems when linking a shared object
4526 statically. (The native linker doesn't enter __rtinit into
4527 the normal table at all, but having a local symbol can make
4528 the objdump output easier to read.) */
4529 if (isym
.n_sclass
== C_EXT
4531 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4532 isym
.n_sclass
= C_HIDEXT
;
4534 /* The value of a C_FILE symbol is the symbol index of the
4535 next C_FILE symbol. The value of the last C_FILE symbol
4536 is -1. We try to get this right, below, just before we
4537 write the symbols out, but in the general case we may
4538 have to write the symbol out twice. */
4539 if (isym
.n_sclass
== C_FILE
)
4541 if (flinfo
->last_file_index
!= -1
4542 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4544 /* We must correct the value of the last C_FILE entry. */
4545 flinfo
->last_file
.n_value
= *indexp
;
4546 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4548 /* The last C_FILE symbol is in this input file. */
4549 bfd_coff_swap_sym_out (output_bfd
,
4550 (void *) &flinfo
->last_file
,
4551 (void *) (flinfo
->outsyms
4552 + ((flinfo
->last_file_index
4558 /* We have already written out the last C_FILE
4559 symbol. We need to write it out again. We
4560 borrow *outsym temporarily. */
4563 bfd_coff_swap_sym_out (output_bfd
,
4564 (void *) &flinfo
->last_file
,
4567 pos
= obj_sym_filepos (output_bfd
);
4568 pos
+= flinfo
->last_file_index
* osymesz
;
4569 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4570 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4576 flinfo
->last_file_index
= *indexp
;
4577 flinfo
->last_file
= isym
;
4580 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4581 into the line numbers. We update the symbol values when
4582 we handle the line numbers. */
4583 if (isym
.n_sclass
== C_BINCL
4584 || isym
.n_sclass
== C_EINCL
)
4586 isym
.n_value
= flinfo
->line_filepos
;
4589 /* The value of a C_BSTAT symbol is the symbol table
4590 index of the containing csect. */
4591 else if (isym
.n_sclass
== C_BSTAT
)
4595 indx
= isym
.n_value
;
4596 if (indx
< obj_raw_syment_count (input_bfd
))
4600 symindx
= flinfo
->sym_indices
[indx
];
4604 isym
.n_value
= symindx
;
4607 else if (isym
.n_sclass
!= C_ESTAT
4608 && isym
.n_sclass
!= C_DECL
4609 && isym
.n_scnum
> 0)
4611 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4612 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4613 + (*csectpp
)->output_offset
4617 /* Update visibility. */
4618 isym
.n_type
&= ~SYM_V_MASK
;
4619 isym
.n_type
|= (*sym_hash
)->visibility
;
4621 /* Output the symbol. */
4622 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4627 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4629 union internal_auxent aux
;
4631 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4632 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4635 if (isymp
->n_sclass
== C_FILE
)
4637 /* This is the file name (or some comment put in by
4638 the compiler). If it is long, we must put it in
4639 the string table. */
4640 if (aux
.x_file
.x_n
.x_n
.x_zeroes
== 0
4641 && aux
.x_file
.x_n
.x_n
.x_offset
!= 0)
4643 const char *filename
;
4646 BFD_ASSERT (aux
.x_file
.x_n
.x_n
.x_offset
4647 >= STRING_SIZE_SIZE
);
4648 if (strings
== NULL
)
4650 strings
= _bfd_coff_read_string_table (input_bfd
);
4651 if (strings
== NULL
)
4654 if ((bfd_size_type
) aux
.x_file
.x_n
.x_n
.x_offset
>= obj_coff_strings_len (input_bfd
))
4655 filename
= _("<corrupt>");
4657 filename
= strings
+ aux
.x_file
.x_n
.x_n
.x_offset
;
4658 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
4660 if (indx
== (bfd_size_type
) -1)
4662 aux
.x_file
.x_n
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4665 else if (CSECT_SYM_P (isymp
->n_sclass
)
4666 && i
+ 1 == isymp
->n_numaux
)
4669 /* We don't support type checking. I don't know if
4671 aux
.x_csect
.x_parmhash
= 0;
4672 /* I don't think anybody uses these fields, but we'd
4673 better clobber them just in case. */
4674 aux
.x_csect
.x_stab
= 0;
4675 aux
.x_csect
.x_snstab
= 0;
4677 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4681 indx
= aux
.x_csect
.x_scnlen
.l
;
4682 if (indx
< obj_raw_syment_count (input_bfd
))
4686 symindx
= flinfo
->sym_indices
[indx
];
4689 aux
.x_csect
.x_scnlen
.l
= 0;
4693 aux
.x_csect
.x_scnlen
.l
= symindx
;
4698 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4702 if (ISFCN (isymp
->n_type
)
4703 || ISTAG (isymp
->n_sclass
)
4704 || isymp
->n_sclass
== C_BLOCK
4705 || isymp
->n_sclass
== C_FCN
)
4707 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4709 && indx
< obj_raw_syment_count (input_bfd
))
4711 /* We look forward through the symbol for
4712 the index of the next symbol we are going
4713 to include. I don't know if this is
4715 while (flinfo
->sym_indices
[indx
] < 0
4716 && indx
< obj_raw_syment_count (input_bfd
))
4718 if (indx
>= obj_raw_syment_count (input_bfd
))
4719 indx
= output_index
;
4721 indx
= flinfo
->sym_indices
[indx
];
4722 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4727 indx
= aux
.x_sym
.x_tagndx
.l
;
4728 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4732 symindx
= flinfo
->sym_indices
[indx
];
4734 aux
.x_sym
.x_tagndx
.l
= 0;
4736 aux
.x_sym
.x_tagndx
.l
= symindx
;
4741 /* Copy over the line numbers, unless we are stripping
4742 them. We do this on a symbol by symbol basis in
4743 order to more easily handle garbage collection. */
4744 if (CSECT_SYM_P (isymp
->n_sclass
)
4746 && isymp
->n_numaux
> 1
4747 && ISFCN (isymp
->n_type
)
4748 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4750 if (*lineno_counts
== 0)
4751 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4754 asection
*enclosing
;
4755 unsigned int enc_count
;
4756 bfd_signed_vma linoff
;
4757 struct internal_lineno lin
;
4764 /* Read in the enclosing section's line-number
4765 information, if we haven't already. */
4767 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4768 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4769 if (oline
!= enclosing
)
4771 pos
= enclosing
->line_filepos
;
4772 amt
= linesz
* enc_count
;
4773 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4774 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
4780 /* Copy across the first entry, adjusting its
4782 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4783 - enclosing
->line_filepos
);
4784 linp
= flinfo
->linenos
+ linoff
;
4785 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4786 lin
.l_addr
.l_symndx
= *indexp
;
4787 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4789 /* Copy the other entries, adjusting their addresses. */
4790 linpend
= linp
+ *lineno_counts
* linesz
;
4791 offset
= (o
->output_section
->vma
4794 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4796 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4797 lin
.l_addr
.l_paddr
+= offset
;
4798 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4801 /* Write out the entries we've just processed. */
4802 pos
= (o
->output_section
->line_filepos
4803 + o
->output_section
->lineno_count
* linesz
);
4804 amt
= linesz
* *lineno_counts
;
4805 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4806 || bfd_bwrite (flinfo
->linenos
+ linoff
,
4807 amt
, output_bfd
) != amt
)
4809 o
->output_section
->lineno_count
+= *lineno_counts
;
4811 /* Record the offset of the symbol's line numbers
4812 in the output file. */
4813 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4817 struct internal_syment
*iisp
, *iispend
;
4820 bfd_vma range_start
, range_end
;
4823 /* Update any C_BINCL or C_EINCL symbols
4824 that refer to a line number in the
4825 range we just output. */
4826 iisp
= flinfo
->internal_syms
;
4827 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4828 iindp
= flinfo
->sym_indices
;
4829 oos
= flinfo
->outsyms
;
4830 range_start
= enclosing
->line_filepos
+ linoff
;
4831 range_end
= range_start
+ *lineno_counts
* linesz
;
4832 while (iisp
< iispend
)
4835 && (iisp
->n_sclass
== C_BINCL
4836 || iisp
->n_sclass
== C_EINCL
)
4837 && iisp
->n_value
>= range_start
4838 && iisp
->n_value
< range_end
)
4840 struct internal_syment iis
;
4842 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4843 iis
.n_value
= (iisp
->n_value
4846 bfd_coff_swap_sym_out (output_bfd
,
4851 iiadd
= 1 + iisp
->n_numaux
;
4853 oos
+= iiadd
* osymesz
;
4861 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4862 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4873 lineno_counts
+= add
;
4877 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4878 symbol will be the first symbol in the next input file. In the
4879 normal case, this will save us from writing out the C_FILE symbol
4881 if (flinfo
->last_file_index
!= -1
4882 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4884 flinfo
->last_file
.n_value
= output_index
;
4885 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
4886 (void *) (flinfo
->outsyms
4887 + ((flinfo
->last_file_index
- syment_base
)
4891 /* Write the modified symbols to the output file. */
4892 if (outsym
> flinfo
->outsyms
)
4894 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4895 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
4896 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4897 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
4900 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4901 + (outsym
- flinfo
->outsyms
) / osymesz
)
4904 obj_raw_syment_count (output_bfd
) = output_index
;
4907 /* Don't let the linker relocation routines discard the symbols. */
4908 keep_syms
= obj_coff_keep_syms (input_bfd
);
4909 obj_coff_keep_syms (input_bfd
) = true;
4911 /* Relocate the contents of each section. */
4912 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4916 if (! o
->linker_mark
)
4917 /* This section was omitted from the link. */
4920 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4922 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4925 /* We have set filepos correctly for the sections we created to
4926 represent csects, so bfd_get_section_contents should work. */
4927 if (coff_section_data (input_bfd
, o
) != NULL
4928 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4929 contents
= coff_section_data (input_bfd
, o
)->contents
;
4932 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4933 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
4935 contents
= flinfo
->contents
;
4938 if ((o
->flags
& SEC_RELOC
) != 0)
4941 struct internal_reloc
*internal_relocs
;
4942 struct internal_reloc
*irel
;
4944 struct internal_reloc
*irelend
;
4945 struct xcoff_link_hash_entry
**rel_hash
;
4948 /* Read in the relocs. */
4949 target_index
= o
->output_section
->target_index
;
4950 internal_relocs
= (xcoff_read_internal_relocs
4951 (input_bfd
, o
, false, flinfo
->external_relocs
,
4953 (flinfo
->section_info
[target_index
].relocs
4954 + o
->output_section
->reloc_count
)));
4955 if (internal_relocs
== NULL
)
4958 /* Call processor specific code to relocate the section
4960 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
4964 flinfo
->internal_syms
,
4965 xcoff_data (input_bfd
)->csects
))
4968 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4969 irel
= internal_relocs
;
4970 irelend
= irel
+ o
->reloc_count
;
4971 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
4972 + o
->output_section
->reloc_count
);
4973 for (; irel
< irelend
; irel
++, rel_hash
++)
4975 struct xcoff_link_hash_entry
*h
= NULL
;
4979 /* Adjust the reloc address and symbol index. */
4981 irel
->r_vaddr
+= offset
;
4983 r_symndx
= irel
->r_symndx
;
4988 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4990 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
4993 && h
->smclas
!= XMC_TD
4994 && (irel
->r_type
== R_TOC
4995 || irel
->r_type
== R_GL
4996 || irel
->r_type
== R_TCL
4997 || irel
->r_type
== R_TRL
4998 || irel
->r_type
== R_TRLA
))
5000 /* This is a TOC relative reloc with a symbol
5001 attached. The symbol should be the one which
5002 this reloc is for. We want to make this
5003 reloc against the TOC address of the symbol,
5004 not the symbol itself. */
5005 BFD_ASSERT (h
->toc_section
!= NULL
);
5006 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
5007 if (h
->u
.toc_indx
!= -1)
5008 irel
->r_symndx
= h
->u
.toc_indx
;
5011 struct xcoff_toc_rel_hash
*n
;
5012 struct xcoff_link_section_info
*si
;
5016 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
5019 si
= flinfo
->section_info
+ target_index
;
5020 n
->next
= si
->toc_rel_hashes
;
5023 si
->toc_rel_hashes
= n
;
5028 /* This is a global symbol. */
5030 irel
->r_symndx
= h
->indx
;
5033 /* This symbol is being written at the end
5034 of the file, and we do not yet know the
5035 symbol index. We save the pointer to the
5036 hash table entry in the rel_hash list.
5037 We set the indx field to -2 to indicate
5038 that this symbol must not be stripped. */
5047 indx
= flinfo
->sym_indices
[r_symndx
];
5051 struct internal_syment
*is
;
5053 /* Relocations against a TC0 TOC anchor are
5054 automatically transformed to be against
5055 the TOC anchor in the output file. */
5056 is
= flinfo
->internal_syms
+ r_symndx
;
5057 if (is
->n_sclass
== C_HIDEXT
5058 && is
->n_numaux
> 0)
5061 union internal_auxent aux
;
5065 obj_coff_external_syms (input_bfd
))
5066 + ((r_symndx
+ is
->n_numaux
)
5068 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
5069 is
->n_type
, is
->n_sclass
,
5073 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
5074 && aux
.x_csect
.x_smclas
== XMC_TC0
)
5075 indx
= flinfo
->toc_symindx
;
5080 irel
->r_symndx
= indx
;
5084 struct internal_syment
*is
;
5087 char buf
[SYMNMLEN
+ 1];
5089 /* This reloc is against a symbol we are
5090 stripping. It would be possible to handle
5091 this case, but I don't think it's worth it. */
5092 is
= flinfo
->internal_syms
+ r_symndx
;
5094 if (is
->n_sclass
!= C_DWARF
)
5096 name
= (_bfd_coff_internal_syment_name
5097 (input_bfd
, is
, buf
));
5102 (*flinfo
->info
->callbacks
->unattached_reloc
)
5103 (flinfo
->info
, name
,
5104 input_bfd
, o
, irel
->r_vaddr
);
5110 if ((o
->flags
& SEC_DEBUGGING
) == 0
5111 && xcoff_need_ldrel_p (flinfo
->info
, irel
, h
, o
))
5118 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
5120 sec
= xcoff_symbol_section (h
);
5121 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
5122 o
->output_section
, input_bfd
,
5128 o
->output_section
->reloc_count
+= o
->reloc_count
;
5131 /* Write out the modified section contents. */
5132 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5133 contents
, (file_ptr
) o
->output_offset
,
5138 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5140 if (! flinfo
->info
->keep_memory
)
5142 if (! _bfd_coff_free_symbols (input_bfd
))
5149 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5156 /* Sort relocs by VMA. This is called via qsort. */
5159 xcoff_sort_relocs (const void * p1
, const void * p2
)
5161 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
5162 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
5164 if (r1
->r_vaddr
> r2
->r_vaddr
)
5166 else if (r1
->r_vaddr
< r2
->r_vaddr
)
5172 /* Return true if section SEC is a TOC section. */
5175 xcoff_toc_section_p (asection
*sec
)
5180 if (name
[0] == '.' && name
[1] == 't')
5184 if (name
[3] == '0' && name
[4] == 0)
5189 if (name
[2] == 'd' && name
[3] == 0)
5195 /* See if the link requires a TOC (it usually does!). If so, find a
5196 good place to put the TOC anchor csect, and write out the associated
5200 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
5202 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5206 struct internal_syment irsym
;
5207 union internal_auxent iraux
;
5211 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5212 index of a csect at the beginning of the TOC. */
5213 toc_start
= ~(bfd_vma
) 0;
5216 for (input_bfd
= flinfo
->info
->input_bfds
;
5218 input_bfd
= input_bfd
->link
.next
)
5219 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5220 if (sec
->gc_mark
!= 0 && xcoff_toc_section_p (sec
))
5222 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5223 if (toc_start
> start
)
5226 section_index
= sec
->output_section
->target_index
;
5229 end
= start
+ sec
->size
;
5234 /* There's no need for a TC0 symbol if we don't have a TOC. */
5235 if (toc_end
< toc_start
)
5237 xcoff_data (output_bfd
)->toc
= toc_start
;
5241 if (toc_end
- toc_start
< 0x8000)
5242 /* Every TOC csect can be accessed from TOC_START. */
5243 best_address
= toc_start
;
5246 /* Find the lowest TOC csect that is still within range of TOC_END. */
5247 best_address
= toc_end
;
5248 for (input_bfd
= flinfo
->info
->input_bfds
;
5250 input_bfd
= input_bfd
->link
.next
)
5251 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5252 if (sec
->gc_mark
!= 0 && xcoff_toc_section_p (sec
))
5254 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5255 if (start
< best_address
5256 && start
+ 0x8000 >= toc_end
)
5258 best_address
= start
;
5259 section_index
= sec
->output_section
->target_index
;
5263 /* Make sure that the start of the TOC is also within range. */
5264 if (best_address
> toc_start
+ 0x8000)
5267 (_("TOC overflow: %#" PRIx64
" > 0x10000; try -mminimal-toc "
5269 (uint64_t) (toc_end
- toc_start
));
5270 bfd_set_error (bfd_error_file_too_big
);
5275 /* Record the chosen TOC value. */
5276 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5277 xcoff_data (output_bfd
)->toc
= best_address
;
5278 xcoff_data (output_bfd
)->sntoc
= section_index
;
5280 /* Fill out the TC0 symbol. */
5281 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5284 irsym
.n_value
= best_address
;
5285 irsym
.n_scnum
= section_index
;
5286 irsym
.n_sclass
= C_HIDEXT
;
5287 irsym
.n_type
= T_NULL
;
5289 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
5291 /* Fill out the auxiliary csect information. */
5292 memset (&iraux
, 0, sizeof iraux
);
5293 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5294 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5295 iraux
.x_csect
.x_scnlen
.l
= 0;
5296 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5297 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5299 /* Write the contents to the file. */
5300 pos
= obj_sym_filepos (output_bfd
);
5301 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5302 size
= 2 * bfd_coff_symesz (output_bfd
);
5303 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5304 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
5306 obj_raw_syment_count (output_bfd
) += 2;
5311 /* Write out a non-XCOFF global symbol. */
5314 xcoff_write_global_symbol (struct bfd_hash_entry
*bh
, void * inf
)
5316 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) bh
;
5317 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
5320 struct internal_syment isym
;
5321 union internal_auxent aux
;
5326 output_bfd
= flinfo
->output_bfd
;
5327 outsym
= flinfo
->outsyms
;
5329 if (h
->root
.type
== bfd_link_hash_warning
)
5331 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5332 if (h
->root
.type
== bfd_link_hash_new
)
5336 /* If this symbol was garbage collected, just skip it. */
5337 if (xcoff_hash_table (flinfo
->info
)->gc
5338 && (h
->flags
& XCOFF_MARK
) == 0)
5341 /* If we need a .loader section entry, write it out. */
5342 if (h
->ldsym
!= NULL
)
5344 struct internal_ldsym
*ldsym
;
5349 if (h
->root
.type
== bfd_link_hash_undefined
5350 || h
->root
.type
== bfd_link_hash_undefweak
)
5354 ldsym
->l_scnum
= N_UNDEF
;
5355 ldsym
->l_smtype
= XTY_ER
;
5356 impbfd
= h
->root
.u
.undef
.abfd
;
5359 else if (h
->root
.type
== bfd_link_hash_defined
5360 || h
->root
.type
== bfd_link_hash_defweak
)
5364 sec
= h
->root
.u
.def
.section
;
5365 ldsym
->l_value
= (sec
->output_section
->vma
5366 + sec
->output_offset
5367 + h
->root
.u
.def
.value
);
5368 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5369 ldsym
->l_smtype
= XTY_SD
;
5370 impbfd
= sec
->owner
;
5376 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5377 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5378 || (h
->flags
& XCOFF_IMPORT
) != 0)
5380 Import symbols are defined so the check above will make
5381 the l_smtype XTY_SD. But this is not correct, it should
5383 ldsym
->l_smtype
|= L_IMPORT
;
5385 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5386 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5387 || (h
->flags
& XCOFF_EXPORT
) != 0)
5388 ldsym
->l_smtype
|= L_EXPORT
;
5390 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5391 ldsym
->l_smtype
|= L_ENTRY
;
5393 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5394 ldsym
->l_smtype
= XTY_SD
;
5396 ldsym
->l_smclas
= h
->smclas
;
5398 if (ldsym
->l_smtype
& L_IMPORT
)
5400 if ((h
->root
.type
== bfd_link_hash_defined
5401 || h
->root
.type
== bfd_link_hash_defweak
)
5402 && (h
->root
.u
.def
.value
!= 0))
5403 ldsym
->l_smclas
= XMC_XO
;
5405 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5406 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5407 ldsym
->l_smclas
= XMC_SV3264
;
5409 else if (h
->flags
& XCOFF_SYSCALL32
)
5410 ldsym
->l_smclas
= XMC_SV
;
5412 else if (h
->flags
& XCOFF_SYSCALL64
)
5413 ldsym
->l_smclas
= XMC_SV64
;
5416 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5420 else if (ldsym
->l_ifile
== 0)
5422 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5424 else if (impbfd
== NULL
)
5428 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5429 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5435 BFD_ASSERT (h
->ldindx
>= 0);
5437 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5440 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
5444 /* If this symbol needs global linkage code, write it out. */
5445 if (h
->root
.type
== bfd_link_hash_defined
5446 && (h
->root
.u
.def
.section
5447 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
5453 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5455 /* The first instruction in the global linkage code loads a
5456 specific TOC element. */
5457 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5458 + h
->descriptor
->toc_section
->output_offset
5459 - xcoff_data (output_bfd
)->toc
);
5461 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5462 tocoff
+= h
->descriptor
->u
.toc_offset
;
5464 /* The first instruction in the glink code needs to be
5465 cooked to hold the correct offset in the toc. The
5466 rest are just output raw. */
5467 bfd_put_32 (output_bfd
,
5468 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5470 /* Start with i == 1 to get past the first instruction done above
5471 The /4 is because the glink code is in bytes and we are going
5473 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5474 bfd_put_32 (output_bfd
,
5475 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5479 /* If we created a TOC entry for this symbol, write out the required
5481 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5486 struct internal_reloc
*irel
;
5487 struct internal_syment irsym
;
5488 union internal_auxent iraux
;
5490 tocsec
= h
->toc_section
;
5491 osec
= tocsec
->output_section
;
5492 oindx
= osec
->target_index
;
5493 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5494 irel
->r_vaddr
= (osec
->vma
5495 + tocsec
->output_offset
5499 irel
->r_symndx
= h
->indx
;
5503 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5506 BFD_ASSERT (h
->ldindx
>= 0);
5508 /* Initialize the aux union here instead of closer to when it is
5509 written out below because the length of the csect depends on
5510 whether the output is 32 or 64 bit. */
5511 memset (&iraux
, 0, sizeof iraux
);
5512 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5513 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5514 iraux
.x_csect
.x_smclas
= XMC_TC
;
5516 /* 32 bit uses a 32 bit R_POS to do the relocations
5517 64 bit uses a 64 bit R_POS to do the relocations
5519 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5521 Which one is determined by the backend. */
5522 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5525 iraux
.x_csect
.x_scnlen
.l
= 8;
5527 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5530 iraux
.x_csect
.x_scnlen
.l
= 4;
5535 irel
->r_type
= R_POS
;
5536 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5537 ++osec
->reloc_count
;
5539 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5540 output_bfd
, irel
, NULL
, h
))
5543 /* We need to emit a symbol to define a csect which holds
5545 if (flinfo
->info
->strip
!= strip_all
)
5547 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
,
5549 &irsym
, h
->root
.root
.string
);
5553 irsym
.n_value
= irel
->r_vaddr
;
5554 irsym
.n_scnum
= osec
->target_index
;
5555 irsym
.n_sclass
= C_HIDEXT
;
5556 irsym
.n_type
= T_NULL
;
5559 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5560 outsym
+= bfd_coff_symesz (output_bfd
);
5562 /* Note : iraux is initialized above. */
5563 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5564 0, 1, (void *) outsym
);
5565 outsym
+= bfd_coff_auxesz (output_bfd
);
5569 /* We aren't going to write out the symbols below, so we
5570 need to write them out now. */
5571 pos
= obj_sym_filepos (output_bfd
);
5572 pos
+= (obj_raw_syment_count (output_bfd
)
5573 * bfd_coff_symesz (output_bfd
));
5574 amt
= outsym
- flinfo
->outsyms
;
5575 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5576 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5578 obj_raw_syment_count (output_bfd
) +=
5579 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5581 outsym
= flinfo
->outsyms
;
5586 /* If this symbol is a specially defined function descriptor, write
5587 it out. The first word is the address of the function code
5588 itself, the second word is the address of the TOC, and the third
5592 The addresses for the 32 bit will take 4 bytes and the addresses
5593 for 64 bit will take 8 bytes. Similar for the relocs. This type
5594 of logic was also done above to create a TOC entry in
5595 xcoff_write_global_symbol. */
5596 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5597 && h
->root
.type
== bfd_link_hash_defined
5598 && (h
->root
.u
.def
.section
5599 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
5605 struct xcoff_link_hash_entry
*hentry
;
5607 struct internal_reloc
*irel
;
5609 unsigned int reloc_size
, byte_size
;
5611 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5616 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5624 sec
= h
->root
.u
.def
.section
;
5625 osec
= sec
->output_section
;
5626 oindx
= osec
->target_index
;
5627 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5629 hentry
= h
->descriptor
;
5630 BFD_ASSERT (hentry
!= NULL
5631 && (hentry
->root
.type
== bfd_link_hash_defined
5632 || hentry
->root
.type
== bfd_link_hash_defweak
));
5633 esec
= hentry
->root
.u
.def
.section
;
5635 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5636 irel
->r_vaddr
= (osec
->vma
5637 + sec
->output_offset
5638 + h
->root
.u
.def
.value
);
5639 irel
->r_symndx
= esec
->output_section
->target_index
;
5640 irel
->r_type
= R_POS
;
5641 irel
->r_size
= reloc_size
;
5642 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5643 ++osec
->reloc_count
;
5645 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5646 output_bfd
, irel
, esec
, NULL
))
5649 /* There are three items to write out,
5650 the address of the code
5651 the address of the toc anchor
5652 the environment pointer.
5653 We are ignoring the environment pointer. So set it to zero. */
5654 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5656 bfd_put_64 (output_bfd
,
5657 (esec
->output_section
->vma
+ esec
->output_offset
5658 + hentry
->root
.u
.def
.value
),
5660 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5661 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5666 This logic was already called above so the error case where
5667 the backend is neither has already been checked. */
5668 bfd_put_32 (output_bfd
,
5669 (esec
->output_section
->vma
+ esec
->output_offset
5670 + hentry
->root
.u
.def
.value
),
5672 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5673 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5676 tsec
= coff_section_from_bfd_index (output_bfd
,
5677 xcoff_data (output_bfd
)->sntoc
);
5680 irel
->r_vaddr
= (osec
->vma
5681 + sec
->output_offset
5682 + h
->root
.u
.def
.value
5684 irel
->r_symndx
= tsec
->output_section
->target_index
;
5685 irel
->r_type
= R_POS
;
5686 irel
->r_size
= reloc_size
;
5687 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5688 ++osec
->reloc_count
;
5690 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5691 output_bfd
, irel
, tsec
, NULL
))
5695 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
5697 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5702 && (flinfo
->info
->strip
== strip_all
5703 || (flinfo
->info
->strip
== strip_some
5704 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
5705 false, false) == NULL
)))
5707 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5712 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5714 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5718 memset (&aux
, 0, sizeof aux
);
5720 h
->indx
= obj_raw_syment_count (output_bfd
);
5722 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5723 &isym
, h
->root
.root
.string
);
5727 if (h
->root
.type
== bfd_link_hash_undefined
5728 || h
->root
.type
== bfd_link_hash_undefweak
)
5731 isym
.n_scnum
= N_UNDEF
;
5732 if (h
->root
.type
== bfd_link_hash_undefweak
5733 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5734 isym
.n_sclass
= C_WEAKEXT
;
5736 isym
.n_sclass
= C_EXT
;
5737 aux
.x_csect
.x_smtyp
= XTY_ER
;
5739 else if ((h
->root
.type
== bfd_link_hash_defined
5740 || h
->root
.type
== bfd_link_hash_defweak
)
5741 && h
->smclas
== XMC_XO
)
5743 BFD_ASSERT (bfd_is_abs_symbol (&h
->root
));
5744 isym
.n_value
= h
->root
.u
.def
.value
;
5745 isym
.n_scnum
= N_UNDEF
;
5746 if (h
->root
.type
== bfd_link_hash_defweak
5747 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5748 isym
.n_sclass
= C_WEAKEXT
;
5750 isym
.n_sclass
= C_EXT
;
5751 aux
.x_csect
.x_smtyp
= XTY_ER
;
5753 else if (h
->root
.type
== bfd_link_hash_defined
5754 || h
->root
.type
== bfd_link_hash_defweak
)
5756 struct xcoff_link_size_list
*l
;
5758 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5759 + h
->root
.u
.def
.section
->output_offset
5760 + h
->root
.u
.def
.value
);
5761 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5762 isym
.n_scnum
= N_ABS
;
5764 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5765 isym
.n_sclass
= C_HIDEXT
;
5766 aux
.x_csect
.x_smtyp
= XTY_SD
;
5768 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5770 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
5776 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5782 else if (h
->root
.type
== bfd_link_hash_common
)
5784 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5785 + h
->root
.u
.c
.p
->section
->output_offset
);
5786 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5787 isym
.n_sclass
= C_EXT
;
5788 aux
.x_csect
.x_smtyp
= XTY_CM
;
5789 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5794 isym
.n_type
= T_NULL
;
5797 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5798 outsym
+= bfd_coff_symesz (output_bfd
);
5800 aux
.x_csect
.x_smclas
= h
->smclas
;
5801 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5803 outsym
+= bfd_coff_auxesz (output_bfd
);
5805 if ((h
->root
.type
== bfd_link_hash_defined
5806 || h
->root
.type
== bfd_link_hash_defweak
)
5807 && h
->smclas
!= XMC_XO
)
5809 /* We just output an SD symbol. Now output an LD symbol. */
5812 if (h
->root
.type
== bfd_link_hash_defweak
5813 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5814 isym
.n_sclass
= C_WEAKEXT
;
5816 isym
.n_sclass
= C_EXT
;
5817 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5818 outsym
+= bfd_coff_symesz (output_bfd
);
5820 aux
.x_csect
.x_smtyp
= XTY_LD
;
5821 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5822 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5824 outsym
+= bfd_coff_auxesz (output_bfd
);
5827 pos
= obj_sym_filepos (output_bfd
);
5828 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5829 amt
= outsym
- flinfo
->outsyms
;
5830 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5831 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5833 obj_raw_syment_count (output_bfd
) +=
5834 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5839 /* Handle a link order which is supposed to generate a reloc. */
5842 xcoff_reloc_link_order (bfd
*output_bfd
,
5843 struct xcoff_final_link_info
*flinfo
,
5844 asection
*output_section
,
5845 struct bfd_link_order
*link_order
)
5847 reloc_howto_type
*howto
;
5848 struct xcoff_link_hash_entry
*h
;
5852 struct internal_reloc
*irel
;
5853 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5855 if (link_order
->type
== bfd_section_reloc_link_order
)
5856 /* We need to somehow locate a symbol in the right section. The
5857 symbol must either have a value of zero, or we must adjust
5858 the addend by the value of the symbol. FIXME: Write this
5859 when we need it. The old linker couldn't handle this anyhow. */
5862 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5865 bfd_set_error (bfd_error_bad_value
);
5869 h
= ((struct xcoff_link_hash_entry
*)
5870 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
5871 link_order
->u
.reloc
.p
->u
.name
,
5872 false, false, true));
5875 (*flinfo
->info
->callbacks
->unattached_reloc
)
5876 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
5880 hsec
= xcoff_symbol_section (h
);
5881 if (h
->root
.type
== bfd_link_hash_defined
5882 || h
->root
.type
== bfd_link_hash_defweak
)
5883 hval
= h
->root
.u
.def
.value
;
5887 addend
= link_order
->u
.reloc
.p
->addend
;
5889 addend
+= (hsec
->output_section
->vma
5890 + hsec
->output_offset
5897 bfd_reloc_status_type rstat
;
5900 size
= bfd_get_reloc_size (howto
);
5901 buf
= bfd_zmalloc (size
);
5902 if (buf
== NULL
&& size
!= 0)
5905 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5911 case bfd_reloc_outofrange
:
5913 case bfd_reloc_overflow
:
5914 (*flinfo
->info
->callbacks
->reloc_overflow
)
5915 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5916 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0);
5919 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5920 (file_ptr
) link_order
->offset
, size
);
5926 /* Store the reloc information in the right place. It will get
5927 swapped and written out at the end of the final_link routine. */
5928 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
5929 + output_section
->reloc_count
);
5930 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
5931 + output_section
->reloc_count
);
5933 memset (irel
, 0, sizeof (struct internal_reloc
));
5934 *rel_hash_ptr
= NULL
;
5936 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5939 irel
->r_symndx
= h
->indx
;
5942 /* Set the index to -2 to force this symbol to get written out. */
5948 irel
->r_type
= howto
->type
;
5949 irel
->r_size
= howto
->bitsize
- 1;
5950 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5951 irel
->r_size
|= 0x80;
5953 ++output_section
->reloc_count
;
5955 /* Now output the reloc to the .loader section. */
5956 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
5958 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
5959 output_bfd
, irel
, hsec
, h
))
5966 /* Do the final link step. */
5969 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5971 bfd_size_type symesz
;
5972 struct xcoff_final_link_info flinfo
;
5974 struct bfd_link_order
*p
;
5975 bfd_size_type max_contents_size
;
5976 bfd_size_type max_sym_count
;
5977 bfd_size_type max_lineno_count
;
5978 bfd_size_type max_reloc_count
;
5979 bfd_size_type max_output_reloc_count
;
5980 file_ptr rel_filepos
;
5982 file_ptr line_filepos
;
5983 unsigned int linesz
;
5985 bfd_byte
*external_relocs
= NULL
;
5986 char strbuf
[STRING_SIZE_SIZE
];
5990 if (bfd_link_pic (info
))
5991 abfd
->flags
|= DYNAMIC
;
5993 symesz
= bfd_coff_symesz (abfd
);
5996 flinfo
.output_bfd
= abfd
;
5997 flinfo
.strtab
= NULL
;
5998 flinfo
.section_info
= NULL
;
5999 flinfo
.last_file_index
= -1;
6000 flinfo
.toc_symindx
= -1;
6001 flinfo
.internal_syms
= NULL
;
6002 flinfo
.sym_indices
= NULL
;
6003 flinfo
.outsyms
= NULL
;
6004 flinfo
.linenos
= NULL
;
6005 flinfo
.contents
= NULL
;
6006 flinfo
.external_relocs
= NULL
;
6008 if (xcoff_hash_table (info
)->loader_section
)
6010 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
6011 + bfd_xcoff_ldhdrsz (abfd
));
6012 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
6013 + bfd_xcoff_ldhdrsz (abfd
)
6014 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
6015 * bfd_xcoff_ldsymsz (abfd
)));
6019 flinfo
.ldsym
= NULL
;
6020 flinfo
.ldrel
= NULL
;
6023 xcoff_data (abfd
)->coff
.link_info
= info
;
6025 flinfo
.strtab
= _bfd_stringtab_init ();
6026 if (flinfo
.strtab
== NULL
)
6029 /* Count the relocation entries required for the output file.
6030 (We've already counted the line numbers.) Determine a few
6032 max_contents_size
= 0;
6033 max_lineno_count
= 0;
6034 max_reloc_count
= 0;
6035 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6038 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6040 if (p
->type
== bfd_indirect_link_order
)
6044 sec
= p
->u
.indirect
.section
;
6046 /* Mark all sections which are to be included in the
6047 link. This will normally be every section. We need
6048 to do this so that we can identify any sections which
6049 the linker has decided to not include. */
6050 sec
->linker_mark
= true;
6052 o
->reloc_count
+= sec
->reloc_count
;
6054 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
6056 if (sec
->rawsize
> max_contents_size
)
6057 max_contents_size
= sec
->rawsize
;
6058 if (sec
->size
> max_contents_size
)
6059 max_contents_size
= sec
->size
;
6061 if (coff_section_data (sec
->owner
, sec
) != NULL
6062 && xcoff_section_data (sec
->owner
, sec
) != NULL
6063 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
6064 > max_lineno_count
))
6066 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
6067 if (sec
->reloc_count
> max_reloc_count
)
6068 max_reloc_count
= sec
->reloc_count
;
6070 else if (p
->type
== bfd_section_reloc_link_order
6071 || p
->type
== bfd_symbol_reloc_link_order
)
6076 /* Compute the file positions for all the sections. */
6077 if (abfd
->output_has_begun
)
6079 if (xcoff_hash_table (info
)->file_align
!= 0)
6086 file_align
= xcoff_hash_table (info
)->file_align
;
6087 if (file_align
!= 0)
6093 /* Insert .pad sections before every section which has
6094 contents and is loaded, if it is preceded by some other
6095 section which has contents and is loaded. */
6096 saw_contents
= true;
6097 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6099 if (strcmp (o
->name
, ".pad") == 0)
6100 saw_contents
= false;
6101 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
6102 && (o
->flags
& SEC_LOAD
) != 0)
6105 saw_contents
= true;
6110 /* Create a pad section and place it before the section
6111 that needs padding. This requires unlinking and
6112 relinking the bfd's section list. */
6114 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
6116 n
->alignment_power
= 0;
6118 bfd_section_list_remove (abfd
, n
);
6119 bfd_section_list_insert_before (abfd
, o
, n
);
6120 saw_contents
= false;
6125 /* Reset the section indices after inserting the new
6128 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6131 o
->target_index
= indx
;
6133 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
6135 /* Work out appropriate sizes for the .pad sections to force
6136 each section to land on a page boundary. This bit of
6137 code knows what compute_section_file_positions is going
6139 sofar
= bfd_coff_filhsz (abfd
);
6140 sofar
+= bfd_coff_aoutsz (abfd
);
6141 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
6142 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6143 if ((bfd_xcoff_is_reloc_count_overflow
6144 (abfd
, (bfd_vma
) o
->reloc_count
))
6145 || (bfd_xcoff_is_lineno_count_overflow
6146 (abfd
, (bfd_vma
) o
->lineno_count
)))
6147 /* 64 does not overflow, need to check if 32 does */
6148 sofar
+= bfd_coff_scnhsz (abfd
);
6150 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6152 if (strcmp (o
->name
, ".pad") == 0)
6156 BFD_ASSERT (o
->size
== 0);
6157 pageoff
= sofar
& (file_align
- 1);
6160 o
->size
= file_align
- pageoff
;
6161 sofar
+= file_align
- pageoff
;
6162 o
->flags
|= SEC_HAS_CONTENTS
;
6167 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
6168 sofar
+= BFD_ALIGN (o
->size
,
6169 1 << o
->alignment_power
);
6174 if (! bfd_coff_compute_section_file_positions (abfd
))
6178 /* Allocate space for the pointers we need to keep for the relocs. */
6182 /* We use section_count + 1, rather than section_count, because
6183 the target_index fields are 1 based. */
6184 amt
= abfd
->section_count
+ 1;
6185 amt
*= sizeof (struct xcoff_link_section_info
);
6186 flinfo
.section_info
= bfd_malloc (amt
);
6187 if (flinfo
.section_info
== NULL
)
6189 for (i
= 0; i
<= abfd
->section_count
; i
++)
6191 flinfo
.section_info
[i
].relocs
= NULL
;
6192 flinfo
.section_info
[i
].rel_hashes
= NULL
;
6193 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6197 /* Set the file positions for the relocs. */
6198 rel_filepos
= obj_relocbase (abfd
);
6199 relsz
= bfd_coff_relsz (abfd
);
6200 max_output_reloc_count
= 0;
6201 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6203 if (o
->reloc_count
== 0)
6207 /* A stripped file has no relocs. However, we still
6208 allocate the buffers, so that later code doesn't have to
6209 worry about whether we are stripping or not. */
6210 if (info
->strip
== strip_all
)
6214 o
->flags
|= SEC_RELOC
;
6215 o
->rel_filepos
= rel_filepos
;
6216 rel_filepos
+= o
->reloc_count
* relsz
;
6219 /* We don't know the indices of global symbols until we have
6220 written out all the local symbols. For each section in
6221 the output file, we keep an array of pointers to hash
6222 table entries. Each entry in the array corresponds to a
6223 reloc. When we find a reloc against a global symbol, we
6224 set the corresponding entry in this array so that we can
6225 fix up the symbol index after we have written out all the
6228 Because of this problem, we also keep the relocs in
6229 memory until the end of the link. This wastes memory.
6230 We could backpatch the file later, I suppose, although it
6232 amt
= o
->reloc_count
;
6233 amt
*= sizeof (struct internal_reloc
);
6234 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6236 amt
= o
->reloc_count
;
6237 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6238 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6240 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
6241 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6244 if (o
->reloc_count
> max_output_reloc_count
)
6245 max_output_reloc_count
= o
->reloc_count
;
6249 /* We now know the size of the relocs, so we can determine the file
6250 positions of the line numbers. */
6251 line_filepos
= rel_filepos
;
6252 flinfo
.line_filepos
= line_filepos
;
6253 linesz
= bfd_coff_linesz (abfd
);
6254 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6256 if (o
->lineno_count
== 0)
6257 o
->line_filepos
= 0;
6260 o
->line_filepos
= line_filepos
;
6261 line_filepos
+= o
->lineno_count
* linesz
;
6264 /* Reset the reloc and lineno counts, so that we can use them to
6265 count the number of entries we have output so far. */
6267 o
->lineno_count
= 0;
6270 obj_sym_filepos (abfd
) = line_filepos
;
6272 /* Figure out the largest number of symbols in an input BFD. Take
6273 the opportunity to clear the output_has_begun fields of all the
6274 input BFD's. We want at least 6 symbols, since that is the
6275 number which xcoff_write_global_symbol may need. */
6277 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6281 sub
->output_has_begun
= false;
6282 sz
= obj_raw_syment_count (sub
);
6283 if (sz
> max_sym_count
)
6287 /* Allocate some buffers used while linking. */
6288 amt
= max_sym_count
* sizeof (struct internal_syment
);
6289 flinfo
.internal_syms
= bfd_malloc (amt
);
6291 amt
= max_sym_count
* sizeof (long);
6292 flinfo
.sym_indices
= bfd_malloc (amt
);
6294 amt
= (max_sym_count
+ 1) * symesz
;
6295 flinfo
.outsyms
= bfd_malloc (amt
);
6297 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6298 flinfo
.linenos
= bfd_malloc (amt
);
6300 amt
= max_contents_size
;
6301 flinfo
.contents
= bfd_malloc (amt
);
6303 amt
= max_reloc_count
* relsz
;
6304 flinfo
.external_relocs
= bfd_malloc (amt
);
6306 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6307 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6308 || flinfo
.outsyms
== NULL
6309 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
6310 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
6311 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6314 obj_raw_syment_count (abfd
) = 0;
6316 /* Find a TOC symbol, if we need one. */
6317 if (!xcoff_find_tc0 (abfd
, &flinfo
))
6320 /* We now know the position of everything in the file, except that
6321 we don't know the size of the symbol table and therefore we don't
6322 know where the string table starts. We just build the string
6323 table in memory as we go along. We process all the relocations
6324 for a single input file at once. */
6325 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6327 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6329 if (p
->type
== bfd_indirect_link_order
6330 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6332 sub
= p
->u
.indirect
.section
->owner
;
6333 if (! sub
->output_has_begun
)
6335 if (! xcoff_link_input_bfd (&flinfo
, sub
))
6337 sub
->output_has_begun
= true;
6340 else if (p
->type
== bfd_section_reloc_link_order
6341 || p
->type
== bfd_symbol_reloc_link_order
)
6343 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
6348 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6354 /* Free up the buffers used by xcoff_link_input_bfd. */
6355 free (flinfo
.internal_syms
);
6356 flinfo
.internal_syms
= NULL
;
6357 free (flinfo
.sym_indices
);
6358 flinfo
.sym_indices
= NULL
;
6359 free (flinfo
.linenos
);
6360 flinfo
.linenos
= NULL
;
6361 free (flinfo
.contents
);
6362 flinfo
.contents
= NULL
;
6363 free (flinfo
.external_relocs
);
6364 flinfo
.external_relocs
= NULL
;
6366 /* The value of the last C_FILE symbol is supposed to be -1. Write
6368 if (flinfo
.last_file_index
!= -1)
6370 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
6371 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
6372 (void *) flinfo
.outsyms
);
6373 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
6374 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6375 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
6379 /* Write out all the global symbols which do not come from XCOFF
6381 bfd_hash_traverse (&info
->hash
->table
, xcoff_write_global_symbol
, &flinfo
);
6383 free (flinfo
.outsyms
);
6384 flinfo
.outsyms
= NULL
;
6386 /* Now that we have written out all the global symbols, we know the
6387 symbol indices to use for relocs against them, and we can finally
6388 write out the relocs. */
6389 amt
= max_output_reloc_count
* relsz
;
6390 external_relocs
= bfd_malloc (amt
);
6391 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6394 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6396 struct internal_reloc
*irel
;
6397 struct internal_reloc
*irelend
;
6398 struct xcoff_link_hash_entry
**rel_hash
;
6399 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6401 bfd_size_type rel_size
;
6403 /* A stripped file has no relocs. */
6404 if (info
->strip
== strip_all
)
6410 if (o
->reloc_count
== 0)
6413 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6414 irelend
= irel
+ o
->reloc_count
;
6415 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
6416 for (; irel
< irelend
; irel
++, rel_hash
++)
6418 if (*rel_hash
!= NULL
)
6420 if ((*rel_hash
)->indx
< 0)
6422 (*info
->callbacks
->unattached_reloc
)
6423 (info
, (*rel_hash
)->root
.root
.string
,
6424 NULL
, o
, irel
->r_vaddr
);
6425 (*rel_hash
)->indx
= 0;
6427 irel
->r_symndx
= (*rel_hash
)->indx
;
6431 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6432 toc_rel_hash
!= NULL
;
6433 toc_rel_hash
= toc_rel_hash
->next
)
6435 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6437 (*info
->callbacks
->unattached_reloc
)
6438 (info
, toc_rel_hash
->h
->root
.root
.string
,
6439 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
);
6440 toc_rel_hash
->h
->u
.toc_indx
= 0;
6442 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6445 /* XCOFF requires that the relocs be sorted by address. We tend
6446 to produce them in the order in which their containing csects
6447 appear in the symbol table, which is not necessarily by
6448 address. So we sort them here. There may be a better way to
6450 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
6451 o
->reloc_count
, sizeof (struct internal_reloc
),
6454 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6455 irelend
= irel
+ o
->reloc_count
;
6456 erel
= external_relocs
;
6457 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6458 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6460 rel_size
= relsz
* o
->reloc_count
;
6461 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6462 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6466 free (external_relocs
);
6467 external_relocs
= NULL
;
6469 /* Free up the section information. */
6470 if (flinfo
.section_info
!= NULL
)
6474 for (i
= 0; i
< abfd
->section_count
; i
++)
6476 free (flinfo
.section_info
[i
].relocs
);
6477 free (flinfo
.section_info
[i
].rel_hashes
);
6479 free (flinfo
.section_info
);
6480 flinfo
.section_info
= NULL
;
6483 /* Write out the loader section contents. */
6484 o
= xcoff_hash_table (info
)->loader_section
;
6487 && o
->output_section
!= bfd_abs_section_ptr
)
6489 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
6490 == (xcoff_hash_table (info
)->loader_section
->contents
6491 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6492 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6493 (file_ptr
) o
->output_offset
, o
->size
))
6497 /* Write out the magic sections. */
6498 o
= xcoff_hash_table (info
)->linkage_section
;
6501 && o
->output_section
!= bfd_abs_section_ptr
6502 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6503 (file_ptr
) o
->output_offset
,
6506 o
= xcoff_hash_table (info
)->toc_section
;
6509 && o
->output_section
!= bfd_abs_section_ptr
6510 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6511 (file_ptr
) o
->output_offset
,
6514 o
= xcoff_hash_table (info
)->descriptor_section
;
6517 && o
->output_section
!= bfd_abs_section_ptr
6518 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6519 (file_ptr
) o
->output_offset
,
6523 /* Write out the string table. */
6524 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6525 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6528 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
6530 amt
= STRING_SIZE_SIZE
;
6531 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6533 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
6536 _bfd_stringtab_free (flinfo
.strtab
);
6538 /* Write out the debugging string table. */
6539 o
= xcoff_hash_table (info
)->debug_section
;
6542 && o
->output_section
!= bfd_abs_section_ptr
)
6544 struct bfd_strtab_hash
*debug_strtab
;
6546 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6547 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6548 >= _bfd_stringtab_size (debug_strtab
));
6549 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6550 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6552 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6556 /* Setting symcount to 0 will cause write_object_contents to
6557 not try to write out the symbols. */
6563 if (flinfo
.strtab
!= NULL
)
6564 _bfd_stringtab_free (flinfo
.strtab
);
6566 if (flinfo
.section_info
!= NULL
)
6570 for (i
= 0; i
< abfd
->section_count
; i
++)
6572 free (flinfo
.section_info
[i
].relocs
);
6573 free (flinfo
.section_info
[i
].rel_hashes
);
6575 free (flinfo
.section_info
);
6578 free (flinfo
.internal_syms
);
6579 free (flinfo
.sym_indices
);
6580 free (flinfo
.outsyms
);
6581 free (flinfo
.linenos
);
6582 free (flinfo
.contents
);
6583 free (flinfo
.external_relocs
);
6584 free (external_relocs
);