1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "coff/internal.h"
27 #include "coff/xcoff.h"
30 #include "libiberty.h"
32 /* This file holds the XCOFF linker code. */
34 #undef STRING_SIZE_SIZE
35 #define STRING_SIZE_SIZE 4
37 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
38 This flag will only be used on input sections. */
40 #define SEC_MARK (SEC_ROM)
42 /* The list of import files. */
44 struct xcoff_import_file
46 /* The next entry in the list. */
47 struct xcoff_import_file *next;
52 /* The member name. */
56 /* Information we keep for each section in the output file during the
59 struct xcoff_link_section_info
61 /* The relocs to be output. */
62 struct internal_reloc *relocs;
63 /* For each reloc against a global symbol whose index was not known
64 when the reloc was handled, the global hash table entry. */
65 struct xcoff_link_hash_entry **rel_hashes;
66 /* If there is a TOC relative reloc against a global symbol, and the
67 index of the TOC symbol is not known when the reloc was handled,
68 an entry is added to this linked list. This is not an array,
69 like rel_hashes, because this case is quite uncommon. */
70 struct xcoff_toc_rel_hash
72 struct xcoff_toc_rel_hash *next;
73 struct xcoff_link_hash_entry *h;
74 struct internal_reloc *rel;
78 /* Information that the XCOFF linker collects about an archive. */
79 struct xcoff_archive_info
81 /* The archive described by this entry. */
84 /* The import path and import filename to use when referring to
85 this archive in the .loader section. */
89 /* True if the archive contains a dynamic object. */
90 unsigned int contains_shared_object_p : 1;
92 /* True if the previous field is valid. */
93 unsigned int know_contains_shared_object_p : 1;
96 struct xcoff_link_hash_table
98 struct bfd_link_hash_table root;
100 /* The .debug string hash table. We need to compute this while
101 reading the input files, so that we know how large the .debug
102 section will be before we assign section positions. */
103 struct bfd_strtab_hash *debug_strtab;
105 /* The .debug section we will use for the final output. */
106 asection *debug_section;
108 /* The .loader section we will use for the final output. */
109 asection *loader_section;
111 /* A count of non TOC relative relocs which will need to be
112 allocated in the .loader section. */
115 /* The .loader section header. */
116 struct internal_ldhdr ldhdr;
118 /* The .gl section we use to hold global linkage code. */
119 asection *linkage_section;
121 /* The .tc section we use to hold toc entries we build for global
123 asection *toc_section;
125 /* The .ds section we use to hold function descriptors which we
126 create for exported symbols. */
127 asection *descriptor_section;
129 /* The list of import files. */
130 struct xcoff_import_file *imports;
132 /* Required alignment of sections within the output file. */
133 unsigned long file_align;
135 /* Whether the .text section must be read-only. */
138 /* Whether -brtl was specified. */
141 /* Whether garbage collection was done. */
144 /* A linked list of symbols for which we have size information. */
145 struct xcoff_link_size_list
147 struct xcoff_link_size_list *next;
148 struct xcoff_link_hash_entry *h;
153 /* Information about archives. */
156 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
157 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
160 /* Information that we pass around while doing the final link step. */
162 struct xcoff_final_link_info
164 /* General link information. */
165 struct bfd_link_info *info;
168 /* Hash table for long symbol names. */
169 struct bfd_strtab_hash *strtab;
170 /* Array of information kept for each output section, indexed by the
171 target_index field. */
172 struct xcoff_link_section_info *section_info;
173 /* Symbol index of last C_FILE symbol (-1 if none). */
174 long last_file_index;
175 /* Contents of last C_FILE symbol. */
176 struct internal_syment last_file;
177 /* Symbol index of TOC symbol. */
179 /* Start of .loader symbols. */
181 /* Next .loader reloc to swap out. */
183 /* File position of start of line numbers. */
184 file_ptr line_filepos;
185 /* Buffer large enough to hold swapped symbols of any input file. */
186 struct internal_syment *internal_syms;
187 /* Buffer large enough to hold output indices of symbols of any
190 /* Buffer large enough to hold output symbols for any input file. */
192 /* Buffer large enough to hold external line numbers for any input
195 /* Buffer large enough to hold any input section. */
197 /* Buffer large enough to hold external relocs of any input section. */
198 bfd_byte *external_relocs;
201 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
205 /* Routines to read XCOFF dynamic information. This don't really
206 belong here, but we already have the ldsym manipulation routines
209 /* Read the contents of a section. */
212 xcoff_get_section_contents (bfd *abfd, asection *sec)
214 if (coff_section_data (abfd, sec) == NULL)
216 bfd_size_type amt = sizeof (struct coff_section_tdata);
218 sec->used_by_bfd = bfd_zalloc (abfd, amt);
219 if (sec->used_by_bfd == NULL)
223 if (coff_section_data (abfd, sec)->contents == NULL)
227 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
229 if (contents != NULL)
233 coff_section_data (abfd, sec)->contents = contents;
239 /* Get the size required to hold the dynamic symbols. */
242 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
246 struct internal_ldhdr ldhdr;
248 if ((abfd->flags & DYNAMIC) == 0)
250 bfd_set_error (bfd_error_invalid_operation);
254 lsec = bfd_get_section_by_name (abfd, ".loader");
257 bfd_set_error (bfd_error_no_symbols);
261 if (! xcoff_get_section_contents (abfd, lsec))
263 contents = coff_section_data (abfd, lsec)->contents;
265 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
267 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
270 /* Get the dynamic symbols. */
273 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
277 struct internal_ldhdr ldhdr;
279 bfd_byte *elsym, *elsymend;
280 coff_symbol_type *symbuf;
282 if ((abfd->flags & DYNAMIC) == 0)
284 bfd_set_error (bfd_error_invalid_operation);
288 lsec = bfd_get_section_by_name (abfd, ".loader");
291 bfd_set_error (bfd_error_no_symbols);
295 if (! xcoff_get_section_contents (abfd, lsec))
297 contents = coff_section_data (abfd, lsec)->contents;
299 coff_section_data (abfd, lsec)->keep_contents = TRUE;
301 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
303 strings = (char *) contents + ldhdr.l_stoff;
305 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
309 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
311 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
312 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
314 struct internal_ldsym ldsym;
316 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
318 symbuf->symbol.the_bfd = abfd;
320 if (ldsym._l._l_l._l_zeroes == 0)
321 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
326 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
329 memcpy (c, ldsym._l._l_name, SYMNMLEN);
331 symbuf->symbol.name = c;
334 if (ldsym.l_smclas == XMC_XO)
335 symbuf->symbol.section = bfd_abs_section_ptr;
337 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
339 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
341 symbuf->symbol.flags = BSF_NO_FLAGS;
342 if ((ldsym.l_smtype & L_EXPORT) != 0)
344 if ((ldsym.l_smtype & L_WEAK) != 0)
345 symbuf->symbol.flags |= BSF_WEAK;
347 symbuf->symbol.flags |= BSF_GLOBAL;
350 /* FIXME: We have no way to record the other information stored
351 with the loader symbol. */
352 *psyms = (asymbol *) symbuf;
357 return ldhdr.l_nsyms;
360 /* Get the size required to hold the dynamic relocs. */
363 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
367 struct internal_ldhdr ldhdr;
369 if ((abfd->flags & DYNAMIC) == 0)
371 bfd_set_error (bfd_error_invalid_operation);
375 lsec = bfd_get_section_by_name (abfd, ".loader");
378 bfd_set_error (bfd_error_no_symbols);
382 if (! xcoff_get_section_contents (abfd, lsec))
384 contents = coff_section_data (abfd, lsec)->contents;
386 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
388 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
391 /* Get the dynamic relocs. */
394 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
400 struct internal_ldhdr ldhdr;
402 bfd_byte *elrel, *elrelend;
404 if ((abfd->flags & DYNAMIC) == 0)
406 bfd_set_error (bfd_error_invalid_operation);
410 lsec = bfd_get_section_by_name (abfd, ".loader");
413 bfd_set_error (bfd_error_no_symbols);
417 if (! xcoff_get_section_contents (abfd, lsec))
419 contents = coff_section_data (abfd, lsec)->contents;
421 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
423 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
427 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
429 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
430 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
433 struct internal_ldrel ldrel;
435 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
437 if (ldrel.l_symndx >= 3)
438 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
444 switch (ldrel.l_symndx)
460 sec = bfd_get_section_by_name (abfd, name);
463 bfd_set_error (bfd_error_bad_value);
467 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
470 relbuf->address = ldrel.l_vaddr;
473 /* Most dynamic relocs have the same type. FIXME: This is only
474 correct if ldrel.l_rtype == 0. In other cases, we should use
475 a different howto. */
476 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
478 /* FIXME: We have no way to record the l_rsecnm field. */
485 return ldhdr.l_nreloc;
488 /* Hash functions for xcoff_link_hash_table's archive_info. */
491 xcoff_archive_info_hash (const void *data)
493 const struct xcoff_archive_info *info;
495 info = (const struct xcoff_archive_info *) data;
496 return htab_hash_pointer (info->archive);
500 xcoff_archive_info_eq (const void *data1, const void *data2)
502 const struct xcoff_archive_info *info1;
503 const struct xcoff_archive_info *info2;
505 info1 = (const struct xcoff_archive_info *) data1;
506 info2 = (const struct xcoff_archive_info *) data2;
507 return info1->archive == info2->archive;
510 /* Return information about archive ARCHIVE. Return NULL on error. */
512 static struct xcoff_archive_info *
513 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
515 struct xcoff_link_hash_table *htab;
516 struct xcoff_archive_info *entryp, entry;
519 htab = xcoff_hash_table (info);
520 entry.archive = archive;
521 slot = htab_find_slot (htab->archive_info, &entry, INSERT);
528 entryp = bfd_zalloc (archive, sizeof (entry));
532 entryp->archive = archive;
538 /* Routine to create an entry in an XCOFF link hash table. */
540 static struct bfd_hash_entry *
541 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
542 struct bfd_hash_table *table,
545 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
547 /* Allocate the structure if it has not already been allocated by a
550 ret = bfd_hash_allocate (table, sizeof (* ret));
554 /* Call the allocation method of the superclass. */
555 ret = ((struct xcoff_link_hash_entry *)
556 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
560 /* Set local fields. */
562 ret->toc_section = NULL;
563 ret->u.toc_indx = -1;
564 ret->descriptor = NULL;
568 ret->smclas = XMC_UA;
571 return (struct bfd_hash_entry *) ret;
574 /* Destroy an XCOFF link hash table. */
577 _bfd_xcoff_bfd_link_hash_table_free (bfd *obfd)
579 struct xcoff_link_hash_table *ret;
581 ret = (struct xcoff_link_hash_table *) obfd->link.hash;
582 if (ret->archive_info)
583 htab_delete (ret->archive_info);
584 if (ret->debug_strtab)
585 _bfd_stringtab_free (ret->debug_strtab);
586 _bfd_generic_link_hash_table_free (obfd);
589 /* Create an XCOFF link hash table. */
591 struct bfd_link_hash_table *
592 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
594 struct xcoff_link_hash_table *ret;
595 bfd_size_type amt = sizeof (* ret);
597 ret = bfd_zmalloc (amt);
600 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
601 sizeof (struct xcoff_link_hash_entry)))
607 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
608 ret->archive_info = htab_create (37, xcoff_archive_info_hash,
609 xcoff_archive_info_eq, NULL);
610 if (!ret->debug_strtab || !ret->archive_info)
612 _bfd_xcoff_bfd_link_hash_table_free (abfd);
615 ret->root.hash_table_free = _bfd_xcoff_bfd_link_hash_table_free;
617 /* The linker will always generate a full a.out header. We need to
618 record that fact now, before the sizeof_headers routine could be
620 xcoff_data (abfd)->full_aouthdr = TRUE;
625 /* Read internal relocs for an XCOFF csect. This is a wrapper around
626 _bfd_coff_read_internal_relocs which tries to take advantage of any
627 relocs which may have been cached for the enclosing section. */
629 static struct internal_reloc *
630 xcoff_read_internal_relocs (bfd *abfd,
633 bfd_byte *external_relocs,
634 bfd_boolean require_internal,
635 struct internal_reloc *internal_relocs)
637 if (coff_section_data (abfd, sec) != NULL
638 && coff_section_data (abfd, sec)->relocs == NULL
639 && xcoff_section_data (abfd, sec) != NULL)
643 enclosing = xcoff_section_data (abfd, sec)->enclosing;
645 if (enclosing != NULL
646 && (coff_section_data (abfd, enclosing) == NULL
647 || coff_section_data (abfd, enclosing)->relocs == NULL)
649 && enclosing->reloc_count > 0)
651 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
652 external_relocs, FALSE, NULL)
657 if (enclosing != NULL
658 && coff_section_data (abfd, enclosing) != NULL
659 && coff_section_data (abfd, enclosing)->relocs != NULL)
663 off = ((sec->rel_filepos - enclosing->rel_filepos)
664 / bfd_coff_relsz (abfd));
666 if (! require_internal)
667 return coff_section_data (abfd, enclosing)->relocs + off;
668 memcpy (internal_relocs,
669 coff_section_data (abfd, enclosing)->relocs + off,
670 sec->reloc_count * sizeof (struct internal_reloc));
671 return internal_relocs;
675 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
676 require_internal, internal_relocs);
679 /* Split FILENAME into an import path and an import filename,
680 storing them in *IMPPATH and *IMPFILE respectively. */
683 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
684 const char **imppath, const char **impfile)
690 base = lbasename (filename);
691 length = base - filename;
693 /* The filename has no directory component, so use an empty path. */
695 else if (length == 1)
696 /* The filename is in the root directory. */
700 /* Extract the (non-empty) directory part. Note that we don't
701 need to strip duplicate directory separators from any part
702 of the string; the native linker doesn't do that either. */
703 path = bfd_alloc (abfd, length);
706 memcpy (path, filename, length - 1);
707 path[length - 1] = 0;
714 /* Set ARCHIVE's import path as though its filename had been given
718 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
719 bfd *archive, const char *filename)
721 struct xcoff_archive_info *archive_info;
723 archive_info = xcoff_get_archive_info (info, archive);
724 return (archive_info != NULL
725 && bfd_xcoff_split_import_path (archive, filename,
726 &archive_info->imppath,
727 &archive_info->impfile));
730 /* H is an imported symbol. Set the import module's path, file and member
731 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
732 no specific import module is specified. */
735 xcoff_set_import_path (struct bfd_link_info *info,
736 struct xcoff_link_hash_entry *h,
737 const char *imppath, const char *impfile,
738 const char *impmember)
741 struct xcoff_import_file **pp;
743 /* We overload the ldindx field to hold the l_ifile value for this
745 BFD_ASSERT (h->ldsym == NULL);
746 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
751 /* We start c at 1 because the first entry in the import list is
752 reserved for the library search path. */
753 for (pp = &xcoff_hash_table (info)->imports, c = 1;
755 pp = &(*pp)->next, ++c)
757 if (filename_cmp ((*pp)->path, imppath) == 0
758 && filename_cmp ((*pp)->file, impfile) == 0
759 && filename_cmp ((*pp)->member, impmember) == 0)
765 struct xcoff_import_file *n;
766 bfd_size_type amt = sizeof (* n);
768 n = bfd_alloc (info->output_bfd, amt);
774 n->member = impmember;
782 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
783 Return true if LDSYM defines H. */
786 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
787 struct internal_ldsym *ldsym)
789 /* If we didn't know about H before processing LDSYM, LDSYM
790 definitely defines H. */
791 if (h->root.type == bfd_link_hash_new)
794 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
795 dynamic symbol, LDSYM trumps the current definition of H. */
796 if ((ldsym->l_smtype & L_WEAK) == 0
797 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
798 && (h->flags & XCOFF_DEF_REGULAR) == 0
799 && (h->root.type == bfd_link_hash_defweak
800 || h->root.type == bfd_link_hash_undefweak))
803 /* If H is currently undefined, LDSYM defines it. */
804 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
805 && (h->root.type == bfd_link_hash_undefined
806 || h->root.type == bfd_link_hash_undefweak))
812 /* This function is used to add symbols from a dynamic object to the
813 global symbol table. */
816 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
820 struct internal_ldhdr ldhdr;
822 bfd_byte *elsym, *elsymend;
823 struct xcoff_import_file *n;
825 struct xcoff_import_file **pp;
827 /* We can only handle a dynamic object if we are generating an XCOFF
829 if (info->output_bfd->xvec != abfd->xvec)
832 (_("%pB: XCOFF shared object when not producing XCOFF output"),
834 bfd_set_error (bfd_error_invalid_operation);
838 /* The symbols we use from a dynamic object are not the symbols in
839 the normal symbol table, but, rather, the symbols in the export
840 table. If there is a global symbol in a dynamic object which is
841 not in the export table, the loader will not be able to find it,
842 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
843 libc.a has symbols in the export table which are not in the
846 /* Read in the .loader section. FIXME: We should really use the
847 o_snloader field in the a.out header, rather than grabbing the
849 lsec = bfd_get_section_by_name (abfd, ".loader");
853 (_("%pB: dynamic object with no .loader section"),
855 bfd_set_error (bfd_error_no_symbols);
859 if (! xcoff_get_section_contents (abfd, lsec))
861 contents = coff_section_data (abfd, lsec)->contents;
863 /* Remove the sections from this object, so that they do not get
864 included in the link. */
865 bfd_section_list_clear (abfd);
867 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
869 strings = (char *) contents + ldhdr.l_stoff;
871 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
873 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
875 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
877 struct internal_ldsym ldsym;
878 char nambuf[SYMNMLEN + 1];
880 struct xcoff_link_hash_entry *h;
882 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
884 /* We are only interested in exported symbols. */
885 if ((ldsym.l_smtype & L_EXPORT) == 0)
888 if (ldsym._l._l_l._l_zeroes == 0)
889 name = strings + ldsym._l._l_l._l_offset;
892 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
893 nambuf[SYMNMLEN] = '\0';
897 /* Normally we could not call xcoff_link_hash_lookup in an add
898 symbols routine, since we might not be using an XCOFF hash
899 table. However, we verified above that we are using an XCOFF
902 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
907 if (!xcoff_dynamic_definition_p (h, &ldsym))
910 h->flags |= XCOFF_DEF_DYNAMIC;
911 h->smclas = ldsym.l_smclas;
912 if (h->smclas == XMC_XO)
914 /* This symbol has an absolute value. */
915 if ((ldsym.l_smtype & L_WEAK) != 0)
916 h->root.type = bfd_link_hash_defweak;
918 h->root.type = bfd_link_hash_defined;
919 h->root.u.def.section = bfd_abs_section_ptr;
920 h->root.u.def.value = ldsym.l_value;
924 /* Otherwise, we don't bother to actually define the symbol,
925 since we don't have a section to put it in anyhow.
926 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
927 should be imported from the symbol's undef.abfd. */
928 if ((ldsym.l_smtype & L_WEAK) != 0)
929 h->root.type = bfd_link_hash_undefweak;
931 h->root.type = bfd_link_hash_undefined;
932 h->root.u.undef.abfd = abfd;
935 /* If this symbol defines a function descriptor, then it
936 implicitly defines the function code as well. */
937 if (h->smclas == XMC_DS
938 || (h->smclas == XMC_XO && name[0] != '.'))
939 h->flags |= XCOFF_DESCRIPTOR;
940 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
942 struct xcoff_link_hash_entry *hds;
949 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
953 strcpy (dsnm + 1, name);
954 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
964 if (xcoff_dynamic_definition_p (hds, &ldsym))
966 hds->root.type = h->root.type;
967 hds->flags |= XCOFF_DEF_DYNAMIC;
968 if (h->smclas == XMC_XO)
970 /* An absolute symbol appears to actually define code, not a
971 function descriptor. This is how some math functions are
972 implemented on AIX 4.1. */
973 hds->smclas = XMC_XO;
974 hds->root.u.def.section = bfd_abs_section_ptr;
975 hds->root.u.def.value = ldsym.l_value;
979 hds->smclas = XMC_PR;
980 hds->root.u.undef.abfd = abfd;
981 /* We do not want to add this to the undefined
988 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
990 free (coff_section_data (abfd, lsec)->contents);
991 coff_section_data (abfd, lsec)->contents = NULL;
994 /* Record this file in the import files. */
995 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
1000 if (abfd->my_archive == NULL || bfd_is_thin_archive (abfd->my_archive))
1002 if (!bfd_xcoff_split_import_path (abfd, abfd->filename,
1003 &n->path, &n->file))
1009 struct xcoff_archive_info *archive_info;
1011 archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1012 if (!archive_info->impfile)
1014 if (!bfd_xcoff_split_import_path (archive_info->archive,
1015 archive_info->archive->filename,
1016 &archive_info->imppath,
1017 &archive_info->impfile))
1020 n->path = archive_info->imppath;
1021 n->file = archive_info->impfile;
1022 n->member = bfd_get_filename (abfd);
1025 /* We start c at 1 because the first import file number is reserved
1027 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1029 pp = &(*pp)->next, ++c)
1033 xcoff_data (abfd)->import_file_id = c;
1038 /* xcoff_link_create_extra_sections
1040 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1043 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1045 bfd_boolean return_value = FALSE;
1047 if (info->output_bfd->xvec == abfd->xvec)
1049 /* We need to build a .loader section, so we do it here. This
1050 won't work if we're producing an XCOFF output file with no
1051 XCOFF input files. FIXME. */
1053 if (!bfd_link_relocatable (info)
1054 && xcoff_hash_table (info)->loader_section == NULL)
1057 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1059 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1063 xcoff_hash_table (info)->loader_section = lsec;
1066 /* Likewise for the linkage section. */
1067 if (xcoff_hash_table (info)->linkage_section == NULL)
1070 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1073 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1077 xcoff_hash_table (info)->linkage_section = lsec;
1078 lsec->alignment_power = 2;
1081 /* Likewise for the TOC section. */
1082 if (xcoff_hash_table (info)->toc_section == NULL)
1085 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1088 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1092 xcoff_hash_table (info)->toc_section = tsec;
1093 tsec->alignment_power = 2;
1096 /* Likewise for the descriptor section. */
1097 if (xcoff_hash_table (info)->descriptor_section == NULL)
1100 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1103 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1107 xcoff_hash_table (info)->descriptor_section = dsec;
1108 dsec->alignment_power = 2;
1111 /* Likewise for the .debug section. */
1112 if (xcoff_hash_table (info)->debug_section == NULL
1113 && info->strip != strip_all)
1116 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1118 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1122 xcoff_hash_table (info)->debug_section = dsec;
1126 return_value = TRUE;
1130 return return_value;
1133 /* Returns the index of reloc in RELOCS with the least address greater
1134 than or equal to ADDRESS. The relocs are sorted by address. */
1136 static bfd_size_type
1137 xcoff_find_reloc (struct internal_reloc *relocs,
1138 bfd_size_type count,
1141 bfd_size_type min, max, this;
1145 if (count == 1 && relocs[0].r_vaddr < address)
1154 /* Do a binary search over (min,max]. */
1155 while (min + 1 < max)
1159 this = (max + min) / 2;
1160 raddr = relocs[this].r_vaddr;
1161 if (raddr > address)
1163 else if (raddr < address)
1172 if (relocs[min].r_vaddr < address)
1176 && relocs[min - 1].r_vaddr == address)
1182 /* Add all the symbols from an object file to the hash table.
1184 XCOFF is a weird format. A normal XCOFF .o files will have three
1185 COFF sections--.text, .data, and .bss--but each COFF section will
1186 contain many csects. These csects are described in the symbol
1187 table. From the linker's point of view, each csect must be
1188 considered a section in its own right. For example, a TOC entry is
1189 handled as a small XMC_TC csect. The linker must be able to merge
1190 different TOC entries together, which means that it must be able to
1191 extract the XMC_TC csects from the .data section of the input .o
1194 From the point of view of our linker, this is, of course, a hideous
1195 nightmare. We cope by actually creating sections for each csect,
1196 and discarding the original sections. We then have to handle the
1197 relocation entries carefully, since the only way to tell which
1198 csect they belong to is to examine the address. */
1201 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1203 unsigned int n_tmask;
1204 unsigned int n_btshft;
1205 bfd_boolean default_copy;
1206 bfd_size_type symcount;
1207 struct xcoff_link_hash_entry **sym_hash;
1208 asection **csect_cache;
1209 unsigned int *lineno_counts;
1210 bfd_size_type linesz;
1212 asection *last_real;
1213 bfd_boolean keep_syms;
1215 unsigned int csect_index;
1216 asection *first_csect;
1217 bfd_size_type symesz;
1220 struct reloc_info_struct
1222 struct internal_reloc *relocs;
1225 } *reloc_info = NULL;
1228 keep_syms = obj_coff_keep_syms (abfd);
1230 if ((abfd->flags & DYNAMIC) != 0
1231 && ! info->static_link)
1233 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1237 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1238 if (! xcoff_link_create_extra_sections (abfd, info))
1241 if ((abfd->flags & DYNAMIC) != 0
1242 && ! info->static_link)
1245 n_tmask = coff_data (abfd)->local_n_tmask;
1246 n_btshft = coff_data (abfd)->local_n_btshft;
1248 /* Define macros so that ISFCN, et. al., macros work correctly. */
1249 #define N_TMASK n_tmask
1250 #define N_BTSHFT n_btshft
1252 if (info->keep_memory)
1253 default_copy = FALSE;
1255 default_copy = TRUE;
1257 symcount = obj_raw_syment_count (abfd);
1259 /* We keep a list of the linker hash table entries that correspond
1260 to each external symbol. */
1261 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1262 sym_hash = bfd_zalloc (abfd, amt);
1263 if (sym_hash == NULL && symcount != 0)
1265 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1267 /* Because of the weird stuff we are doing with XCOFF csects, we can
1268 not easily determine which section a symbol is in, so we store
1269 the information in the tdata for the input file. */
1270 amt = symcount * sizeof (asection *);
1271 csect_cache = bfd_zalloc (abfd, amt);
1272 if (csect_cache == NULL && symcount != 0)
1274 xcoff_data (abfd)->csects = csect_cache;
1276 /* We garbage-collect line-number information on a symbol-by-symbol
1277 basis, so we need to have quick access to the number of entries
1279 amt = symcount * sizeof (unsigned int);
1280 lineno_counts = bfd_zalloc (abfd, amt);
1281 if (lineno_counts == NULL && symcount != 0)
1283 xcoff_data (abfd)->lineno_counts = lineno_counts;
1285 /* While splitting sections into csects, we need to assign the
1286 relocs correctly. The relocs and the csects must both be in
1287 order by VMA within a given section, so we handle this by
1288 scanning along the relocs as we process the csects. We index
1289 into reloc_info using the section target_index. */
1290 amt = abfd->section_count + 1;
1291 amt *= sizeof (struct reloc_info_struct);
1292 reloc_info = bfd_zmalloc (amt);
1293 if (reloc_info == NULL)
1296 /* Read in the relocs and line numbers for each section. */
1297 linesz = bfd_coff_linesz (abfd);
1299 for (o = abfd->sections; o != NULL; o = o->next)
1303 if ((o->flags & SEC_RELOC) != 0)
1305 reloc_info[o->target_index].relocs =
1306 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1307 amt = o->reloc_count;
1308 amt *= sizeof (asection *);
1309 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1310 if (reloc_info[o->target_index].csects == NULL)
1314 if ((info->strip == strip_none || info->strip == strip_some)
1315 && o->lineno_count > 0)
1319 amt = linesz * o->lineno_count;
1320 linenos = bfd_malloc (amt);
1321 if (linenos == NULL)
1323 reloc_info[o->target_index].linenos = linenos;
1324 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1325 || bfd_bread (linenos, amt, abfd) != amt)
1330 /* Don't let the linker relocation routines discard the symbols. */
1331 obj_coff_keep_syms (abfd) = TRUE;
1337 symesz = bfd_coff_symesz (abfd);
1338 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1339 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1340 esym_end = esym + symcount * symesz;
1342 while (esym < esym_end)
1344 struct internal_syment sym;
1345 union internal_auxent aux;
1347 char buf[SYMNMLEN + 1];
1351 struct xcoff_link_hash_entry *set_toc;
1353 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1355 /* In this pass we are only interested in symbols with csect
1357 if (!CSECT_SYM_P (sym.n_sclass))
1360 Normally csect is a .pr, .rw etc. created in the loop
1361 If C_FILE or first time, handle special
1363 Advance esym, sym_hash, csect_hash ptrs. */
1364 if (sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1367 *csect_cache = csect;
1368 else if (first_csect == NULL
1369 || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1370 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1372 *csect_cache = NULL;
1373 esym += (sym.n_numaux + 1) * symesz;
1374 sym_hash += sym.n_numaux + 1;
1375 csect_cache += sym.n_numaux + 1;
1376 lineno_counts += sym.n_numaux + 1;
1381 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1386 /* If this symbol has line number information attached to it,
1387 and we're not stripping it, count the number of entries and
1388 add them to the count for this csect. In the final link pass
1389 we are going to attach line number information by symbol,
1390 rather than by section, in order to more easily handle
1391 garbage collection. */
1392 if ((info->strip == strip_none || info->strip == strip_some)
1395 && ISFCN (sym.n_type))
1397 union internal_auxent auxlin;
1399 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1400 sym.n_type, sym.n_sclass,
1401 0, sym.n_numaux, (void *) &auxlin);
1403 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1405 asection *enclosing;
1406 bfd_signed_vma linoff;
1408 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1409 if (enclosing == NULL)
1412 /* xgettext:c-format */
1413 (_("%pB: `%s' has line numbers but no enclosing section"),
1415 bfd_set_error (bfd_error_bad_value);
1418 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1419 - enclosing->line_filepos);
1420 /* Explicit cast to bfd_signed_vma for compiler. */
1421 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1423 struct internal_lineno lin;
1424 bfd_byte *linpstart;
1426 linpstart = (reloc_info[enclosing->target_index].linenos
1428 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1430 && ((bfd_size_type) lin.l_addr.l_symndx
1432 - (bfd_byte *) obj_coff_external_syms (abfd))
1435 bfd_byte *linpend, *linp;
1437 linpend = (reloc_info[enclosing->target_index].linenos
1438 + enclosing->lineno_count * linesz);
1439 for (linp = linpstart + linesz;
1443 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1445 if (lin.l_lnno == 0)
1448 *lineno_counts = (linp - linpstart) / linesz;
1449 /* The setting of line_filepos will only be
1450 useful if all the line number entries for a
1451 csect are contiguous; this only matters for
1453 if (csect->line_filepos == 0)
1454 csect->line_filepos =
1455 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1461 /* Pick up the csect auxiliary information. */
1462 if (sym.n_numaux == 0)
1465 /* xgettext:c-format */
1466 (_("%pB: class %d symbol `%s' has no aux entries"),
1467 abfd, sym.n_sclass, name);
1468 bfd_set_error (bfd_error_bad_value);
1472 bfd_coff_swap_aux_in (abfd,
1473 (void *) (esym + symesz * sym.n_numaux),
1474 sym.n_type, sym.n_sclass,
1475 sym.n_numaux - 1, sym.n_numaux,
1478 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1488 /* xgettext:c-format */
1489 (_("%pB: symbol `%s' has unrecognized csect type %d"),
1491 bfd_set_error (bfd_error_bad_value);
1495 /* This is an external reference. */
1496 if (sym.n_sclass == C_HIDEXT
1497 || sym.n_scnum != N_UNDEF
1498 || aux.x_csect.x_scnlen.l != 0)
1501 /* xgettext:c-format */
1502 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1504 abfd, name, sym.n_sclass, sym.n_scnum,
1505 (int64_t) aux.x_csect.x_scnlen.l);
1506 bfd_set_error (bfd_error_bad_value);
1510 /* An XMC_XO external reference is actually a reference to
1511 an absolute location. */
1512 if (aux.x_csect.x_smclas != XMC_XO)
1513 section = bfd_und_section_ptr;
1516 section = bfd_abs_section_ptr;
1517 value = sym.n_value;
1523 csect_index = -(unsigned) 1;
1525 /* When we see a TOC anchor, we record the TOC value. */
1526 if (aux.x_csect.x_smclas == XMC_TC0)
1528 if (sym.n_sclass != C_HIDEXT
1529 || aux.x_csect.x_scnlen.l != 0)
1532 /* xgettext:c-format */
1533 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64),
1534 abfd, name, sym.n_sclass, (int64_t) aux.x_csect.x_scnlen.l);
1535 bfd_set_error (bfd_error_bad_value);
1538 xcoff_data (abfd)->toc = sym.n_value;
1541 /* We must merge TOC entries for the same symbol. We can
1542 merge two TOC entries if they are both C_HIDEXT, they
1543 both have the same name, they are both 4 or 8 bytes long, and
1544 they both have a relocation table entry for an external
1545 symbol with the same name. Unfortunately, this means
1546 that we must look through the relocations. Ick.
1548 Logic for 32 bit vs 64 bit.
1549 32 bit has a csect length of 4 for TOC
1550 64 bit has a csect length of 8 for TOC
1552 The conditions to get past the if-check are not that bad.
1553 They are what is used to create the TOC csects in the first
1555 if (aux.x_csect.x_smclas == XMC_TC
1556 && sym.n_sclass == C_HIDEXT
1557 && info->output_bfd->xvec == abfd->xvec
1558 && ((bfd_xcoff_is_xcoff32 (abfd)
1559 && aux.x_csect.x_scnlen.l == 4)
1560 || (bfd_xcoff_is_xcoff64 (abfd)
1561 && aux.x_csect.x_scnlen.l == 8)))
1563 asection *enclosing;
1564 struct internal_reloc *relocs;
1565 bfd_size_type relindx;
1566 struct internal_reloc *rel;
1568 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1569 if (enclosing == NULL)
1572 relocs = reloc_info[enclosing->target_index].relocs;
1573 amt = enclosing->reloc_count;
1574 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1575 rel = relocs + relindx;
1577 /* 32 bit R_POS r_size is 31
1578 64 bit R_POS r_size is 63 */
1579 if (relindx < enclosing->reloc_count
1580 && rel->r_vaddr == (bfd_vma) sym.n_value
1581 && rel->r_type == R_POS
1582 && ((bfd_xcoff_is_xcoff32 (abfd)
1583 && rel->r_size == 31)
1584 || (bfd_xcoff_is_xcoff64 (abfd)
1585 && rel->r_size == 63)))
1589 struct internal_syment relsym;
1591 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1592 + rel->r_symndx * symesz);
1593 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1594 if (EXTERN_SYM_P (relsym.n_sclass))
1596 const char *relname;
1597 char relbuf[SYMNMLEN + 1];
1599 struct xcoff_link_hash_entry *h;
1601 /* At this point we know that the TOC entry is
1602 for an externally visible symbol. */
1603 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1605 if (relname == NULL)
1608 /* We only merge TOC entries if the TC name is
1609 the same as the symbol name. This handles
1610 the normal case, but not common cases like
1611 SYM.P4 which gcc generates to store SYM + 4
1612 in the TOC. FIXME. */
1613 if (strcmp (name, relname) == 0)
1615 copy = (! info->keep_memory
1616 || relsym._n._n_n._n_zeroes != 0
1617 || relsym._n._n_n._n_offset == 0);
1618 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1619 relname, TRUE, copy,
1624 /* At this point h->root.type could be
1625 bfd_link_hash_new. That should be OK,
1626 since we know for sure that we will come
1627 across this symbol as we step through the
1630 /* We store h in *sym_hash for the
1631 convenience of the relocate_section
1635 if (h->toc_section != NULL)
1637 asection **rel_csects;
1639 /* We already have a TOC entry for this
1640 symbol, so we can just ignore this
1643 reloc_info[enclosing->target_index].csects;
1644 rel_csects[relindx] = bfd_und_section_ptr;
1648 /* We are about to create a TOC entry for
1657 asection *enclosing;
1659 /* We need to create a new section. We get the name from
1660 the csect storage mapping class, so that the linker can
1661 accumulate similar csects together. */
1663 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1667 /* The enclosing section is the main section : .data, .text
1668 or .bss that the csect is coming from. */
1669 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1670 if (enclosing == NULL)
1673 if (! bfd_is_abs_section (enclosing)
1674 && ((bfd_vma) sym.n_value < enclosing->vma
1675 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1676 > enclosing->vma + enclosing->size)))
1679 /* xgettext:c-format */
1680 (_("%pB: csect `%s' not in enclosing section"),
1682 bfd_set_error (bfd_error_bad_value);
1685 csect->vma = sym.n_value;
1686 csect->filepos = (enclosing->filepos
1689 csect->size = aux.x_csect.x_scnlen.l;
1690 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1691 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1693 /* Record the enclosing section in the tdata for this new
1695 amt = sizeof (struct coff_section_tdata);
1696 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1697 if (csect->used_by_bfd == NULL)
1699 amt = sizeof (struct xcoff_section_tdata);
1700 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1701 if (coff_section_data (abfd, csect)->tdata == NULL)
1703 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1704 xcoff_section_data (abfd, csect)->lineno_count =
1705 enclosing->lineno_count;
1707 if (enclosing->owner == abfd)
1709 struct internal_reloc *relocs;
1710 bfd_size_type relindx;
1711 struct internal_reloc *rel;
1712 asection **rel_csect;
1714 relocs = reloc_info[enclosing->target_index].relocs;
1715 amt = enclosing->reloc_count;
1716 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1718 rel = relocs + relindx;
1719 rel_csect = (reloc_info[enclosing->target_index].csects
1722 csect->rel_filepos = (enclosing->rel_filepos
1723 + relindx * bfd_coff_relsz (abfd));
1724 while (relindx < enclosing->reloc_count
1725 && *rel_csect == NULL
1726 && rel->r_vaddr < csect->vma + csect->size)
1730 csect->flags |= SEC_RELOC;
1731 ++csect->reloc_count;
1738 /* There are a number of other fields and section flags
1739 which we do not bother to set. */
1741 csect_index = ((esym
1742 - (bfd_byte *) obj_coff_external_syms (abfd))
1745 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1747 if (first_csect == NULL)
1748 first_csect = csect;
1750 /* If this symbol is external, we treat it as starting at the
1751 beginning of the newly created section. */
1752 if (EXTERN_SYM_P (sym.n_sclass))
1758 /* If this is a TOC section for a symbol, record it. */
1759 if (set_toc != NULL)
1760 set_toc->toc_section = csect;
1765 /* This is a label definition. The x_scnlen field is the
1766 symbol index of the csect. Usually the XTY_LD symbol will
1767 follow its appropriate XTY_SD symbol. The .set pseudo op can
1768 cause the XTY_LD to not follow the XTY_SD symbol. */
1773 if (aux.x_csect.x_scnlen.l < 0
1774 || (aux.x_csect.x_scnlen.l
1775 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1779 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1781 || (section->flags & SEC_HAS_CONTENTS) == 0)
1787 /* xgettext:c-format */
1788 (_("%pB: misplaced XTY_LD `%s'"),
1790 bfd_set_error (bfd_error_bad_value);
1794 value = sym.n_value - csect->vma;
1799 /* This is an unitialized csect. We could base the name on
1800 the storage mapping class, but we don't bother except for
1801 an XMC_TD symbol. If this csect is externally visible,
1802 it is a common symbol. We put XMC_TD symbols in sections
1803 named .tocbss, and rely on the linker script to put that
1806 if (aux.x_csect.x_smclas == XMC_TD)
1808 /* The linker script puts the .td section in the data
1809 section after the .tc section. */
1810 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1814 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1819 csect->vma = sym.n_value;
1820 csect->size = aux.x_csect.x_scnlen.l;
1821 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1822 /* There are a number of other fields and section flags
1823 which we do not bother to set. */
1825 csect_index = ((esym
1826 - (bfd_byte *) obj_coff_external_syms (abfd))
1829 amt = sizeof (struct coff_section_tdata);
1830 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1831 if (csect->used_by_bfd == NULL)
1833 amt = sizeof (struct xcoff_section_tdata);
1834 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1835 if (coff_section_data (abfd, csect)->tdata == NULL)
1837 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1839 if (first_csect == NULL)
1840 first_csect = csect;
1842 if (EXTERN_SYM_P (sym.n_sclass))
1844 csect->flags |= SEC_IS_COMMON;
1847 value = aux.x_csect.x_scnlen.l;
1853 /* Check for magic symbol names. */
1854 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1855 && aux.x_csect.x_smclas != XMC_TC
1856 && aux.x_csect.x_smclas != XMC_TD)
1862 if (strcmp (name, "_text") == 0)
1863 i = XCOFF_SPECIAL_SECTION_TEXT;
1864 else if (strcmp (name, "_etext") == 0)
1865 i = XCOFF_SPECIAL_SECTION_ETEXT;
1866 else if (strcmp (name, "_data") == 0)
1867 i = XCOFF_SPECIAL_SECTION_DATA;
1868 else if (strcmp (name, "_edata") == 0)
1869 i = XCOFF_SPECIAL_SECTION_EDATA;
1870 else if (strcmp (name, "_end") == 0)
1871 i = XCOFF_SPECIAL_SECTION_END;
1873 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1874 i = XCOFF_SPECIAL_SECTION_END2;
1877 xcoff_hash_table (info)->special_sections[i] = csect;
1880 /* Now we have enough information to add the symbol to the
1881 linker hash table. */
1883 if (EXTERN_SYM_P (sym.n_sclass))
1888 BFD_ASSERT (section != NULL);
1890 /* We must copy the name into memory if we got it from the
1891 syment itself, rather than the string table. */
1892 copy = default_copy;
1893 if (sym._n._n_n._n_zeroes != 0
1894 || sym._n._n_n._n_offset == 0)
1897 /* Ignore global linkage code when linking statically. */
1898 if (info->static_link
1899 && (smtyp == XTY_SD || smtyp == XTY_LD)
1900 && aux.x_csect.x_smclas == XMC_GL)
1902 section = bfd_und_section_ptr;
1906 /* The AIX linker appears to only detect multiple symbol
1907 definitions when there is a reference to the symbol. If
1908 a symbol is defined multiple times, and the only
1909 references are from the same object file, the AIX linker
1910 appears to permit it. It does not merge the different
1911 definitions, but handles them independently. On the
1912 other hand, if there is a reference, the linker reports
1915 This matters because the AIX <net/net_globals.h> header
1916 file actually defines an initialized array, so we have to
1917 actually permit that to work.
1919 Just to make matters even more confusing, the AIX linker
1920 appears to permit multiple symbol definitions whenever
1921 the second definition is in an archive rather than an
1922 object file. This may be a consequence of the manner in
1923 which it handles archives: I think it may load the entire
1924 archive in as separate csects, and then let garbage
1925 collection discard symbols.
1927 We also have to handle the case of statically linking a
1928 shared object, which will cause symbol redefinitions,
1929 although this is an easier case to detect. */
1930 else if (info->output_bfd->xvec == abfd->xvec)
1932 if (! bfd_is_und_section (section))
1933 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1934 name, TRUE, copy, FALSE);
1936 /* Make a copy of the symbol name to prevent problems with
1938 *sym_hash = ((struct xcoff_link_hash_entry *)
1939 bfd_wrapped_link_hash_lookup (abfd, info, name,
1940 TRUE, TRUE, FALSE));
1942 if (*sym_hash == NULL)
1944 if (((*sym_hash)->root.type == bfd_link_hash_defined
1945 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1946 && ! bfd_is_und_section (section)
1947 && ! bfd_is_com_section (section))
1949 /* This is a second definition of a defined symbol. */
1950 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1951 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1953 /* The existing symbol is from a shared library.
1955 (*sym_hash)->root.type = bfd_link_hash_undefined;
1956 (*sym_hash)->root.u.undef.abfd =
1957 (*sym_hash)->root.u.def.section->owner;
1959 else if (abfd->my_archive != NULL)
1961 /* This is a redefinition in an object contained
1962 in an archive. Just ignore it. See the
1964 section = bfd_und_section_ptr;
1967 else if (sym.n_sclass == C_AIX_WEAKEXT
1968 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1970 /* At least one of the definitions is weak.
1971 Allow the normal rules to take effect. */
1973 else if ((*sym_hash)->root.u.undef.next != NULL
1974 || info->hash->undefs_tail == &(*sym_hash)->root)
1976 /* This symbol has been referenced. In this
1977 case, we just continue and permit the
1978 multiple definition error. See the comment
1979 above about the behaviour of the AIX linker. */
1981 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1983 /* The symbols are both csects of the same
1984 class. There is at least a chance that this
1985 is a semi-legitimate redefinition. */
1986 section = bfd_und_section_ptr;
1988 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1991 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1992 && (*sym_hash)->root.type == bfd_link_hash_defined
1993 && (bfd_is_und_section (section)
1994 || bfd_is_com_section (section)))
1996 /* This is a reference to a multiply defined symbol.
1997 Report the error now. See the comment above
1998 about the behaviour of the AIX linker. We could
1999 also do this with warning symbols, but I'm not
2000 sure the XCOFF linker is wholly prepared to
2001 handle them, and that would only be a warning,
2003 (*info->callbacks->multiple_definition) (info,
2007 /* Try not to give this error too many times. */
2008 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2012 /* _bfd_generic_link_add_one_symbol may call the linker to
2013 generate an error message, and the linker may try to read
2014 the symbol table to give a good error. Right now, the
2015 line numbers are in an inconsistent state, since they are
2016 counted both in the real sections and in the new csects.
2017 We need to leave the count in the real sections so that
2018 the linker can report the line number of the error
2019 correctly, so temporarily clobber the link to the csects
2020 so that the linker will not try to read the line numbers
2021 a second time from the csects. */
2022 BFD_ASSERT (last_real->next == first_csect);
2023 last_real->next = NULL;
2024 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2025 if (! (_bfd_generic_link_add_one_symbol
2026 (info, abfd, name, flags, section, value,
2028 (struct bfd_link_hash_entry **) sym_hash)))
2030 last_real->next = first_csect;
2032 if (smtyp == XTY_CM)
2034 if ((*sym_hash)->root.type != bfd_link_hash_common
2035 || (*sym_hash)->root.u.c.p->section != csect)
2036 /* We don't need the common csect we just created. */
2039 (*sym_hash)->root.u.c.p->alignment_power
2040 = csect->alignment_power;
2043 if (info->output_bfd->xvec == abfd->xvec)
2049 || section == bfd_und_section_ptr)
2050 flag = XCOFF_REF_REGULAR;
2052 flag = XCOFF_DEF_REGULAR;
2053 (*sym_hash)->flags |= flag;
2055 if ((*sym_hash)->smclas == XMC_UA
2056 || flag == XCOFF_DEF_REGULAR)
2057 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2061 if (smtyp == XTY_ER)
2062 *csect_cache = section;
2065 *csect_cache = csect;
2067 xcoff_section_data (abfd, csect)->last_symndx
2068 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2071 esym += (sym.n_numaux + 1) * symesz;
2072 sym_hash += sym.n_numaux + 1;
2073 csect_cache += sym.n_numaux + 1;
2074 lineno_counts += sym.n_numaux + 1;
2077 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2079 /* Make sure that we have seen all the relocs. */
2080 for (o = abfd->sections; o != first_csect; o = o->next)
2082 /* Debugging sections have no csects. */
2083 if (bfd_get_section_flags (abfd, o) & SEC_DEBUGGING)
2086 /* Reset the section size and the line number count, since the
2087 data is now attached to the csects. Don't reset the size of
2088 the .debug section, since we need to read it below in
2089 bfd_xcoff_size_dynamic_sections. */
2090 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2092 o->lineno_count = 0;
2094 if ((o->flags & SEC_RELOC) != 0)
2097 struct internal_reloc *rel;
2098 asection **rel_csect;
2100 rel = reloc_info[o->target_index].relocs;
2101 rel_csect = reloc_info[o->target_index].csects;
2103 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2105 if (*rel_csect == NULL)
2108 /* xgettext:c-format */
2109 (_("%pB: reloc %s:%" PRId64 " not in csect"),
2110 abfd, o->name, (int64_t) i);
2111 bfd_set_error (bfd_error_bad_value);
2115 /* We identify all function symbols that are the target
2116 of a relocation, so that we can create glue code for
2117 functions imported from dynamic objects. */
2118 if (info->output_bfd->xvec == abfd->xvec
2119 && *rel_csect != bfd_und_section_ptr
2120 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2122 struct xcoff_link_hash_entry *h;
2124 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2125 /* If the symbol name starts with a period, it is
2126 the code of a function. If the symbol is
2127 currently undefined, then add an undefined symbol
2128 for the function descriptor. This should do no
2129 harm, because any regular object that defines the
2130 function should also define the function
2131 descriptor. It helps, because it means that we
2132 will identify the function descriptor with a
2133 dynamic object if a dynamic object defines it. */
2134 if (h->root.root.string[0] == '.'
2135 && h->descriptor == NULL)
2137 struct xcoff_link_hash_entry *hds;
2138 struct bfd_link_hash_entry *bh;
2140 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2141 h->root.root.string + 1,
2145 if (hds->root.type == bfd_link_hash_new)
2148 if (! (_bfd_generic_link_add_one_symbol
2149 (info, abfd, hds->root.root.string,
2150 (flagword) 0, bfd_und_section_ptr,
2151 (bfd_vma) 0, NULL, FALSE,
2154 hds = (struct xcoff_link_hash_entry *) bh;
2156 hds->flags |= XCOFF_DESCRIPTOR;
2157 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2158 hds->descriptor = h;
2159 h->descriptor = hds;
2161 if (h->root.root.string[0] == '.')
2162 h->flags |= XCOFF_CALLED;
2166 free (reloc_info[o->target_index].csects);
2167 reloc_info[o->target_index].csects = NULL;
2169 /* Reset SEC_RELOC and the reloc_count, since the reloc
2170 information is now attached to the csects. */
2171 o->flags &=~ SEC_RELOC;
2174 /* If we are not keeping memory, free the reloc information. */
2175 if (! info->keep_memory
2176 && coff_section_data (abfd, o) != NULL
2177 && coff_section_data (abfd, o)->relocs != NULL
2178 && ! coff_section_data (abfd, o)->keep_relocs)
2180 free (coff_section_data (abfd, o)->relocs);
2181 coff_section_data (abfd, o)->relocs = NULL;
2185 /* Free up the line numbers. FIXME: We could cache these
2186 somewhere for the final link, to avoid reading them again. */
2187 if (reloc_info[o->target_index].linenos != NULL)
2189 free (reloc_info[o->target_index].linenos);
2190 reloc_info[o->target_index].linenos = NULL;
2196 obj_coff_keep_syms (abfd) = keep_syms;
2201 if (reloc_info != NULL)
2203 for (o = abfd->sections; o != NULL; o = o->next)
2205 if (reloc_info[o->target_index].csects != NULL)
2206 free (reloc_info[o->target_index].csects);
2207 if (reloc_info[o->target_index].linenos != NULL)
2208 free (reloc_info[o->target_index].linenos);
2212 obj_coff_keep_syms (abfd) = keep_syms;
2219 /* Add symbols from an XCOFF object file. */
2222 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2224 if (! _bfd_coff_get_external_symbols (abfd))
2226 if (! xcoff_link_add_symbols (abfd, info))
2228 if (! info->keep_memory)
2230 if (! _bfd_coff_free_symbols (abfd))
2236 /* Look through the loader symbols to see if this dynamic object
2237 should be included in the link. The native linker uses the loader
2238 symbols, not the normal symbol table, so we do too. */
2241 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2242 struct bfd_link_info *info,
2243 bfd_boolean *pneeded,
2248 struct internal_ldhdr ldhdr;
2249 const char *strings;
2250 bfd_byte *elsym, *elsymend;
2254 lsec = bfd_get_section_by_name (abfd, ".loader");
2256 /* There are no symbols, so don't try to include it. */
2259 if (! xcoff_get_section_contents (abfd, lsec))
2261 contents = coff_section_data (abfd, lsec)->contents;
2263 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2265 strings = (char *) contents + ldhdr.l_stoff;
2267 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2269 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2270 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2272 struct internal_ldsym ldsym;
2273 char nambuf[SYMNMLEN + 1];
2275 struct bfd_link_hash_entry *h;
2277 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2279 /* We are only interested in exported symbols. */
2280 if ((ldsym.l_smtype & L_EXPORT) == 0)
2283 if (ldsym._l._l_l._l_zeroes == 0)
2284 name = strings + ldsym._l._l_l._l_offset;
2287 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2288 nambuf[SYMNMLEN] = '\0';
2292 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2294 /* We are only interested in symbols that are currently
2295 undefined. At this point we know that we are using an XCOFF
2298 && h->type == bfd_link_hash_undefined
2299 && (((struct xcoff_link_hash_entry *) h)->flags
2300 & XCOFF_DEF_DYNAMIC) == 0)
2302 if (!(*info->callbacks
2303 ->add_archive_element) (info, abfd, name, subsbfd))
2310 /* We do not need this shared object. */
2311 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2313 free (coff_section_data (abfd, lsec)->contents);
2314 coff_section_data (abfd, lsec)->contents = NULL;
2320 /* Look through the symbols to see if this object file should be
2321 included in the link. */
2324 xcoff_link_check_ar_symbols (bfd *abfd,
2325 struct bfd_link_info *info,
2326 bfd_boolean *pneeded,
2329 bfd_size_type symesz;
2335 if ((abfd->flags & DYNAMIC) != 0
2336 && ! info->static_link
2337 && info->output_bfd->xvec == abfd->xvec)
2338 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
2340 symesz = bfd_coff_symesz (abfd);
2341 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2342 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2343 while (esym < esym_end)
2345 struct internal_syment sym;
2347 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2349 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2352 char buf[SYMNMLEN + 1];
2353 struct bfd_link_hash_entry *h;
2355 /* This symbol is externally visible, and is defined by this
2357 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2361 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2363 /* We are only interested in symbols that are currently
2364 undefined. If a symbol is currently known to be common,
2365 XCOFF linkers do not bring in an object file which
2366 defines it. We also don't bring in symbols to satisfy
2367 undefined references in shared objects. */
2369 && h->type == bfd_link_hash_undefined
2370 && (info->output_bfd->xvec != abfd->xvec
2371 || (((struct xcoff_link_hash_entry *) h)->flags
2372 & XCOFF_DEF_DYNAMIC) == 0))
2374 if (!(*info->callbacks
2375 ->add_archive_element) (info, abfd, name, subsbfd))
2382 esym += (sym.n_numaux + 1) * symesz;
2385 /* We do not need this object file. */
2389 /* Check a single archive element to see if we need to include it in
2390 the link. *PNEEDED is set according to whether this element is
2391 needed in the link or not. This is called via
2392 _bfd_generic_link_add_archive_symbols. */
2395 xcoff_link_check_archive_element (bfd *abfd,
2396 struct bfd_link_info *info,
2397 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
2398 const char *name ATTRIBUTE_UNUSED,
2399 bfd_boolean *pneeded)
2401 bfd_boolean keep_syms_p;
2404 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2405 if (!_bfd_coff_get_external_symbols (abfd))
2409 if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2414 /* Potentially, the add_archive_element hook may have set a
2415 substitute BFD for us. */
2419 && !_bfd_coff_free_symbols (oldbfd))
2421 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2422 if (!_bfd_coff_get_external_symbols (abfd))
2425 if (!xcoff_link_add_symbols (abfd, info))
2427 if (info->keep_memory)
2433 if (!_bfd_coff_free_symbols (abfd))
2440 /* Given an XCOFF BFD, add symbols to the global hash table as
2444 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2446 switch (bfd_get_format (abfd))
2449 return xcoff_link_add_object_symbols (abfd, info);
2452 /* If the archive has a map, do the usual search. We then need
2453 to check the archive for dynamic objects, because they may not
2454 appear in the archive map even though they should, perhaps, be
2455 included. If the archive has no map, we just consider each object
2456 file in turn, since that apparently is what the AIX native linker
2458 if (bfd_has_map (abfd))
2460 if (! (_bfd_generic_link_add_archive_symbols
2461 (abfd, info, xcoff_link_check_archive_element)))
2468 member = bfd_openr_next_archived_file (abfd, NULL);
2469 while (member != NULL)
2471 if (bfd_check_format (member, bfd_object)
2472 && (info->output_bfd->xvec == member->xvec)
2473 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2477 if (! xcoff_link_check_archive_element (member, info,
2478 NULL, NULL, &needed))
2481 member->archive_pass = -1;
2483 member = bfd_openr_next_archived_file (abfd, member);
2490 bfd_set_error (bfd_error_wrong_format);
2496 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2497 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2498 struct bfd_link_hash_entry *harg)
2500 struct xcoff_link_hash_entry *h;
2502 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2505 h = (struct xcoff_link_hash_entry *) harg;
2506 h->flags |= XCOFF_DEF_REGULAR;
2510 /* If symbol H has not been interpreted as a function descriptor,
2511 see whether it should be. Set up its descriptor information if so. */
2514 xcoff_find_function (struct bfd_link_info *info,
2515 struct xcoff_link_hash_entry *h)
2517 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2518 && h->root.root.string[0] != '.')
2521 struct xcoff_link_hash_entry *hfn;
2524 amt = strlen (h->root.root.string) + 2;
2525 fnname = bfd_malloc (amt);
2529 strcpy (fnname + 1, h->root.root.string);
2530 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2531 fnname, FALSE, FALSE, TRUE);
2534 && hfn->smclas == XMC_PR
2535 && (hfn->root.type == bfd_link_hash_defined
2536 || hfn->root.type == bfd_link_hash_defweak))
2538 h->flags |= XCOFF_DESCRIPTOR;
2539 h->descriptor = hfn;
2540 hfn->descriptor = h;
2546 /* Return true if the given bfd contains at least one shared object. */
2549 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2552 struct xcoff_archive_info *archive_info;
2555 archive_info = xcoff_get_archive_info (info, archive);
2556 if (!archive_info->know_contains_shared_object_p)
2558 member = bfd_openr_next_archived_file (archive, NULL);
2559 while (member != NULL && (member->flags & DYNAMIC) == 0)
2560 member = bfd_openr_next_archived_file (archive, member);
2562 archive_info->contains_shared_object_p = (member != NULL);
2563 archive_info->know_contains_shared_object_p = 1;
2565 return archive_info->contains_shared_object_p;
2568 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2569 qualifies for export by -bexpall. */
2572 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2574 /* Exclude symbols beginning with '_'. */
2575 if (h->root.root.string[0] == '_')
2578 /* Exclude archive members that would otherwise be unreferenced. */
2579 if ((h->flags & XCOFF_MARK) == 0
2580 && (h->root.type == bfd_link_hash_defined
2581 || h->root.type == bfd_link_hash_defweak)
2582 && h->root.u.def.section->owner != NULL
2583 && h->root.u.def.section->owner->my_archive != NULL)
2589 /* Return true if symbol H qualifies for the forms of automatic export
2590 specified by AUTO_EXPORT_FLAGS. */
2593 xcoff_auto_export_p (struct bfd_link_info *info,
2594 struct xcoff_link_hash_entry *h,
2595 unsigned int auto_export_flags)
2597 /* Don't automatically export things that were explicitly exported. */
2598 if ((h->flags & XCOFF_EXPORT) != 0)
2601 /* Don't export things that we don't define. */
2602 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2605 /* Don't export functions; export their descriptors instead. */
2606 if (h->root.root.string[0] == '.')
2609 /* We don't export a symbol which is being defined by an object
2610 included from an archive which contains a shared object. The
2611 rationale is that if an archive contains both an unshared and
2612 a shared object, then there must be some reason that the
2613 unshared object is unshared, and we don't want to start
2614 providing a shared version of it. In particular, this solves
2615 a bug involving the _savefNN set of functions. gcc will call
2616 those functions without providing a slot to restore the TOC,
2617 so it is essential that these functions be linked in directly
2618 and not from a shared object, which means that a shared
2619 object which also happens to link them in must not export
2620 them. This is confusing, but I haven't been able to think of
2621 a different approach. Note that the symbols can, of course,
2622 be exported explicitly. */
2623 if (h->root.type == bfd_link_hash_defined
2624 || h->root.type == bfd_link_hash_defweak)
2628 owner = h->root.u.def.section->owner;
2630 && owner->my_archive != NULL
2631 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2635 /* Otherwise, all symbols are exported by -bexpfull. */
2636 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2639 /* Despite its name, -bexpall exports most but not all symbols. */
2640 if ((auto_export_flags & XCOFF_EXPALL) != 0
2641 && xcoff_covered_by_expall_p (h))
2647 /* Return true if relocation REL needs to be copied to the .loader section.
2648 If REL is against a global symbol, H is that symbol, otherwise it
2652 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2653 struct xcoff_link_hash_entry *h)
2655 if (!xcoff_hash_table (info)->loader_section)
2658 switch (rel->r_type)
2665 /* We should never need a .loader reloc for a TOC-relative reloc. */
2669 /* In this case, relocations against defined symbols can be resolved
2672 || h->root.type == bfd_link_hash_defined
2673 || h->root.type == bfd_link_hash_defweak
2674 || h->root.type == bfd_link_hash_common)
2677 /* We will always provide a local definition of function symbols,
2678 even if we don't have one yet. */
2679 if ((h->flags & XCOFF_CALLED) != 0)
2688 /* Absolute relocations against absolute symbols can be
2689 resolved statically. */
2690 if (h != NULL && bfd_is_abs_symbol (&h->root))
2697 /* Mark a symbol as not being garbage, including the section in which
2700 static inline bfd_boolean
2701 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2703 if ((h->flags & XCOFF_MARK) != 0)
2706 h->flags |= XCOFF_MARK;
2708 /* If we're marking an undefined symbol, try find some way of
2710 if (!bfd_link_relocatable (info)
2711 && (h->flags & XCOFF_IMPORT) == 0
2712 && (h->flags & XCOFF_DEF_REGULAR) == 0
2713 && (h->root.type == bfd_link_hash_undefined
2714 || h->root.type == bfd_link_hash_undefweak))
2716 /* First check whether this symbol can be interpreted as an
2717 undefined function descriptor for a defined function symbol. */
2718 if (!xcoff_find_function (info, h))
2721 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2722 && (h->descriptor->root.type == bfd_link_hash_defined
2723 || h->descriptor->root.type == bfd_link_hash_defweak))
2725 /* This is a descriptor for a defined symbol, but the input
2726 objects have not defined the descriptor itself. Fill in
2727 the definition automatically.
2729 Note that we do this even if we found a dynamic definition
2730 of H. The local function definition logically overrides
2734 sec = xcoff_hash_table (info)->descriptor_section;
2735 h->root.type = bfd_link_hash_defined;
2736 h->root.u.def.section = sec;
2737 h->root.u.def.value = sec->size;
2739 h->flags |= XCOFF_DEF_REGULAR;
2741 /* The size of the function descriptor depends on whether this
2742 is xcoff32 (12) or xcoff64 (24). */
2743 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2745 /* A function descriptor uses two relocs: one for the
2746 associated code, and one for the TOC address. */
2747 xcoff_hash_table (info)->ldrel_count += 2;
2748 sec->reloc_count += 2;
2750 /* Mark the function itself. */
2751 if (!xcoff_mark_symbol (info, h->descriptor))
2754 /* Mark the TOC section, so that we get an anchor
2755 to relocate against. */
2756 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2759 /* We handle writing out the contents of the descriptor in
2760 xcoff_write_global_symbol. */
2762 else if (info->static_link)
2763 /* We can't get a symbol value dynamically, so just assume
2764 that it's undefined. */
2765 h->flags |= XCOFF_WAS_UNDEFINED;
2766 else if ((h->flags & XCOFF_CALLED) != 0)
2768 /* This is a function symbol for which we need to create
2771 struct xcoff_link_hash_entry *hds;
2773 /* Mark the descriptor (and its TOC section). */
2774 hds = h->descriptor;
2775 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2776 || hds->root.type == bfd_link_hash_undefweak)
2777 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2778 if (!xcoff_mark_symbol (info, hds))
2781 /* Treat this symbol as undefined if the descriptor was. */
2782 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2783 h->flags |= XCOFF_WAS_UNDEFINED;
2785 /* Allocate room for the global linkage code itself. */
2786 sec = xcoff_hash_table (info)->linkage_section;
2787 h->root.type = bfd_link_hash_defined;
2788 h->root.u.def.section = sec;
2789 h->root.u.def.value = sec->size;
2791 h->flags |= XCOFF_DEF_REGULAR;
2792 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2794 /* The global linkage code requires a TOC entry for the
2796 if (hds->toc_section == NULL)
2801 xcoff32 uses 4 bytes in the toc.
2802 xcoff64 uses 8 bytes in the toc. */
2803 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2805 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2810 /* Allocate room in the fallback TOC section. */
2811 hds->toc_section = xcoff_hash_table (info)->toc_section;
2812 hds->u.toc_offset = hds->toc_section->size;
2813 hds->toc_section->size += byte_size;
2814 if (!xcoff_mark (info, hds->toc_section))
2817 /* Allocate room for a static and dynamic R_TOC
2819 ++xcoff_hash_table (info)->ldrel_count;
2820 ++hds->toc_section->reloc_count;
2822 /* Set the index to -2 to force this symbol to
2825 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2828 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2830 /* Record that the symbol was undefined, then import it.
2831 -brtl links use a special fake import file. */
2832 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2833 if (xcoff_hash_table (info)->rtld)
2835 if (!xcoff_set_import_path (info, h, "", "..", ""))
2840 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2846 if (h->root.type == bfd_link_hash_defined
2847 || h->root.type == bfd_link_hash_defweak)
2851 hsec = h->root.u.def.section;
2852 if (! bfd_is_abs_section (hsec)
2853 && (hsec->flags & SEC_MARK) == 0)
2855 if (! xcoff_mark (info, hsec))
2860 if (h->toc_section != NULL
2861 && (h->toc_section->flags & SEC_MARK) == 0)
2863 if (! xcoff_mark (info, h->toc_section))
2870 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2871 If the symbol exists, set FLAGS. */
2874 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2875 const char *name, unsigned int flags)
2877 struct xcoff_link_hash_entry *h;
2879 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2880 FALSE, FALSE, TRUE);
2884 if (h->root.type == bfd_link_hash_defined
2885 || h->root.type == bfd_link_hash_defweak)
2887 if (!xcoff_mark (info, h->root.u.def.section))
2894 /* The mark phase of garbage collection. For a given section, mark
2895 it, and all the sections which define symbols to which it refers.
2896 Because this function needs to look at the relocs, we also count
2897 the number of relocs which need to be copied into the .loader
2901 xcoff_mark (struct bfd_link_info *info, asection *sec)
2903 if (bfd_is_abs_section (sec)
2904 || (sec->flags & SEC_MARK) != 0)
2907 sec->flags |= SEC_MARK;
2909 if (sec->owner->xvec == info->output_bfd->xvec
2910 && coff_section_data (sec->owner, sec) != NULL
2911 && xcoff_section_data (sec->owner, sec) != NULL)
2913 struct xcoff_link_hash_entry **syms;
2914 struct internal_reloc *rel, *relend;
2916 unsigned long i, first, last;
2918 /* Mark all the symbols in this section. */
2919 syms = obj_xcoff_sym_hashes (sec->owner);
2920 csects = xcoff_data (sec->owner)->csects;
2921 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2922 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2923 for (i = first; i <= last; i++)
2924 if (csects[i] == sec
2926 && (syms[i]->flags & XCOFF_MARK) == 0)
2928 if (!xcoff_mark_symbol (info, syms[i]))
2932 /* Look through the section relocs. */
2933 if ((sec->flags & SEC_RELOC) != 0
2934 && sec->reloc_count > 0)
2936 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2940 relend = rel + sec->reloc_count;
2941 for (; rel < relend; rel++)
2943 struct xcoff_link_hash_entry *h;
2945 if ((unsigned int) rel->r_symndx
2946 > obj_raw_syment_count (sec->owner))
2949 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2952 if ((h->flags & XCOFF_MARK) == 0)
2954 if (!xcoff_mark_symbol (info, h))
2962 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2964 && (rsec->flags & SEC_MARK) == 0)
2966 if (!xcoff_mark (info, rsec))
2971 /* See if this reloc needs to be copied into the .loader
2973 if (xcoff_need_ldrel_p (info, rel, h))
2975 ++xcoff_hash_table (info)->ldrel_count;
2977 h->flags |= XCOFF_LDREL;
2981 if (! info->keep_memory
2982 && coff_section_data (sec->owner, sec) != NULL
2983 && coff_section_data (sec->owner, sec)->relocs != NULL
2984 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2986 free (coff_section_data (sec->owner, sec)->relocs);
2987 coff_section_data (sec->owner, sec)->relocs = NULL;
2995 /* Routines that are called after all the input files have been
2996 handled, but before the sections are laid out in memory. */
2998 /* The sweep phase of garbage collection. Remove all garbage
3002 xcoff_sweep (struct bfd_link_info *info)
3006 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3010 for (o = sub->sections; o != NULL; o = o->next)
3012 if ((o->flags & SEC_MARK) == 0)
3014 /* Keep all sections from non-XCOFF input files. Keep
3015 special sections. Keep .debug sections for the
3017 if (sub->xvec != info->output_bfd->xvec
3018 || o == xcoff_hash_table (info)->debug_section
3019 || o == xcoff_hash_table (info)->loader_section
3020 || o == xcoff_hash_table (info)->linkage_section
3021 || o == xcoff_hash_table (info)->descriptor_section
3022 || (bfd_get_section_flags (sub, o) & SEC_DEBUGGING)
3023 || strcmp (o->name, ".debug") == 0)
3024 o->flags |= SEC_MARK;
3035 /* Record the number of elements in a set. This is used to output the
3036 correct csect length. */
3039 bfd_xcoff_link_record_set (bfd *output_bfd,
3040 struct bfd_link_info *info,
3041 struct bfd_link_hash_entry *harg,
3044 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3045 struct xcoff_link_size_list *n;
3048 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3051 /* This will hardly ever be called. I don't want to burn four bytes
3052 per global symbol, so instead the size is kept on a linked list
3053 attached to the hash table. */
3055 n = bfd_alloc (output_bfd, amt);
3058 n->next = xcoff_hash_table (info)->size_list;
3061 xcoff_hash_table (info)->size_list = n;
3063 h->flags |= XCOFF_HAS_SIZE;
3068 /* Import a symbol. */
3071 bfd_xcoff_import_symbol (bfd *output_bfd,
3072 struct bfd_link_info *info,
3073 struct bfd_link_hash_entry *harg,
3075 const char *imppath,
3076 const char *impfile,
3077 const char *impmember,
3078 unsigned int syscall_flag)
3080 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3082 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3085 /* A symbol name which starts with a period is the code for a
3086 function. If the symbol is undefined, then add an undefined
3087 symbol for the function descriptor, and import that instead. */
3088 if (h->root.root.string[0] == '.'
3089 && h->root.type == bfd_link_hash_undefined
3090 && val == (bfd_vma) -1)
3092 struct xcoff_link_hash_entry *hds;
3094 hds = h->descriptor;
3097 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3098 h->root.root.string + 1,
3102 if (hds->root.type == bfd_link_hash_new)
3104 hds->root.type = bfd_link_hash_undefined;
3105 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3107 hds->flags |= XCOFF_DESCRIPTOR;
3108 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3109 hds->descriptor = h;
3110 h->descriptor = hds;
3113 /* Now, if the descriptor is undefined, import the descriptor
3114 rather than the symbol we were told to import. FIXME: Is
3115 this correct in all cases? */
3116 if (hds->root.type == bfd_link_hash_undefined)
3120 h->flags |= (XCOFF_IMPORT | syscall_flag);
3122 if (val != (bfd_vma) -1)
3124 if (h->root.type == bfd_link_hash_defined
3125 && (!bfd_is_abs_symbol (&h->root)
3126 || h->root.u.def.value != val))
3127 (*info->callbacks->multiple_definition) (info, &h->root, output_bfd,
3128 bfd_abs_section_ptr, val);
3130 h->root.type = bfd_link_hash_defined;
3131 h->root.u.def.section = bfd_abs_section_ptr;
3132 h->root.u.def.value = val;
3136 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3142 /* Export a symbol. */
3145 bfd_xcoff_export_symbol (bfd *output_bfd,
3146 struct bfd_link_info *info,
3147 struct bfd_link_hash_entry *harg)
3149 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3151 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3154 h->flags |= XCOFF_EXPORT;
3156 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3157 I'm just going to ignore it until somebody explains it. */
3159 /* Make sure we don't garbage collect this symbol. */
3160 if (! xcoff_mark_symbol (info, h))
3163 /* If this is a function descriptor, make sure we don't garbage
3164 collect the associated function code. We normally don't have to
3165 worry about this, because the descriptor will be attached to a
3166 section with relocs, but if we are creating the descriptor
3167 ourselves those relocs will not be visible to the mark code. */
3168 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3170 if (! xcoff_mark_symbol (info, h->descriptor))
3177 /* Count a reloc against a symbol. This is called for relocs
3178 generated by the linker script, typically for global constructors
3182 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3183 struct bfd_link_info *info,
3186 struct xcoff_link_hash_entry *h;
3188 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3191 h = ((struct xcoff_link_hash_entry *)
3192 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3196 _bfd_error_handler (_("%s: no such symbol"), name);
3197 bfd_set_error (bfd_error_no_symbols);
3201 h->flags |= XCOFF_REF_REGULAR;
3202 if (xcoff_hash_table (info)->loader_section)
3204 h->flags |= XCOFF_LDREL;
3205 ++xcoff_hash_table (info)->ldrel_count;
3208 /* Mark the symbol to avoid garbage collection. */
3209 if (! xcoff_mark_symbol (info, h))
3215 /* This function is called for each symbol to which the linker script
3219 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3220 struct bfd_link_info *info,
3223 struct xcoff_link_hash_entry *h;
3225 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3228 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3233 h->flags |= XCOFF_DEF_REGULAR;
3238 /* An xcoff_link_hash_traverse callback for which DATA points to an
3239 xcoff_loader_info. Mark all symbols that should be automatically
3243 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3245 struct xcoff_loader_info *ldinfo;
3247 ldinfo = (struct xcoff_loader_info *) data;
3248 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3250 if (!xcoff_mark_symbol (ldinfo->info, h))
3251 ldinfo->failed = TRUE;
3256 /* Add a symbol to the .loader symbols, if necessary. */
3258 /* INPUT_BFD has an external symbol associated with hash table entry H
3259 and csect CSECT. Return true if INPUT_BFD defines H. */
3262 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3265 switch (h->root.type)
3267 case bfd_link_hash_defined:
3268 case bfd_link_hash_defweak:
3269 /* No input bfd owns absolute symbols. They are written by
3270 xcoff_write_global_symbol instead. */
3271 return (!bfd_is_abs_section (csect)
3272 && h->root.u.def.section == csect);
3274 case bfd_link_hash_common:
3275 return h->root.u.c.p->section->owner == input_bfd;
3277 case bfd_link_hash_undefined:
3278 case bfd_link_hash_undefweak:
3279 /* We can't treat undef.abfd as the owner because that bfd
3280 might be a dynamic object. Allow any bfd to claim it. */
3288 /* See if H should have a loader symbol associated with it. */
3291 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3292 struct xcoff_link_hash_entry *h)
3296 /* Warn if this symbol is exported but not defined. */
3297 if ((h->flags & XCOFF_EXPORT) != 0
3298 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3301 (_("warning: attempt to export undefined symbol `%s'"),
3302 h->root.root.string);
3306 /* We need to add a symbol to the .loader section if it is mentioned
3307 in a reloc which we are copying to the .loader section and it was
3308 not defined or common, or if it is the entry point, or if it is
3310 if (((h->flags & XCOFF_LDREL) == 0
3311 || h->root.type == bfd_link_hash_defined
3312 || h->root.type == bfd_link_hash_defweak
3313 || h->root.type == bfd_link_hash_common)
3314 && (h->flags & XCOFF_ENTRY) == 0
3315 && (h->flags & XCOFF_EXPORT) == 0)
3318 /* We need to add this symbol to the .loader symbols. */
3320 BFD_ASSERT (h->ldsym == NULL);
3321 amt = sizeof (struct internal_ldsym);
3322 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3323 if (h->ldsym == NULL)
3325 ldinfo->failed = TRUE;
3329 if ((h->flags & XCOFF_IMPORT) != 0)
3331 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3332 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3334 h->ldsym->l_ifile = h->ldindx;
3337 /* The first 3 symbol table indices are reserved to indicate the
3338 data, text and bss sections. */
3339 h->ldindx = ldinfo->ldsym_count + 3;
3341 ++ldinfo->ldsym_count;
3343 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3344 h->ldsym, h->root.root.string))
3347 h->flags |= XCOFF_BUILT_LDSYM;
3351 /* An xcoff_htab_traverse callback that is called for each symbol
3352 once garbage collection is complete. */
3355 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3357 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3359 /* __rtinit, this symbol has special handling. */
3360 if (h->flags & XCOFF_RTINIT)
3363 /* We don't want to garbage collect symbols which are not defined in
3364 XCOFF files. This is a convenient place to mark them. */
3365 if (xcoff_hash_table (ldinfo->info)->gc
3366 && (h->flags & XCOFF_MARK) == 0
3367 && (h->root.type == bfd_link_hash_defined
3368 || h->root.type == bfd_link_hash_defweak)
3369 && (h->root.u.def.section->owner == NULL
3370 || (h->root.u.def.section->owner->xvec
3371 != ldinfo->info->output_bfd->xvec)))
3372 h->flags |= XCOFF_MARK;
3374 /* Skip discarded symbols. */
3375 if (xcoff_hash_table (ldinfo->info)->gc
3376 && (h->flags & XCOFF_MARK) == 0)
3379 /* If this is still a common symbol, and it wasn't garbage
3380 collected, we need to actually allocate space for it in the .bss
3382 if (h->root.type == bfd_link_hash_common
3383 && h->root.u.c.p->section->size == 0)
3385 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3386 h->root.u.c.p->section->size = h->root.u.c.size;
3389 if (xcoff_hash_table (ldinfo->info)->loader_section)
3391 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3392 h->flags |= XCOFF_EXPORT;
3394 if (!xcoff_build_ldsym (ldinfo, h))
3401 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3402 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3403 csect information, if any. NAME is the function's name if the name
3404 is stored in the .debug section, otherwise it is null.
3406 Return 1 if we should include an appropriately-adjusted ISYM
3407 in the output file, 0 if we should discard ISYM, or -1 if an
3411 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3412 struct internal_syment *isym,
3413 union internal_auxent *aux,
3414 struct xcoff_link_hash_entry *h,
3415 asection *csect, const char *name)
3419 /* If we are skipping this csect, we want to strip the symbol too. */
3423 /* Likewise if we garbage-collected the csect. */
3424 if (xcoff_hash_table (info)->gc
3425 && !bfd_is_abs_section (csect)
3426 && !bfd_is_und_section (csect)
3427 && (csect->flags & SEC_MARK) == 0)
3430 /* An XCOFF linker always removes C_STAT symbols. */
3431 if (isym->n_sclass == C_STAT)
3434 /* We generate the TOC anchor separately. */
3435 if (isym->n_sclass == C_HIDEXT
3436 && aux->x_csect.x_smclas == XMC_TC0)
3439 /* If we are stripping all symbols, we want to discard this one. */
3440 if (info->strip == strip_all)
3443 /* Discard symbols that are defined elsewhere. */
3444 if (EXTERN_SYM_P (isym->n_sclass))
3446 if ((h->flags & XCOFF_ALLOCATED) != 0)
3448 if (!xcoff_final_definition_p (input_bfd, h, csect))
3452 /* If we're discarding local symbols, check whether ISYM is local. */
3453 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3454 if (info->discard == discard_all
3455 && !EXTERN_SYM_P (isym->n_sclass)
3456 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3459 /* If we're stripping debugging symbols, check whether ISYM is one. */
3460 if (info->strip == strip_debugger
3461 && isym->n_scnum == N_DEBUG)
3464 /* If we are stripping symbols based on name, check how ISYM's
3465 name should be handled. */
3466 if (info->strip == strip_some
3467 || info->discard == discard_l)
3469 char buf[SYMNMLEN + 1];
3473 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3478 if (info->strip == strip_some
3479 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3482 if (info->discard == discard_l
3483 && !EXTERN_SYM_P (isym->n_sclass)
3484 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3485 && bfd_is_local_label_name (input_bfd, name))
3492 /* Lay out the .loader section, filling in the header and the import paths.
3493 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3496 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3497 const char *libpath)
3500 struct xcoff_link_hash_table *htab;
3501 struct internal_ldhdr *ldhdr;
3502 struct xcoff_import_file *fl;
3503 bfd_size_type stoff;
3504 size_t impsize, impcount;
3508 /* Work out the size of the import file names. Each import file ID
3509 consists of three null terminated strings: the path, the file
3510 name, and the archive member name. The first entry in the list
3511 of names is the path to use to find objects, which the linker has
3512 passed in as the libpath argument. For some reason, the path
3513 entry in the other import file names appears to always be empty. */
3514 output_bfd = ldinfo->output_bfd;
3515 htab = xcoff_hash_table (ldinfo->info);
3516 impsize = strlen (libpath) + 3;
3518 for (fl = htab->imports; fl != NULL; fl = fl->next)
3521 impsize += (strlen (fl->path)
3523 + strlen (fl->member)
3527 /* Set up the .loader section header. */
3528 ldhdr = &htab->ldhdr;
3529 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3530 ldhdr->l_nsyms = ldinfo->ldsym_count;
3531 ldhdr->l_nreloc = htab->ldrel_count;
3532 ldhdr->l_istlen = impsize;
3533 ldhdr->l_nimpid = impcount;
3534 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3535 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3536 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3537 ldhdr->l_stlen = ldinfo->string_size;
3538 stoff = ldhdr->l_impoff + impsize;
3539 if (ldinfo->string_size == 0)
3542 ldhdr->l_stoff = stoff;
3544 /* 64 bit elements to ldhdr
3545 The swap out routine for 32 bit will ignore them.
3546 Nothing fancy, symbols come after the header and relocs come
3548 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3549 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3550 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3552 /* We now know the final size of the .loader section. Allocate
3554 lsec = htab->loader_section;
3555 lsec->size = stoff + ldhdr->l_stlen;
3556 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3557 if (lsec->contents == NULL)
3560 /* Set up the header. */
3561 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3563 /* Set up the import file names. */
3564 out = (char *) lsec->contents + ldhdr->l_impoff;
3565 strcpy (out, libpath);
3566 out += strlen (libpath) + 1;
3569 for (fl = htab->imports; fl != NULL; fl = fl->next)
3574 while ((*out++ = *s++) != '\0')
3577 while ((*out++ = *s++) != '\0')
3580 while ((*out++ = *s++) != '\0')
3584 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3586 /* Set up the symbol string table. */
3587 if (ldinfo->string_size > 0)
3589 memcpy (out, ldinfo->strings, ldinfo->string_size);
3590 free (ldinfo->strings);
3591 ldinfo->strings = NULL;
3594 /* We can't set up the symbol table or the relocs yet, because we
3595 don't yet know the final position of the various sections. The
3596 .loader symbols are written out when the corresponding normal
3597 symbols are written out in xcoff_link_input_bfd or
3598 xcoff_write_global_symbol. The .loader relocs are written out
3599 when the corresponding normal relocs are handled in
3600 xcoff_link_input_bfd. */
3605 /* Build the .loader section. This is called by the XCOFF linker
3606 emulation before_allocation routine. We must set the size of the
3607 .loader section before the linker lays out the output file.
3608 LIBPATH is the library path to search for shared objects; this is
3609 normally built from the -L arguments passed to the linker. ENTRY
3610 is the name of the entry point symbol (the -e linker option).
3611 FILE_ALIGN is the alignment to use for sections within the file
3612 (the -H linker option). MAXSTACK is the maximum stack size (the
3613 -bmaxstack linker option). MAXDATA is the maximum data size (the
3614 -bmaxdata linker option). GC is whether to do garbage collection
3615 (the -bgc linker option). MODTYPE is the module type (the
3616 -bmodtype linker option). TEXTRO is whether the text section must
3617 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3618 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3619 is set by this routine to csects with magic names like _end. */
3622 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3623 struct bfd_link_info *info,
3624 const char *libpath,
3626 unsigned long file_align,
3627 unsigned long maxstack,
3628 unsigned long maxdata,
3632 unsigned int auto_export_flags,
3633 asection **special_sections,
3636 struct xcoff_loader_info ldinfo;
3640 struct bfd_strtab_hash *debug_strtab;
3641 bfd_byte *debug_contents = NULL;
3644 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3646 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3647 special_sections[i] = NULL;
3651 ldinfo.failed = FALSE;
3652 ldinfo.output_bfd = output_bfd;
3654 ldinfo.auto_export_flags = auto_export_flags;
3655 ldinfo.ldsym_count = 0;
3656 ldinfo.string_size = 0;
3657 ldinfo.strings = NULL;
3658 ldinfo.string_alc = 0;
3660 xcoff_data (output_bfd)->maxstack = maxstack;
3661 xcoff_data (output_bfd)->maxdata = maxdata;
3662 xcoff_data (output_bfd)->modtype = modtype;
3664 xcoff_hash_table (info)->file_align = file_align;
3665 xcoff_hash_table (info)->textro = textro;
3666 xcoff_hash_table (info)->rtld = rtld;
3669 if (xcoff_hash_table (info)->loader_section
3670 && (info->init_function || info->fini_function || rtld))
3672 struct xcoff_link_hash_entry *hsym;
3673 struct internal_ldsym *ldsym;
3675 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3676 "__rtinit", FALSE, FALSE, TRUE);
3680 (_("error: undefined symbol __rtinit"));
3684 xcoff_mark_symbol (info, hsym);
3685 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3687 /* __rtinit initialized. */
3688 amt = sizeof (* ldsym);
3689 ldsym = bfd_malloc (amt);
3691 ldsym->l_value = 0; /* Will be filled in later. */
3692 ldsym->l_scnum = 2; /* Data section. */
3693 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3694 ldsym->l_smclas = 5; /* .rw. */
3695 ldsym->l_ifile = 0; /* Special system loader symbol. */
3696 ldsym->l_parm = 0; /* NA. */
3698 /* Force __rtinit to be the first symbol in the loader symbol table
3699 See xcoff_build_ldsyms
3701 The first 3 symbol table indices are reserved to indicate the data,
3702 text and bss sections. */
3703 BFD_ASSERT (0 == ldinfo.ldsym_count);
3706 ldinfo.ldsym_count = 1;
3707 hsym->ldsym = ldsym;
3709 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3710 hsym->ldsym, hsym->root.root.string))
3713 /* This symbol is written out by xcoff_write_global_symbol
3714 Set stuff up so xcoff_write_global_symbol logic works. */
3715 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3716 hsym->root.type = bfd_link_hash_defined;
3717 hsym->root.u.def.value = 0;
3720 /* Garbage collect unused sections. */
3721 if (bfd_link_relocatable (info) || !gc)
3724 xcoff_hash_table (info)->gc = FALSE;
3726 /* We still need to call xcoff_mark, in order to set ldrel_count
3728 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3732 for (o = sub->sections; o != NULL; o = o->next)
3734 /* We shouldn't unconditionaly mark the TOC section.
3735 The output file should only have a TOC if either
3736 (a) one of the input files did or (b) we end up
3737 creating TOC references as part of the link process. */
3738 if (o != xcoff_hash_table (info)->toc_section
3739 && (o->flags & SEC_MARK) == 0)
3741 if (! xcoff_mark (info, o))
3750 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3752 if (info->init_function != NULL
3753 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3755 if (info->fini_function != NULL
3756 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3758 if (auto_export_flags != 0)
3760 xcoff_link_hash_traverse (xcoff_hash_table (info),
3761 xcoff_mark_auto_exports, &ldinfo);
3766 xcoff_hash_table (info)->gc = TRUE;
3769 /* Return special sections to the caller. */
3770 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3772 sec = xcoff_hash_table (info)->special_sections[i];
3776 && (sec->flags & SEC_MARK) == 0)
3779 special_sections[i] = sec;
3782 if (info->input_bfds == NULL)
3783 /* I'm not sure what to do in this bizarre case. */
3786 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3791 if (xcoff_hash_table (info)->loader_section
3792 && !xcoff_build_loader_section (&ldinfo, libpath))
3795 /* Allocate space for the magic sections. */
3796 sec = xcoff_hash_table (info)->linkage_section;
3799 sec->contents = bfd_zalloc (output_bfd, sec->size);
3800 if (sec->contents == NULL)
3803 sec = xcoff_hash_table (info)->toc_section;
3806 sec->contents = bfd_zalloc (output_bfd, sec->size);
3807 if (sec->contents == NULL)
3810 sec = xcoff_hash_table (info)->descriptor_section;
3813 sec->contents = bfd_zalloc (output_bfd, sec->size);
3814 if (sec->contents == NULL)
3818 /* Now that we've done garbage collection, decide which symbols to keep,
3819 and figure out the contents of the .debug section. */
3820 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3822 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3825 bfd_size_type symcount;
3828 unsigned int *lineno_counts;
3829 struct xcoff_link_hash_entry **sym_hash;
3830 bfd_byte *esym, *esymend;
3831 bfd_size_type symesz;
3833 if (sub->xvec != info->output_bfd->xvec)
3836 if ((sub->flags & DYNAMIC) != 0
3837 && !info->static_link)
3840 if (! _bfd_coff_get_external_symbols (sub))
3843 symcount = obj_raw_syment_count (sub);
3844 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3845 if (debug_index == NULL)
3847 xcoff_data (sub)->debug_indices = debug_index;
3849 if (info->strip == strip_all
3850 || info->strip == strip_debugger
3851 || info->discard == discard_all)
3852 /* We're stripping all debugging information, so there's no need
3853 to read SUB's .debug section. */
3857 /* Grab the contents of SUB's .debug section, if any. */
3858 subdeb = bfd_get_section_by_name (sub, ".debug");
3859 if (subdeb != NULL && subdeb->size > 0)
3861 /* We use malloc and copy the names into the debug
3862 stringtab, rather than bfd_alloc, because I expect
3863 that, when linking many files together, many of the
3864 strings will be the same. Storing the strings in the
3865 hash table should save space in this case. */
3866 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3871 csectpp = xcoff_data (sub)->csects;
3872 lineno_counts = xcoff_data (sub)->lineno_counts;
3873 sym_hash = obj_xcoff_sym_hashes (sub);
3874 symesz = bfd_coff_symesz (sub);
3875 esym = (bfd_byte *) obj_coff_external_syms (sub);
3876 esymend = esym + symcount * symesz;
3878 while (esym < esymend)
3880 struct internal_syment sym;
3881 union internal_auxent aux;
3886 bfd_coff_swap_sym_in (sub, esym, &sym);
3888 /* Read in the csect information, if any. */
3889 if (CSECT_SYM_P (sym.n_sclass))
3891 BFD_ASSERT (sym.n_numaux > 0);
3892 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3893 sym.n_type, sym.n_sclass,
3894 sym.n_numaux - 1, sym.n_numaux, &aux);
3897 /* If this symbol's name is stored in the debug section,
3898 get a pointer to it. */
3899 if (debug_contents != NULL
3900 && sym._n._n_n._n_zeroes == 0
3901 && bfd_coff_symname_in_debug (sub, &sym))
3902 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3906 /* Decide whether to copy this symbol to the output file. */
3908 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3909 *sym_hash, csect, name);
3914 /* Use a debug_index of -2 to record that a symbol should
3919 /* See whether we should store the symbol name in the
3920 output .debug section. */
3925 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3926 if (indx == (bfd_size_type) -1)
3928 *debug_index = indx;
3933 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3934 if (*lineno_counts > 0)
3935 csect->output_section->lineno_count += *lineno_counts;
3938 esym += (sym.n_numaux + 1) * symesz;
3939 csectpp += sym.n_numaux + 1;
3940 sym_hash += sym.n_numaux + 1;
3941 lineno_counts += sym.n_numaux + 1;
3942 debug_index += sym.n_numaux + 1;
3947 free (debug_contents);
3948 debug_contents = NULL;
3950 /* Clear the size of subdeb, so that it is not included directly
3951 in the output file. */
3955 if (! info->keep_memory)
3957 if (! _bfd_coff_free_symbols (sub))
3962 if (info->strip != strip_all)
3963 xcoff_hash_table (info)->debug_section->size =
3964 _bfd_stringtab_size (debug_strtab);
3969 if (ldinfo.strings != NULL)
3970 free (ldinfo.strings);
3971 if (debug_contents != NULL)
3972 free (debug_contents);
3977 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3982 struct bfd_in_memory *bim;
3984 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3991 abfd->link.next = 0;
3992 abfd->format = bfd_object;
3993 abfd->iostream = (void *) bim;
3994 abfd->flags = BFD_IN_MEMORY;
3995 abfd->iovec = &_bfd_memory_iovec;
3996 abfd->direction = write_direction;
4000 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
4003 /* need to reset to unknown or it will not be read back in correctly */
4004 abfd->format = bfd_unknown;
4005 abfd->direction = read_direction;
4011 /* Return the section that defines H. Return null if no section does. */
4014 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
4016 switch (h->root.type)
4018 case bfd_link_hash_defined:
4019 case bfd_link_hash_defweak:
4020 return h->root.u.def.section;
4022 case bfd_link_hash_common:
4023 return h->root.u.c.p->section;
4030 /* Add a .loader relocation for input relocation IREL. If the loader
4031 relocation should be against an output section, HSEC points to the
4032 input section that IREL is against, otherwise HSEC is null. H is the
4033 symbol that IREL is against, or null if it isn't against a global symbol.
4034 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4037 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
4038 asection *output_section, bfd *reference_bfd,
4039 struct internal_reloc *irel, asection *hsec,
4040 struct xcoff_link_hash_entry *h)
4042 struct internal_ldrel ldrel;
4044 ldrel.l_vaddr = irel->r_vaddr;
4047 const char *secname;
4049 secname = hsec->output_section->name;
4050 if (strcmp (secname, ".text") == 0)
4052 else if (strcmp (secname, ".data") == 0)
4054 else if (strcmp (secname, ".bss") == 0)
4059 /* xgettext:c-format */
4060 (_("%pB: loader reloc in unrecognized section `%s'"),
4061 reference_bfd, secname);
4062 bfd_set_error (bfd_error_nonrepresentable_section);
4071 /* xgettext:c-format */
4072 (_("%pB: `%s' in loader reloc but not loader sym"),
4073 reference_bfd, h->root.root.string);
4074 bfd_set_error (bfd_error_bad_value);
4077 ldrel.l_symndx = h->ldindx;
4080 ldrel.l_symndx = -(bfd_size_type) 1;
4082 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4083 ldrel.l_rsecnm = output_section->target_index;
4084 if (xcoff_hash_table (flinfo->info)->textro
4085 && strcmp (output_section->name, ".text") == 0)
4088 /* xgettext:c-format */
4089 (_("%pB: loader reloc in read-only section %pA"),
4090 reference_bfd, output_section);
4091 bfd_set_error (bfd_error_invalid_operation);
4094 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
4095 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
4099 /* Link an input file into the linker output file. This function
4100 handles all the sections and relocations of the input file at once. */
4103 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
4107 const char *strings;
4108 bfd_size_type syment_base;
4109 unsigned int n_tmask;
4110 unsigned int n_btshft;
4111 bfd_boolean copy, hash;
4112 bfd_size_type isymesz;
4113 bfd_size_type osymesz;
4114 bfd_size_type linesz;
4117 struct xcoff_link_hash_entry **sym_hash;
4118 struct internal_syment *isymp;
4120 unsigned int *lineno_counts;
4123 unsigned long output_index;
4127 bfd_boolean keep_syms;
4130 /* We can just skip DYNAMIC files, unless this is a static link. */
4131 if ((input_bfd->flags & DYNAMIC) != 0
4132 && ! flinfo->info->static_link)
4135 /* Move all the symbols to the output file. */
4136 output_bfd = flinfo->output_bfd;
4138 syment_base = obj_raw_syment_count (output_bfd);
4139 isymesz = bfd_coff_symesz (input_bfd);
4140 osymesz = bfd_coff_symesz (output_bfd);
4141 linesz = bfd_coff_linesz (input_bfd);
4142 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4144 n_tmask = coff_data (input_bfd)->local_n_tmask;
4145 n_btshft = coff_data (input_bfd)->local_n_btshft;
4147 /* Define macros so that ISFCN, et. al., macros work correctly. */
4148 #define N_TMASK n_tmask
4149 #define N_BTSHFT n_btshft
4152 if (! flinfo->info->keep_memory)
4155 if (flinfo->info->traditional_format)
4158 if (! _bfd_coff_get_external_symbols (input_bfd))
4161 /* Make one pass over the symbols and assign indices to symbols that
4162 we have decided to keep. Also use create .loader symbol information
4163 and update information in hash table entries. */
4164 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4165 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4166 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4167 csectpp = xcoff_data (input_bfd)->csects;
4168 debug_index = xcoff_data (input_bfd)->debug_indices;
4169 isymp = flinfo->internal_syms;
4170 indexp = flinfo->sym_indices;
4171 output_index = syment_base;
4172 while (esym < esym_end)
4174 union internal_auxent aux;
4178 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4180 /* Read in the csect information, if any. */
4181 if (CSECT_SYM_P (isymp->n_sclass))
4183 BFD_ASSERT (isymp->n_numaux > 0);
4184 bfd_coff_swap_aux_in (input_bfd,
4185 (void *) (esym + isymesz * isymp->n_numaux),
4186 isymp->n_type, isymp->n_sclass,
4187 isymp->n_numaux - 1, isymp->n_numaux,
4190 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4193 /* If this symbol is in the .loader section, swap out the
4194 .loader symbol information. If this is an external symbol
4195 reference to a defined symbol, though, then wait until we get
4196 to the definition. */
4197 if (EXTERN_SYM_P (isymp->n_sclass)
4198 && *sym_hash != NULL
4199 && (*sym_hash)->ldsym != NULL
4200 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4202 struct xcoff_link_hash_entry *h;
4203 struct internal_ldsym *ldsym;
4207 if (isymp->n_scnum > 0)
4209 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4210 ldsym->l_value = (isymp->n_value
4211 + (*csectpp)->output_section->vma
4212 + (*csectpp)->output_offset
4217 ldsym->l_scnum = isymp->n_scnum;
4218 ldsym->l_value = isymp->n_value;
4221 ldsym->l_smtype = smtyp;
4222 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4223 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4224 || (h->flags & XCOFF_IMPORT) != 0)
4225 ldsym->l_smtype |= L_IMPORT;
4226 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4227 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4228 || (h->flags & XCOFF_EXPORT) != 0)
4229 ldsym->l_smtype |= L_EXPORT;
4230 if ((h->flags & XCOFF_ENTRY) != 0)
4231 ldsym->l_smtype |= L_ENTRY;
4232 if (isymp->n_sclass == C_AIX_WEAKEXT)
4233 ldsym->l_smtype |= L_WEAK;
4235 ldsym->l_smclas = aux.x_csect.x_smclas;
4237 if (ldsym->l_ifile == (bfd_size_type) -1)
4239 else if (ldsym->l_ifile == 0)
4241 if ((ldsym->l_smtype & L_IMPORT) == 0)
4247 if (h->root.type == bfd_link_hash_defined
4248 || h->root.type == bfd_link_hash_defweak)
4249 impbfd = h->root.u.def.section->owner;
4250 else if (h->root.type == bfd_link_hash_undefined
4251 || h->root.type == bfd_link_hash_undefweak)
4252 impbfd = h->root.u.undef.abfd;
4260 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
4261 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4268 BFD_ASSERT (h->ldindx >= 0);
4269 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
4272 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
4275 /* Fill in snentry now that we know the target_index. */
4276 if ((h->flags & XCOFF_ENTRY) != 0
4277 && (h->root.type == bfd_link_hash_defined
4278 || h->root.type == bfd_link_hash_defweak))
4280 xcoff_data (output_bfd)->snentry =
4281 h->root.u.def.section->output_section->target_index;
4285 add = 1 + isymp->n_numaux;
4287 if (*debug_index == -2)
4288 /* We've decided to strip this symbol. */
4292 /* Assign the next unused index to this symbol. */
4293 *indexp = output_index;
4295 if (EXTERN_SYM_P (isymp->n_sclass))
4297 BFD_ASSERT (*sym_hash != NULL);
4298 (*sym_hash)->indx = output_index;
4301 /* If this is a symbol in the TOC which we may have merged
4302 (class XMC_TC), remember the symbol index of the TOC
4304 if (isymp->n_sclass == C_HIDEXT
4305 && aux.x_csect.x_smclas == XMC_TC
4306 && *sym_hash != NULL)
4308 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4309 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4310 (*sym_hash)->u.toc_indx = output_index;
4313 output_index += add;
4316 esym += add * isymesz;
4322 for (--add; add > 0; --add)
4326 /* Now write out the symbols that we decided to keep. */
4328 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4329 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4330 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4331 isymp = flinfo->internal_syms;
4332 indexp = flinfo->sym_indices;
4333 csectpp = xcoff_data (input_bfd)->csects;
4334 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4335 debug_index = xcoff_data (input_bfd)->debug_indices;
4336 outsym = flinfo->outsyms;
4339 while (esym < esym_end)
4343 add = 1 + isymp->n_numaux;
4346 esym += add * isymesz;
4349 struct internal_syment isym;
4352 /* Adjust the symbol in order to output it. */
4354 if (isym._n._n_n._n_zeroes == 0
4355 && isym._n._n_n._n_offset != 0)
4357 /* This symbol has a long name. Enter it in the string
4358 table we are building. If *debug_index != -1, the
4359 name has already been entered in the .debug section. */
4360 if (*debug_index >= 0)
4361 isym._n._n_n._n_offset = *debug_index;
4367 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4371 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
4372 if (indx == (bfd_size_type) -1)
4374 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4378 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4379 multiple definition problems when linking a shared object
4380 statically. (The native linker doesn't enter __rtinit into
4381 the normal table at all, but having a local symbol can make
4382 the objdump output easier to read.) */
4383 if (isym.n_sclass == C_EXT
4385 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
4386 isym.n_sclass = C_HIDEXT;
4388 /* The value of a C_FILE symbol is the symbol index of the
4389 next C_FILE symbol. The value of the last C_FILE symbol
4390 is -1. We try to get this right, below, just before we
4391 write the symbols out, but in the general case we may
4392 have to write the symbol out twice. */
4393 if (isym.n_sclass == C_FILE)
4395 if (flinfo->last_file_index != -1
4396 && flinfo->last_file.n_value != (bfd_vma) *indexp)
4398 /* We must correct the value of the last C_FILE entry. */
4399 flinfo->last_file.n_value = *indexp;
4400 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
4402 /* The last C_FILE symbol is in this input file. */
4403 bfd_coff_swap_sym_out (output_bfd,
4404 (void *) &flinfo->last_file,
4405 (void *) (flinfo->outsyms
4406 + ((flinfo->last_file_index
4412 /* We have already written out the last C_FILE
4413 symbol. We need to write it out again. We
4414 borrow *outsym temporarily. */
4417 bfd_coff_swap_sym_out (output_bfd,
4418 (void *) &flinfo->last_file,
4421 pos = obj_sym_filepos (output_bfd);
4422 pos += flinfo->last_file_index * osymesz;
4423 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4424 || (bfd_bwrite (outsym, osymesz, output_bfd)
4430 flinfo->last_file_index = *indexp;
4431 flinfo->last_file = isym;
4434 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4435 into the line numbers. We update the symbol values when
4436 we handle the line numbers. */
4437 if (isym.n_sclass == C_BINCL
4438 || isym.n_sclass == C_EINCL)
4440 isym.n_value = flinfo->line_filepos;
4443 /* The value of a C_BSTAT symbol is the symbol table
4444 index of the containing csect. */
4445 else if (isym.n_sclass == C_BSTAT)
4449 indx = isym.n_value;
4450 if (indx < obj_raw_syment_count (input_bfd))
4454 symindx = flinfo->sym_indices[indx];
4458 isym.n_value = symindx;
4461 else if (isym.n_sclass != C_ESTAT
4462 && isym.n_sclass != C_DECL
4463 && isym.n_scnum > 0)
4465 isym.n_scnum = (*csectpp)->output_section->target_index;
4466 isym.n_value += ((*csectpp)->output_section->vma
4467 + (*csectpp)->output_offset
4471 /* Output the symbol. */
4472 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4477 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4479 union internal_auxent aux;
4481 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4482 isymp->n_sclass, i, isymp->n_numaux,
4485 if (isymp->n_sclass == C_FILE)
4487 /* This is the file name (or some comment put in by
4488 the compiler). If it is long, we must put it in
4489 the string table. */
4490 if (aux.x_file.x_n.x_zeroes == 0
4491 && aux.x_file.x_n.x_offset != 0)
4493 const char *filename;
4496 BFD_ASSERT (aux.x_file.x_n.x_offset
4497 >= STRING_SIZE_SIZE);
4498 if (strings == NULL)
4500 strings = _bfd_coff_read_string_table (input_bfd);
4501 if (strings == NULL)
4504 if ((bfd_size_type) aux.x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd))
4505 filename = _("<corrupt>");
4507 filename = strings + aux.x_file.x_n.x_offset;
4508 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4510 if (indx == (bfd_size_type) -1)
4512 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4515 else if (CSECT_SYM_P (isymp->n_sclass)
4516 && i + 1 == isymp->n_numaux)
4519 /* We don't support type checking. I don't know if
4521 aux.x_csect.x_parmhash = 0;
4522 /* I don't think anybody uses these fields, but we'd
4523 better clobber them just in case. */
4524 aux.x_csect.x_stab = 0;
4525 aux.x_csect.x_snstab = 0;
4527 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4531 indx = aux.x_csect.x_scnlen.l;
4532 if (indx < obj_raw_syment_count (input_bfd))
4536 symindx = flinfo->sym_indices[indx];
4539 aux.x_csect.x_scnlen.l = 0;
4543 aux.x_csect.x_scnlen.l = symindx;
4548 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4552 if (ISFCN (isymp->n_type)
4553 || ISTAG (isymp->n_sclass)
4554 || isymp->n_sclass == C_BLOCK
4555 || isymp->n_sclass == C_FCN)
4557 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4559 && indx < obj_raw_syment_count (input_bfd))
4561 /* We look forward through the symbol for
4562 the index of the next symbol we are going
4563 to include. I don't know if this is
4565 while (flinfo->sym_indices[indx] < 0
4566 && indx < obj_raw_syment_count (input_bfd))
4568 if (indx >= obj_raw_syment_count (input_bfd))
4569 indx = output_index;
4571 indx = flinfo->sym_indices[indx];
4572 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4577 indx = aux.x_sym.x_tagndx.l;
4578 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4582 symindx = flinfo->sym_indices[indx];
4584 aux.x_sym.x_tagndx.l = 0;
4586 aux.x_sym.x_tagndx.l = symindx;
4591 /* Copy over the line numbers, unless we are stripping
4592 them. We do this on a symbol by symbol basis in
4593 order to more easily handle garbage collection. */
4594 if (CSECT_SYM_P (isymp->n_sclass)
4596 && isymp->n_numaux > 1
4597 && ISFCN (isymp->n_type)
4598 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4600 if (*lineno_counts == 0)
4601 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4604 asection *enclosing;
4605 unsigned int enc_count;
4606 bfd_signed_vma linoff;
4607 struct internal_lineno lin;
4614 /* Read in the enclosing section's line-number
4615 information, if we haven't already. */
4617 enclosing = xcoff_section_data (abfd, o)->enclosing;
4618 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4619 if (oline != enclosing)
4621 pos = enclosing->line_filepos;
4622 amt = linesz * enc_count;
4623 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4624 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4630 /* Copy across the first entry, adjusting its
4632 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4633 - enclosing->line_filepos);
4634 linp = flinfo->linenos + linoff;
4635 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4636 lin.l_addr.l_symndx = *indexp;
4637 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4639 /* Copy the other entries, adjusting their addresses. */
4640 linpend = linp + *lineno_counts * linesz;
4641 offset = (o->output_section->vma
4644 for (linp += linesz; linp < linpend; linp += linesz)
4646 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4647 lin.l_addr.l_paddr += offset;
4648 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4651 /* Write out the entries we've just processed. */
4652 pos = (o->output_section->line_filepos
4653 + o->output_section->lineno_count * linesz);
4654 amt = linesz * *lineno_counts;
4655 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4656 || bfd_bwrite (flinfo->linenos + linoff,
4657 amt, output_bfd) != amt)
4659 o->output_section->lineno_count += *lineno_counts;
4661 /* Record the offset of the symbol's line numbers
4662 in the output file. */
4663 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4667 struct internal_syment *iisp, *iispend;
4670 bfd_vma range_start, range_end;
4673 /* Update any C_BINCL or C_EINCL symbols
4674 that refer to a line number in the
4675 range we just output. */
4676 iisp = flinfo->internal_syms;
4677 iispend = iisp + obj_raw_syment_count (input_bfd);
4678 iindp = flinfo->sym_indices;
4679 oos = flinfo->outsyms;
4680 range_start = enclosing->line_filepos + linoff;
4681 range_end = range_start + *lineno_counts * linesz;
4682 while (iisp < iispend)
4685 && (iisp->n_sclass == C_BINCL
4686 || iisp->n_sclass == C_EINCL)
4687 && iisp->n_value >= range_start
4688 && iisp->n_value < range_end)
4690 struct internal_syment iis;
4692 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4693 iis.n_value = (iisp->n_value
4696 bfd_coff_swap_sym_out (output_bfd,
4701 iiadd = 1 + iisp->n_numaux;
4703 oos += iiadd * osymesz;
4711 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4712 isymp->n_sclass, i, isymp->n_numaux,
4723 lineno_counts += add;
4727 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4728 symbol will be the first symbol in the next input file. In the
4729 normal case, this will save us from writing out the C_FILE symbol
4731 if (flinfo->last_file_index != -1
4732 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4734 flinfo->last_file.n_value = output_index;
4735 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4736 (void *) (flinfo->outsyms
4737 + ((flinfo->last_file_index - syment_base)
4741 /* Write the modified symbols to the output file. */
4742 if (outsym > flinfo->outsyms)
4744 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4745 bfd_size_type amt = outsym - flinfo->outsyms;
4746 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4747 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4750 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4751 + (outsym - flinfo->outsyms) / osymesz)
4754 obj_raw_syment_count (output_bfd) = output_index;
4757 /* Don't let the linker relocation routines discard the symbols. */
4758 keep_syms = obj_coff_keep_syms (input_bfd);
4759 obj_coff_keep_syms (input_bfd) = TRUE;
4761 /* Relocate the contents of each section. */
4762 for (o = input_bfd->sections; o != NULL; o = o->next)
4766 if (! o->linker_mark)
4767 /* This section was omitted from the link. */
4770 if ((o->flags & SEC_HAS_CONTENTS) == 0
4772 || (o->flags & SEC_IN_MEMORY) != 0)
4775 /* We have set filepos correctly for the sections we created to
4776 represent csects, so bfd_get_section_contents should work. */
4777 if (coff_section_data (input_bfd, o) != NULL
4778 && coff_section_data (input_bfd, o)->contents != NULL)
4779 contents = coff_section_data (input_bfd, o)->contents;
4782 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4783 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4785 contents = flinfo->contents;
4788 if ((o->flags & SEC_RELOC) != 0)
4791 struct internal_reloc *internal_relocs;
4792 struct internal_reloc *irel;
4794 struct internal_reloc *irelend;
4795 struct xcoff_link_hash_entry **rel_hash;
4798 /* Read in the relocs. */
4799 target_index = o->output_section->target_index;
4800 internal_relocs = (xcoff_read_internal_relocs
4801 (input_bfd, o, FALSE, flinfo->external_relocs,
4803 (flinfo->section_info[target_index].relocs
4804 + o->output_section->reloc_count)));
4805 if (internal_relocs == NULL)
4808 /* Call processor specific code to relocate the section
4810 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4814 flinfo->internal_syms,
4815 xcoff_data (input_bfd)->csects))
4818 offset = o->output_section->vma + o->output_offset - o->vma;
4819 irel = internal_relocs;
4820 irelend = irel + o->reloc_count;
4821 rel_hash = (flinfo->section_info[target_index].rel_hashes
4822 + o->output_section->reloc_count);
4823 for (; irel < irelend; irel++, rel_hash++)
4825 struct xcoff_link_hash_entry *h = NULL;
4829 /* Adjust the reloc address and symbol index. */
4831 irel->r_vaddr += offset;
4833 r_symndx = irel->r_symndx;
4838 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4840 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4843 && h->smclas != XMC_TD
4844 && (irel->r_type == R_TOC
4845 || irel->r_type == R_GL
4846 || irel->r_type == R_TCL
4847 || irel->r_type == R_TRL
4848 || irel->r_type == R_TRLA))
4850 /* This is a TOC relative reloc with a symbol
4851 attached. The symbol should be the one which
4852 this reloc is for. We want to make this
4853 reloc against the TOC address of the symbol,
4854 not the symbol itself. */
4855 BFD_ASSERT (h->toc_section != NULL);
4856 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4857 if (h->u.toc_indx != -1)
4858 irel->r_symndx = h->u.toc_indx;
4861 struct xcoff_toc_rel_hash *n;
4862 struct xcoff_link_section_info *si;
4866 n = bfd_alloc (flinfo->output_bfd, amt);
4869 si = flinfo->section_info + target_index;
4870 n->next = si->toc_rel_hashes;
4873 si->toc_rel_hashes = n;
4878 /* This is a global symbol. */
4880 irel->r_symndx = h->indx;
4883 /* This symbol is being written at the end
4884 of the file, and we do not yet know the
4885 symbol index. We save the pointer to the
4886 hash table entry in the rel_hash list.
4887 We set the indx field to -2 to indicate
4888 that this symbol must not be stripped. */
4897 indx = flinfo->sym_indices[r_symndx];
4901 struct internal_syment *is;
4903 /* Relocations against a TC0 TOC anchor are
4904 automatically transformed to be against
4905 the TOC anchor in the output file. */
4906 is = flinfo->internal_syms + r_symndx;
4907 if (is->n_sclass == C_HIDEXT
4908 && is->n_numaux > 0)
4911 union internal_auxent aux;
4915 obj_coff_external_syms (input_bfd))
4916 + ((r_symndx + is->n_numaux)
4918 bfd_coff_swap_aux_in (input_bfd, auxptr,
4919 is->n_type, is->n_sclass,
4923 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4924 && aux.x_csect.x_smclas == XMC_TC0)
4925 indx = flinfo->toc_symindx;
4930 irel->r_symndx = indx;
4934 struct internal_syment *is;
4937 char buf[SYMNMLEN + 1];
4939 /* This reloc is against a symbol we are
4940 stripping. It would be possible to handle
4941 this case, but I don't think it's worth it. */
4942 is = flinfo->internal_syms + r_symndx;
4944 if (is->n_sclass != C_DWARF)
4946 name = (_bfd_coff_internal_syment_name
4947 (input_bfd, is, buf));
4952 (*flinfo->info->callbacks->unattached_reloc)
4953 (flinfo->info, name,
4954 input_bfd, o, irel->r_vaddr);
4960 if ((o->flags & SEC_DEBUGGING) == 0
4961 && xcoff_need_ldrel_p (flinfo->info, irel, h))
4968 sec = xcoff_data (input_bfd)->csects[r_symndx];
4970 sec = xcoff_symbol_section (h);
4971 if (!xcoff_create_ldrel (output_bfd, flinfo,
4972 o->output_section, input_bfd,
4978 o->output_section->reloc_count += o->reloc_count;
4981 /* Write out the modified section contents. */
4982 if (! bfd_set_section_contents (output_bfd, o->output_section,
4983 contents, (file_ptr) o->output_offset,
4988 obj_coff_keep_syms (input_bfd) = keep_syms;
4990 if (! flinfo->info->keep_memory)
4992 if (! _bfd_coff_free_symbols (input_bfd))
5002 /* Sort relocs by VMA. This is called via qsort. */
5005 xcoff_sort_relocs (const void * p1, const void * p2)
5007 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5008 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5010 if (r1->r_vaddr > r2->r_vaddr)
5012 else if (r1->r_vaddr < r2->r_vaddr)
5018 /* Return true if section SEC is a TOC section. */
5020 static inline bfd_boolean
5021 xcoff_toc_section_p (asection *sec)
5026 if (name[0] == '.' && name[1] == 't')
5030 if (name[3] == '0' && name[4] == 0)
5035 if (name[2] == 'd' && name[3] == 0)
5041 /* See if the link requires a TOC (it usually does!). If so, find a
5042 good place to put the TOC anchor csect, and write out the associated
5046 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5048 bfd_vma toc_start, toc_end, start, end, best_address;
5052 struct internal_syment irsym;
5053 union internal_auxent iraux;
5057 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5058 index of a csect at the beginning of the TOC. */
5059 toc_start = ~(bfd_vma) 0;
5062 for (input_bfd = flinfo->info->input_bfds;
5064 input_bfd = input_bfd->link.next)
5065 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5066 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5068 start = sec->output_section->vma + sec->output_offset;
5069 if (toc_start > start)
5072 section_index = sec->output_section->target_index;
5075 end = start + sec->size;
5080 /* There's no need for a TC0 symbol if we don't have a TOC. */
5081 if (toc_end < toc_start)
5083 xcoff_data (output_bfd)->toc = toc_start;
5087 if (toc_end - toc_start < 0x8000)
5088 /* Every TOC csect can be accessed from TOC_START. */
5089 best_address = toc_start;
5092 /* Find the lowest TOC csect that is still within range of TOC_END. */
5093 best_address = toc_end;
5094 for (input_bfd = flinfo->info->input_bfds;
5096 input_bfd = input_bfd->link.next)
5097 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5098 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5100 start = sec->output_section->vma + sec->output_offset;
5101 if (start < best_address
5102 && start + 0x8000 >= toc_end)
5104 best_address = start;
5105 section_index = sec->output_section->target_index;
5109 /* Make sure that the start of the TOC is also within range. */
5110 if (best_address > toc_start + 0x8000)
5113 (_("TOC overflow: %#" PRIx64 " > 0x10000; try -mminimal-toc "
5115 (uint64_t) (toc_end - toc_start));
5116 bfd_set_error (bfd_error_file_too_big);
5121 /* Record the chosen TOC value. */
5122 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5123 xcoff_data (output_bfd)->toc = best_address;
5124 xcoff_data (output_bfd)->sntoc = section_index;
5126 /* Fill out the TC0 symbol. */
5127 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
5130 irsym.n_value = best_address;
5131 irsym.n_scnum = section_index;
5132 irsym.n_sclass = C_HIDEXT;
5133 irsym.n_type = T_NULL;
5135 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5137 /* Fill out the auxillary csect information. */
5138 memset (&iraux, 0, sizeof iraux);
5139 iraux.x_csect.x_smtyp = XTY_SD;
5140 iraux.x_csect.x_smclas = XMC_TC0;
5141 iraux.x_csect.x_scnlen.l = 0;
5142 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5143 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5145 /* Write the contents to the file. */
5146 pos = obj_sym_filepos (output_bfd);
5147 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5148 size = 2 * bfd_coff_symesz (output_bfd);
5149 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5150 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5152 obj_raw_syment_count (output_bfd) += 2;
5157 /* Write out a non-XCOFF global symbol. */
5160 xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
5162 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh;
5163 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5166 struct internal_syment isym;
5167 union internal_auxent aux;
5172 output_bfd = flinfo->output_bfd;
5173 outsym = flinfo->outsyms;
5175 if (h->root.type == bfd_link_hash_warning)
5177 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5178 if (h->root.type == bfd_link_hash_new)
5182 /* If this symbol was garbage collected, just skip it. */
5183 if (xcoff_hash_table (flinfo->info)->gc
5184 && (h->flags & XCOFF_MARK) == 0)
5187 /* If we need a .loader section entry, write it out. */
5188 if (h->ldsym != NULL)
5190 struct internal_ldsym *ldsym;
5195 if (h->root.type == bfd_link_hash_undefined
5196 || h->root.type == bfd_link_hash_undefweak)
5200 ldsym->l_scnum = N_UNDEF;
5201 ldsym->l_smtype = XTY_ER;
5202 impbfd = h->root.u.undef.abfd;
5205 else if (h->root.type == bfd_link_hash_defined
5206 || h->root.type == bfd_link_hash_defweak)
5210 sec = h->root.u.def.section;
5211 ldsym->l_value = (sec->output_section->vma
5212 + sec->output_offset
5213 + h->root.u.def.value);
5214 ldsym->l_scnum = sec->output_section->target_index;
5215 ldsym->l_smtype = XTY_SD;
5216 impbfd = sec->owner;
5222 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5223 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5224 || (h->flags & XCOFF_IMPORT) != 0)
5226 Import symbols are defined so the check above will make
5227 the l_smtype XTY_SD. But this is not correct, it should
5229 ldsym->l_smtype |= L_IMPORT;
5231 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5232 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5233 || (h->flags & XCOFF_EXPORT) != 0)
5234 ldsym->l_smtype |= L_EXPORT;
5236 if ((h->flags & XCOFF_ENTRY) != 0)
5237 ldsym->l_smtype |= L_ENTRY;
5239 if ((h->flags & XCOFF_RTINIT) != 0)
5240 ldsym->l_smtype = XTY_SD;
5242 ldsym->l_smclas = h->smclas;
5244 if (ldsym->l_smtype & L_IMPORT)
5246 if ((h->root.type == bfd_link_hash_defined
5247 || h->root.type == bfd_link_hash_defweak)
5248 && (h->root.u.def.value != 0))
5249 ldsym->l_smclas = XMC_XO;
5251 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5252 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5253 ldsym->l_smclas = XMC_SV3264;
5255 else if (h->flags & XCOFF_SYSCALL32)
5256 ldsym->l_smclas = XMC_SV;
5258 else if (h->flags & XCOFF_SYSCALL64)
5259 ldsym->l_smclas = XMC_SV64;
5262 if (ldsym->l_ifile == -(bfd_size_type) 1)
5266 else if (ldsym->l_ifile == 0)
5268 if ((ldsym->l_smtype & L_IMPORT) == 0)
5270 else if (impbfd == NULL)
5274 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5275 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5281 BFD_ASSERT (h->ldindx >= 0);
5283 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5286 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5290 /* If this symbol needs global linkage code, write it out. */
5291 if (h->root.type == bfd_link_hash_defined
5292 && (h->root.u.def.section
5293 == xcoff_hash_table (flinfo->info)->linkage_section))
5299 p = h->root.u.def.section->contents + h->root.u.def.value;
5301 /* The first instruction in the global linkage code loads a
5302 specific TOC element. */
5303 tocoff = (h->descriptor->toc_section->output_section->vma
5304 + h->descriptor->toc_section->output_offset
5305 - xcoff_data (output_bfd)->toc);
5307 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5308 tocoff += h->descriptor->u.toc_offset;
5310 /* The first instruction in the glink code needs to be
5311 cooked to hold the correct offset in the toc. The
5312 rest are just output raw. */
5313 bfd_put_32 (output_bfd,
5314 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5316 /* Start with i == 1 to get past the first instruction done above
5317 The /4 is because the glink code is in bytes and we are going
5319 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5320 bfd_put_32 (output_bfd,
5321 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5325 /* If we created a TOC entry for this symbol, write out the required
5327 if ((h->flags & XCOFF_SET_TOC) != 0)
5332 struct internal_reloc *irel;
5333 struct internal_syment irsym;
5334 union internal_auxent iraux;
5336 tocsec = h->toc_section;
5337 osec = tocsec->output_section;
5338 oindx = osec->target_index;
5339 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5340 irel->r_vaddr = (osec->vma
5341 + tocsec->output_offset
5345 irel->r_symndx = h->indx;
5349 irel->r_symndx = obj_raw_syment_count (output_bfd);
5352 BFD_ASSERT (h->ldindx >= 0);
5354 /* Initialize the aux union here instead of closer to when it is
5355 written out below because the length of the csect depends on
5356 whether the output is 32 or 64 bit. */
5357 memset (&iraux, 0, sizeof iraux);
5358 iraux.x_csect.x_smtyp = XTY_SD;
5359 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5360 iraux.x_csect.x_smclas = XMC_TC;
5362 /* 32 bit uses a 32 bit R_POS to do the relocations
5363 64 bit uses a 64 bit R_POS to do the relocations
5365 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5367 Which one is determined by the backend. */
5368 if (bfd_xcoff_is_xcoff64 (output_bfd))
5371 iraux.x_csect.x_scnlen.l = 8;
5373 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5376 iraux.x_csect.x_scnlen.l = 4;
5381 irel->r_type = R_POS;
5382 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5383 ++osec->reloc_count;
5385 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5386 output_bfd, irel, NULL, h))
5389 /* We need to emit a symbol to define a csect which holds
5391 if (flinfo->info->strip != strip_all)
5393 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info,
5395 &irsym, h->root.root.string);
5399 irsym.n_value = irel->r_vaddr;
5400 irsym.n_scnum = osec->target_index;
5401 irsym.n_sclass = C_HIDEXT;
5402 irsym.n_type = T_NULL;
5405 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5406 outsym += bfd_coff_symesz (output_bfd);
5408 /* Note : iraux is initialized above. */
5409 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5410 0, 1, (void *) outsym);
5411 outsym += bfd_coff_auxesz (output_bfd);
5415 /* We aren't going to write out the symbols below, so we
5416 need to write them out now. */
5417 pos = obj_sym_filepos (output_bfd);
5418 pos += (obj_raw_syment_count (output_bfd)
5419 * bfd_coff_symesz (output_bfd));
5420 amt = outsym - flinfo->outsyms;
5421 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5422 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5424 obj_raw_syment_count (output_bfd) +=
5425 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5427 outsym = flinfo->outsyms;
5432 /* If this symbol is a specially defined function descriptor, write
5433 it out. The first word is the address of the function code
5434 itself, the second word is the address of the TOC, and the third
5438 The addresses for the 32 bit will take 4 bytes and the addresses
5439 for 64 bit will take 8 bytes. Similar for the relocs. This type
5440 of logic was also done above to create a TOC entry in
5441 xcoff_write_global_symbol. */
5442 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5443 && h->root.type == bfd_link_hash_defined
5444 && (h->root.u.def.section
5445 == xcoff_hash_table (flinfo->info)->descriptor_section))
5451 struct xcoff_link_hash_entry *hentry;
5453 struct internal_reloc *irel;
5455 unsigned int reloc_size, byte_size;
5457 if (bfd_xcoff_is_xcoff64 (output_bfd))
5462 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5470 sec = h->root.u.def.section;
5471 osec = sec->output_section;
5472 oindx = osec->target_index;
5473 p = sec->contents + h->root.u.def.value;
5475 hentry = h->descriptor;
5476 BFD_ASSERT (hentry != NULL
5477 && (hentry->root.type == bfd_link_hash_defined
5478 || hentry->root.type == bfd_link_hash_defweak));
5479 esec = hentry->root.u.def.section;
5481 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5482 irel->r_vaddr = (osec->vma
5483 + sec->output_offset
5484 + h->root.u.def.value);
5485 irel->r_symndx = esec->output_section->target_index;
5486 irel->r_type = R_POS;
5487 irel->r_size = reloc_size;
5488 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5489 ++osec->reloc_count;
5491 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5492 output_bfd, irel, esec, NULL))
5495 /* There are three items to write out,
5496 the address of the code
5497 the address of the toc anchor
5498 the environment pointer.
5499 We are ignoring the environment pointer. So set it to zero. */
5500 if (bfd_xcoff_is_xcoff64 (output_bfd))
5502 bfd_put_64 (output_bfd,
5503 (esec->output_section->vma + esec->output_offset
5504 + hentry->root.u.def.value),
5506 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5507 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5512 This logic was already called above so the error case where
5513 the backend is neither has already been checked. */
5514 bfd_put_32 (output_bfd,
5515 (esec->output_section->vma + esec->output_offset
5516 + hentry->root.u.def.value),
5518 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5519 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5522 tsec = coff_section_from_bfd_index (output_bfd,
5523 xcoff_data (output_bfd)->sntoc);
5526 irel->r_vaddr = (osec->vma
5527 + sec->output_offset
5528 + h->root.u.def.value
5530 irel->r_symndx = tsec->output_section->target_index;
5531 irel->r_type = R_POS;
5532 irel->r_size = reloc_size;
5533 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5534 ++osec->reloc_count;
5536 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5537 output_bfd, irel, tsec, NULL))
5541 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5543 BFD_ASSERT (outsym == flinfo->outsyms);
5548 && (flinfo->info->strip == strip_all
5549 || (flinfo->info->strip == strip_some
5550 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5551 FALSE, FALSE) == NULL)))
5553 BFD_ASSERT (outsym == flinfo->outsyms);
5558 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5560 BFD_ASSERT (outsym == flinfo->outsyms);
5564 memset (&aux, 0, sizeof aux);
5566 h->indx = obj_raw_syment_count (output_bfd);
5568 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
5569 &isym, h->root.root.string);
5573 if (h->root.type == bfd_link_hash_undefined
5574 || h->root.type == bfd_link_hash_undefweak)
5577 isym.n_scnum = N_UNDEF;
5578 if (h->root.type == bfd_link_hash_undefweak
5579 && C_WEAKEXT == C_AIX_WEAKEXT)
5580 isym.n_sclass = C_WEAKEXT;
5582 isym.n_sclass = C_EXT;
5583 aux.x_csect.x_smtyp = XTY_ER;
5585 else if ((h->root.type == bfd_link_hash_defined
5586 || h->root.type == bfd_link_hash_defweak)
5587 && h->smclas == XMC_XO)
5589 BFD_ASSERT (bfd_is_abs_symbol (&h->root));
5590 isym.n_value = h->root.u.def.value;
5591 isym.n_scnum = N_UNDEF;
5592 if (h->root.type == bfd_link_hash_defweak
5593 && C_WEAKEXT == C_AIX_WEAKEXT)
5594 isym.n_sclass = C_WEAKEXT;
5596 isym.n_sclass = C_EXT;
5597 aux.x_csect.x_smtyp = XTY_ER;
5599 else if (h->root.type == bfd_link_hash_defined
5600 || h->root.type == bfd_link_hash_defweak)
5602 struct xcoff_link_size_list *l;
5604 isym.n_value = (h->root.u.def.section->output_section->vma
5605 + h->root.u.def.section->output_offset
5606 + h->root.u.def.value);
5607 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5608 isym.n_scnum = N_ABS;
5610 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5611 isym.n_sclass = C_HIDEXT;
5612 aux.x_csect.x_smtyp = XTY_SD;
5614 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5616 for (l = xcoff_hash_table (flinfo->info)->size_list;
5622 aux.x_csect.x_scnlen.l = l->size;
5628 else if (h->root.type == bfd_link_hash_common)
5630 isym.n_value = (h->root.u.c.p->section->output_section->vma
5631 + h->root.u.c.p->section->output_offset);
5632 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5633 isym.n_sclass = C_EXT;
5634 aux.x_csect.x_smtyp = XTY_CM;
5635 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5640 isym.n_type = T_NULL;
5643 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5644 outsym += bfd_coff_symesz (output_bfd);
5646 aux.x_csect.x_smclas = h->smclas;
5647 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5649 outsym += bfd_coff_auxesz (output_bfd);
5651 if ((h->root.type == bfd_link_hash_defined
5652 || h->root.type == bfd_link_hash_defweak)
5653 && h->smclas != XMC_XO)
5655 /* We just output an SD symbol. Now output an LD symbol. */
5658 if (h->root.type == bfd_link_hash_defweak
5659 && C_WEAKEXT == C_AIX_WEAKEXT)
5660 isym.n_sclass = C_WEAKEXT;
5662 isym.n_sclass = C_EXT;
5663 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5664 outsym += bfd_coff_symesz (output_bfd);
5666 aux.x_csect.x_smtyp = XTY_LD;
5667 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5668 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5670 outsym += bfd_coff_auxesz (output_bfd);
5673 pos = obj_sym_filepos (output_bfd);
5674 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5675 amt = outsym - flinfo->outsyms;
5676 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5677 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5679 obj_raw_syment_count (output_bfd) +=
5680 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5685 /* Handle a link order which is supposed to generate a reloc. */
5688 xcoff_reloc_link_order (bfd *output_bfd,
5689 struct xcoff_final_link_info *flinfo,
5690 asection *output_section,
5691 struct bfd_link_order *link_order)
5693 reloc_howto_type *howto;
5694 struct xcoff_link_hash_entry *h;
5698 struct internal_reloc *irel;
5699 struct xcoff_link_hash_entry **rel_hash_ptr;
5701 if (link_order->type == bfd_section_reloc_link_order)
5702 /* We need to somehow locate a symbol in the right section. The
5703 symbol must either have a value of zero, or we must adjust
5704 the addend by the value of the symbol. FIXME: Write this
5705 when we need it. The old linker couldn't handle this anyhow. */
5708 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5711 bfd_set_error (bfd_error_bad_value);
5715 h = ((struct xcoff_link_hash_entry *)
5716 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5717 link_order->u.reloc.p->u.name,
5718 FALSE, FALSE, TRUE));
5721 (*flinfo->info->callbacks->unattached_reloc)
5722 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
5726 hsec = xcoff_symbol_section (h);
5727 if (h->root.type == bfd_link_hash_defined
5728 || h->root.type == bfd_link_hash_defweak)
5729 hval = h->root.u.def.value;
5733 addend = link_order->u.reloc.p->addend;
5735 addend += (hsec->output_section->vma
5736 + hsec->output_offset
5743 bfd_reloc_status_type rstat;
5746 size = bfd_get_reloc_size (howto);
5747 buf = bfd_zmalloc (size);
5748 if (buf == NULL && size != 0)
5751 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5757 case bfd_reloc_outofrange:
5759 case bfd_reloc_overflow:
5760 (*flinfo->info->callbacks->reloc_overflow)
5761 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5762 howto->name, addend, NULL, NULL, (bfd_vma) 0);
5765 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5766 (file_ptr) link_order->offset, size);
5772 /* Store the reloc information in the right place. It will get
5773 swapped and written out at the end of the final_link routine. */
5774 irel = (flinfo->section_info[output_section->target_index].relocs
5775 + output_section->reloc_count);
5776 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5777 + output_section->reloc_count);
5779 memset (irel, 0, sizeof (struct internal_reloc));
5780 *rel_hash_ptr = NULL;
5782 irel->r_vaddr = output_section->vma + link_order->offset;
5785 irel->r_symndx = h->indx;
5788 /* Set the index to -2 to force this symbol to get written out. */
5794 irel->r_type = howto->type;
5795 irel->r_size = howto->bitsize - 1;
5796 if (howto->complain_on_overflow == complain_overflow_signed)
5797 irel->r_size |= 0x80;
5799 ++output_section->reloc_count;
5801 /* Now output the reloc to the .loader section. */
5802 if (xcoff_hash_table (flinfo->info)->loader_section)
5804 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5805 output_bfd, irel, hsec, h))
5812 /* Do the final link step. */
5815 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5817 bfd_size_type symesz;
5818 struct xcoff_final_link_info flinfo;
5820 struct bfd_link_order *p;
5821 bfd_size_type max_contents_size;
5822 bfd_size_type max_sym_count;
5823 bfd_size_type max_lineno_count;
5824 bfd_size_type max_reloc_count;
5825 bfd_size_type max_output_reloc_count;
5826 file_ptr rel_filepos;
5828 file_ptr line_filepos;
5829 unsigned int linesz;
5831 bfd_byte *external_relocs = NULL;
5832 char strbuf[STRING_SIZE_SIZE];
5836 if (bfd_link_pic (info))
5837 abfd->flags |= DYNAMIC;
5839 symesz = bfd_coff_symesz (abfd);
5842 flinfo.output_bfd = abfd;
5843 flinfo.strtab = NULL;
5844 flinfo.section_info = NULL;
5845 flinfo.last_file_index = -1;
5846 flinfo.toc_symindx = -1;
5847 flinfo.internal_syms = NULL;
5848 flinfo.sym_indices = NULL;
5849 flinfo.outsyms = NULL;
5850 flinfo.linenos = NULL;
5851 flinfo.contents = NULL;
5852 flinfo.external_relocs = NULL;
5854 if (xcoff_hash_table (info)->loader_section)
5856 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5857 + bfd_xcoff_ldhdrsz (abfd));
5858 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5859 + bfd_xcoff_ldhdrsz (abfd)
5860 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5861 * bfd_xcoff_ldsymsz (abfd)));
5865 flinfo.ldsym = NULL;
5866 flinfo.ldrel = NULL;
5869 xcoff_data (abfd)->coff.link_info = info;
5871 flinfo.strtab = _bfd_stringtab_init ();
5872 if (flinfo.strtab == NULL)
5875 /* Count the relocation entries required for the output file.
5876 (We've already counted the line numbers.) Determine a few
5878 max_contents_size = 0;
5879 max_lineno_count = 0;
5880 max_reloc_count = 0;
5881 for (o = abfd->sections; o != NULL; o = o->next)
5884 for (p = o->map_head.link_order; p != NULL; p = p->next)
5886 if (p->type == bfd_indirect_link_order)
5890 sec = p->u.indirect.section;
5892 /* Mark all sections which are to be included in the
5893 link. This will normally be every section. We need
5894 to do this so that we can identify any sections which
5895 the linker has decided to not include. */
5896 sec->linker_mark = TRUE;
5898 o->reloc_count += sec->reloc_count;
5900 if ((sec->flags & SEC_IN_MEMORY) == 0)
5902 if (sec->rawsize > max_contents_size)
5903 max_contents_size = sec->rawsize;
5904 if (sec->size > max_contents_size)
5905 max_contents_size = sec->size;
5907 if (coff_section_data (sec->owner, sec) != NULL
5908 && xcoff_section_data (sec->owner, sec) != NULL
5909 && (xcoff_section_data (sec->owner, sec)->lineno_count
5910 > max_lineno_count))
5912 xcoff_section_data (sec->owner, sec)->lineno_count;
5913 if (sec->reloc_count > max_reloc_count)
5914 max_reloc_count = sec->reloc_count;
5916 else if (p->type == bfd_section_reloc_link_order
5917 || p->type == bfd_symbol_reloc_link_order)
5922 /* Compute the file positions for all the sections. */
5923 if (abfd->output_has_begun)
5925 if (xcoff_hash_table (info)->file_align != 0)
5932 file_align = xcoff_hash_table (info)->file_align;
5933 if (file_align != 0)
5935 bfd_boolean saw_contents;
5939 /* Insert .pad sections before every section which has
5940 contents and is loaded, if it is preceded by some other
5941 section which has contents and is loaded. */
5942 saw_contents = TRUE;
5943 for (o = abfd->sections; o != NULL; o = o->next)
5945 if (strcmp (o->name, ".pad") == 0)
5946 saw_contents = FALSE;
5947 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5948 && (o->flags & SEC_LOAD) != 0)
5951 saw_contents = TRUE;
5956 /* Create a pad section and place it before the section
5957 that needs padding. This requires unlinking and
5958 relinking the bfd's section list. */
5960 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5962 n->alignment_power = 0;
5964 bfd_section_list_remove (abfd, n);
5965 bfd_section_list_insert_before (abfd, o, n);
5966 saw_contents = FALSE;
5971 /* Reset the section indices after inserting the new
5974 for (o = abfd->sections; o != NULL; o = o->next)
5977 o->target_index = indx;
5979 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5981 /* Work out appropriate sizes for the .pad sections to force
5982 each section to land on a page boundary. This bit of
5983 code knows what compute_section_file_positions is going
5985 sofar = bfd_coff_filhsz (abfd);
5986 sofar += bfd_coff_aoutsz (abfd);
5987 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5988 for (o = abfd->sections; o != NULL; o = o->next)
5989 if ((bfd_xcoff_is_reloc_count_overflow
5990 (abfd, (bfd_vma) o->reloc_count))
5991 || (bfd_xcoff_is_lineno_count_overflow
5992 (abfd, (bfd_vma) o->lineno_count)))
5993 /* 64 does not overflow, need to check if 32 does */
5994 sofar += bfd_coff_scnhsz (abfd);
5996 for (o = abfd->sections; o != NULL; o = o->next)
5998 if (strcmp (o->name, ".pad") == 0)
6002 BFD_ASSERT (o->size == 0);
6003 pageoff = sofar & (file_align - 1);
6006 o->size = file_align - pageoff;
6007 sofar += file_align - pageoff;
6008 o->flags |= SEC_HAS_CONTENTS;
6013 if ((o->flags & SEC_HAS_CONTENTS) != 0)
6014 sofar += BFD_ALIGN (o->size,
6015 1 << o->alignment_power);
6020 if (! bfd_coff_compute_section_file_positions (abfd))
6024 /* Allocate space for the pointers we need to keep for the relocs. */
6028 /* We use section_count + 1, rather than section_count, because
6029 the target_index fields are 1 based. */
6030 amt = abfd->section_count + 1;
6031 amt *= sizeof (struct xcoff_link_section_info);
6032 flinfo.section_info = bfd_malloc (amt);
6033 if (flinfo.section_info == NULL)
6035 for (i = 0; i <= abfd->section_count; i++)
6037 flinfo.section_info[i].relocs = NULL;
6038 flinfo.section_info[i].rel_hashes = NULL;
6039 flinfo.section_info[i].toc_rel_hashes = NULL;
6043 /* Set the file positions for the relocs. */
6044 rel_filepos = obj_relocbase (abfd);
6045 relsz = bfd_coff_relsz (abfd);
6046 max_output_reloc_count = 0;
6047 for (o = abfd->sections; o != NULL; o = o->next)
6049 if (o->reloc_count == 0)
6053 /* A stripped file has no relocs. However, we still
6054 allocate the buffers, so that later code doesn't have to
6055 worry about whether we are stripping or not. */
6056 if (info->strip == strip_all)
6060 o->flags |= SEC_RELOC;
6061 o->rel_filepos = rel_filepos;
6062 rel_filepos += o->reloc_count * relsz;
6065 /* We don't know the indices of global symbols until we have
6066 written out all the local symbols. For each section in
6067 the output file, we keep an array of pointers to hash
6068 table entries. Each entry in the array corresponds to a
6069 reloc. When we find a reloc against a global symbol, we
6070 set the corresponding entry in this array so that we can
6071 fix up the symbol index after we have written out all the
6074 Because of this problem, we also keep the relocs in
6075 memory until the end of the link. This wastes memory.
6076 We could backpatch the file later, I suppose, although it
6078 amt = o->reloc_count;
6079 amt *= sizeof (struct internal_reloc);
6080 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6082 amt = o->reloc_count;
6083 amt *= sizeof (struct xcoff_link_hash_entry *);
6084 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6086 if (flinfo.section_info[o->target_index].relocs == NULL
6087 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6090 if (o->reloc_count > max_output_reloc_count)
6091 max_output_reloc_count = o->reloc_count;
6095 /* We now know the size of the relocs, so we can determine the file
6096 positions of the line numbers. */
6097 line_filepos = rel_filepos;
6098 flinfo.line_filepos = line_filepos;
6099 linesz = bfd_coff_linesz (abfd);
6100 for (o = abfd->sections; o != NULL; o = o->next)
6102 if (o->lineno_count == 0)
6103 o->line_filepos = 0;
6106 o->line_filepos = line_filepos;
6107 line_filepos += o->lineno_count * linesz;
6110 /* Reset the reloc and lineno counts, so that we can use them to
6111 count the number of entries we have output so far. */
6113 o->lineno_count = 0;
6116 obj_sym_filepos (abfd) = line_filepos;
6118 /* Figure out the largest number of symbols in an input BFD. Take
6119 the opportunity to clear the output_has_begun fields of all the
6120 input BFD's. We want at least 6 symbols, since that is the
6121 number which xcoff_write_global_symbol may need. */
6123 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6127 sub->output_has_begun = FALSE;
6128 sz = obj_raw_syment_count (sub);
6129 if (sz > max_sym_count)
6133 /* Allocate some buffers used while linking. */
6134 amt = max_sym_count * sizeof (struct internal_syment);
6135 flinfo.internal_syms = bfd_malloc (amt);
6137 amt = max_sym_count * sizeof (long);
6138 flinfo.sym_indices = bfd_malloc (amt);
6140 amt = (max_sym_count + 1) * symesz;
6141 flinfo.outsyms = bfd_malloc (amt);
6143 amt = max_lineno_count * bfd_coff_linesz (abfd);
6144 flinfo.linenos = bfd_malloc (amt);
6146 amt = max_contents_size;
6147 flinfo.contents = bfd_malloc (amt);
6149 amt = max_reloc_count * relsz;
6150 flinfo.external_relocs = bfd_malloc (amt);
6152 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6153 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6154 || flinfo.outsyms == NULL
6155 || (flinfo.linenos == NULL && max_lineno_count > 0)
6156 || (flinfo.contents == NULL && max_contents_size > 0)
6157 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6160 obj_raw_syment_count (abfd) = 0;
6162 /* Find a TOC symbol, if we need one. */
6163 if (!xcoff_find_tc0 (abfd, &flinfo))
6166 /* We now know the position of everything in the file, except that
6167 we don't know the size of the symbol table and therefore we don't
6168 know where the string table starts. We just build the string
6169 table in memory as we go along. We process all the relocations
6170 for a single input file at once. */
6171 for (o = abfd->sections; o != NULL; o = o->next)
6173 for (p = o->map_head.link_order; p != NULL; p = p->next)
6175 if (p->type == bfd_indirect_link_order
6176 && p->u.indirect.section->owner->xvec == abfd->xvec)
6178 sub = p->u.indirect.section->owner;
6179 if (! sub->output_has_begun)
6181 if (! xcoff_link_input_bfd (&flinfo, sub))
6183 sub->output_has_begun = TRUE;
6186 else if (p->type == bfd_section_reloc_link_order
6187 || p->type == bfd_symbol_reloc_link_order)
6189 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6194 if (! _bfd_default_link_order (abfd, info, o, p))
6200 /* Free up the buffers used by xcoff_link_input_bfd. */
6201 if (flinfo.internal_syms != NULL)
6203 free (flinfo.internal_syms);
6204 flinfo.internal_syms = NULL;
6206 if (flinfo.sym_indices != NULL)
6208 free (flinfo.sym_indices);
6209 flinfo.sym_indices = NULL;
6211 if (flinfo.linenos != NULL)
6213 free (flinfo.linenos);
6214 flinfo.linenos = NULL;
6216 if (flinfo.contents != NULL)
6218 free (flinfo.contents);
6219 flinfo.contents = NULL;
6221 if (flinfo.external_relocs != NULL)
6223 free (flinfo.external_relocs);
6224 flinfo.external_relocs = NULL;
6227 /* The value of the last C_FILE symbol is supposed to be -1. Write
6229 if (flinfo.last_file_index != -1)
6231 flinfo.last_file.n_value = -(bfd_vma) 1;
6232 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
6233 (void *) flinfo.outsyms);
6234 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
6235 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6236 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
6240 /* Write out all the global symbols which do not come from XCOFF
6242 bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo);
6244 if (flinfo.outsyms != NULL)
6246 free (flinfo.outsyms);
6247 flinfo.outsyms = NULL;
6250 /* Now that we have written out all the global symbols, we know the
6251 symbol indices to use for relocs against them, and we can finally
6252 write out the relocs. */
6253 amt = max_output_reloc_count * relsz;
6254 external_relocs = bfd_malloc (amt);
6255 if (external_relocs == NULL && max_output_reloc_count != 0)
6258 for (o = abfd->sections; o != NULL; o = o->next)
6260 struct internal_reloc *irel;
6261 struct internal_reloc *irelend;
6262 struct xcoff_link_hash_entry **rel_hash;
6263 struct xcoff_toc_rel_hash *toc_rel_hash;
6265 bfd_size_type rel_size;
6267 /* A stripped file has no relocs. */
6268 if (info->strip == strip_all)
6274 if (o->reloc_count == 0)
6277 irel = flinfo.section_info[o->target_index].relocs;
6278 irelend = irel + o->reloc_count;
6279 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
6280 for (; irel < irelend; irel++, rel_hash++)
6282 if (*rel_hash != NULL)
6284 if ((*rel_hash)->indx < 0)
6286 (*info->callbacks->unattached_reloc)
6287 (info, (*rel_hash)->root.root.string,
6288 NULL, o, irel->r_vaddr);
6289 (*rel_hash)->indx = 0;
6291 irel->r_symndx = (*rel_hash)->indx;
6295 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6296 toc_rel_hash != NULL;
6297 toc_rel_hash = toc_rel_hash->next)
6299 if (toc_rel_hash->h->u.toc_indx < 0)
6301 (*info->callbacks->unattached_reloc)
6302 (info, toc_rel_hash->h->root.root.string,
6303 NULL, o, toc_rel_hash->rel->r_vaddr);
6304 toc_rel_hash->h->u.toc_indx = 0;
6306 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6309 /* XCOFF requires that the relocs be sorted by address. We tend
6310 to produce them in the order in which their containing csects
6311 appear in the symbol table, which is not necessarily by
6312 address. So we sort them here. There may be a better way to
6314 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6315 o->reloc_count, sizeof (struct internal_reloc),
6318 irel = flinfo.section_info[o->target_index].relocs;
6319 irelend = irel + o->reloc_count;
6320 erel = external_relocs;
6321 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6322 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6324 rel_size = relsz * o->reloc_count;
6325 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6326 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6330 if (external_relocs != NULL)
6332 free (external_relocs);
6333 external_relocs = NULL;
6336 /* Free up the section information. */
6337 if (flinfo.section_info != NULL)
6341 for (i = 0; i < abfd->section_count; i++)
6343 if (flinfo.section_info[i].relocs != NULL)
6344 free (flinfo.section_info[i].relocs);
6345 if (flinfo.section_info[i].rel_hashes != NULL)
6346 free (flinfo.section_info[i].rel_hashes);
6348 free (flinfo.section_info);
6349 flinfo.section_info = NULL;
6352 /* Write out the loader section contents. */
6353 o = xcoff_hash_table (info)->loader_section;
6356 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
6357 == (xcoff_hash_table (info)->loader_section->contents
6358 + xcoff_hash_table (info)->ldhdr.l_impoff));
6359 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6360 (file_ptr) o->output_offset, o->size))
6364 /* Write out the magic sections. */
6365 o = xcoff_hash_table (info)->linkage_section;
6367 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6368 (file_ptr) o->output_offset,
6371 o = xcoff_hash_table (info)->toc_section;
6373 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6374 (file_ptr) o->output_offset,
6377 o = xcoff_hash_table (info)->descriptor_section;
6379 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6380 (file_ptr) o->output_offset,
6384 /* Write out the string table. */
6385 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6386 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6389 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6391 amt = STRING_SIZE_SIZE;
6392 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6394 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6397 _bfd_stringtab_free (flinfo.strtab);
6399 /* Write out the debugging string table. */
6400 o = xcoff_hash_table (info)->debug_section;
6403 struct bfd_strtab_hash *debug_strtab;
6405 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6406 BFD_ASSERT (o->output_section->size - o->output_offset
6407 >= _bfd_stringtab_size (debug_strtab));
6408 pos = o->output_section->filepos + o->output_offset;
6409 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6411 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6415 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6416 not try to write out the symbols. */
6417 bfd_get_symcount (abfd) = 0;
6422 if (flinfo.strtab != NULL)
6423 _bfd_stringtab_free (flinfo.strtab);
6425 if (flinfo.section_info != NULL)
6429 for (i = 0; i < abfd->section_count; i++)
6431 if (flinfo.section_info[i].relocs != NULL)
6432 free (flinfo.section_info[i].relocs);
6433 if (flinfo.section_info[i].rel_hashes != NULL)
6434 free (flinfo.section_info[i].rel_hashes);
6436 free (flinfo.section_info);
6439 if (flinfo.internal_syms != NULL)
6440 free (flinfo.internal_syms);
6441 if (flinfo.sym_indices != NULL)
6442 free (flinfo.sym_indices);
6443 if (flinfo.outsyms != NULL)
6444 free (flinfo.outsyms);
6445 if (flinfo.linenos != NULL)
6446 free (flinfo.linenos);
6447 if (flinfo.contents != NULL)
6448 free (flinfo.contents);
6449 if (flinfo.external_relocs != NULL)
6450 free (flinfo.external_relocs);
6451 if (external_relocs != NULL)
6452 free (external_relocs);