1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
31 #include "libiberty.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. */
91 struct xcoff_link_hash_table
93 struct bfd_link_hash_table root
;
95 /* The .debug string hash table. We need to compute this while
96 reading the input files, so that we know how large the .debug
97 section will be before we assign section positions. */
98 struct bfd_strtab_hash
*debug_strtab
;
100 /* The .debug section we will use for the final output. */
101 asection
*debug_section
;
103 /* The .loader section we will use for the final output. */
104 asection
*loader_section
;
106 /* A count of non TOC relative relocs which will need to be
107 allocated in the .loader section. */
110 /* The .loader section header. */
111 struct internal_ldhdr ldhdr
;
113 /* The .gl section we use to hold global linkage code. */
114 asection
*linkage_section
;
116 /* The .tc section we use to hold toc entries we build for global
118 asection
*toc_section
;
120 /* The .ds section we use to hold function descriptors which we
121 create for exported symbols. */
122 asection
*descriptor_section
;
124 /* The list of import files. */
125 struct xcoff_import_file
*imports
;
127 /* Required alignment of sections within the output file. */
128 unsigned long file_align
;
130 /* Whether the .text section must be read-only. */
133 /* Whether -brtl was specified. */
136 /* Whether garbage collection was done. */
139 /* A linked list of symbols for which we have size information. */
140 struct xcoff_link_size_list
142 struct xcoff_link_size_list
*next
;
143 struct xcoff_link_hash_entry
*h
;
148 /* Information about archives. */
151 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
152 asection
*special_sections
[XCOFF_NUMBER_OF_SPECIAL_SECTIONS
];
155 /* Information that we pass around while doing the final link step. */
157 struct xcoff_final_link_info
159 /* General link information. */
160 struct bfd_link_info
*info
;
163 /* Hash table for long symbol names. */
164 struct bfd_strtab_hash
*strtab
;
165 /* Array of information kept for each output section, indexed by the
166 target_index field. */
167 struct xcoff_link_section_info
*section_info
;
168 /* Symbol index of last C_FILE symbol (-1 if none). */
169 long last_file_index
;
170 /* Contents of last C_FILE symbol. */
171 struct internal_syment last_file
;
172 /* Symbol index of TOC symbol. */
174 /* Start of .loader symbols. */
176 /* Next .loader reloc to swap out. */
178 /* File position of start of line numbers. */
179 file_ptr line_filepos
;
180 /* Buffer large enough to hold swapped symbols of any input file. */
181 struct internal_syment
*internal_syms
;
182 /* Buffer large enough to hold output indices of symbols of any
185 /* Buffer large enough to hold output symbols for any input file. */
187 /* Buffer large enough to hold external line numbers for any input
190 /* Buffer large enough to hold any input section. */
192 /* Buffer large enough to hold external relocs of any input section. */
193 bfd_byte
*external_relocs
;
196 static bfd_boolean
xcoff_mark (struct bfd_link_info
*, asection
*);
200 /* Routines to read XCOFF dynamic information. This don't really
201 belong here, but we already have the ldsym manipulation routines
204 /* Read the contents of a section. */
207 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
209 if (coff_section_data (abfd
, sec
) == NULL
)
211 bfd_size_type amt
= sizeof (struct coff_section_tdata
);
213 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
214 if (sec
->used_by_bfd
== NULL
)
218 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
222 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
224 if (contents
!= NULL
)
228 coff_section_data (abfd
, sec
)->contents
= contents
;
234 /* Get the size required to hold the dynamic symbols. */
237 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
241 struct internal_ldhdr ldhdr
;
243 if ((abfd
->flags
& DYNAMIC
) == 0)
245 bfd_set_error (bfd_error_invalid_operation
);
249 lsec
= bfd_get_section_by_name (abfd
, ".loader");
252 bfd_set_error (bfd_error_no_symbols
);
256 if (! xcoff_get_section_contents (abfd
, lsec
))
258 contents
= coff_section_data (abfd
, lsec
)->contents
;
260 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
262 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
265 /* Get the dynamic symbols. */
268 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
272 struct internal_ldhdr ldhdr
;
274 bfd_byte
*elsym
, *elsymend
;
275 coff_symbol_type
*symbuf
;
277 if ((abfd
->flags
& DYNAMIC
) == 0)
279 bfd_set_error (bfd_error_invalid_operation
);
283 lsec
= bfd_get_section_by_name (abfd
, ".loader");
286 bfd_set_error (bfd_error_no_symbols
);
290 if (! xcoff_get_section_contents (abfd
, lsec
))
292 contents
= coff_section_data (abfd
, lsec
)->contents
;
294 coff_section_data (abfd
, lsec
)->keep_contents
= TRUE
;
296 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
298 strings
= (char *) contents
+ ldhdr
.l_stoff
;
300 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
304 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
306 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
307 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
309 struct internal_ldsym ldsym
;
311 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
313 symbuf
->symbol
.the_bfd
= abfd
;
315 if (ldsym
._l
._l_l
._l_zeroes
== 0)
316 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
321 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
324 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
326 symbuf
->symbol
.name
= c
;
329 if (ldsym
.l_smclas
== XMC_XO
)
330 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
332 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
334 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
336 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
337 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
339 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
340 symbuf
->symbol
.flags
|= BSF_WEAK
;
342 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
345 /* FIXME: We have no way to record the other information stored
346 with the loader symbol. */
347 *psyms
= (asymbol
*) symbuf
;
352 return ldhdr
.l_nsyms
;
355 /* Get the size required to hold the dynamic relocs. */
358 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
362 struct internal_ldhdr ldhdr
;
364 if ((abfd
->flags
& DYNAMIC
) == 0)
366 bfd_set_error (bfd_error_invalid_operation
);
370 lsec
= bfd_get_section_by_name (abfd
, ".loader");
373 bfd_set_error (bfd_error_no_symbols
);
377 if (! xcoff_get_section_contents (abfd
, lsec
))
379 contents
= coff_section_data (abfd
, lsec
)->contents
;
381 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
383 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
386 /* Get the dynamic relocs. */
389 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
395 struct internal_ldhdr ldhdr
;
397 bfd_byte
*elrel
, *elrelend
;
399 if ((abfd
->flags
& DYNAMIC
) == 0)
401 bfd_set_error (bfd_error_invalid_operation
);
405 lsec
= bfd_get_section_by_name (abfd
, ".loader");
408 bfd_set_error (bfd_error_no_symbols
);
412 if (! xcoff_get_section_contents (abfd
, lsec
))
414 contents
= coff_section_data (abfd
, lsec
)->contents
;
416 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
418 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
422 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
424 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
425 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
428 struct internal_ldrel ldrel
;
430 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
432 if (ldrel
.l_symndx
>= 3)
433 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
439 switch (ldrel
.l_symndx
)
455 sec
= bfd_get_section_by_name (abfd
, name
);
458 bfd_set_error (bfd_error_bad_value
);
462 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
465 relbuf
->address
= ldrel
.l_vaddr
;
468 /* Most dynamic relocs have the same type. FIXME: This is only
469 correct if ldrel.l_rtype == 0. In other cases, we should use
470 a different howto. */
471 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
473 /* FIXME: We have no way to record the l_rsecnm field. */
480 return ldhdr
.l_nreloc
;
483 /* Hash functions for xcoff_link_hash_table's archive_info. */
486 xcoff_archive_info_hash (const void *data
)
488 const struct xcoff_archive_info
*info
;
490 info
= (const struct xcoff_archive_info
*) data
;
491 return htab_hash_pointer (info
->archive
);
495 xcoff_archive_info_eq (const void *data1
, const void *data2
)
497 const struct xcoff_archive_info
*info1
;
498 const struct xcoff_archive_info
*info2
;
500 info1
= (const struct xcoff_archive_info
*) data1
;
501 info2
= (const struct xcoff_archive_info
*) data2
;
502 return info1
->archive
== info2
->archive
;
505 /* Return information about archive ARCHIVE. Return NULL on error. */
507 static struct xcoff_archive_info
*
508 xcoff_get_archive_info (struct bfd_link_info
*info
, bfd
*archive
)
510 struct xcoff_link_hash_table
*htab
;
511 struct xcoff_archive_info
*entryp
, entry
;
514 htab
= xcoff_hash_table (info
);
515 entry
.archive
= archive
;
516 slot
= htab_find_slot (htab
->archive_info
, &entry
, INSERT
);
523 entryp
= bfd_zalloc (archive
, sizeof (entry
));
527 entryp
->archive
= archive
;
533 /* Routine to create an entry in an XCOFF link hash table. */
535 static struct bfd_hash_entry
*
536 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
537 struct bfd_hash_table
*table
,
540 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
542 /* Allocate the structure if it has not already been allocated by a
545 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
549 /* Call the allocation method of the superclass. */
550 ret
= ((struct xcoff_link_hash_entry
*)
551 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
555 /* Set local fields. */
557 ret
->toc_section
= NULL
;
558 ret
->u
.toc_indx
= -1;
559 ret
->descriptor
= NULL
;
563 ret
->smclas
= XMC_UA
;
566 return (struct bfd_hash_entry
*) ret
;
569 /* Create a XCOFF link hash table. */
571 struct bfd_link_hash_table
*
572 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
574 struct xcoff_link_hash_table
*ret
;
575 bfd_size_type amt
= sizeof (* ret
);
577 ret
= bfd_malloc (amt
);
580 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
581 sizeof (struct xcoff_link_hash_entry
)))
587 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
588 ret
->debug_section
= NULL
;
589 ret
->loader_section
= NULL
;
590 ret
->ldrel_count
= 0;
591 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
592 ret
->linkage_section
= NULL
;
593 ret
->toc_section
= NULL
;
594 ret
->descriptor_section
= NULL
;
599 ret
->archive_info
= htab_create (37, xcoff_archive_info_hash
,
600 xcoff_archive_info_eq
, NULL
);
601 memset (ret
->special_sections
, 0, sizeof ret
->special_sections
);
603 /* The linker will always generate a full a.out header. We need to
604 record that fact now, before the sizeof_headers routine could be
606 xcoff_data (abfd
)->full_aouthdr
= TRUE
;
611 /* Free a XCOFF link hash table. */
614 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table
*hash
)
616 struct xcoff_link_hash_table
*ret
= (struct xcoff_link_hash_table
*) hash
;
618 _bfd_stringtab_free (ret
->debug_strtab
);
619 bfd_hash_table_free (&ret
->root
.table
);
623 /* Read internal relocs for an XCOFF csect. This is a wrapper around
624 _bfd_coff_read_internal_relocs which tries to take advantage of any
625 relocs which may have been cached for the enclosing section. */
627 static struct internal_reloc
*
628 xcoff_read_internal_relocs (bfd
*abfd
,
631 bfd_byte
*external_relocs
,
632 bfd_boolean require_internal
,
633 struct internal_reloc
*internal_relocs
)
635 if (coff_section_data (abfd
, sec
) != NULL
636 && coff_section_data (abfd
, sec
)->relocs
== NULL
637 && xcoff_section_data (abfd
, sec
) != NULL
)
641 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
643 if (enclosing
!= NULL
644 && (coff_section_data (abfd
, enclosing
) == NULL
645 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
647 && enclosing
->reloc_count
> 0)
649 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, TRUE
,
650 external_relocs
, FALSE
, NULL
)
655 if (enclosing
!= NULL
656 && coff_section_data (abfd
, enclosing
) != NULL
657 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
661 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
662 / bfd_coff_relsz (abfd
));
664 if (! require_internal
)
665 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
666 memcpy (internal_relocs
,
667 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
668 sec
->reloc_count
* sizeof (struct internal_reloc
));
669 return internal_relocs
;
673 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
674 require_internal
, internal_relocs
);
677 /* Split FILENAME into an import path and an import filename,
678 storing them in *IMPPATH and *IMPFILE respectively. */
681 bfd_xcoff_split_import_path (bfd
*abfd
, const char *filename
,
682 const char **imppath
, const char **impfile
)
684 const char *basename
;
688 basename
= lbasename (filename
);
689 length
= basename
- filename
;
691 /* The filename has no directory component, so use an empty path. */
693 else if (length
== 1)
694 /* The filename is in the root directory. */
698 /* Extract the (non-empty) directory part. Note that we don't
699 need to strip duplicate directory separators from any part
700 of the string; the native linker doesn't do that either. */
701 path
= bfd_alloc (abfd
, length
);
704 memcpy (path
, filename
, length
- 1);
705 path
[length
- 1] = 0;
712 /* Set ARCHIVE's import path as though its filename had been given
716 bfd_xcoff_set_archive_import_path (struct bfd_link_info
*info
,
717 bfd
*archive
, const char *filename
)
719 struct xcoff_archive_info
*archive_info
;
721 archive_info
= xcoff_get_archive_info (info
, archive
);
722 return (archive_info
!= NULL
723 && bfd_xcoff_split_import_path (archive
, filename
,
724 &archive_info
->imppath
,
725 &archive_info
->impfile
));
728 /* H is an imported symbol. Set the import module's path, file and member
729 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
730 no specific import module is specified. */
733 xcoff_set_import_path (struct bfd_link_info
*info
,
734 struct xcoff_link_hash_entry
*h
,
735 const char *imppath
, const char *impfile
,
736 const char *impmember
)
739 struct xcoff_import_file
**pp
;
741 /* We overload the ldindx field to hold the l_ifile value for this
743 BFD_ASSERT (h
->ldsym
== NULL
);
744 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
749 /* We start c at 1 because the first entry in the import list is
750 reserved for the library search path. */
751 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
753 pp
= &(*pp
)->next
, ++c
)
755 if (strcmp ((*pp
)->path
, imppath
) == 0
756 && strcmp ((*pp
)->file
, impfile
) == 0
757 && strcmp ((*pp
)->member
, impmember
) == 0)
763 struct xcoff_import_file
*n
;
764 bfd_size_type amt
= sizeof (* n
);
766 n
= bfd_alloc (info
->output_bfd
, amt
);
772 n
->member
= impmember
;
780 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
781 Return true if LDSYM defines H. */
784 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry
*h
,
785 struct internal_ldsym
*ldsym
)
787 /* If we didn't know about H before processing LDSYM, LDSYM
788 definitely defines H. */
789 if (h
->root
.type
== bfd_link_hash_new
)
792 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
793 dynamic symbol, LDSYM trumps the current definition of H. */
794 if ((ldsym
->l_smtype
& L_WEAK
) == 0
795 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
796 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
797 && (h
->root
.type
== bfd_link_hash_defweak
798 || h
->root
.type
== bfd_link_hash_undefweak
))
801 /* If H is currently undefined, LDSYM defines it. */
802 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
803 && (h
->root
.type
== bfd_link_hash_undefined
804 || h
->root
.type
== bfd_link_hash_undefweak
))
810 /* This function is used to add symbols from a dynamic object to the
811 global symbol table. */
814 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
818 struct internal_ldhdr ldhdr
;
820 bfd_byte
*elsym
, *elsymend
;
821 struct xcoff_import_file
*n
;
823 struct xcoff_import_file
**pp
;
825 /* We can only handle a dynamic object if we are generating an XCOFF
827 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
829 (*_bfd_error_handler
)
830 (_("%s: XCOFF shared object when not producing XCOFF output"),
831 bfd_get_filename (abfd
));
832 bfd_set_error (bfd_error_invalid_operation
);
836 /* The symbols we use from a dynamic object are not the symbols in
837 the normal symbol table, but, rather, the symbols in the export
838 table. If there is a global symbol in a dynamic object which is
839 not in the export table, the loader will not be able to find it,
840 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
841 libc.a has symbols in the export table which are not in the
844 /* Read in the .loader section. FIXME: We should really use the
845 o_snloader field in the a.out header, rather than grabbing the
847 lsec
= bfd_get_section_by_name (abfd
, ".loader");
850 (*_bfd_error_handler
)
851 (_("%s: dynamic object with no .loader section"),
852 bfd_get_filename (abfd
));
853 bfd_set_error (bfd_error_no_symbols
);
857 if (! xcoff_get_section_contents (abfd
, lsec
))
859 contents
= coff_section_data (abfd
, lsec
)->contents
;
861 /* Remove the sections from this object, so that they do not get
862 included in the link. */
863 bfd_section_list_clear (abfd
);
865 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
867 strings
= (char *) contents
+ ldhdr
.l_stoff
;
869 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
871 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
873 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
875 struct internal_ldsym ldsym
;
876 char nambuf
[SYMNMLEN
+ 1];
878 struct xcoff_link_hash_entry
*h
;
880 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
882 /* We are only interested in exported symbols. */
883 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
886 if (ldsym
._l
._l_l
._l_zeroes
== 0)
887 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
890 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
891 nambuf
[SYMNMLEN
] = '\0';
895 /* Normally we could not call xcoff_link_hash_lookup in an add
896 symbols routine, since we might not be using an XCOFF hash
897 table. However, we verified above that we are using an XCOFF
900 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
,
905 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
908 h
->flags
|= XCOFF_DEF_DYNAMIC
;
909 h
->smclas
= ldsym
.l_smclas
;
910 if (h
->smclas
== XMC_XO
)
912 /* This symbol has an absolute value. */
913 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
914 h
->root
.type
= bfd_link_hash_defweak
;
916 h
->root
.type
= bfd_link_hash_defined
;
917 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
918 h
->root
.u
.def
.value
= ldsym
.l_value
;
922 /* Otherwise, we don't bother to actually define the symbol,
923 since we don't have a section to put it in anyhow.
924 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
925 should be imported from the symbol's undef.abfd. */
926 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
927 h
->root
.type
= bfd_link_hash_undefweak
;
929 h
->root
.type
= bfd_link_hash_undefined
;
930 h
->root
.u
.undef
.abfd
= abfd
;
933 /* If this symbol defines a function descriptor, then it
934 implicitly defines the function code as well. */
935 if (h
->smclas
== XMC_DS
936 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
937 h
->flags
|= XCOFF_DESCRIPTOR
;
938 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
940 struct xcoff_link_hash_entry
*hds
;
947 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
951 strcpy (dsnm
+ 1, name
);
952 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
962 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
964 hds
->root
.type
= h
->root
.type
;
965 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
966 if (h
->smclas
== XMC_XO
)
968 /* An absolute symbol appears to actually define code, not a
969 function descriptor. This is how some math functions are
970 implemented on AIX 4.1. */
971 hds
->smclas
= XMC_XO
;
972 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
973 hds
->root
.u
.def
.value
= ldsym
.l_value
;
977 hds
->smclas
= XMC_PR
;
978 hds
->root
.u
.undef
.abfd
= abfd
;
979 /* We do not want to add this to the undefined
986 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
988 free (coff_section_data (abfd
, lsec
)->contents
);
989 coff_section_data (abfd
, lsec
)->contents
= NULL
;
992 /* Record this file in the import files. */
993 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
998 if (abfd
->my_archive
== NULL
)
1000 if (!bfd_xcoff_split_import_path (abfd
, abfd
->filename
,
1001 &n
->path
, &n
->file
))
1007 struct xcoff_archive_info
*archive_info
;
1009 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1010 if (!archive_info
->impfile
)
1012 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1013 archive_info
->archive
->filename
,
1014 &archive_info
->imppath
,
1015 &archive_info
->impfile
))
1018 n
->path
= archive_info
->imppath
;
1019 n
->file
= archive_info
->impfile
;
1020 n
->member
= bfd_get_filename (abfd
);
1023 /* We start c at 1 because the first import file number is reserved
1025 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1027 pp
= &(*pp
)->next
, ++c
)
1031 xcoff_data (abfd
)->import_file_id
= c
;
1036 /* xcoff_link_create_extra_sections
1038 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1041 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1043 bfd_boolean return_value
= FALSE
;
1045 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1047 /* We need to build a .loader section, so we do it here. This
1048 won't work if we're producing an XCOFF output file with no
1049 XCOFF input files. FIXME. */
1051 if (!info
->relocatable
1052 && xcoff_hash_table (info
)->loader_section
== NULL
)
1055 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1057 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1061 xcoff_hash_table (info
)->loader_section
= lsec
;
1064 /* Likewise for the linkage section. */
1065 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1068 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1071 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1075 xcoff_hash_table (info
)->linkage_section
= lsec
;
1076 lsec
->alignment_power
= 2;
1079 /* Likewise for the TOC section. */
1080 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1083 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1086 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1090 xcoff_hash_table (info
)->toc_section
= tsec
;
1091 tsec
->alignment_power
= 2;
1094 /* Likewise for the descriptor section. */
1095 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1098 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1101 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1105 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1106 dsec
->alignment_power
= 2;
1109 /* Likewise for the .debug section. */
1110 if (xcoff_hash_table (info
)->debug_section
== NULL
1111 && info
->strip
!= strip_all
)
1114 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1116 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1120 xcoff_hash_table (info
)->debug_section
= dsec
;
1124 return_value
= TRUE
;
1128 return return_value
;
1131 /* Returns the index of reloc in RELOCS with the least address greater
1132 than or equal to ADDRESS. The relocs are sorted by address. */
1134 static bfd_size_type
1135 xcoff_find_reloc (struct internal_reloc
*relocs
,
1136 bfd_size_type count
,
1139 bfd_size_type min
, max
, this;
1143 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1152 /* Do a binary search over (min,max]. */
1153 while (min
+ 1 < max
)
1157 this = (max
+ min
) / 2;
1158 raddr
= relocs
[this].r_vaddr
;
1159 if (raddr
> address
)
1161 else if (raddr
< address
)
1170 if (relocs
[min
].r_vaddr
< address
)
1174 && relocs
[min
- 1].r_vaddr
== address
)
1180 /* Add all the symbols from an object file to the hash table.
1182 XCOFF is a weird format. A normal XCOFF .o files will have three
1183 COFF sections--.text, .data, and .bss--but each COFF section will
1184 contain many csects. These csects are described in the symbol
1185 table. From the linker's point of view, each csect must be
1186 considered a section in its own right. For example, a TOC entry is
1187 handled as a small XMC_TC csect. The linker must be able to merge
1188 different TOC entries together, which means that it must be able to
1189 extract the XMC_TC csects from the .data section of the input .o
1192 From the point of view of our linker, this is, of course, a hideous
1193 nightmare. We cope by actually creating sections for each csect,
1194 and discarding the original sections. We then have to handle the
1195 relocation entries carefully, since the only way to tell which
1196 csect they belong to is to examine the address. */
1199 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1201 unsigned int n_tmask
;
1202 unsigned int n_btshft
;
1203 bfd_boolean default_copy
;
1204 bfd_size_type symcount
;
1205 struct xcoff_link_hash_entry
**sym_hash
;
1206 asection
**csect_cache
;
1207 unsigned int *lineno_counts
;
1208 bfd_size_type linesz
;
1210 asection
*last_real
;
1211 bfd_boolean keep_syms
;
1213 unsigned int csect_index
;
1214 asection
*first_csect
;
1215 bfd_size_type symesz
;
1218 struct reloc_info_struct
1220 struct internal_reloc
*relocs
;
1223 } *reloc_info
= NULL
;
1226 keep_syms
= obj_coff_keep_syms (abfd
);
1228 if ((abfd
->flags
& DYNAMIC
) != 0
1229 && ! info
->static_link
)
1231 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1235 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1236 if (! xcoff_link_create_extra_sections (abfd
, info
))
1239 if ((abfd
->flags
& DYNAMIC
) != 0
1240 && ! info
->static_link
)
1243 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1244 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1246 /* Define macros so that ISFCN, et. al., macros work correctly. */
1247 #define N_TMASK n_tmask
1248 #define N_BTSHFT n_btshft
1250 if (info
->keep_memory
)
1251 default_copy
= FALSE
;
1253 default_copy
= TRUE
;
1255 symcount
= obj_raw_syment_count (abfd
);
1257 /* We keep a list of the linker hash table entries that correspond
1258 to each external symbol. */
1259 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1260 sym_hash
= bfd_zalloc (abfd
, amt
);
1261 if (sym_hash
== NULL
&& symcount
!= 0)
1263 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1265 /* Because of the weird stuff we are doing with XCOFF csects, we can
1266 not easily determine which section a symbol is in, so we store
1267 the information in the tdata for the input file. */
1268 amt
= symcount
* sizeof (asection
*);
1269 csect_cache
= bfd_zalloc (abfd
, amt
);
1270 if (csect_cache
== NULL
&& symcount
!= 0)
1272 xcoff_data (abfd
)->csects
= csect_cache
;
1274 /* We garbage-collect line-number information on a symbol-by-symbol
1275 basis, so we need to have quick access to the number of entries
1277 amt
= symcount
* sizeof (unsigned int);
1278 lineno_counts
= bfd_zalloc (abfd
, amt
);
1279 if (lineno_counts
== NULL
&& symcount
!= 0)
1281 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1283 /* While splitting sections into csects, we need to assign the
1284 relocs correctly. The relocs and the csects must both be in
1285 order by VMA within a given section, so we handle this by
1286 scanning along the relocs as we process the csects. We index
1287 into reloc_info using the section target_index. */
1288 amt
= abfd
->section_count
+ 1;
1289 amt
*= sizeof (struct reloc_info_struct
);
1290 reloc_info
= bfd_zmalloc (amt
);
1291 if (reloc_info
== NULL
)
1294 /* Read in the relocs and line numbers for each section. */
1295 linesz
= bfd_coff_linesz (abfd
);
1297 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1301 if ((o
->flags
& SEC_RELOC
) != 0)
1303 reloc_info
[o
->target_index
].relocs
=
1304 xcoff_read_internal_relocs (abfd
, o
, TRUE
, NULL
, FALSE
, NULL
);
1305 amt
= o
->reloc_count
;
1306 amt
*= sizeof (asection
*);
1307 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1308 if (reloc_info
[o
->target_index
].csects
== NULL
)
1312 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1313 && o
->lineno_count
> 0)
1317 amt
= linesz
* o
->lineno_count
;
1318 linenos
= bfd_malloc (amt
);
1319 if (linenos
== NULL
)
1321 reloc_info
[o
->target_index
].linenos
= linenos
;
1322 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1323 || bfd_bread (linenos
, amt
, abfd
) != amt
)
1328 /* Don't let the linker relocation routines discard the symbols. */
1329 obj_coff_keep_syms (abfd
) = TRUE
;
1335 symesz
= bfd_coff_symesz (abfd
);
1336 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1337 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1338 esym_end
= esym
+ symcount
* symesz
;
1340 while (esym
< esym_end
)
1342 struct internal_syment sym
;
1343 union internal_auxent aux
;
1345 char buf
[SYMNMLEN
+ 1];
1349 struct xcoff_link_hash_entry
*set_toc
;
1351 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1353 /* In this pass we are only interested in symbols with csect
1355 if (!CSECT_SYM_P (sym
.n_sclass
))
1358 Normally csect is a .pr, .rw etc. created in the loop
1359 If C_FILE or first time, handle special
1361 Advance esym, sym_hash, csect_hash ptrs. */
1362 if (sym
.n_sclass
== C_FILE
)
1365 *csect_cache
= csect
;
1366 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1367 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1369 *csect_cache
= NULL
;
1370 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1371 sym_hash
+= sym
.n_numaux
+ 1;
1372 csect_cache
+= sym
.n_numaux
+ 1;
1373 lineno_counts
+= sym
.n_numaux
+ 1;
1378 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1383 /* If this symbol has line number information attached to it,
1384 and we're not stripping it, count the number of entries and
1385 add them to the count for this csect. In the final link pass
1386 we are going to attach line number information by symbol,
1387 rather than by section, in order to more easily handle
1388 garbage collection. */
1389 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1392 && ISFCN (sym
.n_type
))
1394 union internal_auxent auxlin
;
1396 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1397 sym
.n_type
, sym
.n_sclass
,
1398 0, sym
.n_numaux
, (void *) &auxlin
);
1400 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1402 asection
*enclosing
;
1403 bfd_signed_vma linoff
;
1405 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1406 if (enclosing
== NULL
)
1408 (*_bfd_error_handler
)
1409 (_("%B: `%s' has line numbers but no enclosing section"),
1411 bfd_set_error (bfd_error_bad_value
);
1414 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1415 - enclosing
->line_filepos
);
1416 /* Explicit cast to bfd_signed_vma for compiler. */
1417 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1419 struct internal_lineno lin
;
1420 bfd_byte
*linpstart
;
1422 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1424 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1426 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1428 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1431 bfd_byte
*linpend
, *linp
;
1433 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1434 + enclosing
->lineno_count
* linesz
);
1435 for (linp
= linpstart
+ linesz
;
1439 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1441 if (lin
.l_lnno
== 0)
1444 *lineno_counts
= (linp
- linpstart
) / linesz
;
1445 /* The setting of line_filepos will only be
1446 useful if all the line number entries for a
1447 csect are contiguous; this only matters for
1449 if (csect
->line_filepos
== 0)
1450 csect
->line_filepos
=
1451 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1457 /* Pick up the csect auxiliary information. */
1458 if (sym
.n_numaux
== 0)
1460 (*_bfd_error_handler
)
1461 (_("%B: class %d symbol `%s' has no aux entries"),
1462 abfd
, sym
.n_sclass
, name
);
1463 bfd_set_error (bfd_error_bad_value
);
1467 bfd_coff_swap_aux_in (abfd
,
1468 (void *) (esym
+ symesz
* sym
.n_numaux
),
1469 sym
.n_type
, sym
.n_sclass
,
1470 sym
.n_numaux
- 1, sym
.n_numaux
,
1473 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1482 (*_bfd_error_handler
)
1483 (_("%B: symbol `%s' has unrecognized csect type %d"),
1485 bfd_set_error (bfd_error_bad_value
);
1489 /* This is an external reference. */
1490 if (sym
.n_sclass
== C_HIDEXT
1491 || sym
.n_scnum
!= N_UNDEF
1492 || aux
.x_csect
.x_scnlen
.l
!= 0)
1494 (*_bfd_error_handler
)
1495 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1496 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1497 aux
.x_csect
.x_scnlen
.l
);
1498 bfd_set_error (bfd_error_bad_value
);
1502 /* An XMC_XO external reference is actually a reference to
1503 an absolute location. */
1504 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1505 section
= bfd_und_section_ptr
;
1508 section
= bfd_abs_section_ptr
;
1509 value
= sym
.n_value
;
1515 csect_index
= -(unsigned) 1;
1517 /* When we see a TOC anchor, we record the TOC value. */
1518 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1520 if (sym
.n_sclass
!= C_HIDEXT
1521 || aux
.x_csect
.x_scnlen
.l
!= 0)
1523 (*_bfd_error_handler
)
1524 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1525 abfd
, name
, sym
.n_sclass
, aux
.x_csect
.x_scnlen
.l
);
1526 bfd_set_error (bfd_error_bad_value
);
1529 xcoff_data (abfd
)->toc
= sym
.n_value
;
1532 /* We must merge TOC entries for the same symbol. We can
1533 merge two TOC entries if they are both C_HIDEXT, they
1534 both have the same name, they are both 4 or 8 bytes long, and
1535 they both have a relocation table entry for an external
1536 symbol with the same name. Unfortunately, this means
1537 that we must look through the relocations. Ick.
1539 Logic for 32 bit vs 64 bit.
1540 32 bit has a csect length of 4 for TOC
1541 64 bit has a csect length of 8 for TOC
1543 The conditions to get past the if-check are not that bad.
1544 They are what is used to create the TOC csects in the first
1546 if (aux
.x_csect
.x_smclas
== XMC_TC
1547 && sym
.n_sclass
== C_HIDEXT
1548 && info
->output_bfd
->xvec
== abfd
->xvec
1549 && ((bfd_xcoff_is_xcoff32 (abfd
)
1550 && aux
.x_csect
.x_scnlen
.l
== 4)
1551 || (bfd_xcoff_is_xcoff64 (abfd
)
1552 && aux
.x_csect
.x_scnlen
.l
== 8)))
1554 asection
*enclosing
;
1555 struct internal_reloc
*relocs
;
1556 bfd_size_type relindx
;
1557 struct internal_reloc
*rel
;
1559 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1560 if (enclosing
== NULL
)
1563 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1564 amt
= enclosing
->reloc_count
;
1565 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1566 rel
= relocs
+ relindx
;
1568 /* 32 bit R_POS r_size is 31
1569 64 bit R_POS r_size is 63 */
1570 if (relindx
< enclosing
->reloc_count
1571 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1572 && rel
->r_type
== R_POS
1573 && ((bfd_xcoff_is_xcoff32 (abfd
)
1574 && rel
->r_size
== 31)
1575 || (bfd_xcoff_is_xcoff64 (abfd
)
1576 && rel
->r_size
== 63)))
1580 struct internal_syment relsym
;
1582 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1583 + rel
->r_symndx
* symesz
);
1584 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1585 if (EXTERN_SYM_P (relsym
.n_sclass
))
1587 const char *relname
;
1588 char relbuf
[SYMNMLEN
+ 1];
1590 struct xcoff_link_hash_entry
*h
;
1592 /* At this point we know that the TOC entry is
1593 for an externally visible symbol. */
1594 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1596 if (relname
== NULL
)
1599 /* We only merge TOC entries if the TC name is
1600 the same as the symbol name. This handles
1601 the normal case, but not common cases like
1602 SYM.P4 which gcc generates to store SYM + 4
1603 in the TOC. FIXME. */
1604 if (strcmp (name
, relname
) == 0)
1606 copy
= (! info
->keep_memory
1607 || relsym
._n
._n_n
._n_zeroes
!= 0
1608 || relsym
._n
._n_n
._n_offset
== 0);
1609 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1610 relname
, TRUE
, copy
,
1615 /* At this point h->root.type could be
1616 bfd_link_hash_new. That should be OK,
1617 since we know for sure that we will come
1618 across this symbol as we step through the
1621 /* We store h in *sym_hash for the
1622 convenience of the relocate_section
1626 if (h
->toc_section
!= NULL
)
1628 asection
**rel_csects
;
1630 /* We already have a TOC entry for this
1631 symbol, so we can just ignore this
1634 reloc_info
[enclosing
->target_index
].csects
;
1635 rel_csects
[relindx
] = bfd_und_section_ptr
;
1639 /* We are about to create a TOC entry for
1648 asection
*enclosing
;
1650 /* We need to create a new section. We get the name from
1651 the csect storage mapping class, so that the linker can
1652 accumulate similar csects together. */
1654 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1658 /* The enclosing section is the main section : .data, .text
1659 or .bss that the csect is coming from. */
1660 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1661 if (enclosing
== NULL
)
1664 if (! bfd_is_abs_section (enclosing
)
1665 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1666 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1667 > enclosing
->vma
+ enclosing
->size
)))
1669 (*_bfd_error_handler
)
1670 (_("%B: csect `%s' not in enclosing section"),
1672 bfd_set_error (bfd_error_bad_value
);
1675 csect
->vma
= sym
.n_value
;
1676 csect
->filepos
= (enclosing
->filepos
1679 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1680 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1681 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1683 /* Record the enclosing section in the tdata for this new
1685 amt
= sizeof (struct coff_section_tdata
);
1686 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1687 if (csect
->used_by_bfd
== NULL
)
1689 amt
= sizeof (struct xcoff_section_tdata
);
1690 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1691 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1693 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1694 xcoff_section_data (abfd
, csect
)->lineno_count
=
1695 enclosing
->lineno_count
;
1697 if (enclosing
->owner
== abfd
)
1699 struct internal_reloc
*relocs
;
1700 bfd_size_type relindx
;
1701 struct internal_reloc
*rel
;
1702 asection
**rel_csect
;
1704 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1705 amt
= enclosing
->reloc_count
;
1706 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1708 rel
= relocs
+ relindx
;
1709 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1712 csect
->rel_filepos
= (enclosing
->rel_filepos
1713 + relindx
* bfd_coff_relsz (abfd
));
1714 while (relindx
< enclosing
->reloc_count
1715 && *rel_csect
== NULL
1716 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1720 csect
->flags
|= SEC_RELOC
;
1721 ++csect
->reloc_count
;
1728 /* There are a number of other fields and section flags
1729 which we do not bother to set. */
1731 csect_index
= ((esym
1732 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1735 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1737 if (first_csect
== NULL
)
1738 first_csect
= csect
;
1740 /* If this symbol is external, we treat it as starting at the
1741 beginning of the newly created section. */
1742 if (EXTERN_SYM_P (sym
.n_sclass
))
1748 /* If this is a TOC section for a symbol, record it. */
1749 if (set_toc
!= NULL
)
1750 set_toc
->toc_section
= csect
;
1755 /* This is a label definition. The x_scnlen field is the
1756 symbol index of the csect. Usually the XTY_LD symbol will
1757 follow its appropriate XTY_SD symbol. The .set pseudo op can
1758 cause the XTY_LD to not follow the XTY_SD symbol. */
1763 if (aux
.x_csect
.x_scnlen
.l
< 0
1764 || (aux
.x_csect
.x_scnlen
.l
1765 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1769 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1771 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1776 (*_bfd_error_handler
)
1777 (_("%B: misplaced XTY_LD `%s'"),
1779 bfd_set_error (bfd_error_bad_value
);
1783 value
= sym
.n_value
- csect
->vma
;
1788 /* This is an unitialized csect. We could base the name on
1789 the storage mapping class, but we don't bother except for
1790 an XMC_TD symbol. If this csect is externally visible,
1791 it is a common symbol. We put XMC_TD symbols in sections
1792 named .tocbss, and rely on the linker script to put that
1795 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1797 /* The linker script puts the .td section in the data
1798 section after the .tc section. */
1799 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1803 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1808 csect
->vma
= sym
.n_value
;
1809 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1810 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1811 /* There are a number of other fields and section flags
1812 which we do not bother to set. */
1814 csect_index
= ((esym
1815 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1818 amt
= sizeof (struct coff_section_tdata
);
1819 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1820 if (csect
->used_by_bfd
== NULL
)
1822 amt
= sizeof (struct xcoff_section_tdata
);
1823 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1824 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1826 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1828 if (first_csect
== NULL
)
1829 first_csect
= csect
;
1831 if (EXTERN_SYM_P (sym
.n_sclass
))
1833 csect
->flags
|= SEC_IS_COMMON
;
1836 value
= aux
.x_csect
.x_scnlen
.l
;
1842 /* Check for magic symbol names. */
1843 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1844 && aux
.x_csect
.x_smclas
!= XMC_TC
1845 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1851 if (strcmp (name
, "_text") == 0)
1852 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1853 else if (strcmp (name
, "_etext") == 0)
1854 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1855 else if (strcmp (name
, "_data") == 0)
1856 i
= XCOFF_SPECIAL_SECTION_DATA
;
1857 else if (strcmp (name
, "_edata") == 0)
1858 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1859 else if (strcmp (name
, "_end") == 0)
1860 i
= XCOFF_SPECIAL_SECTION_END
;
1862 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1863 i
= XCOFF_SPECIAL_SECTION_END2
;
1866 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1869 /* Now we have enough information to add the symbol to the
1870 linker hash table. */
1872 if (EXTERN_SYM_P (sym
.n_sclass
))
1877 BFD_ASSERT (section
!= NULL
);
1879 /* We must copy the name into memory if we got it from the
1880 syment itself, rather than the string table. */
1881 copy
= default_copy
;
1882 if (sym
._n
._n_n
._n_zeroes
!= 0
1883 || sym
._n
._n_n
._n_offset
== 0)
1886 /* The AIX linker appears to only detect multiple symbol
1887 definitions when there is a reference to the symbol. If
1888 a symbol is defined multiple times, and the only
1889 references are from the same object file, the AIX linker
1890 appears to permit it. It does not merge the different
1891 definitions, but handles them independently. On the
1892 other hand, if there is a reference, the linker reports
1895 This matters because the AIX <net/net_globals.h> header
1896 file actually defines an initialized array, so we have to
1897 actually permit that to work.
1899 Just to make matters even more confusing, the AIX linker
1900 appears to permit multiple symbol definitions whenever
1901 the second definition is in an archive rather than an
1902 object file. This may be a consequence of the manner in
1903 which it handles archives: I think it may load the entire
1904 archive in as separate csects, and then let garbage
1905 collection discard symbols.
1907 We also have to handle the case of statically linking a
1908 shared object, which will cause symbol redefinitions,
1909 although this is an easier case to detect. */
1911 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1913 if (! bfd_is_und_section (section
))
1914 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1915 name
, TRUE
, copy
, FALSE
);
1917 /* Make a copy of the symbol name to prevent problems with
1919 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1920 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1921 TRUE
, TRUE
, FALSE
));
1923 if (*sym_hash
== NULL
)
1925 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1926 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1927 && ! bfd_is_und_section (section
)
1928 && ! bfd_is_com_section (section
))
1930 /* This is a second definition of a defined symbol. */
1931 if ((abfd
->flags
& DYNAMIC
) != 0
1932 && ((*sym_hash
)->smclas
!= XMC_GL
1933 || aux
.x_csect
.x_smclas
== XMC_GL
1934 || ((*sym_hash
)->root
.u
.def
.section
->owner
->flags
1937 /* The new symbol is from a shared library, and
1938 either the existing symbol is not global
1939 linkage code or this symbol is global linkage
1940 code. If the existing symbol is global
1941 linkage code and the new symbol is not, then
1942 we want to use the new symbol. */
1943 section
= bfd_und_section_ptr
;
1946 else if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
1947 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
1949 /* The existing symbol is from a shared library.
1951 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1952 (*sym_hash
)->root
.u
.undef
.abfd
=
1953 (*sym_hash
)->root
.u
.def
.section
->owner
;
1955 else if (abfd
->my_archive
!= NULL
)
1957 /* This is a redefinition in an object contained
1958 in an archive. Just ignore it. See the
1960 section
= bfd_und_section_ptr
;
1963 else if (sym
.n_sclass
== C_AIX_WEAKEXT
1964 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1966 /* At least one of the definitions is weak.
1967 Allow the normal rules to take effect. */
1969 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1970 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1972 /* This symbol has been referenced. In this
1973 case, we just continue and permit the
1974 multiple definition error. See the comment
1975 above about the behaviour of the AIX linker. */
1977 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1979 /* The symbols are both csects of the same
1980 class. There is at least a chance that this
1981 is a semi-legitimate redefinition. */
1982 section
= bfd_und_section_ptr
;
1984 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
1987 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
1988 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
1989 && (bfd_is_und_section (section
)
1990 || bfd_is_com_section (section
)))
1992 /* This is a reference to a multiply defined symbol.
1993 Report the error now. See the comment above
1994 about the behaviour of the AIX linker. We could
1995 also do this with warning symbols, but I'm not
1996 sure the XCOFF linker is wholly prepared to
1997 handle them, and that would only be a warning,
1999 if (! ((*info
->callbacks
->multiple_definition
)
2000 (info
, (*sym_hash
)->root
.root
.string
,
2001 NULL
, NULL
, (bfd_vma
) 0,
2002 (*sym_hash
)->root
.u
.def
.section
->owner
,
2003 (*sym_hash
)->root
.u
.def
.section
,
2004 (*sym_hash
)->root
.u
.def
.value
)))
2006 /* Try not to give this error too many times. */
2007 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2011 /* _bfd_generic_link_add_one_symbol may call the linker to
2012 generate an error message, and the linker may try to read
2013 the symbol table to give a good error. Right now, the
2014 line numbers are in an inconsistent state, since they are
2015 counted both in the real sections and in the new csects.
2016 We need to leave the count in the real sections so that
2017 the linker can report the line number of the error
2018 correctly, so temporarily clobber the link to the csects
2019 so that the linker will not try to read the line numbers
2020 a second time from the csects. */
2021 BFD_ASSERT (last_real
->next
== first_csect
);
2022 last_real
->next
= NULL
;
2023 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2024 if (! (_bfd_generic_link_add_one_symbol
2025 (info
, abfd
, name
, flags
, section
, value
,
2027 (struct bfd_link_hash_entry
**) sym_hash
)))
2029 last_real
->next
= first_csect
;
2031 if (smtyp
== XTY_CM
)
2033 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2034 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2035 /* We don't need the common csect we just created. */
2038 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2039 = csect
->alignment_power
;
2042 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2046 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
2047 flag
= XCOFF_REF_REGULAR
;
2049 flag
= XCOFF_DEF_REGULAR
;
2050 (*sym_hash
)->flags
|= flag
;
2052 if ((*sym_hash
)->smclas
== XMC_UA
2053 || flag
== XCOFF_DEF_REGULAR
)
2054 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2058 if (smtyp
== XTY_ER
)
2059 *csect_cache
= section
;
2062 *csect_cache
= csect
;
2064 xcoff_section_data (abfd
, csect
)->last_symndx
2065 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2068 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2069 sym_hash
+= sym
.n_numaux
+ 1;
2070 csect_cache
+= sym
.n_numaux
+ 1;
2071 lineno_counts
+= sym
.n_numaux
+ 1;
2074 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2076 /* Make sure that we have seen all the relocs. */
2077 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2079 /* Reset the section size and the line number count, since the
2080 data is now attached to the csects. Don't reset the size of
2081 the .debug section, since we need to read it below in
2082 bfd_xcoff_size_dynamic_sections. */
2083 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
2085 o
->lineno_count
= 0;
2087 if ((o
->flags
& SEC_RELOC
) != 0)
2090 struct internal_reloc
*rel
;
2091 asection
**rel_csect
;
2093 rel
= reloc_info
[o
->target_index
].relocs
;
2094 rel_csect
= reloc_info
[o
->target_index
].csects
;
2096 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2098 if (*rel_csect
== NULL
)
2100 (*_bfd_error_handler
)
2101 (_("%B: reloc %s:%d not in csect"),
2103 bfd_set_error (bfd_error_bad_value
);
2107 /* We identify all function symbols that are the target
2108 of a relocation, so that we can create glue code for
2109 functions imported from dynamic objects. */
2110 if (info
->output_bfd
->xvec
== abfd
->xvec
2111 && *rel_csect
!= bfd_und_section_ptr
2112 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2114 struct xcoff_link_hash_entry
*h
;
2116 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2117 /* If the symbol name starts with a period, it is
2118 the code of a function. If the symbol is
2119 currently undefined, then add an undefined symbol
2120 for the function descriptor. This should do no
2121 harm, because any regular object that defines the
2122 function should also define the function
2123 descriptor. It helps, because it means that we
2124 will identify the function descriptor with a
2125 dynamic object if a dynamic object defines it. */
2126 if (h
->root
.root
.string
[0] == '.'
2127 && h
->descriptor
== NULL
)
2129 struct xcoff_link_hash_entry
*hds
;
2130 struct bfd_link_hash_entry
*bh
;
2132 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2133 h
->root
.root
.string
+ 1,
2137 if (hds
->root
.type
== bfd_link_hash_new
)
2140 if (! (_bfd_generic_link_add_one_symbol
2141 (info
, abfd
, hds
->root
.root
.string
,
2142 (flagword
) 0, bfd_und_section_ptr
,
2143 (bfd_vma
) 0, NULL
, FALSE
,
2146 hds
= (struct xcoff_link_hash_entry
*) bh
;
2148 hds
->flags
|= XCOFF_DESCRIPTOR
;
2149 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2150 hds
->descriptor
= h
;
2151 h
->descriptor
= hds
;
2153 if (h
->root
.root
.string
[0] == '.')
2154 h
->flags
|= XCOFF_CALLED
;
2158 free (reloc_info
[o
->target_index
].csects
);
2159 reloc_info
[o
->target_index
].csects
= NULL
;
2161 /* Reset SEC_RELOC and the reloc_count, since the reloc
2162 information is now attached to the csects. */
2163 o
->flags
&=~ SEC_RELOC
;
2166 /* If we are not keeping memory, free the reloc information. */
2167 if (! info
->keep_memory
2168 && coff_section_data (abfd
, o
) != NULL
2169 && coff_section_data (abfd
, o
)->relocs
!= NULL
2170 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2172 free (coff_section_data (abfd
, o
)->relocs
);
2173 coff_section_data (abfd
, o
)->relocs
= NULL
;
2177 /* Free up the line numbers. FIXME: We could cache these
2178 somewhere for the final link, to avoid reading them again. */
2179 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2181 free (reloc_info
[o
->target_index
].linenos
);
2182 reloc_info
[o
->target_index
].linenos
= NULL
;
2188 obj_coff_keep_syms (abfd
) = keep_syms
;
2193 if (reloc_info
!= NULL
)
2195 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2197 if (reloc_info
[o
->target_index
].csects
!= NULL
)
2198 free (reloc_info
[o
->target_index
].csects
);
2199 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2200 free (reloc_info
[o
->target_index
].linenos
);
2204 obj_coff_keep_syms (abfd
) = keep_syms
;
2211 /* Add symbols from an XCOFF object file. */
2214 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2216 if (! _bfd_coff_get_external_symbols (abfd
))
2218 if (! xcoff_link_add_symbols (abfd
, info
))
2220 if (! info
->keep_memory
)
2222 if (! _bfd_coff_free_symbols (abfd
))
2228 /* Look through the loader symbols to see if this dynamic object
2229 should be included in the link. The native linker uses the loader
2230 symbols, not the normal symbol table, so we do too. */
2233 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2234 struct bfd_link_info
*info
,
2235 bfd_boolean
*pneeded
)
2239 struct internal_ldhdr ldhdr
;
2240 const char *strings
;
2241 bfd_byte
*elsym
, *elsymend
;
2245 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2247 /* There are no symbols, so don't try to include it. */
2250 if (! xcoff_get_section_contents (abfd
, lsec
))
2252 contents
= coff_section_data (abfd
, lsec
)->contents
;
2254 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2256 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2258 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2260 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2261 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2263 struct internal_ldsym ldsym
;
2264 char nambuf
[SYMNMLEN
+ 1];
2266 struct bfd_link_hash_entry
*h
;
2268 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2270 /* We are only interested in exported symbols. */
2271 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2274 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2275 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2278 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2279 nambuf
[SYMNMLEN
] = '\0';
2283 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2285 /* We are only interested in symbols that are currently
2286 undefined. At this point we know that we are using an XCOFF
2289 && h
->type
== bfd_link_hash_undefined
2290 && (((struct xcoff_link_hash_entry
*) h
)->flags
2291 & XCOFF_DEF_DYNAMIC
) == 0)
2293 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
2300 /* We do not need this shared object. */
2301 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2303 free (coff_section_data (abfd
, lsec
)->contents
);
2304 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2310 /* Look through the symbols to see if this object file should be
2311 included in the link. */
2314 xcoff_link_check_ar_symbols (bfd
*abfd
,
2315 struct bfd_link_info
*info
,
2316 bfd_boolean
*pneeded
)
2318 bfd_size_type symesz
;
2324 if ((abfd
->flags
& DYNAMIC
) != 0
2325 && ! info
->static_link
2326 && info
->output_bfd
->xvec
== abfd
->xvec
)
2327 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
);
2329 symesz
= bfd_coff_symesz (abfd
);
2330 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2331 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2332 while (esym
< esym_end
)
2334 struct internal_syment sym
;
2336 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2338 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2341 char buf
[SYMNMLEN
+ 1];
2342 struct bfd_link_hash_entry
*h
;
2344 /* This symbol is externally visible, and is defined by this
2346 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2350 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2352 /* We are only interested in symbols that are currently
2353 undefined. If a symbol is currently known to be common,
2354 XCOFF linkers do not bring in an object file which
2355 defines it. We also don't bring in symbols to satisfy
2356 undefined references in shared objects. */
2358 && h
->type
== bfd_link_hash_undefined
2359 && (info
->output_bfd
->xvec
!= abfd
->xvec
2360 || (((struct xcoff_link_hash_entry
*) h
)->flags
2361 & XCOFF_DEF_DYNAMIC
) == 0))
2363 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
2370 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2373 /* We do not need this object file. */
2377 /* Check a single archive element to see if we need to include it in
2378 the link. *PNEEDED is set according to whether this element is
2379 needed in the link or not. This is called via
2380 _bfd_generic_link_add_archive_symbols. */
2383 xcoff_link_check_archive_element (bfd
*abfd
,
2384 struct bfd_link_info
*info
,
2385 bfd_boolean
*pneeded
)
2387 bfd_boolean keep_syms_p
;
2389 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2390 if (! _bfd_coff_get_external_symbols (abfd
))
2393 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
2398 if (! xcoff_link_add_symbols (abfd
, info
))
2400 if (info
->keep_memory
)
2406 if (! _bfd_coff_free_symbols (abfd
))
2413 /* Given an XCOFF BFD, add symbols to the global hash table as
2417 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2419 switch (bfd_get_format (abfd
))
2422 return xcoff_link_add_object_symbols (abfd
, info
);
2425 /* If the archive has a map, do the usual search. We then need
2426 to check the archive for dynamic objects, because they may not
2427 appear in the archive map even though they should, perhaps, be
2428 included. If the archive has no map, we just consider each object
2429 file in turn, since that apparently is what the AIX native linker
2431 if (bfd_has_map (abfd
))
2433 if (! (_bfd_generic_link_add_archive_symbols
2434 (abfd
, info
, xcoff_link_check_archive_element
)))
2441 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2442 while (member
!= NULL
)
2444 if (bfd_check_format (member
, bfd_object
)
2445 && (info
->output_bfd
->xvec
== member
->xvec
)
2446 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2450 if (! xcoff_link_check_archive_element (member
, info
,
2454 member
->archive_pass
= -1;
2456 member
= bfd_openr_next_archived_file (abfd
, member
);
2463 bfd_set_error (bfd_error_wrong_format
);
2468 /* If symbol H has not been interpreted as a function descriptor,
2469 see whether it should be. Set up its descriptor information if so. */
2472 xcoff_find_function (struct bfd_link_info
*info
,
2473 struct xcoff_link_hash_entry
*h
)
2475 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2476 && h
->root
.root
.string
[0] != '.')
2479 struct xcoff_link_hash_entry
*hfn
;
2482 amt
= strlen (h
->root
.root
.string
) + 2;
2483 fnname
= bfd_malloc (amt
);
2487 strcpy (fnname
+ 1, h
->root
.root
.string
);
2488 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2489 fnname
, FALSE
, FALSE
, TRUE
);
2492 && hfn
->smclas
== XMC_PR
2493 && (hfn
->root
.type
== bfd_link_hash_defined
2494 || hfn
->root
.type
== bfd_link_hash_defweak
))
2496 h
->flags
|= XCOFF_DESCRIPTOR
;
2497 h
->descriptor
= hfn
;
2498 hfn
->descriptor
= h
;
2504 /* Return true if the given bfd contains at least one shared object. */
2507 xcoff_archive_contains_shared_object_p (bfd
*archive
)
2511 member
= bfd_openr_next_archived_file (archive
, NULL
);
2512 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2513 member
= bfd_openr_next_archived_file (archive
, member
);
2514 return member
!= NULL
;
2517 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2518 qualifies for export by -bexpall. */
2521 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2523 /* Exclude symbols beginning with '_'. */
2524 if (h
->root
.root
.string
[0] == '_')
2527 /* Exclude archive members that would otherwise be unreferenced. */
2528 if ((h
->flags
& XCOFF_MARK
) == 0
2529 && (h
->root
.type
== bfd_link_hash_defined
2530 || h
->root
.type
== bfd_link_hash_defweak
)
2531 && h
->root
.u
.def
.section
->owner
!= NULL
2532 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2538 /* Return true if symbol H qualifies for the forms of automatic export
2539 specified by AUTO_EXPORT_FLAGS. */
2542 xcoff_auto_export_p (struct xcoff_link_hash_entry
*h
,
2543 unsigned int auto_export_flags
)
2545 /* Don't automatically export things that were explicitly exported. */
2546 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2549 /* Don't export things that we don't define. */
2550 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2553 /* Don't export functions; export their descriptors instead. */
2554 if (h
->root
.root
.string
[0] == '.')
2557 /* We don't export a symbol which is being defined by an object
2558 included from an archive which contains a shared object. The
2559 rationale is that if an archive contains both an unshared and
2560 a shared object, then there must be some reason that the
2561 unshared object is unshared, and we don't want to start
2562 providing a shared version of it. In particular, this solves
2563 a bug involving the _savefNN set of functions. gcc will call
2564 those functions without providing a slot to restore the TOC,
2565 so it is essential that these functions be linked in directly
2566 and not from a shared object, which means that a shared
2567 object which also happens to link them in must not export
2568 them. This is confusing, but I haven't been able to think of
2569 a different approach. Note that the symbols can, of course,
2570 be exported explicitly. */
2571 if (h
->root
.type
== bfd_link_hash_defined
2572 || h
->root
.type
== bfd_link_hash_defweak
)
2576 owner
= h
->root
.u
.def
.section
->owner
;
2578 && owner
->my_archive
!= NULL
2579 && xcoff_archive_contains_shared_object_p (owner
->my_archive
))
2583 /* Otherwise, all symbols are exported by -bexpfull. */
2584 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2587 /* Despite its name, -bexpall exports most but not all symbols. */
2588 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2589 && xcoff_covered_by_expall_p (h
))
2595 /* Return true if relocation REL needs to be copied to the .loader section.
2596 If REL is against a global symbol, H is that symbol, otherwise it
2600 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2601 struct xcoff_link_hash_entry
*h
)
2603 if (!xcoff_hash_table (info
)->loader_section
)
2606 switch (rel
->r_type
)
2613 /* We should never need a .loader reloc for a TOC-relative reloc. */
2617 /* In this case, relocations against defined symbols can be resolved
2620 || h
->root
.type
== bfd_link_hash_defined
2621 || h
->root
.type
== bfd_link_hash_defweak
2622 || h
->root
.type
== bfd_link_hash_common
)
2625 /* We will always provide a local definition of function symbols,
2626 even if we don't have one yet. */
2627 if ((h
->flags
& XCOFF_CALLED
) != 0)
2636 /* Absolute relocations against absolute symbols can be
2637 resolved statically. */
2639 && (h
->root
.type
== bfd_link_hash_defined
2640 || h
->root
.type
== bfd_link_hash_defweak
)
2641 && bfd_is_abs_section (h
->root
.u
.def
.section
))
2648 /* Mark a symbol as not being garbage, including the section in which
2651 static inline bfd_boolean
2652 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2654 if ((h
->flags
& XCOFF_MARK
) != 0)
2657 h
->flags
|= XCOFF_MARK
;
2659 /* If we're marking an undefined symbol, try find some way of
2661 if (!info
->relocatable
2662 && (h
->flags
& XCOFF_IMPORT
) == 0
2663 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2664 && (h
->root
.type
== bfd_link_hash_undefined
2665 || h
->root
.type
== bfd_link_hash_undefweak
))
2667 /* First check whether this symbol can be interpreted as an
2668 undefined function descriptor for a defined function symbol. */
2669 if (!xcoff_find_function (info
, h
))
2672 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2673 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2674 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2676 /* This is a descriptor for a defined symbol, but the input
2677 objects have not defined the descriptor itself. Fill in
2678 the definition automatically.
2680 Note that we do this even if we found a dynamic definition
2681 of H. The local function definition logically overrides
2685 sec
= xcoff_hash_table (info
)->descriptor_section
;
2686 h
->root
.type
= bfd_link_hash_defined
;
2687 h
->root
.u
.def
.section
= sec
;
2688 h
->root
.u
.def
.value
= sec
->size
;
2690 h
->flags
|= XCOFF_DEF_REGULAR
;
2692 /* The size of the function descriptor depends on whether this
2693 is xcoff32 (12) or xcoff64 (24). */
2694 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2696 /* A function descriptor uses two relocs: one for the
2697 associated code, and one for the TOC address. */
2698 xcoff_hash_table (info
)->ldrel_count
+= 2;
2699 sec
->reloc_count
+= 2;
2701 /* Mark the function itself. */
2702 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2705 /* Mark the TOC section, so that we get an anchor
2706 to relocate against. */
2707 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2710 /* We handle writing out the contents of the descriptor in
2711 xcoff_write_global_symbol. */
2713 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2715 /* This is a function symbol for which we need to create
2718 struct xcoff_link_hash_entry
*hds
;
2720 /* Mark the descriptor (and its TOC section). */
2721 hds
= h
->descriptor
;
2722 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2723 || hds
->root
.type
== bfd_link_hash_undefweak
)
2724 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2725 if (!xcoff_mark_symbol (info
, hds
))
2728 /* Treat this symbol as undefined if the descriptor was. */
2729 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2730 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2732 /* Allocate room for the global linkage code itself. */
2733 sec
= xcoff_hash_table (info
)->linkage_section
;
2734 h
->root
.type
= bfd_link_hash_defined
;
2735 h
->root
.u
.def
.section
= sec
;
2736 h
->root
.u
.def
.value
= sec
->size
;
2738 h
->flags
|= XCOFF_DEF_REGULAR
;
2739 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2741 /* The global linkage code requires a TOC entry for the
2743 if (hds
->toc_section
== NULL
)
2748 xcoff32 uses 4 bytes in the toc.
2749 xcoff64 uses 8 bytes in the toc. */
2750 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2752 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2757 /* Allocate room in the fallback TOC section. */
2758 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2759 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2760 hds
->toc_section
->size
+= byte_size
;
2761 if (!xcoff_mark (info
, hds
->toc_section
))
2764 /* Allocate room for a static and dynamic R_TOC
2766 ++xcoff_hash_table (info
)->ldrel_count
;
2767 ++hds
->toc_section
->reloc_count
;
2769 /* Set the index to -2 to force this symbol to
2772 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2775 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2777 /* Record that the symbol was undefined, then import it.
2778 -brtl links use a special fake import file. */
2779 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2780 if (xcoff_hash_table (info
)->rtld
)
2782 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2787 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2793 if (h
->root
.type
== bfd_link_hash_defined
2794 || h
->root
.type
== bfd_link_hash_defweak
)
2798 hsec
= h
->root
.u
.def
.section
;
2799 if (! bfd_is_abs_section (hsec
)
2800 && (hsec
->flags
& SEC_MARK
) == 0)
2802 if (! xcoff_mark (info
, hsec
))
2807 if (h
->toc_section
!= NULL
2808 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2810 if (! xcoff_mark (info
, h
->toc_section
))
2817 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2818 If the symbol exists, set FLAGS. */
2821 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2822 const char *name
, unsigned int flags
)
2824 struct xcoff_link_hash_entry
*h
;
2826 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2827 FALSE
, FALSE
, TRUE
);
2831 if (h
->root
.type
== bfd_link_hash_defined
2832 || h
->root
.type
== bfd_link_hash_defweak
)
2834 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2841 /* The mark phase of garbage collection. For a given section, mark
2842 it, and all the sections which define symbols to which it refers.
2843 Because this function needs to look at the relocs, we also count
2844 the number of relocs which need to be copied into the .loader
2848 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2850 if (bfd_is_abs_section (sec
)
2851 || (sec
->flags
& SEC_MARK
) != 0)
2854 sec
->flags
|= SEC_MARK
;
2856 if (sec
->owner
->xvec
== info
->output_bfd
->xvec
2857 && coff_section_data (sec
->owner
, sec
) != NULL
2858 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2860 struct xcoff_link_hash_entry
**syms
;
2861 struct internal_reloc
*rel
, *relend
;
2863 unsigned long i
, first
, last
;
2865 /* Mark all the symbols in this section. */
2866 syms
= obj_xcoff_sym_hashes (sec
->owner
);
2867 csects
= xcoff_data (sec
->owner
)->csects
;
2868 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
2869 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
2870 for (i
= first
; i
<= last
; i
++)
2871 if (csects
[i
] == sec
2873 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
2875 if (!xcoff_mark_symbol (info
, syms
[i
]))
2879 /* Look through the section relocs. */
2880 if ((sec
->flags
& SEC_RELOC
) != 0
2881 && sec
->reloc_count
> 0)
2883 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, TRUE
,
2887 relend
= rel
+ sec
->reloc_count
;
2888 for (; rel
< relend
; rel
++)
2890 struct xcoff_link_hash_entry
*h
;
2892 if ((unsigned int) rel
->r_symndx
2893 > obj_raw_syment_count (sec
->owner
))
2896 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2899 if ((h
->flags
& XCOFF_MARK
) == 0)
2901 if (!xcoff_mark_symbol (info
, h
))
2909 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2911 && (rsec
->flags
& SEC_MARK
) == 0)
2913 if (!xcoff_mark (info
, rsec
))
2918 /* See if this reloc needs to be copied into the .loader
2920 if (xcoff_need_ldrel_p (info
, rel
, h
))
2922 ++xcoff_hash_table (info
)->ldrel_count
;
2924 h
->flags
|= XCOFF_LDREL
;
2928 if (! info
->keep_memory
2929 && coff_section_data (sec
->owner
, sec
) != NULL
2930 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2931 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2933 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2934 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2942 /* Routines that are called after all the input files have been
2943 handled, but before the sections are laid out in memory. */
2945 /* The sweep phase of garbage collection. Remove all garbage
2949 xcoff_sweep (struct bfd_link_info
*info
)
2953 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2957 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2959 if ((o
->flags
& SEC_MARK
) == 0)
2961 /* Keep all sections from non-XCOFF input files. Keep
2962 special sections. Keep .debug sections for the
2964 if (sub
->xvec
!= info
->output_bfd
->xvec
2965 || o
== xcoff_hash_table (info
)->debug_section
2966 || o
== xcoff_hash_table (info
)->loader_section
2967 || o
== xcoff_hash_table (info
)->linkage_section
2968 || o
== xcoff_hash_table (info
)->descriptor_section
2969 || strcmp (o
->name
, ".debug") == 0)
2970 o
->flags
|= SEC_MARK
;
2981 /* Record the number of elements in a set. This is used to output the
2982 correct csect length. */
2985 bfd_xcoff_link_record_set (bfd
*output_bfd
,
2986 struct bfd_link_info
*info
,
2987 struct bfd_link_hash_entry
*harg
,
2990 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2991 struct xcoff_link_size_list
*n
;
2994 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2997 /* This will hardly ever be called. I don't want to burn four bytes
2998 per global symbol, so instead the size is kept on a linked list
2999 attached to the hash table. */
3001 n
= bfd_alloc (output_bfd
, amt
);
3004 n
->next
= xcoff_hash_table (info
)->size_list
;
3007 xcoff_hash_table (info
)->size_list
= n
;
3009 h
->flags
|= XCOFF_HAS_SIZE
;
3014 /* Import a symbol. */
3017 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3018 struct bfd_link_info
*info
,
3019 struct bfd_link_hash_entry
*harg
,
3021 const char *imppath
,
3022 const char *impfile
,
3023 const char *impmember
,
3024 unsigned int syscall_flag
)
3026 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3028 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3031 /* A symbol name which starts with a period is the code for a
3032 function. If the symbol is undefined, then add an undefined
3033 symbol for the function descriptor, and import that instead. */
3034 if (h
->root
.root
.string
[0] == '.'
3035 && h
->root
.type
== bfd_link_hash_undefined
3036 && val
== (bfd_vma
) -1)
3038 struct xcoff_link_hash_entry
*hds
;
3040 hds
= h
->descriptor
;
3043 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3044 h
->root
.root
.string
+ 1,
3048 if (hds
->root
.type
== bfd_link_hash_new
)
3050 hds
->root
.type
= bfd_link_hash_undefined
;
3051 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3053 hds
->flags
|= XCOFF_DESCRIPTOR
;
3054 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3055 hds
->descriptor
= h
;
3056 h
->descriptor
= hds
;
3059 /* Now, if the descriptor is undefined, import the descriptor
3060 rather than the symbol we were told to import. FIXME: Is
3061 this correct in all cases? */
3062 if (hds
->root
.type
== bfd_link_hash_undefined
)
3066 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3068 if (val
!= (bfd_vma
) -1)
3070 if (h
->root
.type
== bfd_link_hash_defined
3071 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
3072 || h
->root
.u
.def
.value
!= val
))
3074 if (! ((*info
->callbacks
->multiple_definition
)
3075 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
3076 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
3077 output_bfd
, bfd_abs_section_ptr
, val
)))
3081 h
->root
.type
= bfd_link_hash_defined
;
3082 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3083 h
->root
.u
.def
.value
= val
;
3087 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3093 /* Export a symbol. */
3096 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3097 struct bfd_link_info
*info
,
3098 struct bfd_link_hash_entry
*harg
)
3100 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3102 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3105 h
->flags
|= XCOFF_EXPORT
;
3107 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3108 I'm just going to ignore it until somebody explains it. */
3110 /* Make sure we don't garbage collect this symbol. */
3111 if (! xcoff_mark_symbol (info
, h
))
3114 /* If this is a function descriptor, make sure we don't garbage
3115 collect the associated function code. We normally don't have to
3116 worry about this, because the descriptor will be attached to a
3117 section with relocs, but if we are creating the descriptor
3118 ourselves those relocs will not be visible to the mark code. */
3119 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3121 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3128 /* Count a reloc against a symbol. This is called for relocs
3129 generated by the linker script, typically for global constructors
3133 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3134 struct bfd_link_info
*info
,
3137 struct xcoff_link_hash_entry
*h
;
3139 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3142 h
= ((struct xcoff_link_hash_entry
*)
3143 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, FALSE
, FALSE
,
3147 (*_bfd_error_handler
) (_("%s: no such symbol"), name
);
3148 bfd_set_error (bfd_error_no_symbols
);
3152 h
->flags
|= XCOFF_REF_REGULAR
;
3153 if (xcoff_hash_table (info
)->loader_section
)
3155 h
->flags
|= XCOFF_LDREL
;
3156 ++xcoff_hash_table (info
)->ldrel_count
;
3159 /* Mark the symbol to avoid garbage collection. */
3160 if (! xcoff_mark_symbol (info
, h
))
3166 /* This function is called for each symbol to which the linker script
3170 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3171 struct bfd_link_info
*info
,
3174 struct xcoff_link_hash_entry
*h
;
3176 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3179 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
, TRUE
,
3184 h
->flags
|= XCOFF_DEF_REGULAR
;
3189 /* An xcoff_link_hash_traverse callback for which DATA points to an
3190 xcoff_loader_info. Mark all symbols that should be automatically
3194 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3196 struct xcoff_loader_info
*ldinfo
;
3198 ldinfo
= (struct xcoff_loader_info
*) data
;
3199 if (xcoff_auto_export_p (h
, ldinfo
->auto_export_flags
))
3201 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3202 ldinfo
->failed
= TRUE
;
3207 /* Add a symbol to the .loader symbols, if necessary. */
3209 /* INPUT_BFD has an external symbol associated with hash table entry H
3210 and csect CSECT. Return true if INPUT_BFD defines H. */
3213 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3216 switch (h
->root
.type
)
3218 case bfd_link_hash_defined
:
3219 case bfd_link_hash_defweak
:
3220 /* No input bfd owns absolute symbols. They are written by
3221 xcoff_write_global_symbol instead. */
3222 return (!bfd_is_abs_section (csect
)
3223 && h
->root
.u
.def
.section
== csect
);
3225 case bfd_link_hash_common
:
3226 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3228 case bfd_link_hash_undefined
:
3229 case bfd_link_hash_undefweak
:
3230 /* We can't treat undef.abfd as the owner because that bfd
3231 might be a dynamic object. Allow any bfd to claim it. */
3239 /* See if H should have a loader symbol associated with it. */
3242 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3243 struct xcoff_link_hash_entry
*h
)
3247 /* Warn if this symbol is exported but not defined. */
3248 if ((h
->flags
& XCOFF_EXPORT
) != 0
3249 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3251 (*_bfd_error_handler
)
3252 (_("warning: attempt to export undefined symbol `%s'"),
3253 h
->root
.root
.string
);
3257 /* We need to add a symbol to the .loader section if it is mentioned
3258 in a reloc which we are copying to the .loader section and it was
3259 not defined or common, or if it is the entry point, or if it is
3261 if (((h
->flags
& XCOFF_LDREL
) == 0
3262 || h
->root
.type
== bfd_link_hash_defined
3263 || h
->root
.type
== bfd_link_hash_defweak
3264 || h
->root
.type
== bfd_link_hash_common
)
3265 && (h
->flags
& XCOFF_ENTRY
) == 0
3266 && (h
->flags
& XCOFF_EXPORT
) == 0)
3269 /* We need to add this symbol to the .loader symbols. */
3271 BFD_ASSERT (h
->ldsym
== NULL
);
3272 amt
= sizeof (struct internal_ldsym
);
3273 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3274 if (h
->ldsym
== NULL
)
3276 ldinfo
->failed
= TRUE
;
3280 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3282 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3283 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3285 h
->ldsym
->l_ifile
= h
->ldindx
;
3288 /* The first 3 symbol table indices are reserved to indicate the
3289 data, text and bss sections. */
3290 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3292 ++ldinfo
->ldsym_count
;
3294 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3295 h
->ldsym
, h
->root
.root
.string
))
3298 h
->flags
|= XCOFF_BUILT_LDSYM
;
3302 /* An xcoff_htab_traverse callback that is called for each symbol
3303 once garbage collection is complete. */
3306 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3308 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3310 if (h
->root
.type
== bfd_link_hash_warning
)
3311 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
3313 /* __rtinit, this symbol has special handling. */
3314 if (h
->flags
& XCOFF_RTINIT
)
3317 /* If this is a final link, and the symbol was defined as a common
3318 symbol in a regular object file, and there was no definition in
3319 any dynamic object, then the linker will have allocated space for
3320 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3321 will not have been set. */
3322 if (h
->root
.type
== bfd_link_hash_defined
3323 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
3324 && (h
->flags
& XCOFF_REF_REGULAR
) != 0
3325 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
3326 && (bfd_is_abs_section (h
->root
.u
.def
.section
)
3327 || (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0))
3328 h
->flags
|= XCOFF_DEF_REGULAR
;
3330 /* We don't want to garbage collect symbols which are not defined in
3331 XCOFF files. This is a convenient place to mark them. */
3332 if (xcoff_hash_table (ldinfo
->info
)->gc
3333 && (h
->flags
& XCOFF_MARK
) == 0
3334 && (h
->root
.type
== bfd_link_hash_defined
3335 || h
->root
.type
== bfd_link_hash_defweak
)
3336 && (h
->root
.u
.def
.section
->owner
== NULL
3337 || (h
->root
.u
.def
.section
->owner
->xvec
3338 != ldinfo
->info
->output_bfd
->xvec
)))
3339 h
->flags
|= XCOFF_MARK
;
3341 /* Skip discarded symbols. */
3342 if (xcoff_hash_table (ldinfo
->info
)->gc
3343 && (h
->flags
& XCOFF_MARK
) == 0)
3346 /* If this is still a common symbol, and it wasn't garbage
3347 collected, we need to actually allocate space for it in the .bss
3349 if (h
->root
.type
== bfd_link_hash_common
3350 && h
->root
.u
.c
.p
->section
->size
== 0)
3352 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3353 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3356 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3358 if (xcoff_auto_export_p (h
, ldinfo
->auto_export_flags
))
3359 h
->flags
|= XCOFF_EXPORT
;
3361 if (!xcoff_build_ldsym (ldinfo
, h
))
3368 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3369 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3370 csect information, if any. NAME is the function's name if the name
3371 is stored in the .debug section, otherwise it is null.
3373 Return 1 if we should include an appropriately-adjusted ISYM
3374 in the output file, 0 if we should discard ISYM, or -1 if an
3378 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3379 struct internal_syment
*isym
,
3380 union internal_auxent
*aux
,
3381 struct xcoff_link_hash_entry
*h
,
3382 asection
*csect
, const char *name
)
3386 /* If we are skipping this csect, we want to strip the symbol too. */
3390 /* Likewise if we garbage-collected the csect. */
3391 if (xcoff_hash_table (info
)->gc
3392 && !bfd_is_abs_section (csect
)
3393 && !bfd_is_und_section (csect
)
3394 && (csect
->flags
& SEC_MARK
) == 0)
3397 /* An XCOFF linker always removes C_STAT symbols. */
3398 if (isym
->n_sclass
== C_STAT
)
3401 /* We generate the TOC anchor separately. */
3402 if (isym
->n_sclass
== C_HIDEXT
3403 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3406 /* If we are stripping all symbols, we want to discard this one. */
3407 if (info
->strip
== strip_all
)
3410 /* Discard symbols that are defined elsewhere. */
3411 if (EXTERN_SYM_P (isym
->n_sclass
))
3413 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3415 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3419 /* If we're discarding local symbols, check whether ISYM is local. */
3420 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3421 if (info
->discard
== discard_all
3422 && !EXTERN_SYM_P (isym
->n_sclass
)
3423 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3426 /* If we're stripping debugging symbols, check whether ISYM is one. */
3427 if (info
->strip
== strip_debugger
3428 && isym
->n_scnum
== N_DEBUG
)
3431 /* If we are stripping symbols based on name, check how ISYM's
3432 name should be handled. */
3433 if (info
->strip
== strip_some
3434 || info
->discard
== discard_l
)
3436 char buf
[SYMNMLEN
+ 1];
3440 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3445 if (info
->strip
== strip_some
3446 && bfd_hash_lookup (info
->keep_hash
, name
, FALSE
, FALSE
) == NULL
)
3449 if (info
->discard
== discard_l
3450 && !EXTERN_SYM_P (isym
->n_sclass
)
3451 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3452 && bfd_is_local_label_name (input_bfd
, name
))
3459 /* Lay out the .loader section, filling in the header and the import paths.
3460 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3463 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
,
3464 const char *libpath
)
3467 struct xcoff_link_hash_table
*htab
;
3468 struct internal_ldhdr
*ldhdr
;
3469 struct xcoff_import_file
*fl
;
3470 bfd_size_type stoff
;
3471 size_t impsize
, impcount
;
3475 /* Work out the size of the import file names. Each import file ID
3476 consists of three null terminated strings: the path, the file
3477 name, and the archive member name. The first entry in the list
3478 of names is the path to use to find objects, which the linker has
3479 passed in as the libpath argument. For some reason, the path
3480 entry in the other import file names appears to always be empty. */
3481 output_bfd
= ldinfo
->output_bfd
;
3482 htab
= xcoff_hash_table (ldinfo
->info
);
3483 impsize
= strlen (libpath
) + 3;
3485 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3488 impsize
+= (strlen (fl
->path
)
3490 + strlen (fl
->member
)
3494 /* Set up the .loader section header. */
3495 ldhdr
= &htab
->ldhdr
;
3496 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3497 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3498 ldhdr
->l_nreloc
= htab
->ldrel_count
;
3499 ldhdr
->l_istlen
= impsize
;
3500 ldhdr
->l_nimpid
= impcount
;
3501 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3502 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3503 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3504 ldhdr
->l_stlen
= ldinfo
->string_size
;
3505 stoff
= ldhdr
->l_impoff
+ impsize
;
3506 if (ldinfo
->string_size
== 0)
3509 ldhdr
->l_stoff
= stoff
;
3511 /* 64 bit elements to ldhdr
3512 The swap out routine for 32 bit will ignore them.
3513 Nothing fancy, symbols come after the header and relocs come
3515 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3516 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3517 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3519 /* We now know the final size of the .loader section. Allocate
3521 lsec
= htab
->loader_section
;
3522 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3523 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3524 if (lsec
->contents
== NULL
)
3527 /* Set up the header. */
3528 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3530 /* Set up the import file names. */
3531 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3532 strcpy (out
, libpath
);
3533 out
+= strlen (libpath
) + 1;
3536 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3541 while ((*out
++ = *s
++) != '\0')
3544 while ((*out
++ = *s
++) != '\0')
3547 while ((*out
++ = *s
++) != '\0')
3551 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3553 /* Set up the symbol string table. */
3554 if (ldinfo
->string_size
> 0)
3556 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
3557 free (ldinfo
->strings
);
3558 ldinfo
->strings
= NULL
;
3561 /* We can't set up the symbol table or the relocs yet, because we
3562 don't yet know the final position of the various sections. The
3563 .loader symbols are written out when the corresponding normal
3564 symbols are written out in xcoff_link_input_bfd or
3565 xcoff_write_global_symbol. The .loader relocs are written out
3566 when the corresponding normal relocs are handled in
3567 xcoff_link_input_bfd. */
3572 /* Build the .loader section. This is called by the XCOFF linker
3573 emulation before_allocation routine. We must set the size of the
3574 .loader section before the linker lays out the output file.
3575 LIBPATH is the library path to search for shared objects; this is
3576 normally built from the -L arguments passed to the linker. ENTRY
3577 is the name of the entry point symbol (the -e linker option).
3578 FILE_ALIGN is the alignment to use for sections within the file
3579 (the -H linker option). MAXSTACK is the maximum stack size (the
3580 -bmaxstack linker option). MAXDATA is the maximum data size (the
3581 -bmaxdata linker option). GC is whether to do garbage collection
3582 (the -bgc linker option). MODTYPE is the module type (the
3583 -bmodtype linker option). TEXTRO is whether the text section must
3584 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3585 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3586 is set by this routine to csects with magic names like _end. */
3589 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3590 struct bfd_link_info
*info
,
3591 const char *libpath
,
3593 unsigned long file_align
,
3594 unsigned long maxstack
,
3595 unsigned long maxdata
,
3599 unsigned int auto_export_flags
,
3600 asection
**special_sections
,
3603 struct xcoff_loader_info ldinfo
;
3607 struct bfd_strtab_hash
*debug_strtab
;
3608 bfd_byte
*debug_contents
= NULL
;
3611 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3613 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3614 special_sections
[i
] = NULL
;
3618 ldinfo
.failed
= FALSE
;
3619 ldinfo
.output_bfd
= output_bfd
;
3621 ldinfo
.auto_export_flags
= auto_export_flags
;
3622 ldinfo
.ldsym_count
= 0;
3623 ldinfo
.string_size
= 0;
3624 ldinfo
.strings
= NULL
;
3625 ldinfo
.string_alc
= 0;
3627 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3628 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3629 xcoff_data (output_bfd
)->modtype
= modtype
;
3631 xcoff_hash_table (info
)->file_align
= file_align
;
3632 xcoff_hash_table (info
)->textro
= textro
;
3633 xcoff_hash_table (info
)->rtld
= rtld
;
3636 if (xcoff_hash_table (info
)->loader_section
3637 && (info
->init_function
|| info
->fini_function
|| rtld
))
3639 struct xcoff_link_hash_entry
*hsym
;
3640 struct internal_ldsym
*ldsym
;
3642 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3643 "__rtinit", FALSE
, FALSE
, TRUE
);
3646 (*_bfd_error_handler
)
3647 (_("error: undefined symbol __rtinit"));
3651 xcoff_mark_symbol (info
, hsym
);
3652 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3654 /* __rtinit initialized. */
3655 amt
= sizeof (* ldsym
);
3656 ldsym
= bfd_malloc (amt
);
3658 ldsym
->l_value
= 0; /* Will be filled in later. */
3659 ldsym
->l_scnum
= 2; /* Data section. */
3660 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3661 ldsym
->l_smclas
= 5; /* .rw. */
3662 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3663 ldsym
->l_parm
= 0; /* NA. */
3665 /* Force __rtinit to be the first symbol in the loader symbol table
3666 See xcoff_build_ldsyms
3668 The first 3 symbol table indices are reserved to indicate the data,
3669 text and bss sections. */
3670 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3673 ldinfo
.ldsym_count
= 1;
3674 hsym
->ldsym
= ldsym
;
3676 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3677 hsym
->ldsym
, hsym
->root
.root
.string
))
3680 /* This symbol is written out by xcoff_write_global_symbol
3681 Set stuff up so xcoff_write_global_symbol logic works. */
3682 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3683 hsym
->root
.type
= bfd_link_hash_defined
;
3684 hsym
->root
.u
.def
.value
= 0;
3687 /* Garbage collect unused sections. */
3688 if (info
->relocatable
|| !gc
)
3691 xcoff_hash_table (info
)->gc
= FALSE
;
3693 /* We still need to call xcoff_mark, in order to set ldrel_count
3695 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3699 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3701 /* We shouldn't unconditionaly mark the TOC section.
3702 The output file should only have a TOC if either
3703 (a) one of the input files did or (b) we end up
3704 creating TOC references as part of the link process. */
3705 if (o
!= xcoff_hash_table (info
)->toc_section
3706 && (o
->flags
& SEC_MARK
) == 0)
3708 if (! xcoff_mark (info
, o
))
3717 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3719 if (info
->init_function
!= NULL
3720 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3722 if (info
->fini_function
!= NULL
3723 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3725 if (auto_export_flags
!= 0)
3727 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3728 xcoff_mark_auto_exports
, &ldinfo
);
3733 xcoff_hash_table (info
)->gc
= TRUE
;
3736 /* Return special sections to the caller. */
3737 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3739 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3743 && (sec
->flags
& SEC_MARK
) == 0)
3746 special_sections
[i
] = sec
;
3749 if (info
->input_bfds
== NULL
)
3750 /* I'm not sure what to do in this bizarre case. */
3753 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3758 if (xcoff_hash_table (info
)->loader_section
3759 && !xcoff_build_loader_section (&ldinfo
, libpath
))
3762 /* Allocate space for the magic sections. */
3763 sec
= xcoff_hash_table (info
)->linkage_section
;
3766 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3767 if (sec
->contents
== NULL
)
3770 sec
= xcoff_hash_table (info
)->toc_section
;
3773 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3774 if (sec
->contents
== NULL
)
3777 sec
= xcoff_hash_table (info
)->descriptor_section
;
3780 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3781 if (sec
->contents
== NULL
)
3785 /* Now that we've done garbage collection, decide which symbols to keep,
3786 and figure out the contents of the .debug section. */
3787 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3789 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3792 bfd_size_type symcount
;
3795 unsigned int *lineno_counts
;
3796 struct xcoff_link_hash_entry
**sym_hash
;
3797 bfd_byte
*esym
, *esymend
;
3798 bfd_size_type symesz
;
3800 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3803 if ((sub
->flags
& DYNAMIC
) != 0
3804 && !info
->static_link
)
3807 if (! _bfd_coff_get_external_symbols (sub
))
3810 symcount
= obj_raw_syment_count (sub
);
3811 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
3812 if (debug_index
== NULL
)
3814 xcoff_data (sub
)->debug_indices
= debug_index
;
3816 if (info
->strip
== strip_all
3817 || info
->strip
== strip_debugger
3818 || info
->discard
== discard_all
)
3819 /* We're stripping all debugging information, so there's no need
3820 to read SUB's .debug section. */
3824 /* Grab the contents of SUB's .debug section, if any. */
3825 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3826 if (subdeb
!= NULL
&& subdeb
->size
> 0)
3828 /* We use malloc and copy the names into the debug
3829 stringtab, rather than bfd_alloc, because I expect
3830 that, when linking many files together, many of the
3831 strings will be the same. Storing the strings in the
3832 hash table should save space in this case. */
3833 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
3838 csectpp
= xcoff_data (sub
)->csects
;
3839 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
3840 sym_hash
= obj_xcoff_sym_hashes (sub
);
3841 symesz
= bfd_coff_symesz (sub
);
3842 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3843 esymend
= esym
+ symcount
* symesz
;
3845 while (esym
< esymend
)
3847 struct internal_syment sym
;
3848 union internal_auxent aux
;
3853 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
3855 /* Read in the csect information, if any. */
3856 if (CSECT_SYM_P (sym
.n_sclass
))
3858 BFD_ASSERT (sym
.n_numaux
> 0);
3859 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
3860 sym
.n_type
, sym
.n_sclass
,
3861 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
3864 /* If this symbol's name is stored in the debug section,
3865 get a pointer to it. */
3866 if (debug_contents
!= NULL
3867 && sym
._n
._n_n
._n_zeroes
== 0
3868 && bfd_coff_symname_in_debug (sub
, &sym
))
3869 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3873 /* Decide whether to copy this symbol to the output file. */
3875 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
3876 *sym_hash
, csect
, name
);
3881 /* Use a debug_index of -2 to record that a symbol should
3886 /* See whether we should store the symbol name in the
3887 output .debug section. */
3892 indx
= _bfd_stringtab_add (debug_strtab
, name
, TRUE
, TRUE
);
3893 if (indx
== (bfd_size_type
) -1)
3895 *debug_index
= indx
;
3900 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
3901 if (*lineno_counts
> 0)
3902 csect
->output_section
->lineno_count
+= *lineno_counts
;
3905 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3906 csectpp
+= sym
.n_numaux
+ 1;
3907 sym_hash
+= sym
.n_numaux
+ 1;
3908 lineno_counts
+= sym
.n_numaux
+ 1;
3909 debug_index
+= sym
.n_numaux
+ 1;
3914 free (debug_contents
);
3915 debug_contents
= NULL
;
3917 /* Clear the size of subdeb, so that it is not included directly
3918 in the output file. */
3922 if (! info
->keep_memory
)
3924 if (! _bfd_coff_free_symbols (sub
))
3929 if (info
->strip
!= strip_all
)
3930 xcoff_hash_table (info
)->debug_section
->size
=
3931 _bfd_stringtab_size (debug_strtab
);
3936 if (ldinfo
.strings
!= NULL
)
3937 free (ldinfo
.strings
);
3938 if (debug_contents
!= NULL
)
3939 free (debug_contents
);
3944 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
3949 struct bfd_in_memory
*bim
;
3951 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
3958 abfd
->link_next
= 0;
3959 abfd
->format
= bfd_object
;
3960 abfd
->iostream
= (void *) bim
;
3961 abfd
->flags
= BFD_IN_MEMORY
;
3962 abfd
->direction
= write_direction
;
3965 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
3968 /* need to reset to unknown or it will not be read back in correctly */
3969 abfd
->format
= bfd_unknown
;
3970 abfd
->direction
= read_direction
;
3976 /* Return the section that defines H. Return null if no section does. */
3979 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
3981 switch (h
->root
.type
)
3983 case bfd_link_hash_defined
:
3984 case bfd_link_hash_defweak
:
3985 return h
->root
.u
.def
.section
;
3987 case bfd_link_hash_common
:
3988 return h
->root
.u
.c
.p
->section
;
3995 /* Add a .loader relocation for input relocation IREL. If the loader
3996 relocation should be against an output section, HSEC points to the
3997 input section that IREL is against, otherwise HSEC is null. H is the
3998 symbol that IREL is against, or null if it isn't against a global symbol.
3999 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4002 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*finfo
,
4003 asection
*output_section
, bfd
*reference_bfd
,
4004 struct internal_reloc
*irel
, asection
*hsec
,
4005 struct xcoff_link_hash_entry
*h
)
4007 struct internal_ldrel ldrel
;
4009 ldrel
.l_vaddr
= irel
->r_vaddr
;
4012 const char *secname
;
4014 secname
= hsec
->output_section
->name
;
4015 if (strcmp (secname
, ".text") == 0)
4017 else if (strcmp (secname
, ".data") == 0)
4019 else if (strcmp (secname
, ".bss") == 0)
4023 (*_bfd_error_handler
)
4024 (_("%B: loader reloc in unrecognized section `%s'"),
4025 reference_bfd
, secname
);
4026 bfd_set_error (bfd_error_nonrepresentable_section
);
4034 (*_bfd_error_handler
)
4035 (_("%B: `%s' in loader reloc but not loader sym"),
4036 reference_bfd
, h
->root
.root
.string
);
4037 bfd_set_error (bfd_error_bad_value
);
4040 ldrel
.l_symndx
= h
->ldindx
;
4043 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4045 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4046 ldrel
.l_rsecnm
= output_section
->target_index
;
4047 if (xcoff_hash_table (finfo
->info
)->textro
4048 && strcmp (output_section
->name
, ".text") == 0)
4050 (*_bfd_error_handler
)
4051 (_("%B: loader reloc in read-only section %A"),
4052 reference_bfd
, output_section
);
4053 bfd_set_error (bfd_error_invalid_operation
);
4056 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
4057 finfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4061 /* Link an input file into the linker output file. This function
4062 handles all the sections and relocations of the input file at once. */
4065 xcoff_link_input_bfd (struct xcoff_final_link_info
*finfo
,
4069 const char *strings
;
4070 bfd_size_type syment_base
;
4071 unsigned int n_tmask
;
4072 unsigned int n_btshft
;
4073 bfd_boolean copy
, hash
;
4074 bfd_size_type isymesz
;
4075 bfd_size_type osymesz
;
4076 bfd_size_type linesz
;
4079 struct xcoff_link_hash_entry
**sym_hash
;
4080 struct internal_syment
*isymp
;
4082 unsigned int *lineno_counts
;
4085 unsigned long output_index
;
4089 bfd_boolean keep_syms
;
4092 /* We can just skip DYNAMIC files, unless this is a static link. */
4093 if ((input_bfd
->flags
& DYNAMIC
) != 0
4094 && ! finfo
->info
->static_link
)
4097 /* Move all the symbols to the output file. */
4098 output_bfd
= finfo
->output_bfd
;
4100 syment_base
= obj_raw_syment_count (output_bfd
);
4101 isymesz
= bfd_coff_symesz (input_bfd
);
4102 osymesz
= bfd_coff_symesz (output_bfd
);
4103 linesz
= bfd_coff_linesz (input_bfd
);
4104 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4106 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4107 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4109 /* Define macros so that ISFCN, et. al., macros work correctly. */
4110 #define N_TMASK n_tmask
4111 #define N_BTSHFT n_btshft
4114 if (! finfo
->info
->keep_memory
)
4117 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4120 if (! _bfd_coff_get_external_symbols (input_bfd
))
4123 /* Make one pass over the symbols and assign indices to symbols that
4124 we have decided to keep. Also use create .loader symbol information
4125 and update information in hash table entries. */
4126 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4127 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4128 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4129 csectpp
= xcoff_data (input_bfd
)->csects
;
4130 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4131 isymp
= finfo
->internal_syms
;
4132 indexp
= finfo
->sym_indices
;
4133 output_index
= syment_base
;
4134 while (esym
< esym_end
)
4136 union internal_auxent aux
;
4140 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4142 /* Read in the csect information, if any. */
4143 if (CSECT_SYM_P (isymp
->n_sclass
))
4145 BFD_ASSERT (isymp
->n_numaux
> 0);
4146 bfd_coff_swap_aux_in (input_bfd
,
4147 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4148 isymp
->n_type
, isymp
->n_sclass
,
4149 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4152 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4155 /* If this symbol is in the .loader section, swap out the
4156 .loader symbol information. If this is an external symbol
4157 reference to a defined symbol, though, then wait until we get
4158 to the definition. */
4159 if (EXTERN_SYM_P (isymp
->n_sclass
)
4160 && *sym_hash
!= NULL
4161 && (*sym_hash
)->ldsym
!= NULL
4162 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4164 struct xcoff_link_hash_entry
*h
;
4165 struct internal_ldsym
*ldsym
;
4169 if (isymp
->n_scnum
> 0)
4171 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4172 ldsym
->l_value
= (isymp
->n_value
4173 + (*csectpp
)->output_section
->vma
4174 + (*csectpp
)->output_offset
4179 ldsym
->l_scnum
= isymp
->n_scnum
;
4180 ldsym
->l_value
= isymp
->n_value
;
4183 ldsym
->l_smtype
= smtyp
;
4184 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4185 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4186 || (h
->flags
& XCOFF_IMPORT
) != 0)
4187 ldsym
->l_smtype
|= L_IMPORT
;
4188 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4189 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4190 || (h
->flags
& XCOFF_EXPORT
) != 0)
4191 ldsym
->l_smtype
|= L_EXPORT
;
4192 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4193 ldsym
->l_smtype
|= L_ENTRY
;
4194 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4195 ldsym
->l_smtype
|= L_WEAK
;
4197 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4199 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4201 else if (ldsym
->l_ifile
== 0)
4203 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4209 if (h
->root
.type
== bfd_link_hash_defined
4210 || h
->root
.type
== bfd_link_hash_defweak
)
4211 impbfd
= h
->root
.u
.def
.section
->owner
;
4212 else if (h
->root
.type
== bfd_link_hash_undefined
4213 || h
->root
.type
== bfd_link_hash_undefweak
)
4214 impbfd
= h
->root
.u
.undef
.abfd
;
4222 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
4223 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4230 BFD_ASSERT (h
->ldindx
>= 0);
4231 bfd_xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
4234 * bfd_xcoff_ldsymsz (finfo
->output_bfd
))));
4237 /* Fill in snentry now that we know the target_index. */
4238 if ((h
->flags
& XCOFF_ENTRY
) != 0
4239 && (h
->root
.type
== bfd_link_hash_defined
4240 || h
->root
.type
== bfd_link_hash_defweak
))
4242 xcoff_data (output_bfd
)->snentry
=
4243 h
->root
.u
.def
.section
->output_section
->target_index
;
4247 add
= 1 + isymp
->n_numaux
;
4249 if (*debug_index
== -2)
4250 /* We've decided to strip this symbol. */
4254 /* Assign the next unused index to this symbol. */
4255 *indexp
= output_index
;
4257 if (EXTERN_SYM_P (isymp
->n_sclass
))
4259 BFD_ASSERT (*sym_hash
!= NULL
);
4260 (*sym_hash
)->indx
= output_index
;
4263 /* If this is a symbol in the TOC which we may have merged
4264 (class XMC_TC), remember the symbol index of the TOC
4266 if (isymp
->n_sclass
== C_HIDEXT
4267 && aux
.x_csect
.x_smclas
== XMC_TC
4268 && *sym_hash
!= NULL
)
4270 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4271 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4272 (*sym_hash
)->u
.toc_indx
= output_index
;
4275 output_index
+= add
;
4278 esym
+= add
* isymesz
;
4284 for (--add
; add
> 0; --add
)
4288 /* Now write out the symbols that we decided to keep. */
4290 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4291 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4292 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4293 isymp
= finfo
->internal_syms
;
4294 indexp
= finfo
->sym_indices
;
4295 csectpp
= xcoff_data (input_bfd
)->csects
;
4296 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4297 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4298 outsym
= finfo
->outsyms
;
4301 while (esym
< esym_end
)
4305 add
= 1 + isymp
->n_numaux
;
4308 esym
+= add
* isymesz
;
4311 struct internal_syment isym
;
4314 /* Adjust the symbol in order to output it. */
4316 if (isym
._n
._n_n
._n_zeroes
== 0
4317 && isym
._n
._n_n
._n_offset
!= 0)
4319 /* This symbol has a long name. Enter it in the string
4320 table we are building. If *debug_index != -1, the
4321 name has already been entered in the .debug section. */
4322 if (*debug_index
>= 0)
4323 isym
._n
._n_n
._n_offset
= *debug_index
;
4329 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4333 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
4334 if (indx
== (bfd_size_type
) -1)
4336 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4340 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4341 multiple definition problems when linking a shared object
4342 statically. (The native linker doesn't enter __rtinit into
4343 the normal table at all, but having a local symbol can make
4344 the objdump output easier to read.) */
4345 if (isym
.n_sclass
== C_EXT
4347 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4348 isym
.n_sclass
= C_HIDEXT
;
4350 /* The value of a C_FILE symbol is the symbol index of the
4351 next C_FILE symbol. The value of the last C_FILE symbol
4352 is -1. We try to get this right, below, just before we
4353 write the symbols out, but in the general case we may
4354 have to write the symbol out twice. */
4355 if (isym
.n_sclass
== C_FILE
)
4357 if (finfo
->last_file_index
!= -1
4358 && finfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4360 /* We must correct the value of the last C_FILE entry. */
4361 finfo
->last_file
.n_value
= *indexp
;
4362 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4364 /* The last C_FILE symbol is in this input file. */
4365 bfd_coff_swap_sym_out (output_bfd
,
4366 (void *) &finfo
->last_file
,
4367 (void *) (finfo
->outsyms
4368 + ((finfo
->last_file_index
4374 /* We have already written out the last C_FILE
4375 symbol. We need to write it out again. We
4376 borrow *outsym temporarily. */
4379 bfd_coff_swap_sym_out (output_bfd
,
4380 (void *) &finfo
->last_file
,
4383 pos
= obj_sym_filepos (output_bfd
);
4384 pos
+= finfo
->last_file_index
* osymesz
;
4385 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4386 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4392 finfo
->last_file_index
= *indexp
;
4393 finfo
->last_file
= isym
;
4396 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4397 into the line numbers. We update the symbol values when
4398 we handle the line numbers. */
4399 if (isym
.n_sclass
== C_BINCL
4400 || isym
.n_sclass
== C_EINCL
)
4402 isym
.n_value
= finfo
->line_filepos
;
4405 /* The value of a C_BSTAT symbol is the symbol table
4406 index of the containing csect. */
4407 else if (isym
.n_sclass
== C_BSTAT
)
4411 indx
= isym
.n_value
;
4412 if (indx
< obj_raw_syment_count (input_bfd
))
4416 symindx
= finfo
->sym_indices
[indx
];
4420 isym
.n_value
= symindx
;
4423 else if (isym
.n_sclass
!= C_ESTAT
4424 && isym
.n_sclass
!= C_DECL
4425 && isym
.n_scnum
> 0)
4427 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4428 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4429 + (*csectpp
)->output_offset
4433 /* Output the symbol. */
4434 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4439 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4441 union internal_auxent aux
;
4443 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4444 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4447 if (isymp
->n_sclass
== C_FILE
)
4449 /* This is the file name (or some comment put in by
4450 the compiler). If it is long, we must put it in
4451 the string table. */
4452 if (aux
.x_file
.x_n
.x_zeroes
== 0
4453 && aux
.x_file
.x_n
.x_offset
!= 0)
4455 const char *filename
;
4458 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4459 >= STRING_SIZE_SIZE
);
4460 if (strings
== NULL
)
4462 strings
= _bfd_coff_read_string_table (input_bfd
);
4463 if (strings
== NULL
)
4466 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4467 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
4469 if (indx
== (bfd_size_type
) -1)
4471 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4474 else if (CSECT_SYM_P (isymp
->n_sclass
)
4475 && i
+ 1 == isymp
->n_numaux
)
4478 /* We don't support type checking. I don't know if
4480 aux
.x_csect
.x_parmhash
= 0;
4481 /* I don't think anybody uses these fields, but we'd
4482 better clobber them just in case. */
4483 aux
.x_csect
.x_stab
= 0;
4484 aux
.x_csect
.x_snstab
= 0;
4486 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4490 indx
= aux
.x_csect
.x_scnlen
.l
;
4491 if (indx
< obj_raw_syment_count (input_bfd
))
4495 symindx
= finfo
->sym_indices
[indx
];
4498 aux
.x_csect
.x_scnlen
.l
= 0;
4502 aux
.x_csect
.x_scnlen
.l
= symindx
;
4507 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4511 if (ISFCN (isymp
->n_type
)
4512 || ISTAG (isymp
->n_sclass
)
4513 || isymp
->n_sclass
== C_BLOCK
4514 || isymp
->n_sclass
== C_FCN
)
4516 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4518 && indx
< obj_raw_syment_count (input_bfd
))
4520 /* We look forward through the symbol for
4521 the index of the next symbol we are going
4522 to include. I don't know if this is
4524 while (finfo
->sym_indices
[indx
] < 0
4525 && indx
< obj_raw_syment_count (input_bfd
))
4527 if (indx
>= obj_raw_syment_count (input_bfd
))
4528 indx
= output_index
;
4530 indx
= finfo
->sym_indices
[indx
];
4531 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4536 indx
= aux
.x_sym
.x_tagndx
.l
;
4537 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4541 symindx
= finfo
->sym_indices
[indx
];
4543 aux
.x_sym
.x_tagndx
.l
= 0;
4545 aux
.x_sym
.x_tagndx
.l
= symindx
;
4550 /* Copy over the line numbers, unless we are stripping
4551 them. We do this on a symbol by symbol basis in
4552 order to more easily handle garbage collection. */
4553 if (CSECT_SYM_P (isymp
->n_sclass
)
4555 && isymp
->n_numaux
> 1
4556 && ISFCN (isymp
->n_type
)
4557 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4559 if (*lineno_counts
== 0)
4560 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4563 asection
*enclosing
;
4564 unsigned int enc_count
;
4565 bfd_signed_vma linoff
;
4566 struct internal_lineno lin
;
4573 /* Read in the enclosing section's line-number
4574 information, if we haven't already. */
4576 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4577 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4578 if (oline
!= enclosing
)
4580 pos
= enclosing
->line_filepos
;
4581 amt
= linesz
* enc_count
;
4582 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4583 || (bfd_bread (finfo
->linenos
, amt
, input_bfd
)
4589 /* Copy across the first entry, adjusting its
4591 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4592 - enclosing
->line_filepos
);
4593 linp
= finfo
->linenos
+ linoff
;
4594 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4595 lin
.l_addr
.l_symndx
= *indexp
;
4596 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4598 /* Copy the other entries, adjusting their addresses. */
4599 linpend
= linp
+ *lineno_counts
* linesz
;
4600 offset
= (o
->output_section
->vma
4603 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4605 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4606 lin
.l_addr
.l_paddr
+= offset
;
4607 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4610 /* Write out the entries we've just processed. */
4611 pos
= (o
->output_section
->line_filepos
4612 + o
->output_section
->lineno_count
* linesz
);
4613 amt
= linesz
* *lineno_counts
;
4614 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4615 || bfd_bwrite (finfo
->linenos
+ linoff
,
4616 amt
, output_bfd
) != amt
)
4618 o
->output_section
->lineno_count
+= *lineno_counts
;
4620 /* Record the offset of the symbol's line numbers
4621 in the output file. */
4622 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4626 struct internal_syment
*iisp
, *iispend
;
4629 bfd_vma range_start
, range_end
;
4632 /* Update any C_BINCL or C_EINCL symbols
4633 that refer to a line number in the
4634 range we just output. */
4635 iisp
= finfo
->internal_syms
;
4636 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4637 iindp
= finfo
->sym_indices
;
4638 oos
= finfo
->outsyms
;
4639 range_start
= enclosing
->line_filepos
+ linoff
;
4640 range_end
= range_start
+ *lineno_counts
* linesz
;
4641 while (iisp
< iispend
)
4644 && (iisp
->n_sclass
== C_BINCL
4645 || iisp
->n_sclass
== C_EINCL
)
4646 && iisp
->n_value
>= range_start
4647 && iisp
->n_value
< range_end
)
4649 struct internal_syment iis
;
4651 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4652 iis
.n_value
= (iisp
->n_value
4655 bfd_coff_swap_sym_out (output_bfd
,
4660 iiadd
= 1 + iisp
->n_numaux
;
4662 oos
+= iiadd
* osymesz
;
4670 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4671 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4682 lineno_counts
+= add
;
4686 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4687 symbol will be the first symbol in the next input file. In the
4688 normal case, this will save us from writing out the C_FILE symbol
4690 if (finfo
->last_file_index
!= -1
4691 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4693 finfo
->last_file
.n_value
= output_index
;
4694 bfd_coff_swap_sym_out (output_bfd
, (void *) &finfo
->last_file
,
4695 (void *) (finfo
->outsyms
4696 + ((finfo
->last_file_index
- syment_base
)
4700 /* Write the modified symbols to the output file. */
4701 if (outsym
> finfo
->outsyms
)
4703 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4704 bfd_size_type amt
= outsym
- finfo
->outsyms
;
4705 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4706 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
4709 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4710 + (outsym
- finfo
->outsyms
) / osymesz
)
4713 obj_raw_syment_count (output_bfd
) = output_index
;
4716 /* Don't let the linker relocation routines discard the symbols. */
4717 keep_syms
= obj_coff_keep_syms (input_bfd
);
4718 obj_coff_keep_syms (input_bfd
) = TRUE
;
4720 /* Relocate the contents of each section. */
4721 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4725 if (! o
->linker_mark
)
4726 /* This section was omitted from the link. */
4729 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4731 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4734 /* We have set filepos correctly for the sections we created to
4735 represent csects, so bfd_get_section_contents should work. */
4736 if (coff_section_data (input_bfd
, o
) != NULL
4737 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4738 contents
= coff_section_data (input_bfd
, o
)->contents
;
4741 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4742 if (!bfd_get_section_contents (input_bfd
, o
, finfo
->contents
, 0, sz
))
4744 contents
= finfo
->contents
;
4747 if ((o
->flags
& SEC_RELOC
) != 0)
4750 struct internal_reloc
*internal_relocs
;
4751 struct internal_reloc
*irel
;
4753 struct internal_reloc
*irelend
;
4754 struct xcoff_link_hash_entry
**rel_hash
;
4757 /* Read in the relocs. */
4758 target_index
= o
->output_section
->target_index
;
4759 internal_relocs
= (xcoff_read_internal_relocs
4760 (input_bfd
, o
, FALSE
, finfo
->external_relocs
,
4762 (finfo
->section_info
[target_index
].relocs
4763 + o
->output_section
->reloc_count
)));
4764 if (internal_relocs
== NULL
)
4767 /* Call processor specific code to relocate the section
4769 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
4773 finfo
->internal_syms
,
4774 xcoff_data (input_bfd
)->csects
))
4777 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4778 irel
= internal_relocs
;
4779 irelend
= irel
+ o
->reloc_count
;
4780 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
4781 + o
->output_section
->reloc_count
);
4782 for (; irel
< irelend
; irel
++, rel_hash
++)
4784 struct xcoff_link_hash_entry
*h
= NULL
;
4788 /* Adjust the reloc address and symbol index. */
4790 irel
->r_vaddr
+= offset
;
4792 r_symndx
= irel
->r_symndx
;
4797 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4799 if (r_symndx
!= -1 && finfo
->info
->strip
!= strip_all
)
4802 && h
->smclas
!= XMC_TD
4803 && (irel
->r_type
== R_TOC
4804 || irel
->r_type
== R_GL
4805 || irel
->r_type
== R_TCL
4806 || irel
->r_type
== R_TRL
4807 || irel
->r_type
== R_TRLA
))
4809 /* This is a TOC relative reloc with a symbol
4810 attached. The symbol should be the one which
4811 this reloc is for. We want to make this
4812 reloc against the TOC address of the symbol,
4813 not the symbol itself. */
4814 BFD_ASSERT (h
->toc_section
!= NULL
);
4815 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4816 if (h
->u
.toc_indx
!= -1)
4817 irel
->r_symndx
= h
->u
.toc_indx
;
4820 struct xcoff_toc_rel_hash
*n
;
4821 struct xcoff_link_section_info
*si
;
4825 n
= bfd_alloc (finfo
->output_bfd
, amt
);
4828 si
= finfo
->section_info
+ target_index
;
4829 n
->next
= si
->toc_rel_hashes
;
4832 si
->toc_rel_hashes
= n
;
4837 /* This is a global symbol. */
4839 irel
->r_symndx
= h
->indx
;
4842 /* This symbol is being written at the end
4843 of the file, and we do not yet know the
4844 symbol index. We save the pointer to the
4845 hash table entry in the rel_hash list.
4846 We set the indx field to -2 to indicate
4847 that this symbol must not be stripped. */
4856 indx
= finfo
->sym_indices
[r_symndx
];
4860 struct internal_syment
*is
;
4862 /* Relocations against a TC0 TOC anchor are
4863 automatically transformed to be against
4864 the TOC anchor in the output file. */
4865 is
= finfo
->internal_syms
+ r_symndx
;
4866 if (is
->n_sclass
== C_HIDEXT
4867 && is
->n_numaux
> 0)
4870 union internal_auxent aux
;
4874 obj_coff_external_syms (input_bfd
))
4875 + ((r_symndx
+ is
->n_numaux
)
4877 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4878 is
->n_type
, is
->n_sclass
,
4882 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4883 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4884 indx
= finfo
->toc_symindx
;
4889 irel
->r_symndx
= indx
;
4893 struct internal_syment
*is
;
4896 char buf
[SYMNMLEN
+ 1];
4898 /* This reloc is against a symbol we are
4899 stripping. It would be possible to handle
4900 this case, but I don't think it's worth it. */
4901 is
= finfo
->internal_syms
+ r_symndx
;
4903 name
= (_bfd_coff_internal_syment_name
4904 (input_bfd
, is
, buf
));
4909 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4910 (finfo
->info
, name
, input_bfd
, o
,
4917 if (xcoff_need_ldrel_p (finfo
->info
, irel
, h
))
4924 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4926 sec
= xcoff_symbol_section (h
);
4927 if (!xcoff_create_ldrel (output_bfd
, finfo
,
4928 o
->output_section
, input_bfd
,
4934 o
->output_section
->reloc_count
+= o
->reloc_count
;
4937 /* Write out the modified section contents. */
4938 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4939 contents
, (file_ptr
) o
->output_offset
,
4944 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4946 if (! finfo
->info
->keep_memory
)
4948 if (! _bfd_coff_free_symbols (input_bfd
))
4958 /* Sort relocs by VMA. This is called via qsort. */
4961 xcoff_sort_relocs (const void * p1
, const void * p2
)
4963 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
4964 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
4966 if (r1
->r_vaddr
> r2
->r_vaddr
)
4968 else if (r1
->r_vaddr
< r2
->r_vaddr
)
4974 /* Return true if section SEC is a TOC section. */
4976 static inline bfd_boolean
4977 xcoff_toc_section_p (asection
*sec
)
4982 if (name
[0] == '.' && name
[1] == 't')
4986 if (name
[3] == '0' && name
[4] == 0)
4991 if (name
[2] == 'd' && name
[3] == 0)
4997 /* See if the link requires a TOC (it usually does!). If so, find a
4998 good place to put the TOC anchor csect, and write out the associated
5002 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*finfo
)
5004 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5008 struct internal_syment irsym
;
5009 union internal_auxent iraux
;
5013 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5014 index of a csect at the beginning of the TOC. */
5015 toc_start
= ~(bfd_vma
) 0;
5018 for (input_bfd
= finfo
->info
->input_bfds
;
5020 input_bfd
= input_bfd
->link_next
)
5021 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5022 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5024 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5025 if (toc_start
> start
)
5028 section_index
= sec
->output_section
->target_index
;
5031 end
= start
+ sec
->size
;
5036 /* There's no need for a TC0 symbol if we don't have a TOC. */
5037 if (toc_end
< toc_start
)
5039 xcoff_data (output_bfd
)->toc
= toc_start
;
5043 if (toc_end
- toc_start
< 0x8000)
5044 /* Every TOC csect can be accessed from TOC_START. */
5045 best_address
= toc_start
;
5048 /* Find the lowest TOC csect that is still within range of TOC_END. */
5049 best_address
= toc_end
;
5050 for (input_bfd
= finfo
->info
->input_bfds
;
5052 input_bfd
= input_bfd
->link_next
)
5053 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5054 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5056 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5057 if (start
< best_address
5058 && start
+ 0x8000 >= toc_end
)
5060 best_address
= start
;
5061 section_index
= sec
->output_section
->target_index
;
5065 /* Make sure that the start of the TOC is also within range. */
5066 if (best_address
> toc_start
+ 0x8000)
5068 (*_bfd_error_handler
)
5069 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5071 (unsigned long) (toc_end
- toc_start
));
5072 bfd_set_error (bfd_error_file_too_big
);
5077 /* Record the chosen TOC value. */
5078 finfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5079 xcoff_data (output_bfd
)->toc
= best_address
;
5080 xcoff_data (output_bfd
)->sntoc
= section_index
;
5082 /* Fill out the TC0 symbol. */
5083 if (!bfd_xcoff_put_symbol_name (output_bfd
, finfo
->strtab
, &irsym
, "TOC"))
5085 irsym
.n_value
= best_address
;
5086 irsym
.n_scnum
= section_index
;
5087 irsym
.n_sclass
= C_HIDEXT
;
5088 irsym
.n_type
= T_NULL
;
5090 bfd_coff_swap_sym_out (output_bfd
, &irsym
, finfo
->outsyms
);
5092 /* Fill out the auxillary csect information. */
5093 memset (&iraux
, 0, sizeof iraux
);
5094 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5095 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5096 iraux
.x_csect
.x_scnlen
.l
= 0;
5097 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5098 finfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5100 /* Write the contents to the file. */
5101 pos
= obj_sym_filepos (output_bfd
);
5102 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5103 size
= 2 * bfd_coff_symesz (output_bfd
);
5104 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5105 || bfd_bwrite (finfo
->outsyms
, size
, output_bfd
) != size
)
5107 obj_raw_syment_count (output_bfd
) += 2;
5112 /* Write out a non-XCOFF global symbol. */
5115 xcoff_write_global_symbol (struct xcoff_link_hash_entry
*h
, void * inf
)
5117 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) inf
;
5120 struct internal_syment isym
;
5121 union internal_auxent aux
;
5126 output_bfd
= finfo
->output_bfd
;
5127 outsym
= finfo
->outsyms
;
5129 if (h
->root
.type
== bfd_link_hash_warning
)
5131 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5132 if (h
->root
.type
== bfd_link_hash_new
)
5136 /* If this symbol was garbage collected, just skip it. */
5137 if (xcoff_hash_table (finfo
->info
)->gc
5138 && (h
->flags
& XCOFF_MARK
) == 0)
5141 /* If we need a .loader section entry, write it out. */
5142 if (h
->ldsym
!= NULL
)
5144 struct internal_ldsym
*ldsym
;
5149 if (h
->root
.type
== bfd_link_hash_undefined
5150 || h
->root
.type
== bfd_link_hash_undefweak
)
5154 ldsym
->l_scnum
= N_UNDEF
;
5155 ldsym
->l_smtype
= XTY_ER
;
5156 impbfd
= h
->root
.u
.undef
.abfd
;
5159 else if (h
->root
.type
== bfd_link_hash_defined
5160 || h
->root
.type
== bfd_link_hash_defweak
)
5164 sec
= h
->root
.u
.def
.section
;
5165 ldsym
->l_value
= (sec
->output_section
->vma
5166 + sec
->output_offset
5167 + h
->root
.u
.def
.value
);
5168 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5169 ldsym
->l_smtype
= XTY_SD
;
5170 impbfd
= sec
->owner
;
5176 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5177 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5178 || (h
->flags
& XCOFF_IMPORT
) != 0)
5180 Import symbols are defined so the check above will make
5181 the l_smtype XTY_SD. But this is not correct, it should
5183 ldsym
->l_smtype
|= L_IMPORT
;
5185 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5186 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5187 || (h
->flags
& XCOFF_EXPORT
) != 0)
5188 ldsym
->l_smtype
|= L_EXPORT
;
5190 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5191 ldsym
->l_smtype
|= L_ENTRY
;
5193 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5194 ldsym
->l_smtype
= XTY_SD
;
5196 ldsym
->l_smclas
= h
->smclas
;
5198 if (ldsym
->l_smtype
& L_IMPORT
)
5200 if ((h
->root
.type
== bfd_link_hash_defined
5201 || h
->root
.type
== bfd_link_hash_defweak
)
5202 && (h
->root
.u
.def
.value
!= 0))
5203 ldsym
->l_smclas
= XMC_XO
;
5205 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5206 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5207 ldsym
->l_smclas
= XMC_SV3264
;
5209 else if (h
->flags
& XCOFF_SYSCALL32
)
5210 ldsym
->l_smclas
= XMC_SV
;
5212 else if (h
->flags
& XCOFF_SYSCALL64
)
5213 ldsym
->l_smclas
= XMC_SV64
;
5216 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5220 else if (ldsym
->l_ifile
== 0)
5222 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5224 else if (impbfd
== NULL
)
5228 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5229 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5235 BFD_ASSERT (h
->ldindx
>= 0);
5237 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5240 * bfd_xcoff_ldsymsz(finfo
->output_bfd
)));
5244 /* If this symbol needs global linkage code, write it out. */
5245 if (h
->root
.type
== bfd_link_hash_defined
5246 && (h
->root
.u
.def
.section
5247 == xcoff_hash_table (finfo
->info
)->linkage_section
))
5253 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5255 /* The first instruction in the global linkage code loads a
5256 specific TOC element. */
5257 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5258 + h
->descriptor
->toc_section
->output_offset
5259 - xcoff_data (output_bfd
)->toc
);
5261 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5262 tocoff
+= h
->descriptor
->u
.toc_offset
;
5264 /* The first instruction in the glink code needs to be
5265 cooked to to hold the correct offset in the toc. The
5266 rest are just output raw. */
5267 bfd_put_32 (output_bfd
,
5268 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5270 /* Start with i == 1 to get past the first instruction done above
5271 The /4 is because the glink code is in bytes and we are going
5273 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5274 bfd_put_32 (output_bfd
,
5275 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5279 /* If we created a TOC entry for this symbol, write out the required
5281 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5286 struct internal_reloc
*irel
;
5287 struct internal_syment irsym
;
5288 union internal_auxent iraux
;
5290 tocsec
= h
->toc_section
;
5291 osec
= tocsec
->output_section
;
5292 oindx
= osec
->target_index
;
5293 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5294 irel
->r_vaddr
= (osec
->vma
5295 + tocsec
->output_offset
5299 irel
->r_symndx
= h
->indx
;
5303 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5306 BFD_ASSERT (h
->ldindx
>= 0);
5308 /* Initialize the aux union here instead of closer to when it is
5309 written out below because the length of the csect depends on
5310 whether the output is 32 or 64 bit. */
5311 memset (&iraux
, 0, sizeof iraux
);
5312 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5313 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5314 iraux
.x_csect
.x_smclas
= XMC_TC
;
5316 /* 32 bit uses a 32 bit R_POS to do the relocations
5317 64 bit uses a 64 bit R_POS to do the relocations
5319 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5321 Which one is determined by the backend. */
5322 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5325 iraux
.x_csect
.x_scnlen
.l
= 8;
5327 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5330 iraux
.x_csect
.x_scnlen
.l
= 4;
5335 irel
->r_type
= R_POS
;
5336 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5337 ++osec
->reloc_count
;
5339 if (!xcoff_create_ldrel (output_bfd
, finfo
, osec
,
5340 output_bfd
, irel
, NULL
, h
))
5343 /* We need to emit a symbol to define a csect which holds
5345 if (finfo
->info
->strip
!= strip_all
)
5347 result
= bfd_xcoff_put_symbol_name (output_bfd
, finfo
->strtab
,
5348 &irsym
, h
->root
.root
.string
);
5352 irsym
.n_value
= irel
->r_vaddr
;
5353 irsym
.n_scnum
= osec
->target_index
;
5354 irsym
.n_sclass
= C_HIDEXT
;
5355 irsym
.n_type
= T_NULL
;
5358 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5359 outsym
+= bfd_coff_symesz (output_bfd
);
5361 /* Note : iraux is initialized above. */
5362 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5363 0, 1, (void *) outsym
);
5364 outsym
+= bfd_coff_auxesz (output_bfd
);
5368 /* We aren't going to write out the symbols below, so we
5369 need to write them out now. */
5370 pos
= obj_sym_filepos (output_bfd
);
5371 pos
+= (obj_raw_syment_count (output_bfd
)
5372 * bfd_coff_symesz (output_bfd
));
5373 amt
= outsym
- finfo
->outsyms
;
5374 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5375 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
5377 obj_raw_syment_count (output_bfd
) +=
5378 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5380 outsym
= finfo
->outsyms
;
5385 /* If this symbol is a specially defined function descriptor, write
5386 it out. The first word is the address of the function code
5387 itself, the second word is the address of the TOC, and the third
5391 The addresses for the 32 bit will take 4 bytes and the addresses
5392 for 64 bit will take 8 bytes. Similar for the relocs. This type
5393 of logic was also done above to create a TOC entry in
5394 xcoff_write_global_symbol. */
5395 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5396 && h
->root
.type
== bfd_link_hash_defined
5397 && (h
->root
.u
.def
.section
5398 == xcoff_hash_table (finfo
->info
)->descriptor_section
))
5404 struct xcoff_link_hash_entry
*hentry
;
5406 struct internal_reloc
*irel
;
5408 unsigned int reloc_size
, byte_size
;
5410 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5415 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5423 sec
= h
->root
.u
.def
.section
;
5424 osec
= sec
->output_section
;
5425 oindx
= osec
->target_index
;
5426 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5428 hentry
= h
->descriptor
;
5429 BFD_ASSERT (hentry
!= NULL
5430 && (hentry
->root
.type
== bfd_link_hash_defined
5431 || hentry
->root
.type
== bfd_link_hash_defweak
));
5432 esec
= hentry
->root
.u
.def
.section
;
5434 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5435 irel
->r_vaddr
= (osec
->vma
5436 + sec
->output_offset
5437 + h
->root
.u
.def
.value
);
5438 irel
->r_symndx
= esec
->output_section
->target_index
;
5439 irel
->r_type
= R_POS
;
5440 irel
->r_size
= reloc_size
;
5441 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5442 ++osec
->reloc_count
;
5444 if (!xcoff_create_ldrel (output_bfd
, finfo
, osec
,
5445 output_bfd
, irel
, esec
, NULL
))
5448 /* There are three items to write out,
5449 the address of the code
5450 the address of the toc anchor
5451 the environment pointer.
5452 We are ignoring the environment pointer. So set it to zero. */
5453 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5455 bfd_put_64 (output_bfd
,
5456 (esec
->output_section
->vma
+ esec
->output_offset
5457 + hentry
->root
.u
.def
.value
),
5459 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5460 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5465 This logic was already called above so the error case where
5466 the backend is neither has already been checked. */
5467 bfd_put_32 (output_bfd
,
5468 (esec
->output_section
->vma
+ esec
->output_offset
5469 + hentry
->root
.u
.def
.value
),
5471 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5472 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5475 tsec
= coff_section_from_bfd_index (output_bfd
,
5476 xcoff_data (output_bfd
)->sntoc
);
5479 irel
->r_vaddr
= (osec
->vma
5480 + sec
->output_offset
5481 + h
->root
.u
.def
.value
5483 irel
->r_symndx
= tsec
->output_section
->target_index
;
5484 irel
->r_type
= R_POS
;
5485 irel
->r_size
= reloc_size
;
5486 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5487 ++osec
->reloc_count
;
5489 if (!xcoff_create_ldrel (output_bfd
, finfo
, osec
,
5490 output_bfd
, irel
, tsec
, NULL
))
5494 if (h
->indx
>= 0 || finfo
->info
->strip
== strip_all
)
5496 BFD_ASSERT (outsym
== finfo
->outsyms
);
5501 && (finfo
->info
->strip
== strip_all
5502 || (finfo
->info
->strip
== strip_some
5503 && bfd_hash_lookup (finfo
->info
->keep_hash
, h
->root
.root
.string
,
5504 FALSE
, FALSE
) == NULL
)))
5506 BFD_ASSERT (outsym
== finfo
->outsyms
);
5511 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5513 BFD_ASSERT (outsym
== finfo
->outsyms
);
5517 memset (&aux
, 0, sizeof aux
);
5519 h
->indx
= obj_raw_syment_count (output_bfd
);
5521 result
= bfd_xcoff_put_symbol_name (output_bfd
, finfo
->strtab
, &isym
,
5522 h
->root
.root
.string
);
5526 if (h
->root
.type
== bfd_link_hash_undefined
5527 || h
->root
.type
== bfd_link_hash_undefweak
)
5530 isym
.n_scnum
= N_UNDEF
;
5531 if (h
->root
.type
== bfd_link_hash_undefweak
5532 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5533 isym
.n_sclass
= C_WEAKEXT
;
5535 isym
.n_sclass
= C_EXT
;
5536 aux
.x_csect
.x_smtyp
= XTY_ER
;
5538 else if ((h
->root
.type
== bfd_link_hash_defined
5539 || h
->root
.type
== bfd_link_hash_defweak
)
5540 && h
->smclas
== XMC_XO
)
5542 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5543 isym
.n_value
= h
->root
.u
.def
.value
;
5544 isym
.n_scnum
= N_UNDEF
;
5545 if (h
->root
.type
== bfd_link_hash_undefweak
5546 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5547 isym
.n_sclass
= C_WEAKEXT
;
5549 isym
.n_sclass
= C_EXT
;
5550 aux
.x_csect
.x_smtyp
= XTY_ER
;
5552 else if (h
->root
.type
== bfd_link_hash_defined
5553 || h
->root
.type
== bfd_link_hash_defweak
)
5555 struct xcoff_link_size_list
*l
;
5557 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5558 + h
->root
.u
.def
.section
->output_offset
5559 + h
->root
.u
.def
.value
);
5560 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5561 isym
.n_scnum
= N_ABS
;
5563 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5564 isym
.n_sclass
= C_HIDEXT
;
5565 aux
.x_csect
.x_smtyp
= XTY_SD
;
5567 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5569 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
5575 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5581 else if (h
->root
.type
== bfd_link_hash_common
)
5583 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5584 + h
->root
.u
.c
.p
->section
->output_offset
);
5585 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5586 isym
.n_sclass
= C_EXT
;
5587 aux
.x_csect
.x_smtyp
= XTY_CM
;
5588 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5593 isym
.n_type
= T_NULL
;
5596 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5597 outsym
+= bfd_coff_symesz (output_bfd
);
5599 aux
.x_csect
.x_smclas
= h
->smclas
;
5600 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5602 outsym
+= bfd_coff_auxesz (output_bfd
);
5604 if ((h
->root
.type
== bfd_link_hash_defined
5605 || h
->root
.type
== bfd_link_hash_defweak
)
5606 && h
->smclas
!= XMC_XO
)
5608 /* We just output an SD symbol. Now output an LD symbol. */
5611 if (h
->root
.type
== bfd_link_hash_undefweak
5612 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5613 isym
.n_sclass
= C_WEAKEXT
;
5615 isym
.n_sclass
= C_EXT
;
5616 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5617 outsym
+= bfd_coff_symesz (output_bfd
);
5619 aux
.x_csect
.x_smtyp
= XTY_LD
;
5620 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5621 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5623 outsym
+= bfd_coff_auxesz (output_bfd
);
5626 pos
= obj_sym_filepos (output_bfd
);
5627 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5628 amt
= outsym
- finfo
->outsyms
;
5629 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5630 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
5632 obj_raw_syment_count (output_bfd
) +=
5633 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5638 /* Handle a link order which is supposed to generate a reloc. */
5641 xcoff_reloc_link_order (bfd
*output_bfd
,
5642 struct xcoff_final_link_info
*finfo
,
5643 asection
*output_section
,
5644 struct bfd_link_order
*link_order
)
5646 reloc_howto_type
*howto
;
5647 struct xcoff_link_hash_entry
*h
;
5651 struct internal_reloc
*irel
;
5652 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5654 if (link_order
->type
== bfd_section_reloc_link_order
)
5655 /* We need to somehow locate a symbol in the right section. The
5656 symbol must either have a value of zero, or we must adjust
5657 the addend by the value of the symbol. FIXME: Write this
5658 when we need it. The old linker couldn't handle this anyhow. */
5661 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5664 bfd_set_error (bfd_error_bad_value
);
5668 h
= ((struct xcoff_link_hash_entry
*)
5669 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
5670 link_order
->u
.reloc
.p
->u
.name
,
5671 FALSE
, FALSE
, TRUE
));
5674 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
5675 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0)))
5680 hsec
= xcoff_symbol_section (h
);
5681 if (h
->root
.type
== bfd_link_hash_defined
5682 || h
->root
.type
== bfd_link_hash_defweak
)
5683 hval
= h
->root
.u
.def
.value
;
5687 addend
= link_order
->u
.reloc
.p
->addend
;
5689 addend
+= (hsec
->output_section
->vma
5690 + hsec
->output_offset
5697 bfd_reloc_status_type rstat
;
5700 size
= bfd_get_reloc_size (howto
);
5701 buf
= bfd_zmalloc (size
);
5705 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5711 case bfd_reloc_outofrange
:
5713 case bfd_reloc_overflow
:
5714 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
5715 (finfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5716 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0)))
5723 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5724 (file_ptr
) link_order
->offset
, size
);
5730 /* Store the reloc information in the right place. It will get
5731 swapped and written out at the end of the final_link routine. */
5732 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
5733 + output_section
->reloc_count
);
5734 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
5735 + output_section
->reloc_count
);
5737 memset (irel
, 0, sizeof (struct internal_reloc
));
5738 *rel_hash_ptr
= NULL
;
5740 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5743 irel
->r_symndx
= h
->indx
;
5746 /* Set the index to -2 to force this symbol to get written out. */
5752 irel
->r_type
= howto
->type
;
5753 irel
->r_size
= howto
->bitsize
- 1;
5754 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5755 irel
->r_size
|= 0x80;
5757 ++output_section
->reloc_count
;
5759 /* Now output the reloc to the .loader section. */
5760 if (xcoff_hash_table (finfo
->info
)->loader_section
)
5762 if (!xcoff_create_ldrel (output_bfd
, finfo
, output_section
,
5763 output_bfd
, irel
, hsec
, h
))
5770 /* Do the final link step. */
5773 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5775 bfd_size_type symesz
;
5776 struct xcoff_final_link_info finfo
;
5778 struct bfd_link_order
*p
;
5779 bfd_size_type max_contents_size
;
5780 bfd_size_type max_sym_count
;
5781 bfd_size_type max_lineno_count
;
5782 bfd_size_type max_reloc_count
;
5783 bfd_size_type max_output_reloc_count
;
5784 file_ptr rel_filepos
;
5786 file_ptr line_filepos
;
5787 unsigned int linesz
;
5789 bfd_byte
*external_relocs
= NULL
;
5790 char strbuf
[STRING_SIZE_SIZE
];
5795 abfd
->flags
|= DYNAMIC
;
5797 symesz
= bfd_coff_symesz (abfd
);
5800 finfo
.output_bfd
= abfd
;
5801 finfo
.strtab
= NULL
;
5802 finfo
.section_info
= NULL
;
5803 finfo
.last_file_index
= -1;
5804 finfo
.toc_symindx
= -1;
5805 finfo
.internal_syms
= NULL
;
5806 finfo
.sym_indices
= NULL
;
5807 finfo
.outsyms
= NULL
;
5808 finfo
.linenos
= NULL
;
5809 finfo
.contents
= NULL
;
5810 finfo
.external_relocs
= NULL
;
5812 if (xcoff_hash_table (info
)->loader_section
)
5814 finfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5815 + bfd_xcoff_ldhdrsz (abfd
));
5816 finfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5817 + bfd_xcoff_ldhdrsz (abfd
)
5818 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5819 * bfd_xcoff_ldsymsz (abfd
)));
5827 xcoff_data (abfd
)->coff
.link_info
= info
;
5829 finfo
.strtab
= _bfd_stringtab_init ();
5830 if (finfo
.strtab
== NULL
)
5833 /* Count the relocation entries required for the output file.
5834 (We've already counted the line numbers.) Determine a few
5836 max_contents_size
= 0;
5837 max_lineno_count
= 0;
5838 max_reloc_count
= 0;
5839 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5842 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5844 if (p
->type
== bfd_indirect_link_order
)
5848 sec
= p
->u
.indirect
.section
;
5850 /* Mark all sections which are to be included in the
5851 link. This will normally be every section. We need
5852 to do this so that we can identify any sections which
5853 the linker has decided to not include. */
5854 sec
->linker_mark
= TRUE
;
5856 o
->reloc_count
+= sec
->reloc_count
;
5858 if (sec
->rawsize
> max_contents_size
)
5859 max_contents_size
= sec
->rawsize
;
5860 if (sec
->size
> max_contents_size
)
5861 max_contents_size
= sec
->size
;
5862 if (coff_section_data (sec
->owner
, sec
) != NULL
5863 && xcoff_section_data (sec
->owner
, sec
) != NULL
5864 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5865 > max_lineno_count
))
5867 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5868 if (sec
->reloc_count
> max_reloc_count
)
5869 max_reloc_count
= sec
->reloc_count
;
5871 else if (p
->type
== bfd_section_reloc_link_order
5872 || p
->type
== bfd_symbol_reloc_link_order
)
5877 /* Compute the file positions for all the sections. */
5878 if (abfd
->output_has_begun
)
5880 if (xcoff_hash_table (info
)->file_align
!= 0)
5887 file_align
= xcoff_hash_table (info
)->file_align
;
5888 if (file_align
!= 0)
5890 bfd_boolean saw_contents
;
5894 /* Insert .pad sections before every section which has
5895 contents and is loaded, if it is preceded by some other
5896 section which has contents and is loaded. */
5897 saw_contents
= TRUE
;
5898 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5900 if (strcmp (o
->name
, ".pad") == 0)
5901 saw_contents
= FALSE
;
5902 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5903 && (o
->flags
& SEC_LOAD
) != 0)
5906 saw_contents
= TRUE
;
5911 /* Create a pad section and place it before the section
5912 that needs padding. This requires unlinking and
5913 relinking the bfd's section list. */
5915 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
5917 n
->alignment_power
= 0;
5919 bfd_section_list_remove (abfd
, n
);
5920 bfd_section_list_insert_before (abfd
, o
, n
);
5921 saw_contents
= FALSE
;
5926 /* Reset the section indices after inserting the new
5929 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5932 o
->target_index
= indx
;
5934 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
5936 /* Work out appropriate sizes for the .pad sections to force
5937 each section to land on a page boundary. This bit of
5938 code knows what compute_section_file_positions is going
5940 sofar
= bfd_coff_filhsz (abfd
);
5941 sofar
+= bfd_coff_aoutsz (abfd
);
5942 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
5943 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5944 if ((bfd_xcoff_is_reloc_count_overflow
5945 (abfd
, (bfd_vma
) o
->reloc_count
))
5946 || (bfd_xcoff_is_lineno_count_overflow
5947 (abfd
, (bfd_vma
) o
->lineno_count
)))
5948 /* 64 does not overflow, need to check if 32 does */
5949 sofar
+= bfd_coff_scnhsz (abfd
);
5951 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5953 if (strcmp (o
->name
, ".pad") == 0)
5957 BFD_ASSERT (o
->size
== 0);
5958 pageoff
= sofar
& (file_align
- 1);
5961 o
->size
= file_align
- pageoff
;
5962 sofar
+= file_align
- pageoff
;
5963 o
->flags
|= SEC_HAS_CONTENTS
;
5968 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
5969 sofar
+= BFD_ALIGN (o
->size
,
5970 1 << o
->alignment_power
);
5975 if (! bfd_coff_compute_section_file_positions (abfd
))
5979 /* Allocate space for the pointers we need to keep for the relocs. */
5983 /* We use section_count + 1, rather than section_count, because
5984 the target_index fields are 1 based. */
5985 amt
= abfd
->section_count
+ 1;
5986 amt
*= sizeof (struct xcoff_link_section_info
);
5987 finfo
.section_info
= bfd_malloc (amt
);
5988 if (finfo
.section_info
== NULL
)
5990 for (i
= 0; i
<= abfd
->section_count
; i
++)
5992 finfo
.section_info
[i
].relocs
= NULL
;
5993 finfo
.section_info
[i
].rel_hashes
= NULL
;
5994 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
5998 /* Set the file positions for the relocs. */
5999 rel_filepos
= obj_relocbase (abfd
);
6000 relsz
= bfd_coff_relsz (abfd
);
6001 max_output_reloc_count
= 0;
6002 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6004 if (o
->reloc_count
== 0)
6008 /* A stripped file has no relocs. However, we still
6009 allocate the buffers, so that later code doesn't have to
6010 worry about whether we are stripping or not. */
6011 if (info
->strip
== strip_all
)
6015 o
->flags
|= SEC_RELOC
;
6016 o
->rel_filepos
= rel_filepos
;
6017 rel_filepos
+= o
->reloc_count
* relsz
;
6020 /* We don't know the indices of global symbols until we have
6021 written out all the local symbols. For each section in
6022 the output file, we keep an array of pointers to hash
6023 table entries. Each entry in the array corresponds to a
6024 reloc. When we find a reloc against a global symbol, we
6025 set the corresponding entry in this array so that we can
6026 fix up the symbol index after we have written out all the
6029 Because of this problem, we also keep the relocs in
6030 memory until the end of the link. This wastes memory.
6031 We could backpatch the file later, I suppose, although it
6033 amt
= o
->reloc_count
;
6034 amt
*= sizeof (struct internal_reloc
);
6035 finfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6037 amt
= o
->reloc_count
;
6038 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6039 finfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6041 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
6042 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6045 if (o
->reloc_count
> max_output_reloc_count
)
6046 max_output_reloc_count
= o
->reloc_count
;
6050 /* We now know the size of the relocs, so we can determine the file
6051 positions of the line numbers. */
6052 line_filepos
= rel_filepos
;
6053 finfo
.line_filepos
= line_filepos
;
6054 linesz
= bfd_coff_linesz (abfd
);
6055 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6057 if (o
->lineno_count
== 0)
6058 o
->line_filepos
= 0;
6061 o
->line_filepos
= line_filepos
;
6062 line_filepos
+= o
->lineno_count
* linesz
;
6065 /* Reset the reloc and lineno counts, so that we can use them to
6066 count the number of entries we have output so far. */
6068 o
->lineno_count
= 0;
6071 obj_sym_filepos (abfd
) = line_filepos
;
6073 /* Figure out the largest number of symbols in an input BFD. Take
6074 the opportunity to clear the output_has_begun fields of all the
6075 input BFD's. We want at least 6 symbols, since that is the
6076 number which xcoff_write_global_symbol may need. */
6078 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6082 sub
->output_has_begun
= FALSE
;
6083 sz
= obj_raw_syment_count (sub
);
6084 if (sz
> max_sym_count
)
6088 /* Allocate some buffers used while linking. */
6089 amt
= max_sym_count
* sizeof (struct internal_syment
);
6090 finfo
.internal_syms
= bfd_malloc (amt
);
6092 amt
= max_sym_count
* sizeof (long);
6093 finfo
.sym_indices
= bfd_malloc (amt
);
6095 amt
= (max_sym_count
+ 1) * symesz
;
6096 finfo
.outsyms
= bfd_malloc (amt
);
6098 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6099 finfo
.linenos
= bfd_malloc (amt
);
6101 amt
= max_contents_size
;
6102 finfo
.contents
= bfd_malloc (amt
);
6104 amt
= max_reloc_count
* relsz
;
6105 finfo
.external_relocs
= bfd_malloc (amt
);
6107 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6108 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6109 || finfo
.outsyms
== NULL
6110 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
6111 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
6112 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6115 obj_raw_syment_count (abfd
) = 0;
6117 /* Find a TOC symbol, if we need one. */
6118 if (!xcoff_find_tc0 (abfd
, &finfo
))
6121 /* We now know the position of everything in the file, except that
6122 we don't know the size of the symbol table and therefore we don't
6123 know where the string table starts. We just build the string
6124 table in memory as we go along. We process all the relocations
6125 for a single input file at once. */
6126 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6128 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6130 if (p
->type
== bfd_indirect_link_order
6131 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6133 sub
= p
->u
.indirect
.section
->owner
;
6134 if (! sub
->output_has_begun
)
6136 if (! xcoff_link_input_bfd (&finfo
, sub
))
6138 sub
->output_has_begun
= TRUE
;
6141 else if (p
->type
== bfd_section_reloc_link_order
6142 || p
->type
== bfd_symbol_reloc_link_order
)
6144 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
6149 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6155 /* Free up the buffers used by xcoff_link_input_bfd. */
6156 if (finfo
.internal_syms
!= NULL
)
6158 free (finfo
.internal_syms
);
6159 finfo
.internal_syms
= NULL
;
6161 if (finfo
.sym_indices
!= NULL
)
6163 free (finfo
.sym_indices
);
6164 finfo
.sym_indices
= NULL
;
6166 if (finfo
.linenos
!= NULL
)
6168 free (finfo
.linenos
);
6169 finfo
.linenos
= NULL
;
6171 if (finfo
.contents
!= NULL
)
6173 free (finfo
.contents
);
6174 finfo
.contents
= NULL
;
6176 if (finfo
.external_relocs
!= NULL
)
6178 free (finfo
.external_relocs
);
6179 finfo
.external_relocs
= NULL
;
6182 /* The value of the last C_FILE symbol is supposed to be -1. Write
6184 if (finfo
.last_file_index
!= -1)
6186 finfo
.last_file
.n_value
= -(bfd_vma
) 1;
6187 bfd_coff_swap_sym_out (abfd
, (void *) &finfo
.last_file
,
6188 (void *) finfo
.outsyms
);
6189 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
6190 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6191 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
6195 /* Write out all the global symbols which do not come from XCOFF
6197 xcoff_link_hash_traverse (xcoff_hash_table (info
),
6198 xcoff_write_global_symbol
,
6201 if (finfo
.outsyms
!= NULL
)
6203 free (finfo
.outsyms
);
6204 finfo
.outsyms
= NULL
;
6207 /* Now that we have written out all the global symbols, we know the
6208 symbol indices to use for relocs against them, and we can finally
6209 write out the relocs. */
6210 amt
= max_output_reloc_count
* relsz
;
6211 external_relocs
= bfd_malloc (amt
);
6212 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6215 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6217 struct internal_reloc
*irel
;
6218 struct internal_reloc
*irelend
;
6219 struct xcoff_link_hash_entry
**rel_hash
;
6220 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6222 bfd_size_type rel_size
;
6224 /* A stripped file has no relocs. */
6225 if (info
->strip
== strip_all
)
6231 if (o
->reloc_count
== 0)
6234 irel
= finfo
.section_info
[o
->target_index
].relocs
;
6235 irelend
= irel
+ o
->reloc_count
;
6236 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
6237 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6239 if (*rel_hash
!= NULL
)
6241 if ((*rel_hash
)->indx
< 0)
6243 if (! ((*info
->callbacks
->unattached_reloc
)
6244 (info
, (*rel_hash
)->root
.root
.string
,
6245 NULL
, o
, irel
->r_vaddr
)))
6247 (*rel_hash
)->indx
= 0;
6249 irel
->r_symndx
= (*rel_hash
)->indx
;
6253 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6254 toc_rel_hash
!= NULL
;
6255 toc_rel_hash
= toc_rel_hash
->next
)
6257 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6259 if (! ((*info
->callbacks
->unattached_reloc
)
6260 (info
, toc_rel_hash
->h
->root
.root
.string
,
6261 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
6263 toc_rel_hash
->h
->u
.toc_indx
= 0;
6265 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6268 /* XCOFF requires that the relocs be sorted by address. We tend
6269 to produce them in the order in which their containing csects
6270 appear in the symbol table, which is not necessarily by
6271 address. So we sort them here. There may be a better way to
6273 qsort ((void *) finfo
.section_info
[o
->target_index
].relocs
,
6274 o
->reloc_count
, sizeof (struct internal_reloc
),
6277 irel
= finfo
.section_info
[o
->target_index
].relocs
;
6278 irelend
= irel
+ o
->reloc_count
;
6279 erel
= external_relocs
;
6280 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6281 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6283 rel_size
= relsz
* o
->reloc_count
;
6284 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6285 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6289 if (external_relocs
!= NULL
)
6291 free (external_relocs
);
6292 external_relocs
= NULL
;
6295 /* Free up the section information. */
6296 if (finfo
.section_info
!= NULL
)
6300 for (i
= 0; i
< abfd
->section_count
; i
++)
6302 if (finfo
.section_info
[i
].relocs
!= NULL
)
6303 free (finfo
.section_info
[i
].relocs
);
6304 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
6305 free (finfo
.section_info
[i
].rel_hashes
);
6307 free (finfo
.section_info
);
6308 finfo
.section_info
= NULL
;
6311 /* Write out the loader section contents. */
6312 o
= xcoff_hash_table (info
)->loader_section
;
6315 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
6316 == (xcoff_hash_table (info
)->loader_section
->contents
6317 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6318 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6319 (file_ptr
) o
->output_offset
, o
->size
))
6323 /* Write out the magic sections. */
6324 o
= xcoff_hash_table (info
)->linkage_section
;
6326 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6327 (file_ptr
) o
->output_offset
,
6330 o
= xcoff_hash_table (info
)->toc_section
;
6332 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6333 (file_ptr
) o
->output_offset
,
6336 o
= xcoff_hash_table (info
)->descriptor_section
;
6338 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6339 (file_ptr
) o
->output_offset
,
6343 /* Write out the string table. */
6344 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6345 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6348 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
6350 amt
= STRING_SIZE_SIZE
;
6351 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6353 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
6356 _bfd_stringtab_free (finfo
.strtab
);
6358 /* Write out the debugging string table. */
6359 o
= xcoff_hash_table (info
)->debug_section
;
6362 struct bfd_strtab_hash
*debug_strtab
;
6364 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6365 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6366 >= _bfd_stringtab_size (debug_strtab
));
6367 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6368 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6370 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6374 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6375 not try to write out the symbols. */
6376 bfd_get_symcount (abfd
) = 0;
6381 if (finfo
.strtab
!= NULL
)
6382 _bfd_stringtab_free (finfo
.strtab
);
6384 if (finfo
.section_info
!= NULL
)
6388 for (i
= 0; i
< abfd
->section_count
; i
++)
6390 if (finfo
.section_info
[i
].relocs
!= NULL
)
6391 free (finfo
.section_info
[i
].relocs
);
6392 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
6393 free (finfo
.section_info
[i
].rel_hashes
);
6395 free (finfo
.section_info
);
6398 if (finfo
.internal_syms
!= NULL
)
6399 free (finfo
.internal_syms
);
6400 if (finfo
.sym_indices
!= NULL
)
6401 free (finfo
.sym_indices
);
6402 if (finfo
.outsyms
!= NULL
)
6403 free (finfo
.outsyms
);
6404 if (finfo
.linenos
!= NULL
)
6405 free (finfo
.linenos
);
6406 if (finfo
.contents
!= NULL
)
6407 free (finfo
.contents
);
6408 if (finfo
.external_relocs
!= NULL
)
6409 free (finfo
.external_relocs
);
6410 if (external_relocs
!= NULL
)
6411 free (external_relocs
);