1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011 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. */
90 /* True if the archive contains a dynamic object. */
91 unsigned int contains_shared_object_p
: 1;
93 /* True if the previous field is valid. */
94 unsigned int know_contains_shared_object_p
: 1;
97 struct xcoff_link_hash_table
99 struct bfd_link_hash_table root
;
101 /* The .debug string hash table. We need to compute this while
102 reading the input files, so that we know how large the .debug
103 section will be before we assign section positions. */
104 struct bfd_strtab_hash
*debug_strtab
;
106 /* The .debug section we will use for the final output. */
107 asection
*debug_section
;
109 /* The .loader section we will use for the final output. */
110 asection
*loader_section
;
112 /* A count of non TOC relative relocs which will need to be
113 allocated in the .loader section. */
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr
;
119 /* The .gl section we use to hold global linkage code. */
120 asection
*linkage_section
;
122 /* The .tc section we use to hold toc entries we build for global
124 asection
*toc_section
;
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection
*descriptor_section
;
130 /* The list of import files. */
131 struct xcoff_import_file
*imports
;
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align
;
136 /* Whether the .text section must be read-only. */
139 /* Whether -brtl was specified. */
142 /* Whether garbage collection was done. */
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
148 struct xcoff_link_size_list
*next
;
149 struct xcoff_link_hash_entry
*h
;
154 /* Information about archives. */
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection
*special_sections
[XCOFF_NUMBER_OF_SPECIAL_SECTIONS
];
161 /* Information that we pass around while doing the final link step. */
163 struct xcoff_final_link_info
165 /* General link information. */
166 struct bfd_link_info
*info
;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash
*strtab
;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info
*section_info
;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index
;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file
;
178 /* Symbol index of TOC symbol. */
180 /* Start of .loader symbols. */
182 /* Next .loader reloc to swap out. */
184 /* File position of start of line numbers. */
185 file_ptr line_filepos
;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment
*internal_syms
;
188 /* Buffer large enough to hold output indices of symbols of any
191 /* Buffer large enough to hold output symbols for any input file. */
193 /* Buffer large enough to hold external line numbers for any input
196 /* Buffer large enough to hold any input section. */
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte
*external_relocs
;
202 static bfd_boolean
xcoff_mark (struct bfd_link_info
*, asection
*);
206 /* Routines to read XCOFF dynamic information. This don't really
207 belong here, but we already have the ldsym manipulation routines
210 /* Read the contents of a section. */
213 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
215 if (coff_section_data (abfd
, sec
) == NULL
)
217 bfd_size_type amt
= sizeof (struct coff_section_tdata
);
219 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
220 if (sec
->used_by_bfd
== NULL
)
224 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
228 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
230 if (contents
!= NULL
)
234 coff_section_data (abfd
, sec
)->contents
= contents
;
240 /* Get the size required to hold the dynamic symbols. */
243 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
247 struct internal_ldhdr ldhdr
;
249 if ((abfd
->flags
& DYNAMIC
) == 0)
251 bfd_set_error (bfd_error_invalid_operation
);
255 lsec
= bfd_get_section_by_name (abfd
, ".loader");
258 bfd_set_error (bfd_error_no_symbols
);
262 if (! xcoff_get_section_contents (abfd
, lsec
))
264 contents
= coff_section_data (abfd
, lsec
)->contents
;
266 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
268 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
271 /* Get the dynamic symbols. */
274 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
278 struct internal_ldhdr ldhdr
;
280 bfd_byte
*elsym
, *elsymend
;
281 coff_symbol_type
*symbuf
;
283 if ((abfd
->flags
& DYNAMIC
) == 0)
285 bfd_set_error (bfd_error_invalid_operation
);
289 lsec
= bfd_get_section_by_name (abfd
, ".loader");
292 bfd_set_error (bfd_error_no_symbols
);
296 if (! xcoff_get_section_contents (abfd
, lsec
))
298 contents
= coff_section_data (abfd
, lsec
)->contents
;
300 coff_section_data (abfd
, lsec
)->keep_contents
= TRUE
;
302 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
304 strings
= (char *) contents
+ ldhdr
.l_stoff
;
306 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
310 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
312 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
313 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
315 struct internal_ldsym ldsym
;
317 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
319 symbuf
->symbol
.the_bfd
= abfd
;
321 if (ldsym
._l
._l_l
._l_zeroes
== 0)
322 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
327 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
330 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
332 symbuf
->symbol
.name
= c
;
335 if (ldsym
.l_smclas
== XMC_XO
)
336 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
338 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
340 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
342 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
343 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
345 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
346 symbuf
->symbol
.flags
|= BSF_WEAK
;
348 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
351 /* FIXME: We have no way to record the other information stored
352 with the loader symbol. */
353 *psyms
= (asymbol
*) symbuf
;
358 return ldhdr
.l_nsyms
;
361 /* Get the size required to hold the dynamic relocs. */
364 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
368 struct internal_ldhdr ldhdr
;
370 if ((abfd
->flags
& DYNAMIC
) == 0)
372 bfd_set_error (bfd_error_invalid_operation
);
376 lsec
= bfd_get_section_by_name (abfd
, ".loader");
379 bfd_set_error (bfd_error_no_symbols
);
383 if (! xcoff_get_section_contents (abfd
, lsec
))
385 contents
= coff_section_data (abfd
, lsec
)->contents
;
387 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
389 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
392 /* Get the dynamic relocs. */
395 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
401 struct internal_ldhdr ldhdr
;
403 bfd_byte
*elrel
, *elrelend
;
405 if ((abfd
->flags
& DYNAMIC
) == 0)
407 bfd_set_error (bfd_error_invalid_operation
);
411 lsec
= bfd_get_section_by_name (abfd
, ".loader");
414 bfd_set_error (bfd_error_no_symbols
);
418 if (! xcoff_get_section_contents (abfd
, lsec
))
420 contents
= coff_section_data (abfd
, lsec
)->contents
;
422 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
424 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
428 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
430 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
431 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
434 struct internal_ldrel ldrel
;
436 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
438 if (ldrel
.l_symndx
>= 3)
439 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
445 switch (ldrel
.l_symndx
)
461 sec
= bfd_get_section_by_name (abfd
, name
);
464 bfd_set_error (bfd_error_bad_value
);
468 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
471 relbuf
->address
= ldrel
.l_vaddr
;
474 /* Most dynamic relocs have the same type. FIXME: This is only
475 correct if ldrel.l_rtype == 0. In other cases, we should use
476 a different howto. */
477 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
479 /* FIXME: We have no way to record the l_rsecnm field. */
486 return ldhdr
.l_nreloc
;
489 /* Hash functions for xcoff_link_hash_table's archive_info. */
492 xcoff_archive_info_hash (const void *data
)
494 const struct xcoff_archive_info
*info
;
496 info
= (const struct xcoff_archive_info
*) data
;
497 return htab_hash_pointer (info
->archive
);
501 xcoff_archive_info_eq (const void *data1
, const void *data2
)
503 const struct xcoff_archive_info
*info1
;
504 const struct xcoff_archive_info
*info2
;
506 info1
= (const struct xcoff_archive_info
*) data1
;
507 info2
= (const struct xcoff_archive_info
*) data2
;
508 return info1
->archive
== info2
->archive
;
511 /* Return information about archive ARCHIVE. Return NULL on error. */
513 static struct xcoff_archive_info
*
514 xcoff_get_archive_info (struct bfd_link_info
*info
, bfd
*archive
)
516 struct xcoff_link_hash_table
*htab
;
517 struct xcoff_archive_info
*entryp
, entry
;
520 htab
= xcoff_hash_table (info
);
521 entry
.archive
= archive
;
522 slot
= htab_find_slot (htab
->archive_info
, &entry
, INSERT
);
529 entryp
= bfd_zalloc (archive
, sizeof (entry
));
533 entryp
->archive
= archive
;
539 /* Routine to create an entry in an XCOFF link hash table. */
541 static struct bfd_hash_entry
*
542 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
543 struct bfd_hash_table
*table
,
546 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
548 /* Allocate the structure if it has not already been allocated by a
551 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
555 /* Call the allocation method of the superclass. */
556 ret
= ((struct xcoff_link_hash_entry
*)
557 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
561 /* Set local fields. */
563 ret
->toc_section
= NULL
;
564 ret
->u
.toc_indx
= -1;
565 ret
->descriptor
= NULL
;
569 ret
->smclas
= XMC_UA
;
572 return (struct bfd_hash_entry
*) ret
;
575 /* Create a XCOFF link hash table. */
577 struct bfd_link_hash_table
*
578 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
580 struct xcoff_link_hash_table
*ret
;
581 bfd_size_type amt
= sizeof (* ret
);
583 ret
= bfd_zmalloc (amt
);
586 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
587 sizeof (struct xcoff_link_hash_entry
)))
593 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
594 ret
->archive_info
= htab_create (37, xcoff_archive_info_hash
,
595 xcoff_archive_info_eq
, NULL
);
597 /* The linker will always generate a full a.out header. We need to
598 record that fact now, before the sizeof_headers routine could be
600 xcoff_data (abfd
)->full_aouthdr
= TRUE
;
605 /* Free a XCOFF link hash table. */
608 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table
*hash
)
610 struct xcoff_link_hash_table
*ret
= (struct xcoff_link_hash_table
*) hash
;
612 _bfd_stringtab_free (ret
->debug_strtab
);
613 bfd_hash_table_free (&ret
->root
.table
);
617 /* Read internal relocs for an XCOFF csect. This is a wrapper around
618 _bfd_coff_read_internal_relocs which tries to take advantage of any
619 relocs which may have been cached for the enclosing section. */
621 static struct internal_reloc
*
622 xcoff_read_internal_relocs (bfd
*abfd
,
625 bfd_byte
*external_relocs
,
626 bfd_boolean require_internal
,
627 struct internal_reloc
*internal_relocs
)
629 if (coff_section_data (abfd
, sec
) != NULL
630 && coff_section_data (abfd
, sec
)->relocs
== NULL
631 && xcoff_section_data (abfd
, sec
) != NULL
)
635 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
637 if (enclosing
!= NULL
638 && (coff_section_data (abfd
, enclosing
) == NULL
639 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
641 && enclosing
->reloc_count
> 0)
643 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, TRUE
,
644 external_relocs
, FALSE
, NULL
)
649 if (enclosing
!= NULL
650 && coff_section_data (abfd
, enclosing
) != NULL
651 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
655 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
656 / bfd_coff_relsz (abfd
));
658 if (! require_internal
)
659 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
660 memcpy (internal_relocs
,
661 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
662 sec
->reloc_count
* sizeof (struct internal_reloc
));
663 return internal_relocs
;
667 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
668 require_internal
, internal_relocs
);
671 /* Split FILENAME into an import path and an import filename,
672 storing them in *IMPPATH and *IMPFILE respectively. */
675 bfd_xcoff_split_import_path (bfd
*abfd
, const char *filename
,
676 const char **imppath
, const char **impfile
)
682 base
= lbasename (filename
);
683 length
= base
- filename
;
685 /* The filename has no directory component, so use an empty path. */
687 else if (length
== 1)
688 /* The filename is in the root directory. */
692 /* Extract the (non-empty) directory part. Note that we don't
693 need to strip duplicate directory separators from any part
694 of the string; the native linker doesn't do that either. */
695 path
= bfd_alloc (abfd
, length
);
698 memcpy (path
, filename
, length
- 1);
699 path
[length
- 1] = 0;
706 /* Set ARCHIVE's import path as though its filename had been given
710 bfd_xcoff_set_archive_import_path (struct bfd_link_info
*info
,
711 bfd
*archive
, const char *filename
)
713 struct xcoff_archive_info
*archive_info
;
715 archive_info
= xcoff_get_archive_info (info
, archive
);
716 return (archive_info
!= NULL
717 && bfd_xcoff_split_import_path (archive
, filename
,
718 &archive_info
->imppath
,
719 &archive_info
->impfile
));
722 /* H is an imported symbol. Set the import module's path, file and member
723 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
724 no specific import module is specified. */
727 xcoff_set_import_path (struct bfd_link_info
*info
,
728 struct xcoff_link_hash_entry
*h
,
729 const char *imppath
, const char *impfile
,
730 const char *impmember
)
733 struct xcoff_import_file
**pp
;
735 /* We overload the ldindx field to hold the l_ifile value for this
737 BFD_ASSERT (h
->ldsym
== NULL
);
738 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
743 /* We start c at 1 because the first entry in the import list is
744 reserved for the library search path. */
745 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
747 pp
= &(*pp
)->next
, ++c
)
749 if (filename_cmp ((*pp
)->path
, imppath
) == 0
750 && filename_cmp ((*pp
)->file
, impfile
) == 0
751 && filename_cmp ((*pp
)->member
, impmember
) == 0)
757 struct xcoff_import_file
*n
;
758 bfd_size_type amt
= sizeof (* n
);
760 n
= bfd_alloc (info
->output_bfd
, amt
);
766 n
->member
= impmember
;
774 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
775 Return true if LDSYM defines H. */
778 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry
*h
,
779 struct internal_ldsym
*ldsym
)
781 /* If we didn't know about H before processing LDSYM, LDSYM
782 definitely defines H. */
783 if (h
->root
.type
== bfd_link_hash_new
)
786 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
787 dynamic symbol, LDSYM trumps the current definition of H. */
788 if ((ldsym
->l_smtype
& L_WEAK
) == 0
789 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
790 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
791 && (h
->root
.type
== bfd_link_hash_defweak
792 || h
->root
.type
== bfd_link_hash_undefweak
))
795 /* If H is currently undefined, LDSYM defines it. */
796 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
797 && (h
->root
.type
== bfd_link_hash_undefined
798 || h
->root
.type
== bfd_link_hash_undefweak
))
804 /* This function is used to add symbols from a dynamic object to the
805 global symbol table. */
808 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
812 struct internal_ldhdr ldhdr
;
814 bfd_byte
*elsym
, *elsymend
;
815 struct xcoff_import_file
*n
;
817 struct xcoff_import_file
**pp
;
819 /* We can only handle a dynamic object if we are generating an XCOFF
821 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
823 (*_bfd_error_handler
)
824 (_("%s: XCOFF shared object when not producing XCOFF output"),
825 bfd_get_filename (abfd
));
826 bfd_set_error (bfd_error_invalid_operation
);
830 /* The symbols we use from a dynamic object are not the symbols in
831 the normal symbol table, but, rather, the symbols in the export
832 table. If there is a global symbol in a dynamic object which is
833 not in the export table, the loader will not be able to find it,
834 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
835 libc.a has symbols in the export table which are not in the
838 /* Read in the .loader section. FIXME: We should really use the
839 o_snloader field in the a.out header, rather than grabbing the
841 lsec
= bfd_get_section_by_name (abfd
, ".loader");
844 (*_bfd_error_handler
)
845 (_("%s: dynamic object with no .loader section"),
846 bfd_get_filename (abfd
));
847 bfd_set_error (bfd_error_no_symbols
);
851 if (! xcoff_get_section_contents (abfd
, lsec
))
853 contents
= coff_section_data (abfd
, lsec
)->contents
;
855 /* Remove the sections from this object, so that they do not get
856 included in the link. */
857 bfd_section_list_clear (abfd
);
859 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
861 strings
= (char *) contents
+ ldhdr
.l_stoff
;
863 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
865 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
867 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
869 struct internal_ldsym ldsym
;
870 char nambuf
[SYMNMLEN
+ 1];
872 struct xcoff_link_hash_entry
*h
;
874 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
876 /* We are only interested in exported symbols. */
877 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
880 if (ldsym
._l
._l_l
._l_zeroes
== 0)
881 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
884 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
885 nambuf
[SYMNMLEN
] = '\0';
889 /* Normally we could not call xcoff_link_hash_lookup in an add
890 symbols routine, since we might not be using an XCOFF hash
891 table. However, we verified above that we are using an XCOFF
894 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
,
899 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
902 h
->flags
|= XCOFF_DEF_DYNAMIC
;
903 h
->smclas
= ldsym
.l_smclas
;
904 if (h
->smclas
== XMC_XO
)
906 /* This symbol has an absolute value. */
907 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
908 h
->root
.type
= bfd_link_hash_defweak
;
910 h
->root
.type
= bfd_link_hash_defined
;
911 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
912 h
->root
.u
.def
.value
= ldsym
.l_value
;
916 /* Otherwise, we don't bother to actually define the symbol,
917 since we don't have a section to put it in anyhow.
918 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
919 should be imported from the symbol's undef.abfd. */
920 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
921 h
->root
.type
= bfd_link_hash_undefweak
;
923 h
->root
.type
= bfd_link_hash_undefined
;
924 h
->root
.u
.undef
.abfd
= abfd
;
927 /* If this symbol defines a function descriptor, then it
928 implicitly defines the function code as well. */
929 if (h
->smclas
== XMC_DS
930 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
931 h
->flags
|= XCOFF_DESCRIPTOR
;
932 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
934 struct xcoff_link_hash_entry
*hds
;
941 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
945 strcpy (dsnm
+ 1, name
);
946 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
956 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
958 hds
->root
.type
= h
->root
.type
;
959 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
960 if (h
->smclas
== XMC_XO
)
962 /* An absolute symbol appears to actually define code, not a
963 function descriptor. This is how some math functions are
964 implemented on AIX 4.1. */
965 hds
->smclas
= XMC_XO
;
966 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
967 hds
->root
.u
.def
.value
= ldsym
.l_value
;
971 hds
->smclas
= XMC_PR
;
972 hds
->root
.u
.undef
.abfd
= abfd
;
973 /* We do not want to add this to the undefined
980 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
982 free (coff_section_data (abfd
, lsec
)->contents
);
983 coff_section_data (abfd
, lsec
)->contents
= NULL
;
986 /* Record this file in the import files. */
987 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
992 if (abfd
->my_archive
== NULL
)
994 if (!bfd_xcoff_split_import_path (abfd
, abfd
->filename
,
1001 struct xcoff_archive_info
*archive_info
;
1003 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1004 if (!archive_info
->impfile
)
1006 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1007 archive_info
->archive
->filename
,
1008 &archive_info
->imppath
,
1009 &archive_info
->impfile
))
1012 n
->path
= archive_info
->imppath
;
1013 n
->file
= archive_info
->impfile
;
1014 n
->member
= bfd_get_filename (abfd
);
1017 /* We start c at 1 because the first import file number is reserved
1019 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1021 pp
= &(*pp
)->next
, ++c
)
1025 xcoff_data (abfd
)->import_file_id
= c
;
1030 /* xcoff_link_create_extra_sections
1032 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1035 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1037 bfd_boolean return_value
= FALSE
;
1039 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1041 /* We need to build a .loader section, so we do it here. This
1042 won't work if we're producing an XCOFF output file with no
1043 XCOFF input files. FIXME. */
1045 if (!info
->relocatable
1046 && xcoff_hash_table (info
)->loader_section
== NULL
)
1049 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1051 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1055 xcoff_hash_table (info
)->loader_section
= lsec
;
1058 /* Likewise for the linkage section. */
1059 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1062 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1065 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1069 xcoff_hash_table (info
)->linkage_section
= lsec
;
1070 lsec
->alignment_power
= 2;
1073 /* Likewise for the TOC section. */
1074 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1077 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1080 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1084 xcoff_hash_table (info
)->toc_section
= tsec
;
1085 tsec
->alignment_power
= 2;
1088 /* Likewise for the descriptor section. */
1089 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1092 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1095 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1099 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1100 dsec
->alignment_power
= 2;
1103 /* Likewise for the .debug section. */
1104 if (xcoff_hash_table (info
)->debug_section
== NULL
1105 && info
->strip
!= strip_all
)
1108 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1110 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1114 xcoff_hash_table (info
)->debug_section
= dsec
;
1118 return_value
= TRUE
;
1122 return return_value
;
1125 /* Returns the index of reloc in RELOCS with the least address greater
1126 than or equal to ADDRESS. The relocs are sorted by address. */
1128 static bfd_size_type
1129 xcoff_find_reloc (struct internal_reloc
*relocs
,
1130 bfd_size_type count
,
1133 bfd_size_type min
, max
, this;
1137 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1146 /* Do a binary search over (min,max]. */
1147 while (min
+ 1 < max
)
1151 this = (max
+ min
) / 2;
1152 raddr
= relocs
[this].r_vaddr
;
1153 if (raddr
> address
)
1155 else if (raddr
< address
)
1164 if (relocs
[min
].r_vaddr
< address
)
1168 && relocs
[min
- 1].r_vaddr
== address
)
1174 /* Add all the symbols from an object file to the hash table.
1176 XCOFF is a weird format. A normal XCOFF .o files will have three
1177 COFF sections--.text, .data, and .bss--but each COFF section will
1178 contain many csects. These csects are described in the symbol
1179 table. From the linker's point of view, each csect must be
1180 considered a section in its own right. For example, a TOC entry is
1181 handled as a small XMC_TC csect. The linker must be able to merge
1182 different TOC entries together, which means that it must be able to
1183 extract the XMC_TC csects from the .data section of the input .o
1186 From the point of view of our linker, this is, of course, a hideous
1187 nightmare. We cope by actually creating sections for each csect,
1188 and discarding the original sections. We then have to handle the
1189 relocation entries carefully, since the only way to tell which
1190 csect they belong to is to examine the address. */
1193 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1195 unsigned int n_tmask
;
1196 unsigned int n_btshft
;
1197 bfd_boolean default_copy
;
1198 bfd_size_type symcount
;
1199 struct xcoff_link_hash_entry
**sym_hash
;
1200 asection
**csect_cache
;
1201 unsigned int *lineno_counts
;
1202 bfd_size_type linesz
;
1204 asection
*last_real
;
1205 bfd_boolean keep_syms
;
1207 unsigned int csect_index
;
1208 asection
*first_csect
;
1209 bfd_size_type symesz
;
1212 struct reloc_info_struct
1214 struct internal_reloc
*relocs
;
1217 } *reloc_info
= NULL
;
1220 keep_syms
= obj_coff_keep_syms (abfd
);
1222 if ((abfd
->flags
& DYNAMIC
) != 0
1223 && ! info
->static_link
)
1225 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1229 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1230 if (! xcoff_link_create_extra_sections (abfd
, info
))
1233 if ((abfd
->flags
& DYNAMIC
) != 0
1234 && ! info
->static_link
)
1237 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1238 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1240 /* Define macros so that ISFCN, et. al., macros work correctly. */
1241 #define N_TMASK n_tmask
1242 #define N_BTSHFT n_btshft
1244 if (info
->keep_memory
)
1245 default_copy
= FALSE
;
1247 default_copy
= TRUE
;
1249 symcount
= obj_raw_syment_count (abfd
);
1251 /* We keep a list of the linker hash table entries that correspond
1252 to each external symbol. */
1253 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1254 sym_hash
= bfd_zalloc (abfd
, amt
);
1255 if (sym_hash
== NULL
&& symcount
!= 0)
1257 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1259 /* Because of the weird stuff we are doing with XCOFF csects, we can
1260 not easily determine which section a symbol is in, so we store
1261 the information in the tdata for the input file. */
1262 amt
= symcount
* sizeof (asection
*);
1263 csect_cache
= bfd_zalloc (abfd
, amt
);
1264 if (csect_cache
== NULL
&& symcount
!= 0)
1266 xcoff_data (abfd
)->csects
= csect_cache
;
1268 /* We garbage-collect line-number information on a symbol-by-symbol
1269 basis, so we need to have quick access to the number of entries
1271 amt
= symcount
* sizeof (unsigned int);
1272 lineno_counts
= bfd_zalloc (abfd
, amt
);
1273 if (lineno_counts
== NULL
&& symcount
!= 0)
1275 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1277 /* While splitting sections into csects, we need to assign the
1278 relocs correctly. The relocs and the csects must both be in
1279 order by VMA within a given section, so we handle this by
1280 scanning along the relocs as we process the csects. We index
1281 into reloc_info using the section target_index. */
1282 amt
= abfd
->section_count
+ 1;
1283 amt
*= sizeof (struct reloc_info_struct
);
1284 reloc_info
= bfd_zmalloc (amt
);
1285 if (reloc_info
== NULL
)
1288 /* Read in the relocs and line numbers for each section. */
1289 linesz
= bfd_coff_linesz (abfd
);
1291 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1295 if ((o
->flags
& SEC_RELOC
) != 0)
1297 reloc_info
[o
->target_index
].relocs
=
1298 xcoff_read_internal_relocs (abfd
, o
, TRUE
, NULL
, FALSE
, NULL
);
1299 amt
= o
->reloc_count
;
1300 amt
*= sizeof (asection
*);
1301 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1302 if (reloc_info
[o
->target_index
].csects
== NULL
)
1306 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1307 && o
->lineno_count
> 0)
1311 amt
= linesz
* o
->lineno_count
;
1312 linenos
= bfd_malloc (amt
);
1313 if (linenos
== NULL
)
1315 reloc_info
[o
->target_index
].linenos
= linenos
;
1316 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1317 || bfd_bread (linenos
, amt
, abfd
) != amt
)
1322 /* Don't let the linker relocation routines discard the symbols. */
1323 obj_coff_keep_syms (abfd
) = TRUE
;
1329 symesz
= bfd_coff_symesz (abfd
);
1330 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1331 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1332 esym_end
= esym
+ symcount
* symesz
;
1334 while (esym
< esym_end
)
1336 struct internal_syment sym
;
1337 union internal_auxent aux
;
1339 char buf
[SYMNMLEN
+ 1];
1343 struct xcoff_link_hash_entry
*set_toc
;
1345 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1347 /* In this pass we are only interested in symbols with csect
1349 if (!CSECT_SYM_P (sym
.n_sclass
))
1352 Normally csect is a .pr, .rw etc. created in the loop
1353 If C_FILE or first time, handle special
1355 Advance esym, sym_hash, csect_hash ptrs. */
1356 if (sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1359 *csect_cache
= csect
;
1360 else if (first_csect
== NULL
1361 || sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1362 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1364 *csect_cache
= NULL
;
1365 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1366 sym_hash
+= sym
.n_numaux
+ 1;
1367 csect_cache
+= sym
.n_numaux
+ 1;
1368 lineno_counts
+= sym
.n_numaux
+ 1;
1373 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1378 /* If this symbol has line number information attached to it,
1379 and we're not stripping it, count the number of entries and
1380 add them to the count for this csect. In the final link pass
1381 we are going to attach line number information by symbol,
1382 rather than by section, in order to more easily handle
1383 garbage collection. */
1384 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1387 && ISFCN (sym
.n_type
))
1389 union internal_auxent auxlin
;
1391 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1392 sym
.n_type
, sym
.n_sclass
,
1393 0, sym
.n_numaux
, (void *) &auxlin
);
1395 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1397 asection
*enclosing
;
1398 bfd_signed_vma linoff
;
1400 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1401 if (enclosing
== NULL
)
1403 (*_bfd_error_handler
)
1404 (_("%B: `%s' has line numbers but no enclosing section"),
1406 bfd_set_error (bfd_error_bad_value
);
1409 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1410 - enclosing
->line_filepos
);
1411 /* Explicit cast to bfd_signed_vma for compiler. */
1412 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1414 struct internal_lineno lin
;
1415 bfd_byte
*linpstart
;
1417 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1419 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1421 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1423 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1426 bfd_byte
*linpend
, *linp
;
1428 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1429 + enclosing
->lineno_count
* linesz
);
1430 for (linp
= linpstart
+ linesz
;
1434 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1436 if (lin
.l_lnno
== 0)
1439 *lineno_counts
= (linp
- linpstart
) / linesz
;
1440 /* The setting of line_filepos will only be
1441 useful if all the line number entries for a
1442 csect are contiguous; this only matters for
1444 if (csect
->line_filepos
== 0)
1445 csect
->line_filepos
=
1446 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1452 /* Pick up the csect auxiliary information. */
1453 if (sym
.n_numaux
== 0)
1455 (*_bfd_error_handler
)
1456 (_("%B: class %d symbol `%s' has no aux entries"),
1457 abfd
, sym
.n_sclass
, name
);
1458 bfd_set_error (bfd_error_bad_value
);
1462 bfd_coff_swap_aux_in (abfd
,
1463 (void *) (esym
+ symesz
* sym
.n_numaux
),
1464 sym
.n_type
, sym
.n_sclass
,
1465 sym
.n_numaux
- 1, sym
.n_numaux
,
1468 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1477 (*_bfd_error_handler
)
1478 (_("%B: symbol `%s' has unrecognized csect type %d"),
1480 bfd_set_error (bfd_error_bad_value
);
1484 /* This is an external reference. */
1485 if (sym
.n_sclass
== C_HIDEXT
1486 || sym
.n_scnum
!= N_UNDEF
1487 || aux
.x_csect
.x_scnlen
.l
!= 0)
1489 (*_bfd_error_handler
)
1490 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1491 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1492 aux
.x_csect
.x_scnlen
.l
);
1493 bfd_set_error (bfd_error_bad_value
);
1497 /* An XMC_XO external reference is actually a reference to
1498 an absolute location. */
1499 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1500 section
= bfd_und_section_ptr
;
1503 section
= bfd_abs_section_ptr
;
1504 value
= sym
.n_value
;
1510 csect_index
= -(unsigned) 1;
1512 /* When we see a TOC anchor, we record the TOC value. */
1513 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1515 if (sym
.n_sclass
!= C_HIDEXT
1516 || aux
.x_csect
.x_scnlen
.l
!= 0)
1518 (*_bfd_error_handler
)
1519 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1520 abfd
, name
, sym
.n_sclass
, aux
.x_csect
.x_scnlen
.l
);
1521 bfd_set_error (bfd_error_bad_value
);
1524 xcoff_data (abfd
)->toc
= sym
.n_value
;
1527 /* We must merge TOC entries for the same symbol. We can
1528 merge two TOC entries if they are both C_HIDEXT, they
1529 both have the same name, they are both 4 or 8 bytes long, and
1530 they both have a relocation table entry for an external
1531 symbol with the same name. Unfortunately, this means
1532 that we must look through the relocations. Ick.
1534 Logic for 32 bit vs 64 bit.
1535 32 bit has a csect length of 4 for TOC
1536 64 bit has a csect length of 8 for TOC
1538 The conditions to get past the if-check are not that bad.
1539 They are what is used to create the TOC csects in the first
1541 if (aux
.x_csect
.x_smclas
== XMC_TC
1542 && sym
.n_sclass
== C_HIDEXT
1543 && info
->output_bfd
->xvec
== abfd
->xvec
1544 && ((bfd_xcoff_is_xcoff32 (abfd
)
1545 && aux
.x_csect
.x_scnlen
.l
== 4)
1546 || (bfd_xcoff_is_xcoff64 (abfd
)
1547 && aux
.x_csect
.x_scnlen
.l
== 8)))
1549 asection
*enclosing
;
1550 struct internal_reloc
*relocs
;
1551 bfd_size_type relindx
;
1552 struct internal_reloc
*rel
;
1554 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1555 if (enclosing
== NULL
)
1558 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1559 amt
= enclosing
->reloc_count
;
1560 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1561 rel
= relocs
+ relindx
;
1563 /* 32 bit R_POS r_size is 31
1564 64 bit R_POS r_size is 63 */
1565 if (relindx
< enclosing
->reloc_count
1566 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1567 && rel
->r_type
== R_POS
1568 && ((bfd_xcoff_is_xcoff32 (abfd
)
1569 && rel
->r_size
== 31)
1570 || (bfd_xcoff_is_xcoff64 (abfd
)
1571 && rel
->r_size
== 63)))
1575 struct internal_syment relsym
;
1577 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1578 + rel
->r_symndx
* symesz
);
1579 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1580 if (EXTERN_SYM_P (relsym
.n_sclass
))
1582 const char *relname
;
1583 char relbuf
[SYMNMLEN
+ 1];
1585 struct xcoff_link_hash_entry
*h
;
1587 /* At this point we know that the TOC entry is
1588 for an externally visible symbol. */
1589 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1591 if (relname
== NULL
)
1594 /* We only merge TOC entries if the TC name is
1595 the same as the symbol name. This handles
1596 the normal case, but not common cases like
1597 SYM.P4 which gcc generates to store SYM + 4
1598 in the TOC. FIXME. */
1599 if (strcmp (name
, relname
) == 0)
1601 copy
= (! info
->keep_memory
1602 || relsym
._n
._n_n
._n_zeroes
!= 0
1603 || relsym
._n
._n_n
._n_offset
== 0);
1604 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1605 relname
, TRUE
, copy
,
1610 /* At this point h->root.type could be
1611 bfd_link_hash_new. That should be OK,
1612 since we know for sure that we will come
1613 across this symbol as we step through the
1616 /* We store h in *sym_hash for the
1617 convenience of the relocate_section
1621 if (h
->toc_section
!= NULL
)
1623 asection
**rel_csects
;
1625 /* We already have a TOC entry for this
1626 symbol, so we can just ignore this
1629 reloc_info
[enclosing
->target_index
].csects
;
1630 rel_csects
[relindx
] = bfd_und_section_ptr
;
1634 /* We are about to create a TOC entry for
1643 asection
*enclosing
;
1645 /* We need to create a new section. We get the name from
1646 the csect storage mapping class, so that the linker can
1647 accumulate similar csects together. */
1649 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1653 /* The enclosing section is the main section : .data, .text
1654 or .bss that the csect is coming from. */
1655 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1656 if (enclosing
== NULL
)
1659 if (! bfd_is_abs_section (enclosing
)
1660 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1661 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1662 > enclosing
->vma
+ enclosing
->size
)))
1664 (*_bfd_error_handler
)
1665 (_("%B: csect `%s' not in enclosing section"),
1667 bfd_set_error (bfd_error_bad_value
);
1670 csect
->vma
= sym
.n_value
;
1671 csect
->filepos
= (enclosing
->filepos
1674 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1675 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1676 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1678 /* Record the enclosing section in the tdata for this new
1680 amt
= sizeof (struct coff_section_tdata
);
1681 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1682 if (csect
->used_by_bfd
== NULL
)
1684 amt
= sizeof (struct xcoff_section_tdata
);
1685 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1686 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1688 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1689 xcoff_section_data (abfd
, csect
)->lineno_count
=
1690 enclosing
->lineno_count
;
1692 if (enclosing
->owner
== abfd
)
1694 struct internal_reloc
*relocs
;
1695 bfd_size_type relindx
;
1696 struct internal_reloc
*rel
;
1697 asection
**rel_csect
;
1699 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1700 amt
= enclosing
->reloc_count
;
1701 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1703 rel
= relocs
+ relindx
;
1704 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1707 csect
->rel_filepos
= (enclosing
->rel_filepos
1708 + relindx
* bfd_coff_relsz (abfd
));
1709 while (relindx
< enclosing
->reloc_count
1710 && *rel_csect
== NULL
1711 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1715 csect
->flags
|= SEC_RELOC
;
1716 ++csect
->reloc_count
;
1723 /* There are a number of other fields and section flags
1724 which we do not bother to set. */
1726 csect_index
= ((esym
1727 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1730 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1732 if (first_csect
== NULL
)
1733 first_csect
= csect
;
1735 /* If this symbol is external, we treat it as starting at the
1736 beginning of the newly created section. */
1737 if (EXTERN_SYM_P (sym
.n_sclass
))
1743 /* If this is a TOC section for a symbol, record it. */
1744 if (set_toc
!= NULL
)
1745 set_toc
->toc_section
= csect
;
1750 /* This is a label definition. The x_scnlen field is the
1751 symbol index of the csect. Usually the XTY_LD symbol will
1752 follow its appropriate XTY_SD symbol. The .set pseudo op can
1753 cause the XTY_LD to not follow the XTY_SD symbol. */
1758 if (aux
.x_csect
.x_scnlen
.l
< 0
1759 || (aux
.x_csect
.x_scnlen
.l
1760 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1764 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1766 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1771 (*_bfd_error_handler
)
1772 (_("%B: misplaced XTY_LD `%s'"),
1774 bfd_set_error (bfd_error_bad_value
);
1778 value
= sym
.n_value
- csect
->vma
;
1783 /* This is an unitialized csect. We could base the name on
1784 the storage mapping class, but we don't bother except for
1785 an XMC_TD symbol. If this csect is externally visible,
1786 it is a common symbol. We put XMC_TD symbols in sections
1787 named .tocbss, and rely on the linker script to put that
1790 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1792 /* The linker script puts the .td section in the data
1793 section after the .tc section. */
1794 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1798 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1803 csect
->vma
= sym
.n_value
;
1804 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1805 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1806 /* There are a number of other fields and section flags
1807 which we do not bother to set. */
1809 csect_index
= ((esym
1810 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1813 amt
= sizeof (struct coff_section_tdata
);
1814 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1815 if (csect
->used_by_bfd
== NULL
)
1817 amt
= sizeof (struct xcoff_section_tdata
);
1818 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1819 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1821 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1823 if (first_csect
== NULL
)
1824 first_csect
= csect
;
1826 if (EXTERN_SYM_P (sym
.n_sclass
))
1828 csect
->flags
|= SEC_IS_COMMON
;
1831 value
= aux
.x_csect
.x_scnlen
.l
;
1837 /* Check for magic symbol names. */
1838 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1839 && aux
.x_csect
.x_smclas
!= XMC_TC
1840 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1846 if (strcmp (name
, "_text") == 0)
1847 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1848 else if (strcmp (name
, "_etext") == 0)
1849 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1850 else if (strcmp (name
, "_data") == 0)
1851 i
= XCOFF_SPECIAL_SECTION_DATA
;
1852 else if (strcmp (name
, "_edata") == 0)
1853 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1854 else if (strcmp (name
, "_end") == 0)
1855 i
= XCOFF_SPECIAL_SECTION_END
;
1857 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1858 i
= XCOFF_SPECIAL_SECTION_END2
;
1861 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1864 /* Now we have enough information to add the symbol to the
1865 linker hash table. */
1867 if (EXTERN_SYM_P (sym
.n_sclass
))
1872 BFD_ASSERT (section
!= NULL
);
1874 /* We must copy the name into memory if we got it from the
1875 syment itself, rather than the string table. */
1876 copy
= default_copy
;
1877 if (sym
._n
._n_n
._n_zeroes
!= 0
1878 || sym
._n
._n_n
._n_offset
== 0)
1881 /* Ignore global linkage code when linking statically. */
1882 if (info
->static_link
1883 && (smtyp
== XTY_SD
|| smtyp
== XTY_LD
)
1884 && aux
.x_csect
.x_smclas
== XMC_GL
)
1886 section
= bfd_und_section_ptr
;
1890 /* The AIX linker appears to only detect multiple symbol
1891 definitions when there is a reference to the symbol. If
1892 a symbol is defined multiple times, and the only
1893 references are from the same object file, the AIX linker
1894 appears to permit it. It does not merge the different
1895 definitions, but handles them independently. On the
1896 other hand, if there is a reference, the linker reports
1899 This matters because the AIX <net/net_globals.h> header
1900 file actually defines an initialized array, so we have to
1901 actually permit that to work.
1903 Just to make matters even more confusing, the AIX linker
1904 appears to permit multiple symbol definitions whenever
1905 the second definition is in an archive rather than an
1906 object file. This may be a consequence of the manner in
1907 which it handles archives: I think it may load the entire
1908 archive in as separate csects, and then let garbage
1909 collection discard symbols.
1911 We also have to handle the case of statically linking a
1912 shared object, which will cause symbol redefinitions,
1913 although this is an easier case to detect. */
1914 else if (info
->output_bfd
->xvec
== abfd
->xvec
)
1916 if (! bfd_is_und_section (section
))
1917 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1918 name
, TRUE
, copy
, FALSE
);
1920 /* Make a copy of the symbol name to prevent problems with
1922 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1923 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1924 TRUE
, TRUE
, FALSE
));
1926 if (*sym_hash
== NULL
)
1928 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1929 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1930 && ! bfd_is_und_section (section
)
1931 && ! bfd_is_com_section (section
))
1933 /* This is a second definition of a defined symbol. */
1934 if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
1935 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
1937 /* The existing symbol is from a shared library.
1939 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1940 (*sym_hash
)->root
.u
.undef
.abfd
=
1941 (*sym_hash
)->root
.u
.def
.section
->owner
;
1943 else if (abfd
->my_archive
!= NULL
)
1945 /* This is a redefinition in an object contained
1946 in an archive. Just ignore it. See the
1948 section
= bfd_und_section_ptr
;
1951 else if (sym
.n_sclass
== C_AIX_WEAKEXT
1952 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1954 /* At least one of the definitions is weak.
1955 Allow the normal rules to take effect. */
1957 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1958 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1960 /* This symbol has been referenced. In this
1961 case, we just continue and permit the
1962 multiple definition error. See the comment
1963 above about the behaviour of the AIX linker. */
1965 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1967 /* The symbols are both csects of the same
1968 class. There is at least a chance that this
1969 is a semi-legitimate redefinition. */
1970 section
= bfd_und_section_ptr
;
1972 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
1975 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
1976 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
1977 && (bfd_is_und_section (section
)
1978 || bfd_is_com_section (section
)))
1980 /* This is a reference to a multiply defined symbol.
1981 Report the error now. See the comment above
1982 about the behaviour of the AIX linker. We could
1983 also do this with warning symbols, but I'm not
1984 sure the XCOFF linker is wholly prepared to
1985 handle them, and that would only be a warning,
1987 if (! ((*info
->callbacks
->multiple_definition
)
1988 (info
, &(*sym_hash
)->root
, NULL
, NULL
, (bfd_vma
) 0)))
1990 /* Try not to give this error too many times. */
1991 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
1995 /* _bfd_generic_link_add_one_symbol may call the linker to
1996 generate an error message, and the linker may try to read
1997 the symbol table to give a good error. Right now, the
1998 line numbers are in an inconsistent state, since they are
1999 counted both in the real sections and in the new csects.
2000 We need to leave the count in the real sections so that
2001 the linker can report the line number of the error
2002 correctly, so temporarily clobber the link to the csects
2003 so that the linker will not try to read the line numbers
2004 a second time from the csects. */
2005 BFD_ASSERT (last_real
->next
== first_csect
);
2006 last_real
->next
= NULL
;
2007 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2008 if (! (_bfd_generic_link_add_one_symbol
2009 (info
, abfd
, name
, flags
, section
, value
,
2011 (struct bfd_link_hash_entry
**) sym_hash
)))
2013 last_real
->next
= first_csect
;
2015 if (smtyp
== XTY_CM
)
2017 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2018 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2019 /* We don't need the common csect we just created. */
2022 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2023 = csect
->alignment_power
;
2026 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2032 || section
== bfd_und_section_ptr
)
2033 flag
= XCOFF_REF_REGULAR
;
2035 flag
= XCOFF_DEF_REGULAR
;
2036 (*sym_hash
)->flags
|= flag
;
2038 if ((*sym_hash
)->smclas
== XMC_UA
2039 || flag
== XCOFF_DEF_REGULAR
)
2040 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2044 if (smtyp
== XTY_ER
)
2045 *csect_cache
= section
;
2048 *csect_cache
= csect
;
2050 xcoff_section_data (abfd
, csect
)->last_symndx
2051 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2054 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2055 sym_hash
+= sym
.n_numaux
+ 1;
2056 csect_cache
+= sym
.n_numaux
+ 1;
2057 lineno_counts
+= sym
.n_numaux
+ 1;
2060 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2062 /* Make sure that we have seen all the relocs. */
2063 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2065 /* Debugging sections have no csects. */
2066 if (bfd_get_section_flags (abfd
, o
) & SEC_DEBUGGING
)
2069 /* Reset the section size and the line number count, since the
2070 data is now attached to the csects. Don't reset the size of
2071 the .debug section, since we need to read it below in
2072 bfd_xcoff_size_dynamic_sections. */
2073 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
2075 o
->lineno_count
= 0;
2077 if ((o
->flags
& SEC_RELOC
) != 0)
2080 struct internal_reloc
*rel
;
2081 asection
**rel_csect
;
2083 rel
= reloc_info
[o
->target_index
].relocs
;
2084 rel_csect
= reloc_info
[o
->target_index
].csects
;
2086 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2088 if (*rel_csect
== NULL
)
2090 (*_bfd_error_handler
)
2091 (_("%B: reloc %s:%d not in csect"),
2093 bfd_set_error (bfd_error_bad_value
);
2097 /* We identify all function symbols that are the target
2098 of a relocation, so that we can create glue code for
2099 functions imported from dynamic objects. */
2100 if (info
->output_bfd
->xvec
== abfd
->xvec
2101 && *rel_csect
!= bfd_und_section_ptr
2102 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2104 struct xcoff_link_hash_entry
*h
;
2106 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2107 /* If the symbol name starts with a period, it is
2108 the code of a function. If the symbol is
2109 currently undefined, then add an undefined symbol
2110 for the function descriptor. This should do no
2111 harm, because any regular object that defines the
2112 function should also define the function
2113 descriptor. It helps, because it means that we
2114 will identify the function descriptor with a
2115 dynamic object if a dynamic object defines it. */
2116 if (h
->root
.root
.string
[0] == '.'
2117 && h
->descriptor
== NULL
)
2119 struct xcoff_link_hash_entry
*hds
;
2120 struct bfd_link_hash_entry
*bh
;
2122 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2123 h
->root
.root
.string
+ 1,
2127 if (hds
->root
.type
== bfd_link_hash_new
)
2130 if (! (_bfd_generic_link_add_one_symbol
2131 (info
, abfd
, hds
->root
.root
.string
,
2132 (flagword
) 0, bfd_und_section_ptr
,
2133 (bfd_vma
) 0, NULL
, FALSE
,
2136 hds
= (struct xcoff_link_hash_entry
*) bh
;
2138 hds
->flags
|= XCOFF_DESCRIPTOR
;
2139 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2140 hds
->descriptor
= h
;
2141 h
->descriptor
= hds
;
2143 if (h
->root
.root
.string
[0] == '.')
2144 h
->flags
|= XCOFF_CALLED
;
2148 free (reloc_info
[o
->target_index
].csects
);
2149 reloc_info
[o
->target_index
].csects
= NULL
;
2151 /* Reset SEC_RELOC and the reloc_count, since the reloc
2152 information is now attached to the csects. */
2153 o
->flags
&=~ SEC_RELOC
;
2156 /* If we are not keeping memory, free the reloc information. */
2157 if (! info
->keep_memory
2158 && coff_section_data (abfd
, o
) != NULL
2159 && coff_section_data (abfd
, o
)->relocs
!= NULL
2160 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2162 free (coff_section_data (abfd
, o
)->relocs
);
2163 coff_section_data (abfd
, o
)->relocs
= NULL
;
2167 /* Free up the line numbers. FIXME: We could cache these
2168 somewhere for the final link, to avoid reading them again. */
2169 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2171 free (reloc_info
[o
->target_index
].linenos
);
2172 reloc_info
[o
->target_index
].linenos
= NULL
;
2178 obj_coff_keep_syms (abfd
) = keep_syms
;
2183 if (reloc_info
!= NULL
)
2185 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2187 if (reloc_info
[o
->target_index
].csects
!= NULL
)
2188 free (reloc_info
[o
->target_index
].csects
);
2189 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2190 free (reloc_info
[o
->target_index
].linenos
);
2194 obj_coff_keep_syms (abfd
) = keep_syms
;
2201 /* Add symbols from an XCOFF object file. */
2204 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2206 if (! _bfd_coff_get_external_symbols (abfd
))
2208 if (! xcoff_link_add_symbols (abfd
, info
))
2210 if (! info
->keep_memory
)
2212 if (! _bfd_coff_free_symbols (abfd
))
2218 /* Look through the loader symbols to see if this dynamic object
2219 should be included in the link. The native linker uses the loader
2220 symbols, not the normal symbol table, so we do too. */
2223 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2224 struct bfd_link_info
*info
,
2225 bfd_boolean
*pneeded
,
2230 struct internal_ldhdr ldhdr
;
2231 const char *strings
;
2232 bfd_byte
*elsym
, *elsymend
;
2236 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2238 /* There are no symbols, so don't try to include it. */
2241 if (! xcoff_get_section_contents (abfd
, lsec
))
2243 contents
= coff_section_data (abfd
, lsec
)->contents
;
2245 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2247 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2249 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2251 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2252 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2254 struct internal_ldsym ldsym
;
2255 char nambuf
[SYMNMLEN
+ 1];
2257 struct bfd_link_hash_entry
*h
;
2259 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2261 /* We are only interested in exported symbols. */
2262 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2265 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2266 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2269 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2270 nambuf
[SYMNMLEN
] = '\0';
2274 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2276 /* We are only interested in symbols that are currently
2277 undefined. At this point we know that we are using an XCOFF
2280 && h
->type
== bfd_link_hash_undefined
2281 && (((struct xcoff_link_hash_entry
*) h
)->flags
2282 & XCOFF_DEF_DYNAMIC
) == 0)
2284 if (!(*info
->callbacks
2285 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2292 /* We do not need this shared object. */
2293 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2295 free (coff_section_data (abfd
, lsec
)->contents
);
2296 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2302 /* Look through the symbols to see if this object file should be
2303 included in the link. */
2306 xcoff_link_check_ar_symbols (bfd
*abfd
,
2307 struct bfd_link_info
*info
,
2308 bfd_boolean
*pneeded
,
2311 bfd_size_type symesz
;
2317 if ((abfd
->flags
& DYNAMIC
) != 0
2318 && ! info
->static_link
2319 && info
->output_bfd
->xvec
== abfd
->xvec
)
2320 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
, subsbfd
);
2322 symesz
= bfd_coff_symesz (abfd
);
2323 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2324 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2325 while (esym
< esym_end
)
2327 struct internal_syment sym
;
2329 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2331 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2334 char buf
[SYMNMLEN
+ 1];
2335 struct bfd_link_hash_entry
*h
;
2337 /* This symbol is externally visible, and is defined by this
2339 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2343 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2345 /* We are only interested in symbols that are currently
2346 undefined. If a symbol is currently known to be common,
2347 XCOFF linkers do not bring in an object file which
2348 defines it. We also don't bring in symbols to satisfy
2349 undefined references in shared objects. */
2351 && h
->type
== bfd_link_hash_undefined
2352 && (info
->output_bfd
->xvec
!= abfd
->xvec
2353 || (((struct xcoff_link_hash_entry
*) h
)->flags
2354 & XCOFF_DEF_DYNAMIC
) == 0))
2356 if (!(*info
->callbacks
2357 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2364 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2367 /* We do not need this object file. */
2371 /* Check a single archive element to see if we need to include it in
2372 the link. *PNEEDED is set according to whether this element is
2373 needed in the link or not. This is called via
2374 _bfd_generic_link_add_archive_symbols. */
2377 xcoff_link_check_archive_element (bfd
*abfd
,
2378 struct bfd_link_info
*info
,
2379 bfd_boolean
*pneeded
)
2381 bfd_boolean keep_syms_p
;
2384 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2385 if (!_bfd_coff_get_external_symbols (abfd
))
2389 if (!xcoff_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
2394 /* Potentially, the add_archive_element hook may have set a
2395 substitute BFD for us. */
2399 && !_bfd_coff_free_symbols (oldbfd
))
2401 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2402 if (!_bfd_coff_get_external_symbols (abfd
))
2405 if (!xcoff_link_add_symbols (abfd
, info
))
2407 if (info
->keep_memory
)
2413 if (!_bfd_coff_free_symbols (abfd
))
2420 /* Given an XCOFF BFD, add symbols to the global hash table as
2424 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2426 switch (bfd_get_format (abfd
))
2429 return xcoff_link_add_object_symbols (abfd
, info
);
2432 /* If the archive has a map, do the usual search. We then need
2433 to check the archive for dynamic objects, because they may not
2434 appear in the archive map even though they should, perhaps, be
2435 included. If the archive has no map, we just consider each object
2436 file in turn, since that apparently is what the AIX native linker
2438 if (bfd_has_map (abfd
))
2440 if (! (_bfd_generic_link_add_archive_symbols
2441 (abfd
, info
, xcoff_link_check_archive_element
)))
2448 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2449 while (member
!= NULL
)
2451 if (bfd_check_format (member
, bfd_object
)
2452 && (info
->output_bfd
->xvec
== member
->xvec
)
2453 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2457 if (! xcoff_link_check_archive_element (member
, info
,
2461 member
->archive_pass
= -1;
2463 member
= bfd_openr_next_archived_file (abfd
, member
);
2470 bfd_set_error (bfd_error_wrong_format
);
2476 _bfd_xcoff_define_common_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2477 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2478 struct bfd_link_hash_entry
*harg
)
2480 struct xcoff_link_hash_entry
*h
;
2482 if (!bfd_generic_define_common_symbol (output_bfd
, info
, harg
))
2485 h
= (struct xcoff_link_hash_entry
*) harg
;
2486 h
->flags
|= XCOFF_DEF_REGULAR
;
2490 /* If symbol H has not been interpreted as a function descriptor,
2491 see whether it should be. Set up its descriptor information if so. */
2494 xcoff_find_function (struct bfd_link_info
*info
,
2495 struct xcoff_link_hash_entry
*h
)
2497 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2498 && h
->root
.root
.string
[0] != '.')
2501 struct xcoff_link_hash_entry
*hfn
;
2504 amt
= strlen (h
->root
.root
.string
) + 2;
2505 fnname
= bfd_malloc (amt
);
2509 strcpy (fnname
+ 1, h
->root
.root
.string
);
2510 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2511 fnname
, FALSE
, FALSE
, TRUE
);
2514 && hfn
->smclas
== XMC_PR
2515 && (hfn
->root
.type
== bfd_link_hash_defined
2516 || hfn
->root
.type
== bfd_link_hash_defweak
))
2518 h
->flags
|= XCOFF_DESCRIPTOR
;
2519 h
->descriptor
= hfn
;
2520 hfn
->descriptor
= h
;
2526 /* Return true if the given bfd contains at least one shared object. */
2529 xcoff_archive_contains_shared_object_p (struct bfd_link_info
*info
,
2532 struct xcoff_archive_info
*archive_info
;
2535 archive_info
= xcoff_get_archive_info (info
, archive
);
2536 if (!archive_info
->know_contains_shared_object_p
)
2538 member
= bfd_openr_next_archived_file (archive
, NULL
);
2539 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2540 member
= bfd_openr_next_archived_file (archive
, member
);
2542 archive_info
->contains_shared_object_p
= (member
!= NULL
);
2543 archive_info
->know_contains_shared_object_p
= 1;
2545 return archive_info
->contains_shared_object_p
;
2548 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2549 qualifies for export by -bexpall. */
2552 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2554 /* Exclude symbols beginning with '_'. */
2555 if (h
->root
.root
.string
[0] == '_')
2558 /* Exclude archive members that would otherwise be unreferenced. */
2559 if ((h
->flags
& XCOFF_MARK
) == 0
2560 && (h
->root
.type
== bfd_link_hash_defined
2561 || h
->root
.type
== bfd_link_hash_defweak
)
2562 && h
->root
.u
.def
.section
->owner
!= NULL
2563 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2569 /* Return true if symbol H qualifies for the forms of automatic export
2570 specified by AUTO_EXPORT_FLAGS. */
2573 xcoff_auto_export_p (struct bfd_link_info
*info
,
2574 struct xcoff_link_hash_entry
*h
,
2575 unsigned int auto_export_flags
)
2577 /* Don't automatically export things that were explicitly exported. */
2578 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2581 /* Don't export things that we don't define. */
2582 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2585 /* Don't export functions; export their descriptors instead. */
2586 if (h
->root
.root
.string
[0] == '.')
2589 /* We don't export a symbol which is being defined by an object
2590 included from an archive which contains a shared object. The
2591 rationale is that if an archive contains both an unshared and
2592 a shared object, then there must be some reason that the
2593 unshared object is unshared, and we don't want to start
2594 providing a shared version of it. In particular, this solves
2595 a bug involving the _savefNN set of functions. gcc will call
2596 those functions without providing a slot to restore the TOC,
2597 so it is essential that these functions be linked in directly
2598 and not from a shared object, which means that a shared
2599 object which also happens to link them in must not export
2600 them. This is confusing, but I haven't been able to think of
2601 a different approach. Note that the symbols can, of course,
2602 be exported explicitly. */
2603 if (h
->root
.type
== bfd_link_hash_defined
2604 || h
->root
.type
== bfd_link_hash_defweak
)
2608 owner
= h
->root
.u
.def
.section
->owner
;
2610 && owner
->my_archive
!= NULL
2611 && xcoff_archive_contains_shared_object_p (info
, owner
->my_archive
))
2615 /* Otherwise, all symbols are exported by -bexpfull. */
2616 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2619 /* Despite its name, -bexpall exports most but not all symbols. */
2620 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2621 && xcoff_covered_by_expall_p (h
))
2627 /* Return true if relocation REL needs to be copied to the .loader section.
2628 If REL is against a global symbol, H is that symbol, otherwise it
2632 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2633 struct xcoff_link_hash_entry
*h
)
2635 if (!xcoff_hash_table (info
)->loader_section
)
2638 switch (rel
->r_type
)
2645 /* We should never need a .loader reloc for a TOC-relative reloc. */
2649 /* In this case, relocations against defined symbols can be resolved
2652 || h
->root
.type
== bfd_link_hash_defined
2653 || h
->root
.type
== bfd_link_hash_defweak
2654 || h
->root
.type
== bfd_link_hash_common
)
2657 /* We will always provide a local definition of function symbols,
2658 even if we don't have one yet. */
2659 if ((h
->flags
& XCOFF_CALLED
) != 0)
2668 /* Absolute relocations against absolute symbols can be
2669 resolved statically. */
2671 && (h
->root
.type
== bfd_link_hash_defined
2672 || h
->root
.type
== bfd_link_hash_defweak
)
2673 && bfd_is_abs_section (h
->root
.u
.def
.section
))
2680 /* Mark a symbol as not being garbage, including the section in which
2683 static inline bfd_boolean
2684 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2686 if ((h
->flags
& XCOFF_MARK
) != 0)
2689 h
->flags
|= XCOFF_MARK
;
2691 /* If we're marking an undefined symbol, try find some way of
2693 if (!info
->relocatable
2694 && (h
->flags
& XCOFF_IMPORT
) == 0
2695 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2696 && (h
->root
.type
== bfd_link_hash_undefined
2697 || h
->root
.type
== bfd_link_hash_undefweak
))
2699 /* First check whether this symbol can be interpreted as an
2700 undefined function descriptor for a defined function symbol. */
2701 if (!xcoff_find_function (info
, h
))
2704 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2705 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2706 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2708 /* This is a descriptor for a defined symbol, but the input
2709 objects have not defined the descriptor itself. Fill in
2710 the definition automatically.
2712 Note that we do this even if we found a dynamic definition
2713 of H. The local function definition logically overrides
2717 sec
= xcoff_hash_table (info
)->descriptor_section
;
2718 h
->root
.type
= bfd_link_hash_defined
;
2719 h
->root
.u
.def
.section
= sec
;
2720 h
->root
.u
.def
.value
= sec
->size
;
2722 h
->flags
|= XCOFF_DEF_REGULAR
;
2724 /* The size of the function descriptor depends on whether this
2725 is xcoff32 (12) or xcoff64 (24). */
2726 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2728 /* A function descriptor uses two relocs: one for the
2729 associated code, and one for the TOC address. */
2730 xcoff_hash_table (info
)->ldrel_count
+= 2;
2731 sec
->reloc_count
+= 2;
2733 /* Mark the function itself. */
2734 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2737 /* Mark the TOC section, so that we get an anchor
2738 to relocate against. */
2739 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2742 /* We handle writing out the contents of the descriptor in
2743 xcoff_write_global_symbol. */
2745 else if (info
->static_link
)
2746 /* We can't get a symbol value dynamically, so just assume
2747 that it's undefined. */
2748 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2749 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2751 /* This is a function symbol for which we need to create
2754 struct xcoff_link_hash_entry
*hds
;
2756 /* Mark the descriptor (and its TOC section). */
2757 hds
= h
->descriptor
;
2758 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2759 || hds
->root
.type
== bfd_link_hash_undefweak
)
2760 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2761 if (!xcoff_mark_symbol (info
, hds
))
2764 /* Treat this symbol as undefined if the descriptor was. */
2765 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2766 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2768 /* Allocate room for the global linkage code itself. */
2769 sec
= xcoff_hash_table (info
)->linkage_section
;
2770 h
->root
.type
= bfd_link_hash_defined
;
2771 h
->root
.u
.def
.section
= sec
;
2772 h
->root
.u
.def
.value
= sec
->size
;
2774 h
->flags
|= XCOFF_DEF_REGULAR
;
2775 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2777 /* The global linkage code requires a TOC entry for the
2779 if (hds
->toc_section
== NULL
)
2784 xcoff32 uses 4 bytes in the toc.
2785 xcoff64 uses 8 bytes in the toc. */
2786 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2788 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2793 /* Allocate room in the fallback TOC section. */
2794 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2795 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2796 hds
->toc_section
->size
+= byte_size
;
2797 if (!xcoff_mark (info
, hds
->toc_section
))
2800 /* Allocate room for a static and dynamic R_TOC
2802 ++xcoff_hash_table (info
)->ldrel_count
;
2803 ++hds
->toc_section
->reloc_count
;
2805 /* Set the index to -2 to force this symbol to
2808 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2811 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2813 /* Record that the symbol was undefined, then import it.
2814 -brtl links use a special fake import file. */
2815 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2816 if (xcoff_hash_table (info
)->rtld
)
2818 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2823 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2829 if (h
->root
.type
== bfd_link_hash_defined
2830 || h
->root
.type
== bfd_link_hash_defweak
)
2834 hsec
= h
->root
.u
.def
.section
;
2835 if (! bfd_is_abs_section (hsec
)
2836 && (hsec
->flags
& SEC_MARK
) == 0)
2838 if (! xcoff_mark (info
, hsec
))
2843 if (h
->toc_section
!= NULL
2844 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2846 if (! xcoff_mark (info
, h
->toc_section
))
2853 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2854 If the symbol exists, set FLAGS. */
2857 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2858 const char *name
, unsigned int flags
)
2860 struct xcoff_link_hash_entry
*h
;
2862 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2863 FALSE
, FALSE
, TRUE
);
2867 if (h
->root
.type
== bfd_link_hash_defined
2868 || h
->root
.type
== bfd_link_hash_defweak
)
2870 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2877 /* The mark phase of garbage collection. For a given section, mark
2878 it, and all the sections which define symbols to which it refers.
2879 Because this function needs to look at the relocs, we also count
2880 the number of relocs which need to be copied into the .loader
2884 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2886 if (bfd_is_abs_section (sec
)
2887 || (sec
->flags
& SEC_MARK
) != 0)
2890 sec
->flags
|= SEC_MARK
;
2892 if (sec
->owner
->xvec
== info
->output_bfd
->xvec
2893 && coff_section_data (sec
->owner
, sec
) != NULL
2894 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2896 struct xcoff_link_hash_entry
**syms
;
2897 struct internal_reloc
*rel
, *relend
;
2899 unsigned long i
, first
, last
;
2901 /* Mark all the symbols in this section. */
2902 syms
= obj_xcoff_sym_hashes (sec
->owner
);
2903 csects
= xcoff_data (sec
->owner
)->csects
;
2904 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
2905 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
2906 for (i
= first
; i
<= last
; i
++)
2907 if (csects
[i
] == sec
2909 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
2911 if (!xcoff_mark_symbol (info
, syms
[i
]))
2915 /* Look through the section relocs. */
2916 if ((sec
->flags
& SEC_RELOC
) != 0
2917 && sec
->reloc_count
> 0)
2919 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, TRUE
,
2923 relend
= rel
+ sec
->reloc_count
;
2924 for (; rel
< relend
; rel
++)
2926 struct xcoff_link_hash_entry
*h
;
2928 if ((unsigned int) rel
->r_symndx
2929 > obj_raw_syment_count (sec
->owner
))
2932 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2935 if ((h
->flags
& XCOFF_MARK
) == 0)
2937 if (!xcoff_mark_symbol (info
, h
))
2945 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2947 && (rsec
->flags
& SEC_MARK
) == 0)
2949 if (!xcoff_mark (info
, rsec
))
2954 /* See if this reloc needs to be copied into the .loader
2956 if (xcoff_need_ldrel_p (info
, rel
, h
))
2958 ++xcoff_hash_table (info
)->ldrel_count
;
2960 h
->flags
|= XCOFF_LDREL
;
2964 if (! info
->keep_memory
2965 && coff_section_data (sec
->owner
, sec
) != NULL
2966 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2967 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2969 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2970 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2978 /* Routines that are called after all the input files have been
2979 handled, but before the sections are laid out in memory. */
2981 /* The sweep phase of garbage collection. Remove all garbage
2985 xcoff_sweep (struct bfd_link_info
*info
)
2989 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2993 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2995 if ((o
->flags
& SEC_MARK
) == 0)
2997 /* Keep all sections from non-XCOFF input files. Keep
2998 special sections. Keep .debug sections for the
3000 if (sub
->xvec
!= info
->output_bfd
->xvec
3001 || o
== xcoff_hash_table (info
)->debug_section
3002 || o
== xcoff_hash_table (info
)->loader_section
3003 || o
== xcoff_hash_table (info
)->linkage_section
3004 || o
== xcoff_hash_table (info
)->descriptor_section
3005 || (bfd_get_section_flags (sub
, o
) & SEC_DEBUGGING
)
3006 || strcmp (o
->name
, ".debug") == 0)
3007 o
->flags
|= SEC_MARK
;
3018 /* Record the number of elements in a set. This is used to output the
3019 correct csect length. */
3022 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3023 struct bfd_link_info
*info
,
3024 struct bfd_link_hash_entry
*harg
,
3027 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3028 struct xcoff_link_size_list
*n
;
3031 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3034 /* This will hardly ever be called. I don't want to burn four bytes
3035 per global symbol, so instead the size is kept on a linked list
3036 attached to the hash table. */
3038 n
= bfd_alloc (output_bfd
, amt
);
3041 n
->next
= xcoff_hash_table (info
)->size_list
;
3044 xcoff_hash_table (info
)->size_list
= n
;
3046 h
->flags
|= XCOFF_HAS_SIZE
;
3051 /* Import a symbol. */
3054 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3055 struct bfd_link_info
*info
,
3056 struct bfd_link_hash_entry
*harg
,
3058 const char *imppath
,
3059 const char *impfile
,
3060 const char *impmember
,
3061 unsigned int syscall_flag
)
3063 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3065 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3068 /* A symbol name which starts with a period is the code for a
3069 function. If the symbol is undefined, then add an undefined
3070 symbol for the function descriptor, and import that instead. */
3071 if (h
->root
.root
.string
[0] == '.'
3072 && h
->root
.type
== bfd_link_hash_undefined
3073 && val
== (bfd_vma
) -1)
3075 struct xcoff_link_hash_entry
*hds
;
3077 hds
= h
->descriptor
;
3080 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3081 h
->root
.root
.string
+ 1,
3085 if (hds
->root
.type
== bfd_link_hash_new
)
3087 hds
->root
.type
= bfd_link_hash_undefined
;
3088 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3090 hds
->flags
|= XCOFF_DESCRIPTOR
;
3091 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3092 hds
->descriptor
= h
;
3093 h
->descriptor
= hds
;
3096 /* Now, if the descriptor is undefined, import the descriptor
3097 rather than the symbol we were told to import. FIXME: Is
3098 this correct in all cases? */
3099 if (hds
->root
.type
== bfd_link_hash_undefined
)
3103 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3105 if (val
!= (bfd_vma
) -1)
3107 if (h
->root
.type
== bfd_link_hash_defined
3108 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
3109 || h
->root
.u
.def
.value
!= val
))
3111 if (! ((*info
->callbacks
->multiple_definition
)
3112 (info
, &h
->root
, output_bfd
, bfd_abs_section_ptr
, val
)))
3116 h
->root
.type
= bfd_link_hash_defined
;
3117 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3118 h
->root
.u
.def
.value
= val
;
3122 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3128 /* Export a symbol. */
3131 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3132 struct bfd_link_info
*info
,
3133 struct bfd_link_hash_entry
*harg
)
3135 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3137 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3140 h
->flags
|= XCOFF_EXPORT
;
3142 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3143 I'm just going to ignore it until somebody explains it. */
3145 /* Make sure we don't garbage collect this symbol. */
3146 if (! xcoff_mark_symbol (info
, h
))
3149 /* If this is a function descriptor, make sure we don't garbage
3150 collect the associated function code. We normally don't have to
3151 worry about this, because the descriptor will be attached to a
3152 section with relocs, but if we are creating the descriptor
3153 ourselves those relocs will not be visible to the mark code. */
3154 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3156 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3163 /* Count a reloc against a symbol. This is called for relocs
3164 generated by the linker script, typically for global constructors
3168 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3169 struct bfd_link_info
*info
,
3172 struct xcoff_link_hash_entry
*h
;
3174 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3177 h
= ((struct xcoff_link_hash_entry
*)
3178 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, FALSE
, FALSE
,
3182 (*_bfd_error_handler
) (_("%s: no such symbol"), name
);
3183 bfd_set_error (bfd_error_no_symbols
);
3187 h
->flags
|= XCOFF_REF_REGULAR
;
3188 if (xcoff_hash_table (info
)->loader_section
)
3190 h
->flags
|= XCOFF_LDREL
;
3191 ++xcoff_hash_table (info
)->ldrel_count
;
3194 /* Mark the symbol to avoid garbage collection. */
3195 if (! xcoff_mark_symbol (info
, h
))
3201 /* This function is called for each symbol to which the linker script
3205 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3206 struct bfd_link_info
*info
,
3209 struct xcoff_link_hash_entry
*h
;
3211 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3214 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
, TRUE
,
3219 h
->flags
|= XCOFF_DEF_REGULAR
;
3224 /* An xcoff_link_hash_traverse callback for which DATA points to an
3225 xcoff_loader_info. Mark all symbols that should be automatically
3229 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3231 struct xcoff_loader_info
*ldinfo
;
3233 ldinfo
= (struct xcoff_loader_info
*) data
;
3234 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3236 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3237 ldinfo
->failed
= TRUE
;
3242 /* Add a symbol to the .loader symbols, if necessary. */
3244 /* INPUT_BFD has an external symbol associated with hash table entry H
3245 and csect CSECT. Return true if INPUT_BFD defines H. */
3248 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3251 switch (h
->root
.type
)
3253 case bfd_link_hash_defined
:
3254 case bfd_link_hash_defweak
:
3255 /* No input bfd owns absolute symbols. They are written by
3256 xcoff_write_global_symbol instead. */
3257 return (!bfd_is_abs_section (csect
)
3258 && h
->root
.u
.def
.section
== csect
);
3260 case bfd_link_hash_common
:
3261 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3263 case bfd_link_hash_undefined
:
3264 case bfd_link_hash_undefweak
:
3265 /* We can't treat undef.abfd as the owner because that bfd
3266 might be a dynamic object. Allow any bfd to claim it. */
3274 /* See if H should have a loader symbol associated with it. */
3277 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3278 struct xcoff_link_hash_entry
*h
)
3282 /* Warn if this symbol is exported but not defined. */
3283 if ((h
->flags
& XCOFF_EXPORT
) != 0
3284 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3286 (*_bfd_error_handler
)
3287 (_("warning: attempt to export undefined symbol `%s'"),
3288 h
->root
.root
.string
);
3292 /* We need to add a symbol to the .loader section if it is mentioned
3293 in a reloc which we are copying to the .loader section and it was
3294 not defined or common, or if it is the entry point, or if it is
3296 if (((h
->flags
& XCOFF_LDREL
) == 0
3297 || h
->root
.type
== bfd_link_hash_defined
3298 || h
->root
.type
== bfd_link_hash_defweak
3299 || h
->root
.type
== bfd_link_hash_common
)
3300 && (h
->flags
& XCOFF_ENTRY
) == 0
3301 && (h
->flags
& XCOFF_EXPORT
) == 0)
3304 /* We need to add this symbol to the .loader symbols. */
3306 BFD_ASSERT (h
->ldsym
== NULL
);
3307 amt
= sizeof (struct internal_ldsym
);
3308 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3309 if (h
->ldsym
== NULL
)
3311 ldinfo
->failed
= TRUE
;
3315 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3317 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3318 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3320 h
->ldsym
->l_ifile
= h
->ldindx
;
3323 /* The first 3 symbol table indices are reserved to indicate the
3324 data, text and bss sections. */
3325 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3327 ++ldinfo
->ldsym_count
;
3329 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3330 h
->ldsym
, h
->root
.root
.string
))
3333 h
->flags
|= XCOFF_BUILT_LDSYM
;
3337 /* An xcoff_htab_traverse callback that is called for each symbol
3338 once garbage collection is complete. */
3341 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3343 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3345 /* __rtinit, this symbol has special handling. */
3346 if (h
->flags
& XCOFF_RTINIT
)
3349 /* We don't want to garbage collect symbols which are not defined in
3350 XCOFF files. This is a convenient place to mark them. */
3351 if (xcoff_hash_table (ldinfo
->info
)->gc
3352 && (h
->flags
& XCOFF_MARK
) == 0
3353 && (h
->root
.type
== bfd_link_hash_defined
3354 || h
->root
.type
== bfd_link_hash_defweak
)
3355 && (h
->root
.u
.def
.section
->owner
== NULL
3356 || (h
->root
.u
.def
.section
->owner
->xvec
3357 != ldinfo
->info
->output_bfd
->xvec
)))
3358 h
->flags
|= XCOFF_MARK
;
3360 /* Skip discarded symbols. */
3361 if (xcoff_hash_table (ldinfo
->info
)->gc
3362 && (h
->flags
& XCOFF_MARK
) == 0)
3365 /* If this is still a common symbol, and it wasn't garbage
3366 collected, we need to actually allocate space for it in the .bss
3368 if (h
->root
.type
== bfd_link_hash_common
3369 && h
->root
.u
.c
.p
->section
->size
== 0)
3371 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3372 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3375 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3377 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3378 h
->flags
|= XCOFF_EXPORT
;
3380 if (!xcoff_build_ldsym (ldinfo
, h
))
3387 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3388 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3389 csect information, if any. NAME is the function's name if the name
3390 is stored in the .debug section, otherwise it is null.
3392 Return 1 if we should include an appropriately-adjusted ISYM
3393 in the output file, 0 if we should discard ISYM, or -1 if an
3397 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3398 struct internal_syment
*isym
,
3399 union internal_auxent
*aux
,
3400 struct xcoff_link_hash_entry
*h
,
3401 asection
*csect
, const char *name
)
3405 /* If we are skipping this csect, we want to strip the symbol too. */
3409 /* Likewise if we garbage-collected the csect. */
3410 if (xcoff_hash_table (info
)->gc
3411 && !bfd_is_abs_section (csect
)
3412 && !bfd_is_und_section (csect
)
3413 && (csect
->flags
& SEC_MARK
) == 0)
3416 /* An XCOFF linker always removes C_STAT symbols. */
3417 if (isym
->n_sclass
== C_STAT
)
3420 /* We generate the TOC anchor separately. */
3421 if (isym
->n_sclass
== C_HIDEXT
3422 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3425 /* If we are stripping all symbols, we want to discard this one. */
3426 if (info
->strip
== strip_all
)
3429 /* Discard symbols that are defined elsewhere. */
3430 if (EXTERN_SYM_P (isym
->n_sclass
))
3432 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3434 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3438 /* If we're discarding local symbols, check whether ISYM is local. */
3439 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3440 if (info
->discard
== discard_all
3441 && !EXTERN_SYM_P (isym
->n_sclass
)
3442 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3445 /* If we're stripping debugging symbols, check whether ISYM is one. */
3446 if (info
->strip
== strip_debugger
3447 && isym
->n_scnum
== N_DEBUG
)
3450 /* If we are stripping symbols based on name, check how ISYM's
3451 name should be handled. */
3452 if (info
->strip
== strip_some
3453 || info
->discard
== discard_l
)
3455 char buf
[SYMNMLEN
+ 1];
3459 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3464 if (info
->strip
== strip_some
3465 && bfd_hash_lookup (info
->keep_hash
, name
, FALSE
, FALSE
) == NULL
)
3468 if (info
->discard
== discard_l
3469 && !EXTERN_SYM_P (isym
->n_sclass
)
3470 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3471 && bfd_is_local_label_name (input_bfd
, name
))
3478 /* Lay out the .loader section, filling in the header and the import paths.
3479 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3482 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
,
3483 const char *libpath
)
3486 struct xcoff_link_hash_table
*htab
;
3487 struct internal_ldhdr
*ldhdr
;
3488 struct xcoff_import_file
*fl
;
3489 bfd_size_type stoff
;
3490 size_t impsize
, impcount
;
3494 /* Work out the size of the import file names. Each import file ID
3495 consists of three null terminated strings: the path, the file
3496 name, and the archive member name. The first entry in the list
3497 of names is the path to use to find objects, which the linker has
3498 passed in as the libpath argument. For some reason, the path
3499 entry in the other import file names appears to always be empty. */
3500 output_bfd
= ldinfo
->output_bfd
;
3501 htab
= xcoff_hash_table (ldinfo
->info
);
3502 impsize
= strlen (libpath
) + 3;
3504 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3507 impsize
+= (strlen (fl
->path
)
3509 + strlen (fl
->member
)
3513 /* Set up the .loader section header. */
3514 ldhdr
= &htab
->ldhdr
;
3515 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3516 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3517 ldhdr
->l_nreloc
= htab
->ldrel_count
;
3518 ldhdr
->l_istlen
= impsize
;
3519 ldhdr
->l_nimpid
= impcount
;
3520 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3521 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3522 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3523 ldhdr
->l_stlen
= ldinfo
->string_size
;
3524 stoff
= ldhdr
->l_impoff
+ impsize
;
3525 if (ldinfo
->string_size
== 0)
3528 ldhdr
->l_stoff
= stoff
;
3530 /* 64 bit elements to ldhdr
3531 The swap out routine for 32 bit will ignore them.
3532 Nothing fancy, symbols come after the header and relocs come
3534 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3535 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3536 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3538 /* We now know the final size of the .loader section. Allocate
3540 lsec
= htab
->loader_section
;
3541 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3542 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3543 if (lsec
->contents
== NULL
)
3546 /* Set up the header. */
3547 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3549 /* Set up the import file names. */
3550 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3551 strcpy (out
, libpath
);
3552 out
+= strlen (libpath
) + 1;
3555 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3560 while ((*out
++ = *s
++) != '\0')
3563 while ((*out
++ = *s
++) != '\0')
3566 while ((*out
++ = *s
++) != '\0')
3570 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3572 /* Set up the symbol string table. */
3573 if (ldinfo
->string_size
> 0)
3575 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
3576 free (ldinfo
->strings
);
3577 ldinfo
->strings
= NULL
;
3580 /* We can't set up the symbol table or the relocs yet, because we
3581 don't yet know the final position of the various sections. The
3582 .loader symbols are written out when the corresponding normal
3583 symbols are written out in xcoff_link_input_bfd or
3584 xcoff_write_global_symbol. The .loader relocs are written out
3585 when the corresponding normal relocs are handled in
3586 xcoff_link_input_bfd. */
3591 /* Build the .loader section. This is called by the XCOFF linker
3592 emulation before_allocation routine. We must set the size of the
3593 .loader section before the linker lays out the output file.
3594 LIBPATH is the library path to search for shared objects; this is
3595 normally built from the -L arguments passed to the linker. ENTRY
3596 is the name of the entry point symbol (the -e linker option).
3597 FILE_ALIGN is the alignment to use for sections within the file
3598 (the -H linker option). MAXSTACK is the maximum stack size (the
3599 -bmaxstack linker option). MAXDATA is the maximum data size (the
3600 -bmaxdata linker option). GC is whether to do garbage collection
3601 (the -bgc linker option). MODTYPE is the module type (the
3602 -bmodtype linker option). TEXTRO is whether the text section must
3603 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3604 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3605 is set by this routine to csects with magic names like _end. */
3608 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3609 struct bfd_link_info
*info
,
3610 const char *libpath
,
3612 unsigned long file_align
,
3613 unsigned long maxstack
,
3614 unsigned long maxdata
,
3618 unsigned int auto_export_flags
,
3619 asection
**special_sections
,
3622 struct xcoff_loader_info ldinfo
;
3626 struct bfd_strtab_hash
*debug_strtab
;
3627 bfd_byte
*debug_contents
= NULL
;
3630 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3632 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3633 special_sections
[i
] = NULL
;
3637 ldinfo
.failed
= FALSE
;
3638 ldinfo
.output_bfd
= output_bfd
;
3640 ldinfo
.auto_export_flags
= auto_export_flags
;
3641 ldinfo
.ldsym_count
= 0;
3642 ldinfo
.string_size
= 0;
3643 ldinfo
.strings
= NULL
;
3644 ldinfo
.string_alc
= 0;
3646 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3647 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3648 xcoff_data (output_bfd
)->modtype
= modtype
;
3650 xcoff_hash_table (info
)->file_align
= file_align
;
3651 xcoff_hash_table (info
)->textro
= textro
;
3652 xcoff_hash_table (info
)->rtld
= rtld
;
3655 if (xcoff_hash_table (info
)->loader_section
3656 && (info
->init_function
|| info
->fini_function
|| rtld
))
3658 struct xcoff_link_hash_entry
*hsym
;
3659 struct internal_ldsym
*ldsym
;
3661 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3662 "__rtinit", FALSE
, FALSE
, TRUE
);
3665 (*_bfd_error_handler
)
3666 (_("error: undefined symbol __rtinit"));
3670 xcoff_mark_symbol (info
, hsym
);
3671 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3673 /* __rtinit initialized. */
3674 amt
= sizeof (* ldsym
);
3675 ldsym
= bfd_malloc (amt
);
3677 ldsym
->l_value
= 0; /* Will be filled in later. */
3678 ldsym
->l_scnum
= 2; /* Data section. */
3679 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3680 ldsym
->l_smclas
= 5; /* .rw. */
3681 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3682 ldsym
->l_parm
= 0; /* NA. */
3684 /* Force __rtinit to be the first symbol in the loader symbol table
3685 See xcoff_build_ldsyms
3687 The first 3 symbol table indices are reserved to indicate the data,
3688 text and bss sections. */
3689 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3692 ldinfo
.ldsym_count
= 1;
3693 hsym
->ldsym
= ldsym
;
3695 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3696 hsym
->ldsym
, hsym
->root
.root
.string
))
3699 /* This symbol is written out by xcoff_write_global_symbol
3700 Set stuff up so xcoff_write_global_symbol logic works. */
3701 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3702 hsym
->root
.type
= bfd_link_hash_defined
;
3703 hsym
->root
.u
.def
.value
= 0;
3706 /* Garbage collect unused sections. */
3707 if (info
->relocatable
|| !gc
)
3710 xcoff_hash_table (info
)->gc
= FALSE
;
3712 /* We still need to call xcoff_mark, in order to set ldrel_count
3714 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3718 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3720 /* We shouldn't unconditionaly mark the TOC section.
3721 The output file should only have a TOC if either
3722 (a) one of the input files did or (b) we end up
3723 creating TOC references as part of the link process. */
3724 if (o
!= xcoff_hash_table (info
)->toc_section
3725 && (o
->flags
& SEC_MARK
) == 0)
3727 if (! xcoff_mark (info
, o
))
3736 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3738 if (info
->init_function
!= NULL
3739 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3741 if (info
->fini_function
!= NULL
3742 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3744 if (auto_export_flags
!= 0)
3746 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3747 xcoff_mark_auto_exports
, &ldinfo
);
3752 xcoff_hash_table (info
)->gc
= TRUE
;
3755 /* Return special sections to the caller. */
3756 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3758 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3762 && (sec
->flags
& SEC_MARK
) == 0)
3765 special_sections
[i
] = sec
;
3768 if (info
->input_bfds
== NULL
)
3769 /* I'm not sure what to do in this bizarre case. */
3772 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3777 if (xcoff_hash_table (info
)->loader_section
3778 && !xcoff_build_loader_section (&ldinfo
, libpath
))
3781 /* Allocate space for the magic sections. */
3782 sec
= xcoff_hash_table (info
)->linkage_section
;
3785 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3786 if (sec
->contents
== NULL
)
3789 sec
= xcoff_hash_table (info
)->toc_section
;
3792 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3793 if (sec
->contents
== NULL
)
3796 sec
= xcoff_hash_table (info
)->descriptor_section
;
3799 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3800 if (sec
->contents
== NULL
)
3804 /* Now that we've done garbage collection, decide which symbols to keep,
3805 and figure out the contents of the .debug section. */
3806 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3808 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3811 bfd_size_type symcount
;
3814 unsigned int *lineno_counts
;
3815 struct xcoff_link_hash_entry
**sym_hash
;
3816 bfd_byte
*esym
, *esymend
;
3817 bfd_size_type symesz
;
3819 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3822 if ((sub
->flags
& DYNAMIC
) != 0
3823 && !info
->static_link
)
3826 if (! _bfd_coff_get_external_symbols (sub
))
3829 symcount
= obj_raw_syment_count (sub
);
3830 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
3831 if (debug_index
== NULL
)
3833 xcoff_data (sub
)->debug_indices
= debug_index
;
3835 if (info
->strip
== strip_all
3836 || info
->strip
== strip_debugger
3837 || info
->discard
== discard_all
)
3838 /* We're stripping all debugging information, so there's no need
3839 to read SUB's .debug section. */
3843 /* Grab the contents of SUB's .debug section, if any. */
3844 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3845 if (subdeb
!= NULL
&& subdeb
->size
> 0)
3847 /* We use malloc and copy the names into the debug
3848 stringtab, rather than bfd_alloc, because I expect
3849 that, when linking many files together, many of the
3850 strings will be the same. Storing the strings in the
3851 hash table should save space in this case. */
3852 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
3857 csectpp
= xcoff_data (sub
)->csects
;
3858 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
3859 sym_hash
= obj_xcoff_sym_hashes (sub
);
3860 symesz
= bfd_coff_symesz (sub
);
3861 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3862 esymend
= esym
+ symcount
* symesz
;
3864 while (esym
< esymend
)
3866 struct internal_syment sym
;
3867 union internal_auxent aux
;
3872 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
3874 /* Read in the csect information, if any. */
3875 if (CSECT_SYM_P (sym
.n_sclass
))
3877 BFD_ASSERT (sym
.n_numaux
> 0);
3878 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
3879 sym
.n_type
, sym
.n_sclass
,
3880 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
3883 /* If this symbol's name is stored in the debug section,
3884 get a pointer to it. */
3885 if (debug_contents
!= NULL
3886 && sym
._n
._n_n
._n_zeroes
== 0
3887 && bfd_coff_symname_in_debug (sub
, &sym
))
3888 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3892 /* Decide whether to copy this symbol to the output file. */
3894 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
3895 *sym_hash
, csect
, name
);
3900 /* Use a debug_index of -2 to record that a symbol should
3905 /* See whether we should store the symbol name in the
3906 output .debug section. */
3911 indx
= _bfd_stringtab_add (debug_strtab
, name
, TRUE
, TRUE
);
3912 if (indx
== (bfd_size_type
) -1)
3914 *debug_index
= indx
;
3919 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
3920 if (*lineno_counts
> 0)
3921 csect
->output_section
->lineno_count
+= *lineno_counts
;
3924 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3925 csectpp
+= sym
.n_numaux
+ 1;
3926 sym_hash
+= sym
.n_numaux
+ 1;
3927 lineno_counts
+= sym
.n_numaux
+ 1;
3928 debug_index
+= sym
.n_numaux
+ 1;
3933 free (debug_contents
);
3934 debug_contents
= NULL
;
3936 /* Clear the size of subdeb, so that it is not included directly
3937 in the output file. */
3941 if (! info
->keep_memory
)
3943 if (! _bfd_coff_free_symbols (sub
))
3948 if (info
->strip
!= strip_all
)
3949 xcoff_hash_table (info
)->debug_section
->size
=
3950 _bfd_stringtab_size (debug_strtab
);
3955 if (ldinfo
.strings
!= NULL
)
3956 free (ldinfo
.strings
);
3957 if (debug_contents
!= NULL
)
3958 free (debug_contents
);
3963 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
3968 struct bfd_in_memory
*bim
;
3970 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
3977 abfd
->link_next
= 0;
3978 abfd
->format
= bfd_object
;
3979 abfd
->iostream
= (void *) bim
;
3980 abfd
->flags
= BFD_IN_MEMORY
;
3981 abfd
->iovec
= &_bfd_memory_iovec
;
3982 abfd
->direction
= write_direction
;
3986 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
3989 /* need to reset to unknown or it will not be read back in correctly */
3990 abfd
->format
= bfd_unknown
;
3991 abfd
->direction
= read_direction
;
3997 /* Return the section that defines H. Return null if no section does. */
4000 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
4002 switch (h
->root
.type
)
4004 case bfd_link_hash_defined
:
4005 case bfd_link_hash_defweak
:
4006 return h
->root
.u
.def
.section
;
4008 case bfd_link_hash_common
:
4009 return h
->root
.u
.c
.p
->section
;
4016 /* Add a .loader relocation for input relocation IREL. If the loader
4017 relocation should be against an output section, HSEC points to the
4018 input section that IREL is against, otherwise HSEC is null. H is the
4019 symbol that IREL is against, or null if it isn't against a global symbol.
4020 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4023 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
4024 asection
*output_section
, bfd
*reference_bfd
,
4025 struct internal_reloc
*irel
, asection
*hsec
,
4026 struct xcoff_link_hash_entry
*h
)
4028 struct internal_ldrel ldrel
;
4030 ldrel
.l_vaddr
= irel
->r_vaddr
;
4033 const char *secname
;
4035 secname
= hsec
->output_section
->name
;
4036 if (strcmp (secname
, ".text") == 0)
4038 else if (strcmp (secname
, ".data") == 0)
4040 else if (strcmp (secname
, ".bss") == 0)
4044 (*_bfd_error_handler
)
4045 (_("%B: loader reloc in unrecognized section `%s'"),
4046 reference_bfd
, secname
);
4047 bfd_set_error (bfd_error_nonrepresentable_section
);
4055 (*_bfd_error_handler
)
4056 (_("%B: `%s' in loader reloc but not loader sym"),
4057 reference_bfd
, h
->root
.root
.string
);
4058 bfd_set_error (bfd_error_bad_value
);
4061 ldrel
.l_symndx
= h
->ldindx
;
4064 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4066 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4067 ldrel
.l_rsecnm
= output_section
->target_index
;
4068 if (xcoff_hash_table (flinfo
->info
)->textro
4069 && strcmp (output_section
->name
, ".text") == 0)
4071 (*_bfd_error_handler
)
4072 (_("%B: loader reloc in read-only section %A"),
4073 reference_bfd
, output_section
);
4074 bfd_set_error (bfd_error_invalid_operation
);
4077 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
4078 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4082 /* Link an input file into the linker output file. This function
4083 handles all the sections and relocations of the input file at once. */
4086 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
4090 const char *strings
;
4091 bfd_size_type syment_base
;
4092 unsigned int n_tmask
;
4093 unsigned int n_btshft
;
4094 bfd_boolean copy
, hash
;
4095 bfd_size_type isymesz
;
4096 bfd_size_type osymesz
;
4097 bfd_size_type linesz
;
4100 struct xcoff_link_hash_entry
**sym_hash
;
4101 struct internal_syment
*isymp
;
4103 unsigned int *lineno_counts
;
4106 unsigned long output_index
;
4110 bfd_boolean keep_syms
;
4113 /* We can just skip DYNAMIC files, unless this is a static link. */
4114 if ((input_bfd
->flags
& DYNAMIC
) != 0
4115 && ! flinfo
->info
->static_link
)
4118 /* Move all the symbols to the output file. */
4119 output_bfd
= flinfo
->output_bfd
;
4121 syment_base
= obj_raw_syment_count (output_bfd
);
4122 isymesz
= bfd_coff_symesz (input_bfd
);
4123 osymesz
= bfd_coff_symesz (output_bfd
);
4124 linesz
= bfd_coff_linesz (input_bfd
);
4125 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4127 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4128 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4130 /* Define macros so that ISFCN, et. al., macros work correctly. */
4131 #define N_TMASK n_tmask
4132 #define N_BTSHFT n_btshft
4135 if (! flinfo
->info
->keep_memory
)
4138 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4141 if (! _bfd_coff_get_external_symbols (input_bfd
))
4144 /* Make one pass over the symbols and assign indices to symbols that
4145 we have decided to keep. Also use create .loader symbol information
4146 and update information in hash table entries. */
4147 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4148 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4149 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4150 csectpp
= xcoff_data (input_bfd
)->csects
;
4151 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4152 isymp
= flinfo
->internal_syms
;
4153 indexp
= flinfo
->sym_indices
;
4154 output_index
= syment_base
;
4155 while (esym
< esym_end
)
4157 union internal_auxent aux
;
4161 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4163 /* Read in the csect information, if any. */
4164 if (CSECT_SYM_P (isymp
->n_sclass
))
4166 BFD_ASSERT (isymp
->n_numaux
> 0);
4167 bfd_coff_swap_aux_in (input_bfd
,
4168 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4169 isymp
->n_type
, isymp
->n_sclass
,
4170 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4173 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4176 /* If this symbol is in the .loader section, swap out the
4177 .loader symbol information. If this is an external symbol
4178 reference to a defined symbol, though, then wait until we get
4179 to the definition. */
4180 if (EXTERN_SYM_P (isymp
->n_sclass
)
4181 && *sym_hash
!= NULL
4182 && (*sym_hash
)->ldsym
!= NULL
4183 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4185 struct xcoff_link_hash_entry
*h
;
4186 struct internal_ldsym
*ldsym
;
4190 if (isymp
->n_scnum
> 0)
4192 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4193 ldsym
->l_value
= (isymp
->n_value
4194 + (*csectpp
)->output_section
->vma
4195 + (*csectpp
)->output_offset
4200 ldsym
->l_scnum
= isymp
->n_scnum
;
4201 ldsym
->l_value
= isymp
->n_value
;
4204 ldsym
->l_smtype
= smtyp
;
4205 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4206 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4207 || (h
->flags
& XCOFF_IMPORT
) != 0)
4208 ldsym
->l_smtype
|= L_IMPORT
;
4209 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4210 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4211 || (h
->flags
& XCOFF_EXPORT
) != 0)
4212 ldsym
->l_smtype
|= L_EXPORT
;
4213 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4214 ldsym
->l_smtype
|= L_ENTRY
;
4215 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4216 ldsym
->l_smtype
|= L_WEAK
;
4218 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4220 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4222 else if (ldsym
->l_ifile
== 0)
4224 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4230 if (h
->root
.type
== bfd_link_hash_defined
4231 || h
->root
.type
== bfd_link_hash_defweak
)
4232 impbfd
= h
->root
.u
.def
.section
->owner
;
4233 else if (h
->root
.type
== bfd_link_hash_undefined
4234 || h
->root
.type
== bfd_link_hash_undefweak
)
4235 impbfd
= h
->root
.u
.undef
.abfd
;
4243 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
4244 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4251 BFD_ASSERT (h
->ldindx
>= 0);
4252 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
4255 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
4258 /* Fill in snentry now that we know the target_index. */
4259 if ((h
->flags
& XCOFF_ENTRY
) != 0
4260 && (h
->root
.type
== bfd_link_hash_defined
4261 || h
->root
.type
== bfd_link_hash_defweak
))
4263 xcoff_data (output_bfd
)->snentry
=
4264 h
->root
.u
.def
.section
->output_section
->target_index
;
4268 add
= 1 + isymp
->n_numaux
;
4270 if (*debug_index
== -2)
4271 /* We've decided to strip this symbol. */
4275 /* Assign the next unused index to this symbol. */
4276 *indexp
= output_index
;
4278 if (EXTERN_SYM_P (isymp
->n_sclass
))
4280 BFD_ASSERT (*sym_hash
!= NULL
);
4281 (*sym_hash
)->indx
= output_index
;
4284 /* If this is a symbol in the TOC which we may have merged
4285 (class XMC_TC), remember the symbol index of the TOC
4287 if (isymp
->n_sclass
== C_HIDEXT
4288 && aux
.x_csect
.x_smclas
== XMC_TC
4289 && *sym_hash
!= NULL
)
4291 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4292 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4293 (*sym_hash
)->u
.toc_indx
= output_index
;
4296 output_index
+= add
;
4299 esym
+= add
* isymesz
;
4305 for (--add
; add
> 0; --add
)
4309 /* Now write out the symbols that we decided to keep. */
4311 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4312 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4313 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4314 isymp
= flinfo
->internal_syms
;
4315 indexp
= flinfo
->sym_indices
;
4316 csectpp
= xcoff_data (input_bfd
)->csects
;
4317 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4318 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4319 outsym
= flinfo
->outsyms
;
4322 while (esym
< esym_end
)
4326 add
= 1 + isymp
->n_numaux
;
4329 esym
+= add
* isymesz
;
4332 struct internal_syment isym
;
4335 /* Adjust the symbol in order to output it. */
4337 if (isym
._n
._n_n
._n_zeroes
== 0
4338 && isym
._n
._n_n
._n_offset
!= 0)
4340 /* This symbol has a long name. Enter it in the string
4341 table we are building. If *debug_index != -1, the
4342 name has already been entered in the .debug section. */
4343 if (*debug_index
>= 0)
4344 isym
._n
._n_n
._n_offset
= *debug_index
;
4350 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4354 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
4355 if (indx
== (bfd_size_type
) -1)
4357 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4361 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4362 multiple definition problems when linking a shared object
4363 statically. (The native linker doesn't enter __rtinit into
4364 the normal table at all, but having a local symbol can make
4365 the objdump output easier to read.) */
4366 if (isym
.n_sclass
== C_EXT
4368 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4369 isym
.n_sclass
= C_HIDEXT
;
4371 /* The value of a C_FILE symbol is the symbol index of the
4372 next C_FILE symbol. The value of the last C_FILE symbol
4373 is -1. We try to get this right, below, just before we
4374 write the symbols out, but in the general case we may
4375 have to write the symbol out twice. */
4376 if (isym
.n_sclass
== C_FILE
)
4378 if (flinfo
->last_file_index
!= -1
4379 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4381 /* We must correct the value of the last C_FILE entry. */
4382 flinfo
->last_file
.n_value
= *indexp
;
4383 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4385 /* The last C_FILE symbol is in this input file. */
4386 bfd_coff_swap_sym_out (output_bfd
,
4387 (void *) &flinfo
->last_file
,
4388 (void *) (flinfo
->outsyms
4389 + ((flinfo
->last_file_index
4395 /* We have already written out the last C_FILE
4396 symbol. We need to write it out again. We
4397 borrow *outsym temporarily. */
4400 bfd_coff_swap_sym_out (output_bfd
,
4401 (void *) &flinfo
->last_file
,
4404 pos
= obj_sym_filepos (output_bfd
);
4405 pos
+= flinfo
->last_file_index
* osymesz
;
4406 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4407 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4413 flinfo
->last_file_index
= *indexp
;
4414 flinfo
->last_file
= isym
;
4417 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4418 into the line numbers. We update the symbol values when
4419 we handle the line numbers. */
4420 if (isym
.n_sclass
== C_BINCL
4421 || isym
.n_sclass
== C_EINCL
)
4423 isym
.n_value
= flinfo
->line_filepos
;
4426 /* The value of a C_BSTAT symbol is the symbol table
4427 index of the containing csect. */
4428 else if (isym
.n_sclass
== C_BSTAT
)
4432 indx
= isym
.n_value
;
4433 if (indx
< obj_raw_syment_count (input_bfd
))
4437 symindx
= flinfo
->sym_indices
[indx
];
4441 isym
.n_value
= symindx
;
4444 else if (isym
.n_sclass
!= C_ESTAT
4445 && isym
.n_sclass
!= C_DECL
4446 && isym
.n_scnum
> 0)
4448 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4449 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4450 + (*csectpp
)->output_offset
4454 /* Output the symbol. */
4455 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4460 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4462 union internal_auxent aux
;
4464 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4465 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4468 if (isymp
->n_sclass
== C_FILE
)
4470 /* This is the file name (or some comment put in by
4471 the compiler). If it is long, we must put it in
4472 the string table. */
4473 if (aux
.x_file
.x_n
.x_zeroes
== 0
4474 && aux
.x_file
.x_n
.x_offset
!= 0)
4476 const char *filename
;
4479 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4480 >= STRING_SIZE_SIZE
);
4481 if (strings
== NULL
)
4483 strings
= _bfd_coff_read_string_table (input_bfd
);
4484 if (strings
== NULL
)
4487 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4488 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
4490 if (indx
== (bfd_size_type
) -1)
4492 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4495 else if (CSECT_SYM_P (isymp
->n_sclass
)
4496 && i
+ 1 == isymp
->n_numaux
)
4499 /* We don't support type checking. I don't know if
4501 aux
.x_csect
.x_parmhash
= 0;
4502 /* I don't think anybody uses these fields, but we'd
4503 better clobber them just in case. */
4504 aux
.x_csect
.x_stab
= 0;
4505 aux
.x_csect
.x_snstab
= 0;
4507 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4511 indx
= aux
.x_csect
.x_scnlen
.l
;
4512 if (indx
< obj_raw_syment_count (input_bfd
))
4516 symindx
= flinfo
->sym_indices
[indx
];
4519 aux
.x_csect
.x_scnlen
.l
= 0;
4523 aux
.x_csect
.x_scnlen
.l
= symindx
;
4528 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4532 if (ISFCN (isymp
->n_type
)
4533 || ISTAG (isymp
->n_sclass
)
4534 || isymp
->n_sclass
== C_BLOCK
4535 || isymp
->n_sclass
== C_FCN
)
4537 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4539 && indx
< obj_raw_syment_count (input_bfd
))
4541 /* We look forward through the symbol for
4542 the index of the next symbol we are going
4543 to include. I don't know if this is
4545 while (flinfo
->sym_indices
[indx
] < 0
4546 && indx
< obj_raw_syment_count (input_bfd
))
4548 if (indx
>= obj_raw_syment_count (input_bfd
))
4549 indx
= output_index
;
4551 indx
= flinfo
->sym_indices
[indx
];
4552 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4557 indx
= aux
.x_sym
.x_tagndx
.l
;
4558 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4562 symindx
= flinfo
->sym_indices
[indx
];
4564 aux
.x_sym
.x_tagndx
.l
= 0;
4566 aux
.x_sym
.x_tagndx
.l
= symindx
;
4571 /* Copy over the line numbers, unless we are stripping
4572 them. We do this on a symbol by symbol basis in
4573 order to more easily handle garbage collection. */
4574 if (CSECT_SYM_P (isymp
->n_sclass
)
4576 && isymp
->n_numaux
> 1
4577 && ISFCN (isymp
->n_type
)
4578 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4580 if (*lineno_counts
== 0)
4581 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4584 asection
*enclosing
;
4585 unsigned int enc_count
;
4586 bfd_signed_vma linoff
;
4587 struct internal_lineno lin
;
4594 /* Read in the enclosing section's line-number
4595 information, if we haven't already. */
4597 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4598 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4599 if (oline
!= enclosing
)
4601 pos
= enclosing
->line_filepos
;
4602 amt
= linesz
* enc_count
;
4603 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4604 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
4610 /* Copy across the first entry, adjusting its
4612 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4613 - enclosing
->line_filepos
);
4614 linp
= flinfo
->linenos
+ linoff
;
4615 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4616 lin
.l_addr
.l_symndx
= *indexp
;
4617 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4619 /* Copy the other entries, adjusting their addresses. */
4620 linpend
= linp
+ *lineno_counts
* linesz
;
4621 offset
= (o
->output_section
->vma
4624 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4626 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4627 lin
.l_addr
.l_paddr
+= offset
;
4628 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4631 /* Write out the entries we've just processed. */
4632 pos
= (o
->output_section
->line_filepos
4633 + o
->output_section
->lineno_count
* linesz
);
4634 amt
= linesz
* *lineno_counts
;
4635 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4636 || bfd_bwrite (flinfo
->linenos
+ linoff
,
4637 amt
, output_bfd
) != amt
)
4639 o
->output_section
->lineno_count
+= *lineno_counts
;
4641 /* Record the offset of the symbol's line numbers
4642 in the output file. */
4643 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4647 struct internal_syment
*iisp
, *iispend
;
4650 bfd_vma range_start
, range_end
;
4653 /* Update any C_BINCL or C_EINCL symbols
4654 that refer to a line number in the
4655 range we just output. */
4656 iisp
= flinfo
->internal_syms
;
4657 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4658 iindp
= flinfo
->sym_indices
;
4659 oos
= flinfo
->outsyms
;
4660 range_start
= enclosing
->line_filepos
+ linoff
;
4661 range_end
= range_start
+ *lineno_counts
* linesz
;
4662 while (iisp
< iispend
)
4665 && (iisp
->n_sclass
== C_BINCL
4666 || iisp
->n_sclass
== C_EINCL
)
4667 && iisp
->n_value
>= range_start
4668 && iisp
->n_value
< range_end
)
4670 struct internal_syment iis
;
4672 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4673 iis
.n_value
= (iisp
->n_value
4676 bfd_coff_swap_sym_out (output_bfd
,
4681 iiadd
= 1 + iisp
->n_numaux
;
4683 oos
+= iiadd
* osymesz
;
4691 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4692 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4703 lineno_counts
+= add
;
4707 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4708 symbol will be the first symbol in the next input file. In the
4709 normal case, this will save us from writing out the C_FILE symbol
4711 if (flinfo
->last_file_index
!= -1
4712 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4714 flinfo
->last_file
.n_value
= output_index
;
4715 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
4716 (void *) (flinfo
->outsyms
4717 + ((flinfo
->last_file_index
- syment_base
)
4721 /* Write the modified symbols to the output file. */
4722 if (outsym
> flinfo
->outsyms
)
4724 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4725 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
4726 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4727 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
4730 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4731 + (outsym
- flinfo
->outsyms
) / osymesz
)
4734 obj_raw_syment_count (output_bfd
) = output_index
;
4737 /* Don't let the linker relocation routines discard the symbols. */
4738 keep_syms
= obj_coff_keep_syms (input_bfd
);
4739 obj_coff_keep_syms (input_bfd
) = TRUE
;
4741 /* Relocate the contents of each section. */
4742 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4746 if (! o
->linker_mark
)
4747 /* This section was omitted from the link. */
4750 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4752 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4755 /* We have set filepos correctly for the sections we created to
4756 represent csects, so bfd_get_section_contents should work. */
4757 if (coff_section_data (input_bfd
, o
) != NULL
4758 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4759 contents
= coff_section_data (input_bfd
, o
)->contents
;
4762 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4763 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
4765 contents
= flinfo
->contents
;
4768 if ((o
->flags
& SEC_RELOC
) != 0)
4771 struct internal_reloc
*internal_relocs
;
4772 struct internal_reloc
*irel
;
4774 struct internal_reloc
*irelend
;
4775 struct xcoff_link_hash_entry
**rel_hash
;
4778 /* Read in the relocs. */
4779 target_index
= o
->output_section
->target_index
;
4780 internal_relocs
= (xcoff_read_internal_relocs
4781 (input_bfd
, o
, FALSE
, flinfo
->external_relocs
,
4783 (flinfo
->section_info
[target_index
].relocs
4784 + o
->output_section
->reloc_count
)));
4785 if (internal_relocs
== NULL
)
4788 /* Call processor specific code to relocate the section
4790 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
4794 flinfo
->internal_syms
,
4795 xcoff_data (input_bfd
)->csects
))
4798 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4799 irel
= internal_relocs
;
4800 irelend
= irel
+ o
->reloc_count
;
4801 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
4802 + o
->output_section
->reloc_count
);
4803 for (; irel
< irelend
; irel
++, rel_hash
++)
4805 struct xcoff_link_hash_entry
*h
= NULL
;
4809 /* Adjust the reloc address and symbol index. */
4811 irel
->r_vaddr
+= offset
;
4813 r_symndx
= irel
->r_symndx
;
4818 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4820 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
4823 && h
->smclas
!= XMC_TD
4824 && (irel
->r_type
== R_TOC
4825 || irel
->r_type
== R_GL
4826 || irel
->r_type
== R_TCL
4827 || irel
->r_type
== R_TRL
4828 || irel
->r_type
== R_TRLA
))
4830 /* This is a TOC relative reloc with a symbol
4831 attached. The symbol should be the one which
4832 this reloc is for. We want to make this
4833 reloc against the TOC address of the symbol,
4834 not the symbol itself. */
4835 BFD_ASSERT (h
->toc_section
!= NULL
);
4836 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4837 if (h
->u
.toc_indx
!= -1)
4838 irel
->r_symndx
= h
->u
.toc_indx
;
4841 struct xcoff_toc_rel_hash
*n
;
4842 struct xcoff_link_section_info
*si
;
4846 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
4849 si
= flinfo
->section_info
+ target_index
;
4850 n
->next
= si
->toc_rel_hashes
;
4853 si
->toc_rel_hashes
= n
;
4858 /* This is a global symbol. */
4860 irel
->r_symndx
= h
->indx
;
4863 /* This symbol is being written at the end
4864 of the file, and we do not yet know the
4865 symbol index. We save the pointer to the
4866 hash table entry in the rel_hash list.
4867 We set the indx field to -2 to indicate
4868 that this symbol must not be stripped. */
4877 indx
= flinfo
->sym_indices
[r_symndx
];
4881 struct internal_syment
*is
;
4883 /* Relocations against a TC0 TOC anchor are
4884 automatically transformed to be against
4885 the TOC anchor in the output file. */
4886 is
= flinfo
->internal_syms
+ r_symndx
;
4887 if (is
->n_sclass
== C_HIDEXT
4888 && is
->n_numaux
> 0)
4891 union internal_auxent aux
;
4895 obj_coff_external_syms (input_bfd
))
4896 + ((r_symndx
+ is
->n_numaux
)
4898 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4899 is
->n_type
, is
->n_sclass
,
4903 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4904 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4905 indx
= flinfo
->toc_symindx
;
4910 irel
->r_symndx
= indx
;
4914 struct internal_syment
*is
;
4917 char buf
[SYMNMLEN
+ 1];
4919 /* This reloc is against a symbol we are
4920 stripping. It would be possible to handle
4921 this case, but I don't think it's worth it. */
4922 is
= flinfo
->internal_syms
+ r_symndx
;
4924 if (is
->n_sclass
!= C_DWARF
)
4926 name
= (_bfd_coff_internal_syment_name
4927 (input_bfd
, is
, buf
));
4932 if (!(*flinfo
->info
->callbacks
->unattached_reloc
)
4933 (flinfo
->info
, name
, input_bfd
, o
,
4941 if ((o
->flags
& SEC_DEBUGGING
) == 0
4942 && xcoff_need_ldrel_p (flinfo
->info
, irel
, h
))
4949 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4951 sec
= xcoff_symbol_section (h
);
4952 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
4953 o
->output_section
, input_bfd
,
4959 o
->output_section
->reloc_count
+= o
->reloc_count
;
4962 /* Write out the modified section contents. */
4963 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4964 contents
, (file_ptr
) o
->output_offset
,
4969 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4971 if (! flinfo
->info
->keep_memory
)
4973 if (! _bfd_coff_free_symbols (input_bfd
))
4983 /* Sort relocs by VMA. This is called via qsort. */
4986 xcoff_sort_relocs (const void * p1
, const void * p2
)
4988 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
4989 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
4991 if (r1
->r_vaddr
> r2
->r_vaddr
)
4993 else if (r1
->r_vaddr
< r2
->r_vaddr
)
4999 /* Return true if section SEC is a TOC section. */
5001 static inline bfd_boolean
5002 xcoff_toc_section_p (asection
*sec
)
5007 if (name
[0] == '.' && name
[1] == 't')
5011 if (name
[3] == '0' && name
[4] == 0)
5016 if (name
[2] == 'd' && name
[3] == 0)
5022 /* See if the link requires a TOC (it usually does!). If so, find a
5023 good place to put the TOC anchor csect, and write out the associated
5027 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
5029 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5033 struct internal_syment irsym
;
5034 union internal_auxent iraux
;
5038 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5039 index of a csect at the beginning of the TOC. */
5040 toc_start
= ~(bfd_vma
) 0;
5043 for (input_bfd
= flinfo
->info
->input_bfds
;
5045 input_bfd
= input_bfd
->link_next
)
5046 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5047 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5049 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5050 if (toc_start
> start
)
5053 section_index
= sec
->output_section
->target_index
;
5056 end
= start
+ sec
->size
;
5061 /* There's no need for a TC0 symbol if we don't have a TOC. */
5062 if (toc_end
< toc_start
)
5064 xcoff_data (output_bfd
)->toc
= toc_start
;
5068 if (toc_end
- toc_start
< 0x8000)
5069 /* Every TOC csect can be accessed from TOC_START. */
5070 best_address
= toc_start
;
5073 /* Find the lowest TOC csect that is still within range of TOC_END. */
5074 best_address
= toc_end
;
5075 for (input_bfd
= flinfo
->info
->input_bfds
;
5077 input_bfd
= input_bfd
->link_next
)
5078 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5079 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5081 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5082 if (start
< best_address
5083 && start
+ 0x8000 >= toc_end
)
5085 best_address
= start
;
5086 section_index
= sec
->output_section
->target_index
;
5090 /* Make sure that the start of the TOC is also within range. */
5091 if (best_address
> toc_start
+ 0x8000)
5093 (*_bfd_error_handler
)
5094 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5096 (unsigned long) (toc_end
- toc_start
));
5097 bfd_set_error (bfd_error_file_too_big
);
5102 /* Record the chosen TOC value. */
5103 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5104 xcoff_data (output_bfd
)->toc
= best_address
;
5105 xcoff_data (output_bfd
)->sntoc
= section_index
;
5107 /* Fill out the TC0 symbol. */
5108 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
, &irsym
, "TOC"))
5110 irsym
.n_value
= best_address
;
5111 irsym
.n_scnum
= section_index
;
5112 irsym
.n_sclass
= C_HIDEXT
;
5113 irsym
.n_type
= T_NULL
;
5115 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
5117 /* Fill out the auxillary csect information. */
5118 memset (&iraux
, 0, sizeof iraux
);
5119 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5120 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5121 iraux
.x_csect
.x_scnlen
.l
= 0;
5122 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5123 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5125 /* Write the contents to the file. */
5126 pos
= obj_sym_filepos (output_bfd
);
5127 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5128 size
= 2 * bfd_coff_symesz (output_bfd
);
5129 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5130 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
5132 obj_raw_syment_count (output_bfd
) += 2;
5137 /* Write out a non-XCOFF global symbol. */
5140 xcoff_write_global_symbol (struct bfd_hash_entry
*bh
, void * inf
)
5142 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) bh
;
5143 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
5146 struct internal_syment isym
;
5147 union internal_auxent aux
;
5152 output_bfd
= flinfo
->output_bfd
;
5153 outsym
= flinfo
->outsyms
;
5155 if (h
->root
.type
== bfd_link_hash_warning
)
5157 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5158 if (h
->root
.type
== bfd_link_hash_new
)
5162 /* If this symbol was garbage collected, just skip it. */
5163 if (xcoff_hash_table (flinfo
->info
)->gc
5164 && (h
->flags
& XCOFF_MARK
) == 0)
5167 /* If we need a .loader section entry, write it out. */
5168 if (h
->ldsym
!= NULL
)
5170 struct internal_ldsym
*ldsym
;
5175 if (h
->root
.type
== bfd_link_hash_undefined
5176 || h
->root
.type
== bfd_link_hash_undefweak
)
5180 ldsym
->l_scnum
= N_UNDEF
;
5181 ldsym
->l_smtype
= XTY_ER
;
5182 impbfd
= h
->root
.u
.undef
.abfd
;
5185 else if (h
->root
.type
== bfd_link_hash_defined
5186 || h
->root
.type
== bfd_link_hash_defweak
)
5190 sec
= h
->root
.u
.def
.section
;
5191 ldsym
->l_value
= (sec
->output_section
->vma
5192 + sec
->output_offset
5193 + h
->root
.u
.def
.value
);
5194 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5195 ldsym
->l_smtype
= XTY_SD
;
5196 impbfd
= sec
->owner
;
5202 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5203 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5204 || (h
->flags
& XCOFF_IMPORT
) != 0)
5206 Import symbols are defined so the check above will make
5207 the l_smtype XTY_SD. But this is not correct, it should
5209 ldsym
->l_smtype
|= L_IMPORT
;
5211 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5212 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5213 || (h
->flags
& XCOFF_EXPORT
) != 0)
5214 ldsym
->l_smtype
|= L_EXPORT
;
5216 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5217 ldsym
->l_smtype
|= L_ENTRY
;
5219 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5220 ldsym
->l_smtype
= XTY_SD
;
5222 ldsym
->l_smclas
= h
->smclas
;
5224 if (ldsym
->l_smtype
& L_IMPORT
)
5226 if ((h
->root
.type
== bfd_link_hash_defined
5227 || h
->root
.type
== bfd_link_hash_defweak
)
5228 && (h
->root
.u
.def
.value
!= 0))
5229 ldsym
->l_smclas
= XMC_XO
;
5231 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5232 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5233 ldsym
->l_smclas
= XMC_SV3264
;
5235 else if (h
->flags
& XCOFF_SYSCALL32
)
5236 ldsym
->l_smclas
= XMC_SV
;
5238 else if (h
->flags
& XCOFF_SYSCALL64
)
5239 ldsym
->l_smclas
= XMC_SV64
;
5242 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5246 else if (ldsym
->l_ifile
== 0)
5248 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5250 else if (impbfd
== NULL
)
5254 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5255 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5261 BFD_ASSERT (h
->ldindx
>= 0);
5263 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5266 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
5270 /* If this symbol needs global linkage code, write it out. */
5271 if (h
->root
.type
== bfd_link_hash_defined
5272 && (h
->root
.u
.def
.section
5273 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
5279 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5281 /* The first instruction in the global linkage code loads a
5282 specific TOC element. */
5283 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5284 + h
->descriptor
->toc_section
->output_offset
5285 - xcoff_data (output_bfd
)->toc
);
5287 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5288 tocoff
+= h
->descriptor
->u
.toc_offset
;
5290 /* The first instruction in the glink code needs to be
5291 cooked to to hold the correct offset in the toc. The
5292 rest are just output raw. */
5293 bfd_put_32 (output_bfd
,
5294 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5296 /* Start with i == 1 to get past the first instruction done above
5297 The /4 is because the glink code is in bytes and we are going
5299 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5300 bfd_put_32 (output_bfd
,
5301 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5305 /* If we created a TOC entry for this symbol, write out the required
5307 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5312 struct internal_reloc
*irel
;
5313 struct internal_syment irsym
;
5314 union internal_auxent iraux
;
5316 tocsec
= h
->toc_section
;
5317 osec
= tocsec
->output_section
;
5318 oindx
= osec
->target_index
;
5319 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5320 irel
->r_vaddr
= (osec
->vma
5321 + tocsec
->output_offset
5325 irel
->r_symndx
= h
->indx
;
5329 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5332 BFD_ASSERT (h
->ldindx
>= 0);
5334 /* Initialize the aux union here instead of closer to when it is
5335 written out below because the length of the csect depends on
5336 whether the output is 32 or 64 bit. */
5337 memset (&iraux
, 0, sizeof iraux
);
5338 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5339 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5340 iraux
.x_csect
.x_smclas
= XMC_TC
;
5342 /* 32 bit uses a 32 bit R_POS to do the relocations
5343 64 bit uses a 64 bit R_POS to do the relocations
5345 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5347 Which one is determined by the backend. */
5348 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5351 iraux
.x_csect
.x_scnlen
.l
= 8;
5353 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5356 iraux
.x_csect
.x_scnlen
.l
= 4;
5361 irel
->r_type
= R_POS
;
5362 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5363 ++osec
->reloc_count
;
5365 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5366 output_bfd
, irel
, NULL
, h
))
5369 /* We need to emit a symbol to define a csect which holds
5371 if (flinfo
->info
->strip
!= strip_all
)
5373 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
,
5374 &irsym
, h
->root
.root
.string
);
5378 irsym
.n_value
= irel
->r_vaddr
;
5379 irsym
.n_scnum
= osec
->target_index
;
5380 irsym
.n_sclass
= C_HIDEXT
;
5381 irsym
.n_type
= T_NULL
;
5384 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5385 outsym
+= bfd_coff_symesz (output_bfd
);
5387 /* Note : iraux is initialized above. */
5388 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5389 0, 1, (void *) outsym
);
5390 outsym
+= bfd_coff_auxesz (output_bfd
);
5394 /* We aren't going to write out the symbols below, so we
5395 need to write them out now. */
5396 pos
= obj_sym_filepos (output_bfd
);
5397 pos
+= (obj_raw_syment_count (output_bfd
)
5398 * bfd_coff_symesz (output_bfd
));
5399 amt
= outsym
- flinfo
->outsyms
;
5400 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5401 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5403 obj_raw_syment_count (output_bfd
) +=
5404 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5406 outsym
= flinfo
->outsyms
;
5411 /* If this symbol is a specially defined function descriptor, write
5412 it out. The first word is the address of the function code
5413 itself, the second word is the address of the TOC, and the third
5417 The addresses for the 32 bit will take 4 bytes and the addresses
5418 for 64 bit will take 8 bytes. Similar for the relocs. This type
5419 of logic was also done above to create a TOC entry in
5420 xcoff_write_global_symbol. */
5421 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5422 && h
->root
.type
== bfd_link_hash_defined
5423 && (h
->root
.u
.def
.section
5424 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
5430 struct xcoff_link_hash_entry
*hentry
;
5432 struct internal_reloc
*irel
;
5434 unsigned int reloc_size
, byte_size
;
5436 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5441 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5449 sec
= h
->root
.u
.def
.section
;
5450 osec
= sec
->output_section
;
5451 oindx
= osec
->target_index
;
5452 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5454 hentry
= h
->descriptor
;
5455 BFD_ASSERT (hentry
!= NULL
5456 && (hentry
->root
.type
== bfd_link_hash_defined
5457 || hentry
->root
.type
== bfd_link_hash_defweak
));
5458 esec
= hentry
->root
.u
.def
.section
;
5460 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5461 irel
->r_vaddr
= (osec
->vma
5462 + sec
->output_offset
5463 + h
->root
.u
.def
.value
);
5464 irel
->r_symndx
= esec
->output_section
->target_index
;
5465 irel
->r_type
= R_POS
;
5466 irel
->r_size
= reloc_size
;
5467 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5468 ++osec
->reloc_count
;
5470 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5471 output_bfd
, irel
, esec
, NULL
))
5474 /* There are three items to write out,
5475 the address of the code
5476 the address of the toc anchor
5477 the environment pointer.
5478 We are ignoring the environment pointer. So set it to zero. */
5479 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5481 bfd_put_64 (output_bfd
,
5482 (esec
->output_section
->vma
+ esec
->output_offset
5483 + hentry
->root
.u
.def
.value
),
5485 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5486 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5491 This logic was already called above so the error case where
5492 the backend is neither has already been checked. */
5493 bfd_put_32 (output_bfd
,
5494 (esec
->output_section
->vma
+ esec
->output_offset
5495 + hentry
->root
.u
.def
.value
),
5497 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5498 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5501 tsec
= coff_section_from_bfd_index (output_bfd
,
5502 xcoff_data (output_bfd
)->sntoc
);
5505 irel
->r_vaddr
= (osec
->vma
5506 + sec
->output_offset
5507 + h
->root
.u
.def
.value
5509 irel
->r_symndx
= tsec
->output_section
->target_index
;
5510 irel
->r_type
= R_POS
;
5511 irel
->r_size
= reloc_size
;
5512 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5513 ++osec
->reloc_count
;
5515 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5516 output_bfd
, irel
, tsec
, NULL
))
5520 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
5522 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5527 && (flinfo
->info
->strip
== strip_all
5528 || (flinfo
->info
->strip
== strip_some
5529 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
5530 FALSE
, FALSE
) == NULL
)))
5532 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5537 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5539 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5543 memset (&aux
, 0, sizeof aux
);
5545 h
->indx
= obj_raw_syment_count (output_bfd
);
5547 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
, &isym
,
5548 h
->root
.root
.string
);
5552 if (h
->root
.type
== bfd_link_hash_undefined
5553 || h
->root
.type
== bfd_link_hash_undefweak
)
5556 isym
.n_scnum
= N_UNDEF
;
5557 if (h
->root
.type
== bfd_link_hash_undefweak
5558 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5559 isym
.n_sclass
= C_WEAKEXT
;
5561 isym
.n_sclass
= C_EXT
;
5562 aux
.x_csect
.x_smtyp
= XTY_ER
;
5564 else if ((h
->root
.type
== bfd_link_hash_defined
5565 || h
->root
.type
== bfd_link_hash_defweak
)
5566 && h
->smclas
== XMC_XO
)
5568 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5569 isym
.n_value
= h
->root
.u
.def
.value
;
5570 isym
.n_scnum
= N_UNDEF
;
5571 if (h
->root
.type
== bfd_link_hash_undefweak
5572 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5573 isym
.n_sclass
= C_WEAKEXT
;
5575 isym
.n_sclass
= C_EXT
;
5576 aux
.x_csect
.x_smtyp
= XTY_ER
;
5578 else if (h
->root
.type
== bfd_link_hash_defined
5579 || h
->root
.type
== bfd_link_hash_defweak
)
5581 struct xcoff_link_size_list
*l
;
5583 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5584 + h
->root
.u
.def
.section
->output_offset
5585 + h
->root
.u
.def
.value
);
5586 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5587 isym
.n_scnum
= N_ABS
;
5589 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5590 isym
.n_sclass
= C_HIDEXT
;
5591 aux
.x_csect
.x_smtyp
= XTY_SD
;
5593 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5595 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
5601 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5607 else if (h
->root
.type
== bfd_link_hash_common
)
5609 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5610 + h
->root
.u
.c
.p
->section
->output_offset
);
5611 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5612 isym
.n_sclass
= C_EXT
;
5613 aux
.x_csect
.x_smtyp
= XTY_CM
;
5614 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5619 isym
.n_type
= T_NULL
;
5622 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5623 outsym
+= bfd_coff_symesz (output_bfd
);
5625 aux
.x_csect
.x_smclas
= h
->smclas
;
5626 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5628 outsym
+= bfd_coff_auxesz (output_bfd
);
5630 if ((h
->root
.type
== bfd_link_hash_defined
5631 || h
->root
.type
== bfd_link_hash_defweak
)
5632 && h
->smclas
!= XMC_XO
)
5634 /* We just output an SD symbol. Now output an LD symbol. */
5637 if (h
->root
.type
== bfd_link_hash_undefweak
5638 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5639 isym
.n_sclass
= C_WEAKEXT
;
5641 isym
.n_sclass
= C_EXT
;
5642 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5643 outsym
+= bfd_coff_symesz (output_bfd
);
5645 aux
.x_csect
.x_smtyp
= XTY_LD
;
5646 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5647 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5649 outsym
+= bfd_coff_auxesz (output_bfd
);
5652 pos
= obj_sym_filepos (output_bfd
);
5653 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5654 amt
= outsym
- flinfo
->outsyms
;
5655 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5656 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5658 obj_raw_syment_count (output_bfd
) +=
5659 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5664 /* Handle a link order which is supposed to generate a reloc. */
5667 xcoff_reloc_link_order (bfd
*output_bfd
,
5668 struct xcoff_final_link_info
*flinfo
,
5669 asection
*output_section
,
5670 struct bfd_link_order
*link_order
)
5672 reloc_howto_type
*howto
;
5673 struct xcoff_link_hash_entry
*h
;
5677 struct internal_reloc
*irel
;
5678 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5680 if (link_order
->type
== bfd_section_reloc_link_order
)
5681 /* We need to somehow locate a symbol in the right section. The
5682 symbol must either have a value of zero, or we must adjust
5683 the addend by the value of the symbol. FIXME: Write this
5684 when we need it. The old linker couldn't handle this anyhow. */
5687 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5690 bfd_set_error (bfd_error_bad_value
);
5694 h
= ((struct xcoff_link_hash_entry
*)
5695 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
5696 link_order
->u
.reloc
.p
->u
.name
,
5697 FALSE
, FALSE
, TRUE
));
5700 if (! ((*flinfo
->info
->callbacks
->unattached_reloc
)
5701 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0)))
5706 hsec
= xcoff_symbol_section (h
);
5707 if (h
->root
.type
== bfd_link_hash_defined
5708 || h
->root
.type
== bfd_link_hash_defweak
)
5709 hval
= h
->root
.u
.def
.value
;
5713 addend
= link_order
->u
.reloc
.p
->addend
;
5715 addend
+= (hsec
->output_section
->vma
5716 + hsec
->output_offset
5723 bfd_reloc_status_type rstat
;
5726 size
= bfd_get_reloc_size (howto
);
5727 buf
= bfd_zmalloc (size
);
5731 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5737 case bfd_reloc_outofrange
:
5739 case bfd_reloc_overflow
:
5740 if (! ((*flinfo
->info
->callbacks
->reloc_overflow
)
5741 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5742 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0)))
5749 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5750 (file_ptr
) link_order
->offset
, size
);
5756 /* Store the reloc information in the right place. It will get
5757 swapped and written out at the end of the final_link routine. */
5758 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
5759 + output_section
->reloc_count
);
5760 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
5761 + output_section
->reloc_count
);
5763 memset (irel
, 0, sizeof (struct internal_reloc
));
5764 *rel_hash_ptr
= NULL
;
5766 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5769 irel
->r_symndx
= h
->indx
;
5772 /* Set the index to -2 to force this symbol to get written out. */
5778 irel
->r_type
= howto
->type
;
5779 irel
->r_size
= howto
->bitsize
- 1;
5780 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5781 irel
->r_size
|= 0x80;
5783 ++output_section
->reloc_count
;
5785 /* Now output the reloc to the .loader section. */
5786 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
5788 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
5789 output_bfd
, irel
, hsec
, h
))
5796 /* Do the final link step. */
5799 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5801 bfd_size_type symesz
;
5802 struct xcoff_final_link_info flinfo
;
5804 struct bfd_link_order
*p
;
5805 bfd_size_type max_contents_size
;
5806 bfd_size_type max_sym_count
;
5807 bfd_size_type max_lineno_count
;
5808 bfd_size_type max_reloc_count
;
5809 bfd_size_type max_output_reloc_count
;
5810 file_ptr rel_filepos
;
5812 file_ptr line_filepos
;
5813 unsigned int linesz
;
5815 bfd_byte
*external_relocs
= NULL
;
5816 char strbuf
[STRING_SIZE_SIZE
];
5821 abfd
->flags
|= DYNAMIC
;
5823 symesz
= bfd_coff_symesz (abfd
);
5826 flinfo
.output_bfd
= abfd
;
5827 flinfo
.strtab
= NULL
;
5828 flinfo
.section_info
= NULL
;
5829 flinfo
.last_file_index
= -1;
5830 flinfo
.toc_symindx
= -1;
5831 flinfo
.internal_syms
= NULL
;
5832 flinfo
.sym_indices
= NULL
;
5833 flinfo
.outsyms
= NULL
;
5834 flinfo
.linenos
= NULL
;
5835 flinfo
.contents
= NULL
;
5836 flinfo
.external_relocs
= NULL
;
5838 if (xcoff_hash_table (info
)->loader_section
)
5840 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5841 + bfd_xcoff_ldhdrsz (abfd
));
5842 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5843 + bfd_xcoff_ldhdrsz (abfd
)
5844 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5845 * bfd_xcoff_ldsymsz (abfd
)));
5849 flinfo
.ldsym
= NULL
;
5850 flinfo
.ldrel
= NULL
;
5853 xcoff_data (abfd
)->coff
.link_info
= info
;
5855 flinfo
.strtab
= _bfd_stringtab_init ();
5856 if (flinfo
.strtab
== NULL
)
5859 /* Count the relocation entries required for the output file.
5860 (We've already counted the line numbers.) Determine a few
5862 max_contents_size
= 0;
5863 max_lineno_count
= 0;
5864 max_reloc_count
= 0;
5865 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5868 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5870 if (p
->type
== bfd_indirect_link_order
)
5874 sec
= p
->u
.indirect
.section
;
5876 /* Mark all sections which are to be included in the
5877 link. This will normally be every section. We need
5878 to do this so that we can identify any sections which
5879 the linker has decided to not include. */
5880 sec
->linker_mark
= TRUE
;
5882 o
->reloc_count
+= sec
->reloc_count
;
5884 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
5886 if (sec
->rawsize
> max_contents_size
)
5887 max_contents_size
= sec
->rawsize
;
5888 if (sec
->size
> max_contents_size
)
5889 max_contents_size
= sec
->size
;
5891 if (coff_section_data (sec
->owner
, sec
) != NULL
5892 && xcoff_section_data (sec
->owner
, sec
) != NULL
5893 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5894 > max_lineno_count
))
5896 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5897 if (sec
->reloc_count
> max_reloc_count
)
5898 max_reloc_count
= sec
->reloc_count
;
5900 else if (p
->type
== bfd_section_reloc_link_order
5901 || p
->type
== bfd_symbol_reloc_link_order
)
5906 /* Compute the file positions for all the sections. */
5907 if (abfd
->output_has_begun
)
5909 if (xcoff_hash_table (info
)->file_align
!= 0)
5916 file_align
= xcoff_hash_table (info
)->file_align
;
5917 if (file_align
!= 0)
5919 bfd_boolean saw_contents
;
5923 /* Insert .pad sections before every section which has
5924 contents and is loaded, if it is preceded by some other
5925 section which has contents and is loaded. */
5926 saw_contents
= TRUE
;
5927 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5929 if (strcmp (o
->name
, ".pad") == 0)
5930 saw_contents
= FALSE
;
5931 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5932 && (o
->flags
& SEC_LOAD
) != 0)
5935 saw_contents
= TRUE
;
5940 /* Create a pad section and place it before the section
5941 that needs padding. This requires unlinking and
5942 relinking the bfd's section list. */
5944 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
5946 n
->alignment_power
= 0;
5948 bfd_section_list_remove (abfd
, n
);
5949 bfd_section_list_insert_before (abfd
, o
, n
);
5950 saw_contents
= FALSE
;
5955 /* Reset the section indices after inserting the new
5958 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5961 o
->target_index
= indx
;
5963 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
5965 /* Work out appropriate sizes for the .pad sections to force
5966 each section to land on a page boundary. This bit of
5967 code knows what compute_section_file_positions is going
5969 sofar
= bfd_coff_filhsz (abfd
);
5970 sofar
+= bfd_coff_aoutsz (abfd
);
5971 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
5972 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5973 if ((bfd_xcoff_is_reloc_count_overflow
5974 (abfd
, (bfd_vma
) o
->reloc_count
))
5975 || (bfd_xcoff_is_lineno_count_overflow
5976 (abfd
, (bfd_vma
) o
->lineno_count
)))
5977 /* 64 does not overflow, need to check if 32 does */
5978 sofar
+= bfd_coff_scnhsz (abfd
);
5980 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5982 if (strcmp (o
->name
, ".pad") == 0)
5986 BFD_ASSERT (o
->size
== 0);
5987 pageoff
= sofar
& (file_align
- 1);
5990 o
->size
= file_align
- pageoff
;
5991 sofar
+= file_align
- pageoff
;
5992 o
->flags
|= SEC_HAS_CONTENTS
;
5997 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
5998 sofar
+= BFD_ALIGN (o
->size
,
5999 1 << o
->alignment_power
);
6004 if (! bfd_coff_compute_section_file_positions (abfd
))
6008 /* Allocate space for the pointers we need to keep for the relocs. */
6012 /* We use section_count + 1, rather than section_count, because
6013 the target_index fields are 1 based. */
6014 amt
= abfd
->section_count
+ 1;
6015 amt
*= sizeof (struct xcoff_link_section_info
);
6016 flinfo
.section_info
= bfd_malloc (amt
);
6017 if (flinfo
.section_info
== NULL
)
6019 for (i
= 0; i
<= abfd
->section_count
; i
++)
6021 flinfo
.section_info
[i
].relocs
= NULL
;
6022 flinfo
.section_info
[i
].rel_hashes
= NULL
;
6023 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6027 /* Set the file positions for the relocs. */
6028 rel_filepos
= obj_relocbase (abfd
);
6029 relsz
= bfd_coff_relsz (abfd
);
6030 max_output_reloc_count
= 0;
6031 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6033 if (o
->reloc_count
== 0)
6037 /* A stripped file has no relocs. However, we still
6038 allocate the buffers, so that later code doesn't have to
6039 worry about whether we are stripping or not. */
6040 if (info
->strip
== strip_all
)
6044 o
->flags
|= SEC_RELOC
;
6045 o
->rel_filepos
= rel_filepos
;
6046 rel_filepos
+= o
->reloc_count
* relsz
;
6049 /* We don't know the indices of global symbols until we have
6050 written out all the local symbols. For each section in
6051 the output file, we keep an array of pointers to hash
6052 table entries. Each entry in the array corresponds to a
6053 reloc. When we find a reloc against a global symbol, we
6054 set the corresponding entry in this array so that we can
6055 fix up the symbol index after we have written out all the
6058 Because of this problem, we also keep the relocs in
6059 memory until the end of the link. This wastes memory.
6060 We could backpatch the file later, I suppose, although it
6062 amt
= o
->reloc_count
;
6063 amt
*= sizeof (struct internal_reloc
);
6064 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6066 amt
= o
->reloc_count
;
6067 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6068 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6070 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
6071 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6074 if (o
->reloc_count
> max_output_reloc_count
)
6075 max_output_reloc_count
= o
->reloc_count
;
6079 /* We now know the size of the relocs, so we can determine the file
6080 positions of the line numbers. */
6081 line_filepos
= rel_filepos
;
6082 flinfo
.line_filepos
= line_filepos
;
6083 linesz
= bfd_coff_linesz (abfd
);
6084 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6086 if (o
->lineno_count
== 0)
6087 o
->line_filepos
= 0;
6090 o
->line_filepos
= line_filepos
;
6091 line_filepos
+= o
->lineno_count
* linesz
;
6094 /* Reset the reloc and lineno counts, so that we can use them to
6095 count the number of entries we have output so far. */
6097 o
->lineno_count
= 0;
6100 obj_sym_filepos (abfd
) = line_filepos
;
6102 /* Figure out the largest number of symbols in an input BFD. Take
6103 the opportunity to clear the output_has_begun fields of all the
6104 input BFD's. We want at least 6 symbols, since that is the
6105 number which xcoff_write_global_symbol may need. */
6107 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6111 sub
->output_has_begun
= FALSE
;
6112 sz
= obj_raw_syment_count (sub
);
6113 if (sz
> max_sym_count
)
6117 /* Allocate some buffers used while linking. */
6118 amt
= max_sym_count
* sizeof (struct internal_syment
);
6119 flinfo
.internal_syms
= bfd_malloc (amt
);
6121 amt
= max_sym_count
* sizeof (long);
6122 flinfo
.sym_indices
= bfd_malloc (amt
);
6124 amt
= (max_sym_count
+ 1) * symesz
;
6125 flinfo
.outsyms
= bfd_malloc (amt
);
6127 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6128 flinfo
.linenos
= bfd_malloc (amt
);
6130 amt
= max_contents_size
;
6131 flinfo
.contents
= bfd_malloc (amt
);
6133 amt
= max_reloc_count
* relsz
;
6134 flinfo
.external_relocs
= bfd_malloc (amt
);
6136 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6137 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6138 || flinfo
.outsyms
== NULL
6139 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
6140 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
6141 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6144 obj_raw_syment_count (abfd
) = 0;
6146 /* Find a TOC symbol, if we need one. */
6147 if (!xcoff_find_tc0 (abfd
, &flinfo
))
6150 /* We now know the position of everything in the file, except that
6151 we don't know the size of the symbol table and therefore we don't
6152 know where the string table starts. We just build the string
6153 table in memory as we go along. We process all the relocations
6154 for a single input file at once. */
6155 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6157 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6159 if (p
->type
== bfd_indirect_link_order
6160 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6162 sub
= p
->u
.indirect
.section
->owner
;
6163 if (! sub
->output_has_begun
)
6165 if (! xcoff_link_input_bfd (&flinfo
, sub
))
6167 sub
->output_has_begun
= TRUE
;
6170 else if (p
->type
== bfd_section_reloc_link_order
6171 || p
->type
== bfd_symbol_reloc_link_order
)
6173 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
6178 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6184 /* Free up the buffers used by xcoff_link_input_bfd. */
6185 if (flinfo
.internal_syms
!= NULL
)
6187 free (flinfo
.internal_syms
);
6188 flinfo
.internal_syms
= NULL
;
6190 if (flinfo
.sym_indices
!= NULL
)
6192 free (flinfo
.sym_indices
);
6193 flinfo
.sym_indices
= NULL
;
6195 if (flinfo
.linenos
!= NULL
)
6197 free (flinfo
.linenos
);
6198 flinfo
.linenos
= NULL
;
6200 if (flinfo
.contents
!= NULL
)
6202 free (flinfo
.contents
);
6203 flinfo
.contents
= NULL
;
6205 if (flinfo
.external_relocs
!= NULL
)
6207 free (flinfo
.external_relocs
);
6208 flinfo
.external_relocs
= NULL
;
6211 /* The value of the last C_FILE symbol is supposed to be -1. Write
6213 if (flinfo
.last_file_index
!= -1)
6215 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
6216 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
6217 (void *) flinfo
.outsyms
);
6218 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
6219 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6220 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
6224 /* Write out all the global symbols which do not come from XCOFF
6226 bfd_hash_traverse (&info
->hash
->table
, xcoff_write_global_symbol
, &flinfo
);
6228 if (flinfo
.outsyms
!= NULL
)
6230 free (flinfo
.outsyms
);
6231 flinfo
.outsyms
= NULL
;
6234 /* Now that we have written out all the global symbols, we know the
6235 symbol indices to use for relocs against them, and we can finally
6236 write out the relocs. */
6237 amt
= max_output_reloc_count
* relsz
;
6238 external_relocs
= bfd_malloc (amt
);
6239 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6242 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6244 struct internal_reloc
*irel
;
6245 struct internal_reloc
*irelend
;
6246 struct xcoff_link_hash_entry
**rel_hash
;
6247 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6249 bfd_size_type rel_size
;
6251 /* A stripped file has no relocs. */
6252 if (info
->strip
== strip_all
)
6258 if (o
->reloc_count
== 0)
6261 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6262 irelend
= irel
+ o
->reloc_count
;
6263 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
6264 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6266 if (*rel_hash
!= NULL
)
6268 if ((*rel_hash
)->indx
< 0)
6270 if (! ((*info
->callbacks
->unattached_reloc
)
6271 (info
, (*rel_hash
)->root
.root
.string
,
6272 NULL
, o
, irel
->r_vaddr
)))
6274 (*rel_hash
)->indx
= 0;
6276 irel
->r_symndx
= (*rel_hash
)->indx
;
6280 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6281 toc_rel_hash
!= NULL
;
6282 toc_rel_hash
= toc_rel_hash
->next
)
6284 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6286 if (! ((*info
->callbacks
->unattached_reloc
)
6287 (info
, toc_rel_hash
->h
->root
.root
.string
,
6288 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
6290 toc_rel_hash
->h
->u
.toc_indx
= 0;
6292 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6295 /* XCOFF requires that the relocs be sorted by address. We tend
6296 to produce them in the order in which their containing csects
6297 appear in the symbol table, which is not necessarily by
6298 address. So we sort them here. There may be a better way to
6300 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
6301 o
->reloc_count
, sizeof (struct internal_reloc
),
6304 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6305 irelend
= irel
+ o
->reloc_count
;
6306 erel
= external_relocs
;
6307 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6308 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6310 rel_size
= relsz
* o
->reloc_count
;
6311 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6312 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6316 if (external_relocs
!= NULL
)
6318 free (external_relocs
);
6319 external_relocs
= NULL
;
6322 /* Free up the section information. */
6323 if (flinfo
.section_info
!= NULL
)
6327 for (i
= 0; i
< abfd
->section_count
; i
++)
6329 if (flinfo
.section_info
[i
].relocs
!= NULL
)
6330 free (flinfo
.section_info
[i
].relocs
);
6331 if (flinfo
.section_info
[i
].rel_hashes
!= NULL
)
6332 free (flinfo
.section_info
[i
].rel_hashes
);
6334 free (flinfo
.section_info
);
6335 flinfo
.section_info
= NULL
;
6338 /* Write out the loader section contents. */
6339 o
= xcoff_hash_table (info
)->loader_section
;
6342 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
6343 == (xcoff_hash_table (info
)->loader_section
->contents
6344 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6345 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6346 (file_ptr
) o
->output_offset
, o
->size
))
6350 /* Write out the magic sections. */
6351 o
= xcoff_hash_table (info
)->linkage_section
;
6353 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6354 (file_ptr
) o
->output_offset
,
6357 o
= xcoff_hash_table (info
)->toc_section
;
6359 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6360 (file_ptr
) o
->output_offset
,
6363 o
= xcoff_hash_table (info
)->descriptor_section
;
6365 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6366 (file_ptr
) o
->output_offset
,
6370 /* Write out the string table. */
6371 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6372 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6375 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
6377 amt
= STRING_SIZE_SIZE
;
6378 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6380 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
6383 _bfd_stringtab_free (flinfo
.strtab
);
6385 /* Write out the debugging string table. */
6386 o
= xcoff_hash_table (info
)->debug_section
;
6389 struct bfd_strtab_hash
*debug_strtab
;
6391 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6392 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6393 >= _bfd_stringtab_size (debug_strtab
));
6394 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6395 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6397 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6401 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6402 not try to write out the symbols. */
6403 bfd_get_symcount (abfd
) = 0;
6408 if (flinfo
.strtab
!= NULL
)
6409 _bfd_stringtab_free (flinfo
.strtab
);
6411 if (flinfo
.section_info
!= NULL
)
6415 for (i
= 0; i
< abfd
->section_count
; i
++)
6417 if (flinfo
.section_info
[i
].relocs
!= NULL
)
6418 free (flinfo
.section_info
[i
].relocs
);
6419 if (flinfo
.section_info
[i
].rel_hashes
!= NULL
)
6420 free (flinfo
.section_info
[i
].rel_hashes
);
6422 free (flinfo
.section_info
);
6425 if (flinfo
.internal_syms
!= NULL
)
6426 free (flinfo
.internal_syms
);
6427 if (flinfo
.sym_indices
!= NULL
)
6428 free (flinfo
.sym_indices
);
6429 if (flinfo
.outsyms
!= NULL
)
6430 free (flinfo
.outsyms
);
6431 if (flinfo
.linenos
!= NULL
)
6432 free (flinfo
.linenos
);
6433 if (flinfo
.contents
!= NULL
)
6434 free (flinfo
.contents
);
6435 if (flinfo
.external_relocs
!= NULL
)
6436 free (flinfo
.external_relocs
);
6437 if (external_relocs
!= NULL
)
6438 free (external_relocs
);