1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
31 #include "libiberty.h"
33 /* This file holds the XCOFF linker code. */
35 #undef STRING_SIZE_SIZE
36 #define STRING_SIZE_SIZE 4
38 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
39 This flag will only be used on input sections. */
41 #define SEC_MARK (SEC_ROM)
43 /* The list of import files. */
45 struct xcoff_import_file
47 /* The next entry in the list. */
48 struct xcoff_import_file *next;
53 /* The member name. */
57 /* Information we keep for each section in the output file during the
60 struct xcoff_link_section_info
62 /* The relocs to be output. */
63 struct internal_reloc *relocs;
64 /* For each reloc against a global symbol whose index was not known
65 when the reloc was handled, the global hash table entry. */
66 struct xcoff_link_hash_entry **rel_hashes;
67 /* If there is a TOC relative reloc against a global symbol, and the
68 index of the TOC symbol is not known when the reloc was handled,
69 an entry is added to this linked list. This is not an array,
70 like rel_hashes, because this case is quite uncommon. */
71 struct xcoff_toc_rel_hash
73 struct xcoff_toc_rel_hash *next;
74 struct xcoff_link_hash_entry *h;
75 struct internal_reloc *rel;
79 /* Information that the XCOFF linker collects about an archive. */
80 struct xcoff_archive_info
82 /* The archive described by this entry. */
85 /* The import path and import filename to use when referring to
86 this archive in the .loader section. */
90 /* True if the archive contains a dynamic object. */
91 unsigned int contains_shared_object_p : 1;
93 /* True if the previous field is valid. */
94 unsigned int know_contains_shared_object_p : 1;
97 struct xcoff_link_hash_table
99 struct bfd_link_hash_table root;
101 /* The .debug string hash table. We need to compute this while
102 reading the input files, so that we know how large the .debug
103 section will be before we assign section positions. */
104 struct bfd_strtab_hash *debug_strtab;
106 /* The .debug section we will use for the final output. */
107 asection *debug_section;
109 /* The .loader section we will use for the final output. */
110 asection *loader_section;
112 /* A count of non TOC relative relocs which will need to be
113 allocated in the .loader section. */
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr;
119 /* The .gl section we use to hold global linkage code. */
120 asection *linkage_section;
122 /* The .tc section we use to hold toc entries we build for global
124 asection *toc_section;
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection *descriptor_section;
130 /* The list of import files. */
131 struct xcoff_import_file *imports;
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align;
136 /* Whether the .text section must be read-only. */
139 /* Whether -brtl was specified. */
142 /* Whether garbage collection was done. */
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
148 struct xcoff_link_size_list *next;
149 struct xcoff_link_hash_entry *h;
154 /* Information about archives. */
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
161 /* Information that we pass around while doing the final link step. */
163 struct xcoff_final_link_info
165 /* General link information. */
166 struct bfd_link_info *info;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash *strtab;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info *section_info;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file;
178 /* Symbol index of TOC symbol. */
180 /* Start of .loader symbols. */
182 /* Next .loader reloc to swap out. */
184 /* File position of start of line numbers. */
185 file_ptr line_filepos;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment *internal_syms;
188 /* Buffer large enough to hold output indices of symbols of any
191 /* Buffer large enough to hold output symbols for any input file. */
193 /* Buffer large enough to hold external line numbers for any input
196 /* Buffer large enough to hold any input section. */
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte *external_relocs;
202 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
206 /* Routines to read XCOFF dynamic information. This don't really
207 belong here, but we already have the ldsym manipulation routines
210 /* Read the contents of a section. */
213 xcoff_get_section_contents (bfd *abfd, asection *sec)
215 if (coff_section_data (abfd, sec) == NULL)
217 bfd_size_type amt = sizeof (struct coff_section_tdata);
219 sec->used_by_bfd = bfd_zalloc (abfd, amt);
220 if (sec->used_by_bfd == NULL)
224 if (coff_section_data (abfd, sec)->contents == NULL)
228 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
230 if (contents != NULL)
234 coff_section_data (abfd, sec)->contents = contents;
240 /* Get the size required to hold the dynamic symbols. */
243 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
247 struct internal_ldhdr ldhdr;
249 if ((abfd->flags & DYNAMIC) == 0)
251 bfd_set_error (bfd_error_invalid_operation);
255 lsec = bfd_get_section_by_name (abfd, ".loader");
258 bfd_set_error (bfd_error_no_symbols);
262 if (! xcoff_get_section_contents (abfd, lsec))
264 contents = coff_section_data (abfd, lsec)->contents;
266 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
268 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
271 /* Get the dynamic symbols. */
274 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
278 struct internal_ldhdr ldhdr;
280 bfd_byte *elsym, *elsymend;
281 coff_symbol_type *symbuf;
283 if ((abfd->flags & DYNAMIC) == 0)
285 bfd_set_error (bfd_error_invalid_operation);
289 lsec = bfd_get_section_by_name (abfd, ".loader");
292 bfd_set_error (bfd_error_no_symbols);
296 if (! xcoff_get_section_contents (abfd, lsec))
298 contents = coff_section_data (abfd, lsec)->contents;
300 coff_section_data (abfd, lsec)->keep_contents = TRUE;
302 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
304 strings = (char *) contents + ldhdr.l_stoff;
306 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
310 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
312 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
313 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
315 struct internal_ldsym ldsym;
317 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
319 symbuf->symbol.the_bfd = abfd;
321 if (ldsym._l._l_l._l_zeroes == 0)
322 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
327 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
330 memcpy (c, ldsym._l._l_name, SYMNMLEN);
332 symbuf->symbol.name = c;
335 if (ldsym.l_smclas == XMC_XO)
336 symbuf->symbol.section = bfd_abs_section_ptr;
338 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
340 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
342 symbuf->symbol.flags = BSF_NO_FLAGS;
343 if ((ldsym.l_smtype & L_EXPORT) != 0)
345 if ((ldsym.l_smtype & L_WEAK) != 0)
346 symbuf->symbol.flags |= BSF_WEAK;
348 symbuf->symbol.flags |= BSF_GLOBAL;
351 /* FIXME: We have no way to record the other information stored
352 with the loader symbol. */
353 *psyms = (asymbol *) symbuf;
358 return ldhdr.l_nsyms;
361 /* Get the size required to hold the dynamic relocs. */
364 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
368 struct internal_ldhdr ldhdr;
370 if ((abfd->flags & DYNAMIC) == 0)
372 bfd_set_error (bfd_error_invalid_operation);
376 lsec = bfd_get_section_by_name (abfd, ".loader");
379 bfd_set_error (bfd_error_no_symbols);
383 if (! xcoff_get_section_contents (abfd, lsec))
385 contents = coff_section_data (abfd, lsec)->contents;
387 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
389 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
392 /* Get the dynamic relocs. */
395 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
401 struct internal_ldhdr ldhdr;
403 bfd_byte *elrel, *elrelend;
405 if ((abfd->flags & DYNAMIC) == 0)
407 bfd_set_error (bfd_error_invalid_operation);
411 lsec = bfd_get_section_by_name (abfd, ".loader");
414 bfd_set_error (bfd_error_no_symbols);
418 if (! xcoff_get_section_contents (abfd, lsec))
420 contents = coff_section_data (abfd, lsec)->contents;
422 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
424 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
428 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
430 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
431 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
434 struct internal_ldrel ldrel;
436 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
438 if (ldrel.l_symndx >= 3)
439 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
445 switch (ldrel.l_symndx)
461 sec = bfd_get_section_by_name (abfd, name);
464 bfd_set_error (bfd_error_bad_value);
468 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
471 relbuf->address = ldrel.l_vaddr;
474 /* Most dynamic relocs have the same type. FIXME: This is only
475 correct if ldrel.l_rtype == 0. In other cases, we should use
476 a different howto. */
477 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
479 /* FIXME: We have no way to record the l_rsecnm field. */
486 return ldhdr.l_nreloc;
489 /* Hash functions for xcoff_link_hash_table's archive_info. */
492 xcoff_archive_info_hash (const void *data)
494 const struct xcoff_archive_info *info;
496 info = (const struct xcoff_archive_info *) data;
497 return htab_hash_pointer (info->archive);
501 xcoff_archive_info_eq (const void *data1, const void *data2)
503 const struct xcoff_archive_info *info1;
504 const struct xcoff_archive_info *info2;
506 info1 = (const struct xcoff_archive_info *) data1;
507 info2 = (const struct xcoff_archive_info *) data2;
508 return info1->archive == info2->archive;
511 /* Return information about archive ARCHIVE. Return NULL on error. */
513 static struct xcoff_archive_info *
514 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
516 struct xcoff_link_hash_table *htab;
517 struct xcoff_archive_info *entryp, entry;
520 htab = xcoff_hash_table (info);
521 entry.archive = archive;
522 slot = htab_find_slot (htab->archive_info, &entry, INSERT);
529 entryp = bfd_zalloc (archive, sizeof (entry));
533 entryp->archive = archive;
539 /* Routine to create an entry in an XCOFF link hash table. */
541 static struct bfd_hash_entry *
542 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
543 struct bfd_hash_table *table,
546 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
548 /* Allocate the structure if it has not already been allocated by a
551 ret = bfd_hash_allocate (table, sizeof (* ret));
555 /* Call the allocation method of the superclass. */
556 ret = ((struct xcoff_link_hash_entry *)
557 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
561 /* Set local fields. */
563 ret->toc_section = NULL;
564 ret->u.toc_indx = -1;
565 ret->descriptor = NULL;
569 ret->smclas = XMC_UA;
572 return (struct bfd_hash_entry *) ret;
575 /* Create a XCOFF link hash table. */
577 struct bfd_link_hash_table *
578 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
580 struct xcoff_link_hash_table *ret;
581 bfd_size_type amt = sizeof (* ret);
583 ret = bfd_malloc (amt);
586 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
587 sizeof (struct xcoff_link_hash_entry)))
593 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
594 ret->debug_section = NULL;
595 ret->loader_section = NULL;
596 ret->ldrel_count = 0;
597 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
598 ret->linkage_section = NULL;
599 ret->toc_section = NULL;
600 ret->descriptor_section = NULL;
605 ret->archive_info = htab_create (37, xcoff_archive_info_hash,
606 xcoff_archive_info_eq, NULL);
607 memset (ret->special_sections, 0, sizeof ret->special_sections);
609 /* The linker will always generate a full a.out header. We need to
610 record that fact now, before the sizeof_headers routine could be
612 xcoff_data (abfd)->full_aouthdr = TRUE;
617 /* Free a XCOFF link hash table. */
620 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
622 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
624 _bfd_stringtab_free (ret->debug_strtab);
625 bfd_hash_table_free (&ret->root.table);
629 /* Read internal relocs for an XCOFF csect. This is a wrapper around
630 _bfd_coff_read_internal_relocs which tries to take advantage of any
631 relocs which may have been cached for the enclosing section. */
633 static struct internal_reloc *
634 xcoff_read_internal_relocs (bfd *abfd,
637 bfd_byte *external_relocs,
638 bfd_boolean require_internal,
639 struct internal_reloc *internal_relocs)
641 if (coff_section_data (abfd, sec) != NULL
642 && coff_section_data (abfd, sec)->relocs == NULL
643 && xcoff_section_data (abfd, sec) != NULL)
647 enclosing = xcoff_section_data (abfd, sec)->enclosing;
649 if (enclosing != NULL
650 && (coff_section_data (abfd, enclosing) == NULL
651 || coff_section_data (abfd, enclosing)->relocs == NULL)
653 && enclosing->reloc_count > 0)
655 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
656 external_relocs, FALSE, NULL)
661 if (enclosing != NULL
662 && coff_section_data (abfd, enclosing) != NULL
663 && coff_section_data (abfd, enclosing)->relocs != NULL)
667 off = ((sec->rel_filepos - enclosing->rel_filepos)
668 / bfd_coff_relsz (abfd));
670 if (! require_internal)
671 return coff_section_data (abfd, enclosing)->relocs + off;
672 memcpy (internal_relocs,
673 coff_section_data (abfd, enclosing)->relocs + off,
674 sec->reloc_count * sizeof (struct internal_reloc));
675 return internal_relocs;
679 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
680 require_internal, internal_relocs);
683 /* Split FILENAME into an import path and an import filename,
684 storing them in *IMPPATH and *IMPFILE respectively. */
687 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
688 const char **imppath, const char **impfile)
694 base = lbasename (filename);
695 length = base - filename;
697 /* The filename has no directory component, so use an empty path. */
699 else if (length == 1)
700 /* The filename is in the root directory. */
704 /* Extract the (non-empty) directory part. Note that we don't
705 need to strip duplicate directory separators from any part
706 of the string; the native linker doesn't do that either. */
707 path = bfd_alloc (abfd, length);
710 memcpy (path, filename, length - 1);
711 path[length - 1] = 0;
718 /* Set ARCHIVE's import path as though its filename had been given
722 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
723 bfd *archive, const char *filename)
725 struct xcoff_archive_info *archive_info;
727 archive_info = xcoff_get_archive_info (info, archive);
728 return (archive_info != NULL
729 && bfd_xcoff_split_import_path (archive, filename,
730 &archive_info->imppath,
731 &archive_info->impfile));
734 /* H is an imported symbol. Set the import module's path, file and member
735 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
736 no specific import module is specified. */
739 xcoff_set_import_path (struct bfd_link_info *info,
740 struct xcoff_link_hash_entry *h,
741 const char *imppath, const char *impfile,
742 const char *impmember)
745 struct xcoff_import_file **pp;
747 /* We overload the ldindx field to hold the l_ifile value for this
749 BFD_ASSERT (h->ldsym == NULL);
750 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
755 /* We start c at 1 because the first entry in the import list is
756 reserved for the library search path. */
757 for (pp = &xcoff_hash_table (info)->imports, c = 1;
759 pp = &(*pp)->next, ++c)
761 if (filename_cmp ((*pp)->path, imppath) == 0
762 && filename_cmp ((*pp)->file, impfile) == 0
763 && filename_cmp ((*pp)->member, impmember) == 0)
769 struct xcoff_import_file *n;
770 bfd_size_type amt = sizeof (* n);
772 n = bfd_alloc (info->output_bfd, amt);
778 n->member = impmember;
786 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
787 Return true if LDSYM defines H. */
790 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
791 struct internal_ldsym *ldsym)
793 /* If we didn't know about H before processing LDSYM, LDSYM
794 definitely defines H. */
795 if (h->root.type == bfd_link_hash_new)
798 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
799 dynamic symbol, LDSYM trumps the current definition of H. */
800 if ((ldsym->l_smtype & L_WEAK) == 0
801 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
802 && (h->flags & XCOFF_DEF_REGULAR) == 0
803 && (h->root.type == bfd_link_hash_defweak
804 || h->root.type == bfd_link_hash_undefweak))
807 /* If H is currently undefined, LDSYM defines it. */
808 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
809 && (h->root.type == bfd_link_hash_undefined
810 || h->root.type == bfd_link_hash_undefweak))
816 /* This function is used to add symbols from a dynamic object to the
817 global symbol table. */
820 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
824 struct internal_ldhdr ldhdr;
826 bfd_byte *elsym, *elsymend;
827 struct xcoff_import_file *n;
829 struct xcoff_import_file **pp;
831 /* We can only handle a dynamic object if we are generating an XCOFF
833 if (info->output_bfd->xvec != abfd->xvec)
835 (*_bfd_error_handler)
836 (_("%s: XCOFF shared object when not producing XCOFF output"),
837 bfd_get_filename (abfd));
838 bfd_set_error (bfd_error_invalid_operation);
842 /* The symbols we use from a dynamic object are not the symbols in
843 the normal symbol table, but, rather, the symbols in the export
844 table. If there is a global symbol in a dynamic object which is
845 not in the export table, the loader will not be able to find it,
846 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
847 libc.a has symbols in the export table which are not in the
850 /* Read in the .loader section. FIXME: We should really use the
851 o_snloader field in the a.out header, rather than grabbing the
853 lsec = bfd_get_section_by_name (abfd, ".loader");
856 (*_bfd_error_handler)
857 (_("%s: dynamic object with no .loader section"),
858 bfd_get_filename (abfd));
859 bfd_set_error (bfd_error_no_symbols);
863 if (! xcoff_get_section_contents (abfd, lsec))
865 contents = coff_section_data (abfd, lsec)->contents;
867 /* Remove the sections from this object, so that they do not get
868 included in the link. */
869 bfd_section_list_clear (abfd);
871 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
873 strings = (char *) contents + ldhdr.l_stoff;
875 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
877 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
879 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
881 struct internal_ldsym ldsym;
882 char nambuf[SYMNMLEN + 1];
884 struct xcoff_link_hash_entry *h;
886 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
888 /* We are only interested in exported symbols. */
889 if ((ldsym.l_smtype & L_EXPORT) == 0)
892 if (ldsym._l._l_l._l_zeroes == 0)
893 name = strings + ldsym._l._l_l._l_offset;
896 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
897 nambuf[SYMNMLEN] = '\0';
901 /* Normally we could not call xcoff_link_hash_lookup in an add
902 symbols routine, since we might not be using an XCOFF hash
903 table. However, we verified above that we are using an XCOFF
906 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
911 if (!xcoff_dynamic_definition_p (h, &ldsym))
914 h->flags |= XCOFF_DEF_DYNAMIC;
915 h->smclas = ldsym.l_smclas;
916 if (h->smclas == XMC_XO)
918 /* This symbol has an absolute value. */
919 if ((ldsym.l_smtype & L_WEAK) != 0)
920 h->root.type = bfd_link_hash_defweak;
922 h->root.type = bfd_link_hash_defined;
923 h->root.u.def.section = bfd_abs_section_ptr;
924 h->root.u.def.value = ldsym.l_value;
928 /* Otherwise, we don't bother to actually define the symbol,
929 since we don't have a section to put it in anyhow.
930 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
931 should be imported from the symbol's undef.abfd. */
932 if ((ldsym.l_smtype & L_WEAK) != 0)
933 h->root.type = bfd_link_hash_undefweak;
935 h->root.type = bfd_link_hash_undefined;
936 h->root.u.undef.abfd = abfd;
939 /* If this symbol defines a function descriptor, then it
940 implicitly defines the function code as well. */
941 if (h->smclas == XMC_DS
942 || (h->smclas == XMC_XO && name[0] != '.'))
943 h->flags |= XCOFF_DESCRIPTOR;
944 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
946 struct xcoff_link_hash_entry *hds;
953 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
957 strcpy (dsnm + 1, name);
958 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
968 if (xcoff_dynamic_definition_p (hds, &ldsym))
970 hds->root.type = h->root.type;
971 hds->flags |= XCOFF_DEF_DYNAMIC;
972 if (h->smclas == XMC_XO)
974 /* An absolute symbol appears to actually define code, not a
975 function descriptor. This is how some math functions are
976 implemented on AIX 4.1. */
977 hds->smclas = XMC_XO;
978 hds->root.u.def.section = bfd_abs_section_ptr;
979 hds->root.u.def.value = ldsym.l_value;
983 hds->smclas = XMC_PR;
984 hds->root.u.undef.abfd = abfd;
985 /* We do not want to add this to the undefined
992 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
994 free (coff_section_data (abfd, lsec)->contents);
995 coff_section_data (abfd, lsec)->contents = NULL;
998 /* Record this file in the import files. */
999 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
1004 if (abfd->my_archive == NULL)
1006 if (!bfd_xcoff_split_import_path (abfd, abfd->filename,
1007 &n->path, &n->file))
1013 struct xcoff_archive_info *archive_info;
1015 archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1016 if (!archive_info->impfile)
1018 if (!bfd_xcoff_split_import_path (archive_info->archive,
1019 archive_info->archive->filename,
1020 &archive_info->imppath,
1021 &archive_info->impfile))
1024 n->path = archive_info->imppath;
1025 n->file = archive_info->impfile;
1026 n->member = bfd_get_filename (abfd);
1029 /* We start c at 1 because the first import file number is reserved
1031 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1033 pp = &(*pp)->next, ++c)
1037 xcoff_data (abfd)->import_file_id = c;
1042 /* xcoff_link_create_extra_sections
1044 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1047 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1049 bfd_boolean return_value = FALSE;
1051 if (info->output_bfd->xvec == abfd->xvec)
1053 /* We need to build a .loader section, so we do it here. This
1054 won't work if we're producing an XCOFF output file with no
1055 XCOFF input files. FIXME. */
1057 if (!info->relocatable
1058 && xcoff_hash_table (info)->loader_section == NULL)
1061 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1063 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1067 xcoff_hash_table (info)->loader_section = lsec;
1070 /* Likewise for the linkage section. */
1071 if (xcoff_hash_table (info)->linkage_section == NULL)
1074 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1077 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1081 xcoff_hash_table (info)->linkage_section = lsec;
1082 lsec->alignment_power = 2;
1085 /* Likewise for the TOC section. */
1086 if (xcoff_hash_table (info)->toc_section == NULL)
1089 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1092 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1096 xcoff_hash_table (info)->toc_section = tsec;
1097 tsec->alignment_power = 2;
1100 /* Likewise for the descriptor section. */
1101 if (xcoff_hash_table (info)->descriptor_section == NULL)
1104 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1107 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1111 xcoff_hash_table (info)->descriptor_section = dsec;
1112 dsec->alignment_power = 2;
1115 /* Likewise for the .debug section. */
1116 if (xcoff_hash_table (info)->debug_section == NULL
1117 && info->strip != strip_all)
1120 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1122 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1126 xcoff_hash_table (info)->debug_section = dsec;
1130 return_value = TRUE;
1134 return return_value;
1137 /* Returns the index of reloc in RELOCS with the least address greater
1138 than or equal to ADDRESS. The relocs are sorted by address. */
1140 static bfd_size_type
1141 xcoff_find_reloc (struct internal_reloc *relocs,
1142 bfd_size_type count,
1145 bfd_size_type min, max, this;
1149 if (count == 1 && relocs[0].r_vaddr < address)
1158 /* Do a binary search over (min,max]. */
1159 while (min + 1 < max)
1163 this = (max + min) / 2;
1164 raddr = relocs[this].r_vaddr;
1165 if (raddr > address)
1167 else if (raddr < address)
1176 if (relocs[min].r_vaddr < address)
1180 && relocs[min - 1].r_vaddr == address)
1186 /* Add all the symbols from an object file to the hash table.
1188 XCOFF is a weird format. A normal XCOFF .o files will have three
1189 COFF sections--.text, .data, and .bss--but each COFF section will
1190 contain many csects. These csects are described in the symbol
1191 table. From the linker's point of view, each csect must be
1192 considered a section in its own right. For example, a TOC entry is
1193 handled as a small XMC_TC csect. The linker must be able to merge
1194 different TOC entries together, which means that it must be able to
1195 extract the XMC_TC csects from the .data section of the input .o
1198 From the point of view of our linker, this is, of course, a hideous
1199 nightmare. We cope by actually creating sections for each csect,
1200 and discarding the original sections. We then have to handle the
1201 relocation entries carefully, since the only way to tell which
1202 csect they belong to is to examine the address. */
1205 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1207 unsigned int n_tmask;
1208 unsigned int n_btshft;
1209 bfd_boolean default_copy;
1210 bfd_size_type symcount;
1211 struct xcoff_link_hash_entry **sym_hash;
1212 asection **csect_cache;
1213 unsigned int *lineno_counts;
1214 bfd_size_type linesz;
1216 asection *last_real;
1217 bfd_boolean keep_syms;
1219 unsigned int csect_index;
1220 asection *first_csect;
1221 bfd_size_type symesz;
1224 struct reloc_info_struct
1226 struct internal_reloc *relocs;
1229 } *reloc_info = NULL;
1232 keep_syms = obj_coff_keep_syms (abfd);
1234 if ((abfd->flags & DYNAMIC) != 0
1235 && ! info->static_link)
1237 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1241 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1242 if (! xcoff_link_create_extra_sections (abfd, info))
1245 if ((abfd->flags & DYNAMIC) != 0
1246 && ! info->static_link)
1249 n_tmask = coff_data (abfd)->local_n_tmask;
1250 n_btshft = coff_data (abfd)->local_n_btshft;
1252 /* Define macros so that ISFCN, et. al., macros work correctly. */
1253 #define N_TMASK n_tmask
1254 #define N_BTSHFT n_btshft
1256 if (info->keep_memory)
1257 default_copy = FALSE;
1259 default_copy = TRUE;
1261 symcount = obj_raw_syment_count (abfd);
1263 /* We keep a list of the linker hash table entries that correspond
1264 to each external symbol. */
1265 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1266 sym_hash = bfd_zalloc (abfd, amt);
1267 if (sym_hash == NULL && symcount != 0)
1269 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1271 /* Because of the weird stuff we are doing with XCOFF csects, we can
1272 not easily determine which section a symbol is in, so we store
1273 the information in the tdata for the input file. */
1274 amt = symcount * sizeof (asection *);
1275 csect_cache = bfd_zalloc (abfd, amt);
1276 if (csect_cache == NULL && symcount != 0)
1278 xcoff_data (abfd)->csects = csect_cache;
1280 /* We garbage-collect line-number information on a symbol-by-symbol
1281 basis, so we need to have quick access to the number of entries
1283 amt = symcount * sizeof (unsigned int);
1284 lineno_counts = bfd_zalloc (abfd, amt);
1285 if (lineno_counts == NULL && symcount != 0)
1287 xcoff_data (abfd)->lineno_counts = lineno_counts;
1289 /* While splitting sections into csects, we need to assign the
1290 relocs correctly. The relocs and the csects must both be in
1291 order by VMA within a given section, so we handle this by
1292 scanning along the relocs as we process the csects. We index
1293 into reloc_info using the section target_index. */
1294 amt = abfd->section_count + 1;
1295 amt *= sizeof (struct reloc_info_struct);
1296 reloc_info = bfd_zmalloc (amt);
1297 if (reloc_info == NULL)
1300 /* Read in the relocs and line numbers for each section. */
1301 linesz = bfd_coff_linesz (abfd);
1303 for (o = abfd->sections; o != NULL; o = o->next)
1307 if ((o->flags & SEC_RELOC) != 0)
1309 reloc_info[o->target_index].relocs =
1310 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1311 amt = o->reloc_count;
1312 amt *= sizeof (asection *);
1313 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1314 if (reloc_info[o->target_index].csects == NULL)
1318 if ((info->strip == strip_none || info->strip == strip_some)
1319 && o->lineno_count > 0)
1323 amt = linesz * o->lineno_count;
1324 linenos = bfd_malloc (amt);
1325 if (linenos == NULL)
1327 reloc_info[o->target_index].linenos = linenos;
1328 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1329 || bfd_bread (linenos, amt, abfd) != amt)
1334 /* Don't let the linker relocation routines discard the symbols. */
1335 obj_coff_keep_syms (abfd) = TRUE;
1341 symesz = bfd_coff_symesz (abfd);
1342 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1343 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1344 esym_end = esym + symcount * symesz;
1346 while (esym < esym_end)
1348 struct internal_syment sym;
1349 union internal_auxent aux;
1351 char buf[SYMNMLEN + 1];
1355 struct xcoff_link_hash_entry *set_toc;
1357 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1359 /* In this pass we are only interested in symbols with csect
1361 if (!CSECT_SYM_P (sym.n_sclass))
1364 Normally csect is a .pr, .rw etc. created in the loop
1365 If C_FILE or first time, handle special
1367 Advance esym, sym_hash, csect_hash ptrs. */
1368 if (sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1371 *csect_cache = csect;
1372 else if (first_csect == NULL
1373 || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1374 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1376 *csect_cache = NULL;
1377 esym += (sym.n_numaux + 1) * symesz;
1378 sym_hash += sym.n_numaux + 1;
1379 csect_cache += sym.n_numaux + 1;
1380 lineno_counts += sym.n_numaux + 1;
1385 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1390 /* If this symbol has line number information attached to it,
1391 and we're not stripping it, count the number of entries and
1392 add them to the count for this csect. In the final link pass
1393 we are going to attach line number information by symbol,
1394 rather than by section, in order to more easily handle
1395 garbage collection. */
1396 if ((info->strip == strip_none || info->strip == strip_some)
1399 && ISFCN (sym.n_type))
1401 union internal_auxent auxlin;
1403 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1404 sym.n_type, sym.n_sclass,
1405 0, sym.n_numaux, (void *) &auxlin);
1407 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1409 asection *enclosing;
1410 bfd_signed_vma linoff;
1412 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1413 if (enclosing == NULL)
1415 (*_bfd_error_handler)
1416 (_("%B: `%s' has line numbers but no enclosing section"),
1418 bfd_set_error (bfd_error_bad_value);
1421 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1422 - enclosing->line_filepos);
1423 /* Explicit cast to bfd_signed_vma for compiler. */
1424 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1426 struct internal_lineno lin;
1427 bfd_byte *linpstart;
1429 linpstart = (reloc_info[enclosing->target_index].linenos
1431 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1433 && ((bfd_size_type) lin.l_addr.l_symndx
1435 - (bfd_byte *) obj_coff_external_syms (abfd))
1438 bfd_byte *linpend, *linp;
1440 linpend = (reloc_info[enclosing->target_index].linenos
1441 + enclosing->lineno_count * linesz);
1442 for (linp = linpstart + linesz;
1446 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1448 if (lin.l_lnno == 0)
1451 *lineno_counts = (linp - linpstart) / linesz;
1452 /* The setting of line_filepos will only be
1453 useful if all the line number entries for a
1454 csect are contiguous; this only matters for
1456 if (csect->line_filepos == 0)
1457 csect->line_filepos =
1458 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1464 /* Pick up the csect auxiliary information. */
1465 if (sym.n_numaux == 0)
1467 (*_bfd_error_handler)
1468 (_("%B: class %d symbol `%s' has no aux entries"),
1469 abfd, sym.n_sclass, name);
1470 bfd_set_error (bfd_error_bad_value);
1474 bfd_coff_swap_aux_in (abfd,
1475 (void *) (esym + symesz * sym.n_numaux),
1476 sym.n_type, sym.n_sclass,
1477 sym.n_numaux - 1, sym.n_numaux,
1480 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1489 (*_bfd_error_handler)
1490 (_("%B: symbol `%s' has unrecognized csect type %d"),
1492 bfd_set_error (bfd_error_bad_value);
1496 /* This is an external reference. */
1497 if (sym.n_sclass == C_HIDEXT
1498 || sym.n_scnum != N_UNDEF
1499 || aux.x_csect.x_scnlen.l != 0)
1501 (*_bfd_error_handler)
1502 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1503 abfd, name, sym.n_sclass, sym.n_scnum,
1504 aux.x_csect.x_scnlen.l);
1505 bfd_set_error (bfd_error_bad_value);
1509 /* An XMC_XO external reference is actually a reference to
1510 an absolute location. */
1511 if (aux.x_csect.x_smclas != XMC_XO)
1512 section = bfd_und_section_ptr;
1515 section = bfd_abs_section_ptr;
1516 value = sym.n_value;
1522 csect_index = -(unsigned) 1;
1524 /* When we see a TOC anchor, we record the TOC value. */
1525 if (aux.x_csect.x_smclas == XMC_TC0)
1527 if (sym.n_sclass != C_HIDEXT
1528 || aux.x_csect.x_scnlen.l != 0)
1530 (*_bfd_error_handler)
1531 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1532 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1533 bfd_set_error (bfd_error_bad_value);
1536 xcoff_data (abfd)->toc = sym.n_value;
1539 /* We must merge TOC entries for the same symbol. We can
1540 merge two TOC entries if they are both C_HIDEXT, they
1541 both have the same name, they are both 4 or 8 bytes long, and
1542 they both have a relocation table entry for an external
1543 symbol with the same name. Unfortunately, this means
1544 that we must look through the relocations. Ick.
1546 Logic for 32 bit vs 64 bit.
1547 32 bit has a csect length of 4 for TOC
1548 64 bit has a csect length of 8 for TOC
1550 The conditions to get past the if-check are not that bad.
1551 They are what is used to create the TOC csects in the first
1553 if (aux.x_csect.x_smclas == XMC_TC
1554 && sym.n_sclass == C_HIDEXT
1555 && info->output_bfd->xvec == abfd->xvec
1556 && ((bfd_xcoff_is_xcoff32 (abfd)
1557 && aux.x_csect.x_scnlen.l == 4)
1558 || (bfd_xcoff_is_xcoff64 (abfd)
1559 && aux.x_csect.x_scnlen.l == 8)))
1561 asection *enclosing;
1562 struct internal_reloc *relocs;
1563 bfd_size_type relindx;
1564 struct internal_reloc *rel;
1566 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1567 if (enclosing == NULL)
1570 relocs = reloc_info[enclosing->target_index].relocs;
1571 amt = enclosing->reloc_count;
1572 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1573 rel = relocs + relindx;
1575 /* 32 bit R_POS r_size is 31
1576 64 bit R_POS r_size is 63 */
1577 if (relindx < enclosing->reloc_count
1578 && rel->r_vaddr == (bfd_vma) sym.n_value
1579 && rel->r_type == R_POS
1580 && ((bfd_xcoff_is_xcoff32 (abfd)
1581 && rel->r_size == 31)
1582 || (bfd_xcoff_is_xcoff64 (abfd)
1583 && rel->r_size == 63)))
1587 struct internal_syment relsym;
1589 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1590 + rel->r_symndx * symesz);
1591 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1592 if (EXTERN_SYM_P (relsym.n_sclass))
1594 const char *relname;
1595 char relbuf[SYMNMLEN + 1];
1597 struct xcoff_link_hash_entry *h;
1599 /* At this point we know that the TOC entry is
1600 for an externally visible symbol. */
1601 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1603 if (relname == NULL)
1606 /* We only merge TOC entries if the TC name is
1607 the same as the symbol name. This handles
1608 the normal case, but not common cases like
1609 SYM.P4 which gcc generates to store SYM + 4
1610 in the TOC. FIXME. */
1611 if (strcmp (name, relname) == 0)
1613 copy = (! info->keep_memory
1614 || relsym._n._n_n._n_zeroes != 0
1615 || relsym._n._n_n._n_offset == 0);
1616 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1617 relname, TRUE, copy,
1622 /* At this point h->root.type could be
1623 bfd_link_hash_new. That should be OK,
1624 since we know for sure that we will come
1625 across this symbol as we step through the
1628 /* We store h in *sym_hash for the
1629 convenience of the relocate_section
1633 if (h->toc_section != NULL)
1635 asection **rel_csects;
1637 /* We already have a TOC entry for this
1638 symbol, so we can just ignore this
1641 reloc_info[enclosing->target_index].csects;
1642 rel_csects[relindx] = bfd_und_section_ptr;
1646 /* We are about to create a TOC entry for
1655 asection *enclosing;
1657 /* We need to create a new section. We get the name from
1658 the csect storage mapping class, so that the linker can
1659 accumulate similar csects together. */
1661 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1665 /* The enclosing section is the main section : .data, .text
1666 or .bss that the csect is coming from. */
1667 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1668 if (enclosing == NULL)
1671 if (! bfd_is_abs_section (enclosing)
1672 && ((bfd_vma) sym.n_value < enclosing->vma
1673 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1674 > enclosing->vma + enclosing->size)))
1676 (*_bfd_error_handler)
1677 (_("%B: csect `%s' not in enclosing section"),
1679 bfd_set_error (bfd_error_bad_value);
1682 csect->vma = sym.n_value;
1683 csect->filepos = (enclosing->filepos
1686 csect->size = aux.x_csect.x_scnlen.l;
1687 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1688 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1690 /* Record the enclosing section in the tdata for this new
1692 amt = sizeof (struct coff_section_tdata);
1693 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1694 if (csect->used_by_bfd == NULL)
1696 amt = sizeof (struct xcoff_section_tdata);
1697 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1698 if (coff_section_data (abfd, csect)->tdata == NULL)
1700 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1701 xcoff_section_data (abfd, csect)->lineno_count =
1702 enclosing->lineno_count;
1704 if (enclosing->owner == abfd)
1706 struct internal_reloc *relocs;
1707 bfd_size_type relindx;
1708 struct internal_reloc *rel;
1709 asection **rel_csect;
1711 relocs = reloc_info[enclosing->target_index].relocs;
1712 amt = enclosing->reloc_count;
1713 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1715 rel = relocs + relindx;
1716 rel_csect = (reloc_info[enclosing->target_index].csects
1719 csect->rel_filepos = (enclosing->rel_filepos
1720 + relindx * bfd_coff_relsz (abfd));
1721 while (relindx < enclosing->reloc_count
1722 && *rel_csect == NULL
1723 && rel->r_vaddr < csect->vma + csect->size)
1727 csect->flags |= SEC_RELOC;
1728 ++csect->reloc_count;
1735 /* There are a number of other fields and section flags
1736 which we do not bother to set. */
1738 csect_index = ((esym
1739 - (bfd_byte *) obj_coff_external_syms (abfd))
1742 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1744 if (first_csect == NULL)
1745 first_csect = csect;
1747 /* If this symbol is external, we treat it as starting at the
1748 beginning of the newly created section. */
1749 if (EXTERN_SYM_P (sym.n_sclass))
1755 /* If this is a TOC section for a symbol, record it. */
1756 if (set_toc != NULL)
1757 set_toc->toc_section = csect;
1762 /* This is a label definition. The x_scnlen field is the
1763 symbol index of the csect. Usually the XTY_LD symbol will
1764 follow its appropriate XTY_SD symbol. The .set pseudo op can
1765 cause the XTY_LD to not follow the XTY_SD symbol. */
1770 if (aux.x_csect.x_scnlen.l < 0
1771 || (aux.x_csect.x_scnlen.l
1772 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1776 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1778 || (section->flags & SEC_HAS_CONTENTS) == 0)
1783 (*_bfd_error_handler)
1784 (_("%B: misplaced XTY_LD `%s'"),
1786 bfd_set_error (bfd_error_bad_value);
1790 value = sym.n_value - csect->vma;
1795 /* This is an unitialized csect. We could base the name on
1796 the storage mapping class, but we don't bother except for
1797 an XMC_TD symbol. If this csect is externally visible,
1798 it is a common symbol. We put XMC_TD symbols in sections
1799 named .tocbss, and rely on the linker script to put that
1802 if (aux.x_csect.x_smclas == XMC_TD)
1804 /* The linker script puts the .td section in the data
1805 section after the .tc section. */
1806 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1810 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1815 csect->vma = sym.n_value;
1816 csect->size = aux.x_csect.x_scnlen.l;
1817 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1818 /* There are a number of other fields and section flags
1819 which we do not bother to set. */
1821 csect_index = ((esym
1822 - (bfd_byte *) obj_coff_external_syms (abfd))
1825 amt = sizeof (struct coff_section_tdata);
1826 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1827 if (csect->used_by_bfd == NULL)
1829 amt = sizeof (struct xcoff_section_tdata);
1830 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1831 if (coff_section_data (abfd, csect)->tdata == NULL)
1833 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1835 if (first_csect == NULL)
1836 first_csect = csect;
1838 if (EXTERN_SYM_P (sym.n_sclass))
1840 csect->flags |= SEC_IS_COMMON;
1843 value = aux.x_csect.x_scnlen.l;
1849 /* Check for magic symbol names. */
1850 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1851 && aux.x_csect.x_smclas != XMC_TC
1852 && aux.x_csect.x_smclas != XMC_TD)
1858 if (strcmp (name, "_text") == 0)
1859 i = XCOFF_SPECIAL_SECTION_TEXT;
1860 else if (strcmp (name, "_etext") == 0)
1861 i = XCOFF_SPECIAL_SECTION_ETEXT;
1862 else if (strcmp (name, "_data") == 0)
1863 i = XCOFF_SPECIAL_SECTION_DATA;
1864 else if (strcmp (name, "_edata") == 0)
1865 i = XCOFF_SPECIAL_SECTION_EDATA;
1866 else if (strcmp (name, "_end") == 0)
1867 i = XCOFF_SPECIAL_SECTION_END;
1869 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1870 i = XCOFF_SPECIAL_SECTION_END2;
1873 xcoff_hash_table (info)->special_sections[i] = csect;
1876 /* Now we have enough information to add the symbol to the
1877 linker hash table. */
1879 if (EXTERN_SYM_P (sym.n_sclass))
1884 BFD_ASSERT (section != NULL);
1886 /* We must copy the name into memory if we got it from the
1887 syment itself, rather than the string table. */
1888 copy = default_copy;
1889 if (sym._n._n_n._n_zeroes != 0
1890 || sym._n._n_n._n_offset == 0)
1893 /* Ignore global linkage code when linking statically. */
1894 if (info->static_link
1895 && (smtyp == XTY_SD || smtyp == XTY_LD)
1896 && aux.x_csect.x_smclas == XMC_GL)
1898 section = bfd_und_section_ptr;
1902 /* The AIX linker appears to only detect multiple symbol
1903 definitions when there is a reference to the symbol. If
1904 a symbol is defined multiple times, and the only
1905 references are from the same object file, the AIX linker
1906 appears to permit it. It does not merge the different
1907 definitions, but handles them independently. On the
1908 other hand, if there is a reference, the linker reports
1911 This matters because the AIX <net/net_globals.h> header
1912 file actually defines an initialized array, so we have to
1913 actually permit that to work.
1915 Just to make matters even more confusing, the AIX linker
1916 appears to permit multiple symbol definitions whenever
1917 the second definition is in an archive rather than an
1918 object file. This may be a consequence of the manner in
1919 which it handles archives: I think it may load the entire
1920 archive in as separate csects, and then let garbage
1921 collection discard symbols.
1923 We also have to handle the case of statically linking a
1924 shared object, which will cause symbol redefinitions,
1925 although this is an easier case to detect. */
1926 else if (info->output_bfd->xvec == abfd->xvec)
1928 if (! bfd_is_und_section (section))
1929 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1930 name, TRUE, copy, FALSE);
1932 /* Make a copy of the symbol name to prevent problems with
1934 *sym_hash = ((struct xcoff_link_hash_entry *)
1935 bfd_wrapped_link_hash_lookup (abfd, info, name,
1936 TRUE, TRUE, FALSE));
1938 if (*sym_hash == NULL)
1940 if (((*sym_hash)->root.type == bfd_link_hash_defined
1941 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1942 && ! bfd_is_und_section (section)
1943 && ! bfd_is_com_section (section))
1945 /* This is a second definition of a defined symbol. */
1946 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
1947 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
1949 /* The existing symbol is from a shared library.
1951 (*sym_hash)->root.type = bfd_link_hash_undefined;
1952 (*sym_hash)->root.u.undef.abfd =
1953 (*sym_hash)->root.u.def.section->owner;
1955 else if (abfd->my_archive != NULL)
1957 /* This is a redefinition in an object contained
1958 in an archive. Just ignore it. See the
1960 section = bfd_und_section_ptr;
1963 else if (sym.n_sclass == C_AIX_WEAKEXT
1964 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1966 /* At least one of the definitions is weak.
1967 Allow the normal rules to take effect. */
1969 else if ((*sym_hash)->root.u.undef.next != NULL
1970 || info->hash->undefs_tail == &(*sym_hash)->root)
1972 /* This symbol has been referenced. In this
1973 case, we just continue and permit the
1974 multiple definition error. See the comment
1975 above about the behaviour of the AIX linker. */
1977 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1979 /* The symbols are both csects of the same
1980 class. There is at least a chance that this
1981 is a semi-legitimate redefinition. */
1982 section = bfd_und_section_ptr;
1984 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1987 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1988 && (*sym_hash)->root.type == bfd_link_hash_defined
1989 && (bfd_is_und_section (section)
1990 || bfd_is_com_section (section)))
1992 /* This is a reference to a multiply defined symbol.
1993 Report the error now. See the comment above
1994 about the behaviour of the AIX linker. We could
1995 also do this with warning symbols, but I'm not
1996 sure the XCOFF linker is wholly prepared to
1997 handle them, and that would only be a warning,
1999 if (! ((*info->callbacks->multiple_definition)
2000 (info, &(*sym_hash)->root, NULL, NULL, (bfd_vma) 0)))
2002 /* Try not to give this error too many times. */
2003 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2007 /* _bfd_generic_link_add_one_symbol may call the linker to
2008 generate an error message, and the linker may try to read
2009 the symbol table to give a good error. Right now, the
2010 line numbers are in an inconsistent state, since they are
2011 counted both in the real sections and in the new csects.
2012 We need to leave the count in the real sections so that
2013 the linker can report the line number of the error
2014 correctly, so temporarily clobber the link to the csects
2015 so that the linker will not try to read the line numbers
2016 a second time from the csects. */
2017 BFD_ASSERT (last_real->next == first_csect);
2018 last_real->next = NULL;
2019 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2020 if (! (_bfd_generic_link_add_one_symbol
2021 (info, abfd, name, flags, section, value,
2023 (struct bfd_link_hash_entry **) sym_hash)))
2025 last_real->next = first_csect;
2027 if (smtyp == XTY_CM)
2029 if ((*sym_hash)->root.type != bfd_link_hash_common
2030 || (*sym_hash)->root.u.c.p->section != csect)
2031 /* We don't need the common csect we just created. */
2034 (*sym_hash)->root.u.c.p->alignment_power
2035 = csect->alignment_power;
2038 if (info->output_bfd->xvec == abfd->xvec)
2044 || section == bfd_und_section_ptr)
2045 flag = XCOFF_REF_REGULAR;
2047 flag = XCOFF_DEF_REGULAR;
2048 (*sym_hash)->flags |= flag;
2050 if ((*sym_hash)->smclas == XMC_UA
2051 || flag == XCOFF_DEF_REGULAR)
2052 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2056 if (smtyp == XTY_ER)
2057 *csect_cache = section;
2060 *csect_cache = csect;
2062 xcoff_section_data (abfd, csect)->last_symndx
2063 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2066 esym += (sym.n_numaux + 1) * symesz;
2067 sym_hash += sym.n_numaux + 1;
2068 csect_cache += sym.n_numaux + 1;
2069 lineno_counts += sym.n_numaux + 1;
2072 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2074 /* Make sure that we have seen all the relocs. */
2075 for (o = abfd->sections; o != first_csect; o = o->next)
2077 /* Debugging sections have no csects. */
2078 if (bfd_get_section_flags (abfd, o) & SEC_DEBUGGING)
2081 /* Reset the section size and the line number count, since the
2082 data is now attached to the csects. Don't reset the size of
2083 the .debug section, since we need to read it below in
2084 bfd_xcoff_size_dynamic_sections. */
2085 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2087 o->lineno_count = 0;
2089 if ((o->flags & SEC_RELOC) != 0)
2092 struct internal_reloc *rel;
2093 asection **rel_csect;
2095 rel = reloc_info[o->target_index].relocs;
2096 rel_csect = reloc_info[o->target_index].csects;
2098 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2100 if (*rel_csect == NULL)
2102 (*_bfd_error_handler)
2103 (_("%B: reloc %s:%d not in csect"),
2105 bfd_set_error (bfd_error_bad_value);
2109 /* We identify all function symbols that are the target
2110 of a relocation, so that we can create glue code for
2111 functions imported from dynamic objects. */
2112 if (info->output_bfd->xvec == abfd->xvec
2113 && *rel_csect != bfd_und_section_ptr
2114 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2116 struct xcoff_link_hash_entry *h;
2118 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2119 /* If the symbol name starts with a period, it is
2120 the code of a function. If the symbol is
2121 currently undefined, then add an undefined symbol
2122 for the function descriptor. This should do no
2123 harm, because any regular object that defines the
2124 function should also define the function
2125 descriptor. It helps, because it means that we
2126 will identify the function descriptor with a
2127 dynamic object if a dynamic object defines it. */
2128 if (h->root.root.string[0] == '.'
2129 && h->descriptor == NULL)
2131 struct xcoff_link_hash_entry *hds;
2132 struct bfd_link_hash_entry *bh;
2134 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2135 h->root.root.string + 1,
2139 if (hds->root.type == bfd_link_hash_new)
2142 if (! (_bfd_generic_link_add_one_symbol
2143 (info, abfd, hds->root.root.string,
2144 (flagword) 0, bfd_und_section_ptr,
2145 (bfd_vma) 0, NULL, FALSE,
2148 hds = (struct xcoff_link_hash_entry *) bh;
2150 hds->flags |= XCOFF_DESCRIPTOR;
2151 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2152 hds->descriptor = h;
2153 h->descriptor = hds;
2155 if (h->root.root.string[0] == '.')
2156 h->flags |= XCOFF_CALLED;
2160 free (reloc_info[o->target_index].csects);
2161 reloc_info[o->target_index].csects = NULL;
2163 /* Reset SEC_RELOC and the reloc_count, since the reloc
2164 information is now attached to the csects. */
2165 o->flags &=~ SEC_RELOC;
2168 /* If we are not keeping memory, free the reloc information. */
2169 if (! info->keep_memory
2170 && coff_section_data (abfd, o) != NULL
2171 && coff_section_data (abfd, o)->relocs != NULL
2172 && ! coff_section_data (abfd, o)->keep_relocs)
2174 free (coff_section_data (abfd, o)->relocs);
2175 coff_section_data (abfd, o)->relocs = NULL;
2179 /* Free up the line numbers. FIXME: We could cache these
2180 somewhere for the final link, to avoid reading them again. */
2181 if (reloc_info[o->target_index].linenos != NULL)
2183 free (reloc_info[o->target_index].linenos);
2184 reloc_info[o->target_index].linenos = NULL;
2190 obj_coff_keep_syms (abfd) = keep_syms;
2195 if (reloc_info != NULL)
2197 for (o = abfd->sections; o != NULL; o = o->next)
2199 if (reloc_info[o->target_index].csects != NULL)
2200 free (reloc_info[o->target_index].csects);
2201 if (reloc_info[o->target_index].linenos != NULL)
2202 free (reloc_info[o->target_index].linenos);
2206 obj_coff_keep_syms (abfd) = keep_syms;
2213 /* Add symbols from an XCOFF object file. */
2216 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2218 if (! _bfd_coff_get_external_symbols (abfd))
2220 if (! xcoff_link_add_symbols (abfd, info))
2222 if (! info->keep_memory)
2224 if (! _bfd_coff_free_symbols (abfd))
2230 /* Look through the loader symbols to see if this dynamic object
2231 should be included in the link. The native linker uses the loader
2232 symbols, not the normal symbol table, so we do too. */
2235 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2236 struct bfd_link_info *info,
2237 bfd_boolean *pneeded,
2242 struct internal_ldhdr ldhdr;
2243 const char *strings;
2244 bfd_byte *elsym, *elsymend;
2248 lsec = bfd_get_section_by_name (abfd, ".loader");
2250 /* There are no symbols, so don't try to include it. */
2253 if (! xcoff_get_section_contents (abfd, lsec))
2255 contents = coff_section_data (abfd, lsec)->contents;
2257 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2259 strings = (char *) contents + ldhdr.l_stoff;
2261 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2263 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2264 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2266 struct internal_ldsym ldsym;
2267 char nambuf[SYMNMLEN + 1];
2269 struct bfd_link_hash_entry *h;
2271 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2273 /* We are only interested in exported symbols. */
2274 if ((ldsym.l_smtype & L_EXPORT) == 0)
2277 if (ldsym._l._l_l._l_zeroes == 0)
2278 name = strings + ldsym._l._l_l._l_offset;
2281 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2282 nambuf[SYMNMLEN] = '\0';
2286 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2288 /* We are only interested in symbols that are currently
2289 undefined. At this point we know that we are using an XCOFF
2292 && h->type == bfd_link_hash_undefined
2293 && (((struct xcoff_link_hash_entry *) h)->flags
2294 & XCOFF_DEF_DYNAMIC) == 0)
2296 if (!(*info->callbacks
2297 ->add_archive_element) (info, abfd, name, subsbfd))
2304 /* We do not need this shared object. */
2305 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2307 free (coff_section_data (abfd, lsec)->contents);
2308 coff_section_data (abfd, lsec)->contents = NULL;
2314 /* Look through the symbols to see if this object file should be
2315 included in the link. */
2318 xcoff_link_check_ar_symbols (bfd *abfd,
2319 struct bfd_link_info *info,
2320 bfd_boolean *pneeded,
2323 bfd_size_type symesz;
2329 if ((abfd->flags & DYNAMIC) != 0
2330 && ! info->static_link
2331 && info->output_bfd->xvec == abfd->xvec)
2332 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
2334 symesz = bfd_coff_symesz (abfd);
2335 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2336 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2337 while (esym < esym_end)
2339 struct internal_syment sym;
2341 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2343 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2346 char buf[SYMNMLEN + 1];
2347 struct bfd_link_hash_entry *h;
2349 /* This symbol is externally visible, and is defined by this
2351 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2355 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2357 /* We are only interested in symbols that are currently
2358 undefined. If a symbol is currently known to be common,
2359 XCOFF linkers do not bring in an object file which
2360 defines it. We also don't bring in symbols to satisfy
2361 undefined references in shared objects. */
2363 && h->type == bfd_link_hash_undefined
2364 && (info->output_bfd->xvec != abfd->xvec
2365 || (((struct xcoff_link_hash_entry *) h)->flags
2366 & XCOFF_DEF_DYNAMIC) == 0))
2368 if (!(*info->callbacks
2369 ->add_archive_element) (info, abfd, name, subsbfd))
2376 esym += (sym.n_numaux + 1) * symesz;
2379 /* We do not need this object file. */
2383 /* Check a single archive element to see if we need to include it in
2384 the link. *PNEEDED is set according to whether this element is
2385 needed in the link or not. This is called via
2386 _bfd_generic_link_add_archive_symbols. */
2389 xcoff_link_check_archive_element (bfd *abfd,
2390 struct bfd_link_info *info,
2391 bfd_boolean *pneeded)
2393 bfd_boolean keep_syms_p;
2396 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2397 if (!_bfd_coff_get_external_symbols (abfd))
2401 if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2406 /* Potentially, the add_archive_element hook may have set a
2407 substitute BFD for us. */
2411 && !_bfd_coff_free_symbols (oldbfd))
2413 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2414 if (!_bfd_coff_get_external_symbols (abfd))
2417 if (!xcoff_link_add_symbols (abfd, info))
2419 if (info->keep_memory)
2425 if (!_bfd_coff_free_symbols (abfd))
2432 /* Given an XCOFF BFD, add symbols to the global hash table as
2436 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2438 switch (bfd_get_format (abfd))
2441 return xcoff_link_add_object_symbols (abfd, info);
2444 /* If the archive has a map, do the usual search. We then need
2445 to check the archive for dynamic objects, because they may not
2446 appear in the archive map even though they should, perhaps, be
2447 included. If the archive has no map, we just consider each object
2448 file in turn, since that apparently is what the AIX native linker
2450 if (bfd_has_map (abfd))
2452 if (! (_bfd_generic_link_add_archive_symbols
2453 (abfd, info, xcoff_link_check_archive_element)))
2460 member = bfd_openr_next_archived_file (abfd, NULL);
2461 while (member != NULL)
2463 if (bfd_check_format (member, bfd_object)
2464 && (info->output_bfd->xvec == member->xvec)
2465 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2469 if (! xcoff_link_check_archive_element (member, info,
2473 member->archive_pass = -1;
2475 member = bfd_openr_next_archived_file (abfd, member);
2482 bfd_set_error (bfd_error_wrong_format);
2488 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2489 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2490 struct bfd_link_hash_entry *harg)
2492 struct xcoff_link_hash_entry *h;
2494 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2497 h = (struct xcoff_link_hash_entry *) harg;
2498 h->flags |= XCOFF_DEF_REGULAR;
2502 /* If symbol H has not been interpreted as a function descriptor,
2503 see whether it should be. Set up its descriptor information if so. */
2506 xcoff_find_function (struct bfd_link_info *info,
2507 struct xcoff_link_hash_entry *h)
2509 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2510 && h->root.root.string[0] != '.')
2513 struct xcoff_link_hash_entry *hfn;
2516 amt = strlen (h->root.root.string) + 2;
2517 fnname = bfd_malloc (amt);
2521 strcpy (fnname + 1, h->root.root.string);
2522 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2523 fnname, FALSE, FALSE, TRUE);
2526 && hfn->smclas == XMC_PR
2527 && (hfn->root.type == bfd_link_hash_defined
2528 || hfn->root.type == bfd_link_hash_defweak))
2530 h->flags |= XCOFF_DESCRIPTOR;
2531 h->descriptor = hfn;
2532 hfn->descriptor = h;
2538 /* Return true if the given bfd contains at least one shared object. */
2541 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2544 struct xcoff_archive_info *archive_info;
2547 archive_info = xcoff_get_archive_info (info, archive);
2548 if (!archive_info->know_contains_shared_object_p)
2550 member = bfd_openr_next_archived_file (archive, NULL);
2551 while (member != NULL && (member->flags & DYNAMIC) == 0)
2552 member = bfd_openr_next_archived_file (archive, member);
2554 archive_info->contains_shared_object_p = (member != NULL);
2555 archive_info->know_contains_shared_object_p = 1;
2557 return archive_info->contains_shared_object_p;
2560 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2561 qualifies for export by -bexpall. */
2564 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2566 /* Exclude symbols beginning with '_'. */
2567 if (h->root.root.string[0] == '_')
2570 /* Exclude archive members that would otherwise be unreferenced. */
2571 if ((h->flags & XCOFF_MARK) == 0
2572 && (h->root.type == bfd_link_hash_defined
2573 || h->root.type == bfd_link_hash_defweak)
2574 && h->root.u.def.section->owner != NULL
2575 && h->root.u.def.section->owner->my_archive != NULL)
2581 /* Return true if symbol H qualifies for the forms of automatic export
2582 specified by AUTO_EXPORT_FLAGS. */
2585 xcoff_auto_export_p (struct bfd_link_info *info,
2586 struct xcoff_link_hash_entry *h,
2587 unsigned int auto_export_flags)
2589 /* Don't automatically export things that were explicitly exported. */
2590 if ((h->flags & XCOFF_EXPORT) != 0)
2593 /* Don't export things that we don't define. */
2594 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2597 /* Don't export functions; export their descriptors instead. */
2598 if (h->root.root.string[0] == '.')
2601 /* We don't export a symbol which is being defined by an object
2602 included from an archive which contains a shared object. The
2603 rationale is that if an archive contains both an unshared and
2604 a shared object, then there must be some reason that the
2605 unshared object is unshared, and we don't want to start
2606 providing a shared version of it. In particular, this solves
2607 a bug involving the _savefNN set of functions. gcc will call
2608 those functions without providing a slot to restore the TOC,
2609 so it is essential that these functions be linked in directly
2610 and not from a shared object, which means that a shared
2611 object which also happens to link them in must not export
2612 them. This is confusing, but I haven't been able to think of
2613 a different approach. Note that the symbols can, of course,
2614 be exported explicitly. */
2615 if (h->root.type == bfd_link_hash_defined
2616 || h->root.type == bfd_link_hash_defweak)
2620 owner = h->root.u.def.section->owner;
2622 && owner->my_archive != NULL
2623 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2627 /* Otherwise, all symbols are exported by -bexpfull. */
2628 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2631 /* Despite its name, -bexpall exports most but not all symbols. */
2632 if ((auto_export_flags & XCOFF_EXPALL) != 0
2633 && xcoff_covered_by_expall_p (h))
2639 /* Return true if relocation REL needs to be copied to the .loader section.
2640 If REL is against a global symbol, H is that symbol, otherwise it
2644 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2645 struct xcoff_link_hash_entry *h)
2647 if (!xcoff_hash_table (info)->loader_section)
2650 switch (rel->r_type)
2657 /* We should never need a .loader reloc for a TOC-relative reloc. */
2661 /* In this case, relocations against defined symbols can be resolved
2664 || h->root.type == bfd_link_hash_defined
2665 || h->root.type == bfd_link_hash_defweak
2666 || h->root.type == bfd_link_hash_common)
2669 /* We will always provide a local definition of function symbols,
2670 even if we don't have one yet. */
2671 if ((h->flags & XCOFF_CALLED) != 0)
2680 /* Absolute relocations against absolute symbols can be
2681 resolved statically. */
2683 && (h->root.type == bfd_link_hash_defined
2684 || h->root.type == bfd_link_hash_defweak)
2685 && bfd_is_abs_section (h->root.u.def.section))
2692 /* Mark a symbol as not being garbage, including the section in which
2695 static inline bfd_boolean
2696 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2698 if ((h->flags & XCOFF_MARK) != 0)
2701 h->flags |= XCOFF_MARK;
2703 /* If we're marking an undefined symbol, try find some way of
2705 if (!info->relocatable
2706 && (h->flags & XCOFF_IMPORT) == 0
2707 && (h->flags & XCOFF_DEF_REGULAR) == 0
2708 && (h->root.type == bfd_link_hash_undefined
2709 || h->root.type == bfd_link_hash_undefweak))
2711 /* First check whether this symbol can be interpreted as an
2712 undefined function descriptor for a defined function symbol. */
2713 if (!xcoff_find_function (info, h))
2716 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2717 && (h->descriptor->root.type == bfd_link_hash_defined
2718 || h->descriptor->root.type == bfd_link_hash_defweak))
2720 /* This is a descriptor for a defined symbol, but the input
2721 objects have not defined the descriptor itself. Fill in
2722 the definition automatically.
2724 Note that we do this even if we found a dynamic definition
2725 of H. The local function definition logically overrides
2729 sec = xcoff_hash_table (info)->descriptor_section;
2730 h->root.type = bfd_link_hash_defined;
2731 h->root.u.def.section = sec;
2732 h->root.u.def.value = sec->size;
2734 h->flags |= XCOFF_DEF_REGULAR;
2736 /* The size of the function descriptor depends on whether this
2737 is xcoff32 (12) or xcoff64 (24). */
2738 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2740 /* A function descriptor uses two relocs: one for the
2741 associated code, and one for the TOC address. */
2742 xcoff_hash_table (info)->ldrel_count += 2;
2743 sec->reloc_count += 2;
2745 /* Mark the function itself. */
2746 if (!xcoff_mark_symbol (info, h->descriptor))
2749 /* Mark the TOC section, so that we get an anchor
2750 to relocate against. */
2751 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2754 /* We handle writing out the contents of the descriptor in
2755 xcoff_write_global_symbol. */
2757 else if (info->static_link)
2758 /* We can't get a symbol value dynamically, so just assume
2759 that it's undefined. */
2760 h->flags |= XCOFF_WAS_UNDEFINED;
2761 else if ((h->flags & XCOFF_CALLED) != 0)
2763 /* This is a function symbol for which we need to create
2766 struct xcoff_link_hash_entry *hds;
2768 /* Mark the descriptor (and its TOC section). */
2769 hds = h->descriptor;
2770 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2771 || hds->root.type == bfd_link_hash_undefweak)
2772 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2773 if (!xcoff_mark_symbol (info, hds))
2776 /* Treat this symbol as undefined if the descriptor was. */
2777 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2778 h->flags |= XCOFF_WAS_UNDEFINED;
2780 /* Allocate room for the global linkage code itself. */
2781 sec = xcoff_hash_table (info)->linkage_section;
2782 h->root.type = bfd_link_hash_defined;
2783 h->root.u.def.section = sec;
2784 h->root.u.def.value = sec->size;
2786 h->flags |= XCOFF_DEF_REGULAR;
2787 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2789 /* The global linkage code requires a TOC entry for the
2791 if (hds->toc_section == NULL)
2796 xcoff32 uses 4 bytes in the toc.
2797 xcoff64 uses 8 bytes in the toc. */
2798 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2800 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2805 /* Allocate room in the fallback TOC section. */
2806 hds->toc_section = xcoff_hash_table (info)->toc_section;
2807 hds->u.toc_offset = hds->toc_section->size;
2808 hds->toc_section->size += byte_size;
2809 if (!xcoff_mark (info, hds->toc_section))
2812 /* Allocate room for a static and dynamic R_TOC
2814 ++xcoff_hash_table (info)->ldrel_count;
2815 ++hds->toc_section->reloc_count;
2817 /* Set the index to -2 to force this symbol to
2820 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2823 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2825 /* Record that the symbol was undefined, then import it.
2826 -brtl links use a special fake import file. */
2827 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2828 if (xcoff_hash_table (info)->rtld)
2830 if (!xcoff_set_import_path (info, h, "", "..", ""))
2835 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2841 if (h->root.type == bfd_link_hash_defined
2842 || h->root.type == bfd_link_hash_defweak)
2846 hsec = h->root.u.def.section;
2847 if (! bfd_is_abs_section (hsec)
2848 && (hsec->flags & SEC_MARK) == 0)
2850 if (! xcoff_mark (info, hsec))
2855 if (h->toc_section != NULL
2856 && (h->toc_section->flags & SEC_MARK) == 0)
2858 if (! xcoff_mark (info, h->toc_section))
2865 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2866 If the symbol exists, set FLAGS. */
2869 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2870 const char *name, unsigned int flags)
2872 struct xcoff_link_hash_entry *h;
2874 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2875 FALSE, FALSE, TRUE);
2879 if (h->root.type == bfd_link_hash_defined
2880 || h->root.type == bfd_link_hash_defweak)
2882 if (!xcoff_mark (info, h->root.u.def.section))
2889 /* The mark phase of garbage collection. For a given section, mark
2890 it, and all the sections which define symbols to which it refers.
2891 Because this function needs to look at the relocs, we also count
2892 the number of relocs which need to be copied into the .loader
2896 xcoff_mark (struct bfd_link_info *info, asection *sec)
2898 if (bfd_is_abs_section (sec)
2899 || (sec->flags & SEC_MARK) != 0)
2902 sec->flags |= SEC_MARK;
2904 if (sec->owner->xvec == info->output_bfd->xvec
2905 && coff_section_data (sec->owner, sec) != NULL
2906 && xcoff_section_data (sec->owner, sec) != NULL)
2908 struct xcoff_link_hash_entry **syms;
2909 struct internal_reloc *rel, *relend;
2911 unsigned long i, first, last;
2913 /* Mark all the symbols in this section. */
2914 syms = obj_xcoff_sym_hashes (sec->owner);
2915 csects = xcoff_data (sec->owner)->csects;
2916 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2917 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2918 for (i = first; i <= last; i++)
2919 if (csects[i] == sec
2921 && (syms[i]->flags & XCOFF_MARK) == 0)
2923 if (!xcoff_mark_symbol (info, syms[i]))
2927 /* Look through the section relocs. */
2928 if ((sec->flags & SEC_RELOC) != 0
2929 && sec->reloc_count > 0)
2931 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2935 relend = rel + sec->reloc_count;
2936 for (; rel < relend; rel++)
2938 struct xcoff_link_hash_entry *h;
2940 if ((unsigned int) rel->r_symndx
2941 > obj_raw_syment_count (sec->owner))
2944 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2947 if ((h->flags & XCOFF_MARK) == 0)
2949 if (!xcoff_mark_symbol (info, h))
2957 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2959 && (rsec->flags & SEC_MARK) == 0)
2961 if (!xcoff_mark (info, rsec))
2966 /* See if this reloc needs to be copied into the .loader
2968 if (xcoff_need_ldrel_p (info, rel, h))
2970 ++xcoff_hash_table (info)->ldrel_count;
2972 h->flags |= XCOFF_LDREL;
2976 if (! info->keep_memory
2977 && coff_section_data (sec->owner, sec) != NULL
2978 && coff_section_data (sec->owner, sec)->relocs != NULL
2979 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2981 free (coff_section_data (sec->owner, sec)->relocs);
2982 coff_section_data (sec->owner, sec)->relocs = NULL;
2990 /* Routines that are called after all the input files have been
2991 handled, but before the sections are laid out in memory. */
2993 /* The sweep phase of garbage collection. Remove all garbage
2997 xcoff_sweep (struct bfd_link_info *info)
3001 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3005 for (o = sub->sections; o != NULL; o = o->next)
3007 if ((o->flags & SEC_MARK) == 0)
3009 /* Keep all sections from non-XCOFF input files. Keep
3010 special sections. Keep .debug sections for the
3012 if (sub->xvec != info->output_bfd->xvec
3013 || o == xcoff_hash_table (info)->debug_section
3014 || o == xcoff_hash_table (info)->loader_section
3015 || o == xcoff_hash_table (info)->linkage_section
3016 || o == xcoff_hash_table (info)->descriptor_section
3017 || (bfd_get_section_flags (sub, o) & SEC_DEBUGGING)
3018 || strcmp (o->name, ".debug") == 0)
3019 o->flags |= SEC_MARK;
3030 /* Record the number of elements in a set. This is used to output the
3031 correct csect length. */
3034 bfd_xcoff_link_record_set (bfd *output_bfd,
3035 struct bfd_link_info *info,
3036 struct bfd_link_hash_entry *harg,
3039 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3040 struct xcoff_link_size_list *n;
3043 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3046 /* This will hardly ever be called. I don't want to burn four bytes
3047 per global symbol, so instead the size is kept on a linked list
3048 attached to the hash table. */
3050 n = bfd_alloc (output_bfd, amt);
3053 n->next = xcoff_hash_table (info)->size_list;
3056 xcoff_hash_table (info)->size_list = n;
3058 h->flags |= XCOFF_HAS_SIZE;
3063 /* Import a symbol. */
3066 bfd_xcoff_import_symbol (bfd *output_bfd,
3067 struct bfd_link_info *info,
3068 struct bfd_link_hash_entry *harg,
3070 const char *imppath,
3071 const char *impfile,
3072 const char *impmember,
3073 unsigned int syscall_flag)
3075 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3077 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3080 /* A symbol name which starts with a period is the code for a
3081 function. If the symbol is undefined, then add an undefined
3082 symbol for the function descriptor, and import that instead. */
3083 if (h->root.root.string[0] == '.'
3084 && h->root.type == bfd_link_hash_undefined
3085 && val == (bfd_vma) -1)
3087 struct xcoff_link_hash_entry *hds;
3089 hds = h->descriptor;
3092 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3093 h->root.root.string + 1,
3097 if (hds->root.type == bfd_link_hash_new)
3099 hds->root.type = bfd_link_hash_undefined;
3100 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3102 hds->flags |= XCOFF_DESCRIPTOR;
3103 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3104 hds->descriptor = h;
3105 h->descriptor = hds;
3108 /* Now, if the descriptor is undefined, import the descriptor
3109 rather than the symbol we were told to import. FIXME: Is
3110 this correct in all cases? */
3111 if (hds->root.type == bfd_link_hash_undefined)
3115 h->flags |= (XCOFF_IMPORT | syscall_flag);
3117 if (val != (bfd_vma) -1)
3119 if (h->root.type == bfd_link_hash_defined
3120 && (! bfd_is_abs_section (h->root.u.def.section)
3121 || h->root.u.def.value != val))
3123 if (! ((*info->callbacks->multiple_definition)
3124 (info, &h->root, output_bfd, bfd_abs_section_ptr, val)))
3128 h->root.type = bfd_link_hash_defined;
3129 h->root.u.def.section = bfd_abs_section_ptr;
3130 h->root.u.def.value = val;
3134 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3140 /* Export a symbol. */
3143 bfd_xcoff_export_symbol (bfd *output_bfd,
3144 struct bfd_link_info *info,
3145 struct bfd_link_hash_entry *harg)
3147 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3149 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3152 h->flags |= XCOFF_EXPORT;
3154 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3155 I'm just going to ignore it until somebody explains it. */
3157 /* Make sure we don't garbage collect this symbol. */
3158 if (! xcoff_mark_symbol (info, h))
3161 /* If this is a function descriptor, make sure we don't garbage
3162 collect the associated function code. We normally don't have to
3163 worry about this, because the descriptor will be attached to a
3164 section with relocs, but if we are creating the descriptor
3165 ourselves those relocs will not be visible to the mark code. */
3166 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3168 if (! xcoff_mark_symbol (info, h->descriptor))
3175 /* Count a reloc against a symbol. This is called for relocs
3176 generated by the linker script, typically for global constructors
3180 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3181 struct bfd_link_info *info,
3184 struct xcoff_link_hash_entry *h;
3186 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3189 h = ((struct xcoff_link_hash_entry *)
3190 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
3194 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3195 bfd_set_error (bfd_error_no_symbols);
3199 h->flags |= XCOFF_REF_REGULAR;
3200 if (xcoff_hash_table (info)->loader_section)
3202 h->flags |= XCOFF_LDREL;
3203 ++xcoff_hash_table (info)->ldrel_count;
3206 /* Mark the symbol to avoid garbage collection. */
3207 if (! xcoff_mark_symbol (info, h))
3213 /* This function is called for each symbol to which the linker script
3217 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3218 struct bfd_link_info *info,
3221 struct xcoff_link_hash_entry *h;
3223 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3226 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
3231 h->flags |= XCOFF_DEF_REGULAR;
3236 /* An xcoff_link_hash_traverse callback for which DATA points to an
3237 xcoff_loader_info. Mark all symbols that should be automatically
3241 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3243 struct xcoff_loader_info *ldinfo;
3245 ldinfo = (struct xcoff_loader_info *) data;
3246 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3248 if (!xcoff_mark_symbol (ldinfo->info, h))
3249 ldinfo->failed = TRUE;
3254 /* Add a symbol to the .loader symbols, if necessary. */
3256 /* INPUT_BFD has an external symbol associated with hash table entry H
3257 and csect CSECT. Return true if INPUT_BFD defines H. */
3260 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3263 switch (h->root.type)
3265 case bfd_link_hash_defined:
3266 case bfd_link_hash_defweak:
3267 /* No input bfd owns absolute symbols. They are written by
3268 xcoff_write_global_symbol instead. */
3269 return (!bfd_is_abs_section (csect)
3270 && h->root.u.def.section == csect);
3272 case bfd_link_hash_common:
3273 return h->root.u.c.p->section->owner == input_bfd;
3275 case bfd_link_hash_undefined:
3276 case bfd_link_hash_undefweak:
3277 /* We can't treat undef.abfd as the owner because that bfd
3278 might be a dynamic object. Allow any bfd to claim it. */
3286 /* See if H should have a loader symbol associated with it. */
3289 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3290 struct xcoff_link_hash_entry *h)
3294 /* Warn if this symbol is exported but not defined. */
3295 if ((h->flags & XCOFF_EXPORT) != 0
3296 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3298 (*_bfd_error_handler)
3299 (_("warning: attempt to export undefined symbol `%s'"),
3300 h->root.root.string);
3304 /* We need to add a symbol to the .loader section if it is mentioned
3305 in a reloc which we are copying to the .loader section and it was
3306 not defined or common, or if it is the entry point, or if it is
3308 if (((h->flags & XCOFF_LDREL) == 0
3309 || h->root.type == bfd_link_hash_defined
3310 || h->root.type == bfd_link_hash_defweak
3311 || h->root.type == bfd_link_hash_common)
3312 && (h->flags & XCOFF_ENTRY) == 0
3313 && (h->flags & XCOFF_EXPORT) == 0)
3316 /* We need to add this symbol to the .loader symbols. */
3318 BFD_ASSERT (h->ldsym == NULL);
3319 amt = sizeof (struct internal_ldsym);
3320 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3321 if (h->ldsym == NULL)
3323 ldinfo->failed = TRUE;
3327 if ((h->flags & XCOFF_IMPORT) != 0)
3329 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3330 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3332 h->ldsym->l_ifile = h->ldindx;
3335 /* The first 3 symbol table indices are reserved to indicate the
3336 data, text and bss sections. */
3337 h->ldindx = ldinfo->ldsym_count + 3;
3339 ++ldinfo->ldsym_count;
3341 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3342 h->ldsym, h->root.root.string))
3345 h->flags |= XCOFF_BUILT_LDSYM;
3349 /* An xcoff_htab_traverse callback that is called for each symbol
3350 once garbage collection is complete. */
3353 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3355 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3357 if (h->root.type == bfd_link_hash_warning)
3358 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3360 /* __rtinit, this symbol has special handling. */
3361 if (h->flags & XCOFF_RTINIT)
3364 /* We don't want to garbage collect symbols which are not defined in
3365 XCOFF files. This is a convenient place to mark them. */
3366 if (xcoff_hash_table (ldinfo->info)->gc
3367 && (h->flags & XCOFF_MARK) == 0
3368 && (h->root.type == bfd_link_hash_defined
3369 || h->root.type == bfd_link_hash_defweak)
3370 && (h->root.u.def.section->owner == NULL
3371 || (h->root.u.def.section->owner->xvec
3372 != ldinfo->info->output_bfd->xvec)))
3373 h->flags |= XCOFF_MARK;
3375 /* Skip discarded symbols. */
3376 if (xcoff_hash_table (ldinfo->info)->gc
3377 && (h->flags & XCOFF_MARK) == 0)
3380 /* If this is still a common symbol, and it wasn't garbage
3381 collected, we need to actually allocate space for it in the .bss
3383 if (h->root.type == bfd_link_hash_common
3384 && h->root.u.c.p->section->size == 0)
3386 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3387 h->root.u.c.p->section->size = h->root.u.c.size;
3390 if (xcoff_hash_table (ldinfo->info)->loader_section)
3392 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3393 h->flags |= XCOFF_EXPORT;
3395 if (!xcoff_build_ldsym (ldinfo, h))
3402 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3403 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3404 csect information, if any. NAME is the function's name if the name
3405 is stored in the .debug section, otherwise it is null.
3407 Return 1 if we should include an appropriately-adjusted ISYM
3408 in the output file, 0 if we should discard ISYM, or -1 if an
3412 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3413 struct internal_syment *isym,
3414 union internal_auxent *aux,
3415 struct xcoff_link_hash_entry *h,
3416 asection *csect, const char *name)
3420 /* If we are skipping this csect, we want to strip the symbol too. */
3424 /* Likewise if we garbage-collected the csect. */
3425 if (xcoff_hash_table (info)->gc
3426 && !bfd_is_abs_section (csect)
3427 && !bfd_is_und_section (csect)
3428 && (csect->flags & SEC_MARK) == 0)
3431 /* An XCOFF linker always removes C_STAT symbols. */
3432 if (isym->n_sclass == C_STAT)
3435 /* We generate the TOC anchor separately. */
3436 if (isym->n_sclass == C_HIDEXT
3437 && aux->x_csect.x_smclas == XMC_TC0)
3440 /* If we are stripping all symbols, we want to discard this one. */
3441 if (info->strip == strip_all)
3444 /* Discard symbols that are defined elsewhere. */
3445 if (EXTERN_SYM_P (isym->n_sclass))
3447 if ((h->flags & XCOFF_ALLOCATED) != 0)
3449 if (!xcoff_final_definition_p (input_bfd, h, csect))
3453 /* If we're discarding local symbols, check whether ISYM is local. */
3454 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3455 if (info->discard == discard_all
3456 && !EXTERN_SYM_P (isym->n_sclass)
3457 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3460 /* If we're stripping debugging symbols, check whether ISYM is one. */
3461 if (info->strip == strip_debugger
3462 && isym->n_scnum == N_DEBUG)
3465 /* If we are stripping symbols based on name, check how ISYM's
3466 name should be handled. */
3467 if (info->strip == strip_some
3468 || info->discard == discard_l)
3470 char buf[SYMNMLEN + 1];
3474 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3479 if (info->strip == strip_some
3480 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3483 if (info->discard == discard_l
3484 && !EXTERN_SYM_P (isym->n_sclass)
3485 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3486 && bfd_is_local_label_name (input_bfd, name))
3493 /* Lay out the .loader section, filling in the header and the import paths.
3494 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */
3497 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo,
3498 const char *libpath)
3501 struct xcoff_link_hash_table *htab;
3502 struct internal_ldhdr *ldhdr;
3503 struct xcoff_import_file *fl;
3504 bfd_size_type stoff;
3505 size_t impsize, impcount;
3509 /* Work out the size of the import file names. Each import file ID
3510 consists of three null terminated strings: the path, the file
3511 name, and the archive member name. The first entry in the list
3512 of names is the path to use to find objects, which the linker has
3513 passed in as the libpath argument. For some reason, the path
3514 entry in the other import file names appears to always be empty. */
3515 output_bfd = ldinfo->output_bfd;
3516 htab = xcoff_hash_table (ldinfo->info);
3517 impsize = strlen (libpath) + 3;
3519 for (fl = htab->imports; fl != NULL; fl = fl->next)
3522 impsize += (strlen (fl->path)
3524 + strlen (fl->member)
3528 /* Set up the .loader section header. */
3529 ldhdr = &htab->ldhdr;
3530 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3531 ldhdr->l_nsyms = ldinfo->ldsym_count;
3532 ldhdr->l_nreloc = htab->ldrel_count;
3533 ldhdr->l_istlen = impsize;
3534 ldhdr->l_nimpid = impcount;
3535 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3536 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3537 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3538 ldhdr->l_stlen = ldinfo->string_size;
3539 stoff = ldhdr->l_impoff + impsize;
3540 if (ldinfo->string_size == 0)
3543 ldhdr->l_stoff = stoff;
3545 /* 64 bit elements to ldhdr
3546 The swap out routine for 32 bit will ignore them.
3547 Nothing fancy, symbols come after the header and relocs come
3549 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3550 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3551 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3553 /* We now know the final size of the .loader section. Allocate
3555 lsec = htab->loader_section;
3556 lsec->size = stoff + ldhdr->l_stlen;
3557 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3558 if (lsec->contents == NULL)
3561 /* Set up the header. */
3562 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3564 /* Set up the import file names. */
3565 out = (char *) lsec->contents + ldhdr->l_impoff;
3566 strcpy (out, libpath);
3567 out += strlen (libpath) + 1;
3570 for (fl = htab->imports; fl != NULL; fl = fl->next)
3575 while ((*out++ = *s++) != '\0')
3578 while ((*out++ = *s++) != '\0')
3581 while ((*out++ = *s++) != '\0')
3585 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3587 /* Set up the symbol string table. */
3588 if (ldinfo->string_size > 0)
3590 memcpy (out, ldinfo->strings, ldinfo->string_size);
3591 free (ldinfo->strings);
3592 ldinfo->strings = NULL;
3595 /* We can't set up the symbol table or the relocs yet, because we
3596 don't yet know the final position of the various sections. The
3597 .loader symbols are written out when the corresponding normal
3598 symbols are written out in xcoff_link_input_bfd or
3599 xcoff_write_global_symbol. The .loader relocs are written out
3600 when the corresponding normal relocs are handled in
3601 xcoff_link_input_bfd. */
3606 /* Build the .loader section. This is called by the XCOFF linker
3607 emulation before_allocation routine. We must set the size of the
3608 .loader section before the linker lays out the output file.
3609 LIBPATH is the library path to search for shared objects; this is
3610 normally built from the -L arguments passed to the linker. ENTRY
3611 is the name of the entry point symbol (the -e linker option).
3612 FILE_ALIGN is the alignment to use for sections within the file
3613 (the -H linker option). MAXSTACK is the maximum stack size (the
3614 -bmaxstack linker option). MAXDATA is the maximum data size (the
3615 -bmaxdata linker option). GC is whether to do garbage collection
3616 (the -bgc linker option). MODTYPE is the module type (the
3617 -bmodtype linker option). TEXTRO is whether the text section must
3618 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3619 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3620 is set by this routine to csects with magic names like _end. */
3623 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3624 struct bfd_link_info *info,
3625 const char *libpath,
3627 unsigned long file_align,
3628 unsigned long maxstack,
3629 unsigned long maxdata,
3633 unsigned int auto_export_flags,
3634 asection **special_sections,
3637 struct xcoff_loader_info ldinfo;
3641 struct bfd_strtab_hash *debug_strtab;
3642 bfd_byte *debug_contents = NULL;
3645 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3647 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3648 special_sections[i] = NULL;
3652 ldinfo.failed = FALSE;
3653 ldinfo.output_bfd = output_bfd;
3655 ldinfo.auto_export_flags = auto_export_flags;
3656 ldinfo.ldsym_count = 0;
3657 ldinfo.string_size = 0;
3658 ldinfo.strings = NULL;
3659 ldinfo.string_alc = 0;
3661 xcoff_data (output_bfd)->maxstack = maxstack;
3662 xcoff_data (output_bfd)->maxdata = maxdata;
3663 xcoff_data (output_bfd)->modtype = modtype;
3665 xcoff_hash_table (info)->file_align = file_align;
3666 xcoff_hash_table (info)->textro = textro;
3667 xcoff_hash_table (info)->rtld = rtld;
3670 if (xcoff_hash_table (info)->loader_section
3671 && (info->init_function || info->fini_function || rtld))
3673 struct xcoff_link_hash_entry *hsym;
3674 struct internal_ldsym *ldsym;
3676 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3677 "__rtinit", FALSE, FALSE, TRUE);
3680 (*_bfd_error_handler)
3681 (_("error: undefined symbol __rtinit"));
3685 xcoff_mark_symbol (info, hsym);
3686 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3688 /* __rtinit initialized. */
3689 amt = sizeof (* ldsym);
3690 ldsym = bfd_malloc (amt);
3692 ldsym->l_value = 0; /* Will be filled in later. */
3693 ldsym->l_scnum = 2; /* Data section. */
3694 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3695 ldsym->l_smclas = 5; /* .rw. */
3696 ldsym->l_ifile = 0; /* Special system loader symbol. */
3697 ldsym->l_parm = 0; /* NA. */
3699 /* Force __rtinit to be the first symbol in the loader symbol table
3700 See xcoff_build_ldsyms
3702 The first 3 symbol table indices are reserved to indicate the data,
3703 text and bss sections. */
3704 BFD_ASSERT (0 == ldinfo.ldsym_count);
3707 ldinfo.ldsym_count = 1;
3708 hsym->ldsym = ldsym;
3710 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3711 hsym->ldsym, hsym->root.root.string))
3714 /* This symbol is written out by xcoff_write_global_symbol
3715 Set stuff up so xcoff_write_global_symbol logic works. */
3716 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3717 hsym->root.type = bfd_link_hash_defined;
3718 hsym->root.u.def.value = 0;
3721 /* Garbage collect unused sections. */
3722 if (info->relocatable || !gc)
3725 xcoff_hash_table (info)->gc = FALSE;
3727 /* We still need to call xcoff_mark, in order to set ldrel_count
3729 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3733 for (o = sub->sections; o != NULL; o = o->next)
3735 /* We shouldn't unconditionaly mark the TOC section.
3736 The output file should only have a TOC if either
3737 (a) one of the input files did or (b) we end up
3738 creating TOC references as part of the link process. */
3739 if (o != xcoff_hash_table (info)->toc_section
3740 && (o->flags & SEC_MARK) == 0)
3742 if (! xcoff_mark (info, o))
3751 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3753 if (info->init_function != NULL
3754 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3756 if (info->fini_function != NULL
3757 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3759 if (auto_export_flags != 0)
3761 xcoff_link_hash_traverse (xcoff_hash_table (info),
3762 xcoff_mark_auto_exports, &ldinfo);
3767 xcoff_hash_table (info)->gc = TRUE;
3770 /* Return special sections to the caller. */
3771 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3773 sec = xcoff_hash_table (info)->special_sections[i];
3777 && (sec->flags & SEC_MARK) == 0)
3780 special_sections[i] = sec;
3783 if (info->input_bfds == NULL)
3784 /* I'm not sure what to do in this bizarre case. */
3787 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3792 if (xcoff_hash_table (info)->loader_section
3793 && !xcoff_build_loader_section (&ldinfo, libpath))
3796 /* Allocate space for the magic sections. */
3797 sec = xcoff_hash_table (info)->linkage_section;
3800 sec->contents = bfd_zalloc (output_bfd, sec->size);
3801 if (sec->contents == NULL)
3804 sec = xcoff_hash_table (info)->toc_section;
3807 sec->contents = bfd_zalloc (output_bfd, sec->size);
3808 if (sec->contents == NULL)
3811 sec = xcoff_hash_table (info)->descriptor_section;
3814 sec->contents = bfd_zalloc (output_bfd, sec->size);
3815 if (sec->contents == NULL)
3819 /* Now that we've done garbage collection, decide which symbols to keep,
3820 and figure out the contents of the .debug section. */
3821 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3823 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3826 bfd_size_type symcount;
3829 unsigned int *lineno_counts;
3830 struct xcoff_link_hash_entry **sym_hash;
3831 bfd_byte *esym, *esymend;
3832 bfd_size_type symesz;
3834 if (sub->xvec != info->output_bfd->xvec)
3837 if ((sub->flags & DYNAMIC) != 0
3838 && !info->static_link)
3841 if (! _bfd_coff_get_external_symbols (sub))
3844 symcount = obj_raw_syment_count (sub);
3845 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3846 if (debug_index == NULL)
3848 xcoff_data (sub)->debug_indices = debug_index;
3850 if (info->strip == strip_all
3851 || info->strip == strip_debugger
3852 || info->discard == discard_all)
3853 /* We're stripping all debugging information, so there's no need
3854 to read SUB's .debug section. */
3858 /* Grab the contents of SUB's .debug section, if any. */
3859 subdeb = bfd_get_section_by_name (sub, ".debug");
3860 if (subdeb != NULL && subdeb->size > 0)
3862 /* We use malloc and copy the names into the debug
3863 stringtab, rather than bfd_alloc, because I expect
3864 that, when linking many files together, many of the
3865 strings will be the same. Storing the strings in the
3866 hash table should save space in this case. */
3867 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3872 csectpp = xcoff_data (sub)->csects;
3873 lineno_counts = xcoff_data (sub)->lineno_counts;
3874 sym_hash = obj_xcoff_sym_hashes (sub);
3875 symesz = bfd_coff_symesz (sub);
3876 esym = (bfd_byte *) obj_coff_external_syms (sub);
3877 esymend = esym + symcount * symesz;
3879 while (esym < esymend)
3881 struct internal_syment sym;
3882 union internal_auxent aux;
3887 bfd_coff_swap_sym_in (sub, esym, &sym);
3889 /* Read in the csect information, if any. */
3890 if (CSECT_SYM_P (sym.n_sclass))
3892 BFD_ASSERT (sym.n_numaux > 0);
3893 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3894 sym.n_type, sym.n_sclass,
3895 sym.n_numaux - 1, sym.n_numaux, &aux);
3898 /* If this symbol's name is stored in the debug section,
3899 get a pointer to it. */
3900 if (debug_contents != NULL
3901 && sym._n._n_n._n_zeroes == 0
3902 && bfd_coff_symname_in_debug (sub, &sym))
3903 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3907 /* Decide whether to copy this symbol to the output file. */
3909 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3910 *sym_hash, csect, name);
3915 /* Use a debug_index of -2 to record that a symbol should
3920 /* See whether we should store the symbol name in the
3921 output .debug section. */
3926 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3927 if (indx == (bfd_size_type) -1)
3929 *debug_index = indx;
3934 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3935 if (*lineno_counts > 0)
3936 csect->output_section->lineno_count += *lineno_counts;
3939 esym += (sym.n_numaux + 1) * symesz;
3940 csectpp += sym.n_numaux + 1;
3941 sym_hash += sym.n_numaux + 1;
3942 lineno_counts += sym.n_numaux + 1;
3943 debug_index += sym.n_numaux + 1;
3948 free (debug_contents);
3949 debug_contents = NULL;
3951 /* Clear the size of subdeb, so that it is not included directly
3952 in the output file. */
3956 if (! info->keep_memory)
3958 if (! _bfd_coff_free_symbols (sub))
3963 if (info->strip != strip_all)
3964 xcoff_hash_table (info)->debug_section->size =
3965 _bfd_stringtab_size (debug_strtab);
3970 if (ldinfo.strings != NULL)
3971 free (ldinfo.strings);
3972 if (debug_contents != NULL)
3973 free (debug_contents);
3978 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3983 struct bfd_in_memory *bim;
3985 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3992 abfd->link_next = 0;
3993 abfd->format = bfd_object;
3994 abfd->iostream = (void *) bim;
3995 abfd->flags = BFD_IN_MEMORY;
3996 abfd->iovec = &_bfd_memory_iovec;
3997 abfd->direction = write_direction;
4001 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
4004 /* need to reset to unknown or it will not be read back in correctly */
4005 abfd->format = bfd_unknown;
4006 abfd->direction = read_direction;
4012 /* Return the section that defines H. Return null if no section does. */
4015 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
4017 switch (h->root.type)
4019 case bfd_link_hash_defined:
4020 case bfd_link_hash_defweak:
4021 return h->root.u.def.section;
4023 case bfd_link_hash_common:
4024 return h->root.u.c.p->section;
4031 /* Add a .loader relocation for input relocation IREL. If the loader
4032 relocation should be against an output section, HSEC points to the
4033 input section that IREL is against, otherwise HSEC is null. H is the
4034 symbol that IREL is against, or null if it isn't against a global symbol.
4035 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
4038 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
4039 asection *output_section, bfd *reference_bfd,
4040 struct internal_reloc *irel, asection *hsec,
4041 struct xcoff_link_hash_entry *h)
4043 struct internal_ldrel ldrel;
4045 ldrel.l_vaddr = irel->r_vaddr;
4048 const char *secname;
4050 secname = hsec->output_section->name;
4051 if (strcmp (secname, ".text") == 0)
4053 else if (strcmp (secname, ".data") == 0)
4055 else if (strcmp (secname, ".bss") == 0)
4059 (*_bfd_error_handler)
4060 (_("%B: loader reloc in unrecognized section `%s'"),
4061 reference_bfd, secname);
4062 bfd_set_error (bfd_error_nonrepresentable_section);
4070 (*_bfd_error_handler)
4071 (_("%B: `%s' in loader reloc but not loader sym"),
4072 reference_bfd, h->root.root.string);
4073 bfd_set_error (bfd_error_bad_value);
4076 ldrel.l_symndx = h->ldindx;
4079 ldrel.l_symndx = -(bfd_size_type) 1;
4081 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4082 ldrel.l_rsecnm = output_section->target_index;
4083 if (xcoff_hash_table (flinfo->info)->textro
4084 && strcmp (output_section->name, ".text") == 0)
4086 (*_bfd_error_handler)
4087 (_("%B: loader reloc in read-only section %A"),
4088 reference_bfd, output_section);
4089 bfd_set_error (bfd_error_invalid_operation);
4092 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
4093 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
4097 /* Link an input file into the linker output file. This function
4098 handles all the sections and relocations of the input file at once. */
4101 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
4105 const char *strings;
4106 bfd_size_type syment_base;
4107 unsigned int n_tmask;
4108 unsigned int n_btshft;
4109 bfd_boolean copy, hash;
4110 bfd_size_type isymesz;
4111 bfd_size_type osymesz;
4112 bfd_size_type linesz;
4115 struct xcoff_link_hash_entry **sym_hash;
4116 struct internal_syment *isymp;
4118 unsigned int *lineno_counts;
4121 unsigned long output_index;
4125 bfd_boolean keep_syms;
4128 /* We can just skip DYNAMIC files, unless this is a static link. */
4129 if ((input_bfd->flags & DYNAMIC) != 0
4130 && ! flinfo->info->static_link)
4133 /* Move all the symbols to the output file. */
4134 output_bfd = flinfo->output_bfd;
4136 syment_base = obj_raw_syment_count (output_bfd);
4137 isymesz = bfd_coff_symesz (input_bfd);
4138 osymesz = bfd_coff_symesz (output_bfd);
4139 linesz = bfd_coff_linesz (input_bfd);
4140 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4142 n_tmask = coff_data (input_bfd)->local_n_tmask;
4143 n_btshft = coff_data (input_bfd)->local_n_btshft;
4145 /* Define macros so that ISFCN, et. al., macros work correctly. */
4146 #define N_TMASK n_tmask
4147 #define N_BTSHFT n_btshft
4150 if (! flinfo->info->keep_memory)
4153 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4156 if (! _bfd_coff_get_external_symbols (input_bfd))
4159 /* Make one pass over the symbols and assign indices to symbols that
4160 we have decided to keep. Also use create .loader symbol information
4161 and update information in hash table entries. */
4162 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4163 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4164 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4165 csectpp = xcoff_data (input_bfd)->csects;
4166 debug_index = xcoff_data (input_bfd)->debug_indices;
4167 isymp = flinfo->internal_syms;
4168 indexp = flinfo->sym_indices;
4169 output_index = syment_base;
4170 while (esym < esym_end)
4172 union internal_auxent aux;
4176 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
4178 /* Read in the csect information, if any. */
4179 if (CSECT_SYM_P (isymp->n_sclass))
4181 BFD_ASSERT (isymp->n_numaux > 0);
4182 bfd_coff_swap_aux_in (input_bfd,
4183 (void *) (esym + isymesz * isymp->n_numaux),
4184 isymp->n_type, isymp->n_sclass,
4185 isymp->n_numaux - 1, isymp->n_numaux,
4188 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4191 /* If this symbol is in the .loader section, swap out the
4192 .loader symbol information. If this is an external symbol
4193 reference to a defined symbol, though, then wait until we get
4194 to the definition. */
4195 if (EXTERN_SYM_P (isymp->n_sclass)
4196 && *sym_hash != NULL
4197 && (*sym_hash)->ldsym != NULL
4198 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
4200 struct xcoff_link_hash_entry *h;
4201 struct internal_ldsym *ldsym;
4205 if (isymp->n_scnum > 0)
4207 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4208 ldsym->l_value = (isymp->n_value
4209 + (*csectpp)->output_section->vma
4210 + (*csectpp)->output_offset
4215 ldsym->l_scnum = isymp->n_scnum;
4216 ldsym->l_value = isymp->n_value;
4219 ldsym->l_smtype = smtyp;
4220 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4221 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4222 || (h->flags & XCOFF_IMPORT) != 0)
4223 ldsym->l_smtype |= L_IMPORT;
4224 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4225 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4226 || (h->flags & XCOFF_EXPORT) != 0)
4227 ldsym->l_smtype |= L_EXPORT;
4228 if ((h->flags & XCOFF_ENTRY) != 0)
4229 ldsym->l_smtype |= L_ENTRY;
4230 if (isymp->n_sclass == C_AIX_WEAKEXT)
4231 ldsym->l_smtype |= L_WEAK;
4233 ldsym->l_smclas = aux.x_csect.x_smclas;
4235 if (ldsym->l_ifile == (bfd_size_type) -1)
4237 else if (ldsym->l_ifile == 0)
4239 if ((ldsym->l_smtype & L_IMPORT) == 0)
4245 if (h->root.type == bfd_link_hash_defined
4246 || h->root.type == bfd_link_hash_defweak)
4247 impbfd = h->root.u.def.section->owner;
4248 else if (h->root.type == bfd_link_hash_undefined
4249 || h->root.type == bfd_link_hash_undefweak)
4250 impbfd = h->root.u.undef.abfd;
4258 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
4259 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4266 BFD_ASSERT (h->ldindx >= 0);
4267 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
4270 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
4273 /* Fill in snentry now that we know the target_index. */
4274 if ((h->flags & XCOFF_ENTRY) != 0
4275 && (h->root.type == bfd_link_hash_defined
4276 || h->root.type == bfd_link_hash_defweak))
4278 xcoff_data (output_bfd)->snentry =
4279 h->root.u.def.section->output_section->target_index;
4283 add = 1 + isymp->n_numaux;
4285 if (*debug_index == -2)
4286 /* We've decided to strip this symbol. */
4290 /* Assign the next unused index to this symbol. */
4291 *indexp = output_index;
4293 if (EXTERN_SYM_P (isymp->n_sclass))
4295 BFD_ASSERT (*sym_hash != NULL);
4296 (*sym_hash)->indx = output_index;
4299 /* If this is a symbol in the TOC which we may have merged
4300 (class XMC_TC), remember the symbol index of the TOC
4302 if (isymp->n_sclass == C_HIDEXT
4303 && aux.x_csect.x_smclas == XMC_TC
4304 && *sym_hash != NULL)
4306 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4307 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4308 (*sym_hash)->u.toc_indx = output_index;
4311 output_index += add;
4314 esym += add * isymesz;
4320 for (--add; add > 0; --add)
4324 /* Now write out the symbols that we decided to keep. */
4326 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4327 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4328 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4329 isymp = flinfo->internal_syms;
4330 indexp = flinfo->sym_indices;
4331 csectpp = xcoff_data (input_bfd)->csects;
4332 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
4333 debug_index = xcoff_data (input_bfd)->debug_indices;
4334 outsym = flinfo->outsyms;
4337 while (esym < esym_end)
4341 add = 1 + isymp->n_numaux;
4344 esym += add * isymesz;
4347 struct internal_syment isym;
4350 /* Adjust the symbol in order to output it. */
4352 if (isym._n._n_n._n_zeroes == 0
4353 && isym._n._n_n._n_offset != 0)
4355 /* This symbol has a long name. Enter it in the string
4356 table we are building. If *debug_index != -1, the
4357 name has already been entered in the .debug section. */
4358 if (*debug_index >= 0)
4359 isym._n._n_n._n_offset = *debug_index;
4365 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4369 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
4370 if (indx == (bfd_size_type) -1)
4372 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4376 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
4377 multiple definition problems when linking a shared object
4378 statically. (The native linker doesn't enter __rtinit into
4379 the normal table at all, but having a local symbol can make
4380 the objdump output easier to read.) */
4381 if (isym.n_sclass == C_EXT
4383 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
4384 isym.n_sclass = C_HIDEXT;
4386 /* The value of a C_FILE symbol is the symbol index of the
4387 next C_FILE symbol. The value of the last C_FILE symbol
4388 is -1. We try to get this right, below, just before we
4389 write the symbols out, but in the general case we may
4390 have to write the symbol out twice. */
4391 if (isym.n_sclass == C_FILE)
4393 if (flinfo->last_file_index != -1
4394 && flinfo->last_file.n_value != (bfd_vma) *indexp)
4396 /* We must correct the value of the last C_FILE entry. */
4397 flinfo->last_file.n_value = *indexp;
4398 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
4400 /* The last C_FILE symbol is in this input file. */
4401 bfd_coff_swap_sym_out (output_bfd,
4402 (void *) &flinfo->last_file,
4403 (void *) (flinfo->outsyms
4404 + ((flinfo->last_file_index
4410 /* We have already written out the last C_FILE
4411 symbol. We need to write it out again. We
4412 borrow *outsym temporarily. */
4415 bfd_coff_swap_sym_out (output_bfd,
4416 (void *) &flinfo->last_file,
4419 pos = obj_sym_filepos (output_bfd);
4420 pos += flinfo->last_file_index * osymesz;
4421 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4422 || (bfd_bwrite (outsym, osymesz, output_bfd)
4428 flinfo->last_file_index = *indexp;
4429 flinfo->last_file = isym;
4432 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4433 into the line numbers. We update the symbol values when
4434 we handle the line numbers. */
4435 if (isym.n_sclass == C_BINCL
4436 || isym.n_sclass == C_EINCL)
4438 isym.n_value = flinfo->line_filepos;
4441 /* The value of a C_BSTAT symbol is the symbol table
4442 index of the containing csect. */
4443 else if (isym.n_sclass == C_BSTAT)
4447 indx = isym.n_value;
4448 if (indx < obj_raw_syment_count (input_bfd))
4452 symindx = flinfo->sym_indices[indx];
4456 isym.n_value = symindx;
4459 else if (isym.n_sclass != C_ESTAT
4460 && isym.n_sclass != C_DECL
4461 && isym.n_scnum > 0)
4463 isym.n_scnum = (*csectpp)->output_section->target_index;
4464 isym.n_value += ((*csectpp)->output_section->vma
4465 + (*csectpp)->output_offset
4469 /* Output the symbol. */
4470 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4475 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4477 union internal_auxent aux;
4479 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4480 isymp->n_sclass, i, isymp->n_numaux,
4483 if (isymp->n_sclass == C_FILE)
4485 /* This is the file name (or some comment put in by
4486 the compiler). If it is long, we must put it in
4487 the string table. */
4488 if (aux.x_file.x_n.x_zeroes == 0
4489 && aux.x_file.x_n.x_offset != 0)
4491 const char *filename;
4494 BFD_ASSERT (aux.x_file.x_n.x_offset
4495 >= STRING_SIZE_SIZE);
4496 if (strings == NULL)
4498 strings = _bfd_coff_read_string_table (input_bfd);
4499 if (strings == NULL)
4502 filename = strings + aux.x_file.x_n.x_offset;
4503 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4505 if (indx == (bfd_size_type) -1)
4507 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4510 else if (CSECT_SYM_P (isymp->n_sclass)
4511 && i + 1 == isymp->n_numaux)
4514 /* We don't support type checking. I don't know if
4516 aux.x_csect.x_parmhash = 0;
4517 /* I don't think anybody uses these fields, but we'd
4518 better clobber them just in case. */
4519 aux.x_csect.x_stab = 0;
4520 aux.x_csect.x_snstab = 0;
4522 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4526 indx = aux.x_csect.x_scnlen.l;
4527 if (indx < obj_raw_syment_count (input_bfd))
4531 symindx = flinfo->sym_indices[indx];
4534 aux.x_csect.x_scnlen.l = 0;
4538 aux.x_csect.x_scnlen.l = symindx;
4543 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4547 if (ISFCN (isymp->n_type)
4548 || ISTAG (isymp->n_sclass)
4549 || isymp->n_sclass == C_BLOCK
4550 || isymp->n_sclass == C_FCN)
4552 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4554 && indx < obj_raw_syment_count (input_bfd))
4556 /* We look forward through the symbol for
4557 the index of the next symbol we are going
4558 to include. I don't know if this is
4560 while (flinfo->sym_indices[indx] < 0
4561 && indx < obj_raw_syment_count (input_bfd))
4563 if (indx >= obj_raw_syment_count (input_bfd))
4564 indx = output_index;
4566 indx = flinfo->sym_indices[indx];
4567 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4572 indx = aux.x_sym.x_tagndx.l;
4573 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4577 symindx = flinfo->sym_indices[indx];
4579 aux.x_sym.x_tagndx.l = 0;
4581 aux.x_sym.x_tagndx.l = symindx;
4586 /* Copy over the line numbers, unless we are stripping
4587 them. We do this on a symbol by symbol basis in
4588 order to more easily handle garbage collection. */
4589 if (CSECT_SYM_P (isymp->n_sclass)
4591 && isymp->n_numaux > 1
4592 && ISFCN (isymp->n_type)
4593 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4595 if (*lineno_counts == 0)
4596 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4599 asection *enclosing;
4600 unsigned int enc_count;
4601 bfd_signed_vma linoff;
4602 struct internal_lineno lin;
4609 /* Read in the enclosing section's line-number
4610 information, if we haven't already. */
4612 enclosing = xcoff_section_data (abfd, o)->enclosing;
4613 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4614 if (oline != enclosing)
4616 pos = enclosing->line_filepos;
4617 amt = linesz * enc_count;
4618 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4619 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4625 /* Copy across the first entry, adjusting its
4627 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4628 - enclosing->line_filepos);
4629 linp = flinfo->linenos + linoff;
4630 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4631 lin.l_addr.l_symndx = *indexp;
4632 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4634 /* Copy the other entries, adjusting their addresses. */
4635 linpend = linp + *lineno_counts * linesz;
4636 offset = (o->output_section->vma
4639 for (linp += linesz; linp < linpend; linp += linesz)
4641 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4642 lin.l_addr.l_paddr += offset;
4643 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4646 /* Write out the entries we've just processed. */
4647 pos = (o->output_section->line_filepos
4648 + o->output_section->lineno_count * linesz);
4649 amt = linesz * *lineno_counts;
4650 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4651 || bfd_bwrite (flinfo->linenos + linoff,
4652 amt, output_bfd) != amt)
4654 o->output_section->lineno_count += *lineno_counts;
4656 /* Record the offset of the symbol's line numbers
4657 in the output file. */
4658 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4662 struct internal_syment *iisp, *iispend;
4665 bfd_vma range_start, range_end;
4668 /* Update any C_BINCL or C_EINCL symbols
4669 that refer to a line number in the
4670 range we just output. */
4671 iisp = flinfo->internal_syms;
4672 iispend = iisp + obj_raw_syment_count (input_bfd);
4673 iindp = flinfo->sym_indices;
4674 oos = flinfo->outsyms;
4675 range_start = enclosing->line_filepos + linoff;
4676 range_end = range_start + *lineno_counts * linesz;
4677 while (iisp < iispend)
4680 && (iisp->n_sclass == C_BINCL
4681 || iisp->n_sclass == C_EINCL)
4682 && iisp->n_value >= range_start
4683 && iisp->n_value < range_end)
4685 struct internal_syment iis;
4687 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4688 iis.n_value = (iisp->n_value
4691 bfd_coff_swap_sym_out (output_bfd,
4696 iiadd = 1 + iisp->n_numaux;
4698 oos += iiadd * osymesz;
4706 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4707 isymp->n_sclass, i, isymp->n_numaux,
4718 lineno_counts += add;
4722 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4723 symbol will be the first symbol in the next input file. In the
4724 normal case, this will save us from writing out the C_FILE symbol
4726 if (flinfo->last_file_index != -1
4727 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4729 flinfo->last_file.n_value = output_index;
4730 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4731 (void *) (flinfo->outsyms
4732 + ((flinfo->last_file_index - syment_base)
4736 /* Write the modified symbols to the output file. */
4737 if (outsym > flinfo->outsyms)
4739 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4740 bfd_size_type amt = outsym - flinfo->outsyms;
4741 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4742 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4745 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4746 + (outsym - flinfo->outsyms) / osymesz)
4749 obj_raw_syment_count (output_bfd) = output_index;
4752 /* Don't let the linker relocation routines discard the symbols. */
4753 keep_syms = obj_coff_keep_syms (input_bfd);
4754 obj_coff_keep_syms (input_bfd) = TRUE;
4756 /* Relocate the contents of each section. */
4757 for (o = input_bfd->sections; o != NULL; o = o->next)
4761 if (! o->linker_mark)
4762 /* This section was omitted from the link. */
4765 if ((o->flags & SEC_HAS_CONTENTS) == 0
4767 || (o->flags & SEC_IN_MEMORY) != 0)
4770 /* We have set filepos correctly for the sections we created to
4771 represent csects, so bfd_get_section_contents should work. */
4772 if (coff_section_data (input_bfd, o) != NULL
4773 && coff_section_data (input_bfd, o)->contents != NULL)
4774 contents = coff_section_data (input_bfd, o)->contents;
4777 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4778 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4780 contents = flinfo->contents;
4783 if ((o->flags & SEC_RELOC) != 0)
4786 struct internal_reloc *internal_relocs;
4787 struct internal_reloc *irel;
4789 struct internal_reloc *irelend;
4790 struct xcoff_link_hash_entry **rel_hash;
4793 /* Read in the relocs. */
4794 target_index = o->output_section->target_index;
4795 internal_relocs = (xcoff_read_internal_relocs
4796 (input_bfd, o, FALSE, flinfo->external_relocs,
4798 (flinfo->section_info[target_index].relocs
4799 + o->output_section->reloc_count)));
4800 if (internal_relocs == NULL)
4803 /* Call processor specific code to relocate the section
4805 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4809 flinfo->internal_syms,
4810 xcoff_data (input_bfd)->csects))
4813 offset = o->output_section->vma + o->output_offset - o->vma;
4814 irel = internal_relocs;
4815 irelend = irel + o->reloc_count;
4816 rel_hash = (flinfo->section_info[target_index].rel_hashes
4817 + o->output_section->reloc_count);
4818 for (; irel < irelend; irel++, rel_hash++)
4820 struct xcoff_link_hash_entry *h = NULL;
4824 /* Adjust the reloc address and symbol index. */
4826 irel->r_vaddr += offset;
4828 r_symndx = irel->r_symndx;
4833 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4835 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4838 && h->smclas != XMC_TD
4839 && (irel->r_type == R_TOC
4840 || irel->r_type == R_GL
4841 || irel->r_type == R_TCL
4842 || irel->r_type == R_TRL
4843 || irel->r_type == R_TRLA))
4845 /* This is a TOC relative reloc with a symbol
4846 attached. The symbol should be the one which
4847 this reloc is for. We want to make this
4848 reloc against the TOC address of the symbol,
4849 not the symbol itself. */
4850 BFD_ASSERT (h->toc_section != NULL);
4851 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4852 if (h->u.toc_indx != -1)
4853 irel->r_symndx = h->u.toc_indx;
4856 struct xcoff_toc_rel_hash *n;
4857 struct xcoff_link_section_info *si;
4861 n = bfd_alloc (flinfo->output_bfd, amt);
4864 si = flinfo->section_info + target_index;
4865 n->next = si->toc_rel_hashes;
4868 si->toc_rel_hashes = n;
4873 /* This is a global symbol. */
4875 irel->r_symndx = h->indx;
4878 /* This symbol is being written at the end
4879 of the file, and we do not yet know the
4880 symbol index. We save the pointer to the
4881 hash table entry in the rel_hash list.
4882 We set the indx field to -2 to indicate
4883 that this symbol must not be stripped. */
4892 indx = flinfo->sym_indices[r_symndx];
4896 struct internal_syment *is;
4898 /* Relocations against a TC0 TOC anchor are
4899 automatically transformed to be against
4900 the TOC anchor in the output file. */
4901 is = flinfo->internal_syms + r_symndx;
4902 if (is->n_sclass == C_HIDEXT
4903 && is->n_numaux > 0)
4906 union internal_auxent aux;
4910 obj_coff_external_syms (input_bfd))
4911 + ((r_symndx + is->n_numaux)
4913 bfd_coff_swap_aux_in (input_bfd, auxptr,
4914 is->n_type, is->n_sclass,
4918 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4919 && aux.x_csect.x_smclas == XMC_TC0)
4920 indx = flinfo->toc_symindx;
4925 irel->r_symndx = indx;
4929 struct internal_syment *is;
4932 char buf[SYMNMLEN + 1];
4934 /* This reloc is against a symbol we are
4935 stripping. It would be possible to handle
4936 this case, but I don't think it's worth it. */
4937 is = flinfo->internal_syms + r_symndx;
4939 if (is->n_sclass != C_DWARF)
4941 name = (_bfd_coff_internal_syment_name
4942 (input_bfd, is, buf));
4947 if (!(*flinfo->info->callbacks->unattached_reloc)
4948 (flinfo->info, name, input_bfd, o,
4956 if ((o->flags & SEC_DEBUGGING) == 0
4957 && xcoff_need_ldrel_p (flinfo->info, irel, h))
4964 sec = xcoff_data (input_bfd)->csects[r_symndx];
4966 sec = xcoff_symbol_section (h);
4967 if (!xcoff_create_ldrel (output_bfd, flinfo,
4968 o->output_section, input_bfd,
4974 o->output_section->reloc_count += o->reloc_count;
4977 /* Write out the modified section contents. */
4978 if (! bfd_set_section_contents (output_bfd, o->output_section,
4979 contents, (file_ptr) o->output_offset,
4984 obj_coff_keep_syms (input_bfd) = keep_syms;
4986 if (! flinfo->info->keep_memory)
4988 if (! _bfd_coff_free_symbols (input_bfd))
4998 /* Sort relocs by VMA. This is called via qsort. */
5001 xcoff_sort_relocs (const void * p1, const void * p2)
5003 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5004 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5006 if (r1->r_vaddr > r2->r_vaddr)
5008 else if (r1->r_vaddr < r2->r_vaddr)
5014 /* Return true if section SEC is a TOC section. */
5016 static inline bfd_boolean
5017 xcoff_toc_section_p (asection *sec)
5022 if (name[0] == '.' && name[1] == 't')
5026 if (name[3] == '0' && name[4] == 0)
5031 if (name[2] == 'd' && name[3] == 0)
5037 /* See if the link requires a TOC (it usually does!). If so, find a
5038 good place to put the TOC anchor csect, and write out the associated
5042 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5044 bfd_vma toc_start, toc_end, start, end, best_address;
5048 struct internal_syment irsym;
5049 union internal_auxent iraux;
5053 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5054 index of a csect at the beginning of the TOC. */
5055 toc_start = ~(bfd_vma) 0;
5058 for (input_bfd = flinfo->info->input_bfds;
5060 input_bfd = input_bfd->link_next)
5061 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5062 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5064 start = sec->output_section->vma + sec->output_offset;
5065 if (toc_start > start)
5068 section_index = sec->output_section->target_index;
5071 end = start + sec->size;
5076 /* There's no need for a TC0 symbol if we don't have a TOC. */
5077 if (toc_end < toc_start)
5079 xcoff_data (output_bfd)->toc = toc_start;
5083 if (toc_end - toc_start < 0x8000)
5084 /* Every TOC csect can be accessed from TOC_START. */
5085 best_address = toc_start;
5088 /* Find the lowest TOC csect that is still within range of TOC_END. */
5089 best_address = toc_end;
5090 for (input_bfd = flinfo->info->input_bfds;
5092 input_bfd = input_bfd->link_next)
5093 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5094 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
5096 start = sec->output_section->vma + sec->output_offset;
5097 if (start < best_address
5098 && start + 0x8000 >= toc_end)
5100 best_address = start;
5101 section_index = sec->output_section->target_index;
5105 /* Make sure that the start of the TOC is also within range. */
5106 if (best_address > toc_start + 0x8000)
5108 (*_bfd_error_handler)
5109 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
5111 (unsigned long) (toc_end - toc_start));
5112 bfd_set_error (bfd_error_file_too_big);
5117 /* Record the chosen TOC value. */
5118 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5119 xcoff_data (output_bfd)->toc = best_address;
5120 xcoff_data (output_bfd)->sntoc = section_index;
5122 /* Fill out the TC0 symbol. */
5123 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &irsym, "TOC"))
5125 irsym.n_value = best_address;
5126 irsym.n_scnum = section_index;
5127 irsym.n_sclass = C_HIDEXT;
5128 irsym.n_type = T_NULL;
5130 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5132 /* Fill out the auxillary csect information. */
5133 memset (&iraux, 0, sizeof iraux);
5134 iraux.x_csect.x_smtyp = XTY_SD;
5135 iraux.x_csect.x_smclas = XMC_TC0;
5136 iraux.x_csect.x_scnlen.l = 0;
5137 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5138 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5140 /* Write the contents to the file. */
5141 pos = obj_sym_filepos (output_bfd);
5142 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5143 size = 2 * bfd_coff_symesz (output_bfd);
5144 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5145 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5147 obj_raw_syment_count (output_bfd) += 2;
5152 /* Write out a non-XCOFF global symbol. */
5155 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
5157 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5160 struct internal_syment isym;
5161 union internal_auxent aux;
5166 output_bfd = flinfo->output_bfd;
5167 outsym = flinfo->outsyms;
5169 if (h->root.type == bfd_link_hash_warning)
5171 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5172 if (h->root.type == bfd_link_hash_new)
5176 /* If this symbol was garbage collected, just skip it. */
5177 if (xcoff_hash_table (flinfo->info)->gc
5178 && (h->flags & XCOFF_MARK) == 0)
5181 /* If we need a .loader section entry, write it out. */
5182 if (h->ldsym != NULL)
5184 struct internal_ldsym *ldsym;
5189 if (h->root.type == bfd_link_hash_undefined
5190 || h->root.type == bfd_link_hash_undefweak)
5194 ldsym->l_scnum = N_UNDEF;
5195 ldsym->l_smtype = XTY_ER;
5196 impbfd = h->root.u.undef.abfd;
5199 else if (h->root.type == bfd_link_hash_defined
5200 || h->root.type == bfd_link_hash_defweak)
5204 sec = h->root.u.def.section;
5205 ldsym->l_value = (sec->output_section->vma
5206 + sec->output_offset
5207 + h->root.u.def.value);
5208 ldsym->l_scnum = sec->output_section->target_index;
5209 ldsym->l_smtype = XTY_SD;
5210 impbfd = sec->owner;
5216 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5217 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5218 || (h->flags & XCOFF_IMPORT) != 0)
5220 Import symbols are defined so the check above will make
5221 the l_smtype XTY_SD. But this is not correct, it should
5223 ldsym->l_smtype |= L_IMPORT;
5225 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5226 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5227 || (h->flags & XCOFF_EXPORT) != 0)
5228 ldsym->l_smtype |= L_EXPORT;
5230 if ((h->flags & XCOFF_ENTRY) != 0)
5231 ldsym->l_smtype |= L_ENTRY;
5233 if ((h->flags & XCOFF_RTINIT) != 0)
5234 ldsym->l_smtype = XTY_SD;
5236 ldsym->l_smclas = h->smclas;
5238 if (ldsym->l_smtype & L_IMPORT)
5240 if ((h->root.type == bfd_link_hash_defined
5241 || h->root.type == bfd_link_hash_defweak)
5242 && (h->root.u.def.value != 0))
5243 ldsym->l_smclas = XMC_XO;
5245 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5246 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5247 ldsym->l_smclas = XMC_SV3264;
5249 else if (h->flags & XCOFF_SYSCALL32)
5250 ldsym->l_smclas = XMC_SV;
5252 else if (h->flags & XCOFF_SYSCALL64)
5253 ldsym->l_smclas = XMC_SV64;
5256 if (ldsym->l_ifile == -(bfd_size_type) 1)
5260 else if (ldsym->l_ifile == 0)
5262 if ((ldsym->l_smtype & L_IMPORT) == 0)
5264 else if (impbfd == NULL)
5268 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5269 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5275 BFD_ASSERT (h->ldindx >= 0);
5277 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5280 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5284 /* If this symbol needs global linkage code, write it out. */
5285 if (h->root.type == bfd_link_hash_defined
5286 && (h->root.u.def.section
5287 == xcoff_hash_table (flinfo->info)->linkage_section))
5293 p = h->root.u.def.section->contents + h->root.u.def.value;
5295 /* The first instruction in the global linkage code loads a
5296 specific TOC element. */
5297 tocoff = (h->descriptor->toc_section->output_section->vma
5298 + h->descriptor->toc_section->output_offset
5299 - xcoff_data (output_bfd)->toc);
5301 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5302 tocoff += h->descriptor->u.toc_offset;
5304 /* The first instruction in the glink code needs to be
5305 cooked to to hold the correct offset in the toc. The
5306 rest are just output raw. */
5307 bfd_put_32 (output_bfd,
5308 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5310 /* Start with i == 1 to get past the first instruction done above
5311 The /4 is because the glink code is in bytes and we are going
5313 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5314 bfd_put_32 (output_bfd,
5315 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5319 /* If we created a TOC entry for this symbol, write out the required
5321 if ((h->flags & XCOFF_SET_TOC) != 0)
5326 struct internal_reloc *irel;
5327 struct internal_syment irsym;
5328 union internal_auxent iraux;
5330 tocsec = h->toc_section;
5331 osec = tocsec->output_section;
5332 oindx = osec->target_index;
5333 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5334 irel->r_vaddr = (osec->vma
5335 + tocsec->output_offset
5339 irel->r_symndx = h->indx;
5343 irel->r_symndx = obj_raw_syment_count (output_bfd);
5346 BFD_ASSERT (h->ldindx >= 0);
5348 /* Initialize the aux union here instead of closer to when it is
5349 written out below because the length of the csect depends on
5350 whether the output is 32 or 64 bit. */
5351 memset (&iraux, 0, sizeof iraux);
5352 iraux.x_csect.x_smtyp = XTY_SD;
5353 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5354 iraux.x_csect.x_smclas = XMC_TC;
5356 /* 32 bit uses a 32 bit R_POS to do the relocations
5357 64 bit uses a 64 bit R_POS to do the relocations
5359 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5361 Which one is determined by the backend. */
5362 if (bfd_xcoff_is_xcoff64 (output_bfd))
5365 iraux.x_csect.x_scnlen.l = 8;
5367 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5370 iraux.x_csect.x_scnlen.l = 4;
5375 irel->r_type = R_POS;
5376 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5377 ++osec->reloc_count;
5379 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5380 output_bfd, irel, NULL, h))
5383 /* We need to emit a symbol to define a csect which holds
5385 if (flinfo->info->strip != strip_all)
5387 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab,
5388 &irsym, h->root.root.string);
5392 irsym.n_value = irel->r_vaddr;
5393 irsym.n_scnum = osec->target_index;
5394 irsym.n_sclass = C_HIDEXT;
5395 irsym.n_type = T_NULL;
5398 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5399 outsym += bfd_coff_symesz (output_bfd);
5401 /* Note : iraux is initialized above. */
5402 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5403 0, 1, (void *) outsym);
5404 outsym += bfd_coff_auxesz (output_bfd);
5408 /* We aren't going to write out the symbols below, so we
5409 need to write them out now. */
5410 pos = obj_sym_filepos (output_bfd);
5411 pos += (obj_raw_syment_count (output_bfd)
5412 * bfd_coff_symesz (output_bfd));
5413 amt = outsym - flinfo->outsyms;
5414 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5415 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5417 obj_raw_syment_count (output_bfd) +=
5418 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5420 outsym = flinfo->outsyms;
5425 /* If this symbol is a specially defined function descriptor, write
5426 it out. The first word is the address of the function code
5427 itself, the second word is the address of the TOC, and the third
5431 The addresses for the 32 bit will take 4 bytes and the addresses
5432 for 64 bit will take 8 bytes. Similar for the relocs. This type
5433 of logic was also done above to create a TOC entry in
5434 xcoff_write_global_symbol. */
5435 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5436 && h->root.type == bfd_link_hash_defined
5437 && (h->root.u.def.section
5438 == xcoff_hash_table (flinfo->info)->descriptor_section))
5444 struct xcoff_link_hash_entry *hentry;
5446 struct internal_reloc *irel;
5448 unsigned int reloc_size, byte_size;
5450 if (bfd_xcoff_is_xcoff64 (output_bfd))
5455 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5463 sec = h->root.u.def.section;
5464 osec = sec->output_section;
5465 oindx = osec->target_index;
5466 p = sec->contents + h->root.u.def.value;
5468 hentry = h->descriptor;
5469 BFD_ASSERT (hentry != NULL
5470 && (hentry->root.type == bfd_link_hash_defined
5471 || hentry->root.type == bfd_link_hash_defweak));
5472 esec = hentry->root.u.def.section;
5474 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5475 irel->r_vaddr = (osec->vma
5476 + sec->output_offset
5477 + h->root.u.def.value);
5478 irel->r_symndx = esec->output_section->target_index;
5479 irel->r_type = R_POS;
5480 irel->r_size = reloc_size;
5481 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5482 ++osec->reloc_count;
5484 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5485 output_bfd, irel, esec, NULL))
5488 /* There are three items to write out,
5489 the address of the code
5490 the address of the toc anchor
5491 the environment pointer.
5492 We are ignoring the environment pointer. So set it to zero. */
5493 if (bfd_xcoff_is_xcoff64 (output_bfd))
5495 bfd_put_64 (output_bfd,
5496 (esec->output_section->vma + esec->output_offset
5497 + hentry->root.u.def.value),
5499 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5500 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5505 This logic was already called above so the error case where
5506 the backend is neither has already been checked. */
5507 bfd_put_32 (output_bfd,
5508 (esec->output_section->vma + esec->output_offset
5509 + hentry->root.u.def.value),
5511 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5512 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5515 tsec = coff_section_from_bfd_index (output_bfd,
5516 xcoff_data (output_bfd)->sntoc);
5519 irel->r_vaddr = (osec->vma
5520 + sec->output_offset
5521 + h->root.u.def.value
5523 irel->r_symndx = tsec->output_section->target_index;
5524 irel->r_type = R_POS;
5525 irel->r_size = reloc_size;
5526 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5527 ++osec->reloc_count;
5529 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5530 output_bfd, irel, tsec, NULL))
5534 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5536 BFD_ASSERT (outsym == flinfo->outsyms);
5541 && (flinfo->info->strip == strip_all
5542 || (flinfo->info->strip == strip_some
5543 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5544 FALSE, FALSE) == NULL)))
5546 BFD_ASSERT (outsym == flinfo->outsyms);
5551 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5553 BFD_ASSERT (outsym == flinfo->outsyms);
5557 memset (&aux, 0, sizeof aux);
5559 h->indx = obj_raw_syment_count (output_bfd);
5561 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->strtab, &isym,
5562 h->root.root.string);
5566 if (h->root.type == bfd_link_hash_undefined
5567 || h->root.type == bfd_link_hash_undefweak)
5570 isym.n_scnum = N_UNDEF;
5571 if (h->root.type == bfd_link_hash_undefweak
5572 && C_WEAKEXT == C_AIX_WEAKEXT)
5573 isym.n_sclass = C_WEAKEXT;
5575 isym.n_sclass = C_EXT;
5576 aux.x_csect.x_smtyp = XTY_ER;
5578 else if ((h->root.type == bfd_link_hash_defined
5579 || h->root.type == bfd_link_hash_defweak)
5580 && h->smclas == XMC_XO)
5582 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5583 isym.n_value = h->root.u.def.value;
5584 isym.n_scnum = N_UNDEF;
5585 if (h->root.type == bfd_link_hash_undefweak
5586 && C_WEAKEXT == C_AIX_WEAKEXT)
5587 isym.n_sclass = C_WEAKEXT;
5589 isym.n_sclass = C_EXT;
5590 aux.x_csect.x_smtyp = XTY_ER;
5592 else if (h->root.type == bfd_link_hash_defined
5593 || h->root.type == bfd_link_hash_defweak)
5595 struct xcoff_link_size_list *l;
5597 isym.n_value = (h->root.u.def.section->output_section->vma
5598 + h->root.u.def.section->output_offset
5599 + h->root.u.def.value);
5600 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5601 isym.n_scnum = N_ABS;
5603 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5604 isym.n_sclass = C_HIDEXT;
5605 aux.x_csect.x_smtyp = XTY_SD;
5607 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5609 for (l = xcoff_hash_table (flinfo->info)->size_list;
5615 aux.x_csect.x_scnlen.l = l->size;
5621 else if (h->root.type == bfd_link_hash_common)
5623 isym.n_value = (h->root.u.c.p->section->output_section->vma
5624 + h->root.u.c.p->section->output_offset);
5625 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5626 isym.n_sclass = C_EXT;
5627 aux.x_csect.x_smtyp = XTY_CM;
5628 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5633 isym.n_type = T_NULL;
5636 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5637 outsym += bfd_coff_symesz (output_bfd);
5639 aux.x_csect.x_smclas = h->smclas;
5640 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5642 outsym += bfd_coff_auxesz (output_bfd);
5644 if ((h->root.type == bfd_link_hash_defined
5645 || h->root.type == bfd_link_hash_defweak)
5646 && h->smclas != XMC_XO)
5648 /* We just output an SD symbol. Now output an LD symbol. */
5651 if (h->root.type == bfd_link_hash_undefweak
5652 && C_WEAKEXT == C_AIX_WEAKEXT)
5653 isym.n_sclass = C_WEAKEXT;
5655 isym.n_sclass = C_EXT;
5656 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5657 outsym += bfd_coff_symesz (output_bfd);
5659 aux.x_csect.x_smtyp = XTY_LD;
5660 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5661 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5663 outsym += bfd_coff_auxesz (output_bfd);
5666 pos = obj_sym_filepos (output_bfd);
5667 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5668 amt = outsym - flinfo->outsyms;
5669 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5670 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5672 obj_raw_syment_count (output_bfd) +=
5673 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5678 /* Handle a link order which is supposed to generate a reloc. */
5681 xcoff_reloc_link_order (bfd *output_bfd,
5682 struct xcoff_final_link_info *flinfo,
5683 asection *output_section,
5684 struct bfd_link_order *link_order)
5686 reloc_howto_type *howto;
5687 struct xcoff_link_hash_entry *h;
5691 struct internal_reloc *irel;
5692 struct xcoff_link_hash_entry **rel_hash_ptr;
5694 if (link_order->type == bfd_section_reloc_link_order)
5695 /* We need to somehow locate a symbol in the right section. The
5696 symbol must either have a value of zero, or we must adjust
5697 the addend by the value of the symbol. FIXME: Write this
5698 when we need it. The old linker couldn't handle this anyhow. */
5701 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5704 bfd_set_error (bfd_error_bad_value);
5708 h = ((struct xcoff_link_hash_entry *)
5709 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5710 link_order->u.reloc.p->u.name,
5711 FALSE, FALSE, TRUE));
5714 if (! ((*flinfo->info->callbacks->unattached_reloc)
5715 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5720 hsec = xcoff_symbol_section (h);
5721 if (h->root.type == bfd_link_hash_defined
5722 || h->root.type == bfd_link_hash_defweak)
5723 hval = h->root.u.def.value;
5727 addend = link_order->u.reloc.p->addend;
5729 addend += (hsec->output_section->vma
5730 + hsec->output_offset
5737 bfd_reloc_status_type rstat;
5740 size = bfd_get_reloc_size (howto);
5741 buf = bfd_zmalloc (size);
5745 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5751 case bfd_reloc_outofrange:
5753 case bfd_reloc_overflow:
5754 if (! ((*flinfo->info->callbacks->reloc_overflow)
5755 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5756 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5763 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5764 (file_ptr) link_order->offset, size);
5770 /* Store the reloc information in the right place. It will get
5771 swapped and written out at the end of the final_link routine. */
5772 irel = (flinfo->section_info[output_section->target_index].relocs
5773 + output_section->reloc_count);
5774 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5775 + output_section->reloc_count);
5777 memset (irel, 0, sizeof (struct internal_reloc));
5778 *rel_hash_ptr = NULL;
5780 irel->r_vaddr = output_section->vma + link_order->offset;
5783 irel->r_symndx = h->indx;
5786 /* Set the index to -2 to force this symbol to get written out. */
5792 irel->r_type = howto->type;
5793 irel->r_size = howto->bitsize - 1;
5794 if (howto->complain_on_overflow == complain_overflow_signed)
5795 irel->r_size |= 0x80;
5797 ++output_section->reloc_count;
5799 /* Now output the reloc to the .loader section. */
5800 if (xcoff_hash_table (flinfo->info)->loader_section)
5802 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5803 output_bfd, irel, hsec, h))
5810 /* Do the final link step. */
5813 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5815 bfd_size_type symesz;
5816 struct xcoff_final_link_info flinfo;
5818 struct bfd_link_order *p;
5819 bfd_size_type max_contents_size;
5820 bfd_size_type max_sym_count;
5821 bfd_size_type max_lineno_count;
5822 bfd_size_type max_reloc_count;
5823 bfd_size_type max_output_reloc_count;
5824 file_ptr rel_filepos;
5826 file_ptr line_filepos;
5827 unsigned int linesz;
5829 bfd_byte *external_relocs = NULL;
5830 char strbuf[STRING_SIZE_SIZE];
5835 abfd->flags |= DYNAMIC;
5837 symesz = bfd_coff_symesz (abfd);
5840 flinfo.output_bfd = abfd;
5841 flinfo.strtab = NULL;
5842 flinfo.section_info = NULL;
5843 flinfo.last_file_index = -1;
5844 flinfo.toc_symindx = -1;
5845 flinfo.internal_syms = NULL;
5846 flinfo.sym_indices = NULL;
5847 flinfo.outsyms = NULL;
5848 flinfo.linenos = NULL;
5849 flinfo.contents = NULL;
5850 flinfo.external_relocs = NULL;
5852 if (xcoff_hash_table (info)->loader_section)
5854 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5855 + bfd_xcoff_ldhdrsz (abfd));
5856 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5857 + bfd_xcoff_ldhdrsz (abfd)
5858 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5859 * bfd_xcoff_ldsymsz (abfd)));
5863 flinfo.ldsym = NULL;
5864 flinfo.ldrel = NULL;
5867 xcoff_data (abfd)->coff.link_info = info;
5869 flinfo.strtab = _bfd_stringtab_init ();
5870 if (flinfo.strtab == NULL)
5873 /* Count the relocation entries required for the output file.
5874 (We've already counted the line numbers.) Determine a few
5876 max_contents_size = 0;
5877 max_lineno_count = 0;
5878 max_reloc_count = 0;
5879 for (o = abfd->sections; o != NULL; o = o->next)
5882 for (p = o->map_head.link_order; p != NULL; p = p->next)
5884 if (p->type == bfd_indirect_link_order)
5888 sec = p->u.indirect.section;
5890 /* Mark all sections which are to be included in the
5891 link. This will normally be every section. We need
5892 to do this so that we can identify any sections which
5893 the linker has decided to not include. */
5894 sec->linker_mark = TRUE;
5896 o->reloc_count += sec->reloc_count;
5898 if ((sec->flags & SEC_IN_MEMORY) == 0)
5900 if (sec->rawsize > max_contents_size)
5901 max_contents_size = sec->rawsize;
5902 if (sec->size > max_contents_size)
5903 max_contents_size = sec->size;
5905 if (coff_section_data (sec->owner, sec) != NULL
5906 && xcoff_section_data (sec->owner, sec) != NULL
5907 && (xcoff_section_data (sec->owner, sec)->lineno_count
5908 > max_lineno_count))
5910 xcoff_section_data (sec->owner, sec)->lineno_count;
5911 if (sec->reloc_count > max_reloc_count)
5912 max_reloc_count = sec->reloc_count;
5914 else if (p->type == bfd_section_reloc_link_order
5915 || p->type == bfd_symbol_reloc_link_order)
5920 /* Compute the file positions for all the sections. */
5921 if (abfd->output_has_begun)
5923 if (xcoff_hash_table (info)->file_align != 0)
5930 file_align = xcoff_hash_table (info)->file_align;
5931 if (file_align != 0)
5933 bfd_boolean saw_contents;
5937 /* Insert .pad sections before every section which has
5938 contents and is loaded, if it is preceded by some other
5939 section which has contents and is loaded. */
5940 saw_contents = TRUE;
5941 for (o = abfd->sections; o != NULL; o = o->next)
5943 if (strcmp (o->name, ".pad") == 0)
5944 saw_contents = FALSE;
5945 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5946 && (o->flags & SEC_LOAD) != 0)
5949 saw_contents = TRUE;
5954 /* Create a pad section and place it before the section
5955 that needs padding. This requires unlinking and
5956 relinking the bfd's section list. */
5958 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5960 n->alignment_power = 0;
5962 bfd_section_list_remove (abfd, n);
5963 bfd_section_list_insert_before (abfd, o, n);
5964 saw_contents = FALSE;
5969 /* Reset the section indices after inserting the new
5972 for (o = abfd->sections; o != NULL; o = o->next)
5975 o->target_index = indx;
5977 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5979 /* Work out appropriate sizes for the .pad sections to force
5980 each section to land on a page boundary. This bit of
5981 code knows what compute_section_file_positions is going
5983 sofar = bfd_coff_filhsz (abfd);
5984 sofar += bfd_coff_aoutsz (abfd);
5985 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5986 for (o = abfd->sections; o != NULL; o = o->next)
5987 if ((bfd_xcoff_is_reloc_count_overflow
5988 (abfd, (bfd_vma) o->reloc_count))
5989 || (bfd_xcoff_is_lineno_count_overflow
5990 (abfd, (bfd_vma) o->lineno_count)))
5991 /* 64 does not overflow, need to check if 32 does */
5992 sofar += bfd_coff_scnhsz (abfd);
5994 for (o = abfd->sections; o != NULL; o = o->next)
5996 if (strcmp (o->name, ".pad") == 0)
6000 BFD_ASSERT (o->size == 0);
6001 pageoff = sofar & (file_align - 1);
6004 o->size = file_align - pageoff;
6005 sofar += file_align - pageoff;
6006 o->flags |= SEC_HAS_CONTENTS;
6011 if ((o->flags & SEC_HAS_CONTENTS) != 0)
6012 sofar += BFD_ALIGN (o->size,
6013 1 << o->alignment_power);
6018 if (! bfd_coff_compute_section_file_positions (abfd))
6022 /* Allocate space for the pointers we need to keep for the relocs. */
6026 /* We use section_count + 1, rather than section_count, because
6027 the target_index fields are 1 based. */
6028 amt = abfd->section_count + 1;
6029 amt *= sizeof (struct xcoff_link_section_info);
6030 flinfo.section_info = bfd_malloc (amt);
6031 if (flinfo.section_info == NULL)
6033 for (i = 0; i <= abfd->section_count; i++)
6035 flinfo.section_info[i].relocs = NULL;
6036 flinfo.section_info[i].rel_hashes = NULL;
6037 flinfo.section_info[i].toc_rel_hashes = NULL;
6041 /* Set the file positions for the relocs. */
6042 rel_filepos = obj_relocbase (abfd);
6043 relsz = bfd_coff_relsz (abfd);
6044 max_output_reloc_count = 0;
6045 for (o = abfd->sections; o != NULL; o = o->next)
6047 if (o->reloc_count == 0)
6051 /* A stripped file has no relocs. However, we still
6052 allocate the buffers, so that later code doesn't have to
6053 worry about whether we are stripping or not. */
6054 if (info->strip == strip_all)
6058 o->flags |= SEC_RELOC;
6059 o->rel_filepos = rel_filepos;
6060 rel_filepos += o->reloc_count * relsz;
6063 /* We don't know the indices of global symbols until we have
6064 written out all the local symbols. For each section in
6065 the output file, we keep an array of pointers to hash
6066 table entries. Each entry in the array corresponds to a
6067 reloc. When we find a reloc against a global symbol, we
6068 set the corresponding entry in this array so that we can
6069 fix up the symbol index after we have written out all the
6072 Because of this problem, we also keep the relocs in
6073 memory until the end of the link. This wastes memory.
6074 We could backpatch the file later, I suppose, although it
6076 amt = o->reloc_count;
6077 amt *= sizeof (struct internal_reloc);
6078 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6080 amt = o->reloc_count;
6081 amt *= sizeof (struct xcoff_link_hash_entry *);
6082 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6084 if (flinfo.section_info[o->target_index].relocs == NULL
6085 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6088 if (o->reloc_count > max_output_reloc_count)
6089 max_output_reloc_count = o->reloc_count;
6093 /* We now know the size of the relocs, so we can determine the file
6094 positions of the line numbers. */
6095 line_filepos = rel_filepos;
6096 flinfo.line_filepos = line_filepos;
6097 linesz = bfd_coff_linesz (abfd);
6098 for (o = abfd->sections; o != NULL; o = o->next)
6100 if (o->lineno_count == 0)
6101 o->line_filepos = 0;
6104 o->line_filepos = line_filepos;
6105 line_filepos += o->lineno_count * linesz;
6108 /* Reset the reloc and lineno counts, so that we can use them to
6109 count the number of entries we have output so far. */
6111 o->lineno_count = 0;
6114 obj_sym_filepos (abfd) = line_filepos;
6116 /* Figure out the largest number of symbols in an input BFD. Take
6117 the opportunity to clear the output_has_begun fields of all the
6118 input BFD's. We want at least 6 symbols, since that is the
6119 number which xcoff_write_global_symbol may need. */
6121 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
6125 sub->output_has_begun = FALSE;
6126 sz = obj_raw_syment_count (sub);
6127 if (sz > max_sym_count)
6131 /* Allocate some buffers used while linking. */
6132 amt = max_sym_count * sizeof (struct internal_syment);
6133 flinfo.internal_syms = bfd_malloc (amt);
6135 amt = max_sym_count * sizeof (long);
6136 flinfo.sym_indices = bfd_malloc (amt);
6138 amt = (max_sym_count + 1) * symesz;
6139 flinfo.outsyms = bfd_malloc (amt);
6141 amt = max_lineno_count * bfd_coff_linesz (abfd);
6142 flinfo.linenos = bfd_malloc (amt);
6144 amt = max_contents_size;
6145 flinfo.contents = bfd_malloc (amt);
6147 amt = max_reloc_count * relsz;
6148 flinfo.external_relocs = bfd_malloc (amt);
6150 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6151 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6152 || flinfo.outsyms == NULL
6153 || (flinfo.linenos == NULL && max_lineno_count > 0)
6154 || (flinfo.contents == NULL && max_contents_size > 0)
6155 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6158 obj_raw_syment_count (abfd) = 0;
6160 /* Find a TOC symbol, if we need one. */
6161 if (!xcoff_find_tc0 (abfd, &flinfo))
6164 /* We now know the position of everything in the file, except that
6165 we don't know the size of the symbol table and therefore we don't
6166 know where the string table starts. We just build the string
6167 table in memory as we go along. We process all the relocations
6168 for a single input file at once. */
6169 for (o = abfd->sections; o != NULL; o = o->next)
6171 for (p = o->map_head.link_order; p != NULL; p = p->next)
6173 if (p->type == bfd_indirect_link_order
6174 && p->u.indirect.section->owner->xvec == abfd->xvec)
6176 sub = p->u.indirect.section->owner;
6177 if (! sub->output_has_begun)
6179 if (! xcoff_link_input_bfd (&flinfo, sub))
6181 sub->output_has_begun = TRUE;
6184 else if (p->type == bfd_section_reloc_link_order
6185 || p->type == bfd_symbol_reloc_link_order)
6187 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6192 if (! _bfd_default_link_order (abfd, info, o, p))
6198 /* Free up the buffers used by xcoff_link_input_bfd. */
6199 if (flinfo.internal_syms != NULL)
6201 free (flinfo.internal_syms);
6202 flinfo.internal_syms = NULL;
6204 if (flinfo.sym_indices != NULL)
6206 free (flinfo.sym_indices);
6207 flinfo.sym_indices = NULL;
6209 if (flinfo.linenos != NULL)
6211 free (flinfo.linenos);
6212 flinfo.linenos = NULL;
6214 if (flinfo.contents != NULL)
6216 free (flinfo.contents);
6217 flinfo.contents = NULL;
6219 if (flinfo.external_relocs != NULL)
6221 free (flinfo.external_relocs);
6222 flinfo.external_relocs = NULL;
6225 /* The value of the last C_FILE symbol is supposed to be -1. Write
6227 if (flinfo.last_file_index != -1)
6229 flinfo.last_file.n_value = -(bfd_vma) 1;
6230 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
6231 (void *) flinfo.outsyms);
6232 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
6233 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6234 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
6238 /* Write out all the global symbols which do not come from XCOFF
6240 xcoff_link_hash_traverse (xcoff_hash_table (info),
6241 xcoff_write_global_symbol,
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++, erel += relsz)
6282 if (*rel_hash != NULL)
6284 if ((*rel_hash)->indx < 0)
6286 if (! ((*info->callbacks->unattached_reloc)
6287 (info, (*rel_hash)->root.root.string,
6288 NULL, o, irel->r_vaddr)))
6290 (*rel_hash)->indx = 0;
6292 irel->r_symndx = (*rel_hash)->indx;
6296 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6297 toc_rel_hash != NULL;
6298 toc_rel_hash = toc_rel_hash->next)
6300 if (toc_rel_hash->h->u.toc_indx < 0)
6302 if (! ((*info->callbacks->unattached_reloc)
6303 (info, toc_rel_hash->h->root.root.string,
6304 NULL, o, toc_rel_hash->rel->r_vaddr)))
6306 toc_rel_hash->h->u.toc_indx = 0;
6308 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6311 /* XCOFF requires that the relocs be sorted by address. We tend
6312 to produce them in the order in which their containing csects
6313 appear in the symbol table, which is not necessarily by
6314 address. So we sort them here. There may be a better way to
6316 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6317 o->reloc_count, sizeof (struct internal_reloc),
6320 irel = flinfo.section_info[o->target_index].relocs;
6321 irelend = irel + o->reloc_count;
6322 erel = external_relocs;
6323 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6324 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6326 rel_size = relsz * o->reloc_count;
6327 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6328 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6332 if (external_relocs != NULL)
6334 free (external_relocs);
6335 external_relocs = NULL;
6338 /* Free up the section information. */
6339 if (flinfo.section_info != NULL)
6343 for (i = 0; i < abfd->section_count; i++)
6345 if (flinfo.section_info[i].relocs != NULL)
6346 free (flinfo.section_info[i].relocs);
6347 if (flinfo.section_info[i].rel_hashes != NULL)
6348 free (flinfo.section_info[i].rel_hashes);
6350 free (flinfo.section_info);
6351 flinfo.section_info = NULL;
6354 /* Write out the loader section contents. */
6355 o = xcoff_hash_table (info)->loader_section;
6358 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
6359 == (xcoff_hash_table (info)->loader_section->contents
6360 + xcoff_hash_table (info)->ldhdr.l_impoff));
6361 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6362 (file_ptr) o->output_offset, o->size))
6366 /* Write out the magic sections. */
6367 o = xcoff_hash_table (info)->linkage_section;
6369 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6370 (file_ptr) o->output_offset,
6373 o = xcoff_hash_table (info)->toc_section;
6375 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6376 (file_ptr) o->output_offset,
6379 o = xcoff_hash_table (info)->descriptor_section;
6381 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6382 (file_ptr) o->output_offset,
6386 /* Write out the string table. */
6387 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6388 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6391 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6393 amt = STRING_SIZE_SIZE;
6394 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6396 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6399 _bfd_stringtab_free (flinfo.strtab);
6401 /* Write out the debugging string table. */
6402 o = xcoff_hash_table (info)->debug_section;
6405 struct bfd_strtab_hash *debug_strtab;
6407 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6408 BFD_ASSERT (o->output_section->size - o->output_offset
6409 >= _bfd_stringtab_size (debug_strtab));
6410 pos = o->output_section->filepos + o->output_offset;
6411 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6413 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6417 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6418 not try to write out the symbols. */
6419 bfd_get_symcount (abfd) = 0;
6424 if (flinfo.strtab != NULL)
6425 _bfd_stringtab_free (flinfo.strtab);
6427 if (flinfo.section_info != NULL)
6431 for (i = 0; i < abfd->section_count; i++)
6433 if (flinfo.section_info[i].relocs != NULL)
6434 free (flinfo.section_info[i].relocs);
6435 if (flinfo.section_info[i].rel_hashes != NULL)
6436 free (flinfo.section_info[i].rel_hashes);
6438 free (flinfo.section_info);
6441 if (flinfo.internal_syms != NULL)
6442 free (flinfo.internal_syms);
6443 if (flinfo.sym_indices != NULL)
6444 free (flinfo.sym_indices);
6445 if (flinfo.outsyms != NULL)
6446 free (flinfo.outsyms);
6447 if (flinfo.linenos != NULL)
6448 free (flinfo.linenos);
6449 if (flinfo.contents != NULL)
6450 free (flinfo.contents);
6451 if (flinfo.external_relocs != NULL)
6452 free (flinfo.external_relocs);
6453 if (external_relocs != NULL)
6454 free (external_relocs);