1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "coff/internal.h"
27 #include "coff/xcoff.h"
30 #include "libiberty.h"
31 #include "xcofflink.h"
33 /* This file holds the XCOFF linker code. */
35 #undef STRING_SIZE_SIZE
36 #define STRING_SIZE_SIZE 4
38 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
39 This flag will only be used on input sections. */
41 #define SEC_MARK (SEC_ROM)
43 /* The list of import files. */
45 struct xcoff_import_file
47 /* The next entry in the list. */
48 struct xcoff_import_file
*next
;
53 /* The member name. */
57 /* Information we keep for each section in the output file during the
60 struct xcoff_link_section_info
62 /* The relocs to be output. */
63 struct internal_reloc
*relocs
;
64 /* For each reloc against a global symbol whose index was not known
65 when the reloc was handled, the global hash table entry. */
66 struct xcoff_link_hash_entry
**rel_hashes
;
67 /* If there is a TOC relative reloc against a global symbol, and the
68 index of the TOC symbol is not known when the reloc was handled,
69 an entry is added to this linked list. This is not an array,
70 like rel_hashes, because this case is quite uncommon. */
71 struct xcoff_toc_rel_hash
73 struct xcoff_toc_rel_hash
*next
;
74 struct xcoff_link_hash_entry
*h
;
75 struct internal_reloc
*rel
;
79 /* Information that the XCOFF linker collects about an archive. */
80 struct xcoff_archive_info
82 /* The archive described by this entry. */
85 /* The import path and import filename to use when referring to
86 this archive in the .loader section. */
90 /* True if the archive contains a dynamic object. */
91 unsigned int contains_shared_object_p
: 1;
93 /* True if the previous field is valid. */
94 unsigned int know_contains_shared_object_p
: 1;
97 struct xcoff_link_hash_table
99 struct bfd_link_hash_table root
;
101 /* The .debug string hash table. We need to compute this while
102 reading the input files, so that we know how large the .debug
103 section will be before we assign section positions. */
104 struct bfd_strtab_hash
*debug_strtab
;
106 /* The .debug section we will use for the final output. */
107 asection
*debug_section
;
109 /* The .loader section we will use for the final output. */
110 asection
*loader_section
;
112 /* A count of non TOC relative relocs which will need to be
113 allocated in the .loader section. */
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr
;
119 /* The .gl section we use to hold global linkage code. */
120 asection
*linkage_section
;
122 /* The .tc section we use to hold toc entries we build for global
124 asection
*toc_section
;
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection
*descriptor_section
;
130 /* The list of import files. */
131 struct xcoff_import_file
*imports
;
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align
;
136 /* Whether the .text section must be read-only. */
139 /* Whether -brtl was specified. */
142 /* Whether garbage collection was done. */
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
148 struct xcoff_link_size_list
*next
;
149 struct xcoff_link_hash_entry
*h
;
154 /* Information about archives. */
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection
*special_sections
[XCOFF_NUMBER_OF_SPECIAL_SECTIONS
];
161 /* Information that we pass around while doing the final link step. */
163 struct xcoff_final_link_info
165 /* General link information. */
166 struct bfd_link_info
*info
;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash
*strtab
;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info
*section_info
;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index
;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file
;
178 /* Symbol index of TOC symbol. */
180 /* Start of .loader symbols. */
182 /* Next .loader reloc to swap out. */
184 /* File position of start of line numbers. */
185 file_ptr line_filepos
;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment
*internal_syms
;
188 /* Buffer large enough to hold output indices of symbols of any
191 /* Buffer large enough to hold output symbols for any input file. */
193 /* Buffer large enough to hold external line numbers for any input
196 /* Buffer large enough to hold any input section. */
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte
*external_relocs
;
202 static bfd_boolean
xcoff_mark (struct bfd_link_info
*, asection
*);
206 /* Routines to read XCOFF dynamic information. This don't really
207 belong here, but we already have the ldsym manipulation routines
210 /* Read the contents of a section. */
213 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
215 if (coff_section_data (abfd
, sec
) == NULL
)
217 size_t amt
= sizeof (struct coff_section_tdata
);
219 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
220 if (sec
->used_by_bfd
== NULL
)
224 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
228 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
233 coff_section_data (abfd
, sec
)->contents
= contents
;
239 /* Get the size required to hold the dynamic symbols. */
242 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
246 struct internal_ldhdr ldhdr
;
248 if ((abfd
->flags
& DYNAMIC
) == 0)
250 bfd_set_error (bfd_error_invalid_operation
);
254 lsec
= bfd_get_section_by_name (abfd
, ".loader");
257 bfd_set_error (bfd_error_no_symbols
);
261 if (! xcoff_get_section_contents (abfd
, lsec
))
263 contents
= coff_section_data (abfd
, lsec
)->contents
;
265 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
267 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
270 /* Get the dynamic symbols. */
273 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
277 struct internal_ldhdr ldhdr
;
279 bfd_byte
*elsym
, *elsymend
;
280 coff_symbol_type
*symbuf
;
282 if ((abfd
->flags
& DYNAMIC
) == 0)
284 bfd_set_error (bfd_error_invalid_operation
);
288 lsec
= bfd_get_section_by_name (abfd
, ".loader");
291 bfd_set_error (bfd_error_no_symbols
);
295 if (! xcoff_get_section_contents (abfd
, lsec
))
297 contents
= coff_section_data (abfd
, lsec
)->contents
;
299 coff_section_data (abfd
, lsec
)->keep_contents
= TRUE
;
301 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
303 strings
= (char *) contents
+ ldhdr
.l_stoff
;
305 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
309 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
311 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
312 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
314 struct internal_ldsym ldsym
;
316 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
318 symbuf
->symbol
.the_bfd
= abfd
;
320 if (ldsym
._l
._l_l
._l_zeroes
== 0)
321 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
326 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
329 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
331 symbuf
->symbol
.name
= c
;
334 if (ldsym
.l_smclas
== XMC_XO
)
335 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
337 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
339 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
341 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
342 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
344 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
345 symbuf
->symbol
.flags
|= BSF_WEAK
;
347 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
350 /* FIXME: We have no way to record the other information stored
351 with the loader symbol. */
352 *psyms
= (asymbol
*) symbuf
;
357 return ldhdr
.l_nsyms
;
360 /* Get the size required to hold the dynamic relocs. */
363 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
367 struct internal_ldhdr ldhdr
;
369 if ((abfd
->flags
& DYNAMIC
) == 0)
371 bfd_set_error (bfd_error_invalid_operation
);
375 lsec
= bfd_get_section_by_name (abfd
, ".loader");
378 bfd_set_error (bfd_error_no_symbols
);
382 if (! xcoff_get_section_contents (abfd
, lsec
))
384 contents
= coff_section_data (abfd
, lsec
)->contents
;
386 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
388 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
391 /* Get the dynamic relocs. */
394 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
400 struct internal_ldhdr ldhdr
;
402 bfd_byte
*elrel
, *elrelend
;
404 if ((abfd
->flags
& DYNAMIC
) == 0)
406 bfd_set_error (bfd_error_invalid_operation
);
410 lsec
= bfd_get_section_by_name (abfd
, ".loader");
413 bfd_set_error (bfd_error_no_symbols
);
417 if (! xcoff_get_section_contents (abfd
, lsec
))
419 contents
= coff_section_data (abfd
, lsec
)->contents
;
421 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
423 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
427 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
429 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
430 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
433 struct internal_ldrel ldrel
;
435 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
437 if (ldrel
.l_symndx
>= 3)
438 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
444 switch (ldrel
.l_symndx
)
460 sec
= bfd_get_section_by_name (abfd
, name
);
463 bfd_set_error (bfd_error_bad_value
);
467 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
470 relbuf
->address
= ldrel
.l_vaddr
;
473 /* Most dynamic relocs have the same type. FIXME: This is only
474 correct if ldrel.l_rtype == 0. In other cases, we should use
475 a different howto. */
476 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
478 /* FIXME: We have no way to record the l_rsecnm field. */
485 return ldhdr
.l_nreloc
;
488 /* Hash functions for xcoff_link_hash_table's archive_info. */
491 xcoff_archive_info_hash (const void *data
)
493 const struct xcoff_archive_info
*info
;
495 info
= (const struct xcoff_archive_info
*) data
;
496 return htab_hash_pointer (info
->archive
);
500 xcoff_archive_info_eq (const void *data1
, const void *data2
)
502 const struct xcoff_archive_info
*info1
;
503 const struct xcoff_archive_info
*info2
;
505 info1
= (const struct xcoff_archive_info
*) data1
;
506 info2
= (const struct xcoff_archive_info
*) data2
;
507 return info1
->archive
== info2
->archive
;
510 /* Return information about archive ARCHIVE. Return NULL on error. */
512 static struct xcoff_archive_info
*
513 xcoff_get_archive_info (struct bfd_link_info
*info
, bfd
*archive
)
515 struct xcoff_link_hash_table
*htab
;
516 struct xcoff_archive_info
*entryp
, entry
;
519 htab
= xcoff_hash_table (info
);
520 entry
.archive
= archive
;
521 slot
= htab_find_slot (htab
->archive_info
, &entry
, INSERT
);
528 entryp
= bfd_zalloc (info
->output_bfd
, sizeof (entry
));
532 entryp
->archive
= archive
;
538 /* Routine to create an entry in an XCOFF link hash table. */
540 static struct bfd_hash_entry
*
541 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
542 struct bfd_hash_table
*table
,
545 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
547 /* Allocate the structure if it has not already been allocated by a
550 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
554 /* Call the allocation method of the superclass. */
555 ret
= ((struct xcoff_link_hash_entry
*)
556 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
560 /* Set local fields. */
562 ret
->toc_section
= NULL
;
563 ret
->u
.toc_indx
= -1;
564 ret
->descriptor
= NULL
;
568 ret
->smclas
= XMC_UA
;
571 return (struct bfd_hash_entry
*) ret
;
574 /* Destroy an XCOFF link hash table. */
577 _bfd_xcoff_bfd_link_hash_table_free (bfd
*obfd
)
579 struct xcoff_link_hash_table
*ret
;
581 ret
= (struct xcoff_link_hash_table
*) obfd
->link
.hash
;
582 if (ret
->archive_info
)
583 htab_delete (ret
->archive_info
);
584 if (ret
->debug_strtab
)
585 _bfd_stringtab_free (ret
->debug_strtab
);
586 _bfd_generic_link_hash_table_free (obfd
);
589 /* Create an XCOFF link hash table. */
591 struct bfd_link_hash_table
*
592 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
594 struct xcoff_link_hash_table
*ret
;
595 size_t amt
= sizeof (* ret
);
597 ret
= bfd_zmalloc (amt
);
600 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
601 sizeof (struct xcoff_link_hash_entry
)))
607 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
608 ret
->archive_info
= htab_create (37, xcoff_archive_info_hash
,
609 xcoff_archive_info_eq
, NULL
);
610 if (!ret
->debug_strtab
|| !ret
->archive_info
)
612 _bfd_xcoff_bfd_link_hash_table_free (abfd
);
615 ret
->root
.hash_table_free
= _bfd_xcoff_bfd_link_hash_table_free
;
617 /* The linker will always generate a full a.out header. We need to
618 record that fact now, before the sizeof_headers routine could be
620 xcoff_data (abfd
)->full_aouthdr
= TRUE
;
625 /* Read internal relocs for an XCOFF csect. This is a wrapper around
626 _bfd_coff_read_internal_relocs which tries to take advantage of any
627 relocs which may have been cached for the enclosing section. */
629 static struct internal_reloc
*
630 xcoff_read_internal_relocs (bfd
*abfd
,
633 bfd_byte
*external_relocs
,
634 bfd_boolean require_internal
,
635 struct internal_reloc
*internal_relocs
)
637 if (coff_section_data (abfd
, sec
) != NULL
638 && coff_section_data (abfd
, sec
)->relocs
== NULL
639 && xcoff_section_data (abfd
, sec
) != NULL
)
643 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
645 if (enclosing
!= NULL
646 && (coff_section_data (abfd
, enclosing
) == NULL
647 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
649 && enclosing
->reloc_count
> 0)
651 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, TRUE
,
652 external_relocs
, FALSE
, NULL
)
657 if (enclosing
!= NULL
658 && coff_section_data (abfd
, enclosing
) != NULL
659 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
663 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
664 / bfd_coff_relsz (abfd
));
666 if (! require_internal
)
667 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
668 memcpy (internal_relocs
,
669 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
670 sec
->reloc_count
* sizeof (struct internal_reloc
));
671 return internal_relocs
;
675 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
676 require_internal
, internal_relocs
);
679 /* Split FILENAME into an import path and an import filename,
680 storing them in *IMPPATH and *IMPFILE respectively. */
683 bfd_xcoff_split_import_path (bfd
*abfd
, const char *filename
,
684 const char **imppath
, const char **impfile
)
690 base
= lbasename (filename
);
691 length
= base
- filename
;
693 /* The filename has no directory component, so use an empty path. */
695 else if (length
== 1)
696 /* The filename is in the root directory. */
700 /* Extract the (non-empty) directory part. Note that we don't
701 need to strip duplicate directory separators from any part
702 of the string; the native linker doesn't do that either. */
703 path
= bfd_alloc (abfd
, length
);
706 memcpy (path
, filename
, length
- 1);
707 path
[length
- 1] = 0;
714 /* Set ARCHIVE's import path as though its filename had been given
718 bfd_xcoff_set_archive_import_path (struct bfd_link_info
*info
,
719 bfd
*archive
, const char *filename
)
721 struct xcoff_archive_info
*archive_info
;
723 archive_info
= xcoff_get_archive_info (info
, archive
);
724 return (archive_info
!= NULL
725 && bfd_xcoff_split_import_path (archive
, filename
,
726 &archive_info
->imppath
,
727 &archive_info
->impfile
));
730 /* H is an imported symbol. Set the import module's path, file and member
731 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
732 no specific import module is specified. */
735 xcoff_set_import_path (struct bfd_link_info
*info
,
736 struct xcoff_link_hash_entry
*h
,
737 const char *imppath
, const char *impfile
,
738 const char *impmember
)
741 struct xcoff_import_file
**pp
;
743 /* We overload the ldindx field to hold the l_ifile value for this
745 BFD_ASSERT (h
->ldsym
== NULL
);
746 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
751 /* We start c at 1 because the first entry in the import list is
752 reserved for the library search path. */
753 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
755 pp
= &(*pp
)->next
, ++c
)
757 if (filename_cmp ((*pp
)->path
, imppath
) == 0
758 && filename_cmp ((*pp
)->file
, impfile
) == 0
759 && filename_cmp ((*pp
)->member
, impmember
) == 0)
765 struct xcoff_import_file
*n
;
766 size_t amt
= sizeof (*n
);
768 n
= bfd_alloc (info
->output_bfd
, amt
);
774 n
->member
= impmember
;
782 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
783 Return true if LDSYM defines H. */
786 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry
*h
,
787 struct internal_ldsym
*ldsym
)
789 /* If we didn't know about H before processing LDSYM, LDSYM
790 definitely defines H. */
791 if (h
->root
.type
== bfd_link_hash_new
)
794 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
795 dynamic symbol, LDSYM trumps the current definition of H. */
796 if ((ldsym
->l_smtype
& L_WEAK
) == 0
797 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
798 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
799 && (h
->root
.type
== bfd_link_hash_defweak
800 || h
->root
.type
== bfd_link_hash_undefweak
))
803 /* If H is currently undefined, LDSYM defines it. */
804 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
805 && (h
->root
.type
== bfd_link_hash_undefined
806 || h
->root
.type
== bfd_link_hash_undefweak
))
812 /* This function is used to add symbols from a dynamic object to the
813 global symbol table. */
816 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
820 struct internal_ldhdr ldhdr
;
822 bfd_byte
*elsym
, *elsymend
;
823 struct xcoff_import_file
*n
;
825 struct xcoff_import_file
**pp
;
827 /* We can only handle a dynamic object if we are generating an XCOFF
829 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
832 (_("%pB: XCOFF shared object when not producing XCOFF output"),
834 bfd_set_error (bfd_error_invalid_operation
);
838 /* The symbols we use from a dynamic object are not the symbols in
839 the normal symbol table, but, rather, the symbols in the export
840 table. If there is a global symbol in a dynamic object which is
841 not in the export table, the loader will not be able to find it,
842 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
843 libc.a has symbols in the export table which are not in the
846 /* Read in the .loader section. FIXME: We should really use the
847 o_snloader field in the a.out header, rather than grabbing the
849 lsec
= bfd_get_section_by_name (abfd
, ".loader");
853 (_("%pB: dynamic object with no .loader section"),
855 bfd_set_error (bfd_error_no_symbols
);
859 if (! xcoff_get_section_contents (abfd
, lsec
))
861 contents
= coff_section_data (abfd
, lsec
)->contents
;
863 /* Remove the sections from this object, so that they do not get
864 included in the link. */
865 bfd_section_list_clear (abfd
);
867 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
869 strings
= (char *) contents
+ ldhdr
.l_stoff
;
871 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
873 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
875 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
877 struct internal_ldsym ldsym
;
878 char nambuf
[SYMNMLEN
+ 1];
880 struct xcoff_link_hash_entry
*h
;
882 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
884 /* We are only interested in exported symbols. */
885 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
888 if (ldsym
._l
._l_l
._l_zeroes
== 0)
889 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
892 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
893 nambuf
[SYMNMLEN
] = '\0';
897 /* Normally we could not call xcoff_link_hash_lookup in an add
898 symbols routine, since we might not be using an XCOFF hash
899 table. However, we verified above that we are using an XCOFF
902 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
,
907 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
910 h
->flags
|= XCOFF_DEF_DYNAMIC
;
911 h
->smclas
= ldsym
.l_smclas
;
912 if (h
->smclas
== XMC_XO
)
914 /* This symbol has an absolute value. */
915 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
916 h
->root
.type
= bfd_link_hash_defweak
;
918 h
->root
.type
= bfd_link_hash_defined
;
919 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
920 h
->root
.u
.def
.value
= ldsym
.l_value
;
924 /* Otherwise, we don't bother to actually define the symbol,
925 since we don't have a section to put it in anyhow.
926 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
927 should be imported from the symbol's undef.abfd. */
928 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
929 h
->root
.type
= bfd_link_hash_undefweak
;
931 h
->root
.type
= bfd_link_hash_undefined
;
932 h
->root
.u
.undef
.abfd
= abfd
;
935 /* If this symbol defines a function descriptor, then it
936 implicitly defines the function code as well. */
937 if (h
->smclas
== XMC_DS
938 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
939 h
->flags
|= XCOFF_DESCRIPTOR
;
940 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
942 struct xcoff_link_hash_entry
*hds
;
949 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
953 strcpy (dsnm
+ 1, name
);
954 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
964 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
966 hds
->root
.type
= h
->root
.type
;
967 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
968 if (h
->smclas
== XMC_XO
)
970 /* An absolute symbol appears to actually define code, not a
971 function descriptor. This is how some math functions are
972 implemented on AIX 4.1. */
973 hds
->smclas
= XMC_XO
;
974 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
975 hds
->root
.u
.def
.value
= ldsym
.l_value
;
979 hds
->smclas
= XMC_PR
;
980 hds
->root
.u
.undef
.abfd
= abfd
;
981 /* We do not want to add this to the undefined
988 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
990 free (coff_section_data (abfd
, lsec
)->contents
);
991 coff_section_data (abfd
, lsec
)->contents
= NULL
;
994 /* Record this file in the import files. */
995 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
1000 if (abfd
->my_archive
== NULL
|| bfd_is_thin_archive (abfd
->my_archive
))
1002 if (!bfd_xcoff_split_import_path (abfd
, bfd_get_filename (abfd
),
1003 &n
->path
, &n
->file
))
1009 struct xcoff_archive_info
*archive_info
;
1011 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1012 if (!archive_info
->impfile
)
1014 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1015 bfd_get_filename (archive_info
1017 &archive_info
->imppath
,
1018 &archive_info
->impfile
))
1021 n
->path
= archive_info
->imppath
;
1022 n
->file
= archive_info
->impfile
;
1023 n
->member
= bfd_get_filename (abfd
);
1026 /* We start c at 1 because the first import file number is reserved
1028 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1030 pp
= &(*pp
)->next
, ++c
)
1034 xcoff_data (abfd
)->import_file_id
= c
;
1039 /* xcoff_link_create_extra_sections
1041 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1044 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1046 bfd_boolean return_value
= FALSE
;
1048 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1050 /* We need to build a .loader section, so we do it here. This
1051 won't work if we're producing an XCOFF output file with no
1052 XCOFF input files. FIXME. */
1054 if (!bfd_link_relocatable (info
)
1055 && xcoff_hash_table (info
)->loader_section
== NULL
)
1058 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1060 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1064 xcoff_hash_table (info
)->loader_section
= lsec
;
1067 /* Likewise for the linkage section. */
1068 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1071 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1074 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1078 xcoff_hash_table (info
)->linkage_section
= lsec
;
1079 lsec
->alignment_power
= 2;
1082 /* Likewise for the TOC section. */
1083 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1086 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1089 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1093 xcoff_hash_table (info
)->toc_section
= tsec
;
1094 tsec
->alignment_power
= 2;
1097 /* Likewise for the descriptor section. */
1098 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1101 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1104 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1108 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1109 dsec
->alignment_power
= 2;
1112 /* Likewise for the .debug section. */
1113 if (xcoff_hash_table (info
)->debug_section
== NULL
1114 && info
->strip
!= strip_all
)
1117 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1119 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1123 xcoff_hash_table (info
)->debug_section
= dsec
;
1127 return_value
= TRUE
;
1131 return return_value
;
1134 /* Returns the index of reloc in RELOCS with the least address greater
1135 than or equal to ADDRESS. The relocs are sorted by address. */
1137 static bfd_size_type
1138 xcoff_find_reloc (struct internal_reloc
*relocs
,
1139 bfd_size_type count
,
1142 bfd_size_type min
, max
, this;
1146 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1155 /* Do a binary search over (min,max]. */
1156 while (min
+ 1 < max
)
1160 this = (max
+ min
) / 2;
1161 raddr
= relocs
[this].r_vaddr
;
1162 if (raddr
> address
)
1164 else if (raddr
< address
)
1173 if (relocs
[min
].r_vaddr
< address
)
1177 && relocs
[min
- 1].r_vaddr
== address
)
1183 /* Add all the symbols from an object file to the hash table.
1185 XCOFF is a weird format. A normal XCOFF .o files will have three
1186 COFF sections--.text, .data, and .bss--but each COFF section will
1187 contain many csects. These csects are described in the symbol
1188 table. From the linker's point of view, each csect must be
1189 considered a section in its own right. For example, a TOC entry is
1190 handled as a small XMC_TC csect. The linker must be able to merge
1191 different TOC entries together, which means that it must be able to
1192 extract the XMC_TC csects from the .data section of the input .o
1195 From the point of view of our linker, this is, of course, a hideous
1196 nightmare. We cope by actually creating sections for each csect,
1197 and discarding the original sections. We then have to handle the
1198 relocation entries carefully, since the only way to tell which
1199 csect they belong to is to examine the address. */
1202 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1204 unsigned int n_tmask
;
1205 unsigned int n_btshft
;
1206 bfd_boolean default_copy
;
1207 bfd_size_type symcount
;
1208 struct xcoff_link_hash_entry
**sym_hash
;
1209 asection
**csect_cache
;
1210 unsigned int *lineno_counts
;
1211 bfd_size_type linesz
;
1213 asection
*last_real
;
1214 bfd_boolean keep_syms
;
1216 unsigned int csect_index
;
1217 asection
*first_csect
;
1218 bfd_size_type symesz
;
1221 struct reloc_info_struct
1223 struct internal_reloc
*relocs
;
1226 } *reloc_info
= NULL
;
1229 keep_syms
= obj_coff_keep_syms (abfd
);
1231 if ((abfd
->flags
& DYNAMIC
) != 0
1232 && ! info
->static_link
)
1234 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1238 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1239 if (! xcoff_link_create_extra_sections (abfd
, info
))
1242 if ((abfd
->flags
& DYNAMIC
) != 0
1243 && ! info
->static_link
)
1246 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1247 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1249 /* Define macros so that ISFCN, et. al., macros work correctly. */
1250 #define N_TMASK n_tmask
1251 #define N_BTSHFT n_btshft
1253 if (info
->keep_memory
)
1254 default_copy
= FALSE
;
1256 default_copy
= TRUE
;
1258 symcount
= obj_raw_syment_count (abfd
);
1260 /* We keep a list of the linker hash table entries that correspond
1261 to each external symbol. */
1262 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1263 sym_hash
= bfd_zalloc (abfd
, amt
);
1264 if (sym_hash
== NULL
&& symcount
!= 0)
1266 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1268 /* Because of the weird stuff we are doing with XCOFF csects, we can
1269 not easily determine which section a symbol is in, so we store
1270 the information in the tdata for the input file. */
1271 amt
= symcount
* sizeof (asection
*);
1272 csect_cache
= bfd_zalloc (abfd
, amt
);
1273 if (csect_cache
== NULL
&& symcount
!= 0)
1275 xcoff_data (abfd
)->csects
= csect_cache
;
1277 /* We garbage-collect line-number information on a symbol-by-symbol
1278 basis, so we need to have quick access to the number of entries
1280 amt
= symcount
* sizeof (unsigned int);
1281 lineno_counts
= bfd_zalloc (abfd
, amt
);
1282 if (lineno_counts
== NULL
&& symcount
!= 0)
1284 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1286 /* While splitting sections into csects, we need to assign the
1287 relocs correctly. The relocs and the csects must both be in
1288 order by VMA within a given section, so we handle this by
1289 scanning along the relocs as we process the csects. We index
1290 into reloc_info using the section target_index. */
1291 amt
= abfd
->section_count
+ 1;
1292 amt
*= sizeof (struct reloc_info_struct
);
1293 reloc_info
= bfd_zmalloc (amt
);
1294 if (reloc_info
== NULL
)
1297 /* Read in the relocs and line numbers for each section. */
1298 linesz
= bfd_coff_linesz (abfd
);
1300 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1304 if ((o
->flags
& SEC_RELOC
) != 0)
1306 reloc_info
[o
->target_index
].relocs
=
1307 xcoff_read_internal_relocs (abfd
, o
, TRUE
, NULL
, FALSE
, NULL
);
1308 amt
= o
->reloc_count
;
1309 amt
*= sizeof (asection
*);
1310 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1311 if (reloc_info
[o
->target_index
].csects
== NULL
)
1315 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1316 && o
->lineno_count
> 0)
1320 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0)
1322 if (_bfd_mul_overflow (linesz
, o
->lineno_count
, &amt
))
1324 bfd_set_error (bfd_error_file_too_big
);
1327 linenos
= _bfd_malloc_and_read (abfd
, amt
, amt
);
1328 if (linenos
== NULL
)
1330 reloc_info
[o
->target_index
].linenos
= linenos
;
1334 /* Don't let the linker relocation routines discard the symbols. */
1335 obj_coff_keep_syms (abfd
) = TRUE
;
1341 symesz
= bfd_coff_symesz (abfd
);
1342 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1343 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1344 esym_end
= esym
+ symcount
* symesz
;
1346 while (esym
< esym_end
)
1348 struct internal_syment sym
;
1349 union internal_auxent aux
;
1351 char buf
[SYMNMLEN
+ 1];
1355 struct xcoff_link_hash_entry
*set_toc
;
1357 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1359 /* In this pass we are only interested in symbols with csect
1361 if (!CSECT_SYM_P (sym
.n_sclass
))
1364 Normally csect is a .pr, .rw etc. created in the loop
1365 If C_FILE or first time, handle special
1367 Advance esym, sym_hash, csect_hash ptrs. */
1368 if (sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1371 *csect_cache
= csect
;
1372 else if (first_csect
== NULL
1373 || sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1374 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1376 *csect_cache
= NULL
;
1377 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1378 sym_hash
+= sym
.n_numaux
+ 1;
1379 csect_cache
+= sym
.n_numaux
+ 1;
1380 lineno_counts
+= sym
.n_numaux
+ 1;
1385 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1390 /* If this symbol has line number information attached to it,
1391 and we're not stripping it, count the number of entries and
1392 add them to the count for this csect. In the final link pass
1393 we are going to attach line number information by symbol,
1394 rather than by section, in order to more easily handle
1395 garbage collection. */
1396 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1399 && ISFCN (sym
.n_type
))
1401 union internal_auxent auxlin
;
1403 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1404 sym
.n_type
, sym
.n_sclass
,
1405 0, sym
.n_numaux
, (void *) &auxlin
);
1407 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1409 asection
*enclosing
;
1410 bfd_signed_vma linoff
;
1412 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1413 if (enclosing
== NULL
)
1416 /* xgettext:c-format */
1417 (_("%pB: `%s' has line numbers but no enclosing section"),
1419 bfd_set_error (bfd_error_bad_value
);
1422 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1423 - enclosing
->line_filepos
);
1424 /* Explicit cast to bfd_signed_vma for compiler. */
1425 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1427 struct internal_lineno lin
;
1428 bfd_byte
*linpstart
;
1430 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1432 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1434 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1436 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1439 bfd_byte
*linpend
, *linp
;
1441 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1442 + enclosing
->lineno_count
* linesz
);
1443 for (linp
= linpstart
+ linesz
;
1447 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1449 if (lin
.l_lnno
== 0)
1452 *lineno_counts
= (linp
- linpstart
) / linesz
;
1453 /* The setting of line_filepos will only be
1454 useful if all the line number entries for a
1455 csect are contiguous; this only matters for
1457 if (csect
->line_filepos
== 0)
1458 csect
->line_filepos
=
1459 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1465 /* Pick up the csect auxiliary information. */
1466 if (sym
.n_numaux
== 0)
1469 /* xgettext:c-format */
1470 (_("%pB: class %d symbol `%s' has no aux entries"),
1471 abfd
, sym
.n_sclass
, name
);
1472 bfd_set_error (bfd_error_bad_value
);
1476 bfd_coff_swap_aux_in (abfd
,
1477 (void *) (esym
+ symesz
* sym
.n_numaux
),
1478 sym
.n_type
, sym
.n_sclass
,
1479 sym
.n_numaux
- 1, sym
.n_numaux
,
1482 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1492 /* xgettext:c-format */
1493 (_("%pB: symbol `%s' has unrecognized csect type %d"),
1495 bfd_set_error (bfd_error_bad_value
);
1499 /* This is an external reference. */
1500 if (sym
.n_sclass
== C_HIDEXT
1501 || sym
.n_scnum
!= N_UNDEF
1502 || aux
.x_csect
.x_scnlen
.l
!= 0)
1505 /* xgettext:c-format */
1506 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1508 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1509 (int64_t) aux
.x_csect
.x_scnlen
.l
);
1510 bfd_set_error (bfd_error_bad_value
);
1514 /* An XMC_XO external reference is actually a reference to
1515 an absolute location. */
1516 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1517 section
= bfd_und_section_ptr
;
1520 section
= bfd_abs_section_ptr
;
1521 value
= sym
.n_value
;
1527 csect_index
= -(unsigned) 1;
1529 /* When we see a TOC anchor, we record the TOC value. */
1530 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1532 if (sym
.n_sclass
!= C_HIDEXT
1533 || aux
.x_csect
.x_scnlen
.l
!= 0)
1536 /* xgettext:c-format */
1537 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64
),
1538 abfd
, name
, sym
.n_sclass
, (int64_t) aux
.x_csect
.x_scnlen
.l
);
1539 bfd_set_error (bfd_error_bad_value
);
1542 xcoff_data (abfd
)->toc
= sym
.n_value
;
1545 /* We must merge TOC entries for the same symbol. We can
1546 merge two TOC entries if they are both C_HIDEXT, they
1547 both have the same name, they are both 4 or 8 bytes long, and
1548 they both have a relocation table entry for an external
1549 symbol with the same name. Unfortunately, this means
1550 that we must look through the relocations. Ick.
1552 Logic for 32 bit vs 64 bit.
1553 32 bit has a csect length of 4 for TOC
1554 64 bit has a csect length of 8 for TOC
1556 The conditions to get past the if-check are not that bad.
1557 They are what is used to create the TOC csects in the first
1559 if (aux
.x_csect
.x_smclas
== XMC_TC
1560 && sym
.n_sclass
== C_HIDEXT
1561 && info
->output_bfd
->xvec
== abfd
->xvec
1562 && ((bfd_xcoff_is_xcoff32 (abfd
)
1563 && aux
.x_csect
.x_scnlen
.l
== 4)
1564 || (bfd_xcoff_is_xcoff64 (abfd
)
1565 && aux
.x_csect
.x_scnlen
.l
== 8)))
1567 asection
*enclosing
;
1568 struct internal_reloc
*relocs
;
1569 bfd_size_type relindx
;
1570 struct internal_reloc
*rel
;
1572 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1573 if (enclosing
== NULL
)
1576 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1577 amt
= enclosing
->reloc_count
;
1578 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1579 rel
= relocs
+ relindx
;
1581 /* 32 bit R_POS r_size is 31
1582 64 bit R_POS r_size is 63 */
1583 if (relindx
< enclosing
->reloc_count
1584 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1585 && rel
->r_type
== R_POS
1586 && ((bfd_xcoff_is_xcoff32 (abfd
)
1587 && rel
->r_size
== 31)
1588 || (bfd_xcoff_is_xcoff64 (abfd
)
1589 && rel
->r_size
== 63)))
1593 struct internal_syment relsym
;
1595 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1596 + rel
->r_symndx
* symesz
);
1597 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1598 if (EXTERN_SYM_P (relsym
.n_sclass
))
1600 const char *relname
;
1601 char relbuf
[SYMNMLEN
+ 1];
1603 struct xcoff_link_hash_entry
*h
;
1605 /* At this point we know that the TOC entry is
1606 for an externally visible symbol. */
1607 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1609 if (relname
== NULL
)
1612 /* We only merge TOC entries if the TC name is
1613 the same as the symbol name. This handles
1614 the normal case, but not common cases like
1615 SYM.P4 which gcc generates to store SYM + 4
1616 in the TOC. FIXME. */
1617 if (strcmp (name
, relname
) == 0)
1619 copy
= (! info
->keep_memory
1620 || relsym
._n
._n_n
._n_zeroes
!= 0
1621 || relsym
._n
._n_n
._n_offset
== 0);
1622 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1623 relname
, TRUE
, copy
,
1628 /* At this point h->root.type could be
1629 bfd_link_hash_new. That should be OK,
1630 since we know for sure that we will come
1631 across this symbol as we step through the
1634 /* We store h in *sym_hash for the
1635 convenience of the relocate_section
1639 if (h
->toc_section
!= NULL
)
1641 asection
**rel_csects
;
1643 /* We already have a TOC entry for this
1644 symbol, so we can just ignore this
1647 reloc_info
[enclosing
->target_index
].csects
;
1648 rel_csects
[relindx
] = bfd_und_section_ptr
;
1652 /* We are about to create a TOC entry for
1661 asection
*enclosing
;
1663 /* We need to create a new section. We get the name from
1664 the csect storage mapping class, so that the linker can
1665 accumulate similar csects together. */
1667 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1671 /* The enclosing section is the main section : .data, .text
1672 or .bss that the csect is coming from. */
1673 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1674 if (enclosing
== NULL
)
1677 if (! bfd_is_abs_section (enclosing
)
1678 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1679 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1680 > enclosing
->vma
+ enclosing
->size
)))
1683 /* xgettext:c-format */
1684 (_("%pB: csect `%s' not in enclosing section"),
1686 bfd_set_error (bfd_error_bad_value
);
1689 csect
->vma
= sym
.n_value
;
1690 csect
->filepos
= (enclosing
->filepos
1693 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1694 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1695 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1697 /* Record the enclosing section in the tdata for this new
1699 amt
= sizeof (struct coff_section_tdata
);
1700 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1701 if (csect
->used_by_bfd
== NULL
)
1703 amt
= sizeof (struct xcoff_section_tdata
);
1704 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1705 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1707 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1708 xcoff_section_data (abfd
, csect
)->lineno_count
=
1709 enclosing
->lineno_count
;
1711 if (enclosing
->owner
== abfd
)
1713 struct internal_reloc
*relocs
;
1714 bfd_size_type relindx
;
1715 struct internal_reloc
*rel
;
1716 asection
**rel_csect
;
1718 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1719 amt
= enclosing
->reloc_count
;
1720 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1722 rel
= relocs
+ relindx
;
1723 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1726 csect
->rel_filepos
= (enclosing
->rel_filepos
1727 + relindx
* bfd_coff_relsz (abfd
));
1728 while (relindx
< enclosing
->reloc_count
1729 && *rel_csect
== NULL
1730 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1734 csect
->flags
|= SEC_RELOC
;
1735 ++csect
->reloc_count
;
1742 /* There are a number of other fields and section flags
1743 which we do not bother to set. */
1745 csect_index
= ((esym
1746 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1749 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1751 if (first_csect
== NULL
)
1752 first_csect
= csect
;
1754 /* If this symbol is external, we treat it as starting at the
1755 beginning of the newly created section. */
1756 if (EXTERN_SYM_P (sym
.n_sclass
))
1762 /* If this is a TOC section for a symbol, record it. */
1763 if (set_toc
!= NULL
)
1764 set_toc
->toc_section
= csect
;
1769 /* This is a label definition. The x_scnlen field is the
1770 symbol index of the csect. Usually the XTY_LD symbol will
1771 follow its appropriate XTY_SD symbol. The .set pseudo op can
1772 cause the XTY_LD to not follow the XTY_SD symbol. */
1777 if (aux
.x_csect
.x_scnlen
.l
< 0
1778 || (aux
.x_csect
.x_scnlen
.l
1779 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1783 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1785 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1791 /* xgettext:c-format */
1792 (_("%pB: misplaced XTY_LD `%s'"),
1794 bfd_set_error (bfd_error_bad_value
);
1798 value
= sym
.n_value
- csect
->vma
;
1803 /* This is an unitialized csect. We could base the name on
1804 the storage mapping class, but we don't bother except for
1805 an XMC_TD symbol. If this csect is externally visible,
1806 it is a common symbol. We put XMC_TD symbols in sections
1807 named .tocbss, and rely on the linker script to put that
1810 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1812 /* The linker script puts the .td section in the data
1813 section after the .tc section. */
1814 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1817 else if (aux
.x_csect
.x_smclas
== XMC_UL
)
1819 /* This is a thread-local unitialized csect. */
1820 csect
= bfd_make_section_anyway_with_flags (abfd
, ".tbss",
1821 SEC_ALLOC
| SEC_THREAD_LOCAL
);
1824 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1829 csect
->vma
= sym
.n_value
;
1830 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1831 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1832 /* There are a number of other fields and section flags
1833 which we do not bother to set. */
1835 csect_index
= ((esym
1836 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1839 amt
= sizeof (struct coff_section_tdata
);
1840 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1841 if (csect
->used_by_bfd
== NULL
)
1843 amt
= sizeof (struct xcoff_section_tdata
);
1844 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1845 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1847 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1849 if (first_csect
== NULL
)
1850 first_csect
= csect
;
1852 if (EXTERN_SYM_P (sym
.n_sclass
))
1854 csect
->flags
|= SEC_IS_COMMON
;
1857 value
= aux
.x_csect
.x_scnlen
.l
;
1863 /* Check for magic symbol names. */
1864 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1865 && aux
.x_csect
.x_smclas
!= XMC_TC
1866 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1872 if (strcmp (name
, "_text") == 0)
1873 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1874 else if (strcmp (name
, "_etext") == 0)
1875 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1876 else if (strcmp (name
, "_data") == 0)
1877 i
= XCOFF_SPECIAL_SECTION_DATA
;
1878 else if (strcmp (name
, "_edata") == 0)
1879 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1880 else if (strcmp (name
, "_end") == 0)
1881 i
= XCOFF_SPECIAL_SECTION_END
;
1883 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1884 i
= XCOFF_SPECIAL_SECTION_END2
;
1887 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1890 /* Now we have enough information to add the symbol to the
1891 linker hash table. */
1893 if (EXTERN_SYM_P (sym
.n_sclass
))
1895 bfd_boolean copy
, ok
;
1898 BFD_ASSERT (section
!= NULL
);
1900 /* We must copy the name into memory if we got it from the
1901 syment itself, rather than the string table. */
1902 copy
= default_copy
;
1903 if (sym
._n
._n_n
._n_zeroes
!= 0
1904 || sym
._n
._n_n
._n_offset
== 0)
1907 /* Ignore global linkage code when linking statically. */
1908 if (info
->static_link
1909 && (smtyp
== XTY_SD
|| smtyp
== XTY_LD
)
1910 && aux
.x_csect
.x_smclas
== XMC_GL
)
1912 section
= bfd_und_section_ptr
;
1916 /* The AIX linker appears to only detect multiple symbol
1917 definitions when there is a reference to the symbol. If
1918 a symbol is defined multiple times, and the only
1919 references are from the same object file, the AIX linker
1920 appears to permit it. It does not merge the different
1921 definitions, but handles them independently. On the
1922 other hand, if there is a reference, the linker reports
1925 This matters because the AIX <net/net_globals.h> header
1926 file actually defines an initialized array, so we have to
1927 actually permit that to work.
1929 Just to make matters even more confusing, the AIX linker
1930 appears to permit multiple symbol definitions whenever
1931 the second definition is in an archive rather than an
1932 object file. This may be a consequence of the manner in
1933 which it handles archives: I think it may load the entire
1934 archive in as separate csects, and then let garbage
1935 collection discard symbols.
1937 We also have to handle the case of statically linking a
1938 shared object, which will cause symbol redefinitions,
1939 although this is an easier case to detect. */
1940 else if (info
->output_bfd
->xvec
== abfd
->xvec
)
1942 if (! bfd_is_und_section (section
))
1943 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1944 name
, TRUE
, copy
, FALSE
);
1946 /* Make a copy of the symbol name to prevent problems with
1948 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1949 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1950 TRUE
, TRUE
, FALSE
));
1952 if (*sym_hash
== NULL
)
1954 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1955 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1956 && ! bfd_is_und_section (section
)
1957 && ! bfd_is_com_section (section
))
1959 /* This is a second definition of a defined symbol. */
1960 if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
1961 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
1963 /* The existing symbol is from a shared library.
1965 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1966 (*sym_hash
)->root
.u
.undef
.abfd
=
1967 (*sym_hash
)->root
.u
.def
.section
->owner
;
1969 else if (abfd
->my_archive
!= NULL
)
1971 /* This is a redefinition in an object contained
1972 in an archive. Just ignore it. See the
1974 section
= bfd_und_section_ptr
;
1977 else if (sym
.n_sclass
== C_AIX_WEAKEXT
1978 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1980 /* At least one of the definitions is weak.
1981 Allow the normal rules to take effect. */
1983 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1984 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1986 /* This symbol has been referenced. In this
1987 case, we just continue and permit the
1988 multiple definition error. See the comment
1989 above about the behaviour of the AIX linker. */
1991 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1993 /* The symbols are both csects of the same
1994 class. There is at least a chance that this
1995 is a semi-legitimate redefinition. */
1996 section
= bfd_und_section_ptr
;
1998 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
2001 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
2002 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
2003 && (bfd_is_und_section (section
)
2004 || bfd_is_com_section (section
)))
2006 /* This is a reference to a multiply defined symbol.
2007 Report the error now. See the comment above
2008 about the behaviour of the AIX linker. We could
2009 also do this with warning symbols, but I'm not
2010 sure the XCOFF linker is wholly prepared to
2011 handle them, and that would only be a warning,
2013 (*info
->callbacks
->multiple_definition
) (info
,
2017 /* Try not to give this error too many times. */
2018 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2022 /* _bfd_generic_link_add_one_symbol may call the linker to
2023 generate an error message, and the linker may try to read
2024 the symbol table to give a good error. Right now, the
2025 line numbers are in an inconsistent state, since they are
2026 counted both in the real sections and in the new csects.
2027 We need to leave the count in the real sections so that
2028 the linker can report the line number of the error
2029 correctly, so temporarily clobber the link to the csects
2030 so that the linker will not try to read the line numbers
2031 a second time from the csects. */
2032 BFD_ASSERT (last_real
->next
== first_csect
);
2033 last_real
->next
= NULL
;
2034 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2035 ok
= (_bfd_generic_link_add_one_symbol
2036 (info
, abfd
, name
, flags
, section
, value
, NULL
, copy
, TRUE
,
2037 (struct bfd_link_hash_entry
**) sym_hash
));
2038 last_real
->next
= first_csect
;
2042 if (smtyp
== XTY_CM
)
2044 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2045 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2046 /* We don't need the common csect we just created. */
2049 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2050 = csect
->alignment_power
;
2053 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2059 || section
== bfd_und_section_ptr
)
2060 flag
= XCOFF_REF_REGULAR
;
2062 flag
= XCOFF_DEF_REGULAR
;
2063 (*sym_hash
)->flags
|= flag
;
2065 if ((*sym_hash
)->smclas
== XMC_UA
2066 || flag
== XCOFF_DEF_REGULAR
)
2067 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2071 if (smtyp
== XTY_ER
)
2072 *csect_cache
= section
;
2075 *csect_cache
= csect
;
2077 xcoff_section_data (abfd
, csect
)->last_symndx
2078 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2081 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2082 sym_hash
+= sym
.n_numaux
+ 1;
2083 csect_cache
+= sym
.n_numaux
+ 1;
2084 lineno_counts
+= sym
.n_numaux
+ 1;
2087 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2089 /* Make sure that we have seen all the relocs. */
2090 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2092 /* Debugging sections have no csects. */
2093 if (bfd_section_flags (o
) & SEC_DEBUGGING
)
2096 /* Reset the section size and the line number count, since the
2097 data is now attached to the csects. Don't reset the size of
2098 the .debug section, since we need to read it below in
2099 bfd_xcoff_size_dynamic_sections. */
2100 if (strcmp (bfd_section_name (o
), ".debug") != 0)
2102 o
->lineno_count
= 0;
2104 if ((o
->flags
& SEC_RELOC
) != 0)
2107 struct internal_reloc
*rel
;
2108 asection
**rel_csect
;
2110 rel
= reloc_info
[o
->target_index
].relocs
;
2111 rel_csect
= reloc_info
[o
->target_index
].csects
;
2113 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2115 if (*rel_csect
== NULL
)
2118 /* xgettext:c-format */
2119 (_("%pB: reloc %s:%" PRId64
" not in csect"),
2120 abfd
, o
->name
, (int64_t) i
);
2121 bfd_set_error (bfd_error_bad_value
);
2125 /* We identify all function symbols that are the target
2126 of a relocation, so that we can create glue code for
2127 functions imported from dynamic objects. */
2128 if (info
->output_bfd
->xvec
== abfd
->xvec
2129 && *rel_csect
!= bfd_und_section_ptr
2130 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2132 struct xcoff_link_hash_entry
*h
;
2134 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2135 /* If the symbol name starts with a period, it is
2136 the code of a function. If the symbol is
2137 currently undefined, then add an undefined symbol
2138 for the function descriptor. This should do no
2139 harm, because any regular object that defines the
2140 function should also define the function
2141 descriptor. It helps, because it means that we
2142 will identify the function descriptor with a
2143 dynamic object if a dynamic object defines it. */
2144 if (h
->root
.root
.string
[0] == '.'
2145 && h
->descriptor
== NULL
)
2147 struct xcoff_link_hash_entry
*hds
;
2148 struct bfd_link_hash_entry
*bh
;
2150 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2151 h
->root
.root
.string
+ 1,
2155 if (hds
->root
.type
== bfd_link_hash_new
)
2158 if (! (_bfd_generic_link_add_one_symbol
2159 (info
, abfd
, hds
->root
.root
.string
,
2160 (flagword
) 0, bfd_und_section_ptr
,
2161 (bfd_vma
) 0, NULL
, FALSE
,
2164 hds
= (struct xcoff_link_hash_entry
*) bh
;
2166 hds
->flags
|= XCOFF_DESCRIPTOR
;
2167 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2168 hds
->descriptor
= h
;
2169 h
->descriptor
= hds
;
2171 if (h
->root
.root
.string
[0] == '.')
2172 h
->flags
|= XCOFF_CALLED
;
2176 free (reloc_info
[o
->target_index
].csects
);
2177 reloc_info
[o
->target_index
].csects
= NULL
;
2179 /* Reset SEC_RELOC and the reloc_count, since the reloc
2180 information is now attached to the csects. */
2181 o
->flags
&=~ SEC_RELOC
;
2184 /* If we are not keeping memory, free the reloc information. */
2185 if (! info
->keep_memory
2186 && coff_section_data (abfd
, o
) != NULL
2187 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2189 free (coff_section_data (abfd
, o
)->relocs
);
2190 coff_section_data (abfd
, o
)->relocs
= NULL
;
2194 /* Free up the line numbers. FIXME: We could cache these
2195 somewhere for the final link, to avoid reading them again. */
2196 free (reloc_info
[o
->target_index
].linenos
);
2197 reloc_info
[o
->target_index
].linenos
= NULL
;
2202 obj_coff_keep_syms (abfd
) = keep_syms
;
2207 if (reloc_info
!= NULL
)
2209 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2211 free (reloc_info
[o
->target_index
].csects
);
2212 free (reloc_info
[o
->target_index
].linenos
);
2216 obj_coff_keep_syms (abfd
) = keep_syms
;
2223 /* Add symbols from an XCOFF object file. */
2226 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2228 if (! _bfd_coff_get_external_symbols (abfd
))
2230 if (! xcoff_link_add_symbols (abfd
, info
))
2232 if (! info
->keep_memory
)
2234 if (! _bfd_coff_free_symbols (abfd
))
2240 /* Look through the loader symbols to see if this dynamic object
2241 should be included in the link. The native linker uses the loader
2242 symbols, not the normal symbol table, so we do too. */
2245 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2246 struct bfd_link_info
*info
,
2247 bfd_boolean
*pneeded
,
2252 struct internal_ldhdr ldhdr
;
2253 const char *strings
;
2254 bfd_byte
*elsym
, *elsymend
;
2258 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2260 /* There are no symbols, so don't try to include it. */
2263 if (! xcoff_get_section_contents (abfd
, lsec
))
2265 contents
= coff_section_data (abfd
, lsec
)->contents
;
2267 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2269 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2271 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2273 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2274 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2276 struct internal_ldsym ldsym
;
2277 char nambuf
[SYMNMLEN
+ 1];
2279 struct bfd_link_hash_entry
*h
;
2281 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2283 /* We are only interested in exported symbols. */
2284 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2287 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2288 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2291 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2292 nambuf
[SYMNMLEN
] = '\0';
2296 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2298 /* We are only interested in symbols that are currently
2299 undefined. At this point we know that we are using an XCOFF
2302 && h
->type
== bfd_link_hash_undefined
2303 && (((struct xcoff_link_hash_entry
*) h
)->flags
2304 & XCOFF_DEF_DYNAMIC
) == 0)
2306 if (!(*info
->callbacks
2307 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2314 /* We do not need this shared object. */
2315 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2317 free (coff_section_data (abfd
, lsec
)->contents
);
2318 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2324 /* Look through the symbols to see if this object file should be
2325 included in the link. */
2328 xcoff_link_check_ar_symbols (bfd
*abfd
,
2329 struct bfd_link_info
*info
,
2330 bfd_boolean
*pneeded
,
2333 bfd_size_type symesz
;
2339 if ((abfd
->flags
& DYNAMIC
) != 0
2340 && ! info
->static_link
2341 && info
->output_bfd
->xvec
== abfd
->xvec
)
2342 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
, subsbfd
);
2344 symesz
= bfd_coff_symesz (abfd
);
2345 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2346 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2347 while (esym
< esym_end
)
2349 struct internal_syment sym
;
2351 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2353 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2356 char buf
[SYMNMLEN
+ 1];
2357 struct bfd_link_hash_entry
*h
;
2359 /* This symbol is externally visible, and is defined by this
2361 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2365 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2367 /* We are only interested in symbols that are currently
2368 undefined. If a symbol is currently known to be common,
2369 XCOFF linkers do not bring in an object file which
2370 defines it. We also don't bring in symbols to satisfy
2371 undefined references in shared objects. */
2373 && h
->type
== bfd_link_hash_undefined
2374 && (info
->output_bfd
->xvec
!= abfd
->xvec
2375 || (((struct xcoff_link_hash_entry
*) h
)->flags
2376 & XCOFF_DEF_DYNAMIC
) == 0))
2378 if (!(*info
->callbacks
2379 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2386 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2389 /* We do not need this object file. */
2393 /* Check a single archive element to see if we need to include it in
2394 the link. *PNEEDED is set according to whether this element is
2395 needed in the link or not. This is called via
2396 _bfd_generic_link_add_archive_symbols. */
2399 xcoff_link_check_archive_element (bfd
*abfd
,
2400 struct bfd_link_info
*info
,
2401 struct bfd_link_hash_entry
*h ATTRIBUTE_UNUSED
,
2402 const char *name ATTRIBUTE_UNUSED
,
2403 bfd_boolean
*pneeded
)
2405 bfd_boolean keep_syms_p
;
2408 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2409 if (!_bfd_coff_get_external_symbols (abfd
))
2413 if (!xcoff_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
2418 /* Potentially, the add_archive_element hook may have set a
2419 substitute BFD for us. */
2423 && !_bfd_coff_free_symbols (oldbfd
))
2425 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2426 if (!_bfd_coff_get_external_symbols (abfd
))
2429 if (!xcoff_link_add_symbols (abfd
, info
))
2431 if (info
->keep_memory
)
2437 if (!_bfd_coff_free_symbols (abfd
))
2444 /* Given an XCOFF BFD, add symbols to the global hash table as
2448 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2450 switch (bfd_get_format (abfd
))
2453 return xcoff_link_add_object_symbols (abfd
, info
);
2456 /* If the archive has a map, do the usual search. We then need
2457 to check the archive for dynamic objects, because they may not
2458 appear in the archive map even though they should, perhaps, be
2459 included. If the archive has no map, we just consider each object
2460 file in turn, since that apparently is what the AIX native linker
2462 if (bfd_has_map (abfd
))
2464 if (! (_bfd_generic_link_add_archive_symbols
2465 (abfd
, info
, xcoff_link_check_archive_element
)))
2472 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2473 while (member
!= NULL
)
2475 if (bfd_check_format (member
, bfd_object
)
2476 && (info
->output_bfd
->xvec
== member
->xvec
)
2477 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2481 if (! xcoff_link_check_archive_element (member
, info
,
2482 NULL
, NULL
, &needed
))
2485 member
->archive_pass
= -1;
2487 member
= bfd_openr_next_archived_file (abfd
, member
);
2494 bfd_set_error (bfd_error_wrong_format
);
2500 _bfd_xcoff_define_common_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2501 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2502 struct bfd_link_hash_entry
*harg
)
2504 struct xcoff_link_hash_entry
*h
;
2506 if (!bfd_generic_define_common_symbol (output_bfd
, info
, harg
))
2509 h
= (struct xcoff_link_hash_entry
*) harg
;
2510 h
->flags
|= XCOFF_DEF_REGULAR
;
2514 /* If symbol H has not been interpreted as a function descriptor,
2515 see whether it should be. Set up its descriptor information if so. */
2518 xcoff_find_function (struct bfd_link_info
*info
,
2519 struct xcoff_link_hash_entry
*h
)
2521 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2522 && h
->root
.root
.string
[0] != '.')
2525 struct xcoff_link_hash_entry
*hfn
;
2528 amt
= strlen (h
->root
.root
.string
) + 2;
2529 fnname
= bfd_malloc (amt
);
2533 strcpy (fnname
+ 1, h
->root
.root
.string
);
2534 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2535 fnname
, FALSE
, FALSE
, TRUE
);
2538 && hfn
->smclas
== XMC_PR
2539 && (hfn
->root
.type
== bfd_link_hash_defined
2540 || hfn
->root
.type
== bfd_link_hash_defweak
))
2542 h
->flags
|= XCOFF_DESCRIPTOR
;
2543 h
->descriptor
= hfn
;
2544 hfn
->descriptor
= h
;
2550 /* Return true if the given bfd contains at least one shared object. */
2553 xcoff_archive_contains_shared_object_p (struct bfd_link_info
*info
,
2556 struct xcoff_archive_info
*archive_info
;
2559 archive_info
= xcoff_get_archive_info (info
, archive
);
2560 if (!archive_info
->know_contains_shared_object_p
)
2562 member
= bfd_openr_next_archived_file (archive
, NULL
);
2563 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2564 member
= bfd_openr_next_archived_file (archive
, member
);
2566 archive_info
->contains_shared_object_p
= (member
!= NULL
);
2567 archive_info
->know_contains_shared_object_p
= 1;
2569 return archive_info
->contains_shared_object_p
;
2572 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2573 qualifies for export by -bexpall. */
2576 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2578 /* Exclude symbols beginning with '_'. */
2579 if (h
->root
.root
.string
[0] == '_')
2582 /* Exclude archive members that would otherwise be unreferenced. */
2583 if ((h
->flags
& XCOFF_MARK
) == 0
2584 && (h
->root
.type
== bfd_link_hash_defined
2585 || h
->root
.type
== bfd_link_hash_defweak
)
2586 && h
->root
.u
.def
.section
->owner
!= NULL
2587 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2593 /* Return true if symbol H qualifies for the forms of automatic export
2594 specified by AUTO_EXPORT_FLAGS. */
2597 xcoff_auto_export_p (struct bfd_link_info
*info
,
2598 struct xcoff_link_hash_entry
*h
,
2599 unsigned int auto_export_flags
)
2601 /* Don't automatically export things that were explicitly exported. */
2602 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2605 /* Don't export things that we don't define. */
2606 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2609 /* Don't export functions; export their descriptors instead. */
2610 if (h
->root
.root
.string
[0] == '.')
2613 /* We don't export a symbol which is being defined by an object
2614 included from an archive which contains a shared object. The
2615 rationale is that if an archive contains both an unshared and
2616 a shared object, then there must be some reason that the
2617 unshared object is unshared, and we don't want to start
2618 providing a shared version of it. In particular, this solves
2619 a bug involving the _savefNN set of functions. gcc will call
2620 those functions without providing a slot to restore the TOC,
2621 so it is essential that these functions be linked in directly
2622 and not from a shared object, which means that a shared
2623 object which also happens to link them in must not export
2624 them. This is confusing, but I haven't been able to think of
2625 a different approach. Note that the symbols can, of course,
2626 be exported explicitly. */
2627 if (h
->root
.type
== bfd_link_hash_defined
2628 || h
->root
.type
== bfd_link_hash_defweak
)
2632 owner
= h
->root
.u
.def
.section
->owner
;
2634 && owner
->my_archive
!= NULL
2635 && xcoff_archive_contains_shared_object_p (info
, owner
->my_archive
))
2639 /* Otherwise, all symbols are exported by -bexpfull. */
2640 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2643 /* Despite its name, -bexpall exports most but not all symbols. */
2644 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2645 && xcoff_covered_by_expall_p (h
))
2651 /* Return true if relocation REL needs to be copied to the .loader section.
2652 If REL is against a global symbol, H is that symbol, otherwise it
2656 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2657 struct xcoff_link_hash_entry
*h
)
2659 if (!xcoff_hash_table (info
)->loader_section
)
2662 switch (rel
->r_type
)
2669 /* We should never need a .loader reloc for a TOC-relative reloc. */
2673 /* In this case, relocations against defined symbols can be resolved
2676 || h
->root
.type
== bfd_link_hash_defined
2677 || h
->root
.type
== bfd_link_hash_defweak
2678 || h
->root
.type
== bfd_link_hash_common
)
2681 /* We will always provide a local definition of function symbols,
2682 even if we don't have one yet. */
2683 if ((h
->flags
& XCOFF_CALLED
) != 0)
2692 /* Absolute relocations against absolute symbols can be
2693 resolved statically. */
2695 && (h
->root
.type
== bfd_link_hash_defined
2696 || h
->root
.type
== bfd_link_hash_defweak
)
2697 && !h
->root
.rel_from_abs
)
2699 asection
*sec
= h
->root
.u
.def
.section
;
2700 if (bfd_is_abs_section (sec
)
2702 && bfd_is_abs_section (sec
->output_section
)))
2717 /* Mark a symbol as not being garbage, including the section in which
2720 static inline bfd_boolean
2721 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2723 if ((h
->flags
& XCOFF_MARK
) != 0)
2726 h
->flags
|= XCOFF_MARK
;
2728 /* If we're marking an undefined symbol, try find some way of
2730 if (!bfd_link_relocatable (info
)
2731 && (h
->flags
& XCOFF_IMPORT
) == 0
2732 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2733 && (h
->root
.type
== bfd_link_hash_undefined
2734 || h
->root
.type
== bfd_link_hash_undefweak
))
2736 /* First check whether this symbol can be interpreted as an
2737 undefined function descriptor for a defined function symbol. */
2738 if (!xcoff_find_function (info
, h
))
2741 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2742 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2743 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2745 /* This is a descriptor for a defined symbol, but the input
2746 objects have not defined the descriptor itself. Fill in
2747 the definition automatically.
2749 Note that we do this even if we found a dynamic definition
2750 of H. The local function definition logically overrides
2754 sec
= xcoff_hash_table (info
)->descriptor_section
;
2755 h
->root
.type
= bfd_link_hash_defined
;
2756 h
->root
.u
.def
.section
= sec
;
2757 h
->root
.u
.def
.value
= sec
->size
;
2759 h
->flags
|= XCOFF_DEF_REGULAR
;
2761 /* The size of the function descriptor depends on whether this
2762 is xcoff32 (12) or xcoff64 (24). */
2763 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2765 /* A function descriptor uses two relocs: one for the
2766 associated code, and one for the TOC address. */
2767 xcoff_hash_table (info
)->ldrel_count
+= 2;
2768 sec
->reloc_count
+= 2;
2770 /* Mark the function itself. */
2771 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2774 /* Mark the TOC section, so that we get an anchor
2775 to relocate against. */
2776 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2779 /* We handle writing out the contents of the descriptor in
2780 xcoff_write_global_symbol. */
2782 else if (info
->static_link
)
2783 /* We can't get a symbol value dynamically, so just assume
2784 that it's undefined. */
2785 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2786 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2788 /* This is a function symbol for which we need to create
2791 struct xcoff_link_hash_entry
*hds
;
2793 /* Mark the descriptor (and its TOC section). */
2794 hds
= h
->descriptor
;
2795 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2796 || hds
->root
.type
== bfd_link_hash_undefweak
)
2797 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2798 if (!xcoff_mark_symbol (info
, hds
))
2801 /* Treat this symbol as undefined if the descriptor was. */
2802 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2803 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2805 /* Allocate room for the global linkage code itself. */
2806 sec
= xcoff_hash_table (info
)->linkage_section
;
2807 h
->root
.type
= bfd_link_hash_defined
;
2808 h
->root
.u
.def
.section
= sec
;
2809 h
->root
.u
.def
.value
= sec
->size
;
2811 h
->flags
|= XCOFF_DEF_REGULAR
;
2812 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2814 /* The global linkage code requires a TOC entry for the
2816 if (hds
->toc_section
== NULL
)
2821 xcoff32 uses 4 bytes in the toc.
2822 xcoff64 uses 8 bytes in the toc. */
2823 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2825 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2830 /* Allocate room in the fallback TOC section. */
2831 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2832 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2833 hds
->toc_section
->size
+= byte_size
;
2834 if (!xcoff_mark (info
, hds
->toc_section
))
2837 /* Allocate room for a static and dynamic R_TOC
2839 ++xcoff_hash_table (info
)->ldrel_count
;
2840 ++hds
->toc_section
->reloc_count
;
2842 /* Set the index to -2 to force this symbol to
2845 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2848 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2850 /* Record that the symbol was undefined, then import it.
2851 -brtl links use a special fake import file. */
2852 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2853 if (xcoff_hash_table (info
)->rtld
)
2855 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2860 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2866 if (h
->root
.type
== bfd_link_hash_defined
2867 || h
->root
.type
== bfd_link_hash_defweak
)
2871 hsec
= h
->root
.u
.def
.section
;
2872 if (! bfd_is_abs_section (hsec
)
2873 && (hsec
->flags
& SEC_MARK
) == 0)
2875 if (! xcoff_mark (info
, hsec
))
2880 if (h
->toc_section
!= NULL
2881 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2883 if (! xcoff_mark (info
, h
->toc_section
))
2890 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2891 If the symbol exists, set FLAGS. */
2894 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2895 const char *name
, unsigned int flags
)
2897 struct xcoff_link_hash_entry
*h
;
2899 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2900 FALSE
, FALSE
, TRUE
);
2904 if (h
->root
.type
== bfd_link_hash_defined
2905 || h
->root
.type
== bfd_link_hash_defweak
)
2907 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2914 /* The mark phase of garbage collection. For a given section, mark
2915 it, and all the sections which define symbols to which it refers.
2916 Because this function needs to look at the relocs, we also count
2917 the number of relocs which need to be copied into the .loader
2921 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2923 if (bfd_is_const_section (sec
)
2924 || (sec
->flags
& SEC_MARK
) != 0)
2927 sec
->flags
|= SEC_MARK
;
2929 if (sec
->owner
->xvec
== info
->output_bfd
->xvec
2930 && coff_section_data (sec
->owner
, sec
) != NULL
2931 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2933 struct xcoff_link_hash_entry
**syms
;
2934 struct internal_reloc
*rel
, *relend
;
2936 unsigned long i
, first
, last
;
2938 /* Mark all the symbols in this section. */
2939 syms
= obj_xcoff_sym_hashes (sec
->owner
);
2940 csects
= xcoff_data (sec
->owner
)->csects
;
2941 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
2942 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
2943 for (i
= first
; i
<= last
; i
++)
2944 if (csects
[i
] == sec
2946 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
2948 if (!xcoff_mark_symbol (info
, syms
[i
]))
2952 /* Look through the section relocs. */
2953 if ((sec
->flags
& SEC_RELOC
) != 0
2954 && sec
->reloc_count
> 0)
2956 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, TRUE
,
2960 relend
= rel
+ sec
->reloc_count
;
2961 for (; rel
< relend
; rel
++)
2963 struct xcoff_link_hash_entry
*h
;
2965 if ((unsigned int) rel
->r_symndx
2966 > obj_raw_syment_count (sec
->owner
))
2969 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2972 if ((h
->flags
& XCOFF_MARK
) == 0)
2974 if (!xcoff_mark_symbol (info
, h
))
2982 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2984 && (rsec
->flags
& SEC_MARK
) == 0)
2986 if (!xcoff_mark (info
, rsec
))
2991 /* See if this reloc needs to be copied into the .loader
2993 if (xcoff_need_ldrel_p (info
, rel
, h
))
2995 ++xcoff_hash_table (info
)->ldrel_count
;
2997 h
->flags
|= XCOFF_LDREL
;
3001 if (! info
->keep_memory
3002 && coff_section_data (sec
->owner
, sec
) != NULL
3003 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
3005 free (coff_section_data (sec
->owner
, sec
)->relocs
);
3006 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
3014 /* Routines that are called after all the input files have been
3015 handled, but before the sections are laid out in memory. */
3017 /* The sweep phase of garbage collection. Remove all garbage
3021 xcoff_sweep (struct bfd_link_info
*info
)
3025 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3029 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3031 if ((o
->flags
& SEC_MARK
) == 0)
3033 /* Keep all sections from non-XCOFF input files. Keep
3034 special sections. Keep .debug sections for the
3036 if (sub
->xvec
!= info
->output_bfd
->xvec
3037 || o
== xcoff_hash_table (info
)->debug_section
3038 || o
== xcoff_hash_table (info
)->loader_section
3039 || o
== xcoff_hash_table (info
)->linkage_section
3040 || o
== xcoff_hash_table (info
)->descriptor_section
3041 || (bfd_section_flags (o
) & SEC_DEBUGGING
)
3042 || strcmp (o
->name
, ".debug") == 0)
3043 o
->flags
|= SEC_MARK
;
3054 /* Record the number of elements in a set. This is used to output the
3055 correct csect length. */
3058 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3059 struct bfd_link_info
*info
,
3060 struct bfd_link_hash_entry
*harg
,
3063 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3064 struct xcoff_link_size_list
*n
;
3067 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3070 /* This will hardly ever be called. I don't want to burn four bytes
3071 per global symbol, so instead the size is kept on a linked list
3072 attached to the hash table. */
3074 n
= bfd_alloc (output_bfd
, amt
);
3077 n
->next
= xcoff_hash_table (info
)->size_list
;
3080 xcoff_hash_table (info
)->size_list
= n
;
3082 h
->flags
|= XCOFF_HAS_SIZE
;
3087 /* Import a symbol. */
3090 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3091 struct bfd_link_info
*info
,
3092 struct bfd_link_hash_entry
*harg
,
3094 const char *imppath
,
3095 const char *impfile
,
3096 const char *impmember
,
3097 unsigned int syscall_flag
)
3099 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3101 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3104 /* A symbol name which starts with a period is the code for a
3105 function. If the symbol is undefined, then add an undefined
3106 symbol for the function descriptor, and import that instead. */
3107 if (h
->root
.root
.string
[0] == '.'
3108 && h
->root
.type
== bfd_link_hash_undefined
3109 && val
== (bfd_vma
) -1)
3111 struct xcoff_link_hash_entry
*hds
;
3113 hds
= h
->descriptor
;
3116 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3117 h
->root
.root
.string
+ 1,
3121 if (hds
->root
.type
== bfd_link_hash_new
)
3123 hds
->root
.type
= bfd_link_hash_undefined
;
3124 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3126 hds
->flags
|= XCOFF_DESCRIPTOR
;
3127 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3128 hds
->descriptor
= h
;
3129 h
->descriptor
= hds
;
3132 /* Now, if the descriptor is undefined, import the descriptor
3133 rather than the symbol we were told to import. FIXME: Is
3134 this correct in all cases? */
3135 if (hds
->root
.type
== bfd_link_hash_undefined
)
3139 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3141 if (val
!= (bfd_vma
) -1)
3143 if (h
->root
.type
== bfd_link_hash_defined
)
3144 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
, output_bfd
,
3145 bfd_abs_section_ptr
, val
);
3147 h
->root
.type
= bfd_link_hash_defined
;
3148 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3149 h
->root
.u
.def
.value
= val
;
3153 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3159 /* Export a symbol. */
3162 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3163 struct bfd_link_info
*info
,
3164 struct bfd_link_hash_entry
*harg
)
3166 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3168 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3171 h
->flags
|= XCOFF_EXPORT
;
3173 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3174 I'm just going to ignore it until somebody explains it. */
3176 /* Make sure we don't garbage collect this symbol. */
3177 if (! xcoff_mark_symbol (info
, h
))
3180 /* If this is a function descriptor, make sure we don't garbage
3181 collect the associated function code. We normally don't have to
3182 worry about this, because the descriptor will be attached to a
3183 section with relocs, but if we are creating the descriptor
3184 ourselves those relocs will not be visible to the mark code. */
3185 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3187 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3194 /* Count a reloc against a symbol. This is called for relocs
3195 generated by the linker script, typically for global constructors
3199 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3200 struct bfd_link_info
*info
,
3203 struct xcoff_link_hash_entry
*h
;
3205 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3208 h
= ((struct xcoff_link_hash_entry
*)
3209 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, FALSE
, FALSE
,
3213 _bfd_error_handler (_("%s: no such symbol"), name
);
3214 bfd_set_error (bfd_error_no_symbols
);
3218 h
->flags
|= XCOFF_REF_REGULAR
;
3219 if (xcoff_hash_table (info
)->loader_section
)
3221 h
->flags
|= XCOFF_LDREL
;
3222 ++xcoff_hash_table (info
)->ldrel_count
;
3225 /* Mark the symbol to avoid garbage collection. */
3226 if (! xcoff_mark_symbol (info
, h
))
3232 /* This function is called for each symbol to which the linker script
3234 FIXME: In cases like the linker test ld-scripts/defined5 where a
3235 symbol is defined both by an input object file and the script,
3236 the script definition doesn't override the object file definition
3237 as is usual for other targets. At least not when the symbol is
3238 output. Other uses of the symbol value by the linker do use the
3242 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3243 struct bfd_link_info
*info
,
3246 struct xcoff_link_hash_entry
*h
;
3248 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3251 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
, TRUE
,
3256 h
->flags
|= XCOFF_DEF_REGULAR
;
3261 /* An xcoff_link_hash_traverse callback for which DATA points to an
3262 xcoff_loader_info. Mark all symbols that should be automatically
3266 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3268 struct xcoff_loader_info
*ldinfo
;
3270 ldinfo
= (struct xcoff_loader_info
*) data
;
3271 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3273 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3274 ldinfo
->failed
= TRUE
;
3279 /* Add a symbol to the .loader symbols, if necessary. */
3281 /* INPUT_BFD has an external symbol associated with hash table entry H
3282 and csect CSECT. Return true if INPUT_BFD defines H. */
3285 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3288 switch (h
->root
.type
)
3290 case bfd_link_hash_defined
:
3291 case bfd_link_hash_defweak
:
3292 /* No input bfd owns absolute symbols. They are written by
3293 xcoff_write_global_symbol instead. */
3294 return (!bfd_is_abs_section (csect
)
3295 && h
->root
.u
.def
.section
== csect
);
3297 case bfd_link_hash_common
:
3298 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3300 case bfd_link_hash_undefined
:
3301 case bfd_link_hash_undefweak
:
3302 /* We can't treat undef.abfd as the owner because that bfd
3303 might be a dynamic object. Allow any bfd to claim it. */
3311 /* See if H should have a loader symbol associated with it. */
3314 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3315 struct xcoff_link_hash_entry
*h
)
3319 /* Warn if this symbol is exported but not defined. */
3320 if ((h
->flags
& XCOFF_EXPORT
) != 0
3321 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3324 (_("warning: attempt to export undefined symbol `%s'"),
3325 h
->root
.root
.string
);
3329 /* We need to add a symbol to the .loader section if it is mentioned
3330 in a reloc which we are copying to the .loader section and it was
3331 not defined or common, or if it is the entry point, or if it is
3333 if (((h
->flags
& XCOFF_LDREL
) == 0
3334 || h
->root
.type
== bfd_link_hash_defined
3335 || h
->root
.type
== bfd_link_hash_defweak
3336 || h
->root
.type
== bfd_link_hash_common
)
3337 && (h
->flags
& XCOFF_ENTRY
) == 0
3338 && (h
->flags
& XCOFF_EXPORT
) == 0)
3341 /* We need to add this symbol to the .loader symbols. */
3343 BFD_ASSERT (h
->ldsym
== NULL
);
3344 amt
= sizeof (struct internal_ldsym
);
3345 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3346 if (h
->ldsym
== NULL
)
3348 ldinfo
->failed
= TRUE
;
3352 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3354 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3355 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3357 h
->ldsym
->l_ifile
= h
->ldindx
;
3360 /* The first 3 symbol table indices are reserved to indicate the
3361 data, text and bss sections. */
3362 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3364 ++ldinfo
->ldsym_count
;
3366 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3367 h
->ldsym
, h
->root
.root
.string
))
3370 h
->flags
|= XCOFF_BUILT_LDSYM
;
3374 /* An xcoff_htab_traverse callback that is called for each symbol
3375 once garbage collection is complete. */
3378 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3380 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3382 /* __rtinit, this symbol has special handling. */
3383 if (h
->flags
& XCOFF_RTINIT
)
3386 /* We don't want to garbage collect symbols which are not defined in
3387 XCOFF files. This is a convenient place to mark them. */
3388 if (xcoff_hash_table (ldinfo
->info
)->gc
3389 && (h
->flags
& XCOFF_MARK
) == 0
3390 && (h
->root
.type
== bfd_link_hash_defined
3391 || h
->root
.type
== bfd_link_hash_defweak
)
3392 && (h
->root
.u
.def
.section
->owner
== NULL
3393 || (h
->root
.u
.def
.section
->owner
->xvec
3394 != ldinfo
->info
->output_bfd
->xvec
)))
3395 h
->flags
|= XCOFF_MARK
;
3397 /* Skip discarded symbols. */
3398 if (xcoff_hash_table (ldinfo
->info
)->gc
3399 && (h
->flags
& XCOFF_MARK
) == 0)
3402 /* If this is still a common symbol, and it wasn't garbage
3403 collected, we need to actually allocate space for it in the .bss
3405 if (h
->root
.type
== bfd_link_hash_common
3406 && h
->root
.u
.c
.p
->section
->size
== 0)
3408 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3409 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3412 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3414 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3415 h
->flags
|= XCOFF_EXPORT
;
3417 if (!xcoff_build_ldsym (ldinfo
, h
))
3424 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3425 hash table entry H and csect CSECT. AUX contains ISYM's auxiliary
3426 csect information, if any. NAME is the function's name if the name
3427 is stored in the .debug section, otherwise it is null.
3429 Return 1 if we should include an appropriately-adjusted ISYM
3430 in the output file, 0 if we should discard ISYM, or -1 if an
3434 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3435 struct internal_syment
*isym
,
3436 union internal_auxent
*aux
,
3437 struct xcoff_link_hash_entry
*h
,
3438 asection
*csect
, const char *name
)
3442 /* If we are skipping this csect, we want to strip the symbol too. */
3446 /* Likewise if we garbage-collected the csect. */
3447 if (xcoff_hash_table (info
)->gc
3448 && !bfd_is_abs_section (csect
)
3449 && !bfd_is_und_section (csect
)
3450 && (csect
->flags
& SEC_MARK
) == 0)
3453 /* An XCOFF linker always removes C_STAT symbols. */
3454 if (isym
->n_sclass
== C_STAT
)
3457 /* We generate the TOC anchor separately. */
3458 if (isym
->n_sclass
== C_HIDEXT
3459 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3462 /* If we are stripping all symbols, we want to discard this one. */
3463 if (info
->strip
== strip_all
)
3466 /* Discard symbols that are defined elsewhere. */
3467 if (EXTERN_SYM_P (isym
->n_sclass
))
3469 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3471 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3475 /* If we're discarding local symbols, check whether ISYM is local. */
3476 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3477 if (info
->discard
== discard_all
3478 && !EXTERN_SYM_P (isym
->n_sclass
)
3479 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3482 /* If we're stripping debugging symbols, check whether ISYM is one. */
3483 if (info
->strip
== strip_debugger
3484 && isym
->n_scnum
== N_DEBUG
)
3487 /* If we are stripping symbols based on name, check how ISYM's
3488 name should be handled. */
3489 if (info
->strip
== strip_some
3490 || info
->discard
== discard_l
)
3492 char buf
[SYMNMLEN
+ 1];
3496 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3501 if (info
->strip
== strip_some
3502 && bfd_hash_lookup (info
->keep_hash
, name
, FALSE
, FALSE
) == NULL
)
3505 if (info
->discard
== discard_l
3506 && !EXTERN_SYM_P (isym
->n_sclass
)
3507 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3508 && bfd_is_local_label_name (input_bfd
, name
))
3515 /* Lay out the .loader section, filling in the header and the import paths.
3516 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3519 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
,
3520 const char *libpath
)
3523 struct xcoff_link_hash_table
*htab
;
3524 struct internal_ldhdr
*ldhdr
;
3525 struct xcoff_import_file
*fl
;
3526 bfd_size_type stoff
;
3527 size_t impsize
, impcount
;
3531 /* Work out the size of the import file names. Each import file ID
3532 consists of three null terminated strings: the path, the file
3533 name, and the archive member name. The first entry in the list
3534 of names is the path to use to find objects, which the linker has
3535 passed in as the libpath argument. For some reason, the path
3536 entry in the other import file names appears to always be empty. */
3537 output_bfd
= ldinfo
->output_bfd
;
3538 htab
= xcoff_hash_table (ldinfo
->info
);
3539 impsize
= strlen (libpath
) + 3;
3541 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3544 impsize
+= (strlen (fl
->path
)
3546 + strlen (fl
->member
)
3550 /* Set up the .loader section header. */
3551 ldhdr
= &htab
->ldhdr
;
3552 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3553 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3554 ldhdr
->l_nreloc
= htab
->ldrel_count
;
3555 ldhdr
->l_istlen
= impsize
;
3556 ldhdr
->l_nimpid
= impcount
;
3557 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3558 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3559 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3560 ldhdr
->l_stlen
= ldinfo
->string_size
;
3561 stoff
= ldhdr
->l_impoff
+ impsize
;
3562 if (ldinfo
->string_size
== 0)
3565 ldhdr
->l_stoff
= stoff
;
3567 /* 64 bit elements to ldhdr
3568 The swap out routine for 32 bit will ignore them.
3569 Nothing fancy, symbols come after the header and relocs come
3571 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3572 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3573 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3575 /* We now know the final size of the .loader section. Allocate
3577 lsec
= htab
->loader_section
;
3578 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3579 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3580 if (lsec
->contents
== NULL
)
3583 /* Set up the header. */
3584 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3586 /* Set up the import file names. */
3587 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3588 strcpy (out
, libpath
);
3589 out
+= strlen (libpath
) + 1;
3592 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3597 while ((*out
++ = *s
++) != '\0')
3600 while ((*out
++ = *s
++) != '\0')
3603 while ((*out
++ = *s
++) != '\0')
3607 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3609 /* Set up the symbol string table. */
3610 if (ldinfo
->string_size
> 0)
3612 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
3613 free (ldinfo
->strings
);
3614 ldinfo
->strings
= NULL
;
3617 /* We can't set up the symbol table or the relocs yet, because we
3618 don't yet know the final position of the various sections. The
3619 .loader symbols are written out when the corresponding normal
3620 symbols are written out in xcoff_link_input_bfd or
3621 xcoff_write_global_symbol. The .loader relocs are written out
3622 when the corresponding normal relocs are handled in
3623 xcoff_link_input_bfd. */
3628 /* Build the .loader section. This is called by the XCOFF linker
3629 emulation before_allocation routine. We must set the size of the
3630 .loader section before the linker lays out the output file.
3631 LIBPATH is the library path to search for shared objects; this is
3632 normally built from the -L arguments passed to the linker. ENTRY
3633 is the name of the entry point symbol (the -e linker option).
3634 FILE_ALIGN is the alignment to use for sections within the file
3635 (the -H linker option). MAXSTACK is the maximum stack size (the
3636 -bmaxstack linker option). MAXDATA is the maximum data size (the
3637 -bmaxdata linker option). GC is whether to do garbage collection
3638 (the -bgc linker option). MODTYPE is the module type (the
3639 -bmodtype linker option). TEXTRO is whether the text section must
3640 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3641 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3642 is set by this routine to csects with magic names like _end. */
3645 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3646 struct bfd_link_info
*info
,
3647 const char *libpath
,
3649 unsigned long file_align
,
3650 unsigned long maxstack
,
3651 unsigned long maxdata
,
3655 unsigned int auto_export_flags
,
3656 asection
**special_sections
,
3659 struct xcoff_loader_info ldinfo
;
3663 struct bfd_strtab_hash
*debug_strtab
;
3664 bfd_byte
*debug_contents
= NULL
;
3667 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3669 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3670 special_sections
[i
] = NULL
;
3674 ldinfo
.failed
= FALSE
;
3675 ldinfo
.output_bfd
= output_bfd
;
3677 ldinfo
.auto_export_flags
= auto_export_flags
;
3678 ldinfo
.ldsym_count
= 0;
3679 ldinfo
.string_size
= 0;
3680 ldinfo
.strings
= NULL
;
3681 ldinfo
.string_alc
= 0;
3683 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3684 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3685 xcoff_data (output_bfd
)->modtype
= modtype
;
3687 xcoff_hash_table (info
)->file_align
= file_align
;
3688 xcoff_hash_table (info
)->textro
= textro
;
3689 xcoff_hash_table (info
)->rtld
= rtld
;
3692 if (xcoff_hash_table (info
)->loader_section
3693 && (info
->init_function
|| info
->fini_function
|| rtld
))
3695 struct xcoff_link_hash_entry
*hsym
;
3696 struct internal_ldsym
*ldsym
;
3698 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3699 "__rtinit", FALSE
, FALSE
, TRUE
);
3703 (_("error: undefined symbol __rtinit"));
3707 xcoff_mark_symbol (info
, hsym
);
3708 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3710 /* __rtinit initialized. */
3711 amt
= sizeof (* ldsym
);
3712 ldsym
= bfd_malloc (amt
);
3714 ldsym
->l_value
= 0; /* Will be filled in later. */
3715 ldsym
->l_scnum
= 2; /* Data section. */
3716 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3717 ldsym
->l_smclas
= 5; /* .rw. */
3718 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3719 ldsym
->l_parm
= 0; /* NA. */
3721 /* Force __rtinit to be the first symbol in the loader symbol table
3722 See xcoff_build_ldsyms
3724 The first 3 symbol table indices are reserved to indicate the data,
3725 text and bss sections. */
3726 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3729 ldinfo
.ldsym_count
= 1;
3730 hsym
->ldsym
= ldsym
;
3732 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3733 hsym
->ldsym
, hsym
->root
.root
.string
))
3736 /* This symbol is written out by xcoff_write_global_symbol
3737 Set stuff up so xcoff_write_global_symbol logic works. */
3738 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3739 hsym
->root
.type
= bfd_link_hash_defined
;
3740 hsym
->root
.u
.def
.value
= 0;
3743 /* Garbage collect unused sections. */
3744 if (bfd_link_relocatable (info
) || !gc
)
3747 xcoff_hash_table (info
)->gc
= FALSE
;
3749 /* We still need to call xcoff_mark, in order to set ldrel_count
3751 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3755 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3757 /* We shouldn't unconditionaly mark the TOC section.
3758 The output file should only have a TOC if either
3759 (a) one of the input files did or (b) we end up
3760 creating TOC references as part of the link process. */
3761 if (o
!= xcoff_hash_table (info
)->toc_section
3762 && (o
->flags
& SEC_MARK
) == 0)
3764 if (! xcoff_mark (info
, o
))
3773 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3775 if (info
->init_function
!= NULL
3776 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3778 if (info
->fini_function
!= NULL
3779 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3781 if (auto_export_flags
!= 0)
3783 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3784 xcoff_mark_auto_exports
, &ldinfo
);
3789 xcoff_hash_table (info
)->gc
= TRUE
;
3792 /* Return special sections to the caller. */
3793 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3795 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3799 && (sec
->flags
& SEC_MARK
) == 0)
3802 special_sections
[i
] = sec
;
3805 if (info
->input_bfds
== NULL
)
3806 /* I'm not sure what to do in this bizarre case. */
3809 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3814 if (xcoff_hash_table (info
)->loader_section
3815 && !xcoff_build_loader_section (&ldinfo
, libpath
))
3818 /* Allocate space for the magic sections. */
3819 sec
= xcoff_hash_table (info
)->linkage_section
;
3822 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3823 if (sec
->contents
== NULL
)
3826 sec
= xcoff_hash_table (info
)->toc_section
;
3829 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3830 if (sec
->contents
== NULL
)
3833 sec
= xcoff_hash_table (info
)->descriptor_section
;
3836 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3837 if (sec
->contents
== NULL
)
3841 /* Now that we've done garbage collection, decide which symbols to keep,
3842 and figure out the contents of the .debug section. */
3843 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3845 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
3848 bfd_size_type symcount
;
3851 unsigned int *lineno_counts
;
3852 struct xcoff_link_hash_entry
**sym_hash
;
3853 bfd_byte
*esym
, *esymend
;
3854 bfd_size_type symesz
;
3856 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3859 if ((sub
->flags
& DYNAMIC
) != 0
3860 && !info
->static_link
)
3863 if (! _bfd_coff_get_external_symbols (sub
))
3866 symcount
= obj_raw_syment_count (sub
);
3867 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
3868 if (debug_index
== NULL
)
3870 xcoff_data (sub
)->debug_indices
= debug_index
;
3872 if (info
->strip
== strip_all
3873 || info
->strip
== strip_debugger
3874 || info
->discard
== discard_all
)
3875 /* We're stripping all debugging information, so there's no need
3876 to read SUB's .debug section. */
3880 /* Grab the contents of SUB's .debug section, if any. */
3881 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3882 if (subdeb
!= NULL
&& subdeb
->size
> 0)
3884 /* We use malloc and copy the names into the debug
3885 stringtab, rather than bfd_alloc, because I expect
3886 that, when linking many files together, many of the
3887 strings will be the same. Storing the strings in the
3888 hash table should save space in this case. */
3889 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
3894 csectpp
= xcoff_data (sub
)->csects
;
3895 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
3896 sym_hash
= obj_xcoff_sym_hashes (sub
);
3897 symesz
= bfd_coff_symesz (sub
);
3898 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3899 esymend
= esym
+ symcount
* symesz
;
3901 while (esym
< esymend
)
3903 struct internal_syment sym
;
3904 union internal_auxent aux
;
3909 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
3911 /* Read in the csect information, if any. */
3912 if (CSECT_SYM_P (sym
.n_sclass
))
3914 BFD_ASSERT (sym
.n_numaux
> 0);
3915 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
3916 sym
.n_type
, sym
.n_sclass
,
3917 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
3920 /* If this symbol's name is stored in the debug section,
3921 get a pointer to it. */
3922 if (debug_contents
!= NULL
3923 && sym
._n
._n_n
._n_zeroes
== 0
3924 && bfd_coff_symname_in_debug (sub
, &sym
))
3925 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3929 /* Decide whether to copy this symbol to the output file. */
3931 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
3932 *sym_hash
, csect
, name
);
3937 /* Use a debug_index of -2 to record that a symbol should
3942 /* See whether we should store the symbol name in the
3943 output .debug section. */
3948 indx
= _bfd_stringtab_add (debug_strtab
, name
, TRUE
, TRUE
);
3949 if (indx
== (bfd_size_type
) -1)
3951 *debug_index
= indx
;
3956 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
3957 if (*lineno_counts
> 0)
3958 csect
->output_section
->lineno_count
+= *lineno_counts
;
3961 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3962 csectpp
+= sym
.n_numaux
+ 1;
3963 sym_hash
+= sym
.n_numaux
+ 1;
3964 lineno_counts
+= sym
.n_numaux
+ 1;
3965 debug_index
+= sym
.n_numaux
+ 1;
3970 free (debug_contents
);
3971 debug_contents
= NULL
;
3973 /* Clear the size of subdeb, so that it is not included directly
3974 in the output file. */
3978 if (! info
->keep_memory
)
3980 if (! _bfd_coff_free_symbols (sub
))
3985 if (info
->strip
!= strip_all
)
3986 xcoff_hash_table (info
)->debug_section
->size
=
3987 _bfd_stringtab_size (debug_strtab
);
3992 free (ldinfo
.strings
);
3993 free (debug_contents
);
3998 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
4003 struct bfd_in_memory
*bim
;
4005 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
4012 abfd
->link
.next
= 0;
4013 abfd
->format
= bfd_object
;
4014 abfd
->iostream
= (void *) bim
;
4015 abfd
->flags
= BFD_IN_MEMORY
;
4016 abfd
->iovec
= &_bfd_memory_iovec
;
4017 abfd
->direction
= write_direction
;
4021 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
4024 /* need to reset to unknown or it will not be read back in correctly */
4025 abfd
->format
= bfd_unknown
;
4026 abfd
->direction
= read_direction
;
4032 /* Return the section that defines H. Return null if no section does. */
4035 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
4037 switch (h
->root
.type
)
4039 case bfd_link_hash_defined
:
4040 case bfd_link_hash_defweak
:
4041 return h
->root
.u
.def
.section
;
4043 case bfd_link_hash_common
:
4044 return h
->root
.u
.c
.p
->section
;
4051 /* Add a .loader relocation for input relocation IREL. If the loader
4052 relocation should be against an output section, HSEC points to the
4053 input section that IREL is against, otherwise HSEC is null. H is the
4054 symbol that IREL is against, or null if it isn't against a global symbol.
4055 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4058 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
4059 asection
*output_section
, bfd
*reference_bfd
,
4060 struct internal_reloc
*irel
, asection
*hsec
,
4061 struct xcoff_link_hash_entry
*h
)
4063 struct internal_ldrel ldrel
;
4065 ldrel
.l_vaddr
= irel
->r_vaddr
;
4068 const char *secname
;
4070 secname
= hsec
->output_section
->name
;
4071 if (strcmp (secname
, ".text") == 0)
4073 else if (strcmp (secname
, ".data") == 0)
4075 else if (strcmp (secname
, ".bss") == 0)
4077 else if (strcmp (secname
, ".tdata") == 0)
4078 ldrel
.l_symndx
= -1;
4079 else if (strcmp (secname
, ".tbss") == 0)
4080 ldrel
.l_symndx
= -2;
4084 /* xgettext:c-format */
4085 (_("%pB: loader reloc in unrecognized section `%s'"),
4086 reference_bfd
, secname
);
4087 bfd_set_error (bfd_error_nonrepresentable_section
);
4096 /* xgettext:c-format */
4097 (_("%pB: `%s' in loader reloc but not loader sym"),
4098 reference_bfd
, h
->root
.root
.string
);
4099 bfd_set_error (bfd_error_bad_value
);
4102 ldrel
.l_symndx
= h
->ldindx
;
4105 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4107 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4108 ldrel
.l_rsecnm
= output_section
->target_index
;
4109 if (xcoff_hash_table (flinfo
->info
)->textro
4110 && strcmp (output_section
->name
, ".text") == 0)
4113 /* xgettext:c-format */
4114 (_("%pB: loader reloc in read-only section %pA"),
4115 reference_bfd
, output_section
);
4116 bfd_set_error (bfd_error_invalid_operation
);
4119 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
4120 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4124 /* Link an input file into the linker output file. This function
4125 handles all the sections and relocations of the input file at once. */
4128 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
4132 const char *strings
;
4133 bfd_size_type syment_base
;
4134 unsigned int n_tmask
;
4135 unsigned int n_btshft
;
4136 bfd_boolean copy
, hash
;
4137 bfd_size_type isymesz
;
4138 bfd_size_type osymesz
;
4139 bfd_size_type linesz
;
4142 struct xcoff_link_hash_entry
**sym_hash
;
4143 struct internal_syment
*isymp
;
4145 unsigned int *lineno_counts
;
4148 unsigned long output_index
;
4152 bfd_boolean keep_syms
;
4155 /* We can just skip DYNAMIC files, unless this is a static link. */
4156 if ((input_bfd
->flags
& DYNAMIC
) != 0
4157 && ! flinfo
->info
->static_link
)
4160 /* Move all the symbols to the output file. */
4161 output_bfd
= flinfo
->output_bfd
;
4163 syment_base
= obj_raw_syment_count (output_bfd
);
4164 isymesz
= bfd_coff_symesz (input_bfd
);
4165 osymesz
= bfd_coff_symesz (output_bfd
);
4166 linesz
= bfd_coff_linesz (input_bfd
);
4167 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4169 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4170 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4172 /* Define macros so that ISFCN, et. al., macros work correctly. */
4173 #define N_TMASK n_tmask
4174 #define N_BTSHFT n_btshft
4177 if (! flinfo
->info
->keep_memory
)
4180 if (flinfo
->info
->traditional_format
)
4183 if (! _bfd_coff_get_external_symbols (input_bfd
))
4186 /* Make one pass over the symbols and assign indices to symbols that
4187 we have decided to keep. Also use create .loader symbol information
4188 and update information in hash table entries. */
4189 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4190 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4191 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4192 csectpp
= xcoff_data (input_bfd
)->csects
;
4193 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4194 isymp
= flinfo
->internal_syms
;
4195 indexp
= flinfo
->sym_indices
;
4196 output_index
= syment_base
;
4197 while (esym
< esym_end
)
4199 union internal_auxent aux
;
4203 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4205 /* Read in the csect information, if any. */
4206 if (CSECT_SYM_P (isymp
->n_sclass
))
4208 BFD_ASSERT (isymp
->n_numaux
> 0);
4209 bfd_coff_swap_aux_in (input_bfd
,
4210 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4211 isymp
->n_type
, isymp
->n_sclass
,
4212 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4215 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4218 /* If this symbol is in the .loader section, swap out the
4219 .loader symbol information. If this is an external symbol
4220 reference to a defined symbol, though, then wait until we get
4221 to the definition. */
4222 if (EXTERN_SYM_P (isymp
->n_sclass
)
4223 && *sym_hash
!= NULL
4224 && (*sym_hash
)->ldsym
!= NULL
4225 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4227 struct xcoff_link_hash_entry
*h
;
4228 struct internal_ldsym
*ldsym
;
4232 if (isymp
->n_scnum
> 0)
4234 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4235 ldsym
->l_value
= (isymp
->n_value
4236 + (*csectpp
)->output_section
->vma
4237 + (*csectpp
)->output_offset
4242 ldsym
->l_scnum
= isymp
->n_scnum
;
4243 ldsym
->l_value
= isymp
->n_value
;
4246 ldsym
->l_smtype
= smtyp
;
4247 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4248 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4249 || (h
->flags
& XCOFF_IMPORT
) != 0)
4250 ldsym
->l_smtype
|= L_IMPORT
;
4251 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4252 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4253 || (h
->flags
& XCOFF_EXPORT
) != 0)
4254 ldsym
->l_smtype
|= L_EXPORT
;
4255 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4256 ldsym
->l_smtype
|= L_ENTRY
;
4257 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4258 ldsym
->l_smtype
|= L_WEAK
;
4260 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4262 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4264 else if (ldsym
->l_ifile
== 0)
4266 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4272 if (h
->root
.type
== bfd_link_hash_defined
4273 || h
->root
.type
== bfd_link_hash_defweak
)
4274 impbfd
= h
->root
.u
.def
.section
->owner
;
4275 else if (h
->root
.type
== bfd_link_hash_undefined
4276 || h
->root
.type
== bfd_link_hash_undefweak
)
4277 impbfd
= h
->root
.u
.undef
.abfd
;
4285 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
4286 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4293 BFD_ASSERT (h
->ldindx
>= 0);
4294 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
4297 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
4300 /* Fill in snentry now that we know the target_index. */
4301 if ((h
->flags
& XCOFF_ENTRY
) != 0
4302 && (h
->root
.type
== bfd_link_hash_defined
4303 || h
->root
.type
== bfd_link_hash_defweak
))
4305 xcoff_data (output_bfd
)->snentry
=
4306 h
->root
.u
.def
.section
->output_section
->target_index
;
4310 add
= 1 + isymp
->n_numaux
;
4312 if (*debug_index
== -2)
4313 /* We've decided to strip this symbol. */
4317 /* Assign the next unused index to this symbol. */
4318 *indexp
= output_index
;
4320 if (EXTERN_SYM_P (isymp
->n_sclass
))
4322 BFD_ASSERT (*sym_hash
!= NULL
);
4323 (*sym_hash
)->indx
= output_index
;
4326 /* If this is a symbol in the TOC which we may have merged
4327 (class XMC_TC), remember the symbol index of the TOC
4329 if (isymp
->n_sclass
== C_HIDEXT
4330 && aux
.x_csect
.x_smclas
== XMC_TC
4331 && *sym_hash
!= NULL
)
4333 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4334 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4335 (*sym_hash
)->u
.toc_indx
= output_index
;
4338 output_index
+= add
;
4341 esym
+= add
* isymesz
;
4347 for (--add
; add
> 0; --add
)
4351 /* Now write out the symbols that we decided to keep. */
4353 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4354 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4355 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4356 isymp
= flinfo
->internal_syms
;
4357 indexp
= flinfo
->sym_indices
;
4358 csectpp
= xcoff_data (input_bfd
)->csects
;
4359 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4360 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4361 outsym
= flinfo
->outsyms
;
4364 while (esym
< esym_end
)
4368 add
= 1 + isymp
->n_numaux
;
4371 esym
+= add
* isymesz
;
4374 struct internal_syment isym
;
4377 /* Adjust the symbol in order to output it. */
4379 if (isym
._n
._n_n
._n_zeroes
== 0
4380 && isym
._n
._n_n
._n_offset
!= 0)
4382 /* This symbol has a long name. Enter it in the string
4383 table we are building. If *debug_index != -1, the
4384 name has already been entered in the .debug section. */
4385 if (*debug_index
>= 0)
4386 isym
._n
._n_n
._n_offset
= *debug_index
;
4392 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4396 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
4397 if (indx
== (bfd_size_type
) -1)
4399 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4403 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4404 multiple definition problems when linking a shared object
4405 statically. (The native linker doesn't enter __rtinit into
4406 the normal table at all, but having a local symbol can make
4407 the objdump output easier to read.) */
4408 if (isym
.n_sclass
== C_EXT
4410 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4411 isym
.n_sclass
= C_HIDEXT
;
4413 /* The value of a C_FILE symbol is the symbol index of the
4414 next C_FILE symbol. The value of the last C_FILE symbol
4415 is -1. We try to get this right, below, just before we
4416 write the symbols out, but in the general case we may
4417 have to write the symbol out twice. */
4418 if (isym
.n_sclass
== C_FILE
)
4420 if (flinfo
->last_file_index
!= -1
4421 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4423 /* We must correct the value of the last C_FILE entry. */
4424 flinfo
->last_file
.n_value
= *indexp
;
4425 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4427 /* The last C_FILE symbol is in this input file. */
4428 bfd_coff_swap_sym_out (output_bfd
,
4429 (void *) &flinfo
->last_file
,
4430 (void *) (flinfo
->outsyms
4431 + ((flinfo
->last_file_index
4437 /* We have already written out the last C_FILE
4438 symbol. We need to write it out again. We
4439 borrow *outsym temporarily. */
4442 bfd_coff_swap_sym_out (output_bfd
,
4443 (void *) &flinfo
->last_file
,
4446 pos
= obj_sym_filepos (output_bfd
);
4447 pos
+= flinfo
->last_file_index
* osymesz
;
4448 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4449 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4455 flinfo
->last_file_index
= *indexp
;
4456 flinfo
->last_file
= isym
;
4459 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4460 into the line numbers. We update the symbol values when
4461 we handle the line numbers. */
4462 if (isym
.n_sclass
== C_BINCL
4463 || isym
.n_sclass
== C_EINCL
)
4465 isym
.n_value
= flinfo
->line_filepos
;
4468 /* The value of a C_BSTAT symbol is the symbol table
4469 index of the containing csect. */
4470 else if (isym
.n_sclass
== C_BSTAT
)
4474 indx
= isym
.n_value
;
4475 if (indx
< obj_raw_syment_count (input_bfd
))
4479 symindx
= flinfo
->sym_indices
[indx
];
4483 isym
.n_value
= symindx
;
4486 else if (isym
.n_sclass
!= C_ESTAT
4487 && isym
.n_sclass
!= C_DECL
4488 && isym
.n_scnum
> 0)
4490 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4491 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4492 + (*csectpp
)->output_offset
4496 /* Output the symbol. */
4497 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4502 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4504 union internal_auxent aux
;
4506 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4507 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4510 if (isymp
->n_sclass
== C_FILE
)
4512 /* This is the file name (or some comment put in by
4513 the compiler). If it is long, we must put it in
4514 the string table. */
4515 if (aux
.x_file
.x_n
.x_zeroes
== 0
4516 && aux
.x_file
.x_n
.x_offset
!= 0)
4518 const char *filename
;
4521 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4522 >= STRING_SIZE_SIZE
);
4523 if (strings
== NULL
)
4525 strings
= _bfd_coff_read_string_table (input_bfd
);
4526 if (strings
== NULL
)
4529 if ((bfd_size_type
) aux
.x_file
.x_n
.x_offset
>= obj_coff_strings_len (input_bfd
))
4530 filename
= _("<corrupt>");
4532 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4533 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
4535 if (indx
== (bfd_size_type
) -1)
4537 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4540 else if (CSECT_SYM_P (isymp
->n_sclass
)
4541 && i
+ 1 == isymp
->n_numaux
)
4544 /* We don't support type checking. I don't know if
4546 aux
.x_csect
.x_parmhash
= 0;
4547 /* I don't think anybody uses these fields, but we'd
4548 better clobber them just in case. */
4549 aux
.x_csect
.x_stab
= 0;
4550 aux
.x_csect
.x_snstab
= 0;
4552 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4556 indx
= aux
.x_csect
.x_scnlen
.l
;
4557 if (indx
< obj_raw_syment_count (input_bfd
))
4561 symindx
= flinfo
->sym_indices
[indx
];
4564 aux
.x_csect
.x_scnlen
.l
= 0;
4568 aux
.x_csect
.x_scnlen
.l
= symindx
;
4573 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4577 if (ISFCN (isymp
->n_type
)
4578 || ISTAG (isymp
->n_sclass
)
4579 || isymp
->n_sclass
== C_BLOCK
4580 || isymp
->n_sclass
== C_FCN
)
4582 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4584 && indx
< obj_raw_syment_count (input_bfd
))
4586 /* We look forward through the symbol for
4587 the index of the next symbol we are going
4588 to include. I don't know if this is
4590 while (flinfo
->sym_indices
[indx
] < 0
4591 && indx
< obj_raw_syment_count (input_bfd
))
4593 if (indx
>= obj_raw_syment_count (input_bfd
))
4594 indx
= output_index
;
4596 indx
= flinfo
->sym_indices
[indx
];
4597 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4602 indx
= aux
.x_sym
.x_tagndx
.l
;
4603 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4607 symindx
= flinfo
->sym_indices
[indx
];
4609 aux
.x_sym
.x_tagndx
.l
= 0;
4611 aux
.x_sym
.x_tagndx
.l
= symindx
;
4616 /* Copy over the line numbers, unless we are stripping
4617 them. We do this on a symbol by symbol basis in
4618 order to more easily handle garbage collection. */
4619 if (CSECT_SYM_P (isymp
->n_sclass
)
4621 && isymp
->n_numaux
> 1
4622 && ISFCN (isymp
->n_type
)
4623 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4625 if (*lineno_counts
== 0)
4626 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4629 asection
*enclosing
;
4630 unsigned int enc_count
;
4631 bfd_signed_vma linoff
;
4632 struct internal_lineno lin
;
4639 /* Read in the enclosing section's line-number
4640 information, if we haven't already. */
4642 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4643 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4644 if (oline
!= enclosing
)
4646 pos
= enclosing
->line_filepos
;
4647 amt
= linesz
* enc_count
;
4648 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4649 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
4655 /* Copy across the first entry, adjusting its
4657 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4658 - enclosing
->line_filepos
);
4659 linp
= flinfo
->linenos
+ linoff
;
4660 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4661 lin
.l_addr
.l_symndx
= *indexp
;
4662 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4664 /* Copy the other entries, adjusting their addresses. */
4665 linpend
= linp
+ *lineno_counts
* linesz
;
4666 offset
= (o
->output_section
->vma
4669 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4671 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4672 lin
.l_addr
.l_paddr
+= offset
;
4673 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4676 /* Write out the entries we've just processed. */
4677 pos
= (o
->output_section
->line_filepos
4678 + o
->output_section
->lineno_count
* linesz
);
4679 amt
= linesz
* *lineno_counts
;
4680 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4681 || bfd_bwrite (flinfo
->linenos
+ linoff
,
4682 amt
, output_bfd
) != amt
)
4684 o
->output_section
->lineno_count
+= *lineno_counts
;
4686 /* Record the offset of the symbol's line numbers
4687 in the output file. */
4688 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4692 struct internal_syment
*iisp
, *iispend
;
4695 bfd_vma range_start
, range_end
;
4698 /* Update any C_BINCL or C_EINCL symbols
4699 that refer to a line number in the
4700 range we just output. */
4701 iisp
= flinfo
->internal_syms
;
4702 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4703 iindp
= flinfo
->sym_indices
;
4704 oos
= flinfo
->outsyms
;
4705 range_start
= enclosing
->line_filepos
+ linoff
;
4706 range_end
= range_start
+ *lineno_counts
* linesz
;
4707 while (iisp
< iispend
)
4710 && (iisp
->n_sclass
== C_BINCL
4711 || iisp
->n_sclass
== C_EINCL
)
4712 && iisp
->n_value
>= range_start
4713 && iisp
->n_value
< range_end
)
4715 struct internal_syment iis
;
4717 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4718 iis
.n_value
= (iisp
->n_value
4721 bfd_coff_swap_sym_out (output_bfd
,
4726 iiadd
= 1 + iisp
->n_numaux
;
4728 oos
+= iiadd
* osymesz
;
4736 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4737 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4748 lineno_counts
+= add
;
4752 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4753 symbol will be the first symbol in the next input file. In the
4754 normal case, this will save us from writing out the C_FILE symbol
4756 if (flinfo
->last_file_index
!= -1
4757 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4759 flinfo
->last_file
.n_value
= output_index
;
4760 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
4761 (void *) (flinfo
->outsyms
4762 + ((flinfo
->last_file_index
- syment_base
)
4766 /* Write the modified symbols to the output file. */
4767 if (outsym
> flinfo
->outsyms
)
4769 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4770 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
4771 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4772 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
4775 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4776 + (outsym
- flinfo
->outsyms
) / osymesz
)
4779 obj_raw_syment_count (output_bfd
) = output_index
;
4782 /* Don't let the linker relocation routines discard the symbols. */
4783 keep_syms
= obj_coff_keep_syms (input_bfd
);
4784 obj_coff_keep_syms (input_bfd
) = TRUE
;
4786 /* Relocate the contents of each section. */
4787 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4791 if (! o
->linker_mark
)
4792 /* This section was omitted from the link. */
4795 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4797 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4800 /* We have set filepos correctly for the sections we created to
4801 represent csects, so bfd_get_section_contents should work. */
4802 if (coff_section_data (input_bfd
, o
) != NULL
4803 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4804 contents
= coff_section_data (input_bfd
, o
)->contents
;
4807 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4808 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
4810 contents
= flinfo
->contents
;
4813 if ((o
->flags
& SEC_RELOC
) != 0)
4816 struct internal_reloc
*internal_relocs
;
4817 struct internal_reloc
*irel
;
4819 struct internal_reloc
*irelend
;
4820 struct xcoff_link_hash_entry
**rel_hash
;
4823 /* Read in the relocs. */
4824 target_index
= o
->output_section
->target_index
;
4825 internal_relocs
= (xcoff_read_internal_relocs
4826 (input_bfd
, o
, FALSE
, flinfo
->external_relocs
,
4828 (flinfo
->section_info
[target_index
].relocs
4829 + o
->output_section
->reloc_count
)));
4830 if (internal_relocs
== NULL
)
4833 /* Call processor specific code to relocate the section
4835 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
4839 flinfo
->internal_syms
,
4840 xcoff_data (input_bfd
)->csects
))
4843 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4844 irel
= internal_relocs
;
4845 irelend
= irel
+ o
->reloc_count
;
4846 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
4847 + o
->output_section
->reloc_count
);
4848 for (; irel
< irelend
; irel
++, rel_hash
++)
4850 struct xcoff_link_hash_entry
*h
= NULL
;
4854 /* Adjust the reloc address and symbol index. */
4856 irel
->r_vaddr
+= offset
;
4858 r_symndx
= irel
->r_symndx
;
4863 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4865 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
4868 && h
->smclas
!= XMC_TD
4869 && (irel
->r_type
== R_TOC
4870 || irel
->r_type
== R_GL
4871 || irel
->r_type
== R_TCL
4872 || irel
->r_type
== R_TRL
4873 || irel
->r_type
== R_TRLA
))
4875 /* This is a TOC relative reloc with a symbol
4876 attached. The symbol should be the one which
4877 this reloc is for. We want to make this
4878 reloc against the TOC address of the symbol,
4879 not the symbol itself. */
4880 BFD_ASSERT (h
->toc_section
!= NULL
);
4881 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4882 if (h
->u
.toc_indx
!= -1)
4883 irel
->r_symndx
= h
->u
.toc_indx
;
4886 struct xcoff_toc_rel_hash
*n
;
4887 struct xcoff_link_section_info
*si
;
4891 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
4894 si
= flinfo
->section_info
+ target_index
;
4895 n
->next
= si
->toc_rel_hashes
;
4898 si
->toc_rel_hashes
= n
;
4903 /* This is a global symbol. */
4905 irel
->r_symndx
= h
->indx
;
4908 /* This symbol is being written at the end
4909 of the file, and we do not yet know the
4910 symbol index. We save the pointer to the
4911 hash table entry in the rel_hash list.
4912 We set the indx field to -2 to indicate
4913 that this symbol must not be stripped. */
4922 indx
= flinfo
->sym_indices
[r_symndx
];
4926 struct internal_syment
*is
;
4928 /* Relocations against a TC0 TOC anchor are
4929 automatically transformed to be against
4930 the TOC anchor in the output file. */
4931 is
= flinfo
->internal_syms
+ r_symndx
;
4932 if (is
->n_sclass
== C_HIDEXT
4933 && is
->n_numaux
> 0)
4936 union internal_auxent aux
;
4940 obj_coff_external_syms (input_bfd
))
4941 + ((r_symndx
+ is
->n_numaux
)
4943 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4944 is
->n_type
, is
->n_sclass
,
4948 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4949 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4950 indx
= flinfo
->toc_symindx
;
4955 irel
->r_symndx
= indx
;
4959 struct internal_syment
*is
;
4962 char buf
[SYMNMLEN
+ 1];
4964 /* This reloc is against a symbol we are
4965 stripping. It would be possible to handle
4966 this case, but I don't think it's worth it. */
4967 is
= flinfo
->internal_syms
+ r_symndx
;
4969 if (is
->n_sclass
!= C_DWARF
)
4971 name
= (_bfd_coff_internal_syment_name
4972 (input_bfd
, is
, buf
));
4977 (*flinfo
->info
->callbacks
->unattached_reloc
)
4978 (flinfo
->info
, name
,
4979 input_bfd
, o
, irel
->r_vaddr
);
4985 if ((o
->flags
& SEC_DEBUGGING
) == 0
4986 && xcoff_need_ldrel_p (flinfo
->info
, irel
, h
))
4993 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4995 sec
= xcoff_symbol_section (h
);
4996 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
4997 o
->output_section
, input_bfd
,
5003 o
->output_section
->reloc_count
+= o
->reloc_count
;
5006 /* Write out the modified section contents. */
5007 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5008 contents
, (file_ptr
) o
->output_offset
,
5013 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5015 if (! flinfo
->info
->keep_memory
)
5017 if (! _bfd_coff_free_symbols (input_bfd
))
5024 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5031 /* Sort relocs by VMA. This is called via qsort. */
5034 xcoff_sort_relocs (const void * p1
, const void * p2
)
5036 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
5037 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
5039 if (r1
->r_vaddr
> r2
->r_vaddr
)
5041 else if (r1
->r_vaddr
< r2
->r_vaddr
)
5047 /* Return true if section SEC is a TOC section. */
5049 static inline bfd_boolean
5050 xcoff_toc_section_p (asection
*sec
)
5055 if (name
[0] == '.' && name
[1] == 't')
5059 if (name
[3] == '0' && name
[4] == 0)
5064 if (name
[2] == 'd' && name
[3] == 0)
5070 /* See if the link requires a TOC (it usually does!). If so, find a
5071 good place to put the TOC anchor csect, and write out the associated
5075 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
5077 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5081 struct internal_syment irsym
;
5082 union internal_auxent iraux
;
5086 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5087 index of a csect at the beginning of the TOC. */
5088 toc_start
= ~(bfd_vma
) 0;
5091 for (input_bfd
= flinfo
->info
->input_bfds
;
5093 input_bfd
= input_bfd
->link
.next
)
5094 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5095 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5097 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5098 if (toc_start
> start
)
5101 section_index
= sec
->output_section
->target_index
;
5104 end
= start
+ sec
->size
;
5109 /* There's no need for a TC0 symbol if we don't have a TOC. */
5110 if (toc_end
< toc_start
)
5112 xcoff_data (output_bfd
)->toc
= toc_start
;
5116 if (toc_end
- toc_start
< 0x8000)
5117 /* Every TOC csect can be accessed from TOC_START. */
5118 best_address
= toc_start
;
5121 /* Find the lowest TOC csect that is still within range of TOC_END. */
5122 best_address
= toc_end
;
5123 for (input_bfd
= flinfo
->info
->input_bfds
;
5125 input_bfd
= input_bfd
->link
.next
)
5126 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5127 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5129 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5130 if (start
< best_address
5131 && start
+ 0x8000 >= toc_end
)
5133 best_address
= start
;
5134 section_index
= sec
->output_section
->target_index
;
5138 /* Make sure that the start of the TOC is also within range. */
5139 if (best_address
> toc_start
+ 0x8000)
5142 (_("TOC overflow: %#" PRIx64
" > 0x10000; try -mminimal-toc "
5144 (uint64_t) (toc_end
- toc_start
));
5145 bfd_set_error (bfd_error_file_too_big
);
5150 /* Record the chosen TOC value. */
5151 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5152 xcoff_data (output_bfd
)->toc
= best_address
;
5153 xcoff_data (output_bfd
)->sntoc
= section_index
;
5155 /* Fill out the TC0 symbol. */
5156 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5159 irsym
.n_value
= best_address
;
5160 irsym
.n_scnum
= section_index
;
5161 irsym
.n_sclass
= C_HIDEXT
;
5162 irsym
.n_type
= T_NULL
;
5164 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
5166 /* Fill out the auxiliary csect information. */
5167 memset (&iraux
, 0, sizeof iraux
);
5168 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5169 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5170 iraux
.x_csect
.x_scnlen
.l
= 0;
5171 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5172 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5174 /* Write the contents to the file. */
5175 pos
= obj_sym_filepos (output_bfd
);
5176 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5177 size
= 2 * bfd_coff_symesz (output_bfd
);
5178 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5179 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
5181 obj_raw_syment_count (output_bfd
) += 2;
5186 /* Write out a non-XCOFF global symbol. */
5189 xcoff_write_global_symbol (struct bfd_hash_entry
*bh
, void * inf
)
5191 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) bh
;
5192 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
5195 struct internal_syment isym
;
5196 union internal_auxent aux
;
5201 output_bfd
= flinfo
->output_bfd
;
5202 outsym
= flinfo
->outsyms
;
5204 if (h
->root
.type
== bfd_link_hash_warning
)
5206 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5207 if (h
->root
.type
== bfd_link_hash_new
)
5211 /* If this symbol was garbage collected, just skip it. */
5212 if (xcoff_hash_table (flinfo
->info
)->gc
5213 && (h
->flags
& XCOFF_MARK
) == 0)
5216 /* If we need a .loader section entry, write it out. */
5217 if (h
->ldsym
!= NULL
)
5219 struct internal_ldsym
*ldsym
;
5224 if (h
->root
.type
== bfd_link_hash_undefined
5225 || h
->root
.type
== bfd_link_hash_undefweak
)
5229 ldsym
->l_scnum
= N_UNDEF
;
5230 ldsym
->l_smtype
= XTY_ER
;
5231 impbfd
= h
->root
.u
.undef
.abfd
;
5234 else if (h
->root
.type
== bfd_link_hash_defined
5235 || h
->root
.type
== bfd_link_hash_defweak
)
5239 sec
= h
->root
.u
.def
.section
;
5240 ldsym
->l_value
= (sec
->output_section
->vma
5241 + sec
->output_offset
5242 + h
->root
.u
.def
.value
);
5243 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5244 ldsym
->l_smtype
= XTY_SD
;
5245 impbfd
= sec
->owner
;
5251 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5252 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5253 || (h
->flags
& XCOFF_IMPORT
) != 0)
5255 Import symbols are defined so the check above will make
5256 the l_smtype XTY_SD. But this is not correct, it should
5258 ldsym
->l_smtype
|= L_IMPORT
;
5260 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5261 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5262 || (h
->flags
& XCOFF_EXPORT
) != 0)
5263 ldsym
->l_smtype
|= L_EXPORT
;
5265 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5266 ldsym
->l_smtype
|= L_ENTRY
;
5268 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5269 ldsym
->l_smtype
= XTY_SD
;
5271 ldsym
->l_smclas
= h
->smclas
;
5273 if (ldsym
->l_smtype
& L_IMPORT
)
5275 if ((h
->root
.type
== bfd_link_hash_defined
5276 || h
->root
.type
== bfd_link_hash_defweak
)
5277 && (h
->root
.u
.def
.value
!= 0))
5278 ldsym
->l_smclas
= XMC_XO
;
5280 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5281 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5282 ldsym
->l_smclas
= XMC_SV3264
;
5284 else if (h
->flags
& XCOFF_SYSCALL32
)
5285 ldsym
->l_smclas
= XMC_SV
;
5287 else if (h
->flags
& XCOFF_SYSCALL64
)
5288 ldsym
->l_smclas
= XMC_SV64
;
5291 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5295 else if (ldsym
->l_ifile
== 0)
5297 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5299 else if (impbfd
== NULL
)
5303 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5304 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5310 BFD_ASSERT (h
->ldindx
>= 0);
5312 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5315 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
5319 /* If this symbol needs global linkage code, write it out. */
5320 if (h
->root
.type
== bfd_link_hash_defined
5321 && (h
->root
.u
.def
.section
5322 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
5328 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5330 /* The first instruction in the global linkage code loads a
5331 specific TOC element. */
5332 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5333 + h
->descriptor
->toc_section
->output_offset
5334 - xcoff_data (output_bfd
)->toc
);
5336 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5337 tocoff
+= h
->descriptor
->u
.toc_offset
;
5339 /* The first instruction in the glink code needs to be
5340 cooked to hold the correct offset in the toc. The
5341 rest are just output raw. */
5342 bfd_put_32 (output_bfd
,
5343 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5345 /* Start with i == 1 to get past the first instruction done above
5346 The /4 is because the glink code is in bytes and we are going
5348 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5349 bfd_put_32 (output_bfd
,
5350 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5354 /* If we created a TOC entry for this symbol, write out the required
5356 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5361 struct internal_reloc
*irel
;
5362 struct internal_syment irsym
;
5363 union internal_auxent iraux
;
5365 tocsec
= h
->toc_section
;
5366 osec
= tocsec
->output_section
;
5367 oindx
= osec
->target_index
;
5368 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5369 irel
->r_vaddr
= (osec
->vma
5370 + tocsec
->output_offset
5374 irel
->r_symndx
= h
->indx
;
5378 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5381 BFD_ASSERT (h
->ldindx
>= 0);
5383 /* Initialize the aux union here instead of closer to when it is
5384 written out below because the length of the csect depends on
5385 whether the output is 32 or 64 bit. */
5386 memset (&iraux
, 0, sizeof iraux
);
5387 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5388 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5389 iraux
.x_csect
.x_smclas
= XMC_TC
;
5391 /* 32 bit uses a 32 bit R_POS to do the relocations
5392 64 bit uses a 64 bit R_POS to do the relocations
5394 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5396 Which one is determined by the backend. */
5397 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5400 iraux
.x_csect
.x_scnlen
.l
= 8;
5402 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5405 iraux
.x_csect
.x_scnlen
.l
= 4;
5410 irel
->r_type
= R_POS
;
5411 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5412 ++osec
->reloc_count
;
5414 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5415 output_bfd
, irel
, NULL
, h
))
5418 /* We need to emit a symbol to define a csect which holds
5420 if (flinfo
->info
->strip
!= strip_all
)
5422 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
,
5424 &irsym
, h
->root
.root
.string
);
5428 irsym
.n_value
= irel
->r_vaddr
;
5429 irsym
.n_scnum
= osec
->target_index
;
5430 irsym
.n_sclass
= C_HIDEXT
;
5431 irsym
.n_type
= T_NULL
;
5434 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5435 outsym
+= bfd_coff_symesz (output_bfd
);
5437 /* Note : iraux is initialized above. */
5438 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5439 0, 1, (void *) outsym
);
5440 outsym
+= bfd_coff_auxesz (output_bfd
);
5444 /* We aren't going to write out the symbols below, so we
5445 need to write them out now. */
5446 pos
= obj_sym_filepos (output_bfd
);
5447 pos
+= (obj_raw_syment_count (output_bfd
)
5448 * bfd_coff_symesz (output_bfd
));
5449 amt
= outsym
- flinfo
->outsyms
;
5450 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5451 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5453 obj_raw_syment_count (output_bfd
) +=
5454 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5456 outsym
= flinfo
->outsyms
;
5461 /* If this symbol is a specially defined function descriptor, write
5462 it out. The first word is the address of the function code
5463 itself, the second word is the address of the TOC, and the third
5467 The addresses for the 32 bit will take 4 bytes and the addresses
5468 for 64 bit will take 8 bytes. Similar for the relocs. This type
5469 of logic was also done above to create a TOC entry in
5470 xcoff_write_global_symbol. */
5471 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5472 && h
->root
.type
== bfd_link_hash_defined
5473 && (h
->root
.u
.def
.section
5474 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
5480 struct xcoff_link_hash_entry
*hentry
;
5482 struct internal_reloc
*irel
;
5484 unsigned int reloc_size
, byte_size
;
5486 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5491 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5499 sec
= h
->root
.u
.def
.section
;
5500 osec
= sec
->output_section
;
5501 oindx
= osec
->target_index
;
5502 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5504 hentry
= h
->descriptor
;
5505 BFD_ASSERT (hentry
!= NULL
5506 && (hentry
->root
.type
== bfd_link_hash_defined
5507 || hentry
->root
.type
== bfd_link_hash_defweak
));
5508 esec
= hentry
->root
.u
.def
.section
;
5510 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5511 irel
->r_vaddr
= (osec
->vma
5512 + sec
->output_offset
5513 + h
->root
.u
.def
.value
);
5514 irel
->r_symndx
= esec
->output_section
->target_index
;
5515 irel
->r_type
= R_POS
;
5516 irel
->r_size
= reloc_size
;
5517 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5518 ++osec
->reloc_count
;
5520 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5521 output_bfd
, irel
, esec
, NULL
))
5524 /* There are three items to write out,
5525 the address of the code
5526 the address of the toc anchor
5527 the environment pointer.
5528 We are ignoring the environment pointer. So set it to zero. */
5529 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5531 bfd_put_64 (output_bfd
,
5532 (esec
->output_section
->vma
+ esec
->output_offset
5533 + hentry
->root
.u
.def
.value
),
5535 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5536 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5541 This logic was already called above so the error case where
5542 the backend is neither has already been checked. */
5543 bfd_put_32 (output_bfd
,
5544 (esec
->output_section
->vma
+ esec
->output_offset
5545 + hentry
->root
.u
.def
.value
),
5547 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5548 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5551 tsec
= coff_section_from_bfd_index (output_bfd
,
5552 xcoff_data (output_bfd
)->sntoc
);
5555 irel
->r_vaddr
= (osec
->vma
5556 + sec
->output_offset
5557 + h
->root
.u
.def
.value
5559 irel
->r_symndx
= tsec
->output_section
->target_index
;
5560 irel
->r_type
= R_POS
;
5561 irel
->r_size
= reloc_size
;
5562 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5563 ++osec
->reloc_count
;
5565 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5566 output_bfd
, irel
, tsec
, NULL
))
5570 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
5572 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5577 && (flinfo
->info
->strip
== strip_all
5578 || (flinfo
->info
->strip
== strip_some
5579 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
5580 FALSE
, FALSE
) == NULL
)))
5582 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5587 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5589 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5593 memset (&aux
, 0, sizeof aux
);
5595 h
->indx
= obj_raw_syment_count (output_bfd
);
5597 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->info
, flinfo
->strtab
,
5598 &isym
, h
->root
.root
.string
);
5602 if (h
->root
.type
== bfd_link_hash_undefined
5603 || h
->root
.type
== bfd_link_hash_undefweak
)
5606 isym
.n_scnum
= N_UNDEF
;
5607 if (h
->root
.type
== bfd_link_hash_undefweak
5608 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5609 isym
.n_sclass
= C_WEAKEXT
;
5611 isym
.n_sclass
= C_EXT
;
5612 aux
.x_csect
.x_smtyp
= XTY_ER
;
5614 else if ((h
->root
.type
== bfd_link_hash_defined
5615 || h
->root
.type
== bfd_link_hash_defweak
)
5616 && h
->smclas
== XMC_XO
)
5618 BFD_ASSERT (bfd_is_abs_symbol (&h
->root
));
5619 isym
.n_value
= h
->root
.u
.def
.value
;
5620 isym
.n_scnum
= N_UNDEF
;
5621 if (h
->root
.type
== bfd_link_hash_defweak
5622 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5623 isym
.n_sclass
= C_WEAKEXT
;
5625 isym
.n_sclass
= C_EXT
;
5626 aux
.x_csect
.x_smtyp
= XTY_ER
;
5628 else if (h
->root
.type
== bfd_link_hash_defined
5629 || h
->root
.type
== bfd_link_hash_defweak
)
5631 struct xcoff_link_size_list
*l
;
5633 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5634 + h
->root
.u
.def
.section
->output_offset
5635 + h
->root
.u
.def
.value
);
5636 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5637 isym
.n_scnum
= N_ABS
;
5639 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5640 isym
.n_sclass
= C_HIDEXT
;
5641 aux
.x_csect
.x_smtyp
= XTY_SD
;
5643 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5645 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
5651 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5657 else if (h
->root
.type
== bfd_link_hash_common
)
5659 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5660 + h
->root
.u
.c
.p
->section
->output_offset
);
5661 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5662 isym
.n_sclass
= C_EXT
;
5663 aux
.x_csect
.x_smtyp
= XTY_CM
;
5664 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5669 isym
.n_type
= T_NULL
;
5672 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5673 outsym
+= bfd_coff_symesz (output_bfd
);
5675 aux
.x_csect
.x_smclas
= h
->smclas
;
5676 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5678 outsym
+= bfd_coff_auxesz (output_bfd
);
5680 if ((h
->root
.type
== bfd_link_hash_defined
5681 || h
->root
.type
== bfd_link_hash_defweak
)
5682 && h
->smclas
!= XMC_XO
)
5684 /* We just output an SD symbol. Now output an LD symbol. */
5687 if (h
->root
.type
== bfd_link_hash_defweak
5688 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5689 isym
.n_sclass
= C_WEAKEXT
;
5691 isym
.n_sclass
= C_EXT
;
5692 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5693 outsym
+= bfd_coff_symesz (output_bfd
);
5695 aux
.x_csect
.x_smtyp
= XTY_LD
;
5696 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5697 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5699 outsym
+= bfd_coff_auxesz (output_bfd
);
5702 pos
= obj_sym_filepos (output_bfd
);
5703 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5704 amt
= outsym
- flinfo
->outsyms
;
5705 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5706 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5708 obj_raw_syment_count (output_bfd
) +=
5709 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5714 /* Handle a link order which is supposed to generate a reloc. */
5717 xcoff_reloc_link_order (bfd
*output_bfd
,
5718 struct xcoff_final_link_info
*flinfo
,
5719 asection
*output_section
,
5720 struct bfd_link_order
*link_order
)
5722 reloc_howto_type
*howto
;
5723 struct xcoff_link_hash_entry
*h
;
5727 struct internal_reloc
*irel
;
5728 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5730 if (link_order
->type
== bfd_section_reloc_link_order
)
5731 /* We need to somehow locate a symbol in the right section. The
5732 symbol must either have a value of zero, or we must adjust
5733 the addend by the value of the symbol. FIXME: Write this
5734 when we need it. The old linker couldn't handle this anyhow. */
5737 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5740 bfd_set_error (bfd_error_bad_value
);
5744 h
= ((struct xcoff_link_hash_entry
*)
5745 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
5746 link_order
->u
.reloc
.p
->u
.name
,
5747 FALSE
, FALSE
, TRUE
));
5750 (*flinfo
->info
->callbacks
->unattached_reloc
)
5751 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
5755 hsec
= xcoff_symbol_section (h
);
5756 if (h
->root
.type
== bfd_link_hash_defined
5757 || h
->root
.type
== bfd_link_hash_defweak
)
5758 hval
= h
->root
.u
.def
.value
;
5762 addend
= link_order
->u
.reloc
.p
->addend
;
5764 addend
+= (hsec
->output_section
->vma
5765 + hsec
->output_offset
5772 bfd_reloc_status_type rstat
;
5775 size
= bfd_get_reloc_size (howto
);
5776 buf
= bfd_zmalloc (size
);
5777 if (buf
== NULL
&& size
!= 0)
5780 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5786 case bfd_reloc_outofrange
:
5788 case bfd_reloc_overflow
:
5789 (*flinfo
->info
->callbacks
->reloc_overflow
)
5790 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5791 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0);
5794 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5795 (file_ptr
) link_order
->offset
, size
);
5801 /* Store the reloc information in the right place. It will get
5802 swapped and written out at the end of the final_link routine. */
5803 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
5804 + output_section
->reloc_count
);
5805 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
5806 + output_section
->reloc_count
);
5808 memset (irel
, 0, sizeof (struct internal_reloc
));
5809 *rel_hash_ptr
= NULL
;
5811 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5814 irel
->r_symndx
= h
->indx
;
5817 /* Set the index to -2 to force this symbol to get written out. */
5823 irel
->r_type
= howto
->type
;
5824 irel
->r_size
= howto
->bitsize
- 1;
5825 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5826 irel
->r_size
|= 0x80;
5828 ++output_section
->reloc_count
;
5830 /* Now output the reloc to the .loader section. */
5831 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
5833 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
5834 output_bfd
, irel
, hsec
, h
))
5841 /* Do the final link step. */
5844 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5846 bfd_size_type symesz
;
5847 struct xcoff_final_link_info flinfo
;
5849 struct bfd_link_order
*p
;
5850 bfd_size_type max_contents_size
;
5851 bfd_size_type max_sym_count
;
5852 bfd_size_type max_lineno_count
;
5853 bfd_size_type max_reloc_count
;
5854 bfd_size_type max_output_reloc_count
;
5855 file_ptr rel_filepos
;
5857 file_ptr line_filepos
;
5858 unsigned int linesz
;
5860 bfd_byte
*external_relocs
= NULL
;
5861 char strbuf
[STRING_SIZE_SIZE
];
5865 if (bfd_link_pic (info
))
5866 abfd
->flags
|= DYNAMIC
;
5868 symesz
= bfd_coff_symesz (abfd
);
5871 flinfo
.output_bfd
= abfd
;
5872 flinfo
.strtab
= NULL
;
5873 flinfo
.section_info
= NULL
;
5874 flinfo
.last_file_index
= -1;
5875 flinfo
.toc_symindx
= -1;
5876 flinfo
.internal_syms
= NULL
;
5877 flinfo
.sym_indices
= NULL
;
5878 flinfo
.outsyms
= NULL
;
5879 flinfo
.linenos
= NULL
;
5880 flinfo
.contents
= NULL
;
5881 flinfo
.external_relocs
= NULL
;
5883 if (xcoff_hash_table (info
)->loader_section
)
5885 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5886 + bfd_xcoff_ldhdrsz (abfd
));
5887 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5888 + bfd_xcoff_ldhdrsz (abfd
)
5889 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5890 * bfd_xcoff_ldsymsz (abfd
)));
5894 flinfo
.ldsym
= NULL
;
5895 flinfo
.ldrel
= NULL
;
5898 xcoff_data (abfd
)->coff
.link_info
= info
;
5900 flinfo
.strtab
= _bfd_stringtab_init ();
5901 if (flinfo
.strtab
== NULL
)
5904 /* Count the relocation entries required for the output file.
5905 (We've already counted the line numbers.) Determine a few
5907 max_contents_size
= 0;
5908 max_lineno_count
= 0;
5909 max_reloc_count
= 0;
5910 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5913 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5915 if (p
->type
== bfd_indirect_link_order
)
5919 sec
= p
->u
.indirect
.section
;
5921 /* Mark all sections which are to be included in the
5922 link. This will normally be every section. We need
5923 to do this so that we can identify any sections which
5924 the linker has decided to not include. */
5925 sec
->linker_mark
= TRUE
;
5927 o
->reloc_count
+= sec
->reloc_count
;
5929 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
5931 if (sec
->rawsize
> max_contents_size
)
5932 max_contents_size
= sec
->rawsize
;
5933 if (sec
->size
> max_contents_size
)
5934 max_contents_size
= sec
->size
;
5936 if (coff_section_data (sec
->owner
, sec
) != NULL
5937 && xcoff_section_data (sec
->owner
, sec
) != NULL
5938 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5939 > max_lineno_count
))
5941 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5942 if (sec
->reloc_count
> max_reloc_count
)
5943 max_reloc_count
= sec
->reloc_count
;
5945 else if (p
->type
== bfd_section_reloc_link_order
5946 || p
->type
== bfd_symbol_reloc_link_order
)
5951 /* Compute the file positions for all the sections. */
5952 if (abfd
->output_has_begun
)
5954 if (xcoff_hash_table (info
)->file_align
!= 0)
5961 file_align
= xcoff_hash_table (info
)->file_align
;
5962 if (file_align
!= 0)
5964 bfd_boolean saw_contents
;
5968 /* Insert .pad sections before every section which has
5969 contents and is loaded, if it is preceded by some other
5970 section which has contents and is loaded. */
5971 saw_contents
= TRUE
;
5972 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5974 if (strcmp (o
->name
, ".pad") == 0)
5975 saw_contents
= FALSE
;
5976 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5977 && (o
->flags
& SEC_LOAD
) != 0)
5980 saw_contents
= TRUE
;
5985 /* Create a pad section and place it before the section
5986 that needs padding. This requires unlinking and
5987 relinking the bfd's section list. */
5989 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
5991 n
->alignment_power
= 0;
5993 bfd_section_list_remove (abfd
, n
);
5994 bfd_section_list_insert_before (abfd
, o
, n
);
5995 saw_contents
= FALSE
;
6000 /* Reset the section indices after inserting the new
6003 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6006 o
->target_index
= indx
;
6008 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
6010 /* Work out appropriate sizes for the .pad sections to force
6011 each section to land on a page boundary. This bit of
6012 code knows what compute_section_file_positions is going
6014 sofar
= bfd_coff_filhsz (abfd
);
6015 sofar
+= bfd_coff_aoutsz (abfd
);
6016 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
6017 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6018 if ((bfd_xcoff_is_reloc_count_overflow
6019 (abfd
, (bfd_vma
) o
->reloc_count
))
6020 || (bfd_xcoff_is_lineno_count_overflow
6021 (abfd
, (bfd_vma
) o
->lineno_count
)))
6022 /* 64 does not overflow, need to check if 32 does */
6023 sofar
+= bfd_coff_scnhsz (abfd
);
6025 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6027 if (strcmp (o
->name
, ".pad") == 0)
6031 BFD_ASSERT (o
->size
== 0);
6032 pageoff
= sofar
& (file_align
- 1);
6035 o
->size
= file_align
- pageoff
;
6036 sofar
+= file_align
- pageoff
;
6037 o
->flags
|= SEC_HAS_CONTENTS
;
6042 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
6043 sofar
+= BFD_ALIGN (o
->size
,
6044 1 << o
->alignment_power
);
6049 if (! bfd_coff_compute_section_file_positions (abfd
))
6053 /* Allocate space for the pointers we need to keep for the relocs. */
6057 /* We use section_count + 1, rather than section_count, because
6058 the target_index fields are 1 based. */
6059 amt
= abfd
->section_count
+ 1;
6060 amt
*= sizeof (struct xcoff_link_section_info
);
6061 flinfo
.section_info
= bfd_malloc (amt
);
6062 if (flinfo
.section_info
== NULL
)
6064 for (i
= 0; i
<= abfd
->section_count
; i
++)
6066 flinfo
.section_info
[i
].relocs
= NULL
;
6067 flinfo
.section_info
[i
].rel_hashes
= NULL
;
6068 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6072 /* Set the file positions for the relocs. */
6073 rel_filepos
= obj_relocbase (abfd
);
6074 relsz
= bfd_coff_relsz (abfd
);
6075 max_output_reloc_count
= 0;
6076 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6078 if (o
->reloc_count
== 0)
6082 /* A stripped file has no relocs. However, we still
6083 allocate the buffers, so that later code doesn't have to
6084 worry about whether we are stripping or not. */
6085 if (info
->strip
== strip_all
)
6089 o
->flags
|= SEC_RELOC
;
6090 o
->rel_filepos
= rel_filepos
;
6091 rel_filepos
+= o
->reloc_count
* relsz
;
6094 /* We don't know the indices of global symbols until we have
6095 written out all the local symbols. For each section in
6096 the output file, we keep an array of pointers to hash
6097 table entries. Each entry in the array corresponds to a
6098 reloc. When we find a reloc against a global symbol, we
6099 set the corresponding entry in this array so that we can
6100 fix up the symbol index after we have written out all the
6103 Because of this problem, we also keep the relocs in
6104 memory until the end of the link. This wastes memory.
6105 We could backpatch the file later, I suppose, although it
6107 amt
= o
->reloc_count
;
6108 amt
*= sizeof (struct internal_reloc
);
6109 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6111 amt
= o
->reloc_count
;
6112 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6113 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6115 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
6116 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6119 if (o
->reloc_count
> max_output_reloc_count
)
6120 max_output_reloc_count
= o
->reloc_count
;
6124 /* We now know the size of the relocs, so we can determine the file
6125 positions of the line numbers. */
6126 line_filepos
= rel_filepos
;
6127 flinfo
.line_filepos
= line_filepos
;
6128 linesz
= bfd_coff_linesz (abfd
);
6129 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6131 if (o
->lineno_count
== 0)
6132 o
->line_filepos
= 0;
6135 o
->line_filepos
= line_filepos
;
6136 line_filepos
+= o
->lineno_count
* linesz
;
6139 /* Reset the reloc and lineno counts, so that we can use them to
6140 count the number of entries we have output so far. */
6142 o
->lineno_count
= 0;
6145 obj_sym_filepos (abfd
) = line_filepos
;
6147 /* Figure out the largest number of symbols in an input BFD. Take
6148 the opportunity to clear the output_has_begun fields of all the
6149 input BFD's. We want at least 6 symbols, since that is the
6150 number which xcoff_write_global_symbol may need. */
6152 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6156 sub
->output_has_begun
= FALSE
;
6157 sz
= obj_raw_syment_count (sub
);
6158 if (sz
> max_sym_count
)
6162 /* Allocate some buffers used while linking. */
6163 amt
= max_sym_count
* sizeof (struct internal_syment
);
6164 flinfo
.internal_syms
= bfd_malloc (amt
);
6166 amt
= max_sym_count
* sizeof (long);
6167 flinfo
.sym_indices
= bfd_malloc (amt
);
6169 amt
= (max_sym_count
+ 1) * symesz
;
6170 flinfo
.outsyms
= bfd_malloc (amt
);
6172 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6173 flinfo
.linenos
= bfd_malloc (amt
);
6175 amt
= max_contents_size
;
6176 flinfo
.contents
= bfd_malloc (amt
);
6178 amt
= max_reloc_count
* relsz
;
6179 flinfo
.external_relocs
= bfd_malloc (amt
);
6181 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6182 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6183 || flinfo
.outsyms
== NULL
6184 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
6185 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
6186 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6189 obj_raw_syment_count (abfd
) = 0;
6191 /* Find a TOC symbol, if we need one. */
6192 if (!xcoff_find_tc0 (abfd
, &flinfo
))
6195 /* We now know the position of everything in the file, except that
6196 we don't know the size of the symbol table and therefore we don't
6197 know where the string table starts. We just build the string
6198 table in memory as we go along. We process all the relocations
6199 for a single input file at once. */
6200 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6202 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6204 if (p
->type
== bfd_indirect_link_order
6205 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6207 sub
= p
->u
.indirect
.section
->owner
;
6208 if (! sub
->output_has_begun
)
6210 if (! xcoff_link_input_bfd (&flinfo
, sub
))
6212 sub
->output_has_begun
= TRUE
;
6215 else if (p
->type
== bfd_section_reloc_link_order
6216 || p
->type
== bfd_symbol_reloc_link_order
)
6218 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
6223 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6229 /* Free up the buffers used by xcoff_link_input_bfd. */
6230 free (flinfo
.internal_syms
);
6231 flinfo
.internal_syms
= NULL
;
6232 free (flinfo
.sym_indices
);
6233 flinfo
.sym_indices
= NULL
;
6234 free (flinfo
.linenos
);
6235 flinfo
.linenos
= NULL
;
6236 free (flinfo
.contents
);
6237 flinfo
.contents
= NULL
;
6238 free (flinfo
.external_relocs
);
6239 flinfo
.external_relocs
= NULL
;
6241 /* The value of the last C_FILE symbol is supposed to be -1. Write
6243 if (flinfo
.last_file_index
!= -1)
6245 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
6246 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
6247 (void *) flinfo
.outsyms
);
6248 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
6249 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6250 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
6254 /* Write out all the global symbols which do not come from XCOFF
6256 bfd_hash_traverse (&info
->hash
->table
, xcoff_write_global_symbol
, &flinfo
);
6258 free (flinfo
.outsyms
);
6259 flinfo
.outsyms
= NULL
;
6261 /* Now that we have written out all the global symbols, we know the
6262 symbol indices to use for relocs against them, and we can finally
6263 write out the relocs. */
6264 amt
= max_output_reloc_count
* relsz
;
6265 external_relocs
= bfd_malloc (amt
);
6266 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6269 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6271 struct internal_reloc
*irel
;
6272 struct internal_reloc
*irelend
;
6273 struct xcoff_link_hash_entry
**rel_hash
;
6274 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6276 bfd_size_type rel_size
;
6278 /* A stripped file has no relocs. */
6279 if (info
->strip
== strip_all
)
6285 if (o
->reloc_count
== 0)
6288 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6289 irelend
= irel
+ o
->reloc_count
;
6290 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
6291 for (; irel
< irelend
; irel
++, rel_hash
++)
6293 if (*rel_hash
!= NULL
)
6295 if ((*rel_hash
)->indx
< 0)
6297 (*info
->callbacks
->unattached_reloc
)
6298 (info
, (*rel_hash
)->root
.root
.string
,
6299 NULL
, o
, irel
->r_vaddr
);
6300 (*rel_hash
)->indx
= 0;
6302 irel
->r_symndx
= (*rel_hash
)->indx
;
6306 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6307 toc_rel_hash
!= NULL
;
6308 toc_rel_hash
= toc_rel_hash
->next
)
6310 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6312 (*info
->callbacks
->unattached_reloc
)
6313 (info
, toc_rel_hash
->h
->root
.root
.string
,
6314 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
);
6315 toc_rel_hash
->h
->u
.toc_indx
= 0;
6317 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6320 /* XCOFF requires that the relocs be sorted by address. We tend
6321 to produce them in the order in which their containing csects
6322 appear in the symbol table, which is not necessarily by
6323 address. So we sort them here. There may be a better way to
6325 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
6326 o
->reloc_count
, sizeof (struct internal_reloc
),
6329 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6330 irelend
= irel
+ o
->reloc_count
;
6331 erel
= external_relocs
;
6332 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6333 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6335 rel_size
= relsz
* o
->reloc_count
;
6336 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6337 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6341 free (external_relocs
);
6342 external_relocs
= NULL
;
6344 /* Free up the section information. */
6345 if (flinfo
.section_info
!= NULL
)
6349 for (i
= 0; i
< abfd
->section_count
; i
++)
6351 free (flinfo
.section_info
[i
].relocs
);
6352 free (flinfo
.section_info
[i
].rel_hashes
);
6354 free (flinfo
.section_info
);
6355 flinfo
.section_info
= NULL
;
6358 /* Write out the loader section contents. */
6359 o
= xcoff_hash_table (info
)->loader_section
;
6362 && o
->output_section
!= bfd_abs_section_ptr
)
6364 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
6365 == (xcoff_hash_table (info
)->loader_section
->contents
6366 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6367 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6368 (file_ptr
) o
->output_offset
, o
->size
))
6372 /* Write out the magic sections. */
6373 o
= xcoff_hash_table (info
)->linkage_section
;
6376 && o
->output_section
!= bfd_abs_section_ptr
6377 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6378 (file_ptr
) o
->output_offset
,
6381 o
= xcoff_hash_table (info
)->toc_section
;
6384 && o
->output_section
!= bfd_abs_section_ptr
6385 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6386 (file_ptr
) o
->output_offset
,
6389 o
= xcoff_hash_table (info
)->descriptor_section
;
6392 && o
->output_section
!= bfd_abs_section_ptr
6393 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6394 (file_ptr
) o
->output_offset
,
6398 /* Write out the string table. */
6399 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6400 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6403 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
6405 amt
= STRING_SIZE_SIZE
;
6406 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6408 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
6411 _bfd_stringtab_free (flinfo
.strtab
);
6413 /* Write out the debugging string table. */
6414 o
= xcoff_hash_table (info
)->debug_section
;
6417 && o
->output_section
!= bfd_abs_section_ptr
)
6419 struct bfd_strtab_hash
*debug_strtab
;
6421 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6422 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6423 >= _bfd_stringtab_size (debug_strtab
));
6424 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6425 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6427 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6431 /* Setting symcount to 0 will cause write_object_contents to
6432 not try to write out the symbols. */
6438 if (flinfo
.strtab
!= NULL
)
6439 _bfd_stringtab_free (flinfo
.strtab
);
6441 if (flinfo
.section_info
!= NULL
)
6445 for (i
= 0; i
< abfd
->section_count
; i
++)
6447 free (flinfo
.section_info
[i
].relocs
);
6448 free (flinfo
.section_info
[i
].rel_hashes
);
6450 free (flinfo
.section_info
);
6453 free (flinfo
.internal_syms
);
6454 free (flinfo
.sym_indices
);
6455 free (flinfo
.outsyms
);
6456 free (flinfo
.linenos
);
6457 free (flinfo
.contents
);
6458 free (flinfo
.external_relocs
);
6459 free (external_relocs
);