1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 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
)
694 base
= lbasename (filename
);
695 length
= base
- 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 /* Ignore global linkage code when linking statically. */
1893 if (info
->static_link
1894 && (smtyp
== XTY_SD
|| smtyp
== XTY_LD
)
1895 && aux
.x_csect
.x_smclas
== XMC_GL
)
1897 section
= bfd_und_section_ptr
;
1901 /* The AIX linker appears to only detect multiple symbol
1902 definitions when there is a reference to the symbol. If
1903 a symbol is defined multiple times, and the only
1904 references are from the same object file, the AIX linker
1905 appears to permit it. It does not merge the different
1906 definitions, but handles them independently. On the
1907 other hand, if there is a reference, the linker reports
1910 This matters because the AIX <net/net_globals.h> header
1911 file actually defines an initialized array, so we have to
1912 actually permit that to work.
1914 Just to make matters even more confusing, the AIX linker
1915 appears to permit multiple symbol definitions whenever
1916 the second definition is in an archive rather than an
1917 object file. This may be a consequence of the manner in
1918 which it handles archives: I think it may load the entire
1919 archive in as separate csects, and then let garbage
1920 collection discard symbols.
1922 We also have to handle the case of statically linking a
1923 shared object, which will cause symbol redefinitions,
1924 although this is an easier case to detect. */
1925 else if (info
->output_bfd
->xvec
== abfd
->xvec
)
1927 if (! bfd_is_und_section (section
))
1928 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1929 name
, TRUE
, copy
, FALSE
);
1931 /* Make a copy of the symbol name to prevent problems with
1933 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1934 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1935 TRUE
, TRUE
, FALSE
));
1937 if (*sym_hash
== NULL
)
1939 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1940 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1941 && ! bfd_is_und_section (section
)
1942 && ! bfd_is_com_section (section
))
1944 /* This is a second definition of a defined symbol. */
1945 if (((*sym_hash
)->flags
& XCOFF_DEF_REGULAR
) == 0
1946 && ((*sym_hash
)->flags
& XCOFF_DEF_DYNAMIC
) != 0)
1948 /* The existing symbol is from a shared library.
1950 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1951 (*sym_hash
)->root
.u
.undef
.abfd
=
1952 (*sym_hash
)->root
.u
.def
.section
->owner
;
1954 else if (abfd
->my_archive
!= NULL
)
1956 /* This is a redefinition in an object contained
1957 in an archive. Just ignore it. See the
1959 section
= bfd_und_section_ptr
;
1962 else if (sym
.n_sclass
== C_AIX_WEAKEXT
1963 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1965 /* At least one of the definitions is weak.
1966 Allow the normal rules to take effect. */
1968 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1969 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1971 /* This symbol has been referenced. In this
1972 case, we just continue and permit the
1973 multiple definition error. See the comment
1974 above about the behaviour of the AIX linker. */
1976 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1978 /* The symbols are both csects of the same
1979 class. There is at least a chance that this
1980 is a semi-legitimate redefinition. */
1981 section
= bfd_und_section_ptr
;
1983 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
1986 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
1987 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
1988 && (bfd_is_und_section (section
)
1989 || bfd_is_com_section (section
)))
1991 /* This is a reference to a multiply defined symbol.
1992 Report the error now. See the comment above
1993 about the behaviour of the AIX linker. We could
1994 also do this with warning symbols, but I'm not
1995 sure the XCOFF linker is wholly prepared to
1996 handle them, and that would only be a warning,
1998 if (! ((*info
->callbacks
->multiple_definition
)
1999 (info
, (*sym_hash
)->root
.root
.string
,
2000 NULL
, NULL
, (bfd_vma
) 0,
2001 (*sym_hash
)->root
.u
.def
.section
->owner
,
2002 (*sym_hash
)->root
.u
.def
.section
,
2003 (*sym_hash
)->root
.u
.def
.value
)))
2005 /* Try not to give this error too many times. */
2006 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2010 /* _bfd_generic_link_add_one_symbol may call the linker to
2011 generate an error message, and the linker may try to read
2012 the symbol table to give a good error. Right now, the
2013 line numbers are in an inconsistent state, since they are
2014 counted both in the real sections and in the new csects.
2015 We need to leave the count in the real sections so that
2016 the linker can report the line number of the error
2017 correctly, so temporarily clobber the link to the csects
2018 so that the linker will not try to read the line numbers
2019 a second time from the csects. */
2020 BFD_ASSERT (last_real
->next
== first_csect
);
2021 last_real
->next
= NULL
;
2022 flags
= (sym
.n_sclass
== C_EXT
? BSF_GLOBAL
: BSF_WEAK
);
2023 if (! (_bfd_generic_link_add_one_symbol
2024 (info
, abfd
, name
, flags
, section
, value
,
2026 (struct bfd_link_hash_entry
**) sym_hash
)))
2028 last_real
->next
= first_csect
;
2030 if (smtyp
== XTY_CM
)
2032 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2033 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2034 /* We don't need the common csect we just created. */
2037 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2038 = csect
->alignment_power
;
2041 if (info
->output_bfd
->xvec
== abfd
->xvec
)
2047 || section
== bfd_und_section_ptr
)
2048 flag
= XCOFF_REF_REGULAR
;
2050 flag
= XCOFF_DEF_REGULAR
;
2051 (*sym_hash
)->flags
|= flag
;
2053 if ((*sym_hash
)->smclas
== XMC_UA
2054 || flag
== XCOFF_DEF_REGULAR
)
2055 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2059 if (smtyp
== XTY_ER
)
2060 *csect_cache
= section
;
2063 *csect_cache
= csect
;
2065 xcoff_section_data (abfd
, csect
)->last_symndx
2066 = (esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
;
2069 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2070 sym_hash
+= sym
.n_numaux
+ 1;
2071 csect_cache
+= sym
.n_numaux
+ 1;
2072 lineno_counts
+= sym
.n_numaux
+ 1;
2075 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2077 /* Make sure that we have seen all the relocs. */
2078 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2080 /* Reset the section size and the line number count, since the
2081 data is now attached to the csects. Don't reset the size of
2082 the .debug section, since we need to read it below in
2083 bfd_xcoff_size_dynamic_sections. */
2084 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
2086 o
->lineno_count
= 0;
2088 if ((o
->flags
& SEC_RELOC
) != 0)
2091 struct internal_reloc
*rel
;
2092 asection
**rel_csect
;
2094 rel
= reloc_info
[o
->target_index
].relocs
;
2095 rel_csect
= reloc_info
[o
->target_index
].csects
;
2097 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2099 if (*rel_csect
== NULL
)
2101 (*_bfd_error_handler
)
2102 (_("%B: reloc %s:%d not in csect"),
2104 bfd_set_error (bfd_error_bad_value
);
2108 /* We identify all function symbols that are the target
2109 of a relocation, so that we can create glue code for
2110 functions imported from dynamic objects. */
2111 if (info
->output_bfd
->xvec
== abfd
->xvec
2112 && *rel_csect
!= bfd_und_section_ptr
2113 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2115 struct xcoff_link_hash_entry
*h
;
2117 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2118 /* If the symbol name starts with a period, it is
2119 the code of a function. If the symbol is
2120 currently undefined, then add an undefined symbol
2121 for the function descriptor. This should do no
2122 harm, because any regular object that defines the
2123 function should also define the function
2124 descriptor. It helps, because it means that we
2125 will identify the function descriptor with a
2126 dynamic object if a dynamic object defines it. */
2127 if (h
->root
.root
.string
[0] == '.'
2128 && h
->descriptor
== NULL
)
2130 struct xcoff_link_hash_entry
*hds
;
2131 struct bfd_link_hash_entry
*bh
;
2133 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2134 h
->root
.root
.string
+ 1,
2138 if (hds
->root
.type
== bfd_link_hash_new
)
2141 if (! (_bfd_generic_link_add_one_symbol
2142 (info
, abfd
, hds
->root
.root
.string
,
2143 (flagword
) 0, bfd_und_section_ptr
,
2144 (bfd_vma
) 0, NULL
, FALSE
,
2147 hds
= (struct xcoff_link_hash_entry
*) bh
;
2149 hds
->flags
|= XCOFF_DESCRIPTOR
;
2150 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2151 hds
->descriptor
= h
;
2152 h
->descriptor
= hds
;
2154 if (h
->root
.root
.string
[0] == '.')
2155 h
->flags
|= XCOFF_CALLED
;
2159 free (reloc_info
[o
->target_index
].csects
);
2160 reloc_info
[o
->target_index
].csects
= NULL
;
2162 /* Reset SEC_RELOC and the reloc_count, since the reloc
2163 information is now attached to the csects. */
2164 o
->flags
&=~ SEC_RELOC
;
2167 /* If we are not keeping memory, free the reloc information. */
2168 if (! info
->keep_memory
2169 && coff_section_data (abfd
, o
) != NULL
2170 && coff_section_data (abfd
, o
)->relocs
!= NULL
2171 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2173 free (coff_section_data (abfd
, o
)->relocs
);
2174 coff_section_data (abfd
, o
)->relocs
= NULL
;
2178 /* Free up the line numbers. FIXME: We could cache these
2179 somewhere for the final link, to avoid reading them again. */
2180 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2182 free (reloc_info
[o
->target_index
].linenos
);
2183 reloc_info
[o
->target_index
].linenos
= NULL
;
2189 obj_coff_keep_syms (abfd
) = keep_syms
;
2194 if (reloc_info
!= NULL
)
2196 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2198 if (reloc_info
[o
->target_index
].csects
!= NULL
)
2199 free (reloc_info
[o
->target_index
].csects
);
2200 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2201 free (reloc_info
[o
->target_index
].linenos
);
2205 obj_coff_keep_syms (abfd
) = keep_syms
;
2212 /* Add symbols from an XCOFF object file. */
2215 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2217 if (! _bfd_coff_get_external_symbols (abfd
))
2219 if (! xcoff_link_add_symbols (abfd
, info
))
2221 if (! info
->keep_memory
)
2223 if (! _bfd_coff_free_symbols (abfd
))
2229 /* Look through the loader symbols to see if this dynamic object
2230 should be included in the link. The native linker uses the loader
2231 symbols, not the normal symbol table, so we do too. */
2234 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
2235 struct bfd_link_info
*info
,
2236 bfd_boolean
*pneeded
,
2241 struct internal_ldhdr ldhdr
;
2242 const char *strings
;
2243 bfd_byte
*elsym
, *elsymend
;
2247 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2249 /* There are no symbols, so don't try to include it. */
2252 if (! xcoff_get_section_contents (abfd
, lsec
))
2254 contents
= coff_section_data (abfd
, lsec
)->contents
;
2256 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
2258 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2260 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2262 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2263 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2265 struct internal_ldsym ldsym
;
2266 char nambuf
[SYMNMLEN
+ 1];
2268 struct bfd_link_hash_entry
*h
;
2270 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2272 /* We are only interested in exported symbols. */
2273 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2276 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2277 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2280 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2281 nambuf
[SYMNMLEN
] = '\0';
2285 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2287 /* We are only interested in symbols that are currently
2288 undefined. At this point we know that we are using an XCOFF
2291 && h
->type
== bfd_link_hash_undefined
2292 && (((struct xcoff_link_hash_entry
*) h
)->flags
2293 & XCOFF_DEF_DYNAMIC
) == 0)
2295 if (! (*info
->callbacks
->add_archive_element
)
2296 (info
, abfd
, name
, subsbfd
))
2303 /* We do not need this shared object. */
2304 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2306 free (coff_section_data (abfd
, lsec
)->contents
);
2307 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2313 /* Look through the symbols to see if this object file should be
2314 included in the link. */
2317 xcoff_link_check_ar_symbols (bfd
*abfd
,
2318 struct bfd_link_info
*info
,
2319 bfd_boolean
*pneeded
,
2322 bfd_size_type symesz
;
2328 if ((abfd
->flags
& DYNAMIC
) != 0
2329 && ! info
->static_link
2330 && info
->output_bfd
->xvec
== abfd
->xvec
)
2331 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
, subsbfd
);
2333 symesz
= bfd_coff_symesz (abfd
);
2334 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2335 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2336 while (esym
< esym_end
)
2338 struct internal_syment sym
;
2340 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2342 if (EXTERN_SYM_P (sym
.n_sclass
) && sym
.n_scnum
!= N_UNDEF
)
2345 char buf
[SYMNMLEN
+ 1];
2346 struct bfd_link_hash_entry
*h
;
2348 /* This symbol is externally visible, and is defined by this
2350 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2354 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2356 /* We are only interested in symbols that are currently
2357 undefined. If a symbol is currently known to be common,
2358 XCOFF linkers do not bring in an object file which
2359 defines it. We also don't bring in symbols to satisfy
2360 undefined references in shared objects. */
2362 && h
->type
== bfd_link_hash_undefined
2363 && (info
->output_bfd
->xvec
!= abfd
->xvec
2364 || (((struct xcoff_link_hash_entry
*) h
)->flags
2365 & XCOFF_DEF_DYNAMIC
) == 0))
2367 if (! (*info
->callbacks
->add_archive_element
)
2368 (info
, abfd
, name
, subsbfd
))
2375 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2378 /* We do not need this object file. */
2382 /* Check a single archive element to see if we need to include it in
2383 the link. *PNEEDED is set according to whether this element is
2384 needed in the link or not. This is called via
2385 _bfd_generic_link_add_archive_symbols. */
2388 xcoff_link_check_archive_element (bfd
*abfd
,
2389 struct bfd_link_info
*info
,
2390 bfd_boolean
*pneeded
)
2392 bfd_boolean keep_syms_p
;
2393 bfd
*subsbfd
= NULL
;
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
, &subsbfd
))
2404 /* Potentially, the add_archive_element hook may have set a
2405 substitute BFD for us. */
2406 if (subsbfd
&& !_bfd_coff_get_external_symbols (subsbfd
))
2408 if (! xcoff_link_add_symbols (subsbfd
? subsbfd
: abfd
, info
))
2410 if (info
->keep_memory
)
2416 if (! _bfd_coff_free_symbols (abfd
))
2423 /* Given an XCOFF BFD, add symbols to the global hash table as
2427 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2429 switch (bfd_get_format (abfd
))
2432 return xcoff_link_add_object_symbols (abfd
, info
);
2435 /* If the archive has a map, do the usual search. We then need
2436 to check the archive for dynamic objects, because they may not
2437 appear in the archive map even though they should, perhaps, be
2438 included. If the archive has no map, we just consider each object
2439 file in turn, since that apparently is what the AIX native linker
2441 if (bfd_has_map (abfd
))
2443 if (! (_bfd_generic_link_add_archive_symbols
2444 (abfd
, info
, xcoff_link_check_archive_element
)))
2451 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2452 while (member
!= NULL
)
2454 if (bfd_check_format (member
, bfd_object
)
2455 && (info
->output_bfd
->xvec
== member
->xvec
)
2456 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2460 if (! xcoff_link_check_archive_element (member
, info
,
2464 member
->archive_pass
= -1;
2466 member
= bfd_openr_next_archived_file (abfd
, member
);
2473 bfd_set_error (bfd_error_wrong_format
);
2479 _bfd_xcoff_define_common_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2480 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2481 struct bfd_link_hash_entry
*harg
)
2483 struct xcoff_link_hash_entry
*h
;
2485 if (!bfd_generic_define_common_symbol (output_bfd
, info
, harg
))
2488 h
= (struct xcoff_link_hash_entry
*) harg
;
2489 h
->flags
|= XCOFF_DEF_REGULAR
;
2493 /* If symbol H has not been interpreted as a function descriptor,
2494 see whether it should be. Set up its descriptor information if so. */
2497 xcoff_find_function (struct bfd_link_info
*info
,
2498 struct xcoff_link_hash_entry
*h
)
2500 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2501 && h
->root
.root
.string
[0] != '.')
2504 struct xcoff_link_hash_entry
*hfn
;
2507 amt
= strlen (h
->root
.root
.string
) + 2;
2508 fnname
= bfd_malloc (amt
);
2512 strcpy (fnname
+ 1, h
->root
.root
.string
);
2513 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2514 fnname
, FALSE
, FALSE
, TRUE
);
2517 && hfn
->smclas
== XMC_PR
2518 && (hfn
->root
.type
== bfd_link_hash_defined
2519 || hfn
->root
.type
== bfd_link_hash_defweak
))
2521 h
->flags
|= XCOFF_DESCRIPTOR
;
2522 h
->descriptor
= hfn
;
2523 hfn
->descriptor
= h
;
2529 /* Return true if the given bfd contains at least one shared object. */
2532 xcoff_archive_contains_shared_object_p (struct bfd_link_info
*info
,
2535 struct xcoff_archive_info
*archive_info
;
2538 archive_info
= xcoff_get_archive_info (info
, archive
);
2539 if (!archive_info
->know_contains_shared_object_p
)
2541 member
= bfd_openr_next_archived_file (archive
, NULL
);
2542 while (member
!= NULL
&& (member
->flags
& DYNAMIC
) == 0)
2543 member
= bfd_openr_next_archived_file (archive
, member
);
2545 archive_info
->contains_shared_object_p
= (member
!= NULL
);
2546 archive_info
->know_contains_shared_object_p
= 1;
2548 return archive_info
->contains_shared_object_p
;
2551 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2552 qualifies for export by -bexpall. */
2555 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry
*h
)
2557 /* Exclude symbols beginning with '_'. */
2558 if (h
->root
.root
.string
[0] == '_')
2561 /* Exclude archive members that would otherwise be unreferenced. */
2562 if ((h
->flags
& XCOFF_MARK
) == 0
2563 && (h
->root
.type
== bfd_link_hash_defined
2564 || h
->root
.type
== bfd_link_hash_defweak
)
2565 && h
->root
.u
.def
.section
->owner
!= NULL
2566 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2572 /* Return true if symbol H qualifies for the forms of automatic export
2573 specified by AUTO_EXPORT_FLAGS. */
2576 xcoff_auto_export_p (struct bfd_link_info
*info
,
2577 struct xcoff_link_hash_entry
*h
,
2578 unsigned int auto_export_flags
)
2580 /* Don't automatically export things that were explicitly exported. */
2581 if ((h
->flags
& XCOFF_EXPORT
) != 0)
2584 /* Don't export things that we don't define. */
2585 if ((h
->flags
& XCOFF_DEF_REGULAR
) == 0)
2588 /* Don't export functions; export their descriptors instead. */
2589 if (h
->root
.root
.string
[0] == '.')
2592 /* We don't export a symbol which is being defined by an object
2593 included from an archive which contains a shared object. The
2594 rationale is that if an archive contains both an unshared and
2595 a shared object, then there must be some reason that the
2596 unshared object is unshared, and we don't want to start
2597 providing a shared version of it. In particular, this solves
2598 a bug involving the _savefNN set of functions. gcc will call
2599 those functions without providing a slot to restore the TOC,
2600 so it is essential that these functions be linked in directly
2601 and not from a shared object, which means that a shared
2602 object which also happens to link them in must not export
2603 them. This is confusing, but I haven't been able to think of
2604 a different approach. Note that the symbols can, of course,
2605 be exported explicitly. */
2606 if (h
->root
.type
== bfd_link_hash_defined
2607 || h
->root
.type
== bfd_link_hash_defweak
)
2611 owner
= h
->root
.u
.def
.section
->owner
;
2613 && owner
->my_archive
!= NULL
2614 && xcoff_archive_contains_shared_object_p (info
, owner
->my_archive
))
2618 /* Otherwise, all symbols are exported by -bexpfull. */
2619 if ((auto_export_flags
& XCOFF_EXPFULL
) != 0)
2622 /* Despite its name, -bexpall exports most but not all symbols. */
2623 if ((auto_export_flags
& XCOFF_EXPALL
) != 0
2624 && xcoff_covered_by_expall_p (h
))
2630 /* Return true if relocation REL needs to be copied to the .loader section.
2631 If REL is against a global symbol, H is that symbol, otherwise it
2635 xcoff_need_ldrel_p (struct bfd_link_info
*info
, struct internal_reloc
*rel
,
2636 struct xcoff_link_hash_entry
*h
)
2638 if (!xcoff_hash_table (info
)->loader_section
)
2641 switch (rel
->r_type
)
2648 /* We should never need a .loader reloc for a TOC-relative reloc. */
2652 /* In this case, relocations against defined symbols can be resolved
2655 || h
->root
.type
== bfd_link_hash_defined
2656 || h
->root
.type
== bfd_link_hash_defweak
2657 || h
->root
.type
== bfd_link_hash_common
)
2660 /* We will always provide a local definition of function symbols,
2661 even if we don't have one yet. */
2662 if ((h
->flags
& XCOFF_CALLED
) != 0)
2671 /* Absolute relocations against absolute symbols can be
2672 resolved statically. */
2674 && (h
->root
.type
== bfd_link_hash_defined
2675 || h
->root
.type
== bfd_link_hash_defweak
)
2676 && bfd_is_abs_section (h
->root
.u
.def
.section
))
2683 /* Mark a symbol as not being garbage, including the section in which
2686 static inline bfd_boolean
2687 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2689 if ((h
->flags
& XCOFF_MARK
) != 0)
2692 h
->flags
|= XCOFF_MARK
;
2694 /* If we're marking an undefined symbol, try find some way of
2696 if (!info
->relocatable
2697 && (h
->flags
& XCOFF_IMPORT
) == 0
2698 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2699 && (h
->root
.type
== bfd_link_hash_undefined
2700 || h
->root
.type
== bfd_link_hash_undefweak
))
2702 /* First check whether this symbol can be interpreted as an
2703 undefined function descriptor for a defined function symbol. */
2704 if (!xcoff_find_function (info
, h
))
2707 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2708 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2709 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2711 /* This is a descriptor for a defined symbol, but the input
2712 objects have not defined the descriptor itself. Fill in
2713 the definition automatically.
2715 Note that we do this even if we found a dynamic definition
2716 of H. The local function definition logically overrides
2720 sec
= xcoff_hash_table (info
)->descriptor_section
;
2721 h
->root
.type
= bfd_link_hash_defined
;
2722 h
->root
.u
.def
.section
= sec
;
2723 h
->root
.u
.def
.value
= sec
->size
;
2725 h
->flags
|= XCOFF_DEF_REGULAR
;
2727 /* The size of the function descriptor depends on whether this
2728 is xcoff32 (12) or xcoff64 (24). */
2729 sec
->size
+= bfd_xcoff_function_descriptor_size (sec
->owner
);
2731 /* A function descriptor uses two relocs: one for the
2732 associated code, and one for the TOC address. */
2733 xcoff_hash_table (info
)->ldrel_count
+= 2;
2734 sec
->reloc_count
+= 2;
2736 /* Mark the function itself. */
2737 if (!xcoff_mark_symbol (info
, h
->descriptor
))
2740 /* Mark the TOC section, so that we get an anchor
2741 to relocate against. */
2742 if (!xcoff_mark (info
, xcoff_hash_table (info
)->toc_section
))
2745 /* We handle writing out the contents of the descriptor in
2746 xcoff_write_global_symbol. */
2748 else if (info
->static_link
)
2749 /* We can't get a symbol value dynamically, so just assume
2750 that it's undefined. */
2751 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2752 else if ((h
->flags
& XCOFF_CALLED
) != 0)
2754 /* This is a function symbol for which we need to create
2757 struct xcoff_link_hash_entry
*hds
;
2759 /* Mark the descriptor (and its TOC section). */
2760 hds
= h
->descriptor
;
2761 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2762 || hds
->root
.type
== bfd_link_hash_undefweak
)
2763 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2764 if (!xcoff_mark_symbol (info
, hds
))
2767 /* Treat this symbol as undefined if the descriptor was. */
2768 if ((hds
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
2769 h
->flags
|= XCOFF_WAS_UNDEFINED
;
2771 /* Allocate room for the global linkage code itself. */
2772 sec
= xcoff_hash_table (info
)->linkage_section
;
2773 h
->root
.type
= bfd_link_hash_defined
;
2774 h
->root
.u
.def
.section
= sec
;
2775 h
->root
.u
.def
.value
= sec
->size
;
2777 h
->flags
|= XCOFF_DEF_REGULAR
;
2778 sec
->size
+= bfd_xcoff_glink_code_size (info
->output_bfd
);
2780 /* The global linkage code requires a TOC entry for the
2782 if (hds
->toc_section
== NULL
)
2787 xcoff32 uses 4 bytes in the toc.
2788 xcoff64 uses 8 bytes in the toc. */
2789 if (bfd_xcoff_is_xcoff64 (info
->output_bfd
))
2791 else if (bfd_xcoff_is_xcoff32 (info
->output_bfd
))
2796 /* Allocate room in the fallback TOC section. */
2797 hds
->toc_section
= xcoff_hash_table (info
)->toc_section
;
2798 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2799 hds
->toc_section
->size
+= byte_size
;
2800 if (!xcoff_mark (info
, hds
->toc_section
))
2803 /* Allocate room for a static and dynamic R_TOC
2805 ++xcoff_hash_table (info
)->ldrel_count
;
2806 ++hds
->toc_section
->reloc_count
;
2808 /* Set the index to -2 to force this symbol to
2811 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2814 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) == 0)
2816 /* Record that the symbol was undefined, then import it.
2817 -brtl links use a special fake import file. */
2818 h
->flags
|= XCOFF_WAS_UNDEFINED
| XCOFF_IMPORT
;
2819 if (xcoff_hash_table (info
)->rtld
)
2821 if (!xcoff_set_import_path (info
, h
, "", "..", ""))
2826 if (!xcoff_set_import_path (info
, h
, NULL
, NULL
, NULL
))
2832 if (h
->root
.type
== bfd_link_hash_defined
2833 || h
->root
.type
== bfd_link_hash_defweak
)
2837 hsec
= h
->root
.u
.def
.section
;
2838 if (! bfd_is_abs_section (hsec
)
2839 && (hsec
->flags
& SEC_MARK
) == 0)
2841 if (! xcoff_mark (info
, hsec
))
2846 if (h
->toc_section
!= NULL
2847 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2849 if (! xcoff_mark (info
, h
->toc_section
))
2856 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2857 If the symbol exists, set FLAGS. */
2860 xcoff_mark_symbol_by_name (struct bfd_link_info
*info
,
2861 const char *name
, unsigned int flags
)
2863 struct xcoff_link_hash_entry
*h
;
2865 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
2866 FALSE
, FALSE
, TRUE
);
2870 if (h
->root
.type
== bfd_link_hash_defined
2871 || h
->root
.type
== bfd_link_hash_defweak
)
2873 if (!xcoff_mark (info
, h
->root
.u
.def
.section
))
2880 /* The mark phase of garbage collection. For a given section, mark
2881 it, and all the sections which define symbols to which it refers.
2882 Because this function needs to look at the relocs, we also count
2883 the number of relocs which need to be copied into the .loader
2887 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2889 if (bfd_is_abs_section (sec
)
2890 || (sec
->flags
& SEC_MARK
) != 0)
2893 sec
->flags
|= SEC_MARK
;
2895 if (sec
->owner
->xvec
== info
->output_bfd
->xvec
2896 && coff_section_data (sec
->owner
, sec
) != NULL
2897 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2899 struct xcoff_link_hash_entry
**syms
;
2900 struct internal_reloc
*rel
, *relend
;
2902 unsigned long i
, first
, last
;
2904 /* Mark all the symbols in this section. */
2905 syms
= obj_xcoff_sym_hashes (sec
->owner
);
2906 csects
= xcoff_data (sec
->owner
)->csects
;
2907 first
= xcoff_section_data (sec
->owner
, sec
)->first_symndx
;
2908 last
= xcoff_section_data (sec
->owner
, sec
)->last_symndx
;
2909 for (i
= first
; i
<= last
; i
++)
2910 if (csects
[i
] == sec
2912 && (syms
[i
]->flags
& XCOFF_MARK
) == 0)
2914 if (!xcoff_mark_symbol (info
, syms
[i
]))
2918 /* Look through the section relocs. */
2919 if ((sec
->flags
& SEC_RELOC
) != 0
2920 && sec
->reloc_count
> 0)
2922 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, TRUE
,
2926 relend
= rel
+ sec
->reloc_count
;
2927 for (; rel
< relend
; rel
++)
2929 struct xcoff_link_hash_entry
*h
;
2931 if ((unsigned int) rel
->r_symndx
2932 > obj_raw_syment_count (sec
->owner
))
2935 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2938 if ((h
->flags
& XCOFF_MARK
) == 0)
2940 if (!xcoff_mark_symbol (info
, h
))
2948 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2950 && (rsec
->flags
& SEC_MARK
) == 0)
2952 if (!xcoff_mark (info
, rsec
))
2957 /* See if this reloc needs to be copied into the .loader
2959 if (xcoff_need_ldrel_p (info
, rel
, h
))
2961 ++xcoff_hash_table (info
)->ldrel_count
;
2963 h
->flags
|= XCOFF_LDREL
;
2967 if (! info
->keep_memory
2968 && coff_section_data (sec
->owner
, sec
) != NULL
2969 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2970 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2972 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2973 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2981 /* Routines that are called after all the input files have been
2982 handled, but before the sections are laid out in memory. */
2984 /* The sweep phase of garbage collection. Remove all garbage
2988 xcoff_sweep (struct bfd_link_info
*info
)
2992 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2996 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2998 if ((o
->flags
& SEC_MARK
) == 0)
3000 /* Keep all sections from non-XCOFF input files. Keep
3001 special sections. Keep .debug sections for the
3003 if (sub
->xvec
!= info
->output_bfd
->xvec
3004 || o
== xcoff_hash_table (info
)->debug_section
3005 || o
== xcoff_hash_table (info
)->loader_section
3006 || o
== xcoff_hash_table (info
)->linkage_section
3007 || o
== xcoff_hash_table (info
)->descriptor_section
3008 || strcmp (o
->name
, ".debug") == 0)
3009 o
->flags
|= SEC_MARK
;
3020 /* Record the number of elements in a set. This is used to output the
3021 correct csect length. */
3024 bfd_xcoff_link_record_set (bfd
*output_bfd
,
3025 struct bfd_link_info
*info
,
3026 struct bfd_link_hash_entry
*harg
,
3029 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3030 struct xcoff_link_size_list
*n
;
3033 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3036 /* This will hardly ever be called. I don't want to burn four bytes
3037 per global symbol, so instead the size is kept on a linked list
3038 attached to the hash table. */
3040 n
= bfd_alloc (output_bfd
, amt
);
3043 n
->next
= xcoff_hash_table (info
)->size_list
;
3046 xcoff_hash_table (info
)->size_list
= n
;
3048 h
->flags
|= XCOFF_HAS_SIZE
;
3053 /* Import a symbol. */
3056 bfd_xcoff_import_symbol (bfd
*output_bfd
,
3057 struct bfd_link_info
*info
,
3058 struct bfd_link_hash_entry
*harg
,
3060 const char *imppath
,
3061 const char *impfile
,
3062 const char *impmember
,
3063 unsigned int syscall_flag
)
3065 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3067 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3070 /* A symbol name which starts with a period is the code for a
3071 function. If the symbol is undefined, then add an undefined
3072 symbol for the function descriptor, and import that instead. */
3073 if (h
->root
.root
.string
[0] == '.'
3074 && h
->root
.type
== bfd_link_hash_undefined
3075 && val
== (bfd_vma
) -1)
3077 struct xcoff_link_hash_entry
*hds
;
3079 hds
= h
->descriptor
;
3082 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3083 h
->root
.root
.string
+ 1,
3087 if (hds
->root
.type
== bfd_link_hash_new
)
3089 hds
->root
.type
= bfd_link_hash_undefined
;
3090 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
3092 hds
->flags
|= XCOFF_DESCRIPTOR
;
3093 BFD_ASSERT ((h
->flags
& XCOFF_DESCRIPTOR
) == 0);
3094 hds
->descriptor
= h
;
3095 h
->descriptor
= hds
;
3098 /* Now, if the descriptor is undefined, import the descriptor
3099 rather than the symbol we were told to import. FIXME: Is
3100 this correct in all cases? */
3101 if (hds
->root
.type
== bfd_link_hash_undefined
)
3105 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
3107 if (val
!= (bfd_vma
) -1)
3109 if (h
->root
.type
== bfd_link_hash_defined
3110 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
3111 || h
->root
.u
.def
.value
!= val
))
3113 if (! ((*info
->callbacks
->multiple_definition
)
3114 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
3115 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
3116 output_bfd
, bfd_abs_section_ptr
, val
)))
3120 h
->root
.type
= bfd_link_hash_defined
;
3121 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3122 h
->root
.u
.def
.value
= val
;
3126 if (!xcoff_set_import_path (info
, h
, imppath
, impfile
, impmember
))
3132 /* Export a symbol. */
3135 bfd_xcoff_export_symbol (bfd
*output_bfd
,
3136 struct bfd_link_info
*info
,
3137 struct bfd_link_hash_entry
*harg
)
3139 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3141 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3144 h
->flags
|= XCOFF_EXPORT
;
3146 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3147 I'm just going to ignore it until somebody explains it. */
3149 /* Make sure we don't garbage collect this symbol. */
3150 if (! xcoff_mark_symbol (info
, h
))
3153 /* If this is a function descriptor, make sure we don't garbage
3154 collect the associated function code. We normally don't have to
3155 worry about this, because the descriptor will be attached to a
3156 section with relocs, but if we are creating the descriptor
3157 ourselves those relocs will not be visible to the mark code. */
3158 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3160 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3167 /* Count a reloc against a symbol. This is called for relocs
3168 generated by the linker script, typically for global constructors
3172 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
3173 struct bfd_link_info
*info
,
3176 struct xcoff_link_hash_entry
*h
;
3178 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3181 h
= ((struct xcoff_link_hash_entry
*)
3182 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, FALSE
, FALSE
,
3186 (*_bfd_error_handler
) (_("%s: no such symbol"), name
);
3187 bfd_set_error (bfd_error_no_symbols
);
3191 h
->flags
|= XCOFF_REF_REGULAR
;
3192 if (xcoff_hash_table (info
)->loader_section
)
3194 h
->flags
|= XCOFF_LDREL
;
3195 ++xcoff_hash_table (info
)->ldrel_count
;
3198 /* Mark the symbol to avoid garbage collection. */
3199 if (! xcoff_mark_symbol (info
, h
))
3205 /* This function is called for each symbol to which the linker script
3209 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
3210 struct bfd_link_info
*info
,
3213 struct xcoff_link_hash_entry
*h
;
3215 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3218 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
, TRUE
,
3223 h
->flags
|= XCOFF_DEF_REGULAR
;
3228 /* An xcoff_link_hash_traverse callback for which DATA points to an
3229 xcoff_loader_info. Mark all symbols that should be automatically
3233 xcoff_mark_auto_exports (struct xcoff_link_hash_entry
*h
, void *data
)
3235 struct xcoff_loader_info
*ldinfo
;
3237 ldinfo
= (struct xcoff_loader_info
*) data
;
3238 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3240 if (!xcoff_mark_symbol (ldinfo
->info
, h
))
3241 ldinfo
->failed
= TRUE
;
3246 /* Add a symbol to the .loader symbols, if necessary. */
3248 /* INPUT_BFD has an external symbol associated with hash table entry H
3249 and csect CSECT. Return true if INPUT_BFD defines H. */
3252 xcoff_final_definition_p (bfd
*input_bfd
, struct xcoff_link_hash_entry
*h
,
3255 switch (h
->root
.type
)
3257 case bfd_link_hash_defined
:
3258 case bfd_link_hash_defweak
:
3259 /* No input bfd owns absolute symbols. They are written by
3260 xcoff_write_global_symbol instead. */
3261 return (!bfd_is_abs_section (csect
)
3262 && h
->root
.u
.def
.section
== csect
);
3264 case bfd_link_hash_common
:
3265 return h
->root
.u
.c
.p
->section
->owner
== input_bfd
;
3267 case bfd_link_hash_undefined
:
3268 case bfd_link_hash_undefweak
:
3269 /* We can't treat undef.abfd as the owner because that bfd
3270 might be a dynamic object. Allow any bfd to claim it. */
3278 /* See if H should have a loader symbol associated with it. */
3281 xcoff_build_ldsym (struct xcoff_loader_info
*ldinfo
,
3282 struct xcoff_link_hash_entry
*h
)
3286 /* Warn if this symbol is exported but not defined. */
3287 if ((h
->flags
& XCOFF_EXPORT
) != 0
3288 && (h
->flags
& XCOFF_WAS_UNDEFINED
) != 0)
3290 (*_bfd_error_handler
)
3291 (_("warning: attempt to export undefined symbol `%s'"),
3292 h
->root
.root
.string
);
3296 /* We need to add a symbol to the .loader section if it is mentioned
3297 in a reloc which we are copying to the .loader section and it was
3298 not defined or common, or if it is the entry point, or if it is
3300 if (((h
->flags
& XCOFF_LDREL
) == 0
3301 || h
->root
.type
== bfd_link_hash_defined
3302 || h
->root
.type
== bfd_link_hash_defweak
3303 || h
->root
.type
== bfd_link_hash_common
)
3304 && (h
->flags
& XCOFF_ENTRY
) == 0
3305 && (h
->flags
& XCOFF_EXPORT
) == 0)
3308 /* We need to add this symbol to the .loader symbols. */
3310 BFD_ASSERT (h
->ldsym
== NULL
);
3311 amt
= sizeof (struct internal_ldsym
);
3312 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
3313 if (h
->ldsym
== NULL
)
3315 ldinfo
->failed
= TRUE
;
3319 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3321 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3322 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3324 h
->ldsym
->l_ifile
= h
->ldindx
;
3327 /* The first 3 symbol table indices are reserved to indicate the
3328 data, text and bss sections. */
3329 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3331 ++ldinfo
->ldsym_count
;
3333 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
3334 h
->ldsym
, h
->root
.root
.string
))
3337 h
->flags
|= XCOFF_BUILT_LDSYM
;
3341 /* An xcoff_htab_traverse callback that is called for each symbol
3342 once garbage collection is complete. */
3345 xcoff_post_gc_symbol (struct xcoff_link_hash_entry
*h
, void * p
)
3347 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3349 if (h
->root
.type
== bfd_link_hash_warning
)
3350 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
3352 /* __rtinit, this symbol has special handling. */
3353 if (h
->flags
& XCOFF_RTINIT
)
3356 /* We don't want to garbage collect symbols which are not defined in
3357 XCOFF files. This is a convenient place to mark them. */
3358 if (xcoff_hash_table (ldinfo
->info
)->gc
3359 && (h
->flags
& XCOFF_MARK
) == 0
3360 && (h
->root
.type
== bfd_link_hash_defined
3361 || h
->root
.type
== bfd_link_hash_defweak
)
3362 && (h
->root
.u
.def
.section
->owner
== NULL
3363 || (h
->root
.u
.def
.section
->owner
->xvec
3364 != ldinfo
->info
->output_bfd
->xvec
)))
3365 h
->flags
|= XCOFF_MARK
;
3367 /* Skip discarded symbols. */
3368 if (xcoff_hash_table (ldinfo
->info
)->gc
3369 && (h
->flags
& XCOFF_MARK
) == 0)
3372 /* If this is still a common symbol, and it wasn't garbage
3373 collected, we need to actually allocate space for it in the .bss
3375 if (h
->root
.type
== bfd_link_hash_common
3376 && h
->root
.u
.c
.p
->section
->size
== 0)
3378 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3379 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
3382 if (xcoff_hash_table (ldinfo
->info
)->loader_section
)
3384 if (xcoff_auto_export_p (ldinfo
->info
, h
, ldinfo
->auto_export_flags
))
3385 h
->flags
|= XCOFF_EXPORT
;
3387 if (!xcoff_build_ldsym (ldinfo
, h
))
3394 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3395 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3396 csect information, if any. NAME is the function's name if the name
3397 is stored in the .debug section, otherwise it is null.
3399 Return 1 if we should include an appropriately-adjusted ISYM
3400 in the output file, 0 if we should discard ISYM, or -1 if an
3404 xcoff_keep_symbol_p (struct bfd_link_info
*info
, bfd
*input_bfd
,
3405 struct internal_syment
*isym
,
3406 union internal_auxent
*aux
,
3407 struct xcoff_link_hash_entry
*h
,
3408 asection
*csect
, const char *name
)
3412 /* If we are skipping this csect, we want to strip the symbol too. */
3416 /* Likewise if we garbage-collected the csect. */
3417 if (xcoff_hash_table (info
)->gc
3418 && !bfd_is_abs_section (csect
)
3419 && !bfd_is_und_section (csect
)
3420 && (csect
->flags
& SEC_MARK
) == 0)
3423 /* An XCOFF linker always removes C_STAT symbols. */
3424 if (isym
->n_sclass
== C_STAT
)
3427 /* We generate the TOC anchor separately. */
3428 if (isym
->n_sclass
== C_HIDEXT
3429 && aux
->x_csect
.x_smclas
== XMC_TC0
)
3432 /* If we are stripping all symbols, we want to discard this one. */
3433 if (info
->strip
== strip_all
)
3436 /* Discard symbols that are defined elsewhere. */
3437 if (EXTERN_SYM_P (isym
->n_sclass
))
3439 if ((h
->flags
& XCOFF_ALLOCATED
) != 0)
3441 if (!xcoff_final_definition_p (input_bfd
, h
, csect
))
3445 /* If we're discarding local symbols, check whether ISYM is local. */
3446 smtyp
= SMTYP_SMTYP (aux
->x_csect
.x_smtyp
);
3447 if (info
->discard
== discard_all
3448 && !EXTERN_SYM_P (isym
->n_sclass
)
3449 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
))
3452 /* If we're stripping debugging symbols, check whether ISYM is one. */
3453 if (info
->strip
== strip_debugger
3454 && isym
->n_scnum
== N_DEBUG
)
3457 /* If we are stripping symbols based on name, check how ISYM's
3458 name should be handled. */
3459 if (info
->strip
== strip_some
3460 || info
->discard
== discard_l
)
3462 char buf
[SYMNMLEN
+ 1];
3466 name
= _bfd_coff_internal_syment_name (input_bfd
, isym
, buf
);
3471 if (info
->strip
== strip_some
3472 && bfd_hash_lookup (info
->keep_hash
, name
, FALSE
, FALSE
) == NULL
)
3475 if (info
->discard
== discard_l
3476 && !EXTERN_SYM_P (isym
->n_sclass
)
3477 && (isym
->n_sclass
!= C_HIDEXT
|| smtyp
!= XTY_SD
)
3478 && bfd_is_local_label_name (input_bfd
, name
))
3485 /* Lay out the .loader section, filling in the header and the import paths.
3486 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3489 xcoff_build_loader_section (struct xcoff_loader_info
*ldinfo
,
3490 const char *libpath
)
3493 struct xcoff_link_hash_table
*htab
;
3494 struct internal_ldhdr
*ldhdr
;
3495 struct xcoff_import_file
*fl
;
3496 bfd_size_type stoff
;
3497 size_t impsize
, impcount
;
3501 /* Work out the size of the import file names. Each import file ID
3502 consists of three null terminated strings: the path, the file
3503 name, and the archive member name. The first entry in the list
3504 of names is the path to use to find objects, which the linker has
3505 passed in as the libpath argument. For some reason, the path
3506 entry in the other import file names appears to always be empty. */
3507 output_bfd
= ldinfo
->output_bfd
;
3508 htab
= xcoff_hash_table (ldinfo
->info
);
3509 impsize
= strlen (libpath
) + 3;
3511 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3514 impsize
+= (strlen (fl
->path
)
3516 + strlen (fl
->member
)
3520 /* Set up the .loader section header. */
3521 ldhdr
= &htab
->ldhdr
;
3522 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3523 ldhdr
->l_nsyms
= ldinfo
->ldsym_count
;
3524 ldhdr
->l_nreloc
= htab
->ldrel_count
;
3525 ldhdr
->l_istlen
= impsize
;
3526 ldhdr
->l_nimpid
= impcount
;
3527 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3528 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
)
3529 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz (output_bfd
));
3530 ldhdr
->l_stlen
= ldinfo
->string_size
;
3531 stoff
= ldhdr
->l_impoff
+ impsize
;
3532 if (ldinfo
->string_size
== 0)
3535 ldhdr
->l_stoff
= stoff
;
3537 /* 64 bit elements to ldhdr
3538 The swap out routine for 32 bit will ignore them.
3539 Nothing fancy, symbols come after the header and relocs come
3541 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3542 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3543 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3545 /* We now know the final size of the .loader section. Allocate
3547 lsec
= htab
->loader_section
;
3548 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3549 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3550 if (lsec
->contents
== NULL
)
3553 /* Set up the header. */
3554 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3556 /* Set up the import file names. */
3557 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3558 strcpy (out
, libpath
);
3559 out
+= strlen (libpath
) + 1;
3562 for (fl
= htab
->imports
; fl
!= NULL
; fl
= fl
->next
)
3567 while ((*out
++ = *s
++) != '\0')
3570 while ((*out
++ = *s
++) != '\0')
3573 while ((*out
++ = *s
++) != '\0')
3577 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3579 /* Set up the symbol string table. */
3580 if (ldinfo
->string_size
> 0)
3582 memcpy (out
, ldinfo
->strings
, ldinfo
->string_size
);
3583 free (ldinfo
->strings
);
3584 ldinfo
->strings
= NULL
;
3587 /* We can't set up the symbol table or the relocs yet, because we
3588 don't yet know the final position of the various sections. The
3589 .loader symbols are written out when the corresponding normal
3590 symbols are written out in xcoff_link_input_bfd or
3591 xcoff_write_global_symbol. The .loader relocs are written out
3592 when the corresponding normal relocs are handled in
3593 xcoff_link_input_bfd. */
3598 /* Build the .loader section. This is called by the XCOFF linker
3599 emulation before_allocation routine. We must set the size of the
3600 .loader section before the linker lays out the output file.
3601 LIBPATH is the library path to search for shared objects; this is
3602 normally built from the -L arguments passed to the linker. ENTRY
3603 is the name of the entry point symbol (the -e linker option).
3604 FILE_ALIGN is the alignment to use for sections within the file
3605 (the -H linker option). MAXSTACK is the maximum stack size (the
3606 -bmaxstack linker option). MAXDATA is the maximum data size (the
3607 -bmaxdata linker option). GC is whether to do garbage collection
3608 (the -bgc linker option). MODTYPE is the module type (the
3609 -bmodtype linker option). TEXTRO is whether the text section must
3610 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3611 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3612 is set by this routine to csects with magic names like _end. */
3615 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
3616 struct bfd_link_info
*info
,
3617 const char *libpath
,
3619 unsigned long file_align
,
3620 unsigned long maxstack
,
3621 unsigned long maxdata
,
3625 unsigned int auto_export_flags
,
3626 asection
**special_sections
,
3629 struct xcoff_loader_info ldinfo
;
3633 struct bfd_strtab_hash
*debug_strtab
;
3634 bfd_byte
*debug_contents
= NULL
;
3637 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3639 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3640 special_sections
[i
] = NULL
;
3644 ldinfo
.failed
= FALSE
;
3645 ldinfo
.output_bfd
= output_bfd
;
3647 ldinfo
.auto_export_flags
= auto_export_flags
;
3648 ldinfo
.ldsym_count
= 0;
3649 ldinfo
.string_size
= 0;
3650 ldinfo
.strings
= NULL
;
3651 ldinfo
.string_alc
= 0;
3653 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3654 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3655 xcoff_data (output_bfd
)->modtype
= modtype
;
3657 xcoff_hash_table (info
)->file_align
= file_align
;
3658 xcoff_hash_table (info
)->textro
= textro
;
3659 xcoff_hash_table (info
)->rtld
= rtld
;
3662 if (xcoff_hash_table (info
)->loader_section
3663 && (info
->init_function
|| info
->fini_function
|| rtld
))
3665 struct xcoff_link_hash_entry
*hsym
;
3666 struct internal_ldsym
*ldsym
;
3668 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3669 "__rtinit", FALSE
, FALSE
, TRUE
);
3672 (*_bfd_error_handler
)
3673 (_("error: undefined symbol __rtinit"));
3677 xcoff_mark_symbol (info
, hsym
);
3678 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3680 /* __rtinit initialized. */
3681 amt
= sizeof (* ldsym
);
3682 ldsym
= bfd_malloc (amt
);
3684 ldsym
->l_value
= 0; /* Will be filled in later. */
3685 ldsym
->l_scnum
= 2; /* Data section. */
3686 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3687 ldsym
->l_smclas
= 5; /* .rw. */
3688 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3689 ldsym
->l_parm
= 0; /* NA. */
3691 /* Force __rtinit to be the first symbol in the loader symbol table
3692 See xcoff_build_ldsyms
3694 The first 3 symbol table indices are reserved to indicate the data,
3695 text and bss sections. */
3696 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3699 ldinfo
.ldsym_count
= 1;
3700 hsym
->ldsym
= ldsym
;
3702 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3703 hsym
->ldsym
, hsym
->root
.root
.string
))
3706 /* This symbol is written out by xcoff_write_global_symbol
3707 Set stuff up so xcoff_write_global_symbol logic works. */
3708 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3709 hsym
->root
.type
= bfd_link_hash_defined
;
3710 hsym
->root
.u
.def
.value
= 0;
3713 /* Garbage collect unused sections. */
3714 if (info
->relocatable
|| !gc
)
3717 xcoff_hash_table (info
)->gc
= FALSE
;
3719 /* We still need to call xcoff_mark, in order to set ldrel_count
3721 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3725 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3727 /* We shouldn't unconditionaly mark the TOC section.
3728 The output file should only have a TOC if either
3729 (a) one of the input files did or (b) we end up
3730 creating TOC references as part of the link process. */
3731 if (o
!= xcoff_hash_table (info
)->toc_section
3732 && (o
->flags
& SEC_MARK
) == 0)
3734 if (! xcoff_mark (info
, o
))
3743 && !xcoff_mark_symbol_by_name (info
, entry
, XCOFF_ENTRY
))
3745 if (info
->init_function
!= NULL
3746 && !xcoff_mark_symbol_by_name (info
, info
->init_function
, 0))
3748 if (info
->fini_function
!= NULL
3749 && !xcoff_mark_symbol_by_name (info
, info
->fini_function
, 0))
3751 if (auto_export_flags
!= 0)
3753 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3754 xcoff_mark_auto_exports
, &ldinfo
);
3759 xcoff_hash_table (info
)->gc
= TRUE
;
3762 /* Return special sections to the caller. */
3763 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3765 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3769 && (sec
->flags
& SEC_MARK
) == 0)
3772 special_sections
[i
] = sec
;
3775 if (info
->input_bfds
== NULL
)
3776 /* I'm not sure what to do in this bizarre case. */
3779 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_post_gc_symbol
,
3784 if (xcoff_hash_table (info
)->loader_section
3785 && !xcoff_build_loader_section (&ldinfo
, libpath
))
3788 /* Allocate space for the magic sections. */
3789 sec
= xcoff_hash_table (info
)->linkage_section
;
3792 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3793 if (sec
->contents
== NULL
)
3796 sec
= xcoff_hash_table (info
)->toc_section
;
3799 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3800 if (sec
->contents
== NULL
)
3803 sec
= xcoff_hash_table (info
)->descriptor_section
;
3806 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3807 if (sec
->contents
== NULL
)
3811 /* Now that we've done garbage collection, decide which symbols to keep,
3812 and figure out the contents of the .debug section. */
3813 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3815 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3818 bfd_size_type symcount
;
3821 unsigned int *lineno_counts
;
3822 struct xcoff_link_hash_entry
**sym_hash
;
3823 bfd_byte
*esym
, *esymend
;
3824 bfd_size_type symesz
;
3826 if (sub
->xvec
!= info
->output_bfd
->xvec
)
3829 if ((sub
->flags
& DYNAMIC
) != 0
3830 && !info
->static_link
)
3833 if (! _bfd_coff_get_external_symbols (sub
))
3836 symcount
= obj_raw_syment_count (sub
);
3837 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (long));
3838 if (debug_index
== NULL
)
3840 xcoff_data (sub
)->debug_indices
= debug_index
;
3842 if (info
->strip
== strip_all
3843 || info
->strip
== strip_debugger
3844 || info
->discard
== discard_all
)
3845 /* We're stripping all debugging information, so there's no need
3846 to read SUB's .debug section. */
3850 /* Grab the contents of SUB's .debug section, if any. */
3851 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3852 if (subdeb
!= NULL
&& subdeb
->size
> 0)
3854 /* We use malloc and copy the names into the debug
3855 stringtab, rather than bfd_alloc, because I expect
3856 that, when linking many files together, many of the
3857 strings will be the same. Storing the strings in the
3858 hash table should save space in this case. */
3859 if (!bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
3864 csectpp
= xcoff_data (sub
)->csects
;
3865 lineno_counts
= xcoff_data (sub
)->lineno_counts
;
3866 sym_hash
= obj_xcoff_sym_hashes (sub
);
3867 symesz
= bfd_coff_symesz (sub
);
3868 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3869 esymend
= esym
+ symcount
* symesz
;
3871 while (esym
< esymend
)
3873 struct internal_syment sym
;
3874 union internal_auxent aux
;
3879 bfd_coff_swap_sym_in (sub
, esym
, &sym
);
3881 /* Read in the csect information, if any. */
3882 if (CSECT_SYM_P (sym
.n_sclass
))
3884 BFD_ASSERT (sym
.n_numaux
> 0);
3885 bfd_coff_swap_aux_in (sub
, esym
+ symesz
* sym
.n_numaux
,
3886 sym
.n_type
, sym
.n_sclass
,
3887 sym
.n_numaux
- 1, sym
.n_numaux
, &aux
);
3890 /* If this symbol's name is stored in the debug section,
3891 get a pointer to it. */
3892 if (debug_contents
!= NULL
3893 && sym
._n
._n_n
._n_zeroes
== 0
3894 && bfd_coff_symname_in_debug (sub
, &sym
))
3895 name
= (const char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3899 /* Decide whether to copy this symbol to the output file. */
3901 keep_p
= xcoff_keep_symbol_p (info
, sub
, &sym
, &aux
,
3902 *sym_hash
, csect
, name
);
3907 /* Use a debug_index of -2 to record that a symbol should
3912 /* See whether we should store the symbol name in the
3913 output .debug section. */
3918 indx
= _bfd_stringtab_add (debug_strtab
, name
, TRUE
, TRUE
);
3919 if (indx
== (bfd_size_type
) -1)
3921 *debug_index
= indx
;
3926 (*sym_hash
)->flags
|= XCOFF_ALLOCATED
;
3927 if (*lineno_counts
> 0)
3928 csect
->output_section
->lineno_count
+= *lineno_counts
;
3931 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3932 csectpp
+= sym
.n_numaux
+ 1;
3933 sym_hash
+= sym
.n_numaux
+ 1;
3934 lineno_counts
+= sym
.n_numaux
+ 1;
3935 debug_index
+= sym
.n_numaux
+ 1;
3940 free (debug_contents
);
3941 debug_contents
= NULL
;
3943 /* Clear the size of subdeb, so that it is not included directly
3944 in the output file. */
3948 if (! info
->keep_memory
)
3950 if (! _bfd_coff_free_symbols (sub
))
3955 if (info
->strip
!= strip_all
)
3956 xcoff_hash_table (info
)->debug_section
->size
=
3957 _bfd_stringtab_size (debug_strtab
);
3962 if (ldinfo
.strings
!= NULL
)
3963 free (ldinfo
.strings
);
3964 if (debug_contents
!= NULL
)
3965 free (debug_contents
);
3970 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
3975 struct bfd_in_memory
*bim
;
3977 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
3984 abfd
->link_next
= 0;
3985 abfd
->format
= bfd_object
;
3986 abfd
->iostream
= (void *) bim
;
3987 abfd
->flags
= BFD_IN_MEMORY
;
3988 abfd
->iovec
= &_bfd_memory_iovec
;
3989 abfd
->direction
= write_direction
;
3993 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
3996 /* need to reset to unknown or it will not be read back in correctly */
3997 abfd
->format
= bfd_unknown
;
3998 abfd
->direction
= read_direction
;
4004 /* Return the section that defines H. Return null if no section does. */
4007 xcoff_symbol_section (struct xcoff_link_hash_entry
*h
)
4009 switch (h
->root
.type
)
4011 case bfd_link_hash_defined
:
4012 case bfd_link_hash_defweak
:
4013 return h
->root
.u
.def
.section
;
4015 case bfd_link_hash_common
:
4016 return h
->root
.u
.c
.p
->section
;
4023 /* Add a .loader relocation for input relocation IREL. If the loader
4024 relocation should be against an output section, HSEC points to the
4025 input section that IREL is against, otherwise HSEC is null. H is the
4026 symbol that IREL is against, or null if it isn't against a global symbol.
4027 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4030 xcoff_create_ldrel (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
,
4031 asection
*output_section
, bfd
*reference_bfd
,
4032 struct internal_reloc
*irel
, asection
*hsec
,
4033 struct xcoff_link_hash_entry
*h
)
4035 struct internal_ldrel ldrel
;
4037 ldrel
.l_vaddr
= irel
->r_vaddr
;
4040 const char *secname
;
4042 secname
= hsec
->output_section
->name
;
4043 if (strcmp (secname
, ".text") == 0)
4045 else if (strcmp (secname
, ".data") == 0)
4047 else if (strcmp (secname
, ".bss") == 0)
4051 (*_bfd_error_handler
)
4052 (_("%B: loader reloc in unrecognized section `%s'"),
4053 reference_bfd
, secname
);
4054 bfd_set_error (bfd_error_nonrepresentable_section
);
4062 (*_bfd_error_handler
)
4063 (_("%B: `%s' in loader reloc but not loader sym"),
4064 reference_bfd
, h
->root
.root
.string
);
4065 bfd_set_error (bfd_error_bad_value
);
4068 ldrel
.l_symndx
= h
->ldindx
;
4071 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4073 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4074 ldrel
.l_rsecnm
= output_section
->target_index
;
4075 if (xcoff_hash_table (flinfo
->info
)->textro
4076 && strcmp (output_section
->name
, ".text") == 0)
4078 (*_bfd_error_handler
)
4079 (_("%B: loader reloc in read-only section %A"),
4080 reference_bfd
, output_section
);
4081 bfd_set_error (bfd_error_invalid_operation
);
4084 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, flinfo
->ldrel
);
4085 flinfo
->ldrel
+= bfd_xcoff_ldrelsz (output_bfd
);
4089 /* Link an input file into the linker output file. This function
4090 handles all the sections and relocations of the input file at once. */
4093 xcoff_link_input_bfd (struct xcoff_final_link_info
*flinfo
,
4097 const char *strings
;
4098 bfd_size_type syment_base
;
4099 unsigned int n_tmask
;
4100 unsigned int n_btshft
;
4101 bfd_boolean copy
, hash
;
4102 bfd_size_type isymesz
;
4103 bfd_size_type osymesz
;
4104 bfd_size_type linesz
;
4107 struct xcoff_link_hash_entry
**sym_hash
;
4108 struct internal_syment
*isymp
;
4110 unsigned int *lineno_counts
;
4113 unsigned long output_index
;
4117 bfd_boolean keep_syms
;
4120 /* We can just skip DYNAMIC files, unless this is a static link. */
4121 if ((input_bfd
->flags
& DYNAMIC
) != 0
4122 && ! flinfo
->info
->static_link
)
4125 /* Move all the symbols to the output file. */
4126 output_bfd
= flinfo
->output_bfd
;
4128 syment_base
= obj_raw_syment_count (output_bfd
);
4129 isymesz
= bfd_coff_symesz (input_bfd
);
4130 osymesz
= bfd_coff_symesz (output_bfd
);
4131 linesz
= bfd_coff_linesz (input_bfd
);
4132 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4134 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4135 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4137 /* Define macros so that ISFCN, et. al., macros work correctly. */
4138 #define N_TMASK n_tmask
4139 #define N_BTSHFT n_btshft
4142 if (! flinfo
->info
->keep_memory
)
4145 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4148 if (! _bfd_coff_get_external_symbols (input_bfd
))
4151 /* Make one pass over the symbols and assign indices to symbols that
4152 we have decided to keep. Also use create .loader symbol information
4153 and update information in hash table entries. */
4154 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4155 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4156 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4157 csectpp
= xcoff_data (input_bfd
)->csects
;
4158 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4159 isymp
= flinfo
->internal_syms
;
4160 indexp
= flinfo
->sym_indices
;
4161 output_index
= syment_base
;
4162 while (esym
< esym_end
)
4164 union internal_auxent aux
;
4168 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
4170 /* Read in the csect information, if any. */
4171 if (CSECT_SYM_P (isymp
->n_sclass
))
4173 BFD_ASSERT (isymp
->n_numaux
> 0);
4174 bfd_coff_swap_aux_in (input_bfd
,
4175 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
4176 isymp
->n_type
, isymp
->n_sclass
,
4177 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4180 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4183 /* If this symbol is in the .loader section, swap out the
4184 .loader symbol information. If this is an external symbol
4185 reference to a defined symbol, though, then wait until we get
4186 to the definition. */
4187 if (EXTERN_SYM_P (isymp
->n_sclass
)
4188 && *sym_hash
!= NULL
4189 && (*sym_hash
)->ldsym
!= NULL
4190 && xcoff_final_definition_p (input_bfd
, *sym_hash
, *csectpp
))
4192 struct xcoff_link_hash_entry
*h
;
4193 struct internal_ldsym
*ldsym
;
4197 if (isymp
->n_scnum
> 0)
4199 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4200 ldsym
->l_value
= (isymp
->n_value
4201 + (*csectpp
)->output_section
->vma
4202 + (*csectpp
)->output_offset
4207 ldsym
->l_scnum
= isymp
->n_scnum
;
4208 ldsym
->l_value
= isymp
->n_value
;
4211 ldsym
->l_smtype
= smtyp
;
4212 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4213 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4214 || (h
->flags
& XCOFF_IMPORT
) != 0)
4215 ldsym
->l_smtype
|= L_IMPORT
;
4216 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4217 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4218 || (h
->flags
& XCOFF_EXPORT
) != 0)
4219 ldsym
->l_smtype
|= L_EXPORT
;
4220 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4221 ldsym
->l_smtype
|= L_ENTRY
;
4222 if (isymp
->n_sclass
== C_AIX_WEAKEXT
)
4223 ldsym
->l_smtype
|= L_WEAK
;
4225 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4227 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4229 else if (ldsym
->l_ifile
== 0)
4231 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4237 if (h
->root
.type
== bfd_link_hash_defined
4238 || h
->root
.type
== bfd_link_hash_defweak
)
4239 impbfd
= h
->root
.u
.def
.section
->owner
;
4240 else if (h
->root
.type
== bfd_link_hash_undefined
4241 || h
->root
.type
== bfd_link_hash_undefweak
)
4242 impbfd
= h
->root
.u
.undef
.abfd
;
4250 BFD_ASSERT (impbfd
->xvec
== flinfo
->output_bfd
->xvec
);
4251 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4258 BFD_ASSERT (h
->ldindx
>= 0);
4259 bfd_xcoff_swap_ldsym_out (flinfo
->output_bfd
, ldsym
,
4262 * bfd_xcoff_ldsymsz (flinfo
->output_bfd
))));
4265 /* Fill in snentry now that we know the target_index. */
4266 if ((h
->flags
& XCOFF_ENTRY
) != 0
4267 && (h
->root
.type
== bfd_link_hash_defined
4268 || h
->root
.type
== bfd_link_hash_defweak
))
4270 xcoff_data (output_bfd
)->snentry
=
4271 h
->root
.u
.def
.section
->output_section
->target_index
;
4275 add
= 1 + isymp
->n_numaux
;
4277 if (*debug_index
== -2)
4278 /* We've decided to strip this symbol. */
4282 /* Assign the next unused index to this symbol. */
4283 *indexp
= output_index
;
4285 if (EXTERN_SYM_P (isymp
->n_sclass
))
4287 BFD_ASSERT (*sym_hash
!= NULL
);
4288 (*sym_hash
)->indx
= output_index
;
4291 /* If this is a symbol in the TOC which we may have merged
4292 (class XMC_TC), remember the symbol index of the TOC
4294 if (isymp
->n_sclass
== C_HIDEXT
4295 && aux
.x_csect
.x_smclas
== XMC_TC
4296 && *sym_hash
!= NULL
)
4298 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4299 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4300 (*sym_hash
)->u
.toc_indx
= output_index
;
4303 output_index
+= add
;
4306 esym
+= add
* isymesz
;
4312 for (--add
; add
> 0; --add
)
4316 /* Now write out the symbols that we decided to keep. */
4318 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4319 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4320 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4321 isymp
= flinfo
->internal_syms
;
4322 indexp
= flinfo
->sym_indices
;
4323 csectpp
= xcoff_data (input_bfd
)->csects
;
4324 lineno_counts
= xcoff_data (input_bfd
)->lineno_counts
;
4325 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4326 outsym
= flinfo
->outsyms
;
4329 while (esym
< esym_end
)
4333 add
= 1 + isymp
->n_numaux
;
4336 esym
+= add
* isymesz
;
4339 struct internal_syment isym
;
4342 /* Adjust the symbol in order to output it. */
4344 if (isym
._n
._n_n
._n_zeroes
== 0
4345 && isym
._n
._n_n
._n_offset
!= 0)
4347 /* This symbol has a long name. Enter it in the string
4348 table we are building. If *debug_index != -1, the
4349 name has already been entered in the .debug section. */
4350 if (*debug_index
>= 0)
4351 isym
._n
._n_n
._n_offset
= *debug_index
;
4357 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
4361 indx
= _bfd_stringtab_add (flinfo
->strtab
, name
, hash
, copy
);
4362 if (indx
== (bfd_size_type
) -1)
4364 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4368 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4369 multiple definition problems when linking a shared object
4370 statically. (The native linker doesn't enter __rtinit into
4371 the normal table at all, but having a local symbol can make
4372 the objdump output easier to read.) */
4373 if (isym
.n_sclass
== C_EXT
4375 && ((*sym_hash
)->flags
& XCOFF_RTINIT
) != 0)
4376 isym
.n_sclass
= C_HIDEXT
;
4378 /* The value of a C_FILE symbol is the symbol index of the
4379 next C_FILE symbol. The value of the last C_FILE symbol
4380 is -1. We try to get this right, below, just before we
4381 write the symbols out, but in the general case we may
4382 have to write the symbol out twice. */
4383 if (isym
.n_sclass
== C_FILE
)
4385 if (flinfo
->last_file_index
!= -1
4386 && flinfo
->last_file
.n_value
!= (bfd_vma
) *indexp
)
4388 /* We must correct the value of the last C_FILE entry. */
4389 flinfo
->last_file
.n_value
= *indexp
;
4390 if ((bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4392 /* The last C_FILE symbol is in this input file. */
4393 bfd_coff_swap_sym_out (output_bfd
,
4394 (void *) &flinfo
->last_file
,
4395 (void *) (flinfo
->outsyms
4396 + ((flinfo
->last_file_index
4402 /* We have already written out the last C_FILE
4403 symbol. We need to write it out again. We
4404 borrow *outsym temporarily. */
4407 bfd_coff_swap_sym_out (output_bfd
,
4408 (void *) &flinfo
->last_file
,
4411 pos
= obj_sym_filepos (output_bfd
);
4412 pos
+= flinfo
->last_file_index
* osymesz
;
4413 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4414 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
4420 flinfo
->last_file_index
= *indexp
;
4421 flinfo
->last_file
= isym
;
4424 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4425 into the line numbers. We update the symbol values when
4426 we handle the line numbers. */
4427 if (isym
.n_sclass
== C_BINCL
4428 || isym
.n_sclass
== C_EINCL
)
4430 isym
.n_value
= flinfo
->line_filepos
;
4433 /* The value of a C_BSTAT symbol is the symbol table
4434 index of the containing csect. */
4435 else if (isym
.n_sclass
== C_BSTAT
)
4439 indx
= isym
.n_value
;
4440 if (indx
< obj_raw_syment_count (input_bfd
))
4444 symindx
= flinfo
->sym_indices
[indx
];
4448 isym
.n_value
= symindx
;
4451 else if (isym
.n_sclass
!= C_ESTAT
4452 && isym
.n_sclass
!= C_DECL
4453 && isym
.n_scnum
> 0)
4455 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4456 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4457 + (*csectpp
)->output_offset
4461 /* Output the symbol. */
4462 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
4467 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4469 union internal_auxent aux
;
4471 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
4472 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4475 if (isymp
->n_sclass
== C_FILE
)
4477 /* This is the file name (or some comment put in by
4478 the compiler). If it is long, we must put it in
4479 the string table. */
4480 if (aux
.x_file
.x_n
.x_zeroes
== 0
4481 && aux
.x_file
.x_n
.x_offset
!= 0)
4483 const char *filename
;
4486 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4487 >= STRING_SIZE_SIZE
);
4488 if (strings
== NULL
)
4490 strings
= _bfd_coff_read_string_table (input_bfd
);
4491 if (strings
== NULL
)
4494 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4495 indx
= _bfd_stringtab_add (flinfo
->strtab
, filename
,
4497 if (indx
== (bfd_size_type
) -1)
4499 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4502 else if (CSECT_SYM_P (isymp
->n_sclass
)
4503 && i
+ 1 == isymp
->n_numaux
)
4506 /* We don't support type checking. I don't know if
4508 aux
.x_csect
.x_parmhash
= 0;
4509 /* I don't think anybody uses these fields, but we'd
4510 better clobber them just in case. */
4511 aux
.x_csect
.x_stab
= 0;
4512 aux
.x_csect
.x_snstab
= 0;
4514 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4518 indx
= aux
.x_csect
.x_scnlen
.l
;
4519 if (indx
< obj_raw_syment_count (input_bfd
))
4523 symindx
= flinfo
->sym_indices
[indx
];
4526 aux
.x_csect
.x_scnlen
.l
= 0;
4530 aux
.x_csect
.x_scnlen
.l
= symindx
;
4535 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4539 if (ISFCN (isymp
->n_type
)
4540 || ISTAG (isymp
->n_sclass
)
4541 || isymp
->n_sclass
== C_BLOCK
4542 || isymp
->n_sclass
== C_FCN
)
4544 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4546 && indx
< obj_raw_syment_count (input_bfd
))
4548 /* We look forward through the symbol for
4549 the index of the next symbol we are going
4550 to include. I don't know if this is
4552 while (flinfo
->sym_indices
[indx
] < 0
4553 && indx
< obj_raw_syment_count (input_bfd
))
4555 if (indx
>= obj_raw_syment_count (input_bfd
))
4556 indx
= output_index
;
4558 indx
= flinfo
->sym_indices
[indx
];
4559 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4564 indx
= aux
.x_sym
.x_tagndx
.l
;
4565 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4569 symindx
= flinfo
->sym_indices
[indx
];
4571 aux
.x_sym
.x_tagndx
.l
= 0;
4573 aux
.x_sym
.x_tagndx
.l
= symindx
;
4578 /* Copy over the line numbers, unless we are stripping
4579 them. We do this on a symbol by symbol basis in
4580 order to more easily handle garbage collection. */
4581 if (CSECT_SYM_P (isymp
->n_sclass
)
4583 && isymp
->n_numaux
> 1
4584 && ISFCN (isymp
->n_type
)
4585 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4587 if (*lineno_counts
== 0)
4588 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4591 asection
*enclosing
;
4592 unsigned int enc_count
;
4593 bfd_signed_vma linoff
;
4594 struct internal_lineno lin
;
4601 /* Read in the enclosing section's line-number
4602 information, if we haven't already. */
4604 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4605 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4606 if (oline
!= enclosing
)
4608 pos
= enclosing
->line_filepos
;
4609 amt
= linesz
* enc_count
;
4610 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4611 || (bfd_bread (flinfo
->linenos
, amt
, input_bfd
)
4617 /* Copy across the first entry, adjusting its
4619 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4620 - enclosing
->line_filepos
);
4621 linp
= flinfo
->linenos
+ linoff
;
4622 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4623 lin
.l_addr
.l_symndx
= *indexp
;
4624 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4626 /* Copy the other entries, adjusting their addresses. */
4627 linpend
= linp
+ *lineno_counts
* linesz
;
4628 offset
= (o
->output_section
->vma
4631 for (linp
+= linesz
; linp
< linpend
; linp
+= linesz
)
4633 bfd_coff_swap_lineno_in (input_bfd
, linp
, &lin
);
4634 lin
.l_addr
.l_paddr
+= offset
;
4635 bfd_coff_swap_lineno_out (output_bfd
, &lin
, linp
);
4638 /* Write out the entries we've just processed. */
4639 pos
= (o
->output_section
->line_filepos
4640 + o
->output_section
->lineno_count
* linesz
);
4641 amt
= linesz
* *lineno_counts
;
4642 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4643 || bfd_bwrite (flinfo
->linenos
+ linoff
,
4644 amt
, output_bfd
) != amt
)
4646 o
->output_section
->lineno_count
+= *lineno_counts
;
4648 /* Record the offset of the symbol's line numbers
4649 in the output file. */
4650 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= pos
;
4654 struct internal_syment
*iisp
, *iispend
;
4657 bfd_vma range_start
, range_end
;
4660 /* Update any C_BINCL or C_EINCL symbols
4661 that refer to a line number in the
4662 range we just output. */
4663 iisp
= flinfo
->internal_syms
;
4664 iispend
= iisp
+ obj_raw_syment_count (input_bfd
);
4665 iindp
= flinfo
->sym_indices
;
4666 oos
= flinfo
->outsyms
;
4667 range_start
= enclosing
->line_filepos
+ linoff
;
4668 range_end
= range_start
+ *lineno_counts
* linesz
;
4669 while (iisp
< iispend
)
4672 && (iisp
->n_sclass
== C_BINCL
4673 || iisp
->n_sclass
== C_EINCL
)
4674 && iisp
->n_value
>= range_start
4675 && iisp
->n_value
< range_end
)
4677 struct internal_syment iis
;
4679 bfd_coff_swap_sym_in (output_bfd
, oos
, &iis
);
4680 iis
.n_value
= (iisp
->n_value
4683 bfd_coff_swap_sym_out (output_bfd
,
4688 iiadd
= 1 + iisp
->n_numaux
;
4690 oos
+= iiadd
* osymesz
;
4698 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4699 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4710 lineno_counts
+= add
;
4714 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4715 symbol will be the first symbol in the next input file. In the
4716 normal case, this will save us from writing out the C_FILE symbol
4718 if (flinfo
->last_file_index
!= -1
4719 && (bfd_size_type
) flinfo
->last_file_index
>= syment_base
)
4721 flinfo
->last_file
.n_value
= output_index
;
4722 bfd_coff_swap_sym_out (output_bfd
, (void *) &flinfo
->last_file
,
4723 (void *) (flinfo
->outsyms
4724 + ((flinfo
->last_file_index
- syment_base
)
4728 /* Write the modified symbols to the output file. */
4729 if (outsym
> flinfo
->outsyms
)
4731 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4732 bfd_size_type amt
= outsym
- flinfo
->outsyms
;
4733 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4734 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
4737 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4738 + (outsym
- flinfo
->outsyms
) / osymesz
)
4741 obj_raw_syment_count (output_bfd
) = output_index
;
4744 /* Don't let the linker relocation routines discard the symbols. */
4745 keep_syms
= obj_coff_keep_syms (input_bfd
);
4746 obj_coff_keep_syms (input_bfd
) = TRUE
;
4748 /* Relocate the contents of each section. */
4749 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4753 if (! o
->linker_mark
)
4754 /* This section was omitted from the link. */
4757 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4759 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4762 /* We have set filepos correctly for the sections we created to
4763 represent csects, so bfd_get_section_contents should work. */
4764 if (coff_section_data (input_bfd
, o
) != NULL
4765 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4766 contents
= coff_section_data (input_bfd
, o
)->contents
;
4769 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4770 if (!bfd_get_section_contents (input_bfd
, o
, flinfo
->contents
, 0, sz
))
4772 contents
= flinfo
->contents
;
4775 if ((o
->flags
& SEC_RELOC
) != 0)
4778 struct internal_reloc
*internal_relocs
;
4779 struct internal_reloc
*irel
;
4781 struct internal_reloc
*irelend
;
4782 struct xcoff_link_hash_entry
**rel_hash
;
4785 /* Read in the relocs. */
4786 target_index
= o
->output_section
->target_index
;
4787 internal_relocs
= (xcoff_read_internal_relocs
4788 (input_bfd
, o
, FALSE
, flinfo
->external_relocs
,
4790 (flinfo
->section_info
[target_index
].relocs
4791 + o
->output_section
->reloc_count
)));
4792 if (internal_relocs
== NULL
)
4795 /* Call processor specific code to relocate the section
4797 if (! bfd_coff_relocate_section (output_bfd
, flinfo
->info
,
4801 flinfo
->internal_syms
,
4802 xcoff_data (input_bfd
)->csects
))
4805 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4806 irel
= internal_relocs
;
4807 irelend
= irel
+ o
->reloc_count
;
4808 rel_hash
= (flinfo
->section_info
[target_index
].rel_hashes
4809 + o
->output_section
->reloc_count
);
4810 for (; irel
< irelend
; irel
++, rel_hash
++)
4812 struct xcoff_link_hash_entry
*h
= NULL
;
4816 /* Adjust the reloc address and symbol index. */
4818 irel
->r_vaddr
+= offset
;
4820 r_symndx
= irel
->r_symndx
;
4825 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4827 if (r_symndx
!= -1 && flinfo
->info
->strip
!= strip_all
)
4830 && h
->smclas
!= XMC_TD
4831 && (irel
->r_type
== R_TOC
4832 || irel
->r_type
== R_GL
4833 || irel
->r_type
== R_TCL
4834 || irel
->r_type
== R_TRL
4835 || irel
->r_type
== R_TRLA
))
4837 /* This is a TOC relative reloc with a symbol
4838 attached. The symbol should be the one which
4839 this reloc is for. We want to make this
4840 reloc against the TOC address of the symbol,
4841 not the symbol itself. */
4842 BFD_ASSERT (h
->toc_section
!= NULL
);
4843 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4844 if (h
->u
.toc_indx
!= -1)
4845 irel
->r_symndx
= h
->u
.toc_indx
;
4848 struct xcoff_toc_rel_hash
*n
;
4849 struct xcoff_link_section_info
*si
;
4853 n
= bfd_alloc (flinfo
->output_bfd
, amt
);
4856 si
= flinfo
->section_info
+ target_index
;
4857 n
->next
= si
->toc_rel_hashes
;
4860 si
->toc_rel_hashes
= n
;
4865 /* This is a global symbol. */
4867 irel
->r_symndx
= h
->indx
;
4870 /* This symbol is being written at the end
4871 of the file, and we do not yet know the
4872 symbol index. We save the pointer to the
4873 hash table entry in the rel_hash list.
4874 We set the indx field to -2 to indicate
4875 that this symbol must not be stripped. */
4884 indx
= flinfo
->sym_indices
[r_symndx
];
4888 struct internal_syment
*is
;
4890 /* Relocations against a TC0 TOC anchor are
4891 automatically transformed to be against
4892 the TOC anchor in the output file. */
4893 is
= flinfo
->internal_syms
+ r_symndx
;
4894 if (is
->n_sclass
== C_HIDEXT
4895 && is
->n_numaux
> 0)
4898 union internal_auxent aux
;
4902 obj_coff_external_syms (input_bfd
))
4903 + ((r_symndx
+ is
->n_numaux
)
4905 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4906 is
->n_type
, is
->n_sclass
,
4910 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4911 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4912 indx
= flinfo
->toc_symindx
;
4917 irel
->r_symndx
= indx
;
4921 struct internal_syment
*is
;
4924 char buf
[SYMNMLEN
+ 1];
4926 /* This reloc is against a symbol we are
4927 stripping. It would be possible to handle
4928 this case, but I don't think it's worth it. */
4929 is
= flinfo
->internal_syms
+ r_symndx
;
4931 name
= (_bfd_coff_internal_syment_name
4932 (input_bfd
, is
, buf
));
4937 if (! ((*flinfo
->info
->callbacks
->unattached_reloc
)
4938 (flinfo
->info
, name
, input_bfd
, o
,
4945 if (xcoff_need_ldrel_p (flinfo
->info
, irel
, h
))
4952 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4954 sec
= xcoff_symbol_section (h
);
4955 if (!xcoff_create_ldrel (output_bfd
, flinfo
,
4956 o
->output_section
, input_bfd
,
4962 o
->output_section
->reloc_count
+= o
->reloc_count
;
4965 /* Write out the modified section contents. */
4966 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4967 contents
, (file_ptr
) o
->output_offset
,
4972 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4974 if (! flinfo
->info
->keep_memory
)
4976 if (! _bfd_coff_free_symbols (input_bfd
))
4986 /* Sort relocs by VMA. This is called via qsort. */
4989 xcoff_sort_relocs (const void * p1
, const void * p2
)
4991 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
4992 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
4994 if (r1
->r_vaddr
> r2
->r_vaddr
)
4996 else if (r1
->r_vaddr
< r2
->r_vaddr
)
5002 /* Return true if section SEC is a TOC section. */
5004 static inline bfd_boolean
5005 xcoff_toc_section_p (asection
*sec
)
5010 if (name
[0] == '.' && name
[1] == 't')
5014 if (name
[3] == '0' && name
[4] == 0)
5019 if (name
[2] == 'd' && name
[3] == 0)
5025 /* See if the link requires a TOC (it usually does!). If so, find a
5026 good place to put the TOC anchor csect, and write out the associated
5030 xcoff_find_tc0 (bfd
*output_bfd
, struct xcoff_final_link_info
*flinfo
)
5032 bfd_vma toc_start
, toc_end
, start
, end
, best_address
;
5036 struct internal_syment irsym
;
5037 union internal_auxent iraux
;
5041 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5042 index of a csect at the beginning of the TOC. */
5043 toc_start
= ~(bfd_vma
) 0;
5046 for (input_bfd
= flinfo
->info
->input_bfds
;
5048 input_bfd
= input_bfd
->link_next
)
5049 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5050 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5052 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5053 if (toc_start
> start
)
5056 section_index
= sec
->output_section
->target_index
;
5059 end
= start
+ sec
->size
;
5064 /* There's no need for a TC0 symbol if we don't have a TOC. */
5065 if (toc_end
< toc_start
)
5067 xcoff_data (output_bfd
)->toc
= toc_start
;
5071 if (toc_end
- toc_start
< 0x8000)
5072 /* Every TOC csect can be accessed from TOC_START. */
5073 best_address
= toc_start
;
5076 /* Find the lowest TOC csect that is still within range of TOC_END. */
5077 best_address
= toc_end
;
5078 for (input_bfd
= flinfo
->info
->input_bfds
;
5080 input_bfd
= input_bfd
->link_next
)
5081 for (sec
= input_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5082 if ((sec
->flags
& SEC_MARK
) != 0 && xcoff_toc_section_p (sec
))
5084 start
= sec
->output_section
->vma
+ sec
->output_offset
;
5085 if (start
< best_address
5086 && start
+ 0x8000 >= toc_end
)
5088 best_address
= start
;
5089 section_index
= sec
->output_section
->target_index
;
5093 /* Make sure that the start of the TOC is also within range. */
5094 if (best_address
> toc_start
+ 0x8000)
5096 (*_bfd_error_handler
)
5097 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5099 (unsigned long) (toc_end
- toc_start
));
5100 bfd_set_error (bfd_error_file_too_big
);
5105 /* Record the chosen TOC value. */
5106 flinfo
->toc_symindx
= obj_raw_syment_count (output_bfd
);
5107 xcoff_data (output_bfd
)->toc
= best_address
;
5108 xcoff_data (output_bfd
)->sntoc
= section_index
;
5110 /* Fill out the TC0 symbol. */
5111 if (!bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
, &irsym
, "TOC"))
5113 irsym
.n_value
= best_address
;
5114 irsym
.n_scnum
= section_index
;
5115 irsym
.n_sclass
= C_HIDEXT
;
5116 irsym
.n_type
= T_NULL
;
5118 bfd_coff_swap_sym_out (output_bfd
, &irsym
, flinfo
->outsyms
);
5120 /* Fill out the auxillary csect information. */
5121 memset (&iraux
, 0, sizeof iraux
);
5122 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5123 iraux
.x_csect
.x_smclas
= XMC_TC0
;
5124 iraux
.x_csect
.x_scnlen
.l
= 0;
5125 bfd_coff_swap_aux_out (output_bfd
, &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5126 flinfo
->outsyms
+ bfd_coff_symesz (output_bfd
));
5128 /* Write the contents to the file. */
5129 pos
= obj_sym_filepos (output_bfd
);
5130 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5131 size
= 2 * bfd_coff_symesz (output_bfd
);
5132 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5133 || bfd_bwrite (flinfo
->outsyms
, size
, output_bfd
) != size
)
5135 obj_raw_syment_count (output_bfd
) += 2;
5140 /* Write out a non-XCOFF global symbol. */
5143 xcoff_write_global_symbol (struct xcoff_link_hash_entry
*h
, void * inf
)
5145 struct xcoff_final_link_info
*flinfo
= (struct xcoff_final_link_info
*) inf
;
5148 struct internal_syment isym
;
5149 union internal_auxent aux
;
5154 output_bfd
= flinfo
->output_bfd
;
5155 outsym
= flinfo
->outsyms
;
5157 if (h
->root
.type
== bfd_link_hash_warning
)
5159 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
5160 if (h
->root
.type
== bfd_link_hash_new
)
5164 /* If this symbol was garbage collected, just skip it. */
5165 if (xcoff_hash_table (flinfo
->info
)->gc
5166 && (h
->flags
& XCOFF_MARK
) == 0)
5169 /* If we need a .loader section entry, write it out. */
5170 if (h
->ldsym
!= NULL
)
5172 struct internal_ldsym
*ldsym
;
5177 if (h
->root
.type
== bfd_link_hash_undefined
5178 || h
->root
.type
== bfd_link_hash_undefweak
)
5182 ldsym
->l_scnum
= N_UNDEF
;
5183 ldsym
->l_smtype
= XTY_ER
;
5184 impbfd
= h
->root
.u
.undef
.abfd
;
5187 else if (h
->root
.type
== bfd_link_hash_defined
5188 || h
->root
.type
== bfd_link_hash_defweak
)
5192 sec
= h
->root
.u
.def
.section
;
5193 ldsym
->l_value
= (sec
->output_section
->vma
5194 + sec
->output_offset
5195 + h
->root
.u
.def
.value
);
5196 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5197 ldsym
->l_smtype
= XTY_SD
;
5198 impbfd
= sec
->owner
;
5204 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5205 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5206 || (h
->flags
& XCOFF_IMPORT
) != 0)
5208 Import symbols are defined so the check above will make
5209 the l_smtype XTY_SD. But this is not correct, it should
5211 ldsym
->l_smtype
|= L_IMPORT
;
5213 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5214 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5215 || (h
->flags
& XCOFF_EXPORT
) != 0)
5216 ldsym
->l_smtype
|= L_EXPORT
;
5218 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5219 ldsym
->l_smtype
|= L_ENTRY
;
5221 if ((h
->flags
& XCOFF_RTINIT
) != 0)
5222 ldsym
->l_smtype
= XTY_SD
;
5224 ldsym
->l_smclas
= h
->smclas
;
5226 if (ldsym
->l_smtype
& L_IMPORT
)
5228 if ((h
->root
.type
== bfd_link_hash_defined
5229 || h
->root
.type
== bfd_link_hash_defweak
)
5230 && (h
->root
.u
.def
.value
!= 0))
5231 ldsym
->l_smclas
= XMC_XO
;
5233 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
5234 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
5235 ldsym
->l_smclas
= XMC_SV3264
;
5237 else if (h
->flags
& XCOFF_SYSCALL32
)
5238 ldsym
->l_smclas
= XMC_SV
;
5240 else if (h
->flags
& XCOFF_SYSCALL64
)
5241 ldsym
->l_smclas
= XMC_SV64
;
5244 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
5248 else if (ldsym
->l_ifile
== 0)
5250 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5252 else if (impbfd
== NULL
)
5256 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5257 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5263 BFD_ASSERT (h
->ldindx
>= 0);
5265 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
5268 * bfd_xcoff_ldsymsz(flinfo
->output_bfd
)));
5272 /* If this symbol needs global linkage code, write it out. */
5273 if (h
->root
.type
== bfd_link_hash_defined
5274 && (h
->root
.u
.def
.section
5275 == xcoff_hash_table (flinfo
->info
)->linkage_section
))
5281 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5283 /* The first instruction in the global linkage code loads a
5284 specific TOC element. */
5285 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5286 + h
->descriptor
->toc_section
->output_offset
5287 - xcoff_data (output_bfd
)->toc
);
5289 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5290 tocoff
+= h
->descriptor
->u
.toc_offset
;
5292 /* The first instruction in the glink code needs to be
5293 cooked to to hold the correct offset in the toc. The
5294 rest are just output raw. */
5295 bfd_put_32 (output_bfd
,
5296 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
5298 /* Start with i == 1 to get past the first instruction done above
5299 The /4 is because the glink code is in bytes and we are going
5301 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
5302 bfd_put_32 (output_bfd
,
5303 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
5307 /* If we created a TOC entry for this symbol, write out the required
5309 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5314 struct internal_reloc
*irel
;
5315 struct internal_syment irsym
;
5316 union internal_auxent iraux
;
5318 tocsec
= h
->toc_section
;
5319 osec
= tocsec
->output_section
;
5320 oindx
= osec
->target_index
;
5321 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5322 irel
->r_vaddr
= (osec
->vma
5323 + tocsec
->output_offset
5327 irel
->r_symndx
= h
->indx
;
5331 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5334 BFD_ASSERT (h
->ldindx
>= 0);
5336 /* Initialize the aux union here instead of closer to when it is
5337 written out below because the length of the csect depends on
5338 whether the output is 32 or 64 bit. */
5339 memset (&iraux
, 0, sizeof iraux
);
5340 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5341 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5342 iraux
.x_csect
.x_smclas
= XMC_TC
;
5344 /* 32 bit uses a 32 bit R_POS to do the relocations
5345 64 bit uses a 64 bit R_POS to do the relocations
5347 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5349 Which one is determined by the backend. */
5350 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5353 iraux
.x_csect
.x_scnlen
.l
= 8;
5355 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5358 iraux
.x_csect
.x_scnlen
.l
= 4;
5363 irel
->r_type
= R_POS
;
5364 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5365 ++osec
->reloc_count
;
5367 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5368 output_bfd
, irel
, NULL
, h
))
5371 /* We need to emit a symbol to define a csect which holds
5373 if (flinfo
->info
->strip
!= strip_all
)
5375 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
,
5376 &irsym
, h
->root
.root
.string
);
5380 irsym
.n_value
= irel
->r_vaddr
;
5381 irsym
.n_scnum
= osec
->target_index
;
5382 irsym
.n_sclass
= C_HIDEXT
;
5383 irsym
.n_type
= T_NULL
;
5386 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
5387 outsym
+= bfd_coff_symesz (output_bfd
);
5389 /* Note : iraux is initialized above. */
5390 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
5391 0, 1, (void *) outsym
);
5392 outsym
+= bfd_coff_auxesz (output_bfd
);
5396 /* We aren't going to write out the symbols below, so we
5397 need to write them out now. */
5398 pos
= obj_sym_filepos (output_bfd
);
5399 pos
+= (obj_raw_syment_count (output_bfd
)
5400 * bfd_coff_symesz (output_bfd
));
5401 amt
= outsym
- flinfo
->outsyms
;
5402 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5403 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5405 obj_raw_syment_count (output_bfd
) +=
5406 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5408 outsym
= flinfo
->outsyms
;
5413 /* If this symbol is a specially defined function descriptor, write
5414 it out. The first word is the address of the function code
5415 itself, the second word is the address of the TOC, and the third
5419 The addresses for the 32 bit will take 4 bytes and the addresses
5420 for 64 bit will take 8 bytes. Similar for the relocs. This type
5421 of logic was also done above to create a TOC entry in
5422 xcoff_write_global_symbol. */
5423 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5424 && h
->root
.type
== bfd_link_hash_defined
5425 && (h
->root
.u
.def
.section
5426 == xcoff_hash_table (flinfo
->info
)->descriptor_section
))
5432 struct xcoff_link_hash_entry
*hentry
;
5434 struct internal_reloc
*irel
;
5436 unsigned int reloc_size
, byte_size
;
5438 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5443 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
5451 sec
= h
->root
.u
.def
.section
;
5452 osec
= sec
->output_section
;
5453 oindx
= osec
->target_index
;
5454 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5456 hentry
= h
->descriptor
;
5457 BFD_ASSERT (hentry
!= NULL
5458 && (hentry
->root
.type
== bfd_link_hash_defined
5459 || hentry
->root
.type
== bfd_link_hash_defweak
));
5460 esec
= hentry
->root
.u
.def
.section
;
5462 irel
= flinfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5463 irel
->r_vaddr
= (osec
->vma
5464 + sec
->output_offset
5465 + h
->root
.u
.def
.value
);
5466 irel
->r_symndx
= esec
->output_section
->target_index
;
5467 irel
->r_type
= R_POS
;
5468 irel
->r_size
= reloc_size
;
5469 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5470 ++osec
->reloc_count
;
5472 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5473 output_bfd
, irel
, esec
, NULL
))
5476 /* There are three items to write out,
5477 the address of the code
5478 the address of the toc anchor
5479 the environment pointer.
5480 We are ignoring the environment pointer. So set it to zero. */
5481 if (bfd_xcoff_is_xcoff64 (output_bfd
))
5483 bfd_put_64 (output_bfd
,
5484 (esec
->output_section
->vma
+ esec
->output_offset
5485 + hentry
->root
.u
.def
.value
),
5487 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
5488 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
5493 This logic was already called above so the error case where
5494 the backend is neither has already been checked. */
5495 bfd_put_32 (output_bfd
,
5496 (esec
->output_section
->vma
+ esec
->output_offset
5497 + hentry
->root
.u
.def
.value
),
5499 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5500 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
5503 tsec
= coff_section_from_bfd_index (output_bfd
,
5504 xcoff_data (output_bfd
)->sntoc
);
5507 irel
->r_vaddr
= (osec
->vma
5508 + sec
->output_offset
5509 + h
->root
.u
.def
.value
5511 irel
->r_symndx
= tsec
->output_section
->target_index
;
5512 irel
->r_type
= R_POS
;
5513 irel
->r_size
= reloc_size
;
5514 flinfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5515 ++osec
->reloc_count
;
5517 if (!xcoff_create_ldrel (output_bfd
, flinfo
, osec
,
5518 output_bfd
, irel
, tsec
, NULL
))
5522 if (h
->indx
>= 0 || flinfo
->info
->strip
== strip_all
)
5524 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5529 && (flinfo
->info
->strip
== strip_all
5530 || (flinfo
->info
->strip
== strip_some
5531 && bfd_hash_lookup (flinfo
->info
->keep_hash
, h
->root
.root
.string
,
5532 FALSE
, FALSE
) == NULL
)))
5534 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5539 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5541 BFD_ASSERT (outsym
== flinfo
->outsyms
);
5545 memset (&aux
, 0, sizeof aux
);
5547 h
->indx
= obj_raw_syment_count (output_bfd
);
5549 result
= bfd_xcoff_put_symbol_name (output_bfd
, flinfo
->strtab
, &isym
,
5550 h
->root
.root
.string
);
5554 if (h
->root
.type
== bfd_link_hash_undefined
5555 || h
->root
.type
== bfd_link_hash_undefweak
)
5558 isym
.n_scnum
= N_UNDEF
;
5559 if (h
->root
.type
== bfd_link_hash_undefweak
5560 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5561 isym
.n_sclass
= C_WEAKEXT
;
5563 isym
.n_sclass
= C_EXT
;
5564 aux
.x_csect
.x_smtyp
= XTY_ER
;
5566 else if ((h
->root
.type
== bfd_link_hash_defined
5567 || h
->root
.type
== bfd_link_hash_defweak
)
5568 && h
->smclas
== XMC_XO
)
5570 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5571 isym
.n_value
= h
->root
.u
.def
.value
;
5572 isym
.n_scnum
= N_UNDEF
;
5573 if (h
->root
.type
== bfd_link_hash_undefweak
5574 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5575 isym
.n_sclass
= C_WEAKEXT
;
5577 isym
.n_sclass
= C_EXT
;
5578 aux
.x_csect
.x_smtyp
= XTY_ER
;
5580 else if (h
->root
.type
== bfd_link_hash_defined
5581 || h
->root
.type
== bfd_link_hash_defweak
)
5583 struct xcoff_link_size_list
*l
;
5585 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5586 + h
->root
.u
.def
.section
->output_offset
5587 + h
->root
.u
.def
.value
);
5588 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5589 isym
.n_scnum
= N_ABS
;
5591 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5592 isym
.n_sclass
= C_HIDEXT
;
5593 aux
.x_csect
.x_smtyp
= XTY_SD
;
5595 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5597 for (l
= xcoff_hash_table (flinfo
->info
)->size_list
;
5603 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5609 else if (h
->root
.type
== bfd_link_hash_common
)
5611 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5612 + h
->root
.u
.c
.p
->section
->output_offset
);
5613 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5614 isym
.n_sclass
= C_EXT
;
5615 aux
.x_csect
.x_smtyp
= XTY_CM
;
5616 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5621 isym
.n_type
= T_NULL
;
5624 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5625 outsym
+= bfd_coff_symesz (output_bfd
);
5627 aux
.x_csect
.x_smclas
= h
->smclas
;
5628 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5630 outsym
+= bfd_coff_auxesz (output_bfd
);
5632 if ((h
->root
.type
== bfd_link_hash_defined
5633 || h
->root
.type
== bfd_link_hash_defweak
)
5634 && h
->smclas
!= XMC_XO
)
5636 /* We just output an SD symbol. Now output an LD symbol. */
5639 if (h
->root
.type
== bfd_link_hash_undefweak
5640 && C_WEAKEXT
== C_AIX_WEAKEXT
)
5641 isym
.n_sclass
= C_WEAKEXT
;
5643 isym
.n_sclass
= C_EXT
;
5644 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5645 outsym
+= bfd_coff_symesz (output_bfd
);
5647 aux
.x_csect
.x_smtyp
= XTY_LD
;
5648 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5649 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5651 outsym
+= bfd_coff_auxesz (output_bfd
);
5654 pos
= obj_sym_filepos (output_bfd
);
5655 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5656 amt
= outsym
- flinfo
->outsyms
;
5657 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5658 || bfd_bwrite (flinfo
->outsyms
, amt
, output_bfd
) != amt
)
5660 obj_raw_syment_count (output_bfd
) +=
5661 (outsym
- flinfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5666 /* Handle a link order which is supposed to generate a reloc. */
5669 xcoff_reloc_link_order (bfd
*output_bfd
,
5670 struct xcoff_final_link_info
*flinfo
,
5671 asection
*output_section
,
5672 struct bfd_link_order
*link_order
)
5674 reloc_howto_type
*howto
;
5675 struct xcoff_link_hash_entry
*h
;
5679 struct internal_reloc
*irel
;
5680 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5682 if (link_order
->type
== bfd_section_reloc_link_order
)
5683 /* We need to somehow locate a symbol in the right section. The
5684 symbol must either have a value of zero, or we must adjust
5685 the addend by the value of the symbol. FIXME: Write this
5686 when we need it. The old linker couldn't handle this anyhow. */
5689 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5692 bfd_set_error (bfd_error_bad_value
);
5696 h
= ((struct xcoff_link_hash_entry
*)
5697 bfd_wrapped_link_hash_lookup (output_bfd
, flinfo
->info
,
5698 link_order
->u
.reloc
.p
->u
.name
,
5699 FALSE
, FALSE
, TRUE
));
5702 if (! ((*flinfo
->info
->callbacks
->unattached_reloc
)
5703 (flinfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0)))
5708 hsec
= xcoff_symbol_section (h
);
5709 if (h
->root
.type
== bfd_link_hash_defined
5710 || h
->root
.type
== bfd_link_hash_defweak
)
5711 hval
= h
->root
.u
.def
.value
;
5715 addend
= link_order
->u
.reloc
.p
->addend
;
5717 addend
+= (hsec
->output_section
->vma
5718 + hsec
->output_offset
5725 bfd_reloc_status_type rstat
;
5728 size
= bfd_get_reloc_size (howto
);
5729 buf
= bfd_zmalloc (size
);
5733 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5739 case bfd_reloc_outofrange
:
5741 case bfd_reloc_overflow
:
5742 if (! ((*flinfo
->info
->callbacks
->reloc_overflow
)
5743 (flinfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5744 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0)))
5751 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5752 (file_ptr
) link_order
->offset
, size
);
5758 /* Store the reloc information in the right place. It will get
5759 swapped and written out at the end of the final_link routine. */
5760 irel
= (flinfo
->section_info
[output_section
->target_index
].relocs
5761 + output_section
->reloc_count
);
5762 rel_hash_ptr
= (flinfo
->section_info
[output_section
->target_index
].rel_hashes
5763 + output_section
->reloc_count
);
5765 memset (irel
, 0, sizeof (struct internal_reloc
));
5766 *rel_hash_ptr
= NULL
;
5768 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5771 irel
->r_symndx
= h
->indx
;
5774 /* Set the index to -2 to force this symbol to get written out. */
5780 irel
->r_type
= howto
->type
;
5781 irel
->r_size
= howto
->bitsize
- 1;
5782 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5783 irel
->r_size
|= 0x80;
5785 ++output_section
->reloc_count
;
5787 /* Now output the reloc to the .loader section. */
5788 if (xcoff_hash_table (flinfo
->info
)->loader_section
)
5790 if (!xcoff_create_ldrel (output_bfd
, flinfo
, output_section
,
5791 output_bfd
, irel
, hsec
, h
))
5798 /* Do the final link step. */
5801 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5803 bfd_size_type symesz
;
5804 struct xcoff_final_link_info flinfo
;
5806 struct bfd_link_order
*p
;
5807 bfd_size_type max_contents_size
;
5808 bfd_size_type max_sym_count
;
5809 bfd_size_type max_lineno_count
;
5810 bfd_size_type max_reloc_count
;
5811 bfd_size_type max_output_reloc_count
;
5812 file_ptr rel_filepos
;
5814 file_ptr line_filepos
;
5815 unsigned int linesz
;
5817 bfd_byte
*external_relocs
= NULL
;
5818 char strbuf
[STRING_SIZE_SIZE
];
5823 abfd
->flags
|= DYNAMIC
;
5825 symesz
= bfd_coff_symesz (abfd
);
5828 flinfo
.output_bfd
= abfd
;
5829 flinfo
.strtab
= NULL
;
5830 flinfo
.section_info
= NULL
;
5831 flinfo
.last_file_index
= -1;
5832 flinfo
.toc_symindx
= -1;
5833 flinfo
.internal_syms
= NULL
;
5834 flinfo
.sym_indices
= NULL
;
5835 flinfo
.outsyms
= NULL
;
5836 flinfo
.linenos
= NULL
;
5837 flinfo
.contents
= NULL
;
5838 flinfo
.external_relocs
= NULL
;
5840 if (xcoff_hash_table (info
)->loader_section
)
5842 flinfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5843 + bfd_xcoff_ldhdrsz (abfd
));
5844 flinfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5845 + bfd_xcoff_ldhdrsz (abfd
)
5846 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5847 * bfd_xcoff_ldsymsz (abfd
)));
5851 flinfo
.ldsym
= NULL
;
5852 flinfo
.ldrel
= NULL
;
5855 xcoff_data (abfd
)->coff
.link_info
= info
;
5857 flinfo
.strtab
= _bfd_stringtab_init ();
5858 if (flinfo
.strtab
== NULL
)
5861 /* Count the relocation entries required for the output file.
5862 (We've already counted the line numbers.) Determine a few
5864 max_contents_size
= 0;
5865 max_lineno_count
= 0;
5866 max_reloc_count
= 0;
5867 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5870 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5872 if (p
->type
== bfd_indirect_link_order
)
5876 sec
= p
->u
.indirect
.section
;
5878 /* Mark all sections which are to be included in the
5879 link. This will normally be every section. We need
5880 to do this so that we can identify any sections which
5881 the linker has decided to not include. */
5882 sec
->linker_mark
= TRUE
;
5884 o
->reloc_count
+= sec
->reloc_count
;
5886 if ((sec
->flags
& SEC_IN_MEMORY
) == 0)
5888 if (sec
->rawsize
> max_contents_size
)
5889 max_contents_size
= sec
->rawsize
;
5890 if (sec
->size
> max_contents_size
)
5891 max_contents_size
= sec
->size
;
5893 if (coff_section_data (sec
->owner
, sec
) != NULL
5894 && xcoff_section_data (sec
->owner
, sec
) != NULL
5895 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5896 > max_lineno_count
))
5898 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5899 if (sec
->reloc_count
> max_reloc_count
)
5900 max_reloc_count
= sec
->reloc_count
;
5902 else if (p
->type
== bfd_section_reloc_link_order
5903 || p
->type
== bfd_symbol_reloc_link_order
)
5908 /* Compute the file positions for all the sections. */
5909 if (abfd
->output_has_begun
)
5911 if (xcoff_hash_table (info
)->file_align
!= 0)
5918 file_align
= xcoff_hash_table (info
)->file_align
;
5919 if (file_align
!= 0)
5921 bfd_boolean saw_contents
;
5925 /* Insert .pad sections before every section which has
5926 contents and is loaded, if it is preceded by some other
5927 section which has contents and is loaded. */
5928 saw_contents
= TRUE
;
5929 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5931 if (strcmp (o
->name
, ".pad") == 0)
5932 saw_contents
= FALSE
;
5933 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5934 && (o
->flags
& SEC_LOAD
) != 0)
5937 saw_contents
= TRUE
;
5942 /* Create a pad section and place it before the section
5943 that needs padding. This requires unlinking and
5944 relinking the bfd's section list. */
5946 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
5948 n
->alignment_power
= 0;
5950 bfd_section_list_remove (abfd
, n
);
5951 bfd_section_list_insert_before (abfd
, o
, n
);
5952 saw_contents
= FALSE
;
5957 /* Reset the section indices after inserting the new
5960 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5963 o
->target_index
= indx
;
5965 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
5967 /* Work out appropriate sizes for the .pad sections to force
5968 each section to land on a page boundary. This bit of
5969 code knows what compute_section_file_positions is going
5971 sofar
= bfd_coff_filhsz (abfd
);
5972 sofar
+= bfd_coff_aoutsz (abfd
);
5973 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
5974 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5975 if ((bfd_xcoff_is_reloc_count_overflow
5976 (abfd
, (bfd_vma
) o
->reloc_count
))
5977 || (bfd_xcoff_is_lineno_count_overflow
5978 (abfd
, (bfd_vma
) o
->lineno_count
)))
5979 /* 64 does not overflow, need to check if 32 does */
5980 sofar
+= bfd_coff_scnhsz (abfd
);
5982 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5984 if (strcmp (o
->name
, ".pad") == 0)
5988 BFD_ASSERT (o
->size
== 0);
5989 pageoff
= sofar
& (file_align
- 1);
5992 o
->size
= file_align
- pageoff
;
5993 sofar
+= file_align
- pageoff
;
5994 o
->flags
|= SEC_HAS_CONTENTS
;
5999 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
6000 sofar
+= BFD_ALIGN (o
->size
,
6001 1 << o
->alignment_power
);
6006 if (! bfd_coff_compute_section_file_positions (abfd
))
6010 /* Allocate space for the pointers we need to keep for the relocs. */
6014 /* We use section_count + 1, rather than section_count, because
6015 the target_index fields are 1 based. */
6016 amt
= abfd
->section_count
+ 1;
6017 amt
*= sizeof (struct xcoff_link_section_info
);
6018 flinfo
.section_info
= bfd_malloc (amt
);
6019 if (flinfo
.section_info
== NULL
)
6021 for (i
= 0; i
<= abfd
->section_count
; i
++)
6023 flinfo
.section_info
[i
].relocs
= NULL
;
6024 flinfo
.section_info
[i
].rel_hashes
= NULL
;
6025 flinfo
.section_info
[i
].toc_rel_hashes
= NULL
;
6029 /* Set the file positions for the relocs. */
6030 rel_filepos
= obj_relocbase (abfd
);
6031 relsz
= bfd_coff_relsz (abfd
);
6032 max_output_reloc_count
= 0;
6033 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6035 if (o
->reloc_count
== 0)
6039 /* A stripped file has no relocs. However, we still
6040 allocate the buffers, so that later code doesn't have to
6041 worry about whether we are stripping or not. */
6042 if (info
->strip
== strip_all
)
6046 o
->flags
|= SEC_RELOC
;
6047 o
->rel_filepos
= rel_filepos
;
6048 rel_filepos
+= o
->reloc_count
* relsz
;
6051 /* We don't know the indices of global symbols until we have
6052 written out all the local symbols. For each section in
6053 the output file, we keep an array of pointers to hash
6054 table entries. Each entry in the array corresponds to a
6055 reloc. When we find a reloc against a global symbol, we
6056 set the corresponding entry in this array so that we can
6057 fix up the symbol index after we have written out all the
6060 Because of this problem, we also keep the relocs in
6061 memory until the end of the link. This wastes memory.
6062 We could backpatch the file later, I suppose, although it
6064 amt
= o
->reloc_count
;
6065 amt
*= sizeof (struct internal_reloc
);
6066 flinfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
6068 amt
= o
->reloc_count
;
6069 amt
*= sizeof (struct xcoff_link_hash_entry
*);
6070 flinfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
6072 if (flinfo
.section_info
[o
->target_index
].relocs
== NULL
6073 || flinfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
6076 if (o
->reloc_count
> max_output_reloc_count
)
6077 max_output_reloc_count
= o
->reloc_count
;
6081 /* We now know the size of the relocs, so we can determine the file
6082 positions of the line numbers. */
6083 line_filepos
= rel_filepos
;
6084 flinfo
.line_filepos
= line_filepos
;
6085 linesz
= bfd_coff_linesz (abfd
);
6086 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6088 if (o
->lineno_count
== 0)
6089 o
->line_filepos
= 0;
6092 o
->line_filepos
= line_filepos
;
6093 line_filepos
+= o
->lineno_count
* linesz
;
6096 /* Reset the reloc and lineno counts, so that we can use them to
6097 count the number of entries we have output so far. */
6099 o
->lineno_count
= 0;
6102 obj_sym_filepos (abfd
) = line_filepos
;
6104 /* Figure out the largest number of symbols in an input BFD. Take
6105 the opportunity to clear the output_has_begun fields of all the
6106 input BFD's. We want at least 6 symbols, since that is the
6107 number which xcoff_write_global_symbol may need. */
6109 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
6113 sub
->output_has_begun
= FALSE
;
6114 sz
= obj_raw_syment_count (sub
);
6115 if (sz
> max_sym_count
)
6119 /* Allocate some buffers used while linking. */
6120 amt
= max_sym_count
* sizeof (struct internal_syment
);
6121 flinfo
.internal_syms
= bfd_malloc (amt
);
6123 amt
= max_sym_count
* sizeof (long);
6124 flinfo
.sym_indices
= bfd_malloc (amt
);
6126 amt
= (max_sym_count
+ 1) * symesz
;
6127 flinfo
.outsyms
= bfd_malloc (amt
);
6129 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
6130 flinfo
.linenos
= bfd_malloc (amt
);
6132 amt
= max_contents_size
;
6133 flinfo
.contents
= bfd_malloc (amt
);
6135 amt
= max_reloc_count
* relsz
;
6136 flinfo
.external_relocs
= bfd_malloc (amt
);
6138 if ((flinfo
.internal_syms
== NULL
&& max_sym_count
> 0)
6139 || (flinfo
.sym_indices
== NULL
&& max_sym_count
> 0)
6140 || flinfo
.outsyms
== NULL
6141 || (flinfo
.linenos
== NULL
&& max_lineno_count
> 0)
6142 || (flinfo
.contents
== NULL
&& max_contents_size
> 0)
6143 || (flinfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
6146 obj_raw_syment_count (abfd
) = 0;
6148 /* Find a TOC symbol, if we need one. */
6149 if (!xcoff_find_tc0 (abfd
, &flinfo
))
6152 /* We now know the position of everything in the file, except that
6153 we don't know the size of the symbol table and therefore we don't
6154 know where the string table starts. We just build the string
6155 table in memory as we go along. We process all the relocations
6156 for a single input file at once. */
6157 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6159 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
6161 if (p
->type
== bfd_indirect_link_order
6162 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
6164 sub
= p
->u
.indirect
.section
->owner
;
6165 if (! sub
->output_has_begun
)
6167 if (! xcoff_link_input_bfd (&flinfo
, sub
))
6169 sub
->output_has_begun
= TRUE
;
6172 else if (p
->type
== bfd_section_reloc_link_order
6173 || p
->type
== bfd_symbol_reloc_link_order
)
6175 if (! xcoff_reloc_link_order (abfd
, &flinfo
, o
, p
))
6180 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
6186 /* Free up the buffers used by xcoff_link_input_bfd. */
6187 if (flinfo
.internal_syms
!= NULL
)
6189 free (flinfo
.internal_syms
);
6190 flinfo
.internal_syms
= NULL
;
6192 if (flinfo
.sym_indices
!= NULL
)
6194 free (flinfo
.sym_indices
);
6195 flinfo
.sym_indices
= NULL
;
6197 if (flinfo
.linenos
!= NULL
)
6199 free (flinfo
.linenos
);
6200 flinfo
.linenos
= NULL
;
6202 if (flinfo
.contents
!= NULL
)
6204 free (flinfo
.contents
);
6205 flinfo
.contents
= NULL
;
6207 if (flinfo
.external_relocs
!= NULL
)
6209 free (flinfo
.external_relocs
);
6210 flinfo
.external_relocs
= NULL
;
6213 /* The value of the last C_FILE symbol is supposed to be -1. Write
6215 if (flinfo
.last_file_index
!= -1)
6217 flinfo
.last_file
.n_value
= -(bfd_vma
) 1;
6218 bfd_coff_swap_sym_out (abfd
, (void *) &flinfo
.last_file
,
6219 (void *) flinfo
.outsyms
);
6220 pos
= obj_sym_filepos (abfd
) + flinfo
.last_file_index
* symesz
;
6221 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6222 || bfd_bwrite (flinfo
.outsyms
, symesz
, abfd
) != symesz
)
6226 /* Write out all the global symbols which do not come from XCOFF
6228 xcoff_link_hash_traverse (xcoff_hash_table (info
),
6229 xcoff_write_global_symbol
,
6232 if (flinfo
.outsyms
!= NULL
)
6234 free (flinfo
.outsyms
);
6235 flinfo
.outsyms
= NULL
;
6238 /* Now that we have written out all the global symbols, we know the
6239 symbol indices to use for relocs against them, and we can finally
6240 write out the relocs. */
6241 amt
= max_output_reloc_count
* relsz
;
6242 external_relocs
= bfd_malloc (amt
);
6243 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
6246 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
6248 struct internal_reloc
*irel
;
6249 struct internal_reloc
*irelend
;
6250 struct xcoff_link_hash_entry
**rel_hash
;
6251 struct xcoff_toc_rel_hash
*toc_rel_hash
;
6253 bfd_size_type rel_size
;
6255 /* A stripped file has no relocs. */
6256 if (info
->strip
== strip_all
)
6262 if (o
->reloc_count
== 0)
6265 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6266 irelend
= irel
+ o
->reloc_count
;
6267 rel_hash
= flinfo
.section_info
[o
->target_index
].rel_hashes
;
6268 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6270 if (*rel_hash
!= NULL
)
6272 if ((*rel_hash
)->indx
< 0)
6274 if (! ((*info
->callbacks
->unattached_reloc
)
6275 (info
, (*rel_hash
)->root
.root
.string
,
6276 NULL
, o
, irel
->r_vaddr
)))
6278 (*rel_hash
)->indx
= 0;
6280 irel
->r_symndx
= (*rel_hash
)->indx
;
6284 for (toc_rel_hash
= flinfo
.section_info
[o
->target_index
].toc_rel_hashes
;
6285 toc_rel_hash
!= NULL
;
6286 toc_rel_hash
= toc_rel_hash
->next
)
6288 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
6290 if (! ((*info
->callbacks
->unattached_reloc
)
6291 (info
, toc_rel_hash
->h
->root
.root
.string
,
6292 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
6294 toc_rel_hash
->h
->u
.toc_indx
= 0;
6296 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
6299 /* XCOFF requires that the relocs be sorted by address. We tend
6300 to produce them in the order in which their containing csects
6301 appear in the symbol table, which is not necessarily by
6302 address. So we sort them here. There may be a better way to
6304 qsort ((void *) flinfo
.section_info
[o
->target_index
].relocs
,
6305 o
->reloc_count
, sizeof (struct internal_reloc
),
6308 irel
= flinfo
.section_info
[o
->target_index
].relocs
;
6309 irelend
= irel
+ o
->reloc_count
;
6310 erel
= external_relocs
;
6311 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
6312 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
6314 rel_size
= relsz
* o
->reloc_count
;
6315 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
6316 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
6320 if (external_relocs
!= NULL
)
6322 free (external_relocs
);
6323 external_relocs
= NULL
;
6326 /* Free up the section information. */
6327 if (flinfo
.section_info
!= NULL
)
6331 for (i
= 0; i
< abfd
->section_count
; i
++)
6333 if (flinfo
.section_info
[i
].relocs
!= NULL
)
6334 free (flinfo
.section_info
[i
].relocs
);
6335 if (flinfo
.section_info
[i
].rel_hashes
!= NULL
)
6336 free (flinfo
.section_info
[i
].rel_hashes
);
6338 free (flinfo
.section_info
);
6339 flinfo
.section_info
= NULL
;
6342 /* Write out the loader section contents. */
6343 o
= xcoff_hash_table (info
)->loader_section
;
6346 BFD_ASSERT ((bfd_byte
*) flinfo
.ldrel
6347 == (xcoff_hash_table (info
)->loader_section
->contents
6348 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
6349 if (!bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6350 (file_ptr
) o
->output_offset
, o
->size
))
6354 /* Write out the magic sections. */
6355 o
= xcoff_hash_table (info
)->linkage_section
;
6357 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6358 (file_ptr
) o
->output_offset
,
6361 o
= xcoff_hash_table (info
)->toc_section
;
6363 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6364 (file_ptr
) o
->output_offset
,
6367 o
= xcoff_hash_table (info
)->descriptor_section
;
6369 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
6370 (file_ptr
) o
->output_offset
,
6374 /* Write out the string table. */
6375 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
6376 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6379 _bfd_stringtab_size (flinfo
.strtab
) + STRING_SIZE_SIZE
,
6381 amt
= STRING_SIZE_SIZE
;
6382 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
6384 if (! _bfd_stringtab_emit (abfd
, flinfo
.strtab
))
6387 _bfd_stringtab_free (flinfo
.strtab
);
6389 /* Write out the debugging string table. */
6390 o
= xcoff_hash_table (info
)->debug_section
;
6393 struct bfd_strtab_hash
*debug_strtab
;
6395 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
6396 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
6397 >= _bfd_stringtab_size (debug_strtab
));
6398 pos
= o
->output_section
->filepos
+ o
->output_offset
;
6399 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
6401 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
6405 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6406 not try to write out the symbols. */
6407 bfd_get_symcount (abfd
) = 0;
6412 if (flinfo
.strtab
!= NULL
)
6413 _bfd_stringtab_free (flinfo
.strtab
);
6415 if (flinfo
.section_info
!= NULL
)
6419 for (i
= 0; i
< abfd
->section_count
; i
++)
6421 if (flinfo
.section_info
[i
].relocs
!= NULL
)
6422 free (flinfo
.section_info
[i
].relocs
);
6423 if (flinfo
.section_info
[i
].rel_hashes
!= NULL
)
6424 free (flinfo
.section_info
[i
].rel_hashes
);
6426 free (flinfo
.section_info
);
6429 if (flinfo
.internal_syms
!= NULL
)
6430 free (flinfo
.internal_syms
);
6431 if (flinfo
.sym_indices
!= NULL
)
6432 free (flinfo
.sym_indices
);
6433 if (flinfo
.outsyms
!= NULL
)
6434 free (flinfo
.outsyms
);
6435 if (flinfo
.linenos
!= NULL
)
6436 free (flinfo
.linenos
);
6437 if (flinfo
.contents
!= NULL
)
6438 free (flinfo
.contents
);
6439 if (flinfo
.external_relocs
!= NULL
)
6440 free (flinfo
.external_relocs
);
6441 if (external_relocs
!= NULL
)
6442 free (external_relocs
);