1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2014 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "coff/internal.h"
27 #include "coff/xcoff.h"
30 #include "libiberty.h"
32 /* This file holds the XCOFF linker code. */
34 #undef STRING_SIZE_SIZE
35 #define STRING_SIZE_SIZE 4
37 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
38 This flag will only be used on input sections. */
40 #define SEC_MARK (SEC_ROM)
42 /* The list of import files. */
44 struct xcoff_import_file
46 /* The next entry in the list. */
47 struct xcoff_import_file
*next
;
52 /* The member name. */
56 /* Information we keep for each section in the output file during the
59 struct xcoff_link_section_info
61 /* The relocs to be output. */
62 struct internal_reloc
*relocs
;
63 /* For each reloc against a global symbol whose index was not known
64 when the reloc was handled, the global hash table entry. */
65 struct xcoff_link_hash_entry
**rel_hashes
;
66 /* If there is a TOC relative reloc against a global symbol, and the
67 index of the TOC symbol is not known when the reloc was handled,
68 an entry is added to this linked list. This is not an array,
69 like rel_hashes, because this case is quite uncommon. */
70 struct xcoff_toc_rel_hash
72 struct xcoff_toc_rel_hash
*next
;
73 struct xcoff_link_hash_entry
*h
;
74 struct internal_reloc
*rel
;
78 /* Information that the XCOFF linker collects about an archive. */
79 struct xcoff_archive_info
81 /* The archive described by this entry. */
84 /* The import path and import filename to use when referring to
85 this archive in the .loader section. */
89 /* True if the archive contains a dynamic object. */
90 unsigned int contains_shared_object_p
: 1;
92 /* True if the previous field is valid. */
93 unsigned int know_contains_shared_object_p
: 1;
96 struct xcoff_link_hash_table
98 struct bfd_link_hash_table root
;
100 /* The .debug string hash table. We need to compute this while
101 reading the input files, so that we know how large the .debug
102 section will be before we assign section positions. */
103 struct bfd_strtab_hash
*debug_strtab
;
105 /* The .debug section we will use for the final output. */
106 asection
*debug_section
;
108 /* The .loader section we will use for the final output. */
109 asection
*loader_section
;
111 /* A count of non TOC relative relocs which will need to be
112 allocated in the .loader section. */
115 /* The .loader section header. */
116 struct internal_ldhdr ldhdr
;
118 /* The .gl section we use to hold global linkage code. */
119 asection
*linkage_section
;
121 /* The .tc section we use to hold toc entries we build for global
123 asection
*toc_section
;
125 /* The .ds section we use to hold function descriptors which we
126 create for exported symbols. */
127 asection
*descriptor_section
;
129 /* The list of import files. */
130 struct xcoff_import_file
*imports
;
132 /* Required alignment of sections within the output file. */
133 unsigned long file_align
;
135 /* Whether the .text section must be read-only. */
138 /* Whether -brtl was specified. */
141 /* Whether garbage collection was done. */
144 /* A linked list of symbols for which we have size information. */
145 struct xcoff_link_size_list
147 struct xcoff_link_size_list
*next
;
148 struct xcoff_link_hash_entry
*h
;
153 /* Information about archives. */
156 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
157 asection
*special_sections
[XCOFF_NUMBER_OF_SPECIAL_SECTIONS
];
160 /* Information that we pass around while doing the final link step. */
162 struct xcoff_final_link_info
164 /* General link information. */
165 struct bfd_link_info
*info
;
168 /* Hash table for long symbol names. */
169 struct bfd_strtab_hash
*strtab
;
170 /* Array of information kept for each output section, indexed by the
171 target_index field. */
172 struct xcoff_link_section_info
*section_info
;
173 /* Symbol index of last C_FILE symbol (-1 if none). */
174 long last_file_index
;
175 /* Contents of last C_FILE symbol. */
176 struct internal_syment last_file
;
177 /* Symbol index of TOC symbol. */
179 /* Start of .loader symbols. */
181 /* Next .loader reloc to swap out. */
183 /* File position of start of line numbers. */
184 file_ptr line_filepos
;
185 /* Buffer large enough to hold swapped symbols of any input file. */
186 struct internal_syment
*internal_syms
;
187 /* Buffer large enough to hold output indices of symbols of any
190 /* Buffer large enough to hold output symbols for any input file. */
192 /* Buffer large enough to hold external line numbers for any input
195 /* Buffer large enough to hold any input section. */
197 /* Buffer large enough to hold external relocs of any input section. */
198 bfd_byte
*external_relocs
;
201 static bfd_boolean
xcoff_mark (struct bfd_link_info
*, asection
*);
205 /* Routines to read XCOFF dynamic information. This don't really
206 belong here, but we already have the ldsym manipulation routines
209 /* Read the contents of a section. */
212 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
214 if (coff_section_data (abfd
, sec
) == NULL
)
216 bfd_size_type amt
= sizeof (struct coff_section_tdata
);
218 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
219 if (sec
->used_by_bfd
== NULL
)
223 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
227 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
229 if (contents
!= NULL
)
233 coff_section_data (abfd
, sec
)->contents
= contents
;
239 /* Get the size required to hold the dynamic symbols. */
242 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
246 struct internal_ldhdr ldhdr
;
248 if ((abfd
->flags
& DYNAMIC
) == 0)
250 bfd_set_error (bfd_error_invalid_operation
);
254 lsec
= bfd_get_section_by_name (abfd
, ".loader");
257 bfd_set_error (bfd_error_no_symbols
);
261 if (! xcoff_get_section_contents (abfd
, lsec
))
263 contents
= coff_section_data (abfd
, lsec
)->contents
;
265 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
267 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
270 /* Get the dynamic symbols. */
273 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
277 struct internal_ldhdr ldhdr
;
279 bfd_byte
*elsym
, *elsymend
;
280 coff_symbol_type
*symbuf
;
282 if ((abfd
->flags
& DYNAMIC
) == 0)
284 bfd_set_error (bfd_error_invalid_operation
);
288 lsec
= bfd_get_section_by_name (abfd
, ".loader");
291 bfd_set_error (bfd_error_no_symbols
);
295 if (! xcoff_get_section_contents (abfd
, lsec
))
297 contents
= coff_section_data (abfd
, lsec
)->contents
;
299 coff_section_data (abfd
, lsec
)->keep_contents
= TRUE
;
301 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
303 strings
= (char *) contents
+ ldhdr
.l_stoff
;
305 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
309 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
311 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
312 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
314 struct internal_ldsym ldsym
;
316 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
318 symbuf
->symbol
.the_bfd
= abfd
;
320 if (ldsym
._l
._l_l
._l_zeroes
== 0)
321 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
326 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
329 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
331 symbuf
->symbol
.name
= c
;
334 if (ldsym
.l_smclas
== XMC_XO
)
335 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
337 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
339 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
341 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
342 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
344 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
345 symbuf
->symbol
.flags
|= BSF_WEAK
;
347 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
350 /* FIXME: We have no way to record the other information stored
351 with the loader symbol. */
352 *psyms
= (asymbol
*) symbuf
;
357 return ldhdr
.l_nsyms
;
360 /* Get the size required to hold the dynamic relocs. */
363 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
367 struct internal_ldhdr ldhdr
;
369 if ((abfd
->flags
& DYNAMIC
) == 0)
371 bfd_set_error (bfd_error_invalid_operation
);
375 lsec
= bfd_get_section_by_name (abfd
, ".loader");
378 bfd_set_error (bfd_error_no_symbols
);
382 if (! xcoff_get_section_contents (abfd
, lsec
))
384 contents
= coff_section_data (abfd
, lsec
)->contents
;
386 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
388 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
391 /* Get the dynamic relocs. */
394 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
400 struct internal_ldhdr ldhdr
;
402 bfd_byte
*elrel
, *elrelend
;
404 if ((abfd
->flags
& DYNAMIC
) == 0)
406 bfd_set_error (bfd_error_invalid_operation
);
410 lsec
= bfd_get_section_by_name (abfd
, ".loader");
413 bfd_set_error (bfd_error_no_symbols
);
417 if (! xcoff_get_section_contents (abfd
, lsec
))
419 contents
= coff_section_data (abfd
, lsec
)->contents
;
421 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
423 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
427 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
429 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
430 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
433 struct internal_ldrel ldrel
;
435 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
437 if (ldrel
.l_symndx
>= 3)
438 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
444 switch (ldrel
.l_symndx
)
460 sec
= bfd_get_section_by_name (abfd
, name
);
463 bfd_set_error (bfd_error_bad_value
);
467 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
470 relbuf
->address
= ldrel
.l_vaddr
;
473 /* Most dynamic relocs have the same type. FIXME: This is only
474 correct if ldrel.l_rtype == 0. In other cases, we should use
475 a different howto. */
476 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
478 /* FIXME: We have no way to record the l_rsecnm field. */
485 return ldhdr
.l_nreloc
;
488 /* Hash functions for xcoff_link_hash_table's archive_info. */
491 xcoff_archive_info_hash (const void *data
)
493 const struct xcoff_archive_info
*info
;
495 info
= (const struct xcoff_archive_info
*) data
;
496 return htab_hash_pointer (info
->archive
);
500 xcoff_archive_info_eq (const void *data1
, const void *data2
)
502 const struct xcoff_archive_info
*info1
;
503 const struct xcoff_archive_info
*info2
;
505 info1
= (const struct xcoff_archive_info
*) data1
;
506 info2
= (const struct xcoff_archive_info
*) data2
;
507 return info1
->archive
== info2
->archive
;
510 /* Return information about archive ARCHIVE. Return NULL on error. */
512 static struct xcoff_archive_info
*
513 xcoff_get_archive_info (struct bfd_link_info
*info
, bfd
*archive
)
515 struct xcoff_link_hash_table
*htab
;
516 struct xcoff_archive_info
*entryp
, entry
;
519 htab
= xcoff_hash_table (info
);
520 entry
.archive
= archive
;
521 slot
= htab_find_slot (htab
->archive_info
, &entry
, INSERT
);
528 entryp
= bfd_zalloc (archive
, sizeof (entry
));
532 entryp
->archive
= archive
;
538 /* Routine to create an entry in an XCOFF link hash table. */
540 static struct bfd_hash_entry
*
541 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
542 struct bfd_hash_table
*table
,
545 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
547 /* Allocate the structure if it has not already been allocated by a
550 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
554 /* Call the allocation method of the superclass. */
555 ret
= ((struct xcoff_link_hash_entry
*)
556 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
560 /* Set local fields. */
562 ret
->toc_section
= NULL
;
563 ret
->u
.toc_indx
= -1;
564 ret
->descriptor
= NULL
;
568 ret
->smclas
= XMC_UA
;
571 return (struct bfd_hash_entry
*) ret
;
574 /* Create a XCOFF link hash table. */
576 struct bfd_link_hash_table
*
577 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
579 struct xcoff_link_hash_table
*ret
;
580 bfd_size_type amt
= sizeof (* ret
);
582 ret
= bfd_zmalloc (amt
);
585 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
586 sizeof (struct xcoff_link_hash_entry
)))
592 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
593 ret
->archive_info
= htab_create (37, xcoff_archive_info_hash
,
594 xcoff_archive_info_eq
, NULL
);
596 /* The linker will always generate a full a.out header. We need to
597 record that fact now, before the sizeof_headers routine could be
599 xcoff_data (abfd
)->full_aouthdr
= TRUE
;
604 /* Free a XCOFF link hash table. */
607 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table
*hash
)
609 struct xcoff_link_hash_table
*ret
= (struct xcoff_link_hash_table
*) hash
;
611 _bfd_stringtab_free (ret
->debug_strtab
);
612 bfd_hash_table_free (&ret
->root
.table
);
616 /* Read internal relocs for an XCOFF csect. This is a wrapper around
617 _bfd_coff_read_internal_relocs which tries to take advantage of any
618 relocs which may have been cached for the enclosing section. */
620 static struct internal_reloc
*
621 xcoff_read_internal_relocs (bfd
*abfd
,
624 bfd_byte
*external_relocs
,
625 bfd_boolean require_internal
,
626 struct internal_reloc
*internal_relocs
)
628 if (coff_section_data (abfd
, sec
) != NULL
629 && coff_section_data (abfd
, sec
)->relocs
== NULL
630 && xcoff_section_data (abfd
, sec
) != NULL
)
634 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
636 if (enclosing
!= NULL
637 && (coff_section_data (abfd
, enclosing
) == NULL
638 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
640 && enclosing
->reloc_count
> 0)
642 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, TRUE
,
643 external_relocs
, FALSE
, NULL
)
648 if (enclosing
!= NULL
649 && coff_section_data (abfd
, enclosing
) != NULL
650 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
654 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
655 / bfd_coff_relsz (abfd
));
657 if (! require_internal
)
658 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
659 memcpy (internal_relocs
,
660 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
661 sec
->reloc_count
* sizeof (struct internal_reloc
));
662 return internal_relocs
;
666 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
667 require_internal
, internal_relocs
);
670 /* Split FILENAME into an import path and an import filename,
671 storing them in *IMPPATH and *IMPFILE respectively. */
674 bfd_xcoff_split_import_path (bfd
*abfd
, const char *filename
,
675 const char **imppath
, const char **impfile
)
681 base
= lbasename (filename
);
682 length
= base
- filename
;
684 /* The filename has no directory component, so use an empty path. */
686 else if (length
== 1)
687 /* The filename is in the root directory. */
691 /* Extract the (non-empty) directory part. Note that we don't
692 need to strip duplicate directory separators from any part
693 of the string; the native linker doesn't do that either. */
694 path
= bfd_alloc (abfd
, length
);
697 memcpy (path
, filename
, length
- 1);
698 path
[length
- 1] = 0;
705 /* Set ARCHIVE's import path as though its filename had been given
709 bfd_xcoff_set_archive_import_path (struct bfd_link_info
*info
,
710 bfd
*archive
, const char *filename
)
712 struct xcoff_archive_info
*archive_info
;
714 archive_info
= xcoff_get_archive_info (info
, archive
);
715 return (archive_info
!= NULL
716 && bfd_xcoff_split_import_path (archive
, filename
,
717 &archive_info
->imppath
,
718 &archive_info
->impfile
));
721 /* H is an imported symbol. Set the import module's path, file and member
722 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
723 no specific import module is specified. */
726 xcoff_set_import_path (struct bfd_link_info
*info
,
727 struct xcoff_link_hash_entry
*h
,
728 const char *imppath
, const char *impfile
,
729 const char *impmember
)
732 struct xcoff_import_file
**pp
;
734 /* We overload the ldindx field to hold the l_ifile value for this
736 BFD_ASSERT (h
->ldsym
== NULL
);
737 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
742 /* We start c at 1 because the first entry in the import list is
743 reserved for the library search path. */
744 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
746 pp
= &(*pp
)->next
, ++c
)
748 if (filename_cmp ((*pp
)->path
, imppath
) == 0
749 && filename_cmp ((*pp
)->file
, impfile
) == 0
750 && filename_cmp ((*pp
)->member
, impmember
) == 0)
756 struct xcoff_import_file
*n
;
757 bfd_size_type amt
= sizeof (* n
);
759 n
= bfd_alloc (info
->output_bfd
, amt
);
765 n
->member
= impmember
;
773 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
774 Return true if LDSYM defines H. */
777 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry
*h
,
778 struct internal_ldsym
*ldsym
)
780 /* If we didn't know about H before processing LDSYM, LDSYM
781 definitely defines H. */
782 if (h
->root
.type
== bfd_link_hash_new
)
785 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
786 dynamic symbol, LDSYM trumps the current definition of H. */
787 if ((ldsym
->l_smtype
& L_WEAK
) == 0
788 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
789 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
790 && (h
->root
.type
== bfd_link_hash_defweak
791 || h
->root
.type
== bfd_link_hash_undefweak
))
794 /* If H is currently undefined, LDSYM defines it. */
795 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
796 && (h
->root
.type
== bfd_link_hash_undefined
797 || h
->root
.type
== bfd_link_hash_undefweak
))
803 /* This function is used to add symbols from a dynamic object to the
804 global symbol table. */
807 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
811 struct internal_ldhdr ldhdr
;
813 bfd_byte
*elsym
, *elsymend
;
814 struct xcoff_import_file
*n
;
816 struct xcoff_import_file
**pp
;
818 /* We can only handle a dynamic object if we are generating an XCOFF
820 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
822 (*_bfd_error_handler
)
823 (_("%s: XCOFF shared object when not producing XCOFF output"),
824 bfd_get_filename (abfd
));
825 bfd_set_error (bfd_error_invalid_operation
);
829 /* The symbols we use from a dynamic object are not the symbols in
830 the normal symbol table, but, rather, the symbols in the export
831 table. If there is a global symbol in a dynamic object which is
832 not in the export table, the loader will not be able to find it,
833 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
834 libc.a has symbols in the export table which are not in the
837 /* Read in the .loader section. FIXME: We should really use the
838 o_snloader field in the a.out header, rather than grabbing the
840 lsec
= bfd_get_section_by_name (abfd
, ".loader");
843 (*_bfd_error_handler
)
844 (_("%s: dynamic object with no .loader section"),
845 bfd_get_filename (abfd
));
846 bfd_set_error (bfd_error_no_symbols
);
850 if (! xcoff_get_section_contents (abfd
, lsec
))
852 contents
= coff_section_data (abfd
, lsec
)->contents
;
854 /* Remove the sections from this object, so that they do not get
855 included in the link. */
856 bfd_section_list_clear (abfd
);
858 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
860 strings
= (char *) contents
+ ldhdr
.l_stoff
;
862 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
864 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
866 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
868 struct internal_ldsym ldsym
;
869 char nambuf
[SYMNMLEN
+ 1];
871 struct xcoff_link_hash_entry
*h
;
873 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
875 /* We are only interested in exported symbols. */
876 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
879 if (ldsym
._l
._l_l
._l_zeroes
== 0)
880 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
883 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
884 nambuf
[SYMNMLEN
] = '\0';
888 /* Normally we could not call xcoff_link_hash_lookup in an add
889 symbols routine, since we might not be using an XCOFF hash
890 table. However, we verified above that we are using an XCOFF
893 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
,
898 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
901 h
->flags
|= XCOFF_DEF_DYNAMIC
;
902 h
->smclas
= ldsym
.l_smclas
;
903 if (h
->smclas
== XMC_XO
)
905 /* This symbol has an absolute value. */
906 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
907 h
->root
.type
= bfd_link_hash_defweak
;
909 h
->root
.type
= bfd_link_hash_defined
;
910 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
911 h
->root
.u
.def
.value
= ldsym
.l_value
;
915 /* Otherwise, we don't bother to actually define the symbol,
916 since we don't have a section to put it in anyhow.
917 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
918 should be imported from the symbol's undef.abfd. */
919 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
920 h
->root
.type
= bfd_link_hash_undefweak
;
922 h
->root
.type
= bfd_link_hash_undefined
;
923 h
->root
.u
.undef
.abfd
= abfd
;
926 /* If this symbol defines a function descriptor, then it
927 implicitly defines the function code as well. */
928 if (h
->smclas
== XMC_DS
929 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
930 h
->flags
|= XCOFF_DESCRIPTOR
;
931 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
933 struct xcoff_link_hash_entry
*hds
;
940 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
944 strcpy (dsnm
+ 1, name
);
945 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
955 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
957 hds
->root
.type
= h
->root
.type
;
958 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
959 if (h
->smclas
== XMC_XO
)
961 /* An absolute symbol appears to actually define code, not a
962 function descriptor. This is how some math functions are
963 implemented on AIX 4.1. */
964 hds
->smclas
= XMC_XO
;
965 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
966 hds
->root
.u
.def
.value
= ldsym
.l_value
;
970 hds
->smclas
= XMC_PR
;
971 hds
->root
.u
.undef
.abfd
= abfd
;
972 /* We do not want to add this to the undefined
979 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
981 free (coff_section_data (abfd
, lsec
)->contents
);
982 coff_section_data (abfd
, lsec
)->contents
= NULL
;
985 /* Record this file in the import files. */
986 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
991 if (abfd
->my_archive
== NULL
)
993 if (!bfd_xcoff_split_import_path (abfd
, abfd
->filename
,
1000 struct xcoff_archive_info
*archive_info
;
1002 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1003 if (!archive_info
->impfile
)
1005 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1006 archive_info
->archive
->filename
,
1007 &archive_info
->imppath
,
1008 &archive_info
->impfile
))
1011 n
->path
= archive_info
->imppath
;
1012 n
->file
= archive_info
->impfile
;
1013 n
->member
= bfd_get_filename (abfd
);
1016 /* We start c at 1 because the first import file number is reserved
1018 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1020 pp
= &(*pp
)->next
, ++c
)
1024 xcoff_data (abfd
)->import_file_id
= c
;
1029 /* xcoff_link_create_extra_sections
1031 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1034 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1036 bfd_boolean return_value
= FALSE
;
1038 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1040 /* We need to build a .loader section, so we do it here. This
1041 won't work if we're producing an XCOFF output file with no
1042 XCOFF input files. FIXME. */
1044 if (!info
->relocatable
1045 && xcoff_hash_table (info
)->loader_section
== NULL
)
1048 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1050 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1054 xcoff_hash_table (info
)->loader_section
= lsec
;
1057 /* Likewise for the linkage section. */
1058 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1061 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1064 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1068 xcoff_hash_table (info
)->linkage_section
= lsec
;
1069 lsec
->alignment_power
= 2;
1072 /* Likewise for the TOC section. */
1073 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1076 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1079 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1083 xcoff_hash_table (info
)->toc_section
= tsec
;
1084 tsec
->alignment_power
= 2;
1087 /* Likewise for the descriptor section. */
1088 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1091 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1094 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1098 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1099 dsec
->alignment_power
= 2;
1102 /* Likewise for the .debug section. */
1103 if (xcoff_hash_table (info
)->debug_section
== NULL
1104 && info
->strip
!= strip_all
)
1107 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1109 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1113 xcoff_hash_table (info
)->debug_section
= dsec
;
1117 return_value
= TRUE
;
1121 return return_value
;
1124 /* Returns the index of reloc in RELOCS with the least address greater
1125 than or equal to ADDRESS. The relocs are sorted by address. */
1127 static bfd_size_type
1128 xcoff_find_reloc (struct internal_reloc
*relocs
,
1129 bfd_size_type count
,
1132 bfd_size_type min
, max
, this;
1136 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1145 /* Do a binary search over (min,max]. */
1146 while (min
+ 1 < max
)
1150 this = (max
+ min
) / 2;
1151 raddr
= relocs
[this].r_vaddr
;
1152 if (raddr
> address
)
1154 else if (raddr
< address
)
1163 if (relocs
[min
].r_vaddr
< address
)
1167 && relocs
[min
- 1].r_vaddr
== address
)
1173 /* Add all the symbols from an object file to the hash table.
1175 XCOFF is a weird format. A normal XCOFF .o files will have three
1176 COFF sections--.text, .data, and .bss--but each COFF section will
1177 contain many csects. These csects are described in the symbol
1178 table. From the linker's point of view, each csect must be
1179 considered a section in its own right. For example, a TOC entry is
1180 handled as a small XMC_TC csect. The linker must be able to merge
1181 different TOC entries together, which means that it must be able to
1182 extract the XMC_TC csects from the .data section of the input .o
1185 From the point of view of our linker, this is, of course, a hideous
1186 nightmare. We cope by actually creating sections for each csect,
1187 and discarding the original sections. We then have to handle the
1188 relocation entries carefully, since the only way to tell which
1189 csect they belong to is to examine the address. */
1192 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1194 unsigned int n_tmask
;
1195 unsigned int n_btshft
;
1196 bfd_boolean default_copy
;
1197 bfd_size_type symcount
;
1198 struct xcoff_link_hash_entry
**sym_hash
;
1199 asection
**csect_cache
;
1200 unsigned int *lineno_counts
;
1201 bfd_size_type linesz
;
1203 asection
*last_real
;
1204 bfd_boolean keep_syms
;
1206 unsigned int csect_index
;
1207 asection
*first_csect
;
1208 bfd_size_type symesz
;
1211 struct reloc_info_struct
1213 struct internal_reloc
*relocs
;
1216 } *reloc_info
= NULL
;
1219 keep_syms
= obj_coff_keep_syms (abfd
);
1221 if ((abfd
->flags
& DYNAMIC
) != 0
1222 && ! info
->static_link
)
1224 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1228 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1229 if (! xcoff_link_create_extra_sections (abfd
, info
))
1232 if ((abfd
->flags
& DYNAMIC
) != 0
1233 && ! info
->static_link
)
1236 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1237 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1239 /* Define macros so that ISFCN, et. al., macros work correctly. */
1240 #define N_TMASK n_tmask
1241 #define N_BTSHFT n_btshft
1243 if (info
->keep_memory
)
1244 default_copy
= FALSE
;
1246 default_copy
= TRUE
;
1248 symcount
= obj_raw_syment_count (abfd
);
1250 /* We keep a list of the linker hash table entries that correspond
1251 to each external symbol. */
1252 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1253 sym_hash
= bfd_zalloc (abfd
, amt
);
1254 if (sym_hash
== NULL
&& symcount
!= 0)
1256 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1258 /* Because of the weird stuff we are doing with XCOFF csects, we can
1259 not easily determine which section a symbol is in, so we store
1260 the information in the tdata for the input file. */
1261 amt
= symcount
* sizeof (asection
*);
1262 csect_cache
= bfd_zalloc (abfd
, amt
);
1263 if (csect_cache
== NULL
&& symcount
!= 0)
1265 xcoff_data (abfd
)->csects
= csect_cache
;
1267 /* We garbage-collect line-number information on a symbol-by-symbol
1268 basis, so we need to have quick access to the number of entries
1270 amt
= symcount
* sizeof (unsigned int);
1271 lineno_counts
= bfd_zalloc (abfd
, amt
);
1272 if (lineno_counts
== NULL
&& symcount
!= 0)
1274 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1276 /* While splitting sections into csects, we need to assign the
1277 relocs correctly. The relocs and the csects must both be in
1278 order by VMA within a given section, so we handle this by
1279 scanning along the relocs as we process the csects. We index
1280 into reloc_info using the section target_index. */
1281 amt
= abfd
->section_count
+ 1;
1282 amt
*= sizeof (struct reloc_info_struct
);
1283 reloc_info
= bfd_zmalloc (amt
);
1284 if (reloc_info
== NULL
)
1287 /* Read in the relocs and line numbers for each section. */
1288 linesz
= bfd_coff_linesz (abfd
);
1290 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1294 if ((o
->flags
& SEC_RELOC
) != 0)
1296 reloc_info
[o
->target_index
].relocs
=
1297 xcoff_read_internal_relocs (abfd
, o
, TRUE
, NULL
, FALSE
, NULL
);
1298 amt
= o
->reloc_count
;
1299 amt
*= sizeof (asection
*);
1300 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1301 if (reloc_info
[o
->target_index
].csects
== NULL
)
1305 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1306 && o
->lineno_count
> 0)
1310 amt
= linesz
* o
->lineno_count
;
1311 linenos
= bfd_malloc (amt
);
1312 if (linenos
== NULL
)
1314 reloc_info
[o
->target_index
].linenos
= linenos
;
1315 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1316 || bfd_bread (linenos
, amt
, abfd
) != amt
)
1321 /* Don't let the linker relocation routines discard the symbols. */
1322 obj_coff_keep_syms (abfd
) = TRUE
;
1328 symesz
= bfd_coff_symesz (abfd
);
1329 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1330 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1331 esym_end
= esym
+ symcount
* symesz
;
1333 while (esym
< esym_end
)
1335 struct internal_syment sym
;
1336 union internal_auxent aux
;
1338 char buf
[SYMNMLEN
+ 1];
1342 struct xcoff_link_hash_entry
*set_toc
;
1344 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1346 /* In this pass we are only interested in symbols with csect
1348 if (!CSECT_SYM_P (sym
.n_sclass
))
1351 Normally csect is a .pr, .rw etc. created in the loop
1352 If C_FILE or first time, handle special
1354 Advance esym, sym_hash, csect_hash ptrs. */
1355 if (sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1358 *csect_cache
= csect
;
1359 else if (first_csect
== NULL
1360 || sym
.n_sclass
== C_FILE
|| sym
.n_sclass
== C_DWARF
)
1361 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1363 *csect_cache
= NULL
;
1364 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1365 sym_hash
+= sym
.n_numaux
+ 1;
1366 csect_cache
+= sym
.n_numaux
+ 1;
1367 lineno_counts
+= sym
.n_numaux
+ 1;
1372 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1377 /* If this symbol has line number information attached to it,
1378 and we're not stripping it, count the number of entries and
1379 add them to the count for this csect. In the final link pass
1380 we are going to attach line number information by symbol,
1381 rather than by section, in order to more easily handle
1382 garbage collection. */
1383 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1386 && ISFCN (sym
.n_type
))
1388 union internal_auxent auxlin
;
1390 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1391 sym
.n_type
, sym
.n_sclass
,
1392 0, sym
.n_numaux
, (void *) &auxlin
);
1394 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1396 asection
*enclosing
;
1397 bfd_signed_vma linoff
;
1399 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1400 if (enclosing
== NULL
)
1402 (*_bfd_error_handler
)
1403 (_("%B: `%s' has line numbers but no enclosing section"),
1405 bfd_set_error (bfd_error_bad_value
);
1408 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1409 - enclosing
->line_filepos
);
1410 /* Explicit cast to bfd_signed_vma for compiler. */
1411 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1413 struct internal_lineno lin
;
1414 bfd_byte
*linpstart
;
1416 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1418 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1420 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1422 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1425 bfd_byte
*linpend
, *linp
;
1427 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1428 + enclosing
->lineno_count
* linesz
);
1429 for (linp
= linpstart
+ linesz
;
1433 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1435 if (lin
.l_lnno
== 0)
1438 *lineno_counts
= (linp
- linpstart
) / linesz
;
1439 /* The setting of line_filepos will only be
1440 useful if all the line number entries for a
1441 csect are contiguous; this only matters for
1443 if (csect
->line_filepos
== 0)
1444 csect
->line_filepos
=
1445 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1451 /* Pick up the csect auxiliary information. */
1452 if (sym
.n_numaux
== 0)
1454 (*_bfd_error_handler
)
1455 (_("%B: class %d symbol `%s' has no aux entries"),
1456 abfd
, sym
.n_sclass
, name
);
1457 bfd_set_error (bfd_error_bad_value
);
1461 bfd_coff_swap_aux_in (abfd
,
1462 (void *) (esym
+ symesz
* sym
.n_numaux
),
1463 sym
.n_type
, sym
.n_sclass
,
1464 sym
.n_numaux
- 1, sym
.n_numaux
,
1467 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1476 (*_bfd_error_handler
)
1477 (_("%B: symbol `%s' has unrecognized csect type %d"),
1479 bfd_set_error (bfd_error_bad_value
);
1483 /* This is an external reference. */
1484 if (sym
.n_sclass
== C_HIDEXT
1485 || sym
.n_scnum
!= N_UNDEF
1486 || aux
.x_csect
.x_scnlen
.l
!= 0)
1488 (*_bfd_error_handler
)
1489 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1490 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1491 aux
.x_csect
.x_scnlen
.l
);
1492 bfd_set_error (bfd_error_bad_value
);
1496 /* An XMC_XO external reference is actually a reference to
1497 an absolute location. */
1498 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1499 section
= bfd_und_section_ptr
;
1502 section
= bfd_abs_section_ptr
;
1503 value
= sym
.n_value
;
1509 csect_index
= -(unsigned) 1;
1511 /* When we see a TOC anchor, we record the TOC value. */
1512 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1514 if (sym
.n_sclass
!= C_HIDEXT
1515 || aux
.x_csect
.x_scnlen
.l
!= 0)
1517 (*_bfd_error_handler
)
1518 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1519 abfd
, name
, sym
.n_sclass
, aux
.x_csect
.x_scnlen
.l
);
1520 bfd_set_error (bfd_error_bad_value
);
1523 xcoff_data (abfd
)->toc
= sym
.n_value
;
1526 /* We must merge TOC entries for the same symbol. We can
1527 merge two TOC entries if they are both C_HIDEXT, they
1528 both have the same name, they are both 4 or 8 bytes long, and
1529 they both have a relocation table entry for an external
1530 symbol with the same name. Unfortunately, this means
1531 that we must look through the relocations. Ick.
1533 Logic for 32 bit vs 64 bit.
1534 32 bit has a csect length of 4 for TOC
1535 64 bit has a csect length of 8 for TOC
1537 The conditions to get past the if-check are not that bad.
1538 They are what is used to create the TOC csects in the first
1540 if (aux
.x_csect
.x_smclas
== XMC_TC
1541 && sym
.n_sclass
== C_HIDEXT
1542 && info
->output_bfd
->xvec
== abfd
->xvec
1543 && ((bfd_xcoff_is_xcoff32 (abfd
)
1544 && aux
.x_csect
.x_scnlen
.l
== 4)
1545 || (bfd_xcoff_is_xcoff64 (abfd
)
1546 && aux
.x_csect
.x_scnlen
.l
== 8)))
1548 asection
*enclosing
;
1549 struct internal_reloc
*relocs
;
1550 bfd_size_type relindx
;
1551 struct internal_reloc
*rel
;
1553 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1554 if (enclosing
== NULL
)
1557 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1558 amt
= enclosing
->reloc_count
;
1559 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1560 rel
= relocs
+ relindx
;
1562 /* 32 bit R_POS r_size is 31
1563 64 bit R_POS r_size is 63 */
1564 if (relindx
< enclosing
->reloc_count
1565 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1566 && rel
->r_type
== R_POS
1567 && ((bfd_xcoff_is_xcoff32 (abfd
)
1568 && rel
->r_size
== 31)
1569 || (bfd_xcoff_is_xcoff64 (abfd
)
1570 && rel
->r_size
== 63)))
1574 struct internal_syment relsym
;
1576 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1577 + rel
->r_symndx
* symesz
);
1578 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1579 if (EXTERN_SYM_P (relsym
.n_sclass
))
1581 const char *relname
;
1582 char relbuf
[SYMNMLEN
+ 1];
1584 struct xcoff_link_hash_entry
*h
;
1586 /* At this point we know that the TOC entry is
1587 for an externally visible symbol. */
1588 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1590 if (relname
== NULL
)
1593 /* We only merge TOC entries if the TC name is
1594 the same as the symbol name. This handles
1595 the normal case, but not common cases like
1596 SYM.P4 which gcc generates to store SYM + 4
1597 in the TOC. FIXME. */
1598 if (strcmp (name
, relname
) == 0)
1600 copy
= (! info
->keep_memory
1601 || relsym
._n
._n_n
._n_zeroes
!= 0
1602 || relsym
._n
._n_n
._n_offset
== 0);
1603 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1604 relname
, TRUE
, copy
,
1609 /* At this point h->root.type could be
1610 bfd_link_hash_new. That should be OK,
1611 since we know for sure that we will come
1612 across this symbol as we step through the
1615 /* We store h in *sym_hash for the
1616 convenience of the relocate_section
1620 if (h
->toc_section
!= NULL
)
1622 asection
**rel_csects
;
1624 /* We already have a TOC entry for this
1625 symbol, so we can just ignore this
1628 reloc_info
[enclosing
->target_index
].csects
;
1629 rel_csects
[relindx
] = bfd_und_section_ptr
;
1633 /* We are about to create a TOC entry for
1642 asection
*enclosing
;
1644 /* We need to create a new section. We get the name from
1645 the csect storage mapping class, so that the linker can
1646 accumulate similar csects together. */
1648 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1652 /* The enclosing section is the main section : .data, .text
1653 or .bss that the csect is coming from. */
1654 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1655 if (enclosing
== NULL
)
1658 if (! bfd_is_abs_section (enclosing
)
1659 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1660 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1661 > enclosing
->vma
+ enclosing
->size
)))
1663 (*_bfd_error_handler
)
1664 (_("%B: csect `%s' not in enclosing section"),
1666 bfd_set_error (bfd_error_bad_value
);
1669 csect
->vma
= sym
.n_value
;
1670 csect
->filepos
= (enclosing
->filepos
1673 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1674 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1675 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1677 /* Record the enclosing section in the tdata for this new
1679 amt
= sizeof (struct coff_section_tdata
);
1680 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1681 if (csect
->used_by_bfd
== NULL
)
1683 amt
= sizeof (struct xcoff_section_tdata
);
1684 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1685 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1687 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1688 xcoff_section_data (abfd
, csect
)->lineno_count
=
1689 enclosing
->lineno_count
;
1691 if (enclosing
->owner
== abfd
)
1693 struct internal_reloc
*relocs
;
1694 bfd_size_type relindx
;
1695 struct internal_reloc
*rel
;
1696 asection
**rel_csect
;
1698 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1699 amt
= enclosing
->reloc_count
;
1700 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1702 rel
= relocs
+ relindx
;
1703 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1706 csect
->rel_filepos
= (enclosing
->rel_filepos
1707 + relindx
* bfd_coff_relsz (abfd
));
1708 while (relindx
< enclosing
->reloc_count
1709 && *rel_csect
== NULL
1710 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1714 csect
->flags
|= SEC_RELOC
;
1715 ++csect
->reloc_count
;
1722 /* There are a number of other fields and section flags
1723 which we do not bother to set. */
1725 csect_index
= ((esym
1726 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1729 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1731 if (first_csect
== NULL
)
1732 first_csect
= csect
;
1734 /* If this symbol is external, we treat it as starting at the
1735 beginning of the newly created section. */
1736 if (EXTERN_SYM_P (sym
.n_sclass
))
1742 /* If this is a TOC section for a symbol, record it. */
1743 if (set_toc
!= NULL
)
1744 set_toc
->toc_section
= csect
;
1749 /* This is a label definition. The x_scnlen field is the
1750 symbol index of the csect. Usually the XTY_LD symbol will
1751 follow its appropriate XTY_SD symbol. The .set pseudo op can
1752 cause the XTY_LD to not follow the XTY_SD symbol. */
1757 if (aux
.x_csect
.x_scnlen
.l
< 0
1758 || (aux
.x_csect
.x_scnlen
.l
1759 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1763 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1765 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1770 (*_bfd_error_handler
)
1771 (_("%B: misplaced XTY_LD `%s'"),
1773 bfd_set_error (bfd_error_bad_value
);
1777 value
= sym
.n_value
- csect
->vma
;
1782 /* This is an unitialized csect. We could base the name on
1783 the storage mapping class, but we don't bother except for
1784 an XMC_TD symbol. If this csect is externally visible,
1785 it is a common symbol. We put XMC_TD symbols in sections
1786 named .tocbss, and rely on the linker script to put that
1789 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1791 /* The linker script puts the .td section in the data
1792 section after the .tc section. */
1793 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1797 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1802 csect
->vma
= sym
.n_value
;
1803 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1804 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1805 /* There are a number of other fields and section flags
1806 which we do not bother to set. */
1808 csect_index
= ((esym
1809 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1812 amt
= sizeof (struct coff_section_tdata
);
1813 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1814 if (csect
->used_by_bfd
== NULL
)
1816 amt
= sizeof (struct xcoff_section_tdata
);
1817 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1818 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1820 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1822 if (first_csect
== NULL
)
1823 first_csect
= csect
;
1825 if (EXTERN_SYM_P (sym
.n_sclass
))
1827 csect
->flags
|= SEC_IS_COMMON
;
1830 value
= aux
.x_csect
.x_scnlen
.l
;
1836 /* Check for magic symbol names. */
1837 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1838 && aux
.x_csect
.x_smclas
!= XMC_TC
1839 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1845 if (strcmp (name
, "_text") == 0)
1846 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1847 else if (strcmp (name
, "_etext") == 0)
1848 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1849 else if (strcmp (name
, "_data") == 0)
1850 i
= XCOFF_SPECIAL_SECTION_DATA
;
1851 else if (strcmp (name
, "_edata") == 0)
1852 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1853 else if (strcmp (name
, "_end") == 0)
1854 i
= XCOFF_SPECIAL_SECTION_END
;
1856 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1857 i
= XCOFF_SPECIAL_SECTION_END2
;
1860 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1863 /* Now we have enough information to add the symbol to the
1864 linker hash table. */
1866 if (EXTERN_SYM_P (sym
.n_sclass
))
1871 BFD_ASSERT (section
!= NULL
);
1873 /* We must copy the name into memory if we got it from the
1874 syment itself, rather than the string table. */
1875 copy
= default_copy
;
1876 if (sym
._n
._n_n
._n_zeroes
!= 0
1877 || sym
._n
._n_n
._n_offset
== 0)
1880 /* Ignore global linkage code when linking statically. */
1881 if (info
->static_link
1882 && (smtyp
== XTY_SD
|| smtyp
== XTY_LD
)
1883 && aux
.x_csect
.x_smclas
== XMC_GL
)
1885 section
= bfd_und_section_ptr
;
1889 /* The AIX linker appears to only detect multiple symbol
1890 definitions when there is a reference to the symbol. If
1891 a symbol is defined multiple times, and the only
1892 references are from the same object file, the AIX linker
1893 appears to permit it. It does not merge the different
1894 definitions, but handles them independently. On the
1895 other hand, if there is a reference, the linker reports
1898 This matters because the AIX <net/net_globals.h> header
1899 file actually defines an initialized array, so we have to
1900 actually permit that to work.
1902 Just to make matters even more confusing, the AIX linker
1903 appears to permit multiple symbol definitions whenever
1904 the second definition is in an archive rather than an
1905 object file. This may be a consequence of the manner in
1906 which it handles archives: I think it may load the entire
1907 archive in as separate csects, and then let garbage
1908 collection discard symbols.
1910 We also have to handle the case of statically linking a
1911 shared object, which will cause symbol redefinitions,
1912 although this is an easier case to detect. */
1913 else if (info
->output_bfd
->xvec
== abfd
->xvec
)
1915 if (! bfd_is_und_section (section
))
1916 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1917 name
, TRUE
, copy
, FALSE
);
1919 /* Make a copy of the symbol name to prevent problems with
1921 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1922 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1923 TRUE
, TRUE
, FALSE
));
1925 if (*sym_hash
== NULL
)
1927 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1928 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1929 && ! bfd_is_und_section (section
)
1930 && ! bfd_is_com_section (section
))
1932 /* This is a second definition of a defined symbol. */
1933 if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
1934 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
1936 /* The existing symbol is from a shared library.
1938 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1939 (*sym_hash
)->root
.u
.undef
.abfd
=
1940 (*sym_hash
)->root
.u
.def
.section
->owner
;
1942 else if (abfd
->my_archive
!= NULL
)
1944 /* This is a redefinition in an object contained
1945 in an archive. Just ignore it. See the
1947 section
= bfd_und_section_ptr
;
1950 else if (sym
.n_sclass
== C_AIX_WEAKEXT
1951 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1953 /* At least one of the definitions is weak.
1954 Allow the normal rules to take effect. */
1956 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1957 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1959 /* This symbol has been referenced. In this
1960 case, we just continue and permit the
1961 multiple definition error. See the comment
1962 above about the behaviour of the AIX linker. */
1964 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1966 /* The symbols are both csects of the same
1967 class. There is at least a chance that this
1968 is a semi-legitimate redefinition. */
1969 section
= bfd_und_section_ptr
;
1971 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
1974 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
1975 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
1976 && (bfd_is_und_section (section
)
1977 || bfd_is_com_section (section
)))
1979 /* This is a reference to a multiply defined symbol.
1980 Report the error now. See the comment above
1981 about the behaviour of the AIX linker. We could
1982 also do this with warning symbols, but I'm not
1983 sure the XCOFF linker is wholly prepared to
1984 handle them, and that would only be a warning,
1986 if (! ((*info
->callbacks
->multiple_definition
)
1987 (info
, &(*sym_hash
)->root
, NULL
, NULL
, (bfd_vma
) 0)))
1989 /* Try not to give this error too many times. */
1990 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
1994 /* _bfd_generic_link_add_one_symbol may call the linker to
1995 generate an error message, and the linker may try to read
1996 the symbol table to give a good error. Right now, the
1997 line numbers are in an inconsistent state, since they are
1998 counted both in the real sections and in the new csects.
1999 We need to leave the count in the real sections so that
2000 the linker can report the line number of the error
2001 correctly, so temporarily clobber the link to the csects
2002 so that the linker will not try to read the line numbers
2003 a second time from the csects. */
2004 BFD_ASSERT (last_real
->next
== first_csect
);
2005 last_real
->next
= NULL
;
2006 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2007 if (! (_bfd_generic_link_add_one_symbol
2008 (info
, abfd
, name
, flags
, section
, value
,
2010 (struct bfd_link_hash_entry
**) sym_hash
)))
2012 last_real
->next
= first_csect
;
2014 if (smtyp
== XTY_CM
)
2016 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2017 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2018 /* We don't need the common csect we just created. */
2021 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2022 = csect
->alignment_power
;
2025 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2031 || section
== bfd_und_section_ptr
)
2032 flag
= XCOFF_REF_REGULAR
;
2034 flag
= XCOFF_DEF_REGULAR
;
2035 (*sym_hash
)->flags
|= flag
;
2037 if ((*sym_hash
)->smclas
== XMC_UA
2038 || flag
== XCOFF_DEF_REGULAR
)
2039 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2043 if (smtyp
== XTY_ER
)
2044 *csect_cache
= section
;
2047 *csect_cache
= csect
;
2049 xcoff_section_data (abfd
, csect
)->last_symndx
2050 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2053 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2054 sym_hash
+= sym
.n_numaux
+ 1;
2055 csect_cache
+= sym
.n_numaux
+ 1;
2056 lineno_counts
+= sym
.n_numaux
+ 1;
2059 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2061 /* Make sure that we have seen all the relocs. */
2062 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2064 /* Debugging sections have no csects. */
2065 if (bfd_get_section_flags (abfd
, o
) & SEC_DEBUGGING
)
2068 /* Reset the section size and the line number count, since the
2069 data is now attached to the csects. Don't reset the size of
2070 the .debug section, since we need to read it below in
2071 bfd_xcoff_size_dynamic_sections. */
2072 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
2074 o
->lineno_count
= 0;
2076 if ((o
->flags
& SEC_RELOC
) != 0)
2079 struct internal_reloc
*rel
;
2080 asection
**rel_csect
;
2082 rel
= reloc_info
[o
->target_index
].relocs
;
2083 rel_csect
= reloc_info
[o
->target_index
].csects
;
2085 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2087 if (*rel_csect
== NULL
)
2089 (*_bfd_error_handler
)
2090 (_("%B: reloc %s:%d not in csect"),
2092 bfd_set_error (bfd_error_bad_value
);
2096 /* We identify all function symbols that are the target
2097 of a relocation, so that we can create glue code for
2098 functions imported from dynamic objects. */
2099 if (info
->output_bfd
->xvec
== abfd
->xvec
2100 && *rel_csect
!= bfd_und_section_ptr
2101 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2103 struct xcoff_link_hash_entry
*h
;
2105 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2106 /* If the symbol name starts with a period, it is
2107 the code of a function. If the symbol is
2108 currently undefined, then add an undefined symbol
2109 for the function descriptor. This should do no
2110 harm, because any regular object that defines the
2111 function should also define the function
2112 descriptor. It helps, because it means that we
2113 will identify the function descriptor with a
2114 dynamic object if a dynamic object defines it. */
2115 if (h
->root
.root
.string
[0] == '.'
2116 && h
->descriptor
== NULL
)
2118 struct xcoff_link_hash_entry
*hds
;
2119 struct bfd_link_hash_entry
*bh
;
2121 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2122 h
->root
.root
.string
+ 1,
2126 if (hds
->root
.type
== bfd_link_hash_new
)
2129 if (! (_bfd_generic_link_add_one_symbol
2130 (info
, abfd
, hds
->root
.root
.string
,
2131 (flagword
) 0, bfd_und_section_ptr
,
2132 (bfd_vma
) 0, NULL
, FALSE
,
2135 hds
= (struct xcoff_link_hash_entry
*) bh
;
2137 hds
->flags
|= XCOFF_DESCRIPTOR
;
2138 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2139 hds
->descriptor
= h
;
2140 h
->descriptor
= hds
;
2142 if (h
->root
.root
.string
[0] == '.')
2143 h
->flags
|= XCOFF_CALLED
;
2147 free (reloc_info
[o
->target_index
].csects
);
2148 reloc_info
[o
->target_index
].csects
= NULL
;
2150 /* Reset SEC_RELOC and the reloc_count, since the reloc
2151 information is now attached to the csects. */
2152 o
->flags
&=~ SEC_RELOC
;
2155 /* If we are not keeping memory, free the reloc information. */
2156 if (! info
->keep_memory
2157 && coff_section_data (abfd
, o
) != NULL
2158 && coff_section_data (abfd
, o
)->relocs
!= NULL
2159 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2161 free (coff_section_data (abfd
, o
)->relocs
);
2162 coff_section_data (abfd
, o
)->relocs
= NULL
;
2166 /* Free up the line numbers. FIXME: We could cache these
2167 somewhere for the final link, to avoid reading them again. */
2168 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2170 free (reloc_info
[o
->target_index
].linenos
);
2171 reloc_info
[o
->target_index
].linenos
= NULL
;
2177 obj_coff_keep_syms (abfd
) = keep_syms
;
2182 if (reloc_info
!= NULL
)
2184 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2186 if (reloc_info
[o
->target_index
].csects
!= NULL
)
2187 free (reloc_info
[o
->target_index
].csects
);
2188 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2189 free (reloc_info
[o
->target_index
].linenos
);
2193 obj_coff_keep_syms (abfd
) = keep_syms
;
2200 /* Add symbols from an XCOFF object file. */
2203 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2205 if (! _bfd_coff_get_external_symbols (abfd
))
2207 if (! xcoff_link_add_symbols (abfd
, info
))
2209 if (! info
->keep_memory
)
2211 if (! _bfd_coff_free_symbols (abfd
))
2217 /* Look through the loader symbols to see if this dynamic object
2218 should be included in the link. The native linker uses the loader
2219 symbols, not the normal symbol table, so we do too. */
2222 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2223 struct bfd_link_info
*info
,
2224 bfd_boolean
*pneeded
,
2229 struct internal_ldhdr ldhdr
;
2230 const char *strings
;
2231 bfd_byte
*elsym
, *elsymend
;
2235 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2237 /* There are no symbols, so don't try to include it. */
2240 if (! xcoff_get_section_contents (abfd
, lsec
))
2242 contents
= coff_section_data (abfd
, lsec
)->contents
;
2244 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2246 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2248 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2250 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2251 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2253 struct internal_ldsym ldsym
;
2254 char nambuf
[SYMNMLEN
+ 1];
2256 struct bfd_link_hash_entry
*h
;
2258 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2260 /* We are only interested in exported symbols. */
2261 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2264 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2265 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2268 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2269 nambuf
[SYMNMLEN
] = '\0';
2273 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2275 /* We are only interested in symbols that are currently
2276 undefined. At this point we know that we are using an XCOFF
2279 && h
->type
== bfd_link_hash_undefined
2280 && (((struct xcoff_link_hash_entry
*) h
)->flags
2281 & XCOFF_DEF_DYNAMIC
) == 0)
2283 if (!(*info
->callbacks
2284 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2291 /* We do not need this shared object. */
2292 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2294 free (coff_section_data (abfd
, lsec
)->contents
);
2295 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2301 /* Look through the symbols to see if this object file should be
2302 included in the link. */
2305 xcoff_link_check_ar_symbols (bfd
*abfd
,
2306 struct bfd_link_info
*info
,
2307 bfd_boolean
*pneeded
,
2310 bfd_size_type symesz
;
2316 if ((abfd
->flags
& DYNAMIC
) != 0
2317 && ! info
->static_link
2318 && info
->output_bfd
->xvec
== abfd
->xvec
)
2319 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
, subsbfd
);
2321 symesz
= bfd_coff_symesz (abfd
);
2322 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2323 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2324 while (esym
< esym_end
)
2326 struct internal_syment sym
;
2328 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2330 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2333 char buf
[SYMNMLEN
+ 1];
2334 struct bfd_link_hash_entry
*h
;
2336 /* This symbol is externally visible, and is defined by this
2338 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2342 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2344 /* We are only interested in symbols that are currently
2345 undefined. If a symbol is currently known to be common,
2346 XCOFF linkers do not bring in an object file which
2347 defines it. We also don't bring in symbols to satisfy
2348 undefined references in shared objects. */
2350 && h
->type
== bfd_link_hash_undefined
2351 && (info
->output_bfd
->xvec
!= abfd
->xvec
2352 || (((struct xcoff_link_hash_entry
*) h
)->flags
2353 & XCOFF_DEF_DYNAMIC
) == 0))
2355 if (!(*info
->callbacks
2356 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
2363 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2366 /* We do not need this object file. */
2370 /* Check a single archive element to see if we need to include it in
2371 the link. *PNEEDED is set according to whether this element is
2372 needed in the link or not. This is called via
2373 _bfd_generic_link_add_archive_symbols. */
2376 xcoff_link_check_archive_element (bfd
*abfd
,
2377 struct bfd_link_info
*info
,
2378 bfd_boolean
*pneeded
)
2380 bfd_boolean keep_syms_p
;
2383 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2384 if (!_bfd_coff_get_external_symbols (abfd
))
2388 if (!xcoff_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
2393 /* Potentially, the add_archive_element hook may have set a
2394 substitute BFD for us. */
2398 && !_bfd_coff_free_symbols (oldbfd
))
2400 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2401 if (!_bfd_coff_get_external_symbols (abfd
))
2404 if (!xcoff_link_add_symbols (abfd
, info
))
2406 if (info
->keep_memory
)
2412 if (!_bfd_coff_free_symbols (abfd
))
2419 /* Given an XCOFF BFD, add symbols to the global hash table as
2423 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2425 switch (bfd_get_format (abfd
))
2428 return xcoff_link_add_object_symbols (abfd
, info
);
2431 /* If the archive has a map, do the usual search. We then need
2432 to check the archive for dynamic objects, because they may not
2433 appear in the archive map even though they should, perhaps, be
2434 included. If the archive has no map, we just consider each object
2435 file in turn, since that apparently is what the AIX native linker
2437 if (bfd_has_map (abfd
))
2439 if (! (_bfd_generic_link_add_archive_symbols
2440 (abfd
, info
, xcoff_link_check_archive_element
)))
2447 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2448 while (member
!= NULL
)
2450 if (bfd_check_format (member
, bfd_object
)
2451 && (info
->output_bfd
->xvec
== member
->xvec
)
2452 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2456 if (! xcoff_link_check_archive_element (member
, info
,
2460 member
->archive_pass
= -1;
2462 member
= bfd_openr_next_archived_file (abfd
, member
);
2469 bfd_set_error (bfd_error_wrong_format
);
2475 _bfd_xcoff_define_common_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2476 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2477 struct bfd_link_hash_entry
*harg
)
2479 struct xcoff_link_hash_entry
*h
;
2481 if (!bfd_generic_define_common_symbol (output_bfd
, info
, harg
))
2484 h
= (struct xcoff_link_hash_entry
*) harg
;
2485 h
->flags
|= XCOFF_DEF_REGULAR
;
2489 /* If symbol H has not been interpreted as a function descriptor,
2490 see whether it should be. Set up its descriptor information if so. */
2493 xcoff_find_function (struct bfd_link_info
*info
,
2494 struct xcoff_link_hash_entry
*h
)
2496 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2497 && h
->root
.root
.string
[0] != '.')
2500 struct xcoff_link_hash_entry
*hfn
;
2503 amt
= strlen (h
->root
.root
.string
) + 2;
2504 fnname
= bfd_malloc (amt
);
2508 strcpy (fnname
+ 1, h
->root
.root
.string
);
2509 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2510 fnname
, FALSE
, FALSE
, TRUE
);
2513 && hfn
->smclas
== XMC_PR
2514 && (hfn
->root
.type
== bfd_link_hash_defined
2515 || hfn
->root
.type
== bfd_link_hash_defweak
))
2517 h
->flags
|= XCOFF_DESCRIPTOR
;
2518 h
->descriptor
= hfn
;
2519 hfn
->descriptor
= h
;
2525 /* Return true if the given bfd contains at least one shared object. */
2528 xcoff_archive_contains_shared_object_p (struct bfd_link_info
*info
,
2531 struct xcoff_archive_info
*archive_info
;
2534 archive_info
= xcoff_get_archive_info (info
, archive
);
2535 if (!archive_info
->know_contains_shared_object_p
)
2537 member
= bfd_openr_next_archived_file (archive
, NULL
);
2538 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2539 member
= bfd_openr_next_archived_file (archive
, member
);
2541 archive_info
->contains_shared_object_p
= (member
!= NULL
);
2542 archive_info
->know_contains_shared_object_p
= 1;
2544 return archive_info
->contains_shared_object_p
;
2547 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2548 qualifies for export by -bexpall. */
2551 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2553 /* Exclude symbols beginning with '_'. */
2554 if (h
->root
.root
.string
[0] == '_')
2557 /* Exclude archive members that would otherwise be unreferenced. */
2558 if ((h
->flags
& XCOFF_MARK
) == 0
2559 && (h
->root
.type
== bfd_link_hash_defined
2560 || h
->root
.type
== bfd_link_hash_defweak
)
2561 && h
->root
.u
.def
.section
->owner
!= NULL
2562 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2568 /* Return true if symbol H qualifies for the forms of automatic export
2569 specified by AUTO_EXPORT_FLAGS. */
2572 xcoff_auto_export_p (struct bfd_link_info
*info
,
2573 struct xcoff_link_hash_entry
*h
,
2574 unsigned int auto_export_flags
)
2576 /* Don't automatically export things that were explicitly exported. */
2577 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2580 /* Don't export things that we don't define. */
2581 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2584 /* Don't export functions; export their descriptors instead. */
2585 if (h
->root
.root
.string
[0] == '.')
2588 /* We don't export a symbol which is being defined by an object
2589 included from an archive which contains a shared object. The
2590 rationale is that if an archive contains both an unshared and
2591 a shared object, then there must be some reason that the
2592 unshared object is unshared, and we don't want to start
2593 providing a shared version of it. In particular, this solves
2594 a bug involving the _savefNN set of functions. gcc will call
2595 those functions without providing a slot to restore the TOC,
2596 so it is essential that these functions be linked in directly
2597 and not from a shared object, which means that a shared
2598 object which also happens to link them in must not export
2599 them. This is confusing, but I haven't been able to think of
2600 a different approach. Note that the symbols can, of course,
2601 be exported explicitly. */
2602 if (h
->root
.type
== bfd_link_hash_defined
2603 || h
->root
.type
== bfd_link_hash_defweak
)
2607 owner
= h
->root
.u
.def
.section
->owner
;
2609 && owner
->my_archive
!= NULL
2610 && xcoff_archive_contains_shared_object_p (info
, owner
->my_archive
))
2614 /* Otherwise, all symbols are exported by -bexpfull. */
2615 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2618 /* Despite its name, -bexpall exports most but not all symbols. */
2619 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2620 && xcoff_covered_by_expall_p (h
))
2626 /* Return true if relocation REL needs to be copied to the .loader section.
2627 If REL is against a global symbol, H is that symbol, otherwise it
2631 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2632 struct xcoff_link_hash_entry
*h
)
2634 if (!xcoff_hash_table (info
)->loader_section
)
2637 switch (rel
->r_type
)
2644 /* We should never need a .loader reloc for a TOC-relative reloc. */
2648 /* In this case, relocations against defined symbols can be resolved
2651 || h
->root
.type
== bfd_link_hash_defined
2652 || h
->root
.type
== bfd_link_hash_defweak
2653 || h
->root
.type
== bfd_link_hash_common
)
2656 /* We will always provide a local definition of function symbols,
2657 even if we don't have one yet. */
2658 if ((h
->flags
& XCOFF_CALLED
) != 0)
2667 /* Absolute relocations against absolute symbols can be
2668 resolved statically. */
2670 && (h
->root
.type
== bfd_link_hash_defined
2671 || h
->root
.type
== bfd_link_hash_defweak
)
2672 && bfd_is_abs_section (h
->root
.u
.def
.section
))
2679 /* Mark a symbol as not being garbage, including the section in which
2682 static inline bfd_boolean
2683 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2685 if ((h
->flags
& XCOFF_MARK
) != 0)
2688 h
->flags
|= XCOFF_MARK
;
2690 /* If we're marking an undefined symbol, try find some way of
2692 if (!info
->relocatable
2693 && (h
->flags
& XCOFF_IMPORT
) == 0
2694 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2695 && (h
->root
.type
== bfd_link_hash_undefined
2696 || h
->root
.type
== bfd_link_hash_undefweak
))
2698 /* First check whether this symbol can be interpreted as an
2699 undefined function descriptor for a defined function symbol. */
2700 if (!xcoff_find_function (info
, h
))
2703 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2704 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2705 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2707 /* This is a descriptor for a defined symbol, but the input
2708 objects have not defined the descriptor itself. Fill in
2709 the definition automatically.
2711 Note that we do this even if we found a dynamic definition
2712 of H. The local function definition logically overrides
2716 sec
= xcoff_hash_table (info
)->descriptor_section
;
2717 h
->root
.type
= bfd_link_hash_defined
;
2718 h
->root
.u
.def
.section
= sec
;
2719 h
->root
.u
.def
.value
= sec
->size
;
2721 h
->flags
|= XCOFF_DEF_REGULAR
;
2723 /* The size of the function descriptor depends on whether this
2724 is xcoff32 (12) or xcoff64 (24). */
2725 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2727 /* A function descriptor uses two relocs: one for the
2728 associated code, and one for the TOC address. */
2729 xcoff_hash_table (info
)->ldrel_count
+= 2;
2730 sec
->reloc_count
+= 2;
2732 /* Mark the function itself. */
2733 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2736 /* Mark the TOC section, so that we get an anchor
2737 to relocate against. */
2738 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2741 /* We handle writing out the contents of the descriptor in
2742 xcoff_write_global_symbol. */
2744 else if (info
->static_link
)
2745 /* We can't get a symbol value dynamically, so just assume
2746 that it's undefined. */
2747 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2748 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2750 /* This is a function symbol for which we need to create
2753 struct xcoff_link_hash_entry
*hds
;
2755 /* Mark the descriptor (and its TOC section). */
2756 hds
= h
->descriptor
;
2757 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2758 || hds
->root
.type
== bfd_link_hash_undefweak
)
2759 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2760 if (!xcoff_mark_symbol (info
, hds
))
2763 /* Treat this symbol as undefined if the descriptor was. */
2764 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2765 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2767 /* Allocate room for the global linkage code itself. */
2768 sec
= xcoff_hash_table (info
)->linkage_section
;
2769 h
->root
.type
= bfd_link_hash_defined
;
2770 h
->root
.u
.def
.section
= sec
;
2771 h
->root
.u
.def
.value
= sec
->size
;
2773 h
->flags
|= XCOFF_DEF_REGULAR
;
2774 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2776 /* The global linkage code requires a TOC entry for the
2778 if (hds
->toc_section
== NULL
)
2783 xcoff32 uses 4 bytes in the toc.
2784 xcoff64 uses 8 bytes in the toc. */
2785 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2787 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2792 /* Allocate room in the fallback TOC section. */
2793 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2794 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2795 hds
->toc_section
->size
+= byte_size
;
2796 if (!xcoff_mark (info
, hds
->toc_section
))
2799 /* Allocate room for a static and dynamic R_TOC
2801 ++xcoff_hash_table (info
)->ldrel_count
;
2802 ++hds
->toc_section
->reloc_count
;
2804 /* Set the index to -2 to force this symbol to
2807 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2810 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2812 /* Record that the symbol was undefined, then import it.
2813 -brtl links use a special fake import file. */
2814 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2815 if (xcoff_hash_table (info
)->rtld
)
2817 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2822 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2828 if (h
->root
.type
== bfd_link_hash_defined
2829 || h
->root
.type
== bfd_link_hash_defweak
)
2833 hsec
= h
->root
.u
.def
.section
;
2834 if (! bfd_is_abs_section (hsec
)
2835 && (hsec
->flags
& SEC_MARK
) == 0)
2837 if (! xcoff_mark (info
, hsec
))
2842 if (h
->toc_section
!= NULL
2843 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2845 if (! xcoff_mark (info
, h
->toc_section
))
2852 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2853 If the symbol exists, set FLAGS. */
2856 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2857 const char *name
, unsigned int flags
)
2859 struct xcoff_link_hash_entry
*h
;
2861 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2862 FALSE
, FALSE
, TRUE
);
2866 if (h
->root
.type
== bfd_link_hash_defined
2867 || h
->root
.type
== bfd_link_hash_defweak
)
2869 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2876 /* The mark phase of garbage collection. For a given section, mark
2877 it, and all the sections which define symbols to which it refers.
2878 Because this function needs to look at the relocs, we also count
2879 the number of relocs which need to be copied into the .loader
2883 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2885 if (bfd_is_abs_section (sec
)
2886 || (sec
->flags
& SEC_MARK
) != 0)
2889 sec
->flags
|= SEC_MARK
;
2891 if (sec
->owner
->xvec
== info
->output_bfd
->xvec
2892 && coff_section_data (sec
->owner
, sec
) != NULL
2893 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2895 struct xcoff_link_hash_entry
**syms
;
2896 struct internal_reloc
*rel
, *relend
;
2898 unsigned long i
, first
, last
;
2900 /* Mark all the symbols in this section. */
2901 syms
= obj_xcoff_sym_hashes (sec
->owner
);
2902 csects
= xcoff_data (sec
->owner
)->csects
;
2903 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
2904 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
2905 for (i
= first
; i
<= last
; i
++)
2906 if (csects
[i
] == sec
2908 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
2910 if (!xcoff_mark_symbol (info
, syms
[i
]))
2914 /* Look through the section relocs. */
2915 if ((sec
->flags
& SEC_RELOC
) != 0
2916 && sec
->reloc_count
> 0)
2918 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, TRUE
,
2922 relend
= rel
+ sec
->reloc_count
;
2923 for (; rel
< relend
; rel
++)
2925 struct xcoff_link_hash_entry
*h
;
2927 if ((unsigned int) rel
->r_symndx
2928 > obj_raw_syment_count (sec
->owner
))
2931 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2934 if ((h
->flags
& XCOFF_MARK
) == 0)
2936 if (!xcoff_mark_symbol (info
, h
))
2944 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2946 && (rsec
->flags
& SEC_MARK
) == 0)
2948 if (!xcoff_mark (info
, rsec
))
2953 /* See if this reloc needs to be copied into the .loader
2955 if (xcoff_need_ldrel_p (info
, rel
, h
))
2957 ++xcoff_hash_table (info
)->ldrel_count
;
2959 h
->flags
|= XCOFF_LDREL
;
2963 if (! info
->keep_memory
2964 && coff_section_data (sec
->owner
, sec
) != NULL
2965 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2966 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2968 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2969 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2977 /* Routines that are called after all the input files have been
2978 handled, but before the sections are laid out in memory. */
2980 /* The sweep phase of garbage collection. Remove all garbage
2984 xcoff_sweep (struct bfd_link_info
*info
)
2988 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2992 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2994 if ((o
->flags
& SEC_MARK
) == 0)
2996 /* Keep all sections from non-XCOFF input files. Keep
2997 special sections. Keep .debug sections for the
2999 if (sub
->xvec
!= info
->output_bfd
->xvec
3000 || o
== xcoff_hash_table (info
)->debug_section
3001 || o
== xcoff_hash_table (info
)->loader_section
3002 || o
== xcoff_hash_table (info
)->linkage_section
3003 || o
== xcoff_hash_table (info
)->descriptor_section
3004 || (bfd_get_section_flags (sub
, o
) & SEC_DEBUGGING
)
3005 || strcmp (o
->name
, ".debug") == 0)
3006 o
->flags
|= SEC_MARK
;
3017 /* Record the number of elements in a set. This is used to output the
3018 correct csect length. */
3021 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3022 struct bfd_link_info
*info
,
3023 struct bfd_link_hash_entry
*harg
,
3026 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3027 struct xcoff_link_size_list
*n
;
3030 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3033 /* This will hardly ever be called. I don't want to burn four bytes
3034 per global symbol, so instead the size is kept on a linked list
3035 attached to the hash table. */
3037 n
= bfd_alloc (output_bfd
, amt
);
3040 n
->next
= xcoff_hash_table (info
)->size_list
;
3043 xcoff_hash_table (info
)->size_list
= n
;
3045 h
->flags
|= XCOFF_HAS_SIZE
;
3050 /* Import a symbol. */
3053 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3054 struct bfd_link_info
*info
,
3055 struct bfd_link_hash_entry
*harg
,
3057 const char *imppath
,
3058 const char *impfile
,
3059 const char *impmember
,
3060 unsigned int syscall_flag
)
3062 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3064 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3067 /* A symbol name which starts with a period is the code for a
3068 function. If the symbol is undefined, then add an undefined
3069 symbol for the function descriptor, and import that instead. */
3070 if (h
->root
.root
.string
[0] == '.'
3071 && h
->root
.type
== bfd_link_hash_undefined
3072 && val
== (bfd_vma
) -1)
3074 struct xcoff_link_hash_entry
*hds
;
3076 hds
= h
->descriptor
;
3079 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3080 h
->root
.root
.string
+ 1,
3084 if (hds
->root
.type
== bfd_link_hash_new
)
3086 hds
->root
.type
= bfd_link_hash_undefined
;
3087 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3089 hds
->flags
|= XCOFF_DESCRIPTOR
;
3090 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3091 hds
->descriptor
= h
;
3092 h
->descriptor
= hds
;
3095 /* Now, if the descriptor is undefined, import the descriptor
3096 rather than the symbol we were told to import. FIXME: Is
3097 this correct in all cases? */
3098 if (hds
->root
.type
== bfd_link_hash_undefined
)
3102 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3104 if (val
!= (bfd_vma
) -1)
3106 if (h
->root
.type
== bfd_link_hash_defined
3107 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
3108 || h
->root
.u
.def
.value
!= val
))
3110 if (! ((*info
->callbacks
->multiple_definition
)
3111 (info
, &h
->root
, output_bfd
, bfd_abs_section_ptr
, val
)))
3115 h
->root
.type
= bfd_link_hash_defined
;
3116 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3117 h
->root
.u
.def
.value
= val
;
3121 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3127 /* Export a symbol. */
3130 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3131 struct bfd_link_info
*info
,
3132 struct bfd_link_hash_entry
*harg
)
3134 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3136 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3139 h
->flags
|= XCOFF_EXPORT
;
3141 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3142 I'm just going to ignore it until somebody explains it. */
3144 /* Make sure we don't garbage collect this symbol. */
3145 if (! xcoff_mark_symbol (info
, h
))
3148 /* If this is a function descriptor, make sure we don't garbage
3149 collect the associated function code. We normally don't have to
3150 worry about this, because the descriptor will be attached to a
3151 section with relocs, but if we are creating the descriptor
3152 ourselves those relocs will not be visible to the mark code. */
3153 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3155 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3162 /* Count a reloc against a symbol. This is called for relocs
3163 generated by the linker script, typically for global constructors
3167 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3168 struct bfd_link_info
*info
,
3171 struct xcoff_link_hash_entry
*h
;
3173 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3176 h
= ((struct xcoff_link_hash_entry
*)
3177 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, FALSE
, FALSE
,
3181 (*_bfd_error_handler
) (_("%s: no such symbol"), name
);
3182 bfd_set_error (bfd_error_no_symbols
);
3186 h
->flags
|= XCOFF_REF_REGULAR
;
3187 if (xcoff_hash_table (info
)->loader_section
)
3189 h
->flags
|= XCOFF_LDREL
;
3190 ++xcoff_hash_table (info
)->ldrel_count
;
3193 /* Mark the symbol to avoid garbage collection. */
3194 if (! xcoff_mark_symbol (info
, h
))
3200 /* This function is called for each symbol to which the linker script
3204 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3205 struct bfd_link_info
*info
,
3208 struct xcoff_link_hash_entry
*h
;
3210 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3213 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
, TRUE
,
3218 h
->flags
|= XCOFF_DEF_REGULAR
;
3223 /* An xcoff_link_hash_traverse callback for which DATA points to an
3224 xcoff_loader_info. Mark all symbols that should be automatically
3228 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3230 struct xcoff_loader_info
*ldinfo
;
3232 ldinfo
= (struct xcoff_loader_info
*) data
;
3233 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3235 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3236 ldinfo
->failed
= TRUE
;
3241 /* Add a symbol to the .loader symbols, if necessary. */
3243 /* INPUT_BFD has an external symbol associated with hash table entry H
3244 and csect CSECT. Return true if INPUT_BFD defines H. */
3247 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3250 switch (h
->root
.type
)
3252 case bfd_link_hash_defined
:
3253 case bfd_link_hash_defweak
:
3254 /* No input bfd owns absolute symbols. They are written by
3255 xcoff_write_global_symbol instead. */
3256 return (!bfd_is_abs_section (csect
)
3257 && h
->root
.u
.def
.section
== csect
);
3259 case bfd_link_hash_common
:
3260 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3262 case bfd_link_hash_undefined
:
3263 case bfd_link_hash_undefweak
:
3264 /* We can't treat undef.abfd as the owner because that bfd
3265 might be a dynamic object. Allow any bfd to claim it. */
3273 /* See if H should have a loader symbol associated with it. */
3276 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3277 struct xcoff_link_hash_entry
*h
)
3281 /* Warn if this symbol is exported but not defined. */
3282 if ((h
->flags
& XCOFF_EXPORT
) != 0
3283 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3285 (*_bfd_error_handler
)
3286 (_("warning: attempt to export undefined symbol `%s'"),
3287 h
->root
.root
.string
);
3291 /* We need to add a symbol to the .loader section if it is mentioned
3292 in a reloc which we are copying to the .loader section and it was
3293 not defined or common, or if it is the entry point, or if it is
3295 if (((h
->flags
& XCOFF_LDREL
) == 0
3296 || h
->root
.type
== bfd_link_hash_defined
3297 || h
->root
.type
== bfd_link_hash_defweak
3298 || h
->root
.type
== bfd_link_hash_common
)
3299 && (h
->flags
& XCOFF_ENTRY
) == 0
3300 && (h
->flags
& XCOFF_EXPORT
) == 0)
3303 /* We need to add this symbol to the .loader symbols. */
3305 BFD_ASSERT (h
->ldsym
== NULL
);
3306 amt
= sizeof (struct internal_ldsym
);
3307 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3308 if (h
->ldsym
== NULL
)
3310 ldinfo
->failed
= TRUE
;
3314 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3316 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3317 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3319 h
->ldsym
->l_ifile
= h
->ldindx
;
3322 /* The first 3 symbol table indices are reserved to indicate the
3323 data, text and bss sections. */
3324 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3326 ++ldinfo
->ldsym_count
;
3328 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3329 h
->ldsym
, h
->root
.root
.string
))
3332 h
->flags
|= XCOFF_BUILT_LDSYM
;
3336 /* An xcoff_htab_traverse callback that is called for each symbol
3337 once garbage collection is complete. */
3340 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3342 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3344 /* __rtinit, this symbol has special handling. */
3345 if (h
->flags
& XCOFF_RTINIT
)
3348 /* We don't want to garbage collect symbols which are not defined in
3349 XCOFF files. This is a convenient place to mark them. */
3350 if (xcoff_hash_table (ldinfo
->info
)->gc
3351 && (h
->flags
& XCOFF_MARK
) == 0
3352 && (h
->root
.type
== bfd_link_hash_defined
3353 || h
->root
.type
== bfd_link_hash_defweak
)
3354 && (h
->root
.u
.def
.section
->owner
== NULL
3355 || (h
->root
.u
.def
.section
->owner
->xvec
3356 != ldinfo
->info
->output_bfd
->xvec
)))
3357 h
->flags
|= XCOFF_MARK
;
3359 /* Skip discarded symbols. */
3360 if (xcoff_hash_table (ldinfo
->info
)->gc
3361 && (h
->flags
& XCOFF_MARK
) == 0)
3364 /* If this is still a common symbol, and it wasn't garbage
3365 collected, we need to actually allocate space for it in the .bss
3367 if (h
->root
.type
== bfd_link_hash_common
3368 && h
->root
.u
.c
.p
->section
->size
== 0)
3370 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3371 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3374 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3376 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3377 h
->flags
|= XCOFF_EXPORT
;
3379 if (!xcoff_build_ldsym (ldinfo
, h
))
3386 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3387 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3388 csect information, if any. NAME is the function's name if the name
3389 is stored in the .debug section, otherwise it is null.
3391 Return 1 if we should include an appropriately-adjusted ISYM
3392 in the output file, 0 if we should discard ISYM, or -1 if an
3396 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3397 struct internal_syment
*isym
,
3398 union internal_auxent
*aux
,
3399 struct xcoff_link_hash_entry
*h
,
3400 asection
*csect
, const char *name
)
3404 /* If we are skipping this csect, we want to strip the symbol too. */
3408 /* Likewise if we garbage-collected the csect. */
3409 if (xcoff_hash_table (info
)->gc
3410 && !bfd_is_abs_section (csect
)
3411 && !bfd_is_und_section (csect
)
3412 && (csect
->flags
& SEC_MARK
) == 0)
3415 /* An XCOFF linker always removes C_STAT symbols. */
3416 if (isym
->n_sclass
== C_STAT
)
3419 /* We generate the TOC anchor separately. */
3420 if (isym
->n_sclass
== C_HIDEXT
3421 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3424 /* If we are stripping all symbols, we want to discard this one. */
3425 if (info
->strip
== strip_all
)
3428 /* Discard symbols that are defined elsewhere. */
3429 if (EXTERN_SYM_P (isym
->n_sclass
))
3431 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3433 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3437 /* If we're discarding local symbols, check whether ISYM is local. */
3438 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3439 if (info
->discard
== discard_all
3440 && !EXTERN_SYM_P (isym
->n_sclass
)
3441 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3444 /* If we're stripping debugging symbols, check whether ISYM is one. */
3445 if (info
->strip
== strip_debugger
3446 && isym
->n_scnum
== N_DEBUG
)
3449 /* If we are stripping symbols based on name, check how ISYM's
3450 name should be handled. */
3451 if (info
->strip
== strip_some
3452 || info
->discard
== discard_l
)
3454 char buf
[SYMNMLEN
+ 1];
3458 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3463 if (info
->strip
== strip_some
3464 && bfd_hash_lookup (info
->keep_hash
, name
, FALSE
, FALSE
) == NULL
)
3467 if (info
->discard
== discard_l
3468 && !EXTERN_SYM_P (isym
->n_sclass
)
3469 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3470 && bfd_is_local_label_name (input_bfd
, name
))
3477 /* Lay out the .loader section, filling in the header and the import paths.
3478 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3481 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
,
3482 const char *libpath
)
3485 struct xcoff_link_hash_table
*htab
;
3486 struct internal_ldhdr
*ldhdr
;
3487 struct xcoff_import_file
*fl
;
3488 bfd_size_type stoff
;
3489 size_t impsize
, impcount
;
3493 /* Work out the size of the import file names. Each import file ID
3494 consists of three null terminated strings: the path, the file
3495 name, and the archive member name. The first entry in the list
3496 of names is the path to use to find objects, which the linker has
3497 passed in as the libpath argument. For some reason, the path
3498 entry in the other import file names appears to always be empty. */
3499 output_bfd
= ldinfo
->output_bfd
;
3500 htab
= xcoff_hash_table (ldinfo
->info
);
3501 impsize
= strlen (libpath
) + 3;
3503 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3506 impsize
+= (strlen (fl
->path
)
3508 + strlen (fl
->member
)
3512 /* Set up the .loader section header. */
3513 ldhdr
= &htab
->ldhdr
;
3514 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3515 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3516 ldhdr
->l_nreloc
= htab
->ldrel_count
;
3517 ldhdr
->l_istlen
= impsize
;
3518 ldhdr
->l_nimpid
= impcount
;
3519 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3520 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3521 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3522 ldhdr
->l_stlen
= ldinfo
->string_size
;
3523 stoff
= ldhdr
->l_impoff
+ impsize
;
3524 if (ldinfo
->string_size
== 0)
3527 ldhdr
->l_stoff
= stoff
;
3529 /* 64 bit elements to ldhdr
3530 The swap out routine for 32 bit will ignore them.
3531 Nothing fancy, symbols come after the header and relocs come
3533 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3534 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3535 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3537 /* We now know the final size of the .loader section. Allocate
3539 lsec
= htab
->loader_section
;
3540 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3541 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3542 if (lsec
->contents
== NULL
)
3545 /* Set up the header. */
3546 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3548 /* Set up the import file names. */
3549 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3550 strcpy (out
, libpath
);
3551 out
+= strlen (libpath
) + 1;
3554 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3559 while ((*out
++ = *s
++) != '\0')
3562 while ((*out
++ = *s
++) != '\0')
3565 while ((*out
++ = *s
++) != '\0')
3569 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3571 /* Set up the symbol string table. */
3572 if (ldinfo
->string_size
> 0)
3574 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
3575 free (ldinfo
->strings
);
3576 ldinfo
->strings
= NULL
;
3579 /* We can't set up the symbol table or the relocs yet, because we
3580 don't yet know the final position of the various sections. The
3581 .loader symbols are written out when the corresponding normal
3582 symbols are written out in xcoff_link_input_bfd or
3583 xcoff_write_global_symbol. The .loader relocs are written out
3584 when the corresponding normal relocs are handled in
3585 xcoff_link_input_bfd. */
3590 /* Build the .loader section. This is called by the XCOFF linker
3591 emulation before_allocation routine. We must set the size of the
3592 .loader section before the linker lays out the output file.
3593 LIBPATH is the library path to search for shared objects; this is
3594 normally built from the -L arguments passed to the linker. ENTRY
3595 is the name of the entry point symbol (the -e linker option).
3596 FILE_ALIGN is the alignment to use for sections within the file
3597 (the -H linker option). MAXSTACK is the maximum stack size (the
3598 -bmaxstack linker option). MAXDATA is the maximum data size (the
3599 -bmaxdata linker option). GC is whether to do garbage collection
3600 (the -bgc linker option). MODTYPE is the module type (the
3601 -bmodtype linker option). TEXTRO is whether the text section must
3602 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3603 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3604 is set by this routine to csects with magic names like _end. */
3607 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3608 struct bfd_link_info
*info
,
3609 const char *libpath
,
3611 unsigned long file_align
,
3612 unsigned long maxstack
,
3613 unsigned long maxdata
,
3617 unsigned int auto_export_flags
,
3618 asection
**special_sections
,
3621 struct xcoff_loader_info ldinfo
;
3625 struct bfd_strtab_hash
*debug_strtab
;
3626 bfd_byte
*debug_contents
= NULL
;
3629 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3631 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3632 special_sections
[i
] = NULL
;
3636 ldinfo
.failed
= FALSE
;
3637 ldinfo
.output_bfd
= output_bfd
;
3639 ldinfo
.auto_export_flags
= auto_export_flags
;
3640 ldinfo
.ldsym_count
= 0;
3641 ldinfo
.string_size
= 0;
3642 ldinfo
.strings
= NULL
;
3643 ldinfo
.string_alc
= 0;
3645 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3646 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3647 xcoff_data (output_bfd
)->modtype
= modtype
;
3649 xcoff_hash_table (info
)->file_align
= file_align
;
3650 xcoff_hash_table (info
)->textro
= textro
;
3651 xcoff_hash_table (info
)->rtld
= rtld
;
3654 if (xcoff_hash_table (info
)->loader_section
3655 && (info
->init_function
|| info
->fini_function
|| rtld
))
3657 struct xcoff_link_hash_entry
*hsym
;
3658 struct internal_ldsym
*ldsym
;
3660 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3661 "__rtinit", FALSE
, FALSE
, TRUE
);
3664 (*_bfd_error_handler
)
3665 (_("error: undefined symbol __rtinit"));
3669 xcoff_mark_symbol (info
, hsym
);
3670 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3672 /* __rtinit initialized. */
3673 amt
= sizeof (* ldsym
);
3674 ldsym
= bfd_malloc (amt
);
3676 ldsym
->l_value
= 0; /* Will be filled in later. */
3677 ldsym
->l_scnum
= 2; /* Data section. */
3678 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3679 ldsym
->l_smclas
= 5; /* .rw. */
3680 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3681 ldsym
->l_parm
= 0; /* NA. */
3683 /* Force __rtinit to be the first symbol in the loader symbol table
3684 See xcoff_build_ldsyms
3686 The first 3 symbol table indices are reserved to indicate the data,
3687 text and bss sections. */
3688 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3691 ldinfo
.ldsym_count
= 1;
3692 hsym
->ldsym
= ldsym
;
3694 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3695 hsym
->ldsym
, hsym
->root
.root
.string
))
3698 /* This symbol is written out by xcoff_write_global_symbol
3699 Set stuff up so xcoff_write_global_symbol logic works. */
3700 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3701 hsym
->root
.type
= bfd_link_hash_defined
;
3702 hsym
->root
.u
.def
.value
= 0;
3705 /* Garbage collect unused sections. */
3706 if (info
->relocatable
|| !gc
)
3709 xcoff_hash_table (info
)->gc
= FALSE
;
3711 /* We still need to call xcoff_mark, in order to set ldrel_count
3713 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3717 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3719 /* We shouldn't unconditionaly mark the TOC section.
3720 The output file should only have a TOC if either
3721 (a) one of the input files did or (b) we end up
3722 creating TOC references as part of the link process. */
3723 if (o
!= xcoff_hash_table (info
)->toc_section
3724 && (o
->flags
& SEC_MARK
) == 0)
3726 if (! xcoff_mark (info
, o
))
3735 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3737 if (info
->init_function
!= NULL
3738 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3740 if (info
->fini_function
!= NULL
3741 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3743 if (auto_export_flags
!= 0)
3745 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3746 xcoff_mark_auto_exports
, &ldinfo
);
3751 xcoff_hash_table (info
)->gc
= TRUE
;
3754 /* Return special sections to the caller. */
3755 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3757 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3761 && (sec
->flags
& SEC_MARK
) == 0)
3764 special_sections
[i
] = sec
;
3767 if (info
->input_bfds
== NULL
)
3768 /* I'm not sure what to do in this bizarre case. */
3771 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3776 if (xcoff_hash_table (info
)->loader_section
3777 && !xcoff_build_loader_section (&ldinfo
, libpath
))
3780 /* Allocate space for the magic sections. */
3781 sec
= xcoff_hash_table (info
)->linkage_section
;
3784 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3785 if (sec
->contents
== NULL
)
3788 sec
= xcoff_hash_table (info
)->toc_section
;
3791 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3792 if (sec
->contents
== NULL
)
3795 sec
= xcoff_hash_table (info
)->descriptor_section
;
3798 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3799 if (sec
->contents
== NULL
)
3803 /* Now that we've done garbage collection, decide which symbols to keep,
3804 and figure out the contents of the .debug section. */
3805 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3807 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3810 bfd_size_type symcount
;
3813 unsigned int *lineno_counts
;
3814 struct xcoff_link_hash_entry
**sym_hash
;
3815 bfd_byte
*esym
, *esymend
;
3816 bfd_size_type symesz
;
3818 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3821 if ((sub
->flags
& DYNAMIC
) != 0
3822 && !info
->static_link
)
3825 if (! _bfd_coff_get_external_symbols (sub
))
3828 symcount
= obj_raw_syment_count (sub
);
3829 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
3830 if (debug_index
== NULL
)
3832 xcoff_data (sub
)->debug_indices
= debug_index
;
3834 if (info
->strip
== strip_all
3835 || info
->strip
== strip_debugger
3836 || info
->discard
== discard_all
)
3837 /* We're stripping all debugging information, so there's no need
3838 to read SUB's .debug section. */
3842 /* Grab the contents of SUB's .debug section, if any. */
3843 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3844 if (subdeb
!= NULL
&& subdeb
->size
> 0)
3846 /* We use malloc and copy the names into the debug
3847 stringtab, rather than bfd_alloc, because I expect
3848 that, when linking many files together, many of the
3849 strings will be the same. Storing the strings in the
3850 hash table should save space in this case. */
3851 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
3856 csectpp
= xcoff_data (sub
)->csects
;
3857 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
3858 sym_hash
= obj_xcoff_sym_hashes (sub
);
3859 symesz
= bfd_coff_symesz (sub
);
3860 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3861 esymend
= esym
+ symcount
* symesz
;
3863 while (esym
< esymend
)
3865 struct internal_syment sym
;
3866 union internal_auxent aux
;
3871 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
3873 /* Read in the csect information, if any. */
3874 if (CSECT_SYM_P (sym
.n_sclass
))
3876 BFD_ASSERT (sym
.n_numaux
> 0);
3877 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
3878 sym
.n_type
, sym
.n_sclass
,
3879 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
3882 /* If this symbol's name is stored in the debug section,
3883 get a pointer to it. */
3884 if (debug_contents
!= NULL
3885 && sym
._n
._n_n
._n_zeroes
== 0
3886 && bfd_coff_symname_in_debug (sub
, &sym
))
3887 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3891 /* Decide whether to copy this symbol to the output file. */
3893 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
3894 *sym_hash
, csect
, name
);
3899 /* Use a debug_index of -2 to record that a symbol should
3904 /* See whether we should store the symbol name in the
3905 output .debug section. */
3910 indx
= _bfd_stringtab_add (debug_strtab
, name
, TRUE
, TRUE
);
3911 if (indx
== (bfd_size_type
) -1)
3913 *debug_index
= indx
;
3918 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
3919 if (*lineno_counts
> 0)
3920 csect
->output_section
->lineno_count
+= *lineno_counts
;
3923 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3924 csectpp
+= sym
.n_numaux
+ 1;
3925 sym_hash
+= sym
.n_numaux
+ 1;
3926 lineno_counts
+= sym
.n_numaux
+ 1;
3927 debug_index
+= sym
.n_numaux
+ 1;
3932 free (debug_contents
);
3933 debug_contents
= NULL
;
3935 /* Clear the size of subdeb, so that it is not included directly
3936 in the output file. */
3940 if (! info
->keep_memory
)
3942 if (! _bfd_coff_free_symbols (sub
))
3947 if (info
->strip
!= strip_all
)
3948 xcoff_hash_table (info
)->debug_section
->size
=
3949 _bfd_stringtab_size (debug_strtab
);
3954 if (ldinfo
.strings
!= NULL
)
3955 free (ldinfo
.strings
);
3956 if (debug_contents
!= NULL
)
3957 free (debug_contents
);
3962 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
3967 struct bfd_in_memory
*bim
;
3969 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
3976 abfd
->link_next
= 0;
3977 abfd
->format
= bfd_object
;
3978 abfd
->iostream
= (void *) bim
;
3979 abfd
->flags
= BFD_IN_MEMORY
;
3980 abfd
->iovec
= &_bfd_memory_iovec
;
3981 abfd
->direction
= write_direction
;
3985 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
3988 /* need to reset to unknown or it will not be read back in correctly */
3989 abfd
->format
= bfd_unknown
;
3990 abfd
->direction
= read_direction
;
3996 /* Return the section that defines H. Return null if no section does. */
3999 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
4001 switch (h
->root
.type
)
4003 case bfd_link_hash_defined
:
4004 case bfd_link_hash_defweak
:
4005 return h
->root
.u
.def
.section
;
4007 case bfd_link_hash_common
:
4008 return h
->root
.u
.c
.p
->section
;
4015 /* Add a .loader relocation for input relocation IREL. If the loader
4016 relocation should be against an output section, HSEC points to the
4017 input section that IREL is against, otherwise HSEC is null. H is the
4018 symbol that IREL is against, or null if it isn't against a global symbol.
4019 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4022 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
4023 asection
*output_section
, bfd
*reference_bfd
,
4024 struct internal_reloc
*irel
, asection
*hsec
,
4025 struct xcoff_link_hash_entry
*h
)
4027 struct internal_ldrel ldrel
;
4029 ldrel
.l_vaddr
= irel
->r_vaddr
;
4032 const char *secname
;
4034 secname
= hsec
->output_section
->name
;
4035 if (strcmp (secname
, ".text") == 0)
4037 else if (strcmp (secname
, ".data") == 0)
4039 else if (strcmp (secname
, ".bss") == 0)
4043 (*_bfd_error_handler
)
4044 (_("%B: loader reloc in unrecognized section `%s'"),
4045 reference_bfd
, secname
);
4046 bfd_set_error (bfd_error_nonrepresentable_section
);
4054 (*_bfd_error_handler
)
4055 (_("%B: `%s' in loader reloc but not loader sym"),
4056 reference_bfd
, h
->root
.root
.string
);
4057 bfd_set_error (bfd_error_bad_value
);
4060 ldrel
.l_symndx
= h
->ldindx
;
4063 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4065 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4066 ldrel
.l_rsecnm
= output_section
->target_index
;
4067 if (xcoff_hash_table (flinfo
->info
)->textro
4068 && strcmp (output_section
->name
, ".text") == 0)
4070 (*_bfd_error_handler
)
4071 (_("%B: loader reloc in read-only section %A"),
4072 reference_bfd
, output_section
);
4073 bfd_set_error (bfd_error_invalid_operation
);
4076 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
4077 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4081 /* Link an input file into the linker output file. This function
4082 handles all the sections and relocations of the input file at once. */
4085 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
4089 const char *strings
;
4090 bfd_size_type syment_base
;
4091 unsigned int n_tmask
;
4092 unsigned int n_btshft
;
4093 bfd_boolean copy
, hash
;
4094 bfd_size_type isymesz
;
4095 bfd_size_type osymesz
;
4096 bfd_size_type linesz
;
4099 struct xcoff_link_hash_entry
**sym_hash
;
4100 struct internal_syment
*isymp
;
4102 unsigned int *lineno_counts
;
4105 unsigned long output_index
;
4109 bfd_boolean keep_syms
;
4112 /* We can just skip DYNAMIC files, unless this is a static link. */
4113 if ((input_bfd
->flags
& DYNAMIC
) != 0
4114 && ! flinfo
->info
->static_link
)
4117 /* Move all the symbols to the output file. */
4118 output_bfd
= flinfo
->output_bfd
;
4120 syment_base
= obj_raw_syment_count (output_bfd
);
4121 isymesz
= bfd_coff_symesz (input_bfd
);
4122 osymesz
= bfd_coff_symesz (output_bfd
);
4123 linesz
= bfd_coff_linesz (input_bfd
);
4124 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4126 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4127 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4129 /* Define macros so that ISFCN, et. al., macros work correctly. */
4130 #define N_TMASK n_tmask
4131 #define N_BTSHFT n_btshft
4134 if (! flinfo
->info
->keep_memory
)
4137 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4140 if (! _bfd_coff_get_external_symbols (input_bfd
))
4143 /* Make one pass over the symbols and assign indices to symbols that
4144 we have decided to keep. Also use create .loader symbol information
4145 and update information in hash table entries. */
4146 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4147 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4148 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4149 csectpp
= xcoff_data (input_bfd
)->csects
;
4150 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4151 isymp
= flinfo
->internal_syms
;
4152 indexp
= flinfo
->sym_indices
;
4153 output_index
= syment_base
;
4154 while (esym
< esym_end
)
4156 union internal_auxent aux
;
4160 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4162 /* Read in the csect information, if any. */
4163 if (CSECT_SYM_P (isymp
->n_sclass
))
4165 BFD_ASSERT (isymp
->n_numaux
> 0);
4166 bfd_coff_swap_aux_in (input_bfd
,
4167 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4168 isymp
->n_type
, isymp
->n_sclass
,
4169 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4172 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4175 /* If this symbol is in the .loader section, swap out the
4176 .loader symbol information. If this is an external symbol
4177 reference to a defined symbol, though, then wait until we get
4178 to the definition. */
4179 if (EXTERN_SYM_P (isymp
->n_sclass
)
4180 && *sym_hash
!= NULL
4181 && (*sym_hash
)->ldsym
!= NULL
4182 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4184 struct xcoff_link_hash_entry
*h
;
4185 struct internal_ldsym
*ldsym
;
4189 if (isymp
->n_scnum
> 0)
4191 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4192 ldsym
->l_value
= (isymp
->n_value
4193 + (*csectpp
)->output_section
->vma
4194 + (*csectpp
)->output_offset
4199 ldsym
->l_scnum
= isymp
->n_scnum
;
4200 ldsym
->l_value
= isymp
->n_value
;
4203 ldsym
->l_smtype
= smtyp
;
4204 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4205 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4206 || (h
->flags
& XCOFF_IMPORT
) != 0)
4207 ldsym
->l_smtype
|= L_IMPORT
;
4208 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4209 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4210 || (h
->flags
& XCOFF_EXPORT
) != 0)
4211 ldsym
->l_smtype
|= L_EXPORT
;
4212 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4213 ldsym
->l_smtype
|= L_ENTRY
;
4214 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4215 ldsym
->l_smtype
|= L_WEAK
;
4217 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4219 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4221 else if (ldsym
->l_ifile
== 0)
4223 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4229 if (h
->root
.type
== bfd_link_hash_defined
4230 || h
->root
.type
== bfd_link_hash_defweak
)
4231 impbfd
= h
->root
.u
.def
.section
->owner
;
4232 else if (h
->root
.type
== bfd_link_hash_undefined
4233 || h
->root
.type
== bfd_link_hash_undefweak
)
4234 impbfd
= h
->root
.u
.undef
.abfd
;
4242 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
4243 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4250 BFD_ASSERT (h
->ldindx
>= 0);
4251 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
4254 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
4257 /* Fill in snentry now that we know the target_index. */
4258 if ((h
->flags
& XCOFF_ENTRY
) != 0
4259 && (h
->root
.type
== bfd_link_hash_defined
4260 || h
->root
.type
== bfd_link_hash_defweak
))
4262 xcoff_data (output_bfd
)->snentry
=
4263 h
->root
.u
.def
.section
->output_section
->target_index
;
4267 add
= 1 + isymp
->n_numaux
;
4269 if (*debug_index
== -2)
4270 /* We've decided to strip this symbol. */
4274 /* Assign the next unused index to this symbol. */
4275 *indexp
= output_index
;
4277 if (EXTERN_SYM_P (isymp
->n_sclass
))
4279 BFD_ASSERT (*sym_hash
!= NULL
);
4280 (*sym_hash
)->indx
= output_index
;
4283 /* If this is a symbol in the TOC which we may have merged
4284 (class XMC_TC), remember the symbol index of the TOC
4286 if (isymp
->n_sclass
== C_HIDEXT
4287 && aux
.x_csect
.x_smclas
== XMC_TC
4288 && *sym_hash
!= NULL
)
4290 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4291 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4292 (*sym_hash
)->u
.toc_indx
= output_index
;
4295 output_index
+= add
;
4298 esym
+= add
* isymesz
;
4304 for (--add
; add
> 0; --add
)
4308 /* Now write out the symbols that we decided to keep. */
4310 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4311 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4312 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4313 isymp
= flinfo
->internal_syms
;
4314 indexp
= flinfo
->sym_indices
;
4315 csectpp
= xcoff_data (input_bfd
)->csects
;
4316 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4317 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4318 outsym
= flinfo
->outsyms
;
4321 while (esym
< esym_end
)
4325 add
= 1 + isymp
->n_numaux
;
4328 esym
+= add
* isymesz
;
4331 struct internal_syment isym
;
4334 /* Adjust the symbol in order to output it. */
4336 if (isym
._n
._n_n
._n_zeroes
== 0
4337 && isym
._n
._n_n
._n_offset
!= 0)
4339 /* This symbol has a long name. Enter it in the string
4340 table we are building. If *debug_index != -1, the
4341 name has already been entered in the .debug section. */
4342 if (*debug_index
>= 0)
4343 isym
._n
._n_n
._n_offset
= *debug_index
;
4349 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4353 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
4354 if (indx
== (bfd_size_type
) -1)
4356 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4360 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4361 multiple definition problems when linking a shared object
4362 statically. (The native linker doesn't enter __rtinit into
4363 the normal table at all, but having a local symbol can make
4364 the objdump output easier to read.) */
4365 if (isym
.n_sclass
== C_EXT
4367 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4368 isym
.n_sclass
= C_HIDEXT
;
4370 /* The value of a C_FILE symbol is the symbol index of the
4371 next C_FILE symbol. The value of the last C_FILE symbol
4372 is -1. We try to get this right, below, just before we
4373 write the symbols out, but in the general case we may
4374 have to write the symbol out twice. */
4375 if (isym
.n_sclass
== C_FILE
)
4377 if (flinfo
->last_file_index
!= -1
4378 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4380 /* We must correct the value of the last C_FILE entry. */
4381 flinfo
->last_file
.n_value
= *indexp
;
4382 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4384 /* The last C_FILE symbol is in this input file. */
4385 bfd_coff_swap_sym_out (output_bfd
,
4386 (void *) &flinfo
->last_file
,
4387 (void *) (flinfo
->outsyms
4388 + ((flinfo
->last_file_index
4394 /* We have already written out the last C_FILE
4395 symbol. We need to write it out again. We
4396 borrow *outsym temporarily. */
4399 bfd_coff_swap_sym_out (output_bfd
,
4400 (void *) &flinfo
->last_file
,
4403 pos
= obj_sym_filepos (output_bfd
);
4404 pos
+= flinfo
->last_file_index
* osymesz
;
4405 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4406 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4412 flinfo
->last_file_index
= *indexp
;
4413 flinfo
->last_file
= isym
;
4416 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4417 into the line numbers. We update the symbol values when
4418 we handle the line numbers. */
4419 if (isym
.n_sclass
== C_BINCL
4420 || isym
.n_sclass
== C_EINCL
)
4422 isym
.n_value
= flinfo
->line_filepos
;
4425 /* The value of a C_BSTAT symbol is the symbol table
4426 index of the containing csect. */
4427 else if (isym
.n_sclass
== C_BSTAT
)
4431 indx
= isym
.n_value
;
4432 if (indx
< obj_raw_syment_count (input_bfd
))
4436 symindx
= flinfo
->sym_indices
[indx
];
4440 isym
.n_value
= symindx
;
4443 else if (isym
.n_sclass
!= C_ESTAT
4444 && isym
.n_sclass
!= C_DECL
4445 && isym
.n_scnum
> 0)
4447 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4448 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4449 + (*csectpp
)->output_offset
4453 /* Output the symbol. */
4454 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4459 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4461 union internal_auxent aux
;
4463 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4464 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4467 if (isymp
->n_sclass
== C_FILE
)
4469 /* This is the file name (or some comment put in by
4470 the compiler). If it is long, we must put it in
4471 the string table. */
4472 if (aux
.x_file
.x_n
.x_zeroes
== 0
4473 && aux
.x_file
.x_n
.x_offset
!= 0)
4475 const char *filename
;
4478 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4479 >= STRING_SIZE_SIZE
);
4480 if (strings
== NULL
)
4482 strings
= _bfd_coff_read_string_table (input_bfd
);
4483 if (strings
== NULL
)
4486 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4487 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
4489 if (indx
== (bfd_size_type
) -1)
4491 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4494 else if (CSECT_SYM_P (isymp
->n_sclass
)
4495 && i
+ 1 == isymp
->n_numaux
)
4498 /* We don't support type checking. I don't know if
4500 aux
.x_csect
.x_parmhash
= 0;
4501 /* I don't think anybody uses these fields, but we'd
4502 better clobber them just in case. */
4503 aux
.x_csect
.x_stab
= 0;
4504 aux
.x_csect
.x_snstab
= 0;
4506 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4510 indx
= aux
.x_csect
.x_scnlen
.l
;
4511 if (indx
< obj_raw_syment_count (input_bfd
))
4515 symindx
= flinfo
->sym_indices
[indx
];
4518 aux
.x_csect
.x_scnlen
.l
= 0;
4522 aux
.x_csect
.x_scnlen
.l
= symindx
;
4527 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4531 if (ISFCN (isymp
->n_type
)
4532 || ISTAG (isymp
->n_sclass
)
4533 || isymp
->n_sclass
== C_BLOCK
4534 || isymp
->n_sclass
== C_FCN
)
4536 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4538 && indx
< obj_raw_syment_count (input_bfd
))
4540 /* We look forward through the symbol for
4541 the index of the next symbol we are going
4542 to include. I don't know if this is
4544 while (flinfo
->sym_indices
[indx
] < 0
4545 && indx
< obj_raw_syment_count (input_bfd
))
4547 if (indx
>= obj_raw_syment_count (input_bfd
))
4548 indx
= output_index
;
4550 indx
= flinfo
->sym_indices
[indx
];
4551 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4556 indx
= aux
.x_sym
.x_tagndx
.l
;
4557 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4561 symindx
= flinfo
->sym_indices
[indx
];
4563 aux
.x_sym
.x_tagndx
.l
= 0;
4565 aux
.x_sym
.x_tagndx
.l
= symindx
;
4570 /* Copy over the line numbers, unless we are stripping
4571 them. We do this on a symbol by symbol basis in
4572 order to more easily handle garbage collection. */
4573 if (CSECT_SYM_P (isymp
->n_sclass
)
4575 && isymp
->n_numaux
> 1
4576 && ISFCN (isymp
->n_type
)
4577 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4579 if (*lineno_counts
== 0)
4580 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4583 asection
*enclosing
;
4584 unsigned int enc_count
;
4585 bfd_signed_vma linoff
;
4586 struct internal_lineno lin
;
4593 /* Read in the enclosing section's line-number
4594 information, if we haven't already. */
4596 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4597 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4598 if (oline
!= enclosing
)
4600 pos
= enclosing
->line_filepos
;
4601 amt
= linesz
* enc_count
;
4602 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4603 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
4609 /* Copy across the first entry, adjusting its
4611 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4612 - enclosing
->line_filepos
);
4613 linp
= flinfo
->linenos
+ linoff
;
4614 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4615 lin
.l_addr
.l_symndx
= *indexp
;
4616 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4618 /* Copy the other entries, adjusting their addresses. */
4619 linpend
= linp
+ *lineno_counts
* linesz
;
4620 offset
= (o
->output_section
->vma
4623 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4625 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4626 lin
.l_addr
.l_paddr
+= offset
;
4627 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4630 /* Write out the entries we've just processed. */
4631 pos
= (o
->output_section
->line_filepos
4632 + o
->output_section
->lineno_count
* linesz
);
4633 amt
= linesz
* *lineno_counts
;
4634 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4635 || bfd_bwrite (flinfo
->linenos
+ linoff
,
4636 amt
, output_bfd
) != amt
)
4638 o
->output_section
->lineno_count
+= *lineno_counts
;
4640 /* Record the offset of the symbol's line numbers
4641 in the output file. */
4642 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4646 struct internal_syment
*iisp
, *iispend
;
4649 bfd_vma range_start
, range_end
;
4652 /* Update any C_BINCL or C_EINCL symbols
4653 that refer to a line number in the
4654 range we just output. */
4655 iisp
= flinfo
->internal_syms
;
4656 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4657 iindp
= flinfo
->sym_indices
;
4658 oos
= flinfo
->outsyms
;
4659 range_start
= enclosing
->line_filepos
+ linoff
;
4660 range_end
= range_start
+ *lineno_counts
* linesz
;
4661 while (iisp
< iispend
)
4664 && (iisp
->n_sclass
== C_BINCL
4665 || iisp
->n_sclass
== C_EINCL
)
4666 && iisp
->n_value
>= range_start
4667 && iisp
->n_value
< range_end
)
4669 struct internal_syment iis
;
4671 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4672 iis
.n_value
= (iisp
->n_value
4675 bfd_coff_swap_sym_out (output_bfd
,
4680 iiadd
= 1 + iisp
->n_numaux
;
4682 oos
+= iiadd
* osymesz
;
4690 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4691 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4702 lineno_counts
+= add
;
4706 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4707 symbol will be the first symbol in the next input file. In the
4708 normal case, this will save us from writing out the C_FILE symbol
4710 if (flinfo
->last_file_index
!= -1
4711 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4713 flinfo
->last_file
.n_value
= output_index
;
4714 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
4715 (void *) (flinfo
->outsyms
4716 + ((flinfo
->last_file_index
- syment_base
)
4720 /* Write the modified symbols to the output file. */
4721 if (outsym
> flinfo
->outsyms
)
4723 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4724 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
4725 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4726 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
4729 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4730 + (outsym
- flinfo
->outsyms
) / osymesz
)
4733 obj_raw_syment_count (output_bfd
) = output_index
;
4736 /* Don't let the linker relocation routines discard the symbols. */
4737 keep_syms
= obj_coff_keep_syms (input_bfd
);
4738 obj_coff_keep_syms (input_bfd
) = TRUE
;
4740 /* Relocate the contents of each section. */
4741 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4745 if (! o
->linker_mark
)
4746 /* This section was omitted from the link. */
4749 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4751 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4754 /* We have set filepos correctly for the sections we created to
4755 represent csects, so bfd_get_section_contents should work. */
4756 if (coff_section_data (input_bfd
, o
) != NULL
4757 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4758 contents
= coff_section_data (input_bfd
, o
)->contents
;
4761 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4762 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
4764 contents
= flinfo
->contents
;
4767 if ((o
->flags
& SEC_RELOC
) != 0)
4770 struct internal_reloc
*internal_relocs
;
4771 struct internal_reloc
*irel
;
4773 struct internal_reloc
*irelend
;
4774 struct xcoff_link_hash_entry
**rel_hash
;
4777 /* Read in the relocs. */
4778 target_index
= o
->output_section
->target_index
;
4779 internal_relocs
= (xcoff_read_internal_relocs
4780 (input_bfd
, o
, FALSE
, flinfo
->external_relocs
,
4782 (flinfo
->section_info
[target_index
].relocs
4783 + o
->output_section
->reloc_count
)));
4784 if (internal_relocs
== NULL
)
4787 /* Call processor specific code to relocate the section
4789 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
4793 flinfo
->internal_syms
,
4794 xcoff_data (input_bfd
)->csects
))
4797 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4798 irel
= internal_relocs
;
4799 irelend
= irel
+ o
->reloc_count
;
4800 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
4801 + o
->output_section
->reloc_count
);
4802 for (; irel
< irelend
; irel
++, rel_hash
++)
4804 struct xcoff_link_hash_entry
*h
= NULL
;
4808 /* Adjust the reloc address and symbol index. */
4810 irel
->r_vaddr
+= offset
;
4812 r_symndx
= irel
->r_symndx
;
4817 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4819 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
4822 && h
->smclas
!= XMC_TD
4823 && (irel
->r_type
== R_TOC
4824 || irel
->r_type
== R_GL
4825 || irel
->r_type
== R_TCL
4826 || irel
->r_type
== R_TRL
4827 || irel
->r_type
== R_TRLA
))
4829 /* This is a TOC relative reloc with a symbol
4830 attached. The symbol should be the one which
4831 this reloc is for. We want to make this
4832 reloc against the TOC address of the symbol,
4833 not the symbol itself. */
4834 BFD_ASSERT (h
->toc_section
!= NULL
);
4835 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4836 if (h
->u
.toc_indx
!= -1)
4837 irel
->r_symndx
= h
->u
.toc_indx
;
4840 struct xcoff_toc_rel_hash
*n
;
4841 struct xcoff_link_section_info
*si
;
4845 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
4848 si
= flinfo
->section_info
+ target_index
;
4849 n
->next
= si
->toc_rel_hashes
;
4852 si
->toc_rel_hashes
= n
;
4857 /* This is a global symbol. */
4859 irel
->r_symndx
= h
->indx
;
4862 /* This symbol is being written at the end
4863 of the file, and we do not yet know the
4864 symbol index. We save the pointer to the
4865 hash table entry in the rel_hash list.
4866 We set the indx field to -2 to indicate
4867 that this symbol must not be stripped. */
4876 indx
= flinfo
->sym_indices
[r_symndx
];
4880 struct internal_syment
*is
;
4882 /* Relocations against a TC0 TOC anchor are
4883 automatically transformed to be against
4884 the TOC anchor in the output file. */
4885 is
= flinfo
->internal_syms
+ r_symndx
;
4886 if (is
->n_sclass
== C_HIDEXT
4887 && is
->n_numaux
> 0)
4890 union internal_auxent aux
;
4894 obj_coff_external_syms (input_bfd
))
4895 + ((r_symndx
+ is
->n_numaux
)
4897 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4898 is
->n_type
, is
->n_sclass
,
4902 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4903 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4904 indx
= flinfo
->toc_symindx
;
4909 irel
->r_symndx
= indx
;
4913 struct internal_syment
*is
;
4916 char buf
[SYMNMLEN
+ 1];
4918 /* This reloc is against a symbol we are
4919 stripping. It would be possible to handle
4920 this case, but I don't think it's worth it. */
4921 is
= flinfo
->internal_syms
+ r_symndx
;
4923 if (is
->n_sclass
!= C_DWARF
)
4925 name
= (_bfd_coff_internal_syment_name
4926 (input_bfd
, is
, buf
));
4931 if (!(*flinfo
->info
->callbacks
->unattached_reloc
)
4932 (flinfo
->info
, name
, input_bfd
, o
,
4940 if ((o
->flags
& SEC_DEBUGGING
) == 0
4941 && xcoff_need_ldrel_p (flinfo
->info
, irel
, h
))
4948 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4950 sec
= xcoff_symbol_section (h
);
4951 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
4952 o
->output_section
, input_bfd
,
4958 o
->output_section
->reloc_count
+= o
->reloc_count
;
4961 /* Write out the modified section contents. */
4962 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4963 contents
, (file_ptr
) o
->output_offset
,
4968 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4970 if (! flinfo
->info
->keep_memory
)
4972 if (! _bfd_coff_free_symbols (input_bfd
))
4982 /* Sort relocs by VMA. This is called via qsort. */
4985 xcoff_sort_relocs (const void * p1
, const void * p2
)
4987 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
4988 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
4990 if (r1
->r_vaddr
> r2
->r_vaddr
)
4992 else if (r1
->r_vaddr
< r2
->r_vaddr
)
4998 /* Return true if section SEC is a TOC section. */
5000 static inline bfd_boolean
5001 xcoff_toc_section_p (asection
*sec
)
5006 if (name
[0] == '.' && name
[1] == 't')
5010 if (name
[3] == '0' && name
[4] == 0)
5015 if (name
[2] == 'd' && name
[3] == 0)
5021 /* See if the link requires a TOC (it usually does!). If so, find a
5022 good place to put the TOC anchor csect, and write out the associated
5026 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
5028 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5032 struct internal_syment irsym
;
5033 union internal_auxent iraux
;
5037 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5038 index of a csect at the beginning of the TOC. */
5039 toc_start
= ~(bfd_vma
) 0;
5042 for (input_bfd
= flinfo
->info
->input_bfds
;
5044 input_bfd
= input_bfd
->link_next
)
5045 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5046 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5048 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5049 if (toc_start
> start
)
5052 section_index
= sec
->output_section
->target_index
;
5055 end
= start
+ sec
->size
;
5060 /* There's no need for a TC0 symbol if we don't have a TOC. */
5061 if (toc_end
< toc_start
)
5063 xcoff_data (output_bfd
)->toc
= toc_start
;
5067 if (toc_end
- toc_start
< 0x8000)
5068 /* Every TOC csect can be accessed from TOC_START. */
5069 best_address
= toc_start
;
5072 /* Find the lowest TOC csect that is still within range of TOC_END. */
5073 best_address
= toc_end
;
5074 for (input_bfd
= flinfo
->info
->input_bfds
;
5076 input_bfd
= input_bfd
->link_next
)
5077 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5078 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5080 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5081 if (start
< best_address
5082 && start
+ 0x8000 >= toc_end
)
5084 best_address
= start
;
5085 section_index
= sec
->output_section
->target_index
;
5089 /* Make sure that the start of the TOC is also within range. */
5090 if (best_address
> toc_start
+ 0x8000)
5092 (*_bfd_error_handler
)
5093 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5095 (unsigned long) (toc_end
- toc_start
));
5096 bfd_set_error (bfd_error_file_too_big
);
5101 /* Record the chosen TOC value. */
5102 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5103 xcoff_data (output_bfd
)->toc
= best_address
;
5104 xcoff_data (output_bfd
)->sntoc
= section_index
;
5106 /* Fill out the TC0 symbol. */
5107 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
, &irsym
, "TOC"))
5109 irsym
.n_value
= best_address
;
5110 irsym
.n_scnum
= section_index
;
5111 irsym
.n_sclass
= C_HIDEXT
;
5112 irsym
.n_type
= T_NULL
;
5114 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
5116 /* Fill out the auxillary csect information. */
5117 memset (&iraux
, 0, sizeof iraux
);
5118 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5119 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5120 iraux
.x_csect
.x_scnlen
.l
= 0;
5121 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5122 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5124 /* Write the contents to the file. */
5125 pos
= obj_sym_filepos (output_bfd
);
5126 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5127 size
= 2 * bfd_coff_symesz (output_bfd
);
5128 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5129 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
5131 obj_raw_syment_count (output_bfd
) += 2;
5136 /* Write out a non-XCOFF global symbol. */
5139 xcoff_write_global_symbol (struct bfd_hash_entry
*bh
, void * inf
)
5141 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) bh
;
5142 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
5145 struct internal_syment isym
;
5146 union internal_auxent aux
;
5151 output_bfd
= flinfo
->output_bfd
;
5152 outsym
= flinfo
->outsyms
;
5154 if (h
->root
.type
== bfd_link_hash_warning
)
5156 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5157 if (h
->root
.type
== bfd_link_hash_new
)
5161 /* If this symbol was garbage collected, just skip it. */
5162 if (xcoff_hash_table (flinfo
->info
)->gc
5163 && (h
->flags
& XCOFF_MARK
) == 0)
5166 /* If we need a .loader section entry, write it out. */
5167 if (h
->ldsym
!= NULL
)
5169 struct internal_ldsym
*ldsym
;
5174 if (h
->root
.type
== bfd_link_hash_undefined
5175 || h
->root
.type
== bfd_link_hash_undefweak
)
5179 ldsym
->l_scnum
= N_UNDEF
;
5180 ldsym
->l_smtype
= XTY_ER
;
5181 impbfd
= h
->root
.u
.undef
.abfd
;
5184 else if (h
->root
.type
== bfd_link_hash_defined
5185 || h
->root
.type
== bfd_link_hash_defweak
)
5189 sec
= h
->root
.u
.def
.section
;
5190 ldsym
->l_value
= (sec
->output_section
->vma
5191 + sec
->output_offset
5192 + h
->root
.u
.def
.value
);
5193 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5194 ldsym
->l_smtype
= XTY_SD
;
5195 impbfd
= sec
->owner
;
5201 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5202 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5203 || (h
->flags
& XCOFF_IMPORT
) != 0)
5205 Import symbols are defined so the check above will make
5206 the l_smtype XTY_SD. But this is not correct, it should
5208 ldsym
->l_smtype
|= L_IMPORT
;
5210 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5211 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5212 || (h
->flags
& XCOFF_EXPORT
) != 0)
5213 ldsym
->l_smtype
|= L_EXPORT
;
5215 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5216 ldsym
->l_smtype
|= L_ENTRY
;
5218 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5219 ldsym
->l_smtype
= XTY_SD
;
5221 ldsym
->l_smclas
= h
->smclas
;
5223 if (ldsym
->l_smtype
& L_IMPORT
)
5225 if ((h
->root
.type
== bfd_link_hash_defined
5226 || h
->root
.type
== bfd_link_hash_defweak
)
5227 && (h
->root
.u
.def
.value
!= 0))
5228 ldsym
->l_smclas
= XMC_XO
;
5230 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5231 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5232 ldsym
->l_smclas
= XMC_SV3264
;
5234 else if (h
->flags
& XCOFF_SYSCALL32
)
5235 ldsym
->l_smclas
= XMC_SV
;
5237 else if (h
->flags
& XCOFF_SYSCALL64
)
5238 ldsym
->l_smclas
= XMC_SV64
;
5241 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5245 else if (ldsym
->l_ifile
== 0)
5247 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5249 else if (impbfd
== NULL
)
5253 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5254 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5260 BFD_ASSERT (h
->ldindx
>= 0);
5262 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5265 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
5269 /* If this symbol needs global linkage code, write it out. */
5270 if (h
->root
.type
== bfd_link_hash_defined
5271 && (h
->root
.u
.def
.section
5272 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
5278 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5280 /* The first instruction in the global linkage code loads a
5281 specific TOC element. */
5282 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5283 + h
->descriptor
->toc_section
->output_offset
5284 - xcoff_data (output_bfd
)->toc
);
5286 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5287 tocoff
+= h
->descriptor
->u
.toc_offset
;
5289 /* The first instruction in the glink code needs to be
5290 cooked to to hold the correct offset in the toc. The
5291 rest are just output raw. */
5292 bfd_put_32 (output_bfd
,
5293 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5295 /* Start with i == 1 to get past the first instruction done above
5296 The /4 is because the glink code is in bytes and we are going
5298 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5299 bfd_put_32 (output_bfd
,
5300 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5304 /* If we created a TOC entry for this symbol, write out the required
5306 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5311 struct internal_reloc
*irel
;
5312 struct internal_syment irsym
;
5313 union internal_auxent iraux
;
5315 tocsec
= h
->toc_section
;
5316 osec
= tocsec
->output_section
;
5317 oindx
= osec
->target_index
;
5318 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5319 irel
->r_vaddr
= (osec
->vma
5320 + tocsec
->output_offset
5324 irel
->r_symndx
= h
->indx
;
5328 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5331 BFD_ASSERT (h
->ldindx
>= 0);
5333 /* Initialize the aux union here instead of closer to when it is
5334 written out below because the length of the csect depends on
5335 whether the output is 32 or 64 bit. */
5336 memset (&iraux
, 0, sizeof iraux
);
5337 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5338 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5339 iraux
.x_csect
.x_smclas
= XMC_TC
;
5341 /* 32 bit uses a 32 bit R_POS to do the relocations
5342 64 bit uses a 64 bit R_POS to do the relocations
5344 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5346 Which one is determined by the backend. */
5347 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5350 iraux
.x_csect
.x_scnlen
.l
= 8;
5352 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5355 iraux
.x_csect
.x_scnlen
.l
= 4;
5360 irel
->r_type
= R_POS
;
5361 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5362 ++osec
->reloc_count
;
5364 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5365 output_bfd
, irel
, NULL
, h
))
5368 /* We need to emit a symbol to define a csect which holds
5370 if (flinfo
->info
->strip
!= strip_all
)
5372 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
,
5373 &irsym
, h
->root
.root
.string
);
5377 irsym
.n_value
= irel
->r_vaddr
;
5378 irsym
.n_scnum
= osec
->target_index
;
5379 irsym
.n_sclass
= C_HIDEXT
;
5380 irsym
.n_type
= T_NULL
;
5383 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5384 outsym
+= bfd_coff_symesz (output_bfd
);
5386 /* Note : iraux is initialized above. */
5387 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5388 0, 1, (void *) outsym
);
5389 outsym
+= bfd_coff_auxesz (output_bfd
);
5393 /* We aren't going to write out the symbols below, so we
5394 need to write them out now. */
5395 pos
= obj_sym_filepos (output_bfd
);
5396 pos
+= (obj_raw_syment_count (output_bfd
)
5397 * bfd_coff_symesz (output_bfd
));
5398 amt
= outsym
- flinfo
->outsyms
;
5399 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5400 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5402 obj_raw_syment_count (output_bfd
) +=
5403 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5405 outsym
= flinfo
->outsyms
;
5410 /* If this symbol is a specially defined function descriptor, write
5411 it out. The first word is the address of the function code
5412 itself, the second word is the address of the TOC, and the third
5416 The addresses for the 32 bit will take 4 bytes and the addresses
5417 for 64 bit will take 8 bytes. Similar for the relocs. This type
5418 of logic was also done above to create a TOC entry in
5419 xcoff_write_global_symbol. */
5420 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5421 && h
->root
.type
== bfd_link_hash_defined
5422 && (h
->root
.u
.def
.section
5423 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
5429 struct xcoff_link_hash_entry
*hentry
;
5431 struct internal_reloc
*irel
;
5433 unsigned int reloc_size
, byte_size
;
5435 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5440 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5448 sec
= h
->root
.u
.def
.section
;
5449 osec
= sec
->output_section
;
5450 oindx
= osec
->target_index
;
5451 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5453 hentry
= h
->descriptor
;
5454 BFD_ASSERT (hentry
!= NULL
5455 && (hentry
->root
.type
== bfd_link_hash_defined
5456 || hentry
->root
.type
== bfd_link_hash_defweak
));
5457 esec
= hentry
->root
.u
.def
.section
;
5459 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5460 irel
->r_vaddr
= (osec
->vma
5461 + sec
->output_offset
5462 + h
->root
.u
.def
.value
);
5463 irel
->r_symndx
= esec
->output_section
->target_index
;
5464 irel
->r_type
= R_POS
;
5465 irel
->r_size
= reloc_size
;
5466 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5467 ++osec
->reloc_count
;
5469 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5470 output_bfd
, irel
, esec
, NULL
))
5473 /* There are three items to write out,
5474 the address of the code
5475 the address of the toc anchor
5476 the environment pointer.
5477 We are ignoring the environment pointer. So set it to zero. */
5478 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5480 bfd_put_64 (output_bfd
,
5481 (esec
->output_section
->vma
+ esec
->output_offset
5482 + hentry
->root
.u
.def
.value
),
5484 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5485 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5490 This logic was already called above so the error case where
5491 the backend is neither has already been checked. */
5492 bfd_put_32 (output_bfd
,
5493 (esec
->output_section
->vma
+ esec
->output_offset
5494 + hentry
->root
.u
.def
.value
),
5496 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5497 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5500 tsec
= coff_section_from_bfd_index (output_bfd
,
5501 xcoff_data (output_bfd
)->sntoc
);
5504 irel
->r_vaddr
= (osec
->vma
5505 + sec
->output_offset
5506 + h
->root
.u
.def
.value
5508 irel
->r_symndx
= tsec
->output_section
->target_index
;
5509 irel
->r_type
= R_POS
;
5510 irel
->r_size
= reloc_size
;
5511 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5512 ++osec
->reloc_count
;
5514 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5515 output_bfd
, irel
, tsec
, NULL
))
5519 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
5521 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5526 && (flinfo
->info
->strip
== strip_all
5527 || (flinfo
->info
->strip
== strip_some
5528 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
5529 FALSE
, FALSE
) == NULL
)))
5531 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5536 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5538 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5542 memset (&aux
, 0, sizeof aux
);
5544 h
->indx
= obj_raw_syment_count (output_bfd
);
5546 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
, &isym
,
5547 h
->root
.root
.string
);
5551 if (h
->root
.type
== bfd_link_hash_undefined
5552 || h
->root
.type
== bfd_link_hash_undefweak
)
5555 isym
.n_scnum
= N_UNDEF
;
5556 if (h
->root
.type
== bfd_link_hash_undefweak
5557 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5558 isym
.n_sclass
= C_WEAKEXT
;
5560 isym
.n_sclass
= C_EXT
;
5561 aux
.x_csect
.x_smtyp
= XTY_ER
;
5563 else if ((h
->root
.type
== bfd_link_hash_defined
5564 || h
->root
.type
== bfd_link_hash_defweak
)
5565 && h
->smclas
== XMC_XO
)
5567 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5568 isym
.n_value
= h
->root
.u
.def
.value
;
5569 isym
.n_scnum
= N_UNDEF
;
5570 if (h
->root
.type
== bfd_link_hash_undefweak
5571 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5572 isym
.n_sclass
= C_WEAKEXT
;
5574 isym
.n_sclass
= C_EXT
;
5575 aux
.x_csect
.x_smtyp
= XTY_ER
;
5577 else if (h
->root
.type
== bfd_link_hash_defined
5578 || h
->root
.type
== bfd_link_hash_defweak
)
5580 struct xcoff_link_size_list
*l
;
5582 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5583 + h
->root
.u
.def
.section
->output_offset
5584 + h
->root
.u
.def
.value
);
5585 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5586 isym
.n_scnum
= N_ABS
;
5588 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5589 isym
.n_sclass
= C_HIDEXT
;
5590 aux
.x_csect
.x_smtyp
= XTY_SD
;
5592 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5594 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
5600 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5606 else if (h
->root
.type
== bfd_link_hash_common
)
5608 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5609 + h
->root
.u
.c
.p
->section
->output_offset
);
5610 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5611 isym
.n_sclass
= C_EXT
;
5612 aux
.x_csect
.x_smtyp
= XTY_CM
;
5613 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5618 isym
.n_type
= T_NULL
;
5621 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5622 outsym
+= bfd_coff_symesz (output_bfd
);
5624 aux
.x_csect
.x_smclas
= h
->smclas
;
5625 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5627 outsym
+= bfd_coff_auxesz (output_bfd
);
5629 if ((h
->root
.type
== bfd_link_hash_defined
5630 || h
->root
.type
== bfd_link_hash_defweak
)
5631 && h
->smclas
!= XMC_XO
)
5633 /* We just output an SD symbol. Now output an LD symbol. */
5636 if (h
->root
.type
== bfd_link_hash_undefweak
5637 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5638 isym
.n_sclass
= C_WEAKEXT
;
5640 isym
.n_sclass
= C_EXT
;
5641 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5642 outsym
+= bfd_coff_symesz (output_bfd
);
5644 aux
.x_csect
.x_smtyp
= XTY_LD
;
5645 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5646 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5648 outsym
+= bfd_coff_auxesz (output_bfd
);
5651 pos
= obj_sym_filepos (output_bfd
);
5652 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5653 amt
= outsym
- flinfo
->outsyms
;
5654 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5655 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5657 obj_raw_syment_count (output_bfd
) +=
5658 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5663 /* Handle a link order which is supposed to generate a reloc. */
5666 xcoff_reloc_link_order (bfd
*output_bfd
,
5667 struct xcoff_final_link_info
*flinfo
,
5668 asection
*output_section
,
5669 struct bfd_link_order
*link_order
)
5671 reloc_howto_type
*howto
;
5672 struct xcoff_link_hash_entry
*h
;
5676 struct internal_reloc
*irel
;
5677 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5679 if (link_order
->type
== bfd_section_reloc_link_order
)
5680 /* We need to somehow locate a symbol in the right section. The
5681 symbol must either have a value of zero, or we must adjust
5682 the addend by the value of the symbol. FIXME: Write this
5683 when we need it. The old linker couldn't handle this anyhow. */
5686 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5689 bfd_set_error (bfd_error_bad_value
);
5693 h
= ((struct xcoff_link_hash_entry
*)
5694 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
5695 link_order
->u
.reloc
.p
->u
.name
,
5696 FALSE
, FALSE
, TRUE
));
5699 if (! ((*flinfo
->info
->callbacks
->unattached_reloc
)
5700 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0)))
5705 hsec
= xcoff_symbol_section (h
);
5706 if (h
->root
.type
== bfd_link_hash_defined
5707 || h
->root
.type
== bfd_link_hash_defweak
)
5708 hval
= h
->root
.u
.def
.value
;
5712 addend
= link_order
->u
.reloc
.p
->addend
;
5714 addend
+= (hsec
->output_section
->vma
5715 + hsec
->output_offset
5722 bfd_reloc_status_type rstat
;
5725 size
= bfd_get_reloc_size (howto
);
5726 buf
= bfd_zmalloc (size
);
5730 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5736 case bfd_reloc_outofrange
:
5738 case bfd_reloc_overflow
:
5739 if (! ((*flinfo
->info
->callbacks
->reloc_overflow
)
5740 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5741 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0)))
5748 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5749 (file_ptr
) link_order
->offset
, size
);
5755 /* Store the reloc information in the right place. It will get
5756 swapped and written out at the end of the final_link routine. */
5757 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
5758 + output_section
->reloc_count
);
5759 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
5760 + output_section
->reloc_count
);
5762 memset (irel
, 0, sizeof (struct internal_reloc
));
5763 *rel_hash_ptr
= NULL
;
5765 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5768 irel
->r_symndx
= h
->indx
;
5771 /* Set the index to -2 to force this symbol to get written out. */
5777 irel
->r_type
= howto
->type
;
5778 irel
->r_size
= howto
->bitsize
- 1;
5779 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5780 irel
->r_size
|= 0x80;
5782 ++output_section
->reloc_count
;
5784 /* Now output the reloc to the .loader section. */
5785 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
5787 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
5788 output_bfd
, irel
, hsec
, h
))
5795 /* Do the final link step. */
5798 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5800 bfd_size_type symesz
;
5801 struct xcoff_final_link_info flinfo
;
5803 struct bfd_link_order
*p
;
5804 bfd_size_type max_contents_size
;
5805 bfd_size_type max_sym_count
;
5806 bfd_size_type max_lineno_count
;
5807 bfd_size_type max_reloc_count
;
5808 bfd_size_type max_output_reloc_count
;
5809 file_ptr rel_filepos
;
5811 file_ptr line_filepos
;
5812 unsigned int linesz
;
5814 bfd_byte
*external_relocs
= NULL
;
5815 char strbuf
[STRING_SIZE_SIZE
];
5820 abfd
->flags
|= DYNAMIC
;
5822 symesz
= bfd_coff_symesz (abfd
);
5825 flinfo
.output_bfd
= abfd
;
5826 flinfo
.strtab
= NULL
;
5827 flinfo
.section_info
= NULL
;
5828 flinfo
.last_file_index
= -1;
5829 flinfo
.toc_symindx
= -1;
5830 flinfo
.internal_syms
= NULL
;
5831 flinfo
.sym_indices
= NULL
;
5832 flinfo
.outsyms
= NULL
;
5833 flinfo
.linenos
= NULL
;
5834 flinfo
.contents
= NULL
;
5835 flinfo
.external_relocs
= NULL
;
5837 if (xcoff_hash_table (info
)->loader_section
)
5839 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5840 + bfd_xcoff_ldhdrsz (abfd
));
5841 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5842 + bfd_xcoff_ldhdrsz (abfd
)
5843 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5844 * bfd_xcoff_ldsymsz (abfd
)));
5848 flinfo
.ldsym
= NULL
;
5849 flinfo
.ldrel
= NULL
;
5852 xcoff_data (abfd
)->coff
.link_info
= info
;
5854 flinfo
.strtab
= _bfd_stringtab_init ();
5855 if (flinfo
.strtab
== NULL
)
5858 /* Count the relocation entries required for the output file.
5859 (We've already counted the line numbers.) Determine a few
5861 max_contents_size
= 0;
5862 max_lineno_count
= 0;
5863 max_reloc_count
= 0;
5864 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5867 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5869 if (p
->type
== bfd_indirect_link_order
)
5873 sec
= p
->u
.indirect
.section
;
5875 /* Mark all sections which are to be included in the
5876 link. This will normally be every section. We need
5877 to do this so that we can identify any sections which
5878 the linker has decided to not include. */
5879 sec
->linker_mark
= TRUE
;
5881 o
->reloc_count
+= sec
->reloc_count
;
5883 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
5885 if (sec
->rawsize
> max_contents_size
)
5886 max_contents_size
= sec
->rawsize
;
5887 if (sec
->size
> max_contents_size
)
5888 max_contents_size
= sec
->size
;
5890 if (coff_section_data (sec
->owner
, sec
) != NULL
5891 && xcoff_section_data (sec
->owner
, sec
) != NULL
5892 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5893 > max_lineno_count
))
5895 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5896 if (sec
->reloc_count
> max_reloc_count
)
5897 max_reloc_count
= sec
->reloc_count
;
5899 else if (p
->type
== bfd_section_reloc_link_order
5900 || p
->type
== bfd_symbol_reloc_link_order
)
5905 /* Compute the file positions for all the sections. */
5906 if (abfd
->output_has_begun
)
5908 if (xcoff_hash_table (info
)->file_align
!= 0)
5915 file_align
= xcoff_hash_table (info
)->file_align
;
5916 if (file_align
!= 0)
5918 bfd_boolean saw_contents
;
5922 /* Insert .pad sections before every section which has
5923 contents and is loaded, if it is preceded by some other
5924 section which has contents and is loaded. */
5925 saw_contents
= TRUE
;
5926 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5928 if (strcmp (o
->name
, ".pad") == 0)
5929 saw_contents
= FALSE
;
5930 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5931 && (o
->flags
& SEC_LOAD
) != 0)
5934 saw_contents
= TRUE
;
5939 /* Create a pad section and place it before the section
5940 that needs padding. This requires unlinking and
5941 relinking the bfd's section list. */
5943 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
5945 n
->alignment_power
= 0;
5947 bfd_section_list_remove (abfd
, n
);
5948 bfd_section_list_insert_before (abfd
, o
, n
);
5949 saw_contents
= FALSE
;
5954 /* Reset the section indices after inserting the new
5957 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5960 o
->target_index
= indx
;
5962 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
5964 /* Work out appropriate sizes for the .pad sections to force
5965 each section to land on a page boundary. This bit of
5966 code knows what compute_section_file_positions is going
5968 sofar
= bfd_coff_filhsz (abfd
);
5969 sofar
+= bfd_coff_aoutsz (abfd
);
5970 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
5971 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5972 if ((bfd_xcoff_is_reloc_count_overflow
5973 (abfd
, (bfd_vma
) o
->reloc_count
))
5974 || (bfd_xcoff_is_lineno_count_overflow
5975 (abfd
, (bfd_vma
) o
->lineno_count
)))
5976 /* 64 does not overflow, need to check if 32 does */
5977 sofar
+= bfd_coff_scnhsz (abfd
);
5979 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5981 if (strcmp (o
->name
, ".pad") == 0)
5985 BFD_ASSERT (o
->size
== 0);
5986 pageoff
= sofar
& (file_align
- 1);
5989 o
->size
= file_align
- pageoff
;
5990 sofar
+= file_align
- pageoff
;
5991 o
->flags
|= SEC_HAS_CONTENTS
;
5996 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
5997 sofar
+= BFD_ALIGN (o
->size
,
5998 1 << o
->alignment_power
);
6003 if (! bfd_coff_compute_section_file_positions (abfd
))
6007 /* Allocate space for the pointers we need to keep for the relocs. */
6011 /* We use section_count + 1, rather than section_count, because
6012 the target_index fields are 1 based. */
6013 amt
= abfd
->section_count
+ 1;
6014 amt
*= sizeof (struct xcoff_link_section_info
);
6015 flinfo
.section_info
= bfd_malloc (amt
);
6016 if (flinfo
.section_info
== NULL
)
6018 for (i
= 0; i
<= abfd
->section_count
; i
++)
6020 flinfo
.section_info
[i
].relocs
= NULL
;
6021 flinfo
.section_info
[i
].rel_hashes
= NULL
;
6022 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6026 /* Set the file positions for the relocs. */
6027 rel_filepos
= obj_relocbase (abfd
);
6028 relsz
= bfd_coff_relsz (abfd
);
6029 max_output_reloc_count
= 0;
6030 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6032 if (o
->reloc_count
== 0)
6036 /* A stripped file has no relocs. However, we still
6037 allocate the buffers, so that later code doesn't have to
6038 worry about whether we are stripping or not. */
6039 if (info
->strip
== strip_all
)
6043 o
->flags
|= SEC_RELOC
;
6044 o
->rel_filepos
= rel_filepos
;
6045 rel_filepos
+= o
->reloc_count
* relsz
;
6048 /* We don't know the indices of global symbols until we have
6049 written out all the local symbols. For each section in
6050 the output file, we keep an array of pointers to hash
6051 table entries. Each entry in the array corresponds to a
6052 reloc. When we find a reloc against a global symbol, we
6053 set the corresponding entry in this array so that we can
6054 fix up the symbol index after we have written out all the
6057 Because of this problem, we also keep the relocs in
6058 memory until the end of the link. This wastes memory.
6059 We could backpatch the file later, I suppose, although it
6061 amt
= o
->reloc_count
;
6062 amt
*= sizeof (struct internal_reloc
);
6063 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6065 amt
= o
->reloc_count
;
6066 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6067 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6069 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
6070 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6073 if (o
->reloc_count
> max_output_reloc_count
)
6074 max_output_reloc_count
= o
->reloc_count
;
6078 /* We now know the size of the relocs, so we can determine the file
6079 positions of the line numbers. */
6080 line_filepos
= rel_filepos
;
6081 flinfo
.line_filepos
= line_filepos
;
6082 linesz
= bfd_coff_linesz (abfd
);
6083 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6085 if (o
->lineno_count
== 0)
6086 o
->line_filepos
= 0;
6089 o
->line_filepos
= line_filepos
;
6090 line_filepos
+= o
->lineno_count
* linesz
;
6093 /* Reset the reloc and lineno counts, so that we can use them to
6094 count the number of entries we have output so far. */
6096 o
->lineno_count
= 0;
6099 obj_sym_filepos (abfd
) = line_filepos
;
6101 /* Figure out the largest number of symbols in an input BFD. Take
6102 the opportunity to clear the output_has_begun fields of all the
6103 input BFD's. We want at least 6 symbols, since that is the
6104 number which xcoff_write_global_symbol may need. */
6106 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6110 sub
->output_has_begun
= FALSE
;
6111 sz
= obj_raw_syment_count (sub
);
6112 if (sz
> max_sym_count
)
6116 /* Allocate some buffers used while linking. */
6117 amt
= max_sym_count
* sizeof (struct internal_syment
);
6118 flinfo
.internal_syms
= bfd_malloc (amt
);
6120 amt
= max_sym_count
* sizeof (long);
6121 flinfo
.sym_indices
= bfd_malloc (amt
);
6123 amt
= (max_sym_count
+ 1) * symesz
;
6124 flinfo
.outsyms
= bfd_malloc (amt
);
6126 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6127 flinfo
.linenos
= bfd_malloc (amt
);
6129 amt
= max_contents_size
;
6130 flinfo
.contents
= bfd_malloc (amt
);
6132 amt
= max_reloc_count
* relsz
;
6133 flinfo
.external_relocs
= bfd_malloc (amt
);
6135 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6136 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6137 || flinfo
.outsyms
== NULL
6138 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
6139 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
6140 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6143 obj_raw_syment_count (abfd
) = 0;
6145 /* Find a TOC symbol, if we need one. */
6146 if (!xcoff_find_tc0 (abfd
, &flinfo
))
6149 /* We now know the position of everything in the file, except that
6150 we don't know the size of the symbol table and therefore we don't
6151 know where the string table starts. We just build the string
6152 table in memory as we go along. We process all the relocations
6153 for a single input file at once. */
6154 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6156 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6158 if (p
->type
== bfd_indirect_link_order
6159 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6161 sub
= p
->u
.indirect
.section
->owner
;
6162 if (! sub
->output_has_begun
)
6164 if (! xcoff_link_input_bfd (&flinfo
, sub
))
6166 sub
->output_has_begun
= TRUE
;
6169 else if (p
->type
== bfd_section_reloc_link_order
6170 || p
->type
== bfd_symbol_reloc_link_order
)
6172 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
6177 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6183 /* Free up the buffers used by xcoff_link_input_bfd. */
6184 if (flinfo
.internal_syms
!= NULL
)
6186 free (flinfo
.internal_syms
);
6187 flinfo
.internal_syms
= NULL
;
6189 if (flinfo
.sym_indices
!= NULL
)
6191 free (flinfo
.sym_indices
);
6192 flinfo
.sym_indices
= NULL
;
6194 if (flinfo
.linenos
!= NULL
)
6196 free (flinfo
.linenos
);
6197 flinfo
.linenos
= NULL
;
6199 if (flinfo
.contents
!= NULL
)
6201 free (flinfo
.contents
);
6202 flinfo
.contents
= NULL
;
6204 if (flinfo
.external_relocs
!= NULL
)
6206 free (flinfo
.external_relocs
);
6207 flinfo
.external_relocs
= NULL
;
6210 /* The value of the last C_FILE symbol is supposed to be -1. Write
6212 if (flinfo
.last_file_index
!= -1)
6214 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
6215 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
6216 (void *) flinfo
.outsyms
);
6217 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
6218 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6219 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
6223 /* Write out all the global symbols which do not come from XCOFF
6225 bfd_hash_traverse (&info
->hash
->table
, xcoff_write_global_symbol
, &flinfo
);
6227 if (flinfo
.outsyms
!= NULL
)
6229 free (flinfo
.outsyms
);
6230 flinfo
.outsyms
= NULL
;
6233 /* Now that we have written out all the global symbols, we know the
6234 symbol indices to use for relocs against them, and we can finally
6235 write out the relocs. */
6236 amt
= max_output_reloc_count
* relsz
;
6237 external_relocs
= bfd_malloc (amt
);
6238 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6241 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6243 struct internal_reloc
*irel
;
6244 struct internal_reloc
*irelend
;
6245 struct xcoff_link_hash_entry
**rel_hash
;
6246 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6248 bfd_size_type rel_size
;
6250 /* A stripped file has no relocs. */
6251 if (info
->strip
== strip_all
)
6257 if (o
->reloc_count
== 0)
6260 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6261 irelend
= irel
+ o
->reloc_count
;
6262 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
6263 for (; irel
< irelend
; irel
++, rel_hash
++)
6265 if (*rel_hash
!= NULL
)
6267 if ((*rel_hash
)->indx
< 0)
6269 if (! ((*info
->callbacks
->unattached_reloc
)
6270 (info
, (*rel_hash
)->root
.root
.string
,
6271 NULL
, o
, irel
->r_vaddr
)))
6273 (*rel_hash
)->indx
= 0;
6275 irel
->r_symndx
= (*rel_hash
)->indx
;
6279 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6280 toc_rel_hash
!= NULL
;
6281 toc_rel_hash
= toc_rel_hash
->next
)
6283 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6285 if (! ((*info
->callbacks
->unattached_reloc
)
6286 (info
, toc_rel_hash
->h
->root
.root
.string
,
6287 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
6289 toc_rel_hash
->h
->u
.toc_indx
= 0;
6291 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6294 /* XCOFF requires that the relocs be sorted by address. We tend
6295 to produce them in the order in which their containing csects
6296 appear in the symbol table, which is not necessarily by
6297 address. So we sort them here. There may be a better way to
6299 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
6300 o
->reloc_count
, sizeof (struct internal_reloc
),
6303 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6304 irelend
= irel
+ o
->reloc_count
;
6305 erel
= external_relocs
;
6306 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6307 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6309 rel_size
= relsz
* o
->reloc_count
;
6310 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6311 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6315 if (external_relocs
!= NULL
)
6317 free (external_relocs
);
6318 external_relocs
= NULL
;
6321 /* Free up the section information. */
6322 if (flinfo
.section_info
!= NULL
)
6326 for (i
= 0; i
< abfd
->section_count
; i
++)
6328 if (flinfo
.section_info
[i
].relocs
!= NULL
)
6329 free (flinfo
.section_info
[i
].relocs
);
6330 if (flinfo
.section_info
[i
].rel_hashes
!= NULL
)
6331 free (flinfo
.section_info
[i
].rel_hashes
);
6333 free (flinfo
.section_info
);
6334 flinfo
.section_info
= NULL
;
6337 /* Write out the loader section contents. */
6338 o
= xcoff_hash_table (info
)->loader_section
;
6341 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
6342 == (xcoff_hash_table (info
)->loader_section
->contents
6343 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6344 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6345 (file_ptr
) o
->output_offset
, o
->size
))
6349 /* Write out the magic sections. */
6350 o
= xcoff_hash_table (info
)->linkage_section
;
6352 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6353 (file_ptr
) o
->output_offset
,
6356 o
= xcoff_hash_table (info
)->toc_section
;
6358 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6359 (file_ptr
) o
->output_offset
,
6362 o
= xcoff_hash_table (info
)->descriptor_section
;
6364 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6365 (file_ptr
) o
->output_offset
,
6369 /* Write out the string table. */
6370 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6371 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6374 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
6376 amt
= STRING_SIZE_SIZE
;
6377 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6379 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
6382 _bfd_stringtab_free (flinfo
.strtab
);
6384 /* Write out the debugging string table. */
6385 o
= xcoff_hash_table (info
)->debug_section
;
6388 struct bfd_strtab_hash
*debug_strtab
;
6390 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6391 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6392 >= _bfd_stringtab_size (debug_strtab
));
6393 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6394 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6396 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6400 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6401 not try to write out the symbols. */
6402 bfd_get_symcount (abfd
) = 0;
6407 if (flinfo
.strtab
!= NULL
)
6408 _bfd_stringtab_free (flinfo
.strtab
);
6410 if (flinfo
.section_info
!= NULL
)
6414 for (i
= 0; i
< abfd
->section_count
; i
++)
6416 if (flinfo
.section_info
[i
].relocs
!= NULL
)
6417 free (flinfo
.section_info
[i
].relocs
);
6418 if (flinfo
.section_info
[i
].rel_hashes
!= NULL
)
6419 free (flinfo
.section_info
[i
].rel_hashes
);
6421 free (flinfo
.section_info
);
6424 if (flinfo
.internal_syms
!= NULL
)
6425 free (flinfo
.internal_syms
);
6426 if (flinfo
.sym_indices
!= NULL
)
6427 free (flinfo
.sym_indices
);
6428 if (flinfo
.outsyms
!= NULL
)
6429 free (flinfo
.outsyms
);
6430 if (flinfo
.linenos
!= NULL
)
6431 free (flinfo
.linenos
);
6432 if (flinfo
.contents
!= NULL
)
6433 free (flinfo
.contents
);
6434 if (flinfo
.external_relocs
!= NULL
)
6435 free (flinfo
.external_relocs
);
6436 if (external_relocs
!= NULL
)
6437 free (external_relocs
);