1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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 struct xcoff_link_hash_table
80 struct bfd_link_hash_table root;
82 /* The .debug string hash table. We need to compute this while
83 reading the input files, so that we know how large the .debug
84 section will be before we assign section positions. */
85 struct bfd_strtab_hash *debug_strtab;
87 /* The .debug section we will use for the final output. */
88 asection *debug_section;
90 /* The .loader section we will use for the final output. */
91 asection *loader_section;
93 /* A count of non TOC relative relocs which will need to be
94 allocated in the .loader section. */
97 /* The .loader section header. */
98 struct internal_ldhdr ldhdr;
100 /* The .gl section we use to hold global linkage code. */
101 asection *linkage_section;
103 /* The .tc section we use to hold toc entries we build for global
105 asection *toc_section;
107 /* The .ds section we use to hold function descriptors which we
108 create for exported symbols. */
109 asection *descriptor_section;
111 /* The list of import files. */
112 struct xcoff_import_file *imports;
114 /* Required alignment of sections within the output file. */
115 unsigned long file_align;
117 /* Whether the .text section must be read-only. */
120 /* Whether -brtl was specified. */
123 /* Whether garbage collection was done. */
126 /* A linked list of symbols for which we have size information. */
127 struct xcoff_link_size_list
129 struct xcoff_link_size_list *next;
130 struct xcoff_link_hash_entry *h;
135 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
136 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
139 /* Information that we pass around while doing the final link step. */
141 struct xcoff_final_link_info
143 /* General link information. */
144 struct bfd_link_info *info;
147 /* Hash table for long symbol names. */
148 struct bfd_strtab_hash *strtab;
149 /* Array of information kept for each output section, indexed by the
150 target_index field. */
151 struct xcoff_link_section_info *section_info;
152 /* Symbol index of last C_FILE symbol (-1 if none). */
153 long last_file_index;
154 /* Contents of last C_FILE symbol. */
155 struct internal_syment last_file;
156 /* Symbol index of TOC symbol. */
158 /* Start of .loader symbols. */
160 /* Next .loader reloc to swap out. */
162 /* File position of start of line numbers. */
163 file_ptr line_filepos;
164 /* Buffer large enough to hold swapped symbols of any input file. */
165 struct internal_syment *internal_syms;
166 /* Buffer large enough to hold output indices of symbols of any
169 /* Buffer large enough to hold output symbols for any input file. */
171 /* Buffer large enough to hold external line numbers for any input
174 /* Buffer large enough to hold any input section. */
176 /* Buffer large enough to hold external relocs of any input section. */
177 bfd_byte *external_relocs;
180 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
184 /* Routines to read XCOFF dynamic information. This don't really
185 belong here, but we already have the ldsym manipulation routines
188 /* Read the contents of a section. */
191 xcoff_get_section_contents (bfd *abfd, asection *sec)
193 if (coff_section_data (abfd, sec) == NULL)
195 bfd_size_type amt = sizeof (struct coff_section_tdata);
197 sec->used_by_bfd = bfd_zalloc (abfd, amt);
198 if (sec->used_by_bfd == NULL)
202 if (coff_section_data (abfd, sec)->contents == NULL)
206 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
208 if (contents != NULL)
212 coff_section_data (abfd, sec)->contents = contents;
218 /* Get the size required to hold the dynamic symbols. */
221 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
225 struct internal_ldhdr ldhdr;
227 if ((abfd->flags & DYNAMIC) == 0)
229 bfd_set_error (bfd_error_invalid_operation);
233 lsec = bfd_get_section_by_name (abfd, ".loader");
236 bfd_set_error (bfd_error_no_symbols);
240 if (! xcoff_get_section_contents (abfd, lsec))
242 contents = coff_section_data (abfd, lsec)->contents;
244 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
246 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
249 /* Get the dynamic symbols. */
252 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
256 struct internal_ldhdr ldhdr;
258 bfd_byte *elsym, *elsymend;
259 coff_symbol_type *symbuf;
261 if ((abfd->flags & DYNAMIC) == 0)
263 bfd_set_error (bfd_error_invalid_operation);
267 lsec = bfd_get_section_by_name (abfd, ".loader");
270 bfd_set_error (bfd_error_no_symbols);
274 if (! xcoff_get_section_contents (abfd, lsec))
276 contents = coff_section_data (abfd, lsec)->contents;
278 coff_section_data (abfd, lsec)->keep_contents = TRUE;
280 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
282 strings = (char *) contents + ldhdr.l_stoff;
284 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
288 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
290 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
291 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
293 struct internal_ldsym ldsym;
295 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
297 symbuf->symbol.the_bfd = abfd;
299 if (ldsym._l._l_l._l_zeroes == 0)
300 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
305 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
308 memcpy (c, ldsym._l._l_name, SYMNMLEN);
310 symbuf->symbol.name = c;
313 if (ldsym.l_smclas == XMC_XO)
314 symbuf->symbol.section = bfd_abs_section_ptr;
316 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
318 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
320 symbuf->symbol.flags = BSF_NO_FLAGS;
321 if ((ldsym.l_smtype & L_EXPORT) != 0)
323 if ((ldsym.l_smtype & L_WEAK) != 0)
324 symbuf->symbol.flags |= BSF_WEAK;
326 symbuf->symbol.flags |= BSF_GLOBAL;
329 /* FIXME: We have no way to record the other information stored
330 with the loader symbol. */
331 *psyms = (asymbol *) symbuf;
336 return ldhdr.l_nsyms;
339 /* Get the size required to hold the dynamic relocs. */
342 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
346 struct internal_ldhdr ldhdr;
348 if ((abfd->flags & DYNAMIC) == 0)
350 bfd_set_error (bfd_error_invalid_operation);
354 lsec = bfd_get_section_by_name (abfd, ".loader");
357 bfd_set_error (bfd_error_no_symbols);
361 if (! xcoff_get_section_contents (abfd, lsec))
363 contents = coff_section_data (abfd, lsec)->contents;
365 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
367 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
370 /* Get the dynamic relocs. */
373 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
379 struct internal_ldhdr ldhdr;
381 bfd_byte *elrel, *elrelend;
383 if ((abfd->flags & DYNAMIC) == 0)
385 bfd_set_error (bfd_error_invalid_operation);
389 lsec = bfd_get_section_by_name (abfd, ".loader");
392 bfd_set_error (bfd_error_no_symbols);
396 if (! xcoff_get_section_contents (abfd, lsec))
398 contents = coff_section_data (abfd, lsec)->contents;
400 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
402 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
406 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
408 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
409 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
412 struct internal_ldrel ldrel;
414 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
416 if (ldrel.l_symndx >= 3)
417 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
423 switch (ldrel.l_symndx)
439 sec = bfd_get_section_by_name (abfd, name);
442 bfd_set_error (bfd_error_bad_value);
446 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
449 relbuf->address = ldrel.l_vaddr;
452 /* Most dynamic relocs have the same type. FIXME: This is only
453 correct if ldrel.l_rtype == 0. In other cases, we should use
454 a different howto. */
455 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
457 /* FIXME: We have no way to record the l_rsecnm field. */
464 return ldhdr.l_nreloc;
467 /* Routine to create an entry in an XCOFF link hash table. */
469 static struct bfd_hash_entry *
470 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
471 struct bfd_hash_table *table,
474 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
476 /* Allocate the structure if it has not already been allocated by a
479 ret = bfd_hash_allocate (table, sizeof (* ret));
483 /* Call the allocation method of the superclass. */
484 ret = ((struct xcoff_link_hash_entry *)
485 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
489 /* Set local fields. */
491 ret->toc_section = NULL;
492 ret->u.toc_indx = -1;
493 ret->descriptor = NULL;
497 ret->smclas = XMC_UA;
500 return (struct bfd_hash_entry *) ret;
503 /* Create a XCOFF link hash table. */
505 struct bfd_link_hash_table *
506 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
508 struct xcoff_link_hash_table *ret;
509 bfd_size_type amt = sizeof (* ret);
511 ret = bfd_malloc (amt);
514 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
515 sizeof (struct xcoff_link_hash_entry)))
521 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
522 ret->debug_section = NULL;
523 ret->loader_section = NULL;
524 ret->ldrel_count = 0;
525 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
526 ret->linkage_section = NULL;
527 ret->toc_section = NULL;
528 ret->descriptor_section = NULL;
533 memset (ret->special_sections, 0, sizeof ret->special_sections);
535 /* The linker will always generate a full a.out header. We need to
536 record that fact now, before the sizeof_headers routine could be
538 xcoff_data (abfd)->full_aouthdr = TRUE;
543 /* Free a XCOFF link hash table. */
546 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
548 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
550 _bfd_stringtab_free (ret->debug_strtab);
551 bfd_hash_table_free (&ret->root.table);
555 /* Read internal relocs for an XCOFF csect. This is a wrapper around
556 _bfd_coff_read_internal_relocs which tries to take advantage of any
557 relocs which may have been cached for the enclosing section. */
559 static struct internal_reloc *
560 xcoff_read_internal_relocs (bfd *abfd,
563 bfd_byte *external_relocs,
564 bfd_boolean require_internal,
565 struct internal_reloc *internal_relocs)
567 if (coff_section_data (abfd, sec) != NULL
568 && coff_section_data (abfd, sec)->relocs == NULL
569 && xcoff_section_data (abfd, sec) != NULL)
573 enclosing = xcoff_section_data (abfd, sec)->enclosing;
575 if (enclosing != NULL
576 && (coff_section_data (abfd, enclosing) == NULL
577 || coff_section_data (abfd, enclosing)->relocs == NULL)
579 && enclosing->reloc_count > 0)
581 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
582 external_relocs, FALSE, NULL)
587 if (enclosing != NULL
588 && coff_section_data (abfd, enclosing) != NULL
589 && coff_section_data (abfd, enclosing)->relocs != NULL)
593 off = ((sec->rel_filepos - enclosing->rel_filepos)
594 / bfd_coff_relsz (abfd));
596 if (! require_internal)
597 return coff_section_data (abfd, enclosing)->relocs + off;
598 memcpy (internal_relocs,
599 coff_section_data (abfd, enclosing)->relocs + off,
600 sec->reloc_count * sizeof (struct internal_reloc));
601 return internal_relocs;
605 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
606 require_internal, internal_relocs);
609 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
610 Return true if LDSYM defines H. */
613 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
614 struct internal_ldsym *ldsym)
616 /* If we didn't know about H before processing LDSYM, LDSYM
617 definitely defines H. */
618 if (h->root.type == bfd_link_hash_new)
621 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
622 dynamic symbol, LDSYM trumps the current definition of H. */
623 if ((ldsym->l_smtype & L_WEAK) == 0
624 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
625 && (h->flags & XCOFF_DEF_REGULAR) == 0
626 && (h->root.type == bfd_link_hash_defweak
627 || h->root.type == bfd_link_hash_undefweak))
630 /* If H is currently undefined, LDSYM defines it. */
631 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
632 && (h->root.type == bfd_link_hash_undefined
633 || h->root.type == bfd_link_hash_undefweak))
639 /* This function is used to add symbols from a dynamic object to the
640 global symbol table. */
643 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
647 struct internal_ldhdr ldhdr;
649 bfd_byte *elsym, *elsymend;
650 struct xcoff_import_file *n;
655 struct xcoff_import_file **pp;
657 /* We can only handle a dynamic object if we are generating an XCOFF
659 if (info->output_bfd->xvec != abfd->xvec)
661 (*_bfd_error_handler)
662 (_("%s: XCOFF shared object when not producing XCOFF output"),
663 bfd_get_filename (abfd));
664 bfd_set_error (bfd_error_invalid_operation);
668 /* The symbols we use from a dynamic object are not the symbols in
669 the normal symbol table, but, rather, the symbols in the export
670 table. If there is a global symbol in a dynamic object which is
671 not in the export table, the loader will not be able to find it,
672 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
673 libc.a has symbols in the export table which are not in the
676 /* Read in the .loader section. FIXME: We should really use the
677 o_snloader field in the a.out header, rather than grabbing the
679 lsec = bfd_get_section_by_name (abfd, ".loader");
682 (*_bfd_error_handler)
683 (_("%s: dynamic object with no .loader section"),
684 bfd_get_filename (abfd));
685 bfd_set_error (bfd_error_no_symbols);
689 if (! xcoff_get_section_contents (abfd, lsec))
691 contents = coff_section_data (abfd, lsec)->contents;
693 /* Remove the sections from this object, so that they do not get
694 included in the link. */
695 bfd_section_list_clear (abfd);
697 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
699 strings = (char *) contents + ldhdr.l_stoff;
701 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
703 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
705 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
707 struct internal_ldsym ldsym;
708 char nambuf[SYMNMLEN + 1];
710 struct xcoff_link_hash_entry *h;
712 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
714 /* We are only interested in exported symbols. */
715 if ((ldsym.l_smtype & L_EXPORT) == 0)
718 if (ldsym._l._l_l._l_zeroes == 0)
719 name = strings + ldsym._l._l_l._l_offset;
722 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
723 nambuf[SYMNMLEN] = '\0';
727 /* Normally we could not call xcoff_link_hash_lookup in an add
728 symbols routine, since we might not be using an XCOFF hash
729 table. However, we verified above that we are using an XCOFF
732 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
737 if (!xcoff_dynamic_definition_p (h, &ldsym))
740 h->flags |= XCOFF_DEF_DYNAMIC;
741 h->smclas = ldsym.l_smclas;
742 if (h->smclas == XMC_XO)
744 /* This symbol has an absolute value. */
745 if ((ldsym.l_smtype & L_WEAK) != 0)
746 h->root.type = bfd_link_hash_defweak;
748 h->root.type = bfd_link_hash_defined;
749 h->root.u.def.section = bfd_abs_section_ptr;
750 h->root.u.def.value = ldsym.l_value;
754 /* Otherwise, we don't bother to actually define the symbol,
755 since we don't have a section to put it in anyhow.
756 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
757 should be imported from the symbol's undef.abfd. */
758 if ((ldsym.l_smtype & L_WEAK) != 0)
759 h->root.type = bfd_link_hash_undefweak;
761 h->root.type = bfd_link_hash_undefined;
762 h->root.u.undef.abfd = abfd;
765 /* If this symbol defines a function descriptor, then it
766 implicitly defines the function code as well. */
767 if (h->smclas == XMC_DS
768 || (h->smclas == XMC_XO && name[0] != '.'))
769 h->flags |= XCOFF_DESCRIPTOR;
770 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
772 struct xcoff_link_hash_entry *hds;
779 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
783 strcpy (dsnm + 1, name);
784 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
794 if (xcoff_dynamic_definition_p (hds, &ldsym))
796 hds->root.type = h->root.type;
797 hds->flags |= XCOFF_DEF_DYNAMIC;
798 if (h->smclas == XMC_XO)
800 /* An absolute symbol appears to actually define code, not a
801 function descriptor. This is how some math functions are
802 implemented on AIX 4.1. */
803 hds->smclas = XMC_XO;
804 hds->root.u.def.section = bfd_abs_section_ptr;
805 hds->root.u.def.value = ldsym.l_value;
809 hds->smclas = XMC_PR;
810 hds->root.u.undef.abfd = abfd;
811 /* We do not want to add this to the undefined
818 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
820 free (coff_section_data (abfd, lsec)->contents);
821 coff_section_data (abfd, lsec)->contents = NULL;
824 /* Record this file in the import files. */
825 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
830 /* For some reason, the path entry in the import file list for a
831 shared object appears to always be empty. The file name is the
834 if (abfd->my_archive == NULL)
836 bname = bfd_get_filename (abfd);
841 bname = bfd_get_filename (abfd->my_archive);
842 mname = bfd_get_filename (abfd);
844 s = strrchr (bname, '/');
850 /* We start c at 1 because the first import file number is reserved
852 for (pp = &xcoff_hash_table (info)->imports, c = 1;
854 pp = &(*pp)->next, ++c)
858 xcoff_data (abfd)->import_file_id = c;
863 /* xcoff_link_create_extra_sections
865 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
868 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
870 bfd_boolean return_value = FALSE;
872 if (info->output_bfd->xvec == abfd->xvec)
874 /* We need to build a .loader section, so we do it here. This
875 won't work if we're producing an XCOFF output file with no
876 XCOFF input files. FIXME. */
878 if (!info->relocatable
879 && xcoff_hash_table (info)->loader_section == NULL)
882 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
884 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
888 xcoff_hash_table (info)->loader_section = lsec;
891 /* Likewise for the linkage section. */
892 if (xcoff_hash_table (info)->linkage_section == NULL)
895 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
898 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
902 xcoff_hash_table (info)->linkage_section = lsec;
903 lsec->alignment_power = 2;
906 /* Likewise for the TOC section. */
907 if (xcoff_hash_table (info)->toc_section == NULL)
910 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
913 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
917 xcoff_hash_table (info)->toc_section = tsec;
918 tsec->alignment_power = 2;
921 /* Likewise for the descriptor section. */
922 if (xcoff_hash_table (info)->descriptor_section == NULL)
925 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
928 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
932 xcoff_hash_table (info)->descriptor_section = dsec;
933 dsec->alignment_power = 2;
936 /* Likewise for the .debug section. */
937 if (xcoff_hash_table (info)->debug_section == NULL
938 && info->strip != strip_all)
941 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
943 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
947 xcoff_hash_table (info)->debug_section = dsec;
958 /* Returns the index of reloc in RELOCS with the least address greater
959 than or equal to ADDRESS. The relocs are sorted by address. */
962 xcoff_find_reloc (struct internal_reloc *relocs,
966 bfd_size_type min, max, this;
970 if (count == 1 && relocs[0].r_vaddr < address)
979 /* Do a binary search over (min,max]. */
980 while (min + 1 < max)
984 this = (max + min) / 2;
985 raddr = relocs[this].r_vaddr;
988 else if (raddr < address)
997 if (relocs[min].r_vaddr < address)
1001 && relocs[min - 1].r_vaddr == address)
1007 /* Add all the symbols from an object file to the hash table.
1009 XCOFF is a weird format. A normal XCOFF .o files will have three
1010 COFF sections--.text, .data, and .bss--but each COFF section will
1011 contain many csects. These csects are described in the symbol
1012 table. From the linker's point of view, each csect must be
1013 considered a section in its own right. For example, a TOC entry is
1014 handled as a small XMC_TC csect. The linker must be able to merge
1015 different TOC entries together, which means that it must be able to
1016 extract the XMC_TC csects from the .data section of the input .o
1019 From the point of view of our linker, this is, of course, a hideous
1020 nightmare. We cope by actually creating sections for each csect,
1021 and discarding the original sections. We then have to handle the
1022 relocation entries carefully, since the only way to tell which
1023 csect they belong to is to examine the address. */
1026 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1028 unsigned int n_tmask;
1029 unsigned int n_btshft;
1030 bfd_boolean default_copy;
1031 bfd_size_type symcount;
1032 struct xcoff_link_hash_entry **sym_hash;
1033 asection **csect_cache;
1034 unsigned int *lineno_counts;
1035 bfd_size_type linesz;
1037 asection *last_real;
1038 bfd_boolean keep_syms;
1040 unsigned int csect_index;
1041 asection *first_csect;
1042 bfd_size_type symesz;
1045 struct reloc_info_struct
1047 struct internal_reloc *relocs;
1050 } *reloc_info = NULL;
1053 keep_syms = obj_coff_keep_syms (abfd);
1055 if ((abfd->flags & DYNAMIC) != 0
1056 && ! info->static_link)
1058 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1062 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1063 if (! xcoff_link_create_extra_sections (abfd, info))
1066 if ((abfd->flags & DYNAMIC) != 0
1067 && ! info->static_link)
1070 n_tmask = coff_data (abfd)->local_n_tmask;
1071 n_btshft = coff_data (abfd)->local_n_btshft;
1073 /* Define macros so that ISFCN, et. al., macros work correctly. */
1074 #define N_TMASK n_tmask
1075 #define N_BTSHFT n_btshft
1077 if (info->keep_memory)
1078 default_copy = FALSE;
1080 default_copy = TRUE;
1082 symcount = obj_raw_syment_count (abfd);
1084 /* We keep a list of the linker hash table entries that correspond
1085 to each external symbol. */
1086 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1087 sym_hash = bfd_zalloc (abfd, amt);
1088 if (sym_hash == NULL && symcount != 0)
1090 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1092 /* Because of the weird stuff we are doing with XCOFF csects, we can
1093 not easily determine which section a symbol is in, so we store
1094 the information in the tdata for the input file. */
1095 amt = symcount * sizeof (asection *);
1096 csect_cache = bfd_zalloc (abfd, amt);
1097 if (csect_cache == NULL && symcount != 0)
1099 xcoff_data (abfd)->csects = csect_cache;
1101 /* We garbage-collect line-number information on a symbol-by-symbol
1102 basis, so we need to have quick access to the number of entries
1104 amt = symcount * sizeof (unsigned int);
1105 lineno_counts = bfd_zalloc (abfd, amt);
1106 if (lineno_counts == NULL && symcount != 0)
1108 xcoff_data (abfd)->lineno_counts = lineno_counts;
1110 /* While splitting sections into csects, we need to assign the
1111 relocs correctly. The relocs and the csects must both be in
1112 order by VMA within a given section, so we handle this by
1113 scanning along the relocs as we process the csects. We index
1114 into reloc_info using the section target_index. */
1115 amt = abfd->section_count + 1;
1116 amt *= sizeof (struct reloc_info_struct);
1117 reloc_info = bfd_zmalloc (amt);
1118 if (reloc_info == NULL)
1121 /* Read in the relocs and line numbers for each section. */
1122 linesz = bfd_coff_linesz (abfd);
1124 for (o = abfd->sections; o != NULL; o = o->next)
1128 if ((o->flags & SEC_RELOC) != 0)
1130 reloc_info[o->target_index].relocs =
1131 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1132 amt = o->reloc_count;
1133 amt *= sizeof (asection *);
1134 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1135 if (reloc_info[o->target_index].csects == NULL)
1139 if ((info->strip == strip_none || info->strip == strip_some)
1140 && o->lineno_count > 0)
1144 amt = linesz * o->lineno_count;
1145 linenos = bfd_malloc (amt);
1146 if (linenos == NULL)
1148 reloc_info[o->target_index].linenos = linenos;
1149 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1150 || bfd_bread (linenos, amt, abfd) != amt)
1155 /* Don't let the linker relocation routines discard the symbols. */
1156 obj_coff_keep_syms (abfd) = TRUE;
1162 symesz = bfd_coff_symesz (abfd);
1163 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1164 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1165 esym_end = esym + symcount * symesz;
1167 while (esym < esym_end)
1169 struct internal_syment sym;
1170 union internal_auxent aux;
1172 char buf[SYMNMLEN + 1];
1176 struct xcoff_link_hash_entry *set_toc;
1178 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1180 /* In this pass we are only interested in symbols with csect
1182 if (!CSECT_SYM_P (sym.n_sclass))
1185 Normally csect is a .pr, .rw etc. created in the loop
1186 If C_FILE or first time, handle special
1188 Advance esym, sym_hash, csect_hash ptrs. */
1189 if (sym.n_sclass == C_FILE)
1192 *csect_cache = csect;
1193 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1194 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1196 *csect_cache = NULL;
1197 esym += (sym.n_numaux + 1) * symesz;
1198 sym_hash += sym.n_numaux + 1;
1199 csect_cache += sym.n_numaux + 1;
1200 lineno_counts += sym.n_numaux + 1;
1205 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1210 /* If this symbol has line number information attached to it,
1211 and we're not stripping it, count the number of entries and
1212 add them to the count for this csect. In the final link pass
1213 we are going to attach line number information by symbol,
1214 rather than by section, in order to more easily handle
1215 garbage collection. */
1216 if ((info->strip == strip_none || info->strip == strip_some)
1219 && ISFCN (sym.n_type))
1221 union internal_auxent auxlin;
1223 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1224 sym.n_type, sym.n_sclass,
1225 0, sym.n_numaux, (void *) &auxlin);
1227 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1229 asection *enclosing;
1230 bfd_signed_vma linoff;
1232 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1233 if (enclosing == NULL)
1235 (*_bfd_error_handler)
1236 (_("%B: `%s' has line numbers but no enclosing section"),
1238 bfd_set_error (bfd_error_bad_value);
1241 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1242 - enclosing->line_filepos);
1243 /* Explicit cast to bfd_signed_vma for compiler. */
1244 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1246 struct internal_lineno lin;
1247 bfd_byte *linpstart;
1249 linpstart = (reloc_info[enclosing->target_index].linenos
1251 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1253 && ((bfd_size_type) lin.l_addr.l_symndx
1255 - (bfd_byte *) obj_coff_external_syms (abfd))
1258 bfd_byte *linpend, *linp;
1260 linpend = (reloc_info[enclosing->target_index].linenos
1261 + enclosing->lineno_count * linesz);
1262 for (linp = linpstart + linesz;
1266 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1268 if (lin.l_lnno == 0)
1271 *lineno_counts = (linp - linpstart) / linesz;
1272 /* The setting of line_filepos will only be
1273 useful if all the line number entries for a
1274 csect are contiguous; this only matters for
1276 if (csect->line_filepos == 0)
1277 csect->line_filepos =
1278 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1284 /* Pick up the csect auxiliary information. */
1285 if (sym.n_numaux == 0)
1287 (*_bfd_error_handler)
1288 (_("%B: class %d symbol `%s' has no aux entries"),
1289 abfd, sym.n_sclass, name);
1290 bfd_set_error (bfd_error_bad_value);
1294 bfd_coff_swap_aux_in (abfd,
1295 (void *) (esym + symesz * sym.n_numaux),
1296 sym.n_type, sym.n_sclass,
1297 sym.n_numaux - 1, sym.n_numaux,
1300 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1309 (*_bfd_error_handler)
1310 (_("%B: symbol `%s' has unrecognized csect type %d"),
1312 bfd_set_error (bfd_error_bad_value);
1316 /* This is an external reference. */
1317 if (sym.n_sclass == C_HIDEXT
1318 || sym.n_scnum != N_UNDEF
1319 || aux.x_csect.x_scnlen.l != 0)
1321 (*_bfd_error_handler)
1322 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1323 abfd, name, sym.n_sclass, sym.n_scnum,
1324 aux.x_csect.x_scnlen.l);
1325 bfd_set_error (bfd_error_bad_value);
1329 /* An XMC_XO external reference is actually a reference to
1330 an absolute location. */
1331 if (aux.x_csect.x_smclas != XMC_XO)
1332 section = bfd_und_section_ptr;
1335 section = bfd_abs_section_ptr;
1336 value = sym.n_value;
1342 csect_index = -(unsigned) 1;
1344 /* When we see a TOC anchor, we record the TOC value. */
1345 if (aux.x_csect.x_smclas == XMC_TC0)
1347 if (sym.n_sclass != C_HIDEXT
1348 || aux.x_csect.x_scnlen.l != 0)
1350 (*_bfd_error_handler)
1351 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1352 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1353 bfd_set_error (bfd_error_bad_value);
1356 xcoff_data (abfd)->toc = sym.n_value;
1359 /* We must merge TOC entries for the same symbol. We can
1360 merge two TOC entries if they are both C_HIDEXT, they
1361 both have the same name, they are both 4 or 8 bytes long, and
1362 they both have a relocation table entry for an external
1363 symbol with the same name. Unfortunately, this means
1364 that we must look through the relocations. Ick.
1366 Logic for 32 bit vs 64 bit.
1367 32 bit has a csect length of 4 for TOC
1368 64 bit has a csect length of 8 for TOC
1370 The conditions to get past the if-check are not that bad.
1371 They are what is used to create the TOC csects in the first
1373 if (aux.x_csect.x_smclas == XMC_TC
1374 && sym.n_sclass == C_HIDEXT
1375 && info->output_bfd->xvec == abfd->xvec
1376 && ((bfd_xcoff_is_xcoff32 (abfd)
1377 && aux.x_csect.x_scnlen.l == 4)
1378 || (bfd_xcoff_is_xcoff64 (abfd)
1379 && aux.x_csect.x_scnlen.l == 8)))
1381 asection *enclosing;
1382 struct internal_reloc *relocs;
1383 bfd_size_type relindx;
1384 struct internal_reloc *rel;
1386 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1387 if (enclosing == NULL)
1390 relocs = reloc_info[enclosing->target_index].relocs;
1391 amt = enclosing->reloc_count;
1392 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1393 rel = relocs + relindx;
1395 /* 32 bit R_POS r_size is 31
1396 64 bit R_POS r_size is 63 */
1397 if (relindx < enclosing->reloc_count
1398 && rel->r_vaddr == (bfd_vma) sym.n_value
1399 && rel->r_type == R_POS
1400 && ((bfd_xcoff_is_xcoff32 (abfd)
1401 && rel->r_size == 31)
1402 || (bfd_xcoff_is_xcoff64 (abfd)
1403 && rel->r_size == 63)))
1407 struct internal_syment relsym;
1409 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1410 + rel->r_symndx * symesz);
1411 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1412 if (EXTERN_SYM_P (relsym.n_sclass))
1414 const char *relname;
1415 char relbuf[SYMNMLEN + 1];
1417 struct xcoff_link_hash_entry *h;
1419 /* At this point we know that the TOC entry is
1420 for an externally visible symbol. */
1421 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1423 if (relname == NULL)
1426 /* We only merge TOC entries if the TC name is
1427 the same as the symbol name. This handles
1428 the normal case, but not common cases like
1429 SYM.P4 which gcc generates to store SYM + 4
1430 in the TOC. FIXME. */
1431 if (strcmp (name, relname) == 0)
1433 copy = (! info->keep_memory
1434 || relsym._n._n_n._n_zeroes != 0
1435 || relsym._n._n_n._n_offset == 0);
1436 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1437 relname, TRUE, copy,
1442 /* At this point h->root.type could be
1443 bfd_link_hash_new. That should be OK,
1444 since we know for sure that we will come
1445 across this symbol as we step through the
1448 /* We store h in *sym_hash for the
1449 convenience of the relocate_section
1453 if (h->toc_section != NULL)
1455 asection **rel_csects;
1457 /* We already have a TOC entry for this
1458 symbol, so we can just ignore this
1461 reloc_info[enclosing->target_index].csects;
1462 rel_csects[relindx] = bfd_und_section_ptr;
1466 /* We are about to create a TOC entry for
1475 asection *enclosing;
1477 /* We need to create a new section. We get the name from
1478 the csect storage mapping class, so that the linker can
1479 accumulate similar csects together. */
1481 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1485 /* The enclosing section is the main section : .data, .text
1486 or .bss that the csect is coming from. */
1487 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1488 if (enclosing == NULL)
1491 if (! bfd_is_abs_section (enclosing)
1492 && ((bfd_vma) sym.n_value < enclosing->vma
1493 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1494 > enclosing->vma + enclosing->size)))
1496 (*_bfd_error_handler)
1497 (_("%B: csect `%s' not in enclosing section"),
1499 bfd_set_error (bfd_error_bad_value);
1502 csect->vma = sym.n_value;
1503 csect->filepos = (enclosing->filepos
1506 csect->size = aux.x_csect.x_scnlen.l;
1507 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1508 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1510 /* Record the enclosing section in the tdata for this new
1512 amt = sizeof (struct coff_section_tdata);
1513 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1514 if (csect->used_by_bfd == NULL)
1516 amt = sizeof (struct xcoff_section_tdata);
1517 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1518 if (coff_section_data (abfd, csect)->tdata == NULL)
1520 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1521 xcoff_section_data (abfd, csect)->lineno_count =
1522 enclosing->lineno_count;
1524 if (enclosing->owner == abfd)
1526 struct internal_reloc *relocs;
1527 bfd_size_type relindx;
1528 struct internal_reloc *rel;
1529 asection **rel_csect;
1531 relocs = reloc_info[enclosing->target_index].relocs;
1532 amt = enclosing->reloc_count;
1533 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1535 rel = relocs + relindx;
1536 rel_csect = (reloc_info[enclosing->target_index].csects
1539 csect->rel_filepos = (enclosing->rel_filepos
1540 + relindx * bfd_coff_relsz (abfd));
1541 while (relindx < enclosing->reloc_count
1542 && *rel_csect == NULL
1543 && rel->r_vaddr < csect->vma + csect->size)
1547 csect->flags |= SEC_RELOC;
1548 ++csect->reloc_count;
1555 /* There are a number of other fields and section flags
1556 which we do not bother to set. */
1558 csect_index = ((esym
1559 - (bfd_byte *) obj_coff_external_syms (abfd))
1562 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1564 if (first_csect == NULL)
1565 first_csect = csect;
1567 /* If this symbol is external, we treat it as starting at the
1568 beginning of the newly created section. */
1569 if (EXTERN_SYM_P (sym.n_sclass))
1575 /* If this is a TOC section for a symbol, record it. */
1576 if (set_toc != NULL)
1577 set_toc->toc_section = csect;
1582 /* This is a label definition. The x_scnlen field is the
1583 symbol index of the csect. Usually the XTY_LD symbol will
1584 follow its appropriate XTY_SD symbol. The .set pseudo op can
1585 cause the XTY_LD to not follow the XTY_SD symbol. */
1590 if (aux.x_csect.x_scnlen.l < 0
1591 || (aux.x_csect.x_scnlen.l
1592 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1596 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1598 || (section->flags & SEC_HAS_CONTENTS) == 0)
1603 (*_bfd_error_handler)
1604 (_("%B: misplaced XTY_LD `%s'"),
1606 bfd_set_error (bfd_error_bad_value);
1610 value = sym.n_value - csect->vma;
1615 /* This is an unitialized csect. We could base the name on
1616 the storage mapping class, but we don't bother except for
1617 an XMC_TD symbol. If this csect is externally visible,
1618 it is a common symbol. We put XMC_TD symbols in sections
1619 named .tocbss, and rely on the linker script to put that
1622 if (aux.x_csect.x_smclas == XMC_TD)
1624 /* The linker script puts the .td section in the data
1625 section after the .tc section. */
1626 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1630 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1635 csect->vma = sym.n_value;
1636 csect->size = aux.x_csect.x_scnlen.l;
1637 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1638 /* There are a number of other fields and section flags
1639 which we do not bother to set. */
1641 csect_index = ((esym
1642 - (bfd_byte *) obj_coff_external_syms (abfd))
1645 amt = sizeof (struct coff_section_tdata);
1646 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1647 if (csect->used_by_bfd == NULL)
1649 amt = sizeof (struct xcoff_section_tdata);
1650 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1651 if (coff_section_data (abfd, csect)->tdata == NULL)
1653 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1655 if (first_csect == NULL)
1656 first_csect = csect;
1658 if (EXTERN_SYM_P (sym.n_sclass))
1660 csect->flags |= SEC_IS_COMMON;
1663 value = aux.x_csect.x_scnlen.l;
1669 /* Check for magic symbol names. */
1670 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1671 && aux.x_csect.x_smclas != XMC_TC
1672 && aux.x_csect.x_smclas != XMC_TD)
1678 if (strcmp (name, "_text") == 0)
1679 i = XCOFF_SPECIAL_SECTION_TEXT;
1680 else if (strcmp (name, "_etext") == 0)
1681 i = XCOFF_SPECIAL_SECTION_ETEXT;
1682 else if (strcmp (name, "_data") == 0)
1683 i = XCOFF_SPECIAL_SECTION_DATA;
1684 else if (strcmp (name, "_edata") == 0)
1685 i = XCOFF_SPECIAL_SECTION_EDATA;
1686 else if (strcmp (name, "_end") == 0)
1687 i = XCOFF_SPECIAL_SECTION_END;
1689 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1690 i = XCOFF_SPECIAL_SECTION_END2;
1693 xcoff_hash_table (info)->special_sections[i] = csect;
1696 /* Now we have enough information to add the symbol to the
1697 linker hash table. */
1699 if (EXTERN_SYM_P (sym.n_sclass))
1704 BFD_ASSERT (section != NULL);
1706 /* We must copy the name into memory if we got it from the
1707 syment itself, rather than the string table. */
1708 copy = default_copy;
1709 if (sym._n._n_n._n_zeroes != 0
1710 || sym._n._n_n._n_offset == 0)
1713 /* The AIX linker appears to only detect multiple symbol
1714 definitions when there is a reference to the symbol. If
1715 a symbol is defined multiple times, and the only
1716 references are from the same object file, the AIX linker
1717 appears to permit it. It does not merge the different
1718 definitions, but handles them independently. On the
1719 other hand, if there is a reference, the linker reports
1722 This matters because the AIX <net/net_globals.h> header
1723 file actually defines an initialized array, so we have to
1724 actually permit that to work.
1726 Just to make matters even more confusing, the AIX linker
1727 appears to permit multiple symbol definitions whenever
1728 the second definition is in an archive rather than an
1729 object file. This may be a consequence of the manner in
1730 which it handles archives: I think it may load the entire
1731 archive in as separate csects, and then let garbage
1732 collection discard symbols.
1734 We also have to handle the case of statically linking a
1735 shared object, which will cause symbol redefinitions,
1736 although this is an easier case to detect. */
1738 if (info->output_bfd->xvec == abfd->xvec)
1740 if (! bfd_is_und_section (section))
1741 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1742 name, TRUE, copy, FALSE);
1744 /* Make a copy of the symbol name to prevent problems with
1746 *sym_hash = ((struct xcoff_link_hash_entry *)
1747 bfd_wrapped_link_hash_lookup (abfd, info, name,
1748 TRUE, TRUE, FALSE));
1750 if (*sym_hash == NULL)
1752 if (((*sym_hash)->root.type == bfd_link_hash_defined
1753 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1754 && ! bfd_is_und_section (section)
1755 && ! bfd_is_com_section (section))
1757 /* This is a second definition of a defined symbol. */
1758 if ((abfd->flags & DYNAMIC) != 0
1759 && ((*sym_hash)->smclas != XMC_GL
1760 || aux.x_csect.x_smclas == XMC_GL
1761 || ((*sym_hash)->root.u.def.section->owner->flags
1764 /* The new symbol is from a shared library, and
1765 either the existing symbol is not global
1766 linkage code or this symbol is global linkage
1767 code. If the existing symbol is global
1768 linkage code and the new symbol is not, then
1769 we want to use the new symbol. */
1770 section = bfd_und_section_ptr;
1773 else if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1774 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1776 /* The existing symbol is from a shared library.
1778 (*sym_hash)->root.type = bfd_link_hash_undefined;
1779 (*sym_hash)->root.u.undef.abfd =
1780 (*sym_hash)->root.u.def.section->owner;
1782 else if (abfd->my_archive != NULL)
1784 /* This is a redefinition in an object contained
1785 in an archive. Just ignore it. See the
1787 section = bfd_und_section_ptr;
1790 else if (sym.n_sclass == C_AIX_WEAKEXT
1791 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1793 /* At least one of the definitions is weak.
1794 Allow the normal rules to take effect. */
1796 else if ((*sym_hash)->root.u.undef.next != NULL
1797 || info->hash->undefs_tail == &(*sym_hash)->root)
1799 /* This symbol has been referenced. In this
1800 case, we just continue and permit the
1801 multiple definition error. See the comment
1802 above about the behaviour of the AIX linker. */
1804 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1806 /* The symbols are both csects of the same
1807 class. There is at least a chance that this
1808 is a semi-legitimate redefinition. */
1809 section = bfd_und_section_ptr;
1811 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1814 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1815 && (*sym_hash)->root.type == bfd_link_hash_defined
1816 && (bfd_is_und_section (section)
1817 || bfd_is_com_section (section)))
1819 /* This is a reference to a multiply defined symbol.
1820 Report the error now. See the comment above
1821 about the behaviour of the AIX linker. We could
1822 also do this with warning symbols, but I'm not
1823 sure the XCOFF linker is wholly prepared to
1824 handle them, and that would only be a warning,
1826 if (! ((*info->callbacks->multiple_definition)
1827 (info, (*sym_hash)->root.root.string,
1828 NULL, NULL, (bfd_vma) 0,
1829 (*sym_hash)->root.u.def.section->owner,
1830 (*sym_hash)->root.u.def.section,
1831 (*sym_hash)->root.u.def.value)))
1833 /* Try not to give this error too many times. */
1834 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1838 /* _bfd_generic_link_add_one_symbol may call the linker to
1839 generate an error message, and the linker may try to read
1840 the symbol table to give a good error. Right now, the
1841 line numbers are in an inconsistent state, since they are
1842 counted both in the real sections and in the new csects.
1843 We need to leave the count in the real sections so that
1844 the linker can report the line number of the error
1845 correctly, so temporarily clobber the link to the csects
1846 so that the linker will not try to read the line numbers
1847 a second time from the csects. */
1848 BFD_ASSERT (last_real->next == first_csect);
1849 last_real->next = NULL;
1850 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
1851 if (! (_bfd_generic_link_add_one_symbol
1852 (info, abfd, name, flags, section, value,
1854 (struct bfd_link_hash_entry **) sym_hash)))
1856 last_real->next = first_csect;
1858 if (smtyp == XTY_CM)
1860 if ((*sym_hash)->root.type != bfd_link_hash_common
1861 || (*sym_hash)->root.u.c.p->section != csect)
1862 /* We don't need the common csect we just created. */
1865 (*sym_hash)->root.u.c.p->alignment_power
1866 = csect->alignment_power;
1869 if (info->output_bfd->xvec == abfd->xvec)
1873 if (smtyp == XTY_ER || smtyp == XTY_CM)
1874 flag = XCOFF_REF_REGULAR;
1876 flag = XCOFF_DEF_REGULAR;
1877 (*sym_hash)->flags |= flag;
1879 if ((*sym_hash)->smclas == XMC_UA
1880 || flag == XCOFF_DEF_REGULAR)
1881 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1885 if (smtyp == XTY_ER)
1886 *csect_cache = section;
1889 *csect_cache = csect;
1891 xcoff_section_data (abfd, csect)->last_symndx
1892 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
1895 esym += (sym.n_numaux + 1) * symesz;
1896 sym_hash += sym.n_numaux + 1;
1897 csect_cache += sym.n_numaux + 1;
1898 lineno_counts += sym.n_numaux + 1;
1901 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1903 /* Make sure that we have seen all the relocs. */
1904 for (o = abfd->sections; o != first_csect; o = o->next)
1906 /* Reset the section size and the line number count, since the
1907 data is now attached to the csects. Don't reset the size of
1908 the .debug section, since we need to read it below in
1909 bfd_xcoff_size_dynamic_sections. */
1910 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1912 o->lineno_count = 0;
1914 if ((o->flags & SEC_RELOC) != 0)
1917 struct internal_reloc *rel;
1918 asection **rel_csect;
1920 rel = reloc_info[o->target_index].relocs;
1921 rel_csect = reloc_info[o->target_index].csects;
1923 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1925 if (*rel_csect == NULL)
1927 (*_bfd_error_handler)
1928 (_("%B: reloc %s:%d not in csect"),
1930 bfd_set_error (bfd_error_bad_value);
1934 /* We identify all function symbols that are the target
1935 of a relocation, so that we can create glue code for
1936 functions imported from dynamic objects. */
1937 if (info->output_bfd->xvec == abfd->xvec
1938 && *rel_csect != bfd_und_section_ptr
1939 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1941 struct xcoff_link_hash_entry *h;
1943 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1944 /* If the symbol name starts with a period, it is
1945 the code of a function. If the symbol is
1946 currently undefined, then add an undefined symbol
1947 for the function descriptor. This should do no
1948 harm, because any regular object that defines the
1949 function should also define the function
1950 descriptor. It helps, because it means that we
1951 will identify the function descriptor with a
1952 dynamic object if a dynamic object defines it. */
1953 if (h->root.root.string[0] == '.'
1954 && h->descriptor == NULL)
1956 struct xcoff_link_hash_entry *hds;
1957 struct bfd_link_hash_entry *bh;
1959 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1960 h->root.root.string + 1,
1964 if (hds->root.type == bfd_link_hash_new)
1967 if (! (_bfd_generic_link_add_one_symbol
1968 (info, abfd, hds->root.root.string,
1969 (flagword) 0, bfd_und_section_ptr,
1970 (bfd_vma) 0, NULL, FALSE,
1973 hds = (struct xcoff_link_hash_entry *) bh;
1975 hds->flags |= XCOFF_DESCRIPTOR;
1976 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
1977 hds->descriptor = h;
1978 h->descriptor = hds;
1980 if (h->root.root.string[0] == '.')
1981 h->flags |= XCOFF_CALLED;
1985 free (reloc_info[o->target_index].csects);
1986 reloc_info[o->target_index].csects = NULL;
1988 /* Reset SEC_RELOC and the reloc_count, since the reloc
1989 information is now attached to the csects. */
1990 o->flags &=~ SEC_RELOC;
1993 /* If we are not keeping memory, free the reloc information. */
1994 if (! info->keep_memory
1995 && coff_section_data (abfd, o) != NULL
1996 && coff_section_data (abfd, o)->relocs != NULL
1997 && ! coff_section_data (abfd, o)->keep_relocs)
1999 free (coff_section_data (abfd, o)->relocs);
2000 coff_section_data (abfd, o)->relocs = NULL;
2004 /* Free up the line numbers. FIXME: We could cache these
2005 somewhere for the final link, to avoid reading them again. */
2006 if (reloc_info[o->target_index].linenos != NULL)
2008 free (reloc_info[o->target_index].linenos);
2009 reloc_info[o->target_index].linenos = NULL;
2015 obj_coff_keep_syms (abfd) = keep_syms;
2020 if (reloc_info != NULL)
2022 for (o = abfd->sections; o != NULL; o = o->next)
2024 if (reloc_info[o->target_index].csects != NULL)
2025 free (reloc_info[o->target_index].csects);
2026 if (reloc_info[o->target_index].linenos != NULL)
2027 free (reloc_info[o->target_index].linenos);
2031 obj_coff_keep_syms (abfd) = keep_syms;
2038 /* Add symbols from an XCOFF object file. */
2041 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2043 if (! _bfd_coff_get_external_symbols (abfd))
2045 if (! xcoff_link_add_symbols (abfd, info))
2047 if (! info->keep_memory)
2049 if (! _bfd_coff_free_symbols (abfd))
2055 /* Look through the loader symbols to see if this dynamic object
2056 should be included in the link. The native linker uses the loader
2057 symbols, not the normal symbol table, so we do too. */
2060 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2061 struct bfd_link_info *info,
2062 bfd_boolean *pneeded)
2066 struct internal_ldhdr ldhdr;
2067 const char *strings;
2068 bfd_byte *elsym, *elsymend;
2072 lsec = bfd_get_section_by_name (abfd, ".loader");
2074 /* There are no symbols, so don't try to include it. */
2077 if (! xcoff_get_section_contents (abfd, lsec))
2079 contents = coff_section_data (abfd, lsec)->contents;
2081 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2083 strings = (char *) contents + ldhdr.l_stoff;
2085 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2087 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2088 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2090 struct internal_ldsym ldsym;
2091 char nambuf[SYMNMLEN + 1];
2093 struct bfd_link_hash_entry *h;
2095 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2097 /* We are only interested in exported symbols. */
2098 if ((ldsym.l_smtype & L_EXPORT) == 0)
2101 if (ldsym._l._l_l._l_zeroes == 0)
2102 name = strings + ldsym._l._l_l._l_offset;
2105 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2106 nambuf[SYMNMLEN] = '\0';
2110 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2112 /* We are only interested in symbols that are currently
2113 undefined. At this point we know that we are using an XCOFF
2116 && h->type == bfd_link_hash_undefined
2117 && (((struct xcoff_link_hash_entry *) h)->flags
2118 & XCOFF_DEF_DYNAMIC) == 0)
2120 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2127 /* We do not need this shared object. */
2128 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2130 free (coff_section_data (abfd, lsec)->contents);
2131 coff_section_data (abfd, lsec)->contents = NULL;
2137 /* Look through the symbols to see if this object file should be
2138 included in the link. */
2141 xcoff_link_check_ar_symbols (bfd *abfd,
2142 struct bfd_link_info *info,
2143 bfd_boolean *pneeded)
2145 bfd_size_type symesz;
2151 if ((abfd->flags & DYNAMIC) != 0
2152 && ! info->static_link
2153 && info->output_bfd->xvec == abfd->xvec)
2154 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2156 symesz = bfd_coff_symesz (abfd);
2157 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2158 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2159 while (esym < esym_end)
2161 struct internal_syment sym;
2163 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2165 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2168 char buf[SYMNMLEN + 1];
2169 struct bfd_link_hash_entry *h;
2171 /* This symbol is externally visible, and is defined by this
2173 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2177 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2179 /* We are only interested in symbols that are currently
2180 undefined. If a symbol is currently known to be common,
2181 XCOFF linkers do not bring in an object file which
2182 defines it. We also don't bring in symbols to satisfy
2183 undefined references in shared objects. */
2185 && h->type == bfd_link_hash_undefined
2186 && (info->output_bfd->xvec != abfd->xvec
2187 || (((struct xcoff_link_hash_entry *) h)->flags
2188 & XCOFF_DEF_DYNAMIC) == 0))
2190 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2197 esym += (sym.n_numaux + 1) * symesz;
2200 /* We do not need this object file. */
2204 /* Check a single archive element to see if we need to include it in
2205 the link. *PNEEDED is set according to whether this element is
2206 needed in the link or not. This is called via
2207 _bfd_generic_link_add_archive_symbols. */
2210 xcoff_link_check_archive_element (bfd *abfd,
2211 struct bfd_link_info *info,
2212 bfd_boolean *pneeded)
2214 bfd_boolean keep_syms_p;
2216 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2217 if (! _bfd_coff_get_external_symbols (abfd))
2220 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2225 if (! xcoff_link_add_symbols (abfd, info))
2227 if (info->keep_memory)
2233 if (! _bfd_coff_free_symbols (abfd))
2240 /* Given an XCOFF BFD, add symbols to the global hash table as
2244 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2246 switch (bfd_get_format (abfd))
2249 return xcoff_link_add_object_symbols (abfd, info);
2252 /* If the archive has a map, do the usual search. We then need
2253 to check the archive for dynamic objects, because they may not
2254 appear in the archive map even though they should, perhaps, be
2255 included. If the archive has no map, we just consider each object
2256 file in turn, since that apparently is what the AIX native linker
2258 if (bfd_has_map (abfd))
2260 if (! (_bfd_generic_link_add_archive_symbols
2261 (abfd, info, xcoff_link_check_archive_element)))
2268 member = bfd_openr_next_archived_file (abfd, NULL);
2269 while (member != NULL)
2271 if (bfd_check_format (member, bfd_object)
2272 && (info->output_bfd->xvec == member->xvec)
2273 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2277 if (! xcoff_link_check_archive_element (member, info,
2281 member->archive_pass = -1;
2283 member = bfd_openr_next_archived_file (abfd, member);
2290 bfd_set_error (bfd_error_wrong_format);
2295 /* If symbol H has not been interpreted as a function descriptor,
2296 see whether it should be. Set up its descriptor information if so. */
2299 xcoff_find_function (struct bfd_link_info *info,
2300 struct xcoff_link_hash_entry *h)
2302 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2303 && h->root.root.string[0] != '.')
2306 struct xcoff_link_hash_entry *hfn;
2309 amt = strlen (h->root.root.string) + 2;
2310 fnname = bfd_malloc (amt);
2314 strcpy (fnname + 1, h->root.root.string);
2315 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2316 fnname, FALSE, FALSE, TRUE);
2319 && hfn->smclas == XMC_PR
2320 && (hfn->root.type == bfd_link_hash_defined
2321 || hfn->root.type == bfd_link_hash_defweak))
2323 h->flags |= XCOFF_DESCRIPTOR;
2324 h->descriptor = hfn;
2325 hfn->descriptor = h;
2331 /* H is an imported symbol. Set the import module's path, file and member
2332 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
2333 no specific import module is specified. */
2336 xcoff_set_import_path (struct bfd_link_info *info,
2337 struct xcoff_link_hash_entry *h,
2338 const char *imppath, const char *impfile,
2339 const char *impmember)
2342 struct xcoff_import_file **pp;
2344 /* We overload the ldindx field to hold the l_ifile value for this
2346 BFD_ASSERT (h->ldsym == NULL);
2347 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2348 if (imppath == NULL)
2352 /* We start c at 1 because the first entry in the import list is
2353 reserved for the library search path. */
2354 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2356 pp = &(*pp)->next, ++c)
2358 if (strcmp ((*pp)->path, imppath) == 0
2359 && strcmp ((*pp)->file, impfile) == 0
2360 && strcmp ((*pp)->member, impmember) == 0)
2366 struct xcoff_import_file *n;
2367 bfd_size_type amt = sizeof (* n);
2369 n = bfd_alloc (info->output_bfd, amt);
2375 n->member = impmember;
2383 /* Return true if the given bfd contains at least one shared object. */
2386 xcoff_archive_contains_shared_object_p (bfd *archive)
2390 member = bfd_openr_next_archived_file (archive, NULL);
2391 while (member != NULL && (member->flags & DYNAMIC) == 0)
2392 member = bfd_openr_next_archived_file (archive, member);
2393 return member != NULL;
2396 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2397 qualifies for export by -bexpall. */
2400 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2402 /* Exclude symbols beginning with '_'. */
2403 if (h->root.root.string[0] == '_')
2406 /* Exclude archive members that would otherwise be unreferenced. */
2407 if ((h->flags & XCOFF_MARK) == 0
2408 && (h->root.type == bfd_link_hash_defined
2409 || h->root.type == bfd_link_hash_defweak)
2410 && h->root.u.def.section->owner != NULL
2411 && h->root.u.def.section->owner->my_archive != NULL)
2417 /* Return true if symbol H qualifies for the forms of automatic export
2418 specified by AUTO_EXPORT_FLAGS. */
2421 xcoff_auto_export_p (struct xcoff_link_hash_entry *h,
2422 unsigned int auto_export_flags)
2424 /* Don't automatically export things that were explicitly exported. */
2425 if ((h->flags & XCOFF_EXPORT) != 0)
2428 /* Don't export things that we don't define. */
2429 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2432 /* Don't export functions; export their descriptors instead. */
2433 if (h->root.root.string[0] == '.')
2436 /* We don't export a symbol which is being defined by an object
2437 included from an archive which contains a shared object. The
2438 rationale is that if an archive contains both an unshared and
2439 a shared object, then there must be some reason that the
2440 unshared object is unshared, and we don't want to start
2441 providing a shared version of it. In particular, this solves
2442 a bug involving the _savefNN set of functions. gcc will call
2443 those functions without providing a slot to restore the TOC,
2444 so it is essential that these functions be linked in directly
2445 and not from a shared object, which means that a shared
2446 object which also happens to link them in must not export
2447 them. This is confusing, but I haven't been able to think of
2448 a different approach. Note that the symbols can, of course,
2449 be exported explicitly. */
2450 if (h->root.type == bfd_link_hash_defined
2451 || h->root.type == bfd_link_hash_defweak)
2455 owner = h->root.u.def.section->owner;
2457 && owner->my_archive != NULL
2458 && xcoff_archive_contains_shared_object_p (owner->my_archive))
2462 /* Otherwise, all symbols are exported by -bexpfull. */
2463 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2466 /* Despite its name, -bexpall exports most but not all symbols. */
2467 if ((auto_export_flags & XCOFF_EXPALL) != 0
2468 && xcoff_covered_by_expall_p (h))
2474 /* Return true if relocation REL needs to be copied to the .loader section.
2475 If REL is against a global symbol, H is that symbol, otherwise it
2479 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2480 struct xcoff_link_hash_entry *h)
2482 if (!xcoff_hash_table (info)->loader_section)
2485 switch (rel->r_type)
2492 /* We should never need a .loader reloc for a TOC-relative reloc. */
2496 /* In this case, relocations against defined symbols can be resolved
2499 || h->root.type == bfd_link_hash_defined
2500 || h->root.type == bfd_link_hash_defweak
2501 || h->root.type == bfd_link_hash_common)
2504 /* We will always provide a local definition of function symbols,
2505 even if we don't have one yet. */
2506 if ((h->flags & XCOFF_CALLED) != 0)
2515 /* Absolute relocations against absolute symbols can be
2516 resolved statically. */
2518 && (h->root.type == bfd_link_hash_defined
2519 || h->root.type == bfd_link_hash_defweak)
2520 && bfd_is_abs_section (h->root.u.def.section))
2527 /* Mark a symbol as not being garbage, including the section in which
2530 static inline bfd_boolean
2531 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2533 if ((h->flags & XCOFF_MARK) != 0)
2536 h->flags |= XCOFF_MARK;
2538 /* If we're marking an undefined symbol, try find some way of
2540 if (!info->relocatable
2541 && (h->flags & XCOFF_IMPORT) == 0
2542 && (h->flags & XCOFF_DEF_REGULAR) == 0
2543 && (h->root.type == bfd_link_hash_undefined
2544 || h->root.type == bfd_link_hash_undefweak))
2546 /* First check whether this symbol can be interpreted as an
2547 undefined function descriptor for a defined function symbol. */
2548 if (!xcoff_find_function (info, h))
2551 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2552 && (h->descriptor->root.type == bfd_link_hash_defined
2553 || h->descriptor->root.type == bfd_link_hash_defweak))
2555 /* This is a descriptor for a defined symbol, but the input
2556 objects have not defined the descriptor itself. Fill in
2557 the definition automatically.
2559 Note that we do this even if we found a dynamic definition
2560 of H. The local function definition logically overrides
2564 sec = xcoff_hash_table (info)->descriptor_section;
2565 h->root.type = bfd_link_hash_defined;
2566 h->root.u.def.section = sec;
2567 h->root.u.def.value = sec->size;
2569 h->flags |= XCOFF_DEF_REGULAR;
2571 /* The size of the function descriptor depends on whether this
2572 is xcoff32 (12) or xcoff64 (24). */
2573 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2575 /* A function descriptor uses two relocs: one for the
2576 associated code, and one for the TOC address. */
2577 xcoff_hash_table (info)->ldrel_count += 2;
2578 sec->reloc_count += 2;
2580 /* Mark the function itself. */
2581 if (!xcoff_mark_symbol (info, h->descriptor))
2584 /* Mark the TOC section, so that we get an anchor
2585 to relocate against. */
2586 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2589 /* We handle writing out the contents of the descriptor in
2590 xcoff_write_global_symbol. */
2592 else if ((h->flags & XCOFF_CALLED) != 0)
2594 /* This is a function symbol for which we need to create
2597 struct xcoff_link_hash_entry *hds;
2599 /* Mark the descriptor (and its TOC section). */
2600 hds = h->descriptor;
2601 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2602 || hds->root.type == bfd_link_hash_undefweak)
2603 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2604 if (!xcoff_mark_symbol (info, hds))
2607 /* Treat this symbol as undefined if the descriptor was. */
2608 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2609 h->flags |= XCOFF_WAS_UNDEFINED;
2611 /* Allocate room for the global linkage code itself. */
2612 sec = xcoff_hash_table (info)->linkage_section;
2613 h->root.type = bfd_link_hash_defined;
2614 h->root.u.def.section = sec;
2615 h->root.u.def.value = sec->size;
2617 h->flags |= XCOFF_DEF_REGULAR;
2618 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2620 /* The global linkage code requires a TOC entry for the
2622 if (hds->toc_section == NULL)
2627 xcoff32 uses 4 bytes in the toc.
2628 xcoff64 uses 8 bytes in the toc. */
2629 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2631 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2636 /* Allocate room in the fallback TOC section. */
2637 hds->toc_section = xcoff_hash_table (info)->toc_section;
2638 hds->u.toc_offset = hds->toc_section->size;
2639 hds->toc_section->size += byte_size;
2640 if (!xcoff_mark (info, hds->toc_section))
2643 /* Allocate room for a static and dynamic R_TOC
2645 ++xcoff_hash_table (info)->ldrel_count;
2646 ++hds->toc_section->reloc_count;
2648 /* Set the index to -2 to force this symbol to
2651 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2654 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2656 /* Record that the symbol was undefined, then import it.
2657 -brtl links use a special fake import file. */
2658 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2659 if (xcoff_hash_table (info)->rtld)
2661 if (!xcoff_set_import_path (info, h, "", "..", ""))
2666 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2672 if (h->root.type == bfd_link_hash_defined
2673 || h->root.type == bfd_link_hash_defweak)
2677 hsec = h->root.u.def.section;
2678 if (! bfd_is_abs_section (hsec)
2679 && (hsec->flags & SEC_MARK) == 0)
2681 if (! xcoff_mark (info, hsec))
2686 if (h->toc_section != NULL
2687 && (h->toc_section->flags & SEC_MARK) == 0)
2689 if (! xcoff_mark (info, h->toc_section))
2696 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2697 If the symbol exists, set FLAGS. */
2700 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2701 const char *name, unsigned int flags)
2703 struct xcoff_link_hash_entry *h;
2705 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2706 FALSE, FALSE, TRUE);
2710 if (h->root.type == bfd_link_hash_defined
2711 || h->root.type == bfd_link_hash_defweak)
2713 if (!xcoff_mark (info, h->root.u.def.section))
2720 /* The mark phase of garbage collection. For a given section, mark
2721 it, and all the sections which define symbols to which it refers.
2722 Because this function needs to look at the relocs, we also count
2723 the number of relocs which need to be copied into the .loader
2727 xcoff_mark (struct bfd_link_info *info, asection *sec)
2729 if (bfd_is_abs_section (sec)
2730 || (sec->flags & SEC_MARK) != 0)
2733 sec->flags |= SEC_MARK;
2735 if (sec->owner->xvec == info->output_bfd->xvec
2736 && coff_section_data (sec->owner, sec) != NULL
2737 && xcoff_section_data (sec->owner, sec) != NULL)
2739 struct xcoff_link_hash_entry **syms;
2740 struct internal_reloc *rel, *relend;
2742 unsigned long i, first, last;
2744 /* Mark all the symbols in this section. */
2745 syms = obj_xcoff_sym_hashes (sec->owner);
2746 csects = xcoff_data (sec->owner)->csects;
2747 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2748 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2749 for (i = first; i <= last; i++)
2750 if (csects[i] == sec
2752 && (syms[i]->flags & XCOFF_MARK) == 0)
2754 if (!xcoff_mark_symbol (info, syms[i]))
2758 /* Look through the section relocs. */
2759 if ((sec->flags & SEC_RELOC) != 0
2760 && sec->reloc_count > 0)
2762 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2766 relend = rel + sec->reloc_count;
2767 for (; rel < relend; rel++)
2769 struct xcoff_link_hash_entry *h;
2771 if ((unsigned int) rel->r_symndx
2772 > obj_raw_syment_count (sec->owner))
2775 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2778 if ((h->flags & XCOFF_MARK) == 0)
2780 if (!xcoff_mark_symbol (info, h))
2788 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2790 && (rsec->flags & SEC_MARK) == 0)
2792 if (!xcoff_mark (info, rsec))
2797 /* See if this reloc needs to be copied into the .loader
2799 if (xcoff_need_ldrel_p (info, rel, h))
2801 ++xcoff_hash_table (info)->ldrel_count;
2803 h->flags |= XCOFF_LDREL;
2807 if (! info->keep_memory
2808 && coff_section_data (sec->owner, sec) != NULL
2809 && coff_section_data (sec->owner, sec)->relocs != NULL
2810 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2812 free (coff_section_data (sec->owner, sec)->relocs);
2813 coff_section_data (sec->owner, sec)->relocs = NULL;
2821 /* Routines that are called after all the input files have been
2822 handled, but before the sections are laid out in memory. */
2824 /* The sweep phase of garbage collection. Remove all garbage
2828 xcoff_sweep (struct bfd_link_info *info)
2832 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2836 for (o = sub->sections; o != NULL; o = o->next)
2838 if ((o->flags & SEC_MARK) == 0)
2840 /* Keep all sections from non-XCOFF input files. Keep
2841 special sections. Keep .debug sections for the
2843 if (sub->xvec != info->output_bfd->xvec
2844 || o == xcoff_hash_table (info)->debug_section
2845 || o == xcoff_hash_table (info)->loader_section
2846 || o == xcoff_hash_table (info)->linkage_section
2847 || o == xcoff_hash_table (info)->descriptor_section
2848 || strcmp (o->name, ".debug") == 0)
2849 o->flags |= SEC_MARK;
2860 /* Record the number of elements in a set. This is used to output the
2861 correct csect length. */
2864 bfd_xcoff_link_record_set (bfd *output_bfd,
2865 struct bfd_link_info *info,
2866 struct bfd_link_hash_entry *harg,
2869 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2870 struct xcoff_link_size_list *n;
2873 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2876 /* This will hardly ever be called. I don't want to burn four bytes
2877 per global symbol, so instead the size is kept on a linked list
2878 attached to the hash table. */
2880 n = bfd_alloc (output_bfd, amt);
2883 n->next = xcoff_hash_table (info)->size_list;
2886 xcoff_hash_table (info)->size_list = n;
2888 h->flags |= XCOFF_HAS_SIZE;
2893 /* Import a symbol. */
2896 bfd_xcoff_import_symbol (bfd *output_bfd,
2897 struct bfd_link_info *info,
2898 struct bfd_link_hash_entry *harg,
2900 const char *imppath,
2901 const char *impfile,
2902 const char *impmember,
2903 unsigned int syscall_flag)
2905 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2907 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2910 /* A symbol name which starts with a period is the code for a
2911 function. If the symbol is undefined, then add an undefined
2912 symbol for the function descriptor, and import that instead. */
2913 if (h->root.root.string[0] == '.'
2914 && h->root.type == bfd_link_hash_undefined
2915 && val == (bfd_vma) -1)
2917 struct xcoff_link_hash_entry *hds;
2919 hds = h->descriptor;
2922 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2923 h->root.root.string + 1,
2927 if (hds->root.type == bfd_link_hash_new)
2929 hds->root.type = bfd_link_hash_undefined;
2930 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2932 hds->flags |= XCOFF_DESCRIPTOR;
2933 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2934 hds->descriptor = h;
2935 h->descriptor = hds;
2938 /* Now, if the descriptor is undefined, import the descriptor
2939 rather than the symbol we were told to import. FIXME: Is
2940 this correct in all cases? */
2941 if (hds->root.type == bfd_link_hash_undefined)
2945 h->flags |= (XCOFF_IMPORT | syscall_flag);
2947 if (val != (bfd_vma) -1)
2949 if (h->root.type == bfd_link_hash_defined
2950 && (! bfd_is_abs_section (h->root.u.def.section)
2951 || h->root.u.def.value != val))
2953 if (! ((*info->callbacks->multiple_definition)
2954 (info, h->root.root.string, h->root.u.def.section->owner,
2955 h->root.u.def.section, h->root.u.def.value,
2956 output_bfd, bfd_abs_section_ptr, val)))
2960 h->root.type = bfd_link_hash_defined;
2961 h->root.u.def.section = bfd_abs_section_ptr;
2962 h->root.u.def.value = val;
2966 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
2972 /* Export a symbol. */
2975 bfd_xcoff_export_symbol (bfd *output_bfd,
2976 struct bfd_link_info *info,
2977 struct bfd_link_hash_entry *harg)
2979 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2981 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2984 h->flags |= XCOFF_EXPORT;
2986 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2987 I'm just going to ignore it until somebody explains it. */
2989 /* Make sure we don't garbage collect this symbol. */
2990 if (! xcoff_mark_symbol (info, h))
2993 /* If this is a function descriptor, make sure we don't garbage
2994 collect the associated function code. We normally don't have to
2995 worry about this, because the descriptor will be attached to a
2996 section with relocs, but if we are creating the descriptor
2997 ourselves those relocs will not be visible to the mark code. */
2998 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3000 if (! xcoff_mark_symbol (info, h->descriptor))
3007 /* Count a reloc against a symbol. This is called for relocs
3008 generated by the linker script, typically for global constructors
3012 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3013 struct bfd_link_info *info,
3016 struct xcoff_link_hash_entry *h;
3018 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3021 h = ((struct xcoff_link_hash_entry *)
3022 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3026 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3027 bfd_set_error (bfd_error_no_symbols);
3031 h->flags |= XCOFF_REF_REGULAR;
3032 if (xcoff_hash_table (info)->loader_section)
3034 h->flags |= XCOFF_LDREL;
3035 ++xcoff_hash_table (info)->ldrel_count;
3038 /* Mark the symbol to avoid garbage collection. */
3039 if (! xcoff_mark_symbol (info, h))
3045 /* This function is called for each symbol to which the linker script
3049 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3050 struct bfd_link_info *info,
3053 struct xcoff_link_hash_entry *h;
3055 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3058 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3063 h->flags |= XCOFF_DEF_REGULAR;
3068 /* An xcoff_link_hash_traverse callback for which DATA points to an
3069 xcoff_loader_info. Mark all symbols that should be automatically
3073 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3075 struct xcoff_loader_info *ldinfo;
3077 ldinfo = (struct xcoff_loader_info *) data;
3078 if (xcoff_auto_export_p (h, ldinfo->auto_export_flags))
3080 if (!xcoff_mark_symbol (ldinfo->info, h))
3081 ldinfo->failed = TRUE;
3086 /* Add a symbol to the .loader symbols, if necessary. */
3088 /* INPUT_BFD has an external symbol associated with hash table entry H
3089 and csect CSECT. Return true if INPUT_BFD defines H. */
3092 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3095 switch (h->root.type)
3097 case bfd_link_hash_defined:
3098 case bfd_link_hash_defweak:
3099 /* No input bfd owns absolute symbols. They are written by
3100 xcoff_write_global_symbol instead. */
3101 return (!bfd_is_abs_section (csect)
3102 && h->root.u.def.section == csect);
3104 case bfd_link_hash_common:
3105 return h->root.u.c.p->section->owner == input_bfd;
3107 case bfd_link_hash_undefined:
3108 case bfd_link_hash_undefweak:
3109 /* We can't treat undef.abfd as the owner because that bfd
3110 might be a dynamic object. Allow any bfd to claim it. */
3118 /* See if H should have a loader symbol associated with it. */
3121 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3122 struct xcoff_link_hash_entry *h)
3126 /* Warn if this symbol is exported but not defined. */
3127 if ((h->flags & XCOFF_EXPORT) != 0
3128 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3130 (*_bfd_error_handler)
3131 (_("warning: attempt to export undefined symbol `%s'"),
3132 h->root.root.string);
3136 /* We need to add a symbol to the .loader section if it is mentioned
3137 in a reloc which we are copying to the .loader section and it was
3138 not defined or common, or if it is the entry point, or if it is
3140 if (((h->flags & XCOFF_LDREL) == 0
3141 || h->root.type == bfd_link_hash_defined
3142 || h->root.type == bfd_link_hash_defweak
3143 || h->root.type == bfd_link_hash_common)
3144 && (h->flags & XCOFF_ENTRY) == 0
3145 && (h->flags & XCOFF_EXPORT) == 0)
3148 /* We need to add this symbol to the .loader symbols. */
3150 BFD_ASSERT (h->ldsym == NULL);
3151 amt = sizeof (struct internal_ldsym);
3152 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3153 if (h->ldsym == NULL)
3155 ldinfo->failed = TRUE;
3159 if ((h->flags & XCOFF_IMPORT) != 0)
3161 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3162 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3164 h->ldsym->l_ifile = h->ldindx;
3167 /* The first 3 symbol table indices are reserved to indicate the
3168 data, text and bss sections. */
3169 h->ldindx = ldinfo->ldsym_count + 3;
3171 ++ldinfo->ldsym_count;
3173 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3174 h->ldsym, h->root.root.string))
3177 h->flags |= XCOFF_BUILT_LDSYM;
3181 /* An xcoff_htab_traverse callback that is called for each symbol
3182 once garbage collection is complete. */
3185 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3187 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3189 if (h->root.type == bfd_link_hash_warning)
3190 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3192 /* __rtinit, this symbol has special handling. */
3193 if (h->flags & XCOFF_RTINIT)
3196 /* If this is a final link, and the symbol was defined as a common
3197 symbol in a regular object file, and there was no definition in
3198 any dynamic object, then the linker will have allocated space for
3199 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3200 will not have been set. */
3201 if (h->root.type == bfd_link_hash_defined
3202 && (h->flags & XCOFF_DEF_REGULAR) == 0
3203 && (h->flags & XCOFF_REF_REGULAR) != 0
3204 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3205 && (bfd_is_abs_section (h->root.u.def.section)
3206 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3207 h->flags |= XCOFF_DEF_REGULAR;
3209 /* We don't want to garbage collect symbols which are not defined in
3210 XCOFF files. This is a convenient place to mark them. */
3211 if (xcoff_hash_table (ldinfo->info)->gc
3212 && (h->flags & XCOFF_MARK) == 0
3213 && (h->root.type == bfd_link_hash_defined
3214 || h->root.type == bfd_link_hash_defweak)
3215 && (h->root.u.def.section->owner == NULL
3216 || (h->root.u.def.section->owner->xvec
3217 != ldinfo->info->output_bfd->xvec)))
3218 h->flags |= XCOFF_MARK;
3220 /* Skip discarded symbols. */
3221 if (xcoff_hash_table (ldinfo->info)->gc
3222 && (h->flags & XCOFF_MARK) == 0)
3225 /* If this is still a common symbol, and it wasn't garbage
3226 collected, we need to actually allocate space for it in the .bss
3228 if (h->root.type == bfd_link_hash_common
3229 && h->root.u.c.p->section->size == 0)
3231 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3232 h->root.u.c.p->section->size = h->root.u.c.size;
3235 if (xcoff_hash_table (ldinfo->info)->loader_section)
3237 if (xcoff_auto_export_p (h, ldinfo->auto_export_flags))
3238 h->flags |= XCOFF_EXPORT;
3240 if (!xcoff_build_ldsym (ldinfo, h))
3247 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3248 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3249 csect information, if any. NAME is the function's name if the name
3250 is stored in the .debug section, otherwise it is null.
3252 Return 1 if we should include an appropriately-adjusted ISYM
3253 in the output file, 0 if we should discard ISYM, or -1 if an
3257 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3258 struct internal_syment *isym,
3259 union internal_auxent *aux,
3260 struct xcoff_link_hash_entry *h,
3261 asection *csect, const char *name)
3265 /* If we are skipping this csect, we want to strip the symbol too. */
3269 /* Likewise if we garbage-collected the csect. */
3270 if (xcoff_hash_table (info)->gc
3271 && !bfd_is_abs_section (csect)
3272 && !bfd_is_und_section (csect)
3273 && (csect->flags & SEC_MARK) == 0)
3276 /* An XCOFF linker always removes C_STAT symbols. */
3277 if (isym->n_sclass == C_STAT)
3280 /* We generate the TOC anchor separately. */
3281 if (isym->n_sclass == C_HIDEXT
3282 && aux->x_csect.x_smclas == XMC_TC0)
3285 /* If we are stripping all symbols, we want to discard this one. */
3286 if (info->strip == strip_all)
3289 /* Discard symbols that are defined elsewhere. */
3290 if (EXTERN_SYM_P (isym->n_sclass))
3292 if ((h->flags & XCOFF_ALLOCATED) != 0)
3294 if (!xcoff_final_definition_p (input_bfd, h, csect))
3298 /* If we're discarding local symbols, check whether ISYM is local. */
3299 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3300 if (info->discard == discard_all
3301 && !EXTERN_SYM_P (isym->n_sclass)
3302 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3305 /* If we're stripping debugging symbols, check whether ISYM is one. */
3306 if (info->strip == strip_debugger
3307 && isym->n_scnum == N_DEBUG)
3310 /* If we are stripping symbols based on name, check how ISYM's
3311 name should be handled. */
3312 if (info->strip == strip_some
3313 || info->discard == discard_l)
3315 char buf[SYMNMLEN + 1];
3319 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3324 if (info->strip == strip_some
3325 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3328 if (info->discard == discard_l
3329 && !EXTERN_SYM_P (isym->n_sclass)
3330 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3331 && bfd_is_local_label_name (input_bfd, name))
3338 /* Lay out the .loader section, filling in the header and the import paths.
3339 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3342 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3343 const char *libpath)
3346 struct xcoff_link_hash_table *htab;
3347 struct internal_ldhdr *ldhdr;
3348 struct xcoff_import_file *fl;
3349 bfd_size_type stoff;
3350 size_t impsize, impcount;
3354 /* Work out the size of the import file names. Each import file ID
3355 consists of three null terminated strings: the path, the file
3356 name, and the archive member name. The first entry in the list
3357 of names is the path to use to find objects, which the linker has
3358 passed in as the libpath argument. For some reason, the path
3359 entry in the other import file names appears to always be empty. */
3360 output_bfd = ldinfo->output_bfd;
3361 htab = xcoff_hash_table (ldinfo->info);
3362 impsize = strlen (libpath) + 3;
3364 for (fl = htab->imports; fl != NULL; fl = fl->next)
3367 impsize += (strlen (fl->path)
3369 + strlen (fl->member)
3373 /* Set up the .loader section header. */
3374 ldhdr = &htab->ldhdr;
3375 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3376 ldhdr->l_nsyms = ldinfo->ldsym_count;
3377 ldhdr->l_nreloc = htab->ldrel_count;
3378 ldhdr->l_istlen = impsize;
3379 ldhdr->l_nimpid = impcount;
3380 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3381 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3382 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3383 ldhdr->l_stlen = ldinfo->string_size;
3384 stoff = ldhdr->l_impoff + impsize;
3385 if (ldinfo->string_size == 0)
3388 ldhdr->l_stoff = stoff;
3390 /* 64 bit elements to ldhdr
3391 The swap out routine for 32 bit will ignore them.
3392 Nothing fancy, symbols come after the header and relocs come
3394 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3395 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3396 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3398 /* We now know the final size of the .loader section. Allocate
3400 lsec = htab->loader_section;
3401 lsec->size = stoff + ldhdr->l_stlen;
3402 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3403 if (lsec->contents == NULL)
3406 /* Set up the header. */
3407 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3409 /* Set up the import file names. */
3410 out = (char *) lsec->contents + ldhdr->l_impoff;
3411 strcpy (out, libpath);
3412 out += strlen (libpath) + 1;
3415 for (fl = htab->imports; fl != NULL; fl = fl->next)
3420 while ((*out++ = *s++) != '\0')
3423 while ((*out++ = *s++) != '\0')
3426 while ((*out++ = *s++) != '\0')
3430 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3432 /* Set up the symbol string table. */
3433 if (ldinfo->string_size > 0)
3435 memcpy (out, ldinfo->strings, ldinfo->string_size);
3436 free (ldinfo->strings);
3437 ldinfo->strings = NULL;
3440 /* We can't set up the symbol table or the relocs yet, because we
3441 don't yet know the final position of the various sections. The
3442 .loader symbols are written out when the corresponding normal
3443 symbols are written out in xcoff_link_input_bfd or
3444 xcoff_write_global_symbol. The .loader relocs are written out
3445 when the corresponding normal relocs are handled in
3446 xcoff_link_input_bfd. */
3451 /* Build the .loader section. This is called by the XCOFF linker
3452 emulation before_allocation routine. We must set the size of the
3453 .loader section before the linker lays out the output file.
3454 LIBPATH is the library path to search for shared objects; this is
3455 normally built from the -L arguments passed to the linker. ENTRY
3456 is the name of the entry point symbol (the -e linker option).
3457 FILE_ALIGN is the alignment to use for sections within the file
3458 (the -H linker option). MAXSTACK is the maximum stack size (the
3459 -bmaxstack linker option). MAXDATA is the maximum data size (the
3460 -bmaxdata linker option). GC is whether to do garbage collection
3461 (the -bgc linker option). MODTYPE is the module type (the
3462 -bmodtype linker option). TEXTRO is whether the text section must
3463 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3464 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3465 is set by this routine to csects with magic names like _end. */
3468 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3469 struct bfd_link_info *info,
3470 const char *libpath,
3472 unsigned long file_align,
3473 unsigned long maxstack,
3474 unsigned long maxdata,
3478 unsigned int auto_export_flags,
3479 asection **special_sections,
3482 struct xcoff_loader_info ldinfo;
3486 struct bfd_strtab_hash *debug_strtab;
3487 bfd_byte *debug_contents = NULL;
3490 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3492 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3493 special_sections[i] = NULL;
3497 ldinfo.failed = FALSE;
3498 ldinfo.output_bfd = output_bfd;
3500 ldinfo.auto_export_flags = auto_export_flags;
3501 ldinfo.ldsym_count = 0;
3502 ldinfo.string_size = 0;
3503 ldinfo.strings = NULL;
3504 ldinfo.string_alc = 0;
3506 xcoff_data (output_bfd)->maxstack = maxstack;
3507 xcoff_data (output_bfd)->maxdata = maxdata;
3508 xcoff_data (output_bfd)->modtype = modtype;
3510 xcoff_hash_table (info)->file_align = file_align;
3511 xcoff_hash_table (info)->textro = textro;
3512 xcoff_hash_table (info)->rtld = rtld;
3515 if (xcoff_hash_table (info)->loader_section
3516 && (info->init_function || info->fini_function || rtld))
3518 struct xcoff_link_hash_entry *hsym;
3519 struct internal_ldsym *ldsym;
3521 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3522 "__rtinit", FALSE, FALSE, TRUE);
3525 (*_bfd_error_handler)
3526 (_("error: undefined symbol __rtinit"));
3530 xcoff_mark_symbol (info, hsym);
3531 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3533 /* __rtinit initialized. */
3534 amt = sizeof (* ldsym);
3535 ldsym = bfd_malloc (amt);
3537 ldsym->l_value = 0; /* Will be filled in later. */
3538 ldsym->l_scnum = 2; /* Data section. */
3539 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3540 ldsym->l_smclas = 5; /* .rw. */
3541 ldsym->l_ifile = 0; /* Special system loader symbol. */
3542 ldsym->l_parm = 0; /* NA. */
3544 /* Force __rtinit to be the first symbol in the loader symbol table
3545 See xcoff_build_ldsyms
3547 The first 3 symbol table indices are reserved to indicate the data,
3548 text and bss sections. */
3549 BFD_ASSERT (0 == ldinfo.ldsym_count);
3552 ldinfo.ldsym_count = 1;
3553 hsym->ldsym = ldsym;
3555 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3556 hsym->ldsym, hsym->root.root.string))
3559 /* This symbol is written out by xcoff_write_global_symbol
3560 Set stuff up so xcoff_write_global_symbol logic works. */
3561 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3562 hsym->root.type = bfd_link_hash_defined;
3563 hsym->root.u.def.value = 0;
3566 /* Garbage collect unused sections. */
3567 if (info->relocatable || !gc)
3570 xcoff_hash_table (info)->gc = FALSE;
3572 /* We still need to call xcoff_mark, in order to set ldrel_count
3574 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3578 for (o = sub->sections; o != NULL; o = o->next)
3580 /* We shouldn't unconditionaly mark the TOC section.
3581 The output file should only have a TOC if either
3582 (a) one of the input files did or (b) we end up
3583 creating TOC references as part of the link process. */
3584 if (o != xcoff_hash_table (info)->toc_section
3585 && (o->flags & SEC_MARK) == 0)
3587 if (! xcoff_mark (info, o))
3596 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3598 if (info->init_function != NULL
3599 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3601 if (info->fini_function != NULL
3602 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3604 if (auto_export_flags != 0)
3606 xcoff_link_hash_traverse (xcoff_hash_table (info),
3607 xcoff_mark_auto_exports, &ldinfo);
3612 xcoff_hash_table (info)->gc = TRUE;
3615 /* Return special sections to the caller. */
3616 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3618 sec = xcoff_hash_table (info)->special_sections[i];
3622 && (sec->flags & SEC_MARK) == 0)
3625 special_sections[i] = sec;
3628 if (info->input_bfds == NULL)
3629 /* I'm not sure what to do in this bizarre case. */
3632 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3637 if (xcoff_hash_table (info)->loader_section
3638 && !xcoff_build_loader_section (&ldinfo, libpath))
3641 /* Allocate space for the magic sections. */
3642 sec = xcoff_hash_table (info)->linkage_section;
3645 sec->contents = bfd_zalloc (output_bfd, sec->size);
3646 if (sec->contents == NULL)
3649 sec = xcoff_hash_table (info)->toc_section;
3652 sec->contents = bfd_zalloc (output_bfd, sec->size);
3653 if (sec->contents == NULL)
3656 sec = xcoff_hash_table (info)->descriptor_section;
3659 sec->contents = bfd_zalloc (output_bfd, sec->size);
3660 if (sec->contents == NULL)
3664 /* Now that we've done garbage collection, decide which symbols to keep,
3665 and figure out the contents of the .debug section. */
3666 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3668 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3671 bfd_size_type symcount;
3674 unsigned int *lineno_counts;
3675 struct xcoff_link_hash_entry **sym_hash;
3676 bfd_byte *esym, *esymend;
3677 bfd_size_type symesz;
3679 if (sub->xvec != info->output_bfd->xvec)
3682 if ((sub->flags & DYNAMIC) != 0
3683 && !info->static_link)
3686 if (! _bfd_coff_get_external_symbols (sub))
3689 symcount = obj_raw_syment_count (sub);
3690 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3691 if (debug_index == NULL)
3693 xcoff_data (sub)->debug_indices = debug_index;
3695 if (info->strip == strip_all
3696 || info->strip == strip_debugger
3697 || info->discard == discard_all)
3698 /* We're stripping all debugging information, so there's no need
3699 to read SUB's .debug section. */
3703 /* Grab the contents of SUB's .debug section, if any. */
3704 subdeb = bfd_get_section_by_name (sub, ".debug");
3705 if (subdeb != NULL && subdeb->size > 0)
3707 /* We use malloc and copy the names into the debug
3708 stringtab, rather than bfd_alloc, because I expect
3709 that, when linking many files together, many of the
3710 strings will be the same. Storing the strings in the
3711 hash table should save space in this case. */
3712 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3717 csectpp = xcoff_data (sub)->csects;
3718 lineno_counts = xcoff_data (sub)->lineno_counts;
3719 sym_hash = obj_xcoff_sym_hashes (sub);
3720 symesz = bfd_coff_symesz (sub);
3721 esym = (bfd_byte *) obj_coff_external_syms (sub);
3722 esymend = esym + symcount * symesz;
3724 while (esym < esymend)
3726 struct internal_syment sym;
3727 union internal_auxent aux;
3732 bfd_coff_swap_sym_in (sub, esym, &sym);
3734 /* Read in the csect information, if any. */
3735 if (CSECT_SYM_P (sym.n_sclass))
3737 BFD_ASSERT (sym.n_numaux > 0);
3738 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3739 sym.n_type, sym.n_sclass,
3740 sym.n_numaux - 1, sym.n_numaux, &aux);
3743 /* If this symbol's name is stored in the debug section,
3744 get a pointer to it. */
3745 if (debug_contents != NULL
3746 && sym._n._n_n._n_zeroes == 0
3747 && bfd_coff_symname_in_debug (sub, &sym))
3748 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3752 /* Decide whether to copy this symbol to the output file. */
3754 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3755 *sym_hash, csect, name);
3760 /* Use a debug_index of -2 to record that a symbol should
3765 /* See whether we should store the symbol name in the
3766 output .debug section. */
3771 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3772 if (indx == (bfd_size_type) -1)
3774 *debug_index = indx;
3779 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3780 if (*lineno_counts > 0)
3781 csect->output_section->lineno_count += *lineno_counts;
3784 esym += (sym.n_numaux + 1) * symesz;
3785 csectpp += sym.n_numaux + 1;
3786 sym_hash += sym.n_numaux + 1;
3787 lineno_counts += sym.n_numaux + 1;
3788 debug_index += sym.n_numaux + 1;
3793 free (debug_contents);
3794 debug_contents = NULL;
3796 /* Clear the size of subdeb, so that it is not included directly
3797 in the output file. */
3801 if (! info->keep_memory)
3803 if (! _bfd_coff_free_symbols (sub))
3808 if (info->strip != strip_all)
3809 xcoff_hash_table (info)->debug_section->size =
3810 _bfd_stringtab_size (debug_strtab);
3815 if (ldinfo.strings != NULL)
3816 free (ldinfo.strings);
3817 if (debug_contents != NULL)
3818 free (debug_contents);
3823 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3828 struct bfd_in_memory *bim;
3830 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3837 abfd->link_next = 0;
3838 abfd->format = bfd_object;
3839 abfd->iostream = (void *) bim;
3840 abfd->flags = BFD_IN_MEMORY;
3841 abfd->direction = write_direction;
3844 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3847 /* need to reset to unknown or it will not be read back in correctly */
3848 abfd->format = bfd_unknown;
3849 abfd->direction = read_direction;
3855 /* Return the section that defines H. Return null if no section does. */
3858 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
3860 switch (h->root.type)
3862 case bfd_link_hash_defined:
3863 case bfd_link_hash_defweak:
3864 return h->root.u.def.section;
3866 case bfd_link_hash_common:
3867 return h->root.u.c.p->section;
3874 /* Add a .loader relocation for input relocation IREL. If the loader
3875 relocation should be against an output section, HSEC points to the
3876 input section that IREL is against, otherwise HSEC is null. H is the
3877 symbol that IREL is against, or null if it isn't against a global symbol.
3878 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
3881 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *finfo,
3882 asection *output_section, bfd *reference_bfd,
3883 struct internal_reloc *irel, asection *hsec,
3884 struct xcoff_link_hash_entry *h)
3886 struct internal_ldrel ldrel;
3888 ldrel.l_vaddr = irel->r_vaddr;
3891 const char *secname;
3893 secname = hsec->output_section->name;
3894 if (strcmp (secname, ".text") == 0)
3896 else if (strcmp (secname, ".data") == 0)
3898 else if (strcmp (secname, ".bss") == 0)
3902 (*_bfd_error_handler)
3903 (_("%B: loader reloc in unrecognized section `%s'"),
3904 reference_bfd, secname);
3905 bfd_set_error (bfd_error_nonrepresentable_section);
3913 (*_bfd_error_handler)
3914 (_("%B: `%s' in loader reloc but not loader sym"),
3915 reference_bfd, h->root.root.string);
3916 bfd_set_error (bfd_error_bad_value);
3919 ldrel.l_symndx = h->ldindx;
3922 ldrel.l_symndx = -(bfd_size_type) 1;
3924 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
3925 ldrel.l_rsecnm = output_section->target_index;
3926 if (xcoff_hash_table (finfo->info)->textro
3927 && strcmp (output_section->name, ".text") == 0)
3929 (*_bfd_error_handler)
3930 (_("%B: loader reloc in read-only section %A"),
3931 reference_bfd, output_section);
3932 bfd_set_error (bfd_error_invalid_operation);
3935 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
3936 finfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
3940 /* Link an input file into the linker output file. This function
3941 handles all the sections and relocations of the input file at once. */
3944 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3948 const char *strings;
3949 bfd_size_type syment_base;
3950 unsigned int n_tmask;
3951 unsigned int n_btshft;
3952 bfd_boolean copy, hash;
3953 bfd_size_type isymesz;
3954 bfd_size_type osymesz;
3955 bfd_size_type linesz;
3958 struct xcoff_link_hash_entry **sym_hash;
3959 struct internal_syment *isymp;
3961 unsigned int *lineno_counts;
3964 unsigned long output_index;
3968 bfd_boolean keep_syms;
3971 /* We can just skip DYNAMIC files, unless this is a static link. */
3972 if ((input_bfd->flags & DYNAMIC) != 0
3973 && ! finfo->info->static_link)
3976 /* Move all the symbols to the output file. */
3977 output_bfd = finfo->output_bfd;
3979 syment_base = obj_raw_syment_count (output_bfd);
3980 isymesz = bfd_coff_symesz (input_bfd);
3981 osymesz = bfd_coff_symesz (output_bfd);
3982 linesz = bfd_coff_linesz (input_bfd);
3983 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3985 n_tmask = coff_data (input_bfd)->local_n_tmask;
3986 n_btshft = coff_data (input_bfd)->local_n_btshft;
3988 /* Define macros so that ISFCN, et. al., macros work correctly. */
3989 #define N_TMASK n_tmask
3990 #define N_BTSHFT n_btshft
3993 if (! finfo->info->keep_memory)
3996 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3999 if (! _bfd_coff_get_external_symbols (input_bfd))
4002 /* Make one pass over the symbols and assign indices to symbols that
4003 we have decided to keep. Also use create .loader symbol information
4004 and update information in hash table entries. */
4005 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4006 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4007 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4008 csectpp = xcoff_data (input_bfd)->csects;
4009 debug_index = xcoff_data (input_bfd)->debug_indices;
4010 isymp = finfo->internal_syms;
4011 indexp = finfo->sym_indices;
4012 output_index = syment_base;
4013 while (esym < esym_end)
4015 union internal_auxent aux;
4019 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4021 /* Read in the csect information, if any. */
4022 if (CSECT_SYM_P (isymp->n_sclass))
4024 BFD_ASSERT (isymp->n_numaux > 0);
4025 bfd_coff_swap_aux_in (input_bfd,
4026 (void *) (esym + isymesz * isymp->n_numaux),
4027 isymp->n_type, isymp->n_sclass,
4028 isymp->n_numaux - 1, isymp->n_numaux,
4031 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4034 /* If this symbol is in the .loader section, swap out the
4035 .loader symbol information. If this is an external symbol
4036 reference to a defined symbol, though, then wait until we get
4037 to the definition. */
4038 if (EXTERN_SYM_P (isymp->n_sclass)
4039 && *sym_hash != NULL
4040 && (*sym_hash)->ldsym != NULL
4041 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4043 struct xcoff_link_hash_entry *h;
4044 struct internal_ldsym *ldsym;
4048 if (isymp->n_scnum > 0)
4050 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4051 ldsym->l_value = (isymp->n_value
4052 + (*csectpp)->output_section->vma
4053 + (*csectpp)->output_offset
4058 ldsym->l_scnum = isymp->n_scnum;
4059 ldsym->l_value = isymp->n_value;
4062 ldsym->l_smtype = smtyp;
4063 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4064 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4065 || (h->flags & XCOFF_IMPORT) != 0)
4066 ldsym->l_smtype |= L_IMPORT;
4067 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4068 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4069 || (h->flags & XCOFF_EXPORT) != 0)
4070 ldsym->l_smtype |= L_EXPORT;
4071 if ((h->flags & XCOFF_ENTRY) != 0)
4072 ldsym->l_smtype |= L_ENTRY;
4073 if (isymp->n_sclass == C_AIX_WEAKEXT)
4074 ldsym->l_smtype |= L_WEAK;
4076 ldsym->l_smclas = aux.x_csect.x_smclas;
4078 if (ldsym->l_ifile == (bfd_size_type) -1)
4080 else if (ldsym->l_ifile == 0)
4082 if ((ldsym->l_smtype & L_IMPORT) == 0)
4088 if (h->root.type == bfd_link_hash_defined
4089 || h->root.type == bfd_link_hash_defweak)
4090 impbfd = h->root.u.def.section->owner;
4091 else if (h->root.type == bfd_link_hash_undefined
4092 || h->root.type == bfd_link_hash_undefweak)
4093 impbfd = h->root.u.undef.abfd;
4101 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4102 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4109 BFD_ASSERT (h->ldindx >= 0);
4110 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4113 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
4116 /* Fill in snentry now that we know the target_index. */
4117 if ((h->flags & XCOFF_ENTRY) != 0
4118 && (h->root.type == bfd_link_hash_defined
4119 || h->root.type == bfd_link_hash_defweak))
4121 xcoff_data (output_bfd)->snentry =
4122 h->root.u.def.section->output_section->target_index;
4126 add = 1 + isymp->n_numaux;
4128 if (*debug_index == -2)
4129 /* We've decided to strip this symbol. */
4133 /* Assign the next unused index to this symbol. */
4134 *indexp = output_index;
4136 if (EXTERN_SYM_P (isymp->n_sclass))
4138 BFD_ASSERT (*sym_hash != NULL);
4139 (*sym_hash)->indx = output_index;
4142 /* If this is a symbol in the TOC which we may have merged
4143 (class XMC_TC), remember the symbol index of the TOC
4145 if (isymp->n_sclass == C_HIDEXT
4146 && aux.x_csect.x_smclas == XMC_TC
4147 && *sym_hash != NULL)
4149 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4150 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4151 (*sym_hash)->u.toc_indx = output_index;
4154 output_index += add;
4157 esym += add * isymesz;
4163 for (--add; add > 0; --add)
4167 /* Now write out the symbols that we decided to keep. */
4169 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4170 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4171 isymp = finfo->internal_syms;
4172 indexp = finfo->sym_indices;
4173 csectpp = xcoff_data (input_bfd)->csects;
4174 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4175 debug_index = xcoff_data (input_bfd)->debug_indices;
4176 outsym = finfo->outsyms;
4179 while (esym < esym_end)
4183 add = 1 + isymp->n_numaux;
4186 esym += add * isymesz;
4189 struct internal_syment isym;
4192 /* Adjust the symbol in order to output it. */
4194 if (isym._n._n_n._n_zeroes == 0
4195 && isym._n._n_n._n_offset != 0)
4197 /* This symbol has a long name. Enter it in the string
4198 table we are building. If *debug_index != -1, the
4199 name has already been entered in the .debug section. */
4200 if (*debug_index >= 0)
4201 isym._n._n_n._n_offset = *debug_index;
4207 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4211 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4212 if (indx == (bfd_size_type) -1)
4214 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4218 /* The value of a C_FILE symbol is the symbol index of the
4219 next C_FILE symbol. The value of the last C_FILE symbol
4220 is -1. We try to get this right, below, just before we
4221 write the symbols out, but in the general case we may
4222 have to write the symbol out twice. */
4223 if (isym.n_sclass == C_FILE)
4225 if (finfo->last_file_index != -1
4226 && finfo->last_file.n_value != (bfd_vma) *indexp)
4228 /* We must correct the value of the last C_FILE entry. */
4229 finfo->last_file.n_value = *indexp;
4230 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4232 /* The last C_FILE symbol is in this input file. */
4233 bfd_coff_swap_sym_out (output_bfd,
4234 (void *) &finfo->last_file,
4235 (void *) (finfo->outsyms
4236 + ((finfo->last_file_index
4242 /* We have already written out the last C_FILE
4243 symbol. We need to write it out again. We
4244 borrow *outsym temporarily. */
4247 bfd_coff_swap_sym_out (output_bfd,
4248 (void *) &finfo->last_file,
4251 pos = obj_sym_filepos (output_bfd);
4252 pos += finfo->last_file_index * osymesz;
4253 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4254 || (bfd_bwrite (outsym, osymesz, output_bfd)
4260 finfo->last_file_index = *indexp;
4261 finfo->last_file = isym;
4264 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4265 into the line numbers. We update the symbol values when
4266 we handle the line numbers. */
4267 if (isym.n_sclass == C_BINCL
4268 || isym.n_sclass == C_EINCL)
4270 isym.n_value = finfo->line_filepos;
4273 /* The value of a C_BSTAT symbol is the symbol table
4274 index of the containing csect. */
4275 else if (isym.n_sclass == C_BSTAT)
4279 indx = isym.n_value;
4280 if (indx < obj_raw_syment_count (input_bfd))
4284 symindx = finfo->sym_indices[indx];
4288 isym.n_value = symindx;
4291 else if (isym.n_sclass != C_ESTAT
4292 && isym.n_sclass != C_DECL
4293 && isym.n_scnum > 0)
4295 isym.n_scnum = (*csectpp)->output_section->target_index;
4296 isym.n_value += ((*csectpp)->output_section->vma
4297 + (*csectpp)->output_offset
4301 /* Output the symbol. */
4302 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4307 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4309 union internal_auxent aux;
4311 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4312 isymp->n_sclass, i, isymp->n_numaux,
4315 if (isymp->n_sclass == C_FILE)
4317 /* This is the file name (or some comment put in by
4318 the compiler). If it is long, we must put it in
4319 the string table. */
4320 if (aux.x_file.x_n.x_zeroes == 0
4321 && aux.x_file.x_n.x_offset != 0)
4323 const char *filename;
4326 BFD_ASSERT (aux.x_file.x_n.x_offset
4327 >= STRING_SIZE_SIZE);
4328 if (strings == NULL)
4330 strings = _bfd_coff_read_string_table (input_bfd);
4331 if (strings == NULL)
4334 filename = strings + aux.x_file.x_n.x_offset;
4335 indx = _bfd_stringtab_add (finfo->strtab, filename,
4337 if (indx == (bfd_size_type) -1)
4339 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4342 else if (CSECT_SYM_P (isymp->n_sclass)
4343 && i + 1 == isymp->n_numaux)
4346 /* We don't support type checking. I don't know if
4348 aux.x_csect.x_parmhash = 0;
4349 /* I don't think anybody uses these fields, but we'd
4350 better clobber them just in case. */
4351 aux.x_csect.x_stab = 0;
4352 aux.x_csect.x_snstab = 0;
4354 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4358 indx = aux.x_csect.x_scnlen.l;
4359 if (indx < obj_raw_syment_count (input_bfd))
4363 symindx = finfo->sym_indices[indx];
4366 aux.x_csect.x_scnlen.l = 0;
4370 aux.x_csect.x_scnlen.l = symindx;
4375 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4379 if (ISFCN (isymp->n_type)
4380 || ISTAG (isymp->n_sclass)
4381 || isymp->n_sclass == C_BLOCK
4382 || isymp->n_sclass == C_FCN)
4384 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4386 && indx < obj_raw_syment_count (input_bfd))
4388 /* We look forward through the symbol for
4389 the index of the next symbol we are going
4390 to include. I don't know if this is
4392 while (finfo->sym_indices[indx] < 0
4393 && indx < obj_raw_syment_count (input_bfd))
4395 if (indx >= obj_raw_syment_count (input_bfd))
4396 indx = output_index;
4398 indx = finfo->sym_indices[indx];
4399 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4404 indx = aux.x_sym.x_tagndx.l;
4405 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4409 symindx = finfo->sym_indices[indx];
4411 aux.x_sym.x_tagndx.l = 0;
4413 aux.x_sym.x_tagndx.l = symindx;
4418 /* Copy over the line numbers, unless we are stripping
4419 them. We do this on a symbol by symbol basis in
4420 order to more easily handle garbage collection. */
4421 if (CSECT_SYM_P (isymp->n_sclass)
4423 && isymp->n_numaux > 1
4424 && ISFCN (isymp->n_type)
4425 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4427 if (*lineno_counts == 0)
4428 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4431 asection *enclosing;
4432 unsigned int enc_count;
4433 bfd_signed_vma linoff;
4434 struct internal_lineno lin;
4441 /* Read in the enclosing section's line-number
4442 information, if we haven't already. */
4444 enclosing = xcoff_section_data (abfd, o)->enclosing;
4445 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4446 if (oline != enclosing)
4448 pos = enclosing->line_filepos;
4449 amt = linesz * enc_count;
4450 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4451 || (bfd_bread (finfo->linenos, amt, input_bfd)
4457 /* Copy across the first entry, adjusting its
4459 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4460 - enclosing->line_filepos);
4461 linp = finfo->linenos + linoff;
4462 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4463 lin.l_addr.l_symndx = *indexp;
4464 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4466 /* Copy the other entries, adjusting their addresses. */
4467 linpend = linp + *lineno_counts * linesz;
4468 offset = (o->output_section->vma
4471 for (linp += linesz; linp < linpend; linp += linesz)
4473 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4474 lin.l_addr.l_paddr += offset;
4475 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4478 /* Write out the entries we've just processed. */
4479 pos = (o->output_section->line_filepos
4480 + o->output_section->lineno_count * linesz);
4481 amt = linesz * *lineno_counts;
4482 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4483 || bfd_bwrite (finfo->linenos + linoff,
4484 amt, output_bfd) != amt)
4486 o->output_section->lineno_count += *lineno_counts;
4488 /* Record the offset of the symbol's line numbers
4489 in the output file. */
4490 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4494 struct internal_syment *iisp, *iispend;
4497 bfd_vma range_start, range_end;
4500 /* Update any C_BINCL or C_EINCL symbols
4501 that refer to a line number in the
4502 range we just output. */
4503 iisp = finfo->internal_syms;
4504 iispend = iisp + obj_raw_syment_count (input_bfd);
4505 iindp = finfo->sym_indices;
4506 oos = finfo->outsyms;
4507 range_start = enclosing->line_filepos + linoff;
4508 range_end = range_start + *lineno_counts * linesz;
4509 while (iisp < iispend)
4512 && (iisp->n_sclass == C_BINCL
4513 || iisp->n_sclass == C_EINCL)
4514 && iisp->n_value >= range_start
4515 && iisp->n_value < range_end)
4517 struct internal_syment iis;
4519 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4520 iis.n_value = (iisp->n_value
4523 bfd_coff_swap_sym_out (output_bfd,
4528 iiadd = 1 + iisp->n_numaux;
4530 oos += iiadd * osymesz;
4538 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4539 isymp->n_sclass, i, isymp->n_numaux,
4549 lineno_counts += add;
4553 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4554 symbol will be the first symbol in the next input file. In the
4555 normal case, this will save us from writing out the C_FILE symbol
4557 if (finfo->last_file_index != -1
4558 && (bfd_size_type) finfo->last_file_index >= syment_base)
4560 finfo->last_file.n_value = output_index;
4561 bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4562 (void *) (finfo->outsyms
4563 + ((finfo->last_file_index - syment_base)
4567 /* Write the modified symbols to the output file. */
4568 if (outsym > finfo->outsyms)
4570 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4571 bfd_size_type amt = outsym - finfo->outsyms;
4572 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4573 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4576 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4577 + (outsym - finfo->outsyms) / osymesz)
4580 obj_raw_syment_count (output_bfd) = output_index;
4583 /* Don't let the linker relocation routines discard the symbols. */
4584 keep_syms = obj_coff_keep_syms (input_bfd);
4585 obj_coff_keep_syms (input_bfd) = TRUE;
4587 /* Relocate the contents of each section. */
4588 for (o = input_bfd->sections; o != NULL; o = o->next)
4592 if (! o->linker_mark)
4593 /* This section was omitted from the link. */
4596 if ((o->flags & SEC_HAS_CONTENTS) == 0
4598 || (o->flags & SEC_IN_MEMORY) != 0)
4601 /* We have set filepos correctly for the sections we created to
4602 represent csects, so bfd_get_section_contents should work. */
4603 if (coff_section_data (input_bfd, o) != NULL
4604 && coff_section_data (input_bfd, o)->contents != NULL)
4605 contents = coff_section_data (input_bfd, o)->contents;
4608 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4609 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4611 contents = finfo->contents;
4614 if ((o->flags & SEC_RELOC) != 0)
4617 struct internal_reloc *internal_relocs;
4618 struct internal_reloc *irel;
4620 struct internal_reloc *irelend;
4621 struct xcoff_link_hash_entry **rel_hash;
4624 /* Read in the relocs. */
4625 target_index = o->output_section->target_index;
4626 internal_relocs = (xcoff_read_internal_relocs
4627 (input_bfd, o, FALSE, finfo->external_relocs,
4629 (finfo->section_info[target_index].relocs
4630 + o->output_section->reloc_count)));
4631 if (internal_relocs == NULL)
4634 /* Call processor specific code to relocate the section
4636 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4640 finfo->internal_syms,
4641 xcoff_data (input_bfd)->csects))
4644 offset = o->output_section->vma + o->output_offset - o->vma;
4645 irel = internal_relocs;
4646 irelend = irel + o->reloc_count;
4647 rel_hash = (finfo->section_info[target_index].rel_hashes
4648 + o->output_section->reloc_count);
4649 for (; irel < irelend; irel++, rel_hash++)
4651 struct xcoff_link_hash_entry *h = NULL;
4655 /* Adjust the reloc address and symbol index. */
4657 irel->r_vaddr += offset;
4659 r_symndx = irel->r_symndx;
4664 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4666 if (r_symndx != -1 && finfo->info->strip != strip_all)
4669 && h->smclas != XMC_TD
4670 && (irel->r_type == R_TOC
4671 || irel->r_type == R_GL
4672 || irel->r_type == R_TCL
4673 || irel->r_type == R_TRL
4674 || irel->r_type == R_TRLA))
4676 /* This is a TOC relative reloc with a symbol
4677 attached. The symbol should be the one which
4678 this reloc is for. We want to make this
4679 reloc against the TOC address of the symbol,
4680 not the symbol itself. */
4681 BFD_ASSERT (h->toc_section != NULL);
4682 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4683 if (h->u.toc_indx != -1)
4684 irel->r_symndx = h->u.toc_indx;
4687 struct xcoff_toc_rel_hash *n;
4688 struct xcoff_link_section_info *si;
4692 n = bfd_alloc (finfo->output_bfd, amt);
4695 si = finfo->section_info + target_index;
4696 n->next = si->toc_rel_hashes;
4699 si->toc_rel_hashes = n;
4704 /* This is a global symbol. */
4706 irel->r_symndx = h->indx;
4709 /* This symbol is being written at the end
4710 of the file, and we do not yet know the
4711 symbol index. We save the pointer to the
4712 hash table entry in the rel_hash list.
4713 We set the indx field to -2 to indicate
4714 that this symbol must not be stripped. */
4723 indx = finfo->sym_indices[r_symndx];
4727 struct internal_syment *is;
4729 /* Relocations against a TC0 TOC anchor are
4730 automatically transformed to be against
4731 the TOC anchor in the output file. */
4732 is = finfo->internal_syms + r_symndx;
4733 if (is->n_sclass == C_HIDEXT
4734 && is->n_numaux > 0)
4737 union internal_auxent aux;
4741 obj_coff_external_syms (input_bfd))
4742 + ((r_symndx + is->n_numaux)
4744 bfd_coff_swap_aux_in (input_bfd, auxptr,
4745 is->n_type, is->n_sclass,
4749 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4750 && aux.x_csect.x_smclas == XMC_TC0)
4751 indx = finfo->toc_symindx;
4756 irel->r_symndx = indx;
4760 struct internal_syment *is;
4763 char buf[SYMNMLEN + 1];
4765 /* This reloc is against a symbol we are
4766 stripping. It would be possible to handle
4767 this case, but I don't think it's worth it. */
4768 is = finfo->internal_syms + r_symndx;
4770 name = (_bfd_coff_internal_syment_name
4771 (input_bfd, is, buf));
4776 if (! ((*finfo->info->callbacks->unattached_reloc)
4777 (finfo->info, name, input_bfd, o,
4784 if (xcoff_need_ldrel_p (finfo->info, irel, h))
4791 sec = xcoff_data (input_bfd)->csects[r_symndx];
4793 sec = xcoff_symbol_section (h);
4794 if (!xcoff_create_ldrel (output_bfd, finfo,
4795 o->output_section, input_bfd,
4801 o->output_section->reloc_count += o->reloc_count;
4804 /* Write out the modified section contents. */
4805 if (! bfd_set_section_contents (output_bfd, o->output_section,
4806 contents, (file_ptr) o->output_offset,
4811 obj_coff_keep_syms (input_bfd) = keep_syms;
4813 if (! finfo->info->keep_memory)
4815 if (! _bfd_coff_free_symbols (input_bfd))
4825 /* Sort relocs by VMA. This is called via qsort. */
4828 xcoff_sort_relocs (const void * p1, const void * p2)
4830 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4831 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4833 if (r1->r_vaddr > r2->r_vaddr)
4835 else if (r1->r_vaddr < r2->r_vaddr)
4841 /* Return true if section SEC is a TOC section. */
4843 static inline bfd_boolean
4844 xcoff_toc_section_p (asection *sec)
4849 if (name[0] == '.' && name[1] == 't')
4853 if (name[3] == '0' && name[4] == 0)
4858 if (name[2] == 'd' && name[3] == 0)
4864 /* See if the link requires a TOC (it usually does!). If so, find a
4865 good place to put the TOC anchor csect, and write out the associated
4869 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *finfo)
4871 bfd_vma toc_start, toc_end, start, end, best_address;
4875 struct internal_syment irsym;
4876 union internal_auxent iraux;
4880 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
4881 index of a csect at the beginning of the TOC. */
4882 toc_start = ~(bfd_vma) 0;
4885 for (input_bfd = finfo->info->input_bfds;
4887 input_bfd = input_bfd->link_next)
4888 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4889 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
4891 start = sec->output_section->vma + sec->output_offset;
4892 if (toc_start > start)
4895 section_index = sec->output_section->target_index;
4898 end = start + sec->size;
4903 /* There's no need for a TC0 symbol if we don't have a TOC. */
4904 if (toc_end < toc_start)
4906 xcoff_data (output_bfd)->toc = toc_start;
4910 if (toc_end - toc_start < 0x8000)
4911 /* Every TOC csect can be accessed from TOC_START. */
4912 best_address = toc_start;
4915 /* Find the lowest TOC csect that is still within range of TOC_END. */
4916 best_address = toc_end;
4917 for (input_bfd = finfo->info->input_bfds;
4919 input_bfd = input_bfd->link_next)
4920 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4921 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
4923 start = sec->output_section->vma + sec->output_offset;
4924 if (start < best_address
4925 && start + 0x8000 >= toc_end)
4927 best_address = start;
4928 section_index = sec->output_section->target_index;
4932 /* Make sure that the start of the TOC is also within range. */
4933 if (best_address > toc_start + 0x8000)
4935 (*_bfd_error_handler)
4936 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
4938 (unsigned long) (toc_end - toc_start));
4939 bfd_set_error (bfd_error_file_too_big);
4944 /* Record the chosen TOC value. */
4945 finfo->toc_symindx = obj_raw_syment_count (output_bfd);
4946 xcoff_data (output_bfd)->toc = best_address;
4947 xcoff_data (output_bfd)->sntoc = section_index;
4949 /* Fill out the TC0 symbol. */
4950 if (!bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &irsym, "TOC"))
4952 irsym.n_value = best_address;
4953 irsym.n_scnum = section_index;
4954 irsym.n_sclass = C_HIDEXT;
4955 irsym.n_type = T_NULL;
4957 bfd_coff_swap_sym_out (output_bfd, &irsym, finfo->outsyms);
4959 /* Fill out the auxillary csect information. */
4960 memset (&iraux, 0, sizeof iraux);
4961 iraux.x_csect.x_smtyp = XTY_SD;
4962 iraux.x_csect.x_smclas = XMC_TC0;
4963 iraux.x_csect.x_scnlen.l = 0;
4964 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
4965 finfo->outsyms + bfd_coff_symesz (output_bfd));
4967 /* Write the contents to the file. */
4968 pos = obj_sym_filepos (output_bfd);
4969 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
4970 size = 2 * bfd_coff_symesz (output_bfd);
4971 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4972 || bfd_bwrite (finfo->outsyms, size, output_bfd) != size)
4974 obj_raw_syment_count (output_bfd) += 2;
4979 /* Write out a non-XCOFF global symbol. */
4982 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4984 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4987 struct internal_syment isym;
4988 union internal_auxent aux;
4993 output_bfd = finfo->output_bfd;
4994 outsym = finfo->outsyms;
4996 if (h->root.type == bfd_link_hash_warning)
4998 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4999 if (h->root.type == bfd_link_hash_new)
5003 /* If this symbol was garbage collected, just skip it. */
5004 if (xcoff_hash_table (finfo->info)->gc
5005 && (h->flags & XCOFF_MARK) == 0)
5008 /* If we need a .loader section entry, write it out. */
5009 if (h->ldsym != NULL)
5011 struct internal_ldsym *ldsym;
5016 if (h->root.type == bfd_link_hash_undefined
5017 || h->root.type == bfd_link_hash_undefweak)
5021 ldsym->l_scnum = N_UNDEF;
5022 ldsym->l_smtype = XTY_ER;
5023 impbfd = h->root.u.undef.abfd;
5026 else if (h->root.type == bfd_link_hash_defined
5027 || h->root.type == bfd_link_hash_defweak)
5031 sec = h->root.u.def.section;
5032 ldsym->l_value = (sec->output_section->vma
5033 + sec->output_offset
5034 + h->root.u.def.value);
5035 ldsym->l_scnum = sec->output_section->target_index;
5036 ldsym->l_smtype = XTY_SD;
5037 impbfd = sec->owner;
5043 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5044 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5045 || (h->flags & XCOFF_IMPORT) != 0)
5047 Import symbols are defined so the check above will make
5048 the l_smtype XTY_SD. But this is not correct, it should
5050 ldsym->l_smtype |= L_IMPORT;
5052 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5053 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5054 || (h->flags & XCOFF_EXPORT) != 0)
5055 ldsym->l_smtype |= L_EXPORT;
5057 if ((h->flags & XCOFF_ENTRY) != 0)
5058 ldsym->l_smtype |= L_ENTRY;
5060 if ((h->flags & XCOFF_RTINIT) != 0)
5061 ldsym->l_smtype = XTY_SD;
5063 ldsym->l_smclas = h->smclas;
5065 if (ldsym->l_smtype & L_IMPORT)
5067 if ((h->root.type == bfd_link_hash_defined
5068 || h->root.type == bfd_link_hash_defweak)
5069 && (h->root.u.def.value != 0))
5070 ldsym->l_smclas = XMC_XO;
5072 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5073 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5074 ldsym->l_smclas = XMC_SV3264;
5076 else if (h->flags & XCOFF_SYSCALL32)
5077 ldsym->l_smclas = XMC_SV;
5079 else if (h->flags & XCOFF_SYSCALL64)
5080 ldsym->l_smclas = XMC_SV64;
5083 if (ldsym->l_ifile == -(bfd_size_type) 1)
5087 else if (ldsym->l_ifile == 0)
5089 if ((ldsym->l_smtype & L_IMPORT) == 0)
5091 else if (impbfd == NULL)
5095 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5096 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5102 BFD_ASSERT (h->ldindx >= 0);
5104 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5107 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
5111 /* If this symbol needs global linkage code, write it out. */
5112 if (h->root.type == bfd_link_hash_defined
5113 && (h->root.u.def.section
5114 == xcoff_hash_table (finfo->info)->linkage_section))
5120 p = h->root.u.def.section->contents + h->root.u.def.value;
5122 /* The first instruction in the global linkage code loads a
5123 specific TOC element. */
5124 tocoff = (h->descriptor->toc_section->output_section->vma
5125 + h->descriptor->toc_section->output_offset
5126 - xcoff_data (output_bfd)->toc);
5128 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5129 tocoff += h->descriptor->u.toc_offset;
5131 /* The first instruction in the glink code needs to be
5132 cooked to to hold the correct offset in the toc. The
5133 rest are just output raw. */
5134 bfd_put_32 (output_bfd,
5135 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5137 /* Start with i == 1 to get past the first instruction done above
5138 The /4 is because the glink code is in bytes and we are going
5140 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5141 bfd_put_32 (output_bfd,
5142 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5146 /* If we created a TOC entry for this symbol, write out the required
5148 if ((h->flags & XCOFF_SET_TOC) != 0)
5153 struct internal_reloc *irel;
5154 struct internal_syment irsym;
5155 union internal_auxent iraux;
5157 tocsec = h->toc_section;
5158 osec = tocsec->output_section;
5159 oindx = osec->target_index;
5160 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5161 irel->r_vaddr = (osec->vma
5162 + tocsec->output_offset
5166 irel->r_symndx = h->indx;
5170 irel->r_symndx = obj_raw_syment_count (output_bfd);
5173 BFD_ASSERT (h->ldindx >= 0);
5175 /* Initialize the aux union here instead of closer to when it is
5176 written out below because the length of the csect depends on
5177 whether the output is 32 or 64 bit. */
5178 memset (&iraux, 0, sizeof iraux);
5179 iraux.x_csect.x_smtyp = XTY_SD;
5180 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5181 iraux.x_csect.x_smclas = XMC_TC;
5183 /* 32 bit uses a 32 bit R_POS to do the relocations
5184 64 bit uses a 64 bit R_POS to do the relocations
5186 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5188 Which one is determined by the backend. */
5189 if (bfd_xcoff_is_xcoff64 (output_bfd))
5192 iraux.x_csect.x_scnlen.l = 8;
5194 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5197 iraux.x_csect.x_scnlen.l = 4;
5202 irel->r_type = R_POS;
5203 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5204 ++osec->reloc_count;
5206 if (!xcoff_create_ldrel (output_bfd, finfo, osec,
5207 output_bfd, irel, NULL, h))
5210 /* We need to emit a symbol to define a csect which holds
5212 if (finfo->info->strip != strip_all)
5214 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
5215 &irsym, h->root.root.string);
5219 irsym.n_value = irel->r_vaddr;
5220 irsym.n_scnum = osec->target_index;
5221 irsym.n_sclass = C_HIDEXT;
5222 irsym.n_type = T_NULL;
5225 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5226 outsym += bfd_coff_symesz (output_bfd);
5228 /* Note : iraux is initialized above. */
5229 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5230 0, 1, (void *) outsym);
5231 outsym += bfd_coff_auxesz (output_bfd);
5235 /* We aren't going to write out the symbols below, so we
5236 need to write them out now. */
5237 pos = obj_sym_filepos (output_bfd);
5238 pos += (obj_raw_syment_count (output_bfd)
5239 * bfd_coff_symesz (output_bfd));
5240 amt = outsym - finfo->outsyms;
5241 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5242 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5244 obj_raw_syment_count (output_bfd) +=
5245 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5247 outsym = finfo->outsyms;
5252 /* If this symbol is a specially defined function descriptor, write
5253 it out. The first word is the address of the function code
5254 itself, the second word is the address of the TOC, and the third
5258 The addresses for the 32 bit will take 4 bytes and the addresses
5259 for 64 bit will take 8 bytes. Similar for the relocs. This type
5260 of logic was also done above to create a TOC entry in
5261 xcoff_write_global_symbol. */
5262 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5263 && h->root.type == bfd_link_hash_defined
5264 && (h->root.u.def.section
5265 == xcoff_hash_table (finfo->info)->descriptor_section))
5271 struct xcoff_link_hash_entry *hentry;
5273 struct internal_reloc *irel;
5275 unsigned int reloc_size, byte_size;
5277 if (bfd_xcoff_is_xcoff64 (output_bfd))
5282 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5290 sec = h->root.u.def.section;
5291 osec = sec->output_section;
5292 oindx = osec->target_index;
5293 p = sec->contents + h->root.u.def.value;
5295 hentry = h->descriptor;
5296 BFD_ASSERT (hentry != NULL
5297 && (hentry->root.type == bfd_link_hash_defined
5298 || hentry->root.type == bfd_link_hash_defweak));
5299 esec = hentry->root.u.def.section;
5301 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5302 irel->r_vaddr = (osec->vma
5303 + sec->output_offset
5304 + h->root.u.def.value);
5305 irel->r_symndx = esec->output_section->target_index;
5306 irel->r_type = R_POS;
5307 irel->r_size = reloc_size;
5308 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5309 ++osec->reloc_count;
5311 if (!xcoff_create_ldrel (output_bfd, finfo, osec,
5312 output_bfd, irel, esec, NULL))
5315 /* There are three items to write out,
5316 the address of the code
5317 the address of the toc anchor
5318 the environment pointer.
5319 We are ignoring the environment pointer. So set it to zero. */
5320 if (bfd_xcoff_is_xcoff64 (output_bfd))
5322 bfd_put_64 (output_bfd,
5323 (esec->output_section->vma + esec->output_offset
5324 + hentry->root.u.def.value),
5326 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5327 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5332 This logic was already called above so the error case where
5333 the backend is neither has already been checked. */
5334 bfd_put_32 (output_bfd,
5335 (esec->output_section->vma + esec->output_offset
5336 + hentry->root.u.def.value),
5338 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5339 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5342 tsec = coff_section_from_bfd_index (output_bfd,
5343 xcoff_data (output_bfd)->sntoc);
5346 irel->r_vaddr = (osec->vma
5347 + sec->output_offset
5348 + h->root.u.def.value
5350 irel->r_symndx = tsec->output_section->target_index;
5351 irel->r_type = R_POS;
5352 irel->r_size = reloc_size;
5353 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5354 ++osec->reloc_count;
5356 if (!xcoff_create_ldrel (output_bfd, finfo, osec,
5357 output_bfd, irel, tsec, NULL))
5361 if (h->indx >= 0 || finfo->info->strip == strip_all)
5363 BFD_ASSERT (outsym == finfo->outsyms);
5368 && (finfo->info->strip == strip_all
5369 || (finfo->info->strip == strip_some
5370 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5371 FALSE, FALSE) == NULL)))
5373 BFD_ASSERT (outsym == finfo->outsyms);
5378 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5380 BFD_ASSERT (outsym == finfo->outsyms);
5384 memset (&aux, 0, sizeof aux);
5386 h->indx = obj_raw_syment_count (output_bfd);
5388 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5389 h->root.root.string);
5393 if (h->root.type == bfd_link_hash_undefined
5394 || h->root.type == bfd_link_hash_undefweak)
5397 isym.n_scnum = N_UNDEF;
5398 if (h->root.type == bfd_link_hash_undefweak
5399 && C_WEAKEXT == C_AIX_WEAKEXT)
5400 isym.n_sclass = C_WEAKEXT;
5402 isym.n_sclass = C_EXT;
5403 aux.x_csect.x_smtyp = XTY_ER;
5405 else if ((h->root.type == bfd_link_hash_defined
5406 || h->root.type == bfd_link_hash_defweak)
5407 && h->smclas == XMC_XO)
5409 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5410 isym.n_value = h->root.u.def.value;
5411 isym.n_scnum = N_UNDEF;
5412 if (h->root.type == bfd_link_hash_undefweak
5413 && C_WEAKEXT == C_AIX_WEAKEXT)
5414 isym.n_sclass = C_WEAKEXT;
5416 isym.n_sclass = C_EXT;
5417 aux.x_csect.x_smtyp = XTY_ER;
5419 else if (h->root.type == bfd_link_hash_defined
5420 || h->root.type == bfd_link_hash_defweak)
5422 struct xcoff_link_size_list *l;
5424 isym.n_value = (h->root.u.def.section->output_section->vma
5425 + h->root.u.def.section->output_offset
5426 + h->root.u.def.value);
5427 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5428 isym.n_scnum = N_ABS;
5430 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5431 isym.n_sclass = C_HIDEXT;
5432 aux.x_csect.x_smtyp = XTY_SD;
5434 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5436 for (l = xcoff_hash_table (finfo->info)->size_list;
5442 aux.x_csect.x_scnlen.l = l->size;
5448 else if (h->root.type == bfd_link_hash_common)
5450 isym.n_value = (h->root.u.c.p->section->output_section->vma
5451 + h->root.u.c.p->section->output_offset);
5452 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5453 isym.n_sclass = C_EXT;
5454 aux.x_csect.x_smtyp = XTY_CM;
5455 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5460 isym.n_type = T_NULL;
5463 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5464 outsym += bfd_coff_symesz (output_bfd);
5466 aux.x_csect.x_smclas = h->smclas;
5467 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5469 outsym += bfd_coff_auxesz (output_bfd);
5471 if ((h->root.type == bfd_link_hash_defined
5472 || h->root.type == bfd_link_hash_defweak)
5473 && h->smclas != XMC_XO)
5475 /* We just output an SD symbol. Now output an LD symbol. */
5478 if (h->root.type == bfd_link_hash_undefweak
5479 && C_WEAKEXT == C_AIX_WEAKEXT)
5480 isym.n_sclass = C_WEAKEXT;
5482 isym.n_sclass = C_EXT;
5483 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5484 outsym += bfd_coff_symesz (output_bfd);
5486 aux.x_csect.x_smtyp = XTY_LD;
5487 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5488 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5490 outsym += bfd_coff_auxesz (output_bfd);
5493 pos = obj_sym_filepos (output_bfd);
5494 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5495 amt = outsym - finfo->outsyms;
5496 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5497 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5499 obj_raw_syment_count (output_bfd) +=
5500 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5505 /* Handle a link order which is supposed to generate a reloc. */
5508 xcoff_reloc_link_order (bfd *output_bfd,
5509 struct xcoff_final_link_info *finfo,
5510 asection *output_section,
5511 struct bfd_link_order *link_order)
5513 reloc_howto_type *howto;
5514 struct xcoff_link_hash_entry *h;
5518 struct internal_reloc *irel;
5519 struct xcoff_link_hash_entry **rel_hash_ptr;
5521 if (link_order->type == bfd_section_reloc_link_order)
5522 /* We need to somehow locate a symbol in the right section. The
5523 symbol must either have a value of zero, or we must adjust
5524 the addend by the value of the symbol. FIXME: Write this
5525 when we need it. The old linker couldn't handle this anyhow. */
5528 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5531 bfd_set_error (bfd_error_bad_value);
5535 h = ((struct xcoff_link_hash_entry *)
5536 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5537 link_order->u.reloc.p->u.name,
5538 FALSE, FALSE, TRUE));
5541 if (! ((*finfo->info->callbacks->unattached_reloc)
5542 (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5547 hsec = xcoff_symbol_section (h);
5548 if (h->root.type == bfd_link_hash_defined
5549 || h->root.type == bfd_link_hash_defweak)
5550 hval = h->root.u.def.value;
5554 addend = link_order->u.reloc.p->addend;
5556 addend += (hsec->output_section->vma
5557 + hsec->output_offset
5564 bfd_reloc_status_type rstat;
5567 size = bfd_get_reloc_size (howto);
5568 buf = bfd_zmalloc (size);
5572 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5578 case bfd_reloc_outofrange:
5580 case bfd_reloc_overflow:
5581 if (! ((*finfo->info->callbacks->reloc_overflow)
5582 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5583 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5590 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5591 (file_ptr) link_order->offset, size);
5597 /* Store the reloc information in the right place. It will get
5598 swapped and written out at the end of the final_link routine. */
5599 irel = (finfo->section_info[output_section->target_index].relocs
5600 + output_section->reloc_count);
5601 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5602 + output_section->reloc_count);
5604 memset (irel, 0, sizeof (struct internal_reloc));
5605 *rel_hash_ptr = NULL;
5607 irel->r_vaddr = output_section->vma + link_order->offset;
5610 irel->r_symndx = h->indx;
5613 /* Set the index to -2 to force this symbol to get written out. */
5619 irel->r_type = howto->type;
5620 irel->r_size = howto->bitsize - 1;
5621 if (howto->complain_on_overflow == complain_overflow_signed)
5622 irel->r_size |= 0x80;
5624 ++output_section->reloc_count;
5626 /* Now output the reloc to the .loader section. */
5627 if (xcoff_hash_table (finfo->info)->loader_section)
5629 if (!xcoff_create_ldrel (output_bfd, finfo, output_section,
5630 output_bfd, irel, hsec, h))
5637 /* Do the final link step. */
5640 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5642 bfd_size_type symesz;
5643 struct xcoff_final_link_info finfo;
5645 struct bfd_link_order *p;
5646 bfd_size_type max_contents_size;
5647 bfd_size_type max_sym_count;
5648 bfd_size_type max_lineno_count;
5649 bfd_size_type max_reloc_count;
5650 bfd_size_type max_output_reloc_count;
5651 file_ptr rel_filepos;
5653 file_ptr line_filepos;
5654 unsigned int linesz;
5656 bfd_byte *external_relocs = NULL;
5657 char strbuf[STRING_SIZE_SIZE];
5662 abfd->flags |= DYNAMIC;
5664 symesz = bfd_coff_symesz (abfd);
5667 finfo.output_bfd = abfd;
5668 finfo.strtab = NULL;
5669 finfo.section_info = NULL;
5670 finfo.last_file_index = -1;
5671 finfo.toc_symindx = -1;
5672 finfo.internal_syms = NULL;
5673 finfo.sym_indices = NULL;
5674 finfo.outsyms = NULL;
5675 finfo.linenos = NULL;
5676 finfo.contents = NULL;
5677 finfo.external_relocs = NULL;
5679 if (xcoff_hash_table (info)->loader_section)
5681 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5682 + bfd_xcoff_ldhdrsz (abfd));
5683 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5684 + bfd_xcoff_ldhdrsz (abfd)
5685 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5686 * bfd_xcoff_ldsymsz (abfd)));
5694 xcoff_data (abfd)->coff.link_info = info;
5696 finfo.strtab = _bfd_stringtab_init ();
5697 if (finfo.strtab == NULL)
5700 /* Count the relocation entries required for the output file.
5701 (We've already counted the line numbers.) Determine a few
5703 max_contents_size = 0;
5704 max_lineno_count = 0;
5705 max_reloc_count = 0;
5706 for (o = abfd->sections; o != NULL; o = o->next)
5709 for (p = o->map_head.link_order; p != NULL; p = p->next)
5711 if (p->type == bfd_indirect_link_order)
5715 sec = p->u.indirect.section;
5717 /* Mark all sections which are to be included in the
5718 link. This will normally be every section. We need
5719 to do this so that we can identify any sections which
5720 the linker has decided to not include. */
5721 sec->linker_mark = TRUE;
5723 o->reloc_count += sec->reloc_count;
5725 if (sec->rawsize > max_contents_size)
5726 max_contents_size = sec->rawsize;
5727 if (sec->size > max_contents_size)
5728 max_contents_size = sec->size;
5729 if (coff_section_data (sec->owner, sec) != NULL
5730 && xcoff_section_data (sec->owner, sec) != NULL
5731 && (xcoff_section_data (sec->owner, sec)->lineno_count
5732 > max_lineno_count))
5734 xcoff_section_data (sec->owner, sec)->lineno_count;
5735 if (sec->reloc_count > max_reloc_count)
5736 max_reloc_count = sec->reloc_count;
5738 else if (p->type == bfd_section_reloc_link_order
5739 || p->type == bfd_symbol_reloc_link_order)
5744 /* Compute the file positions for all the sections. */
5745 if (abfd->output_has_begun)
5747 if (xcoff_hash_table (info)->file_align != 0)
5754 file_align = xcoff_hash_table (info)->file_align;
5755 if (file_align != 0)
5757 bfd_boolean saw_contents;
5761 /* Insert .pad sections before every section which has
5762 contents and is loaded, if it is preceded by some other
5763 section which has contents and is loaded. */
5764 saw_contents = TRUE;
5765 for (o = abfd->sections; o != NULL; o = o->next)
5767 if (strcmp (o->name, ".pad") == 0)
5768 saw_contents = FALSE;
5769 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5770 && (o->flags & SEC_LOAD) != 0)
5773 saw_contents = TRUE;
5778 /* Create a pad section and place it before the section
5779 that needs padding. This requires unlinking and
5780 relinking the bfd's section list. */
5782 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5784 n->alignment_power = 0;
5786 bfd_section_list_remove (abfd, n);
5787 bfd_section_list_insert_before (abfd, o, n);
5788 saw_contents = FALSE;
5793 /* Reset the section indices after inserting the new
5796 for (o = abfd->sections; o != NULL; o = o->next)
5799 o->target_index = indx;
5801 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5803 /* Work out appropriate sizes for the .pad sections to force
5804 each section to land on a page boundary. This bit of
5805 code knows what compute_section_file_positions is going
5807 sofar = bfd_coff_filhsz (abfd);
5808 sofar += bfd_coff_aoutsz (abfd);
5809 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5810 for (o = abfd->sections; o != NULL; o = o->next)
5811 if ((bfd_xcoff_is_reloc_count_overflow
5812 (abfd, (bfd_vma) o->reloc_count))
5813 || (bfd_xcoff_is_lineno_count_overflow
5814 (abfd, (bfd_vma) o->lineno_count)))
5815 /* 64 does not overflow, need to check if 32 does */
5816 sofar += bfd_coff_scnhsz (abfd);
5818 for (o = abfd->sections; o != NULL; o = o->next)
5820 if (strcmp (o->name, ".pad") == 0)
5824 BFD_ASSERT (o->size == 0);
5825 pageoff = sofar & (file_align - 1);
5828 o->size = file_align - pageoff;
5829 sofar += file_align - pageoff;
5830 o->flags |= SEC_HAS_CONTENTS;
5835 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5836 sofar += BFD_ALIGN (o->size,
5837 1 << o->alignment_power);
5842 if (! bfd_coff_compute_section_file_positions (abfd))
5846 /* Allocate space for the pointers we need to keep for the relocs. */
5850 /* We use section_count + 1, rather than section_count, because
5851 the target_index fields are 1 based. */
5852 amt = abfd->section_count + 1;
5853 amt *= sizeof (struct xcoff_link_section_info);
5854 finfo.section_info = bfd_malloc (amt);
5855 if (finfo.section_info == NULL)
5857 for (i = 0; i <= abfd->section_count; i++)
5859 finfo.section_info[i].relocs = NULL;
5860 finfo.section_info[i].rel_hashes = NULL;
5861 finfo.section_info[i].toc_rel_hashes = NULL;
5865 /* Set the file positions for the relocs. */
5866 rel_filepos = obj_relocbase (abfd);
5867 relsz = bfd_coff_relsz (abfd);
5868 max_output_reloc_count = 0;
5869 for (o = abfd->sections; o != NULL; o = o->next)
5871 if (o->reloc_count == 0)
5875 /* A stripped file has no relocs. However, we still
5876 allocate the buffers, so that later code doesn't have to
5877 worry about whether we are stripping or not. */
5878 if (info->strip == strip_all)
5882 o->flags |= SEC_RELOC;
5883 o->rel_filepos = rel_filepos;
5884 rel_filepos += o->reloc_count * relsz;
5887 /* We don't know the indices of global symbols until we have
5888 written out all the local symbols. For each section in
5889 the output file, we keep an array of pointers to hash
5890 table entries. Each entry in the array corresponds to a
5891 reloc. When we find a reloc against a global symbol, we
5892 set the corresponding entry in this array so that we can
5893 fix up the symbol index after we have written out all the
5896 Because of this problem, we also keep the relocs in
5897 memory until the end of the link. This wastes memory.
5898 We could backpatch the file later, I suppose, although it
5900 amt = o->reloc_count;
5901 amt *= sizeof (struct internal_reloc);
5902 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5904 amt = o->reloc_count;
5905 amt *= sizeof (struct xcoff_link_hash_entry *);
5906 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5908 if (finfo.section_info[o->target_index].relocs == NULL
5909 || finfo.section_info[o->target_index].rel_hashes == NULL)
5912 if (o->reloc_count > max_output_reloc_count)
5913 max_output_reloc_count = o->reloc_count;
5917 /* We now know the size of the relocs, so we can determine the file
5918 positions of the line numbers. */
5919 line_filepos = rel_filepos;
5920 finfo.line_filepos = line_filepos;
5921 linesz = bfd_coff_linesz (abfd);
5922 for (o = abfd->sections; o != NULL; o = o->next)
5924 if (o->lineno_count == 0)
5925 o->line_filepos = 0;
5928 o->line_filepos = line_filepos;
5929 line_filepos += o->lineno_count * linesz;
5932 /* Reset the reloc and lineno counts, so that we can use them to
5933 count the number of entries we have output so far. */
5935 o->lineno_count = 0;
5938 obj_sym_filepos (abfd) = line_filepos;
5940 /* Figure out the largest number of symbols in an input BFD. Take
5941 the opportunity to clear the output_has_begun fields of all the
5942 input BFD's. We want at least 6 symbols, since that is the
5943 number which xcoff_write_global_symbol may need. */
5945 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5949 sub->output_has_begun = FALSE;
5950 sz = obj_raw_syment_count (sub);
5951 if (sz > max_sym_count)
5955 /* Allocate some buffers used while linking. */
5956 amt = max_sym_count * sizeof (struct internal_syment);
5957 finfo.internal_syms = bfd_malloc (amt);
5959 amt = max_sym_count * sizeof (long);
5960 finfo.sym_indices = bfd_malloc (amt);
5962 amt = (max_sym_count + 1) * symesz;
5963 finfo.outsyms = bfd_malloc (amt);
5965 amt = max_lineno_count * bfd_coff_linesz (abfd);
5966 finfo.linenos = bfd_malloc (amt);
5968 amt = max_contents_size;
5969 finfo.contents = bfd_malloc (amt);
5971 amt = max_reloc_count * relsz;
5972 finfo.external_relocs = bfd_malloc (amt);
5974 if ((finfo.internal_syms == NULL && max_sym_count > 0)
5975 || (finfo.sym_indices == NULL && max_sym_count > 0)
5976 || finfo.outsyms == NULL
5977 || (finfo.linenos == NULL && max_lineno_count > 0)
5978 || (finfo.contents == NULL && max_contents_size > 0)
5979 || (finfo.external_relocs == NULL && max_reloc_count > 0))
5982 obj_raw_syment_count (abfd) = 0;
5984 /* Find a TOC symbol, if we need one. */
5985 if (!xcoff_find_tc0 (abfd, &finfo))
5988 /* We now know the position of everything in the file, except that
5989 we don't know the size of the symbol table and therefore we don't
5990 know where the string table starts. We just build the string
5991 table in memory as we go along. We process all the relocations
5992 for a single input file at once. */
5993 for (o = abfd->sections; o != NULL; o = o->next)
5995 for (p = o->map_head.link_order; p != NULL; p = p->next)
5997 if (p->type == bfd_indirect_link_order
5998 && p->u.indirect.section->owner->xvec == abfd->xvec)
6000 sub = p->u.indirect.section->owner;
6001 if (! sub->output_has_begun)
6003 if (! xcoff_link_input_bfd (&finfo, sub))
6005 sub->output_has_begun = TRUE;
6008 else if (p->type == bfd_section_reloc_link_order
6009 || p->type == bfd_symbol_reloc_link_order)
6011 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
6016 if (! _bfd_default_link_order (abfd, info, o, p))
6022 /* Free up the buffers used by xcoff_link_input_bfd. */
6023 if (finfo.internal_syms != NULL)
6025 free (finfo.internal_syms);
6026 finfo.internal_syms = NULL;
6028 if (finfo.sym_indices != NULL)
6030 free (finfo.sym_indices);
6031 finfo.sym_indices = NULL;
6033 if (finfo.linenos != NULL)
6035 free (finfo.linenos);
6036 finfo.linenos = NULL;
6038 if (finfo.contents != NULL)
6040 free (finfo.contents);
6041 finfo.contents = NULL;
6043 if (finfo.external_relocs != NULL)
6045 free (finfo.external_relocs);
6046 finfo.external_relocs = NULL;
6049 /* The value of the last C_FILE symbol is supposed to be -1. Write
6051 if (finfo.last_file_index != -1)
6053 finfo.last_file.n_value = -(bfd_vma) 1;
6054 bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
6055 (void *) finfo.outsyms);
6056 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
6057 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6058 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
6062 /* Write out all the global symbols which do not come from XCOFF
6064 xcoff_link_hash_traverse (xcoff_hash_table (info),
6065 xcoff_write_global_symbol,
6068 if (finfo.outsyms != NULL)
6070 free (finfo.outsyms);
6071 finfo.outsyms = NULL;
6074 /* Now that we have written out all the global symbols, we know the
6075 symbol indices to use for relocs against them, and we can finally
6076 write out the relocs. */
6077 amt = max_output_reloc_count * relsz;
6078 external_relocs = bfd_malloc (amt);
6079 if (external_relocs == NULL && max_output_reloc_count != 0)
6082 for (o = abfd->sections; o != NULL; o = o->next)
6084 struct internal_reloc *irel;
6085 struct internal_reloc *irelend;
6086 struct xcoff_link_hash_entry **rel_hash;
6087 struct xcoff_toc_rel_hash *toc_rel_hash;
6089 bfd_size_type rel_size;
6091 /* A stripped file has no relocs. */
6092 if (info->strip == strip_all)
6098 if (o->reloc_count == 0)
6101 irel = finfo.section_info[o->target_index].relocs;
6102 irelend = irel + o->reloc_count;
6103 rel_hash = finfo.section_info[o->target_index].rel_hashes;
6104 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6106 if (*rel_hash != NULL)
6108 if ((*rel_hash)->indx < 0)
6110 if (! ((*info->callbacks->unattached_reloc)
6111 (info, (*rel_hash)->root.root.string,
6112 NULL, o, irel->r_vaddr)))
6114 (*rel_hash)->indx = 0;
6116 irel->r_symndx = (*rel_hash)->indx;
6120 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
6121 toc_rel_hash != NULL;
6122 toc_rel_hash = toc_rel_hash->next)
6124 if (toc_rel_hash->h->u.toc_indx < 0)
6126 if (! ((*info->callbacks->unattached_reloc)
6127 (info, toc_rel_hash->h->root.root.string,
6128 NULL, o, toc_rel_hash->rel->r_vaddr)))
6130 toc_rel_hash->h->u.toc_indx = 0;
6132 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6135 /* XCOFF requires that the relocs be sorted by address. We tend
6136 to produce them in the order in which their containing csects
6137 appear in the symbol table, which is not necessarily by
6138 address. So we sort them here. There may be a better way to
6140 qsort ((void *) finfo.section_info[o->target_index].relocs,
6141 o->reloc_count, sizeof (struct internal_reloc),
6144 irel = finfo.section_info[o->target_index].relocs;
6145 irelend = irel + o->reloc_count;
6146 erel = external_relocs;
6147 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6148 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6150 rel_size = relsz * o->reloc_count;
6151 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6152 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6156 if (external_relocs != NULL)
6158 free (external_relocs);
6159 external_relocs = NULL;
6162 /* Free up the section information. */
6163 if (finfo.section_info != NULL)
6167 for (i = 0; i < abfd->section_count; i++)
6169 if (finfo.section_info[i].relocs != NULL)
6170 free (finfo.section_info[i].relocs);
6171 if (finfo.section_info[i].rel_hashes != NULL)
6172 free (finfo.section_info[i].rel_hashes);
6174 free (finfo.section_info);
6175 finfo.section_info = NULL;
6178 /* Write out the loader section contents. */
6179 o = xcoff_hash_table (info)->loader_section;
6182 BFD_ASSERT ((bfd_byte *) finfo.ldrel
6183 == (xcoff_hash_table (info)->loader_section->contents
6184 + xcoff_hash_table (info)->ldhdr.l_impoff));
6185 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6186 (file_ptr) o->output_offset, o->size))
6190 /* Write out the magic sections. */
6191 o = xcoff_hash_table (info)->linkage_section;
6193 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6194 (file_ptr) o->output_offset,
6197 o = xcoff_hash_table (info)->toc_section;
6199 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6200 (file_ptr) o->output_offset,
6203 o = xcoff_hash_table (info)->descriptor_section;
6205 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6206 (file_ptr) o->output_offset,
6210 /* Write out the string table. */
6211 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6212 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6215 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
6217 amt = STRING_SIZE_SIZE;
6218 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6220 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
6223 _bfd_stringtab_free (finfo.strtab);
6225 /* Write out the debugging string table. */
6226 o = xcoff_hash_table (info)->debug_section;
6229 struct bfd_strtab_hash *debug_strtab;
6231 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6232 BFD_ASSERT (o->output_section->size - o->output_offset
6233 >= _bfd_stringtab_size (debug_strtab));
6234 pos = o->output_section->filepos + o->output_offset;
6235 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6237 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6241 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6242 not try to write out the symbols. */
6243 bfd_get_symcount (abfd) = 0;
6248 if (finfo.strtab != NULL)
6249 _bfd_stringtab_free (finfo.strtab);
6251 if (finfo.section_info != NULL)
6255 for (i = 0; i < abfd->section_count; i++)
6257 if (finfo.section_info[i].relocs != NULL)
6258 free (finfo.section_info[i].relocs);
6259 if (finfo.section_info[i].rel_hashes != NULL)
6260 free (finfo.section_info[i].rel_hashes);
6262 free (finfo.section_info);
6265 if (finfo.internal_syms != NULL)
6266 free (finfo.internal_syms);
6267 if (finfo.sym_indices != NULL)
6268 free (finfo.sym_indices);
6269 if (finfo.outsyms != NULL)
6270 free (finfo.outsyms);
6271 if (finfo.linenos != NULL)
6272 free (finfo.linenos);
6273 if (finfo.contents != NULL)
6274 free (finfo.contents);
6275 if (finfo.external_relocs != NULL)
6276 free (finfo.external_relocs);
6277 if (external_relocs != NULL)
6278 free (external_relocs);