1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
31 #include "libiberty.h"
33 /* This file holds the XCOFF linker code. */
35 #undef STRING_SIZE_SIZE
36 #define STRING_SIZE_SIZE 4
38 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
39 This flag will only be used on input sections. */
41 #define SEC_MARK (SEC_ROM)
43 /* The list of import files. */
45 struct xcoff_import_file
47 /* The next entry in the list. */
48 struct xcoff_import_file
*next
;
53 /* The member name. */
57 /* Information we keep for each section in the output file during the
60 struct xcoff_link_section_info
62 /* The relocs to be output. */
63 struct internal_reloc
*relocs
;
64 /* For each reloc against a global symbol whose index was not known
65 when the reloc was handled, the global hash table entry. */
66 struct xcoff_link_hash_entry
**rel_hashes
;
67 /* If there is a TOC relative reloc against a global symbol, and the
68 index of the TOC symbol is not known when the reloc was handled,
69 an entry is added to this linked list. This is not an array,
70 like rel_hashes, because this case is quite uncommon. */
71 struct xcoff_toc_rel_hash
73 struct xcoff_toc_rel_hash
*next
;
74 struct xcoff_link_hash_entry
*h
;
75 struct internal_reloc
*rel
;
79 /* Information that the XCOFF linker collects about an archive. */
80 struct xcoff_archive_info
82 /* The archive described by this entry. */
85 /* The import path and import filename to use when referring to
86 this archive in the .loader section. */
90 /* True if the archive contains a dynamic object. */
91 unsigned int contains_shared_object_p
: 1;
93 /* True if the previous field is valid. */
94 unsigned int know_contains_shared_object_p
: 1;
97 struct xcoff_link_hash_table
99 struct bfd_link_hash_table root
;
101 /* The .debug string hash table. We need to compute this while
102 reading the input files, so that we know how large the .debug
103 section will be before we assign section positions. */
104 struct bfd_strtab_hash
*debug_strtab
;
106 /* The .debug section we will use for the final output. */
107 asection
*debug_section
;
109 /* The .loader section we will use for the final output. */
110 asection
*loader_section
;
112 /* A count of non TOC relative relocs which will need to be
113 allocated in the .loader section. */
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr
;
119 /* The .gl section we use to hold global linkage code. */
120 asection
*linkage_section
;
122 /* The .tc section we use to hold toc entries we build for global
124 asection
*toc_section
;
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection
*descriptor_section
;
130 /* The list of import files. */
131 struct xcoff_import_file
*imports
;
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align
;
136 /* Whether the .text section must be read-only. */
139 /* Whether -brtl was specified. */
142 /* Whether garbage collection was done. */
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
148 struct xcoff_link_size_list
*next
;
149 struct xcoff_link_hash_entry
*h
;
154 /* Information about archives. */
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection
*special_sections
[XCOFF_NUMBER_OF_SPECIAL_SECTIONS
];
161 /* Information that we pass around while doing the final link step. */
163 struct xcoff_final_link_info
165 /* General link information. */
166 struct bfd_link_info
*info
;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash
*strtab
;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info
*section_info
;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index
;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file
;
178 /* Symbol index of TOC symbol. */
180 /* Start of .loader symbols. */
182 /* Next .loader reloc to swap out. */
184 /* File position of start of line numbers. */
185 file_ptr line_filepos
;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment
*internal_syms
;
188 /* Buffer large enough to hold output indices of symbols of any
191 /* Buffer large enough to hold output symbols for any input file. */
193 /* Buffer large enough to hold external line numbers for any input
196 /* Buffer large enough to hold any input section. */
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte
*external_relocs
;
202 static bfd_boolean
xcoff_mark (struct bfd_link_info
*, asection
*);
206 /* Routines to read XCOFF dynamic information. This don't really
207 belong here, but we already have the ldsym manipulation routines
210 /* Read the contents of a section. */
213 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
215 if (coff_section_data (abfd
, sec
) == NULL
)
217 bfd_size_type amt
= sizeof (struct coff_section_tdata
);
219 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
220 if (sec
->used_by_bfd
== NULL
)
224 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
228 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
230 if (contents
!= NULL
)
234 coff_section_data (abfd
, sec
)->contents
= contents
;
240 /* Get the size required to hold the dynamic symbols. */
243 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
247 struct internal_ldhdr ldhdr
;
249 if ((abfd
->flags
& DYNAMIC
) == 0)
251 bfd_set_error (bfd_error_invalid_operation
);
255 lsec
= bfd_get_section_by_name (abfd
, ".loader");
258 bfd_set_error (bfd_error_no_symbols
);
262 if (! xcoff_get_section_contents (abfd
, lsec
))
264 contents
= coff_section_data (abfd
, lsec
)->contents
;
266 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
268 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
271 /* Get the dynamic symbols. */
274 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
278 struct internal_ldhdr ldhdr
;
280 bfd_byte
*elsym
, *elsymend
;
281 coff_symbol_type
*symbuf
;
283 if ((abfd
->flags
& DYNAMIC
) == 0)
285 bfd_set_error (bfd_error_invalid_operation
);
289 lsec
= bfd_get_section_by_name (abfd
, ".loader");
292 bfd_set_error (bfd_error_no_symbols
);
296 if (! xcoff_get_section_contents (abfd
, lsec
))
298 contents
= coff_section_data (abfd
, lsec
)->contents
;
300 coff_section_data (abfd
, lsec
)->keep_contents
= TRUE
;
302 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
304 strings
= (char *) contents
+ ldhdr
.l_stoff
;
306 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
310 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
312 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
313 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
315 struct internal_ldsym ldsym
;
317 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
319 symbuf
->symbol
.the_bfd
= abfd
;
321 if (ldsym
._l
._l_l
._l_zeroes
== 0)
322 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
327 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
330 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
332 symbuf
->symbol
.name
= c
;
335 if (ldsym
.l_smclas
== XMC_XO
)
336 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
338 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
340 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
342 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
343 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
345 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
346 symbuf
->symbol
.flags
|= BSF_WEAK
;
348 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
351 /* FIXME: We have no way to record the other information stored
352 with the loader symbol. */
353 *psyms
= (asymbol
*) symbuf
;
358 return ldhdr
.l_nsyms
;
361 /* Get the size required to hold the dynamic relocs. */
364 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
368 struct internal_ldhdr ldhdr
;
370 if ((abfd
->flags
& DYNAMIC
) == 0)
372 bfd_set_error (bfd_error_invalid_operation
);
376 lsec
= bfd_get_section_by_name (abfd
, ".loader");
379 bfd_set_error (bfd_error_no_symbols
);
383 if (! xcoff_get_section_contents (abfd
, lsec
))
385 contents
= coff_section_data (abfd
, lsec
)->contents
;
387 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
389 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
392 /* Get the dynamic relocs. */
395 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
401 struct internal_ldhdr ldhdr
;
403 bfd_byte
*elrel
, *elrelend
;
405 if ((abfd
->flags
& DYNAMIC
) == 0)
407 bfd_set_error (bfd_error_invalid_operation
);
411 lsec
= bfd_get_section_by_name (abfd
, ".loader");
414 bfd_set_error (bfd_error_no_symbols
);
418 if (! xcoff_get_section_contents (abfd
, lsec
))
420 contents
= coff_section_data (abfd
, lsec
)->contents
;
422 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
424 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
428 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
430 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
431 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
434 struct internal_ldrel ldrel
;
436 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
438 if (ldrel
.l_symndx
>= 3)
439 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
445 switch (ldrel
.l_symndx
)
461 sec
= bfd_get_section_by_name (abfd
, name
);
464 bfd_set_error (bfd_error_bad_value
);
468 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
471 relbuf
->address
= ldrel
.l_vaddr
;
474 /* Most dynamic relocs have the same type. FIXME: This is only
475 correct if ldrel.l_rtype == 0. In other cases, we should use
476 a different howto. */
477 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
479 /* FIXME: We have no way to record the l_rsecnm field. */
486 return ldhdr
.l_nreloc
;
489 /* Hash functions for xcoff_link_hash_table's archive_info. */
492 xcoff_archive_info_hash (const void *data
)
494 const struct xcoff_archive_info
*info
;
496 info
= (const struct xcoff_archive_info
*) data
;
497 return htab_hash_pointer (info
->archive
);
501 xcoff_archive_info_eq (const void *data1
, const void *data2
)
503 const struct xcoff_archive_info
*info1
;
504 const struct xcoff_archive_info
*info2
;
506 info1
= (const struct xcoff_archive_info
*) data1
;
507 info2
= (const struct xcoff_archive_info
*) data2
;
508 return info1
->archive
== info2
->archive
;
511 /* Return information about archive ARCHIVE. Return NULL on error. */
513 static struct xcoff_archive_info
*
514 xcoff_get_archive_info (struct bfd_link_info
*info
, bfd
*archive
)
516 struct xcoff_link_hash_table
*htab
;
517 struct xcoff_archive_info
*entryp
, entry
;
520 htab
= xcoff_hash_table (info
);
521 entry
.archive
= archive
;
522 slot
= htab_find_slot (htab
->archive_info
, &entry
, INSERT
);
529 entryp
= bfd_zalloc (archive
, sizeof (entry
));
533 entryp
->archive
= archive
;
539 /* Routine to create an entry in an XCOFF link hash table. */
541 static struct bfd_hash_entry
*
542 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
543 struct bfd_hash_table
*table
,
546 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
548 /* Allocate the structure if it has not already been allocated by a
551 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
555 /* Call the allocation method of the superclass. */
556 ret
= ((struct xcoff_link_hash_entry
*)
557 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
561 /* Set local fields. */
563 ret
->toc_section
= NULL
;
564 ret
->u
.toc_indx
= -1;
565 ret
->descriptor
= NULL
;
569 ret
->smclas
= XMC_UA
;
572 return (struct bfd_hash_entry
*) ret
;
575 /* Create a XCOFF link hash table. */
577 struct bfd_link_hash_table
*
578 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
580 struct xcoff_link_hash_table
*ret
;
581 bfd_size_type amt
= sizeof (* ret
);
583 ret
= bfd_malloc (amt
);
586 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
587 sizeof (struct xcoff_link_hash_entry
)))
593 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
594 ret
->debug_section
= NULL
;
595 ret
->loader_section
= NULL
;
596 ret
->ldrel_count
= 0;
597 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
598 ret
->linkage_section
= NULL
;
599 ret
->toc_section
= NULL
;
600 ret
->descriptor_section
= NULL
;
605 ret
->archive_info
= htab_create (37, xcoff_archive_info_hash
,
606 xcoff_archive_info_eq
, NULL
);
607 memset (ret
->special_sections
, 0, sizeof ret
->special_sections
);
609 /* The linker will always generate a full a.out header. We need to
610 record that fact now, before the sizeof_headers routine could be
612 xcoff_data (abfd
)->full_aouthdr
= TRUE
;
617 /* Free a XCOFF link hash table. */
620 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table
*hash
)
622 struct xcoff_link_hash_table
*ret
= (struct xcoff_link_hash_table
*) hash
;
624 _bfd_stringtab_free (ret
->debug_strtab
);
625 bfd_hash_table_free (&ret
->root
.table
);
629 /* Read internal relocs for an XCOFF csect. This is a wrapper around
630 _bfd_coff_read_internal_relocs which tries to take advantage of any
631 relocs which may have been cached for the enclosing section. */
633 static struct internal_reloc
*
634 xcoff_read_internal_relocs (bfd
*abfd
,
637 bfd_byte
*external_relocs
,
638 bfd_boolean require_internal
,
639 struct internal_reloc
*internal_relocs
)
641 if (coff_section_data (abfd
, sec
) != NULL
642 && coff_section_data (abfd
, sec
)->relocs
== NULL
643 && xcoff_section_data (abfd
, sec
) != NULL
)
647 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
649 if (enclosing
!= NULL
650 && (coff_section_data (abfd
, enclosing
) == NULL
651 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
653 && enclosing
->reloc_count
> 0)
655 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, TRUE
,
656 external_relocs
, FALSE
, NULL
)
661 if (enclosing
!= NULL
662 && coff_section_data (abfd
, enclosing
) != NULL
663 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
667 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
668 / bfd_coff_relsz (abfd
));
670 if (! require_internal
)
671 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
672 memcpy (internal_relocs
,
673 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
674 sec
->reloc_count
* sizeof (struct internal_reloc
));
675 return internal_relocs
;
679 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
680 require_internal
, internal_relocs
);
683 /* Split FILENAME into an import path and an import filename,
684 storing them in *IMPPATH and *IMPFILE respectively. */
687 bfd_xcoff_split_import_path (bfd
*abfd
, const char *filename
,
688 const char **imppath
, const char **impfile
)
690 const char *basename
;
694 basename
= lbasename (filename
);
695 length
= basename
- filename
;
697 /* The filename has no directory component, so use an empty path. */
699 else if (length
== 1)
700 /* The filename is in the root directory. */
704 /* Extract the (non-empty) directory part. Note that we don't
705 need to strip duplicate directory separators from any part
706 of the string; the native linker doesn't do that either. */
707 path
= bfd_alloc (abfd
, length
);
710 memcpy (path
, filename
, length
- 1);
711 path
[length
- 1] = 0;
718 /* Set ARCHIVE's import path as though its filename had been given
722 bfd_xcoff_set_archive_import_path (struct bfd_link_info
*info
,
723 bfd
*archive
, const char *filename
)
725 struct xcoff_archive_info
*archive_info
;
727 archive_info
= xcoff_get_archive_info (info
, archive
);
728 return (archive_info
!= NULL
729 && bfd_xcoff_split_import_path (archive
, filename
,
730 &archive_info
->imppath
,
731 &archive_info
->impfile
));
734 /* H is an imported symbol. Set the import module's path, file and member
735 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
736 no specific import module is specified. */
739 xcoff_set_import_path (struct bfd_link_info
*info
,
740 struct xcoff_link_hash_entry
*h
,
741 const char *imppath
, const char *impfile
,
742 const char *impmember
)
745 struct xcoff_import_file
**pp
;
747 /* We overload the ldindx field to hold the l_ifile value for this
749 BFD_ASSERT (h
->ldsym
== NULL
);
750 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
755 /* We start c at 1 because the first entry in the import list is
756 reserved for the library search path. */
757 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
759 pp
= &(*pp
)->next
, ++c
)
761 if (strcmp ((*pp
)->path
, imppath
) == 0
762 && strcmp ((*pp
)->file
, impfile
) == 0
763 && strcmp ((*pp
)->member
, impmember
) == 0)
769 struct xcoff_import_file
*n
;
770 bfd_size_type amt
= sizeof (* n
);
772 n
= bfd_alloc (info
->output_bfd
, amt
);
778 n
->member
= impmember
;
786 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
787 Return true if LDSYM defines H. */
790 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry
*h
,
791 struct internal_ldsym
*ldsym
)
793 /* If we didn't know about H before processing LDSYM, LDSYM
794 definitely defines H. */
795 if (h
->root
.type
== bfd_link_hash_new
)
798 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
799 dynamic symbol, LDSYM trumps the current definition of H. */
800 if ((ldsym
->l_smtype
& L_WEAK
) == 0
801 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
802 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
803 && (h
->root
.type
== bfd_link_hash_defweak
804 || h
->root
.type
== bfd_link_hash_undefweak
))
807 /* If H is currently undefined, LDSYM defines it. */
808 if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
809 && (h
->root
.type
== bfd_link_hash_undefined
810 || h
->root
.type
== bfd_link_hash_undefweak
))
816 /* This function is used to add symbols from a dynamic object to the
817 global symbol table. */
820 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
824 struct internal_ldhdr ldhdr
;
826 bfd_byte
*elsym
, *elsymend
;
827 struct xcoff_import_file
*n
;
829 struct xcoff_import_file
**pp
;
831 /* We can only handle a dynamic object if we are generating an XCOFF
833 if (info
->output_bfd
->xvec
!= abfd
->xvec
)
835 (*_bfd_error_handler
)
836 (_("%s: XCOFF shared object when not producing XCOFF output"),
837 bfd_get_filename (abfd
));
838 bfd_set_error (bfd_error_invalid_operation
);
842 /* The symbols we use from a dynamic object are not the symbols in
843 the normal symbol table, but, rather, the symbols in the export
844 table. If there is a global symbol in a dynamic object which is
845 not in the export table, the loader will not be able to find it,
846 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
847 libc.a has symbols in the export table which are not in the
850 /* Read in the .loader section. FIXME: We should really use the
851 o_snloader field in the a.out header, rather than grabbing the
853 lsec
= bfd_get_section_by_name (abfd
, ".loader");
856 (*_bfd_error_handler
)
857 (_("%s: dynamic object with no .loader section"),
858 bfd_get_filename (abfd
));
859 bfd_set_error (bfd_error_no_symbols
);
863 if (! xcoff_get_section_contents (abfd
, lsec
))
865 contents
= coff_section_data (abfd
, lsec
)->contents
;
867 /* Remove the sections from this object, so that they do not get
868 included in the link. */
869 bfd_section_list_clear (abfd
);
871 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
873 strings
= (char *) contents
+ ldhdr
.l_stoff
;
875 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
877 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
879 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
881 struct internal_ldsym ldsym
;
882 char nambuf
[SYMNMLEN
+ 1];
884 struct xcoff_link_hash_entry
*h
;
886 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
888 /* We are only interested in exported symbols. */
889 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
892 if (ldsym
._l
._l_l
._l_zeroes
== 0)
893 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
896 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
897 nambuf
[SYMNMLEN
] = '\0';
901 /* Normally we could not call xcoff_link_hash_lookup in an add
902 symbols routine, since we might not be using an XCOFF hash
903 table. However, we verified above that we are using an XCOFF
906 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
,
911 if (!xcoff_dynamic_definition_p (h
, &ldsym
))
914 h
->flags
|= XCOFF_DEF_DYNAMIC
;
915 h
->smclas
= ldsym
.l_smclas
;
916 if (h
->smclas
== XMC_XO
)
918 /* This symbol has an absolute value. */
919 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
920 h
->root
.type
= bfd_link_hash_defweak
;
922 h
->root
.type
= bfd_link_hash_defined
;
923 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
924 h
->root
.u
.def
.value
= ldsym
.l_value
;
928 /* Otherwise, we don't bother to actually define the symbol,
929 since we don't have a section to put it in anyhow.
930 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
931 should be imported from the symbol's undef.abfd. */
932 if ((ldsym
.l_smtype
& L_WEAK
) != 0)
933 h
->root
.type
= bfd_link_hash_undefweak
;
935 h
->root
.type
= bfd_link_hash_undefined
;
936 h
->root
.u
.undef
.abfd
= abfd
;
939 /* If this symbol defines a function descriptor, then it
940 implicitly defines the function code as well. */
941 if (h
->smclas
== XMC_DS
942 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
943 h
->flags
|= XCOFF_DESCRIPTOR
;
944 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
946 struct xcoff_link_hash_entry
*hds
;
953 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
957 strcpy (dsnm
+ 1, name
);
958 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
968 if (xcoff_dynamic_definition_p (hds
, &ldsym
))
970 hds
->root
.type
= h
->root
.type
;
971 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
972 if (h
->smclas
== XMC_XO
)
974 /* An absolute symbol appears to actually define code, not a
975 function descriptor. This is how some math functions are
976 implemented on AIX 4.1. */
977 hds
->smclas
= XMC_XO
;
978 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
979 hds
->root
.u
.def
.value
= ldsym
.l_value
;
983 hds
->smclas
= XMC_PR
;
984 hds
->root
.u
.undef
.abfd
= abfd
;
985 /* We do not want to add this to the undefined
992 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
994 free (coff_section_data (abfd
, lsec
)->contents
);
995 coff_section_data (abfd
, lsec
)->contents
= NULL
;
998 /* Record this file in the import files. */
999 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
1004 if (abfd
->my_archive
== NULL
)
1006 if (!bfd_xcoff_split_import_path (abfd
, abfd
->filename
,
1007 &n
->path
, &n
->file
))
1013 struct xcoff_archive_info
*archive_info
;
1015 archive_info
= xcoff_get_archive_info (info
, abfd
->my_archive
);
1016 if (!archive_info
->impfile
)
1018 if (!bfd_xcoff_split_import_path (archive_info
->archive
,
1019 archive_info
->archive
->filename
,
1020 &archive_info
->imppath
,
1021 &archive_info
->impfile
))
1024 n
->path
= archive_info
->imppath
;
1025 n
->file
= archive_info
->impfile
;
1026 n
->member
= bfd_get_filename (abfd
);
1029 /* We start c at 1 because the first import file number is reserved
1031 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1033 pp
= &(*pp
)->next
, ++c
)
1037 xcoff_data (abfd
)->import_file_id
= c
;
1042 /* xcoff_link_create_extra_sections
1044 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1047 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
1049 bfd_boolean return_value
= FALSE
;
1051 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1053 /* We need to build a .loader section, so we do it here. This
1054 won't work if we're producing an XCOFF output file with no
1055 XCOFF input files. FIXME. */
1057 if (!info
->relocatable
1058 && xcoff_hash_table (info
)->loader_section
== NULL
)
1061 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1063 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
1067 xcoff_hash_table (info
)->loader_section
= lsec
;
1070 /* Likewise for the linkage section. */
1071 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1074 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1077 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
1081 xcoff_hash_table (info
)->linkage_section
= lsec
;
1082 lsec
->alignment_power
= 2;
1085 /* Likewise for the TOC section. */
1086 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1089 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1092 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
1096 xcoff_hash_table (info
)->toc_section
= tsec
;
1097 tsec
->alignment_power
= 2;
1100 /* Likewise for the descriptor section. */
1101 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1104 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1107 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
1111 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1112 dsec
->alignment_power
= 2;
1115 /* Likewise for the .debug section. */
1116 if (xcoff_hash_table (info
)->debug_section
== NULL
1117 && info
->strip
!= strip_all
)
1120 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1122 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
1126 xcoff_hash_table (info
)->debug_section
= dsec
;
1130 return_value
= TRUE
;
1134 return return_value
;
1137 /* Returns the index of reloc in RELOCS with the least address greater
1138 than or equal to ADDRESS. The relocs are sorted by address. */
1140 static bfd_size_type
1141 xcoff_find_reloc (struct internal_reloc
*relocs
,
1142 bfd_size_type count
,
1145 bfd_size_type min
, max
, this;
1149 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1158 /* Do a binary search over (min,max]. */
1159 while (min
+ 1 < max
)
1163 this = (max
+ min
) / 2;
1164 raddr
= relocs
[this].r_vaddr
;
1165 if (raddr
> address
)
1167 else if (raddr
< address
)
1176 if (relocs
[min
].r_vaddr
< address
)
1180 && relocs
[min
- 1].r_vaddr
== address
)
1186 /* Add all the symbols from an object file to the hash table.
1188 XCOFF is a weird format. A normal XCOFF .o files will have three
1189 COFF sections--.text, .data, and .bss--but each COFF section will
1190 contain many csects. These csects are described in the symbol
1191 table. From the linker's point of view, each csect must be
1192 considered a section in its own right. For example, a TOC entry is
1193 handled as a small XMC_TC csect. The linker must be able to merge
1194 different TOC entries together, which means that it must be able to
1195 extract the XMC_TC csects from the .data section of the input .o
1198 From the point of view of our linker, this is, of course, a hideous
1199 nightmare. We cope by actually creating sections for each csect,
1200 and discarding the original sections. We then have to handle the
1201 relocation entries carefully, since the only way to tell which
1202 csect they belong to is to examine the address. */
1205 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1207 unsigned int n_tmask
;
1208 unsigned int n_btshft
;
1209 bfd_boolean default_copy
;
1210 bfd_size_type symcount
;
1211 struct xcoff_link_hash_entry
**sym_hash
;
1212 asection
**csect_cache
;
1213 unsigned int *lineno_counts
;
1214 bfd_size_type linesz
;
1216 asection
*last_real
;
1217 bfd_boolean keep_syms
;
1219 unsigned int csect_index
;
1220 asection
*first_csect
;
1221 bfd_size_type symesz
;
1224 struct reloc_info_struct
1226 struct internal_reloc
*relocs
;
1229 } *reloc_info
= NULL
;
1232 keep_syms
= obj_coff_keep_syms (abfd
);
1234 if ((abfd
->flags
& DYNAMIC
) != 0
1235 && ! info
->static_link
)
1237 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1241 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1242 if (! xcoff_link_create_extra_sections (abfd
, info
))
1245 if ((abfd
->flags
& DYNAMIC
) != 0
1246 && ! info
->static_link
)
1249 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1250 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1252 /* Define macros so that ISFCN, et. al., macros work correctly. */
1253 #define N_TMASK n_tmask
1254 #define N_BTSHFT n_btshft
1256 if (info
->keep_memory
)
1257 default_copy
= FALSE
;
1259 default_copy
= TRUE
;
1261 symcount
= obj_raw_syment_count (abfd
);
1263 /* We keep a list of the linker hash table entries that correspond
1264 to each external symbol. */
1265 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1266 sym_hash
= bfd_zalloc (abfd
, amt
);
1267 if (sym_hash
== NULL
&& symcount
!= 0)
1269 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1271 /* Because of the weird stuff we are doing with XCOFF csects, we can
1272 not easily determine which section a symbol is in, so we store
1273 the information in the tdata for the input file. */
1274 amt
= symcount
* sizeof (asection
*);
1275 csect_cache
= bfd_zalloc (abfd
, amt
);
1276 if (csect_cache
== NULL
&& symcount
!= 0)
1278 xcoff_data (abfd
)->csects
= csect_cache
;
1280 /* We garbage-collect line-number information on a symbol-by-symbol
1281 basis, so we need to have quick access to the number of entries
1283 amt
= symcount
* sizeof (unsigned int);
1284 lineno_counts
= bfd_zalloc (abfd
, amt
);
1285 if (lineno_counts
== NULL
&& symcount
!= 0)
1287 xcoff_data (abfd
)->lineno_counts
= lineno_counts
;
1289 /* While splitting sections into csects, we need to assign the
1290 relocs correctly. The relocs and the csects must both be in
1291 order by VMA within a given section, so we handle this by
1292 scanning along the relocs as we process the csects. We index
1293 into reloc_info using the section target_index. */
1294 amt
= abfd
->section_count
+ 1;
1295 amt
*= sizeof (struct reloc_info_struct
);
1296 reloc_info
= bfd_zmalloc (amt
);
1297 if (reloc_info
== NULL
)
1300 /* Read in the relocs and line numbers for each section. */
1301 linesz
= bfd_coff_linesz (abfd
);
1303 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1307 if ((o
->flags
& SEC_RELOC
) != 0)
1309 reloc_info
[o
->target_index
].relocs
=
1310 xcoff_read_internal_relocs (abfd
, o
, TRUE
, NULL
, FALSE
, NULL
);
1311 amt
= o
->reloc_count
;
1312 amt
*= sizeof (asection
*);
1313 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1314 if (reloc_info
[o
->target_index
].csects
== NULL
)
1318 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1319 && o
->lineno_count
> 0)
1323 amt
= linesz
* o
->lineno_count
;
1324 linenos
= bfd_malloc (amt
);
1325 if (linenos
== NULL
)
1327 reloc_info
[o
->target_index
].linenos
= linenos
;
1328 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1329 || bfd_bread (linenos
, amt
, abfd
) != amt
)
1334 /* Don't let the linker relocation routines discard the symbols. */
1335 obj_coff_keep_syms (abfd
) = TRUE
;
1341 symesz
= bfd_coff_symesz (abfd
);
1342 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1343 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1344 esym_end
= esym
+ symcount
* symesz
;
1346 while (esym
< esym_end
)
1348 struct internal_syment sym
;
1349 union internal_auxent aux
;
1351 char buf
[SYMNMLEN
+ 1];
1355 struct xcoff_link_hash_entry
*set_toc
;
1357 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1359 /* In this pass we are only interested in symbols with csect
1361 if (!CSECT_SYM_P (sym
.n_sclass
))
1364 Normally csect is a .pr, .rw etc. created in the loop
1365 If C_FILE or first time, handle special
1367 Advance esym, sym_hash, csect_hash ptrs. */
1368 if (sym
.n_sclass
== C_FILE
)
1371 *csect_cache
= csect
;
1372 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1373 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1375 *csect_cache
= NULL
;
1376 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1377 sym_hash
+= sym
.n_numaux
+ 1;
1378 csect_cache
+= sym
.n_numaux
+ 1;
1379 lineno_counts
+= sym
.n_numaux
+ 1;
1384 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1389 /* If this symbol has line number information attached to it,
1390 and we're not stripping it, count the number of entries and
1391 add them to the count for this csect. In the final link pass
1392 we are going to attach line number information by symbol,
1393 rather than by section, in order to more easily handle
1394 garbage collection. */
1395 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1398 && ISFCN (sym
.n_type
))
1400 union internal_auxent auxlin
;
1402 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1403 sym
.n_type
, sym
.n_sclass
,
1404 0, sym
.n_numaux
, (void *) &auxlin
);
1406 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1408 asection
*enclosing
;
1409 bfd_signed_vma linoff
;
1411 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1412 if (enclosing
== NULL
)
1414 (*_bfd_error_handler
)
1415 (_("%B: `%s' has line numbers but no enclosing section"),
1417 bfd_set_error (bfd_error_bad_value
);
1420 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1421 - enclosing
->line_filepos
);
1422 /* Explicit cast to bfd_signed_vma for compiler. */
1423 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1425 struct internal_lineno lin
;
1426 bfd_byte
*linpstart
;
1428 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1430 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1432 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1434 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1437 bfd_byte
*linpend
, *linp
;
1439 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1440 + enclosing
->lineno_count
* linesz
);
1441 for (linp
= linpstart
+ linesz
;
1445 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1447 if (lin
.l_lnno
== 0)
1450 *lineno_counts
= (linp
- linpstart
) / linesz
;
1451 /* The setting of line_filepos will only be
1452 useful if all the line number entries for a
1453 csect are contiguous; this only matters for
1455 if (csect
->line_filepos
== 0)
1456 csect
->line_filepos
=
1457 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1463 /* Pick up the csect auxiliary information. */
1464 if (sym
.n_numaux
== 0)
1466 (*_bfd_error_handler
)
1467 (_("%B: class %d symbol `%s' has no aux entries"),
1468 abfd
, sym
.n_sclass
, name
);
1469 bfd_set_error (bfd_error_bad_value
);
1473 bfd_coff_swap_aux_in (abfd
,
1474 (void *) (esym
+ symesz
* sym
.n_numaux
),
1475 sym
.n_type
, sym
.n_sclass
,
1476 sym
.n_numaux
- 1, sym
.n_numaux
,
1479 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1488 (*_bfd_error_handler
)
1489 (_("%B: symbol `%s' has unrecognized csect type %d"),
1491 bfd_set_error (bfd_error_bad_value
);
1495 /* This is an external reference. */
1496 if (sym
.n_sclass
== C_HIDEXT
1497 || sym
.n_scnum
!= N_UNDEF
1498 || aux
.x_csect
.x_scnlen
.l
!= 0)
1500 (*_bfd_error_handler
)
1501 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1502 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1503 aux
.x_csect
.x_scnlen
.l
);
1504 bfd_set_error (bfd_error_bad_value
);
1508 /* An XMC_XO external reference is actually a reference to
1509 an absolute location. */
1510 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1511 section
= bfd_und_section_ptr
;
1514 section
= bfd_abs_section_ptr
;
1515 value
= sym
.n_value
;
1521 csect_index
= -(unsigned) 1;
1523 /* When we see a TOC anchor, we record the TOC value. */
1524 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1526 if (sym
.n_sclass
!= C_HIDEXT
1527 || aux
.x_csect
.x_scnlen
.l
!= 0)
1529 (*_bfd_error_handler
)
1530 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1531 abfd
, name
, sym
.n_sclass
, aux
.x_csect
.x_scnlen
.l
);
1532 bfd_set_error (bfd_error_bad_value
);
1535 xcoff_data (abfd
)->toc
= sym
.n_value
;
1538 /* We must merge TOC entries for the same symbol. We can
1539 merge two TOC entries if they are both C_HIDEXT, they
1540 both have the same name, they are both 4 or 8 bytes long, and
1541 they both have a relocation table entry for an external
1542 symbol with the same name. Unfortunately, this means
1543 that we must look through the relocations. Ick.
1545 Logic for 32 bit vs 64 bit.
1546 32 bit has a csect length of 4 for TOC
1547 64 bit has a csect length of 8 for TOC
1549 The conditions to get past the if-check are not that bad.
1550 They are what is used to create the TOC csects in the first
1552 if (aux
.x_csect
.x_smclas
== XMC_TC
1553 && sym
.n_sclass
== C_HIDEXT
1554 && info
->output_bfd
->xvec
== abfd
->xvec
1555 && ((bfd_xcoff_is_xcoff32 (abfd
)
1556 && aux
.x_csect
.x_scnlen
.l
== 4)
1557 || (bfd_xcoff_is_xcoff64 (abfd
)
1558 && aux
.x_csect
.x_scnlen
.l
== 8)))
1560 asection
*enclosing
;
1561 struct internal_reloc
*relocs
;
1562 bfd_size_type relindx
;
1563 struct internal_reloc
*rel
;
1565 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1566 if (enclosing
== NULL
)
1569 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1570 amt
= enclosing
->reloc_count
;
1571 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1572 rel
= relocs
+ relindx
;
1574 /* 32 bit R_POS r_size is 31
1575 64 bit R_POS r_size is 63 */
1576 if (relindx
< enclosing
->reloc_count
1577 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1578 && rel
->r_type
== R_POS
1579 && ((bfd_xcoff_is_xcoff32 (abfd
)
1580 && rel
->r_size
== 31)
1581 || (bfd_xcoff_is_xcoff64 (abfd
)
1582 && rel
->r_size
== 63)))
1586 struct internal_syment relsym
;
1588 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1589 + rel
->r_symndx
* symesz
);
1590 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1591 if (EXTERN_SYM_P (relsym
.n_sclass
))
1593 const char *relname
;
1594 char relbuf
[SYMNMLEN
+ 1];
1596 struct xcoff_link_hash_entry
*h
;
1598 /* At this point we know that the TOC entry is
1599 for an externally visible symbol. */
1600 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1602 if (relname
== NULL
)
1605 /* We only merge TOC entries if the TC name is
1606 the same as the symbol name. This handles
1607 the normal case, but not common cases like
1608 SYM.P4 which gcc generates to store SYM + 4
1609 in the TOC. FIXME. */
1610 if (strcmp (name
, relname
) == 0)
1612 copy
= (! info
->keep_memory
1613 || relsym
._n
._n_n
._n_zeroes
!= 0
1614 || relsym
._n
._n_n
._n_offset
== 0);
1615 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1616 relname
, TRUE
, copy
,
1621 /* At this point h->root.type could be
1622 bfd_link_hash_new. That should be OK,
1623 since we know for sure that we will come
1624 across this symbol as we step through the
1627 /* We store h in *sym_hash for the
1628 convenience of the relocate_section
1632 if (h
->toc_section
!= NULL
)
1634 asection
**rel_csects
;
1636 /* We already have a TOC entry for this
1637 symbol, so we can just ignore this
1640 reloc_info
[enclosing
->target_index
].csects
;
1641 rel_csects
[relindx
] = bfd_und_section_ptr
;
1645 /* We are about to create a TOC entry for
1654 asection
*enclosing
;
1656 /* We need to create a new section. We get the name from
1657 the csect storage mapping class, so that the linker can
1658 accumulate similar csects together. */
1660 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1664 /* The enclosing section is the main section : .data, .text
1665 or .bss that the csect is coming from. */
1666 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1667 if (enclosing
== NULL
)
1670 if (! bfd_is_abs_section (enclosing
)
1671 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1672 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1673 > enclosing
->vma
+ enclosing
->size
)))
1675 (*_bfd_error_handler
)
1676 (_("%B: csect `%s' not in enclosing section"),
1678 bfd_set_error (bfd_error_bad_value
);
1681 csect
->vma
= sym
.n_value
;
1682 csect
->filepos
= (enclosing
->filepos
1685 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1686 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1687 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1689 /* Record the enclosing section in the tdata for this new
1691 amt
= sizeof (struct coff_section_tdata
);
1692 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1693 if (csect
->used_by_bfd
== NULL
)
1695 amt
= sizeof (struct xcoff_section_tdata
);
1696 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1697 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1699 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1700 xcoff_section_data (abfd
, csect
)->lineno_count
=
1701 enclosing
->lineno_count
;
1703 if (enclosing
->owner
== abfd
)
1705 struct internal_reloc
*relocs
;
1706 bfd_size_type relindx
;
1707 struct internal_reloc
*rel
;
1708 asection
**rel_csect
;
1710 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1711 amt
= enclosing
->reloc_count
;
1712 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1714 rel
= relocs
+ relindx
;
1715 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1718 csect
->rel_filepos
= (enclosing
->rel_filepos
1719 + relindx
* bfd_coff_relsz (abfd
));
1720 while (relindx
< enclosing
->reloc_count
1721 && *rel_csect
== NULL
1722 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1726 csect
->flags
|= SEC_RELOC
;
1727 ++csect
->reloc_count
;
1734 /* There are a number of other fields and section flags
1735 which we do not bother to set. */
1737 csect_index
= ((esym
1738 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1741 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1743 if (first_csect
== NULL
)
1744 first_csect
= csect
;
1746 /* If this symbol is external, we treat it as starting at the
1747 beginning of the newly created section. */
1748 if (EXTERN_SYM_P (sym
.n_sclass
))
1754 /* If this is a TOC section for a symbol, record it. */
1755 if (set_toc
!= NULL
)
1756 set_toc
->toc_section
= csect
;
1761 /* This is a label definition. The x_scnlen field is the
1762 symbol index of the csect. Usually the XTY_LD symbol will
1763 follow its appropriate XTY_SD symbol. The .set pseudo op can
1764 cause the XTY_LD to not follow the XTY_SD symbol. */
1769 if (aux
.x_csect
.x_scnlen
.l
< 0
1770 || (aux
.x_csect
.x_scnlen
.l
1771 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1775 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1777 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1782 (*_bfd_error_handler
)
1783 (_("%B: misplaced XTY_LD `%s'"),
1785 bfd_set_error (bfd_error_bad_value
);
1789 value
= sym
.n_value
- csect
->vma
;
1794 /* This is an unitialized csect. We could base the name on
1795 the storage mapping class, but we don't bother except for
1796 an XMC_TD symbol. If this csect is externally visible,
1797 it is a common symbol. We put XMC_TD symbols in sections
1798 named .tocbss, and rely on the linker script to put that
1801 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1803 /* The linker script puts the .td section in the data
1804 section after the .tc section. */
1805 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1809 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1814 csect
->vma
= sym
.n_value
;
1815 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1816 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1817 /* There are a number of other fields and section flags
1818 which we do not bother to set. */
1820 csect_index
= ((esym
1821 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1824 amt
= sizeof (struct coff_section_tdata
);
1825 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1826 if (csect
->used_by_bfd
== NULL
)
1828 amt
= sizeof (struct xcoff_section_tdata
);
1829 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1830 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1832 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1834 if (first_csect
== NULL
)
1835 first_csect
= csect
;
1837 if (EXTERN_SYM_P (sym
.n_sclass
))
1839 csect
->flags
|= SEC_IS_COMMON
;
1842 value
= aux
.x_csect
.x_scnlen
.l
;
1848 /* Check for magic symbol names. */
1849 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1850 && aux
.x_csect
.x_smclas
!= XMC_TC
1851 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1857 if (strcmp (name
, "_text") == 0)
1858 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1859 else if (strcmp (name
, "_etext") == 0)
1860 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1861 else if (strcmp (name
, "_data") == 0)
1862 i
= XCOFF_SPECIAL_SECTION_DATA
;
1863 else if (strcmp (name
, "_edata") == 0)
1864 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1865 else if (strcmp (name
, "_end") == 0)
1866 i
= XCOFF_SPECIAL_SECTION_END
;
1868 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1869 i
= XCOFF_SPECIAL_SECTION_END2
;
1872 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1875 /* Now we have enough information to add the symbol to the
1876 linker hash table. */
1878 if (EXTERN_SYM_P (sym
.n_sclass
))
1883 BFD_ASSERT (section
!= NULL
);
1885 /* We must copy the name into memory if we got it from the
1886 syment itself, rather than the string table. */
1887 copy
= default_copy
;
1888 if (sym
._n
._n_n
._n_zeroes
!= 0
1889 || sym
._n
._n_n
._n_offset
== 0)
1892 /* The AIX linker appears to only detect multiple symbol
1893 definitions when there is a reference to the symbol. If
1894 a symbol is defined multiple times, and the only
1895 references are from the same object file, the AIX linker
1896 appears to permit it. It does not merge the different
1897 definitions, but handles them independently. On the
1898 other hand, if there is a reference, the linker reports
1901 This matters because the AIX <net/net_globals.h> header
1902 file actually defines an initialized array, so we have to
1903 actually permit that to work.
1905 Just to make matters even more confusing, the AIX linker
1906 appears to permit multiple symbol definitions whenever
1907 the second definition is in an archive rather than an
1908 object file. This may be a consequence of the manner in
1909 which it handles archives: I think it may load the entire
1910 archive in as separate csects, and then let garbage
1911 collection discard symbols.
1913 We also have to handle the case of statically linking a
1914 shared object, which will cause symbol redefinitions,
1915 although this is an easier case to detect. */
1917 if (info
->output_bfd
->xvec
== abfd
->xvec
)
1919 if (! bfd_is_und_section (section
))
1920 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1921 name
, TRUE
, copy
, FALSE
);
1923 /* Make a copy of the symbol name to prevent problems with
1925 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1926 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1927 TRUE
, TRUE
, FALSE
));
1929 if (*sym_hash
== NULL
)
1931 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1932 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1933 && ! bfd_is_und_section (section
)
1934 && ! bfd_is_com_section (section
))
1936 /* This is a second definition of a defined symbol. */
1937 if ((abfd
->flags
& DYNAMIC
) != 0
1938 && ((*sym_hash
)->smclas
!= XMC_GL
1939 || aux
.x_csect
.x_smclas
== XMC_GL
1940 || ((*sym_hash
)->root
.u
.def
.section
->owner
->flags
1943 /* The new symbol is from a shared library, and
1944 either the existing symbol is not global
1945 linkage code or this symbol is global linkage
1946 code. If the existing symbol is global
1947 linkage code and the new symbol is not, then
1948 we want to use the new symbol. */
1949 section
= bfd_und_section_ptr
;
1952 else if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
1953 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
1955 /* The existing symbol is from a shared library.
1957 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1958 (*sym_hash
)->root
.u
.undef
.abfd
=
1959 (*sym_hash
)->root
.u
.def
.section
->owner
;
1961 else if (abfd
->my_archive
!= NULL
)
1963 /* This is a redefinition in an object contained
1964 in an archive. Just ignore it. See the
1966 section
= bfd_und_section_ptr
;
1969 else if (sym
.n_sclass
== C_AIX_WEAKEXT
1970 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1972 /* At least one of the definitions is weak.
1973 Allow the normal rules to take effect. */
1975 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1976 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1978 /* This symbol has been referenced. In this
1979 case, we just continue and permit the
1980 multiple definition error. See the comment
1981 above about the behaviour of the AIX linker. */
1983 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1985 /* The symbols are both csects of the same
1986 class. There is at least a chance that this
1987 is a semi-legitimate redefinition. */
1988 section
= bfd_und_section_ptr
;
1990 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
1993 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
1994 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
1995 && (bfd_is_und_section (section
)
1996 || bfd_is_com_section (section
)))
1998 /* This is a reference to a multiply defined symbol.
1999 Report the error now. See the comment above
2000 about the behaviour of the AIX linker. We could
2001 also do this with warning symbols, but I'm not
2002 sure the XCOFF linker is wholly prepared to
2003 handle them, and that would only be a warning,
2005 if (! ((*info
->callbacks
->multiple_definition
)
2006 (info
, (*sym_hash
)->root
.root
.string
,
2007 NULL
, NULL
, (bfd_vma
) 0,
2008 (*sym_hash
)->root
.u
.def
.section
->owner
,
2009 (*sym_hash
)->root
.u
.def
.section
,
2010 (*sym_hash
)->root
.u
.def
.value
)))
2012 /* Try not to give this error too many times. */
2013 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2017 /* _bfd_generic_link_add_one_symbol may call the linker to
2018 generate an error message, and the linker may try to read
2019 the symbol table to give a good error. Right now, the
2020 line numbers are in an inconsistent state, since they are
2021 counted both in the real sections and in the new csects.
2022 We need to leave the count in the real sections so that
2023 the linker can report the line number of the error
2024 correctly, so temporarily clobber the link to the csects
2025 so that the linker will not try to read the line numbers
2026 a second time from the csects. */
2027 BFD_ASSERT (last_real
->next
== first_csect
);
2028 last_real
->next
= NULL
;
2029 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2030 if (! (_bfd_generic_link_add_one_symbol
2031 (info
, abfd
, name
, flags
, section
, value
,
2033 (struct bfd_link_hash_entry
**) sym_hash
)))
2035 last_real
->next
= first_csect
;
2037 if (smtyp
== XTY_CM
)
2039 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2040 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2041 /* We don't need the common csect we just created. */
2044 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2045 = csect
->alignment_power
;
2048 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2052 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
2053 flag
= XCOFF_REF_REGULAR
;
2055 flag
= XCOFF_DEF_REGULAR
;
2056 (*sym_hash
)->flags
|= flag
;
2058 if ((*sym_hash
)->smclas
== XMC_UA
2059 || flag
== XCOFF_DEF_REGULAR
)
2060 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2064 if (smtyp
== XTY_ER
)
2065 *csect_cache
= section
;
2068 *csect_cache
= csect
;
2070 xcoff_section_data (abfd
, csect
)->last_symndx
2071 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2074 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2075 sym_hash
+= sym
.n_numaux
+ 1;
2076 csect_cache
+= sym
.n_numaux
+ 1;
2077 lineno_counts
+= sym
.n_numaux
+ 1;
2080 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2082 /* Make sure that we have seen all the relocs. */
2083 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2085 /* Reset the section size and the line number count, since the
2086 data is now attached to the csects. Don't reset the size of
2087 the .debug section, since we need to read it below in
2088 bfd_xcoff_size_dynamic_sections. */
2089 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
2091 o
->lineno_count
= 0;
2093 if ((o
->flags
& SEC_RELOC
) != 0)
2096 struct internal_reloc
*rel
;
2097 asection
**rel_csect
;
2099 rel
= reloc_info
[o
->target_index
].relocs
;
2100 rel_csect
= reloc_info
[o
->target_index
].csects
;
2102 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2104 if (*rel_csect
== NULL
)
2106 (*_bfd_error_handler
)
2107 (_("%B: reloc %s:%d not in csect"),
2109 bfd_set_error (bfd_error_bad_value
);
2113 /* We identify all function symbols that are the target
2114 of a relocation, so that we can create glue code for
2115 functions imported from dynamic objects. */
2116 if (info
->output_bfd
->xvec
== abfd
->xvec
2117 && *rel_csect
!= bfd_und_section_ptr
2118 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2120 struct xcoff_link_hash_entry
*h
;
2122 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2123 /* If the symbol name starts with a period, it is
2124 the code of a function. If the symbol is
2125 currently undefined, then add an undefined symbol
2126 for the function descriptor. This should do no
2127 harm, because any regular object that defines the
2128 function should also define the function
2129 descriptor. It helps, because it means that we
2130 will identify the function descriptor with a
2131 dynamic object if a dynamic object defines it. */
2132 if (h
->root
.root
.string
[0] == '.'
2133 && h
->descriptor
== NULL
)
2135 struct xcoff_link_hash_entry
*hds
;
2136 struct bfd_link_hash_entry
*bh
;
2138 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2139 h
->root
.root
.string
+ 1,
2143 if (hds
->root
.type
== bfd_link_hash_new
)
2146 if (! (_bfd_generic_link_add_one_symbol
2147 (info
, abfd
, hds
->root
.root
.string
,
2148 (flagword
) 0, bfd_und_section_ptr
,
2149 (bfd_vma
) 0, NULL
, FALSE
,
2152 hds
= (struct xcoff_link_hash_entry
*) bh
;
2154 hds
->flags
|= XCOFF_DESCRIPTOR
;
2155 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2156 hds
->descriptor
= h
;
2157 h
->descriptor
= hds
;
2159 if (h
->root
.root
.string
[0] == '.')
2160 h
->flags
|= XCOFF_CALLED
;
2164 free (reloc_info
[o
->target_index
].csects
);
2165 reloc_info
[o
->target_index
].csects
= NULL
;
2167 /* Reset SEC_RELOC and the reloc_count, since the reloc
2168 information is now attached to the csects. */
2169 o
->flags
&=~ SEC_RELOC
;
2172 /* If we are not keeping memory, free the reloc information. */
2173 if (! info
->keep_memory
2174 && coff_section_data (abfd
, o
) != NULL
2175 && coff_section_data (abfd
, o
)->relocs
!= NULL
2176 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2178 free (coff_section_data (abfd
, o
)->relocs
);
2179 coff_section_data (abfd
, o
)->relocs
= NULL
;
2183 /* Free up the line numbers. FIXME: We could cache these
2184 somewhere for the final link, to avoid reading them again. */
2185 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2187 free (reloc_info
[o
->target_index
].linenos
);
2188 reloc_info
[o
->target_index
].linenos
= NULL
;
2194 obj_coff_keep_syms (abfd
) = keep_syms
;
2199 if (reloc_info
!= NULL
)
2201 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2203 if (reloc_info
[o
->target_index
].csects
!= NULL
)
2204 free (reloc_info
[o
->target_index
].csects
);
2205 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2206 free (reloc_info
[o
->target_index
].linenos
);
2210 obj_coff_keep_syms (abfd
) = keep_syms
;
2217 /* Add symbols from an XCOFF object file. */
2220 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2222 if (! _bfd_coff_get_external_symbols (abfd
))
2224 if (! xcoff_link_add_symbols (abfd
, info
))
2226 if (! info
->keep_memory
)
2228 if (! _bfd_coff_free_symbols (abfd
))
2234 /* Look through the loader symbols to see if this dynamic object
2235 should be included in the link. The native linker uses the loader
2236 symbols, not the normal symbol table, so we do too. */
2239 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2240 struct bfd_link_info
*info
,
2241 bfd_boolean
*pneeded
)
2245 struct internal_ldhdr ldhdr
;
2246 const char *strings
;
2247 bfd_byte
*elsym
, *elsymend
;
2251 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2253 /* There are no symbols, so don't try to include it. */
2256 if (! xcoff_get_section_contents (abfd
, lsec
))
2258 contents
= coff_section_data (abfd
, lsec
)->contents
;
2260 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2262 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2264 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2266 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2267 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2269 struct internal_ldsym ldsym
;
2270 char nambuf
[SYMNMLEN
+ 1];
2272 struct bfd_link_hash_entry
*h
;
2274 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2276 /* We are only interested in exported symbols. */
2277 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2280 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2281 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2284 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2285 nambuf
[SYMNMLEN
] = '\0';
2289 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2291 /* We are only interested in symbols that are currently
2292 undefined. At this point we know that we are using an XCOFF
2295 && h
->type
== bfd_link_hash_undefined
2296 && (((struct xcoff_link_hash_entry
*) h
)->flags
2297 & XCOFF_DEF_DYNAMIC
) == 0)
2299 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
2306 /* We do not need this shared object. */
2307 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2309 free (coff_section_data (abfd
, lsec
)->contents
);
2310 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2316 /* Look through the symbols to see if this object file should be
2317 included in the link. */
2320 xcoff_link_check_ar_symbols (bfd
*abfd
,
2321 struct bfd_link_info
*info
,
2322 bfd_boolean
*pneeded
)
2324 bfd_size_type symesz
;
2330 if ((abfd
->flags
& DYNAMIC
) != 0
2331 && ! info
->static_link
2332 && info
->output_bfd
->xvec
== abfd
->xvec
)
2333 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
);
2335 symesz
= bfd_coff_symesz (abfd
);
2336 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2337 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2338 while (esym
< esym_end
)
2340 struct internal_syment sym
;
2342 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2344 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2347 char buf
[SYMNMLEN
+ 1];
2348 struct bfd_link_hash_entry
*h
;
2350 /* This symbol is externally visible, and is defined by this
2352 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2356 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2358 /* We are only interested in symbols that are currently
2359 undefined. If a symbol is currently known to be common,
2360 XCOFF linkers do not bring in an object file which
2361 defines it. We also don't bring in symbols to satisfy
2362 undefined references in shared objects. */
2364 && h
->type
== bfd_link_hash_undefined
2365 && (info
->output_bfd
->xvec
!= abfd
->xvec
2366 || (((struct xcoff_link_hash_entry
*) h
)->flags
2367 & XCOFF_DEF_DYNAMIC
) == 0))
2369 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
2376 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2379 /* We do not need this object file. */
2383 /* Check a single archive element to see if we need to include it in
2384 the link. *PNEEDED is set according to whether this element is
2385 needed in the link or not. This is called via
2386 _bfd_generic_link_add_archive_symbols. */
2389 xcoff_link_check_archive_element (bfd
*abfd
,
2390 struct bfd_link_info
*info
,
2391 bfd_boolean
*pneeded
)
2393 bfd_boolean keep_syms_p
;
2395 keep_syms_p
= (obj_coff_external_syms (abfd
) != NULL
);
2396 if (! _bfd_coff_get_external_symbols (abfd
))
2399 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
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 ((h
->flags
& XCOFF_CALLED
) != 0)
2746 /* This is a function symbol for which we need to create
2749 struct xcoff_link_hash_entry
*hds
;
2751 /* Mark the descriptor (and its TOC section). */
2752 hds
= h
->descriptor
;
2753 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2754 || hds
->root
.type
== bfd_link_hash_undefweak
)
2755 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2756 if (!xcoff_mark_symbol (info
, hds
))
2759 /* Treat this symbol as undefined if the descriptor was. */
2760 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2761 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2763 /* Allocate room for the global linkage code itself. */
2764 sec
= xcoff_hash_table (info
)->linkage_section
;
2765 h
->root
.type
= bfd_link_hash_defined
;
2766 h
->root
.u
.def
.section
= sec
;
2767 h
->root
.u
.def
.value
= sec
->size
;
2769 h
->flags
|= XCOFF_DEF_REGULAR
;
2770 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2772 /* The global linkage code requires a TOC entry for the
2774 if (hds
->toc_section
== NULL
)
2779 xcoff32 uses 4 bytes in the toc.
2780 xcoff64 uses 8 bytes in the toc. */
2781 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2783 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2788 /* Allocate room in the fallback TOC section. */
2789 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2790 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2791 hds
->toc_section
->size
+= byte_size
;
2792 if (!xcoff_mark (info
, hds
->toc_section
))
2795 /* Allocate room for a static and dynamic R_TOC
2797 ++xcoff_hash_table (info
)->ldrel_count
;
2798 ++hds
->toc_section
->reloc_count
;
2800 /* Set the index to -2 to force this symbol to
2803 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2806 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2808 /* Record that the symbol was undefined, then import it.
2809 -brtl links use a special fake import file. */
2810 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2811 if (xcoff_hash_table (info
)->rtld
)
2813 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2818 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2824 if (h
->root
.type
== bfd_link_hash_defined
2825 || h
->root
.type
== bfd_link_hash_defweak
)
2829 hsec
= h
->root
.u
.def
.section
;
2830 if (! bfd_is_abs_section (hsec
)
2831 && (hsec
->flags
& SEC_MARK
) == 0)
2833 if (! xcoff_mark (info
, hsec
))
2838 if (h
->toc_section
!= NULL
2839 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2841 if (! xcoff_mark (info
, h
->toc_section
))
2848 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2849 If the symbol exists, set FLAGS. */
2852 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2853 const char *name
, unsigned int flags
)
2855 struct xcoff_link_hash_entry
*h
;
2857 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2858 FALSE
, FALSE
, TRUE
);
2862 if (h
->root
.type
== bfd_link_hash_defined
2863 || h
->root
.type
== bfd_link_hash_defweak
)
2865 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2872 /* The mark phase of garbage collection. For a given section, mark
2873 it, and all the sections which define symbols to which it refers.
2874 Because this function needs to look at the relocs, we also count
2875 the number of relocs which need to be copied into the .loader
2879 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2881 if (bfd_is_abs_section (sec
)
2882 || (sec
->flags
& SEC_MARK
) != 0)
2885 sec
->flags
|= SEC_MARK
;
2887 if (sec
->owner
->xvec
== info
->output_bfd
->xvec
2888 && coff_section_data (sec
->owner
, sec
) != NULL
2889 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2891 struct xcoff_link_hash_entry
**syms
;
2892 struct internal_reloc
*rel
, *relend
;
2894 unsigned long i
, first
, last
;
2896 /* Mark all the symbols in this section. */
2897 syms
= obj_xcoff_sym_hashes (sec
->owner
);
2898 csects
= xcoff_data (sec
->owner
)->csects
;
2899 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
2900 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
2901 for (i
= first
; i
<= last
; i
++)
2902 if (csects
[i
] == sec
2904 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
2906 if (!xcoff_mark_symbol (info
, syms
[i
]))
2910 /* Look through the section relocs. */
2911 if ((sec
->flags
& SEC_RELOC
) != 0
2912 && sec
->reloc_count
> 0)
2914 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, TRUE
,
2918 relend
= rel
+ sec
->reloc_count
;
2919 for (; rel
< relend
; rel
++)
2921 struct xcoff_link_hash_entry
*h
;
2923 if ((unsigned int) rel
->r_symndx
2924 > obj_raw_syment_count (sec
->owner
))
2927 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2930 if ((h
->flags
& XCOFF_MARK
) == 0)
2932 if (!xcoff_mark_symbol (info
, h
))
2940 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2942 && (rsec
->flags
& SEC_MARK
) == 0)
2944 if (!xcoff_mark (info
, rsec
))
2949 /* See if this reloc needs to be copied into the .loader
2951 if (xcoff_need_ldrel_p (info
, rel
, h
))
2953 ++xcoff_hash_table (info
)->ldrel_count
;
2955 h
->flags
|= XCOFF_LDREL
;
2959 if (! info
->keep_memory
2960 && coff_section_data (sec
->owner
, sec
) != NULL
2961 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2962 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2964 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2965 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2973 /* Routines that are called after all the input files have been
2974 handled, but before the sections are laid out in memory. */
2976 /* The sweep phase of garbage collection. Remove all garbage
2980 xcoff_sweep (struct bfd_link_info
*info
)
2984 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2988 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2990 if ((o
->flags
& SEC_MARK
) == 0)
2992 /* Keep all sections from non-XCOFF input files. Keep
2993 special sections. Keep .debug sections for the
2995 if (sub
->xvec
!= info
->output_bfd
->xvec
2996 || o
== xcoff_hash_table (info
)->debug_section
2997 || o
== xcoff_hash_table (info
)->loader_section
2998 || o
== xcoff_hash_table (info
)->linkage_section
2999 || o
== xcoff_hash_table (info
)->descriptor_section
3000 || strcmp (o
->name
, ".debug") == 0)
3001 o
->flags
|= SEC_MARK
;
3012 /* Record the number of elements in a set. This is used to output the
3013 correct csect length. */
3016 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3017 struct bfd_link_info
*info
,
3018 struct bfd_link_hash_entry
*harg
,
3021 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3022 struct xcoff_link_size_list
*n
;
3025 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3028 /* This will hardly ever be called. I don't want to burn four bytes
3029 per global symbol, so instead the size is kept on a linked list
3030 attached to the hash table. */
3032 n
= bfd_alloc (output_bfd
, amt
);
3035 n
->next
= xcoff_hash_table (info
)->size_list
;
3038 xcoff_hash_table (info
)->size_list
= n
;
3040 h
->flags
|= XCOFF_HAS_SIZE
;
3045 /* Import a symbol. */
3048 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3049 struct bfd_link_info
*info
,
3050 struct bfd_link_hash_entry
*harg
,
3052 const char *imppath
,
3053 const char *impfile
,
3054 const char *impmember
,
3055 unsigned int syscall_flag
)
3057 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3059 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3062 /* A symbol name which starts with a period is the code for a
3063 function. If the symbol is undefined, then add an undefined
3064 symbol for the function descriptor, and import that instead. */
3065 if (h
->root
.root
.string
[0] == '.'
3066 && h
->root
.type
== bfd_link_hash_undefined
3067 && val
== (bfd_vma
) -1)
3069 struct xcoff_link_hash_entry
*hds
;
3071 hds
= h
->descriptor
;
3074 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3075 h
->root
.root
.string
+ 1,
3079 if (hds
->root
.type
== bfd_link_hash_new
)
3081 hds
->root
.type
= bfd_link_hash_undefined
;
3082 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3084 hds
->flags
|= XCOFF_DESCRIPTOR
;
3085 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3086 hds
->descriptor
= h
;
3087 h
->descriptor
= hds
;
3090 /* Now, if the descriptor is undefined, import the descriptor
3091 rather than the symbol we were told to import. FIXME: Is
3092 this correct in all cases? */
3093 if (hds
->root
.type
== bfd_link_hash_undefined
)
3097 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3099 if (val
!= (bfd_vma
) -1)
3101 if (h
->root
.type
== bfd_link_hash_defined
3102 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
3103 || h
->root
.u
.def
.value
!= val
))
3105 if (! ((*info
->callbacks
->multiple_definition
)
3106 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
3107 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
3108 output_bfd
, bfd_abs_section_ptr
, val
)))
3112 h
->root
.type
= bfd_link_hash_defined
;
3113 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3114 h
->root
.u
.def
.value
= val
;
3118 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3124 /* Export a symbol. */
3127 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3128 struct bfd_link_info
*info
,
3129 struct bfd_link_hash_entry
*harg
)
3131 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3133 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3136 h
->flags
|= XCOFF_EXPORT
;
3138 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3139 I'm just going to ignore it until somebody explains it. */
3141 /* Make sure we don't garbage collect this symbol. */
3142 if (! xcoff_mark_symbol (info
, h
))
3145 /* If this is a function descriptor, make sure we don't garbage
3146 collect the associated function code. We normally don't have to
3147 worry about this, because the descriptor will be attached to a
3148 section with relocs, but if we are creating the descriptor
3149 ourselves those relocs will not be visible to the mark code. */
3150 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3152 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3159 /* Count a reloc against a symbol. This is called for relocs
3160 generated by the linker script, typically for global constructors
3164 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3165 struct bfd_link_info
*info
,
3168 struct xcoff_link_hash_entry
*h
;
3170 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3173 h
= ((struct xcoff_link_hash_entry
*)
3174 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, FALSE
, FALSE
,
3178 (*_bfd_error_handler
) (_("%s: no such symbol"), name
);
3179 bfd_set_error (bfd_error_no_symbols
);
3183 h
->flags
|= XCOFF_REF_REGULAR
;
3184 if (xcoff_hash_table (info
)->loader_section
)
3186 h
->flags
|= XCOFF_LDREL
;
3187 ++xcoff_hash_table (info
)->ldrel_count
;
3190 /* Mark the symbol to avoid garbage collection. */
3191 if (! xcoff_mark_symbol (info
, h
))
3197 /* This function is called for each symbol to which the linker script
3201 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3202 struct bfd_link_info
*info
,
3205 struct xcoff_link_hash_entry
*h
;
3207 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3210 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
, TRUE
,
3215 h
->flags
|= XCOFF_DEF_REGULAR
;
3220 /* An xcoff_link_hash_traverse callback for which DATA points to an
3221 xcoff_loader_info. Mark all symbols that should be automatically
3225 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3227 struct xcoff_loader_info
*ldinfo
;
3229 ldinfo
= (struct xcoff_loader_info
*) data
;
3230 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3232 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3233 ldinfo
->failed
= TRUE
;
3238 /* Add a symbol to the .loader symbols, if necessary. */
3240 /* INPUT_BFD has an external symbol associated with hash table entry H
3241 and csect CSECT. Return true if INPUT_BFD defines H. */
3244 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3247 switch (h
->root
.type
)
3249 case bfd_link_hash_defined
:
3250 case bfd_link_hash_defweak
:
3251 /* No input bfd owns absolute symbols. They are written by
3252 xcoff_write_global_symbol instead. */
3253 return (!bfd_is_abs_section (csect
)
3254 && h
->root
.u
.def
.section
== csect
);
3256 case bfd_link_hash_common
:
3257 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3259 case bfd_link_hash_undefined
:
3260 case bfd_link_hash_undefweak
:
3261 /* We can't treat undef.abfd as the owner because that bfd
3262 might be a dynamic object. Allow any bfd to claim it. */
3270 /* See if H should have a loader symbol associated with it. */
3273 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3274 struct xcoff_link_hash_entry
*h
)
3278 /* Warn if this symbol is exported but not defined. */
3279 if ((h
->flags
& XCOFF_EXPORT
) != 0
3280 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3282 (*_bfd_error_handler
)
3283 (_("warning: attempt to export undefined symbol `%s'"),
3284 h
->root
.root
.string
);
3288 /* We need to add a symbol to the .loader section if it is mentioned
3289 in a reloc which we are copying to the .loader section and it was
3290 not defined or common, or if it is the entry point, or if it is
3292 if (((h
->flags
& XCOFF_LDREL
) == 0
3293 || h
->root
.type
== bfd_link_hash_defined
3294 || h
->root
.type
== bfd_link_hash_defweak
3295 || h
->root
.type
== bfd_link_hash_common
)
3296 && (h
->flags
& XCOFF_ENTRY
) == 0
3297 && (h
->flags
& XCOFF_EXPORT
) == 0)
3300 /* We need to add this symbol to the .loader symbols. */
3302 BFD_ASSERT (h
->ldsym
== NULL
);
3303 amt
= sizeof (struct internal_ldsym
);
3304 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3305 if (h
->ldsym
== NULL
)
3307 ldinfo
->failed
= TRUE
;
3311 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3313 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3314 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3316 h
->ldsym
->l_ifile
= h
->ldindx
;
3319 /* The first 3 symbol table indices are reserved to indicate the
3320 data, text and bss sections. */
3321 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3323 ++ldinfo
->ldsym_count
;
3325 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3326 h
->ldsym
, h
->root
.root
.string
))
3329 h
->flags
|= XCOFF_BUILT_LDSYM
;
3333 /* An xcoff_htab_traverse callback that is called for each symbol
3334 once garbage collection is complete. */
3337 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3339 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3341 if (h
->root
.type
== bfd_link_hash_warning
)
3342 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
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
->direction
= write_direction
;
3983 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
3986 /* need to reset to unknown or it will not be read back in correctly */
3987 abfd
->format
= bfd_unknown
;
3988 abfd
->direction
= read_direction
;
3994 /* Return the section that defines H. Return null if no section does. */
3997 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
3999 switch (h
->root
.type
)
4001 case bfd_link_hash_defined
:
4002 case bfd_link_hash_defweak
:
4003 return h
->root
.u
.def
.section
;
4005 case bfd_link_hash_common
:
4006 return h
->root
.u
.c
.p
->section
;
4013 /* Add a .loader relocation for input relocation IREL. If the loader
4014 relocation should be against an output section, HSEC points to the
4015 input section that IREL is against, otherwise HSEC is null. H is the
4016 symbol that IREL is against, or null if it isn't against a global symbol.
4017 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4020 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*finfo
,
4021 asection
*output_section
, bfd
*reference_bfd
,
4022 struct internal_reloc
*irel
, asection
*hsec
,
4023 struct xcoff_link_hash_entry
*h
)
4025 struct internal_ldrel ldrel
;
4027 ldrel
.l_vaddr
= irel
->r_vaddr
;
4030 const char *secname
;
4032 secname
= hsec
->output_section
->name
;
4033 if (strcmp (secname
, ".text") == 0)
4035 else if (strcmp (secname
, ".data") == 0)
4037 else if (strcmp (secname
, ".bss") == 0)
4041 (*_bfd_error_handler
)
4042 (_("%B: loader reloc in unrecognized section `%s'"),
4043 reference_bfd
, secname
);
4044 bfd_set_error (bfd_error_nonrepresentable_section
);
4052 (*_bfd_error_handler
)
4053 (_("%B: `%s' in loader reloc but not loader sym"),
4054 reference_bfd
, h
->root
.root
.string
);
4055 bfd_set_error (bfd_error_bad_value
);
4058 ldrel
.l_symndx
= h
->ldindx
;
4061 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4063 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4064 ldrel
.l_rsecnm
= output_section
->target_index
;
4065 if (xcoff_hash_table (finfo
->info
)->textro
4066 && strcmp (output_section
->name
, ".text") == 0)
4068 (*_bfd_error_handler
)
4069 (_("%B: loader reloc in read-only section %A"),
4070 reference_bfd
, output_section
);
4071 bfd_set_error (bfd_error_invalid_operation
);
4074 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
4075 finfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4079 /* Link an input file into the linker output file. This function
4080 handles all the sections and relocations of the input file at once. */
4083 xcoff_link_input_bfd (struct xcoff_final_link_info
*finfo
,
4087 const char *strings
;
4088 bfd_size_type syment_base
;
4089 unsigned int n_tmask
;
4090 unsigned int n_btshft
;
4091 bfd_boolean copy
, hash
;
4092 bfd_size_type isymesz
;
4093 bfd_size_type osymesz
;
4094 bfd_size_type linesz
;
4097 struct xcoff_link_hash_entry
**sym_hash
;
4098 struct internal_syment
*isymp
;
4100 unsigned int *lineno_counts
;
4103 unsigned long output_index
;
4107 bfd_boolean keep_syms
;
4110 /* We can just skip DYNAMIC files, unless this is a static link. */
4111 if ((input_bfd
->flags
& DYNAMIC
) != 0
4112 && ! finfo
->info
->static_link
)
4115 /* Move all the symbols to the output file. */
4116 output_bfd
= finfo
->output_bfd
;
4118 syment_base
= obj_raw_syment_count (output_bfd
);
4119 isymesz
= bfd_coff_symesz (input_bfd
);
4120 osymesz
= bfd_coff_symesz (output_bfd
);
4121 linesz
= bfd_coff_linesz (input_bfd
);
4122 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4124 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4125 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4127 /* Define macros so that ISFCN, et. al., macros work correctly. */
4128 #define N_TMASK n_tmask
4129 #define N_BTSHFT n_btshft
4132 if (! finfo
->info
->keep_memory
)
4135 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4138 if (! _bfd_coff_get_external_symbols (input_bfd
))
4141 /* Make one pass over the symbols and assign indices to symbols that
4142 we have decided to keep. Also use create .loader symbol information
4143 and update information in hash table entries. */
4144 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4145 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4146 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4147 csectpp
= xcoff_data (input_bfd
)->csects
;
4148 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4149 isymp
= finfo
->internal_syms
;
4150 indexp
= finfo
->sym_indices
;
4151 output_index
= syment_base
;
4152 while (esym
< esym_end
)
4154 union internal_auxent aux
;
4158 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4160 /* Read in the csect information, if any. */
4161 if (CSECT_SYM_P (isymp
->n_sclass
))
4163 BFD_ASSERT (isymp
->n_numaux
> 0);
4164 bfd_coff_swap_aux_in (input_bfd
,
4165 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4166 isymp
->n_type
, isymp
->n_sclass
,
4167 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4170 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4173 /* If this symbol is in the .loader section, swap out the
4174 .loader symbol information. If this is an external symbol
4175 reference to a defined symbol, though, then wait until we get
4176 to the definition. */
4177 if (EXTERN_SYM_P (isymp
->n_sclass
)
4178 && *sym_hash
!= NULL
4179 && (*sym_hash
)->ldsym
!= NULL
4180 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4182 struct xcoff_link_hash_entry
*h
;
4183 struct internal_ldsym
*ldsym
;
4187 if (isymp
->n_scnum
> 0)
4189 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4190 ldsym
->l_value
= (isymp
->n_value
4191 + (*csectpp
)->output_section
->vma
4192 + (*csectpp
)->output_offset
4197 ldsym
->l_scnum
= isymp
->n_scnum
;
4198 ldsym
->l_value
= isymp
->n_value
;
4201 ldsym
->l_smtype
= smtyp
;
4202 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4203 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4204 || (h
->flags
& XCOFF_IMPORT
) != 0)
4205 ldsym
->l_smtype
|= L_IMPORT
;
4206 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4207 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4208 || (h
->flags
& XCOFF_EXPORT
) != 0)
4209 ldsym
->l_smtype
|= L_EXPORT
;
4210 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4211 ldsym
->l_smtype
|= L_ENTRY
;
4212 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4213 ldsym
->l_smtype
|= L_WEAK
;
4215 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4217 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4219 else if (ldsym
->l_ifile
== 0)
4221 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4227 if (h
->root
.type
== bfd_link_hash_defined
4228 || h
->root
.type
== bfd_link_hash_defweak
)
4229 impbfd
= h
->root
.u
.def
.section
->owner
;
4230 else if (h
->root
.type
== bfd_link_hash_undefined
4231 || h
->root
.type
== bfd_link_hash_undefweak
)
4232 impbfd
= h
->root
.u
.undef
.abfd
;
4240 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
4241 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4248 BFD_ASSERT (h
->ldindx
>= 0);
4249 bfd_xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
4252 * bfd_xcoff_ldsymsz (finfo
->output_bfd
))));
4255 /* Fill in snentry now that we know the target_index. */
4256 if ((h
->flags
& XCOFF_ENTRY
) != 0
4257 && (h
->root
.type
== bfd_link_hash_defined
4258 || h
->root
.type
== bfd_link_hash_defweak
))
4260 xcoff_data (output_bfd
)->snentry
=
4261 h
->root
.u
.def
.section
->output_section
->target_index
;
4265 add
= 1 + isymp
->n_numaux
;
4267 if (*debug_index
== -2)
4268 /* We've decided to strip this symbol. */
4272 /* Assign the next unused index to this symbol. */
4273 *indexp
= output_index
;
4275 if (EXTERN_SYM_P (isymp
->n_sclass
))
4277 BFD_ASSERT (*sym_hash
!= NULL
);
4278 (*sym_hash
)->indx
= output_index
;
4281 /* If this is a symbol in the TOC which we may have merged
4282 (class XMC_TC), remember the symbol index of the TOC
4284 if (isymp
->n_sclass
== C_HIDEXT
4285 && aux
.x_csect
.x_smclas
== XMC_TC
4286 && *sym_hash
!= NULL
)
4288 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4289 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4290 (*sym_hash
)->u
.toc_indx
= output_index
;
4293 output_index
+= add
;
4296 esym
+= add
* isymesz
;
4302 for (--add
; add
> 0; --add
)
4306 /* Now write out the symbols that we decided to keep. */
4308 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4309 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4310 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4311 isymp
= finfo
->internal_syms
;
4312 indexp
= finfo
->sym_indices
;
4313 csectpp
= xcoff_data (input_bfd
)->csects
;
4314 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4315 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4316 outsym
= finfo
->outsyms
;
4319 while (esym
< esym_end
)
4323 add
= 1 + isymp
->n_numaux
;
4326 esym
+= add
* isymesz
;
4329 struct internal_syment isym
;
4332 /* Adjust the symbol in order to output it. */
4334 if (isym
._n
._n_n
._n_zeroes
== 0
4335 && isym
._n
._n_n
._n_offset
!= 0)
4337 /* This symbol has a long name. Enter it in the string
4338 table we are building. If *debug_index != -1, the
4339 name has already been entered in the .debug section. */
4340 if (*debug_index
>= 0)
4341 isym
._n
._n_n
._n_offset
= *debug_index
;
4347 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4351 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
4352 if (indx
== (bfd_size_type
) -1)
4354 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4358 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4359 multiple definition problems when linking a shared object
4360 statically. (The native linker doesn't enter __rtinit into
4361 the normal table at all, but having a local symbol can make
4362 the objdump output easier to read.) */
4363 if (isym
.n_sclass
== C_EXT
4365 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4366 isym
.n_sclass
= C_HIDEXT
;
4368 /* The value of a C_FILE symbol is the symbol index of the
4369 next C_FILE symbol. The value of the last C_FILE symbol
4370 is -1. We try to get this right, below, just before we
4371 write the symbols out, but in the general case we may
4372 have to write the symbol out twice. */
4373 if (isym
.n_sclass
== C_FILE
)
4375 if (finfo
->last_file_index
!= -1
4376 && finfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4378 /* We must correct the value of the last C_FILE entry. */
4379 finfo
->last_file
.n_value
= *indexp
;
4380 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4382 /* The last C_FILE symbol is in this input file. */
4383 bfd_coff_swap_sym_out (output_bfd
,
4384 (void *) &finfo
->last_file
,
4385 (void *) (finfo
->outsyms
4386 + ((finfo
->last_file_index
4392 /* We have already written out the last C_FILE
4393 symbol. We need to write it out again. We
4394 borrow *outsym temporarily. */
4397 bfd_coff_swap_sym_out (output_bfd
,
4398 (void *) &finfo
->last_file
,
4401 pos
= obj_sym_filepos (output_bfd
);
4402 pos
+= finfo
->last_file_index
* osymesz
;
4403 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4404 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4410 finfo
->last_file_index
= *indexp
;
4411 finfo
->last_file
= isym
;
4414 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4415 into the line numbers. We update the symbol values when
4416 we handle the line numbers. */
4417 if (isym
.n_sclass
== C_BINCL
4418 || isym
.n_sclass
== C_EINCL
)
4420 isym
.n_value
= finfo
->line_filepos
;
4423 /* The value of a C_BSTAT symbol is the symbol table
4424 index of the containing csect. */
4425 else if (isym
.n_sclass
== C_BSTAT
)
4429 indx
= isym
.n_value
;
4430 if (indx
< obj_raw_syment_count (input_bfd
))
4434 symindx
= finfo
->sym_indices
[indx
];
4438 isym
.n_value
= symindx
;
4441 else if (isym
.n_sclass
!= C_ESTAT
4442 && isym
.n_sclass
!= C_DECL
4443 && isym
.n_scnum
> 0)
4445 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4446 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4447 + (*csectpp
)->output_offset
4451 /* Output the symbol. */
4452 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4457 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4459 union internal_auxent aux
;
4461 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4462 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4465 if (isymp
->n_sclass
== C_FILE
)
4467 /* This is the file name (or some comment put in by
4468 the compiler). If it is long, we must put it in
4469 the string table. */
4470 if (aux
.x_file
.x_n
.x_zeroes
== 0
4471 && aux
.x_file
.x_n
.x_offset
!= 0)
4473 const char *filename
;
4476 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4477 >= STRING_SIZE_SIZE
);
4478 if (strings
== NULL
)
4480 strings
= _bfd_coff_read_string_table (input_bfd
);
4481 if (strings
== NULL
)
4484 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4485 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
4487 if (indx
== (bfd_size_type
) -1)
4489 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4492 else if (CSECT_SYM_P (isymp
->n_sclass
)
4493 && i
+ 1 == isymp
->n_numaux
)
4496 /* We don't support type checking. I don't know if
4498 aux
.x_csect
.x_parmhash
= 0;
4499 /* I don't think anybody uses these fields, but we'd
4500 better clobber them just in case. */
4501 aux
.x_csect
.x_stab
= 0;
4502 aux
.x_csect
.x_snstab
= 0;
4504 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4508 indx
= aux
.x_csect
.x_scnlen
.l
;
4509 if (indx
< obj_raw_syment_count (input_bfd
))
4513 symindx
= finfo
->sym_indices
[indx
];
4516 aux
.x_csect
.x_scnlen
.l
= 0;
4520 aux
.x_csect
.x_scnlen
.l
= symindx
;
4525 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4529 if (ISFCN (isymp
->n_type
)
4530 || ISTAG (isymp
->n_sclass
)
4531 || isymp
->n_sclass
== C_BLOCK
4532 || isymp
->n_sclass
== C_FCN
)
4534 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4536 && indx
< obj_raw_syment_count (input_bfd
))
4538 /* We look forward through the symbol for
4539 the index of the next symbol we are going
4540 to include. I don't know if this is
4542 while (finfo
->sym_indices
[indx
] < 0
4543 && indx
< obj_raw_syment_count (input_bfd
))
4545 if (indx
>= obj_raw_syment_count (input_bfd
))
4546 indx
= output_index
;
4548 indx
= finfo
->sym_indices
[indx
];
4549 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4554 indx
= aux
.x_sym
.x_tagndx
.l
;
4555 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4559 symindx
= finfo
->sym_indices
[indx
];
4561 aux
.x_sym
.x_tagndx
.l
= 0;
4563 aux
.x_sym
.x_tagndx
.l
= symindx
;
4568 /* Copy over the line numbers, unless we are stripping
4569 them. We do this on a symbol by symbol basis in
4570 order to more easily handle garbage collection. */
4571 if (CSECT_SYM_P (isymp
->n_sclass
)
4573 && isymp
->n_numaux
> 1
4574 && ISFCN (isymp
->n_type
)
4575 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4577 if (*lineno_counts
== 0)
4578 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4581 asection
*enclosing
;
4582 unsigned int enc_count
;
4583 bfd_signed_vma linoff
;
4584 struct internal_lineno lin
;
4591 /* Read in the enclosing section's line-number
4592 information, if we haven't already. */
4594 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4595 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4596 if (oline
!= enclosing
)
4598 pos
= enclosing
->line_filepos
;
4599 amt
= linesz
* enc_count
;
4600 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4601 || (bfd_bread (finfo
->linenos
, amt
, input_bfd
)
4607 /* Copy across the first entry, adjusting its
4609 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4610 - enclosing
->line_filepos
);
4611 linp
= finfo
->linenos
+ linoff
;
4612 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4613 lin
.l_addr
.l_symndx
= *indexp
;
4614 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4616 /* Copy the other entries, adjusting their addresses. */
4617 linpend
= linp
+ *lineno_counts
* linesz
;
4618 offset
= (o
->output_section
->vma
4621 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4623 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4624 lin
.l_addr
.l_paddr
+= offset
;
4625 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4628 /* Write out the entries we've just processed. */
4629 pos
= (o
->output_section
->line_filepos
4630 + o
->output_section
->lineno_count
* linesz
);
4631 amt
= linesz
* *lineno_counts
;
4632 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4633 || bfd_bwrite (finfo
->linenos
+ linoff
,
4634 amt
, output_bfd
) != amt
)
4636 o
->output_section
->lineno_count
+= *lineno_counts
;
4638 /* Record the offset of the symbol's line numbers
4639 in the output file. */
4640 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4644 struct internal_syment
*iisp
, *iispend
;
4647 bfd_vma range_start
, range_end
;
4650 /* Update any C_BINCL or C_EINCL symbols
4651 that refer to a line number in the
4652 range we just output. */
4653 iisp
= finfo
->internal_syms
;
4654 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4655 iindp
= finfo
->sym_indices
;
4656 oos
= finfo
->outsyms
;
4657 range_start
= enclosing
->line_filepos
+ linoff
;
4658 range_end
= range_start
+ *lineno_counts
* linesz
;
4659 while (iisp
< iispend
)
4662 && (iisp
->n_sclass
== C_BINCL
4663 || iisp
->n_sclass
== C_EINCL
)
4664 && iisp
->n_value
>= range_start
4665 && iisp
->n_value
< range_end
)
4667 struct internal_syment iis
;
4669 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4670 iis
.n_value
= (iisp
->n_value
4673 bfd_coff_swap_sym_out (output_bfd
,
4678 iiadd
= 1 + iisp
->n_numaux
;
4680 oos
+= iiadd
* osymesz
;
4688 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4689 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4700 lineno_counts
+= add
;
4704 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4705 symbol will be the first symbol in the next input file. In the
4706 normal case, this will save us from writing out the C_FILE symbol
4708 if (finfo
->last_file_index
!= -1
4709 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4711 finfo
->last_file
.n_value
= output_index
;
4712 bfd_coff_swap_sym_out (output_bfd
, (void *) &finfo
->last_file
,
4713 (void *) (finfo
->outsyms
4714 + ((finfo
->last_file_index
- syment_base
)
4718 /* Write the modified symbols to the output file. */
4719 if (outsym
> finfo
->outsyms
)
4721 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4722 bfd_size_type amt
= outsym
- finfo
->outsyms
;
4723 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4724 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
4727 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4728 + (outsym
- finfo
->outsyms
) / osymesz
)
4731 obj_raw_syment_count (output_bfd
) = output_index
;
4734 /* Don't let the linker relocation routines discard the symbols. */
4735 keep_syms
= obj_coff_keep_syms (input_bfd
);
4736 obj_coff_keep_syms (input_bfd
) = TRUE
;
4738 /* Relocate the contents of each section. */
4739 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4743 if (! o
->linker_mark
)
4744 /* This section was omitted from the link. */
4747 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4749 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4752 /* We have set filepos correctly for the sections we created to
4753 represent csects, so bfd_get_section_contents should work. */
4754 if (coff_section_data (input_bfd
, o
) != NULL
4755 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4756 contents
= coff_section_data (input_bfd
, o
)->contents
;
4759 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4760 if (!bfd_get_section_contents (input_bfd
, o
, finfo
->contents
, 0, sz
))
4762 contents
= finfo
->contents
;
4765 if ((o
->flags
& SEC_RELOC
) != 0)
4768 struct internal_reloc
*internal_relocs
;
4769 struct internal_reloc
*irel
;
4771 struct internal_reloc
*irelend
;
4772 struct xcoff_link_hash_entry
**rel_hash
;
4775 /* Read in the relocs. */
4776 target_index
= o
->output_section
->target_index
;
4777 internal_relocs
= (xcoff_read_internal_relocs
4778 (input_bfd
, o
, FALSE
, finfo
->external_relocs
,
4780 (finfo
->section_info
[target_index
].relocs
4781 + o
->output_section
->reloc_count
)));
4782 if (internal_relocs
== NULL
)
4785 /* Call processor specific code to relocate the section
4787 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
4791 finfo
->internal_syms
,
4792 xcoff_data (input_bfd
)->csects
))
4795 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4796 irel
= internal_relocs
;
4797 irelend
= irel
+ o
->reloc_count
;
4798 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
4799 + o
->output_section
->reloc_count
);
4800 for (; irel
< irelend
; irel
++, rel_hash
++)
4802 struct xcoff_link_hash_entry
*h
= NULL
;
4806 /* Adjust the reloc address and symbol index. */
4808 irel
->r_vaddr
+= offset
;
4810 r_symndx
= irel
->r_symndx
;
4815 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4817 if (r_symndx
!= -1 && finfo
->info
->strip
!= strip_all
)
4820 && h
->smclas
!= XMC_TD
4821 && (irel
->r_type
== R_TOC
4822 || irel
->r_type
== R_GL
4823 || irel
->r_type
== R_TCL
4824 || irel
->r_type
== R_TRL
4825 || irel
->r_type
== R_TRLA
))
4827 /* This is a TOC relative reloc with a symbol
4828 attached. The symbol should be the one which
4829 this reloc is for. We want to make this
4830 reloc against the TOC address of the symbol,
4831 not the symbol itself. */
4832 BFD_ASSERT (h
->toc_section
!= NULL
);
4833 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4834 if (h
->u
.toc_indx
!= -1)
4835 irel
->r_symndx
= h
->u
.toc_indx
;
4838 struct xcoff_toc_rel_hash
*n
;
4839 struct xcoff_link_section_info
*si
;
4843 n
= bfd_alloc (finfo
->output_bfd
, amt
);
4846 si
= finfo
->section_info
+ target_index
;
4847 n
->next
= si
->toc_rel_hashes
;
4850 si
->toc_rel_hashes
= n
;
4855 /* This is a global symbol. */
4857 irel
->r_symndx
= h
->indx
;
4860 /* This symbol is being written at the end
4861 of the file, and we do not yet know the
4862 symbol index. We save the pointer to the
4863 hash table entry in the rel_hash list.
4864 We set the indx field to -2 to indicate
4865 that this symbol must not be stripped. */
4874 indx
= finfo
->sym_indices
[r_symndx
];
4878 struct internal_syment
*is
;
4880 /* Relocations against a TC0 TOC anchor are
4881 automatically transformed to be against
4882 the TOC anchor in the output file. */
4883 is
= finfo
->internal_syms
+ r_symndx
;
4884 if (is
->n_sclass
== C_HIDEXT
4885 && is
->n_numaux
> 0)
4888 union internal_auxent aux
;
4892 obj_coff_external_syms (input_bfd
))
4893 + ((r_symndx
+ is
->n_numaux
)
4895 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4896 is
->n_type
, is
->n_sclass
,
4900 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4901 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4902 indx
= finfo
->toc_symindx
;
4907 irel
->r_symndx
= indx
;
4911 struct internal_syment
*is
;
4914 char buf
[SYMNMLEN
+ 1];
4916 /* This reloc is against a symbol we are
4917 stripping. It would be possible to handle
4918 this case, but I don't think it's worth it. */
4919 is
= finfo
->internal_syms
+ r_symndx
;
4921 name
= (_bfd_coff_internal_syment_name
4922 (input_bfd
, is
, buf
));
4927 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4928 (finfo
->info
, name
, input_bfd
, o
,
4935 if (xcoff_need_ldrel_p (finfo
->info
, irel
, h
))
4942 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4944 sec
= xcoff_symbol_section (h
);
4945 if (!xcoff_create_ldrel (output_bfd
, finfo
,
4946 o
->output_section
, input_bfd
,
4952 o
->output_section
->reloc_count
+= o
->reloc_count
;
4955 /* Write out the modified section contents. */
4956 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4957 contents
, (file_ptr
) o
->output_offset
,
4962 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4964 if (! finfo
->info
->keep_memory
)
4966 if (! _bfd_coff_free_symbols (input_bfd
))
4976 /* Sort relocs by VMA. This is called via qsort. */
4979 xcoff_sort_relocs (const void * p1
, const void * p2
)
4981 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
4982 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
4984 if (r1
->r_vaddr
> r2
->r_vaddr
)
4986 else if (r1
->r_vaddr
< r2
->r_vaddr
)
4992 /* Return true if section SEC is a TOC section. */
4994 static inline bfd_boolean
4995 xcoff_toc_section_p (asection
*sec
)
5000 if (name
[0] == '.' && name
[1] == 't')
5004 if (name
[3] == '0' && name
[4] == 0)
5009 if (name
[2] == 'd' && name
[3] == 0)
5015 /* See if the link requires a TOC (it usually does!). If so, find a
5016 good place to put the TOC anchor csect, and write out the associated
5020 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*finfo
)
5022 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5026 struct internal_syment irsym
;
5027 union internal_auxent iraux
;
5031 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5032 index of a csect at the beginning of the TOC. */
5033 toc_start
= ~(bfd_vma
) 0;
5036 for (input_bfd
= finfo
->info
->input_bfds
;
5038 input_bfd
= input_bfd
->link_next
)
5039 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5040 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5042 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5043 if (toc_start
> start
)
5046 section_index
= sec
->output_section
->target_index
;
5049 end
= start
+ sec
->size
;
5054 /* There's no need for a TC0 symbol if we don't have a TOC. */
5055 if (toc_end
< toc_start
)
5057 xcoff_data (output_bfd
)->toc
= toc_start
;
5061 if (toc_end
- toc_start
< 0x8000)
5062 /* Every TOC csect can be accessed from TOC_START. */
5063 best_address
= toc_start
;
5066 /* Find the lowest TOC csect that is still within range of TOC_END. */
5067 best_address
= toc_end
;
5068 for (input_bfd
= finfo
->info
->input_bfds
;
5070 input_bfd
= input_bfd
->link_next
)
5071 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5072 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5074 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5075 if (start
< best_address
5076 && start
+ 0x8000 >= toc_end
)
5078 best_address
= start
;
5079 section_index
= sec
->output_section
->target_index
;
5083 /* Make sure that the start of the TOC is also within range. */
5084 if (best_address
> toc_start
+ 0x8000)
5086 (*_bfd_error_handler
)
5087 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5089 (unsigned long) (toc_end
- toc_start
));
5090 bfd_set_error (bfd_error_file_too_big
);
5095 /* Record the chosen TOC value. */
5096 finfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5097 xcoff_data (output_bfd
)->toc
= best_address
;
5098 xcoff_data (output_bfd
)->sntoc
= section_index
;
5100 /* Fill out the TC0 symbol. */
5101 if (!bfd_xcoff_put_symbol_name (output_bfd
, finfo
->strtab
, &irsym
, "TOC"))
5103 irsym
.n_value
= best_address
;
5104 irsym
.n_scnum
= section_index
;
5105 irsym
.n_sclass
= C_HIDEXT
;
5106 irsym
.n_type
= T_NULL
;
5108 bfd_coff_swap_sym_out (output_bfd
, &irsym
, finfo
->outsyms
);
5110 /* Fill out the auxillary csect information. */
5111 memset (&iraux
, 0, sizeof iraux
);
5112 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5113 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5114 iraux
.x_csect
.x_scnlen
.l
= 0;
5115 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5116 finfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5118 /* Write the contents to the file. */
5119 pos
= obj_sym_filepos (output_bfd
);
5120 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5121 size
= 2 * bfd_coff_symesz (output_bfd
);
5122 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5123 || bfd_bwrite (finfo
->outsyms
, size
, output_bfd
) != size
)
5125 obj_raw_syment_count (output_bfd
) += 2;
5130 /* Write out a non-XCOFF global symbol. */
5133 xcoff_write_global_symbol (struct xcoff_link_hash_entry
*h
, void * inf
)
5135 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) inf
;
5138 struct internal_syment isym
;
5139 union internal_auxent aux
;
5144 output_bfd
= finfo
->output_bfd
;
5145 outsym
= finfo
->outsyms
;
5147 if (h
->root
.type
== bfd_link_hash_warning
)
5149 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5150 if (h
->root
.type
== bfd_link_hash_new
)
5154 /* If this symbol was garbage collected, just skip it. */
5155 if (xcoff_hash_table (finfo
->info
)->gc
5156 && (h
->flags
& XCOFF_MARK
) == 0)
5159 /* If we need a .loader section entry, write it out. */
5160 if (h
->ldsym
!= NULL
)
5162 struct internal_ldsym
*ldsym
;
5167 if (h
->root
.type
== bfd_link_hash_undefined
5168 || h
->root
.type
== bfd_link_hash_undefweak
)
5172 ldsym
->l_scnum
= N_UNDEF
;
5173 ldsym
->l_smtype
= XTY_ER
;
5174 impbfd
= h
->root
.u
.undef
.abfd
;
5177 else if (h
->root
.type
== bfd_link_hash_defined
5178 || h
->root
.type
== bfd_link_hash_defweak
)
5182 sec
= h
->root
.u
.def
.section
;
5183 ldsym
->l_value
= (sec
->output_section
->vma
5184 + sec
->output_offset
5185 + h
->root
.u
.def
.value
);
5186 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5187 ldsym
->l_smtype
= XTY_SD
;
5188 impbfd
= sec
->owner
;
5194 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5195 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5196 || (h
->flags
& XCOFF_IMPORT
) != 0)
5198 Import symbols are defined so the check above will make
5199 the l_smtype XTY_SD. But this is not correct, it should
5201 ldsym
->l_smtype
|= L_IMPORT
;
5203 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5204 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5205 || (h
->flags
& XCOFF_EXPORT
) != 0)
5206 ldsym
->l_smtype
|= L_EXPORT
;
5208 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5209 ldsym
->l_smtype
|= L_ENTRY
;
5211 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5212 ldsym
->l_smtype
= XTY_SD
;
5214 ldsym
->l_smclas
= h
->smclas
;
5216 if (ldsym
->l_smtype
& L_IMPORT
)
5218 if ((h
->root
.type
== bfd_link_hash_defined
5219 || h
->root
.type
== bfd_link_hash_defweak
)
5220 && (h
->root
.u
.def
.value
!= 0))
5221 ldsym
->l_smclas
= XMC_XO
;
5223 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5224 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5225 ldsym
->l_smclas
= XMC_SV3264
;
5227 else if (h
->flags
& XCOFF_SYSCALL32
)
5228 ldsym
->l_smclas
= XMC_SV
;
5230 else if (h
->flags
& XCOFF_SYSCALL64
)
5231 ldsym
->l_smclas
= XMC_SV64
;
5234 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5238 else if (ldsym
->l_ifile
== 0)
5240 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5242 else if (impbfd
== NULL
)
5246 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5247 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5253 BFD_ASSERT (h
->ldindx
>= 0);
5255 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5258 * bfd_xcoff_ldsymsz(finfo
->output_bfd
)));
5262 /* If this symbol needs global linkage code, write it out. */
5263 if (h
->root
.type
== bfd_link_hash_defined
5264 && (h
->root
.u
.def
.section
5265 == xcoff_hash_table (finfo
->info
)->linkage_section
))
5271 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5273 /* The first instruction in the global linkage code loads a
5274 specific TOC element. */
5275 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5276 + h
->descriptor
->toc_section
->output_offset
5277 - xcoff_data (output_bfd
)->toc
);
5279 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5280 tocoff
+= h
->descriptor
->u
.toc_offset
;
5282 /* The first instruction in the glink code needs to be
5283 cooked to to hold the correct offset in the toc. The
5284 rest are just output raw. */
5285 bfd_put_32 (output_bfd
,
5286 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5288 /* Start with i == 1 to get past the first instruction done above
5289 The /4 is because the glink code is in bytes and we are going
5291 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5292 bfd_put_32 (output_bfd
,
5293 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5297 /* If we created a TOC entry for this symbol, write out the required
5299 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5304 struct internal_reloc
*irel
;
5305 struct internal_syment irsym
;
5306 union internal_auxent iraux
;
5308 tocsec
= h
->toc_section
;
5309 osec
= tocsec
->output_section
;
5310 oindx
= osec
->target_index
;
5311 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5312 irel
->r_vaddr
= (osec
->vma
5313 + tocsec
->output_offset
5317 irel
->r_symndx
= h
->indx
;
5321 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5324 BFD_ASSERT (h
->ldindx
>= 0);
5326 /* Initialize the aux union here instead of closer to when it is
5327 written out below because the length of the csect depends on
5328 whether the output is 32 or 64 bit. */
5329 memset (&iraux
, 0, sizeof iraux
);
5330 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5331 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5332 iraux
.x_csect
.x_smclas
= XMC_TC
;
5334 /* 32 bit uses a 32 bit R_POS to do the relocations
5335 64 bit uses a 64 bit R_POS to do the relocations
5337 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5339 Which one is determined by the backend. */
5340 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5343 iraux
.x_csect
.x_scnlen
.l
= 8;
5345 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5348 iraux
.x_csect
.x_scnlen
.l
= 4;
5353 irel
->r_type
= R_POS
;
5354 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5355 ++osec
->reloc_count
;
5357 if (!xcoff_create_ldrel (output_bfd
, finfo
, osec
,
5358 output_bfd
, irel
, NULL
, h
))
5361 /* We need to emit a symbol to define a csect which holds
5363 if (finfo
->info
->strip
!= strip_all
)
5365 result
= bfd_xcoff_put_symbol_name (output_bfd
, finfo
->strtab
,
5366 &irsym
, h
->root
.root
.string
);
5370 irsym
.n_value
= irel
->r_vaddr
;
5371 irsym
.n_scnum
= osec
->target_index
;
5372 irsym
.n_sclass
= C_HIDEXT
;
5373 irsym
.n_type
= T_NULL
;
5376 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5377 outsym
+= bfd_coff_symesz (output_bfd
);
5379 /* Note : iraux is initialized above. */
5380 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5381 0, 1, (void *) outsym
);
5382 outsym
+= bfd_coff_auxesz (output_bfd
);
5386 /* We aren't going to write out the symbols below, so we
5387 need to write them out now. */
5388 pos
= obj_sym_filepos (output_bfd
);
5389 pos
+= (obj_raw_syment_count (output_bfd
)
5390 * bfd_coff_symesz (output_bfd
));
5391 amt
= outsym
- finfo
->outsyms
;
5392 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5393 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
5395 obj_raw_syment_count (output_bfd
) +=
5396 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5398 outsym
= finfo
->outsyms
;
5403 /* If this symbol is a specially defined function descriptor, write
5404 it out. The first word is the address of the function code
5405 itself, the second word is the address of the TOC, and the third
5409 The addresses for the 32 bit will take 4 bytes and the addresses
5410 for 64 bit will take 8 bytes. Similar for the relocs. This type
5411 of logic was also done above to create a TOC entry in
5412 xcoff_write_global_symbol. */
5413 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5414 && h
->root
.type
== bfd_link_hash_defined
5415 && (h
->root
.u
.def
.section
5416 == xcoff_hash_table (finfo
->info
)->descriptor_section
))
5422 struct xcoff_link_hash_entry
*hentry
;
5424 struct internal_reloc
*irel
;
5426 unsigned int reloc_size
, byte_size
;
5428 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5433 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5441 sec
= h
->root
.u
.def
.section
;
5442 osec
= sec
->output_section
;
5443 oindx
= osec
->target_index
;
5444 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5446 hentry
= h
->descriptor
;
5447 BFD_ASSERT (hentry
!= NULL
5448 && (hentry
->root
.type
== bfd_link_hash_defined
5449 || hentry
->root
.type
== bfd_link_hash_defweak
));
5450 esec
= hentry
->root
.u
.def
.section
;
5452 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5453 irel
->r_vaddr
= (osec
->vma
5454 + sec
->output_offset
5455 + h
->root
.u
.def
.value
);
5456 irel
->r_symndx
= esec
->output_section
->target_index
;
5457 irel
->r_type
= R_POS
;
5458 irel
->r_size
= reloc_size
;
5459 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5460 ++osec
->reloc_count
;
5462 if (!xcoff_create_ldrel (output_bfd
, finfo
, osec
,
5463 output_bfd
, irel
, esec
, NULL
))
5466 /* There are three items to write out,
5467 the address of the code
5468 the address of the toc anchor
5469 the environment pointer.
5470 We are ignoring the environment pointer. So set it to zero. */
5471 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5473 bfd_put_64 (output_bfd
,
5474 (esec
->output_section
->vma
+ esec
->output_offset
5475 + hentry
->root
.u
.def
.value
),
5477 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5478 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5483 This logic was already called above so the error case where
5484 the backend is neither has already been checked. */
5485 bfd_put_32 (output_bfd
,
5486 (esec
->output_section
->vma
+ esec
->output_offset
5487 + hentry
->root
.u
.def
.value
),
5489 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5490 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5493 tsec
= coff_section_from_bfd_index (output_bfd
,
5494 xcoff_data (output_bfd
)->sntoc
);
5497 irel
->r_vaddr
= (osec
->vma
5498 + sec
->output_offset
5499 + h
->root
.u
.def
.value
5501 irel
->r_symndx
= tsec
->output_section
->target_index
;
5502 irel
->r_type
= R_POS
;
5503 irel
->r_size
= reloc_size
;
5504 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5505 ++osec
->reloc_count
;
5507 if (!xcoff_create_ldrel (output_bfd
, finfo
, osec
,
5508 output_bfd
, irel
, tsec
, NULL
))
5512 if (h
->indx
>= 0 || finfo
->info
->strip
== strip_all
)
5514 BFD_ASSERT (outsym
== finfo
->outsyms
);
5519 && (finfo
->info
->strip
== strip_all
5520 || (finfo
->info
->strip
== strip_some
5521 && bfd_hash_lookup (finfo
->info
->keep_hash
, h
->root
.root
.string
,
5522 FALSE
, FALSE
) == NULL
)))
5524 BFD_ASSERT (outsym
== finfo
->outsyms
);
5529 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5531 BFD_ASSERT (outsym
== finfo
->outsyms
);
5535 memset (&aux
, 0, sizeof aux
);
5537 h
->indx
= obj_raw_syment_count (output_bfd
);
5539 result
= bfd_xcoff_put_symbol_name (output_bfd
, finfo
->strtab
, &isym
,
5540 h
->root
.root
.string
);
5544 if (h
->root
.type
== bfd_link_hash_undefined
5545 || h
->root
.type
== bfd_link_hash_undefweak
)
5548 isym
.n_scnum
= N_UNDEF
;
5549 if (h
->root
.type
== bfd_link_hash_undefweak
5550 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5551 isym
.n_sclass
= C_WEAKEXT
;
5553 isym
.n_sclass
= C_EXT
;
5554 aux
.x_csect
.x_smtyp
= XTY_ER
;
5556 else if ((h
->root
.type
== bfd_link_hash_defined
5557 || h
->root
.type
== bfd_link_hash_defweak
)
5558 && h
->smclas
== XMC_XO
)
5560 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5561 isym
.n_value
= h
->root
.u
.def
.value
;
5562 isym
.n_scnum
= N_UNDEF
;
5563 if (h
->root
.type
== bfd_link_hash_undefweak
5564 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5565 isym
.n_sclass
= C_WEAKEXT
;
5567 isym
.n_sclass
= C_EXT
;
5568 aux
.x_csect
.x_smtyp
= XTY_ER
;
5570 else if (h
->root
.type
== bfd_link_hash_defined
5571 || h
->root
.type
== bfd_link_hash_defweak
)
5573 struct xcoff_link_size_list
*l
;
5575 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5576 + h
->root
.u
.def
.section
->output_offset
5577 + h
->root
.u
.def
.value
);
5578 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5579 isym
.n_scnum
= N_ABS
;
5581 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5582 isym
.n_sclass
= C_HIDEXT
;
5583 aux
.x_csect
.x_smtyp
= XTY_SD
;
5585 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5587 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
5593 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5599 else if (h
->root
.type
== bfd_link_hash_common
)
5601 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5602 + h
->root
.u
.c
.p
->section
->output_offset
);
5603 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5604 isym
.n_sclass
= C_EXT
;
5605 aux
.x_csect
.x_smtyp
= XTY_CM
;
5606 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5611 isym
.n_type
= T_NULL
;
5614 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5615 outsym
+= bfd_coff_symesz (output_bfd
);
5617 aux
.x_csect
.x_smclas
= h
->smclas
;
5618 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5620 outsym
+= bfd_coff_auxesz (output_bfd
);
5622 if ((h
->root
.type
== bfd_link_hash_defined
5623 || h
->root
.type
== bfd_link_hash_defweak
)
5624 && h
->smclas
!= XMC_XO
)
5626 /* We just output an SD symbol. Now output an LD symbol. */
5629 if (h
->root
.type
== bfd_link_hash_undefweak
5630 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5631 isym
.n_sclass
= C_WEAKEXT
;
5633 isym
.n_sclass
= C_EXT
;
5634 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5635 outsym
+= bfd_coff_symesz (output_bfd
);
5637 aux
.x_csect
.x_smtyp
= XTY_LD
;
5638 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5639 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5641 outsym
+= bfd_coff_auxesz (output_bfd
);
5644 pos
= obj_sym_filepos (output_bfd
);
5645 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5646 amt
= outsym
- finfo
->outsyms
;
5647 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5648 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
5650 obj_raw_syment_count (output_bfd
) +=
5651 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5656 /* Handle a link order which is supposed to generate a reloc. */
5659 xcoff_reloc_link_order (bfd
*output_bfd
,
5660 struct xcoff_final_link_info
*finfo
,
5661 asection
*output_section
,
5662 struct bfd_link_order
*link_order
)
5664 reloc_howto_type
*howto
;
5665 struct xcoff_link_hash_entry
*h
;
5669 struct internal_reloc
*irel
;
5670 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5672 if (link_order
->type
== bfd_section_reloc_link_order
)
5673 /* We need to somehow locate a symbol in the right section. The
5674 symbol must either have a value of zero, or we must adjust
5675 the addend by the value of the symbol. FIXME: Write this
5676 when we need it. The old linker couldn't handle this anyhow. */
5679 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5682 bfd_set_error (bfd_error_bad_value
);
5686 h
= ((struct xcoff_link_hash_entry
*)
5687 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
5688 link_order
->u
.reloc
.p
->u
.name
,
5689 FALSE
, FALSE
, TRUE
));
5692 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
5693 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0)))
5698 hsec
= xcoff_symbol_section (h
);
5699 if (h
->root
.type
== bfd_link_hash_defined
5700 || h
->root
.type
== bfd_link_hash_defweak
)
5701 hval
= h
->root
.u
.def
.value
;
5705 addend
= link_order
->u
.reloc
.p
->addend
;
5707 addend
+= (hsec
->output_section
->vma
5708 + hsec
->output_offset
5715 bfd_reloc_status_type rstat
;
5718 size
= bfd_get_reloc_size (howto
);
5719 buf
= bfd_zmalloc (size
);
5723 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5729 case bfd_reloc_outofrange
:
5731 case bfd_reloc_overflow
:
5732 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
5733 (finfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5734 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0)))
5741 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5742 (file_ptr
) link_order
->offset
, size
);
5748 /* Store the reloc information in the right place. It will get
5749 swapped and written out at the end of the final_link routine. */
5750 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
5751 + output_section
->reloc_count
);
5752 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
5753 + output_section
->reloc_count
);
5755 memset (irel
, 0, sizeof (struct internal_reloc
));
5756 *rel_hash_ptr
= NULL
;
5758 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5761 irel
->r_symndx
= h
->indx
;
5764 /* Set the index to -2 to force this symbol to get written out. */
5770 irel
->r_type
= howto
->type
;
5771 irel
->r_size
= howto
->bitsize
- 1;
5772 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5773 irel
->r_size
|= 0x80;
5775 ++output_section
->reloc_count
;
5777 /* Now output the reloc to the .loader section. */
5778 if (xcoff_hash_table (finfo
->info
)->loader_section
)
5780 if (!xcoff_create_ldrel (output_bfd
, finfo
, output_section
,
5781 output_bfd
, irel
, hsec
, h
))
5788 /* Do the final link step. */
5791 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5793 bfd_size_type symesz
;
5794 struct xcoff_final_link_info finfo
;
5796 struct bfd_link_order
*p
;
5797 bfd_size_type max_contents_size
;
5798 bfd_size_type max_sym_count
;
5799 bfd_size_type max_lineno_count
;
5800 bfd_size_type max_reloc_count
;
5801 bfd_size_type max_output_reloc_count
;
5802 file_ptr rel_filepos
;
5804 file_ptr line_filepos
;
5805 unsigned int linesz
;
5807 bfd_byte
*external_relocs
= NULL
;
5808 char strbuf
[STRING_SIZE_SIZE
];
5813 abfd
->flags
|= DYNAMIC
;
5815 symesz
= bfd_coff_symesz (abfd
);
5818 finfo
.output_bfd
= abfd
;
5819 finfo
.strtab
= NULL
;
5820 finfo
.section_info
= NULL
;
5821 finfo
.last_file_index
= -1;
5822 finfo
.toc_symindx
= -1;
5823 finfo
.internal_syms
= NULL
;
5824 finfo
.sym_indices
= NULL
;
5825 finfo
.outsyms
= NULL
;
5826 finfo
.linenos
= NULL
;
5827 finfo
.contents
= NULL
;
5828 finfo
.external_relocs
= NULL
;
5830 if (xcoff_hash_table (info
)->loader_section
)
5832 finfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5833 + bfd_xcoff_ldhdrsz (abfd
));
5834 finfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5835 + bfd_xcoff_ldhdrsz (abfd
)
5836 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5837 * bfd_xcoff_ldsymsz (abfd
)));
5845 xcoff_data (abfd
)->coff
.link_info
= info
;
5847 finfo
.strtab
= _bfd_stringtab_init ();
5848 if (finfo
.strtab
== NULL
)
5851 /* Count the relocation entries required for the output file.
5852 (We've already counted the line numbers.) Determine a few
5854 max_contents_size
= 0;
5855 max_lineno_count
= 0;
5856 max_reloc_count
= 0;
5857 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5860 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5862 if (p
->type
== bfd_indirect_link_order
)
5866 sec
= p
->u
.indirect
.section
;
5868 /* Mark all sections which are to be included in the
5869 link. This will normally be every section. We need
5870 to do this so that we can identify any sections which
5871 the linker has decided to not include. */
5872 sec
->linker_mark
= TRUE
;
5874 o
->reloc_count
+= sec
->reloc_count
;
5876 if (sec
->rawsize
> max_contents_size
)
5877 max_contents_size
= sec
->rawsize
;
5878 if (sec
->size
> max_contents_size
)
5879 max_contents_size
= sec
->size
;
5880 if (coff_section_data (sec
->owner
, sec
) != NULL
5881 && xcoff_section_data (sec
->owner
, sec
) != NULL
5882 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5883 > max_lineno_count
))
5885 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5886 if (sec
->reloc_count
> max_reloc_count
)
5887 max_reloc_count
= sec
->reloc_count
;
5889 else if (p
->type
== bfd_section_reloc_link_order
5890 || p
->type
== bfd_symbol_reloc_link_order
)
5895 /* Compute the file positions for all the sections. */
5896 if (abfd
->output_has_begun
)
5898 if (xcoff_hash_table (info
)->file_align
!= 0)
5905 file_align
= xcoff_hash_table (info
)->file_align
;
5906 if (file_align
!= 0)
5908 bfd_boolean saw_contents
;
5912 /* Insert .pad sections before every section which has
5913 contents and is loaded, if it is preceded by some other
5914 section which has contents and is loaded. */
5915 saw_contents
= TRUE
;
5916 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5918 if (strcmp (o
->name
, ".pad") == 0)
5919 saw_contents
= FALSE
;
5920 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5921 && (o
->flags
& SEC_LOAD
) != 0)
5924 saw_contents
= TRUE
;
5929 /* Create a pad section and place it before the section
5930 that needs padding. This requires unlinking and
5931 relinking the bfd's section list. */
5933 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
5935 n
->alignment_power
= 0;
5937 bfd_section_list_remove (abfd
, n
);
5938 bfd_section_list_insert_before (abfd
, o
, n
);
5939 saw_contents
= FALSE
;
5944 /* Reset the section indices after inserting the new
5947 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5950 o
->target_index
= indx
;
5952 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
5954 /* Work out appropriate sizes for the .pad sections to force
5955 each section to land on a page boundary. This bit of
5956 code knows what compute_section_file_positions is going
5958 sofar
= bfd_coff_filhsz (abfd
);
5959 sofar
+= bfd_coff_aoutsz (abfd
);
5960 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
5961 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5962 if ((bfd_xcoff_is_reloc_count_overflow
5963 (abfd
, (bfd_vma
) o
->reloc_count
))
5964 || (bfd_xcoff_is_lineno_count_overflow
5965 (abfd
, (bfd_vma
) o
->lineno_count
)))
5966 /* 64 does not overflow, need to check if 32 does */
5967 sofar
+= bfd_coff_scnhsz (abfd
);
5969 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5971 if (strcmp (o
->name
, ".pad") == 0)
5975 BFD_ASSERT (o
->size
== 0);
5976 pageoff
= sofar
& (file_align
- 1);
5979 o
->size
= file_align
- pageoff
;
5980 sofar
+= file_align
- pageoff
;
5981 o
->flags
|= SEC_HAS_CONTENTS
;
5986 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
5987 sofar
+= BFD_ALIGN (o
->size
,
5988 1 << o
->alignment_power
);
5993 if (! bfd_coff_compute_section_file_positions (abfd
))
5997 /* Allocate space for the pointers we need to keep for the relocs. */
6001 /* We use section_count + 1, rather than section_count, because
6002 the target_index fields are 1 based. */
6003 amt
= abfd
->section_count
+ 1;
6004 amt
*= sizeof (struct xcoff_link_section_info
);
6005 finfo
.section_info
= bfd_malloc (amt
);
6006 if (finfo
.section_info
== NULL
)
6008 for (i
= 0; i
<= abfd
->section_count
; i
++)
6010 finfo
.section_info
[i
].relocs
= NULL
;
6011 finfo
.section_info
[i
].rel_hashes
= NULL
;
6012 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6016 /* Set the file positions for the relocs. */
6017 rel_filepos
= obj_relocbase (abfd
);
6018 relsz
= bfd_coff_relsz (abfd
);
6019 max_output_reloc_count
= 0;
6020 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6022 if (o
->reloc_count
== 0)
6026 /* A stripped file has no relocs. However, we still
6027 allocate the buffers, so that later code doesn't have to
6028 worry about whether we are stripping or not. */
6029 if (info
->strip
== strip_all
)
6033 o
->flags
|= SEC_RELOC
;
6034 o
->rel_filepos
= rel_filepos
;
6035 rel_filepos
+= o
->reloc_count
* relsz
;
6038 /* We don't know the indices of global symbols until we have
6039 written out all the local symbols. For each section in
6040 the output file, we keep an array of pointers to hash
6041 table entries. Each entry in the array corresponds to a
6042 reloc. When we find a reloc against a global symbol, we
6043 set the corresponding entry in this array so that we can
6044 fix up the symbol index after we have written out all the
6047 Because of this problem, we also keep the relocs in
6048 memory until the end of the link. This wastes memory.
6049 We could backpatch the file later, I suppose, although it
6051 amt
= o
->reloc_count
;
6052 amt
*= sizeof (struct internal_reloc
);
6053 finfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6055 amt
= o
->reloc_count
;
6056 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6057 finfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6059 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
6060 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6063 if (o
->reloc_count
> max_output_reloc_count
)
6064 max_output_reloc_count
= o
->reloc_count
;
6068 /* We now know the size of the relocs, so we can determine the file
6069 positions of the line numbers. */
6070 line_filepos
= rel_filepos
;
6071 finfo
.line_filepos
= line_filepos
;
6072 linesz
= bfd_coff_linesz (abfd
);
6073 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6075 if (o
->lineno_count
== 0)
6076 o
->line_filepos
= 0;
6079 o
->line_filepos
= line_filepos
;
6080 line_filepos
+= o
->lineno_count
* linesz
;
6083 /* Reset the reloc and lineno counts, so that we can use them to
6084 count the number of entries we have output so far. */
6086 o
->lineno_count
= 0;
6089 obj_sym_filepos (abfd
) = line_filepos
;
6091 /* Figure out the largest number of symbols in an input BFD. Take
6092 the opportunity to clear the output_has_begun fields of all the
6093 input BFD's. We want at least 6 symbols, since that is the
6094 number which xcoff_write_global_symbol may need. */
6096 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6100 sub
->output_has_begun
= FALSE
;
6101 sz
= obj_raw_syment_count (sub
);
6102 if (sz
> max_sym_count
)
6106 /* Allocate some buffers used while linking. */
6107 amt
= max_sym_count
* sizeof (struct internal_syment
);
6108 finfo
.internal_syms
= bfd_malloc (amt
);
6110 amt
= max_sym_count
* sizeof (long);
6111 finfo
.sym_indices
= bfd_malloc (amt
);
6113 amt
= (max_sym_count
+ 1) * symesz
;
6114 finfo
.outsyms
= bfd_malloc (amt
);
6116 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6117 finfo
.linenos
= bfd_malloc (amt
);
6119 amt
= max_contents_size
;
6120 finfo
.contents
= bfd_malloc (amt
);
6122 amt
= max_reloc_count
* relsz
;
6123 finfo
.external_relocs
= bfd_malloc (amt
);
6125 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6126 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6127 || finfo
.outsyms
== NULL
6128 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
6129 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
6130 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6133 obj_raw_syment_count (abfd
) = 0;
6135 /* Find a TOC symbol, if we need one. */
6136 if (!xcoff_find_tc0 (abfd
, &finfo
))
6139 /* We now know the position of everything in the file, except that
6140 we don't know the size of the symbol table and therefore we don't
6141 know where the string table starts. We just build the string
6142 table in memory as we go along. We process all the relocations
6143 for a single input file at once. */
6144 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6146 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6148 if (p
->type
== bfd_indirect_link_order
6149 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6151 sub
= p
->u
.indirect
.section
->owner
;
6152 if (! sub
->output_has_begun
)
6154 if (! xcoff_link_input_bfd (&finfo
, sub
))
6156 sub
->output_has_begun
= TRUE
;
6159 else if (p
->type
== bfd_section_reloc_link_order
6160 || p
->type
== bfd_symbol_reloc_link_order
)
6162 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
6167 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6173 /* Free up the buffers used by xcoff_link_input_bfd. */
6174 if (finfo
.internal_syms
!= NULL
)
6176 free (finfo
.internal_syms
);
6177 finfo
.internal_syms
= NULL
;
6179 if (finfo
.sym_indices
!= NULL
)
6181 free (finfo
.sym_indices
);
6182 finfo
.sym_indices
= NULL
;
6184 if (finfo
.linenos
!= NULL
)
6186 free (finfo
.linenos
);
6187 finfo
.linenos
= NULL
;
6189 if (finfo
.contents
!= NULL
)
6191 free (finfo
.contents
);
6192 finfo
.contents
= NULL
;
6194 if (finfo
.external_relocs
!= NULL
)
6196 free (finfo
.external_relocs
);
6197 finfo
.external_relocs
= NULL
;
6200 /* The value of the last C_FILE symbol is supposed to be -1. Write
6202 if (finfo
.last_file_index
!= -1)
6204 finfo
.last_file
.n_value
= -(bfd_vma
) 1;
6205 bfd_coff_swap_sym_out (abfd
, (void *) &finfo
.last_file
,
6206 (void *) finfo
.outsyms
);
6207 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
6208 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6209 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
6213 /* Write out all the global symbols which do not come from XCOFF
6215 xcoff_link_hash_traverse (xcoff_hash_table (info
),
6216 xcoff_write_global_symbol
,
6219 if (finfo
.outsyms
!= NULL
)
6221 free (finfo
.outsyms
);
6222 finfo
.outsyms
= NULL
;
6225 /* Now that we have written out all the global symbols, we know the
6226 symbol indices to use for relocs against them, and we can finally
6227 write out the relocs. */
6228 amt
= max_output_reloc_count
* relsz
;
6229 external_relocs
= bfd_malloc (amt
);
6230 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6233 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6235 struct internal_reloc
*irel
;
6236 struct internal_reloc
*irelend
;
6237 struct xcoff_link_hash_entry
**rel_hash
;
6238 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6240 bfd_size_type rel_size
;
6242 /* A stripped file has no relocs. */
6243 if (info
->strip
== strip_all
)
6249 if (o
->reloc_count
== 0)
6252 irel
= finfo
.section_info
[o
->target_index
].relocs
;
6253 irelend
= irel
+ o
->reloc_count
;
6254 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
6255 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6257 if (*rel_hash
!= NULL
)
6259 if ((*rel_hash
)->indx
< 0)
6261 if (! ((*info
->callbacks
->unattached_reloc
)
6262 (info
, (*rel_hash
)->root
.root
.string
,
6263 NULL
, o
, irel
->r_vaddr
)))
6265 (*rel_hash
)->indx
= 0;
6267 irel
->r_symndx
= (*rel_hash
)->indx
;
6271 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6272 toc_rel_hash
!= NULL
;
6273 toc_rel_hash
= toc_rel_hash
->next
)
6275 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6277 if (! ((*info
->callbacks
->unattached_reloc
)
6278 (info
, toc_rel_hash
->h
->root
.root
.string
,
6279 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
6281 toc_rel_hash
->h
->u
.toc_indx
= 0;
6283 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6286 /* XCOFF requires that the relocs be sorted by address. We tend
6287 to produce them in the order in which their containing csects
6288 appear in the symbol table, which is not necessarily by
6289 address. So we sort them here. There may be a better way to
6291 qsort ((void *) finfo
.section_info
[o
->target_index
].relocs
,
6292 o
->reloc_count
, sizeof (struct internal_reloc
),
6295 irel
= finfo
.section_info
[o
->target_index
].relocs
;
6296 irelend
= irel
+ o
->reloc_count
;
6297 erel
= external_relocs
;
6298 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6299 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6301 rel_size
= relsz
* o
->reloc_count
;
6302 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6303 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6307 if (external_relocs
!= NULL
)
6309 free (external_relocs
);
6310 external_relocs
= NULL
;
6313 /* Free up the section information. */
6314 if (finfo
.section_info
!= NULL
)
6318 for (i
= 0; i
< abfd
->section_count
; i
++)
6320 if (finfo
.section_info
[i
].relocs
!= NULL
)
6321 free (finfo
.section_info
[i
].relocs
);
6322 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
6323 free (finfo
.section_info
[i
].rel_hashes
);
6325 free (finfo
.section_info
);
6326 finfo
.section_info
= NULL
;
6329 /* Write out the loader section contents. */
6330 o
= xcoff_hash_table (info
)->loader_section
;
6333 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
6334 == (xcoff_hash_table (info
)->loader_section
->contents
6335 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6336 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6337 (file_ptr
) o
->output_offset
, o
->size
))
6341 /* Write out the magic sections. */
6342 o
= xcoff_hash_table (info
)->linkage_section
;
6344 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6345 (file_ptr
) o
->output_offset
,
6348 o
= xcoff_hash_table (info
)->toc_section
;
6350 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6351 (file_ptr
) o
->output_offset
,
6354 o
= xcoff_hash_table (info
)->descriptor_section
;
6356 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6357 (file_ptr
) o
->output_offset
,
6361 /* Write out the string table. */
6362 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6363 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6366 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
6368 amt
= STRING_SIZE_SIZE
;
6369 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6371 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
6374 _bfd_stringtab_free (finfo
.strtab
);
6376 /* Write out the debugging string table. */
6377 o
= xcoff_hash_table (info
)->debug_section
;
6380 struct bfd_strtab_hash
*debug_strtab
;
6382 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6383 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6384 >= _bfd_stringtab_size (debug_strtab
));
6385 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6386 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6388 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6392 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6393 not try to write out the symbols. */
6394 bfd_get_symcount (abfd
) = 0;
6399 if (finfo
.strtab
!= NULL
)
6400 _bfd_stringtab_free (finfo
.strtab
);
6402 if (finfo
.section_info
!= NULL
)
6406 for (i
= 0; i
< abfd
->section_count
; i
++)
6408 if (finfo
.section_info
[i
].relocs
!= NULL
)
6409 free (finfo
.section_info
[i
].relocs
);
6410 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
6411 free (finfo
.section_info
[i
].rel_hashes
);
6413 free (finfo
.section_info
);
6416 if (finfo
.internal_syms
!= NULL
)
6417 free (finfo
.internal_syms
);
6418 if (finfo
.sym_indices
!= NULL
)
6419 free (finfo
.sym_indices
);
6420 if (finfo
.outsyms
!= NULL
)
6421 free (finfo
.outsyms
);
6422 if (finfo
.linenos
!= NULL
)
6423 free (finfo
.linenos
);
6424 if (finfo
.contents
!= NULL
)
6425 free (finfo
.contents
);
6426 if (finfo
.external_relocs
!= NULL
)
6427 free (finfo
.external_relocs
);
6428 if (external_relocs
!= NULL
)
6429 free (external_relocs
);