1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 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"
32 /* This file holds the XCOFF linker code. */
34 #undef STRING_SIZE_SIZE
35 #define STRING_SIZE_SIZE 4
37 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
38 This flag will only be used on input sections. */
40 #define SEC_MARK (SEC_ROM)
42 /* The list of import files. */
44 struct xcoff_import_file
46 /* The next entry in the list. */
47 struct xcoff_import_file
*next
;
52 /* The member name. */
56 /* Information we keep for each section in the output file during the
59 struct xcoff_link_section_info
61 /* The relocs to be output. */
62 struct internal_reloc
*relocs
;
63 /* For each reloc against a global symbol whose index was not known
64 when the reloc was handled, the global hash table entry. */
65 struct xcoff_link_hash_entry
**rel_hashes
;
66 /* If there is a TOC relative reloc against a global symbol, and the
67 index of the TOC symbol is not known when the reloc was handled,
68 an entry is added to this linked list. This is not an array,
69 like rel_hashes, because this case is quite uncommon. */
70 struct xcoff_toc_rel_hash
72 struct xcoff_toc_rel_hash
*next
;
73 struct xcoff_link_hash_entry
*h
;
74 struct internal_reloc
*rel
;
78 /* Information that we pass around while doing the final link step. */
80 struct xcoff_final_link_info
82 /* General link information. */
83 struct bfd_link_info
*info
;
86 /* Hash table for long symbol names. */
87 struct bfd_strtab_hash
*strtab
;
88 /* Array of information kept for each output section, indexed by the
89 target_index field. */
90 struct xcoff_link_section_info
*section_info
;
91 /* Symbol index of last C_FILE symbol (-1 if none). */
93 /* Contents of last C_FILE symbol. */
94 struct internal_syment last_file
;
95 /* Symbol index of TOC symbol. */
97 /* Start of .loader symbols. */
99 /* Next .loader reloc to swap out. */
101 /* File position of start of line numbers. */
102 file_ptr line_filepos
;
103 /* Buffer large enough to hold swapped symbols of any input file. */
104 struct internal_syment
*internal_syms
;
105 /* Buffer large enough to hold output indices of symbols of any
108 /* Buffer large enough to hold output symbols for any input file. */
110 /* Buffer large enough to hold external line numbers for any input
113 /* Buffer large enough to hold any input section. */
115 /* Buffer large enough to hold external relocs of any input section. */
116 bfd_byte
*external_relocs
;
119 static bfd_boolean
xcoff_mark (struct bfd_link_info
*, asection
*);
123 /* Routines to read XCOFF dynamic information. This don't really
124 belong here, but we already have the ldsym manipulation routines
127 /* Read the contents of a section. */
130 xcoff_get_section_contents (bfd
*abfd
, asection
*sec
)
132 if (coff_section_data (abfd
, sec
) == NULL
)
134 bfd_size_type amt
= sizeof (struct coff_section_tdata
);
136 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
137 if (sec
->used_by_bfd
== NULL
)
141 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
145 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
147 if (contents
!= NULL
)
151 coff_section_data (abfd
, sec
)->contents
= contents
;
157 /* Get the size required to hold the dynamic symbols. */
160 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd
*abfd
)
164 struct internal_ldhdr ldhdr
;
166 if ((abfd
->flags
& DYNAMIC
) == 0)
168 bfd_set_error (bfd_error_invalid_operation
);
172 lsec
= bfd_get_section_by_name (abfd
, ".loader");
175 bfd_set_error (bfd_error_no_symbols
);
179 if (! xcoff_get_section_contents (abfd
, lsec
))
181 contents
= coff_section_data (abfd
, lsec
)->contents
;
183 bfd_xcoff_swap_ldhdr_in (abfd
, (void *) contents
, &ldhdr
);
185 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
188 /* Get the dynamic symbols. */
191 _bfd_xcoff_canonicalize_dynamic_symtab (bfd
*abfd
, asymbol
**psyms
)
195 struct internal_ldhdr ldhdr
;
197 bfd_byte
*elsym
, *elsymend
;
198 coff_symbol_type
*symbuf
;
200 if ((abfd
->flags
& DYNAMIC
) == 0)
202 bfd_set_error (bfd_error_invalid_operation
);
206 lsec
= bfd_get_section_by_name (abfd
, ".loader");
209 bfd_set_error (bfd_error_no_symbols
);
213 if (! xcoff_get_section_contents (abfd
, lsec
))
215 contents
= coff_section_data (abfd
, lsec
)->contents
;
217 coff_section_data (abfd
, lsec
)->keep_contents
= TRUE
;
219 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
221 strings
= (char *) contents
+ ldhdr
.l_stoff
;
223 symbuf
= bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (* symbuf
));
227 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
229 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
230 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
), symbuf
++, psyms
++)
232 struct internal_ldsym ldsym
;
234 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
236 symbuf
->symbol
.the_bfd
= abfd
;
238 if (ldsym
._l
._l_l
._l_zeroes
== 0)
239 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
244 c
= bfd_alloc (abfd
, (bfd_size_type
) SYMNMLEN
+ 1);
247 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
249 symbuf
->symbol
.name
= c
;
252 if (ldsym
.l_smclas
== XMC_XO
)
253 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
255 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
257 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
259 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
260 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
261 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
263 /* FIXME: We have no way to record the other information stored
264 with the loader symbol. */
265 *psyms
= (asymbol
*) symbuf
;
270 return ldhdr
.l_nsyms
;
273 /* Get the size required to hold the dynamic relocs. */
276 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd
*abfd
)
280 struct internal_ldhdr ldhdr
;
282 if ((abfd
->flags
& DYNAMIC
) == 0)
284 bfd_set_error (bfd_error_invalid_operation
);
288 lsec
= bfd_get_section_by_name (abfd
, ".loader");
291 bfd_set_error (bfd_error_no_symbols
);
295 if (! xcoff_get_section_contents (abfd
, lsec
))
297 contents
= coff_section_data (abfd
, lsec
)->contents
;
299 bfd_xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
301 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
304 /* Get the dynamic relocs. */
307 _bfd_xcoff_canonicalize_dynamic_reloc (bfd
*abfd
,
313 struct internal_ldhdr ldhdr
;
315 bfd_byte
*elrel
, *elrelend
;
317 if ((abfd
->flags
& DYNAMIC
) == 0)
319 bfd_set_error (bfd_error_invalid_operation
);
323 lsec
= bfd_get_section_by_name (abfd
, ".loader");
326 bfd_set_error (bfd_error_no_symbols
);
330 if (! xcoff_get_section_contents (abfd
, lsec
))
332 contents
= coff_section_data (abfd
, lsec
)->contents
;
334 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
336 relbuf
= bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
340 elrel
= contents
+ bfd_xcoff_loader_reloc_offset(abfd
, &ldhdr
);
342 elrelend
= elrel
+ ldhdr
.l_nreloc
* bfd_xcoff_ldrelsz(abfd
);
343 for (; elrel
< elrelend
; elrel
+= bfd_xcoff_ldrelsz(abfd
), relbuf
++,
346 struct internal_ldrel ldrel
;
348 bfd_xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
350 if (ldrel
.l_symndx
>= 3)
351 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
357 switch (ldrel
.l_symndx
)
373 sec
= bfd_get_section_by_name (abfd
, name
);
376 bfd_set_error (bfd_error_bad_value
);
380 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
383 relbuf
->address
= ldrel
.l_vaddr
;
386 /* Most dynamic relocs have the same type. FIXME: This is only
387 correct if ldrel.l_rtype == 0. In other cases, we should use
388 a different howto. */
389 relbuf
->howto
= bfd_xcoff_dynamic_reloc_howto(abfd
);
391 /* FIXME: We have no way to record the l_rsecnm field. */
398 return ldhdr
.l_nreloc
;
401 /* Routine to create an entry in an XCOFF link hash table. */
403 static struct bfd_hash_entry
*
404 xcoff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
405 struct bfd_hash_table
*table
,
408 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
410 /* Allocate the structure if it has not already been allocated by a
413 ret
= bfd_hash_allocate (table
, sizeof (* ret
));
417 /* Call the allocation method of the superclass. */
418 ret
= ((struct xcoff_link_hash_entry
*)
419 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
423 /* Set local fields. */
425 ret
->toc_section
= NULL
;
426 ret
->u
.toc_indx
= -1;
427 ret
->descriptor
= NULL
;
431 ret
->smclas
= XMC_UA
;
434 return (struct bfd_hash_entry
*) ret
;
437 /* Create a XCOFF link hash table. */
439 struct bfd_link_hash_table
*
440 _bfd_xcoff_bfd_link_hash_table_create (bfd
*abfd
)
442 struct xcoff_link_hash_table
*ret
;
443 bfd_size_type amt
= sizeof (* ret
);
445 ret
= bfd_malloc (amt
);
448 if (!_bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
,
449 sizeof (struct xcoff_link_hash_entry
)))
455 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
456 ret
->debug_section
= NULL
;
457 ret
->loader_section
= NULL
;
458 ret
->ldrel_count
= 0;
459 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
460 ret
->linkage_section
= NULL
;
461 ret
->toc_section
= NULL
;
462 ret
->descriptor_section
= NULL
;
467 memset (ret
->special_sections
, 0, sizeof ret
->special_sections
);
469 /* The linker will always generate a full a.out header. We need to
470 record that fact now, before the sizeof_headers routine could be
472 xcoff_data (abfd
)->full_aouthdr
= TRUE
;
477 /* Free a XCOFF link hash table. */
480 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table
*hash
)
482 struct xcoff_link_hash_table
*ret
= (struct xcoff_link_hash_table
*) hash
;
484 _bfd_stringtab_free (ret
->debug_strtab
);
485 bfd_hash_table_free (&ret
->root
.table
);
489 /* Read internal relocs for an XCOFF csect. This is a wrapper around
490 _bfd_coff_read_internal_relocs which tries to take advantage of any
491 relocs which may have been cached for the enclosing section. */
493 static struct internal_reloc
*
494 xcoff_read_internal_relocs (bfd
*abfd
,
497 bfd_byte
*external_relocs
,
498 bfd_boolean require_internal
,
499 struct internal_reloc
*internal_relocs
)
501 if (coff_section_data (abfd
, sec
) != NULL
502 && coff_section_data (abfd
, sec
)->relocs
== NULL
503 && xcoff_section_data (abfd
, sec
) != NULL
)
507 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
509 if (enclosing
!= NULL
510 && (coff_section_data (abfd
, enclosing
) == NULL
511 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
513 && enclosing
->reloc_count
> 0)
515 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, TRUE
,
516 external_relocs
, FALSE
, NULL
)
521 if (enclosing
!= NULL
522 && coff_section_data (abfd
, enclosing
) != NULL
523 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
527 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
528 / bfd_coff_relsz (abfd
));
530 if (! require_internal
)
531 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
532 memcpy (internal_relocs
,
533 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
534 sec
->reloc_count
* sizeof (struct internal_reloc
));
535 return internal_relocs
;
539 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
540 require_internal
, internal_relocs
);
543 /* This function is used to add symbols from a dynamic object to the
544 global symbol table. */
547 xcoff_link_add_dynamic_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
551 struct internal_ldhdr ldhdr
;
553 bfd_byte
*elsym
, *elsymend
;
554 struct xcoff_import_file
*n
;
559 struct xcoff_import_file
**pp
;
561 /* We can only handle a dynamic object if we are generating an XCOFF
563 if (info
->hash
->creator
!= abfd
->xvec
)
565 (*_bfd_error_handler
)
566 (_("%s: XCOFF shared object when not producing XCOFF output"),
567 bfd_get_filename (abfd
));
568 bfd_set_error (bfd_error_invalid_operation
);
572 /* The symbols we use from a dynamic object are not the symbols in
573 the normal symbol table, but, rather, the symbols in the export
574 table. If there is a global symbol in a dynamic object which is
575 not in the export table, the loader will not be able to find it,
576 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
577 libc.a has symbols in the export table which are not in the
580 /* Read in the .loader section. FIXME: We should really use the
581 o_snloader field in the a.out header, rather than grabbing the
583 lsec
= bfd_get_section_by_name (abfd
, ".loader");
586 (*_bfd_error_handler
)
587 (_("%s: dynamic object with no .loader section"),
588 bfd_get_filename (abfd
));
589 bfd_set_error (bfd_error_no_symbols
);
593 if (! xcoff_get_section_contents (abfd
, lsec
))
595 contents
= coff_section_data (abfd
, lsec
)->contents
;
597 /* Remove the sections from this object, so that they do not get
598 included in the link. */
599 bfd_section_list_clear (abfd
);
601 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
603 strings
= (char *) contents
+ ldhdr
.l_stoff
;
605 elsym
= contents
+ bfd_xcoff_loader_symbol_offset(abfd
, &ldhdr
);
607 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz(abfd
);
609 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz(abfd
))
611 struct internal_ldsym ldsym
;
612 char nambuf
[SYMNMLEN
+ 1];
614 struct xcoff_link_hash_entry
*h
;
616 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
618 /* We are only interested in exported symbols. */
619 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
622 if (ldsym
._l
._l_l
._l_zeroes
== 0)
623 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
626 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
627 nambuf
[SYMNMLEN
] = '\0';
631 /* Normally we could not call xcoff_link_hash_lookup in an add
632 symbols routine, since we might not be using an XCOFF hash
633 table. However, we verified above that we are using an XCOFF
636 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
,
641 h
->flags
|= XCOFF_DEF_DYNAMIC
;
643 /* If the symbol is undefined, and the BFD it was found in is
644 not a dynamic object, change the BFD to this dynamic object,
645 so that we can get the correct import file ID. */
646 if ((h
->root
.type
== bfd_link_hash_undefined
647 || h
->root
.type
== bfd_link_hash_undefweak
)
648 && (h
->root
.u
.undef
.abfd
== NULL
649 || (h
->root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0))
650 h
->root
.u
.undef
.abfd
= abfd
;
652 if (h
->root
.type
== bfd_link_hash_new
)
654 h
->root
.type
= bfd_link_hash_undefined
;
655 h
->root
.u
.undef
.abfd
= abfd
;
656 /* We do not want to add this to the undefined symbol list. */
659 if (h
->smclas
== XMC_UA
660 || h
->root
.type
== bfd_link_hash_undefined
661 || h
->root
.type
== bfd_link_hash_undefweak
)
662 h
->smclas
= ldsym
.l_smclas
;
664 /* Unless this is an XMC_XO symbol, we don't bother to actually
665 define it, since we don't have a section to put it in anyhow.
666 Instead, the relocation routines handle the DEF_DYNAMIC flag
669 if (h
->smclas
== XMC_XO
670 && (h
->root
.type
== bfd_link_hash_undefined
671 || h
->root
.type
== bfd_link_hash_undefweak
))
673 /* This symbol has an absolute value. */
674 h
->root
.type
= bfd_link_hash_defined
;
675 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
676 h
->root
.u
.def
.value
= ldsym
.l_value
;
679 /* If this symbol defines a function descriptor, then it
680 implicitly defines the function code as well. */
681 if (h
->smclas
== XMC_DS
682 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
683 h
->flags
|= XCOFF_DESCRIPTOR
;
684 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
686 struct xcoff_link_hash_entry
*hds
;
693 dsnm
= bfd_malloc ((bfd_size_type
) strlen (name
) + 2);
697 strcpy (dsnm
+ 1, name
);
698 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
704 if (hds
->root
.type
== bfd_link_hash_new
)
706 hds
->root
.type
= bfd_link_hash_undefined
;
707 hds
->root
.u
.undef
.abfd
= abfd
;
708 /* We do not want to add this to the undefined
716 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
717 if (hds
->smclas
== XMC_UA
)
718 hds
->smclas
= XMC_PR
;
720 /* An absolute symbol appears to actually define code, not a
721 function descriptor. This is how some math functions are
722 implemented on AIX 4.1. */
723 if (h
->smclas
== XMC_XO
724 && (hds
->root
.type
== bfd_link_hash_undefined
725 || hds
->root
.type
== bfd_link_hash_undefweak
))
727 hds
->smclas
= XMC_XO
;
728 hds
->root
.type
= bfd_link_hash_defined
;
729 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
730 hds
->root
.u
.def
.value
= ldsym
.l_value
;
735 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
737 free (coff_section_data (abfd
, lsec
)->contents
);
738 coff_section_data (abfd
, lsec
)->contents
= NULL
;
741 /* Record this file in the import files. */
742 n
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (struct xcoff_import_file
));
747 /* For some reason, the path entry in the import file list for a
748 shared object appears to always be empty. The file name is the
751 if (abfd
->my_archive
== NULL
)
753 bname
= bfd_get_filename (abfd
);
758 bname
= bfd_get_filename (abfd
->my_archive
);
759 mname
= bfd_get_filename (abfd
);
761 s
= strrchr (bname
, '/');
767 /* We start c at 1 because the first import file number is reserved
769 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
771 pp
= &(*pp
)->next
, ++c
)
775 xcoff_data (abfd
)->import_file_id
= c
;
780 /* xcoff_link_create_extra_sections
782 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
785 xcoff_link_create_extra_sections (bfd
* abfd
, struct bfd_link_info
*info
)
787 bfd_boolean return_value
= FALSE
;
789 if (info
->hash
->creator
== abfd
->xvec
)
791 /* We need to build a .loader section, so we do it here. This
792 won't work if we're producing an XCOFF output file with no
793 XCOFF input files. FIXME. */
795 if (xcoff_hash_table (info
)->loader_section
== NULL
)
798 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
800 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".loader", flags
);
804 xcoff_hash_table (info
)->loader_section
= lsec
;
807 /* Likewise for the linkage section. */
808 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
811 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
814 lsec
= bfd_make_section_anyway_with_flags (abfd
, ".gl", flags
);
818 xcoff_hash_table (info
)->linkage_section
= lsec
;
819 lsec
->alignment_power
= 2;
822 /* Likewise for the TOC section. */
823 if (xcoff_hash_table (info
)->toc_section
== NULL
)
826 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
829 tsec
= bfd_make_section_anyway_with_flags (abfd
, ".tc", flags
);
833 xcoff_hash_table (info
)->toc_section
= tsec
;
834 tsec
->alignment_power
= 2;
837 /* Likewise for the descriptor section. */
838 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
841 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
844 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".ds", flags
);
848 xcoff_hash_table (info
)->descriptor_section
= dsec
;
849 dsec
->alignment_power
= 2;
852 /* Likewise for the .debug section. */
853 if (xcoff_hash_table (info
)->debug_section
== NULL
854 && info
->strip
!= strip_all
)
857 flagword flags
= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
859 dsec
= bfd_make_section_anyway_with_flags (abfd
, ".debug", flags
);
863 xcoff_hash_table (info
)->debug_section
= dsec
;
874 /* Returns the index of reloc in RELOCS with the least address greater
875 than or equal to ADDRESS. The relocs are sorted by address. */
878 xcoff_find_reloc (struct internal_reloc
*relocs
,
882 bfd_size_type min
, max
, this;
886 if (count
== 1 && relocs
[0].r_vaddr
< address
)
895 /* Do a binary search over (min,max]. */
896 while (min
+ 1 < max
)
900 this = (max
+ min
) / 2;
901 raddr
= relocs
[this].r_vaddr
;
904 else if (raddr
< address
)
913 if (relocs
[min
].r_vaddr
< address
)
917 && relocs
[min
- 1].r_vaddr
== address
)
923 /* Add all the symbols from an object file to the hash table.
925 XCOFF is a weird format. A normal XCOFF .o files will have three
926 COFF sections--.text, .data, and .bss--but each COFF section will
927 contain many csects. These csects are described in the symbol
928 table. From the linker's point of view, each csect must be
929 considered a section in its own right. For example, a TOC entry is
930 handled as a small XMC_TC csect. The linker must be able to merge
931 different TOC entries together, which means that it must be able to
932 extract the XMC_TC csects from the .data section of the input .o
935 From the point of view of our linker, this is, of course, a hideous
936 nightmare. We cope by actually creating sections for each csect,
937 and discarding the original sections. We then have to handle the
938 relocation entries carefully, since the only way to tell which
939 csect they belong to is to examine the address. */
942 xcoff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
944 unsigned int n_tmask
;
945 unsigned int n_btshft
;
946 bfd_boolean default_copy
;
947 bfd_size_type symcount
;
948 struct xcoff_link_hash_entry
**sym_hash
;
949 asection
**csect_cache
;
950 bfd_size_type linesz
;
953 bfd_boolean keep_syms
;
955 unsigned int csect_index
;
956 asection
*first_csect
;
957 bfd_size_type symesz
;
960 struct reloc_info_struct
962 struct internal_reloc
*relocs
;
965 } *reloc_info
= NULL
;
968 keep_syms
= obj_coff_keep_syms (abfd
);
970 if ((abfd
->flags
& DYNAMIC
) != 0
971 && ! info
->static_link
)
973 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
977 /* Create the loader, toc, gl, ds and debug sections, if needed. */
978 if (! xcoff_link_create_extra_sections (abfd
, info
))
981 if ((abfd
->flags
& DYNAMIC
) != 0
982 && ! info
->static_link
)
985 n_tmask
= coff_data (abfd
)->local_n_tmask
;
986 n_btshft
= coff_data (abfd
)->local_n_btshft
;
988 /* Define macros so that ISFCN, et. al., macros work correctly. */
989 #define N_TMASK n_tmask
990 #define N_BTSHFT n_btshft
992 if (info
->keep_memory
)
993 default_copy
= FALSE
;
997 symcount
= obj_raw_syment_count (abfd
);
999 /* We keep a list of the linker hash table entries that correspond
1000 to each external symbol. */
1001 amt
= symcount
* sizeof (struct xcoff_link_hash_entry
*);
1002 sym_hash
= bfd_zalloc (abfd
, amt
);
1003 if (sym_hash
== NULL
&& symcount
!= 0)
1005 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1007 /* Because of the weird stuff we are doing with XCOFF csects, we can
1008 not easily determine which section a symbol is in, so we store
1009 the information in the tdata for the input file. */
1010 amt
= symcount
* sizeof (asection
*);
1011 csect_cache
= bfd_zalloc (abfd
, amt
);
1012 if (csect_cache
== NULL
&& symcount
!= 0)
1014 xcoff_data (abfd
)->csects
= csect_cache
;
1016 /* While splitting sections into csects, we need to assign the
1017 relocs correctly. The relocs and the csects must both be in
1018 order by VMA within a given section, so we handle this by
1019 scanning along the relocs as we process the csects. We index
1020 into reloc_info using the section target_index. */
1021 amt
= abfd
->section_count
+ 1;
1022 amt
*= sizeof (struct reloc_info_struct
);
1023 reloc_info
= bfd_zmalloc (amt
);
1024 if (reloc_info
== NULL
)
1027 /* Read in the relocs and line numbers for each section. */
1028 linesz
= bfd_coff_linesz (abfd
);
1030 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1034 if ((o
->flags
& SEC_RELOC
) != 0)
1036 reloc_info
[o
->target_index
].relocs
=
1037 xcoff_read_internal_relocs (abfd
, o
, TRUE
, NULL
, FALSE
, NULL
);
1038 amt
= o
->reloc_count
;
1039 amt
*= sizeof (asection
*);
1040 reloc_info
[o
->target_index
].csects
= bfd_zmalloc (amt
);
1041 if (reloc_info
[o
->target_index
].csects
== NULL
)
1045 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1046 && o
->lineno_count
> 0)
1050 amt
= linesz
* o
->lineno_count
;
1051 linenos
= bfd_malloc (amt
);
1052 if (linenos
== NULL
)
1054 reloc_info
[o
->target_index
].linenos
= linenos
;
1055 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1056 || bfd_bread (linenos
, amt
, abfd
) != amt
)
1061 /* Don't let the linker relocation routines discard the symbols. */
1062 obj_coff_keep_syms (abfd
) = TRUE
;
1068 symesz
= bfd_coff_symesz (abfd
);
1069 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1070 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1071 esym_end
= esym
+ symcount
* symesz
;
1073 while (esym
< esym_end
)
1075 struct internal_syment sym
;
1076 union internal_auxent aux
;
1078 char buf
[SYMNMLEN
+ 1];
1083 struct xcoff_link_hash_entry
*set_toc
;
1085 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
1087 /* In this pass we are only interested in symbols with csect
1089 if (sym
.n_sclass
!= C_EXT
&& sym
.n_sclass
!= C_HIDEXT
)
1092 Normally csect is a .pr, .rw etc. created in the loop
1093 If C_FILE or first time, handle special
1095 Advance esym, sym_hash, csect_hash ptr's
1096 Keep track of the last_symndx for the current file. */
1097 if (sym
.n_sclass
== C_FILE
&& csect
!= NULL
)
1099 xcoff_section_data (abfd
, csect
)->last_symndx
=
1101 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1107 *csect_cache
= csect
;
1108 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1109 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1111 *csect_cache
= NULL
;
1112 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1113 sym_hash
+= sym
.n_numaux
+ 1;
1114 csect_cache
+= sym
.n_numaux
+ 1;
1119 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1124 /* If this symbol has line number information attached to it,
1125 and we're not stripping it, count the number of entries and
1126 add them to the count for this csect. In the final link pass
1127 we are going to attach line number information by symbol,
1128 rather than by section, in order to more easily handle
1129 garbage collection. */
1130 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1133 && ISFCN (sym
.n_type
))
1135 union internal_auxent auxlin
;
1137 bfd_coff_swap_aux_in (abfd
, (void *) (esym
+ symesz
),
1138 sym
.n_type
, sym
.n_sclass
,
1139 0, sym
.n_numaux
, (void *) &auxlin
);
1141 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1143 asection
*enclosing
;
1144 bfd_signed_vma linoff
;
1146 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1147 if (enclosing
== NULL
)
1149 (*_bfd_error_handler
)
1150 (_("%B: `%s' has line numbers but no enclosing section"),
1152 bfd_set_error (bfd_error_bad_value
);
1155 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1156 - enclosing
->line_filepos
);
1157 /* Explicit cast to bfd_signed_vma for compiler. */
1158 if (linoff
< (bfd_signed_vma
) (enclosing
->lineno_count
* linesz
))
1160 struct internal_lineno lin
;
1161 bfd_byte
*linpstart
;
1163 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1165 bfd_coff_swap_lineno_in (abfd
, (void *) linpstart
, (void *) &lin
);
1167 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1169 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1172 bfd_byte
*linpend
, *linp
;
1174 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1175 + enclosing
->lineno_count
* linesz
);
1176 for (linp
= linpstart
+ linesz
;
1180 bfd_coff_swap_lineno_in (abfd
, (void *) linp
,
1182 if (lin
.l_lnno
== 0)
1185 csect
->lineno_count
+= (linp
- linpstart
) / linesz
;
1186 /* The setting of line_filepos will only be
1187 useful if all the line number entries for a
1188 csect are contiguous; this only matters for
1190 if (csect
->line_filepos
== 0)
1191 csect
->line_filepos
=
1192 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1198 /* Pick up the csect auxiliary information. */
1199 if (sym
.n_numaux
== 0)
1201 (*_bfd_error_handler
)
1202 (_("%B: class %d symbol `%s' has no aux entries"),
1203 abfd
, sym
.n_sclass
, name
);
1204 bfd_set_error (bfd_error_bad_value
);
1208 bfd_coff_swap_aux_in (abfd
,
1209 (void *) (esym
+ symesz
* sym
.n_numaux
),
1210 sym
.n_type
, sym
.n_sclass
,
1211 sym
.n_numaux
- 1, sym
.n_numaux
,
1214 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1224 (*_bfd_error_handler
)
1225 (_("%B: symbol `%s' has unrecognized csect type %d"),
1227 bfd_set_error (bfd_error_bad_value
);
1231 /* This is an external reference. */
1232 if (sym
.n_sclass
== C_HIDEXT
1233 || sym
.n_scnum
!= N_UNDEF
1234 || aux
.x_csect
.x_scnlen
.l
!= 0)
1236 (*_bfd_error_handler
)
1237 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1238 abfd
, name
, sym
.n_sclass
, sym
.n_scnum
,
1239 aux
.x_csect
.x_scnlen
.l
);
1240 bfd_set_error (bfd_error_bad_value
);
1244 /* An XMC_XO external reference is actually a reference to
1245 an absolute location. */
1246 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1247 section
= bfd_und_section_ptr
;
1250 section
= bfd_abs_section_ptr
;
1251 value
= sym
.n_value
;
1256 /* This is a csect definition. */
1259 xcoff_section_data (abfd
, csect
)->last_symndx
=
1260 ((esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)) / symesz
);
1264 csect_index
= -(unsigned) 1;
1266 /* When we see a TOC anchor, we record the TOC value. */
1267 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1269 if (sym
.n_sclass
!= C_HIDEXT
1270 || aux
.x_csect
.x_scnlen
.l
!= 0)
1272 (*_bfd_error_handler
)
1273 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1274 abfd
, name
, sym
.n_sclass
, aux
.x_csect
.x_scnlen
.l
);
1275 bfd_set_error (bfd_error_bad_value
);
1278 xcoff_data (abfd
)->toc
= sym
.n_value
;
1281 /* We must merge TOC entries for the same symbol. We can
1282 merge two TOC entries if they are both C_HIDEXT, they
1283 both have the same name, they are both 4 or 8 bytes long, and
1284 they both have a relocation table entry for an external
1285 symbol with the same name. Unfortunately, this means
1286 that we must look through the relocations. Ick.
1288 Logic for 32 bit vs 64 bit.
1289 32 bit has a csect length of 4 for TOC
1290 64 bit has a csect length of 8 for TOC
1292 The conditions to get past the if-check are not that bad.
1293 They are what is used to create the TOC csects in the first
1295 if (aux
.x_csect
.x_smclas
== XMC_TC
1296 && sym
.n_sclass
== C_HIDEXT
1297 && info
->hash
->creator
== abfd
->xvec
1298 && ((bfd_xcoff_is_xcoff32 (abfd
)
1299 && aux
.x_csect
.x_scnlen
.l
== 4)
1300 || (bfd_xcoff_is_xcoff64 (abfd
)
1301 && aux
.x_csect
.x_scnlen
.l
== 8)))
1303 asection
*enclosing
;
1304 struct internal_reloc
*relocs
;
1305 bfd_size_type relindx
;
1306 struct internal_reloc
*rel
;
1308 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1309 if (enclosing
== NULL
)
1312 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1313 amt
= enclosing
->reloc_count
;
1314 relindx
= xcoff_find_reloc (relocs
, amt
, sym
.n_value
);
1315 rel
= relocs
+ relindx
;
1317 /* 32 bit R_POS r_size is 31
1318 64 bit R_POS r_size is 63 */
1319 if (relindx
< enclosing
->reloc_count
1320 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1321 && rel
->r_type
== R_POS
1322 && ((bfd_xcoff_is_xcoff32 (abfd
)
1323 && rel
->r_size
== 31)
1324 || (bfd_xcoff_is_xcoff64 (abfd
)
1325 && rel
->r_size
== 63)))
1329 struct internal_syment relsym
;
1331 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1332 + rel
->r_symndx
* symesz
);
1333 bfd_coff_swap_sym_in (abfd
, (void *) erelsym
, (void *) &relsym
);
1334 if (relsym
.n_sclass
== C_EXT
)
1336 const char *relname
;
1337 char relbuf
[SYMNMLEN
+ 1];
1339 struct xcoff_link_hash_entry
*h
;
1341 /* At this point we know that the TOC entry is
1342 for an externally visible symbol. */
1343 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1345 if (relname
== NULL
)
1348 /* We only merge TOC entries if the TC name is
1349 the same as the symbol name. This handles
1350 the normal case, but not common cases like
1351 SYM.P4 which gcc generates to store SYM + 4
1352 in the TOC. FIXME. */
1353 if (strcmp (name
, relname
) == 0)
1355 copy
= (! info
->keep_memory
1356 || relsym
._n
._n_n
._n_zeroes
!= 0
1357 || relsym
._n
._n_n
._n_offset
== 0);
1358 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1359 relname
, TRUE
, copy
,
1364 /* At this point h->root.type could be
1365 bfd_link_hash_new. That should be OK,
1366 since we know for sure that we will come
1367 across this symbol as we step through the
1370 /* We store h in *sym_hash for the
1371 convenience of the relocate_section
1375 if (h
->toc_section
!= NULL
)
1377 asection
**rel_csects
;
1379 /* We already have a TOC entry for this
1380 symbol, so we can just ignore this
1383 reloc_info
[enclosing
->target_index
].csects
;
1384 rel_csects
[relindx
] = bfd_und_section_ptr
;
1388 /* We are about to create a TOC entry for
1397 asection
*enclosing
;
1399 /* We need to create a new section. We get the name from
1400 the csect storage mapping class, so that the linker can
1401 accumulate similar csects together. */
1403 csect
= bfd_xcoff_create_csect_from_smclas(abfd
, &aux
, name
);
1407 /* The enclosing section is the main section : .data, .text
1408 or .bss that the csect is coming from. */
1409 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1410 if (enclosing
== NULL
)
1413 if (! bfd_is_abs_section (enclosing
)
1414 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1415 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1416 > enclosing
->vma
+ enclosing
->size
)))
1418 (*_bfd_error_handler
)
1419 (_("%B: csect `%s' not in enclosing section"),
1421 bfd_set_error (bfd_error_bad_value
);
1424 csect
->vma
= sym
.n_value
;
1425 csect
->filepos
= (enclosing
->filepos
1428 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1429 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1430 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1432 /* Record the enclosing section in the tdata for this new
1434 amt
= sizeof (struct coff_section_tdata
);
1435 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1436 if (csect
->used_by_bfd
== NULL
)
1438 amt
= sizeof (struct xcoff_section_tdata
);
1439 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1440 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1442 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1443 xcoff_section_data (abfd
, csect
)->lineno_count
=
1444 enclosing
->lineno_count
;
1446 if (enclosing
->owner
== abfd
)
1448 struct internal_reloc
*relocs
;
1449 bfd_size_type relindx
;
1450 struct internal_reloc
*rel
;
1451 asection
**rel_csect
;
1453 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1454 amt
= enclosing
->reloc_count
;
1455 relindx
= xcoff_find_reloc (relocs
, amt
, csect
->vma
);
1457 rel
= relocs
+ relindx
;
1458 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1461 csect
->rel_filepos
= (enclosing
->rel_filepos
1462 + relindx
* bfd_coff_relsz (abfd
));
1463 while (relindx
< enclosing
->reloc_count
1464 && *rel_csect
== NULL
1465 && rel
->r_vaddr
< csect
->vma
+ csect
->size
)
1469 csect
->flags
|= SEC_RELOC
;
1470 ++csect
->reloc_count
;
1477 /* There are a number of other fields and section flags
1478 which we do not bother to set. */
1480 csect_index
= ((esym
1481 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1484 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1486 if (first_csect
== NULL
)
1487 first_csect
= csect
;
1489 /* If this symbol is C_EXT, we treat it as starting at the
1490 beginning of the newly created section. */
1491 if (sym
.n_sclass
== C_EXT
)
1497 /* If this is a TOC section for a symbol, record it. */
1498 if (set_toc
!= NULL
)
1499 set_toc
->toc_section
= csect
;
1504 /* This is a label definition. The x_scnlen field is the
1505 symbol index of the csect. Usually the XTY_LD symbol will
1506 follow its appropriate XTY_SD symbol. The .set pseudo op can
1507 cause the XTY_LD to not follow the XTY_SD symbol. */
1512 if (aux
.x_csect
.x_scnlen
.l
< 0
1513 || (aux
.x_csect
.x_scnlen
.l
1514 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1518 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1520 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1525 (*_bfd_error_handler
)
1526 (_("%B: misplaced XTY_LD `%s'"),
1528 bfd_set_error (bfd_error_bad_value
);
1532 value
= sym
.n_value
- csect
->vma
;
1537 /* This is an unitialized csect. We could base the name on
1538 the storage mapping class, but we don't bother except for
1539 an XMC_TD symbol. If this csect is externally visible,
1540 it is a common symbol. We put XMC_TD symbols in sections
1541 named .tocbss, and rely on the linker script to put that
1546 xcoff_section_data (abfd
, csect
)->last_symndx
=
1548 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1552 if (aux
.x_csect
.x_smclas
== XMC_TD
)
1554 /* The linker script puts the .td section in the data
1555 section after the .tc section. */
1556 csect
= bfd_make_section_anyway_with_flags (abfd
, ".td",
1560 csect
= bfd_make_section_anyway_with_flags (abfd
, ".bss",
1565 csect
->vma
= sym
.n_value
;
1566 csect
->size
= aux
.x_csect
.x_scnlen
.l
;
1567 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1568 /* There are a number of other fields and section flags
1569 which we do not bother to set. */
1571 csect_index
= ((esym
1572 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1575 amt
= sizeof (struct coff_section_tdata
);
1576 csect
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
1577 if (csect
->used_by_bfd
== NULL
)
1579 amt
= sizeof (struct xcoff_section_tdata
);
1580 coff_section_data (abfd
, csect
)->tdata
= bfd_zalloc (abfd
, amt
);
1581 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1583 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1585 if (first_csect
== NULL
)
1586 first_csect
= csect
;
1588 if (sym
.n_sclass
== C_EXT
)
1590 csect
->flags
|= SEC_IS_COMMON
;
1593 value
= aux
.x_csect
.x_scnlen
.l
;
1599 /* Check for magic symbol names. */
1600 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1601 && aux
.x_csect
.x_smclas
!= XMC_TC
1602 && aux
.x_csect
.x_smclas
!= XMC_TD
)
1608 if (strcmp (name
, "_text") == 0)
1609 i
= XCOFF_SPECIAL_SECTION_TEXT
;
1610 else if (strcmp (name
, "_etext") == 0)
1611 i
= XCOFF_SPECIAL_SECTION_ETEXT
;
1612 else if (strcmp (name
, "_data") == 0)
1613 i
= XCOFF_SPECIAL_SECTION_DATA
;
1614 else if (strcmp (name
, "_edata") == 0)
1615 i
= XCOFF_SPECIAL_SECTION_EDATA
;
1616 else if (strcmp (name
, "_end") == 0)
1617 i
= XCOFF_SPECIAL_SECTION_END
;
1619 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1620 i
= XCOFF_SPECIAL_SECTION_END2
;
1623 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1626 /* Now we have enough information to add the symbol to the
1627 linker hash table. */
1629 if (sym
.n_sclass
== C_EXT
)
1633 BFD_ASSERT (section
!= NULL
);
1635 /* We must copy the name into memory if we got it from the
1636 syment itself, rather than the string table. */
1637 copy
= default_copy
;
1638 if (sym
._n
._n_n
._n_zeroes
!= 0
1639 || sym
._n
._n_n
._n_offset
== 0)
1642 /* The AIX linker appears to only detect multiple symbol
1643 definitions when there is a reference to the symbol. If
1644 a symbol is defined multiple times, and the only
1645 references are from the same object file, the AIX linker
1646 appears to permit it. It does not merge the different
1647 definitions, but handles them independently. On the
1648 other hand, if there is a reference, the linker reports
1651 This matters because the AIX <net/net_globals.h> header
1652 file actually defines an initialized array, so we have to
1653 actually permit that to work.
1655 Just to make matters even more confusing, the AIX linker
1656 appears to permit multiple symbol definitions whenever
1657 the second definition is in an archive rather than an
1658 object file. This may be a consequence of the manner in
1659 which it handles archives: I think it may load the entire
1660 archive in as separate csects, and then let garbage
1661 collection discard symbols.
1663 We also have to handle the case of statically linking a
1664 shared object, which will cause symbol redefinitions,
1665 although this is an easier case to detect. */
1667 if (info
->hash
->creator
== abfd
->xvec
)
1669 if (! bfd_is_und_section (section
))
1670 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1671 name
, TRUE
, copy
, FALSE
);
1673 /* Make a copy of the symbol name to prevent problems with
1675 *sym_hash
= ((struct xcoff_link_hash_entry
*)
1676 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
1677 TRUE
, TRUE
, FALSE
));
1679 if (*sym_hash
== NULL
)
1681 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1682 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1683 && ! bfd_is_und_section (section
)
1684 && ! bfd_is_com_section (section
))
1686 /* This is a second definition of a defined symbol. */
1687 if ((abfd
->flags
& DYNAMIC
) != 0
1688 && ((*sym_hash
)->smclas
!= XMC_GL
1689 || aux
.x_csect
.x_smclas
== XMC_GL
1690 || ((*sym_hash
)->root
.u
.def
.section
->owner
->flags
1693 /* The new symbol is from a shared library, and
1694 either the existing symbol is not global
1695 linkage code or this symbol is global linkage
1696 code. If the existing symbol is global
1697 linkage code and the new symbol is not, then
1698 we want to use the new symbol. */
1699 section
= bfd_und_section_ptr
;
1702 else if (((*sym_hash
)->root
.u
.def
.section
->owner
->flags
1705 /* The existing symbol is from a shared library.
1707 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1708 (*sym_hash
)->root
.u
.undef
.abfd
=
1709 (*sym_hash
)->root
.u
.def
.section
->owner
;
1711 else if (abfd
->my_archive
!= NULL
)
1713 /* This is a redefinition in an object contained
1714 in an archive. Just ignore it. See the
1716 section
= bfd_und_section_ptr
;
1719 else if ((*sym_hash
)->root
.u
.undef
.next
!= NULL
1720 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
1722 /* This symbol has been referenced. In this
1723 case, we just continue and permit the
1724 multiple definition error. See the comment
1725 above about the behaviour of the AIX linker. */
1727 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
1729 /* The symbols are both csects of the same
1730 class. There is at least a chance that this
1731 is a semi-legitimate redefinition. */
1732 section
= bfd_und_section_ptr
;
1734 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
1737 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
1738 && ((*sym_hash
)->root
.type
== bfd_link_hash_defined
1739 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1740 && (bfd_is_und_section (section
)
1741 || bfd_is_com_section (section
)))
1743 /* This is a reference to a multiply defined symbol.
1744 Report the error now. See the comment above
1745 about the behaviour of the AIX linker. We could
1746 also do this with warning symbols, but I'm not
1747 sure the XCOFF linker is wholly prepared to
1748 handle them, and that would only be a warning,
1750 if (! ((*info
->callbacks
->multiple_definition
)
1751 (info
, (*sym_hash
)->root
.root
.string
,
1752 NULL
, NULL
, (bfd_vma
) 0,
1753 (*sym_hash
)->root
.u
.def
.section
->owner
,
1754 (*sym_hash
)->root
.u
.def
.section
,
1755 (*sym_hash
)->root
.u
.def
.value
)))
1757 /* Try not to give this error too many times. */
1758 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
1762 /* _bfd_generic_link_add_one_symbol may call the linker to
1763 generate an error message, and the linker may try to read
1764 the symbol table to give a good error. Right now, the
1765 line numbers are in an inconsistent state, since they are
1766 counted both in the real sections and in the new csects.
1767 We need to leave the count in the real sections so that
1768 the linker can report the line number of the error
1769 correctly, so temporarily clobber the link to the csects
1770 so that the linker will not try to read the line numbers
1771 a second time from the csects. */
1772 BFD_ASSERT (last_real
->next
== first_csect
);
1773 last_real
->next
= NULL
;
1774 if (! (_bfd_generic_link_add_one_symbol
1775 (info
, abfd
, name
, flags
, section
, value
,
1777 (struct bfd_link_hash_entry
**) sym_hash
)))
1779 last_real
->next
= first_csect
;
1781 if (smtyp
== XTY_CM
)
1783 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
1784 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
1785 /* We don't need the common csect we just created. */
1788 (*sym_hash
)->root
.u
.c
.p
->alignment_power
1789 = csect
->alignment_power
;
1792 if (info
->hash
->creator
== abfd
->xvec
)
1796 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
1797 flag
= XCOFF_REF_REGULAR
;
1799 flag
= XCOFF_DEF_REGULAR
;
1800 (*sym_hash
)->flags
|= flag
;
1802 if ((*sym_hash
)->smclas
== XMC_UA
1803 || flag
== XCOFF_DEF_REGULAR
)
1804 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
1808 *csect_cache
= csect
;
1810 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1811 sym_hash
+= sym
.n_numaux
+ 1;
1812 csect_cache
+= sym
.n_numaux
+ 1;
1815 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
1817 /* Make sure that we have seen all the relocs. */
1818 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
1820 /* Reset the section size and the line number count, since the
1821 data is now attached to the csects. Don't reset the size of
1822 the .debug section, since we need to read it below in
1823 bfd_xcoff_size_dynamic_sections. */
1824 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
1826 o
->lineno_count
= 0;
1828 if ((o
->flags
& SEC_RELOC
) != 0)
1831 struct internal_reloc
*rel
;
1832 asection
**rel_csect
;
1834 rel
= reloc_info
[o
->target_index
].relocs
;
1835 rel_csect
= reloc_info
[o
->target_index
].csects
;
1837 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
1839 if (*rel_csect
== NULL
)
1841 (*_bfd_error_handler
)
1842 (_("%B: reloc %s:%d not in csect"),
1844 bfd_set_error (bfd_error_bad_value
);
1848 /* We identify all symbols which are called, so that we
1849 can create glue code for calls to functions imported
1850 from dynamic objects. */
1851 if (info
->hash
->creator
== abfd
->xvec
1852 && *rel_csect
!= bfd_und_section_ptr
1853 && (rel
->r_type
== R_BR
1854 || rel
->r_type
== R_RBR
)
1855 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
1857 struct xcoff_link_hash_entry
*h
;
1859 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
1860 h
->flags
|= XCOFF_CALLED
;
1861 /* If the symbol name starts with a period, it is
1862 the code of a function. If the symbol is
1863 currently undefined, then add an undefined symbol
1864 for the function descriptor. This should do no
1865 harm, because any regular object that defines the
1866 function should also define the function
1867 descriptor. It helps, because it means that we
1868 will identify the function descriptor with a
1869 dynamic object if a dynamic object defines it. */
1870 if (h
->root
.root
.string
[0] == '.'
1871 && h
->descriptor
== NULL
)
1873 struct xcoff_link_hash_entry
*hds
;
1874 struct bfd_link_hash_entry
*bh
;
1876 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1877 h
->root
.root
.string
+ 1,
1881 if (hds
->root
.type
== bfd_link_hash_new
)
1884 if (! (_bfd_generic_link_add_one_symbol
1885 (info
, abfd
, hds
->root
.root
.string
,
1886 (flagword
) 0, bfd_und_section_ptr
,
1887 (bfd_vma
) 0, NULL
, FALSE
,
1890 hds
= (struct xcoff_link_hash_entry
*) bh
;
1892 hds
->flags
|= XCOFF_DESCRIPTOR
;
1893 BFD_ASSERT ((hds
->flags
& XCOFF_CALLED
) == 0
1894 && (h
->flags
& XCOFF_DESCRIPTOR
) == 0);
1895 hds
->descriptor
= h
;
1896 h
->descriptor
= hds
;
1901 free (reloc_info
[o
->target_index
].csects
);
1902 reloc_info
[o
->target_index
].csects
= NULL
;
1904 /* Reset SEC_RELOC and the reloc_count, since the reloc
1905 information is now attached to the csects. */
1906 o
->flags
&=~ SEC_RELOC
;
1909 /* If we are not keeping memory, free the reloc information. */
1910 if (! info
->keep_memory
1911 && coff_section_data (abfd
, o
) != NULL
1912 && coff_section_data (abfd
, o
)->relocs
!= NULL
1913 && ! coff_section_data (abfd
, o
)->keep_relocs
)
1915 free (coff_section_data (abfd
, o
)->relocs
);
1916 coff_section_data (abfd
, o
)->relocs
= NULL
;
1920 /* Free up the line numbers. FIXME: We could cache these
1921 somewhere for the final link, to avoid reading them again. */
1922 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
1924 free (reloc_info
[o
->target_index
].linenos
);
1925 reloc_info
[o
->target_index
].linenos
= NULL
;
1931 obj_coff_keep_syms (abfd
) = keep_syms
;
1936 if (reloc_info
!= NULL
)
1938 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1940 if (reloc_info
[o
->target_index
].csects
!= NULL
)
1941 free (reloc_info
[o
->target_index
].csects
);
1942 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
1943 free (reloc_info
[o
->target_index
].linenos
);
1947 obj_coff_keep_syms (abfd
) = keep_syms
;
1954 /* Add symbols from an XCOFF object file. */
1957 xcoff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
1959 if (! _bfd_coff_get_external_symbols (abfd
))
1961 if (! xcoff_link_add_symbols (abfd
, info
))
1963 if (! info
->keep_memory
)
1965 if (! _bfd_coff_free_symbols (abfd
))
1971 /* Look through the loader symbols to see if this dynamic object
1972 should be included in the link. The native linker uses the loader
1973 symbols, not the normal symbol table, so we do too. */
1976 xcoff_link_check_dynamic_ar_symbols (bfd
*abfd
,
1977 struct bfd_link_info
*info
,
1978 bfd_boolean
*pneeded
)
1982 struct internal_ldhdr ldhdr
;
1983 const char *strings
;
1984 bfd_byte
*elsym
, *elsymend
;
1988 lsec
= bfd_get_section_by_name (abfd
, ".loader");
1990 /* There are no symbols, so don't try to include it. */
1993 if (! xcoff_get_section_contents (abfd
, lsec
))
1995 contents
= coff_section_data (abfd
, lsec
)->contents
;
1997 bfd_xcoff_swap_ldhdr_in (abfd
, contents
, &ldhdr
);
1999 strings
= (char *) contents
+ ldhdr
.l_stoff
;
2001 elsym
= contents
+ bfd_xcoff_loader_symbol_offset (abfd
, &ldhdr
);
2003 elsymend
= elsym
+ ldhdr
.l_nsyms
* bfd_xcoff_ldsymsz (abfd
);
2004 for (; elsym
< elsymend
; elsym
+= bfd_xcoff_ldsymsz (abfd
))
2006 struct internal_ldsym ldsym
;
2007 char nambuf
[SYMNMLEN
+ 1];
2009 struct bfd_link_hash_entry
*h
;
2011 bfd_xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2013 /* We are only interested in exported symbols. */
2014 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2017 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2018 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2021 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2022 nambuf
[SYMNMLEN
] = '\0';
2026 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2028 /* We are only interested in symbols that are currently
2029 undefined. At this point we know that we are using an XCOFF
2032 && h
->type
== bfd_link_hash_undefined
2033 && (((struct xcoff_link_hash_entry
*) h
)->flags
2034 & XCOFF_DEF_DYNAMIC
) == 0)
2036 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
2043 /* We do not need this shared object. */
2044 if (contents
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2046 free (coff_section_data (abfd
, lsec
)->contents
);
2047 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2053 /* Look through the symbols to see if this object file should be
2054 included in the link. */
2057 xcoff_link_check_ar_symbols (bfd
*abfd
,
2058 struct bfd_link_info
*info
,
2059 bfd_boolean
*pneeded
)
2061 bfd_size_type symesz
;
2067 if ((abfd
->flags
& DYNAMIC
) != 0
2068 && ! info
->static_link
2069 && info
->hash
->creator
== abfd
->xvec
)
2070 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
);
2072 symesz
= bfd_coff_symesz (abfd
);
2073 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
2074 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
2075 while (esym
< esym_end
)
2077 struct internal_syment sym
;
2079 bfd_coff_swap_sym_in (abfd
, (void *) esym
, (void *) &sym
);
2081 if (sym
.n_sclass
== C_EXT
&& sym
.n_scnum
!= N_UNDEF
)
2084 char buf
[SYMNMLEN
+ 1];
2085 struct bfd_link_hash_entry
*h
;
2087 /* This symbol is externally visible, and is defined by this
2089 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
2093 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
2095 /* We are only interested in symbols that are currently
2096 undefined. If a symbol is currently known to be common,
2097 XCOFF linkers do not bring in an object file which
2098 defines it. We also don't bring in symbols to satisfy
2099 undefined references in shared objects. */
2101 && h
->type
== bfd_link_hash_undefined
2102 && (info
->hash
->creator
!= abfd
->xvec
2103 || (((struct xcoff_link_hash_entry
*) h
)->flags
2104 & XCOFF_DEF_DYNAMIC
) == 0))
2106 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
2113 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2116 /* We do not need this object file. */
2120 /* Check a single archive element to see if we need to include it in
2121 the link. *PNEEDED is set according to whether this element is
2122 needed in the link or not. This is called via
2123 _bfd_generic_link_add_archive_symbols. */
2126 xcoff_link_check_archive_element (bfd
*abfd
,
2127 struct bfd_link_info
*info
,
2128 bfd_boolean
*pneeded
)
2130 if (! _bfd_coff_get_external_symbols (abfd
))
2133 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
2138 if (! xcoff_link_add_symbols (abfd
, info
))
2142 if (! info
->keep_memory
|| ! *pneeded
)
2144 if (! _bfd_coff_free_symbols (abfd
))
2151 /* Given an XCOFF BFD, add symbols to the global hash table as
2155 _bfd_xcoff_bfd_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2157 switch (bfd_get_format (abfd
))
2160 return xcoff_link_add_object_symbols (abfd
, info
);
2163 /* If the archive has a map, do the usual search. We then need
2164 to check the archive for dynamic objects, because they may not
2165 appear in the archive map even though they should, perhaps, be
2166 included. If the archive has no map, we just consider each object
2167 file in turn, since that apparently is what the AIX native linker
2169 if (bfd_has_map (abfd
))
2171 if (! (_bfd_generic_link_add_archive_symbols
2172 (abfd
, info
, xcoff_link_check_archive_element
)))
2179 member
= bfd_openr_next_archived_file (abfd
, NULL
);
2180 while (member
!= NULL
)
2182 if (bfd_check_format (member
, bfd_object
)
2183 && (info
->hash
->creator
== member
->xvec
)
2184 && (! bfd_has_map (abfd
) || (member
->flags
& DYNAMIC
) != 0))
2188 if (! xcoff_link_check_archive_element (member
, info
,
2192 member
->archive_pass
= -1;
2194 member
= bfd_openr_next_archived_file (abfd
, member
);
2201 bfd_set_error (bfd_error_wrong_format
);
2206 /* Mark a symbol as not being garbage, including the section in which
2209 static inline bfd_boolean
2210 xcoff_mark_symbol (struct bfd_link_info
*info
, struct xcoff_link_hash_entry
*h
)
2212 if ((h
->flags
& XCOFF_MARK
) != 0)
2215 h
->flags
|= XCOFF_MARK
;
2216 if (h
->root
.type
== bfd_link_hash_defined
2217 || h
->root
.type
== bfd_link_hash_defweak
)
2221 hsec
= h
->root
.u
.def
.section
;
2222 if (! bfd_is_abs_section (hsec
)
2223 && (hsec
->flags
& SEC_MARK
) == 0)
2225 if (! xcoff_mark (info
, hsec
))
2230 if (h
->toc_section
!= NULL
2231 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2233 if (! xcoff_mark (info
, h
->toc_section
))
2240 /* The mark phase of garbage collection. For a given section, mark
2241 it, and all the sections which define symbols to which it refers.
2242 Because this function needs to look at the relocs, we also count
2243 the number of relocs which need to be copied into the .loader
2247 xcoff_mark (struct bfd_link_info
*info
, asection
*sec
)
2249 if (bfd_is_abs_section (sec
)
2250 || (sec
->flags
& SEC_MARK
) != 0)
2253 sec
->flags
|= SEC_MARK
;
2255 if (sec
->owner
->xvec
== info
->hash
->creator
2256 && coff_section_data (sec
->owner
, sec
) != NULL
2257 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2259 struct xcoff_link_hash_entry
**hp
, **hpend
;
2260 struct internal_reloc
*rel
, *relend
;
2262 /* Mark all the symbols in this section. */
2263 hp
= (obj_xcoff_sym_hashes (sec
->owner
)
2264 + xcoff_section_data (sec
->owner
, sec
)->first_symndx
);
2265 hpend
= (obj_xcoff_sym_hashes (sec
->owner
)
2266 + xcoff_section_data (sec
->owner
, sec
)->last_symndx
);
2267 for (; hp
< hpend
; hp
++)
2269 struct xcoff_link_hash_entry
*h
;
2273 && (h
->flags
& XCOFF_MARK
) == 0)
2275 if (! xcoff_mark_symbol (info
, h
))
2280 /* Look through the section relocs. */
2281 if ((sec
->flags
& SEC_RELOC
) != 0
2282 && sec
->reloc_count
> 0)
2284 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, TRUE
,
2288 relend
= rel
+ sec
->reloc_count
;
2289 for (; rel
< relend
; rel
++)
2292 struct xcoff_link_hash_entry
*h
;
2294 if ((unsigned int) rel
->r_symndx
2295 > obj_raw_syment_count (sec
->owner
))
2298 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2300 && (h
->flags
& XCOFF_MARK
) == 0)
2302 if (! xcoff_mark_symbol (info
, h
))
2306 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2308 && (rsec
->flags
& SEC_MARK
) == 0)
2310 if (! xcoff_mark (info
, rsec
))
2314 /* See if this reloc needs to be copied into the .loader
2316 switch (rel
->r_type
)
2320 || h
->root
.type
== bfd_link_hash_defined
2321 || h
->root
.type
== bfd_link_hash_defweak
2322 || h
->root
.type
== bfd_link_hash_common
2323 || ((h
->flags
& XCOFF_CALLED
) != 0
2324 && (h
->root
.type
== bfd_link_hash_undefined
2325 || h
->root
.type
== bfd_link_hash_undefweak
)
2326 && h
->root
.root
.string
[0] == '.'
2327 && h
->descriptor
!= NULL
2328 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
2329 || ((h
->descriptor
->flags
& XCOFF_IMPORT
) != 0
2330 && (h
->descriptor
->flags
2331 & XCOFF_DEF_REGULAR
) == 0))))
2338 ++xcoff_hash_table (info
)->ldrel_count
;
2340 h
->flags
|= XCOFF_LDREL
;
2347 /* We should never need a .loader reloc for a TOC
2353 if (! info
->keep_memory
2354 && coff_section_data (sec
->owner
, sec
) != NULL
2355 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2356 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2358 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2359 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2367 /* Routines that are called after all the input files have been
2368 handled, but before the sections are laid out in memory. */
2370 /* The sweep phase of garbage collection. Remove all garbage
2374 xcoff_sweep (struct bfd_link_info
*info
)
2378 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2382 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2384 if ((o
->flags
& SEC_MARK
) == 0)
2386 /* Keep all sections from non-XCOFF input files. Keep
2387 special sections. Keep .debug sections for the
2389 if (sub
->xvec
!= info
->hash
->creator
2390 || o
== xcoff_hash_table (info
)->debug_section
2391 || o
== xcoff_hash_table (info
)->loader_section
2392 || o
== xcoff_hash_table (info
)->linkage_section
2393 || o
== xcoff_hash_table (info
)->toc_section
2394 || o
== xcoff_hash_table (info
)->descriptor_section
2395 || strcmp (o
->name
, ".debug") == 0)
2396 o
->flags
|= SEC_MARK
;
2401 o
->lineno_count
= 0;
2408 /* Record the number of elements in a set. This is used to output the
2409 correct csect length. */
2412 bfd_xcoff_link_record_set (bfd
*output_bfd
,
2413 struct bfd_link_info
*info
,
2414 struct bfd_link_hash_entry
*harg
,
2417 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2418 struct xcoff_link_size_list
*n
;
2421 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2424 /* This will hardly ever be called. I don't want to burn four bytes
2425 per global symbol, so instead the size is kept on a linked list
2426 attached to the hash table. */
2428 n
= bfd_alloc (output_bfd
, amt
);
2431 n
->next
= xcoff_hash_table (info
)->size_list
;
2434 xcoff_hash_table (info
)->size_list
= n
;
2436 h
->flags
|= XCOFF_HAS_SIZE
;
2441 /* Import a symbol. */
2444 bfd_xcoff_import_symbol (bfd
*output_bfd
,
2445 struct bfd_link_info
*info
,
2446 struct bfd_link_hash_entry
*harg
,
2448 const char *imppath
,
2449 const char *impfile
,
2450 const char *impmember
,
2451 unsigned int syscall_flag
)
2453 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2455 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2458 /* A symbol name which starts with a period is the code for a
2459 function. If the symbol is undefined, then add an undefined
2460 symbol for the function descriptor, and import that instead. */
2461 if (h
->root
.root
.string
[0] == '.'
2462 && h
->root
.type
== bfd_link_hash_undefined
2463 && val
== (bfd_vma
) -1)
2465 struct xcoff_link_hash_entry
*hds
;
2467 hds
= h
->descriptor
;
2470 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2471 h
->root
.root
.string
+ 1,
2475 if (hds
->root
.type
== bfd_link_hash_new
)
2477 hds
->root
.type
= bfd_link_hash_undefined
;
2478 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
2480 hds
->flags
|= XCOFF_DESCRIPTOR
;
2481 BFD_ASSERT ((hds
->flags
& XCOFF_CALLED
) == 0
2482 && (h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2483 hds
->descriptor
= h
;
2484 h
->descriptor
= hds
;
2487 /* Now, if the descriptor is undefined, import the descriptor
2488 rather than the symbol we were told to import. FIXME: Is
2489 this correct in all cases? */
2490 if (hds
->root
.type
== bfd_link_hash_undefined
)
2494 h
->flags
|= (XCOFF_IMPORT
| syscall_flag
);
2496 if (val
!= (bfd_vma
) -1)
2498 if (h
->root
.type
== bfd_link_hash_defined
2499 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
2500 || h
->root
.u
.def
.value
!= val
))
2502 if (! ((*info
->callbacks
->multiple_definition
)
2503 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
2504 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
2505 output_bfd
, bfd_abs_section_ptr
, val
)))
2509 h
->root
.type
= bfd_link_hash_defined
;
2510 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2511 h
->root
.u
.def
.value
= val
;
2514 /* We overload the ldindx field to hold the l_ifile value for this
2516 BFD_ASSERT (h
->ldsym
== NULL
);
2517 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
2518 if (imppath
== NULL
)
2523 struct xcoff_import_file
**pp
;
2525 /* We start c at 1 because the first entry in the import list is
2526 reserved for the library search path. */
2527 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
2529 pp
= &(*pp
)->next
, ++c
)
2531 if (strcmp ((*pp
)->path
, imppath
) == 0
2532 && strcmp ((*pp
)->file
, impfile
) == 0
2533 && strcmp ((*pp
)->member
, impmember
) == 0)
2539 struct xcoff_import_file
*n
;
2540 bfd_size_type amt
= sizeof (* n
);
2542 n
= bfd_alloc (output_bfd
, amt
);
2548 n
->member
= impmember
;
2558 /* Export a symbol. */
2561 bfd_xcoff_export_symbol (bfd
*output_bfd
,
2562 struct bfd_link_info
*info
,
2563 struct bfd_link_hash_entry
*harg
)
2565 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2567 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2570 h
->flags
|= XCOFF_EXPORT
;
2572 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2573 I'm just going to ignore it until somebody explains it. */
2575 /* See if this is a function descriptor. It may be one even though
2576 it is not so marked. */
2577 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2578 && h
->root
.root
.string
[0] != '.')
2581 struct xcoff_link_hash_entry
*hfn
;
2582 bfd_size_type amt
= strlen (h
->root
.root
.string
) + 2;
2584 fnname
= bfd_malloc (amt
);
2588 strcpy (fnname
+ 1, h
->root
.root
.string
);
2589 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2590 fnname
, FALSE
, FALSE
, TRUE
);
2593 && hfn
->smclas
== XMC_PR
2594 && (hfn
->root
.type
== bfd_link_hash_defined
2595 || hfn
->root
.type
== bfd_link_hash_defweak
))
2597 h
->flags
|= XCOFF_DESCRIPTOR
;
2598 h
->descriptor
= hfn
;
2599 hfn
->descriptor
= h
;
2603 /* Make sure we don't garbage collect this symbol. */
2604 if (! xcoff_mark_symbol (info
, h
))
2607 /* If this is a function descriptor, make sure we don't garbage
2608 collect the associated function code. We normally don't have to
2609 worry about this, because the descriptor will be attached to a
2610 section with relocs, but if we are creating the descriptor
2611 ourselves those relocs will not be visible to the mark code. */
2612 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
2614 if (! xcoff_mark_symbol (info
, h
->descriptor
))
2621 /* Count a reloc against a symbol. This is called for relocs
2622 generated by the linker script, typically for global constructors
2626 bfd_xcoff_link_count_reloc (bfd
*output_bfd
,
2627 struct bfd_link_info
*info
,
2630 struct xcoff_link_hash_entry
*h
;
2632 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2635 h
= ((struct xcoff_link_hash_entry
*)
2636 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, FALSE
, FALSE
,
2640 (*_bfd_error_handler
) (_("%s: no such symbol"), name
);
2641 bfd_set_error (bfd_error_no_symbols
);
2645 h
->flags
|= XCOFF_REF_REGULAR
| XCOFF_LDREL
;
2646 ++xcoff_hash_table (info
)->ldrel_count
;
2648 /* Mark the symbol to avoid garbage collection. */
2649 if (! xcoff_mark_symbol (info
, h
))
2655 /* This function is called for each symbol to which the linker script
2659 bfd_xcoff_record_link_assignment (bfd
*output_bfd
,
2660 struct bfd_link_info
*info
,
2663 struct xcoff_link_hash_entry
*h
;
2665 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2668 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, TRUE
, TRUE
,
2673 h
->flags
|= XCOFF_DEF_REGULAR
;
2678 /* Add a symbol to the .loader symbols, if necessary. */
2681 xcoff_build_ldsyms (struct xcoff_link_hash_entry
*h
, void * p
)
2683 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
2686 if (h
->root
.type
== bfd_link_hash_warning
)
2687 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
2689 /* __rtinit, this symbol has special handling. */
2690 if (h
->flags
& XCOFF_RTINIT
)
2693 /* If this is a final link, and the symbol was defined as a common
2694 symbol in a regular object file, and there was no definition in
2695 any dynamic object, then the linker will have allocated space for
2696 the symbol in a common section but the XCOFF_DEF_REGULAR flag
2697 will not have been set. */
2698 if (h
->root
.type
== bfd_link_hash_defined
2699 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2700 && (h
->flags
& XCOFF_REF_REGULAR
) != 0
2701 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
2702 && (bfd_is_abs_section (h
->root
.u
.def
.section
)
2703 || (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0))
2704 h
->flags
|= XCOFF_DEF_REGULAR
;
2706 /* If all defined symbols should be exported, mark them now. We
2707 don't want to export the actual functions, just the function
2709 if (ldinfo
->export_defineds
2710 && (h
->flags
& XCOFF_DEF_REGULAR
) != 0
2711 && h
->root
.root
.string
[0] != '.')
2715 /* We don't export a symbol which is being defined by an object
2716 included from an archive which contains a shared object. The
2717 rationale is that if an archive contains both an unshared and
2718 a shared object, then there must be some reason that the
2719 unshared object is unshared, and we don't want to start
2720 providing a shared version of it. In particular, this solves
2721 a bug involving the _savefNN set of functions. gcc will call
2722 those functions without providing a slot to restore the TOC,
2723 so it is essential that these functions be linked in directly
2724 and not from a shared object, which means that a shared
2725 object which also happens to link them in must not export
2726 them. This is confusing, but I haven't been able to think of
2727 a different approach. Note that the symbols can, of course,
2728 be exported explicitly. */
2730 if ((h
->root
.type
== bfd_link_hash_defined
2731 || h
->root
.type
== bfd_link_hash_defweak
)
2732 && h
->root
.u
.def
.section
->owner
!= NULL
2733 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
2735 bfd
*arbfd
, *member
;
2737 arbfd
= h
->root
.u
.def
.section
->owner
->my_archive
;
2738 member
= bfd_openr_next_archived_file (arbfd
, NULL
);
2739 while (member
!= NULL
)
2741 if ((member
->flags
& DYNAMIC
) != 0)
2746 member
= bfd_openr_next_archived_file (arbfd
, member
);
2751 h
->flags
|= XCOFF_EXPORT
;
2754 /* We don't want to garbage collect symbols which are not defined in
2755 XCOFF files. This is a convenient place to mark them. */
2756 if (xcoff_hash_table (ldinfo
->info
)->gc
2757 && (h
->flags
& XCOFF_MARK
) == 0
2758 && (h
->root
.type
== bfd_link_hash_defined
2759 || h
->root
.type
== bfd_link_hash_defweak
)
2760 && (h
->root
.u
.def
.section
->owner
== NULL
2761 || (h
->root
.u
.def
.section
->owner
->xvec
2762 != ldinfo
->info
->hash
->creator
)))
2763 h
->flags
|= XCOFF_MARK
;
2765 /* If this symbol is called and defined in a dynamic object, or it
2766 is imported, then we need to set up global linkage code for it.
2767 (Unless we did garbage collection and we didn't need this
2769 if ((h
->flags
& XCOFF_CALLED
) != 0
2770 && (h
->root
.type
== bfd_link_hash_undefined
2771 || h
->root
.type
== bfd_link_hash_undefweak
)
2772 && h
->root
.root
.string
[0] == '.'
2773 && h
->descriptor
!= NULL
2774 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
2775 || ((h
->descriptor
->flags
& XCOFF_IMPORT
) != 0
2776 && (h
->descriptor
->flags
& XCOFF_DEF_REGULAR
) == 0))
2777 && (! xcoff_hash_table (ldinfo
->info
)->gc
2778 || (h
->flags
& XCOFF_MARK
) != 0))
2781 struct xcoff_link_hash_entry
*hds
;
2783 sec
= xcoff_hash_table (ldinfo
->info
)->linkage_section
;
2784 h
->root
.type
= bfd_link_hash_defined
;
2785 h
->root
.u
.def
.section
= sec
;
2786 h
->root
.u
.def
.value
= sec
->size
;
2788 h
->flags
|= XCOFF_DEF_REGULAR
;
2789 sec
->size
+= bfd_xcoff_glink_code_size(ldinfo
->output_bfd
);
2791 /* The global linkage code requires a TOC entry for the
2793 hds
= h
->descriptor
;
2794 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2795 || hds
->root
.type
== bfd_link_hash_undefweak
)
2796 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
2797 hds
->flags
|= XCOFF_MARK
;
2798 if (hds
->toc_section
== NULL
)
2803 xcoff32 uses 4 bytes in the toc.
2804 xcoff64 uses 8 bytes in the toc. */
2805 if (bfd_xcoff_is_xcoff64 (ldinfo
->output_bfd
))
2807 else if (bfd_xcoff_is_xcoff32 (ldinfo
->output_bfd
))
2812 hds
->toc_section
= xcoff_hash_table (ldinfo
->info
)->toc_section
;
2813 hds
->u
.toc_offset
= hds
->toc_section
->size
;
2814 hds
->toc_section
->size
+= byte_size
;
2815 ++xcoff_hash_table (ldinfo
->info
)->ldrel_count
;
2816 ++hds
->toc_section
->reloc_count
;
2818 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2820 /* We need to call xcoff_build_ldsyms recursively here,
2821 because we may already have passed hds on the traversal. */
2822 xcoff_build_ldsyms (hds
, p
);
2826 /* If this symbol is exported, but not defined, we need to try to
2828 if ((h
->flags
& XCOFF_EXPORT
) != 0
2829 && (h
->flags
& XCOFF_IMPORT
) == 0
2830 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2831 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
2832 && (h
->root
.type
== bfd_link_hash_undefined
2833 || h
->root
.type
== bfd_link_hash_undefweak
))
2835 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
2836 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
2837 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
2841 /* This is an undefined function descriptor associated with
2842 a defined entry point. We can build up a function
2843 descriptor ourselves. Believe it or not, the AIX linker
2844 actually does this, and there are cases where we need to
2846 sec
= xcoff_hash_table (ldinfo
->info
)->descriptor_section
;
2847 h
->root
.type
= bfd_link_hash_defined
;
2848 h
->root
.u
.def
.section
= sec
;
2849 h
->root
.u
.def
.value
= sec
->size
;
2851 h
->flags
|= XCOFF_DEF_REGULAR
;
2853 /* The size of the function descriptor depends if this is an
2854 xcoff32 (12) or xcoff64 (24). */
2856 bfd_xcoff_function_descriptor_size(ldinfo
->output_bfd
);
2858 /* A function descriptor uses two relocs: one for the
2859 associated code, and one for the TOC address. */
2860 xcoff_hash_table (ldinfo
->info
)->ldrel_count
+= 2;
2861 sec
->reloc_count
+= 2;
2863 /* We handle writing out the contents of the descriptor in
2864 xcoff_write_global_symbol. */
2868 (*_bfd_error_handler
)
2869 (_("warning: attempt to export undefined symbol `%s'"),
2870 h
->root
.root
.string
);
2876 /* If this is still a common symbol, and it wasn't garbage
2877 collected, we need to actually allocate space for it in the .bss
2879 if (h
->root
.type
== bfd_link_hash_common
2880 && (! xcoff_hash_table (ldinfo
->info
)->gc
2881 || (h
->flags
& XCOFF_MARK
) != 0)
2882 && h
->root
.u
.c
.p
->section
->size
== 0)
2884 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
2885 h
->root
.u
.c
.p
->section
->size
= h
->root
.u
.c
.size
;
2888 /* We need to add a symbol to the .loader section if it is mentioned
2889 in a reloc which we are copying to the .loader section and it was
2890 not defined or common, or if it is the entry point, or if it is
2893 if (((h
->flags
& XCOFF_LDREL
) == 0
2894 || h
->root
.type
== bfd_link_hash_defined
2895 || h
->root
.type
== bfd_link_hash_defweak
2896 || h
->root
.type
== bfd_link_hash_common
)
2897 && (h
->flags
& XCOFF_ENTRY
) == 0
2898 && (h
->flags
& XCOFF_EXPORT
) == 0)
2904 /* We don't need to add this symbol if we did garbage collection and
2905 we did not mark this symbol. */
2906 if (xcoff_hash_table (ldinfo
->info
)->gc
2907 && (h
->flags
& XCOFF_MARK
) == 0)
2913 /* We may have already processed this symbol due to the recursive
2915 if ((h
->flags
& XCOFF_BUILT_LDSYM
) != 0)
2918 /* We need to add this symbol to the .loader symbols. */
2920 BFD_ASSERT (h
->ldsym
== NULL
);
2921 amt
= sizeof (struct internal_ldsym
);
2922 h
->ldsym
= bfd_zalloc (ldinfo
->output_bfd
, amt
);
2923 if (h
->ldsym
== NULL
)
2925 ldinfo
->failed
= TRUE
;
2929 if ((h
->flags
& XCOFF_IMPORT
) != 0)
2930 h
->ldsym
->l_ifile
= h
->ldindx
;
2932 /* The first 3 symbol table indices are reserved to indicate the
2933 data, text and bss sections. */
2934 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
2936 ++ldinfo
->ldsym_count
;
2938 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
->output_bfd
, ldinfo
,
2939 h
->ldsym
, h
->root
.root
.string
))
2942 h
->flags
|= XCOFF_BUILT_LDSYM
;
2946 /* Build the .loader section. This is called by the XCOFF linker
2947 emulation before_allocation routine. We must set the size of the
2948 .loader section before the linker lays out the output file.
2949 LIBPATH is the library path to search for shared objects; this is
2950 normally built from the -L arguments passed to the linker. ENTRY
2951 is the name of the entry point symbol (the -e linker option).
2952 FILE_ALIGN is the alignment to use for sections within the file
2953 (the -H linker option). MAXSTACK is the maximum stack size (the
2954 -bmaxstack linker option). MAXDATA is the maximum data size (the
2955 -bmaxdata linker option). GC is whether to do garbage collection
2956 (the -bgc linker option). MODTYPE is the module type (the
2957 -bmodtype linker option). TEXTRO is whether the text section must
2958 be read only (the -btextro linker option). EXPORT_DEFINEDS is
2959 whether all defined symbols should be exported (the -unix linker
2960 option). SPECIAL_SECTIONS is set by this routine to csects with
2961 magic names like _end. */
2964 bfd_xcoff_size_dynamic_sections (bfd
*output_bfd
,
2965 struct bfd_link_info
*info
,
2966 const char *libpath
,
2968 unsigned long file_align
,
2969 unsigned long maxstack
,
2970 unsigned long maxdata
,
2974 bfd_boolean export_defineds
,
2975 asection
**special_sections
,
2978 struct xcoff_link_hash_entry
*hentry
;
2980 struct xcoff_loader_info ldinfo
;
2982 size_t impsize
, impcount
;
2983 struct xcoff_import_file
*fl
;
2984 struct internal_ldhdr
*ldhdr
;
2985 bfd_size_type stoff
;
2989 struct bfd_strtab_hash
*debug_strtab
;
2990 bfd_byte
*debug_contents
= NULL
;
2993 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2995 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
2996 special_sections
[i
] = NULL
;
3000 ldinfo
.failed
= FALSE
;
3001 ldinfo
.output_bfd
= output_bfd
;
3003 ldinfo
.export_defineds
= export_defineds
;
3004 ldinfo
.ldsym_count
= 0;
3005 ldinfo
.string_size
= 0;
3006 ldinfo
.strings
= NULL
;
3007 ldinfo
.string_alc
= 0;
3009 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3010 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3011 xcoff_data (output_bfd
)->modtype
= modtype
;
3013 xcoff_hash_table (info
)->file_align
= file_align
;
3014 xcoff_hash_table (info
)->textro
= textro
;
3019 hentry
= xcoff_link_hash_lookup (xcoff_hash_table (info
), entry
,
3020 FALSE
, FALSE
, TRUE
);
3022 hentry
->flags
|= XCOFF_ENTRY
;
3026 if (info
->init_function
|| info
->fini_function
|| rtld
)
3028 struct xcoff_link_hash_entry
*hsym
;
3029 struct internal_ldsym
*ldsym
;
3031 hsym
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3032 "__rtinit", FALSE
, FALSE
, TRUE
);
3035 (*_bfd_error_handler
)
3036 (_("error: undefined symbol __rtinit"));
3040 xcoff_mark_symbol (info
, hsym
);
3041 hsym
->flags
|= (XCOFF_DEF_REGULAR
| XCOFF_RTINIT
);
3043 /* __rtinit initialized. */
3044 amt
= sizeof (* ldsym
);
3045 ldsym
= bfd_malloc (amt
);
3047 ldsym
->l_value
= 0; /* Will be filled in later. */
3048 ldsym
->l_scnum
= 2; /* Data section. */
3049 ldsym
->l_smtype
= XTY_SD
; /* Csect section definition. */
3050 ldsym
->l_smclas
= 5; /* .rw. */
3051 ldsym
->l_ifile
= 0; /* Special system loader symbol. */
3052 ldsym
->l_parm
= 0; /* NA. */
3054 /* Force __rtinit to be the first symbol in the loader symbol table
3055 See xcoff_build_ldsyms
3057 The first 3 symbol table indices are reserved to indicate the data,
3058 text and bss sections. */
3059 BFD_ASSERT (0 == ldinfo
.ldsym_count
);
3062 ldinfo
.ldsym_count
= 1;
3063 hsym
->ldsym
= ldsym
;
3065 if (! bfd_xcoff_put_ldsymbol_name (ldinfo
.output_bfd
, &ldinfo
,
3066 hsym
->ldsym
, hsym
->root
.root
.string
))
3069 /* This symbol is written out by xcoff_write_global_symbol
3070 Set stuff up so xcoff_write_global_symbol logic works. */
3071 hsym
->flags
|= XCOFF_DEF_REGULAR
| XCOFF_MARK
;
3072 hsym
->root
.type
= bfd_link_hash_defined
;
3073 hsym
->root
.u
.def
.value
= 0;
3076 /* Garbage collect unused sections. */
3077 if (info
->relocatable
3080 || (hentry
->root
.type
!= bfd_link_hash_defined
3081 && hentry
->root
.type
!= bfd_link_hash_defweak
))
3084 xcoff_hash_table (info
)->gc
= FALSE
;
3086 /* We still need to call xcoff_mark, in order to set ldrel_count
3088 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3092 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3094 if ((o
->flags
& SEC_MARK
) == 0)
3096 if (! xcoff_mark (info
, o
))
3104 if (! xcoff_mark (info
, hentry
->root
.u
.def
.section
))
3107 xcoff_hash_table (info
)->gc
= TRUE
;
3110 /* Return special sections to the caller. */
3111 for (i
= 0; i
< XCOFF_NUMBER_OF_SPECIAL_SECTIONS
; i
++)
3113 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3117 && (sec
->flags
& SEC_MARK
) == 0)
3120 special_sections
[i
] = sec
;
3123 if (info
->input_bfds
== NULL
)
3124 /* I'm not sure what to do in this bizarre case. */
3127 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_build_ldsyms
,
3132 /* Work out the size of the import file names. Each import file ID
3133 consists of three null terminated strings: the path, the file
3134 name, and the archive member name. The first entry in the list
3135 of names is the path to use to find objects, which the linker has
3136 passed in as the libpath argument. For some reason, the path
3137 entry in the other import file names appears to always be empty. */
3138 impsize
= strlen (libpath
) + 3;
3140 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
3143 impsize
+= (strlen (fl
->path
)
3145 + strlen (fl
->member
)
3149 /* Set up the .loader section header. */
3150 ldhdr
= &xcoff_hash_table (info
)->ldhdr
;
3151 ldhdr
->l_version
= bfd_xcoff_ldhdr_version(output_bfd
);
3152 ldhdr
->l_nsyms
= ldinfo
.ldsym_count
;
3153 ldhdr
->l_nreloc
= xcoff_hash_table (info
)->ldrel_count
;
3154 ldhdr
->l_istlen
= impsize
;
3155 ldhdr
->l_nimpid
= impcount
;
3156 ldhdr
->l_impoff
= (bfd_xcoff_ldhdrsz(output_bfd
)
3157 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz(output_bfd
)
3158 + ldhdr
->l_nreloc
* bfd_xcoff_ldrelsz(output_bfd
));
3159 ldhdr
->l_stlen
= ldinfo
.string_size
;
3160 stoff
= ldhdr
->l_impoff
+ impsize
;
3161 if (ldinfo
.string_size
== 0)
3164 ldhdr
->l_stoff
= stoff
;
3166 /* 64 bit elements to ldhdr
3167 The swap out routine for 32 bit will ignore them.
3168 Nothing fancy, symbols come after the header and relocs come
3170 ldhdr
->l_symoff
= bfd_xcoff_ldhdrsz (output_bfd
);
3171 ldhdr
->l_rldoff
= (bfd_xcoff_ldhdrsz (output_bfd
)
3172 + ldhdr
->l_nsyms
* bfd_xcoff_ldsymsz (output_bfd
));
3174 /* We now know the final size of the .loader section. Allocate
3176 lsec
= xcoff_hash_table (info
)->loader_section
;
3177 lsec
->size
= stoff
+ ldhdr
->l_stlen
;
3178 lsec
->contents
= bfd_zalloc (output_bfd
, lsec
->size
);
3179 if (lsec
->contents
== NULL
)
3182 /* Set up the header. */
3183 bfd_xcoff_swap_ldhdr_out (output_bfd
, ldhdr
, lsec
->contents
);
3185 /* Set up the import file names. */
3186 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3187 strcpy (out
, libpath
);
3188 out
+= strlen (libpath
) + 1;
3191 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
3196 while ((*out
++ = *s
++) != '\0')
3199 while ((*out
++ = *s
++) != '\0')
3202 while ((*out
++ = *s
++) != '\0')
3206 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3208 /* Set up the symbol string table. */
3209 if (ldinfo
.string_size
> 0)
3211 memcpy (out
, ldinfo
.strings
, ldinfo
.string_size
);
3212 free (ldinfo
.strings
);
3213 ldinfo
.strings
= NULL
;
3216 /* We can't set up the symbol table or the relocs yet, because we
3217 don't yet know the final position of the various sections. The
3218 .loader symbols are written out when the corresponding normal
3219 symbols are written out in xcoff_link_input_bfd or
3220 xcoff_write_global_symbol. The .loader relocs are written out
3221 when the corresponding normal relocs are handled in
3222 xcoff_link_input_bfd. */
3224 /* Allocate space for the magic sections. */
3225 sec
= xcoff_hash_table (info
)->linkage_section
;
3228 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3229 if (sec
->contents
== NULL
)
3232 sec
= xcoff_hash_table (info
)->toc_section
;
3235 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3236 if (sec
->contents
== NULL
)
3239 sec
= xcoff_hash_table (info
)->descriptor_section
;
3242 sec
->contents
= bfd_zalloc (output_bfd
, sec
->size
);
3243 if (sec
->contents
== NULL
)
3247 /* Now that we've done garbage collection, figure out the contents
3248 of the .debug section. */
3249 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3251 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3254 bfd_size_type symcount
;
3255 unsigned long *debug_index
;
3257 bfd_byte
*esym
, *esymend
;
3258 bfd_size_type symesz
;
3260 if (sub
->xvec
!= info
->hash
->creator
)
3262 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3263 if (subdeb
== NULL
|| subdeb
->size
== 0)
3266 if (info
->strip
== strip_all
3267 || info
->strip
== strip_debugger
3268 || info
->discard
== discard_all
)
3274 if (! _bfd_coff_get_external_symbols (sub
))
3277 symcount
= obj_raw_syment_count (sub
);
3278 debug_index
= bfd_zalloc (sub
, symcount
* sizeof (unsigned long));
3279 if (debug_index
== NULL
)
3281 xcoff_data (sub
)->debug_indices
= debug_index
;
3283 /* Grab the contents of the .debug section. We use malloc and
3284 copy the names into the debug stringtab, rather than
3285 bfd_alloc, because I expect that, when linking many files
3286 together, many of the strings will be the same. Storing the
3287 strings in the hash table should save space in this case. */
3288 if (! bfd_malloc_and_get_section (sub
, subdeb
, &debug_contents
))
3291 csectpp
= xcoff_data (sub
)->csects
;
3293 /* Dynamic object do not have csectpp's. */
3294 if (NULL
!= csectpp
)
3296 symesz
= bfd_coff_symesz (sub
);
3297 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3298 esymend
= esym
+ symcount
* symesz
;
3300 while (esym
< esymend
)
3302 struct internal_syment sym
;
3304 bfd_coff_swap_sym_in (sub
, (void *) esym
, (void *) &sym
);
3306 *debug_index
= (unsigned long) -1;
3308 if (sym
._n
._n_n
._n_zeroes
== 0
3311 || ((*csectpp
)->flags
& SEC_MARK
) != 0
3312 || *csectpp
== bfd_abs_section_ptr
)
3313 && bfd_coff_symname_in_debug (sub
, &sym
))
3318 name
= (char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3319 indx
= _bfd_stringtab_add (debug_strtab
, name
, TRUE
, TRUE
);
3320 if (indx
== (bfd_size_type
) -1)
3322 *debug_index
= indx
;
3325 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3326 csectpp
+= sym
.n_numaux
+ 1;
3327 debug_index
+= sym
.n_numaux
+ 1;
3331 free (debug_contents
);
3332 debug_contents
= NULL
;
3334 /* Clear the size of subdeb, so that it is not included directly
3335 in the output file. */
3338 if (! info
->keep_memory
)
3340 if (! _bfd_coff_free_symbols (sub
))
3345 if (info
->strip
!= strip_all
)
3346 xcoff_hash_table (info
)->debug_section
->size
=
3347 _bfd_stringtab_size (debug_strtab
);
3352 if (ldinfo
.strings
!= NULL
)
3353 free (ldinfo
.strings
);
3354 if (debug_contents
!= NULL
)
3355 free (debug_contents
);
3360 bfd_xcoff_link_generate_rtinit (bfd
*abfd
,
3365 struct bfd_in_memory
*bim
;
3367 bim
= bfd_malloc ((bfd_size_type
) sizeof (* bim
));
3374 abfd
->link_next
= 0;
3375 abfd
->format
= bfd_object
;
3376 abfd
->iostream
= (void *) bim
;
3377 abfd
->flags
= BFD_IN_MEMORY
;
3378 abfd
->direction
= write_direction
;
3381 if (! bfd_xcoff_generate_rtinit (abfd
, init
, fini
, rtld
))
3384 /* need to reset to unknown or it will not be read back in correctly */
3385 abfd
->format
= bfd_unknown
;
3386 abfd
->direction
= read_direction
;
3392 /* Link an input file into the linker output file. This function
3393 handles all the sections and relocations of the input file at once. */
3396 xcoff_link_input_bfd (struct xcoff_final_link_info
*finfo
,
3400 const char *strings
;
3401 bfd_size_type syment_base
;
3402 unsigned int n_tmask
;
3403 unsigned int n_btshft
;
3404 bfd_boolean copy
, hash
;
3405 bfd_size_type isymesz
;
3406 bfd_size_type osymesz
;
3407 bfd_size_type linesz
;
3410 struct xcoff_link_hash_entry
**sym_hash
;
3411 struct internal_syment
*isymp
;
3413 unsigned long *debug_index
;
3415 unsigned long output_index
;
3419 bfd_boolean keep_syms
;
3422 /* We can just skip DYNAMIC files, unless this is a static link. */
3423 if ((input_bfd
->flags
& DYNAMIC
) != 0
3424 && ! finfo
->info
->static_link
)
3427 /* Move all the symbols to the output file. */
3428 output_bfd
= finfo
->output_bfd
;
3430 syment_base
= obj_raw_syment_count (output_bfd
);
3431 isymesz
= bfd_coff_symesz (input_bfd
);
3432 osymesz
= bfd_coff_symesz (output_bfd
);
3433 linesz
= bfd_coff_linesz (input_bfd
);
3434 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
3436 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
3437 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
3439 /* Define macros so that ISFCN, et. al., macros work correctly. */
3440 #define N_TMASK n_tmask
3441 #define N_BTSHFT n_btshft
3444 if (! finfo
->info
->keep_memory
)
3447 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
3450 if (! _bfd_coff_get_external_symbols (input_bfd
))
3453 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
3454 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
3455 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
3456 csectpp
= xcoff_data (input_bfd
)->csects
;
3457 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
3458 isymp
= finfo
->internal_syms
;
3459 indexp
= finfo
->sym_indices
;
3460 output_index
= syment_base
;
3461 outsym
= finfo
->outsyms
;
3465 while (esym
< esym_end
)
3467 struct internal_syment isym
;
3468 union internal_auxent aux
;
3471 bfd_boolean require
;
3474 bfd_coff_swap_sym_in (input_bfd
, (void *) esym
, (void *) isymp
);
3476 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3478 if (isymp
->n_sclass
== C_EXT
|| isymp
->n_sclass
== C_HIDEXT
)
3480 BFD_ASSERT (isymp
->n_numaux
> 0);
3481 bfd_coff_swap_aux_in (input_bfd
,
3482 (void *) (esym
+ isymesz
* isymp
->n_numaux
),
3483 isymp
->n_type
, isymp
->n_sclass
,
3484 isymp
->n_numaux
- 1, isymp
->n_numaux
,
3487 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
3490 /* Make a copy of *isymp so that the relocate_section function
3491 always sees the original values. This is more reliable than
3492 always recomputing the symbol value even if we are stripping
3496 /* If this symbol is in the .loader section, swap out the
3497 .loader symbol information. If this is an external symbol
3498 reference to a defined symbol, though, then wait until we get
3499 to the definition. */
3500 if (isym
.n_sclass
== C_EXT
3501 && *sym_hash
!= NULL
3502 && (*sym_hash
)->ldsym
!= NULL
3504 || (*sym_hash
)->root
.type
== bfd_link_hash_undefined
))
3506 struct xcoff_link_hash_entry
*h
;
3507 struct internal_ldsym
*ldsym
;
3511 if (isym
.n_scnum
> 0)
3513 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
3514 ldsym
->l_value
= (isym
.n_value
3515 + (*csectpp
)->output_section
->vma
3516 + (*csectpp
)->output_offset
3521 ldsym
->l_scnum
= isym
.n_scnum
;
3522 ldsym
->l_value
= isym
.n_value
;
3525 ldsym
->l_smtype
= smtyp
;
3526 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
3527 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
3528 || (h
->flags
& XCOFF_IMPORT
) != 0)
3529 ldsym
->l_smtype
|= L_IMPORT
;
3530 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
3531 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
3532 || (h
->flags
& XCOFF_EXPORT
) != 0)
3533 ldsym
->l_smtype
|= L_EXPORT
;
3534 if ((h
->flags
& XCOFF_ENTRY
) != 0)
3535 ldsym
->l_smtype
|= L_ENTRY
;
3537 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
3539 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
3541 else if (ldsym
->l_ifile
== 0)
3543 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
3549 if (h
->root
.type
== bfd_link_hash_defined
3550 || h
->root
.type
== bfd_link_hash_defweak
)
3551 impbfd
= h
->root
.u
.def
.section
->owner
;
3552 else if (h
->root
.type
== bfd_link_hash_undefined
3553 || h
->root
.type
== bfd_link_hash_undefweak
)
3554 impbfd
= h
->root
.u
.undef
.abfd
;
3562 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
3563 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
3570 BFD_ASSERT (h
->ldindx
>= 0);
3571 bfd_xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
3574 * bfd_xcoff_ldsymsz (finfo
->output_bfd
))));
3577 /* Fill in snentry now that we know the target_index. */
3578 if ((h
->flags
& XCOFF_ENTRY
) != 0
3579 && (h
->root
.type
== bfd_link_hash_defined
3580 || h
->root
.type
== bfd_link_hash_defweak
))
3582 xcoff_data (output_bfd
)->snentry
=
3583 h
->root
.u
.def
.section
->output_section
->target_index
;
3591 add
= 1 + isym
.n_numaux
;
3593 /* If we are skipping this csect, we want to skip this symbol. */
3594 if (*csectpp
== NULL
)
3597 /* If we garbage collected this csect, we want to skip this
3600 && xcoff_hash_table (finfo
->info
)->gc
3601 && ((*csectpp
)->flags
& SEC_MARK
) == 0
3602 && *csectpp
!= bfd_abs_section_ptr
)
3605 /* An XCOFF linker always skips C_STAT symbols. */
3607 && isymp
->n_sclass
== C_STAT
)
3610 /* We skip all but the first TOC anchor. */
3612 && isymp
->n_sclass
== C_HIDEXT
3613 && aux
.x_csect
.x_smclas
== XMC_TC0
)
3615 if (finfo
->toc_symindx
!= -1)
3619 bfd_vma tocval
, tocend
;
3622 tocval
= ((*csectpp
)->output_section
->vma
3623 + (*csectpp
)->output_offset
3627 /* We want to find out if tocval is a good value to use
3628 as the TOC anchor--that is, whether we can access all
3629 of the TOC using a 16 bit offset from tocval. This
3630 test assumes that the TOC comes at the end of the
3631 output section, as it does in the default linker
3633 tocend
= ((*csectpp
)->output_section
->vma
3634 + (*csectpp
)->output_section
->size
);
3635 for (inp
= finfo
->info
->input_bfds
;
3637 inp
= inp
->link_next
)
3640 for (o
= inp
->sections
; o
!= NULL
; o
= o
->next
)
3641 if (strcmp (o
->name
, ".tocbss") == 0)
3643 bfd_vma new_toc_end
;
3644 new_toc_end
= (o
->output_section
->vma
3647 if (new_toc_end
> tocend
)
3648 tocend
= new_toc_end
;
3653 if (tocval
+ 0x10000 < tocend
)
3655 (*_bfd_error_handler
)
3656 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
3657 (unsigned long) (tocend
- tocval
));
3658 bfd_set_error (bfd_error_file_too_big
);
3662 if (tocval
+ 0x8000 < tocend
)
3666 tocadd
= tocend
- (tocval
+ 0x8000);
3668 isym
.n_value
+= tocadd
;
3671 finfo
->toc_symindx
= output_index
;
3672 xcoff_data (finfo
->output_bfd
)->toc
= tocval
;
3673 xcoff_data (finfo
->output_bfd
)->sntoc
=
3674 (*csectpp
)->output_section
->target_index
;
3680 /* If we are stripping all symbols, we want to skip this one. */
3682 && finfo
->info
->strip
== strip_all
)
3685 /* We can skip resolved external references. */
3687 && isym
.n_sclass
== C_EXT
3689 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
)
3692 /* We can skip common symbols if they got defined somewhere
3695 && isym
.n_sclass
== C_EXT
3697 && ((*sym_hash
)->root
.type
!= bfd_link_hash_common
3698 || (*sym_hash
)->root
.u
.c
.p
->section
!= *csectpp
)
3699 && ((*sym_hash
)->root
.type
!= bfd_link_hash_defined
3700 || (*sym_hash
)->root
.u
.def
.section
!= *csectpp
))
3703 /* Skip local symbols if we are discarding them. */
3705 && finfo
->info
->discard
== discard_all
3706 && isym
.n_sclass
!= C_EXT
3707 && (isym
.n_sclass
!= C_HIDEXT
3708 || smtyp
!= XTY_SD
))
3711 /* If we stripping debugging symbols, and this is a debugging
3712 symbol, then skip it. */
3714 && finfo
->info
->strip
== strip_debugger
3715 && isym
.n_scnum
== N_DEBUG
)
3718 /* If some symbols are stripped based on the name, work out the
3719 name and decide whether to skip this symbol. We don't handle
3720 this correctly for symbols whose names are in the .debug
3721 section; to get it right we would need a new bfd_strtab_hash
3722 function to return the string given the index. */
3724 && (finfo
->info
->strip
== strip_some
3725 || finfo
->info
->discard
== discard_l
)
3726 && (debug_index
== NULL
|| *debug_index
== (unsigned long) -1))
3729 char buf
[SYMNMLEN
+ 1];
3731 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
3736 if ((finfo
->info
->strip
== strip_some
3737 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
,
3739 || (finfo
->info
->discard
== discard_l
3740 && (isym
.n_sclass
!= C_EXT
3741 && (isym
.n_sclass
!= C_HIDEXT
3742 || smtyp
!= XTY_SD
))
3743 && bfd_is_local_label_name (input_bfd
, name
)))
3747 /* We can not skip the first TOC anchor. */
3750 && finfo
->info
->strip
!= strip_all
)
3753 /* We now know whether we are to skip this symbol or not. */
3756 /* Adjust the symbol in order to output it. */
3758 if (isym
._n
._n_n
._n_zeroes
== 0
3759 && isym
._n
._n_n
._n_offset
!= 0)
3761 /* This symbol has a long name. Enter it in the string
3762 table we are building. If *debug_index != -1, the
3763 name has already been entered in the .debug section. */
3764 if (debug_index
!= NULL
&& *debug_index
!= (unsigned long) -1)
3765 isym
._n
._n_n
._n_offset
= *debug_index
;
3771 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
3775 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
3776 if (indx
== (bfd_size_type
) -1)
3778 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
3782 if (isym
.n_sclass
!= C_BSTAT
3783 && isym
.n_sclass
!= C_ESTAT
3784 && isym
.n_sclass
!= C_DECL
3785 && isym
.n_scnum
> 0)
3787 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
3788 isym
.n_value
+= ((*csectpp
)->output_section
->vma
3789 + (*csectpp
)->output_offset
3793 /* The value of a C_FILE symbol is the symbol index of the
3794 next C_FILE symbol. The value of the last C_FILE symbol
3795 is -1. We try to get this right, below, just before we
3796 write the symbols out, but in the general case we may
3797 have to write the symbol out twice. */
3798 if (isym
.n_sclass
== C_FILE
)
3800 if (finfo
->last_file_index
!= -1
3801 && finfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
3803 /* We must correct the value of the last C_FILE entry. */
3804 finfo
->last_file
.n_value
= output_index
;
3805 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
3807 /* The last C_FILE symbol is in this input file. */
3808 bfd_coff_swap_sym_out (output_bfd
,
3809 (void *) &finfo
->last_file
,
3810 (void *) (finfo
->outsyms
3811 + ((finfo
->last_file_index
3817 /* We have already written out the last C_FILE
3818 symbol. We need to write it out again. We
3819 borrow *outsym temporarily. */
3822 bfd_coff_swap_sym_out (output_bfd
,
3823 (void *) &finfo
->last_file
,
3826 pos
= obj_sym_filepos (output_bfd
);
3827 pos
+= finfo
->last_file_index
* osymesz
;
3828 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
3829 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
3835 finfo
->last_file_index
= output_index
;
3836 finfo
->last_file
= isym
;
3839 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3840 into the line numbers. We update the symbol values when
3841 we handle the line numbers. */
3842 if (isym
.n_sclass
== C_BINCL
3843 || isym
.n_sclass
== C_EINCL
)
3845 isym
.n_value
= finfo
->line_filepos
;
3849 /* Output the symbol. */
3851 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
3853 *indexp
= output_index
;
3855 if (isym
.n_sclass
== C_EXT
)
3858 struct xcoff_link_hash_entry
*h
;
3860 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
3862 h
= obj_xcoff_sym_hashes (input_bfd
)[indx
];
3863 BFD_ASSERT (h
!= NULL
);
3864 h
->indx
= output_index
;
3867 /* If this is a symbol in the TOC which we may have merged
3868 (class XMC_TC), remember the symbol index of the TOC
3870 if (isym
.n_sclass
== C_HIDEXT
3871 && aux
.x_csect
.x_smclas
== XMC_TC
3872 && *sym_hash
!= NULL
)
3874 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
3875 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
3876 (*sym_hash
)->u
.toc_indx
= output_index
;
3879 output_index
+= add
;
3880 outsym
+= add
* osymesz
;
3883 esym
+= add
* isymesz
;
3887 if (debug_index
!= NULL
)
3890 for (--add
; add
> 0; --add
)
3894 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3895 done in a separate pass, because we don't know the correct symbol
3896 indices until we have already decided which symbols we are going
3899 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
3900 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
3901 isymp
= finfo
->internal_syms
;
3902 indexp
= finfo
->sym_indices
;
3903 csectpp
= xcoff_data (input_bfd
)->csects
;
3904 outsym
= finfo
->outsyms
;
3905 while (esym
< esym_end
)
3909 add
= 1 + isymp
->n_numaux
;
3912 esym
+= add
* isymesz
;
3917 if (isymp
->n_sclass
== C_BSTAT
)
3919 struct internal_syment isym
;
3923 /* The value of a C_BSTAT symbol is the symbol table
3924 index of the containing csect. */
3925 bfd_coff_swap_sym_in (output_bfd
, (void *) outsym
, (void *) &isym
);
3926 indx
= isym
.n_value
;
3927 if (indx
< obj_raw_syment_count (input_bfd
))
3931 symindx
= finfo
->sym_indices
[indx
];
3935 isym
.n_value
= symindx
;
3936 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
,
3944 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
3946 union internal_auxent aux
;
3948 bfd_coff_swap_aux_in (input_bfd
, (void *) esym
, isymp
->n_type
,
3949 isymp
->n_sclass
, i
, isymp
->n_numaux
,
3952 if (isymp
->n_sclass
== C_FILE
)
3954 /* This is the file name (or some comment put in by
3955 the compiler). If it is long, we must put it in
3956 the string table. */
3957 if (aux
.x_file
.x_n
.x_zeroes
== 0
3958 && aux
.x_file
.x_n
.x_offset
!= 0)
3960 const char *filename
;
3963 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
3964 >= STRING_SIZE_SIZE
);
3965 if (strings
== NULL
)
3967 strings
= _bfd_coff_read_string_table (input_bfd
);
3968 if (strings
== NULL
)
3971 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
3972 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
3974 if (indx
== (bfd_size_type
) -1)
3976 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
3979 else if ((isymp
->n_sclass
== C_EXT
3980 || isymp
->n_sclass
== C_HIDEXT
)
3981 && i
+ 1 == isymp
->n_numaux
)
3984 /* We don't support type checking. I don't know if
3986 aux
.x_csect
.x_parmhash
= 0;
3987 /* I don't think anybody uses these fields, but we'd
3988 better clobber them just in case. */
3989 aux
.x_csect
.x_stab
= 0;
3990 aux
.x_csect
.x_snstab
= 0;
3992 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
3996 indx
= aux
.x_csect
.x_scnlen
.l
;
3997 if (indx
< obj_raw_syment_count (input_bfd
))
4001 symindx
= finfo
->sym_indices
[indx
];
4004 aux
.x_csect
.x_scnlen
.l
= 0;
4008 aux
.x_csect
.x_scnlen
.l
= symindx
;
4013 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4017 if (ISFCN (isymp
->n_type
)
4018 || ISTAG (isymp
->n_sclass
)
4019 || isymp
->n_sclass
== C_BLOCK
4020 || isymp
->n_sclass
== C_FCN
)
4022 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4024 && indx
< obj_raw_syment_count (input_bfd
))
4026 /* We look forward through the symbol for
4027 the index of the next symbol we are going
4028 to include. I don't know if this is
4030 while (finfo
->sym_indices
[indx
] < 0
4031 && indx
< obj_raw_syment_count (input_bfd
))
4033 if (indx
>= obj_raw_syment_count (input_bfd
))
4034 indx
= output_index
;
4036 indx
= finfo
->sym_indices
[indx
];
4037 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4042 indx
= aux
.x_sym
.x_tagndx
.l
;
4043 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4047 symindx
= finfo
->sym_indices
[indx
];
4049 aux
.x_sym
.x_tagndx
.l
= 0;
4051 aux
.x_sym
.x_tagndx
.l
= symindx
;
4056 /* Copy over the line numbers, unless we are stripping
4057 them. We do this on a symbol by symbol basis in
4058 order to more easily handle garbage collection. */
4059 if ((isymp
->n_sclass
== C_EXT
4060 || isymp
->n_sclass
== C_HIDEXT
)
4062 && isymp
->n_numaux
> 1
4063 && ISFCN (isymp
->n_type
)
4064 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4066 if (finfo
->info
->strip
!= strip_none
4067 && finfo
->info
->strip
!= strip_some
)
4068 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4071 asection
*enclosing
;
4072 unsigned int enc_count
;
4073 bfd_signed_vma linoff
;
4074 struct internal_lineno lin
;
4077 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4078 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4079 if (oline
!= enclosing
)
4081 file_ptr pos
= enclosing
->line_filepos
;
4082 bfd_size_type amt
= linesz
* enc_count
;
4083 if (bfd_seek (input_bfd
, pos
, SEEK_SET
) != 0
4084 || (bfd_bread (finfo
->linenos
, amt
, input_bfd
)
4090 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4091 - enclosing
->line_filepos
);
4093 bfd_coff_swap_lineno_in (input_bfd
,
4094 (void *) (finfo
->linenos
+ linoff
),
4097 || ((bfd_size_type
) lin
.l_addr
.l_symndx
4101 obj_coff_external_syms (input_bfd
)))
4103 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4106 bfd_byte
*linpend
, *linp
;
4108 bfd_size_type count
;
4110 lin
.l_addr
.l_symndx
= *indexp
;
4111 bfd_coff_swap_lineno_out (output_bfd
, (void *) &lin
,
4112 (void *) (finfo
->linenos
4115 linpend
= (finfo
->linenos
4116 + enc_count
* linesz
);
4117 offset
= (o
->output_section
->vma
4120 for (linp
= finfo
->linenos
+ linoff
+ linesz
;
4124 bfd_coff_swap_lineno_in (input_bfd
, (void *) linp
,
4126 if (lin
.l_lnno
== 0)
4128 lin
.l_addr
.l_paddr
+= offset
;
4129 bfd_coff_swap_lineno_out (output_bfd
,
4134 count
= (linp
- (finfo
->linenos
+ linoff
)) / linesz
;
4136 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
4137 (o
->output_section
->line_filepos
4138 + o
->output_section
->lineno_count
* linesz
);
4140 if (bfd_seek (output_bfd
,
4141 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
,
4143 || (bfd_bwrite (finfo
->linenos
+ linoff
,
4144 linesz
* count
, output_bfd
)
4148 o
->output_section
->lineno_count
+= count
;
4152 struct internal_syment
*iisp
, *iispend
;
4157 /* Update any C_BINCL or C_EINCL symbols
4158 that refer to a line number in the
4159 range we just output. */
4160 iisp
= finfo
->internal_syms
;
4162 + obj_raw_syment_count (input_bfd
));
4163 iindp
= finfo
->sym_indices
;
4164 oos
= finfo
->outsyms
;
4165 while (iisp
< iispend
)
4168 && (iisp
->n_sclass
== C_BINCL
4169 || iisp
->n_sclass
== C_EINCL
)
4170 && ((bfd_size_type
) iisp
->n_value
4171 >= (bfd_size_type
)(enclosing
->line_filepos
+ linoff
))
4172 && ((bfd_size_type
) iisp
->n_value
4173 < (enclosing
->line_filepos
4174 + enc_count
* linesz
)))
4176 struct internal_syment iis
;
4178 bfd_coff_swap_sym_in (output_bfd
,
4183 - enclosing
->line_filepos
4185 + aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
);
4186 bfd_coff_swap_sym_out (output_bfd
,
4192 iiadd
= 1 + iisp
->n_numaux
;
4194 oos
+= iiadd
* osymesz
;
4203 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, isymp
->n_type
,
4204 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4216 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4217 symbol will be the first symbol in the next input file. In the
4218 normal case, this will save us from writing out the C_FILE symbol
4220 if (finfo
->last_file_index
!= -1
4221 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4223 finfo
->last_file
.n_value
= output_index
;
4224 bfd_coff_swap_sym_out (output_bfd
, (void *) &finfo
->last_file
,
4225 (void *) (finfo
->outsyms
4226 + ((finfo
->last_file_index
- syment_base
)
4230 /* Write the modified symbols to the output file. */
4231 if (outsym
> finfo
->outsyms
)
4233 file_ptr pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
4234 bfd_size_type amt
= outsym
- finfo
->outsyms
;
4235 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4236 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
4239 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4240 + (outsym
- finfo
->outsyms
) / osymesz
)
4243 obj_raw_syment_count (output_bfd
) = output_index
;
4246 /* Don't let the linker relocation routines discard the symbols. */
4247 keep_syms
= obj_coff_keep_syms (input_bfd
);
4248 obj_coff_keep_syms (input_bfd
) = TRUE
;
4250 /* Relocate the contents of each section. */
4251 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4255 if (! o
->linker_mark
)
4256 /* This section was omitted from the link. */
4259 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4261 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4264 /* We have set filepos correctly for the sections we created to
4265 represent csects, so bfd_get_section_contents should work. */
4266 if (coff_section_data (input_bfd
, o
) != NULL
4267 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4268 contents
= coff_section_data (input_bfd
, o
)->contents
;
4271 bfd_size_type sz
= o
->rawsize
? o
->rawsize
: o
->size
;
4272 if (!bfd_get_section_contents (input_bfd
, o
, finfo
->contents
, 0, sz
))
4274 contents
= finfo
->contents
;
4277 if ((o
->flags
& SEC_RELOC
) != 0)
4280 struct internal_reloc
*internal_relocs
;
4281 struct internal_reloc
*irel
;
4283 struct internal_reloc
*irelend
;
4284 struct xcoff_link_hash_entry
**rel_hash
;
4287 /* Read in the relocs. */
4288 target_index
= o
->output_section
->target_index
;
4289 internal_relocs
= (xcoff_read_internal_relocs
4290 (input_bfd
, o
, FALSE
, finfo
->external_relocs
,
4292 (finfo
->section_info
[target_index
].relocs
4293 + o
->output_section
->reloc_count
)));
4294 if (internal_relocs
== NULL
)
4297 /* Call processor specific code to relocate the section
4299 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
4303 finfo
->internal_syms
,
4304 xcoff_data (input_bfd
)->csects
))
4307 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4308 irel
= internal_relocs
;
4309 irelend
= irel
+ o
->reloc_count
;
4310 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
4311 + o
->output_section
->reloc_count
);
4312 for (; irel
< irelend
; irel
++, rel_hash
++)
4314 struct xcoff_link_hash_entry
*h
= NULL
;
4315 struct internal_ldrel ldrel
;
4320 /* Adjust the reloc address and symbol index. */
4322 irel
->r_vaddr
+= offset
;
4324 r_symndx
= irel
->r_symndx
;
4329 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4331 if (r_symndx
!= -1 && finfo
->info
->strip
!= strip_all
)
4334 && h
->smclas
!= XMC_TD
4335 && (irel
->r_type
== R_TOC
4336 || irel
->r_type
== R_GL
4337 || irel
->r_type
== R_TCL
4338 || irel
->r_type
== R_TRL
4339 || irel
->r_type
== R_TRLA
))
4341 /* This is a TOC relative reloc with a symbol
4342 attached. The symbol should be the one which
4343 this reloc is for. We want to make this
4344 reloc against the TOC address of the symbol,
4345 not the symbol itself. */
4346 BFD_ASSERT (h
->toc_section
!= NULL
);
4347 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4348 if (h
->u
.toc_indx
!= -1)
4349 irel
->r_symndx
= h
->u
.toc_indx
;
4352 struct xcoff_toc_rel_hash
*n
;
4353 struct xcoff_link_section_info
*si
;
4357 n
= bfd_alloc (finfo
->output_bfd
, amt
);
4360 si
= finfo
->section_info
+ target_index
;
4361 n
->next
= si
->toc_rel_hashes
;
4364 si
->toc_rel_hashes
= n
;
4369 /* This is a global symbol. */
4371 irel
->r_symndx
= h
->indx
;
4374 /* This symbol is being written at the end
4375 of the file, and we do not yet know the
4376 symbol index. We save the pointer to the
4377 hash table entry in the rel_hash list.
4378 We set the indx field to -2 to indicate
4379 that this symbol must not be stripped. */
4388 indx
= finfo
->sym_indices
[r_symndx
];
4392 struct internal_syment
*is
;
4394 /* Relocations against a TC0 TOC anchor are
4395 automatically transformed to be against
4396 the TOC anchor in the output file. */
4397 is
= finfo
->internal_syms
+ r_symndx
;
4398 if (is
->n_sclass
== C_HIDEXT
4399 && is
->n_numaux
> 0)
4402 union internal_auxent aux
;
4406 obj_coff_external_syms (input_bfd
))
4407 + ((r_symndx
+ is
->n_numaux
)
4409 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4410 is
->n_type
, is
->n_sclass
,
4414 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4415 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4416 indx
= finfo
->toc_symindx
;
4421 irel
->r_symndx
= indx
;
4425 struct internal_syment
*is
;
4428 char buf
[SYMNMLEN
+ 1];
4430 /* This reloc is against a symbol we are
4431 stripping. It would be possible to handle
4432 this case, but I don't think it's worth it. */
4433 is
= finfo
->internal_syms
+ r_symndx
;
4435 name
= (_bfd_coff_internal_syment_name
4436 (input_bfd
, is
, buf
));
4441 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4442 (finfo
->info
, name
, input_bfd
, o
,
4450 switch (irel
->r_type
)
4454 || h
->root
.type
== bfd_link_hash_defined
4455 || h
->root
.type
== bfd_link_hash_defweak
4456 || h
->root
.type
== bfd_link_hash_common
)
4463 /* This reloc needs to be copied into the .loader
4465 ldrel
.l_vaddr
= irel
->r_vaddr
;
4467 ldrel
.l_symndx
= -(bfd_size_type
) 1;
4469 || (h
->root
.type
== bfd_link_hash_defined
4470 || h
->root
.type
== bfd_link_hash_defweak
4471 || h
->root
.type
== bfd_link_hash_common
))
4476 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4477 else if (h
->root
.type
== bfd_link_hash_common
)
4478 sec
= h
->root
.u
.c
.p
->section
;
4480 sec
= h
->root
.u
.def
.section
;
4481 sec
= sec
->output_section
;
4483 if (strcmp (sec
->name
, ".text") == 0)
4485 else if (strcmp (sec
->name
, ".data") == 0)
4487 else if (strcmp (sec
->name
, ".bss") == 0)
4491 (*_bfd_error_handler
)
4492 (_("%B: loader reloc in unrecognized section `%A'"),
4494 bfd_set_error (bfd_error_nonrepresentable_section
);
4500 if (! finfo
->info
->relocatable
4501 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
4502 && (h
->flags
& XCOFF_IMPORT
) == 0)
4504 /* We already called the undefined_symbol
4505 callback for this relocation, in
4506 _bfd_ppc_xcoff_relocate_section. Don't
4507 issue any more warnings. */
4510 if (h
->ldindx
< 0 && ! quiet
)
4512 (*_bfd_error_handler
)
4513 (_("%B: `%s' in loader reloc but not loader sym"),
4515 h
->root
.root
.string
);
4516 bfd_set_error (bfd_error_bad_value
);
4519 ldrel
.l_symndx
= h
->ldindx
;
4521 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4522 ldrel
.l_rsecnm
= o
->output_section
->target_index
;
4523 if (xcoff_hash_table (finfo
->info
)->textro
4524 && strcmp (o
->output_section
->name
, ".text") == 0
4527 (*_bfd_error_handler
)
4528 (_("%B: loader reloc in read-only section %A"),
4529 input_bfd
, o
->output_section
);
4530 bfd_set_error (bfd_error_invalid_operation
);
4533 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
,
4536 finfo
->ldrel
+= bfd_xcoff_ldrelsz(output_bfd
);
4544 /* We should never need a .loader reloc for a TOC
4550 o
->output_section
->reloc_count
+= o
->reloc_count
;
4553 /* Write out the modified section contents. */
4554 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4555 contents
, (file_ptr
) o
->output_offset
,
4560 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4562 if (! finfo
->info
->keep_memory
)
4564 if (! _bfd_coff_free_symbols (input_bfd
))
4574 /* Sort relocs by VMA. This is called via qsort. */
4577 xcoff_sort_relocs (const void * p1
, const void * p2
)
4579 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
4580 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
4582 if (r1
->r_vaddr
> r2
->r_vaddr
)
4584 else if (r1
->r_vaddr
< r2
->r_vaddr
)
4590 /* Write out a non-XCOFF global symbol. */
4593 xcoff_write_global_symbol (struct xcoff_link_hash_entry
*h
, void * inf
)
4595 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) inf
;
4598 struct internal_syment isym
;
4599 union internal_auxent aux
;
4604 output_bfd
= finfo
->output_bfd
;
4605 outsym
= finfo
->outsyms
;
4607 if (h
->root
.type
== bfd_link_hash_warning
)
4609 h
= (struct xcoff_link_hash_entry
*) h
->root
.u
.i
.link
;
4610 if (h
->root
.type
== bfd_link_hash_new
)
4614 /* If this symbol was garbage collected, just skip it. */
4615 if (xcoff_hash_table (finfo
->info
)->gc
4616 && (h
->flags
& XCOFF_MARK
) == 0)
4619 /* If we need a .loader section entry, write it out. */
4620 if (h
->ldsym
!= NULL
)
4622 struct internal_ldsym
*ldsym
;
4627 if (h
->root
.type
== bfd_link_hash_undefined
4628 || h
->root
.type
== bfd_link_hash_undefweak
)
4632 ldsym
->l_scnum
= N_UNDEF
;
4633 ldsym
->l_smtype
= XTY_ER
;
4634 impbfd
= h
->root
.u
.undef
.abfd
;
4637 else if (h
->root
.type
== bfd_link_hash_defined
4638 || h
->root
.type
== bfd_link_hash_defweak
)
4642 sec
= h
->root
.u
.def
.section
;
4643 ldsym
->l_value
= (sec
->output_section
->vma
4644 + sec
->output_offset
4645 + h
->root
.u
.def
.value
);
4646 ldsym
->l_scnum
= sec
->output_section
->target_index
;
4647 ldsym
->l_smtype
= XTY_SD
;
4648 impbfd
= sec
->owner
;
4654 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4655 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4656 || (h
->flags
& XCOFF_IMPORT
) != 0)
4658 Import symbols are defined so the check above will make
4659 the l_smtype XTY_SD. But this is not correct, it should
4661 ldsym
->l_smtype
|= L_IMPORT
;
4663 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4664 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4665 || (h
->flags
& XCOFF_EXPORT
) != 0)
4666 ldsym
->l_smtype
|= L_EXPORT
;
4668 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4669 ldsym
->l_smtype
|= L_ENTRY
;
4671 if ((h
->flags
& XCOFF_RTINIT
) != 0)
4672 ldsym
->l_smtype
= XTY_SD
;
4674 ldsym
->l_smclas
= h
->smclas
;
4676 if (ldsym
->l_smtype
& L_IMPORT
)
4678 if ((h
->root
.type
== bfd_link_hash_defined
4679 || h
->root
.type
== bfd_link_hash_defweak
)
4680 && (h
->root
.u
.def
.value
!= 0))
4681 ldsym
->l_smclas
= XMC_XO
;
4683 else if ((h
->flags
& (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
)) ==
4684 (XCOFF_SYSCALL32
| XCOFF_SYSCALL64
))
4685 ldsym
->l_smclas
= XMC_SV3264
;
4687 else if (h
->flags
& XCOFF_SYSCALL32
)
4688 ldsym
->l_smclas
= XMC_SV
;
4690 else if (h
->flags
& XCOFF_SYSCALL64
)
4691 ldsym
->l_smclas
= XMC_SV64
;
4694 if (ldsym
->l_ifile
== -(bfd_size_type
) 1)
4698 else if (ldsym
->l_ifile
== 0)
4700 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4702 else if (impbfd
== NULL
)
4706 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
4707 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4713 BFD_ASSERT (h
->ldindx
>= 0);
4715 bfd_xcoff_swap_ldsym_out (output_bfd
, ldsym
,
4718 * bfd_xcoff_ldsymsz(finfo
->output_bfd
)));
4722 /* If this symbol needs global linkage code, write it out. */
4723 if (h
->root
.type
== bfd_link_hash_defined
4724 && (h
->root
.u
.def
.section
4725 == xcoff_hash_table (finfo
->info
)->linkage_section
))
4731 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
4733 /* The first instruction in the global linkage code loads a
4734 specific TOC element. */
4735 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
4736 + h
->descriptor
->toc_section
->output_offset
4737 - xcoff_data (output_bfd
)->toc
);
4739 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
4740 tocoff
+= h
->descriptor
->u
.toc_offset
;
4742 /* The first instruction in the glink code needs to be
4743 cooked to to hold the correct offset in the toc. The
4744 rest are just output raw. */
4745 bfd_put_32 (output_bfd
,
4746 bfd_xcoff_glink_code(output_bfd
, 0) | (tocoff
& 0xffff), p
);
4748 /* Start with i == 1 to get past the first instruction done above
4749 The /4 is because the glink code is in bytes and we are going
4751 for (i
= 1; i
< bfd_xcoff_glink_code_size(output_bfd
) / 4; i
++)
4752 bfd_put_32 (output_bfd
,
4753 (bfd_vma
) bfd_xcoff_glink_code(output_bfd
, i
),
4757 /* If we created a TOC entry for this symbol, write out the required
4759 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
4764 struct internal_reloc
*irel
;
4765 struct internal_ldrel ldrel
;
4766 struct internal_syment irsym
;
4767 union internal_auxent iraux
;
4769 tocsec
= h
->toc_section
;
4770 osec
= tocsec
->output_section
;
4771 oindx
= osec
->target_index
;
4772 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
4773 irel
->r_vaddr
= (osec
->vma
4774 + tocsec
->output_offset
4778 irel
->r_symndx
= h
->indx
;
4782 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
4785 BFD_ASSERT (h
->ldindx
>= 0);
4787 /* Initialize the aux union here instead of closer to when it is
4788 written out below because the length of the csect depends on
4789 whether the output is 32 or 64 bit. */
4790 memset (&iraux
, 0, sizeof iraux
);
4791 iraux
.x_csect
.x_smtyp
= XTY_SD
;
4792 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
4793 iraux
.x_csect
.x_smclas
= XMC_TC
;
4795 /* 32 bit uses a 32 bit R_POS to do the relocations
4796 64 bit uses a 64 bit R_POS to do the relocations
4798 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
4800 Which one is determined by the backend. */
4801 if (bfd_xcoff_is_xcoff64 (output_bfd
))
4804 iraux
.x_csect
.x_scnlen
.l
= 8;
4806 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
4809 iraux
.x_csect
.x_scnlen
.l
= 4;
4814 irel
->r_type
= R_POS
;
4815 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
4816 ++osec
->reloc_count
;
4818 ldrel
.l_vaddr
= irel
->r_vaddr
;
4819 ldrel
.l_symndx
= h
->ldindx
;
4820 ldrel
.l_rtype
= (irel
->r_size
<< 8) | R_POS
;
4821 ldrel
.l_rsecnm
= oindx
;
4822 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
4823 finfo
->ldrel
+= bfd_xcoff_ldrelsz(output_bfd
);
4825 /* We need to emit a symbol to define a csect which holds
4827 if (finfo
->info
->strip
!= strip_all
)
4829 result
= bfd_xcoff_put_symbol_name (output_bfd
, finfo
->strtab
,
4830 &irsym
, h
->root
.root
.string
);
4834 irsym
.n_value
= irel
->r_vaddr
;
4835 irsym
.n_scnum
= osec
->target_index
;
4836 irsym
.n_sclass
= C_HIDEXT
;
4837 irsym
.n_type
= T_NULL
;
4840 bfd_coff_swap_sym_out (output_bfd
, (void *) &irsym
, (void *) outsym
);
4841 outsym
+= bfd_coff_symesz (output_bfd
);
4843 /* Note : iraux is initialized above. */
4844 bfd_coff_swap_aux_out (output_bfd
, (void *) &iraux
, T_NULL
, C_HIDEXT
,
4845 0, 1, (void *) outsym
);
4846 outsym
+= bfd_coff_auxesz (output_bfd
);
4850 /* We aren't going to write out the symbols below, so we
4851 need to write them out now. */
4852 pos
= obj_sym_filepos (output_bfd
);
4853 pos
+= (obj_raw_syment_count (output_bfd
)
4854 * bfd_coff_symesz (output_bfd
));
4855 amt
= outsym
- finfo
->outsyms
;
4856 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
4857 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
4859 obj_raw_syment_count (output_bfd
) +=
4860 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
4862 outsym
= finfo
->outsyms
;
4867 /* If this symbol is a specially defined function descriptor, write
4868 it out. The first word is the address of the function code
4869 itself, the second word is the address of the TOC, and the third
4873 The addresses for the 32 bit will take 4 bytes and the addresses
4874 for 64 bit will take 8 bytes. Similar for the relocs. This type
4875 of logic was also done above to create a TOC entry in
4876 xcoff_write_global_symbol. */
4877 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
4878 && h
->root
.type
== bfd_link_hash_defined
4879 && (h
->root
.u
.def
.section
4880 == xcoff_hash_table (finfo
->info
)->descriptor_section
))
4886 struct xcoff_link_hash_entry
*hentry
;
4888 struct internal_reloc
*irel
;
4889 struct internal_ldrel ldrel
;
4891 unsigned int reloc_size
, byte_size
;
4893 if (bfd_xcoff_is_xcoff64 (output_bfd
))
4898 else if (bfd_xcoff_is_xcoff32 (output_bfd
))
4906 sec
= h
->root
.u
.def
.section
;
4907 osec
= sec
->output_section
;
4908 oindx
= osec
->target_index
;
4909 p
= sec
->contents
+ h
->root
.u
.def
.value
;
4911 hentry
= h
->descriptor
;
4912 BFD_ASSERT (hentry
!= NULL
4913 && (hentry
->root
.type
== bfd_link_hash_defined
4914 || hentry
->root
.type
== bfd_link_hash_defweak
));
4915 esec
= hentry
->root
.u
.def
.section
;
4917 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
4918 irel
->r_vaddr
= (osec
->vma
4919 + sec
->output_offset
4920 + h
->root
.u
.def
.value
);
4921 irel
->r_symndx
= esec
->output_section
->target_index
;
4922 irel
->r_type
= R_POS
;
4923 irel
->r_size
= reloc_size
;
4924 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
4925 ++osec
->reloc_count
;
4927 ldrel
.l_vaddr
= irel
->r_vaddr
;
4928 if (strcmp (esec
->output_section
->name
, ".text") == 0)
4930 else if (strcmp (esec
->output_section
->name
, ".data") == 0)
4932 else if (strcmp (esec
->output_section
->name
, ".bss") == 0)
4936 (*_bfd_error_handler
)
4937 (_("%s: loader reloc in unrecognized section `%s'"),
4938 bfd_get_filename (output_bfd
),
4939 esec
->output_section
->name
);
4940 bfd_set_error (bfd_error_nonrepresentable_section
);
4943 ldrel
.l_rtype
= (reloc_size
<< 8) | R_POS
;
4944 ldrel
.l_rsecnm
= oindx
;
4945 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
4946 finfo
->ldrel
+= bfd_xcoff_ldrelsz(output_bfd
);
4948 /* There are three items to write out,
4949 the address of the code
4950 the address of the toc anchor
4951 the environment pointer.
4952 We are ignoring the environment pointer. So set it to zero. */
4953 if (bfd_xcoff_is_xcoff64 (output_bfd
))
4955 bfd_put_64 (output_bfd
,
4956 (esec
->output_section
->vma
+ esec
->output_offset
4957 + hentry
->root
.u
.def
.value
),
4959 bfd_put_64 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 8);
4960 bfd_put_64 (output_bfd
, (bfd_vma
) 0, p
+ 16);
4965 This logic was already called above so the error case where
4966 the backend is neither has already been checked. */
4967 bfd_put_32 (output_bfd
,
4968 (esec
->output_section
->vma
+ esec
->output_offset
4969 + hentry
->root
.u
.def
.value
),
4971 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
4972 bfd_put_32 (output_bfd
, (bfd_vma
) 0, p
+ 8);
4975 tsec
= coff_section_from_bfd_index (output_bfd
,
4976 xcoff_data (output_bfd
)->sntoc
);
4979 irel
->r_vaddr
= (osec
->vma
4980 + sec
->output_offset
4981 + h
->root
.u
.def
.value
4983 irel
->r_symndx
= tsec
->output_section
->target_index
;
4984 irel
->r_type
= R_POS
;
4985 irel
->r_size
= reloc_size
;
4986 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
4987 ++osec
->reloc_count
;
4989 ldrel
.l_vaddr
= irel
->r_vaddr
;
4990 if (strcmp (tsec
->output_section
->name
, ".text") == 0)
4992 else if (strcmp (tsec
->output_section
->name
, ".data") == 0)
4994 else if (strcmp (tsec
->output_section
->name
, ".bss") == 0)
4998 (*_bfd_error_handler
)
4999 (_("%s: loader reloc in unrecognized section `%s'"),
5000 bfd_get_filename (output_bfd
),
5001 tsec
->output_section
->name
);
5002 bfd_set_error (bfd_error_nonrepresentable_section
);
5005 ldrel
.l_rtype
= (reloc_size
<< 8) | R_POS
;
5006 ldrel
.l_rsecnm
= oindx
;
5007 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5008 finfo
->ldrel
+= bfd_xcoff_ldrelsz(output_bfd
);
5011 if (h
->indx
>= 0 || finfo
->info
->strip
== strip_all
)
5013 BFD_ASSERT (outsym
== finfo
->outsyms
);
5018 && (finfo
->info
->strip
== strip_all
5019 || (finfo
->info
->strip
== strip_some
5020 && bfd_hash_lookup (finfo
->info
->keep_hash
, h
->root
.root
.string
,
5021 FALSE
, FALSE
) == NULL
)))
5023 BFD_ASSERT (outsym
== finfo
->outsyms
);
5028 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5030 BFD_ASSERT (outsym
== finfo
->outsyms
);
5034 memset (&aux
, 0, sizeof aux
);
5036 h
->indx
= obj_raw_syment_count (output_bfd
);
5038 result
= bfd_xcoff_put_symbol_name (output_bfd
, finfo
->strtab
, &isym
,
5039 h
->root
.root
.string
);
5043 if (h
->root
.type
== bfd_link_hash_undefined
5044 || h
->root
.type
== bfd_link_hash_undefweak
)
5047 isym
.n_scnum
= N_UNDEF
;
5048 isym
.n_sclass
= C_EXT
;
5049 aux
.x_csect
.x_smtyp
= XTY_ER
;
5051 else if ((h
->root
.type
== bfd_link_hash_defined
5052 || h
->root
.type
== bfd_link_hash_defweak
)
5053 && h
->smclas
== XMC_XO
)
5055 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5056 isym
.n_value
= h
->root
.u
.def
.value
;
5057 isym
.n_scnum
= N_UNDEF
;
5058 isym
.n_sclass
= C_EXT
;
5059 aux
.x_csect
.x_smtyp
= XTY_ER
;
5061 else if (h
->root
.type
== bfd_link_hash_defined
5062 || h
->root
.type
== bfd_link_hash_defweak
)
5064 struct xcoff_link_size_list
*l
;
5066 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5067 + h
->root
.u
.def
.section
->output_offset
5068 + h
->root
.u
.def
.value
);
5069 if (bfd_is_abs_section (h
->root
.u
.def
.section
->output_section
))
5070 isym
.n_scnum
= N_ABS
;
5072 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5073 isym
.n_sclass
= C_HIDEXT
;
5074 aux
.x_csect
.x_smtyp
= XTY_SD
;
5076 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5078 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
5084 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5090 else if (h
->root
.type
== bfd_link_hash_common
)
5092 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5093 + h
->root
.u
.c
.p
->section
->output_offset
);
5094 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5095 isym
.n_sclass
= C_EXT
;
5096 aux
.x_csect
.x_smtyp
= XTY_CM
;
5097 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5102 isym
.n_type
= T_NULL
;
5105 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5106 outsym
+= bfd_coff_symesz (output_bfd
);
5108 aux
.x_csect
.x_smclas
= h
->smclas
;
5109 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5111 outsym
+= bfd_coff_auxesz (output_bfd
);
5113 if ((h
->root
.type
== bfd_link_hash_defined
5114 || h
->root
.type
== bfd_link_hash_defweak
)
5115 && h
->smclas
!= XMC_XO
)
5117 /* We just output an SD symbol. Now output an LD symbol. */
5120 isym
.n_sclass
= C_EXT
;
5121 bfd_coff_swap_sym_out (output_bfd
, (void *) &isym
, (void *) outsym
);
5122 outsym
+= bfd_coff_symesz (output_bfd
);
5124 aux
.x_csect
.x_smtyp
= XTY_LD
;
5125 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5126 bfd_coff_swap_aux_out (output_bfd
, (void *) &aux
, T_NULL
, C_EXT
, 0, 1,
5128 outsym
+= bfd_coff_auxesz (output_bfd
);
5131 pos
= obj_sym_filepos (output_bfd
);
5132 pos
+= obj_raw_syment_count (output_bfd
) * bfd_coff_symesz (output_bfd
);
5133 amt
= outsym
- finfo
->outsyms
;
5134 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
5135 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
5137 obj_raw_syment_count (output_bfd
) +=
5138 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5143 /* Handle a link order which is supposed to generate a reloc. */
5146 xcoff_reloc_link_order (bfd
*output_bfd
,
5147 struct xcoff_final_link_info
*finfo
,
5148 asection
*output_section
,
5149 struct bfd_link_order
*link_order
)
5151 reloc_howto_type
*howto
;
5152 struct xcoff_link_hash_entry
*h
;
5156 struct internal_reloc
*irel
;
5157 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5158 struct internal_ldrel ldrel
;
5160 if (link_order
->type
== bfd_section_reloc_link_order
)
5161 /* We need to somehow locate a symbol in the right section. The
5162 symbol must either have a value of zero, or we must adjust
5163 the addend by the value of the symbol. FIXME: Write this
5164 when we need it. The old linker couldn't handle this anyhow. */
5167 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5170 bfd_set_error (bfd_error_bad_value
);
5174 h
= ((struct xcoff_link_hash_entry
*)
5175 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
5176 link_order
->u
.reloc
.p
->u
.name
,
5177 FALSE
, FALSE
, TRUE
));
5180 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
5181 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, (bfd_vma
) 0)))
5186 if (h
->root
.type
== bfd_link_hash_common
)
5188 hsec
= h
->root
.u
.c
.p
->section
;
5191 else if (h
->root
.type
== bfd_link_hash_defined
5192 || h
->root
.type
== bfd_link_hash_defweak
)
5194 hsec
= h
->root
.u
.def
.section
;
5195 hval
= h
->root
.u
.def
.value
;
5203 addend
= link_order
->u
.reloc
.p
->addend
;
5205 addend
+= (hsec
->output_section
->vma
5206 + hsec
->output_offset
5213 bfd_reloc_status_type rstat
;
5216 size
= bfd_get_reloc_size (howto
);
5217 buf
= bfd_zmalloc (size
);
5221 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5227 case bfd_reloc_outofrange
:
5229 case bfd_reloc_overflow
:
5230 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
5231 (finfo
->info
, NULL
, link_order
->u
.reloc
.p
->u
.name
,
5232 howto
->name
, addend
, NULL
, NULL
, (bfd_vma
) 0)))
5239 ok
= bfd_set_section_contents (output_bfd
, output_section
, (void *) buf
,
5240 (file_ptr
) link_order
->offset
, size
);
5246 /* Store the reloc information in the right place. It will get
5247 swapped and written out at the end of the final_link routine. */
5248 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
5249 + output_section
->reloc_count
);
5250 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
5251 + output_section
->reloc_count
);
5253 memset (irel
, 0, sizeof (struct internal_reloc
));
5254 *rel_hash_ptr
= NULL
;
5256 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5259 irel
->r_symndx
= h
->indx
;
5262 /* Set the index to -2 to force this symbol to get written out. */
5268 irel
->r_type
= howto
->type
;
5269 irel
->r_size
= howto
->bitsize
- 1;
5270 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5271 irel
->r_size
|= 0x80;
5273 ++output_section
->reloc_count
;
5275 /* Now output the reloc to the .loader section. */
5277 ldrel
.l_vaddr
= irel
->r_vaddr
;
5281 const char *secname
;
5283 secname
= hsec
->output_section
->name
;
5285 if (strcmp (secname
, ".text") == 0)
5287 else if (strcmp (secname
, ".data") == 0)
5289 else if (strcmp (secname
, ".bss") == 0)
5293 (*_bfd_error_handler
)
5294 (_("%s: loader reloc in unrecognized section `%s'"),
5295 bfd_get_filename (output_bfd
), secname
);
5296 bfd_set_error (bfd_error_nonrepresentable_section
);
5304 (*_bfd_error_handler
)
5305 (_("%s: `%s' in loader reloc but not loader sym"),
5306 bfd_get_filename (output_bfd
),
5307 h
->root
.root
.string
);
5308 bfd_set_error (bfd_error_bad_value
);
5311 ldrel
.l_symndx
= h
->ldindx
;
5314 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
5315 ldrel
.l_rsecnm
= output_section
->target_index
;
5316 bfd_xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5317 finfo
->ldrel
+= bfd_xcoff_ldrelsz(output_bfd
);
5322 /* Do the final link step. */
5325 _bfd_xcoff_bfd_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
5327 bfd_size_type symesz
;
5328 struct xcoff_final_link_info finfo
;
5330 struct bfd_link_order
*p
;
5331 bfd_size_type max_contents_size
;
5332 bfd_size_type max_sym_count
;
5333 bfd_size_type max_lineno_count
;
5334 bfd_size_type max_reloc_count
;
5335 bfd_size_type max_output_reloc_count
;
5336 file_ptr rel_filepos
;
5338 file_ptr line_filepos
;
5339 unsigned int linesz
;
5341 bfd_byte
*external_relocs
= NULL
;
5342 char strbuf
[STRING_SIZE_SIZE
];
5347 abfd
->flags
|= DYNAMIC
;
5349 symesz
= bfd_coff_symesz (abfd
);
5352 finfo
.output_bfd
= abfd
;
5353 finfo
.strtab
= NULL
;
5354 finfo
.section_info
= NULL
;
5355 finfo
.last_file_index
= -1;
5356 finfo
.toc_symindx
= -1;
5357 finfo
.internal_syms
= NULL
;
5358 finfo
.sym_indices
= NULL
;
5359 finfo
.outsyms
= NULL
;
5360 finfo
.linenos
= NULL
;
5361 finfo
.contents
= NULL
;
5362 finfo
.external_relocs
= NULL
;
5364 finfo
.ldsym
= (xcoff_hash_table (info
)->loader_section
->contents
5365 + bfd_xcoff_ldhdrsz (abfd
));
5366 finfo
.ldrel
= (xcoff_hash_table (info
)->loader_section
->contents
5367 + bfd_xcoff_ldhdrsz(abfd
)
5368 + (xcoff_hash_table (info
)->ldhdr
.l_nsyms
5369 * bfd_xcoff_ldsymsz(abfd
)));
5371 xcoff_data (abfd
)->coff
.link_info
= info
;
5373 finfo
.strtab
= _bfd_stringtab_init ();
5374 if (finfo
.strtab
== NULL
)
5377 /* Count the line number and relocation entries required for the
5378 output file. Determine a few maximum sizes. */
5379 max_contents_size
= 0;
5380 max_lineno_count
= 0;
5381 max_reloc_count
= 0;
5382 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5385 o
->lineno_count
= 0;
5386 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5388 if (p
->type
== bfd_indirect_link_order
)
5392 sec
= p
->u
.indirect
.section
;
5394 /* Mark all sections which are to be included in the
5395 link. This will normally be every section. We need
5396 to do this so that we can identify any sections which
5397 the linker has decided to not include. */
5398 sec
->linker_mark
= TRUE
;
5400 if (info
->strip
== strip_none
5401 || info
->strip
== strip_some
)
5402 o
->lineno_count
+= sec
->lineno_count
;
5404 o
->reloc_count
+= sec
->reloc_count
;
5406 if (sec
->rawsize
> max_contents_size
)
5407 max_contents_size
= sec
->rawsize
;
5408 if (sec
->size
> max_contents_size
)
5409 max_contents_size
= sec
->size
;
5410 if (sec
->lineno_count
> max_lineno_count
)
5411 max_lineno_count
= sec
->lineno_count
;
5412 if (coff_section_data (sec
->owner
, sec
) != NULL
5413 && xcoff_section_data (sec
->owner
, sec
) != NULL
5414 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
5415 > max_lineno_count
))
5417 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
5418 if (sec
->reloc_count
> max_reloc_count
)
5419 max_reloc_count
= sec
->reloc_count
;
5421 else if (p
->type
== bfd_section_reloc_link_order
5422 || p
->type
== bfd_symbol_reloc_link_order
)
5427 /* Compute the file positions for all the sections. */
5428 if (abfd
->output_has_begun
)
5430 if (xcoff_hash_table (info
)->file_align
!= 0)
5437 file_align
= xcoff_hash_table (info
)->file_align
;
5438 if (file_align
!= 0)
5440 bfd_boolean saw_contents
;
5444 /* Insert .pad sections before every section which has
5445 contents and is loaded, if it is preceded by some other
5446 section which has contents and is loaded. */
5447 saw_contents
= TRUE
;
5448 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5450 if (strcmp (o
->name
, ".pad") == 0)
5451 saw_contents
= FALSE
;
5452 else if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
5453 && (o
->flags
& SEC_LOAD
) != 0)
5456 saw_contents
= TRUE
;
5461 /* Create a pad section and place it before the section
5462 that needs padding. This requires unlinking and
5463 relinking the bfd's section list. */
5465 n
= bfd_make_section_anyway_with_flags (abfd
, ".pad",
5467 n
->alignment_power
= 0;
5469 bfd_section_list_remove (abfd
, n
);
5470 bfd_section_list_insert_before (abfd
, o
, n
);
5471 saw_contents
= FALSE
;
5476 /* Reset the section indices after inserting the new
5479 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5482 o
->target_index
= indx
;
5484 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
5486 /* Work out appropriate sizes for the .pad sections to force
5487 each section to land on a page boundary. This bit of
5488 code knows what compute_section_file_positions is going
5490 sofar
= bfd_coff_filhsz (abfd
);
5491 sofar
+= bfd_coff_aoutsz (abfd
);
5492 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
5493 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5494 if ((bfd_xcoff_is_reloc_count_overflow
5495 (abfd
, (bfd_vma
) o
->reloc_count
))
5496 || (bfd_xcoff_is_lineno_count_overflow
5497 (abfd
, (bfd_vma
) o
->lineno_count
)))
5498 /* 64 does not overflow, need to check if 32 does */
5499 sofar
+= bfd_coff_scnhsz (abfd
);
5501 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5503 if (strcmp (o
->name
, ".pad") == 0)
5507 BFD_ASSERT (o
->size
== 0);
5508 pageoff
= sofar
& (file_align
- 1);
5511 o
->size
= file_align
- pageoff
;
5512 sofar
+= file_align
- pageoff
;
5513 o
->flags
|= SEC_HAS_CONTENTS
;
5518 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
5519 sofar
+= BFD_ALIGN (o
->size
,
5520 1 << o
->alignment_power
);
5525 if (! bfd_coff_compute_section_file_positions (abfd
))
5529 /* Allocate space for the pointers we need to keep for the relocs. */
5533 /* We use section_count + 1, rather than section_count, because
5534 the target_index fields are 1 based. */
5535 amt
= abfd
->section_count
+ 1;
5536 amt
*= sizeof (struct xcoff_link_section_info
);
5537 finfo
.section_info
= bfd_malloc (amt
);
5538 if (finfo
.section_info
== NULL
)
5540 for (i
= 0; i
<= abfd
->section_count
; i
++)
5542 finfo
.section_info
[i
].relocs
= NULL
;
5543 finfo
.section_info
[i
].rel_hashes
= NULL
;
5544 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
5548 /* Set the file positions for the relocs. */
5549 rel_filepos
= obj_relocbase (abfd
);
5550 relsz
= bfd_coff_relsz (abfd
);
5551 max_output_reloc_count
= 0;
5552 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5554 if (o
->reloc_count
== 0)
5558 /* A stripped file has no relocs. However, we still
5559 allocate the buffers, so that later code doesn't have to
5560 worry about whether we are stripping or not. */
5561 if (info
->strip
== strip_all
)
5565 o
->flags
|= SEC_RELOC
;
5566 o
->rel_filepos
= rel_filepos
;
5567 rel_filepos
+= o
->reloc_count
* relsz
;
5570 /* We don't know the indices of global symbols until we have
5571 written out all the local symbols. For each section in
5572 the output file, we keep an array of pointers to hash
5573 table entries. Each entry in the array corresponds to a
5574 reloc. When we find a reloc against a global symbol, we
5575 set the corresponding entry in this array so that we can
5576 fix up the symbol index after we have written out all the
5579 Because of this problem, we also keep the relocs in
5580 memory until the end of the link. This wastes memory.
5581 We could backpatch the file later, I suppose, although it
5583 amt
= o
->reloc_count
;
5584 amt
*= sizeof (struct internal_reloc
);
5585 finfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
5587 amt
= o
->reloc_count
;
5588 amt
*= sizeof (struct xcoff_link_hash_entry
*);
5589 finfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
5591 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
5592 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
5595 if (o
->reloc_count
> max_output_reloc_count
)
5596 max_output_reloc_count
= o
->reloc_count
;
5600 /* We now know the size of the relocs, so we can determine the file
5601 positions of the line numbers. */
5602 line_filepos
= rel_filepos
;
5603 finfo
.line_filepos
= line_filepos
;
5604 linesz
= bfd_coff_linesz (abfd
);
5605 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5607 if (o
->lineno_count
== 0)
5608 o
->line_filepos
= 0;
5611 o
->line_filepos
= line_filepos
;
5612 line_filepos
+= o
->lineno_count
* linesz
;
5615 /* Reset the reloc and lineno counts, so that we can use them to
5616 count the number of entries we have output so far. */
5618 o
->lineno_count
= 0;
5621 obj_sym_filepos (abfd
) = line_filepos
;
5623 /* Figure out the largest number of symbols in an input BFD. Take
5624 the opportunity to clear the output_has_begun fields of all the
5625 input BFD's. We want at least 6 symbols, since that is the
5626 number which xcoff_write_global_symbol may need. */
5628 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
5632 sub
->output_has_begun
= FALSE
;
5633 sz
= obj_raw_syment_count (sub
);
5634 if (sz
> max_sym_count
)
5638 /* Allocate some buffers used while linking. */
5639 amt
= max_sym_count
* sizeof (struct internal_syment
);
5640 finfo
.internal_syms
= bfd_malloc (amt
);
5642 amt
= max_sym_count
* sizeof (long);
5643 finfo
.sym_indices
= bfd_malloc (amt
);
5645 amt
= (max_sym_count
+ 1) * symesz
;
5646 finfo
.outsyms
= bfd_malloc (amt
);
5648 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
5649 finfo
.linenos
= bfd_malloc (amt
);
5651 amt
= max_contents_size
;
5652 finfo
.contents
= bfd_malloc (amt
);
5654 amt
= max_reloc_count
* relsz
;
5655 finfo
.external_relocs
= bfd_malloc (amt
);
5657 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
5658 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
5659 || finfo
.outsyms
== NULL
5660 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
5661 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
5662 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
5665 obj_raw_syment_count (abfd
) = 0;
5666 xcoff_data (abfd
)->toc
= (bfd_vma
) -1;
5668 /* We now know the position of everything in the file, except that
5669 we don't know the size of the symbol table and therefore we don't
5670 know where the string table starts. We just build the string
5671 table in memory as we go along. We process all the relocations
5672 for a single input file at once. */
5673 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5675 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5677 if (p
->type
== bfd_indirect_link_order
5678 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
5680 sub
= p
->u
.indirect
.section
->owner
;
5681 if (! sub
->output_has_begun
)
5683 if (! xcoff_link_input_bfd (&finfo
, sub
))
5685 sub
->output_has_begun
= TRUE
;
5688 else if (p
->type
== bfd_section_reloc_link_order
5689 || p
->type
== bfd_symbol_reloc_link_order
)
5691 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
5696 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5702 /* Free up the buffers used by xcoff_link_input_bfd. */
5703 if (finfo
.internal_syms
!= NULL
)
5705 free (finfo
.internal_syms
);
5706 finfo
.internal_syms
= NULL
;
5708 if (finfo
.sym_indices
!= NULL
)
5710 free (finfo
.sym_indices
);
5711 finfo
.sym_indices
= NULL
;
5713 if (finfo
.linenos
!= NULL
)
5715 free (finfo
.linenos
);
5716 finfo
.linenos
= NULL
;
5718 if (finfo
.contents
!= NULL
)
5720 free (finfo
.contents
);
5721 finfo
.contents
= NULL
;
5723 if (finfo
.external_relocs
!= NULL
)
5725 free (finfo
.external_relocs
);
5726 finfo
.external_relocs
= NULL
;
5729 /* The value of the last C_FILE symbol is supposed to be -1. Write
5731 if (finfo
.last_file_index
!= -1)
5733 finfo
.last_file
.n_value
= -(bfd_vma
) 1;
5734 bfd_coff_swap_sym_out (abfd
, (void *) &finfo
.last_file
,
5735 (void *) finfo
.outsyms
);
5736 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
5737 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5738 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
5742 /* Write out all the global symbols which do not come from XCOFF
5744 xcoff_link_hash_traverse (xcoff_hash_table (info
),
5745 xcoff_write_global_symbol
,
5748 if (finfo
.outsyms
!= NULL
)
5750 free (finfo
.outsyms
);
5751 finfo
.outsyms
= NULL
;
5754 /* Now that we have written out all the global symbols, we know the
5755 symbol indices to use for relocs against them, and we can finally
5756 write out the relocs. */
5757 amt
= max_output_reloc_count
* relsz
;
5758 external_relocs
= bfd_malloc (amt
);
5759 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
5762 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5764 struct internal_reloc
*irel
;
5765 struct internal_reloc
*irelend
;
5766 struct xcoff_link_hash_entry
**rel_hash
;
5767 struct xcoff_toc_rel_hash
*toc_rel_hash
;
5769 bfd_size_type rel_size
;
5771 /* A stripped file has no relocs. */
5772 if (info
->strip
== strip_all
)
5778 if (o
->reloc_count
== 0)
5781 irel
= finfo
.section_info
[o
->target_index
].relocs
;
5782 irelend
= irel
+ o
->reloc_count
;
5783 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
5784 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
5786 if (*rel_hash
!= NULL
)
5788 if ((*rel_hash
)->indx
< 0)
5790 if (! ((*info
->callbacks
->unattached_reloc
)
5791 (info
, (*rel_hash
)->root
.root
.string
,
5792 NULL
, o
, irel
->r_vaddr
)))
5794 (*rel_hash
)->indx
= 0;
5796 irel
->r_symndx
= (*rel_hash
)->indx
;
5800 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
5801 toc_rel_hash
!= NULL
;
5802 toc_rel_hash
= toc_rel_hash
->next
)
5804 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
5806 if (! ((*info
->callbacks
->unattached_reloc
)
5807 (info
, toc_rel_hash
->h
->root
.root
.string
,
5808 NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
5810 toc_rel_hash
->h
->u
.toc_indx
= 0;
5812 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
5815 /* XCOFF requires that the relocs be sorted by address. We tend
5816 to produce them in the order in which their containing csects
5817 appear in the symbol table, which is not necessarily by
5818 address. So we sort them here. There may be a better way to
5820 qsort ((void *) finfo
.section_info
[o
->target_index
].relocs
,
5821 o
->reloc_count
, sizeof (struct internal_reloc
),
5824 irel
= finfo
.section_info
[o
->target_index
].relocs
;
5825 irelend
= irel
+ o
->reloc_count
;
5826 erel
= external_relocs
;
5827 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
5828 bfd_coff_swap_reloc_out (abfd
, (void *) irel
, (void *) erel
);
5830 rel_size
= relsz
* o
->reloc_count
;
5831 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
5832 || bfd_bwrite ((void *) external_relocs
, rel_size
, abfd
) != rel_size
)
5836 if (external_relocs
!= NULL
)
5838 free (external_relocs
);
5839 external_relocs
= NULL
;
5842 /* Free up the section information. */
5843 if (finfo
.section_info
!= NULL
)
5847 for (i
= 0; i
< abfd
->section_count
; i
++)
5849 if (finfo
.section_info
[i
].relocs
!= NULL
)
5850 free (finfo
.section_info
[i
].relocs
);
5851 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
5852 free (finfo
.section_info
[i
].rel_hashes
);
5854 free (finfo
.section_info
);
5855 finfo
.section_info
= NULL
;
5858 /* Write out the loader section contents. */
5859 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
5860 == (xcoff_hash_table (info
)->loader_section
->contents
5861 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
5862 o
= xcoff_hash_table (info
)->loader_section
;
5863 if (! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
5864 (file_ptr
) o
->output_offset
, o
->size
))
5867 /* Write out the magic sections. */
5868 o
= xcoff_hash_table (info
)->linkage_section
;
5870 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
5871 (file_ptr
) o
->output_offset
,
5874 o
= xcoff_hash_table (info
)->toc_section
;
5876 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
5877 (file_ptr
) o
->output_offset
,
5880 o
= xcoff_hash_table (info
)->descriptor_section
;
5882 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
5883 (file_ptr
) o
->output_offset
,
5887 /* Write out the string table. */
5888 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
5889 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5892 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
5894 amt
= STRING_SIZE_SIZE
;
5895 if (bfd_bwrite (strbuf
, amt
, abfd
) != amt
)
5897 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
5900 _bfd_stringtab_free (finfo
.strtab
);
5902 /* Write out the debugging string table. */
5903 o
= xcoff_hash_table (info
)->debug_section
;
5906 struct bfd_strtab_hash
*debug_strtab
;
5908 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
5909 BFD_ASSERT (o
->output_section
->size
- o
->output_offset
5910 >= _bfd_stringtab_size (debug_strtab
));
5911 pos
= o
->output_section
->filepos
+ o
->output_offset
;
5912 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5914 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
5918 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
5919 not try to write out the symbols. */
5920 bfd_get_symcount (abfd
) = 0;
5925 if (finfo
.strtab
!= NULL
)
5926 _bfd_stringtab_free (finfo
.strtab
);
5928 if (finfo
.section_info
!= NULL
)
5932 for (i
= 0; i
< abfd
->section_count
; i
++)
5934 if (finfo
.section_info
[i
].relocs
!= NULL
)
5935 free (finfo
.section_info
[i
].relocs
);
5936 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
5937 free (finfo
.section_info
[i
].rel_hashes
);
5939 free (finfo
.section_info
);
5942 if (finfo
.internal_syms
!= NULL
)
5943 free (finfo
.internal_syms
);
5944 if (finfo
.sym_indices
!= NULL
)
5945 free (finfo
.sym_indices
);
5946 if (finfo
.outsyms
!= NULL
)
5947 free (finfo
.outsyms
);
5948 if (finfo
.linenos
!= NULL
)
5949 free (finfo
.linenos
);
5950 if (finfo
.contents
!= NULL
)
5951 free (finfo
.contents
);
5952 if (finfo
.external_relocs
!= NULL
)
5953 free (finfo
.external_relocs
);
5954 if (external_relocs
!= NULL
)
5955 free (external_relocs
);