1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
32 /* This file holds the XCOFF linker code. */
34 #undef STRING_SIZE_SIZE
35 #define STRING_SIZE_SIZE 4
37 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
38 This flag will only be used on input sections. */
40 #define SEC_MARK (SEC_ROM)
42 /* The list of import files. */
44 struct xcoff_import_file
46 /* The next entry in the list. */
47 struct xcoff_import_file *next;
52 /* The member name. */
56 /* Information we keep for each section in the output file during the
59 struct xcoff_link_section_info
61 /* The relocs to be output. */
62 struct internal_reloc *relocs;
63 /* For each reloc against a global symbol whose index was not known
64 when the reloc was handled, the global hash table entry. */
65 struct xcoff_link_hash_entry **rel_hashes;
66 /* If there is a TOC relative reloc against a global symbol, and the
67 index of the TOC symbol is not known when the reloc was handled,
68 an entry is added to this linked list. This is not an array,
69 like rel_hashes, because this case is quite uncommon. */
70 struct xcoff_toc_rel_hash
72 struct xcoff_toc_rel_hash *next;
73 struct xcoff_link_hash_entry *h;
74 struct internal_reloc *rel;
78 /* Information that we pass around while doing the final link step. */
80 struct xcoff_final_link_info
82 /* General link information. */
83 struct bfd_link_info *info;
86 /* Hash table for long symbol names. */
87 struct bfd_strtab_hash *strtab;
88 /* Array of information kept for each output section, indexed by the
89 target_index field. */
90 struct xcoff_link_section_info *section_info;
91 /* Symbol index of last C_FILE symbol (-1 if none). */
93 /* Contents of last C_FILE symbol. */
94 struct internal_syment last_file;
95 /* Symbol index of TOC symbol. */
97 /* Start of .loader symbols. */
99 /* Next .loader reloc to swap out. */
101 /* File position of start of line numbers. */
102 file_ptr line_filepos;
103 /* Buffer large enough to hold swapped symbols of any input file. */
104 struct internal_syment *internal_syms;
105 /* Buffer large enough to hold output indices of symbols of any
108 /* Buffer large enough to hold output symbols for any input file. */
110 /* Buffer large enough to hold external line numbers for any input
113 /* Buffer large enough to hold any input section. */
115 /* Buffer large enough to hold external relocs of any input section. */
116 bfd_byte *external_relocs;
119 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
123 /* Routines to read XCOFF dynamic information. This don't really
124 belong here, but we already have the ldsym manipulation routines
127 /* Read the contents of a section. */
130 xcoff_get_section_contents (bfd *abfd, asection *sec)
132 if (coff_section_data (abfd, sec) == NULL)
134 bfd_size_type amt = sizeof (struct coff_section_tdata);
136 sec->used_by_bfd = bfd_zalloc (abfd, amt);
137 if (sec->used_by_bfd == NULL)
141 if (coff_section_data (abfd, sec)->contents == NULL)
145 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
147 if (contents != NULL)
151 coff_section_data (abfd, sec)->contents = contents;
157 /* Get the size required to hold the dynamic symbols. */
160 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
164 struct internal_ldhdr ldhdr;
166 if ((abfd->flags & DYNAMIC) == 0)
168 bfd_set_error (bfd_error_invalid_operation);
172 lsec = bfd_get_section_by_name (abfd, ".loader");
175 bfd_set_error (bfd_error_no_symbols);
179 if (! xcoff_get_section_contents (abfd, lsec))
181 contents = coff_section_data (abfd, lsec)->contents;
183 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
185 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
188 /* Get the dynamic symbols. */
191 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
195 struct internal_ldhdr ldhdr;
197 bfd_byte *elsym, *elsymend;
198 coff_symbol_type *symbuf;
200 if ((abfd->flags & DYNAMIC) == 0)
202 bfd_set_error (bfd_error_invalid_operation);
206 lsec = bfd_get_section_by_name (abfd, ".loader");
209 bfd_set_error (bfd_error_no_symbols);
213 if (! xcoff_get_section_contents (abfd, lsec))
215 contents = coff_section_data (abfd, lsec)->contents;
217 coff_section_data (abfd, lsec)->keep_contents = TRUE;
219 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
221 strings = (char *) contents + ldhdr.l_stoff;
223 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
227 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
229 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
230 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
232 struct internal_ldsym ldsym;
234 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
236 symbuf->symbol.the_bfd = abfd;
238 if (ldsym._l._l_l._l_zeroes == 0)
239 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
244 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
247 memcpy (c, ldsym._l._l_name, SYMNMLEN);
249 symbuf->symbol.name = c;
252 if (ldsym.l_smclas == XMC_XO)
253 symbuf->symbol.section = bfd_abs_section_ptr;
255 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
257 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
259 symbuf->symbol.flags = BSF_NO_FLAGS;
260 if ((ldsym.l_smtype & L_EXPORT) != 0)
261 symbuf->symbol.flags |= BSF_GLOBAL;
263 /* FIXME: We have no way to record the other information stored
264 with the loader symbol. */
265 *psyms = (asymbol *) symbuf;
270 return ldhdr.l_nsyms;
273 /* Get the size required to hold the dynamic relocs. */
276 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
280 struct internal_ldhdr ldhdr;
282 if ((abfd->flags & DYNAMIC) == 0)
284 bfd_set_error (bfd_error_invalid_operation);
288 lsec = bfd_get_section_by_name (abfd, ".loader");
291 bfd_set_error (bfd_error_no_symbols);
295 if (! xcoff_get_section_contents (abfd, lsec))
297 contents = coff_section_data (abfd, lsec)->contents;
299 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
301 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
304 /* Get the dynamic relocs. */
307 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
313 struct internal_ldhdr ldhdr;
315 bfd_byte *elrel, *elrelend;
317 if ((abfd->flags & DYNAMIC) == 0)
319 bfd_set_error (bfd_error_invalid_operation);
323 lsec = bfd_get_section_by_name (abfd, ".loader");
326 bfd_set_error (bfd_error_no_symbols);
330 if (! xcoff_get_section_contents (abfd, lsec))
332 contents = coff_section_data (abfd, lsec)->contents;
334 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
336 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
340 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
342 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
343 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
346 struct internal_ldrel ldrel;
348 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
350 if (ldrel.l_symndx >= 3)
351 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
357 switch (ldrel.l_symndx)
373 sec = bfd_get_section_by_name (abfd, name);
376 bfd_set_error (bfd_error_bad_value);
380 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
383 relbuf->address = ldrel.l_vaddr;
386 /* Most dynamic relocs have the same type. FIXME: This is only
387 correct if ldrel.l_rtype == 0. In other cases, we should use
388 a different howto. */
389 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
391 /* FIXME: We have no way to record the l_rsecnm field. */
398 return ldhdr.l_nreloc;
401 /* Routine to create an entry in an XCOFF link hash table. */
403 static struct bfd_hash_entry *
404 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
405 struct bfd_hash_table *table,
408 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
410 /* Allocate the structure if it has not already been allocated by a
413 ret = bfd_hash_allocate (table, sizeof (* ret));
417 /* Call the allocation method of the superclass. */
418 ret = ((struct xcoff_link_hash_entry *)
419 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
423 /* Set local fields. */
425 ret->toc_section = NULL;
426 ret->u.toc_indx = -1;
427 ret->descriptor = NULL;
431 ret->smclas = XMC_UA;
434 return (struct bfd_hash_entry *) ret;
437 /* Create a XCOFF link hash table. */
439 struct bfd_link_hash_table *
440 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
442 struct xcoff_link_hash_table *ret;
443 bfd_size_type amt = sizeof (* ret);
445 ret = bfd_malloc (amt);
448 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
449 sizeof (struct xcoff_link_hash_entry)))
455 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
456 ret->debug_section = NULL;
457 ret->loader_section = NULL;
458 ret->ldrel_count = 0;
459 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
460 ret->linkage_section = NULL;
461 ret->toc_section = NULL;
462 ret->descriptor_section = NULL;
467 memset (ret->special_sections, 0, sizeof ret->special_sections);
469 /* The linker will always generate a full a.out header. We need to
470 record that fact now, before the sizeof_headers routine could be
472 xcoff_data (abfd)->full_aouthdr = TRUE;
477 /* Free a XCOFF link hash table. */
480 _bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
482 struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
484 _bfd_stringtab_free (ret->debug_strtab);
485 bfd_hash_table_free (&ret->root.table);
489 /* Read internal relocs for an XCOFF csect. This is a wrapper around
490 _bfd_coff_read_internal_relocs which tries to take advantage of any
491 relocs which may have been cached for the enclosing section. */
493 static struct internal_reloc *
494 xcoff_read_internal_relocs (bfd *abfd,
497 bfd_byte *external_relocs,
498 bfd_boolean require_internal,
499 struct internal_reloc *internal_relocs)
501 if (coff_section_data (abfd, sec) != NULL
502 && coff_section_data (abfd, sec)->relocs == NULL
503 && xcoff_section_data (abfd, sec) != NULL)
507 enclosing = xcoff_section_data (abfd, sec)->enclosing;
509 if (enclosing != NULL
510 && (coff_section_data (abfd, enclosing) == NULL
511 || coff_section_data (abfd, enclosing)->relocs == NULL)
513 && enclosing->reloc_count > 0)
515 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
516 external_relocs, FALSE, NULL)
521 if (enclosing != NULL
522 && coff_section_data (abfd, enclosing) != NULL
523 && coff_section_data (abfd, enclosing)->relocs != NULL)
527 off = ((sec->rel_filepos - enclosing->rel_filepos)
528 / bfd_coff_relsz (abfd));
530 if (! require_internal)
531 return coff_section_data (abfd, enclosing)->relocs + off;
532 memcpy (internal_relocs,
533 coff_section_data (abfd, enclosing)->relocs + off,
534 sec->reloc_count * sizeof (struct internal_reloc));
535 return internal_relocs;
539 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
540 require_internal, internal_relocs);
543 /* This function is used to add symbols from a dynamic object to the
544 global symbol table. */
547 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
551 struct internal_ldhdr ldhdr;
553 bfd_byte *elsym, *elsymend;
554 struct xcoff_import_file *n;
559 struct xcoff_import_file **pp;
561 /* We can only handle a dynamic object if we are generating an XCOFF
563 if (info->output_bfd->xvec != abfd->xvec)
565 (*_bfd_error_handler)
566 (_("%s: XCOFF shared object when not producing XCOFF output"),
567 bfd_get_filename (abfd));
568 bfd_set_error (bfd_error_invalid_operation);
572 /* The symbols we use from a dynamic object are not the symbols in
573 the normal symbol table, but, rather, the symbols in the export
574 table. If there is a global symbol in a dynamic object which is
575 not in the export table, the loader will not be able to find it,
576 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
577 libc.a has symbols in the export table which are not in the
580 /* Read in the .loader section. FIXME: We should really use the
581 o_snloader field in the a.out header, rather than grabbing the
583 lsec = bfd_get_section_by_name (abfd, ".loader");
586 (*_bfd_error_handler)
587 (_("%s: dynamic object with no .loader section"),
588 bfd_get_filename (abfd));
589 bfd_set_error (bfd_error_no_symbols);
593 if (! xcoff_get_section_contents (abfd, lsec))
595 contents = coff_section_data (abfd, lsec)->contents;
597 /* Remove the sections from this object, so that they do not get
598 included in the link. */
599 bfd_section_list_clear (abfd);
601 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
603 strings = (char *) contents + ldhdr.l_stoff;
605 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
607 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
609 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
611 struct internal_ldsym ldsym;
612 char nambuf[SYMNMLEN + 1];
614 struct xcoff_link_hash_entry *h;
616 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
618 /* We are only interested in exported symbols. */
619 if ((ldsym.l_smtype & L_EXPORT) == 0)
622 if (ldsym._l._l_l._l_zeroes == 0)
623 name = strings + ldsym._l._l_l._l_offset;
626 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
627 nambuf[SYMNMLEN] = '\0';
631 /* Normally we could not call xcoff_link_hash_lookup in an add
632 symbols routine, since we might not be using an XCOFF hash
633 table. However, we verified above that we are using an XCOFF
636 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
641 h->flags |= XCOFF_DEF_DYNAMIC;
643 /* If the symbol is undefined, and the BFD it was found in is
644 not a dynamic object, change the BFD to this dynamic object,
645 so that we can get the correct import file ID. */
646 if ((h->root.type == bfd_link_hash_undefined
647 || h->root.type == bfd_link_hash_undefweak)
648 && (h->root.u.undef.abfd == NULL
649 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
650 h->root.u.undef.abfd = abfd;
652 if (h->root.type == bfd_link_hash_new)
654 h->root.type = bfd_link_hash_undefined;
655 h->root.u.undef.abfd = abfd;
656 /* We do not want to add this to the undefined symbol list. */
659 if (h->smclas == XMC_UA
660 || h->root.type == bfd_link_hash_undefined
661 || h->root.type == bfd_link_hash_undefweak)
662 h->smclas = ldsym.l_smclas;
664 /* Unless this is an XMC_XO symbol, we don't bother to actually
665 define it, since we don't have a section to put it in anyhow.
666 Instead, the relocation routines handle the DEF_DYNAMIC flag
669 if (h->smclas == XMC_XO
670 && (h->root.type == bfd_link_hash_undefined
671 || h->root.type == bfd_link_hash_undefweak))
673 /* This symbol has an absolute value. */
674 h->root.type = bfd_link_hash_defined;
675 h->root.u.def.section = bfd_abs_section_ptr;
676 h->root.u.def.value = ldsym.l_value;
679 /* If this symbol defines a function descriptor, then it
680 implicitly defines the function code as well. */
681 if (h->smclas == XMC_DS
682 || (h->smclas == XMC_XO && name[0] != '.'))
683 h->flags |= XCOFF_DESCRIPTOR;
684 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
686 struct xcoff_link_hash_entry *hds;
693 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
697 strcpy (dsnm + 1, name);
698 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
704 if (hds->root.type == bfd_link_hash_new)
706 hds->root.type = bfd_link_hash_undefined;
707 hds->root.u.undef.abfd = abfd;
708 /* We do not want to add this to the undefined
716 hds->flags |= XCOFF_DEF_DYNAMIC;
717 if (hds->smclas == XMC_UA)
718 hds->smclas = XMC_PR;
720 /* An absolute symbol appears to actually define code, not a
721 function descriptor. This is how some math functions are
722 implemented on AIX 4.1. */
723 if (h->smclas == XMC_XO
724 && (hds->root.type == bfd_link_hash_undefined
725 || hds->root.type == bfd_link_hash_undefweak))
727 hds->smclas = XMC_XO;
728 hds->root.type = bfd_link_hash_defined;
729 hds->root.u.def.section = bfd_abs_section_ptr;
730 hds->root.u.def.value = ldsym.l_value;
735 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
737 free (coff_section_data (abfd, lsec)->contents);
738 coff_section_data (abfd, lsec)->contents = NULL;
741 /* Record this file in the import files. */
742 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
747 /* For some reason, the path entry in the import file list for a
748 shared object appears to always be empty. The file name is the
751 if (abfd->my_archive == NULL)
753 bname = bfd_get_filename (abfd);
758 bname = bfd_get_filename (abfd->my_archive);
759 mname = bfd_get_filename (abfd);
761 s = strrchr (bname, '/');
767 /* We start c at 1 because the first import file number is reserved
769 for (pp = &xcoff_hash_table (info)->imports, c = 1;
771 pp = &(*pp)->next, ++c)
775 xcoff_data (abfd)->import_file_id = c;
780 /* xcoff_link_create_extra_sections
782 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
785 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
787 bfd_boolean return_value = FALSE;
789 if (info->output_bfd->xvec == abfd->xvec)
791 /* We need to build a .loader section, so we do it here. This
792 won't work if we're producing an XCOFF output file with no
793 XCOFF input files. FIXME. */
795 if (xcoff_hash_table (info)->loader_section == NULL)
798 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
800 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
804 xcoff_hash_table (info)->loader_section = lsec;
807 /* Likewise for the linkage section. */
808 if (xcoff_hash_table (info)->linkage_section == NULL)
811 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
814 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
818 xcoff_hash_table (info)->linkage_section = lsec;
819 lsec->alignment_power = 2;
822 /* Likewise for the TOC section. */
823 if (xcoff_hash_table (info)->toc_section == NULL)
826 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
829 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
833 xcoff_hash_table (info)->toc_section = tsec;
834 tsec->alignment_power = 2;
837 /* Likewise for the descriptor section. */
838 if (xcoff_hash_table (info)->descriptor_section == NULL)
841 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
844 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
848 xcoff_hash_table (info)->descriptor_section = dsec;
849 dsec->alignment_power = 2;
852 /* Likewise for the .debug section. */
853 if (xcoff_hash_table (info)->debug_section == NULL
854 && info->strip != strip_all)
857 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
859 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
863 xcoff_hash_table (info)->debug_section = dsec;
874 /* Returns the index of reloc in RELOCS with the least address greater
875 than or equal to ADDRESS. The relocs are sorted by address. */
878 xcoff_find_reloc (struct internal_reloc *relocs,
882 bfd_size_type min, max, this;
886 if (count == 1 && relocs[0].r_vaddr < address)
895 /* Do a binary search over (min,max]. */
896 while (min + 1 < max)
900 this = (max + min) / 2;
901 raddr = relocs[this].r_vaddr;
904 else if (raddr < address)
913 if (relocs[min].r_vaddr < address)
917 && relocs[min - 1].r_vaddr == address)
923 /* Add all the symbols from an object file to the hash table.
925 XCOFF is a weird format. A normal XCOFF .o files will have three
926 COFF sections--.text, .data, and .bss--but each COFF section will
927 contain many csects. These csects are described in the symbol
928 table. From the linker's point of view, each csect must be
929 considered a section in its own right. For example, a TOC entry is
930 handled as a small XMC_TC csect. The linker must be able to merge
931 different TOC entries together, which means that it must be able to
932 extract the XMC_TC csects from the .data section of the input .o
935 From the point of view of our linker, this is, of course, a hideous
936 nightmare. We cope by actually creating sections for each csect,
937 and discarding the original sections. We then have to handle the
938 relocation entries carefully, since the only way to tell which
939 csect they belong to is to examine the address. */
942 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
944 unsigned int n_tmask;
945 unsigned int n_btshft;
946 bfd_boolean default_copy;
947 bfd_size_type symcount;
948 struct xcoff_link_hash_entry **sym_hash;
949 asection **csect_cache;
950 bfd_size_type linesz;
953 bfd_boolean keep_syms;
955 unsigned int csect_index;
956 asection *first_csect;
957 bfd_size_type symesz;
960 struct reloc_info_struct
962 struct internal_reloc *relocs;
965 } *reloc_info = NULL;
968 keep_syms = obj_coff_keep_syms (abfd);
970 if ((abfd->flags & DYNAMIC) != 0
971 && ! info->static_link)
973 if (! xcoff_link_add_dynamic_symbols (abfd, info))
977 /* Create the loader, toc, gl, ds and debug sections, if needed. */
978 if (! xcoff_link_create_extra_sections (abfd, info))
981 if ((abfd->flags & DYNAMIC) != 0
982 && ! info->static_link)
985 n_tmask = coff_data (abfd)->local_n_tmask;
986 n_btshft = coff_data (abfd)->local_n_btshft;
988 /* Define macros so that ISFCN, et. al., macros work correctly. */
989 #define N_TMASK n_tmask
990 #define N_BTSHFT n_btshft
992 if (info->keep_memory)
993 default_copy = FALSE;
997 symcount = obj_raw_syment_count (abfd);
999 /* We keep a list of the linker hash table entries that correspond
1000 to each external symbol. */
1001 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1002 sym_hash = bfd_zalloc (abfd, amt);
1003 if (sym_hash == NULL && symcount != 0)
1005 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1007 /* Because of the weird stuff we are doing with XCOFF csects, we can
1008 not easily determine which section a symbol is in, so we store
1009 the information in the tdata for the input file. */
1010 amt = symcount * sizeof (asection *);
1011 csect_cache = bfd_zalloc (abfd, amt);
1012 if (csect_cache == NULL && symcount != 0)
1014 xcoff_data (abfd)->csects = csect_cache;
1016 /* While splitting sections into csects, we need to assign the
1017 relocs correctly. The relocs and the csects must both be in
1018 order by VMA within a given section, so we handle this by
1019 scanning along the relocs as we process the csects. We index
1020 into reloc_info using the section target_index. */
1021 amt = abfd->section_count + 1;
1022 amt *= sizeof (struct reloc_info_struct);
1023 reloc_info = bfd_zmalloc (amt);
1024 if (reloc_info == NULL)
1027 /* Read in the relocs and line numbers for each section. */
1028 linesz = bfd_coff_linesz (abfd);
1030 for (o = abfd->sections; o != NULL; o = o->next)
1034 if ((o->flags & SEC_RELOC) != 0)
1036 reloc_info[o->target_index].relocs =
1037 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1038 amt = o->reloc_count;
1039 amt *= sizeof (asection *);
1040 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1041 if (reloc_info[o->target_index].csects == NULL)
1045 if ((info->strip == strip_none || info->strip == strip_some)
1046 && o->lineno_count > 0)
1050 amt = linesz * o->lineno_count;
1051 linenos = bfd_malloc (amt);
1052 if (linenos == NULL)
1054 reloc_info[o->target_index].linenos = linenos;
1055 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1056 || bfd_bread (linenos, amt, abfd) != amt)
1061 /* Don't let the linker relocation routines discard the symbols. */
1062 obj_coff_keep_syms (abfd) = TRUE;
1068 symesz = bfd_coff_symesz (abfd);
1069 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1070 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1071 esym_end = esym + symcount * symesz;
1073 while (esym < esym_end)
1075 struct internal_syment sym;
1076 union internal_auxent aux;
1078 char buf[SYMNMLEN + 1];
1083 struct xcoff_link_hash_entry *set_toc;
1085 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1087 /* In this pass we are only interested in symbols with csect
1089 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1092 Normally csect is a .pr, .rw etc. created in the loop
1093 If C_FILE or first time, handle special
1095 Advance esym, sym_hash, csect_hash ptr's
1096 Keep track of the last_symndx for the current file. */
1097 if (sym.n_sclass == C_FILE && csect != NULL)
1099 xcoff_section_data (abfd, csect)->last_symndx =
1101 - (bfd_byte *) obj_coff_external_syms (abfd))
1107 *csect_cache = csect;
1108 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1109 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1111 *csect_cache = NULL;
1112 esym += (sym.n_numaux + 1) * symesz;
1113 sym_hash += sym.n_numaux + 1;
1114 csect_cache += sym.n_numaux + 1;
1119 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1124 /* If this symbol has line number information attached to it,
1125 and we're not stripping it, count the number of entries and
1126 add them to the count for this csect. In the final link pass
1127 we are going to attach line number information by symbol,
1128 rather than by section, in order to more easily handle
1129 garbage collection. */
1130 if ((info->strip == strip_none || info->strip == strip_some)
1133 && ISFCN (sym.n_type))
1135 union internal_auxent auxlin;
1137 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1138 sym.n_type, sym.n_sclass,
1139 0, sym.n_numaux, (void *) &auxlin);
1141 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1143 asection *enclosing;
1144 bfd_signed_vma linoff;
1146 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1147 if (enclosing == NULL)
1149 (*_bfd_error_handler)
1150 (_("%B: `%s' has line numbers but no enclosing section"),
1152 bfd_set_error (bfd_error_bad_value);
1155 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1156 - enclosing->line_filepos);
1157 /* Explicit cast to bfd_signed_vma for compiler. */
1158 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1160 struct internal_lineno lin;
1161 bfd_byte *linpstart;
1163 linpstart = (reloc_info[enclosing->target_index].linenos
1165 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1167 && ((bfd_size_type) lin.l_addr.l_symndx
1169 - (bfd_byte *) obj_coff_external_syms (abfd))
1172 bfd_byte *linpend, *linp;
1174 linpend = (reloc_info[enclosing->target_index].linenos
1175 + enclosing->lineno_count * linesz);
1176 for (linp = linpstart + linesz;
1180 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1182 if (lin.l_lnno == 0)
1185 csect->lineno_count += (linp - linpstart) / linesz;
1186 /* The setting of line_filepos will only be
1187 useful if all the line number entries for a
1188 csect are contiguous; this only matters for
1190 if (csect->line_filepos == 0)
1191 csect->line_filepos =
1192 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1198 /* Pick up the csect auxiliary information. */
1199 if (sym.n_numaux == 0)
1201 (*_bfd_error_handler)
1202 (_("%B: class %d symbol `%s' has no aux entries"),
1203 abfd, sym.n_sclass, name);
1204 bfd_set_error (bfd_error_bad_value);
1208 bfd_coff_swap_aux_in (abfd,
1209 (void *) (esym + symesz * sym.n_numaux),
1210 sym.n_type, sym.n_sclass,
1211 sym.n_numaux - 1, sym.n_numaux,
1214 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1224 (*_bfd_error_handler)
1225 (_("%B: symbol `%s' has unrecognized csect type %d"),
1227 bfd_set_error (bfd_error_bad_value);
1231 /* This is an external reference. */
1232 if (sym.n_sclass == C_HIDEXT
1233 || sym.n_scnum != N_UNDEF
1234 || aux.x_csect.x_scnlen.l != 0)
1236 (*_bfd_error_handler)
1237 (_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1238 abfd, name, sym.n_sclass, sym.n_scnum,
1239 aux.x_csect.x_scnlen.l);
1240 bfd_set_error (bfd_error_bad_value);
1244 /* An XMC_XO external reference is actually a reference to
1245 an absolute location. */
1246 if (aux.x_csect.x_smclas != XMC_XO)
1247 section = bfd_und_section_ptr;
1250 section = bfd_abs_section_ptr;
1251 value = sym.n_value;
1256 /* This is a csect definition. */
1259 xcoff_section_data (abfd, csect)->last_symndx =
1260 ((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz);
1264 csect_index = -(unsigned) 1;
1266 /* When we see a TOC anchor, we record the TOC value. */
1267 if (aux.x_csect.x_smclas == XMC_TC0)
1269 if (sym.n_sclass != C_HIDEXT
1270 || aux.x_csect.x_scnlen.l != 0)
1272 (*_bfd_error_handler)
1273 (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1274 abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1275 bfd_set_error (bfd_error_bad_value);
1278 xcoff_data (abfd)->toc = sym.n_value;
1281 /* We must merge TOC entries for the same symbol. We can
1282 merge two TOC entries if they are both C_HIDEXT, they
1283 both have the same name, they are both 4 or 8 bytes long, and
1284 they both have a relocation table entry for an external
1285 symbol with the same name. Unfortunately, this means
1286 that we must look through the relocations. Ick.
1288 Logic for 32 bit vs 64 bit.
1289 32 bit has a csect length of 4 for TOC
1290 64 bit has a csect length of 8 for TOC
1292 The conditions to get past the if-check are not that bad.
1293 They are what is used to create the TOC csects in the first
1295 if (aux.x_csect.x_smclas == XMC_TC
1296 && sym.n_sclass == C_HIDEXT
1297 && info->output_bfd->xvec == abfd->xvec
1298 && ((bfd_xcoff_is_xcoff32 (abfd)
1299 && aux.x_csect.x_scnlen.l == 4)
1300 || (bfd_xcoff_is_xcoff64 (abfd)
1301 && aux.x_csect.x_scnlen.l == 8)))
1303 asection *enclosing;
1304 struct internal_reloc *relocs;
1305 bfd_size_type relindx;
1306 struct internal_reloc *rel;
1308 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1309 if (enclosing == NULL)
1312 relocs = reloc_info[enclosing->target_index].relocs;
1313 amt = enclosing->reloc_count;
1314 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1315 rel = relocs + relindx;
1317 /* 32 bit R_POS r_size is 31
1318 64 bit R_POS r_size is 63 */
1319 if (relindx < enclosing->reloc_count
1320 && rel->r_vaddr == (bfd_vma) sym.n_value
1321 && rel->r_type == R_POS
1322 && ((bfd_xcoff_is_xcoff32 (abfd)
1323 && rel->r_size == 31)
1324 || (bfd_xcoff_is_xcoff64 (abfd)
1325 && rel->r_size == 63)))
1329 struct internal_syment relsym;
1331 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1332 + rel->r_symndx * symesz);
1333 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1334 if (relsym.n_sclass == C_EXT)
1336 const char *relname;
1337 char relbuf[SYMNMLEN + 1];
1339 struct xcoff_link_hash_entry *h;
1341 /* At this point we know that the TOC entry is
1342 for an externally visible symbol. */
1343 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1345 if (relname == NULL)
1348 /* We only merge TOC entries if the TC name is
1349 the same as the symbol name. This handles
1350 the normal case, but not common cases like
1351 SYM.P4 which gcc generates to store SYM + 4
1352 in the TOC. FIXME. */
1353 if (strcmp (name, relname) == 0)
1355 copy = (! info->keep_memory
1356 || relsym._n._n_n._n_zeroes != 0
1357 || relsym._n._n_n._n_offset == 0);
1358 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1359 relname, TRUE, copy,
1364 /* At this point h->root.type could be
1365 bfd_link_hash_new. That should be OK,
1366 since we know for sure that we will come
1367 across this symbol as we step through the
1370 /* We store h in *sym_hash for the
1371 convenience of the relocate_section
1375 if (h->toc_section != NULL)
1377 asection **rel_csects;
1379 /* We already have a TOC entry for this
1380 symbol, so we can just ignore this
1383 reloc_info[enclosing->target_index].csects;
1384 rel_csects[relindx] = bfd_und_section_ptr;
1388 /* We are about to create a TOC entry for
1397 asection *enclosing;
1399 /* We need to create a new section. We get the name from
1400 the csect storage mapping class, so that the linker can
1401 accumulate similar csects together. */
1403 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1407 /* The enclosing section is the main section : .data, .text
1408 or .bss that the csect is coming from. */
1409 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1410 if (enclosing == NULL)
1413 if (! bfd_is_abs_section (enclosing)
1414 && ((bfd_vma) sym.n_value < enclosing->vma
1415 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1416 > enclosing->vma + enclosing->size)))
1418 (*_bfd_error_handler)
1419 (_("%B: csect `%s' not in enclosing section"),
1421 bfd_set_error (bfd_error_bad_value);
1424 csect->vma = sym.n_value;
1425 csect->filepos = (enclosing->filepos
1428 csect->size = aux.x_csect.x_scnlen.l;
1429 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1430 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1432 /* Record the enclosing section in the tdata for this new
1434 amt = sizeof (struct coff_section_tdata);
1435 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1436 if (csect->used_by_bfd == NULL)
1438 amt = sizeof (struct xcoff_section_tdata);
1439 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1440 if (coff_section_data (abfd, csect)->tdata == NULL)
1442 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1443 xcoff_section_data (abfd, csect)->lineno_count =
1444 enclosing->lineno_count;
1446 if (enclosing->owner == abfd)
1448 struct internal_reloc *relocs;
1449 bfd_size_type relindx;
1450 struct internal_reloc *rel;
1451 asection **rel_csect;
1453 relocs = reloc_info[enclosing->target_index].relocs;
1454 amt = enclosing->reloc_count;
1455 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1457 rel = relocs + relindx;
1458 rel_csect = (reloc_info[enclosing->target_index].csects
1461 csect->rel_filepos = (enclosing->rel_filepos
1462 + relindx * bfd_coff_relsz (abfd));
1463 while (relindx < enclosing->reloc_count
1464 && *rel_csect == NULL
1465 && rel->r_vaddr < csect->vma + csect->size)
1469 csect->flags |= SEC_RELOC;
1470 ++csect->reloc_count;
1477 /* There are a number of other fields and section flags
1478 which we do not bother to set. */
1480 csect_index = ((esym
1481 - (bfd_byte *) obj_coff_external_syms (abfd))
1484 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1486 if (first_csect == NULL)
1487 first_csect = csect;
1489 /* If this symbol is C_EXT, we treat it as starting at the
1490 beginning of the newly created section. */
1491 if (sym.n_sclass == C_EXT)
1497 /* If this is a TOC section for a symbol, record it. */
1498 if (set_toc != NULL)
1499 set_toc->toc_section = csect;
1504 /* This is a label definition. The x_scnlen field is the
1505 symbol index of the csect. Usually the XTY_LD symbol will
1506 follow its appropriate XTY_SD symbol. The .set pseudo op can
1507 cause the XTY_LD to not follow the XTY_SD symbol. */
1512 if (aux.x_csect.x_scnlen.l < 0
1513 || (aux.x_csect.x_scnlen.l
1514 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1518 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1520 || (section->flags & SEC_HAS_CONTENTS) == 0)
1525 (*_bfd_error_handler)
1526 (_("%B: misplaced XTY_LD `%s'"),
1528 bfd_set_error (bfd_error_bad_value);
1532 value = sym.n_value - csect->vma;
1537 /* This is an unitialized csect. We could base the name on
1538 the storage mapping class, but we don't bother except for
1539 an XMC_TD symbol. If this csect is externally visible,
1540 it is a common symbol. We put XMC_TD symbols in sections
1541 named .tocbss, and rely on the linker script to put that
1546 xcoff_section_data (abfd, csect)->last_symndx =
1548 - (bfd_byte *) obj_coff_external_syms (abfd))
1552 if (aux.x_csect.x_smclas == XMC_TD)
1554 /* The linker script puts the .td section in the data
1555 section after the .tc section. */
1556 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1560 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1565 csect->vma = sym.n_value;
1566 csect->size = aux.x_csect.x_scnlen.l;
1567 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1568 /* There are a number of other fields and section flags
1569 which we do not bother to set. */
1571 csect_index = ((esym
1572 - (bfd_byte *) obj_coff_external_syms (abfd))
1575 amt = sizeof (struct coff_section_tdata);
1576 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1577 if (csect->used_by_bfd == NULL)
1579 amt = sizeof (struct xcoff_section_tdata);
1580 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1581 if (coff_section_data (abfd, csect)->tdata == NULL)
1583 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1585 if (first_csect == NULL)
1586 first_csect = csect;
1588 if (sym.n_sclass == C_EXT)
1590 csect->flags |= SEC_IS_COMMON;
1593 value = aux.x_csect.x_scnlen.l;
1599 /* Check for magic symbol names. */
1600 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1601 && aux.x_csect.x_smclas != XMC_TC
1602 && aux.x_csect.x_smclas != XMC_TD)
1608 if (strcmp (name, "_text") == 0)
1609 i = XCOFF_SPECIAL_SECTION_TEXT;
1610 else if (strcmp (name, "_etext") == 0)
1611 i = XCOFF_SPECIAL_SECTION_ETEXT;
1612 else if (strcmp (name, "_data") == 0)
1613 i = XCOFF_SPECIAL_SECTION_DATA;
1614 else if (strcmp (name, "_edata") == 0)
1615 i = XCOFF_SPECIAL_SECTION_EDATA;
1616 else if (strcmp (name, "_end") == 0)
1617 i = XCOFF_SPECIAL_SECTION_END;
1619 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1620 i = XCOFF_SPECIAL_SECTION_END2;
1623 xcoff_hash_table (info)->special_sections[i] = csect;
1626 /* Now we have enough information to add the symbol to the
1627 linker hash table. */
1629 if (sym.n_sclass == C_EXT)
1633 BFD_ASSERT (section != NULL);
1635 /* We must copy the name into memory if we got it from the
1636 syment itself, rather than the string table. */
1637 copy = default_copy;
1638 if (sym._n._n_n._n_zeroes != 0
1639 || sym._n._n_n._n_offset == 0)
1642 /* The AIX linker appears to only detect multiple symbol
1643 definitions when there is a reference to the symbol. If
1644 a symbol is defined multiple times, and the only
1645 references are from the same object file, the AIX linker
1646 appears to permit it. It does not merge the different
1647 definitions, but handles them independently. On the
1648 other hand, if there is a reference, the linker reports
1651 This matters because the AIX <net/net_globals.h> header
1652 file actually defines an initialized array, so we have to
1653 actually permit that to work.
1655 Just to make matters even more confusing, the AIX linker
1656 appears to permit multiple symbol definitions whenever
1657 the second definition is in an archive rather than an
1658 object file. This may be a consequence of the manner in
1659 which it handles archives: I think it may load the entire
1660 archive in as separate csects, and then let garbage
1661 collection discard symbols.
1663 We also have to handle the case of statically linking a
1664 shared object, which will cause symbol redefinitions,
1665 although this is an easier case to detect. */
1667 if (info->output_bfd->xvec == abfd->xvec)
1669 if (! bfd_is_und_section (section))
1670 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1671 name, TRUE, copy, FALSE);
1673 /* Make a copy of the symbol name to prevent problems with
1675 *sym_hash = ((struct xcoff_link_hash_entry *)
1676 bfd_wrapped_link_hash_lookup (abfd, info, name,
1677 TRUE, TRUE, FALSE));
1679 if (*sym_hash == NULL)
1681 if (((*sym_hash)->root.type == bfd_link_hash_defined
1682 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1683 && ! bfd_is_und_section (section)
1684 && ! bfd_is_com_section (section))
1686 /* This is a second definition of a defined symbol. */
1687 if ((abfd->flags & DYNAMIC) != 0
1688 && ((*sym_hash)->smclas != XMC_GL
1689 || aux.x_csect.x_smclas == XMC_GL
1690 || ((*sym_hash)->root.u.def.section->owner->flags
1693 /* The new symbol is from a shared library, and
1694 either the existing symbol is not global
1695 linkage code or this symbol is global linkage
1696 code. If the existing symbol is global
1697 linkage code and the new symbol is not, then
1698 we want to use the new symbol. */
1699 section = bfd_und_section_ptr;
1702 else if (((*sym_hash)->root.u.def.section->owner->flags
1705 /* The existing symbol is from a shared library.
1707 (*sym_hash)->root.type = bfd_link_hash_undefined;
1708 (*sym_hash)->root.u.undef.abfd =
1709 (*sym_hash)->root.u.def.section->owner;
1711 else if (abfd->my_archive != NULL)
1713 /* This is a redefinition in an object contained
1714 in an archive. Just ignore it. See the
1716 section = bfd_und_section_ptr;
1719 else if ((*sym_hash)->root.u.undef.next != NULL
1720 || info->hash->undefs_tail == &(*sym_hash)->root)
1722 /* This symbol has been referenced. In this
1723 case, we just continue and permit the
1724 multiple definition error. See the comment
1725 above about the behaviour of the AIX linker. */
1727 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1729 /* The symbols are both csects of the same
1730 class. There is at least a chance that this
1731 is a semi-legitimate redefinition. */
1732 section = bfd_und_section_ptr;
1734 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1737 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1738 && ((*sym_hash)->root.type == bfd_link_hash_defined
1739 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1740 && (bfd_is_und_section (section)
1741 || bfd_is_com_section (section)))
1743 /* This is a reference to a multiply defined symbol.
1744 Report the error now. See the comment above
1745 about the behaviour of the AIX linker. We could
1746 also do this with warning symbols, but I'm not
1747 sure the XCOFF linker is wholly prepared to
1748 handle them, and that would only be a warning,
1750 if (! ((*info->callbacks->multiple_definition)
1751 (info, (*sym_hash)->root.root.string,
1752 NULL, NULL, (bfd_vma) 0,
1753 (*sym_hash)->root.u.def.section->owner,
1754 (*sym_hash)->root.u.def.section,
1755 (*sym_hash)->root.u.def.value)))
1757 /* Try not to give this error too many times. */
1758 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1762 /* _bfd_generic_link_add_one_symbol may call the linker to
1763 generate an error message, and the linker may try to read
1764 the symbol table to give a good error. Right now, the
1765 line numbers are in an inconsistent state, since they are
1766 counted both in the real sections and in the new csects.
1767 We need to leave the count in the real sections so that
1768 the linker can report the line number of the error
1769 correctly, so temporarily clobber the link to the csects
1770 so that the linker will not try to read the line numbers
1771 a second time from the csects. */
1772 BFD_ASSERT (last_real->next == first_csect);
1773 last_real->next = NULL;
1774 if (! (_bfd_generic_link_add_one_symbol
1775 (info, abfd, name, flags, section, value,
1777 (struct bfd_link_hash_entry **) sym_hash)))
1779 last_real->next = first_csect;
1781 if (smtyp == XTY_CM)
1783 if ((*sym_hash)->root.type != bfd_link_hash_common
1784 || (*sym_hash)->root.u.c.p->section != csect)
1785 /* We don't need the common csect we just created. */
1788 (*sym_hash)->root.u.c.p->alignment_power
1789 = csect->alignment_power;
1792 if (info->output_bfd->xvec == abfd->xvec)
1796 if (smtyp == XTY_ER || smtyp == XTY_CM)
1797 flag = XCOFF_REF_REGULAR;
1799 flag = XCOFF_DEF_REGULAR;
1800 (*sym_hash)->flags |= flag;
1802 if ((*sym_hash)->smclas == XMC_UA
1803 || flag == XCOFF_DEF_REGULAR)
1804 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1808 *csect_cache = csect;
1810 esym += (sym.n_numaux + 1) * symesz;
1811 sym_hash += sym.n_numaux + 1;
1812 csect_cache += sym.n_numaux + 1;
1815 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1817 /* Make sure that we have seen all the relocs. */
1818 for (o = abfd->sections; o != first_csect; o = o->next)
1820 /* Reset the section size and the line number count, since the
1821 data is now attached to the csects. Don't reset the size of
1822 the .debug section, since we need to read it below in
1823 bfd_xcoff_size_dynamic_sections. */
1824 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1826 o->lineno_count = 0;
1828 if ((o->flags & SEC_RELOC) != 0)
1831 struct internal_reloc *rel;
1832 asection **rel_csect;
1834 rel = reloc_info[o->target_index].relocs;
1835 rel_csect = reloc_info[o->target_index].csects;
1837 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1839 if (*rel_csect == NULL)
1841 (*_bfd_error_handler)
1842 (_("%B: reloc %s:%d not in csect"),
1844 bfd_set_error (bfd_error_bad_value);
1848 /* We identify all symbols which are called, so that we
1849 can create glue code for calls to functions imported
1850 from dynamic objects. */
1851 if (info->output_bfd->xvec == abfd->xvec
1852 && *rel_csect != bfd_und_section_ptr
1853 && (rel->r_type == R_BR
1854 || rel->r_type == R_RBR)
1855 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1857 struct xcoff_link_hash_entry *h;
1859 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1860 h->flags |= XCOFF_CALLED;
1861 /* If the symbol name starts with a period, it is
1862 the code of a function. If the symbol is
1863 currently undefined, then add an undefined symbol
1864 for the function descriptor. This should do no
1865 harm, because any regular object that defines the
1866 function should also define the function
1867 descriptor. It helps, because it means that we
1868 will identify the function descriptor with a
1869 dynamic object if a dynamic object defines it. */
1870 if (h->root.root.string[0] == '.'
1871 && h->descriptor == NULL)
1873 struct xcoff_link_hash_entry *hds;
1874 struct bfd_link_hash_entry *bh;
1876 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1877 h->root.root.string + 1,
1881 if (hds->root.type == bfd_link_hash_new)
1884 if (! (_bfd_generic_link_add_one_symbol
1885 (info, abfd, hds->root.root.string,
1886 (flagword) 0, bfd_und_section_ptr,
1887 (bfd_vma) 0, NULL, FALSE,
1890 hds = (struct xcoff_link_hash_entry *) bh;
1892 hds->flags |= XCOFF_DESCRIPTOR;
1893 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
1894 && (h->flags & XCOFF_DESCRIPTOR) == 0);
1895 hds->descriptor = h;
1896 h->descriptor = hds;
1901 free (reloc_info[o->target_index].csects);
1902 reloc_info[o->target_index].csects = NULL;
1904 /* Reset SEC_RELOC and the reloc_count, since the reloc
1905 information is now attached to the csects. */
1906 o->flags &=~ SEC_RELOC;
1909 /* If we are not keeping memory, free the reloc information. */
1910 if (! info->keep_memory
1911 && coff_section_data (abfd, o) != NULL
1912 && coff_section_data (abfd, o)->relocs != NULL
1913 && ! coff_section_data (abfd, o)->keep_relocs)
1915 free (coff_section_data (abfd, o)->relocs);
1916 coff_section_data (abfd, o)->relocs = NULL;
1920 /* Free up the line numbers. FIXME: We could cache these
1921 somewhere for the final link, to avoid reading them again. */
1922 if (reloc_info[o->target_index].linenos != NULL)
1924 free (reloc_info[o->target_index].linenos);
1925 reloc_info[o->target_index].linenos = NULL;
1931 obj_coff_keep_syms (abfd) = keep_syms;
1936 if (reloc_info != NULL)
1938 for (o = abfd->sections; o != NULL; o = o->next)
1940 if (reloc_info[o->target_index].csects != NULL)
1941 free (reloc_info[o->target_index].csects);
1942 if (reloc_info[o->target_index].linenos != NULL)
1943 free (reloc_info[o->target_index].linenos);
1947 obj_coff_keep_syms (abfd) = keep_syms;
1954 /* Add symbols from an XCOFF object file. */
1957 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
1959 if (! _bfd_coff_get_external_symbols (abfd))
1961 if (! xcoff_link_add_symbols (abfd, info))
1963 if (! info->keep_memory)
1965 if (! _bfd_coff_free_symbols (abfd))
1971 /* Look through the loader symbols to see if this dynamic object
1972 should be included in the link. The native linker uses the loader
1973 symbols, not the normal symbol table, so we do too. */
1976 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
1977 struct bfd_link_info *info,
1978 bfd_boolean *pneeded)
1982 struct internal_ldhdr ldhdr;
1983 const char *strings;
1984 bfd_byte *elsym, *elsymend;
1988 lsec = bfd_get_section_by_name (abfd, ".loader");
1990 /* There are no symbols, so don't try to include it. */
1993 if (! xcoff_get_section_contents (abfd, lsec))
1995 contents = coff_section_data (abfd, lsec)->contents;
1997 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
1999 strings = (char *) contents + ldhdr.l_stoff;
2001 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2003 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2004 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2006 struct internal_ldsym ldsym;
2007 char nambuf[SYMNMLEN + 1];
2009 struct bfd_link_hash_entry *h;
2011 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2013 /* We are only interested in exported symbols. */
2014 if ((ldsym.l_smtype & L_EXPORT) == 0)
2017 if (ldsym._l._l_l._l_zeroes == 0)
2018 name = strings + ldsym._l._l_l._l_offset;
2021 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2022 nambuf[SYMNMLEN] = '\0';
2026 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2028 /* We are only interested in symbols that are currently
2029 undefined. At this point we know that we are using an XCOFF
2032 && h->type == bfd_link_hash_undefined
2033 && (((struct xcoff_link_hash_entry *) h)->flags
2034 & XCOFF_DEF_DYNAMIC) == 0)
2036 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2043 /* We do not need this shared object. */
2044 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2046 free (coff_section_data (abfd, lsec)->contents);
2047 coff_section_data (abfd, lsec)->contents = NULL;
2053 /* Look through the symbols to see if this object file should be
2054 included in the link. */
2057 xcoff_link_check_ar_symbols (bfd *abfd,
2058 struct bfd_link_info *info,
2059 bfd_boolean *pneeded)
2061 bfd_size_type symesz;
2067 if ((abfd->flags & DYNAMIC) != 0
2068 && ! info->static_link
2069 && info->output_bfd->xvec == abfd->xvec)
2070 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2072 symesz = bfd_coff_symesz (abfd);
2073 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2074 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2075 while (esym < esym_end)
2077 struct internal_syment sym;
2079 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2081 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
2084 char buf[SYMNMLEN + 1];
2085 struct bfd_link_hash_entry *h;
2087 /* This symbol is externally visible, and is defined by this
2089 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2093 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2095 /* We are only interested in symbols that are currently
2096 undefined. If a symbol is currently known to be common,
2097 XCOFF linkers do not bring in an object file which
2098 defines it. We also don't bring in symbols to satisfy
2099 undefined references in shared objects. */
2101 && h->type == bfd_link_hash_undefined
2102 && (info->output_bfd->xvec != abfd->xvec
2103 || (((struct xcoff_link_hash_entry *) h)->flags
2104 & XCOFF_DEF_DYNAMIC) == 0))
2106 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2113 esym += (sym.n_numaux + 1) * symesz;
2116 /* We do not need this object file. */
2120 /* Check a single archive element to see if we need to include it in
2121 the link. *PNEEDED is set according to whether this element is
2122 needed in the link or not. This is called via
2123 _bfd_generic_link_add_archive_symbols. */
2126 xcoff_link_check_archive_element (bfd *abfd,
2127 struct bfd_link_info *info,
2128 bfd_boolean *pneeded)
2130 if (! _bfd_coff_get_external_symbols (abfd))
2133 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2138 if (! xcoff_link_add_symbols (abfd, info))
2142 if (! info->keep_memory || ! *pneeded)
2144 if (! _bfd_coff_free_symbols (abfd))
2151 /* Given an XCOFF BFD, add symbols to the global hash table as
2155 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2157 switch (bfd_get_format (abfd))
2160 return xcoff_link_add_object_symbols (abfd, info);
2163 /* If the archive has a map, do the usual search. We then need
2164 to check the archive for dynamic objects, because they may not
2165 appear in the archive map even though they should, perhaps, be
2166 included. If the archive has no map, we just consider each object
2167 file in turn, since that apparently is what the AIX native linker
2169 if (bfd_has_map (abfd))
2171 if (! (_bfd_generic_link_add_archive_symbols
2172 (abfd, info, xcoff_link_check_archive_element)))
2179 member = bfd_openr_next_archived_file (abfd, NULL);
2180 while (member != NULL)
2182 if (bfd_check_format (member, bfd_object)
2183 && (info->output_bfd->xvec == member->xvec)
2184 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2188 if (! xcoff_link_check_archive_element (member, info,
2192 member->archive_pass = -1;
2194 member = bfd_openr_next_archived_file (abfd, member);
2201 bfd_set_error (bfd_error_wrong_format);
2206 /* Mark a symbol as not being garbage, including the section in which
2209 static inline bfd_boolean
2210 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2212 if ((h->flags & XCOFF_MARK) != 0)
2215 h->flags |= XCOFF_MARK;
2216 if (h->root.type == bfd_link_hash_defined
2217 || h->root.type == bfd_link_hash_defweak)
2221 hsec = h->root.u.def.section;
2222 if (! bfd_is_abs_section (hsec)
2223 && (hsec->flags & SEC_MARK) == 0)
2225 if (! xcoff_mark (info, hsec))
2230 if (h->toc_section != NULL
2231 && (h->toc_section->flags & SEC_MARK) == 0)
2233 if (! xcoff_mark (info, h->toc_section))
2240 /* The mark phase of garbage collection. For a given section, mark
2241 it, and all the sections which define symbols to which it refers.
2242 Because this function needs to look at the relocs, we also count
2243 the number of relocs which need to be copied into the .loader
2247 xcoff_mark (struct bfd_link_info *info, asection *sec)
2249 if (bfd_is_abs_section (sec)
2250 || (sec->flags & SEC_MARK) != 0)
2253 sec->flags |= SEC_MARK;
2255 if (sec->owner->xvec == info->output_bfd->xvec
2256 && coff_section_data (sec->owner, sec) != NULL
2257 && xcoff_section_data (sec->owner, sec) != NULL)
2259 struct xcoff_link_hash_entry **hp, **hpend;
2260 struct internal_reloc *rel, *relend;
2262 /* Mark all the symbols in this section. */
2263 hp = (obj_xcoff_sym_hashes (sec->owner)
2264 + xcoff_section_data (sec->owner, sec)->first_symndx);
2265 hpend = (obj_xcoff_sym_hashes (sec->owner)
2266 + xcoff_section_data (sec->owner, sec)->last_symndx);
2267 for (; hp < hpend; hp++)
2269 struct xcoff_link_hash_entry *h;
2273 && (h->flags & XCOFF_MARK) == 0)
2275 if (! xcoff_mark_symbol (info, h))
2280 /* Look through the section relocs. */
2281 if ((sec->flags & SEC_RELOC) != 0
2282 && sec->reloc_count > 0)
2284 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2288 relend = rel + sec->reloc_count;
2289 for (; rel < relend; rel++)
2292 struct xcoff_link_hash_entry *h;
2294 if ((unsigned int) rel->r_symndx
2295 > obj_raw_syment_count (sec->owner))
2298 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2300 && (h->flags & XCOFF_MARK) == 0)
2302 if (! xcoff_mark_symbol (info, h))
2306 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2308 && (rsec->flags & SEC_MARK) == 0)
2310 if (! xcoff_mark (info, rsec))
2314 /* See if this reloc needs to be copied into the .loader
2316 switch (rel->r_type)
2320 || h->root.type == bfd_link_hash_defined
2321 || h->root.type == bfd_link_hash_defweak
2322 || h->root.type == bfd_link_hash_common
2323 || ((h->flags & XCOFF_CALLED) != 0
2324 && (h->root.type == bfd_link_hash_undefined
2325 || h->root.type == bfd_link_hash_undefweak)
2326 && h->root.root.string[0] == '.'
2327 && h->descriptor != NULL
2328 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2329 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2330 && (h->descriptor->flags
2331 & XCOFF_DEF_REGULAR) == 0))))
2339 && (h->root.type == bfd_link_hash_defined
2340 || h->root.type == bfd_link_hash_defweak)
2341 && bfd_is_abs_section (h->root.u.def.section))
2343 ++xcoff_hash_table (info)->ldrel_count;
2345 h->flags |= XCOFF_LDREL;
2352 /* We should never need a .loader reloc for a TOC
2358 if (! info->keep_memory
2359 && coff_section_data (sec->owner, sec) != NULL
2360 && coff_section_data (sec->owner, sec)->relocs != NULL
2361 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2363 free (coff_section_data (sec->owner, sec)->relocs);
2364 coff_section_data (sec->owner, sec)->relocs = NULL;
2372 /* Routines that are called after all the input files have been
2373 handled, but before the sections are laid out in memory. */
2375 /* The sweep phase of garbage collection. Remove all garbage
2379 xcoff_sweep (struct bfd_link_info *info)
2383 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2387 for (o = sub->sections; o != NULL; o = o->next)
2389 if ((o->flags & SEC_MARK) == 0)
2391 /* Keep all sections from non-XCOFF input files. Keep
2392 special sections. Keep .debug sections for the
2394 if (sub->xvec != info->output_bfd->xvec
2395 || o == xcoff_hash_table (info)->debug_section
2396 || o == xcoff_hash_table (info)->loader_section
2397 || o == xcoff_hash_table (info)->linkage_section
2398 || o == xcoff_hash_table (info)->toc_section
2399 || o == xcoff_hash_table (info)->descriptor_section
2400 || strcmp (o->name, ".debug") == 0)
2401 o->flags |= SEC_MARK;
2406 o->lineno_count = 0;
2413 /* Record the number of elements in a set. This is used to output the
2414 correct csect length. */
2417 bfd_xcoff_link_record_set (bfd *output_bfd,
2418 struct bfd_link_info *info,
2419 struct bfd_link_hash_entry *harg,
2422 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2423 struct xcoff_link_size_list *n;
2426 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2429 /* This will hardly ever be called. I don't want to burn four bytes
2430 per global symbol, so instead the size is kept on a linked list
2431 attached to the hash table. */
2433 n = bfd_alloc (output_bfd, amt);
2436 n->next = xcoff_hash_table (info)->size_list;
2439 xcoff_hash_table (info)->size_list = n;
2441 h->flags |= XCOFF_HAS_SIZE;
2446 /* Import a symbol. */
2449 bfd_xcoff_import_symbol (bfd *output_bfd,
2450 struct bfd_link_info *info,
2451 struct bfd_link_hash_entry *harg,
2453 const char *imppath,
2454 const char *impfile,
2455 const char *impmember,
2456 unsigned int syscall_flag)
2458 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2460 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2463 /* A symbol name which starts with a period is the code for a
2464 function. If the symbol is undefined, then add an undefined
2465 symbol for the function descriptor, and import that instead. */
2466 if (h->root.root.string[0] == '.'
2467 && h->root.type == bfd_link_hash_undefined
2468 && val == (bfd_vma) -1)
2470 struct xcoff_link_hash_entry *hds;
2472 hds = h->descriptor;
2475 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2476 h->root.root.string + 1,
2480 if (hds->root.type == bfd_link_hash_new)
2482 hds->root.type = bfd_link_hash_undefined;
2483 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2485 hds->flags |= XCOFF_DESCRIPTOR;
2486 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2487 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2488 hds->descriptor = h;
2489 h->descriptor = hds;
2492 /* Now, if the descriptor is undefined, import the descriptor
2493 rather than the symbol we were told to import. FIXME: Is
2494 this correct in all cases? */
2495 if (hds->root.type == bfd_link_hash_undefined)
2499 h->flags |= (XCOFF_IMPORT | syscall_flag);
2501 if (val != (bfd_vma) -1)
2503 if (h->root.type == bfd_link_hash_defined
2504 && (! bfd_is_abs_section (h->root.u.def.section)
2505 || h->root.u.def.value != val))
2507 if (! ((*info->callbacks->multiple_definition)
2508 (info, h->root.root.string, h->root.u.def.section->owner,
2509 h->root.u.def.section, h->root.u.def.value,
2510 output_bfd, bfd_abs_section_ptr, val)))
2514 h->root.type = bfd_link_hash_defined;
2515 h->root.u.def.section = bfd_abs_section_ptr;
2516 h->root.u.def.value = val;
2519 /* We overload the ldindx field to hold the l_ifile value for this
2521 BFD_ASSERT (h->ldsym == NULL);
2522 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2523 if (imppath == NULL)
2528 struct xcoff_import_file **pp;
2530 /* We start c at 1 because the first entry in the import list is
2531 reserved for the library search path. */
2532 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2534 pp = &(*pp)->next, ++c)
2536 if (strcmp ((*pp)->path, imppath) == 0
2537 && strcmp ((*pp)->file, impfile) == 0
2538 && strcmp ((*pp)->member, impmember) == 0)
2544 struct xcoff_import_file *n;
2545 bfd_size_type amt = sizeof (* n);
2547 n = bfd_alloc (output_bfd, amt);
2553 n->member = impmember;
2563 /* Export a symbol. */
2566 bfd_xcoff_export_symbol (bfd *output_bfd,
2567 struct bfd_link_info *info,
2568 struct bfd_link_hash_entry *harg)
2570 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2572 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2575 h->flags |= XCOFF_EXPORT;
2577 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2578 I'm just going to ignore it until somebody explains it. */
2580 /* See if this is a function descriptor. It may be one even though
2581 it is not so marked. */
2582 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2583 && h->root.root.string[0] != '.')
2586 struct xcoff_link_hash_entry *hfn;
2587 bfd_size_type amt = strlen (h->root.root.string) + 2;
2589 fnname = bfd_malloc (amt);
2593 strcpy (fnname + 1, h->root.root.string);
2594 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2595 fnname, FALSE, FALSE, TRUE);
2598 && hfn->smclas == XMC_PR
2599 && (hfn->root.type == bfd_link_hash_defined
2600 || hfn->root.type == bfd_link_hash_defweak))
2602 h->flags |= XCOFF_DESCRIPTOR;
2603 h->descriptor = hfn;
2604 hfn->descriptor = h;
2608 /* Make sure we don't garbage collect this symbol. */
2609 if (! xcoff_mark_symbol (info, h))
2612 /* If this is a function descriptor, make sure we don't garbage
2613 collect the associated function code. We normally don't have to
2614 worry about this, because the descriptor will be attached to a
2615 section with relocs, but if we are creating the descriptor
2616 ourselves those relocs will not be visible to the mark code. */
2617 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2619 if (! xcoff_mark_symbol (info, h->descriptor))
2626 /* Count a reloc against a symbol. This is called for relocs
2627 generated by the linker script, typically for global constructors
2631 bfd_xcoff_link_count_reloc (bfd *output_bfd,
2632 struct bfd_link_info *info,
2635 struct xcoff_link_hash_entry *h;
2637 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2640 h = ((struct xcoff_link_hash_entry *)
2641 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2645 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2646 bfd_set_error (bfd_error_no_symbols);
2650 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2651 ++xcoff_hash_table (info)->ldrel_count;
2653 /* Mark the symbol to avoid garbage collection. */
2654 if (! xcoff_mark_symbol (info, h))
2660 /* This function is called for each symbol to which the linker script
2664 bfd_xcoff_record_link_assignment (bfd *output_bfd,
2665 struct bfd_link_info *info,
2668 struct xcoff_link_hash_entry *h;
2670 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2673 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2678 h->flags |= XCOFF_DEF_REGULAR;
2683 /* Add a symbol to the .loader symbols, if necessary. */
2686 xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
2688 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2691 if (h->root.type == bfd_link_hash_warning)
2692 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
2694 /* __rtinit, this symbol has special handling. */
2695 if (h->flags & XCOFF_RTINIT)
2698 /* If this is a final link, and the symbol was defined as a common
2699 symbol in a regular object file, and there was no definition in
2700 any dynamic object, then the linker will have allocated space for
2701 the symbol in a common section but the XCOFF_DEF_REGULAR flag
2702 will not have been set. */
2703 if (h->root.type == bfd_link_hash_defined
2704 && (h->flags & XCOFF_DEF_REGULAR) == 0
2705 && (h->flags & XCOFF_REF_REGULAR) != 0
2706 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2707 && (bfd_is_abs_section (h->root.u.def.section)
2708 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
2709 h->flags |= XCOFF_DEF_REGULAR;
2711 /* If all defined symbols should be exported, mark them now. We
2712 don't want to export the actual functions, just the function
2714 if (ldinfo->export_defineds
2715 && (h->flags & XCOFF_DEF_REGULAR) != 0
2716 && h->root.root.string[0] != '.')
2720 /* We don't export a symbol which is being defined by an object
2721 included from an archive which contains a shared object. The
2722 rationale is that if an archive contains both an unshared and
2723 a shared object, then there must be some reason that the
2724 unshared object is unshared, and we don't want to start
2725 providing a shared version of it. In particular, this solves
2726 a bug involving the _savefNN set of functions. gcc will call
2727 those functions without providing a slot to restore the TOC,
2728 so it is essential that these functions be linked in directly
2729 and not from a shared object, which means that a shared
2730 object which also happens to link them in must not export
2731 them. This is confusing, but I haven't been able to think of
2732 a different approach. Note that the symbols can, of course,
2733 be exported explicitly. */
2735 if ((h->root.type == bfd_link_hash_defined
2736 || h->root.type == bfd_link_hash_defweak)
2737 && h->root.u.def.section->owner != NULL
2738 && h->root.u.def.section->owner->my_archive != NULL)
2740 bfd *arbfd, *member;
2742 arbfd = h->root.u.def.section->owner->my_archive;
2743 member = bfd_openr_next_archived_file (arbfd, NULL);
2744 while (member != NULL)
2746 if ((member->flags & DYNAMIC) != 0)
2751 member = bfd_openr_next_archived_file (arbfd, member);
2756 h->flags |= XCOFF_EXPORT;
2759 /* We don't want to garbage collect symbols which are not defined in
2760 XCOFF files. This is a convenient place to mark them. */
2761 if (xcoff_hash_table (ldinfo->info)->gc
2762 && (h->flags & XCOFF_MARK) == 0
2763 && (h->root.type == bfd_link_hash_defined
2764 || h->root.type == bfd_link_hash_defweak)
2765 && (h->root.u.def.section->owner == NULL
2766 || (h->root.u.def.section->owner->xvec
2767 != ldinfo->info->output_bfd->xvec)))
2768 h->flags |= XCOFF_MARK;
2770 /* If this symbol is called and defined in a dynamic object, or it
2771 is imported, then we need to set up global linkage code for it.
2772 (Unless we did garbage collection and we didn't need this
2774 if ((h->flags & XCOFF_CALLED) != 0
2775 && (h->root.type == bfd_link_hash_undefined
2776 || h->root.type == bfd_link_hash_undefweak)
2777 && h->root.root.string[0] == '.'
2778 && h->descriptor != NULL
2779 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2780 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2781 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
2782 && (! xcoff_hash_table (ldinfo->info)->gc
2783 || (h->flags & XCOFF_MARK) != 0))
2786 struct xcoff_link_hash_entry *hds;
2788 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2789 h->root.type = bfd_link_hash_defined;
2790 h->root.u.def.section = sec;
2791 h->root.u.def.value = sec->size;
2793 h->flags |= XCOFF_DEF_REGULAR;
2794 sec->size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
2796 /* The global linkage code requires a TOC entry for the
2798 hds = h->descriptor;
2799 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2800 || hds->root.type == bfd_link_hash_undefweak)
2801 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2802 hds->flags |= XCOFF_MARK;
2803 if (hds->toc_section == NULL)
2808 xcoff32 uses 4 bytes in the toc.
2809 xcoff64 uses 8 bytes in the toc. */
2810 if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
2812 else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
2817 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2818 hds->u.toc_offset = hds->toc_section->size;
2819 hds->toc_section->size += byte_size;
2820 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2821 ++hds->toc_section->reloc_count;
2823 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2825 /* We need to call xcoff_build_ldsyms recursively here,
2826 because we may already have passed hds on the traversal. */
2827 xcoff_build_ldsyms (hds, p);
2831 /* If this symbol is exported, but not defined, we need to try to
2833 if ((h->flags & XCOFF_EXPORT) != 0
2834 && (h->flags & XCOFF_IMPORT) == 0
2835 && (h->flags & XCOFF_DEF_REGULAR) == 0
2836 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2837 && (h->root.type == bfd_link_hash_undefined
2838 || h->root.type == bfd_link_hash_undefweak))
2840 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2841 && (h->descriptor->root.type == bfd_link_hash_defined
2842 || h->descriptor->root.type == bfd_link_hash_defweak))
2846 /* This is an undefined function descriptor associated with
2847 a defined entry point. We can build up a function
2848 descriptor ourselves. Believe it or not, the AIX linker
2849 actually does this, and there are cases where we need to
2851 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
2852 h->root.type = bfd_link_hash_defined;
2853 h->root.u.def.section = sec;
2854 h->root.u.def.value = sec->size;
2856 h->flags |= XCOFF_DEF_REGULAR;
2858 /* The size of the function descriptor depends if this is an
2859 xcoff32 (12) or xcoff64 (24). */
2861 bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
2863 /* A function descriptor uses two relocs: one for the
2864 associated code, and one for the TOC address. */
2865 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
2866 sec->reloc_count += 2;
2868 /* We handle writing out the contents of the descriptor in
2869 xcoff_write_global_symbol. */
2873 (*_bfd_error_handler)
2874 (_("warning: attempt to export undefined symbol `%s'"),
2875 h->root.root.string);
2881 /* If this is still a common symbol, and it wasn't garbage
2882 collected, we need to actually allocate space for it in the .bss
2884 if (h->root.type == bfd_link_hash_common
2885 && (! xcoff_hash_table (ldinfo->info)->gc
2886 || (h->flags & XCOFF_MARK) != 0)
2887 && h->root.u.c.p->section->size == 0)
2889 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2890 h->root.u.c.p->section->size = h->root.u.c.size;
2893 /* We need to add a symbol to the .loader section if it is mentioned
2894 in a reloc which we are copying to the .loader section and it was
2895 not defined or common, or if it is the entry point, or if it is
2898 if (((h->flags & XCOFF_LDREL) == 0
2899 || h->root.type == bfd_link_hash_defined
2900 || h->root.type == bfd_link_hash_defweak
2901 || h->root.type == bfd_link_hash_common)
2902 && (h->flags & XCOFF_ENTRY) == 0
2903 && (h->flags & XCOFF_EXPORT) == 0)
2909 /* We don't need to add this symbol if we did garbage collection and
2910 we did not mark this symbol. */
2911 if (xcoff_hash_table (ldinfo->info)->gc
2912 && (h->flags & XCOFF_MARK) == 0)
2918 /* We may have already processed this symbol due to the recursive
2920 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2923 /* We need to add this symbol to the .loader symbols. */
2925 BFD_ASSERT (h->ldsym == NULL);
2926 amt = sizeof (struct internal_ldsym);
2927 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
2928 if (h->ldsym == NULL)
2930 ldinfo->failed = TRUE;
2934 if ((h->flags & XCOFF_IMPORT) != 0)
2935 h->ldsym->l_ifile = h->ldindx;
2937 /* The first 3 symbol table indices are reserved to indicate the
2938 data, text and bss sections. */
2939 h->ldindx = ldinfo->ldsym_count + 3;
2941 ++ldinfo->ldsym_count;
2943 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
2944 h->ldsym, h->root.root.string))
2947 h->flags |= XCOFF_BUILT_LDSYM;
2951 /* Build the .loader section. This is called by the XCOFF linker
2952 emulation before_allocation routine. We must set the size of the
2953 .loader section before the linker lays out the output file.
2954 LIBPATH is the library path to search for shared objects; this is
2955 normally built from the -L arguments passed to the linker. ENTRY
2956 is the name of the entry point symbol (the -e linker option).
2957 FILE_ALIGN is the alignment to use for sections within the file
2958 (the -H linker option). MAXSTACK is the maximum stack size (the
2959 -bmaxstack linker option). MAXDATA is the maximum data size (the
2960 -bmaxdata linker option). GC is whether to do garbage collection
2961 (the -bgc linker option). MODTYPE is the module type (the
2962 -bmodtype linker option). TEXTRO is whether the text section must
2963 be read only (the -btextro linker option). EXPORT_DEFINEDS is
2964 whether all defined symbols should be exported (the -unix linker
2965 option). SPECIAL_SECTIONS is set by this routine to csects with
2966 magic names like _end. */
2969 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
2970 struct bfd_link_info *info,
2971 const char *libpath,
2973 unsigned long file_align,
2974 unsigned long maxstack,
2975 unsigned long maxdata,
2979 bfd_boolean export_defineds,
2980 asection **special_sections,
2983 struct xcoff_link_hash_entry *hentry;
2985 struct xcoff_loader_info ldinfo;
2987 size_t impsize, impcount;
2988 struct xcoff_import_file *fl;
2989 struct internal_ldhdr *ldhdr;
2990 bfd_size_type stoff;
2994 struct bfd_strtab_hash *debug_strtab;
2995 bfd_byte *debug_contents = NULL;
2998 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3000 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3001 special_sections[i] = NULL;
3005 ldinfo.failed = FALSE;
3006 ldinfo.output_bfd = output_bfd;
3008 ldinfo.export_defineds = export_defineds;
3009 ldinfo.ldsym_count = 0;
3010 ldinfo.string_size = 0;
3011 ldinfo.strings = NULL;
3012 ldinfo.string_alc = 0;
3014 xcoff_data (output_bfd)->maxstack = maxstack;
3015 xcoff_data (output_bfd)->maxdata = maxdata;
3016 xcoff_data (output_bfd)->modtype = modtype;
3018 xcoff_hash_table (info)->file_align = file_align;
3019 xcoff_hash_table (info)->textro = textro;
3024 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3025 FALSE, FALSE, TRUE);
3027 hentry->flags |= XCOFF_ENTRY;
3031 if (info->init_function || info->fini_function || rtld)
3033 struct xcoff_link_hash_entry *hsym;
3034 struct internal_ldsym *ldsym;
3036 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3037 "__rtinit", FALSE, FALSE, TRUE);
3040 (*_bfd_error_handler)
3041 (_("error: undefined symbol __rtinit"));
3045 xcoff_mark_symbol (info, hsym);
3046 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3048 /* __rtinit initialized. */
3049 amt = sizeof (* ldsym);
3050 ldsym = bfd_malloc (amt);
3052 ldsym->l_value = 0; /* Will be filled in later. */
3053 ldsym->l_scnum = 2; /* Data section. */
3054 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3055 ldsym->l_smclas = 5; /* .rw. */
3056 ldsym->l_ifile = 0; /* Special system loader symbol. */
3057 ldsym->l_parm = 0; /* NA. */
3059 /* Force __rtinit to be the first symbol in the loader symbol table
3060 See xcoff_build_ldsyms
3062 The first 3 symbol table indices are reserved to indicate the data,
3063 text and bss sections. */
3064 BFD_ASSERT (0 == ldinfo.ldsym_count);
3067 ldinfo.ldsym_count = 1;
3068 hsym->ldsym = ldsym;
3070 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3071 hsym->ldsym, hsym->root.root.string))
3074 /* This symbol is written out by xcoff_write_global_symbol
3075 Set stuff up so xcoff_write_global_symbol logic works. */
3076 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3077 hsym->root.type = bfd_link_hash_defined;
3078 hsym->root.u.def.value = 0;
3081 /* Garbage collect unused sections. */
3082 if (info->relocatable
3085 || (hentry->root.type != bfd_link_hash_defined
3086 && hentry->root.type != bfd_link_hash_defweak))
3089 xcoff_hash_table (info)->gc = FALSE;
3091 /* We still need to call xcoff_mark, in order to set ldrel_count
3093 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3097 for (o = sub->sections; o != NULL; o = o->next)
3099 if ((o->flags & SEC_MARK) == 0)
3101 if (! xcoff_mark (info, o))
3109 if (! xcoff_mark (info, hentry->root.u.def.section))
3112 xcoff_hash_table (info)->gc = TRUE;
3115 /* Return special sections to the caller. */
3116 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3118 sec = xcoff_hash_table (info)->special_sections[i];
3122 && (sec->flags & SEC_MARK) == 0)
3125 special_sections[i] = sec;
3128 if (info->input_bfds == NULL)
3129 /* I'm not sure what to do in this bizarre case. */
3132 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3137 /* Work out the size of the import file names. Each import file ID
3138 consists of three null terminated strings: the path, the file
3139 name, and the archive member name. The first entry in the list
3140 of names is the path to use to find objects, which the linker has
3141 passed in as the libpath argument. For some reason, the path
3142 entry in the other import file names appears to always be empty. */
3143 impsize = strlen (libpath) + 3;
3145 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3148 impsize += (strlen (fl->path)
3150 + strlen (fl->member)
3154 /* Set up the .loader section header. */
3155 ldhdr = &xcoff_hash_table (info)->ldhdr;
3156 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3157 ldhdr->l_nsyms = ldinfo.ldsym_count;
3158 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3159 ldhdr->l_istlen = impsize;
3160 ldhdr->l_nimpid = impcount;
3161 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3162 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3163 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3164 ldhdr->l_stlen = ldinfo.string_size;
3165 stoff = ldhdr->l_impoff + impsize;
3166 if (ldinfo.string_size == 0)
3169 ldhdr->l_stoff = stoff;
3171 /* 64 bit elements to ldhdr
3172 The swap out routine for 32 bit will ignore them.
3173 Nothing fancy, symbols come after the header and relocs come
3175 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3176 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3177 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3179 /* We now know the final size of the .loader section. Allocate
3181 lsec = xcoff_hash_table (info)->loader_section;
3182 lsec->size = stoff + ldhdr->l_stlen;
3183 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3184 if (lsec->contents == NULL)
3187 /* Set up the header. */
3188 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3190 /* Set up the import file names. */
3191 out = (char *) lsec->contents + ldhdr->l_impoff;
3192 strcpy (out, libpath);
3193 out += strlen (libpath) + 1;
3196 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3201 while ((*out++ = *s++) != '\0')
3204 while ((*out++ = *s++) != '\0')
3207 while ((*out++ = *s++) != '\0')
3211 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3213 /* Set up the symbol string table. */
3214 if (ldinfo.string_size > 0)
3216 memcpy (out, ldinfo.strings, ldinfo.string_size);
3217 free (ldinfo.strings);
3218 ldinfo.strings = NULL;
3221 /* We can't set up the symbol table or the relocs yet, because we
3222 don't yet know the final position of the various sections. The
3223 .loader symbols are written out when the corresponding normal
3224 symbols are written out in xcoff_link_input_bfd or
3225 xcoff_write_global_symbol. The .loader relocs are written out
3226 when the corresponding normal relocs are handled in
3227 xcoff_link_input_bfd. */
3229 /* Allocate space for the magic sections. */
3230 sec = xcoff_hash_table (info)->linkage_section;
3233 sec->contents = bfd_zalloc (output_bfd, sec->size);
3234 if (sec->contents == NULL)
3237 sec = xcoff_hash_table (info)->toc_section;
3240 sec->contents = bfd_zalloc (output_bfd, sec->size);
3241 if (sec->contents == NULL)
3244 sec = xcoff_hash_table (info)->descriptor_section;
3247 sec->contents = bfd_zalloc (output_bfd, sec->size);
3248 if (sec->contents == NULL)
3252 /* Now that we've done garbage collection, figure out the contents
3253 of the .debug section. */
3254 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3256 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3259 bfd_size_type symcount;
3260 unsigned long *debug_index;
3262 bfd_byte *esym, *esymend;
3263 bfd_size_type symesz;
3265 if (sub->xvec != info->output_bfd->xvec)
3267 subdeb = bfd_get_section_by_name (sub, ".debug");
3268 if (subdeb == NULL || subdeb->size == 0)
3271 if (info->strip == strip_all
3272 || info->strip == strip_debugger
3273 || info->discard == discard_all)
3279 if (! _bfd_coff_get_external_symbols (sub))
3282 symcount = obj_raw_syment_count (sub);
3283 debug_index = bfd_zalloc (sub, symcount * sizeof (unsigned long));
3284 if (debug_index == NULL)
3286 xcoff_data (sub)->debug_indices = debug_index;
3288 /* Grab the contents of the .debug section. We use malloc and
3289 copy the names into the debug stringtab, rather than
3290 bfd_alloc, because I expect that, when linking many files
3291 together, many of the strings will be the same. Storing the
3292 strings in the hash table should save space in this case. */
3293 if (! bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3296 csectpp = xcoff_data (sub)->csects;
3298 /* Dynamic object do not have csectpp's. */
3299 if (NULL != csectpp)
3301 symesz = bfd_coff_symesz (sub);
3302 esym = (bfd_byte *) obj_coff_external_syms (sub);
3303 esymend = esym + symcount * symesz;
3305 while (esym < esymend)
3307 struct internal_syment sym;
3309 bfd_coff_swap_sym_in (sub, (void *) esym, (void *) &sym);
3311 *debug_index = (unsigned long) -1;
3313 if (sym._n._n_n._n_zeroes == 0
3316 || ((*csectpp)->flags & SEC_MARK) != 0
3317 || *csectpp == bfd_abs_section_ptr)
3318 && bfd_coff_symname_in_debug (sub, &sym))
3323 name = (char *) debug_contents + sym._n._n_n._n_offset;
3324 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3325 if (indx == (bfd_size_type) -1)
3327 *debug_index = indx;
3330 esym += (sym.n_numaux + 1) * symesz;
3331 csectpp += sym.n_numaux + 1;
3332 debug_index += sym.n_numaux + 1;
3336 free (debug_contents);
3337 debug_contents = NULL;
3339 /* Clear the size of subdeb, so that it is not included directly
3340 in the output file. */
3343 if (! info->keep_memory)
3345 if (! _bfd_coff_free_symbols (sub))
3350 if (info->strip != strip_all)
3351 xcoff_hash_table (info)->debug_section->size =
3352 _bfd_stringtab_size (debug_strtab);
3357 if (ldinfo.strings != NULL)
3358 free (ldinfo.strings);
3359 if (debug_contents != NULL)
3360 free (debug_contents);
3365 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3370 struct bfd_in_memory *bim;
3372 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3379 abfd->link_next = 0;
3380 abfd->format = bfd_object;
3381 abfd->iostream = (void *) bim;
3382 abfd->flags = BFD_IN_MEMORY;
3383 abfd->direction = write_direction;
3386 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3389 /* need to reset to unknown or it will not be read back in correctly */
3390 abfd->format = bfd_unknown;
3391 abfd->direction = read_direction;
3397 /* Link an input file into the linker output file. This function
3398 handles all the sections and relocations of the input file at once. */
3401 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3405 const char *strings;
3406 bfd_size_type syment_base;
3407 unsigned int n_tmask;
3408 unsigned int n_btshft;
3409 bfd_boolean copy, hash;
3410 bfd_size_type isymesz;
3411 bfd_size_type osymesz;
3412 bfd_size_type linesz;
3415 struct xcoff_link_hash_entry **sym_hash;
3416 struct internal_syment *isymp;
3418 unsigned long *debug_index;
3420 unsigned long output_index;
3424 bfd_boolean keep_syms;
3427 /* We can just skip DYNAMIC files, unless this is a static link. */
3428 if ((input_bfd->flags & DYNAMIC) != 0
3429 && ! finfo->info->static_link)
3432 /* Move all the symbols to the output file. */
3433 output_bfd = finfo->output_bfd;
3435 syment_base = obj_raw_syment_count (output_bfd);
3436 isymesz = bfd_coff_symesz (input_bfd);
3437 osymesz = bfd_coff_symesz (output_bfd);
3438 linesz = bfd_coff_linesz (input_bfd);
3439 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3441 n_tmask = coff_data (input_bfd)->local_n_tmask;
3442 n_btshft = coff_data (input_bfd)->local_n_btshft;
3444 /* Define macros so that ISFCN, et. al., macros work correctly. */
3445 #define N_TMASK n_tmask
3446 #define N_BTSHFT n_btshft
3449 if (! finfo->info->keep_memory)
3452 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3455 if (! _bfd_coff_get_external_symbols (input_bfd))
3458 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3459 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3460 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3461 csectpp = xcoff_data (input_bfd)->csects;
3462 debug_index = xcoff_data (input_bfd)->debug_indices;
3463 isymp = finfo->internal_syms;
3464 indexp = finfo->sym_indices;
3465 output_index = syment_base;
3466 outsym = finfo->outsyms;
3470 while (esym < esym_end)
3472 struct internal_syment isym;
3473 union internal_auxent aux;
3476 bfd_boolean require;
3479 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
3481 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3483 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3485 BFD_ASSERT (isymp->n_numaux > 0);
3486 bfd_coff_swap_aux_in (input_bfd,
3487 (void *) (esym + isymesz * isymp->n_numaux),
3488 isymp->n_type, isymp->n_sclass,
3489 isymp->n_numaux - 1, isymp->n_numaux,
3492 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3495 /* Make a copy of *isymp so that the relocate_section function
3496 always sees the original values. This is more reliable than
3497 always recomputing the symbol value even if we are stripping
3501 /* If this symbol is in the .loader section, swap out the
3502 .loader symbol information. If this is an external symbol
3503 reference to a defined symbol, though, then wait until we get
3504 to the definition. */
3505 if (isym.n_sclass == C_EXT
3506 && *sym_hash != NULL
3507 && (*sym_hash)->ldsym != NULL
3509 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3511 struct xcoff_link_hash_entry *h;
3512 struct internal_ldsym *ldsym;
3516 if (isym.n_scnum > 0)
3518 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3519 ldsym->l_value = (isym.n_value
3520 + (*csectpp)->output_section->vma
3521 + (*csectpp)->output_offset
3526 ldsym->l_scnum = isym.n_scnum;
3527 ldsym->l_value = isym.n_value;
3530 ldsym->l_smtype = smtyp;
3531 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3532 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3533 || (h->flags & XCOFF_IMPORT) != 0)
3534 ldsym->l_smtype |= L_IMPORT;
3535 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3536 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3537 || (h->flags & XCOFF_EXPORT) != 0)
3538 ldsym->l_smtype |= L_EXPORT;
3539 if ((h->flags & XCOFF_ENTRY) != 0)
3540 ldsym->l_smtype |= L_ENTRY;
3542 ldsym->l_smclas = aux.x_csect.x_smclas;
3544 if (ldsym->l_ifile == (bfd_size_type) -1)
3546 else if (ldsym->l_ifile == 0)
3548 if ((ldsym->l_smtype & L_IMPORT) == 0)
3554 if (h->root.type == bfd_link_hash_defined
3555 || h->root.type == bfd_link_hash_defweak)
3556 impbfd = h->root.u.def.section->owner;
3557 else if (h->root.type == bfd_link_hash_undefined
3558 || h->root.type == bfd_link_hash_undefweak)
3559 impbfd = h->root.u.undef.abfd;
3567 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3568 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3575 BFD_ASSERT (h->ldindx >= 0);
3576 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3579 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
3582 /* Fill in snentry now that we know the target_index. */
3583 if ((h->flags & XCOFF_ENTRY) != 0
3584 && (h->root.type == bfd_link_hash_defined
3585 || h->root.type == bfd_link_hash_defweak))
3587 xcoff_data (output_bfd)->snentry =
3588 h->root.u.def.section->output_section->target_index;
3596 add = 1 + isym.n_numaux;
3598 /* If we are skipping this csect, we want to skip this symbol. */
3599 if (*csectpp == NULL)
3602 /* If we garbage collected this csect, we want to skip this
3605 && xcoff_hash_table (finfo->info)->gc
3606 && ((*csectpp)->flags & SEC_MARK) == 0
3607 && *csectpp != bfd_abs_section_ptr)
3610 /* An XCOFF linker always skips C_STAT symbols. */
3612 && isymp->n_sclass == C_STAT)
3615 /* We skip all but the first TOC anchor. */
3617 && isymp->n_sclass == C_HIDEXT
3618 && aux.x_csect.x_smclas == XMC_TC0)
3620 if (finfo->toc_symindx != -1)
3624 bfd_vma tocval, tocend;
3627 tocval = ((*csectpp)->output_section->vma
3628 + (*csectpp)->output_offset
3632 /* We want to find out if tocval is a good value to use
3633 as the TOC anchor--that is, whether we can access all
3634 of the TOC using a 16 bit offset from tocval. This
3635 test assumes that the TOC comes at the end of the
3636 output section, as it does in the default linker
3638 tocend = ((*csectpp)->output_section->vma
3639 + (*csectpp)->output_section->size);
3640 for (inp = finfo->info->input_bfds;
3642 inp = inp->link_next)
3645 for (o = inp->sections; o != NULL; o = o->next)
3646 if (strcmp (o->name, ".tocbss") == 0)
3648 bfd_vma new_toc_end;
3649 new_toc_end = (o->output_section->vma
3652 if (new_toc_end > tocend)
3653 tocend = new_toc_end;
3658 if (tocval + 0x10000 < tocend)
3660 (*_bfd_error_handler)
3661 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
3662 (unsigned long) (tocend - tocval));
3663 bfd_set_error (bfd_error_file_too_big);
3667 if (tocval + 0x8000 < tocend)
3671 tocadd = tocend - (tocval + 0x8000);
3673 isym.n_value += tocadd;
3676 finfo->toc_symindx = output_index;
3677 xcoff_data (finfo->output_bfd)->toc = tocval;
3678 xcoff_data (finfo->output_bfd)->sntoc =
3679 (*csectpp)->output_section->target_index;
3685 /* If we are stripping all symbols, we want to skip this one. */
3687 && finfo->info->strip == strip_all)
3690 /* We can skip resolved external references. */
3692 && isym.n_sclass == C_EXT
3694 && (*sym_hash)->root.type != bfd_link_hash_undefined)
3697 /* We can skip common symbols if they got defined somewhere
3700 && isym.n_sclass == C_EXT
3702 && ((*sym_hash)->root.type != bfd_link_hash_common
3703 || (*sym_hash)->root.u.c.p->section != *csectpp)
3704 && ((*sym_hash)->root.type != bfd_link_hash_defined
3705 || (*sym_hash)->root.u.def.section != *csectpp))
3708 /* Skip local symbols if we are discarding them. */
3710 && finfo->info->discard == discard_all
3711 && isym.n_sclass != C_EXT
3712 && (isym.n_sclass != C_HIDEXT
3713 || smtyp != XTY_SD))
3716 /* If we stripping debugging symbols, and this is a debugging
3717 symbol, then skip it. */
3719 && finfo->info->strip == strip_debugger
3720 && isym.n_scnum == N_DEBUG)
3723 /* If some symbols are stripped based on the name, work out the
3724 name and decide whether to skip this symbol. We don't handle
3725 this correctly for symbols whose names are in the .debug
3726 section; to get it right we would need a new bfd_strtab_hash
3727 function to return the string given the index. */
3729 && (finfo->info->strip == strip_some
3730 || finfo->info->discard == discard_l)
3731 && (debug_index == NULL || *debug_index == (unsigned long) -1))
3734 char buf[SYMNMLEN + 1];
3736 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3741 if ((finfo->info->strip == strip_some
3742 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
3744 || (finfo->info->discard == discard_l
3745 && (isym.n_sclass != C_EXT
3746 && (isym.n_sclass != C_HIDEXT
3747 || smtyp != XTY_SD))
3748 && bfd_is_local_label_name (input_bfd, name)))
3752 /* We can not skip the first TOC anchor. */
3755 && finfo->info->strip != strip_all)
3758 /* We now know whether we are to skip this symbol or not. */
3761 /* Adjust the symbol in order to output it. */
3763 if (isym._n._n_n._n_zeroes == 0
3764 && isym._n._n_n._n_offset != 0)
3766 /* This symbol has a long name. Enter it in the string
3767 table we are building. If *debug_index != -1, the
3768 name has already been entered in the .debug section. */
3769 if (debug_index != NULL && *debug_index != (unsigned long) -1)
3770 isym._n._n_n._n_offset = *debug_index;
3776 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
3780 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3781 if (indx == (bfd_size_type) -1)
3783 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3787 if (isym.n_sclass != C_BSTAT
3788 && isym.n_sclass != C_ESTAT
3789 && isym.n_sclass != C_DECL
3790 && isym.n_scnum > 0)
3792 isym.n_scnum = (*csectpp)->output_section->target_index;
3793 isym.n_value += ((*csectpp)->output_section->vma
3794 + (*csectpp)->output_offset
3798 /* The value of a C_FILE symbol is the symbol index of the
3799 next C_FILE symbol. The value of the last C_FILE symbol
3800 is -1. We try to get this right, below, just before we
3801 write the symbols out, but in the general case we may
3802 have to write the symbol out twice. */
3803 if (isym.n_sclass == C_FILE)
3805 if (finfo->last_file_index != -1
3806 && finfo->last_file.n_value != (bfd_vma) output_index)
3808 /* We must correct the value of the last C_FILE entry. */
3809 finfo->last_file.n_value = output_index;
3810 if ((bfd_size_type) finfo->last_file_index >= syment_base)
3812 /* The last C_FILE symbol is in this input file. */
3813 bfd_coff_swap_sym_out (output_bfd,
3814 (void *) &finfo->last_file,
3815 (void *) (finfo->outsyms
3816 + ((finfo->last_file_index
3822 /* We have already written out the last C_FILE
3823 symbol. We need to write it out again. We
3824 borrow *outsym temporarily. */
3827 bfd_coff_swap_sym_out (output_bfd,
3828 (void *) &finfo->last_file,
3831 pos = obj_sym_filepos (output_bfd);
3832 pos += finfo->last_file_index * osymesz;
3833 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3834 || (bfd_bwrite (outsym, osymesz, output_bfd)
3840 finfo->last_file_index = output_index;
3841 finfo->last_file = isym;
3844 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3845 into the line numbers. We update the symbol values when
3846 we handle the line numbers. */
3847 if (isym.n_sclass == C_BINCL
3848 || isym.n_sclass == C_EINCL)
3850 isym.n_value = finfo->line_filepos;
3854 /* Output the symbol. */
3856 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
3858 *indexp = output_index;
3860 if (isym.n_sclass == C_EXT)
3863 struct xcoff_link_hash_entry *h;
3865 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3867 h = obj_xcoff_sym_hashes (input_bfd)[indx];
3868 BFD_ASSERT (h != NULL);
3869 h->indx = output_index;
3872 /* If this is a symbol in the TOC which we may have merged
3873 (class XMC_TC), remember the symbol index of the TOC
3875 if (isym.n_sclass == C_HIDEXT
3876 && aux.x_csect.x_smclas == XMC_TC
3877 && *sym_hash != NULL)
3879 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3880 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3881 (*sym_hash)->u.toc_indx = output_index;
3884 output_index += add;
3885 outsym += add * osymesz;
3888 esym += add * isymesz;
3892 if (debug_index != NULL)
3895 for (--add; add > 0; --add)
3899 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3900 done in a separate pass, because we don't know the correct symbol
3901 indices until we have already decided which symbols we are going
3904 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3905 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3906 isymp = finfo->internal_syms;
3907 indexp = finfo->sym_indices;
3908 csectpp = xcoff_data (input_bfd)->csects;
3909 outsym = finfo->outsyms;
3910 while (esym < esym_end)
3914 add = 1 + isymp->n_numaux;
3917 esym += add * isymesz;
3922 if (isymp->n_sclass == C_BSTAT)
3924 struct internal_syment isym;
3928 /* The value of a C_BSTAT symbol is the symbol table
3929 index of the containing csect. */
3930 bfd_coff_swap_sym_in (output_bfd, (void *) outsym, (void *) &isym);
3931 indx = isym.n_value;
3932 if (indx < obj_raw_syment_count (input_bfd))
3936 symindx = finfo->sym_indices[indx];
3940 isym.n_value = symindx;
3941 bfd_coff_swap_sym_out (output_bfd, (void *) &isym,
3949 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3951 union internal_auxent aux;
3953 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
3954 isymp->n_sclass, i, isymp->n_numaux,
3957 if (isymp->n_sclass == C_FILE)
3959 /* This is the file name (or some comment put in by
3960 the compiler). If it is long, we must put it in
3961 the string table. */
3962 if (aux.x_file.x_n.x_zeroes == 0
3963 && aux.x_file.x_n.x_offset != 0)
3965 const char *filename;
3968 BFD_ASSERT (aux.x_file.x_n.x_offset
3969 >= STRING_SIZE_SIZE);
3970 if (strings == NULL)
3972 strings = _bfd_coff_read_string_table (input_bfd);
3973 if (strings == NULL)
3976 filename = strings + aux.x_file.x_n.x_offset;
3977 indx = _bfd_stringtab_add (finfo->strtab, filename,
3979 if (indx == (bfd_size_type) -1)
3981 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
3984 else if ((isymp->n_sclass == C_EXT
3985 || isymp->n_sclass == C_HIDEXT)
3986 && i + 1 == isymp->n_numaux)
3989 /* We don't support type checking. I don't know if
3991 aux.x_csect.x_parmhash = 0;
3992 /* I don't think anybody uses these fields, but we'd
3993 better clobber them just in case. */
3994 aux.x_csect.x_stab = 0;
3995 aux.x_csect.x_snstab = 0;
3997 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4001 indx = aux.x_csect.x_scnlen.l;
4002 if (indx < obj_raw_syment_count (input_bfd))
4006 symindx = finfo->sym_indices[indx];
4009 aux.x_csect.x_scnlen.l = 0;
4013 aux.x_csect.x_scnlen.l = symindx;
4018 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4022 if (ISFCN (isymp->n_type)
4023 || ISTAG (isymp->n_sclass)
4024 || isymp->n_sclass == C_BLOCK
4025 || isymp->n_sclass == C_FCN)
4027 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4029 && indx < obj_raw_syment_count (input_bfd))
4031 /* We look forward through the symbol for
4032 the index of the next symbol we are going
4033 to include. I don't know if this is
4035 while (finfo->sym_indices[indx] < 0
4036 && indx < obj_raw_syment_count (input_bfd))
4038 if (indx >= obj_raw_syment_count (input_bfd))
4039 indx = output_index;
4041 indx = finfo->sym_indices[indx];
4042 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4047 indx = aux.x_sym.x_tagndx.l;
4048 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4052 symindx = finfo->sym_indices[indx];
4054 aux.x_sym.x_tagndx.l = 0;
4056 aux.x_sym.x_tagndx.l = symindx;
4061 /* Copy over the line numbers, unless we are stripping
4062 them. We do this on a symbol by symbol basis in
4063 order to more easily handle garbage collection. */
4064 if ((isymp->n_sclass == C_EXT
4065 || isymp->n_sclass == C_HIDEXT)
4067 && isymp->n_numaux > 1
4068 && ISFCN (isymp->n_type)
4069 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4071 if (finfo->info->strip != strip_none
4072 && finfo->info->strip != strip_some)
4073 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4076 asection *enclosing;
4077 unsigned int enc_count;
4078 bfd_signed_vma linoff;
4079 struct internal_lineno lin;
4082 enclosing = xcoff_section_data (abfd, o)->enclosing;
4083 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4084 if (oline != enclosing)
4086 file_ptr pos = enclosing->line_filepos;
4087 bfd_size_type amt = linesz * enc_count;
4088 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4089 || (bfd_bread (finfo->linenos, amt, input_bfd)
4095 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4096 - enclosing->line_filepos);
4098 bfd_coff_swap_lineno_in (input_bfd,
4099 (void *) (finfo->linenos + linoff),
4102 || ((bfd_size_type) lin.l_addr.l_symndx
4106 obj_coff_external_syms (input_bfd)))
4108 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4111 bfd_byte *linpend, *linp;
4113 bfd_size_type count;
4115 lin.l_addr.l_symndx = *indexp;
4116 bfd_coff_swap_lineno_out (output_bfd, (void *) &lin,
4117 (void *) (finfo->linenos
4120 linpend = (finfo->linenos
4121 + enc_count * linesz);
4122 offset = (o->output_section->vma
4125 for (linp = finfo->linenos + linoff + linesz;
4129 bfd_coff_swap_lineno_in (input_bfd, (void *) linp,
4131 if (lin.l_lnno == 0)
4133 lin.l_addr.l_paddr += offset;
4134 bfd_coff_swap_lineno_out (output_bfd,
4139 count = (linp - (finfo->linenos + linoff)) / linesz;
4141 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4142 (o->output_section->line_filepos
4143 + o->output_section->lineno_count * linesz);
4145 if (bfd_seek (output_bfd,
4146 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4148 || (bfd_bwrite (finfo->linenos + linoff,
4149 linesz * count, output_bfd)
4153 o->output_section->lineno_count += count;
4157 struct internal_syment *iisp, *iispend;
4162 /* Update any C_BINCL or C_EINCL symbols
4163 that refer to a line number in the
4164 range we just output. */
4165 iisp = finfo->internal_syms;
4167 + obj_raw_syment_count (input_bfd));
4168 iindp = finfo->sym_indices;
4169 oos = finfo->outsyms;
4170 while (iisp < iispend)
4173 && (iisp->n_sclass == C_BINCL
4174 || iisp->n_sclass == C_EINCL)
4175 && ((bfd_size_type) iisp->n_value
4176 >= (bfd_size_type)(enclosing->line_filepos + linoff))
4177 && ((bfd_size_type) iisp->n_value
4178 < (enclosing->line_filepos
4179 + enc_count * linesz)))
4181 struct internal_syment iis;
4183 bfd_coff_swap_sym_in (output_bfd,
4188 - enclosing->line_filepos
4190 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4191 bfd_coff_swap_sym_out (output_bfd,
4197 iiadd = 1 + iisp->n_numaux;
4199 oos += iiadd * osymesz;
4208 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4209 isymp->n_sclass, i, isymp->n_numaux,
4221 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4222 symbol will be the first symbol in the next input file. In the
4223 normal case, this will save us from writing out the C_FILE symbol
4225 if (finfo->last_file_index != -1
4226 && (bfd_size_type) finfo->last_file_index >= syment_base)
4228 finfo->last_file.n_value = output_index;
4229 bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4230 (void *) (finfo->outsyms
4231 + ((finfo->last_file_index - syment_base)
4235 /* Write the modified symbols to the output file. */
4236 if (outsym > finfo->outsyms)
4238 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4239 bfd_size_type amt = outsym - finfo->outsyms;
4240 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4241 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4244 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4245 + (outsym - finfo->outsyms) / osymesz)
4248 obj_raw_syment_count (output_bfd) = output_index;
4251 /* Don't let the linker relocation routines discard the symbols. */
4252 keep_syms = obj_coff_keep_syms (input_bfd);
4253 obj_coff_keep_syms (input_bfd) = TRUE;
4255 /* Relocate the contents of each section. */
4256 for (o = input_bfd->sections; o != NULL; o = o->next)
4260 if (! o->linker_mark)
4261 /* This section was omitted from the link. */
4264 if ((o->flags & SEC_HAS_CONTENTS) == 0
4266 || (o->flags & SEC_IN_MEMORY) != 0)
4269 /* We have set filepos correctly for the sections we created to
4270 represent csects, so bfd_get_section_contents should work. */
4271 if (coff_section_data (input_bfd, o) != NULL
4272 && coff_section_data (input_bfd, o)->contents != NULL)
4273 contents = coff_section_data (input_bfd, o)->contents;
4276 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4277 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4279 contents = finfo->contents;
4282 if ((o->flags & SEC_RELOC) != 0)
4285 struct internal_reloc *internal_relocs;
4286 struct internal_reloc *irel;
4288 struct internal_reloc *irelend;
4289 struct xcoff_link_hash_entry **rel_hash;
4292 /* Read in the relocs. */
4293 target_index = o->output_section->target_index;
4294 internal_relocs = (xcoff_read_internal_relocs
4295 (input_bfd, o, FALSE, finfo->external_relocs,
4297 (finfo->section_info[target_index].relocs
4298 + o->output_section->reloc_count)));
4299 if (internal_relocs == NULL)
4302 /* Call processor specific code to relocate the section
4304 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4308 finfo->internal_syms,
4309 xcoff_data (input_bfd)->csects))
4312 offset = o->output_section->vma + o->output_offset - o->vma;
4313 irel = internal_relocs;
4314 irelend = irel + o->reloc_count;
4315 rel_hash = (finfo->section_info[target_index].rel_hashes
4316 + o->output_section->reloc_count);
4317 for (; irel < irelend; irel++, rel_hash++)
4319 struct xcoff_link_hash_entry *h = NULL;
4320 struct internal_ldrel ldrel;
4325 /* Adjust the reloc address and symbol index. */
4327 irel->r_vaddr += offset;
4329 r_symndx = irel->r_symndx;
4334 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4336 if (r_symndx != -1 && finfo->info->strip != strip_all)
4339 && h->smclas != XMC_TD
4340 && (irel->r_type == R_TOC
4341 || irel->r_type == R_GL
4342 || irel->r_type == R_TCL
4343 || irel->r_type == R_TRL
4344 || irel->r_type == R_TRLA))
4346 /* This is a TOC relative reloc with a symbol
4347 attached. The symbol should be the one which
4348 this reloc is for. We want to make this
4349 reloc against the TOC address of the symbol,
4350 not the symbol itself. */
4351 BFD_ASSERT (h->toc_section != NULL);
4352 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4353 if (h->u.toc_indx != -1)
4354 irel->r_symndx = h->u.toc_indx;
4357 struct xcoff_toc_rel_hash *n;
4358 struct xcoff_link_section_info *si;
4362 n = bfd_alloc (finfo->output_bfd, amt);
4365 si = finfo->section_info + target_index;
4366 n->next = si->toc_rel_hashes;
4369 si->toc_rel_hashes = n;
4374 /* This is a global symbol. */
4376 irel->r_symndx = h->indx;
4379 /* This symbol is being written at the end
4380 of the file, and we do not yet know the
4381 symbol index. We save the pointer to the
4382 hash table entry in the rel_hash list.
4383 We set the indx field to -2 to indicate
4384 that this symbol must not be stripped. */
4393 indx = finfo->sym_indices[r_symndx];
4397 struct internal_syment *is;
4399 /* Relocations against a TC0 TOC anchor are
4400 automatically transformed to be against
4401 the TOC anchor in the output file. */
4402 is = finfo->internal_syms + r_symndx;
4403 if (is->n_sclass == C_HIDEXT
4404 && is->n_numaux > 0)
4407 union internal_auxent aux;
4411 obj_coff_external_syms (input_bfd))
4412 + ((r_symndx + is->n_numaux)
4414 bfd_coff_swap_aux_in (input_bfd, auxptr,
4415 is->n_type, is->n_sclass,
4419 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4420 && aux.x_csect.x_smclas == XMC_TC0)
4421 indx = finfo->toc_symindx;
4426 irel->r_symndx = indx;
4430 struct internal_syment *is;
4433 char buf[SYMNMLEN + 1];
4435 /* This reloc is against a symbol we are
4436 stripping. It would be possible to handle
4437 this case, but I don't think it's worth it. */
4438 is = finfo->internal_syms + r_symndx;
4440 name = (_bfd_coff_internal_syment_name
4441 (input_bfd, is, buf));
4446 if (! ((*finfo->info->callbacks->unattached_reloc)
4447 (finfo->info, name, input_bfd, o,
4455 switch (irel->r_type)
4459 || h->root.type == bfd_link_hash_defined
4460 || h->root.type == bfd_link_hash_defweak
4461 || h->root.type == bfd_link_hash_common)
4469 && (h->root.type == bfd_link_hash_defined
4470 || h->root.type == bfd_link_hash_defweak)
4471 && bfd_is_abs_section (h->root.u.def.section))
4473 /* This reloc needs to be copied into the .loader
4475 ldrel.l_vaddr = irel->r_vaddr;
4477 ldrel.l_symndx = -(bfd_size_type ) 1;
4479 || (h->root.type == bfd_link_hash_defined
4480 || h->root.type == bfd_link_hash_defweak
4481 || h->root.type == bfd_link_hash_common))
4486 sec = xcoff_data (input_bfd)->csects[r_symndx];
4487 else if (h->root.type == bfd_link_hash_common)
4488 sec = h->root.u.c.p->section;
4490 sec = h->root.u.def.section;
4491 sec = sec->output_section;
4493 if (strcmp (sec->name, ".text") == 0)
4495 else if (strcmp (sec->name, ".data") == 0)
4497 else if (strcmp (sec->name, ".bss") == 0)
4501 (*_bfd_error_handler)
4502 (_("%B: loader reloc in unrecognized section `%A'"),
4504 bfd_set_error (bfd_error_nonrepresentable_section);
4510 if (! finfo->info->relocatable
4511 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
4512 && (h->flags & XCOFF_IMPORT) == 0)
4514 /* We already called the undefined_symbol
4515 callback for this relocation, in
4516 _bfd_ppc_xcoff_relocate_section. Don't
4517 issue any more warnings. */
4520 if (h->ldindx < 0 && ! quiet)
4522 (*_bfd_error_handler)
4523 (_("%B: `%s' in loader reloc but not loader sym"),
4525 h->root.root.string);
4526 bfd_set_error (bfd_error_bad_value);
4529 ldrel.l_symndx = h->ldindx;
4531 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4532 ldrel.l_rsecnm = o->output_section->target_index;
4533 if (xcoff_hash_table (finfo->info)->textro
4534 && strcmp (o->output_section->name, ".text") == 0
4537 (*_bfd_error_handler)
4538 (_("%B: loader reloc in read-only section %A"),
4539 input_bfd, o->output_section);
4540 bfd_set_error (bfd_error_invalid_operation);
4543 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
4546 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4554 /* We should never need a .loader reloc for a TOC
4560 o->output_section->reloc_count += o->reloc_count;
4563 /* Write out the modified section contents. */
4564 if (! bfd_set_section_contents (output_bfd, o->output_section,
4565 contents, (file_ptr) o->output_offset,
4570 obj_coff_keep_syms (input_bfd) = keep_syms;
4572 if (! finfo->info->keep_memory)
4574 if (! _bfd_coff_free_symbols (input_bfd))
4584 /* Sort relocs by VMA. This is called via qsort. */
4587 xcoff_sort_relocs (const void * p1, const void * p2)
4589 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4590 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4592 if (r1->r_vaddr > r2->r_vaddr)
4594 else if (r1->r_vaddr < r2->r_vaddr)
4600 /* Write out a non-XCOFF global symbol. */
4603 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4605 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4608 struct internal_syment isym;
4609 union internal_auxent aux;
4614 output_bfd = finfo->output_bfd;
4615 outsym = finfo->outsyms;
4617 if (h->root.type == bfd_link_hash_warning)
4619 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4620 if (h->root.type == bfd_link_hash_new)
4624 /* If this symbol was garbage collected, just skip it. */
4625 if (xcoff_hash_table (finfo->info)->gc
4626 && (h->flags & XCOFF_MARK) == 0)
4629 /* If we need a .loader section entry, write it out. */
4630 if (h->ldsym != NULL)
4632 struct internal_ldsym *ldsym;
4637 if (h->root.type == bfd_link_hash_undefined
4638 || h->root.type == bfd_link_hash_undefweak)
4642 ldsym->l_scnum = N_UNDEF;
4643 ldsym->l_smtype = XTY_ER;
4644 impbfd = h->root.u.undef.abfd;
4647 else if (h->root.type == bfd_link_hash_defined
4648 || h->root.type == bfd_link_hash_defweak)
4652 sec = h->root.u.def.section;
4653 ldsym->l_value = (sec->output_section->vma
4654 + sec->output_offset
4655 + h->root.u.def.value);
4656 ldsym->l_scnum = sec->output_section->target_index;
4657 ldsym->l_smtype = XTY_SD;
4658 impbfd = sec->owner;
4664 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4665 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4666 || (h->flags & XCOFF_IMPORT) != 0)
4668 Import symbols are defined so the check above will make
4669 the l_smtype XTY_SD. But this is not correct, it should
4671 ldsym->l_smtype |= L_IMPORT;
4673 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4674 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4675 || (h->flags & XCOFF_EXPORT) != 0)
4676 ldsym->l_smtype |= L_EXPORT;
4678 if ((h->flags & XCOFF_ENTRY) != 0)
4679 ldsym->l_smtype |= L_ENTRY;
4681 if ((h->flags & XCOFF_RTINIT) != 0)
4682 ldsym->l_smtype = XTY_SD;
4684 ldsym->l_smclas = h->smclas;
4686 if (ldsym->l_smtype & L_IMPORT)
4688 if ((h->root.type == bfd_link_hash_defined
4689 || h->root.type == bfd_link_hash_defweak)
4690 && (h->root.u.def.value != 0))
4691 ldsym->l_smclas = XMC_XO;
4693 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
4694 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
4695 ldsym->l_smclas = XMC_SV3264;
4697 else if (h->flags & XCOFF_SYSCALL32)
4698 ldsym->l_smclas = XMC_SV;
4700 else if (h->flags & XCOFF_SYSCALL64)
4701 ldsym->l_smclas = XMC_SV64;
4704 if (ldsym->l_ifile == -(bfd_size_type) 1)
4708 else if (ldsym->l_ifile == 0)
4710 if ((ldsym->l_smtype & L_IMPORT) == 0)
4712 else if (impbfd == NULL)
4716 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4717 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4723 BFD_ASSERT (h->ldindx >= 0);
4725 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
4728 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
4732 /* If this symbol needs global linkage code, write it out. */
4733 if (h->root.type == bfd_link_hash_defined
4734 && (h->root.u.def.section
4735 == xcoff_hash_table (finfo->info)->linkage_section))
4741 p = h->root.u.def.section->contents + h->root.u.def.value;
4743 /* The first instruction in the global linkage code loads a
4744 specific TOC element. */
4745 tocoff = (h->descriptor->toc_section->output_section->vma
4746 + h->descriptor->toc_section->output_offset
4747 - xcoff_data (output_bfd)->toc);
4749 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4750 tocoff += h->descriptor->u.toc_offset;
4752 /* The first instruction in the glink code needs to be
4753 cooked to to hold the correct offset in the toc. The
4754 rest are just output raw. */
4755 bfd_put_32 (output_bfd,
4756 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
4758 /* Start with i == 1 to get past the first instruction done above
4759 The /4 is because the glink code is in bytes and we are going
4761 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
4762 bfd_put_32 (output_bfd,
4763 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
4767 /* If we created a TOC entry for this symbol, write out the required
4769 if ((h->flags & XCOFF_SET_TOC) != 0)
4774 struct internal_reloc *irel;
4775 struct internal_ldrel ldrel;
4776 struct internal_syment irsym;
4777 union internal_auxent iraux;
4779 tocsec = h->toc_section;
4780 osec = tocsec->output_section;
4781 oindx = osec->target_index;
4782 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4783 irel->r_vaddr = (osec->vma
4784 + tocsec->output_offset
4788 irel->r_symndx = h->indx;
4792 irel->r_symndx = obj_raw_syment_count (output_bfd);
4795 BFD_ASSERT (h->ldindx >= 0);
4797 /* Initialize the aux union here instead of closer to when it is
4798 written out below because the length of the csect depends on
4799 whether the output is 32 or 64 bit. */
4800 memset (&iraux, 0, sizeof iraux);
4801 iraux.x_csect.x_smtyp = XTY_SD;
4802 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
4803 iraux.x_csect.x_smclas = XMC_TC;
4805 /* 32 bit uses a 32 bit R_POS to do the relocations
4806 64 bit uses a 64 bit R_POS to do the relocations
4808 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
4810 Which one is determined by the backend. */
4811 if (bfd_xcoff_is_xcoff64 (output_bfd))
4814 iraux.x_csect.x_scnlen.l = 8;
4816 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4819 iraux.x_csect.x_scnlen.l = 4;
4824 irel->r_type = R_POS;
4825 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4826 ++osec->reloc_count;
4828 ldrel.l_vaddr = irel->r_vaddr;
4829 ldrel.l_symndx = h->ldindx;
4830 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
4831 ldrel.l_rsecnm = oindx;
4832 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4833 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4835 /* We need to emit a symbol to define a csect which holds
4837 if (finfo->info->strip != strip_all)
4839 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
4840 &irsym, h->root.root.string);
4844 irsym.n_value = irel->r_vaddr;
4845 irsym.n_scnum = osec->target_index;
4846 irsym.n_sclass = C_HIDEXT;
4847 irsym.n_type = T_NULL;
4850 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
4851 outsym += bfd_coff_symesz (output_bfd);
4853 /* Note : iraux is initialized above. */
4854 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
4855 0, 1, (void *) outsym);
4856 outsym += bfd_coff_auxesz (output_bfd);
4860 /* We aren't going to write out the symbols below, so we
4861 need to write them out now. */
4862 pos = obj_sym_filepos (output_bfd);
4863 pos += (obj_raw_syment_count (output_bfd)
4864 * bfd_coff_symesz (output_bfd));
4865 amt = outsym - finfo->outsyms;
4866 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4867 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4869 obj_raw_syment_count (output_bfd) +=
4870 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4872 outsym = finfo->outsyms;
4877 /* If this symbol is a specially defined function descriptor, write
4878 it out. The first word is the address of the function code
4879 itself, the second word is the address of the TOC, and the third
4883 The addresses for the 32 bit will take 4 bytes and the addresses
4884 for 64 bit will take 8 bytes. Similar for the relocs. This type
4885 of logic was also done above to create a TOC entry in
4886 xcoff_write_global_symbol. */
4887 if ((h->flags & XCOFF_DESCRIPTOR) != 0
4888 && h->root.type == bfd_link_hash_defined
4889 && (h->root.u.def.section
4890 == xcoff_hash_table (finfo->info)->descriptor_section))
4896 struct xcoff_link_hash_entry *hentry;
4898 struct internal_reloc *irel;
4899 struct internal_ldrel ldrel;
4901 unsigned int reloc_size, byte_size;
4903 if (bfd_xcoff_is_xcoff64 (output_bfd))
4908 else if (bfd_xcoff_is_xcoff32 (output_bfd))
4916 sec = h->root.u.def.section;
4917 osec = sec->output_section;
4918 oindx = osec->target_index;
4919 p = sec->contents + h->root.u.def.value;
4921 hentry = h->descriptor;
4922 BFD_ASSERT (hentry != NULL
4923 && (hentry->root.type == bfd_link_hash_defined
4924 || hentry->root.type == bfd_link_hash_defweak));
4925 esec = hentry->root.u.def.section;
4927 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4928 irel->r_vaddr = (osec->vma
4929 + sec->output_offset
4930 + h->root.u.def.value);
4931 irel->r_symndx = esec->output_section->target_index;
4932 irel->r_type = R_POS;
4933 irel->r_size = reloc_size;
4934 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4935 ++osec->reloc_count;
4937 ldrel.l_vaddr = irel->r_vaddr;
4938 if (strcmp (esec->output_section->name, ".text") == 0)
4940 else if (strcmp (esec->output_section->name, ".data") == 0)
4942 else if (strcmp (esec->output_section->name, ".bss") == 0)
4946 (*_bfd_error_handler)
4947 (_("%s: loader reloc in unrecognized section `%s'"),
4948 bfd_get_filename (output_bfd),
4949 esec->output_section->name);
4950 bfd_set_error (bfd_error_nonrepresentable_section);
4953 ldrel.l_rtype = (reloc_size << 8) | R_POS;
4954 ldrel.l_rsecnm = oindx;
4955 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4956 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4958 /* There are three items to write out,
4959 the address of the code
4960 the address of the toc anchor
4961 the environment pointer.
4962 We are ignoring the environment pointer. So set it to zero. */
4963 if (bfd_xcoff_is_xcoff64 (output_bfd))
4965 bfd_put_64 (output_bfd,
4966 (esec->output_section->vma + esec->output_offset
4967 + hentry->root.u.def.value),
4969 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
4970 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
4975 This logic was already called above so the error case where
4976 the backend is neither has already been checked. */
4977 bfd_put_32 (output_bfd,
4978 (esec->output_section->vma + esec->output_offset
4979 + hentry->root.u.def.value),
4981 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
4982 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
4985 tsec = coff_section_from_bfd_index (output_bfd,
4986 xcoff_data (output_bfd)->sntoc);
4989 irel->r_vaddr = (osec->vma
4990 + sec->output_offset
4991 + h->root.u.def.value
4993 irel->r_symndx = tsec->output_section->target_index;
4994 irel->r_type = R_POS;
4995 irel->r_size = reloc_size;
4996 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4997 ++osec->reloc_count;
4999 ldrel.l_vaddr = irel->r_vaddr;
5000 if (strcmp (tsec->output_section->name, ".text") == 0)
5002 else if (strcmp (tsec->output_section->name, ".data") == 0)
5004 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5008 (*_bfd_error_handler)
5009 (_("%s: loader reloc in unrecognized section `%s'"),
5010 bfd_get_filename (output_bfd),
5011 tsec->output_section->name);
5012 bfd_set_error (bfd_error_nonrepresentable_section);
5015 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5016 ldrel.l_rsecnm = oindx;
5017 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5018 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5021 if (h->indx >= 0 || finfo->info->strip == strip_all)
5023 BFD_ASSERT (outsym == finfo->outsyms);
5028 && (finfo->info->strip == strip_all
5029 || (finfo->info->strip == strip_some
5030 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5031 FALSE, FALSE) == NULL)))
5033 BFD_ASSERT (outsym == finfo->outsyms);
5038 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5040 BFD_ASSERT (outsym == finfo->outsyms);
5044 memset (&aux, 0, sizeof aux);
5046 h->indx = obj_raw_syment_count (output_bfd);
5048 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5049 h->root.root.string);
5053 if (h->root.type == bfd_link_hash_undefined
5054 || h->root.type == bfd_link_hash_undefweak)
5057 isym.n_scnum = N_UNDEF;
5058 isym.n_sclass = C_EXT;
5059 aux.x_csect.x_smtyp = XTY_ER;
5061 else if ((h->root.type == bfd_link_hash_defined
5062 || h->root.type == bfd_link_hash_defweak)
5063 && h->smclas == XMC_XO)
5065 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5066 isym.n_value = h->root.u.def.value;
5067 isym.n_scnum = N_UNDEF;
5068 isym.n_sclass = C_EXT;
5069 aux.x_csect.x_smtyp = XTY_ER;
5071 else if (h->root.type == bfd_link_hash_defined
5072 || h->root.type == bfd_link_hash_defweak)
5074 struct xcoff_link_size_list *l;
5076 isym.n_value = (h->root.u.def.section->output_section->vma
5077 + h->root.u.def.section->output_offset
5078 + h->root.u.def.value);
5079 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5080 isym.n_scnum = N_ABS;
5082 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5083 isym.n_sclass = C_HIDEXT;
5084 aux.x_csect.x_smtyp = XTY_SD;
5086 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5088 for (l = xcoff_hash_table (finfo->info)->size_list;
5094 aux.x_csect.x_scnlen.l = l->size;
5100 else if (h->root.type == bfd_link_hash_common)
5102 isym.n_value = (h->root.u.c.p->section->output_section->vma
5103 + h->root.u.c.p->section->output_offset);
5104 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5105 isym.n_sclass = C_EXT;
5106 aux.x_csect.x_smtyp = XTY_CM;
5107 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5112 isym.n_type = T_NULL;
5115 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5116 outsym += bfd_coff_symesz (output_bfd);
5118 aux.x_csect.x_smclas = h->smclas;
5119 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5121 outsym += bfd_coff_auxesz (output_bfd);
5123 if ((h->root.type == bfd_link_hash_defined
5124 || h->root.type == bfd_link_hash_defweak)
5125 && h->smclas != XMC_XO)
5127 /* We just output an SD symbol. Now output an LD symbol. */
5130 isym.n_sclass = C_EXT;
5131 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5132 outsym += bfd_coff_symesz (output_bfd);
5134 aux.x_csect.x_smtyp = XTY_LD;
5135 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5136 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5138 outsym += bfd_coff_auxesz (output_bfd);
5141 pos = obj_sym_filepos (output_bfd);
5142 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5143 amt = outsym - finfo->outsyms;
5144 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5145 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5147 obj_raw_syment_count (output_bfd) +=
5148 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5153 /* Handle a link order which is supposed to generate a reloc. */
5156 xcoff_reloc_link_order (bfd *output_bfd,
5157 struct xcoff_final_link_info *finfo,
5158 asection *output_section,
5159 struct bfd_link_order *link_order)
5161 reloc_howto_type *howto;
5162 struct xcoff_link_hash_entry *h;
5166 struct internal_reloc *irel;
5167 struct xcoff_link_hash_entry **rel_hash_ptr;
5168 struct internal_ldrel ldrel;
5170 if (link_order->type == bfd_section_reloc_link_order)
5171 /* We need to somehow locate a symbol in the right section. The
5172 symbol must either have a value of zero, or we must adjust
5173 the addend by the value of the symbol. FIXME: Write this
5174 when we need it. The old linker couldn't handle this anyhow. */
5177 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5180 bfd_set_error (bfd_error_bad_value);
5184 h = ((struct xcoff_link_hash_entry *)
5185 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5186 link_order->u.reloc.p->u.name,
5187 FALSE, FALSE, TRUE));
5190 if (! ((*finfo->info->callbacks->unattached_reloc)
5191 (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5196 if (h->root.type == bfd_link_hash_common)
5198 hsec = h->root.u.c.p->section;
5201 else if (h->root.type == bfd_link_hash_defined
5202 || h->root.type == bfd_link_hash_defweak)
5204 hsec = h->root.u.def.section;
5205 hval = h->root.u.def.value;
5213 addend = link_order->u.reloc.p->addend;
5215 addend += (hsec->output_section->vma
5216 + hsec->output_offset
5223 bfd_reloc_status_type rstat;
5226 size = bfd_get_reloc_size (howto);
5227 buf = bfd_zmalloc (size);
5231 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5237 case bfd_reloc_outofrange:
5239 case bfd_reloc_overflow:
5240 if (! ((*finfo->info->callbacks->reloc_overflow)
5241 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5242 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5249 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5250 (file_ptr) link_order->offset, size);
5256 /* Store the reloc information in the right place. It will get
5257 swapped and written out at the end of the final_link routine. */
5258 irel = (finfo->section_info[output_section->target_index].relocs
5259 + output_section->reloc_count);
5260 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5261 + output_section->reloc_count);
5263 memset (irel, 0, sizeof (struct internal_reloc));
5264 *rel_hash_ptr = NULL;
5266 irel->r_vaddr = output_section->vma + link_order->offset;
5269 irel->r_symndx = h->indx;
5272 /* Set the index to -2 to force this symbol to get written out. */
5278 irel->r_type = howto->type;
5279 irel->r_size = howto->bitsize - 1;
5280 if (howto->complain_on_overflow == complain_overflow_signed)
5281 irel->r_size |= 0x80;
5283 ++output_section->reloc_count;
5285 /* Now output the reloc to the .loader section. */
5287 ldrel.l_vaddr = irel->r_vaddr;
5291 const char *secname;
5293 secname = hsec->output_section->name;
5295 if (strcmp (secname, ".text") == 0)
5297 else if (strcmp (secname, ".data") == 0)
5299 else if (strcmp (secname, ".bss") == 0)
5303 (*_bfd_error_handler)
5304 (_("%s: loader reloc in unrecognized section `%s'"),
5305 bfd_get_filename (output_bfd), secname);
5306 bfd_set_error (bfd_error_nonrepresentable_section);
5314 (*_bfd_error_handler)
5315 (_("%s: `%s' in loader reloc but not loader sym"),
5316 bfd_get_filename (output_bfd),
5317 h->root.root.string);
5318 bfd_set_error (bfd_error_bad_value);
5321 ldrel.l_symndx = h->ldindx;
5324 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5325 ldrel.l_rsecnm = output_section->target_index;
5326 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5327 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5332 /* Do the final link step. */
5335 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5337 bfd_size_type symesz;
5338 struct xcoff_final_link_info finfo;
5340 struct bfd_link_order *p;
5341 bfd_size_type max_contents_size;
5342 bfd_size_type max_sym_count;
5343 bfd_size_type max_lineno_count;
5344 bfd_size_type max_reloc_count;
5345 bfd_size_type max_output_reloc_count;
5346 file_ptr rel_filepos;
5348 file_ptr line_filepos;
5349 unsigned int linesz;
5351 bfd_byte *external_relocs = NULL;
5352 char strbuf[STRING_SIZE_SIZE];
5357 abfd->flags |= DYNAMIC;
5359 symesz = bfd_coff_symesz (abfd);
5362 finfo.output_bfd = abfd;
5363 finfo.strtab = NULL;
5364 finfo.section_info = NULL;
5365 finfo.last_file_index = -1;
5366 finfo.toc_symindx = -1;
5367 finfo.internal_syms = NULL;
5368 finfo.sym_indices = NULL;
5369 finfo.outsyms = NULL;
5370 finfo.linenos = NULL;
5371 finfo.contents = NULL;
5372 finfo.external_relocs = NULL;
5374 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5375 + bfd_xcoff_ldhdrsz (abfd));
5376 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5377 + bfd_xcoff_ldhdrsz(abfd)
5378 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5379 * bfd_xcoff_ldsymsz(abfd)));
5381 xcoff_data (abfd)->coff.link_info = info;
5383 finfo.strtab = _bfd_stringtab_init ();
5384 if (finfo.strtab == NULL)
5387 /* Count the line number and relocation entries required for the
5388 output file. Determine a few maximum sizes. */
5389 max_contents_size = 0;
5390 max_lineno_count = 0;
5391 max_reloc_count = 0;
5392 for (o = abfd->sections; o != NULL; o = o->next)
5395 o->lineno_count = 0;
5396 for (p = o->map_head.link_order; p != NULL; p = p->next)
5398 if (p->type == bfd_indirect_link_order)
5402 sec = p->u.indirect.section;
5404 /* Mark all sections which are to be included in the
5405 link. This will normally be every section. We need
5406 to do this so that we can identify any sections which
5407 the linker has decided to not include. */
5408 sec->linker_mark = TRUE;
5410 if (info->strip == strip_none
5411 || info->strip == strip_some)
5412 o->lineno_count += sec->lineno_count;
5414 o->reloc_count += sec->reloc_count;
5416 if (sec->rawsize > max_contents_size)
5417 max_contents_size = sec->rawsize;
5418 if (sec->size > max_contents_size)
5419 max_contents_size = sec->size;
5420 if (sec->lineno_count > max_lineno_count)
5421 max_lineno_count = sec->lineno_count;
5422 if (coff_section_data (sec->owner, sec) != NULL
5423 && xcoff_section_data (sec->owner, sec) != NULL
5424 && (xcoff_section_data (sec->owner, sec)->lineno_count
5425 > max_lineno_count))
5427 xcoff_section_data (sec->owner, sec)->lineno_count;
5428 if (sec->reloc_count > max_reloc_count)
5429 max_reloc_count = sec->reloc_count;
5431 else if (p->type == bfd_section_reloc_link_order
5432 || p->type == bfd_symbol_reloc_link_order)
5437 /* Compute the file positions for all the sections. */
5438 if (abfd->output_has_begun)
5440 if (xcoff_hash_table (info)->file_align != 0)
5447 file_align = xcoff_hash_table (info)->file_align;
5448 if (file_align != 0)
5450 bfd_boolean saw_contents;
5454 /* Insert .pad sections before every section which has
5455 contents and is loaded, if it is preceded by some other
5456 section which has contents and is loaded. */
5457 saw_contents = TRUE;
5458 for (o = abfd->sections; o != NULL; o = o->next)
5460 if (strcmp (o->name, ".pad") == 0)
5461 saw_contents = FALSE;
5462 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5463 && (o->flags & SEC_LOAD) != 0)
5466 saw_contents = TRUE;
5471 /* Create a pad section and place it before the section
5472 that needs padding. This requires unlinking and
5473 relinking the bfd's section list. */
5475 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5477 n->alignment_power = 0;
5479 bfd_section_list_remove (abfd, n);
5480 bfd_section_list_insert_before (abfd, o, n);
5481 saw_contents = FALSE;
5486 /* Reset the section indices after inserting the new
5489 for (o = abfd->sections; o != NULL; o = o->next)
5492 o->target_index = indx;
5494 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5496 /* Work out appropriate sizes for the .pad sections to force
5497 each section to land on a page boundary. This bit of
5498 code knows what compute_section_file_positions is going
5500 sofar = bfd_coff_filhsz (abfd);
5501 sofar += bfd_coff_aoutsz (abfd);
5502 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5503 for (o = abfd->sections; o != NULL; o = o->next)
5504 if ((bfd_xcoff_is_reloc_count_overflow
5505 (abfd, (bfd_vma) o->reloc_count))
5506 || (bfd_xcoff_is_lineno_count_overflow
5507 (abfd, (bfd_vma) o->lineno_count)))
5508 /* 64 does not overflow, need to check if 32 does */
5509 sofar += bfd_coff_scnhsz (abfd);
5511 for (o = abfd->sections; o != NULL; o = o->next)
5513 if (strcmp (o->name, ".pad") == 0)
5517 BFD_ASSERT (o->size == 0);
5518 pageoff = sofar & (file_align - 1);
5521 o->size = file_align - pageoff;
5522 sofar += file_align - pageoff;
5523 o->flags |= SEC_HAS_CONTENTS;
5528 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5529 sofar += BFD_ALIGN (o->size,
5530 1 << o->alignment_power);
5535 if (! bfd_coff_compute_section_file_positions (abfd))
5539 /* Allocate space for the pointers we need to keep for the relocs. */
5543 /* We use section_count + 1, rather than section_count, because
5544 the target_index fields are 1 based. */
5545 amt = abfd->section_count + 1;
5546 amt *= sizeof (struct xcoff_link_section_info);
5547 finfo.section_info = bfd_malloc (amt);
5548 if (finfo.section_info == NULL)
5550 for (i = 0; i <= abfd->section_count; i++)
5552 finfo.section_info[i].relocs = NULL;
5553 finfo.section_info[i].rel_hashes = NULL;
5554 finfo.section_info[i].toc_rel_hashes = NULL;
5558 /* Set the file positions for the relocs. */
5559 rel_filepos = obj_relocbase (abfd);
5560 relsz = bfd_coff_relsz (abfd);
5561 max_output_reloc_count = 0;
5562 for (o = abfd->sections; o != NULL; o = o->next)
5564 if (o->reloc_count == 0)
5568 /* A stripped file has no relocs. However, we still
5569 allocate the buffers, so that later code doesn't have to
5570 worry about whether we are stripping or not. */
5571 if (info->strip == strip_all)
5575 o->flags |= SEC_RELOC;
5576 o->rel_filepos = rel_filepos;
5577 rel_filepos += o->reloc_count * relsz;
5580 /* We don't know the indices of global symbols until we have
5581 written out all the local symbols. For each section in
5582 the output file, we keep an array of pointers to hash
5583 table entries. Each entry in the array corresponds to a
5584 reloc. When we find a reloc against a global symbol, we
5585 set the corresponding entry in this array so that we can
5586 fix up the symbol index after we have written out all the
5589 Because of this problem, we also keep the relocs in
5590 memory until the end of the link. This wastes memory.
5591 We could backpatch the file later, I suppose, although it
5593 amt = o->reloc_count;
5594 amt *= sizeof (struct internal_reloc);
5595 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5597 amt = o->reloc_count;
5598 amt *= sizeof (struct xcoff_link_hash_entry *);
5599 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5601 if (finfo.section_info[o->target_index].relocs == NULL
5602 || finfo.section_info[o->target_index].rel_hashes == NULL)
5605 if (o->reloc_count > max_output_reloc_count)
5606 max_output_reloc_count = o->reloc_count;
5610 /* We now know the size of the relocs, so we can determine the file
5611 positions of the line numbers. */
5612 line_filepos = rel_filepos;
5613 finfo.line_filepos = line_filepos;
5614 linesz = bfd_coff_linesz (abfd);
5615 for (o = abfd->sections; o != NULL; o = o->next)
5617 if (o->lineno_count == 0)
5618 o->line_filepos = 0;
5621 o->line_filepos = line_filepos;
5622 line_filepos += o->lineno_count * linesz;
5625 /* Reset the reloc and lineno counts, so that we can use them to
5626 count the number of entries we have output so far. */
5628 o->lineno_count = 0;
5631 obj_sym_filepos (abfd) = line_filepos;
5633 /* Figure out the largest number of symbols in an input BFD. Take
5634 the opportunity to clear the output_has_begun fields of all the
5635 input BFD's. We want at least 6 symbols, since that is the
5636 number which xcoff_write_global_symbol may need. */
5638 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5642 sub->output_has_begun = FALSE;
5643 sz = obj_raw_syment_count (sub);
5644 if (sz > max_sym_count)
5648 /* Allocate some buffers used while linking. */
5649 amt = max_sym_count * sizeof (struct internal_syment);
5650 finfo.internal_syms = bfd_malloc (amt);
5652 amt = max_sym_count * sizeof (long);
5653 finfo.sym_indices = bfd_malloc (amt);
5655 amt = (max_sym_count + 1) * symesz;
5656 finfo.outsyms = bfd_malloc (amt);
5658 amt = max_lineno_count * bfd_coff_linesz (abfd);
5659 finfo.linenos = bfd_malloc (amt);
5661 amt = max_contents_size;
5662 finfo.contents = bfd_malloc (amt);
5664 amt = max_reloc_count * relsz;
5665 finfo.external_relocs = bfd_malloc (amt);
5667 if ((finfo.internal_syms == NULL && max_sym_count > 0)
5668 || (finfo.sym_indices == NULL && max_sym_count > 0)
5669 || finfo.outsyms == NULL
5670 || (finfo.linenos == NULL && max_lineno_count > 0)
5671 || (finfo.contents == NULL && max_contents_size > 0)
5672 || (finfo.external_relocs == NULL && max_reloc_count > 0))
5675 obj_raw_syment_count (abfd) = 0;
5676 xcoff_data (abfd)->toc = (bfd_vma) -1;
5678 /* We now know the position of everything in the file, except that
5679 we don't know the size of the symbol table and therefore we don't
5680 know where the string table starts. We just build the string
5681 table in memory as we go along. We process all the relocations
5682 for a single input file at once. */
5683 for (o = abfd->sections; o != NULL; o = o->next)
5685 for (p = o->map_head.link_order; p != NULL; p = p->next)
5687 if (p->type == bfd_indirect_link_order
5688 && p->u.indirect.section->owner->xvec == abfd->xvec)
5690 sub = p->u.indirect.section->owner;
5691 if (! sub->output_has_begun)
5693 if (! xcoff_link_input_bfd (&finfo, sub))
5695 sub->output_has_begun = TRUE;
5698 else if (p->type == bfd_section_reloc_link_order
5699 || p->type == bfd_symbol_reloc_link_order)
5701 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
5706 if (! _bfd_default_link_order (abfd, info, o, p))
5712 /* Free up the buffers used by xcoff_link_input_bfd. */
5713 if (finfo.internal_syms != NULL)
5715 free (finfo.internal_syms);
5716 finfo.internal_syms = NULL;
5718 if (finfo.sym_indices != NULL)
5720 free (finfo.sym_indices);
5721 finfo.sym_indices = NULL;
5723 if (finfo.linenos != NULL)
5725 free (finfo.linenos);
5726 finfo.linenos = NULL;
5728 if (finfo.contents != NULL)
5730 free (finfo.contents);
5731 finfo.contents = NULL;
5733 if (finfo.external_relocs != NULL)
5735 free (finfo.external_relocs);
5736 finfo.external_relocs = NULL;
5739 /* The value of the last C_FILE symbol is supposed to be -1. Write
5741 if (finfo.last_file_index != -1)
5743 finfo.last_file.n_value = -(bfd_vma) 1;
5744 bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
5745 (void *) finfo.outsyms);
5746 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
5747 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5748 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
5752 /* Write out all the global symbols which do not come from XCOFF
5754 xcoff_link_hash_traverse (xcoff_hash_table (info),
5755 xcoff_write_global_symbol,
5758 if (finfo.outsyms != NULL)
5760 free (finfo.outsyms);
5761 finfo.outsyms = NULL;
5764 /* Now that we have written out all the global symbols, we know the
5765 symbol indices to use for relocs against them, and we can finally
5766 write out the relocs. */
5767 amt = max_output_reloc_count * relsz;
5768 external_relocs = bfd_malloc (amt);
5769 if (external_relocs == NULL && max_output_reloc_count != 0)
5772 for (o = abfd->sections; o != NULL; o = o->next)
5774 struct internal_reloc *irel;
5775 struct internal_reloc *irelend;
5776 struct xcoff_link_hash_entry **rel_hash;
5777 struct xcoff_toc_rel_hash *toc_rel_hash;
5779 bfd_size_type rel_size;
5781 /* A stripped file has no relocs. */
5782 if (info->strip == strip_all)
5788 if (o->reloc_count == 0)
5791 irel = finfo.section_info[o->target_index].relocs;
5792 irelend = irel + o->reloc_count;
5793 rel_hash = finfo.section_info[o->target_index].rel_hashes;
5794 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5796 if (*rel_hash != NULL)
5798 if ((*rel_hash)->indx < 0)
5800 if (! ((*info->callbacks->unattached_reloc)
5801 (info, (*rel_hash)->root.root.string,
5802 NULL, o, irel->r_vaddr)))
5804 (*rel_hash)->indx = 0;
5806 irel->r_symndx = (*rel_hash)->indx;
5810 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
5811 toc_rel_hash != NULL;
5812 toc_rel_hash = toc_rel_hash->next)
5814 if (toc_rel_hash->h->u.toc_indx < 0)
5816 if (! ((*info->callbacks->unattached_reloc)
5817 (info, toc_rel_hash->h->root.root.string,
5818 NULL, o, toc_rel_hash->rel->r_vaddr)))
5820 toc_rel_hash->h->u.toc_indx = 0;
5822 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
5825 /* XCOFF requires that the relocs be sorted by address. We tend
5826 to produce them in the order in which their containing csects
5827 appear in the symbol table, which is not necessarily by
5828 address. So we sort them here. There may be a better way to
5830 qsort ((void *) finfo.section_info[o->target_index].relocs,
5831 o->reloc_count, sizeof (struct internal_reloc),
5834 irel = finfo.section_info[o->target_index].relocs;
5835 irelend = irel + o->reloc_count;
5836 erel = external_relocs;
5837 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5838 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
5840 rel_size = relsz * o->reloc_count;
5841 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
5842 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
5846 if (external_relocs != NULL)
5848 free (external_relocs);
5849 external_relocs = NULL;
5852 /* Free up the section information. */
5853 if (finfo.section_info != NULL)
5857 for (i = 0; i < abfd->section_count; i++)
5859 if (finfo.section_info[i].relocs != NULL)
5860 free (finfo.section_info[i].relocs);
5861 if (finfo.section_info[i].rel_hashes != NULL)
5862 free (finfo.section_info[i].rel_hashes);
5864 free (finfo.section_info);
5865 finfo.section_info = NULL;
5868 /* Write out the loader section contents. */
5869 BFD_ASSERT ((bfd_byte *) finfo.ldrel
5870 == (xcoff_hash_table (info)->loader_section->contents
5871 + xcoff_hash_table (info)->ldhdr.l_impoff));
5872 o = xcoff_hash_table (info)->loader_section;
5873 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
5874 (file_ptr) o->output_offset, o->size))
5877 /* Write out the magic sections. */
5878 o = xcoff_hash_table (info)->linkage_section;
5880 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5881 (file_ptr) o->output_offset,
5884 o = xcoff_hash_table (info)->toc_section;
5886 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5887 (file_ptr) o->output_offset,
5890 o = xcoff_hash_table (info)->descriptor_section;
5892 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5893 (file_ptr) o->output_offset,
5897 /* Write out the string table. */
5898 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
5899 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5902 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
5904 amt = STRING_SIZE_SIZE;
5905 if (bfd_bwrite (strbuf, amt, abfd) != amt)
5907 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
5910 _bfd_stringtab_free (finfo.strtab);
5912 /* Write out the debugging string table. */
5913 o = xcoff_hash_table (info)->debug_section;
5916 struct bfd_strtab_hash *debug_strtab;
5918 debug_strtab = xcoff_hash_table (info)->debug_strtab;
5919 BFD_ASSERT (o->output_section->size - o->output_offset
5920 >= _bfd_stringtab_size (debug_strtab));
5921 pos = o->output_section->filepos + o->output_offset;
5922 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5924 if (! _bfd_stringtab_emit (abfd, debug_strtab))
5928 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
5929 not try to write out the symbols. */
5930 bfd_get_symcount (abfd) = 0;
5935 if (finfo.strtab != NULL)
5936 _bfd_stringtab_free (finfo.strtab);
5938 if (finfo.section_info != NULL)
5942 for (i = 0; i < abfd->section_count; i++)
5944 if (finfo.section_info[i].relocs != NULL)
5945 free (finfo.section_info[i].relocs);
5946 if (finfo.section_info[i].rel_hashes != NULL)
5947 free (finfo.section_info[i].rel_hashes);
5949 free (finfo.section_info);
5952 if (finfo.internal_syms != NULL)
5953 free (finfo.internal_syms);
5954 if (finfo.sym_indices != NULL)
5955 free (finfo.sym_indices);
5956 if (finfo.outsyms != NULL)
5957 free (finfo.outsyms);
5958 if (finfo.linenos != NULL)
5959 free (finfo.linenos);
5960 if (finfo.contents != NULL)
5961 free (finfo.contents);
5962 if (finfo.external_relocs != NULL)
5963 free (finfo.external_relocs);
5964 if (external_relocs != NULL)
5965 free (external_relocs);